Skip to content

Commit 55d30cc

Browse files
author
Alexei Starovoitov
committed
Merge branch 'introduce-bpf_preempt_-disable-enable'
Kumar Kartikeya Dwivedi says: ==================== Introduce bpf_preempt_{disable,enable} This set introduces two kfuncs, bpf_preempt_disable and bpf_preempt_enable, which are wrappers around preempt_disable and preempt_enable in the kernel. These functions allow a BPF program to have code sections where preemption is disabled. There are multiple use cases that are served by such a feature, a few are listed below: 1. Writing safe per-CPU alogrithms/data structures that work correctly across different contexts. 2. Writing safe per-CPU allocators similar to bpf_memalloc on top of array/arena memory blobs. 3. Writing locking algorithms in BPF programs natively. Note that local_irq_disable/enable equivalent is also needed for proper IRQ context protection, but that is a more involved change and will be sent later. While bpf_preempt_{disable,enable} is not sufficient for all of these usage scenarios on its own, it is still necessary. The same effect as these kfuncs can in some sense be already achieved using the bpf_spin_lock or rcu_read_lock APIs, therefore from the standpoint of kernel functionality exposure in the verifier, this is well understood territory. Note that these helpers do allow calling kernel helpers and kfuncs from within the non-preemptible region (unless sleepable). Otherwise, any locks built using the preemption helpers will be as limited as existing bpf_spin_lock. Nesting is allowed by keeping a counter for tracking remaining enables required to be performed. Similar approach can be applied to rcu_read_locks in a follow up. Changelog ========= v1: https://lore.kernel.org/bpf/[email protected] * Move kfunc BTF ID declerations above css task kfunc for !CONFIG_CGROUPS config (Alexei) * Add test case for global function call in non-preemptible region (Jiri) ==================== Acked-by: Jiri Olsa <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents dc92feb + 3134396 commit 55d30cc

File tree

5 files changed

+226
-2
lines changed

5 files changed

+226
-2
lines changed

