-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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/20240423061922.2295517-1-memxor@gmail.com * 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 <jolsa@kernel.org> Link: https://lore.kernel.org/r/20240424031315.2757363-1-memxor@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
- Loading branch information
Showing
5 changed files
with
226 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
#include <test_progs.h> | ||
#include <network_helpers.h> | ||
#include <preempt_lock.skel.h> | ||
|
||
void test_preempt_lock(void) | ||
{ | ||
RUN_TESTS(preempt_lock); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
#include <vmlinux.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
#include "bpf_misc.h" | ||
|
||
void bpf_preempt_disable(void) __ksym; | ||
void bpf_preempt_enable(void) __ksym; | ||
|
||
SEC("?tc") | ||
__failure __msg("1 bpf_preempt_enable is missing") | ||
int preempt_lock_missing_1(struct __sk_buff *ctx) | ||
{ | ||
bpf_preempt_disable(); | ||
return 0; | ||
} | ||
|
||
SEC("?tc") | ||
__failure __msg("2 bpf_preempt_enable(s) are missing") | ||
int preempt_lock_missing_2(struct __sk_buff *ctx) | ||
{ | ||
bpf_preempt_disable(); | ||
bpf_preempt_disable(); | ||
return 0; | ||
} | ||
|
||
SEC("?tc") | ||
__failure __msg("3 bpf_preempt_enable(s) are missing") | ||
int preempt_lock_missing_3(struct __sk_buff *ctx) | ||
{ | ||
bpf_preempt_disable(); | ||
bpf_preempt_disable(); | ||
bpf_preempt_disable(); | ||
return 0; | ||
} | ||
|
||
SEC("?tc") | ||
__failure __msg("1 bpf_preempt_enable is missing") | ||
int preempt_lock_missing_3_minus_2(struct __sk_buff *ctx) | ||
{ | ||
bpf_preempt_disable(); | ||
bpf_preempt_disable(); | ||
bpf_preempt_disable(); | ||
bpf_preempt_enable(); | ||
bpf_preempt_enable(); | ||
return 0; | ||
} | ||
|
||
static __noinline void preempt_disable(void) | ||
{ | ||
bpf_preempt_disable(); | ||
} | ||
|
||
static __noinline void preempt_enable(void) | ||
{ | ||
bpf_preempt_enable(); | ||
} | ||
|
||
SEC("?tc") | ||
__failure __msg("1 bpf_preempt_enable is missing") | ||
int preempt_lock_missing_1_subprog(struct __sk_buff *ctx) | ||
{ | ||
preempt_disable(); | ||
return 0; | ||
} | ||
|
||
SEC("?tc") | ||
__failure __msg("2 bpf_preempt_enable(s) are missing") | ||
int preempt_lock_missing_2_subprog(struct __sk_buff *ctx) | ||
{ | ||
preempt_disable(); | ||
preempt_disable(); | ||
return 0; | ||
} | ||
|
||
SEC("?tc") | ||
__failure __msg("1 bpf_preempt_enable is missing") | ||
int preempt_lock_missing_2_minus_1_subprog(struct __sk_buff *ctx) | ||
{ | ||
preempt_disable(); | ||
preempt_disable(); | ||
preempt_enable(); | ||
return 0; | ||
} | ||
|
||
static __noinline void preempt_balance_subprog(void) | ||
{ | ||
preempt_disable(); | ||
preempt_enable(); | ||
} | ||
|
||
SEC("?tc") | ||
__success int preempt_balance(struct __sk_buff *ctx) | ||
{ | ||
bpf_preempt_disable(); | ||
bpf_preempt_enable(); | ||
return 0; | ||
} | ||
|
||
SEC("?tc") | ||
__success int preempt_balance_subprog_test(struct __sk_buff *ctx) | ||
{ | ||
preempt_balance_subprog(); | ||
return 0; | ||
} | ||
|
||
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") | ||
__failure __msg("sleepable helper bpf_copy_from_user#") | ||
int preempt_sleepable_helper(void *ctx) | ||
{ | ||
u32 data; | ||
|
||
bpf_preempt_disable(); | ||
bpf_copy_from_user(&data, sizeof(data), NULL); | ||
bpf_preempt_enable(); | ||
return 0; | ||
} | ||
|
||
int __noinline preempt_global_subprog(void) | ||
{ | ||
preempt_balance_subprog(); | ||
return 0; | ||
} | ||
|
||
SEC("?tc") | ||
__failure __msg("global function calls are not allowed with preemption disabled") | ||
int preempt_global_subprog_test(struct __sk_buff *ctx) | ||
{ | ||
preempt_disable(); | ||
preempt_global_subprog(); | ||
preempt_enable(); | ||
return 0; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |