From: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
To: Oleg Nesterov <oleg@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>,
Ming Lei <tom.leiming@gmail.com>, Shaohua Li <shli@kernel.org>,
Al Viro <viro@zeniv.linux.org.uk>,
Andrew Morton <akpm@linux-foundation.org>,
linux-kernel@vger.kernel.org
Subject: Re: + atomic-improve-atomic_inc_unless_negative-atomic_dec_unless_positive .patch added to -mm tree
Date: Thu, 21 Mar 2013 10:34:54 -0700 [thread overview]
Message-ID: <20130321173454.GZ3637@linux.vnet.ibm.com> (raw)
In-Reply-To: <20130321170827.GA23539@redhat.com>
On Thu, Mar 21, 2013 at 06:08:27PM +0100, Oleg Nesterov wrote:
> On 03/17, Paul E. McKenney wrote:
> >
> > On Sat, Mar 16, 2013 at 07:30:22PM +0100, Oleg Nesterov wrote:
> > >
> > > > The rule is that if an atomic primitive returns non-void, then there is
> > > > a full memory barrier before and after.
> > >
> > > This case is documented...
> > >
> > > > This applies to primitives
> > > > returning boolean as well, with atomic_dec_and_test() setting this
> > > > precedent from what I can see.
> > >
> > > I don't think this is the "fair" comparison. Unlike atomic_add_unless(),
> > > atomic_dec_and_test() always changes the memory even if it "fails".
> > >
> > > If atomic_add_unless() returns 0, nothing was changed and if we add
> > > the barrier it is not clear what it should be paired with.
> > >
> > > But OK. I have to agree that "keep the rules simple" makes sense, so
> > > we should change atomic_add_unless() as well.
> >
> > Agreed!
>
> OK... since nobody volunteered to make a patch, what do you think about
> the change below?
>
> It should "fix" atomic_add_unless() (only on x86) and optimize
> atomic_inc/dec_unless.
>
> With this change atomic_*_unless() can do the unnecessary mb() after
> cmpxchg() fails, but I think this case is very unlikely.
>
> And, in the likely case atomic_inc/dec_unless avoids the 1st cmpxchg()
> which in most cases just reads the memory for the next cmpxchg().
Thank you, Oleg!
Reviewed-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
> Oleg.
>
> --- x/arch/x86/include/asm/atomic.h
> +++ x/arch/x86/include/asm/atomic.h
> @@ -212,15 +212,12 @@ static inline int atomic_xchg(atomic_t *
> static inline int __atomic_add_unless(atomic_t *v, int a, int u)
> {
> int c, old;
> - c = atomic_read(v);
> - for (;;) {
> - if (unlikely(c == (u)))
> - break;
> - old = atomic_cmpxchg((v), c, c + (a));
> + for (c = atomic_read(v); c != u; c = old) {
> + old = atomic_cmpxchg(v, c, c + a);
> if (likely(old == c))
> - break;
> - c = old;
> + return c;
> }
> + smp_mb();
> return c;
> }
>
> --- x/include/linux/atomic.h
> +++ x/include/linux/atomic.h
> @@ -64,11 +64,12 @@ static inline int atomic_inc_not_zero_hi
> static inline int atomic_inc_unless_negative(atomic_t *p)
> {
> int v, v1;
> - for (v = 0; v >= 0; v = v1) {
> + for (v = atomic_read(p); v >= 0; v = v1) {
> v1 = atomic_cmpxchg(p, v, v + 1);
> if (likely(v1 == v))
> return 1;
> }
> + smp_mb();
> return 0;
> }
> #endif
> @@ -77,11 +78,12 @@ static inline int atomic_inc_unless_nega
> static inline int atomic_dec_unless_positive(atomic_t *p)
> {
> int v, v1;
> - for (v = 0; v <= 0; v = v1) {
> + for (atomic_read(p); v <= 0; v = v1) {
> v1 = atomic_cmpxchg(p, v, v - 1);
> if (likely(v1 == v))
> return 1;
> }
> + smp_mb();
> return 0;
> }
> #endif
>
next prev parent reply other threads:[~2013-03-21 17:35 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-03-14 16:24 + atomic-improve-atomic_inc_unless_negative-atomic_dec_unless_positive .patch added to -mm tree Oleg Nesterov
2013-03-15 3:46 ` Ming Lei
2013-03-15 13:46 ` Oleg Nesterov
2013-03-15 15:13 ` Ming Lei
2013-03-15 16:51 ` Oleg Nesterov
2013-03-15 17:23 ` Frederic Weisbecker
2013-03-15 17:51 ` Oleg Nesterov
2013-03-15 18:34 ` Frederic Weisbecker
2013-03-15 20:17 ` Paul E. McKenney
2013-03-16 18:30 ` Oleg Nesterov
2013-03-17 17:26 ` Paul E. McKenney
2013-03-21 17:08 ` Oleg Nesterov
2013-03-21 17:34 ` Paul E. McKenney [this message]
2013-03-21 18:03 ` Eric Dumazet
2013-03-21 18:30 ` Oleg Nesterov
2013-03-21 22:56 ` Eric Dumazet
2013-03-22 12:59 ` Oleg Nesterov
2013-03-22 16:34 ` Paul E. McKenney
2013-03-16 18:19 ` Oleg Nesterov
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=20130321173454.GZ3637@linux.vnet.ibm.com \
--to=paulmck@linux.vnet.ibm.com \
--cc=akpm@linux-foundation.org \
--cc=fweisbec@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=oleg@redhat.com \
--cc=shli@kernel.org \
--cc=tom.leiming@gmail.com \
--cc=viro@zeniv.linux.org.uk \
/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.