Skip to contentSkip to conversation

selftests/bpf: refactor snprintf_btf test to use bpf_strncmp

bpf-next Hoyeon Lee 3 participants 8 messages Nov 5, 2025 – Nov 6, 2025

Subsystems (3)
lore
Share & tools
lore

Hoyeon Lee

author
#0 lore raw

The netif_receive_skb BPF program used in snprintf_btf test still uses a custom __strncmp. This is unnecessary as the bpf_strncmp helper is available and provides the same functionality.

This commit refactors the test to use the bpf_strncmp helper, removing the redundant custom implementation.

---
 .../selftests/bpf/progs/netif_receive_skb.c       | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -14
index 9e067dcbf607..186b8c82b9e6 100644
@@ -31,19 +31,6 @@ struct {
 	__type(value, char[STRSIZE]);
 } strdata SEC(".maps");
 
-static int __strncmp(const void *m1, const void *m2, size_t len)
-{
-	const unsigned char *s1 = m1;
-	const unsigned char *s2 = m2;
-	int i, delta = 0;
-
-	for (i = 0; i < len; i++) {
-		delta = s1[i] - s2[i];
-		if (delta || s1[i] == 0 || s2[i] == 0)
-			break;
-	}
-	return delta;
-}
 
 #if __has_builtin(__builtin_btf_type_id)
 #define	TEST_BTF(_str, _type, _flags, _expected, ...)			\
@@ -69,7 +56,7 @@ static int __strncmp(const void *m1, const void *m2, size_t len)
 				       &_ptr, sizeof(_ptr), _hflags);	\
 		if (ret)						\
 			break;						\
-		_cmp = __strncmp(_str, _expectedval, EXPECTED_STRSIZE);	\
+		_cmp = bpf_strncmp(_str, EXPECTED_STRSIZE, _expectedval); \
 		if (_cmp != 0) {					\
 			bpf_printk("(%d) got %s", _cmp, _str);		\
 			bpf_printk("(%d) expected %s", _cmp,		\
Signature · 1 line
2.51.1
maintainer Acked-by
#1 lore raw
Quoted 9 lines · Hoyeon Lee

The netif_receive_skb BPF program used in snprintf_btf test still uses a custom __strncmp. This is unnecessary as the bpf_strncmp helper is available and provides the same functionality.

This commit refactors the test to use the bpf_strncmp helper, removing the redundant custom implementation.

---

[...]

#2 lore raw
In reply to Hoyeon Lee
Quoted 38 lines · Hoyeon Lee

The netif_receive_skb BPF program used in snprintf_btf test still uses a custom __strncmp. This is unnecessary as the bpf_strncmp helper is available and provides the same functionality.

This commit refactors the test to use the bpf_strncmp helper, removing the redundant custom implementation.

---
 .../selftests/bpf/progs/netif_receive_skb.c       | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +0 -13
index 9e067dcbf607..186b8c82b9e6 100644
@@ -31,19 +31,6 @@ struct {
        __type(value, char[STRSIZE]);
 } strdata SEC(".maps");
 
-static int __strncmp(const void *m1, const void *m2, size_t len)
-{
-       const unsigned char *s1 = m1;
-       const unsigned char *s2 = m2;
-       int i, delta = 0;
-
-       for (i = 0; i < len; i++) {
-               delta = s1[i] - s2[i];
-               if (delta || s1[i] == 0 || s2[i] == 0)
-                       break;
-       }
-       return delta;
-}
 
 #if __has_builtin(__builtin_btf_type_id)
 #define        TEST_BTF(_str, _type, _flags, _expected, ...)                   \

tools/testing/selftests/bpf/progs/netif_receive_skb.c static int __strncmp(const void *m1, const void *m2, size_t len) · line 59

Quoted 5 lines
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -1
@@ -69,7 +56,7 @@ static int __strncmp(const void *m1, const void *m2, size_t len)
                                       &_ptr, sizeof(_ptr), _hflags);   \
                if (ret)                                                \
                        break;                                          \
-               _cmp = __strncmp(_str, _expectedval, EXPECTED_STRSIZE); \
+               _cmp = bpf_strncmp(_str, EXPECTED_STRSIZE, _expectedval); \

Though it's equivalent, the point of the test is to be heavy for the verifier with open coded __strncmp().

pw-bot: cr

maintainer
#3 lore raw
Quoted 48 lines · Alexei Starovoitov
Quoted 38 lines · Hoyeon Lee

The netif_receive_skb BPF program used in snprintf_btf test still uses a custom __strncmp. This is unnecessary as the bpf_strncmp helper is available and provides the same functionality.

This commit refactors the test to use the bpf_strncmp helper, removing the redundant custom implementation.

---
 .../selftests/bpf/progs/netif_receive_skb.c       | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +0 -13
index 9e067dcbf607..186b8c82b9e6 100644
@@ -31,19 +31,6 @@ struct {
        __type(value, char[STRSIZE]);
 } strdata SEC(".maps");
 
-static int __strncmp(const void *m1, const void *m2, size_t len)
-{
-       const unsigned char *s1 = m1;
-       const unsigned char *s2 = m2;
-       int i, delta = 0;
-
-       for (i = 0; i < len; i++) {
-               delta = s1[i] - s2[i];
-               if (delta || s1[i] == 0 || s2[i] == 0)
-                       break;
-       }
-       return delta;
-}
 
 #if __has_builtin(__builtin_btf_type_id)
 #define        TEST_BTF(_str, _type, _flags, _expected, ...)                   \

tools/testing/selftests/bpf/progs/netif_receive_skb.c static int __strncmp(const void *m1, const void *m2, size_t len) · line 59

Quoted 5 lines
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -1
@@ -69,7 +56,7 @@ static int __strncmp(const void *m1, const void *m2, size_t len)
                                       &_ptr, sizeof(_ptr), _hflags);   \
                if (ret)                                                \
                        break;                                          \
