clinuxlinux-kernelrcu

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 shared data and one core to be reader to read shared data.
I need reader to write back some data to share data.
I know that rcu_read_lock()/rcu_read_unlock() are 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 shared data
 //modify the data 
 rcu_assign_pointer(ptr1, ptr2)
 rcu_read_unlock();

Is this code valid?


Solution

  • 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