有人能解释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
相比(较低。
内核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.
*/