Subject: Re: [PATCH net-next-2.6] net: inet_add_protocol() can use cmpxchg()

From: Jarek Poplawski <[email-masked]>
Date: Thu, 9 Sep 2010 17:38:07 +0200

On Thu, Sep 09, 2010 at 02:57:38PM +0200, Eric Dumazet wrote:
> Le jeudi 09 septembre 2010 ?? 09:59 +0000, Jarek Poplawski a écrit :
> 
> > Btw, I wonder if for readability and debuging it shouldn't be made
> > generic as rcu_cmpxchg_pointer() etc.?
> 
> There are a lot of things to do in this area (with proper __rcu sparse
> annotations, this can be not very readable ...)

Well, this is more than I could dream ;-) The main point to me was
autodocumentation in use, similarly to rcu_assign_pointer(). But, of
course, with the description below it's much better.

Thanks,
Jarek P.

> 
> /**
>  * rcu_cmpxchg - atomic compare and exchange, SMP & rcu safe
>  * @p: pointer to value
>  * @old: old value
>  * @new: new value
>  *
>  * Equivalent to :
>  *    ATOMIC_BEGIN
>  *     ret = *p;
>  *     if (ret == old)
>  *             rcu_assign_pointer(*p, new);
>  *    ATOMIC_END
>  *    return ret;
>  *
>  * cmpxpchg() contains full memory barriers, so can be used
>  * in rcu write side without additional smp_wmb() barrier
>  */
> #define rcu_cmpxchg(p, old, new) cmpxchg(p, old, new)
> 
> 
> const struct net_protocol __rcu *
>         inet_protos[MAX_INET_PROTOS] __read_mostly;
> 
> ...
> 
> return !rcu_cmpxchg(&inet_protos[hash], 
>                     NULL,
>                     (const struct net_protocol __force __rcu *)prot) ?
> 		0 : -1;
> 
> ...
>