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