tools/testing/selftests/bpf/prog_tests/map_btf.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/map_btf.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/map_btf.c- Extension
.c- Size
- 2613 bytes
- Lines
- 99
- 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.hnormal_map_btf.skel.hmap_in_map_btf.skel.h
Detected Declarations
function do_test_normal_map_btffunction do_test_map_in_map_btffunction test_map_btf
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2023. Huawei Technologies Co., Ltd */
#include <test_progs.h>
#include "normal_map_btf.skel.h"
#include "map_in_map_btf.skel.h"
static void do_test_normal_map_btf(void)
{
struct normal_map_btf *skel;
int i, err, new_fd = -1;
int map_fd_arr[64];
skel = normal_map_btf__open_and_load();
if (!ASSERT_OK_PTR(skel, "open_load"))
return;
err = normal_map_btf__attach(skel);
if (!ASSERT_OK(err, "attach"))
goto out;
skel->bss->pid = getpid();
usleep(1);
ASSERT_TRUE(skel->bss->done, "done");
/* Use percpu_array to slow bpf_map_free_deferred() down.
* The memory allocation may fail, so doesn't check the returned fd.
*/
for (i = 0; i < ARRAY_SIZE(map_fd_arr); i++)
map_fd_arr[i] = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, NULL, 4, 4, 256, NULL);
/* Close array fd later */
new_fd = dup(bpf_map__fd(skel->maps.array));
out:
normal_map_btf__destroy(skel);
if (new_fd < 0)
return;
/* Use kern_sync_rcu() to wait for the start of the free of the bpf
* program and use an assumed delay to wait for the release of the map
* btf which is held by other maps (e.g, bss). After that, array map
* holds the last reference of map btf.
*/
kern_sync_rcu();
usleep(4000);
/* Spawn multiple kworkers to delay the invocation of
* bpf_map_free_deferred() for array map.
*/
for (i = 0; i < ARRAY_SIZE(map_fd_arr); i++) {
if (map_fd_arr[i] < 0)
continue;
close(map_fd_arr[i]);
}
close(new_fd);
}
static void do_test_map_in_map_btf(void)
{
int err, zero = 0, new_fd = -1;
struct map_in_map_btf *skel;
skel = map_in_map_btf__open_and_load();
if (!ASSERT_OK_PTR(skel, "open_load"))
return;
err = map_in_map_btf__attach(skel);
if (!ASSERT_OK(err, "attach"))
goto out;
skel->bss->pid = getpid();
usleep(1);
ASSERT_TRUE(skel->bss->done, "done");
/* Close inner_array fd later */
new_fd = dup(bpf_map__fd(skel->maps.inner_array));
/* Defer the free of inner_array */
err = bpf_map__delete_elem(skel->maps.outer_array, &zero, sizeof(zero), 0);
ASSERT_OK(err, "delete inner map");
out:
map_in_map_btf__destroy(skel);
if (new_fd < 0)
return;
/* Use kern_sync_rcu() to wait for the start of the free of the bpf
* program and use an assumed delay to wait for the free of the outer
* map and the release of map btf. After that, inner map holds the last
* reference of map btf.
*/
kern_sync_rcu();
usleep(10000);
close(new_fd);
}
Annotation
- Immediate include surface: `test_progs.h`, `normal_map_btf.skel.h`, `map_in_map_btf.skel.h`.
- Detected declarations: `function do_test_normal_map_btf`, `function do_test_map_in_map_btf`, `function test_map_btf`.
- 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.