From: ebiederm@xmission.com (Eric W. Biederman)
To: Andrew Morton <akpm@osdl.org>
Cc: oleg@tv-sign.ru, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] pidhash: Refactor the pid hash table.
Date: Fri, 10 Mar 2006 22:39:12 -0700 [thread overview]
Message-ID: <m1oe0dh75r.fsf@ebiederm.dsl.xmission.com> (raw)
In-Reply-To: <20060310154524.3c293b8f.akpm@osdl.org> (Andrew Morton's message of "Fri, 10 Mar 2006 15:45:24 -0800")
Andrew Morton <akpm@osdl.org> writes:
> ebiederm@xmission.com (Eric W. Biederman) wrote:
>>
>> +fastcall void put_pid(struct pid *pid)
>> +{
>> + if (!pid)
>> + return;
>> + if ((atomic_read(&pid->count) == 1) ||
>> + atomic_dec_and_test(&pid->count))
>> + kmem_cache_free(pid_cachep, pid);
>> +}
>
> This looks odd. It's an RCU callback so it's asynchronous. It doesn't
> take any locks, so if anyone else can have a ref on this thing then the
> refcount can change at any time.
>
> And both sides of the || are basically equivalent. Perhaps you meant &&.
> But I'm more worried by the apparent raciness?
The || was deliberate.
I expect the count to usually be 1. So the atomic_read optimizes out
the atomic operation in the common case. skb_put and kref_put do it
for the same reason.
As for the raciness, that is an interesting case.
Because the decrement is in the rcu callback the count of the structure
is guaranteed to be at least one the entire time the structure is reachable
in an rcu safe manner. So during that interval the count can go up or down,
safely and atomically, and it won't reach zero.
After the structure stops being reachable in an rcu protected manner
through the hash table it devolves into a simple reference counted
structure. Now unless something like /proc captured this pid it will
have a count of exactly one, and I expect this to be relatively
common.
So if pid->count == 1 it means I own the only reference, and can do
what I please with this structure.
If pid->count > 1 then I don't own the only reference and someone
may be racing with me. In that case I do need to do the full
atomic_dec_and_test to see if I have the very last reference to
this structure.
I wasn't certain which part of this you were confused about so I tried
to cover all of my bases. Hopefully I have cleared up your confusion.
Eric
prev parent reply other threads:[~2006-03-11 5:39 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-03-10 17:14 Eric W. Biederman
2006-03-10 23:45 ` Andrew Morton
2006-03-11 5:39 ` Eric W. Biederman [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m1oe0dh75r.fsf@ebiederm.dsl.xmission.com \
--to=ebiederm@xmission.com \
--cc=akpm@osdl.org \
--cc=linux-kernel@vger.kernel.org \
--cc=oleg@tv-sign.ru \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®