tools/testing/selftests/bpf/progs/task_work.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/task_work.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/task_work.c- Extension
.c- Size
- 2333 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.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
vmlinux.hstring.hstdbool.hbpf/bpf_helpers.hbpf/bpf_tracing.hbpf_misc.herrno.h
Detected Declarations
struct elemfunction process_workfunction oncpu_hash_mapfunction oncpu_array_mapfunction oncpu_lru_map
Annotated Snippet
struct elem {
char data[128];
struct bpf_task_work tw;
};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(map_flags, BPF_F_NO_PREALLOC);
__uint(max_entries, 1);
__type(key, int);
__type(value, struct elem);
} hmap SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1);
__type(key, int);
__type(value, struct elem);
} arrmap SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, 1);
__type(key, int);
__type(value, struct elem);
} lrumap SEC(".maps");
static int process_work(struct bpf_map *map, void *key, void *value)
{
struct elem *work = value;
bpf_copy_from_user_str(work->data, sizeof(work->data), (const void *)user_ptr, 0);
return 0;
}
int key = 0;
SEC("perf_event")
int oncpu_hash_map(struct pt_regs *args)
{
struct elem empty_work = { .data = { 0 } };
struct elem *work;
struct task_struct *task;
int err;
task = bpf_get_current_task_btf();
err = bpf_map_update_elem(&hmap, &key, &empty_work, BPF_NOEXIST);
if (err)
return 0;
work = bpf_map_lookup_elem(&hmap, &key);
if (!work)
return 0;
bpf_task_work_schedule_resume(task, &work->tw, &hmap, process_work);
return 0;
}
SEC("perf_event")
int oncpu_array_map(struct pt_regs *args)
{
struct elem *work;
struct task_struct *task;
task = bpf_get_current_task_btf();
work = bpf_map_lookup_elem(&arrmap, &key);
if (!work)
return 0;
bpf_task_work_schedule_signal(task, &work->tw, &arrmap, process_work);
return 0;
}
SEC("perf_event")
int oncpu_lru_map(struct pt_regs *args)
{
struct elem empty_work = { .data = { 0 } };
struct elem *work;
struct task_struct *task;
int err;
task = bpf_get_current_task_btf();
work = bpf_map_lookup_elem(&lrumap, &key);
if (work)
return 0;
err = bpf_map_update_elem(&lrumap, &key, &empty_work, BPF_NOEXIST);
if (err)
return 0;
work = bpf_map_lookup_elem(&lrumap, &key);
if (!work || work->data[0])
return 0;
bpf_task_work_schedule_resume(task, &work->tw, &lrumap, process_work);
return 0;
Annotation
- Immediate include surface: `vmlinux.h`, `string.h`, `stdbool.h`, `bpf/bpf_helpers.h`, `bpf/bpf_tracing.h`, `bpf_misc.h`, `errno.h`.
- Detected declarations: `struct elem`, `function process_work`, `function oncpu_hash_map`, `function oncpu_array_map`, `function oncpu_lru_map`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
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.