Subject: Re: [PATCH] inetpeer: optimizations

From: Jarek Poplawski <[email-masked]>
Date: Sun, 6 Dec 2009 19:22:10 +0100

On Sun, Dec 06, 2009 at 09:47:03AM +0100, Eric Dumazet wrote:
> Jarek Poplawski a écrit :
> > Eric Dumazet wrote, On 12/05/2009 01:11 PM:
> > 
> >> - Use atomic_dec_and_test() in inet_putpeer()
> > 
> > atomic_dec_and_lock()?
> 
> Yes :)
> 
> > 
> >>   This takes/dirties the lock only if necessary.
> > ...
> > 
> >>  void inet_putpeer(struct inet_peer *p)
> >>  {
> >> -	spin_lock_bh(&inet_peer_unused_lock);
> >> -	if (atomic_dec_and_test(&p->refcnt)) {
> >> -		list_add_tail(&p->unused, &unused_peers);
> >> +	local_bh_disable();
> >> +	if (atomic_dec_and_lock(&p->refcnt, &unused_peers.lock)) {
> > 
> > Why not:
> > 	if (atomic_dec_and_test(&p->refcnt)) {
> > 		spin_lock_bh(&inet_peer_unused_lock);
> > 		...
> 
> Because we have to take the lock before doing the final 1 -> 0 refcount transition.
> 
> (Another thread could do the 0 -> 1 transition)

AFAICS this lock here can only to prevent double linking to the
unused_peers list during such transitions. If so, it could be replaced
with the list_empty(&p->unused) test before list_add_tail(), and
atomic_dec_test() without the lock would be enough (unless I miss
something ;-).

Jarek P.