include/linux/bpf_verifier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ struct bpf_verifier_state {
421421
struct bpf_active_lock active_lock;
422422
bool speculative;
423423
bool active_rcu_lock;
424+
u32 active_preempt_lock;
424425
/* If this state was ever pointed-to by other state's loop_entry field
425426
* this flag would be set to true. Used to avoid freeing such states
426427
* while they are still in use.

kernel/bpf/helpers.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,6 +2734,16 @@ __bpf_kfunc int bpf_wq_set_callback_impl(struct bpf_wq *wq,
27342734
return __bpf_async_set_callback(async, callback_fn, aux, flags, BPF_ASYNC_TYPE_WQ);
27352735
}
27362736

2737+
__bpf_kfunc void bpf_preempt_disable(void)
2738+
{
2739+
preempt_disable();
2740+
}
2741+
2742+
__bpf_kfunc void bpf_preempt_enable(void)
2743+
{
2744+
preempt_enable();
2745+
}
2746+
27372747
__bpf_kfunc_end_defs();
27382748

27392749
BTF_KFUNCS_START(generic_btf_ids)
@@ -2814,6 +2824,8 @@ BTF_ID_FLAGS(func, bpf_modify_return_test_tp)
28142824
BTF_ID_FLAGS(func, bpf_wq_init)
28152825
BTF_ID_FLAGS(func, bpf_wq_set_callback_impl)
28162826
BTF_ID_FLAGS(func, bpf_wq_start)
2827+
BTF_ID_FLAGS(func, bpf_preempt_disable)
2828+
BTF_ID_FLAGS(func, bpf_preempt_enable)
28172829
BTF_KFUNCS_END(common_btf_ids)
28182830

28192831
static const struct btf_kfunc_id_set common_kfunc_set = {

kernel/bpf/verifier.c

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,7 @@ static int copy_verifier_state(struct bpf_verifier_state *dst_state,
14341434
}
14351435
dst_state->speculative = src->speculative;
14361436
dst_state->active_rcu_lock = src->active_rcu_lock;
1437+
dst_state->active_preempt_lock = src->active_preempt_lock;
14371438
dst_state->in_sleepable = src->in_sleepable;
14381439
dst_state->curframe = src->curframe;
14391440
dst_state->active_lock.ptr = src->active_lock.ptr;
@@ -9599,6 +9600,13 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
95999600
return -EINVAL;
96009601
}
96019602

9603+
/* Only global subprogs cannot be called with preemption disabled. */
9604+
if (env->cur_state->active_preempt_lock) {
9605+
verbose(env, "global function calls are not allowed with preemption disabled,\n"
9606+
"use static function instead\n");
9607+
return -EINVAL;
9608+
}
9609+
96029610
if (err) {
96039611
verbose(env, "Caller passes invalid args into func#%d ('%s')\n",
96049612
subprog, sub_name);
@@ -10285,6 +10293,17 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
1028510293
env->insn_aux_data[insn_idx].storage_get_func_atomic = true;
1028610294
}
1028710295

10296+
if (env->cur_state->active_preempt_lock) {
10297+
if (fn->might_sleep) {
10298+
verbose(env, "sleepable helper %s#%d in non-preemptible region\n",
10299+
func_id_name(func_id), func_id);
10300+
return -EINVAL;
10301+
}
10302+
10303+
if (in_sleepable(env) && is_storage_get_function(func_id))
10304+
env->insn_aux_data[insn_idx].storage_get_func_atomic = true;
10305+
}
10306+
1028810307
meta.func_id = func_id;
1028910308
/* check args */
1029010309
for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) {
@@ -11027,6 +11046,8 @@ enum special_kfunc_type {
1102711046
KF_bpf_percpu_obj_drop_impl,
1102811047
KF_bpf_throw,
1102911048
KF_bpf_wq_set_callback_impl,
11049+
KF_bpf_preempt_disable,
11050+
KF_bpf_preempt_enable,
1103011051
KF_bpf_iter_css_task_new,
1103111052
};
1103211053

@@ -11081,6 +11102,8 @@ BTF_ID(func, bpf_percpu_obj_new_impl)
1108111102
BTF_ID(func, bpf_percpu_obj_drop_impl)
1108211103
BTF_ID(func, bpf_throw)
1108311104
BTF_ID(func, bpf_wq_set_callback_impl)
11105+
BTF_ID(func, bpf_preempt_disable)
11106+
BTF_ID(func, bpf_preempt_enable)
1108411107
#ifdef CONFIG_CGROUPS
1108511108
BTF_ID(func, bpf_iter_css_task_new)
1108611109
#else
@@ -11107,6 +11130,16 @@ static bool is_kfunc_bpf_rcu_read_unlock(struct bpf_kfunc_call_arg_meta *meta)
1110711130
return meta->func_id == special_kfunc_list[KF_bpf_rcu_read_unlock];
1110811131
}
1110911132

