A common pattern in RCU code is to assign a new value to an RCU
pointer after having read and stored the old value. Introduce a
macro for this pattern.

Signed-off-by: Bart Van Assche <bart.vanass...@wdc.com>
Cc: Paul E. McKenney <paul...@linux.vnet.ibm.com>
Cc: Ingo Molnar <mi...@kernel.org>
Cc: Christoph Hellwig <h...@lst.de>
Cc: Hannes Reinecke <h...@suse.de>
Cc: Johannes Thumshirn <jthumsh...@suse.de>
Cc: Shane M Seymour <shane.seym...@hpe.com>
---
 include/linux/rcupdate.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
index f816fc72b51e..555815ce2e57 100644
--- a/include/linux/rcupdate.h
+++ b/include/linux/rcupdate.h
@@ -561,6 +561,26 @@ static inline void rcu_preempt_sleep_check(void) { }
  */
 #define rcu_pointer_handoff(p) (p)
 
+/**
+ * rcu_swap_protected() - swap an RCU and a regular pointer
+ * @rcu_ptr: RCU pointer
+ * @ptr: regular pointer
+ * @c: the conditions under which the dereference will take place
+ *
+ * Perform swap(@rcu_ptr, @ptr) where @rcu_ptr is an RCU-annotated pointer and
+ * @c is the argument that is passed to the rcu_dereference_protected() call
+ * used to read that pointer.
+ */
+#define rcu_swap_protected(rcu_ptr, ptr, c) do {                       \
+       typeof(ptr) __tmp;                                              \
+                                                                       \
+       BUILD_BUG_ON(!__same_type(typeof(rcu_ptr), typeof(*(ptr)) __rcu *)); \
+       BUILD_BUG_ON(!__same_type(typeof(ptr), typeof(*(rcu_ptr)) *));  \
+       __tmp = rcu_dereference_protected((rcu_ptr), (c));              \
+       rcu_assign_pointer((rcu_ptr), (ptr));                           \
+       (ptr) = __tmp;                                                  \
+} while (0)
+
 /**
  * rcu_read_lock() - mark the beginning of an RCU read-side critical section
  *
-- 
2.14.1

Reply via email to