diff mbox series

[bpf-next,v5,11/12] bpf: selftests: Fix fd cleanup in get_branch_snapshot

Message ID 20210927145941.1383001-12-memxor@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Support kernel module function calls from eBPF | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers fail 1 blamed authors not CCed: john.fastabend@gmail.com; 4 maintainers not CCed: kpsingh@kernel.org shuah@kernel.org linux-kselftest@vger.kernel.org john.fastabend@gmail.com
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 12 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link
bpf/vmtest-bpf-next success VM_Test
bpf/vmtest-bpf-next-PR success PR summary

Commit Message

Kumar Kartikeya Dwivedi Sept. 27, 2021, 2:59 p.m. UTC
Cleanup code uses while (cpu++ < cpu_cnt) for closing fds, which means
it starts iterating from 1 for closing fds. If the first fd is -1, it
skips over it and closes garbage fds (typically zero) in the remaining
array. This leads to test failures for future tests when they end up
storing fd 0 (as the slot becomes free due to close(0)) in ldimm64's BTF
fd, ending up trying to match module BTF id with vmlinux.

This was observed as spurious CI failure for the ksym_module_libbpf and
module_attach tests. The test ends up closing fd 0 and breaking libbpf's
assumption that module BTF fd will always be > 0, which leads to the
kernel thinking that we are pointing to a BTF ID in vmlinux BTF.

Cc: Song Liu <songliubraving@fb.com>
Fixes: 025bd7c753aa (selftests/bpf: Add test for bpf_get_branch_snapshot)
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Comments

Song Liu Sept. 28, 2021, 11:58 p.m. UTC | #1
> On Sep 27, 2021, at 7:59 AM, Kumar Kartikeya Dwivedi <memxor@gmail.com> wrote:
> 
> Cleanup code uses while (cpu++ < cpu_cnt) for closing fds, which means
> it starts iterating from 1 for closing fds. If the first fd is -1, it
> skips over it and closes garbage fds (typically zero) in the remaining
> array. This leads to test failures for future tests when they end up
> storing fd 0 (as the slot becomes free due to close(0)) in ldimm64's BTF
> fd, ending up trying to match module BTF id with vmlinux.
> 
> This was observed as spurious CI failure for the ksym_module_libbpf and
> module_attach tests. The test ends up closing fd 0 and breaking libbpf's
> assumption that module BTF fd will always be > 0, which leads to the
> kernel thinking that we are pointing to a BTF ID in vmlinux BTF.
> 
> Cc: Song Liu <songliubraving@fb.com>
> Fixes: 025bd7c753aa (selftests/bpf: Add test for bpf_get_branch_snapshot)
> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>

Thanks for the fix!

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

> ---
> tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c | 5 ++---
> 1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c b/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c
> index f81db9135ae4..67e86f8d8677 100644
> --- a/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c
> +++ b/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c
> @@ -38,10 +38,9 @@ static int create_perf_events(void)
> 
> static void close_perf_events(void)
> {
> -	int cpu = 0;
> -	int fd;
> +	int cpu, fd;
> 
> -	while (cpu++ < cpu_cnt) {
> +	for (cpu = 0; cpu < cpu_cnt; cpu++) {
> 		fd = pfd_array[cpu];
> 		if (fd < 0)
> 			break;
> -- 
> 2.33.0
>
Alexei Starovoitov Sept. 29, 2021, 8:26 p.m. UTC | #2
On Tue, Sep 28, 2021 at 4:58 PM Song Liu <songliubraving@fb.com> wrote:
>
>
>
> > On Sep 27, 2021, at 7:59 AM, Kumar Kartikeya Dwivedi <memxor@gmail.com> wrote:
> >
> > Cleanup code uses while (cpu++ < cpu_cnt) for closing fds, which means
> > it starts iterating from 1 for closing fds. If the first fd is -1, it
> > skips over it and closes garbage fds (typically zero) in the remaining
> > array. This leads to test failures for future tests when they end up
> > storing fd 0 (as the slot becomes free due to close(0)) in ldimm64's BTF
> > fd, ending up trying to match module BTF id with vmlinux.
> >
> > This was observed as spurious CI failure for the ksym_module_libbpf and
> > module_attach tests. The test ends up closing fd 0 and breaking libbpf's
> > assumption that module BTF fd will always be > 0, which leads to the
> > kernel thinking that we are pointing to a BTF ID in vmlinux BTF.
> >
> > Cc: Song Liu <songliubraving@fb.com>
> > Fixes: 025bd7c753aa (selftests/bpf: Add test for bpf_get_branch_snapshot)
> > Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
>
> Thanks for the fix!
>
> Acked-by: Song Liu <songliubraving@fb.com>

Applied this fix to bpf-next.
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c b/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c
index f81db9135ae4..67e86f8d8677 100644
--- a/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c
+++ b/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c
@@ -38,10 +38,9 @@  static int create_perf_events(void)
 
 static void close_perf_events(void)
 {
-	int cpu = 0;
-	int fd;
+	int cpu, fd;
 
-	while (cpu++ < cpu_cnt) {
+	for (cpu = 0; cpu < cpu_cnt; cpu++) {
 		fd = pfd_array[cpu];
 		if (fd < 0)
 			break;