Messages in this thread | | | Date | Fri, 15 Mar 2013 17:51:31 +0100 | From | Oleg Nesterov <> | Subject | Re: + atomic-improve-atomic_inc_unless_negative-atomic_dec_unless_positive .patch added to -mm tree |
| |
On 03/15, Ming Lei wrote: > > On Fri, Mar 15, 2013 at 9:46 PM, Oleg Nesterov <oleg@redhat.com> wrote: > > On 03/15, Ming Lei wrote: > >> > >> On Fri, Mar 15, 2013 at 12:24 AM, Oleg Nesterov <oleg@redhat.com> wrote: > >> > 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); > >> > >> Unfortunately, the above will exchange the current value even though > >> it is negative, so it isn't correct. > > > > Hmm, why? We always check "v >= 0" before we try to do > > atomic_cmpxchg(old => v) ? > > Sorry, yes, you are right. But then your patch is basically same with the > previous one, isn't it?
Sure, the logic is the same, just the patch (and the code) looks simpler and more understandable.
> And has same problem, see below discussion: > > http://marc.info/?t=136284366900001&r=1&w=2
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?
Paul? Frederic?
Oleg.
| |