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 extern int bpf_copy_from_user_str(void *dst, u32 dst__sz, const void *unsafe_ptr__ign, u64 flags) __weak __ksym; 9 extern void bpf_rcu_read_lock(void) __ksym; 10 extern void bpf_rcu_read_unlock(void) __ksym; 11 12 SEC("?tc") 13 __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region") 14 int preempt_lock_missing_1(struct __sk_buff *ctx) 15 { 16 bpf_preempt_disable(); 17 return 0; 18 } 19 20 SEC("?tc") 21 __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region") 22 int preempt_lock_missing_2(struct __sk_buff *ctx) 23 { 24 bpf_preempt_disable(); 25 bpf_preempt_disable(); 26 return 0; 27 } 28 29 SEC("?tc") 30 __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region") 31 int preempt_lock_missing_3(struct __sk_buff *ctx) 32 { 33 bpf_preempt_disable(); 34 bpf_preempt_disable(); 35 bpf_preempt_disable(); 36 return 0; 37 } 38 39 SEC("?tc") 40 __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region") 41 int preempt_lock_missing_3_minus_2(struct __sk_buff *ctx) 42 { 43 bpf_preempt_disable(); 44 bpf_preempt_disable(); 45 bpf_preempt_disable(); 46 bpf_preempt_enable(); 47 bpf_preempt_enable(); 48 return 0; 49 } 50 51 static __noinline void preempt_disable(void) 52 { 53 bpf_preempt_disable(); 54 } 55 56 static __noinline void preempt_enable(void) 57 { 58 bpf_preempt_enable(); 59 } 60 61 SEC("?tc") 62 __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region") 63 int preempt_lock_missing_1_subprog(struct __sk_buff *ctx) 64 { 65 preempt_disable(); 66 return 0; 67 } 68 69 SEC("?tc") 70 __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region") 71 int preempt_lock_missing_2_subprog(struct __sk_buff *ctx) 72 { 73 preempt_disable(); 74 preempt_disable(); 75 return 0; 76 } 77 78 SEC("?tc") 79 __failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region") 80 int preempt_lock_missing_2_minus_1_subprog(struct __sk_buff *ctx) 81 { 82 preempt_disable(); 83 preempt_disable(); 84 preempt_enable(); 85 return 0; 86 } 87 88 static __noinline void preempt_balance_subprog(void) 89 { 90 preempt_disable(); 91 preempt_enable(); 92 } 93 94 SEC("?tc") 95 __success int preempt_balance(struct __sk_buff *ctx) 96 { 97 bpf_guard_preempt(); 98 return 0; 99 } 100 101 SEC("?tc") 102 __success int preempt_balance_subprog_test(struct __sk_buff *ctx) 103 { 104 preempt_balance_subprog(); 105 return 0; 106 } 107 108 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") 109 __failure __msg("sleepable helper bpf_copy_from_user#") 110 int preempt_sleepable_helper(void *ctx) 111 { 112 u32 data; 113 114 bpf_preempt_disable(); 115 bpf_copy_from_user(&data, sizeof(data), NULL); 116 bpf_preempt_enable(); 117 return 0; 118 } 119 120 SEC("?uprobe.s") 121 __failure __msg("sleepable helper bpf_get_stack#") 122 int preempt_sleepable_get_stack(struct pt_regs *ctx) 123 { 124 struct bpf_stack_build_id stack; 125 126 bpf_preempt_disable(); 127 bpf_get_stack(ctx, &stack, sizeof(stack), 128 BPF_F_USER_STACK | BPF_F_USER_BUILD_ID); 129 bpf_preempt_enable(); 130 return 0; 131 } 132 133 SEC("?uprobe.s") 134 __failure __msg("sleepable helper bpf_get_task_stack#") 135 int preempt_sleepable_get_task_stack(void *ctx) 136 { 137 struct bpf_stack_build_id stack; 138 struct task_struct *task; 139 140 task = bpf_get_current_task_btf(); 141 bpf_preempt_disable(); 142 bpf_get_task_stack(task, &stack, sizeof(stack), 143 BPF_F_USER_STACK | BPF_F_USER_BUILD_ID); 144 bpf_preempt_enable(); 145 return 0; 146 } 147 148 SEC("?uprobe.s") 149 __success 150 int sleepable_get_stack(struct pt_regs *ctx) 151 { 152 struct bpf_stack_build_id stack; 153 154 bpf_get_stack(ctx, &stack, sizeof(stack), 155 BPF_F_USER_STACK | BPF_F_USER_BUILD_ID); 156 return 0; 157 } 158 159 SEC("?uprobe.s") 160 __success 161 int sleepable_get_task_stack(void *ctx) 162 { 163 struct bpf_stack_build_id stack; 164 struct task_struct *task; 165 166 task = bpf_get_current_task_btf(); 167 bpf_get_task_stack(task, &stack, sizeof(stack), 168 BPF_F_USER_STACK | BPF_F_USER_BUILD_ID); 169 return 0; 170 } 171 172 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") 173 __failure __msg("kernel func bpf_copy_from_user_str is sleepable within non-preemptible region") 174 int preempt_sleepable_kfunc(void *ctx) 175 { 176 u32 data; 177 178 bpf_preempt_disable(); 179 bpf_copy_from_user_str(&data, sizeof(data), NULL, 0); 180 bpf_preempt_enable(); 181 return 0; 182 } 183 184 SEC("?fentry/" SYS_PREFIX "sys_getpgid") 185 __failure __msg("program must be sleepable to call sleepable kfunc bpf_copy_from_user_str") 186 __msg("cannot be used in non-sleepable program") 187 int non_sleepable_kfunc(void *ctx) 188 { 189 u32 data; 190 191 bpf_copy_from_user_str(&data, sizeof(data), NULL, 0); 192 return 0; 193 } 194 195 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") 196 __failure __msg("kernel func bpf_copy_from_user_str is sleepable within rcu_read_lock region") 197 __msg("cannot be used in RCU read lock region") 198 int sleepable_kfunc_in_rcu(void *ctx) 199 { 200 u32 data; 201 202 bpf_rcu_read_lock(); 203 bpf_copy_from_user_str(&data, sizeof(data), NULL, 0); 204 bpf_rcu_read_unlock(); 205 return 0; 206 } 207 208 int __noinline preempt_global_subprog(void) 209 { 210 preempt_balance_subprog(); 211 return 0; 212 } 213 214 SEC("?tc") 215 __success 216 int preempt_global_subprog_test(struct __sk_buff *ctx) 217 { 218 preempt_disable(); 219 preempt_global_subprog(); 220 preempt_enable(); 221 return 0; 222 } 223 224 int __noinline 225 global_subprog(int i) 226 { 227 if (i) 228 bpf_printk("%p", &i); 229 return i; 230 } 231 232 int __noinline 233 global_sleepable_helper_subprog(int i) 234 { 235 if (i) 236 bpf_copy_from_user(&i, sizeof(i), NULL); 237 return i; 238 } 239 240 int __noinline 241 global_sleepable_kfunc_subprog(int i) 242 { 243 if (i) 244 bpf_copy_from_user_str(&i, sizeof(i), NULL, 0); 245 global_subprog(i); 246 return i; 247 } 248 249 int __noinline 250 global_subprog_calling_sleepable_global(int i) 251 { 252 if (!i) 253 global_sleepable_kfunc_subprog(i); 254 return i; 255 } 256 257 SEC("?syscall") 258 __failure __msg("sleepable global function") 259 int preempt_global_sleepable_helper_subprog(struct __sk_buff *ctx) 260 { 261 preempt_disable(); 262 if (ctx->mark) 263 global_sleepable_helper_subprog(ctx->mark); 264 preempt_enable(); 265 return 0; 266 } 267 268 SEC("?syscall") 269 __failure __msg("sleepable global function") 270 int preempt_global_sleepable_kfunc_subprog(struct __sk_buff *ctx) 271 { 272 preempt_disable(); 273 if (ctx->mark) 274 global_sleepable_kfunc_subprog(ctx->mark); 275 preempt_enable(); 276 return 0; 277 } 278 279 SEC("?syscall") 280 __failure __msg("sleepable global function") 281 int preempt_global_sleepable_subprog_indirect(struct __sk_buff *ctx) 282 { 283 preempt_disable(); 284 if (ctx->mark) 285 global_subprog_calling_sleepable_global(ctx->mark); 286 preempt_enable(); 287 return 0; 288 } 289 290 char _license[] SEC("license") = "GPL"; 291