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