bpf: selftests: Fix fd cleanup in get_branch_snapshot
authorKumar Kartikeya Dwivedi <memxor@gmail.com>
Mon, 27 Sep 2021 14:59:40 +0000 (20:29 +0530)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 29 Sep 2021 20:25:09 +0000 (13:25 -0700)
commite31eec77e4ab90dcec7d2da93415f839098dc287
treeff59a1f5af8c9e40b522b853b6c22a6ae55d6ec7
parent3103836496e75095ac208cc180a9fe8f7ff33fd8
bpf: selftests: Fix fd cleanup in get_branch_snapshot

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.

Fixes: 025bd7c753aa (selftests/bpf: Add test for bpf_get_branch_snapshot)
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20210927145941.1383001-12-memxor@gmail.com
tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c