11133+
static bool is_kfunc_bpf_preempt_disable(struct bpf_kfunc_call_arg_meta *meta)
11134+
{
11135+
return meta->func_id == special_kfunc_list[KF_bpf_preempt_disable];
11136+
}
11137+
11138+
static bool is_kfunc_bpf_preempt_enable(struct bpf_kfunc_call_arg_meta *meta)
11139+
{
11140+
return meta->func_id == special_kfunc_list[KF_bpf_preempt_enable];
11141+
}
11142+
1111011143
static enum kfunc_ptr_arg_type
1111111144
get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,
1111211145
struct bpf_kfunc_call_arg_meta *meta,
@@ -12195,11 +12228,11 @@ static int check_return_code(struct bpf_verifier_env *env, int regno, const char
1219512228
static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1219612229
int *insn_idx_p)
1219712230
{
12198-
const struct btf_type *t, *ptr_type;
12231+
bool sleepable, rcu_lock, rcu_unlock, preempt_disable, preempt_enable;
1219912232
u32 i, nargs, ptr_type_id, release_ref_obj_id;
1220012233
struct bpf_reg_state *regs = cur_regs(env);
1220112234
const char *func_name, *ptr_type_name;
12202-
bool sleepable, rcu_lock, rcu_unlock;
12235+
const struct btf_type *t, *ptr_type;
1220312236
struct bpf_kfunc_call_arg_meta meta;
1220412237
struct bpf_insn_aux_data *insn_aux;
1220512238
int err, insn_idx = *insn_idx_p;
@@ -12260,6 +12293,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1226012293
rcu_lock = is_kfunc_bpf_rcu_read_lock(&meta);
1226112294
rcu_unlock = is_kfunc_bpf_rcu_read_unlock(&meta);
1226212295

12296+
preempt_disable = is_kfunc_bpf_preempt_disable(&meta);
12297+
preempt_enable = is_kfunc_bpf_preempt_enable(&meta);
12298+
1226312299
if (env->cur_state->active_rcu_lock) {
1226412300
struct bpf_func_state *state;
1226512301
struct bpf_reg_state *reg;
@@ -12292,6 +12328,22 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1229212328
return -EINVAL;
1229312329
}
1229412330

12331+
if (env->cur_state->active_preempt_lock) {
12332+
if (preempt_disable) {
12333+
env->cur_state->active_preempt_lock++;
12334+
} else if (preempt_enable) {
12335+
env->cur_state->active_preempt_lock--;
12336+
} else if (sleepable) {
12337+
verbose(env, "kernel func %s is sleepable within non-preemptible region\n", func_name);
12338+
return -EACCES;
12339+
}
12340+
} else if (preempt_disable) {
12341+
env->cur_state->active_preempt_lock++;
12342+
} else if (preempt_enable) {
12343+
verbose(env, "unmatched attempt to enable preemption (kernel function %s)\n", func_name);
12344+
return -EINVAL;
12345+
}
12346+
1229512347
/* In case of release function, we get register number of refcounted
1229612348
* PTR_TO_BTF_ID in bpf_kfunc_arg_meta, do the release now.
1229712349
*/
@@ -15439,6 +15491,11 @@ static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
1543915491
return -EINVAL;
1544015492
}
1544115493

15494+
if (env->cur_state->active_preempt_lock) {
15495+
verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_preempt_disable-ed region\n");
15496+
return -EINVAL;
15497+
}
15498+
1544215499
if (regs[ctx_reg].type != PTR_TO_CTX) {
1544315500
verbose(env,
1544415501
"at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
@@ -17006,6 +17063,9 @@ static bool states_equal(struct bpf_verifier_env *env,
1700617063
if (old->active_rcu_lock != cur->active_rcu_lock)
1700717064
return false;
1700817065

17066+
if (old->active_preempt_lock != cur->active_preempt_lock)
17067+
return false;
17068+
1700917069
if (old->in_sleepable != cur->in_sleepable)
1701017070
return false;
1701117071

@@ -17957,6 +18017,13 @@ static int do_check(struct bpf_verifier_env *env)
1795718017
return -EINVAL;
1795818018
}
1795918019

18020+
if (env->cur_state->active_preempt_lock && !env->cur_state->curframe) {
18021+
verbose(env, "%d bpf_preempt_enable%s missing\n",
18022+
env->cur_state->active_preempt_lock,
18023+
env->cur_state->active_preempt_lock == 1 ? " is" : "(s) are");
18024+
return -EINVAL;
18025+
}
18026+
1796018027
/* We must do check_reference_leak here before
1796118028
* prepare_func_exit to handle the case when
1796218029
* state->curframe > 0, it may be a callback
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <test_progs.h>
3+
#include <network_helpers.h>
4+
#include <preempt_lock.skel.h>
5+
6+
void test_preempt_lock(void)
7+
{
8+
RUN_TESTS(preempt_lock);
9+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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

Comments
 (0)