rcu_dereference()vs rcu_dereference_protected()?

ayy*_* ch 1 linux linux-device-driver linux-kernel rcu

任何人都可以解释rcu_dereference()和之间的区别是rcu_dereference_protected()什么?

rcu_dereference()包含障碍代码,rcu_dereference_protected()不包含.

何时使用rcu_dereference()以及何时使用rcu_dereference_protected()

Tsy*_*rev 5

简而言之:

  • rcu_dereference()应该在阅读方使用,受保护rcu_read_lock()或类似.
  • rcu_dereference_protected()应该由单个写入器在写入侧(更新侧)使用,或者由锁定保护,这会阻止多个写入器同时修改解除引用的指针.在这种情况下,指针不能在当前线程之外进行修改,因此既不需要编译器也不需要cpu-barrier.

如果怀疑,使用rcu_dereference总是安全的,并且其性能惩罚(与之相比rcu_dereference_protected)很低.

精确描述rcu_dereference_protected在内核4.6:

/**
 * rcu_dereference_protected() - fetch RCU pointer when updates prevented
 * @p: The pointer to read, prior to dereferencing
 * @c: The conditions under which the dereference will take place
 *
 * Return the value of the specified RCU-protected pointer, but omit
 * both the smp_read_barrier_depends() and the READ_ONCE().  This
 * is useful in cases where update-side locks prevent the value of the
 * pointer from changing.  Please note that this primitive does -not-
 * prevent the compiler from repeating this reference or combining it
 * with other references, so it should not be used without protection
 * of appropriate locks.
 *
 * This function is only for update-side use.  Using this function
 * when protected only by rcu_read_lock() will result in infrequent
 * but very ugly failures.
 */
Run Code Online (Sandbox Code Playgroud)