-               _cmp = __strncmp(_str, _expectedval, EXPECTED_STRSIZE); \
+               _cmp = bpf_strncmp(_str, EXPECTED_STRSIZE, _expectedval); \

Though it's equivalent, the point of the test is to be heavy for the verifier with open coded __strncmp().

Quoted 1 line

pw-bot: cr

I double checked that before acking, the test was added as a part of [1]. So it seems to be focused on bpf_snprintf_btf(), not on scalability. And it's not that heavy in terms of instructions budget:

File                     Program                  Verdict  Insns  States
-----------------------  -----------------------  -------  -----  ------
netif_receive_skb.bpf.o  trace_netif_receive_skb  success  18152     629

[1] https://lore.kernel.org/bpf/[email protected]/

#4 lore raw
Quoted 52 lines · Eduard Zingerman
Quoted 48 lines · Alexei Starovoitov
Quoted 38 lines · Hoyeon Lee

The netif_receive_skb BPF program used in snprintf_btf test still uses a custom __strncmp. This is unnecessary as the bpf_strncmp helper is available and provides the same functionality.

This commit refactors the test to use the bpf_strncmp helper, removing the redundant custom implementation.

---
 .../selftests/bpf/progs/netif_receive_skb.c       | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +0 -13
index 9e067dcbf607..186b8c82b9e6 100644
@@ -31,19 +31,6 @@ struct {
        __type(value, char[STRSIZE]);
 } strdata SEC(".maps");
 
-static int __strncmp(const void *m1, const void *m2, size_t len)
-{
-       const unsigned char *s1 = m1;
-       const unsigned char *s2 = m2;
-       int i, delta = 0;
-
-       for (i = 0; i < len; i++) {
-               delta = s1[i] - s2[i];
-               if (delta || s1[i] == 0 || s2[i] == 0)
-                       break;
-       }
-       return delta;
-}
 
 #if __has_builtin(__builtin_btf_type_id)
 #define        TEST_BTF(_str, _type, _flags, _expected, ...)                   \

tools/testing/selftests/bpf/progs/netif_receive_skb.c static int __strncmp(const void *m1, const void *m2, size_t len) · line 59

Quoted 5 lines
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -1
@@ -69,7 +56,7 @@ static int __strncmp(const void *m1, const void *m2, size_t len)
                                       &_ptr, sizeof(_ptr), _hflags);   \
                if (ret)                                                \
                        break;                                          \
-               _cmp = __strncmp(_str, _expectedval, EXPECTED_STRSIZE); \
+               _cmp = bpf_strncmp(_str, EXPECTED_STRSIZE, _expectedval); \

Though it's equivalent, the point of the test is to be heavy for the verifier with open coded __strncmp().

Quoted 1 line

pw-bot: cr

Quoted 7 lines

I double checked that before acking, the test was added as a part of [1]. So it seems to be focused on bpf_snprintf_btf(), not on scalability. And it's not that heavy in terms of instructions budget:

File                     Program                  Verdict  Insns  States
-----------------------  -----------------------  -------  -----  ------
netif_receive_skb.bpf.o  trace_netif_receive_skb  success  18152     629

Is this before or after?
What is the % decrease in insn_processed?
I'd like to better understand the impact of the change.

maintainer
#5 lore raw
Quoted 57 lines · Alexei Starovoitov
Quoted 56 lines · Eduard Zingerman
Quoted 48 lines · Alexei Starovoitov
Quoted 38 lines · Hoyeon Lee

