tools/testing/selftests/bpf/prog_tests/test_task_work.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/test_task_work.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/test_task_work.c- Extension
.c- Size
- 3797 bytes
- Lines
- 158
- 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.hstring.hstdio.htask_work.skel.htask_work_fail.skel.hlinux/bpf.hlinux/perf_event.hsys/syscall.htime.h
Detected Declarations
struct elemfunction perf_event_openfunction verify_mapfunction task_work_runfunction bpf_object__for_each_programfunction test_task_work
Annotated Snippet
struct elem {
char data[128];
struct bpf_task_work tw;
};
static int verify_map(struct bpf_map *map, const char *expected_data)
{
int err;
struct elem value;
int processed_values = 0;
int k, sz;
sz = bpf_map__max_entries(map);
for (k = 0; k < sz; ++k) {
err = bpf_map__lookup_elem(map, &k, sizeof(int), &value, sizeof(struct elem), 0);
if (err)
continue;
if (!ASSERT_EQ(strcmp(expected_data, value.data), 0, "map data")) {
fprintf(stderr, "expected '%s', found '%s' in %s map", expected_data,
value.data, bpf_map__name(map));
return 2;
}
processed_values++;
}
return processed_values == 0;
}
static void task_work_run(const char *prog_name, const char *map_name)
{
struct task_work *skel;
struct bpf_program *prog;
struct bpf_map *map;
struct bpf_link *link = NULL;
int err, pe_fd = -1, pid, status, pipefd[2];
char user_string[] = "hello world";
if (!ASSERT_NEQ(pipe(pipefd), -1, "pipe"))
return;
pid = fork();
if (pid == 0) {
__u64 num = 1;
int i;
char buf;
close(pipefd[1]);
read(pipefd[0], &buf, sizeof(buf));
close(pipefd[0]);
for (i = 0; i < 10000; ++i)
num *= time(0) % 7;
(void)num;
exit(0);
}
if (!ASSERT_GT(pid, 0, "fork() failed")) {
close(pipefd[0]);
close(pipefd[1]);
return;
}
skel = task_work__open();
if (!ASSERT_OK_PTR(skel, "task_work__open"))
return;
bpf_object__for_each_program(prog, skel->obj) {
bpf_program__set_autoload(prog, false);
}
prog = bpf_object__find_program_by_name(skel->obj, prog_name);
if (!ASSERT_OK_PTR(prog, "prog_name"))
goto cleanup;
bpf_program__set_autoload(prog, true);
skel->bss->user_ptr = (char *)user_string;
err = task_work__load(skel);
if (!ASSERT_OK(err, "skel_load"))
goto cleanup;
pe_fd = perf_event_open(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES, pid);
if (pe_fd == -1 && (errno == ENOENT || errno == EOPNOTSUPP)) {
printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n", __func__);
test__skip();
goto cleanup;
}
if (!ASSERT_NEQ(pe_fd, -1, "pe_fd")) {
fprintf(stderr, "perf_event_open errno: %d, pid: %d\n", errno, pid);
goto cleanup;
}
Annotation
- Immediate include surface: `test_progs.h`, `string.h`, `stdio.h`, `task_work.skel.h`, `task_work_fail.skel.h`, `linux/bpf.h`, `linux/perf_event.h`, `sys/syscall.h`.
- Detected declarations: `struct elem`, `function perf_event_open`, `function verify_map`, `function task_work_run`, `function bpf_object__for_each_program`, `function test_task_work`.
- 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.