bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next] selftests/bpf: fix sample_cnt shared between two threads
@ 2020-06-02  5:03 Andrii Nakryiko
  2020-06-02 16:18 ` Song Liu
  0 siblings, 1 reply; 3+ messages in thread
From: Andrii Nakryiko @ 2020-06-02  5:03 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel; +Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko

Make sample_cnt volatile to fix possible selftests failure due to compiler
optimization preventing latest sample_cnt value to be visible to main thread.
sample_cnt is incremented in background thread, which is then joined into main
thread. So in terms of visibility sample_cnt update is ok. But because it's
not volatile, compiler might make optimizations that would prevent main thread
to see latest updated value. Fix this by marking global variable volatile.

Fixes: cb1c9ddd5525 ("selftests/bpf: Add BPF ringbuf selftests")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/testing/selftests/bpf/prog_tests/ringbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/ringbuf.c b/tools/testing/selftests/bpf/prog_tests/ringbuf.c
index bb8541f240e2..2bba908dfa63 100644
--- a/tools/testing/selftests/bpf/prog_tests/ringbuf.c
+++ b/tools/testing/selftests/bpf/prog_tests/ringbuf.c
@@ -25,7 +25,7 @@ struct sample {
 	char comm[16];
 };
 
-static int sample_cnt;
+static volatile int sample_cnt;
 
 static int process_sample(void *ctx, void *data, size_t len)
 {
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: fix sample_cnt shared between two threads
  2020-06-02  5:03 [PATCH bpf-next] selftests/bpf: fix sample_cnt shared between two threads Andrii Nakryiko
@ 2020-06-02 16:18 ` Song Liu
  2020-06-02 19:00   ` Alexei Starovoitov
  0 siblings, 1 reply; 3+ messages in thread
From: Song Liu @ 2020-06-02 16:18 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, Networking, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Kernel Team

On Mon, Jun 1, 2020 at 10:04 PM Andrii Nakryiko <andriin@fb.com> wrote:
>
> Make sample_cnt volatile to fix possible selftests failure due to compiler
> optimization preventing latest sample_cnt value to be visible to main thread.
> sample_cnt is incremented in background thread, which is then joined into main
> thread. So in terms of visibility sample_cnt update is ok. But because it's
> not volatile, compiler might make optimizations that would prevent main thread
> to see latest updated value. Fix this by marking global variable volatile.
>
> Fixes: cb1c9ddd5525 ("selftests/bpf: Add BPF ringbuf selftests")
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>

Acked-by: Song Liu <songliubraving@fb.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: fix sample_cnt shared between two threads
  2020-06-02 16:18 ` Song Liu
@ 2020-06-02 19:00   ` Alexei Starovoitov
  0 siblings, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2020-06-02 19:00 UTC (permalink / raw)
  To: Song Liu
  Cc: Andrii Nakryiko, bpf, Networking, Alexei Starovoitov,
	Daniel Borkmann, Andrii Nakryiko, Kernel Team

On Tue, Jun 2, 2020 at 9:22 AM Song Liu <song@kernel.org> wrote:
>
> On Mon, Jun 1, 2020 at 10:04 PM Andrii Nakryiko <andriin@fb.com> wrote:
> >
> > Make sample_cnt volatile to fix possible selftests failure due to compiler
> > optimization preventing latest sample_cnt value to be visible to main thread.
> > sample_cnt is incremented in background thread, which is then joined into main
> > thread. So in terms of visibility sample_cnt update is ok. But because it's
> > not volatile, compiler might make optimizations that would prevent main thread
> > to see latest updated value. Fix this by marking global variable volatile.
> >
> > Fixes: cb1c9ddd5525 ("selftests/bpf: Add BPF ringbuf selftests")
> > Signed-off-by: Andrii Nakryiko <andriin@fb.com>
>
> Acked-by: Song Liu <songliubraving@fb.com>

Applied. Thanks.

I also pushed trivial commit to fix test_verifier.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-06-02 19:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-02  5:03 [PATCH bpf-next] selftests/bpf: fix sample_cnt shared between two threads Andrii Nakryiko
2020-06-02 16:18 ` Song Liu
2020-06-02 19:00   ` Alexei Starovoitov

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