gpt4 book ai didi

linux - rcu_dereference() vs rcu_dereference_protected()?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:27:00 25 4
gpt4 key购买 nike

谁能解释一下 rcu_dereference()rcu_dereference_protected() 之间的区别?

rcu_dereference() 包含屏障代码,rcu_dereference_protected() 不包含。

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

最佳答案

简而言之:

  • rcu_dereference() 应该用在读端,受rcu_read_lock() 或类似的保护。
  • rcu_dereference_protected() 应该由单个 作者在写入端(更新端)使用,或受锁保护,该锁可防止多个编写者并发修改取消引用的指针。在这种情况下,不能在当前线程的外部修改指针,因此既不需要编译器屏障也不需要 cpu 屏障。

如果有疑问,使用 rcu_dereference 总是安全的,并且它的性能损失(与 rcu_dereference_protected 相比)很低。

精确description对于内核 4.6 中的 rcu_dereference_protected:

/**
* 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.
*/

关于linux - rcu_dereference() vs rcu_dereference_protected()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39251287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com