2008-02-13 22:05:53

by Paul E. McKenney

[permalink] [raw]
Subject: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

Hello!

This is an updated version of the patch posted last November:

http://archives.free.net.ph/message/20071201.003721.cd6ff17c.en.html

This new version permits arguments with side effects, for example:

rcu_assign_pointer(global_p, p++);

and also verifies that the arguments are pointers, while still avoiding
the unnecessary memory barrier when assigning NULL to a pointer.
This memory-barrier avoidance means that rcu_assign_pointer() is now only
permitted for pointers (not array indexes), and so this version emits a
compiler warning if the first argument is not a pointer. I built a "make
allyesconfig" version on an x86 system, and received no such warnings.
If RCU is ever applied to array indexes, then the second patch in this
series should be applied, and the resulting rcu_assign_index() be used.

Given the rather surprising history of subtlely broken implementations of
rcu_assign_pointer(), I took the precaution of generating a full set of
test cases and verified that memory barriers and compiler warnings were
emitted when required. I guess it is the simple things that get you...

Signed-off-by: Paul E. McKenney <[email protected]>
---

rcupdate.h | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)

diff -urpNa -X dontdiff linux-2.6.24/include/linux/rcupdate.h linux-2.6.24-rap/include/linux/rcupdate.h
--- linux-2.6.24/include/linux/rcupdate.h 2008-01-24 14:58:37.000000000 -0800
+++ linux-2.6.24-rap/include/linux/rcupdate.h 2008-02-13 13:36:47.000000000 -0800
@@ -270,12 +270,20 @@ extern struct lockdep_map rcu_lock_map;
* structure after the pointer assignment. More importantly, this
* call documents which pointers will be dereferenced by RCU read-side
* code.
+ *
+ * Throws a compiler warning for non-pointer arguments.
+ *
+ * Does not insert a memory barrier for a NULL pointer.
*/

-#define rcu_assign_pointer(p, v) ({ \
- smp_wmb(); \
- (p) = (v); \
- })
+#define rcu_assign_pointer(p, v) \
+ ({ \
+ typeof(*p) *_________p1 = (v); \
+ \
+ if (!__builtin_constant_p(v) || (_________p1 != NULL)) \
+ smp_wmb(); \
+ (p) = _________p1; \
+ })

/**
* synchronize_sched - block until all CPUs have exited any non-preemptive


2008-02-13 22:07:59

by Paul E. McKenney

[permalink] [raw]
Subject: [PATCH 2/2] add rcu_assign_index() if ever needed

Hello again!

This is a speculative patch that as far as I can tell is not yet required.
If anyone applies RCU to a data structure allocated out of an array, using
array indexes in place of pointers to link the array elements together,
then the rcu_assign_index() function in this patch will be needed to
assign a given element's array index to the RCU-traversed index. The
implementation is exactly that of the old rcu_assign_pointer(), so is
extremely well tested.

The existing rcu_assign_pointer() will emit a compiler warning in cases
where rcu_assign_index() is required.

Signed-off-by: Paul E. McKenney <[email protected]>
---

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

diff -urpNa -X dontdiff linux-2.6.24-rap/include/linux/rcupdate.h linux-2.6.24-rai/include/linux/rcupdate.h
--- linux-2.6.24-rap/include/linux/rcupdate.h 2008-02-13 13:36:47.000000000 -0800
+++ linux-2.6.24-rai/include/linux/rcupdate.h 2008-02-13 10:55:40.000000000 -0800
@@ -286,6 +286,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.
*

2008-02-13 22:12:28

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

On Wed, 13 Feb 2008 14:00:24 -0800
"Paul E. McKenney" <[email protected]> wrote:

> Hello!
>
> This is an updated version of the patch posted last November:
>
> http://archives.free.net.ph/message/20071201.003721.cd6ff17c.en.html
>
> This new version permits arguments with side effects, for example:
>
> rcu_assign_pointer(global_p, p++);
>
> and also verifies that the arguments are pointers, while still avoiding
> the unnecessary memory barrier when assigning NULL to a pointer.
> This memory-barrier avoidance means that rcu_assign_pointer() is now only
> permitted for pointers (not array indexes), and so this version emits a
> compiler warning if the first argument is not a pointer. I built a "make
> allyesconfig" version on an x86 system, and received no such warnings.
> If RCU is ever applied to array indexes, then the second patch in this
> series should be applied, and the resulting rcu_assign_index() be used.
>
> Given the rather surprising history of subtlely broken implementations of
> rcu_assign_pointer(), I took the precaution of generating a full set of
> test cases and verified that memory barriers and compiler warnings were
> emitted when required. I guess it is the simple things that get you...
>
> Signed-off-by: Paul E. McKenney <[email protected]>
> ---
>
> rcupdate.h | 16 ++++++++++++----
> 1 file changed, 12 insertions(+), 4 deletions(-)
>
> diff -urpNa -X dontdiff linux-2.6.24/include/linux/rcupdate.h linux-2.6.24-rap/include/linux/rcupdate.h
> --- linux-2.6.24/include/linux/rcupdate.h 2008-01-24 14:58:37.000000000 -0800
> +++ linux-2.6.24-rap/include/linux/rcupdate.h 2008-02-13 13:36:47.000000000 -0800

whoop, ancient kernel alert.

> @@ -270,12 +270,20 @@ extern struct lockdep_map rcu_lock_map;
> * structure after the pointer assignment. More importantly, this
> * call documents which pointers will be dereferenced by RCU read-side
> * code.
> + *
> + * Throws a compiler warning for non-pointer arguments.
> + *
> + * Does not insert a memory barrier for a NULL pointer.
> */
>
> -#define rcu_assign_pointer(p, v) ({ \
> - smp_wmb(); \
> - (p) = (v); \
> - })
> +#define rcu_assign_pointer(p, v) \
> + ({ \
> + typeof(*p) *_________p1 = (v); \
> + \
> + if (!__builtin_constant_p(v) || (_________p1 != NULL)) \
> + smp_wmb(); \
> + (p) = _________p1; \
> + })

Someone already merged the dont-do-it-for-NULL patch so I reworked this
appropriately. Was too lazy to update the changelog though.

2008-02-13 22:42:05

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

