xref: /linux/tools/testing/selftests/bpf/progs/task_work_stress.c (revision c6ae18e0af5e7c809ac26350043924e062dbb76f)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3 
4 #include <vmlinux.h>
5 #include <string.h>
6 #include <stdbool.h>
7 #include <bpf/bpf_helpers.h>
8 #include <bpf/bpf_tracing.h>
9 #include "bpf_misc.h"
10 
11 #define ENTRIES 128
12 
13 char _license[] SEC("license") = "GPL";
14 
15 __u64 callback_scheduled = 0;
16 __u64 callback_success = 0;
17 __u64 schedule_error = 0;
18 __u64 delete_success = 0;
19 
20 struct elem {
21 	__u32 count;
22 	struct bpf_task_work tw;
23 };
24 
25 struct {
26 	__uint(type, BPF_MAP_TYPE_HASH);
27 	__uint(map_flags, BPF_F_NO_PREALLOC);
28 	__uint(max_entries, ENTRIES);
29 	__type(key, int);
30 	__type(value, struct elem);
31 } hmap SEC(".maps");
32 
33 static int process_work(struct bpf_map *map, void *key, void *value)
34 {
35 	__sync_fetch_and_add(&callback_success, 1);
36 	return 0;
37 }
38 
39 SEC("syscall")
40 int schedule_task_work(void *ctx)
41 {
42 	struct elem empty_work = {.count = 0};
43 	struct elem *work;
44 	int key = 0, err;
45 
46 	key = bpf_ktime_get_ns() % ENTRIES;
47 	work = bpf_map_lookup_elem(&hmap, &key);
48 	if (!work) {
49 		bpf_map_update_elem(&hmap, &key, &empty_work, BPF_NOEXIST);
50 		work = bpf_map_lookup_elem(&hmap, &key);
51 		if (!work)
52 			return 0;
53 	}
54 	err = bpf_task_work_schedule_signal(bpf_get_current_task_btf(), &work->tw, &hmap,
55 					    process_work, NULL);
56 	if (err)
57 		__sync_fetch_and_add(&schedule_error, 1);
58 	else
59 		__sync_fetch_and_add(&callback_scheduled, 1);
60 	return 0;
61 }
62 
63 SEC("syscall")
64 int delete_task_work(void *ctx)
65 {
66 	int key = 0, err;
67 
68 	key = bpf_get_prandom_u32() % ENTRIES;
69 	err = bpf_map_delete_elem(&hmap, &key);
70 	if (!err)
71 		__sync_fetch_and_add(&delete_success, 1);
72 	return 0;
73 }
74