Re: [PATCH 6/6] Documentation/memory-barriers.txt: Clarify cmpxchg()

From: Waiman Long
Date: Tue Oct 15 2019 - 16:31:08 EST


On 10/14/19 1:49 PM, Manfred Spraul wrote:
> Hello Peter,
>
> On 10/14/19 3:03 PM, Peter Zijlstra wrote:
>> On Sat, Oct 12, 2019 at 07:49:58AM +0200, Manfred Spraul wrote:
>>> The documentation in memory-barriers.txt claims that
>>> smp_mb__{before,after}_atomic() are for atomic ops that do not return a
>>> value.
>>>
>>> This is misleading and doesn't match the example in atomic_t.txt,
>>> and e.g. smp_mb__before_atomic() may and is used together with
>>> cmpxchg_relaxed() in the wake_q code.
>>>
>>> The purpose of e.g. smp_mb__before_atomic() is to "upgrade" a following
>>> RMW atomic operation to a full memory barrier.
>>> The return code of the atomic operation has no impact, so all of the
>>> following examples are valid:
>> The value return of atomic ops is relevant in so far that
>> (traditionally) all value returning atomic ops already implied full
>> barriers. That of course changed when we added
>> _release/_acquire/_relaxed variants.
> I've updated the Change description accordingly
>>> 1)
>>> ÂÂÂÂsmp_mb__before_atomic();
>>> ÂÂÂÂatomic_add();
>>>
>>> 2)
>>> ÂÂÂÂsmp_mb__before_atomic();
>>> ÂÂÂÂatomic_xchg_relaxed();
>>>
>>> 3)
>>> ÂÂÂÂsmp_mb__before_atomic();
>>> ÂÂÂÂatomic_fetch_add_relaxed();
>>>
>>> Invalid would be:
>>> ÂÂÂÂsmp_mb__before_atomic();
>>> ÂÂÂÂatomic_set();
>>>
>>> Signed-off-by: Manfred Spraul <manfred@xxxxxxxxxxxxxxxx>
>>> Cc: Waiman Long <longman@xxxxxxxxxx>
>>> Cc: Davidlohr Bueso <dave@xxxxxxxxxxxx>
>>> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
>>> ---
>>> Â Documentation/memory-barriers.txt | 11 ++++++-----
>>> Â 1 file changed, 6 insertions(+), 5 deletions(-)
>>>
>>> diff --git a/Documentation/memory-barriers.txt
>>> b/Documentation/memory-barriers.txt
>>> index 1adbb8a371c7..52076b057400 100644
>>> --- a/Documentation/memory-barriers.txt
>>> +++ b/Documentation/memory-barriers.txt
>>> @@ -1873,12 +1873,13 @@ There are some more advanced barrier functions:
>>> ÂÂ (*) smp_mb__before_atomic();
>>> ÂÂ (*) smp_mb__after_atomic();
>>> Â -ÂÂÂÂ These are for use with atomic (such as add, subtract,
>>> increment and
>>> -ÂÂÂÂ decrement) functions that don't return a value, especially
>>> when used for
>>> - reference counting. These functions do not imply memory
>>> barriers.
>>> +ÂÂÂÂ These are for use with atomic RMW functions (such as add,
>>> subtract,
>>> +ÂÂÂÂ increment, decrement, failed conditional operations, ...) that do
>>> +ÂÂÂÂ not imply memory barriers, but where the code needs a memory
>>> barrier,
>>> +ÂÂÂÂ for example when used for reference counting.
>>> Â -ÂÂÂÂ These are also used for atomic bitop functions that do not
>>> return a
>>> -ÂÂÂÂ value (such as set_bit and clear_bit).
>>> +ÂÂÂÂ These are also used for atomic RMW bitop functions that do
>>> imply a full
>> s/do/do not/ ?
> Sorry, yes, of course

I was wondering the same thing. With the revised patch,

Acked-by: Waiman Long <longman@xxxxxxxxxx>

>>> +ÂÂÂÂ memory barrier (such as set_bit and clear_bit).
>
>