On Wed, Feb 13, 2008 at 02:35:37PM -0800, Stephen Hemminger wrote:
> On Wed, 13 Feb 2008 14:00:24 -0800
> "Paul E. McKenney" <[email protected]> wrote:
>
> > Hello!
> >
> > This is an updated version of the patch posted last November:
> >
> > http://archives.free.net.ph/message/20071201.003721.cd6ff17c.en.html
> >
> > This new version permits arguments with side effects, for example:
> >
> > rcu_assign_pointer(global_p, p++);
> >
> > and also verifies that the arguments are pointers, while still avoiding
> > the unnecessary memory barrier when assigning NULL to a pointer.
> > This memory-barrier avoidance means that rcu_assign_pointer() is now only
> > permitted for pointers (not array indexes), and so this version emits a
> > compiler warning if the first argument is not a pointer. I built a "make
> > allyesconfig" version on an x86 system, and received no such warnings.
> > If RCU is ever applied to array indexes, then the second patch in this
> > series should be applied, and the resulting rcu_assign_index() be used.
> >
> > Given the rather surprising history of subtlely broken implementations of
> > rcu_assign_pointer(), I took the precaution of generating a full set of
> > test cases and verified that memory barriers and compiler warnings were
> > emitted when required. I guess it is the simple things that get you...
> >
> > Signed-off-by: Paul E. McKenney <[email protected]>
> > ---
> >
> > rcupdate.h | 16 ++++++++++++----
> > 1 file changed, 12 insertions(+), 4 deletions(-)
> >
> > diff -urpNa -X dontdiff linux-2.6.24/include/linux/rcupdate.h linux-2.6.24-rap/include/linux/rcupdate.h
> > --- linux-2.6.24/include/linux/rcupdate.h 2008-01-24 14:58:37.000000000 -0800
> > +++ linux-2.6.24-rap/include/linux/rcupdate.h 2008-02-13 13:36:47.000000000 -0800
> > @@ -270,12 +270,20 @@ extern struct lockdep_map rcu_lock_map;
> > * structure after the pointer assignment. More importantly, this
> > * call documents which pointers will be dereferenced by RCU read-side
> > * code.
> > + *
> > + * Throws a compiler warning for non-pointer arguments.
> > + *
> > + * Does not insert a memory barrier for a NULL pointer.
> > */
> >
> > -#define rcu_assign_pointer(p, v) ({ \
> > - smp_wmb(); \
> > - (p) = (v); \
> > - })
> > +#define rcu_assign_pointer(p, v) \
> > + ({ \
> > + typeof(*p) *_________p1 = (v); \
> > + \
> > + if (!__builtin_constant_p(v) || (_________p1 != NULL)) \
> > + smp_wmb(); \
> > + (p) = _________p1; \
> > + })
> >
> > /**
> > * synchronize_sched - block until all CPUs have exited any non-preemptive
>
> Will this still work if p is unsigned long?

Hello, Steve,

If p is unsigned long, then use rcu_assign_index() from the next patch in
the set. Looks like Andrew has applied it to -mm -- so please make sure
that he is aware if you do use it.

Thanx, Paul

2008-02-13 23:38:22

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

On Wed, Feb 13, 2008 at 02:42:33PM -0800, Stephen Hemminger wrote:
> On Wed, 13 Feb 2008 14:41:34 -0800
> "Paul E. McKenney" <[email protected]> wrote:
>
> > On Wed, Feb 13, 2008 at 02:35:37PM -0800, Stephen Hemminger wrote:
> > > On Wed, 13 Feb 2008 14:00:24 -0800
> > > "Paul E. McKenney" <[email protected]> wrote:
> > >
> > > > Hello!
> > > >
> > > > This is an updated version of the patch posted last November:
> > > >
> > > > http://archives.free.net.ph/message/20071201.003721.cd6ff17c.en.html
> > > >
> > > > This new version permits arguments with side effects, for example:
> > > >
> > > > rcu_assign_pointer(global_p, p++);
> > > >
> > > > and also verifies that the arguments are pointers, while still avoiding
> > > > the unnecessary memory barrier when assigning NULL to a pointer.
> > > > This memory-barrier avoidance means that rcu_assign_pointer() is now only
> > > > permitted for pointers (not array indexes), and so this version emits a
> > > > compiler warning if the first argument is not a pointer. I built a "make
> > > > allyesconfig" version on an x86 system, and received no such warnings.
> > > > If RCU is ever applied to array indexes, then the second patch in this
> > > > series should be applied, and the resulting rcu_assign_index() be used.
> > > >
> > > > Given the rather surprising history of subtlely broken implementations of
> > > > rcu_assign_pointer(), I took the precaution of generating a full set of
> > > > test cases and verified that memory barriers and compiler warnings were
> > > > emitted when required. I guess it is the simple things that get you...
> > > >
> > > > Signed-off-by: Paul E. McKenney <[email protected]>
> > > > ---
> > > >
> > > > rcupdate.h | 16 ++++++++++++----
> > > > 1 file changed, 12 insertions(+), 4 deletions(-)
> > > >
> > > > diff -urpNa -X dontdiff linux-2.6.24/include/linux/rcupdate.h linux-2.6.24-rap/include/linux/rcupdate.h
> > > > --- linux-2.6.24/include/linux/rcupdate.h 2008-01-24 14:58:37.000000000 -0800
> > > > +++ linux-2.6.24-rap/include/linux/rcupdate.h 2008-02-13 13:36:47.000000000 -0800
> > > > @@ -270,12 +270,20 @@ extern struct lockdep_map rcu_lock_map;
> > > > * structure after the pointer assignment. More importantly, this
> > > > * call documents which pointers will be dereferenced by RCU read-side
> > > > * code.
> > > > + *
> > > > + * Throws a compiler warning for non-pointer arguments.
> > > > + *
> > > > + * Does not insert a memory barrier for a NULL pointer.
> > > > */
> > > >
> > > > -#define rcu_assign_pointer(p, v) ({ \
> > > > - smp_wmb(); \
> > > > - (p) = (v); \
> > > > - })
> > > > +#define rcu_assign_pointer(p, v) \
> > > > + ({ \
> > > > + typeof(*p) *_________p1 = (v); \
> > > > + \
> > > > + if (!__builtin_constant_p(v) || (_________p1 != NULL)) \
> > > > + smp_wmb(); \
> > > > + (p) = _________p1; \
> > > > + })
> > > >
> > > > /**
> > > > * synchronize_sched - block until all CPUs have exited any non-preemptive
> > >
> > > Will this still work if p is unsigned long?
> >
> > Hello, Steve,
> >
> > If p is unsigned long, then use rcu_assign_index() from the next patch in
> > the set. Looks like Andrew has applied it to -mm -- so please make sure
> > that he is aware if you do use it.
>
> Make sure fib_trie still works and doesn't get warnings.

Ah. It does take a bit to get fib_trie into one's build -- allyesconfig
doesn't cut it. Please accept my apologies for my confusion!!!

Once fib_trie is configured, I do indeed get:

net/ipv4/fib_trie.c: In function ‘node_set_parent’:
net/ipv4/fib_trie.c:182: warning: comparison between pointer and integer

So, given that node->parent is an unsigned long, I changed node_set_parent()
to the following:

static inline void node_set_parent(struct node *node, struct tnode *ptr)
{
rcu_assign_index(node->parent, (unsigned long)ptr | NODE_TYPE(node));
}

This removes the warnings. I am a little ambivalent about this, as
this is really a pointer in disguise rather than an array index, but
patch below. I suppose that another option would be to make node->parent
be a void* and provide appropriate accessor functions/macros.

Thoughts?

Signed-off-by: Paul E. McKenney <[email protected]>
---

fib_trie.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff -urpNa -X dontdiff linux-2.6.25-rc1/net/ipv4/fib_trie.c linux-2.6.25-rc1-fib_trie-warn.compile/net/ipv4/fib_trie.c
--- linux-2.6.25-rc1/net/ipv4/fib_trie.c 2008-02-13 14:38:12.000000000 -0800
+++ linux-2.6.25-rc1-fib_trie-warn.compile/net/ipv4/fib_trie.c 2008-02-13 15:22:02.000000000 -0800
@@ -179,8 +179,7 @@ static inline struct tnode *node_parent_

static inline void node_set_parent(struct node *node, struct tnode *ptr)
{
- rcu_assign_pointer(node->parent,
- (unsigned long)ptr | NODE_TYPE(node));
+ rcu_assign_index(node->parent, (unsigned long)ptr | NODE_TYPE(node));
}

static inline struct node *tnode_get_child(struct tnode *tn, unsigned int i)

2008-02-13 23:52:21

by Stephen Hemminger

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

On Wed, 13 Feb 2008 15:37:44 -0800
"Paul E. McKenney" <[email protected]> wrote:

> On Wed, Feb 13, 2008 at 02:42:33PM -0800, Stephen Hemminger wrote:
> > On Wed, 13 Feb 2008 14:41:34 -0800
> > "Paul E. McKenney" <[email protected]> wrote:
> >
> > > On Wed, Feb 13, 2008 at 02:35:37PM -0800, Stephen Hemminger wrote:
> > > > On Wed, 13 Feb 2008 14:00:24 -0800
> > > > "Paul E. McKenney" <[email protected]> wrote:
> > > >
> > > > > Hello!
> > > > >
> > > > > This is an updated version of the patch posted last November:
> > > > >
> > > > > http://archives.free.net.ph/message/20071201.003721.cd6ff17c.en.html
> > > > >
> > > > > This new version permits arguments with side effects, for example:
> > > > >
> > > > > rcu_assign_pointer(global_p, p++);
> > > > >
> > > > > and also verifies that the arguments are pointers, while still avoiding
> > > > > the unnecessary memory barrier when assigning NULL to a pointer.
> > > > > This memory-barrier avoidance means that rcu_assign_pointer() is now only
> > > > > permitted for pointers (not array indexes), and so this version emits a
> > > > > compiler warning if the first argument is not a pointer. I built a "make
> > > > > allyesconfig" version on an x86 system, and received no such warnings.
> > > > > If RCU is ever applied to array indexes, then the second patch in this
> > > > > series should be applied, and the resulting rcu_assign_index() be used.
> > > > >
> > > > > Given the rather surprising history of subtlely broken implementations of
> > > > > rcu_assign_pointer(), I took the precaution of generating a full set of
> > > > > test cases and verified that memory barriers and compiler warnings were
> > > > > emitted when required. I guess it is the simple things that get you...
> > > > >
> > > > > Signed-off-by: Paul E. McKenney <[email protected]>
> > > > > ---
> > > > >
> > > > > rcupdate.h | 16 ++++++++++++----
> > > > > 1 file changed, 12 insertions(+), 4 deletions(-)
> > > > >
> > > > > diff -urpNa -X dontdiff linux-2.6.24/include/linux/rcupdate.h linux-2.6.24-rap/include/linux/rcupdate.h
> > > > > --- linux-2.6.24/include/linux/rcupdate.h 2008-01-24 14:58:37.000000000 -0800
> > > > > +++ linux-2.6.24-rap/include/linux/rcupdate.h 2008-02-13 13:36:47.000000000 -0800
> > > > > @@ -270,12 +270,20 @@ extern struct lockdep_map rcu_lock_map;
> > > > > * structure after the pointer assignment. More importantly, this
> > > > > * call documents which pointers will be dereferenced by RCU read-side
> > > > > * code.
> > > > > + *
> > > > > + * Throws a compiler warning for non-pointer arguments.
> > > > > + *
> > > > > + * Does not insert a memory barrier for a NULL pointer.
> > > > > */
> > > > >
> > > > > -#define rcu_assign_pointer(p, v) ({ \
> > > > > - smp_wmb(); \
> > > > > - (p) = (v); \
> > > > > - })
> > > > > +#define rcu_assign_pointer(p, v) \
> > > > > + ({ \
> > > > > + typeof(*p) *_________p1 = (v); \
> > > > > + \
> > > > > + if (!__builtin_constant_p(v) || (_________p1 != NULL)) \
> > > > > + smp_wmb(); \
> > > > > + (p) = _________p1; \
> > > > > + })
> > > > >
> > > > > /**
> > > > > * synchronize_sched - block until all CPUs have exited any non-preemptive
> > > >
> > > > Will this still work if p is unsigned long?
> > >
> > > Hello, Steve,
> > >
> > > If p is unsigned long, then use rcu_assign_index() from the next patch in
> > > the set. Looks like Andrew has applied it to -mm -- so please make sure
> > > that he is aware if you do use it.
> >
> > Make sure fib_trie still works and doesn't get warnings.
>
> Ah. It does take a bit to get fib_trie into one's build -- allyesconfig
> doesn't cut it. Please accept my apologies for my confusion!!!
>
> Once fib_trie is configured, I do indeed get:
>
> net/ipv4/fib_trie.c: In function ‘node_set_parent’:
> net/ipv4/fib_trie.c:182: warning: comparison between pointer and integer
>
> So, given that node->parent is an unsigned long, I changed node_set_parent()
> to the following:
>
> static inline void node_set_parent(struct node *node, struct tnode *ptr)
> {
> rcu_assign_index(node->parent, (unsigned long)ptr | NODE_TYPE(node));
> }
>
> This removes the warnings. I am a little ambivalent about this, as
> this is really a pointer in disguise rather than an array index, but
> patch below. I suppose that another option would be to make node->parent
> be a void* and provide appropriate accessor functions/macros.
>
> Thoughts?
>
> Signed-off-by: Paul E. McKenney <[email protected]>

Maybe cast both sides to void * in this case:

static inline void node_set_parent(struct node *node, struct tnode *ptr)
{
rcu_assign_pointer((void *) node->parent, (void *)((unsigned long)ptr | NODE_TYPE(node)));
}

--
Stephen Hemminger <[email protected]>

2008-02-13 23:54:07

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

On Wed, 13 Feb 2008 15:37:44 -0800
"Paul E. McKenney" <[email protected]> wrote:

> Ah. It does take a bit to get fib_trie into one's build -- allyesconfig
> doesn't cut it.

This is not good. The sole purpose of allmodconfig and allyesconfig is for
compilation and linkage coverage testing. Hence we should aim to get as
much code as possible included in those cases.

2008-02-13 23:55:53

by Stephen Hemminger

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

On Wed, 13 Feb 2008 15:52:45 -0800
Andrew Morton <[email protected]> wrote:

> On Wed, 13 Feb 2008 15:37:44 -0800
> "Paul E. McKenney" <[email protected]> wrote:
>
> > Ah. It does take a bit to get fib_trie into one's build -- allyesconfig
> > doesn't cut it.
>
> This is not good. The sole purpose of allmodconfig and allyesconfig is for
> compilation and linkage coverage testing. Hence we should aim to get as
> much code as possible included in those cases.
>

The current model is compile time choice. It is on my long term list
to make this a runtime option.

--
Stephen Hemminger <[email protected]>

2008-02-13 23:57:23

by David Miller

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

From: Andrew Morton <[email protected]>
Date: Wed, 13 Feb 2008 15:52:45 -0800

> On Wed, 13 Feb 2008 15:37:44 -0800
> "Paul E. McKenney" <[email protected]> wrote:
>
> > Ah. It does take a bit to get fib_trie into one's build -- allyesconfig
> > doesn't cut it.
>
> This is not good. The sole purpose of allmodconfig and allyesconfig is for
> compilation and linkage coverage testing. Hence we should aim to get as
> much code as possible included in those cases.

Well, in this case there is a choice, either you use one routing
lookup datastructure or the other. It's not purposefully being hidden
from the everything builds :-)

2008-02-14 00:10:53

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

On Wed, 13 Feb 2008 15:57:38 -0800 (PST)
David Miller <[email protected]> wrote:

> From: Andrew Morton <[email protected]>
> Date: Wed, 13 Feb 2008 15:52:45 -0800
>
> > On Wed, 13 Feb 2008 15:37:44 -0800
> > "Paul E. McKenney" <[email protected]> wrote:
> >
> > > Ah. It does take a bit to get fib_trie into one's build -- allyesconfig
> > > doesn't cut it.
> >
> > This is not good. The sole purpose of allmodconfig and allyesconfig is for
> > compilation and linkage coverage testing. Hence we should aim to get as
> > much code as possible included in those cases.
>
> Well, in this case there is a choice, either you use one routing
> lookup datastructure or the other. It's not purposefully being hidden
> from the everything builds :-)

oic. yes, that is a bit of a problem. Oh well.

`make randconfig' seems to be able to enable CONFIG_IP_FIB_TRIE about one
time in eight ;)

2008-02-14 00:14:36

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

On Wed, Feb 13, 2008 at 03:51:58PM -0800, Stephen Hemminger wrote:
> On Wed, 13 Feb 2008 15:37:44 -0800
> "Paul E. McKenney" <[email protected]> wrote:
> > On Wed, Feb 13, 2008 at 02:42:33PM -0800, Stephen Hemminger wrote:
> > > On Wed, 13 Feb 2008 14:41:34 -0800
> > > "Paul E. McKenney" <[email protected]> wrote:
> > >
> > > > On Wed, Feb 13, 2008 at 02:35:37PM -0800, Stephen Hemminger wrote:
> > > > > On Wed, 13 Feb 2008 14:00:24 -0800
> > > > > "Paul E. McKenney" <[email protected]> wrote:
> > > > >
> > > > > > Hello!
> > > > > >
> > > > > > This is an updated version of the patch posted last November:
> > > > > >
> > > > > > http://archives.free.net.ph/message/20071201.003721.cd6ff17c.en.html
> > > > > >
> > > > > > This new version permits arguments with side effects, for example:
> > > > > >
> > > > > > rcu_assign_pointer(global_p, p++);
> > > > > >
> > > > > > and also verifies that the arguments are pointers, while still avoiding
> > > > > > the unnecessary memory barrier when assigning NULL to a pointer.
> > > > > > This memory-barrier avoidance means that rcu_assign_pointer() is now only
> > > > > > permitted for pointers (not array indexes), and so this version emits a
> > > > > > compiler warning if the first argument is not a pointer. I built a "make
> > > > > > allyesconfig" version on an x86 system, and received no such warnings.
> > > > > > If RCU is ever applied to array indexes, then the second patch in this
> > > > > > series should be applied, and the resulting rcu_assign_index() be used.
> > > > > >
> > > > > > Given the rather surprising history of subtlely broken implementations of
> > > > > > rcu_assign_pointer(), I took the precaution of generating a full set of
> > > > > > test cases and verified that memory barriers and compiler warnings were
> > > > > > emitted when required. I guess it is the simple things that get you...
> > > > > >
> > > > > > Signed-off-by: Paul E. McKenney <[email protected]>
> > > > > > ---
> > > > > >
> > > > > > rcupdate.h | 16 ++++++++++++----
> > > > > > 1 file changed, 12 insertions(+), 4 deletions(-)
> > > > > >
> > > > > > diff -urpNa -X dontdiff linux-2.6.24/include/linux/rcupdate.h linux-2.6.24-rap/include/linux/rcupdate.h
> > > > > > --- linux-2.6.24/include/linux/rcupdate.h 2008-01-24 14:58:37.000000000 -0800
> > > > > > +++ linux-2.6.24-rap/include/linux/rcupdate.h 2008-02-13 13:36:47.000000000 -0800
> > > > > > @@ -270,12 +270,20 @@ extern struct lockdep_map rcu_lock_map;
> > > > > > * structure after the pointer assignment. More importantly, this
> > > > > > * call documents which pointers will be dereferenced by RCU read-side
> > > > > > * code.
> > > > > > + *
> > > > > > + * Throws a compiler warning for non-pointer arguments.
> > > > > > + *
> > > > > > + * Does not insert a memory barrier for a NULL pointer.
> > > > > > */
> > > > > >
> > > > > > -#define rcu_assign_pointer(p, v) ({ \
> > > > > > - smp_wmb(); \
> > > > > > - (p) = (v); \
> > > > > > - })
> > > > > > +#define rcu_assign_pointer(p, v) \
> > > > > > + ({ \
> > > > > > + typeof(*p) *_________p1 = (v); \
> > > > > > + \
> > > > > > + if (!__builtin_constant_p(v) || (_________p1 != NULL)) \
> > > > > > + smp_wmb(); \
> > > > > > + (p) = _________p1; \
> > > > > > + })
> > > > > >
> > > > > > /**
> > > > > > * synchronize_sched - block until all CPUs have exited any non-preemptive
> > > > >
> > > > > Will this still work if p is unsigned long?
> > > >
> > > > Hello, Steve,
> > > >
> > > > If p is unsigned long, then use rcu_assign_index() from the next patch in
> > > > the set. Looks like Andrew has applied it to -mm -- so please make sure
> > > > that he is aware if you do use it.
> > >
> > > Make sure fib_trie still works and doesn't get warnings.
> >
> > Ah. It does take a bit to get fib_trie into one's build -- allyesconfig
> > doesn't cut it. Please accept my apologies for my confusion!!!
> >
> > Once fib_trie is configured, I do indeed get:
> >
> > net/ipv4/fib_trie.c: In function ‘node_set_parent’:
> > net/ipv4/fib_trie.c:182: warning: comparison between pointer and integer
> >
> > So, given that node->parent is an unsigned long, I changed node_set_parent()
> > to the following:
> >
> > static inline void node_set_parent(struct node *node, struct tnode *ptr)
> > {
> > rcu_assign_index(node->parent, (unsigned long)ptr | NODE_TYPE(node));
> > }
> >
> > This removes the warnings. I am a little ambivalent about this, as
> > this is really a pointer in disguise rather than an array index, but
> > patch below. I suppose that another option would be to make node->parent
> > be a void* and provide appropriate accessor functions/macros.
> >
> > Thoughts?
> >
> > Signed-off-by: Paul E. McKenney <[email protected]>
>
> Maybe cast both sides to void * in this case:
>
> static inline void node_set_parent(struct node *node, struct tnode *ptr)
> {
> rcu_assign_pointer((void *) node->parent, (void *)((unsigned long)ptr | NODE_TYPE(node)));
> }

That gets me the following:

net/ipv4/fib_trie.c: In function ‘node_set_parent’:
net/ipv4/fib_trie.c:182: error: invalid lvalue in assignment

However, as with much in computing, an extra level of indirection fixes
things. Your call as to whether or not the cure is preferable to the
disease. ;-)

Signed-off-by: Paul E. McKenney <[email protected]>
---

fib_trie.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff -urpNa -X dontdiff linux-2.6.25-rc1/net/ipv4/fib_trie.c linux-2.6.25-rc1-fib_trie-warn.compile/net/ipv4/fib_trie.c
--- linux-2.6.25-rc1/net/ipv4/fib_trie.c 2008-02-13 14:38:12.000000000 -0800
+++ linux-2.6.25-rc1-fib_trie-warn.compile/net/ipv4/fib_trie.c 2008-02-13 16:10:07.000000000 -0800
@@ -179,8 +179,8 @@ static inline struct tnode *node_parent_

static inline void node_set_parent(struct node *node, struct tnode *ptr)
{
- rcu_assign_pointer(node->parent,
- (unsigned long)ptr | NODE_TYPE(node));
+ rcu_assign_pointer((*(void **)&node->parent),
+ (void *)((unsigned long)ptr | NODE_TYPE(node)));
}

static inline struct node *tnode_get_child(struct tnode *tn, unsigned int i)

2008-02-14 00:31:58

by Stephen Hemminger

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

On Wed, 13 Feb 2008 16:14:04 -0800
"Paul E. McKenney" <[email protected]> wrote:

> On Wed, Feb 13, 2008 at 03:51:58PM -0800, Stephen Hemminger wrote:
> > On Wed, 13 Feb 2008 15:37:44 -0800
> > "Paul E. McKenney" <[email protected]> wrote:
> > > On Wed, Feb 13, 2008 at 02:42:33PM -0800, Stephen Hemminger wrote:
> > > > On Wed, 13 Feb 2008 14:41:34 -0800
> > > > "Paul E. McKenney" <[email protected]> wrote:
> > > >
> > > > > On Wed, Feb 13, 2008 at 02:35:37PM -0800, Stephen Hemminger wrote:
> > > > > > On Wed, 13 Feb 2008 14:00:24 -0800
> > > > > > "Paul E. McKenney" <[email protected]> wrote:
> > > > > >
> > > > > > > Hello!
> > > > > > >
> > > > > > > This is an updated version of the patch posted last November:
> > > > > > >
> > > > > > > http://archives.free.net.ph/message/20071201.003721.cd6ff17c.en.html
> > > > > > >
> > > > > > > This new version permits arguments with side effects, for example:
> > > > > > >
> > > > > > > rcu_assign_pointer(global_p, p++);
> > > > > > >
> > > > > > > and also verifies that the arguments are pointers, while still avoiding
> > > > > > > the unnecessary memory barrier when assigning NULL to a pointer.
> > > > > > > This memory-barrier avoidance means that rcu_assign_pointer() is now only
> > > > > > > permitted for pointers (not array indexes), and so this version emits a
> > > > > > > compiler warning if the first argument is not a pointer. I built a "make
> > > > > > > allyesconfig" version on an x86 system, and received no such warnings.
> > > > > > > If RCU is ever applied to array indexes, then the second patch in this
> > > > > > > series should be applied, and the resulting rcu_assign_index() be used.
> > > > > > >
> > > > > > > Given the rather surprising history of subtlely broken implementations of
> > > > > > > rcu_assign_pointer(), I took the precaution of generating a full set of
> > > > > > > test cases and verified that memory barriers and compiler warnings were
> > > > > > > emitted when required. I guess it is the simple things that get you...
> > > > > > >
> > > > > > > Signed-off-by: Paul E. McKenney <[email protected]>
> > > > > > > ---
> > > > > > >
> > > > > > > rcupdate.h | 16 ++++++++++++----
> > > > > > > 1 file changed, 12 insertions(+), 4 deletions(-)
> > > > > > >
> > > > > > > diff -urpNa -X dontdiff linux-2.6.24/include/linux/rcupdate.h linux-2.6.24-rap/include/linux/rcupdate.h
> > > > > > > --- linux-2.6.24/include/linux/rcupdate.h 2008-01-24 14:58:37.000000000 -0800
> > > > > > > +++ linux-2.6.24-rap/include/linux/rcupdate.h 2008-02-13 13:36:47.000000000 -0800
> > > > > > > @@ -270,12 +270,20 @@ extern struct lockdep_map rcu_lock_map;
> > > > > > > * structure after the pointer assignment. More importantly, this
> > > > > > > * call documents which pointers will be dereferenced by RCU read-side
> > > > > > > * code.
> > > > > > > + *
> > > > > > > + * Throws a compiler warning for non-pointer arguments.
> > > > > > > + *
> > > > > > > + * Does not insert a memory barrier for a NULL pointer.
> > > > > > > */
> > > > > > >
> > > > > > > -#define rcu_assign_pointer(p, v) ({ \
> > > > > > > - smp_wmb(); \
> > > > > > > - (p) = (v); \
> > > > > > > - })
> > > > > > > +#define rcu_assign_pointer(p, v) \
> > > > > > > + ({ \
> > > > > > > + typeof(*p) *_________p1 = (v); \
> > > > > > > + \
> > > > > > > + if (!__builtin_constant_p(v) || (_________p1 != NULL)) \
> > > > > > > + smp_wmb(); \
> > > > > > > + (p) = _________p1; \
> > > > > > > + })
> > > > > > >
> > > > > > > /**
> > > > > > > * synchronize_sched - block until all CPUs have exited any non-preemptive
> > > > > >
> > > > > > Will this still work if p is unsigned long?
> > > > >
> > > > > Hello, Steve,
> > > > >
> > > > > If p is unsigned long, then use rcu_assign_index() from the next patch in
> > > > > the set. Looks like Andrew has applied it to -mm -- so please make sure
> > > > > that he is aware if you do use it.
> > > >
> > > > Make sure fib_trie still works and doesn't get warnings.
> > >
> > > Ah. It does take a bit to get fib_trie into one's build -- allyesconfig
> > > doesn't cut it. Please accept my apologies for my confusion!!!
> > >
> > > Once fib_trie is configured, I do indeed get:
> > >
> > > net/ipv4/fib_trie.c: In function ‘node_set_parent’:
> > > net/ipv4/fib_trie.c:182: warning: comparison between pointer and integer
> > >
> > > So, given that node->parent is an unsigned long, I changed node_set_parent()
> > > to the following:
> > >
> > > static inline void node_set_parent(struct node *node, struct tnode *ptr)
> > > {
> > > rcu_assign_index(node->parent, (unsigned long)ptr | NODE_TYPE(node));
> > > }
> > >
> > > This removes the warnings. I am a little ambivalent about this, as
> > > this is really a pointer in disguise rather than an array index, but
> > > patch below. I suppose that another option would be to make node->parent
> > > be a void* and provide appropriate accessor functions/macros.
> > >
> > > Thoughts?
> > >
> > > Signed-off-by: Paul E. McKenney <[email protected]>
> >
> > Maybe cast both sides to void * in this case:
> >
> > static inline void node_set_parent(struct node *node, struct tnode *ptr)
> > {
> > rcu_assign_pointer((void *) node->parent, (void *)((unsigned long)ptr | NODE_TYPE(node)));
> > }
>
> That gets me the following:
>
> net/ipv4/fib_trie.c: In function ‘node_set_parent’:
> net/ipv4/fib_trie.c:182: error: invalid lvalue in assignment
>
> However, as with much in computing, an extra level of indirection fixes
> things. Your call as to whether or not the cure is preferable to the
> disease. ;-)
>
> Signed-off-by: Paul E. McKenney <[email protected]>
> ---
>
> fib_trie.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff -urpNa -X dontdiff linux-2.6.25-rc1/net/ipv4/fib_trie.c linux-2.6.25-rc1-fib_trie-warn.compile/net/ipv4/fib_trie.c
> --- linux-2.6.25-rc1/net/ipv4/fib_trie.c 2008-02-13 14:38:12.000000000 -0800
> +++ linux-2.6.25-rc1-fib_trie-warn.compile/net/ipv4/fib_trie.c 2008-02-13 16:10:07.000000000 -0800
> @@ -179,8 +179,8 @@ static inline struct tnode *node_parent_
>
> static inline void node_set_parent(struct node *node, struct tnode *ptr)
> {
> - rcu_assign_pointer(node->parent,
> - (unsigned long)ptr | NODE_TYPE(node));
> + rcu_assign_pointer((*(void **)&node->parent),
> + (void *)((unsigned long)ptr | NODE_TYPE(node)));
> }

That is heading towards ugly... Maybe not using the macro at all (for this case) would be best:

static inline void node_set_parent(struct node *node, struct tnode *ptr)
{
smp_wmb();
node->parent = (unsigned long)ptr | NODE_TYPE(node);
}

2008-02-14 00:43:18

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

On Wed, Feb 13, 2008 at 04:27:00PM -0800, Stephen Hemminger wrote:
> On Wed, 13 Feb 2008 16:14:04 -0800
> "Paul E. McKenney" <[email protected]> wrote:
> > On Wed, Feb 13, 2008 at 03:51:58PM -0800, Stephen Hemminger wrote:

[ . . . ]

> > > Maybe cast both sides to void * in this case:
> > >
> > > static inline void node_set_parent(struct node *node, struct tnode *ptr)
> > > {
> > > rcu_assign_pointer((void *) node->parent, (void *)((unsigned long)ptr | NODE_TYPE(node)));
> > > }
> >
> > That gets me the following:
> >
> > net/ipv4/fib_trie.c: In function ‘node_set_parent’:
> > net/ipv4/fib_trie.c:182: error: invalid lvalue in assignment
> >
> > However, as with much in computing, an extra level of indirection fixes
> > things. Your call as to whether or not the cure is preferable to the
> > disease. ;-)
> >
> > Signed-off-by: Paul E. McKenney <[email protected]>
> > ---
> >
> > fib_trie.c | 4 ++--
> > 1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff -urpNa -X dontdiff linux-2.6.25-rc1/net/ipv4/fib_trie.c linux-2.6.25-rc1-fib_trie-warn.compile/net/ipv4/fib_trie.c
> > --- linux-2.6.25-rc1/net/ipv4/fib_trie.c 2008-02-13 14:38:12.000000000 -0800
> > +++ linux-2.6.25-rc1-fib_trie-warn.compile/net/ipv4/fib_trie.c 2008-02-13 16:10:07.000000000 -0800
> > @@ -179,8 +179,8 @@ static inline struct tnode *node_parent_
> >
> > static inline void node_set_parent(struct node *node, struct tnode *ptr)
> > {
> > - rcu_assign_pointer(node->parent,
> > - (unsigned long)ptr | NODE_TYPE(node));
> > + rcu_assign_pointer((*(void **)&node->parent),
> > + (void *)((unsigned long)ptr | NODE_TYPE(node)));
> > }
>
> That is heading towards ugly... Maybe not using the macro at all (for this case) would be best:
>
> static inline void node_set_parent(struct node *node, struct tnode *ptr)
> {
> smp_wmb();
> node->parent = (unsigned long)ptr | NODE_TYPE(node);
> }

Or, alternatively, the rcu_assign_index() patch sent earlier to avoid
the bare memory barrier?

Thanx, Paul

2008-02-14 00:54:22

by Stephen Hemminger

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

On Wed, 13 Feb 2008 16:42:53 -0800
"Paul E. McKenney" <[email protected]> wrote:

> On Wed, Feb 13, 2008 at 04:27:00PM -0800, Stephen Hemminger wrote:
> > On Wed, 13 Feb 2008 16:14:04 -0800
> > "Paul E. McKenney" <[email protected]> wrote:
> > > On Wed, Feb 13, 2008 at 03:51:58PM -0800, Stephen Hemminger wrote:
>
> [ . . . ]
>
> > > > Maybe cast both sides to void * in this case:
> > > >
> > > > static inline void node_set_parent(struct node *node, struct tnode *ptr)
> > > > {
> > > > rcu_assign_pointer((void *) node->parent, (void *)((unsigned long)ptr | NODE_TYPE(node)));
> > > > }
> > >
> > > That gets me the following:
> > >
> > > net/ipv4/fib_trie.c: In function ‘node_set_parent’:
> > > net/ipv4/fib_trie.c:182: error: invalid lvalue in assignment
> > >
> > > However, as with much in computing, an extra level of indirection fixes
> > > things. Your call as to whether or not the cure is preferable to the
> > > disease. ;-)
> > >
> > > Signed-off-by: Paul E. McKenney <[email protected]>
> > > ---
> > >
> > > fib_trie.c | 4 ++--
> > > 1 file changed, 2 insertions(+), 2 deletions(-)
> > >
> > > diff -urpNa -X dontdiff linux-2.6.25-rc1/net/ipv4/fib_trie.c linux-2.6.25-rc1-fib_trie-warn.compile/net/ipv4/fib_trie.c
> > > --- linux-2.6.25-rc1/net/ipv4/fib_trie.c 2008-02-13 14:38:12.000000000 -0800
> > > +++ linux-2.6.25-rc1-fib_trie-warn.compile/net/ipv4/fib_trie.c 2008-02-13 16:10:07.000000000 -0800
> > > @@ -179,8 +179,8 @@ static inline struct tnode *node_parent_
> > >
> > > static inline void node_set_parent(struct node *node, struct tnode *ptr)
> > > {
> > > - rcu_assign_pointer(node->parent,
> > > - (unsigned long)ptr | NODE_TYPE(node));
> > > + rcu_assign_pointer((*(void **)&node->parent),
> > > + (void *)((unsigned long)ptr | NODE_TYPE(node)));
> > > }
> >
> > That is heading towards ugly... Maybe not using the macro at all (for this case) would be best:
> >
> > static inline void node_set_parent(struct node *node, struct tnode *ptr)
> > {
> > smp_wmb();
> > node->parent = (unsigned long)ptr | NODE_TYPE(node);
> > }
>
> Or, alternatively, the rcu_assign_index() patch sent earlier to avoid
> the bare memory barrier?
>
> Thanx, Paul

I am fine with rcu_assign_index(), and add a comment in node_set_parent.

2008-02-14 01:34:59

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

On Wed, Feb 13, 2008 at 04:53:56PM -0800, Stephen Hemminger wrote:
> On Wed, 13 Feb 2008 16:42:53 -0800
> "Paul E. McKenney" <[email protected]> wrote:
> > On Wed, Feb 13, 2008 at 04:27:00PM -0800, Stephen Hemminger wrote:

[ . . . ]

> > > That is heading towards ugly... Maybe not using the macro at all (for this case) would be best:
> > >
> > > static inline void node_set_parent(struct node *node, struct tnode *ptr)
> > > {
> > > smp_wmb();
> > > node->parent = (unsigned long)ptr | NODE_TYPE(node);
> > > }
> >
> > Or, alternatively, the rcu_assign_index() patch sent earlier to avoid
> > the bare memory barrier?
> >
> > Thanx, Paul
>
> I am fine with rcu_assign_index(), and add a comment in node_set_parent.

OK, how about the following?

Signed-off-by: Paul E. McKenney <[email protected]>
---

fib_trie.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)

diff -urpNa -X dontdiff linux-2.6.25-rc1/net/ipv4/fib_trie.c linux-2.6.25-rc1-fib_trie-warn.compile/net/ipv4/fib_trie.c
--- linux-2.6.25-rc1/net/ipv4/fib_trie.c 2008-02-13 14:38:12.000000000 -0800
+++ linux-2.6.25-rc1-fib_trie-warn.compile/net/ipv4/fib_trie.c 2008-02-13 17:31:16.000000000 -0800
@@ -96,6 +96,14 @@ typedef unsigned int t_key;
#define IS_TNODE(n) (!(n->parent & T_LEAF))
#define IS_LEAF(n) (n->parent & T_LEAF)

+/*
+ * The "parent" fields in struct node and struct leaf are really pointers,
+ * but with the possibility that the T_LEAF bit is set. Therefore, both
+ * the C compiler and RCU see them as integers rather than pointers.
+ * This in turn means that rcu_assign_index() must be used to assign
+ * values to these fields, rather than the usual rcu_assign_pointer().
+ */
+
struct node {
unsigned long parent;
t_key key;
@@ -179,8 +187,7 @@ static inline struct tnode *node_parent_

static inline void node_set_parent(struct node *node, struct tnode *ptr)
{
- rcu_assign_pointer(node->parent,
- (unsigned long)ptr | NODE_TYPE(node));
+ rcu_assign_index(node->parent, (unsigned long)ptr | NODE_TYPE(node));
}

static inline struct node *tnode_get_child(struct tnode *tn, unsigned int i)

2008-02-14 01:38:05

by Stephen Hemminger

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

On Wed, 13 Feb 2008 17:34:27 -0800
"Paul E. McKenney" <[email protected]> wrote:

> On Wed, Feb 13, 2008 at 04:53:56PM -0800, Stephen Hemminger wrote:
> > On Wed, 13 Feb 2008 16:42:53 -0800
> > "Paul E. McKenney" <[email protected]> wrote:
> > > On Wed, Feb 13, 2008 at 04:27:00PM -0800, Stephen Hemminger wrote:
>
> [ . . . ]
>
> > > > That is heading towards ugly... Maybe not using the macro at all (for this case) would be best:
> > > >
> > > > static inline void node_set_parent(struct node *node, struct tnode *ptr)
> > > > {
> > > > smp_wmb();
> > > > node->parent = (unsigned long)ptr | NODE_TYPE(node);
> > > > }
> > >
> > > Or, alternatively, the rcu_assign_index() patch sent earlier to avoid
> > > the bare memory barrier?
> > >
> > > Thanx, Paul
> >
> > I am fine with rcu_assign_index(), and add a comment in node_set_parent.
>
> OK, how about the following?
>
> Signed-off-by: Paul E. McKenney <[email protected]>
> ---
>
> fib_trie.c | 11 +++++++++--
> 1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff -urpNa -X dontdiff linux-2.6.25-rc1/net/ipv4/fib_trie.c linux-2.6.25-rc1-fib_trie-warn.compile/net/ipv4/fib_trie.c
> --- linux-2.6.25-rc1/net/ipv4/fib_trie.c 2008-02-13 14:38:12.000000000 -0800
> +++ linux-2.6.25-rc1-fib_trie-warn.compile/net/ipv4/fib_trie.c 2008-02-13 17:31:16.000000000 -0800
> @@ -96,6 +96,14 @@ typedef unsigned int t_key;
> #define IS_TNODE(n) (!(n->parent & T_LEAF))
> #define IS_LEAF(n) (n->parent & T_LEAF)
>
> +/*
> + * The "parent" fields in struct node and struct leaf are really pointers,
> + * but with the possibility that the T_LEAF bit is set. Therefore, both
> + * the C compiler and RCU see them as integers rather than pointers.
> + * This in turn means that rcu_assign_index() must be used to assign
> + * values to these fields, rather than the usual rcu_assign_pointer().
> + */
> +
> struct node {
> unsigned long parent;
> t_key key;
> @@ -179,8 +187,7 @@ static inline struct tnode *node_parent_
>
> static inline void node_set_parent(struct node *node, struct tnode *ptr)
> {
> - rcu_assign_pointer(node->parent,
> - (unsigned long)ptr | NODE_TYPE(node));
> + rcu_assign_index(node->parent, (unsigned long)ptr | NODE_TYPE(node));
> }
>
> static inline struct node *tnode_get_child(struct tnode *tn, unsigned int i)

Yes, thats great.

Subject: Re: [PATCH 2/2] add rcu_assign_index() if ever needed

On Wed, Feb 13, 2008 at 02:05:15PM -0800, Paul E. McKenney wrote:
> Hello again!
>
> This is a speculative patch that as far as I can tell is not yet required.
> If anyone applies RCU to a data structure allocated out of an array, using
> array indexes in place of pointers to link the array elements together,
> then the rcu_assign_index() function in this patch will be needed to
> assign a given element's array index to the RCU-traversed index. The
> implementation is exactly that of the old rcu_assign_pointer(), so is
> extremely well tested.
>
> The existing rcu_assign_pointer() will emit a compiler warning in cases
> where rcu_assign_index() is required.
>
> Signed-off-by: Paul E. McKenney <[email protected]>
> ---
>
> rcupdate.h | 18 ++++++++++++++++++
> 1 file changed, 18 insertions(+)
>
> diff -urpNa -X dontdiff linux-2.6.24-rap/include/linux/rcupdate.h linux-2.6.24-rai/include/linux/rcupdate.h
> --- linux-2.6.24-rap/include/linux/rcupdate.h 2008-02-13 13:36:47.000000000 -0800
> +++ linux-2.6.24-rai/include/linux/rcupdate.h 2008-02-13 10:55:40.000000000 -0800
> @@ -286,6 +286,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
^^^^^^^^

I hope Andrew got that one while porting against the latest -mm :)

Looks good otherwise.


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

--
Thanks and Regards
gautham

2008-02-14 03:42:49

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH 2/2] add rcu_assign_index() if ever needed

On Thu, 14 Feb 2008 09:02:09 +0530 Gautham R Shenoy <[email protected]> wrote:

> > /**
> > + * rcu_assign_index - assign (publicize) a index of a newly
> > + * initialized array elementg that will be dereferenced by RCU
> ^^^^^^^^
>
> I hope Andrew got that one while porting against the latest -mm :)

I don't actually read the comments - I just like to make sure they're
there ;)

2008-02-14 17:08:28

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH 2/2] add rcu_assign_index() if ever needed

On Thu, Feb 14, 2008 at 09:02:09AM +0530, Gautham R Shenoy wrote:
> On Wed, Feb 13, 2008 at 02:05:15PM -0800, Paul E. McKenney wrote:
> > Hello again!
> >
> > This is a speculative patch that as far as I can tell is not yet required.
> > If anyone applies RCU to a data structure allocated out of an array, using
> > array indexes in place of pointers to link the array elements together,
> > then the rcu_assign_index() function in this patch will be needed to
> > assign a given element's array index to the RCU-traversed index. The
> > implementation is exactly that of the old rcu_assign_pointer(), so is
> > extremely well tested.
> >
> > The existing rcu_assign_pointer() will emit a compiler warning in cases
> > where rcu_assign_index() is required.
> >
> > Signed-off-by: Paul E. McKenney <[email protected]>
> > ---
> >
> > rcupdate.h | 18 ++++++++++++++++++
> > 1 file changed, 18 insertions(+)
> >
> > diff -urpNa -X dontdiff linux-2.6.24-rap/include/linux/rcupdate.h linux-2.6.24-rai/include/linux/rcupdate.h
> > --- linux-2.6.24-rap/include/linux/rcupdate.h 2008-02-13 13:36:47.000000000 -0800
> > +++ linux-2.6.24-rai/include/linux/rcupdate.h 2008-02-13 10:55:40.000000000 -0800
> > @@ -286,6 +286,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
> ^^^^^^^^
>
> I hope Andrew got that one while porting against the latest -mm :)
>
> Looks good otherwise.

Good catch!!!

Thanx, Paul

2008-02-14 17:26:43

by Randy Dunlap

[permalink] [raw]
Subject: Re: [PATCH 2/2] add rcu_assign_index() if ever needed

On Wed, 13 Feb 2008 14:05:15 -0800 Paul E. McKenney wrote:

> Hello again!
>
> This is a speculative patch that as far as I can tell is not yet required.
> If anyone applies RCU to a data structure allocated out of an array, using
> array indexes in place of pointers to link the array elements together,
> then the rcu_assign_index() function in this patch will be needed to
> assign a given element's array index to the RCU-traversed index. The
> implementation is exactly that of the old rcu_assign_pointer(), so is
> extremely well tested.
>
> The existing rcu_assign_pointer() will emit a compiler warning in cases
> where rcu_assign_index() is required.
>
> Signed-off-by: Paul E. McKenney <[email protected]>
> ---
>
> rcupdate.h | 18 ++++++++++++++++++
> 1 file changed, 18 insertions(+)
>
> diff -urpNa -X dontdiff linux-2.6.24-rap/include/linux/rcupdate.h linux-2.6.24-rai/include/linux/rcupdate.h
> --- linux-2.6.24-rap/include/linux/rcupdate.h 2008-02-13 13:36:47.000000000 -0800
> +++ linux-2.6.24-rai/include/linux/rcupdate.h 2008-02-13 10:55:40.000000000 -0800
> @@ -286,6 +286,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.
> + */

