Skip to contentSkip to conversation

selftests/bpf: Remove unnecessary volatile and barrier_var()

PATCH In review Puranjay Mohan 3 participants 5 messages Feb 5

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

PATCH 1/1 selftests/bpf: Remove unnecessary volatile and barrier_var()

As the verifier learns new patterns, some of the volatile qualifiers for local variables and some barrier_var() calls can be removed.

---
 tools/testing/selftests/bpf/progs/bpf_gotox.c          |  4 ++--
 tools/testing/selftests/bpf/progs/exhandler_kern.c     |  6 ------
 .../testing/selftests/bpf/progs/freplace_global_func.c |  2 +-
 tools/testing/selftests/bpf/progs/tailcall_bpf2bpf3.c  | 10 +++++-----
 tools/testing/selftests/bpf/progs/test_global_func1.c  |  6 +++---
 tools/testing/selftests/bpf/progs/test_global_func17.c |  1 -
 tools/testing/selftests/bpf/progs/test_global_func2.c  |  4 ++--
 tools/testing/selftests/bpf/progs/test_pkt_access.c    |  4 ++--
 tools/testing/selftests/bpf/progs/test_spin_lock.c     |  8 ++++----
 .../testing/selftests/bpf/progs/test_spin_lock_fail.c  |  4 ++--
 tools/testing/selftests/bpf/progs/test_sysctl_loop1.c  |  5 +----
 tools/testing/selftests/bpf/progs/test_sysctl_prog.c   |  2 +-
 12 files changed, 23 insertions(+), 33 deletions(-)
tools/testing/selftests/bpf/progs/bpf_gotox.c +2 -2
index 216c71b94c64..9cb40d3d9379 100644
@@ -252,7 +252,7 @@ int one_jump_two_maps(struct simple_ctx *ctx __attribute__((unused)))
 	void *jt2[2] = { &&l3, &&l4 };
 	unsigned int a = ctx->x % 2;
 	unsigned int b = (ctx->x / 2) % 2;
-	volatile int ret = 0;
+	int ret = 0;
 
 	if (!(a < 2 && b < 2))
 		return 19;
@@ -278,7 +278,7 @@ int one_map_two_jumps(struct simple_ctx *ctx __attribute__((unused)))
 	void *jt[3] = { &&l1, &&l2, &&l3 };
 	unsigned int a = (ctx->x >> 2) & 1;
 	unsigned int b = (ctx->x >> 3) & 1;
-	volatile int ret = 0;
+	int ret = 0;
 
 	if (ctx->x % 2)
 		goto *jt[a];
tools/testing/selftests/bpf/progs/exhandler_kern.c +0 -6
index 20d009e2d266..8c509971b39c 100644
@@ -37,14 +37,8 @@ int BPF_PROG(trace_task_newtask, struct task_struct *task, u64 clone_flags)
 	 */
 	work = task->task_works;
 	func = work->func;
-	/* Currently verifier will fail for `btf_ptr |= btf_ptr` * instruction.
-	 * To workaround the issue, use barrier_var() and rewrite as below to
-	 * prevent compiler from generating verifier-unfriendly code.
-	 */
-	barrier_var(work);
 	if (work)
 		return 0;
-	barrier_var(func);
 	if (func)
 		return 0;
 	exception_triggered++;
tools/testing/selftests/bpf/progs/freplace_global_func.c +1 -1
index 96cb61a6ce87..6770bbc3cbc1 100644
@@ -5,7 +5,7 @@
 __noinline
 int test_ctx_global_func(struct __sk_buff *skb)
 {
-	volatile int retval = 1;
+	int retval = 1;
 	return retval;
 }
 
tools/testing/selftests/bpf/progs/tailcall_bpf2bpf3.c +5 -5
index 99c8d1d8a187..b50dc92b2126 100644
@@ -14,7 +14,7 @@ struct {
 __noinline
 int subprog_tail2(struct __sk_buff *skb)
 {
-	volatile char arr[64] = {};
+	char arr[64] = {};
 
 	if (load_word(skb, 0) || load_half(skb, 0))
 		bpf_tail_call_static(skb, &jmp_table, 10);
@@ -29,7 +29,7 @@ int subprog_tail2(struct __sk_buff *skb)
 static __noinline
 int subprog_tail(struct __sk_buff *skb)
 {
-	volatile char arr[64] = {};
+	char arr[64] = {};
 
 	bpf_tail_call_static(skb, &jmp_table, 0);
 
@@ -41,7 +41,7 @@ int subprog_tail(struct __sk_buff *skb)
 SEC("tc")
 int classifier_0(struct __sk_buff *skb)
 {
-	volatile char arr[128] = {};
+	char arr[128] = {};
 
 	__sink(arr[sizeof(arr) - 1]);
 
@@ -51,7 +51,7 @@ int classifier_0(struct __sk_buff *skb)
 SEC("tc")
 int classifier_1(struct __sk_buff *skb)
 {
-	volatile char arr[128] = {};
+	char arr[128] = {};
 
 	__sink(arr[sizeof(arr) - 1]);
 
@@ -61,7 +61,7 @@ int classifier_1(struct __sk_buff *skb)
 SEC("tc")
 int entry(struct __sk_buff *skb)
 {
-	volatile char arr[128] = {};
+	char arr[128] = {};
 
 	__sink(arr[sizeof(arr) - 1]);
 
tools/testing/selftests/bpf/progs/test_global_func1.c +3 -3
index fc69ff18880d..1fdeb2246e12 100644
@@ -18,7 +18,7 @@ int f0(int var, struct __sk_buff *skb)
 __attribute__ ((noinline))
 int f1(struct __sk_buff *skb)
 {
-	volatile char buf[MAX_STACK] = {};
+	char buf[MAX_STACK] = {};
 
 	__sink(buf[MAX_STACK - 1]);
 
@@ -30,7 +30,7 @@ int f3(int, struct __sk_buff *skb, int);
 __attribute__ ((noinline))
 int f2(int val, struct __sk_buff *skb)
 {
-	volatile char buf[MAX_STACK] = {};
+	char buf[MAX_STACK] = {};
 
 	__sink(buf[MAX_STACK - 1]);
 
@@ -40,7 +40,7 @@ int f2(int val, struct __sk_buff *skb)
 __attribute__ ((noinline))
 int f3(int val, struct __sk_buff *skb, int var)
 {
-	volatile char buf[MAX_STACK] = {};
+	char buf[MAX_STACK] = {};
 
 	__sink(buf[MAX_STACK - 1]);
 
tools/testing/selftests/bpf/progs/test_global_func17.c +0 -1
index 5de44b09e8ec..a32e11c7d933 100644
@@ -5,7 +5,6 @@
 
 __noinline int foo(int *p)
 {
-	barrier_var(p);
 	return p ? (*p = 42) : 0;
 }
 
tools/testing/selftests/bpf/progs/test_global_func2.c +2 -2
index 2beab9c3b68a..20ceb8a390eb 100644
@@ -16,7 +16,7 @@ int f0(int var, struct __sk_buff *skb)
 __attribute__ ((noinline))
 int f1(struct __sk_buff *skb)
 {
-	volatile char buf[MAX_STACK] = {};
+	char buf[MAX_STACK] = {};
 
 	__sink(buf[MAX_STACK - 1]);
 
@@ -34,7 +34,7 @@ int f2(int val, struct __sk_buff *skb)
 __attribute__ ((noinline))
 int f3(int val, struct __sk_buff *skb, int var)
 {
-	volatile char buf[MAX_STACK] = {};
+	char buf[MAX_STACK] = {};
 
 	__sink(buf[MAX_STACK - 1]);
 
tools/testing/selftests/bpf/progs/test_pkt_access.c +2 -2
index bce7173152c6..4ae55df19487 100644
@@ -50,7 +50,7 @@ int test_pkt_access_subprog2(int val, volatile struct __sk_buff *skb)
 __attribute__ ((noinline))
 int get_skb_len(struct __sk_buff *skb)
 {
-	volatile char buf[MAX_STACK] = {};
+	char buf[MAX_STACK] = {};
 
 	__sink(buf[MAX_STACK - 1]);
 
@@ -74,7 +74,7 @@ int test_pkt_access_subprog3(int val, struct __sk_buff *skb)
 __attribute__ ((noinline))
 int get_skb_ifindex(int val, struct __sk_buff *skb, int var)
 {
-	volatile char buf[MAX_STACK] = {};
+	char buf[MAX_STACK] = {};
 
 	__sink(buf[MAX_STACK - 1]);
 
tools/testing/selftests/bpf/progs/test_spin_lock.c +4 -4
index d8d77bdffd3d..59efa32a61aa 100644
@@ -49,7 +49,7 @@ struct {
 SEC("cgroup_skb/ingress")
 int bpf_spin_lock_test(struct __sk_buff *skb)
 {
-	volatile int credit = 0, max_credit = 100, pkt_len = 64;
+	int credit = 0, max_credit = 100, pkt_len = 64;
 	struct hmap_elem zero = {}, *val;
 	unsigned long long curtime;
 	struct bpf_vqueue *q;
@@ -107,7 +107,7 @@ struct bpf_spin_lock lockA __hidden SEC(".data.A");
 __noinline
 static int static_subprog(struct __sk_buff *ctx)
 {
-	volatile int ret = 0;
+	int ret = 0;
 
 	if (ctx->protocol)
 		return ret;
@@ -117,7 +117,7 @@ static int static_subprog(struct __sk_buff *ctx)
 __noinline
 static int static_subprog_lock(struct __sk_buff *ctx)
 {
-	volatile int ret = 0;
+	int ret = 0;
 
 	ret = static_subprog(ctx);
 	bpf_spin_lock(&lockA);
@@ -127,7 +127,7 @@ static int static_subprog_lock(struct __sk_buff *ctx)
 __noinline
 static int static_subprog_unlock(struct __sk_buff *ctx)
 {
-	volatile int ret = 0;
+	int ret = 0;
 
 	ret = static_subprog(ctx);
 	bpf_spin_unlock(&lockA);
tools/testing/selftests/bpf/progs/test_spin_lock_fail.c +2 -2
index f678ee6bd7ea..a4bbc66e2302 100644
@@ -204,7 +204,7 @@ CHECK(innermapval_mapval, &iv->lock, &v->lock);
 __noinline
 int global_subprog(struct __sk_buff *ctx)
 {
-	volatile int ret = 0;
+	int ret = 0;
 
 	if (ctx->protocol)
 		ret += ctx->protocol;
@@ -214,7 +214,7 @@ int global_subprog(struct __sk_buff *ctx)
 __noinline
 static int static_subprog_call_global(struct __sk_buff *ctx)
 {
-	volatile int ret = 0;
+	int ret = 0;
 
 	if (ctx->protocol)
 		return ret;
tools/testing/selftests/bpf/progs/test_sysctl_loop1.c +1 -4
index 548660e299a5..919e95dbff10 100644
@@ -43,10 +43,7 @@ int sysctl_tcp_mem(struct bpf_sysctl *ctx)
 	unsigned long tcp_mem[TCP_MEM_LOOPS] = {};
 	char value[MAX_VALUE_STR_LEN];
 	unsigned char i, off = 0;
-	/* a workaround to prevent compiler from generating
-	 * codes verifier cannot handle yet.
-	 */
-	volatile int ret;
+	int ret;
 
 	if (ctx->write)
 		return 0;
tools/testing/selftests/bpf/progs/test_sysctl_prog.c +1 -1
index bbdd08764789..f799bffb40dd 100644
@@ -44,7 +44,7 @@ int sysctl_tcp_mem(struct bpf_sysctl *ctx)
 	unsigned long tcp_mem[3] = {0, 0, 0};
 	char value[MAX_VALUE_STR_LEN];
 	unsigned char i, off = 0;
-	volatile int ret;
+	int ret;
 
 	if (ctx->write)
 		return 0;

base-commit: 6e951a9b1af5dc063dfe9e17ca433be099527c43

Signature · 1 line
2.47.3
maintainer
#1 lore raw

On Wed, 2026-02-04 at 16:24 -0800, Puranjay Mohan wrote:

[...]

tools/testing/selftests/bpf/progs/tailcall_bpf2bpf3.c struct { · line 20

Quoted 13 lines
tools/testing/selftests/bpf/progs/tailcall_bpf2bpf3.c +1 -1
index 99c8d1d8a187..b50dc92b2126 100644
@@ -14,7 +14,7 @@ struct {
 __noinline
 int subprog_tail2(struct __sk_buff *skb)
 {
-	volatile char arr[64] = {};
+	char arr[64] = {};
 
 	if (load_word(skb, 0) || load_half(skb, 0))
 		bpf_tail_call_static(skb, &jmp_table, 10);

I think 'volatile' is used here to force specific function stack size. W/o 'volatile' compiler will throw the variable away.
With 'volatile' one should check what's happening.
Could you please double check?

[...]

#2 lore raw
Quoted 24 lines · Eduard Zingerman

On Wed, 2026-02-04 at 16:24 -0800, Puranjay Mohan wrote:

[...]

tools/testing/selftests/bpf/progs/tailcall_bpf2bpf3.c struct { · line 20

Quoted 13 lines
tools/testing/selftests/bpf/progs/tailcall_bpf2bpf3.c +1 -1
index 99c8d1d8a187..b50dc92b2126 100644
@@ -14,7 +14,7 @@ struct {
 __noinline
 int subprog_tail2(struct __sk_buff *skb)
 {
-     volatile char arr[64] = {};
+     char arr[64] = {};
 
      if (load_word(skb, 0) || load_half(skb, 0))
              bpf_tail_call_static(skb, &jmp_table, 10);

I think 'volatile' is used here to force specific function stack size. W/o 'volatile' compiler will throw the variable away.
With 'volatile' one should check what's happening.
Could you please double check?

Quoted 1 line

[...]

without volatile:

Disassembly of section .text:

0000000000000000 <subprog_tail2>:

; {
       0:       bf 16 00 00 00 00 00 00 r6 = r1
       1:       b7 01 00 00 00 00 00 00 r1 = 0x0
;       char arr[64] = {};
       2:       7b 1a f8 ff 00 00 00 00 *(u64 *)(r10 - 0x8) = r1
       3:       7b 1a f0 ff 00 00 00 00 *(u64 *)(r10 - 0x10) = r1
       4:       7b 1a e8 ff 00 00 00 00 *(u64 *)(r10 - 0x18) = r1
       5:       7b 1a e0 ff 00 00 00 00 *(u64 *)(r10 - 0x20) = r1
       6:       7b 1a d8 ff 00 00 00 00 *(u64 *)(r10 - 0x28) = r1
       7:       7b 1a d0 ff 00 00 00 00 *(u64 *)(r10 - 0x30) = r1
       8:       7b 1a c8 ff 00 00 00 00 *(u64 *)(r10 - 0x38) = r1
       9:       7b 1a c0 ff 00 00 00 00 *(u64 *)(r10 - 0x40) = r1

with volatile (bpf-next/master)

0000000000000000 <subprog_tail2>:

; {
       0:       bf 16 00 00 00 00 00 00 r6 = r1
       1:       b7 01 00 00 00 00 00 00 r1 = 0x0
;       volatile char arr[64] = {};
       2:       7b 1a f8 ff 00 00 00 00 *(u64 *)(r10 - 0x8) = r1
       3:       7b 1a f0 ff 00 00 00 00 *(u64 *)(r10 - 0x10) = r1
       4:       7b 1a e8 ff 00 00 00 00 *(u64 *)(r10 - 0x18) = r1
       5:       7b 1a e0 ff 00 00 00 00 *(u64 *)(r10 - 0x20) = r1
       6:       7b 1a d8 ff 00 00 00 00 *(u64 *)(r10 - 0x28) = r1
       7:       7b 1a d0 ff 00 00 00 00 *(u64 *)(r10 - 0x30) = r1
       8:       7b 1a c8 ff 00 00 00 00 *(u64 *)(r10 - 0x38) = r1
       9:       7b 1a c0 ff 00 00 00 00 *(u64 *)(r10 - 0x40) = r1

I see that the commit c8ed66859397 ("selftests/bpf: fix lots of silly mistakes pointed out by compiler")
added the __sink(arr[sizeof(arr) - 1]);

and after that even without volatile, compiler will not remove it.

maintainer
#3 lore raw
Quoted 62 lines · Puranjay Mohan
Quoted 24 lines · Eduard Zingerman

On Wed, 2026-02-04 at 16:24 -0800, Puranjay Mohan wrote:

[...]

tools/testing/selftests/bpf/progs/tailcall_bpf2bpf3.c struct { · line 20

Quoted 13 lines
tools/testing/selftests/bpf/progs/tailcall_bpf2bpf3.c +1 -1
index 99c8d1d8a187..b50dc92b2126 100644
@@ -14,7 +14,7 @@ struct {
 __noinline
 int subprog_tail2(struct __sk_buff *skb)
 {
-     volatile char arr[64] = {};
+     char arr[64] = {};
 
      if (load_word(skb, 0) || load_half(skb, 0))
              bpf_tail_call_static(skb, &jmp_table, 10);

I think 'volatile' is used here to force specific function stack size. W/o 'volatile' compiler will throw the variable away.
With 'volatile' one should check what's happening.
Could you please double check?

Quoted 1 line

[...]

without volatile:

Disassembly of section .text:

0000000000000000 <subprog_tail2>:

; {
       0:       bf 16 00 00 00 00 00 00 r6 = r1
       1:       b7 01 00 00 00 00 00 00 r1 = 0x0
;       char arr[64] = {};
       2:       7b 1a f8 ff 00 00 00 00 *(u64 *)(r10 - 0x8) = r1
       3:       7b 1a f0 ff 00 00 00 00 *(u64 *)(r10 - 0x10) = r1
       4:       7b 1a e8 ff 00 00 00 00 *(u64 *)(r10 - 0x18) = r1
       5:       7b 1a e0 ff 00 00 00 00 *(u64 *)(r10 - 0x20) = r1
       6:       7b 1a d8 ff 00 00 00 00 *(u64 *)(r10 - 0x28) = r1
       7:       7b 1a d0 ff 00 00 00 00 *(u64 *)(r10 - 0x30) = r1
       8:       7b 1a c8 ff 00 00 00 00 *(u64 *)(r10 - 0x38) = r1
       9:       7b 1a c0 ff 00 00 00 00 *(u64 *)(r10 - 0x40) = r1

with volatile (bpf-next/master)

0000000000000000 <subprog_tail2>:

; {
       0:       bf 16 00 00 00 00 00 00 r6 = r1
       1:       b7 01 00 00 00 00 00 00 r1 = 0x0
;       volatile char arr[64] = {};
       2:       7b 1a f8 ff 00 00 00 00 *(u64 *)(r10 - 0x8) = r1
       3:       7b 1a f0 ff 00 00 00 00 *(u64 *)(r10 - 0x10) = r1
       4:       7b 1a e8 ff 00 00 00 00 *(u64 *)(r10 - 0x18) = r1
       5:       7b 1a e0 ff 00 00 00 00 *(u64 *)(r10 - 0x20) = r1
       6:       7b 1a d8 ff 00 00 00 00 *(u64 *)(r10 - 0x28) = r1
       7:       7b 1a d0 ff 00 00 00 00 *(u64 *)(r10 - 0x30) = r1
       8:       7b 1a c8 ff 00 00 00 00 *(u64 *)(r10 - 0x38) = r1
       9:       7b 1a c0 ff 00 00 00 00 *(u64 *)(r10 - 0x40) = r1
Quoted 5 lines

I see that the commit c8ed66859397 ("selftests/bpf: fix lots of silly mistakes pointed out by compiler")
added the __sink(arr[sizeof(arr) - 1]);

and after that even without volatile, compiler will not remove it.

Interesting. __sink is defined as follows:

    #define __sink(expr) asm volatile("" : "+g"(expr))

'g' constraint [1]:

Quoted 2 lines

Any register, memory or immediate integer operand is allowed, except for registers that are not general registers.

'+' modifier [2]:

Quoted 1 line

Means that this operand is both read and written by the instruction.

So, it is not obvious for me, why compiler can't replace 'arr' with a single char variable. It appears that passing an address of the array would be a safer strategy.

[1] https://gcc.gnu.org/onlinedocs/gcc/Simple-Constraints.html
[2] https://gcc.gnu.org/onlinedocs/gcc/Modifiers.html

#4 lore raw
Quoted 79 lines · Eduard Zingerman
Quoted 62 lines · Puranjay Mohan
Quoted 24 lines · Eduard Zingerman

On Wed, 2026-02-04 at 16:24 -0800, Puranjay Mohan wrote:

[...]

tools/testing/selftests/bpf/progs/tailcall_bpf2bpf3.c struct { · line 20

Quoted 13 lines
tools/testing/selftests/bpf/progs/tailcall_bpf2bpf3.c +1 -1
index 99c8d1d8a187..b50dc92b2126 100644
@@ -14,7 +14,7 @@ struct {
 __noinline
 int subprog_tail2(struct __sk_buff *skb)
 {
-     volatile char arr[64] = {};
+     char arr[64] = {};
 
      if (load_word(skb, 0) || load_half(skb, 0))
              bpf_tail_call_static(skb, &jmp_table, 10);

I think 'volatile' is used here to force specific function stack size. W/o 'volatile' compiler will throw the variable away.
With 'volatile' one should check what's happening.
Could you please double check?

Quoted 1 line

[...]

without volatile:

Disassembly of section .text:

0000000000000000 <subprog_tail2>:

; {
       0:       bf 16 00 00 00 00 00 00 r6 = r1
       1:       b7 01 00 00 00 00 00 00 r1 = 0x0
;       char arr[64] = {};
       2:       7b 1a f8 ff 00 00 00 00 *(u64 *)(r10 - 0x8) = r1
       3:       7b 1a f0 ff 00 00 00 00 *(u64 *)(r10 - 0x10) = r1
       4:       7b 1a e8 ff 00 00 00 00 *(u64 *)(r10 - 0x18) = r1
       5:       7b 1a e0 ff 00 00 00 00 *(u64 *)(r10 - 0x20) = r1
       6:       7b 1a d8 ff 00 00 00 00 *(u64 *)(r10 - 0x28) = r1
       7:       7b 1a d0 ff 00 00 00 00 *(u64 *)(r10 - 0x30) = r1
       8:       7b 1a c8 ff 00 00 00 00 *(u64 *)(r10 - 0x38) = r1
       9:       7b 1a c0 ff 00 00 00 00 *(u64 *)(r10 - 0x40) = r1

with volatile (bpf-next/master)

0000000000000000 <subprog_tail2>:

; {
       0:       bf 16 00 00 00 00 00 00 r6 = r1
       1:       b7 01 00 00 00 00 00 00 r1 = 0x0
;       volatile char arr[64] = {};
       2:       7b 1a f8 ff 00 00 00 00 *(u64 *)(r10 - 0x8) = r1
       3:       7b 1a f0 ff 00 00 00 00 *(u64 *)(r10 - 0x10) = r1
       4:       7b 1a e8 ff 00 00 00 00 *(u64 *)(r10 - 0x18) = r1
       5:       7b 1a e0 ff 00 00 00 00 *(u64 *)(r10 - 0x20) = r1
       6:       7b 1a d8 ff 00 00 00 00 *(u64 *)(r10 - 0x28) = r1
       7:       7b 1a d0 ff 00 00 00 00 *(u64 *)(r10 - 0x30) = r1
       8:       7b 1a c8 ff 00 00 00 00 *(u64 *)(r10 - 0x38) = r1
       9:       7b 1a c0 ff 00 00 00 00 *(u64 *)(r10 - 0x40) = r1
Quoted 5 lines

I see that the commit c8ed66859397 ("selftests/bpf: fix lots of silly mistakes pointed out by compiler")
added the __sink(arr[sizeof(arr) - 1]);

and after that even without volatile, compiler will not remove it.

Interesting. __sink is defined as follows:

    #define __sink(expr) asm volatile("" : "+g"(expr))

'g' constraint [1]:

Quoted 2 lines

Any register, memory or immediate integer operand is allowed, except for registers that are not general registers.

Quoted 7 lines

'+' modifier [2]:

Quoted 1 line

Means that this operand is both read and written by the instruction.

So, it is not obvious for me, why compiler can't replace 'arr' with a single char variable. It appears that passing an address of the array would be a safer strategy.

Yeah you are right, I feel the best way is to keep the volatile. or do something like:

#define __sink_ptr(p) asm volatile("" : : "g"(p) : "memory")

__sink_ptr(&arr);

tested it on arm64 gcc with -O3 and it works.

Quoted 2 lines

[1] https://gcc.gnu.org/onlinedocs/gcc/Simple-Constraints.html
[2] https://gcc.gnu.org/onlinedocs/gcc/Modifiers.html