Skip to contentSkip to conversation

Enable static subprog calls in spin lock critical sections

PATCH Superseded Kumar Kartikeya Dwivedi 3 participants 10 messages Feb 4, 2024

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

cover Enable static subprog calls in spin lock critical sections

This set allows a BPF program to make a call to a static subprog within a bpf_spin_lock critical section. This problem has been hit in sched-ext and ghOSt [0] as well, and is mostly an annoyance which is worked around by inling the static subprog into the critical section.

In case of sched-ext, there are a lot of other helper/kfunc calls that need to be allow listed for the support to be complete, but a separate follow up will deal with that.

Unlike static subprogs, global subprogs cannot be allowed yet as the verifier will not explore their body when encountering a call instruction for them. Therefore, we would need an alternative approach (some sort of function summarization to ensure a lock is never taken from a global subprog and all its callees).

[0]: https://lore.kernel.org/bpf/[email protected]

Kumar Kartikeya Dwivedi (2):
bpf: Allow calling static subprogs while holding a bpf_spin_lock selftests/bpf: Add test for static subprog call in lock cs

 kernel/bpf/verifier.c                         | 10 ++-
 .../selftests/bpf/prog_tests/spin_lock.c      |  2 +
 .../selftests/bpf/progs/test_spin_lock.c      | 65 +++++++++++++++++++
 .../selftests/bpf/progs/test_spin_lock_fail.c | 44 +++++++++++++
 .../selftests/bpf/progs/verifier_spin_lock.c  |  2 +-
 5 files changed, 119 insertions(+), 4 deletions(-)

base-commit: 2a79690eae953daaac232f93e6c5ac47ac539f2d

Signature · 1 line
2.40.1
author maintainer
#1 lore raw

PATCH 1/2 bpf: Allow calling static subprogs while holding a bpf_spin_lock

Currently, calling any helpers, kfuncs, or subprogs except the graph data structure (lists, rbtrees) API kfuncs while holding a bpf_spin_lock is not allowed. One of the original motivations of this decision was to force the BPF programmer's hand into keeping the bpf_spin_lock critical section small, and to ensure the execution time of the program does not increase due to lock waiting times. In addition to this, some of the helpers and kfuncs may be unsafe to call while holding a bpf_spin_lock.

However, when it comes to subprog calls, atleast for static subprogs, the verifier is able to explore their instructions during verification. Therefore, it is similar in effect to having the same code inlined into the critical section. Hence, not allowing static subprog calls in the bpf_spin_lock critical section is mostly an annoyance that needs to be worked around, without providing any tangible benefit.

Unlike static subprog calls, global subprog calls are not safe to permit within the critical section, as the verifier does not explore them during verification, therefore whether the same lock will be taken again, or unlocked, cannot be ascertained.

Therefore, allow calling static subprogs within a bpf_spin_lock critical section, and only reject it in case the subprog linkage is global.

---
 kernel/bpf/verifier.c                                  | 10 +++++++---
 tools/testing/selftests/bpf/progs/verifier_spin_lock.c |  2 +-
 2 files changed, 8 insertions(+), 4 deletions(-)
