bobo Lee bobo Lee - 3 years ago 116
Linux Question

Can rcu_assign_pointer be used between rcu_read_lock and rcu_read_unlock?

At begin, I have one cpu core to be a writer to write share data and one core to be reader to read share data.

But now, I need reader to write back some data to share data.

I know that rcu_read_lock and rcu_read_unlock used for reader to get shared data.But I'm not sure reader write back to share data will cause any problem?

In reader:

rcu_read_lock();
get share data
modify the data

rcu_assign_pointer("the share data will be write back")
rcu_read_unlock();


Is this code work?

Answer Source

A fairly fundamental property of RCU is that coordination between multiple writers is handled via another mechanism, e.g. traditional locks. RCU will guarantee that readers see a consistent view of the data, and that no pointers they are using get freed before they are done, but it does not sequence multiple updates from different threads happening simultaneously.

RCU does guarantee one can upgrade from reader to writer status. This might be done by acquiring a spin lock when it is realized an update is required. One must exit the rcu_read_lock/rcu_read_unlock block before calling synchronize_rcu, but call_rcu may queue a callback inside the block.

One is not allowed to block inside rcu_read_lock/rcu_read_unlock blocks, so managing writer mutual exclusion inside such using blocking mutexes will not work.

See kernel.org RCU read-to-write upgrade

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download