tools/testing/selftests/bpf/progs/task_work_fail.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/task_work_fail.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/task_work_fail.c
Extension
.c
Size
2149 bytes
Lines
97
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.

Dependency Surface

Detected Declarations

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");

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")
__failure __msg("doesn't match map pointer in R3")
int mismatch_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_resume(task, &work->tw, &hmap, process_work);
	return 0;
}

SEC("perf_event")
__failure __msg("R2 doesn't point to a map value")
int no_map_task_work(struct pt_regs *args)
{
	struct task_struct *task;
	struct bpf_task_work tw;

	task = bpf_get_current_task_btf();
	bpf_task_work_schedule_resume(task, &tw, &hmap, process_work);
	return 0;
}

SEC("perf_event")
__failure __msg("Possibly NULL pointer passed to trusted R2")
int task_work_null(struct pt_regs *args)
{
	struct task_struct *task;

	task = bpf_get_current_task_btf();
	bpf_task_work_schedule_resume(task, NULL, &hmap, process_work);
	return 0;
}

SEC("perf_event")
__failure __msg("Possibly NULL pointer passed to trusted R3")
int map_null(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_resume(task, &work->tw, NULL, process_work);
	return 0;
}

Annotation

Implementation Notes