kernel/bpf/verifier.c +7 -3
index 64fa188d00ad..f858c959753b 100644
@@ -9493,6 +9493,12 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 	if (subprog_is_global(env, subprog)) {
 		const char *sub_name = subprog_name(env, subprog);
 
+		/* Only global subprogs cannot be called with a lock held. */
+		if (env->cur_state->active_lock.ptr) {
+			verbose(env, "function calls are not allowed while holding a lock\n");
+			return -EINVAL;
+		}
+
 		if (err) {
 			verbose(env, "Caller passes invalid args into func#%d ('%s')\n",
 				subprog, sub_name);
@@ -17644,7 +17650,6 @@ static int do_check(struct bpf_verifier_env *env)
 
 				if (env->cur_state->active_lock.ptr) {
 					if ((insn->src_reg == BPF_REG_0 && insn->imm != BPF_FUNC_spin_unlock) ||
-					    (insn->src_reg == BPF_PSEUDO_CALL) ||
 					    (insn->src_reg == BPF_PSEUDO_KFUNC_CALL &&
 					     (insn->off != 0 || !is_bpf_graph_api_kfunc(insn->imm)))) {
 						verbose(env, "function calls are not allowed while holding a lock\n");
@@ -17692,8 +17697,7 @@ static int do_check(struct bpf_verifier_env *env)
 					return -EINVAL;
 				}
 process_bpf_exit_full:
-				if (env->cur_state->active_lock.ptr &&
-				    !in_rbtree_lock_required_cb(env)) {
+				if (env->cur_state->active_lock.ptr && !env->cur_state->curframe) {
 					verbose(env, "bpf_spin_unlock is missing\n");
 					return -EINVAL;
 				}
tools/testing/selftests/bpf/progs/verifier_spin_lock.c +1 -1
index 9c1aa69650f8..fb316c080c84 100644
@@ -330,7 +330,7 @@ l1_%=:	r7 = r0;					\
 
 SEC("cgroup/skb")
 __description("spin_lock: test10 lock in subprog without unlock")
-__failure __msg("unlock is missing")
+__success
 __failure_unpriv __msg_unpriv("")
 __naked void lock_in_subprog_without_unlock(void)
 {
Signature · 1 line
2.40.1
reviewer Acked-by on v1 1/2
#2 lore raw

Re: [PATCH bpf-next v1 1/2] bpf: Allow calling static subprogs while holding a bpf_spin_lock

Quoted 20 lines · Kumar Kartikeya Dwivedi

Currently, calling any helpers, kfuncs, or subprogs except the graph data structure (lists, rbtrees) API kfuncs while holding a bpf_spin_lock is not allowed. One of the original motivations of this decision was to force the BPF programmer's hand into keeping the bpf_spin_lock critical section small, and to ensure the execution time of the program does not increase due to lock waiting times. In addition to this, some of the helpers and kfuncs may be unsafe to call while holding a bpf_spin_lock.

However, when it comes to subprog calls, atleast for static subprogs, the verifier is able to explore their instructions during verification. Therefore, it is similar in effect to having the same code inlined into the critical section. Hence, not allowing static subprog calls in the bpf_spin_lock critical section is mostly an annoyance that needs to be worked around, without providing any tangible benefit.

Unlike static subprog calls, global subprog calls are not safe to permit within the critical section, as the verifier does not explore them during verification, therefore whether the same lock will be taken again, or unlocked, cannot be ascertained.

Quoted 4 lines

Therefore, allow calling static subprogs within a bpf_spin_lock critical section, and only reject it in case the subprog linkage is global.

SGTM with a small nit below.

Quoted 10 lines
---
  kernel/bpf/verifier.c                                  | 10 +++++++---
  tools/testing/selftests/bpf/progs/verifier_spin_lock.c |  2 +-
  2 files changed, 8 insertions(+), 4 deletions(-)
kernel/bpf/verifier.c +0 -0
index 64fa188d00ad..f858c959753b 100644

kernel/bpf/verifier.c static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 9498

Quoted 6 lines
kernel/bpf/verifier.c +3 -0
@@ -9493,6 +9493,12 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
  	if (subprog_is_global(env, subprog)) {
  		const char *sub_name = subprog_name(env, subprog);
 
+		/* Only global subprogs cannot be called with a lock held. */
+		if (env->cur_state->active_lock.ptr) {
+			verbose(env, "function calls are not allowed while holding a lock\n");

Maybe explicit to mention "global function calls are not allowed ..."?

Quoted 24 lines
kernel/bpf/verifier.c +4 -3
+			return -EINVAL;
+		}
+
  		if (err) {
  			verbose(env, "Caller passes invalid args into func#%d ('%s')\n",
  				subprog, sub_name);
@@ -17644,7 +17650,6 @@ static int do_check(struct bpf_verifier_env *env)
 
  				if (env->cur_state->active_lock.ptr) {
  					if ((insn->src_reg == BPF_REG_0 && insn->imm != BPF_FUNC_spin_unlock) ||
-					    (insn->src_reg == BPF_PSEUDO_CALL) ||
  					    (insn->src_reg == BPF_PSEUDO_KFUNC_CALL &&
  					     (insn->off != 0 || !is_bpf_graph_api_kfunc(insn->imm)))) {
  						verbose(env, "function calls are not allowed while holding a lock\n");
@@ -17692,8 +17697,7 @@ static int do_check(struct bpf_verifier_env *env)
  					return -EINVAL;
  				}
  process_bpf_exit_full:
-				if (env->cur_state->active_lock.ptr &&
-				    !in_rbtree_lock_required_cb(env)) {
+				if (env->cur_state->active_lock.ptr && !env->cur_state->curframe) {
  					verbose(env, "bpf_spin_unlock is missing\n");
  					return -EINVAL;
  				}

[...]

author maintainer
#3 lore raw

Re: [PATCH bpf-next v1 1/2] bpf: Allow calling static subprogs while holding a bpf_spin_lock

Quoted 45 lines · Yonghong Song
Quoted 20 lines · Kumar Kartikeya Dwivedi

Currently, calling any helpers, kfuncs, or subprogs except the graph data structure (lists, rbtrees) API kfuncs while holding a bpf_spin_lock is not allowed. One of the original motivations of this decision was to force the BPF programmer's hand into keeping the bpf_spin_lock critical section small, and to ensure the execution time of the program does not increase due to lock waiting times. In addition to this, some of the helpers and kfuncs may be unsafe to call while holding a bpf_spin_lock.

However, when it comes to subprog calls, atleast for static subprogs, the verifier is able to explore their instructions during verification. Therefore, it is similar in effect to having the same code inlined into the critical section. Hence, not allowing static subprog calls in the bpf_spin_lock critical section is mostly an annoyance that needs to be worked around, without providing any tangible benefit.

Unlike static subprog calls, global subprog calls are not safe to permit within the critical section, as the verifier does not explore them during verification, therefore whether the same lock will be taken again, or unlocked, cannot be ascertained.

Quoted 4 lines

Therefore, allow calling static subprogs within a bpf_spin_lock critical section, and only reject it in case the subprog linkage is global.

SGTM with a small nit below.

Quoted 13 lines
---
  kernel/bpf/verifier.c                                  | 10 +++++++---
  tools/testing/selftests/bpf/progs/verifier_spin_lock.c |  2 +-
  2 files changed, 8 insertions(+), 4 deletions(-)
kernel/bpf/verifier.c +0 -0
index 64fa188d00ad..f858c959753b 100644
@@ -9493,6 +9493,12 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
      if (subprog_is_global(env, subprog)) {
              const char *sub_name = subprog_name(env, subprog);
 
Quoted 6 lines

kernel/bpf/verifier.c static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 9498

Quoted 3 lines
kernel/bpf/verifier.c +3 -0
+             /* Only global subprogs cannot be called with a lock held. */
+             if (env->cur_state->active_lock.ptr) {
+                     verbose(env, "function calls are not allowed while holding a lock\n");

Maybe explicit to mention "global function calls are not allowed ..."?

Ack, I made this change, and also extended the last part with "use a static function instead" to make it more helpful.
Thanks for the review.

Quoted 27 lines
Quoted 24 lines
kernel/bpf/verifier.c +4 -3
+                     return -EINVAL;
+             }
+
              if (err) {
                      verbose(env, "Caller passes invalid args into func#%d ('%s')\n",
                              subprog, sub_name);
@@ -17644,7 +17650,6 @@ static int do_check(struct bpf_verifier_env *env)
 
                              if (env->cur_state->active_lock.ptr) {
                                      if ((insn->src_reg == BPF_REG_0 && insn->imm != BPF_FUNC_spin_unlock) ||
-                                         (insn->src_reg == BPF_PSEUDO_CALL) ||
                                          (insn->src_reg == BPF_PSEUDO_KFUNC_CALL &&
                                           (insn->off != 0 || !is_bpf_graph_api_kfunc(insn->imm)))) {
                                              verbose(env, "function calls are not allowed while holding a lock\n");
@@ -17692,8 +17697,7 @@ static int do_check(struct bpf_verifier_env *env)
                                      return -EINVAL;
                              }
  process_bpf_exit_full:
-                             if (env->cur_state->active_lock.ptr &&
-                                 !in_rbtree_lock_required_cb(env)) {
+                             if (env->cur_state->active_lock.ptr && !env->cur_state->curframe) {
                                      verbose(env, "bpf_spin_unlock is missing\n");
                                      return -EINVAL;
                              }

[...]

Acked-by on v1 1/2
#4 lore raw

Re: [PATCH bpf-next v1 1/2] bpf: Allow calling static subprogs while holding a bpf_spin_lock

In reply to Kumar Kartikeya Dwivedi
Quoted 20 lines · Kumar Kartikeya Dwivedi

Currently, calling any helpers, kfuncs, or subprogs except the graph data structure (lists, rbtrees) API kfuncs while holding a bpf_spin_lock is not allowed. One of the original motivations of this decision was to force the BPF programmer's hand into keeping the bpf_spin_lock critical section small, and to ensure the execution time of the program does not increase due to lock waiting times. In addition to this, some of the helpers and kfuncs may be unsafe to call while holding a bpf_spin_lock.

However, when it comes to subprog calls, atleast for static subprogs, the verifier is able to explore their instructions during verification. Therefore, it is similar in effect to having the same code inlined into the critical section. Hence, not allowing static subprog calls in the bpf_spin_lock critical section is mostly an annoyance that needs to be worked around, without providing any tangible benefit.

Unlike static subprog calls, global subprog calls are not safe to permit within the critical section, as the verifier does not explore them during verification, therefore whether the same lock will be taken again, or unlocked, cannot be ascertained.

Quoted 4 lines

Therefore, allow calling static subprogs within a bpf_spin_lock critical section, and only reject it in case the subprog linkage is global.

Looks good, thanks for this improvement. I had the same suggestion as Yonghong in [0], and also left a question below.

[0]: https://lore.kernel.org/all/[email protected]/

Quoted 31 lines
---
 kernel/bpf/verifier.c                                  | 10 +++++++---
 tools/testing/selftests/bpf/progs/verifier_spin_lock.c |  2 +-
 2 files changed, 8 insertions(+), 4 deletions(-)
kernel/bpf/verifier.c +6 -1
index 64fa188d00ad..f858c959753b 100644
@@ -9493,6 +9493,12 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 	if (subprog_is_global(env, subprog)) {
 		const char *sub_name = subprog_name(env, subprog);
 
+		/* Only global subprogs cannot be called with a lock held. */
+		if (env->cur_state->active_lock.ptr) {
+			verbose(env, "function calls are not allowed while holding a lock\n");
+			return -EINVAL;
+		}
+
 		if (err) {
 			verbose(env, "Caller passes invalid args into func#%d ('%s')\n",
 				subprog, sub_name);
@@ -17644,7 +17650,6 @@ static int do_check(struct bpf_verifier_env *env)
 
 				if (env->cur_state->active_lock.ptr) {
 					if ((insn->src_reg == BPF_REG_0 && insn->imm != BPF_FUNC_spin_unlock) ||
-					    (insn->src_reg == BPF_PSEUDO_CALL) ||
 					    (insn->src_reg == BPF_PSEUDO_KFUNC_CALL &&
 					     (insn->off != 0 || !is_bpf_graph_api_kfunc(insn->imm)))) {
 						verbose(env, "function calls are not allowed while holding a lock\n");

kernel/bpf/verifier.c static int do_check(struct bpf_verifier_env *env) · line 17700

Quoted 6 lines
kernel/bpf/verifier.c +1 -2
@@ -17692,8 +17697,7 @@ static int do_check(struct bpf_verifier_env *env)
 					return -EINVAL;
 				}
 process_bpf_exit_full:
-				if (env->cur_state->active_lock.ptr &&
-				    !in_rbtree_lock_required_cb(env)) {
+				if (env->cur_state->active_lock.ptr && !env->cur_state->curframe) {

Can we do the same thing here for the RCU check below? It seems like the exact same issue, as we're already allowed to call subprogs from within an RCU read region, but the verifier will get confused and think we haven't unlocked by the time we return to the caller.

Assuming that's the case, we can take care of it in a separate patch set.

Quoted 19 lines
kernel/bpf/verifier.c +0 -0
 					verbose(env, "bpf_spin_unlock is missing\n");
 					return -EINVAL;
 				}
tools/testing/selftests/bpf/progs/verifier_spin_lock.c +1 -1
index 9c1aa69650f8..fb316c080c84 100644
@@ -330,7 +330,7 @@ l1_%=:	r7 = r0;					\
 
 SEC("cgroup/skb")
 __description("spin_lock: test10 lock in subprog without unlock")
-__failure __msg("unlock is missing")
+__success
 __failure_unpriv __msg_unpriv("")
 __naked void lock_in_subprog_without_unlock(void)
 {
Signature · 1 line
2.40.1
author maintainer
#5 lore raw

Re: [PATCH bpf-next v1 1/2] bpf: Allow calling static subprogs while holding a bpf_spin_lock

Quoted 77 lines · David Vernet
Quoted 20 lines · Kumar Kartikeya Dwivedi

Currently, calling any helpers, kfuncs, or subprogs except the graph data structure (lists, rbtrees) API kfuncs while holding a bpf_spin_lock is not allowed. One of the original motivations of this decision was to force the BPF programmer's hand into keeping the bpf_spin_lock critical section small, and to ensure the execution time of the program does not increase due to lock waiting times. In addition to this, some of the helpers and kfuncs may be unsafe to call while holding a bpf_spin_lock.

However, when it comes to subprog calls, atleast for static subprogs, the verifier is able to explore their instructions during verification. Therefore, it is similar in effect to having the same code inlined into the critical section. Hence, not allowing static subprog calls in the bpf_spin_lock critical section is mostly an annoyance that needs to be worked around, without providing any tangible benefit.

Unlike static subprog calls, global subprog calls are not safe to permit within the critical section, as the verifier does not explore them during verification, therefore whether the same lock will be taken again, or unlocked, cannot be ascertained.

Quoted 4 lines

Therefore, allow calling static subprogs within a bpf_spin_lock critical section, and only reject it in case the subprog linkage is global.

Looks good, thanks for this improvement. I had the same suggestion as Yonghong in [0], and also left a question below.

[0]: https://lore.kernel.org/all/[email protected]/

Quoted 31 lines
---
 kernel/bpf/verifier.c                                  | 10 +++++++---
 tools/testing/selftests/bpf/progs/verifier_spin_lock.c |  2 +-
 2 files changed, 8 insertions(+), 4 deletions(-)
kernel/bpf/verifier.c +6 -1
index 64fa188d00ad..f858c959753b 100644
@@ -9493,6 +9493,12 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
      if (subprog_is_global(env, subprog)) {
              const char *sub_name = subprog_name(env, subprog);
 
+             /* Only global subprogs cannot be called with a lock held. */
+             if (env->cur_state->active_lock.ptr) {
+                     verbose(env, "function calls are not allowed while holding a lock\n");
+                     return -EINVAL;
+             }
+
              if (err) {
                      verbose(env, "Caller passes invalid args into func#%d ('%s')\n",
                              subprog, sub_name);
@@ -17644,7 +17650,6 @@ static int do_check(struct bpf_verifier_env *env)
 
                              if (env->cur_state->active_lock.ptr) {
                                      if ((insn->src_reg == BPF_REG_0 && insn->imm != BPF_FUNC_spin_unlock) ||
-                                         (insn->src_reg == BPF_PSEUDO_CALL) ||
                                          (insn->src_reg == BPF_PSEUDO_KFUNC_CALL &&
                                           (insn->off != 0 || !is_bpf_graph_api_kfunc(insn->imm)))) {
                                              verbose(env, "function calls are not allowed while holding a lock\n");

kernel/bpf/verifier.c static int do_check(struct bpf_verifier_env *env) · line 17700

Quoted 6 lines
kernel/bpf/verifier.c +1 -2
@@ -17692,8 +17697,7 @@ static int do_check(struct bpf_verifier_env *env)
                                      return -EINVAL;
                              }
 process_bpf_exit_full:
-                             if (env->cur_state->active_lock.ptr &&
-                                 !in_rbtree_lock_required_cb(env)) {
+                             if (env->cur_state->active_lock.ptr && !env->cur_state->curframe) {

Can we do the same thing here for the RCU check below? It seems like the exact same issue, as we're already allowed to call subprogs from within an RCU read region, but the verifier will get confused and think we haven't unlocked by the time we return to the caller.

Quoted 2 lines

Assuming that's the case, we can take care of it in a separate patch set.

Makes sense, I'll send a separate patch for the RCU fix. Thanks for the review.

reviewer
#6 lore raw

Re: [PATCH bpf-next v1 1/2] bpf: Allow calling static subprogs while holding a bpf_spin_lock

Quoted 75 lines · Kumar Kartikeya Dwivedi
Quoted 74 lines · David Vernet
Quoted 20 lines · Kumar Kartikeya Dwivedi

Currently, calling any helpers, kfuncs, or subprogs except the graph data structure (lists, rbtrees) API kfuncs while holding a bpf_spin_lock is not allowed. One of the original motivations of this decision was to force the BPF programmer's hand into keeping the bpf_spin_lock critical section small, and to ensure the execution time of the program does not increase due to lock waiting times. In addition to this, some of the helpers and kfuncs may be unsafe to call while holding a bpf_spin_lock.

However, when it comes to subprog calls, atleast for static subprogs, the verifier is able to explore their instructions during verification. Therefore, it is similar in effect to having the same code inlined into the critical section. Hence, not allowing static subprog calls in the bpf_spin_lock critical section is mostly an annoyance that needs to be worked around, without providing any tangible benefit.

Unlike static subprog calls, global subprog calls are not safe to permit within the critical section, as the verifier does not explore them during verification, therefore whether the same lock will be taken again, or unlocked, cannot be ascertained.

Quoted 4 lines

Therefore, allow calling static subprogs within a bpf_spin_lock critical section, and only reject it in case the subprog linkage is global.

Looks good, thanks for this improvement. I had the same suggestion as Yonghong in [0], and also left a question below.

[0]: https://lore.kernel.org/all/[email protected]/

Quoted 31 lines
---
  kernel/bpf/verifier.c                                  | 10 +++++++---
  tools/testing/selftests/bpf/progs/verifier_spin_lock.c |  2 +-
  2 files changed, 8 insertions(+), 4 deletions(-)
kernel/bpf/verifier.c +6 -1
index 64fa188d00ad..f858c959753b 100644
@@ -9493,6 +9493,12 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
       if (subprog_is_global(env, subprog)) {
               const char *sub_name = subprog_name(env, subprog);
 
+             /* Only global subprogs cannot be called with a lock held. */
+             if (env->cur_state->active_lock.ptr) {
+                     verbose(env, "function calls are not allowed while holding a lock\n");
+                     return -EINVAL;
+             }
+
               if (err) {
                       verbose(env, "Caller passes invalid args into func#%d ('%s')\n",
                               subprog, sub_name);
@@ -17644,7 +17650,6 @@ static int do_check(struct bpf_verifier_env *env)
 
                               if (env->cur_state->active_lock.ptr) {
                                       if ((insn->src_reg == BPF_REG_0 && insn->imm != BPF_FUNC_spin_unlock) ||
-                                         (insn->src_reg == BPF_PSEUDO_CALL) ||
                                           (insn->src_reg == BPF_PSEUDO_KFUNC_CALL &&
                                            (insn->off != 0 || !is_bpf_graph_api_kfunc(insn->imm)))) {
                                               verbose(env, "function calls are not allowed while holding a lock\n");

kernel/bpf/verifier.c static int do_check(struct bpf_verifier_env *env) · line 17700

Quoted 6 lines
kernel/bpf/verifier.c +1 -2
@@ -17692,8 +17697,7 @@ static int do_check(struct bpf_verifier_env *env)
                                       return -EINVAL;
                               }
  process_bpf_exit_full:
-                             if (env->cur_state->active_lock.ptr &&
-                                 !in_rbtree_lock_required_cb(env)) {
+                             if (env->cur_state->active_lock.ptr && !env->cur_state->curframe) {

Can we do the same thing here for the RCU check below? It seems like the exact same issue, as we're already allowed to call subprogs from within an RCU read region, but the verifier will get confused and think we haven't unlocked by the time we return to the caller.

Quoted 4 lines
Quoted 2 lines

Assuming that's the case, we can take care of it in a separate patch set.

Makes sense, I'll send a separate patch for the RCU fix. Thanks for the review.

The following is what I recommended as well in another thread:

https://lore.kernel.org/bpf/[email protected]/T/#mff17cd64eeb1e17bd0e3e046fb52efeef9c86c25

Quoted 1 line
author maintainer
#7 lore raw

PATCH 2/2 selftests/bpf: Add test for static subprog call in lock cs

In reply to Kumar Kartikeya Dwivedi

Add selftests for static subprog calls within bpf_spin_lock critical section, and ensure we still reject global subprog calls. Also test the case where a subprog call will unlock the caller's held lock, or the caller will unlock a lock taken by a subprog call, ensuring correct transfer of lock state across frames on exit.

---
 .../selftests/bpf/prog_tests/spin_lock.c      |  2 +
 .../selftests/bpf/progs/test_spin_lock.c      | 65 +++++++++++++++++++
 .../selftests/bpf/progs/test_spin_lock_fail.c | 44 +++++++++++++
 3 files changed, 111 insertions(+)
tools/testing/selftests/bpf/prog_tests/spin_lock.c +2 -0
index 18d451be57c8..6a4962ca0e5e 100644
@@ -48,6 +48,8 @@ static struct {
 	{ "lock_id_mismatch_innermapval_kptr", "bpf_spin_unlock of different lock" },
 	{ "lock_id_mismatch_innermapval_global", "bpf_spin_unlock of different lock" },
 	{ "lock_id_mismatch_innermapval_mapval", "bpf_spin_unlock of different lock" },
+	{ "lock_global_subprog_call1", "function calls are not allowed while holding a lock" },
+	{ "lock_global_subprog_call2", "function calls are not allowed while holding a lock" },
 };
 
 static int match_regex(const char *pattern, const char *string)
tools/testing/selftests/bpf/progs/test_spin_lock.c +65 -0

69 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_spin_lock_fail.c +44 -0

48 lines not on this page · raw message

Signature · 1 line
2.40.1
reviewer Acked-by on v1 2/2
#8 lore raw

Re: [PATCH bpf-next v1 2/2] selftests/bpf: Add test for static subprog call in lock cs

Quoted 7 lines · Kumar Kartikeya Dwivedi

Add selftests for static subprog calls within bpf_spin_lock critical section, and ensure we still reject global subprog calls. Also test the case where a subprog call will unlock the caller's held lock, or the caller will unlock a lock taken by a subprog call, ensuring correct transfer of lock state across frames on exit.

LGTM with possible verifier message rewording from "function calls are not allowed while holding a lock" to "global function calls are not allowed ...".

Quoted 19 lines
---
  .../selftests/bpf/prog_tests/spin_lock.c      |  2 +
  .../selftests/bpf/progs/test_spin_lock.c      | 65 +++++++++++++++++++
  .../selftests/bpf/progs/test_spin_lock_fail.c | 44 +++++++++++++
  3 files changed, 111 insertions(+)
tools/testing/selftests/bpf/prog_tests/spin_lock.c +2 -0
index 18d451be57c8..6a4962ca0e5e 100644
@@ -48,6 +48,8 @@ static struct {
  	{ "lock_id_mismatch_innermapval_kptr", "bpf_spin_unlock of different lock" },
  	{ "lock_id_mismatch_innermapval_global", "bpf_spin_unlock of different lock" },
  	{ "lock_id_mismatch_innermapval_mapval", "bpf_spin_unlock of different lock" },
+	{ "lock_global_subprog_call1", "function calls are not allowed while holding a lock" },
+	{ "lock_global_subprog_call2", "function calls are not allowed while holding a lock" },
  };
 
  static int match_regex(const char *pattern, const char *string)

[...]

Acked-by on v1 2/2
#9 lore raw

Re: [PATCH bpf-next v1 2/2] selftests/bpf: Add test for static subprog call in lock cs

In reply to Kumar Kartikeya Dwivedi
Quoted 7 lines · Kumar Kartikeya Dwivedi

Add selftests for static subprog calls within bpf_spin_lock critical section, and ensure we still reject global subprog calls. Also test the case where a subprog call will unlock the caller's held lock, or the caller will unlock a lock taken by a subprog call, ensuring correct transfer of lock state across frames on exit.

Same nit as Yonghong again to just slightly improve the error message in the verifier. Otherwise LGTM, thanks.