1 // SPDX-License-Identifier: GPL-2.0
3 #include <bpf/bpf_helpers.h>
4 #include <bpf/bpf_tracing.h>
6 #include "bpf_experimental.h"
8 extern int bpf_copy_from_user_str(void *dst, u32 dst__sz, const void *unsafe_ptr__ign, u64 flags) __weak __ksym;
11 __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region")
12 int preempt_lock_missing_1(struct __sk_buff *ctx)
14 bpf_preempt_disable();
19 __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region")
20 int preempt_lock_missing_2(struct __sk_buff *ctx)
22 bpf_preempt_disable();
23 bpf_preempt_disable();
28 __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region")
29 int preempt_lock_missing_3(struct __sk_buff *ctx)
31 bpf_preempt_disable();
32 bpf_preempt_disable();
33 bpf_preempt_disable();
38 __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region")
39 int preempt_lock_missing_3_minus_2(struct __sk_buff *ctx)
41 bpf_preempt_disable();
42 bpf_preempt_disable();
43 bpf_preempt_disable();
49 static __noinline void preempt_disable(void)
51 bpf_preempt_disable();
54 static __noinline void preempt_enable(void)
60 __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region")
61 int preempt_lock_missing_1_subprog(struct __sk_buff *ctx)
68 __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region")
69 int preempt_lock_missing_2_subprog(struct __sk_buff *ctx)
77 __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region")
78 int preempt_lock_missing_2_minus_1_subprog(struct __sk_buff *ctx)
86 static __noinline void preempt_balance_subprog(void)
93 __success int preempt_balance(struct __sk_buff *ctx)
100 __success int preempt_balance_subprog_test(struct __sk_buff *ctx)
102 preempt_balance_subprog();
106 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
107 __failure __msg("sleepable helper bpf_copy_from_user#")
108 int preempt_sleepable_helper(void *ctx)
112 bpf_preempt_disable();
113 bpf_copy_from_user(&data, sizeof(data), NULL);
114 bpf_preempt_enable();
118 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
119 __failure __msg("kernel func bpf_copy_from_user_str is sleepable within non-preemptible region")
120 int preempt_sleepable_kfunc(void *ctx)
124 bpf_preempt_disable();
125 bpf_copy_from_user_str(&data, sizeof(data), NULL, 0);
126 bpf_preempt_enable();
130 int __noinline preempt_global_subprog(void)
132 preempt_balance_subprog();
137 __failure __msg("global function calls are not allowed with preemption disabled")
138 int preempt_global_subprog_test(struct __sk_buff *ctx)
141 preempt_global_subprog();
146 char _license[] SEC("license") = "GPL";