xref: /linux/tools/testing/selftests/bpf/progs/task_storage_nodeadlock.c (revision 37a93dd5c49b5fda807fd204edf2547c3493319c)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "vmlinux.h"
4 #include <errno.h>
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 
8 char _license[] SEC("license") = "GPL";
9 
10 extern bool CONFIG_PREEMPTION __kconfig __weak;
11 int nr_get_errs = 0;
12 int nr_del_errs = 0;
13 
14 struct {
15 	__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
16 	__uint(map_flags, BPF_F_NO_PREALLOC);
17 	__type(key, int);
18 	__type(value, int);
19 } task_storage SEC(".maps");
20 
21 SEC("lsm.s/socket_post_create")
22 int BPF_PROG(socket_post_create, struct socket *sock, int family, int type,
23 	     int protocol, int kern)
24 {
25 	struct task_struct *task;
26 	int ret, zero = 0;
27 	int *value;
28 
29 	if (!CONFIG_PREEMPTION)
30 		return 0;
31 
32 	task = bpf_get_current_task_btf();
33 	value = bpf_task_storage_get(&task_storage, task, &zero,
34 				     BPF_LOCAL_STORAGE_GET_F_CREATE);
35 	if (!value)
36 		__sync_fetch_and_add(&nr_get_errs, 1);
37 
38 	ret = bpf_task_storage_delete(&task_storage,
39 				      bpf_get_current_task_btf());
40 	if (ret == -EDEADLK || ret == -ETIMEDOUT)
41 		__sync_fetch_and_add(&nr_del_errs, 1);
42 
43 	return 0;
44 }
45