【Linux 内核技术】RCU

在阅读linux 3.10版本的socket 一节源码时,遇到了一个

rcu_dereference();

函数,于是深入了解了一下这个技术。Documentation 链接:【https://docs.kernel.org/RCU/whatisRCU.html?highlight=rcu_dereference#whatisrcu
具体内容:

  1. RCU OVERVIEW
    The basic idea behind RCU is to split updates into “removal” and “reclamation” phases. The removal phase removes references to data items within a data structure (possibly by replacing them with references to new versions of these data items), and can run concurrently with readers. The reason that it is safe to run the removal phase concurrently with readers is the semantics of modern CPUs guarantee that readers will see either the old or the new version of the data structure rather than a partially updated reference. The reclamation phase does the work of reclaiming (e.g., freeing) the data items removed from the data structure during the removal phase. Because reclaiming data items can disrupt any readers concurrently referencing those data items, the reclamation phase must not start until readers no longer hold references to those data items.
    Splitting the update into removal and reclamation phases permits the updater to perform the removal phase immediately, and to defer the reclamation phase until all readers active during the removal phase have completed, either by blocking until they finish or by registering a callback that is invoked after they finish. Only readers that are active during the removal phase need be considered, because any reader starting after the removal phase will be unable to gain a reference to the removed data items, and therefore cannot be disrupted by the reclamation phase.
    So the typical RCU update sequence goes something like the following:
    a) Remove pointers to a data structure, so that subsequent readers cannot gain a reference to it.
    b) Wait for all previous readers to complete their RCU read-side critical sections.
    At this point, there cannot be any readers who hold references to the data structure, so it now may safely be reclaimed (e.g., kfree()d).
    c) Step (b) above is the key idea underlying RCU’s deferred destruction.
    The ability to wait until all readers are done allows RCU readers to use much lighter-weight synchronization, in some cases, absolutely no synchronization at all. In contrast, in more conventional lock-based schemes, readers must use heavy-weight synchronization in order to prevent an updater from deleting the data structure out from under them. This is because lock-based updaters typically update data items in place, and must therefore exclude readers. In contrast, RCU-based updaters typically take advantage of the fact that writes to single aligned pointers are atomic on modern CPUs, allowing atomic insertion, removal, and replacement of data items in a linked structure without disrupting readers. Concurrent RCU readers can then continue accessing the old versions, and can dispense with the atomic operations, memory barriers, and communications cache misses that are so expensive on present-day SMP computer systems, even in absence of lock contention.
    In the three-step procedure shown above, the updater is performing both the removal and the reclamation step, but it is often helpful for an entirely different thread to do the reclamation, as is in fact the case in the Linux kernel’s directory-entry cache (dcache). Even if the same thread performs both the update step (step (a) above) and the reclamation step (step © above), it is often helpful to think of them separately. For example, RCU readers and updaters need not communicate at all, but RCU provides implicit low-overhead communication between readers and reclaimers, namely, in step (b) above.

RCU(Read-Copy Update)目的是提高遍历读取数据的效率,为了达到目的使用RCU机制读取数据的时候不对链表进行耗时的加锁操作。这样在同一时间可以有多个线程同时读取该链表,并且允许一个线程对链表进行修改(修改的时候,需要加锁)。RCU适用于需要频繁的读取数据,而相应修改数据并不多的情景,例如在文件系统中,经常需要查找定位目录,而对目录的修改相对来说并不多,这就是RCU发挥作用的最佳场景。顾名思义就是“读,拷贝更新”,再直白点是“随意读,但更新数据的时候,需要先复制一份副本,在副本上完成修改,再一次性地替换旧数据”。这是 Linux 内核实现的一种针对“读多写少”的共享数据的同步机制。

RCU既允许多个读者同时访问被保护的数据,又允许多个读者和多个写者同时访问被保护的数据(注意:是否可以有多个写者并行访问取决于写者之间使用的同步机制),读者没有任何同步开销,而写者的同步开销则取决于使用的写者间同步机制。但RCU不能替代rwlock,因为如果写比较多时,对读者的性能提高不能弥补写者导致的损失。

读者在访问被RCU保护的共享数据期间不能被阻塞,这是RCU机制得以实现的一个基本前提,也就说当读者在引用被RCU保护的共享数据期间,读者所在的CPU不能发生上下文切换,spinlock和rwlock都需要这样的前提。写者在访问被RCU保护的共享数据时不需要和读者竞争任何锁,只有在有多于一个写者的情况下需要获得某种锁以与其他写者同步。

写者修改数据前首先拷贝一个被修改元素的副本,然后在副本上进行修改,修改完毕后它向垃圾回收器注册一个回调函数以便在适当的时机执行真正的修改操作。等待适当时机的这一时期称为grace period,而CPU发生了上下文切换称为经历一个quiescent state,grace period就是所有CPU都经历一次quiescent state所需要的等待的时间。垃圾收集器就是在grace period之后调用写者注册的回调函数来完成真正的数据修改或数据释放操作的。

RCU 对于链表的处理很高效。

主要的几个API接口:
rcu_read_lock()
rcu_read_unlock()
创建rcu 读着临界区

synchronize_rcu() / call_rcu()
挂起写者,等待读者都退出后释放老的数据值;

rcu_assign_pointer()
写者使用改函数来为一个被RCU保护的指针分配一个新的值;
rcu_dereference()
读者调用来获得一个被RCU保护的指针

/* `p` 指向一块受 RCU 保护的共享数据 */

/* reader */
rcu_read_lock();
p1 = rcu_dereference(p);
if (p1 != NULL) {
    printk("%d\n", p1->field);
}
rcu_read_unlock();

/* free the memory */
p2 = p;
if (p2 != NULL) {
    p = NULL;
    synchronize_rcu();
    kfree(p2);
}

这里指向了一个临界区,p是被RCU保护的。
在这里插入图片描述
如果多个Reader指向了p,例如Reader1,2,3均获得了读取权限。当t2时刻,执行到p=NULL时,synchronize_rcu()就会阻塞,直到t3,这时候才会释放p2。

下面看一下源码:

#define __rcu_assign_pointer(p, v, space) \
    ({ \
        smp_wmb(); \
        (p) = (typeof(*v) __force space *)(v); \
    })

这里smp_wmb() 就是增加了一个内存屏障 ,保存内存屏障之间的指令一定会先于内存屏障后的指令被执行。

#define __rcu_dereference_check(p, c, space) \
    ({ \
        typeof(*p) *_________p1 = (typeof(*p)*__force )ACCESS_ONCE(p); \
        rcu_lockdep_assert(c, "suspicious rcu_dereference_check()" \
                      " usage"); \
        rcu_dereference_sparse(p, space); \
        smp_read_barrier_depends(); \
        ((typeof(*p) __force __kernel *)(_________p1)); \
    })3 行:声明指针 _p1 = p;7 行:smp_read_barrier_depends();8 行:返回 _p1;
static inline void synchronize_rcu(void)
{
	synchronize_sched();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Linux内核中的RCU(Read-Copy-Update)机制是一种用于实现高效并发访问共享数据的技术。在RCU机制中,读操作不会被阻塞,而写操作则通过延迟更新来避免对读操作的影响。然而,当某些情况下读操作被长时间阻塞时,就会发生RCU stall。下面是Linux内核RCU stall的机制的详细介绍: 1. RCU的快速路径:在正常情况下,RCU的快速路径允许读操作不受写操作的影响,从而实现高效并发访问。当一个线程进行读操作时,它会在读取共享数据之前增加一个引用计数,并在读操作完成后减少引用计数。这样,即使有其他线程正在进行写操作,读操作仍然可以顺利进行。 2. 写操作的延迟更新:当一个线程进行写操作时,它会创建一个新的数据版本,并将更新后的数据写入新版本中。然后,它将原来的数据版本标记为废弃,并等待所有正在进行读操作的线程完成后才会释放废弃版本的内存资源。这种延迟更新的方式可以避免对读操作的影响。 3. RCU stall的触发:RCU stall通常在以下情况下触发: - 长时间的写操作:当一个写操作需要很长时间才能完成时,所有正在进行读操作的线程需要等待写操作完成才能继续进行读操作,这可能导致RCU stall的发生。 - 频繁的写操作:如果写操作频繁地竞争同一资源,如数据结构或共享变量,读操作的等待时间会增加,可能导致RCU stall。 - 更新操作的阻塞:当一个更新操作阻塞了RCU的快速路径,使得其他读操作无法顺利进行时,也会引发RCU stall。 4. RCU stall的处理:当RCU stall发生时,内核会通过一些机制来尝试解决或减轻RCU stall的影响,包括: - RCU回调函数处理机制:当RCU stall发生时,内核会通过回调函数处理机制来延迟执行一些需要被延迟的任务,如内存回收等,以减轻RCU stall的影响。 - RCU GP(Grace Period)的处理:GP是一个时间间隔,在这个时间间隔内,所有已经开始进行读操作的线程都可以继续执行,而不会被阻塞。内核会通过GP来确保读操作的一致性,并在GP结束后释放废弃版本的内存资源。 - RCU stall检测机制:内核会定期检测是否发生了长时间的阻塞,如果检测到长时间的阻塞,会尝试唤醒被阻塞的CPU,以解决RCU stall的问题。 通过以上机制,Linux内核RCU机制可以在大多数情况下实现高效并发访问共享数据。然而,在某些特殊情况下,如长时间的写操作或频繁的写操作竞争,可能会引发RCU stall。内核通过回调函数处理、GP处理和RCU stall检测等机制来处理和减轻RCU stall的影响,以保证系统的性能和响应时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值