From: Oliver Neukum <oneukum@suse.de>
To: Greg KH <greg@kroah.com>
Cc: Ming Lei <tom.leiming@gmail.com>,
Peter Zijlstra <peterz@infradead.org>,
gregkh@suse.de, akpm@linux-foundation.org,
linux-kernel@vger.kernel.org, ostrikov@nvidia.com,
adobriyan@gmail.com, eric.dumazet@gmail.com, mingo@elte.hu
Subject: Re: [PATCH 3/3] kref: Remove the memory barriers
Date: Tue, 13 Dec 2011 12:51:25 +0100 [thread overview]
Message-ID: <201112131251.25451.oneukum@suse.de> (raw)
In-Reply-To: <20111212231419.GA11089@kroah.com>
Am Dienstag, 13. Dezember 2011, 00:14:19 schrieb Greg KH:
> > I guess I worried not about the increment, but the decrement.
> > Which makes me wonder what happens if you don't intend
> > to get the kref again, but need to make sure it is usually freed,
> > like:
> >
> > CPU A CPU B
> >
> > kref_get(p)
> > start_io(p)
> > [interrupt from IO]
> > kref_put(p)
> >
> > You need an ordering primitive between start_io() and kref_get()
> > or the counter could go negative.
>
> Really? On an atomic variable? I didn't think this was needed for
> atomics to ensure this type of thing couldn't happen.
If you use an atomic variable you can be sure that the result will be
mathematically correct, even if you touch the variable from many CPUs.
(with add & sub of course) That is, refering to that variable.
It does not guarantee ordering
CPU A CPU B
atomic_set(&a, 1);
atomic_set(&b, 1);
atomic_set(&c, 1);
while (!atomic_read(&c));
d = atomic_read(&a) + atomic_read(&b);
is asking for trouble. You need to do:
CPU A CPU B
atomic_set(&a, 1);
atomic_set(&b, 1);
smp_wmb();
atomic_set(&c, 1);
while (!atomic_read(&c));
smp_rmb();
d = atomic_read(&a) + atomic_read(&b);
Now replace c with an interrupt and you see the problem. It definitely exists,
but my solution was quite bad. The wmb() must be in start_io() in the first example
I gave. Putting it into kref was the wrong place.
Regards
Oliver
PS: even in the example I first gave the result will eventually be 0.
But that is useless because the check for zero is done only in kref_put()
--
- - -
SUSE LINUX Products GmbH, GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer, HRB 16746 (AG Nürnberg)
Maxfeldstraße 5
90409 Nürnberg
Germany
- - -
next prev parent reply other threads:[~2011-12-13 11:49 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-12-10 10:43 [PATCH 0/3] kref: inline and barriers Peter Zijlstra
2011-12-10 10:43 ` [PATCH 1/3] kref: Inline all functions Peter Zijlstra
2011-12-10 14:32 ` Ming Lei
2011-12-10 14:59 ` Peter Zijlstra
2011-12-12 22:11 ` Greg KH
2011-12-13 9:36 ` Peter Zijlstra
2011-12-13 17:15 ` Greg KH
2011-12-13 18:52 ` Peter Zijlstra
2011-12-13 19:11 ` Greg KH
2011-12-13 19:36 ` Peter Zijlstra
2011-12-10 10:43 ` [PATCH 2/3] kref: Implement kref_put in terms of kref_sub Peter Zijlstra
2011-12-10 10:43 ` [PATCH 3/3] kref: Remove the memory barriers Peter Zijlstra
2011-12-10 14:07 ` Ming Lei
2011-12-10 14:58 ` Peter Zijlstra
2011-12-10 15:57 ` Ming Lei
2011-12-10 19:49 ` Peter Zijlstra
2011-12-11 2:22 ` Ming Lei
2011-12-11 12:47 ` Peter Zijlstra
2011-12-11 12:59 ` Ming Lei
2011-12-11 15:35 ` Peter Zijlstra
2011-12-11 20:42 ` Peter Zijlstra
2011-12-12 3:48 ` Ming Lei
2011-12-12 8:54 ` Peter Zijlstra
2011-12-12 9:57 ` Ming Lei
2011-12-12 10:12 ` Peter Zijlstra
2011-12-12 10:32 ` Ming Lei
2011-12-12 11:05 ` Peter Zijlstra
2011-12-12 11:19 ` Ming Lei
2011-12-12 11:13 ` Eric Dumazet
2011-12-12 11:15 ` Oliver Neukum
2011-12-12 10:20 ` Oliver Neukum
2011-12-12 19:30 ` Greg KH
2011-12-12 22:56 ` Oliver Neukum
2011-12-12 23:14 ` Greg KH
2011-12-13 11:51 ` Oliver Neukum [this message]
2011-12-13 9:12 ` Peter Zijlstra
2011-12-13 9:49 ` Oliver Neukum
2011-12-12 8:55 ` Peter Zijlstra
2011-12-12 15:24 ` Greg KH
2011-12-12 8:56 ` Peter Zijlstra
2011-12-12 10:10 ` Ming Lei
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=201112131251.25451.oneukum@suse.de \
--to=oneukum@suse.de \
--cc=adobriyan@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=eric.dumazet@gmail.com \
--cc=greg@kroah.com \
--cc=gregkh@suse.de \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
--cc=ostrikov@nvidia.com \
--cc=peterz@infradead.org \
--cc=tom.leiming@gmail.com \
/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