Skip to contentSkip to conversation

selftests/bpf: fix sample_cnt shared between two threads

PATCH Applied Andrii Nakryiko 3 participants 3 messages Jun 2, 2020

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: fix sample_cnt shared between two threads

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")
---
 tools/testing/selftests/bpf/prog_tests/ringbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/ringbuf.c +1 -1
index bb8541f240e2..2bba908dfa63 100644
@@ -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)
 {
Signature · 1 line
2.24.1
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · 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")
#2 lore raw
Quoted 10 lines · Song Liu
Quoted 8 lines · 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.

Quoted 4 lines
Quoted 2 lines
Fixes cb1c9ddd5525 ("selftests/bpf: Add BPF ringbuf selftests")

Applied. Thanks.

I also pushed trivial commit to fix test_verifier.