s/a index/index/

Along with Gautham's typo fix, you could also make this be passable
kernel-doc notation. :)


See Documentation/kernel-doc-nano-HOWTO.txt for details.

Summary:
The function (or macro) name and short description must be on one line.
This is followed by the parameters, then a "blank" (actually " *") line,
then any (longer) description, notes, etc. So basically:

/**
* rcu_assign_index - assign (publicize) index of a newly initialized array element
* @p: description of @p
* @v: description of @v
*
* This function assigns (publicizes) the index of a newly
* initialized array element 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.
> *

---
~Randy

2008-02-14 18:49:27

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH 2/2] add rcu_assign_index() if ever needed

On Thu, Feb 14, 2008 at 09:24:27AM -0800, Randy Dunlap wrote:
> On Wed, 13 Feb 2008 14:05:15 -0800 Paul E. McKenney wrote:
>
> > Hello again!
> >
> > This is a speculative patch that as far as I can tell is not yet required.
> > If anyone applies RCU to a data structure allocated out of an array, using
> > array indexes in place of pointers to link the array elements together,
> > then the rcu_assign_index() function in this patch will be needed to
> > assign a given element's array index to the RCU-traversed index. The
> > implementation is exactly that of the old rcu_assign_pointer(), so is
> > extremely well tested.
> >
> > The existing rcu_assign_pointer() will emit a compiler warning in cases
> > where rcu_assign_index() is required.
> >
> > Signed-off-by: Paul E. McKenney <[email protected]>
> > ---
> >
> > rcupdate.h | 18 ++++++++++++++++++
> > 1 file changed, 18 insertions(+)
> >
> > diff -urpNa -X dontdiff linux-2.6.24-rap/include/linux/rcupdate.h linux-2.6.24-rai/include/linux/rcupdate.h
> > --- linux-2.6.24-rap/include/linux/rcupdate.h 2008-02-13 13:36:47.000000000 -0800
> > +++ linux-2.6.24-rai/include/linux/rcupdate.h 2008-02-13 10:55:40.000000000 -0800
> > @@ -286,6 +286,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.
> > + */
>
> s/a index/index/
>
> Along with Gautham's typo fix, you could also make this be passable
> kernel-doc notation. :)

