RCU - Basics

来源:互联网 发布:怎样建立网络印刷平台 编辑:程序博客网 时间:2024/04/27 13:34

1. Kernel synchronize control


Disable/enable irq, atomic operations, semaphore, rw_semaphore, BKL(Big Kernel Lock), brlock(Only in 2.4 kernel), spinlock, rwlock, seqlock(Only in 2.6 kernel), RCU(Only in 2.6 kernel).
You can get more details from the following links.
http://www.ibm.com/developerworks/cn/linux/l-synch/part1/index.html
http://www.ibm.com/developerworks/cn/linux/l-synch/part2/index.html
http://hi.baidu.com/yyangjjun/blog/item/bd34fa2ef09bfce78b13995e.html
A figure is attached below to show the performance per CPUs of certain kinds of racing control.

 

Fig 1-1

 

Note: Refcnt means using spin lock together with reference count

 

2. RCU (Read-Copy Update)

 

Let's use an example to introduce the basics of RCU. Say thread 1 is reading node B but thread 0 is going to write node A:

 

Fig 1-2

 

So thread 0 copy B to B' and fill in the content of B', and set memory barrier, and then let A points to B'; Then wait until Thread 1 stop visiting B. That is to say, thread 0 waits for a Quiescent State. So after this Quiescent State, B' is pointed to C and B is delete by thread 0. See below:

 

Fig 1-3

 

But anyway, thread 0 has to use some concurrency control ways such as spin lock or atomic operations for updating data unless only one thread is allowed for writing. 


The following definitions help illuminate the concepts underlying RCU.

  • Live Variable:
    A variable that might be accessed before it is next modified, so that its current value has some possibility of influencing future execution state.
  • Dead Variable:
    A variable that will be modified before it is next accessed, so that its current value cannot possibly have any influence over future execution state.
    Critical Section:
    A region of code that is protected from outside interference by some locking mechanism, such as spinlocks or RCU.
  • Read-Side Critical Section:
    A region of code that is protected from other CPUs' modifications, but which allows multiple CPUs to read simultaneously.
  • Temporary Variable:
    A variable that is only live inside a critical section. One example is an auto variable used as a pointer while traversing a linked list.
  • Permanent Variable:
    A variable that is live outside of critical sections. One example would be the header for a linked list. Although it is possible for the same variable to be temporary sometimes and permanent at other times, this practice can lead to confusion, so is not generally recommended. Relying on the register-allocation capabilities of modern optimizing compilers is usually a far better strategy.
  • Quiescent State:
    A point in the code where all of the current CPU's temporary variables that were previously in use in a critical section are dead. In a non-preemptive Linux kernel, a context switch is a quiescent state for CPUs. In a preemptive Linux kernel, rcu_read_lock() and rcu_read_unlock() suppress preemption for short read-side critical sections, so that context switch is still a quiescent state with respect to these read-side critical sections. Although there are implementations of RCU that do not require preemption to be suppressed [Gamsa99,McK02a], they can be prone to excessively long grace periods.
  • Grace Period:
    Time interval during which all CPUs pass through at least one quiescent state. The key property of a grace period is that the values that were contained in any temporary variable in use in a critical section at the beginning of the grace period cannot possibly have any direct effect after the end of the grace period. This property will be examined more closely in the next section. Note that any time interval containing a grace period is itself a grace period.

The following fingure shows the above concepts.

 

 

 Fig 1-4

 

Notes:

1. Black points represent context switching, which is in Observed Quiescent States;

2. A1 means thread A is reading the resource protected by RCU; E2 means thread E is reading the resource pretected by RCU; etc;

3. The real lines represent continuously executing periods of the threads. Each point on these lines represents a Candidate Quiescent State;

 

(To be continue)

 

References:

[1] http://www.ibm.com/developerworks/cn/linux/l-rcu/
[2] http://www.shangshuwu.cn/index.php/Linux%E5%86%85%E6%A0%B8RCU%E5%90%8C%E6%AD%A5%E6%9C%BA%E5%88%B6
[3] http://www.usenix.org/event/usenix03/tech/freenix03/full_papers/arcangeli/arcangeli_html/node4.html 

原创粉丝点击