All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <a.p.zijlstra@chello.nl>
To: Ming Lei <tom.leiming@gmail.com>
Cc: 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,
	Oliver Neukum <oneukum@suse.de>
Subject: Re: [PATCH 3/3] kref: Remove the memory barriers
Date: Sun, 11 Dec 2011 13:47:07 +0100	[thread overview]
Message-ID: <1323607627.16764.13.camel@twins> (raw)
In-Reply-To: <CACVXFVNAAXAWtTncahb=sADHxmZOmdDOJA1FUX8Qt4ZR3To_qg@mail.gmail.com>

On Sun, 2011-12-11 at 10:22 +0800, Ming Lei wrote:
> On Sun, Dec 11, 2011 at 3:49 AM, Peter Zijlstra <a.p.zijlstra@chello.nl> wrote:
> > On Sat, 2011-12-10 at 23:57 +0800, Ming Lei wrote:
> >
> >> CPU0                  CPU1
> >>
> >> atomic_set(v)
> >> smp_mb()
> >>                               smp_mb()
> >>                               atomic_dec_and_test(v)
> >>
> >> Without the barrier after atomic_set, CPU1 may see a stale
> >> value of v first, then decrease it, so may miss a release operation.
> >
> > Your example is doubly broken. If there's concurrency possible with
> > atomic_set() you've lost.
> 
> kref_init is guaranteed to be run only one time __before__ executing
> kref_get/kref_put.

If used properly, yes. But in that case you still don't need the
barrier. Whatever means you use to make the object visible to other CPUs
will include a barrier.

> > Lets change it to kref_get() aka atomic_inc():
> >
> >        CPU0            CPU1
> >
> >        atomic_inc()
> >                        atomic_dec_and_test()
> >
> > and
> >
> >                        atomic_dec_and_test()
> >        atomic_inc()
> >
> > For if the first is possible, then so is the second.
> 
> Yes, both are reasonable.
> 
> >
> > This illustrates that no matter how many barriers you put in, you're
> > still up shit creek without no paddle because the kref_put() can come in
> > before you do the kref_get(), making the kref_get() the invalid
> > operation.
> 
> So one smp_mb__before_atomic_inc should be added before atomic_inc
> to make sure that CPU0 can see the uptodate ref, right?

No.

Assume v == 1:

	CPU0		CPU1

			atomic_dec_and_test(); /* --v == 0 */
				kfree()

	smp_mb__before_atomic_inc()
	atomic_inc(); <-- OOPS!


You still got an access to already freed memory. There is no amount of
memory barriers that will solve this problem.

> But the initial value of kref is 1, so seems we don't need to consider
> the 0-refs.

There's a dec in there, isn't it. How much is 1-1?



  reply	other threads:[~2011-12-11 12:48 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 [this message]
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
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=1323607627.16764.13.camel@twins \
    --to=a.p.zijlstra@chello.nl \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=eric.dumazet@gmail.com \
    --cc=gregkh@suse.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=oneukum@suse.de \
    --cc=ostrikov@nvidia.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.