Guess I should do the same for rcu_assign_pointer() and probably several
others as well... Good catch!!!

Thanx, Paul

> See Documentation/kernel-doc-nano-HOWTO.txt for details.
>
> Summary:
> The function (or macro) name and short description must be on one line.
> This is followed by the parameters, then a "blank" (actually " *") line,
> then any (longer) description, notes, etc. So basically:
>
> /**
> * rcu_assign_index - assign (publicize) index of a newly initialized array element
> * @p: description of @p
> * @v: description of @v
> *
> * This function assigns (publicizes) the index of a newly
> * initialized array element 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.
> > *
>
> ---
> ~Randy

2008-02-16 00:41:46

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

On Wed, 13 Feb 2008 14:00:24 -0800
"Paul E. McKenney" <[email protected]> wrote:

> Hello!
>
> This is an updated version of the patch posted last November:
>
> http://archives.free.net.ph/message/20071201.003721.cd6ff17c.en.html
>
> This new version permits arguments with side effects, for example:
>
> rcu_assign_pointer(global_p, p++);
>
> and also verifies that the arguments are pointers, while still avoiding
> the unnecessary memory barrier when assigning NULL to a pointer.
> This memory-barrier avoidance means that rcu_assign_pointer() is now only
> permitted for pointers (not array indexes), and so this version emits a
> compiler warning if the first argument is not a pointer. I built a "make
> allyesconfig" version on an x86 system, and received no such warnings.
> If RCU is ever applied to array indexes, then the second patch in this
> series should be applied, and the resulting rcu_assign_index() be used.
>
> Given the rather surprising history of subtlely broken implementations of
> rcu_assign_pointer(), I took the precaution of generating a full set of
> test cases and verified that memory barriers and compiler warnings were
> emitted when required. I guess it is the simple things that get you...
>
> Signed-off-by: Paul E. McKenney <[email protected]>
> ---
>
> rcupdate.h | 16 ++++++++++++----
> 1 file changed, 12 insertions(+), 4 deletions(-)
>
> diff -urpNa -X dontdiff linux-2.6.24/include/linux/rcupdate.h linux-2.6.24-rap/include/linux/rcupdate.h
> --- linux-2.6.24/include/linux/rcupdate.h 2008-01-24 14:58:37.000000000 -0800
> +++ linux-2.6.24-rap/include/linux/rcupdate.h 2008-02-13 13:36:47.000000000 -0800
> @@ -270,12 +270,20 @@ extern struct lockdep_map rcu_lock_map;
> * structure after the pointer assignment. More importantly, this
> * call documents which pointers will be dereferenced by RCU read-side
> * code.
> + *
> + * Throws a compiler warning for non-pointer arguments.
> + *
> + * Does not insert a memory barrier for a NULL pointer.
> */
>
> -#define rcu_assign_pointer(p, v) ({ \
> - smp_wmb(); \
> - (p) = (v); \
> - })
> +#define rcu_assign_pointer(p, v) \
> + ({ \
> + typeof(*p) *_________p1 = (v); \
> + \
> + if (!__builtin_constant_p(v) || (_________p1 != NULL)) \
> + smp_wmb(); \
> + (p) = _________p1; \
> + })
>

