From: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
To: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Oleg Nesterov <oleg@redhat.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: Fri, 15 Mar 2013 13:17:39 -0700 [thread overview]
Message-ID: <20130315201739.GK3656@linux.vnet.ibm.com> (raw)
In-Reply-To: <CAFTL4hwZQcN_sO0SN8bJqEsQdC8ArDvJhFPg62Kh8OQyUe=MUw@mail.gmail.com>
On Fri, Mar 15, 2013 at 07:34:32PM +0100, Frederic Weisbecker wrote:
> 2013/3/15 Oleg Nesterov <oleg@redhat.com>:
> > On 03/15, Frederic Weisbecker wrote:
> >>
> >> > The lack of the barrier?
> >> >
> >> > I thought about this, this should be fine? atomic_add_unless() has the same
> >> > "problem", but this is documented in atomic_ops.txt:
> >> >
> >> > atomic_add_unless requires explicit memory barriers around the operation
> >> > unless it fails (returns 0).
> >> >
> >> > I thought that atomic_add_unless_negative() should have the same
> >> > guarantees?
> >>
> >> I feel very uncomfortable with that. The memory barrier is needed
> >> anyway to make sure we don't deal with a stale value of the atomic val
> >> (wrt. ordering against another object).
> >> The following should really be expected to work without added barrier:
> >>
> >> void put_object(foo *obj)
> >> {
> >> if (atomic_dec_return(obj->ref) == -1)
> >> free_rcu(obj);
> >> }
> >>
> >> bool try_get_object(foo *obj)
> >> {
> >> if (atomic_add_unless_negative(obj, 1))
> >> return true;
> >> return false;
> >> }
> >>
> >> = CPU 0 = = CPU 1
> >> rcu_read_lock()
> >> put_object(obj0);
> >> obj = rcu_derefr(obj0);
> >> rcu_assign_ptr(obj0, NULL);
> >
> > (I guess you meant rcu_assign_ptr() then put_object())
>
> Right.
>
> >
> >> if (try_get_object(obj))
> >> do_something...
> >> else
> >> object is dying
> >> rcu_read_unlock()
> >
> > I must have missed something.
> >
> > do_something() looks fine, if atomic_add_unless_negative() succeeds
> > we do have a barrier?
>
> Ok, I guess the guarantee of a barrier in case of failure is probably
> not needed. But since the only way to safely read the atomic value is
> a cmpxchg like operation, I guess a barrier must be involved in any
> case.
>
> Using atomic_read() may return some stale value.
>
> >
> > Anyway, I understand that it is possible to write the code which
> > won't work without the uncoditional mb().
>
> Yeah that's my fear.
>
> >
> > My point was: should we fix atomic_add_unless() then? If not, why
> > should atomic_add_unless_negative() differ?
>
> They shouldn't differ I guess.
Completely agreed. It is not like memory ordering is simple, so we should
keep the rules simple. Atomic primitives that sometimes imply a memory
barrier seems a bit over the top.
The rule is that if an atomic primitive returns non-void, then there is
a full memory barrier before and after. This applies to primitives
returning boolean as well, with atomic_dec_and_test() setting this
precedent from what I can see.
Thanx, Paul
next prev parent reply other threads:[~2013-03-15 20:17 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 [this message]
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
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=20130315201739.GK3656@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).