diff mbox series

[bpf] Fold smp_mb__before_atomic() into atomic_set_release()

Message ID ec86d38e-cfb4-44aa-8fdb-6c925922d93c@paulmck-laptop (mailing list archive)
State Accepted
Commit e661451ce4e6050486504acb112a50c69acc7ed1
Delegated to: BPF
Headers show
Series [bpf] Fold smp_mb__before_atomic() into atomic_set_release() | expand

Checks

Context Check Description
bpf/vmtest-bpf-VM_Test-0 success Logs for ShellCheck
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-19 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-2 success Logs for build for s390x with gcc
bpf/vmtest-bpf-VM_Test-16 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-1 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-VM_Test-9 fail Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-25 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-VM_Test-6 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-27 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-21 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-11 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-VM_Test-13 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-8 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-3 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-10 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-4 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-22 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-7 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-VM_Test-23 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-12 fail Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-18 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-VM_Test-26 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-15 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-VM_Test-24 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-14 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-28 success Logs for veristat
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for bpf
netdev/fixes_present fail Series targets non-next tree, but doesn't contain any Fixes tags
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1365 this patch: 1365
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/build_clang success Errors and warnings before: 1388 this patch: 1388
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1389 this patch: 1389
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 9 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Paul E. McKenney Oct. 18, 2023, 10:28 p.m. UTC
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);

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>

Comments

Hou Tao Oct. 19, 2023, 1:07 a.m. UTC | #1
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 ?

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
                                            // 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 ?

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);
>
> .
Paul E. McKenney Oct. 19, 2023, 4:54 a.m. UTC | #2
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.

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().

>                                             // 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.

							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);
> >
> > .
>
Hou Tao Oct. 19, 2023, 6:20 a.m. UTC | #3
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);
>>>
>>> .
patchwork-bot+netdevbpf@kernel.org Oct. 19, 2023, 2:20 p.m. UTC | #4
Hello:

This patch was applied to bpf/bpf.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:

On Wed, 18 Oct 2023 15:28:32 -0700 you 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:
> 
> [...]

Here is the summary with links:
  - [bpf] Fold smp_mb__before_atomic() into atomic_set_release()
    https://git.kernel.org/bpf/bpf/c/e661451ce4e6

You are awesome, thank you!
Paul E. McKenney Oct. 19, 2023, 2:25 p.m. UTC | #5
On Thu, Oct 19, 2023 at 02:20:35PM +0800, Hou Tao wrote:
> 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.

That is the place!

> > 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 ?

If there is any possibility of a concurrent reader, that is, a reader
not holding that same lock, then yes, you should use WRITE_ONCE().

Compilers can do pretty vicious things to unmarked reads and writes.
But don't take my word for it, here are a few writeups:

o	"Who's afraid of a big bad optimizing compiler?" (series)
	https://lwn.net/Articles/793253, https://lwn.net/Articles/799218

o	"An introduction to lockless algorithms" (Paolo Bonzini series)
	https://lwn.net/Articles/844224, https://lwn.net/Articles/846700,
	https://lwn.net/Articles/847481, https://lwn.net/Articles/847973,
	https://lwn.net/Articles/849237, https://lwn.net/Articles/850202

o	"Is Parallel Programming Hard, And, If So, What Can You Do About It?"
	Section 4.3.4 ("Accessing Shared Variables")
	https://mirrors.edge.kernel.org/pub/linux/kernel/people/paulmck/perfbook/
perfbook.html

> >>                                             // 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.

Very good!

							Thanx, Paul

> 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);
> >>>
> >>> .
>
Hou Tao Oct. 20, 2023, 1:07 a.m. UTC | #6
Hi Paul,

On 10/19/2023 10:25 PM, Paul E. McKenney wrote:
> On Thu, Oct 19, 2023 at 02:20:35PM +0800, Hou Tao wrote:
>> 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.
> That is the place!
>
>>> 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 ?
> If there is any possibility of a concurrent reader, that is, a reader
> not holding that same lock, then yes, you should use WRITE_ONCE().

Got it. Will do.
>
> Compilers can do pretty vicious things to unmarked reads and writes.
> But don't take my word for it, here are a few writeups:
>
> o	"Who's afraid of a big bad optimizing compiler?" (series)
> 	https://lwn.net/Articles/793253, https://lwn.net/Articles/799218
>
> o	"An introduction to lockless algorithms" (Paolo Bonzini series)
> 	https://lwn.net/Articles/844224, https://lwn.net/Articles/846700,
> 	https://lwn.net/Articles/847481, https://lwn.net/Articles/847973,
> 	https://lwn.net/Articles/849237, https://lwn.net/Articles/850202
>
> o	"Is Parallel Programming Hard, And, If So, What Can You Do About It?"
> 	Section 4.3.4 ("Accessing Shared Variables")
> 	https://mirrors.edge.kernel.org/pub/linux/kernel/people/paulmck/perfbook/
> perfbook.html

Thanks for these excellent articles. Will read these articles carefully
this time.

Regards,
Hou
>
>>>>                                             // 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.
> Very good!
>
> 							Thanx, Paul
>
>> 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);
>>>>>
>>>>> .
diff mbox series

Patch

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);