BPF List
 help / color / mirror / Atom feed
* [PATCH v2 0/2] bpf: Allow bpf_for/bpf_repeat while holding spin
@ 2025-01-04 20:25 Emil Tsalapatis
  2025-01-04 20:25 ` [PATCH v2 1/2] bpf: Allow bpf_for/bpf_repeat calls while holding a spinlock Emil Tsalapatis
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Emil Tsalapatis @ 2025-01-04 20:25 UTC (permalink / raw)
  To: bpf; +Cc: ast, daniel, andrii, eddyz87, Emil Tsalapatis

From: Emil Tsalapatis (Meta) <emil@etsalapatis.com>

In BPF programs, kfunc calls while holding a lock are not allowed
because kfuncs may sleep by default. The exception to this rule are the
functions in special_kfunc_list, which are guaranteed to not sleep. The
bpf_iter_num_* functions used by the bpf_for and bpf_repeat macros make
no function calls themselves, and as such are guaranteed to not sleep.
Add them to special_kfunc_list to allow them within BPF spinlock
critical sections.

Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>

Emil Tsalapatis (2):
  bpf: Allow bpf_for/bpf_repeat calls while holding a spinlock
  selftests/bpf: test bpf_for within spin lock section

 kernel/bpf/verifier.c                         | 20 +++++++++++++-
 .../selftests/bpf/progs/verifier_spin_lock.c  | 26 +++++++++++++++++++
 2 files changed, 45 insertions(+), 1 deletion(-)

-- 
2.47.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] bpf: Allow bpf_for/bpf_repeat calls while holding a spinlock
  2025-01-04 20:25 [PATCH v2 0/2] bpf: Allow bpf_for/bpf_repeat while holding spin Emil Tsalapatis
@ 2025-01-04 20:25 ` Emil Tsalapatis
  2025-01-04 20:25 ` [PATCH v2 2/2] selftests/bpf: test bpf_for within spin lock section Emil Tsalapatis
  2025-01-06 19:10 ` [PATCH v2 0/2] bpf: Allow bpf_for/bpf_repeat while holding spin patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Emil Tsalapatis @ 2025-01-04 20:25 UTC (permalink / raw)
  To: bpf; +Cc: ast, daniel, andrii, eddyz87, Emil Tsalapatis

 Add the bpf_iter_num_* kfuncs called by bpf_for in special_kfunc_list,
 and allow the calls even while holding a spin lock.

Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
Reviewed-by: Eduard Zingerman <eddyz87@gmail.com>
---
 kernel/bpf/verifier.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index d77abb87ffb1..b8ca227c78af 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -11690,6 +11690,9 @@ enum special_kfunc_type {
 	KF_bpf_get_kmem_cache,
 	KF_bpf_local_irq_save,
 	KF_bpf_local_irq_restore,
+	KF_bpf_iter_num_new,
+	KF_bpf_iter_num_next,
+	KF_bpf_iter_num_destroy,
 };
 
 BTF_SET_START(special_kfunc_set)
@@ -11765,6 +11768,9 @@ BTF_ID_UNUSED
 BTF_ID(func, bpf_get_kmem_cache)
 BTF_ID(func, bpf_local_irq_save)
 BTF_ID(func, bpf_local_irq_restore)
+BTF_ID(func, bpf_iter_num_new)
+BTF_ID(func, bpf_iter_num_next)
+BTF_ID(func, bpf_iter_num_destroy)
 
 static bool is_kfunc_ret_null(struct bpf_kfunc_call_arg_meta *meta)
 {
@@ -12151,12 +12157,24 @@ static bool is_bpf_rbtree_api_kfunc(u32 btf_id)
 	       btf_id == special_kfunc_list[KF_bpf_rbtree_first];
 }
 
+static bool is_bpf_iter_num_api_kfunc(u32 btf_id)
+{
+	return btf_id == special_kfunc_list[KF_bpf_iter_num_new] ||
+	       btf_id == special_kfunc_list[KF_bpf_iter_num_next] ||
+	       btf_id == special_kfunc_list[KF_bpf_iter_num_destroy];
+}
+
 static bool is_bpf_graph_api_kfunc(u32 btf_id)
 {
 	return is_bpf_list_api_kfunc(btf_id) || is_bpf_rbtree_api_kfunc(btf_id) ||
 	       btf_id == special_kfunc_list[KF_bpf_refcount_acquire_impl];
 }
 
