Skip to contentSkip to conversation

selftests/bpf: Fix the issue where the error code is 0

PATCH Superseded Feng Yang 2 participants 2 messages Aug 29, 2025 – Sep 5, 2025

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Feng Yang

author
#0 lore raw

PATCH v2 1/1 selftests/bpf: Fix the issue where the error code is 0

From: Feng Yang <[email protected]>

The error message printed here only uses the previous err value, which results in it being printed as 0.
Fix this issue by using libbpf_get_error to retrieve the error.

Fix before:
run_subtest:FAIL:1019 bpf_map__attach_struct_ops failed for map pro_epilogue: err=0

Fix after:
run_subtest:FAIL:1019 bpf_map__attach_struct_ops failed for map pro_epilogue: err=-9

---
Changes in v2:
- Use libbpf_get_error, thanks: Alexei Starovoitov.
- Link to v1: https://lore.kernel.org/all/[email protected]/

---
 tools/testing/selftests/bpf/test_loader.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/test_loader.c +2 -2
index 78423cf89e01..b8e102eb0908 100644
@@ -1082,8 +1082,8 @@ void run_subtest(struct test_loader *tester,
 			}
 			link = bpf_map__attach_struct_ops(map);
 			if (!link) {
-				PRINT_FAIL("bpf_map__attach_struct_ops failed for map %s: err=%d\n",
-					   bpf_map__name(map), err);
+				PRINT_FAIL("bpf_map__attach_struct_ops failed for map %s: err=%ld\n",
+					   bpf_map__name(map), libbpf_get_error(link));
 				goto tobj_cleanup;
 			}
 			links[links_cnt++] = link;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 27 lines · Feng Yang

From: Feng Yang <[email protected]>

The error message printed here only uses the previous err value, which results in it being printed as 0.
Fix this issue by using libbpf_get_error to retrieve the error.

Fix before:
run_subtest:FAIL:1019 bpf_map__attach_struct_ops failed for map pro_epilogue: err=0

Fix after:
run_subtest:FAIL:1019 bpf_map__attach_struct_ops failed for map pro_epilogue: err=-9

---
Changes in v2:
- Use libbpf_get_error, thanks: Alexei Starovoitov.
- Link to v1: https://lore.kernel.org/all/[email protected]/

---
 tools/testing/selftests/bpf/test_loader.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/test_loader.c +0 -0
index 78423cf89e01..b8e102eb0908 100644

tools/testing/selftests/bpf/test_loader.c void run_subtest(struct test_loader *tester, · line 1086

Quoted 7 lines
tools/testing/selftests/bpf/test_loader.c +2 -2
@@ -1082,8 +1082,8 @@ void run_subtest(struct test_loader *tester,
                        }
                        link = bpf_map__attach_struct_ops(map);
                        if (!link) {
-                               PRINT_FAIL("bpf_map__attach_struct_ops failed for map %s: err=%d\n",
-                                          bpf_map__name(map), err);
+                               PRINT_FAIL("bpf_map__attach_struct_ops failed for map %s: err=%ld\n",
+                                          bpf_map__name(map), libbpf_get_error(link));

since libbpf 1.0 all that libbpf_get_error() is doing is returning -errno if link is NULL. It's kind of discouraged now because it's too easy to miss its reliance on errno. So let's use -errno here directly.

pw-bot: cr

Quoted 6 lines
tools/testing/selftests/bpf/test_loader.c +0 -1
                                goto tobj_cleanup;
                        }
                        links[links_cnt++] = link;
--

2.25.1