umm...

net/netfilter/core.c: In function 'nf_register_afinfo':
net/netfilter/core.c:39: warning: initialization discards qualifiers from pointer target type
net/netfilter/nf_log.c: In function 'nf_log_register':
net/netfilter/nf_log.c:37: warning: initialization discards qualifiers from pointer target type
net/netfilter/nf_queue.c: In function 'nf_register_queue_handler':
net/netfilter/nf_queue.c:38: warning: initialization discards qualifiers from pointer target type

2008-02-16 01:24:47

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH 1/2] remove rcu_assign_pointer(NULL) penalty with type/macro safety

On Fri, Feb 15, 2008 at 04:40:24PM -0800, Andrew Morton wrote:
> On Wed, 13 Feb 2008 14:00:24 -0800
> "Paul E. McKenney" <[email protected]> wrote:
>
> > Hello!
> >
> > This is an updated version of the patch posted last November:
> >
> > http://archives.free.net.ph/message/20071201.003721.cd6ff17c.en.html
> >
> > This new version permits arguments with side effects, for example:
> >
> > rcu_assign_pointer(global_p, p++);
> >
> > and also verifies that the arguments are pointers, while still avoiding
> > the unnecessary memory barrier when assigning NULL to a pointer.
> > This memory-barrier avoidance means that rcu_assign_pointer() is now only
> > permitted for pointers (not array indexes), and so this version emits a
> > compiler warning if the first argument is not a pointer. I built a "make
> > allyesconfig" version on an x86 system, and received no such warnings.
> > If RCU is ever applied to array indexes, then the second patch in this
> > series should be applied, and the resulting rcu_assign_index() be used.
> >
> > Given the rather surprising history of subtlely broken implementations of
> > rcu_assign_pointer(), I took the precaution of generating a full set of
> > test cases and verified that memory barriers and compiler warnings were
> > emitted when required. I guess it is the simple things that get you...
> >
> > Signed-off-by: Paul E. McKenney <[email protected]>
> > ---
> >
> > rcupdate.h | 16 ++++++++++++----
> > 1 file changed, 12 insertions(+), 4 deletions(-)
> >
> > diff -urpNa -X dontdiff linux-2.6.24/include/linux/rcupdate.h linux-2.6.24-rap/include/linux/rcupdate.h
> > --- linux-2.6.24/include/linux/rcupdate.h 2008-01-24 14:58:37.000000000 -0800
> > +++ linux-2.6.24-rap/include/linux/rcupdate.h 2008-02-13 13:36:47.000000000 -0800
> > @@ -270,12 +270,20 @@ extern struct lockdep_map rcu_lock_map;
> > * structure after the pointer assignment. More importantly, this
> > * call documents which pointers will be dereferenced by RCU read-side
> > * code.
> > + *
> > + * Throws a compiler warning for non-pointer arguments.
> > + *
> > + * Does not insert a memory barrier for a NULL pointer.
> > */
> >
> > -#define rcu_assign_pointer(p, v) ({ \
> > - smp_wmb(); \
> > - (p) = (v); \
> > - })
> > +#define rcu_assign_pointer(p, v) \
> > + ({ \
> > + typeof(*p) *_________p1 = (v); \
> > + \
> > + if (!__builtin_constant_p(v) || (_________p1 != NULL)) \
> > + smp_wmb(); \
> > + (p) = _________p1; \
> > + })
> >
>
> umm...
>
> net/netfilter/core.c: In function 'nf_register_afinfo':
> net/netfilter/core.c:39: warning: initialization discards qualifiers from pointer target type
> net/netfilter/nf_log.c: In function 'nf_log_register':
> net/netfilter/nf_log.c:37: warning: initialization discards qualifiers from pointer target type
> net/netfilter/nf_queue.c: In function 'nf_register_queue_handler':
> net/netfilter/nf_queue.c:38: warning: initialization discards qualifiers from pointer target type

Hmmm... Netfilter compiles cleanly here. My guess is that your gcc
is more fastidious about const declarations. Could you please either
let me know what arch/gcc-settings you are using, or, alternatively,
see if the following patch fixes things up? The comparison against
NULL should at least emit warnings for non-pointer types -- not as
good as an error, but better than emitting bogus warnings.

So I guess I should stick with simple things like preemptable RCU instead
of the much more difficult task of outsmarting gcc...

Signed-off-by: Paul E. McKenney <[email protected]>
---

rcupdate.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

--- rcupdate.h.old 2008-02-15 17:18:50.000000000 -0800
+++ rcupdate.h 2008-02-15 17:18:52.000000000 -0800
@@ -176,7 +176,7 @@ struct rcu_head {

#define rcu_assign_pointer(p, v) \
({ \
- typeof(*p) *_________p1 = (v); \
+ typeof(p) _________p1 = (v); \
\
if (!__builtin_constant_p(v) || ((_________p1) != NULL)) \
smp_wmb(); \