xref: /linux/tools/testing/selftests/bpf/progs/preempt_lock.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <vmlinux.h>
3 #include <bpf/bpf_helpers.h>
4 #include <bpf/bpf_tracing.h>
5 #include "bpf_misc.h"
6 #include "bpf_experimental.h"
7 
8 SEC("?tc")
9 __failure __msg("1 bpf_preempt_enable is missing")
10 int preempt_lock_missing_1(struct __sk_buff *ctx)
11 {
12 	bpf_preempt_disable();
13 	return 0;
14 }
15 
16 SEC("?tc")
17 __failure __msg("2 bpf_preempt_enable(s) are missing")
18 int preempt_lock_missing_2(struct __sk_buff *ctx)
19 {
20 	bpf_preempt_disable();
21 	bpf_preempt_disable();
22 	return 0;
23 }
24 
25 SEC("?tc")
26 __failure __msg("3 bpf_preempt_enable(s) are missing")
27 int preempt_lock_missing_3(struct __sk_buff *ctx)
28 {
29 	bpf_preempt_disable();
30 	bpf_preempt_disable();
31 	bpf_preempt_disable();
32 	return 0;
33 }
34 
35 SEC("?tc")
36 __failure __msg("1 bpf_preempt_enable is missing")
37 int preempt_lock_missing_3_minus_2(struct __sk_buff *ctx)
38 {
39 	bpf_preempt_disable();
40 	bpf_preempt_disable();
41 	bpf_preempt_disable();
42 	bpf_preempt_enable();
43 	bpf_preempt_enable();
44 	return 0;
45 }
46 
47 static __noinline void preempt_disable(void)
48 {
49 	bpf_preempt_disable();
50 }
51 
52 static __noinline void preempt_enable(void)
53 {
54 	bpf_preempt_enable();
55 }
56 
57 SEC("?tc")
58 __failure __msg("1 bpf_preempt_enable is missing")
59 int preempt_lock_missing_1_subprog(struct __sk_buff *ctx)
60 {
61 	preempt_disable();
62 	return 0;
63 }
64 
65 SEC("?tc")
66 __failure __msg("2 bpf_preempt_enable(s) are missing")
67 int preempt_lock_missing_2_subprog(struct __sk_buff *ctx)
68 {
69 	preempt_disable();
70 	preempt_disable();
71 	return 0;
72 }
73 
74 SEC("?tc")
75 __failure __msg("1 bpf_preempt_enable is missing")
76 int preempt_lock_missing_2_minus_1_subprog(struct __sk_buff *ctx)
77 {
78 	preempt_disable();
79 	preempt_disable();
80 	preempt_enable();
81 	return 0;
82 }
83 
84 static __noinline void preempt_balance_subprog(void)
85 {
86 	preempt_disable();
87 	preempt_enable();
88 }
89 
90 SEC("?tc")
91 __success int preempt_balance(struct __sk_buff *ctx)
92 {
93 	bpf_guard_preempt();
94 	return 0;
95 }
96 
97 SEC("?tc")
98 __success int preempt_balance_subprog_test(struct __sk_buff *ctx)
99 {
100 	preempt_balance_subprog();
101 	return 0;
102 }
103 
104 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
105 __failure __msg("sleepable helper bpf_copy_from_user#")
106 int preempt_sleepable_helper(void *ctx)
107 {
108 	u32 data;
109 
110 	bpf_preempt_disable();
111 	bpf_copy_from_user(&data, sizeof(data), NULL);
112 	bpf_preempt_enable();
113 	return 0;
114 }
115 
116 int __noinline preempt_global_subprog(void)
117 {
118 	preempt_balance_subprog();
119 	return 0;
120 }
121 
122 SEC("?tc")
123 __failure __msg("global function calls are not allowed with preemption disabled")
124 int preempt_global_subprog_test(struct __sk_buff *ctx)
125 {
126 	preempt_disable();
127 	preempt_global_subprog();
128 	preempt_enable();
129 	return 0;
130 }
131 
132 char _license[] SEC("license") = "GPL";
133