The netif_receive_skb BPF program used in snprintf_btf test still uses a custom __strncmp. This is unnecessary as the bpf_strncmp helper is available and provides the same functionality.

This commit refactors the test to use the bpf_strncmp helper, removing the redundant custom implementation.

---
 .../selftests/bpf/progs/netif_receive_skb.c       | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +0 -13
index 9e067dcbf607..186b8c82b9e6 100644
@@ -31,19 +31,6 @@ struct {
        __type(value, char[STRSIZE]);
 } strdata SEC(".maps");
 
-static int __strncmp(const void *m1, const void *m2, size_t len)
-{
-       const unsigned char *s1 = m1;
-       const unsigned char *s2 = m2;
-       int i, delta = 0;
-
-       for (i = 0; i < len; i++) {
-               delta = s1[i] - s2[i];
-               if (delta || s1[i] == 0 || s2[i] == 0)
-                       break;
-       }
-       return delta;
-}
 
 #if __has_builtin(__builtin_btf_type_id)
 #define        TEST_BTF(_str, _type, _flags, _expected, ...)                   \

tools/testing/selftests/bpf/progs/netif_receive_skb.c static int __strncmp(const void *m1, const void *m2, size_t len) · line 59

Quoted 5 lines
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -1
@@ -69,7 +56,7 @@ static int __strncmp(const void *m1, const void *m2, size_t len)
                                       &_ptr, sizeof(_ptr), _hflags);   \
                if (ret)                                                \
                        break;                                          \
-               _cmp = __strncmp(_str, _expectedval, EXPECTED_STRSIZE); \
+               _cmp = bpf_strncmp(_str, EXPECTED_STRSIZE, _expectedval); \

Though it's equivalent, the point of the test is to be heavy for the verifier with open coded __strncmp().

Quoted 1 line

pw-bot: cr

I double checked that before acking, the test was added as a part of [1]. So it seems to be focused on bpf_snprintf_btf(), not on scalability. And it's not that heavy in terms of instructions budget:

Quoted 7 lines
Quoted 3 lines
File                     Program                  Verdict  Insns  States
-----------------------  -----------------------  -------  -----  ------
netif_receive_skb.bpf.o  trace_netif_receive_skb  success  18152     629

Is this before or after?
What is the % decrease in insn_processed?
I'd like to better understand the impact of the change.

That's before, after the change it is as follows:

File                     Program                  Verdict  Insns  States
-----------------------  -----------------------  -------  -----  ------
netif_receive_skb.bpf.o  trace_netif_receive_skb  success   4353     235
-----------------------  -----------------------  -------  -----  ------

So, the overall impact is 18K -> 4K instructions processed.

#6 lore raw
Quoted 67 lines · Eduard Zingerman
Quoted 57 lines · Alexei Starovoitov
Quoted 56 lines · Eduard Zingerman
Quoted 48 lines · Alexei Starovoitov
Quoted 38 lines · Hoyeon Lee

The netif_receive_skb BPF program used in snprintf_btf test still uses a custom __strncmp. This is unnecessary as the bpf_strncmp helper is available and provides the same functionality.

This commit refactors the test to use the bpf_strncmp helper, removing the redundant custom implementation.

---
 .../selftests/bpf/progs/netif_receive_skb.c       | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +0 -13
index 9e067dcbf607..186b8c82b9e6 100644
@@ -31,19 +31,6 @@ struct {
        __type(value, char[STRSIZE]);
 } strdata SEC(".maps");
 
-static int __strncmp(const void *m1, const void *m2, size_t len)
-{
-       const unsigned char *s1 = m1;
-       const unsigned char *s2 = m2;
-       int i, delta = 0;
-
-       for (i = 0; i < len; i++) {
-               delta = s1[i] - s2[i];
-               if (delta || s1[i] == 0 || s2[i] == 0)
-                       break;
-       }
-       return delta;
-}
 
 #if __has_builtin(__builtin_btf_type_id)
 #define        TEST_BTF(_str, _type, _flags, _expected, ...)                   \

tools/testing/selftests/bpf/progs/netif_receive_skb.c static int __strncmp(const void *m1, const void *m2, size_t len) · line 59

Quoted 5 lines
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -1
@@ -69,7 +56,7 @@ static int __strncmp(const void *m1, const void *m2, size_t len)
                                       &_ptr, sizeof(_ptr), _hflags);   \
                if (ret)                                                \
                        break;                                          \
-               _cmp = __strncmp(_str, _expectedval, EXPECTED_STRSIZE); \
+               _cmp = bpf_strncmp(_str, EXPECTED_STRSIZE, _expectedval); \

Though it's equivalent, the point of the test is to be heavy for the verifier with open coded __strncmp().

Quoted 1 line

pw-bot: cr

I double checked that before acking, the test was added as a part of [1]. So it seems to be focused on bpf_snprintf_btf(), not on scalability. And it's not that heavy in terms of instructions budget:

Quoted 7 lines
Quoted 3 lines
File                     Program                  Verdict  Insns  States
-----------------------  -----------------------  -------  -----  ------
netif_receive_skb.bpf.o  trace_netif_receive_skb  success  18152     629

Is this before or after?
What is the % decrease in insn_processed?
I'd like to better understand the impact of the change.

Quoted 8 lines

That's before, after the change it is as follows:

File                     Program                  Verdict  Insns  States
-----------------------  -----------------------  -------  -----  ------
netif_receive_skb.bpf.o  trace_netif_receive_skb  success   4353     235
-----------------------  -----------------------  -------  -----  ------

So, the overall impact is 18K -> 4K instructions processed.

It's large enough impact for the verifier.
I agree that the test was mainly focusing on testing bpf_snprintf_btf(), but it has a nice side effect by testing bounded loops too.
I prefer to keep it as-is.

author
#7 lore raw
Quoted 76 lines · Alexei Starovoitov
Quoted 74 lines · Eduard Zingerman
Quoted 57 lines · Alexei Starovoitov
Quoted 56 lines · Eduard Zingerman
Quoted 48 lines · Alexei Starovoitov
Quoted 38 lines · Hoyeon Lee

The netif_receive_skb BPF program used in snprintf_btf test still uses a custom __strncmp. This is unnecessary as the bpf_strncmp helper is available and provides the same functionality.

This commit refactors the test to use the bpf_strncmp helper, removing the redundant custom implementation.

---
 .../selftests/bpf/progs/netif_receive_skb.c       | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
tools/testing/selftests/bpf/progs/netif_receive_skb.c +0 -13
index 9e067dcbf607..186b8c82b9e6 100644
@@ -31,19 +31,6 @@ struct {
        __type(value, char[STRSIZE]);
 } strdata SEC(".maps");
 
-static int __strncmp(const void *m1, const void *m2, size_t len)
-{
-       const unsigned char *s1 = m1;
-       const unsigned char *s2 = m2;
-       int i, delta = 0;
-
-       for (i = 0; i < len; i++) {
-               delta = s1[i] - s2[i];
-               if (delta || s1[i] == 0 || s2[i] == 0)
-                       break;
-       }
-       return delta;
-}
 
 #if __has_builtin(__builtin_btf_type_id)
 #define        TEST_BTF(_str, _type, _flags, _expected, ...)                   \

tools/testing/selftests/bpf/progs/netif_receive_skb.c static int __strncmp(const void *m1, const void *m2, size_t len) · line 59

Quoted 5 lines
tools/testing/selftests/bpf/progs/netif_receive_skb.c +1 -1
@@ -69,7 +56,7 @@ static int __strncmp(const void *m1, const void *m2, size_t len)
                                       &_ptr, sizeof(_ptr), _hflags);   \
                if (ret)                                                \
                        break;                                          \
-               _cmp = __strncmp(_str, _expectedval, EXPECTED_STRSIZE); \
+               _cmp = bpf_strncmp(_str, EXPECTED_STRSIZE, _expectedval); \

Though it's equivalent, the point of the test is to be heavy for the verifier with open coded __strncmp().

Quoted 1 line

pw-bot: cr

I double checked that before acking, the test was added as a part of [1]. So it seems to be focused on bpf_snprintf_btf(), not on scalability. And it's not that heavy in terms of instructions budget:

Quoted 7 lines
Quoted 3 lines
File                     Program                  Verdict  Insns  States
-----------------------  -----------------------  -------  -----  ------
netif_receive_skb.bpf.o  trace_netif_receive_skb  success  18152     629

Is this before or after?
What is the % decrease in insn_processed?
I'd like to better understand the impact of the change.

That's before, after the change it is as follows:

File                     Program                  Verdict  Insns  States
-----------------------  -----------------------  -------  -----  ------
netif_receive_skb.bpf.o  trace_netif_receive_skb  success   4353     235
-----------------------  -----------------------  -------  -----  ------
Quoted 7 lines
Quoted 1 line

So, the overall impact is 18K -> 4K instructions processed.

It's large enough impact for the verifier.
I agree that the test was mainly focusing on testing bpf_snprintf_btf(), but it has a nice side effect by testing bounded loops too.
I prefer to keep it as-is.

Thanks for the clarification.

Removing the open-coded __strncmp would drop the bounded-loop coverage that this test currently provides (as a side effect), and that stress on the verifier is still valuable.

I'll drop this patch.
Thank you all for the discussion and review.