mirror of
https://github.com/torvalds/linux.git
synced 2026-03-08 04:04:43 +01:00
selftests/bpf: Fix resource leaks caused by missing cleanups
ASAN reported a number of resource leaks: - Add missing *__destroy(skel) calls - Replace bpf_link__detach() with bpf_link__destroy() where appropriate - cgrp_local_storage: Add bpf_link__destroy() when bpf_iter_create fails - dynptr: Add missing bpf_object__close() Acked-by: Eduard Zingerman <eddyz87@gmail.com> Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev> Link: https://lore.kernel.org/r/20260223190736.649171-16-ihor.solodrai@linux.dev Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
parent
68b8bea1ee
commit
71dca2950f
7 changed files with 29 additions and 25 deletions
|
|
@ -202,7 +202,7 @@ static void test_cgroup_iter_sleepable(int cgroup_fd, __u64 cgroup_id)
|
|||
|
||||
iter_fd = bpf_iter_create(bpf_link__fd(link));
|
||||
if (!ASSERT_GE(iter_fd, 0, "iter_create"))
|
||||
goto out;
|
||||
goto out_link;
|
||||
|
||||
/* trigger the program run */
|
||||
(void)read(iter_fd, buf, sizeof(buf));
|
||||
|
|
@ -210,6 +210,8 @@ static void test_cgroup_iter_sleepable(int cgroup_fd, __u64 cgroup_id)
|
|||
ASSERT_EQ(skel->bss->cgroup_id, cgroup_id, "cgroup_id");
|
||||
|
||||
close(iter_fd);
|
||||
out_link:
|
||||
bpf_link__destroy(link);
|
||||
out:
|
||||
cgrp_ls_sleepable__destroy(skel);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -137,11 +137,14 @@ static void verify_success(const char *prog_name, enum test_setup_type setup_typ
|
|||
);
|
||||
|
||||
link = bpf_program__attach(prog);
|
||||
if (!ASSERT_OK_PTR(link, "bpf_program__attach"))
|
||||
if (!ASSERT_OK_PTR(link, "bpf_program__attach")) {
|
||||
bpf_object__close(obj);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
err = bpf_prog_test_run_opts(aux_prog_fd, &topts);
|
||||
bpf_link__destroy(link);
|
||||
bpf_object__close(obj);
|
||||
|
||||
if (!ASSERT_OK(err, "test_run"))
|
||||
goto cleanup;
|
||||
|
|
|
|||
|
|
@ -204,7 +204,7 @@ static void test_skmsg_helpers_with_link(enum bpf_map_type map_type)
|
|||
/* Fail since bpf_link for the same prog type has been created. */
|
||||
link2 = bpf_program__attach_sockmap(prog_clone, map);
|
||||
if (!ASSERT_ERR_PTR(link2, "bpf_program__attach_sockmap")) {
|
||||
bpf_link__detach(link2);
|
||||
bpf_link__destroy(link2);
|
||||
goto out;
|
||||
}
|
||||
|
||||
|
|
@ -230,7 +230,7 @@ static void test_skmsg_helpers_with_link(enum bpf_map_type map_type)
|
|||
if (!ASSERT_OK(err, "bpf_link_update"))
|
||||
goto out;
|
||||
out:
|
||||
bpf_link__detach(link);
|
||||
bpf_link__destroy(link);
|
||||
test_skmsg_load_helpers__destroy(skel);
|
||||
}
|
||||
|
||||
|
|
@ -417,7 +417,7 @@ static void test_sockmap_skb_verdict_attach_with_link(void)
|
|||
if (!ASSERT_OK_PTR(link, "bpf_program__attach_sockmap"))
|
||||
goto out;
|
||||
|
||||
bpf_link__detach(link);
|
||||
bpf_link__destroy(link);
|
||||
|
||||
err = bpf_prog_attach(bpf_program__fd(prog), map, BPF_SK_SKB_STREAM_VERDICT, 0);
|
||||
if (!ASSERT_OK(err, "bpf_prog_attach"))
|
||||
|
|
@ -426,7 +426,7 @@ static void test_sockmap_skb_verdict_attach_with_link(void)
|
|||
/* Fail since attaching with the same prog/map has been done. */
|
||||
link = bpf_program__attach_sockmap(prog, map);
|
||||
if (!ASSERT_ERR_PTR(link, "bpf_program__attach_sockmap"))
|
||||
bpf_link__detach(link);
|
||||
bpf_link__destroy(link);
|
||||
|
||||
err = bpf_prog_detach2(bpf_program__fd(prog), map, BPF_SK_SKB_STREAM_VERDICT);
|
||||
if (!ASSERT_OK(err, "bpf_prog_detach2"))
|
||||
|
|
@ -747,13 +747,13 @@ static void test_sockmap_skb_verdict_peek_with_link(void)
|
|||
test_sockmap_skb_verdict_peek_helper(map);
|
||||
ASSERT_EQ(pass->bss->clone_called, 1, "clone_called");
|
||||
out:
|
||||
bpf_link__detach(link);
|
||||
bpf_link__destroy(link);
|
||||
test_sockmap_pass_prog__destroy(pass);
|
||||
}
|
||||
|
||||
static void test_sockmap_unconnected_unix(void)
|
||||
{
|
||||
int err, map, stream = 0, dgram = 0, zero = 0;
|
||||
int err, map, stream = -1, dgram = -1, zero = 0;
|
||||
struct test_sockmap_pass_prog *skel;
|
||||
|
||||
skel = test_sockmap_pass_prog__open_and_load();
|
||||
|
|
@ -764,22 +764,22 @@ static void test_sockmap_unconnected_unix(void)
|
|||
|
||||
stream = xsocket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (stream < 0)
|
||||
return;
|
||||
goto out;
|
||||
|
||||
dgram = xsocket(AF_UNIX, SOCK_DGRAM, 0);
|
||||
if (dgram < 0) {
|
||||
close(stream);
|
||||
return;
|
||||
}
|
||||
if (dgram < 0)
|
||||
goto out;
|
||||
|
||||
err = bpf_map_update_elem(map, &zero, &stream, BPF_ANY);
|
||||
ASSERT_ERR(err, "bpf_map_update_elem(stream)");
|
||||
if (!ASSERT_ERR(err, "bpf_map_update_elem(stream)"))
|
||||
goto out;
|
||||
|
||||
err = bpf_map_update_elem(map, &zero, &dgram, BPF_ANY);
|
||||
ASSERT_OK(err, "bpf_map_update_elem(dgram)");
|
||||
|
||||
out:
|
||||
close(stream);
|
||||
close(dgram);
|
||||
test_sockmap_pass_prog__destroy(skel);
|
||||
}
|
||||
|
||||
static void test_sockmap_many_socket(void)
|
||||
|
|
@ -1027,7 +1027,7 @@ static void test_sockmap_skb_verdict_vsock_poll(void)
|
|||
if (xrecv_nonblock(conn, &buf, 1, 0) != 1)
|
||||
FAIL("xrecv_nonblock");
|
||||
detach:
|
||||
bpf_link__detach(link);
|
||||
bpf_link__destroy(link);
|
||||
close:
|
||||
xclose(conn);
|
||||
xclose(peer);
|
||||
|
|
|
|||
|
|
@ -899,7 +899,7 @@ static void test_msg_redir_to_listening_with_link(struct test_sockmap_listen *sk
|
|||
|
||||
redir_to_listening(family, sotype, sock_map, verdict_map, REDIR_EGRESS);
|
||||
|
||||
bpf_link__detach(link);
|
||||
bpf_link__destroy(link);
|
||||
}
|
||||
|
||||
static void redir_partial(int family, int sotype, int sock_map, int parser_map)
|
||||
|
|
|
|||
|
|
@ -54,9 +54,7 @@ static void test_private_stack_fail(void)
|
|||
}
|
||||
|
||||
err = struct_ops_private_stack_fail__load(skel);
|
||||
if (!ASSERT_ERR(err, "struct_ops_private_stack_fail__load"))
|
||||
goto cleanup;
|
||||
return;
|
||||
ASSERT_ERR(err, "struct_ops_private_stack_fail__load");
|
||||
|
||||
cleanup:
|
||||
struct_ops_private_stack_fail__destroy(skel);
|
||||
|
|
|
|||
|
|
@ -1360,10 +1360,8 @@ static void test_tc_opts_dev_cleanup_target(int target)
|
|||
|
||||
assert_mprog_count_ifindex(ifindex, target, 4);
|
||||
|
||||
ASSERT_OK(system("ip link del dev tcx_opts1"), "del veth");
|
||||
ASSERT_EQ(if_nametoindex("tcx_opts1"), 0, "dev1_removed");
|
||||
ASSERT_EQ(if_nametoindex("tcx_opts2"), 0, "dev2_removed");
|
||||
return;
|
||||
goto cleanup;
|
||||
|
||||
cleanup3:
|
||||
err = bpf_prog_detach_opts(fd3, loopback, target, &optd);
|
||||
ASSERT_OK(err, "prog_detach");
|
||||
|
|
|
|||
|
|
@ -699,7 +699,7 @@ void test_tc_tunnel(void)
|
|||
return;
|
||||
|
||||
if (!ASSERT_OK(setup(), "global setup"))
|
||||
return;
|
||||
goto out;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(subtests_cfg); i++) {
|
||||
cfg = &subtests_cfg[i];
|
||||
|
|
@ -711,4 +711,7 @@ void test_tc_tunnel(void)
|
|||
subtest_cleanup(cfg);
|
||||
}
|
||||
cleanup();
|
||||
|
||||
out:
|
||||
test_tc_tunnel__destroy(skel);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue