Skip to contentSkip to conversation

fib_trie: rcu_assign_pointer warning fix

PATCH David Miller 3 participants 8 messages Feb 12, 2008 – Feb 13, 2008 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

David Miller

author maintainer
#1 lore raw
In reply to a message not in the archive

From: Stephen Hemminger <[email protected]>
Date: Mon, 11 Feb 2008 16:59:54 -0800

linux-kernel added to CC:, any change to generic kernel infrastructure should be posted there

Quoted 38 lines

Eliminate warnings when rcu_assign_pointer is used with unsigned long. It is reasonable to use RCU with non-pointer values so allow it for general use. Add a comment to explain the if test.

---
 include/linux/rcupdate.h |   13 +++++++------
 1 files changed, 7 insertions(+), 6 deletions(-)
include/linux/rcupdate.h +7 -6
index 37a642c..c44ac87 100644
@@ -172,14 +172,15 @@ struct rcu_head {
  * structure after the pointer assignment.  More importantly, this
  * call documents which pointers will be dereferenced by RCU read-side
  * code.
+ *
+ * If value is the NULL (constant 0), then no barrier is needed.
  */
 
-#define rcu_assign_pointer(p, v) \
-	({ \
-		if (!__builtin_constant_p(v) || \
-		    ((v) != NULL)) \
-			smp_wmb(); \
-		(p) = (v); \
+#define rcu_assign_pointer(p, v)			\
+	({						\
+		if (!(__builtin_constant_p(v) && v))	\
+			smp_wmb();			\
+		(p) = (v);				\
 	})
 
 /**
Signature · 1 line
1.5.3.8
#7 lore raw
In reply to a message not in the archive
Quoted 3 lines · Stephen Hemminger

Eliminate warnings when rcu_assign_pointer is used with unsigned long. It is reasonable to use RCU with non-pointer values so allow it for general use. Add a comment to explain the if test.

Good catch!!! (An apologies for the hassle!!!)

Quoted 34 lines
---
 include/linux/rcupdate.h |   13 +++++++------
 1 files changed, 7 insertions(+), 6 deletions(-)
include/linux/rcupdate.h +7 -6
index 37a642c..c44ac87 100644
@@ -172,14 +172,15 @@ struct rcu_head {
  * structure after the pointer assignment.  More importantly, this
  * call documents which pointers will be dereferenced by RCU read-side
  * code.
+ *
+ * If value is the NULL (constant 0), then no barrier is needed.
  */
 
-#define rcu_assign_pointer(p, v) \
-	({ \
-		if (!__builtin_constant_p(v) || \
-		    ((v) != NULL)) \
-			smp_wmb(); \
-		(p) = (v); \
+#define rcu_assign_pointer(p, v)			\
+	({						\
+		if (!(__builtin_constant_p(v) && v))	\
+			smp_wmb();			\
+		(p) = (v);				\
 	})
 
 /**
Signature · 1 line
1.5.3.8
author maintainer
#8 lore raw

From: "Paul E. McKenney" <[email protected]>
Date: Mon, 11 Feb 2008 17:27:41 -0800

Quoted 8 lines · Stephen Hemminger
Quoted 3 lines

Eliminate warnings when rcu_assign_pointer is used with unsigned long. It is reasonable to use RCU with non-pointer values so allow it for general use. Add a comment to explain the if test.

Good catch!!! (An apologies for the hassle!!!)

Thanks for reviewing Paul, I'll apply this.

#2 lore raw
In reply to David Miller
Quoted 27 lines · David Miller

From: Stephen Hemminger <[email protected]>
Date: Mon, 11 Feb 2008 16:59:54 -0800

linux-kernel added to CC:, any change to generic kernel infrastructure should be posted there

Quoted 21 lines

Eliminate warnings when rcu_assign_pointer is used with unsigned long. It is reasonable to use RCU with non-pointer values so allow it for general use. Add a comment to explain the if test.

---
 include/linux/rcupdate.h |   13 +++++++------
 1 files changed, 7 insertions(+), 6 deletions(-)
include/linux/rcupdate.h +2 -0
index 37a642c..c44ac87 100644
@@ -172,14 +172,15 @@ struct rcu_head {
  * structure after the pointer assignment.  More importantly, this
  * call documents which pointers will be dereferenced by RCU read-side
  * code.
+ *
+ * If value is the NULL (constant 0), then no barrier is needed.
  */
 
Quoted 9 lines

include/linux/rcupdate.h struct rcu_head { · line 181

Quoted 9 lines
include/linux/rcupdate.h +3 -6
-#define rcu_assign_pointer(p, v) \
-	({ \
-		if (!__builtin_constant_p(v) || \
-		    ((v) != NULL)) \
-			smp_wmb(); \
-		(p) = (v); \
+#define rcu_assign_pointer(p, v)			\
+	({						\
+		if (!(__builtin_constant_p(v) && v))	\

...But, "If value is the NULL (constant 0)" we have:

if (!(1 && NULL != 0)) ==> if (!(0)) and the barrier is used?!

Quoted 5 lines

include/linux/rcupdate.h struct rcu_head { · line 186

Quoted 5 lines
include/linux/rcupdate.h +2 -0
+			smp_wmb();			\
+		(p) = (v);				\
 	})
 
 /**

Regards,
Jarek P.

#3 lore raw
Quoted 40 lines · Jarek Poplawski
Quoted 27 lines · David Miller

From: Stephen Hemminger <[email protected]>
Date: Mon, 11 Feb 2008 16:59:54 -0800

linux-kernel added to CC:, any change to generic kernel infrastructure should be posted there

Quoted 21 lines

Eliminate warnings when rcu_assign_pointer is used with unsigned long. It is reasonable to use RCU with non-pointer values so allow it for general use. Add a comment to explain the if test.

---
 include/linux/rcupdate.h |   13 +++++++------
 1 files changed, 7 insertions(+), 6 deletions(-)
include/linux/rcupdate.h +2 -0
index 37a642c..c44ac87 100644
@@ -172,14 +172,15 @@ struct rcu_head {
  * structure after the pointer assignment.  More importantly, this
  * call documents which pointers will be dereferenced by RCU read-side
  * code.
+ *
+ * If value is the NULL (constant 0), then no barrier is needed.
  */
 
Quoted 9 lines

include/linux/rcupdate.h struct rcu_head { · line 181

Quoted 9 lines
include/linux/rcupdate.h +3 -6
-#define rcu_assign_pointer(p, v) \
-	({ \
-		if (!__builtin_constant_p(v) || \
-		    ((v) != NULL)) \
-			smp_wmb(); \
-		(p) = (v); \
+#define rcu_assign_pointer(p, v)			\
+	({						\
+		if (!(__builtin_constant_p(v) && v))	\

...But, "If value is the NULL (constant 0)" we have:

Quoted 1 line

if (!(1 && NULL != 0)) ==> if (!(0)) and the barrier is used?!

"All programmers are blind, especially me."

You are right, Jarek. I ran this through gcc, and the following comes close:

#define rcu_assign_pointer(p, v) \
	({ \
		if (!__builtin_constant_p(v) || (v)) \
			smp_wmb(); \
		(p) = (v); \
	})

But I am concerned about the following case:

	rcu_assign_pointer(global_index, 0);

	. . .

	x = global_array[rcu_dereference(global_index)];

Since arrays have a zero-th element, we would really want a memory barrier in this case.

So how about leaving the index-unfriendly version of rcu_assign_pointer() and adding an rcu_assign_index() as follows?

Thanx, Paul

---

 rcupdate.h |   18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
include/linux/rcupdate.h +18 -0
diff -urpNa -X dontdiff linux-2.6.24/include/linux/rcupdate.h linux-2.6.24-rai/include/linux/rcupdate.h
@@ -278,6 +278,24 @@ extern struct lockdep_map rcu_lock_map;
 					})
 
 /**
+ * rcu_assign_index - assign (publicize) a index of a newly
+ * initialized array elementg that will be dereferenced by RCU
+ * read-side critical sections.  Returns the value assigned.
+ *
+ * Inserts memory barriers on architectures that require them
+ * (pretty much all of them other than x86), and also prevents
+ * the compiler from reordering the code that initializes the
+ * structure after the index assignment.  More importantly, this
+ * call documents which indexes will be dereferenced by RCU read-side
+ * code.
+ */
+
+#define rcu_assign_index(p, v)	({ \
+					smp_wmb(); \
+					(p) = (v); \
+				})
+
+/**
  * synchronize_sched - block until all CPUs have exited any non-preemptive
  * kernel code sequences.
  *
#4 lore raw

On Tue, Feb 12, 2008 at 08:07:29AM -0800, Paul E. McKenney wrote:
...

Quoted 1 line

"All programmers are blind, especially me."

Hmm... I got it my way: you - superheroes - sometimes seem to be just like us - common people... (Probably early in the morning, before dressing your funny costumes?)

Quoted 12 lines

You are right, Jarek. I ran this through gcc, and the following comes close:

#define rcu_assign_pointer(p, v) \
	({ \
		if (!__builtin_constant_p(v) || (v)) \
			smp_wmb(); \
		(p) = (v); \
	})

But I am concerned about the following case:

Quoted 8 lines
	rcu_assign_pointer(global_index, 0);

	. . .

	x = global_array[rcu_dereference(global_index)];

Since arrays have a zero-th element, we would really want a memory barrier in this case.

It seems the above version of this macro uses the barrier for 0, but if I miss something, or for these other: documenting reasons, then of course you are right.

Quoted 16 lines

So how about leaving the index-unfriendly version of rcu_assign_pointer() and adding an rcu_assign_index() as follows?

Thanx, Paul

---

 rcupdate.h |   18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff -urpNa -X dontdiff linux-2.6.24/include/linux/rcupdate.h linux-2.6.24-rai/include/linux/rcupdate.h

include/linux/rcupdate.h extern struct lockdep_map rcu_lock_map; · line 282

Quoted 5 lines
include/linux/rcupdate.h +2 -0
@@ -278,6 +278,24 @@ extern struct lockdep_map rcu_lock_map;
 					})
 
 /**
+ * rcu_assign_index - assign (publicize) a index of a newly
+ * initialized array elementg that will be dereferenced by RCU

---------------------------^
+ * initialized array element that will be dereferenced by RCU

Regards,
Jarek P.

#5 lore raw

On Tue, Feb 12, 2008 at 08:32:18PM +0100, Jarek Poplawski wrote:
...

Quoted 2 lines

It seems the above version of this macro uses the barrier for 0, but if I miss something, or for these other: documenting reasons,

...or __builtin_constants could be used for indexing (?!),

Quoted 2 lines

then of
course you are right.

Jarek P.

#6 lore raw
Quoted 6 lines · Jarek Poplawski

On Tue, Feb 12, 2008 at 08:32:18PM +0100, Jarek Poplawski wrote:
...

Quoted 2 lines

It seems the above version of this macro uses the barrier for 0, but if I miss something, or for these other: documenting reasons,

...or __builtin_constants could be used for indexing (?!),

Yep. For example:

	elem[0].next = 1;
	rcu_assign_index(global_index, 0);

							Thanx, Paul
Quoted 4 lines
Quoted 2 lines

then of
course you are right.

Jarek P.