From: Hou Tao <houtao@huaweicloud.com>
To: paulmck@kernel.org
Cc: bpf@vger.kernel.org, David Vernet <void@manifault.com>,
Andrii Nakryiko <andrii@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
Jiri Olsa <jolsa@kernel.org>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH bpf] Fold smp_mb__before_atomic() into atomic_set_release()
Date: Thu, 19 Oct 2023 14:20:35 +0800 [thread overview]
Message-ID: <7fe984d2-c30c-40ad-83cd-d9fb51b6ce0d@huaweicloud.com> (raw)
In-Reply-To: <f6526ae6-cd52-4d1d-ab2a-7d82e2c818fd@paulmck-laptop>
Hi Paul,
On 10/19/2023 12:54 PM, Paul E. McKenney wrote:
> On Thu, Oct 19, 2023 at 09:07:07AM +0800, Hou Tao wrote:
>> Hi Paul,
>>
>> On 10/19/2023 6:28 AM, Paul E. McKenney wrote:
>>> bpf: Fold smp_mb__before_atomic() into atomic_set_release()
>>>
>>> The bpf_user_ringbuf_drain() BPF_CALL function uses an atomic_set()
>>> immediately preceded by smp_mb__before_atomic() so as to order storing
>>> of ring-buffer consumer and producer positions prior to the atomic_set()
>>> call's clearing of the ->busy flag, as follows:
>>>
>>> smp_mb__before_atomic();
>>> atomic_set(&rb->busy, 0);
>>>
>>> Although this works given current architectures and implementations, and
>>> given that this only needs to order prior writes against a later write.
>>> However, it does so by accident because the smp_mb__before_atomic()
>>> is only guaranteed to work with read-modify-write atomic operations,
>>> and not at all with things like atomic_set() and atomic_read().
>>>
>>> Note especially that smp_mb__before_atomic() will not, repeat *not*,
>>> order the prior write to "a" before the subsequent non-read-modify-write
>>> atomic read from "b", even on strongly ordered systems such as x86:
>>>
>>> WRITE_ONCE(a, 1);
>>> smp_mb__before_atomic();
>>> r1 = atomic_read(&b);
>> The reason is smp_mb__before_atomic() is defined as noop and
>> atomic_read() in x86-64 is just READ_ONCE(), right ?
> The real reason is that smp_mb__before_atomic() is not defined to do
> anything unless followed by an atomic read-modify-write operation,
> and atomic_read(), atomic_64read(), atomic_set(), and so on are not
> read-modify-write operations.
I see. Thanks for explanation. It seems I did not read
Documentation/atomic_t.txt carefully, it said:
The barriers:
smp_mb__{before,after}_atomic()
only apply to the RMW atomic ops and can be used to augment/upgrade the
ordering inherent to the op.
>
> As you point out, one implementation consequence of this is that
> smp_mb__before_atomic() is nothingness on x86.
>
>> And it seems that I also used smp_mb__before_atomic() in a wrong way for
>> patch [1]. The memory order in the posted patch is
>>
>> process X process Y
>> atomic64_dec_and_test(&map->usercnt)
>> READ_ONCE(timer->timer)
>> timer->time = t
> The above two lines are supposed to be accessing the same field, correct?
> If so, process Y's store really should be WRITE_ONCE().
Yes. These two processes are accessing the same field (namely
timer->timer). Is WRITE_ONCE(xx) still necessary when the write of
timer->time in process Y is protected by a spin-lock ?
>
>> // it won't work
>> smp_mb__before_atomic()
>> atomic64_read(&map->usercnt)
>>
>> For the problem, it seems I need to replace smp_mb__before_atomic() by
>> smp_mb() to fix the memory order, right ?
> Yes, because smp_mb() will order the prior store against that later load.
Thanks. Will fix the patch.
Regards,
Hou
>
> Thanx, Paul
>
>> Regards,
>> Hou
>>
>> [1]:
>> https://lore.kernel.org/bpf/20231017125717.241101-2-houtao@huaweicloud.com/
>>
>>
>>> Therefore, replace the smp_mb__before_atomic() and atomic_set() with
>>> atomic_set_release() as follows:
>>>
>>> atomic_set_release(&rb->busy, 0);
>>>
>>> This is no slower (and sometimes is faster) than the original, and also
>>> provides a formal guarantee of ordering that the original lacks.
>>>
>>> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
>>> Acked-by: David Vernet <void@manifault.com>
>>> Cc: Andrii Nakryiko <andrii@kernel.org>
>>> Cc: Alexei Starovoitov <ast@kernel.org>
>>> Cc: Daniel Borkmann <daniel@iogearbox.net>
>>> Cc: Martin KaFai Lau <martin.lau@linux.dev>
>>> Cc: Song Liu <song@kernel.org>
>>> Cc: Yonghong Song <yonghong.song@linux.dev>
>>> Cc: John Fastabend <john.fastabend@gmail.com>
>>> Cc: KP Singh <kpsingh@kernel.org>
>>> Cc: Stanislav Fomichev <sdf@google.com>
>>> Cc: Hao Luo <haoluo@google.com>
>>> Cc: Jiri Olsa <jolsa@kernel.org>
>>> Cc: <bpf@vger.kernel.org>
>>>
>>> diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c
>>> index f045fde632e5..0ee653a936ea 100644
>>> --- a/kernel/bpf/ringbuf.c
>>> +++ b/kernel/bpf/ringbuf.c
>>> @@ -770,8 +770,7 @@ BPF_CALL_4(bpf_user_ringbuf_drain, struct bpf_map *, map,
>>> /* Prevent the clearing of the busy-bit from being reordered before the
>>> * storing of any rb consumer or producer positions.
>>> */
>>> - smp_mb__before_atomic();
>>> - atomic_set(&rb->busy, 0);
>>> + atomic_set_release(&rb->busy, 0);
>>>
>>> if (flags & BPF_RB_FORCE_WAKEUP)
>>> irq_work_queue(&rb->work);
>>>
>>> .
next prev parent reply other threads:[~2023-10-19 6:20 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-18 22:28 [PATCH bpf] Fold smp_mb__before_atomic() into atomic_set_release() Paul E. McKenney
2023-10-19 1:07 ` Hou Tao
2023-10-19 4:54 ` Paul E. McKenney
2023-10-19 6:20 ` Hou Tao [this message]
2023-10-19 14:25 ` Paul E. McKenney
2023-10-20 1:07 ` Hou Tao
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=7fe984d2-c30c-40ad-83cd-d9fb51b6ce0d@huaweicloud.com \
--to=houtao@huaweicloud.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=paulmck@kernel.org \
--cc=sdf@google.com \
--cc=song@kernel.org \
--cc=void@manifault.com \
--cc=yonghong.song@linux.dev \
/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).