tools/testing/selftests/bpf/prog_tests/refcounted_kptr.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/refcounted_kptr.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/refcounted_kptr.c- Extension
.c- Size
- 3042 bytes
- Lines
- 107
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
test_progs.hnetwork_helpers.hrefcounted_kptr.skel.hrefcounted_kptr_fail.skel.h
Detected Declarations
function test_refcounted_kptrfunction test_refcounted_kptr_failfunction test_refcounted_kptr_wrong_ownerfunction test_percpu_hash_refcounted_kptr_refcount_leak
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
#include <test_progs.h>
#include <network_helpers.h>
#include "refcounted_kptr.skel.h"
#include "refcounted_kptr_fail.skel.h"
void test_refcounted_kptr(void)
{
RUN_TESTS(refcounted_kptr);
}
void test_refcounted_kptr_fail(void)
{
RUN_TESTS(refcounted_kptr_fail);
}
void test_refcounted_kptr_wrong_owner(void)
{
LIBBPF_OPTS(bpf_test_run_opts, opts,
.data_in = &pkt_v4,
.data_size_in = sizeof(pkt_v4),
.repeat = 1,
);
struct refcounted_kptr *skel;
int ret;
skel = refcounted_kptr__open_and_load();
if (!ASSERT_OK_PTR(skel, "refcounted_kptr__open_and_load"))
return;
ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_wrong_owner_remove_fail_a1), &opts);
ASSERT_OK(ret, "rbtree_wrong_owner_remove_fail_a1");
ASSERT_OK(opts.retval, "rbtree_wrong_owner_remove_fail_a1 retval");
ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_wrong_owner_remove_fail_b), &opts);
ASSERT_OK(ret, "rbtree_wrong_owner_remove_fail_b");
ASSERT_OK(opts.retval, "rbtree_wrong_owner_remove_fail_b retval");
ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.rbtree_wrong_owner_remove_fail_a2), &opts);
ASSERT_OK(ret, "rbtree_wrong_owner_remove_fail_a2");
ASSERT_OK(opts.retval, "rbtree_wrong_owner_remove_fail_a2 retval");
refcounted_kptr__destroy(skel);
}
void test_percpu_hash_refcounted_kptr_refcount_leak(void)
{
struct refcounted_kptr *skel;
int cpu_nr, fd, err, key = 0;
struct bpf_map *map;
size_t values_sz;
u64 *values;
LIBBPF_OPTS(bpf_test_run_opts, opts,
.data_in = &pkt_v4,
.data_size_in = sizeof(pkt_v4),
.repeat = 1,
);
LIBBPF_OPTS(bpf_test_run_opts, syscall_opts);
cpu_nr = libbpf_num_possible_cpus();
if (!ASSERT_GT(cpu_nr, 0, "libbpf_num_possible_cpus"))
return;
values = calloc(cpu_nr, sizeof(u64));
if (!ASSERT_OK_PTR(values, "calloc values"))
return;
skel = refcounted_kptr__open_and_load();
if (!ASSERT_OK_PTR(skel, "refcounted_kptr__open_and_load")) {
free(values);
return;
}
values_sz = cpu_nr * sizeof(u64);
memset(values, 0, values_sz);
map = skel->maps.percpu_hash;
err = bpf_map__update_elem(map, &key, sizeof(key), values, values_sz, 0);
if (!ASSERT_OK(err, "bpf_map__update_elem"))
goto out;
fd = bpf_program__fd(skel->progs.percpu_hash_refcount_leak);
err = bpf_prog_test_run_opts(fd, &opts);
if (!ASSERT_OK(err, "bpf_prog_test_run_opts"))
goto out;
if (!ASSERT_EQ(opts.retval, 2, "opts.retval"))
goto out;
Annotation
- Immediate include surface: `test_progs.h`, `network_helpers.h`, `refcounted_kptr.skel.h`, `refcounted_kptr_fail.skel.h`.
- Detected declarations: `function test_refcounted_kptr`, `function test_refcounted_kptr_fail`, `function test_refcounted_kptr_wrong_owner`, `function test_percpu_hash_refcounted_kptr_refcount_leak`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.