Skip to contentSkip to conversation

target: use RCU_INIT_POINTER() when NULLing.

PATCH In review Muhammad Falak R Wani 3 participants 10 messages May 1, 2016 – May 3, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 2/2 target: use RCU_INIT_POINTER() when NULLing.

It is safe to use RCU_INIT_POINTER() to NULL, instead of rcu_assign_pointer().
This results in slightly smaller/faster code.

---
 drivers/target/target_core_tpg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/target/target_core_tpg.c +1 -1
index ddf0460..7ae0f08 100644
@@ -684,7 +684,7 @@ void core_tpg_remove_lun(
 		spin_lock(&dev->se_port_lock);
 		list_del(&lun->lun_dev_link);
 		dev->export_count--;
-		rcu_assign_pointer(lun->lun_se_dev, NULL);
+		RCU_INIT_POINTER(lun->lun_se_dev, NULL);
 		spin_unlock(&dev->se_port_lock);
 	}
 	if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/2 target: use RCU_INIT_POINTER() when NULLing.

It is safe to use RCU_INIT_POINTER() to NULL, instead of rcu_assign_pointer().
This results in slightly smaller/faster code.

The follwoing semantic patch was used:
<smpl>
@@
@@
- rcu_assign_pointer
+ RCU_INIT_POINTER
(..., NULL)

</smpl>

---
 drivers/target/target_core_device.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/target/target_core_device.c +2 -2
index a4046ca..6f81e3b 100644
@@ -432,8 +432,8 @@ void core_disable_device_list_for_node(
 	kref_put(&orig->pr_kref, target_pr_kref_release);
 	wait_for_completion(&orig->pr_comp);
 
-	rcu_assign_pointer(orig->se_lun, NULL);
-	rcu_assign_pointer(orig->se_lun_acl, NULL);
+	RCU_INIT_POINTER(orig->se_lun, NULL);
+	RCU_INIT_POINTER(orig->se_lun_acl, NULL);
 
 	kfree_rcu(orig, rcu_head);
 
Signature · 1 line
1.9.1
#2 lore raw
In reply to Muhammad Falak R Wani
Quoted 3 lines · Muhammad Falak R Wani

It is safe to use RCU_INIT_POINTER() to NULL, instead of rcu_assign_pointer().
This results in slightly smaller/faster code.

If this is indeed the case, rcu_assign_pointer should simply check for NULL using __builtin_constant_p and do the right thing transparently instead of burdening it on every user.

#3 lore raw
Quoted 8 lines · Christoph Hellwig
Quoted 3 lines · Muhammad Falak R Wani

It is safe to use RCU_INIT_POINTER() to NULL, instead of rcu_assign_pointer().
This results in slightly smaller/faster code.

If this is indeed the case, rcu_assign_pointer should simply check for NULL using __builtin_constant_p and do the right thing transparently instead of burdening it on every user.

Last time around, there was a compiler bug that prevented this from working correctly. But it could well be time to look at it again. How does the following (untested) patch look?

Thanx, Paul

------------------------------------------------------------------------

include/linux/rcupdate.h +10 -1
index c61b6b9506e7..3a4dbfe63c1a 100644
@@ -650,7 +650,16 @@ static inline void rcu_preempt_sleep_check(void)
  * please be careful when making changes to rcu_assign_pointer() and the
  * other macros that it invokes.
  */
-#define rcu_assign_pointer(p, v) smp_store_release(&p, RCU_INITIALIZER(v))
+#define rcu_assign_pointer(p, v) \
+({ \
+	typeof(v) _r_a_p__v = (v); \
+	\
+	if (__builtin_constant_p(v) && (_r_a_p__v) == NULL) \
+		WRITE_ONCE((p), (_r_a_p__v)); \
+	else \
+		smp_store_release(&p, RCU_INITIALIZER(_r_a_p__v)); \
+	_r_a_p__v; \
+})
 
 /**
  * rcu_access_pointer() - fetch RCU pointer with no dereferencing
#4 lore raw
Quoted 6 lines · Paul E. McKenney
Quoted 5 lines · Christoph Hellwig
Quoted 3 lines · Muhammad Falak R Wani

It is safe to use RCU_INIT_POINTER() to NULL, instead of rcu_assign_pointer().
This results in slightly smaller/faster code.

Quoted 7 lines
Quoted 3 lines

If this is indeed the case, rcu_assign_pointer should simply check for NULL using __builtin_constant_p and do the right thing transparently instead of burdening it on every user.

Last time around, there was a compiler bug that prevented this from working correctly. But it could well be time to look at it again. How does the following (untested) patch look?

Pretty bad, actually...

People use rcu_assign_pointer() for pointers to functions, which gets interesting compiler warnings for some configurations. Please see below for attempt #2.

Thanx, Paul

------------------------------------------------------------------------

commit f55c2ffb4e105fa0450fe495788765dffc4b752e

Author: Paul E. McKenney <[email protected]>
Date:   Sun May 1 18:46:54 2016 -0700

rcu: No ordering for rcu_assign_pointer() of NULL

This commit does a compile-time check for rcu_assign_pointer() of NULL, and uses WRITE_ONCE() rather than smp_store_release() in that case.

include/linux/rcupdate.h +10 -1
index c61b6b9506e7..9b8828c5a9c2 100644
@@ -650,7 +650,16 @@ static inline void rcu_preempt_sleep_check(void)
  * please be careful when making changes to rcu_assign_pointer() and the
  * other macros that it invokes.
  */
-#define rcu_assign_pointer(p, v) smp_store_release(&p, RCU_INITIALIZER(v))
+#define rcu_assign_pointer(p, v) \
+({ \
+	uintptr_t _r_a_p__v = (uintptr_t)(v); \
+	\
+	if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL) \
+		WRITE_ONCE((p), (typeof(v))(_r_a_p__v)); \
+	else \
+		smp_store_release(&p, RCU_INITIALIZER((typeof(v))_r_a_p__v)); \
+	_r_a_p__v; \
+})
 
 /**
  * rcu_access_pointer() - fetch RCU pointer with no dereferencing
#5 lore raw
Quoted 15 lines · Paul E. McKenney
Quoted 6 lines · Paul E. McKenney
Quoted 5 lines · Christoph Hellwig
Quoted 3 lines · Muhammad Falak R Wani

It is safe to use RCU_INIT_POINTER() to NULL, instead of rcu_assign_pointer().
This results in slightly smaller/faster code.

Quoted 7 lines
Quoted 3 lines

If this is indeed the case, rcu_assign_pointer should simply check for NULL using __builtin_constant_p and do the right thing transparently instead of burdening it on every user.

Last time around, there was a compiler bug that prevented this from working correctly. But it could well be time to look at it again. How does the following (untested) patch look?

Quoted 5 lines

Pretty bad, actually...

People use rcu_assign_pointer() for pointers to functions, which gets interesting compiler warnings for some configurations. Please see below for attempt #2.

Perhaps third time is the charm?

Thanx, Paul

------------------------------------------------------------------------

commit 72a616bf7f99b2ef4f211f73c6def7fa884d6ca4

Author: Paul E. McKenney <[email protected]>
Date:   Sun May 1 18:46:54 2016 -0700

rcu: No ordering for rcu_assign_pointer() of NULL

This commit does a compile-time check for rcu_assign_pointer() of NULL, and uses WRITE_ONCE() rather than smp_store_release() in that case.

include/linux/rcupdate.h +10 -1
index c61b6b9506e7..9be61e47badc 100644
@@ -650,7 +650,16 @@ static inline void rcu_preempt_sleep_check(void)
  * please be careful when making changes to rcu_assign_pointer() and the
  * other macros that it invokes.
  */
-#define rcu_assign_pointer(p, v) smp_store_release(&p, RCU_INITIALIZER(v))
+#define rcu_assign_pointer(p, v) \
+({ \
+	uintptr_t _r_a_p__v = (uintptr_t)(v); \
+	\
+	if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL) \
+		WRITE_ONCE((p), (typeof(p))(_r_a_p__v)); \
+	else \
+		smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \
+	_r_a_p__v; \
+})
 
 /**
  * rcu_access_pointer() - fetch RCU pointer with no dereferencing
#6 lore raw

include/linux/rcupdate.h static inline void rcu_preempt_sleep_check(void) · line 662

Quoted 11 lines · Paul E. McKenney
include/linux/rcupdate.h +10 -1
-#define rcu_assign_pointer(p, v) smp_store_release(&p, RCU_INITIALIZER(v))
+#define rcu_assign_pointer(p, v) \
+({ \
+	uintptr_t _r_a_p__v = (uintptr_t)(v); \
+	\
+	if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL) \
+		WRITE_ONCE((p), (typeof(p))(_r_a_p__v)); \
+	else \
+		smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \
+	_r_a_p__v; \
+})

Can't we turn it into an inline (would need different calling conventions for p, though).

#7 lore raw
Quoted 15 lines · Christoph Hellwig

include/linux/rcupdate.h static inline void rcu_preempt_sleep_check(void) · line 662

Quoted 11 lines · Paul E. McKenney
include/linux/rcupdate.h +10 -1
-#define rcu_assign_pointer(p, v) smp_store_release(&p, RCU_INITIALIZER(v))
+#define rcu_assign_pointer(p, v) \
+({ \
+	uintptr_t _r_a_p__v = (uintptr_t)(v); \
+	\
+	if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL) \
+		WRITE_ONCE((p), (typeof(p))(_r_a_p__v)); \
+	else \
+		smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \
+	_r_a_p__v; \
+})

Can't we turn it into an inline (would need different calling conventions for p, though).

And for v. But how do I do that without C++ templates?

Also, does __builtin_constant_p() work reliably on a parameter? Especially when the compiler decides not to do the inlining?

Thanx, Paul

#8 lore raw
Quoted 4 lines · Paul E. McKenney

And for v. But how do I do that without C++ templates?

Also, does __builtin_constant_p() work reliably on a parameter? Especially when the compiler decides not to do the inlining?

Yeah, it's going to be a pain indeed, guess that's why it hasn't been done before..

#9 lore raw
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Paul E. McKenney

And for v. But how do I do that without C++ templates?

Also, does __builtin_constant_p() work reliably on a parameter? Especially when the compiler decides not to do the inlining?

Yeah, it's going to be a pain indeed, guess that's why it hasn't been done before..

Speaking of pain and things that haven't been done before, would you be interested in being in the first group to review an attempt to formalize memory-barriers.txt?

Thanx, Paul