xref: /linux/tools/testing/selftests/bpf/progs/task_ls_recursion.c (revision 23b0f90ba871f096474e1c27c3d14f455189d2d9)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3 
4 #include "vmlinux.h"
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 
8 #ifndef EBUSY
9 #define EBUSY 16
10 #endif
11 
12 char _license[] SEC("license") = "GPL";
13 int nr_del_errs = 0;
14 int test_pid = 0;
15 
16 struct {
17 	__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
18 	__uint(map_flags, BPF_F_NO_PREALLOC);
19 	__type(key, int);
20 	__type(value, long);
21 } map_a SEC(".maps");
22 
23 struct {
24 	__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
25 	__uint(map_flags, BPF_F_NO_PREALLOC);
26 	__type(key, int);
27 	__type(value, long);
28 } map_b SEC(".maps");
29 
30 SEC("fentry/bpf_local_storage_update")
31 int BPF_PROG(on_update)
32 {
33 	struct task_struct *task = bpf_get_current_task_btf();
34 	long *ptr;
35 
36 	if (!test_pid || task->pid != test_pid)
37 		return 0;
38 
39 	/* This will succeed as there is no real deadlock */
40 	ptr = bpf_task_storage_get(&map_a, task, 0,
41 				   BPF_LOCAL_STORAGE_GET_F_CREATE);
42 	if (ptr) {
43 		int err;
44 
45 		*ptr += 1;
46 		err = bpf_task_storage_delete(&map_a, task);
47 		if (err == -EBUSY)
48 			nr_del_errs++;
49 	}
50 
51 	/* This will succeed as there is no real deadlock */
52 	ptr = bpf_task_storage_get(&map_b, task, 0,
53 				   BPF_LOCAL_STORAGE_GET_F_CREATE);
54 	if (ptr)
55 		*ptr += 1;
56 
57 	return 0;
58 }
59 
60 SEC("tp_btf/sys_enter")
61 int BPF_PROG(on_enter, struct pt_regs *regs, long id)
62 {
63 	struct task_struct *task;
64 	long *ptr;
65 
66 	task = bpf_get_current_task_btf();
67 	if (!test_pid || task->pid != test_pid)
68 		return 0;
69 
70 	ptr = bpf_task_storage_get(&map_a, task, 0,
71 				   BPF_LOCAL_STORAGE_GET_F_CREATE);
72 	if (ptr && !*ptr)
73 		*ptr = 200;
74 
75 	ptr = bpf_task_storage_get(&map_b, task, 0,
76 				   BPF_LOCAL_STORAGE_GET_F_CREATE);
77 	if (ptr && !*ptr)
78 		*ptr = 100;
79 	return 0;
80 }
81