Remove rcu_assign_pointer() penalty for NULL pointers
Subsystems (4)
PATCH 1/1 Remove rcu_assign_pointer() penalty for NULL pointers
Hello!
The rcu_assign_pointer() primitive currently unconditionally executes a memory barrier, even when a NULL pointer is being assigned. This has lead some to avoid using rcu_assign_pointer() for NULL pointers, which loses the self-documenting advantages of rcu_assign_pointer() This patch uses __builtin_const_p() to omit needless memory barriers for NULL-pointer assignments at compile time with no runtime penalty, as discussed in the following thread:
http://www.mail-archive.com/[email protected]/msg54852.html
Tested on x86_64 and ppc64, also compiled the four cases (NULL/non-NULL and const/non-const) with gcc version 4.1.2, and hand-checked the assembly output.
---
rcupdate.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
include/linux/rcupdate.h +7 -4
@@ -191,10 +191,13 @@ static inline void rcu_preempt_boost(voi* code. */ -#define rcu_assign_pointer (p, v) ({ \ - smp_wmb(); \ - (p) = (v); \ - }) +#define rcu_assign_pointer (p, v) \ + ({ \ + if (!__builtin_constant_p(v) || \ + ((v) != NULL)) \ + smp_wmb(); \ + (p) = (v); \ + })/** * synchronize_sched - block until all CPUs have exited any non-preemptive
Quoted 9 lines · Paul E. McKenney
The
rcu_assign_pointer()primitive currently unconditionally executes a memory barrier, even when a NULL pointer is being assigned. This has lead some to avoid usingrcu_assign_pointer()for NULL pointers, which loses the self-documenting advantages ofrcu_assign_pointer()This patch uses__builtin_const_p()to omit needless memory barriers for NULL-pointer assignments at compile time with no runtime penalty, as discussed in the following thread:
Quoted 7 lines
http://www.mail-archive.com/[email protected]/msg54852.htmlTested on
x86_64and ppc64, also compiled the four cases (NULL/non-NULL and const/non-const) with gcc version 4.1.2, and hand-checked the assembly output.Signed-off-by Paul E. McKenney <[email protected]>
Thanks a lot for following through with this Paul!
Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txtQuoted 16 lines · Herbert Xu
Quoted 15 lines · Paul E. McKenney
The
rcu_assign_pointer()primitive currently unconditionally executes a memory barrier, even when a NULL pointer is being assigned. This has lead some to avoid usingrcu_assign_pointer()for NULL pointers, which loses the self-documenting advantages ofrcu_assign_pointer()This patch uses__builtin_const_p()to omit needless memory barriers for NULL-pointer assignments at compile time with no runtime penalty, as discussed in the following thread:http://www.mail-archive.com/[email protected]/msg54852.htmlTested on
x86_64and ppc64, also compiled the four cases (NULL/non-NULL and const/non-const) with gcc version 4.1.2, and hand-checked the assembly output.
Quoted 5 lines
Quoted 1 line
Signed-off-by Paul E. McKenney <[email protected]>Acked-by Herbert Xu <[email protected]>Thanks a lot for following through with this Paul!
No problem -- after all, it is not every day that one gets the opportunity to make a simple change that speeds things up and makes kernel hackers lives a bit simpler. ;-)
Thanx, Paul