+static bool kfunc_spin_allowed(u32 btf_id)
+{
+	return is_bpf_graph_api_kfunc(btf_id) || is_bpf_iter_num_api_kfunc(btf_id);
+}
+
 static bool is_sync_callback_calling_kfunc(u32 btf_id)
 {
 	return btf_id == special_kfunc_list[KF_bpf_rbtree_add_impl];
@@ -19048,7 +19066,7 @@ static int do_check(struct bpf_verifier_env *env)
 				if (env->cur_state->active_locks) {
 					if ((insn->src_reg == BPF_REG_0 && insn->imm != BPF_FUNC_spin_unlock) ||
 					    (insn->src_reg == BPF_PSEUDO_KFUNC_CALL &&
-					     (insn->off != 0 || !is_bpf_graph_api_kfunc(insn->imm)))) {
+					     (insn->off != 0 || !kfunc_spin_allowed(insn->imm)))) {
 						verbose(env, "function calls are not allowed while holding a lock\n");
 						return -EINVAL;
 					}
-- 
2.47.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] selftests/bpf: test bpf_for within spin lock section
  2025-01-04 20:25 [PATCH v2 0/2] bpf: Allow bpf_for/bpf_repeat while holding spin Emil Tsalapatis
  2025-01-04 20:25 ` [PATCH v2 1/2] bpf: Allow bpf_for/bpf_repeat calls while holding a spinlock Emil Tsalapatis
@ 2025-01-04 20:25 ` Emil Tsalapatis
  2025-01-06 19:10 ` [PATCH v2 0/2] bpf: Allow bpf_for/bpf_repeat while holding spin patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Emil Tsalapatis @ 2025-01-04 20:25 UTC (permalink / raw)
  To: bpf; +Cc: ast, daniel, andrii, eddyz87, Emil Tsalapatis

Add a selftest to ensure BPF for loops within critical sections are
accepted by the verifier.

Signed-off-by: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
---
 .../selftests/bpf/progs/verifier_spin_lock.c  | 26 +++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/tools/testing/selftests/bpf/progs/verifier_spin_lock.c b/tools/testing/selftests/bpf/progs/verifier_spin_lock.c
index 25599eac9a70..d9d7b05cf6d2 100644
--- a/tools/testing/selftests/bpf/progs/verifier_spin_lock.c
+++ b/tools/testing/selftests/bpf/progs/verifier_spin_lock.c
@@ -530,4 +530,30 @@ l1_%=:	exit;						\
 	: __clobber_all);
 }
 
+SEC("tc")
+__description("spin_lock: loop within a locked region")
+__success __failure_unpriv __msg_unpriv("")
+__retval(0)
+int bpf_loop_inside_locked_region(void)
+{
+	const int zero = 0;
+	struct val *val;
+	int i, j = 0;
+
+	val = bpf_map_lookup_elem(&map_spin_lock, &zero);
+	if (!val)
+		return -1;
+
+	bpf_spin_lock(&val->l);
+	bpf_for(i, 0, 10) {
+		j++;
+		/* Silence "unused variable" warnings. */
+		if (j == 10)
+			break;
+	}
+	bpf_spin_unlock(&val->l);
+
+	return 0;
+}
+
 char _license[] SEC("license") = "GPL";
-- 
2.47.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 0/2] bpf: Allow bpf_for/bpf_repeat while holding spin
  2025-01-04 20:25 [PATCH v2 0/2] bpf: Allow bpf_for/bpf_repeat while holding spin Emil Tsalapatis
  2025-01-04 20:25 ` [PATCH v2 1/2] bpf: Allow bpf_for/bpf_repeat calls while holding a spinlock Emil Tsalapatis
  2025-01-04 20:25 ` [PATCH v2 2/2] selftests/bpf: test bpf_for within spin lock section Emil Tsalapatis
@ 2025-01-06 19:10 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-01-06 19:10 UTC (permalink / raw)
  To: Emil Tsalapatis; +Cc: bpf, ast, daniel, andrii, eddyz87

Hello:

This series was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Sat,  4 Jan 2025 15:25:26 -0500 you wrote:
> From: Emil Tsalapatis (Meta) <emil@etsalapatis.com>
> 
> In BPF programs, kfunc calls while holding a lock are not allowed
> because kfuncs may sleep by default. The exception to this rule are the
> functions in special_kfunc_list, which are guaranteed to not sleep. The
> bpf_iter_num_* functions used by the bpf_for and bpf_repeat macros make
> no function calls themselves, and as such are guaranteed to not sleep.
> Add them to special_kfunc_list to allow them within BPF spinlock
> critical sections.
> 
> [...]

Here is the summary with links:
  - [v2,1/2] bpf: Allow bpf_for/bpf_repeat calls while holding a spinlock
    https://git.kernel.org/bpf/bpf-next/c/512816403ece
  - [v2,2/2] selftests/bpf: test bpf_for within spin lock section
    https://git.kernel.org/bpf/bpf-next/c/87091dd986db

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-01-06 19:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-04 20:25 [PATCH v2 0/2] bpf: Allow bpf_for/bpf_repeat while holding spin Emil Tsalapatis
2025-01-04 20:25 ` [PATCH v2 1/2] bpf: Allow bpf_for/bpf_repeat calls while holding a spinlock Emil Tsalapatis
2025-01-04 20:25 ` [PATCH v2 2/2] selftests/bpf: test bpf_for within spin lock section Emil Tsalapatis
2025-01-06 19:10 ` [PATCH v2 0/2] bpf: Allow bpf_for/bpf_repeat while holding spin patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox