BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v1 0/2] Open up res_spin_lock() in all contexts
@ 2026-07-24 20:16 Kumar Kartikeya Dwivedi
  2026-07-24 20:16 ` [PATCH bpf-next v1 1/2] bpf: Allow bpf_res_spin_lock() " Kumar Kartikeya Dwivedi
  2026-07-24 20:16 ` [PATCH bpf-next v1 2/2] selftests/bpf: Test res spin locks in tracing programs Kumar Kartikeya Dwivedi
  0 siblings, 2 replies; 3+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-07-24 20:16 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Eduard Zingerman, Emil Tsalapatis, kkd, kernel-team

Spin off first patch with added tests from [0]. See commits for details.

  [0]: https://lore.kernel.org/bpf/20260719113551.1294284-1-memxor@gmail.com

Kumar Kartikeya Dwivedi (2):
  bpf: Allow bpf_res_spin_lock() in all contexts
  selftests/bpf: Test res spin locks in tracing programs

 kernel/bpf/verifier.c                         |  2 +
 .../bpf/progs/verifier_helper_restricted.c    | 66 +++++++++++++++++++
 2 files changed, 68 insertions(+)


base-commit: 87267b89459813cb50ab5377e076b639c07b4491
-- 
2.53.0


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

* [PATCH bpf-next v1 1/2] bpf: Allow bpf_res_spin_lock() in all contexts
  2026-07-24 20:16 [PATCH bpf-next v1 0/2] Open up res_spin_lock() in all contexts Kumar Kartikeya Dwivedi
@ 2026-07-24 20:16 ` Kumar Kartikeya Dwivedi
  2026-07-24 20:16 ` [PATCH bpf-next v1 2/2] selftests/bpf: Test res spin locks in tracing programs Kumar Kartikeya Dwivedi
  1 sibling, 0 replies; 3+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-07-24 20:16 UTC (permalink / raw)
  To: bpf
  Cc: Eduard Zingerman, Emil Tsalapatis, Alexei Starovoitov,
	Andrii Nakryiko, Daniel Borkmann, kkd, kernel-team

There is no particular reason to keep bpf_res_spin_lock() disabled in
tracing programs, since it is safe against reentrancy and deadlocks.
Remove the restriction for tracing programs covered by the predicate
is_tracing_prog_type().

This is a prerequisite before the definition of is_tracing_prog_type()
is updated to include raw_tp, fentry, fexit, and fmod_ret. Existing
tracing programs will be updated to use bpf_res_spin_lock() instead when
it is available.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 kernel/bpf/verifier.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 52be0a118cce..57a76c21d624 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -17815,7 +17815,9 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env,
 			verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n");
 			return -EINVAL;
 		}
+	}
 
+	if (btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
 		if (is_tracing_prog_type(prog_type)) {
 			verbose(env, "tracing progs cannot use bpf_spin_lock yet\n");
 			return -EINVAL;
-- 
2.53.0


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

* [PATCH bpf-next v1 2/2] selftests/bpf: Test res spin locks in tracing programs
  2026-07-24 20:16 [PATCH bpf-next v1 0/2] Open up res_spin_lock() in all contexts Kumar Kartikeya Dwivedi
  2026-07-24 20:16 ` [PATCH bpf-next v1 1/2] bpf: Allow bpf_res_spin_lock() " Kumar Kartikeya Dwivedi
@ 2026-07-24 20:16 ` Kumar Kartikeya Dwivedi
  1 sibling, 0 replies; 3+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-07-24 20:16 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Eduard Zingerman, Emil Tsalapatis, kkd, kernel-team

Exercise bpf_res_spin_lock() from kprobe, tracepoint, perf event, and raw
tracepoint programs. Keep the existing bpf_spin_lock() rejection checks so
the tests cover the split verifier policy for the two lock types.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 .../bpf/progs/verifier_helper_restricted.c    | 66 +++++++++++++++++++
 1 file changed, 66 insertions(+)

diff --git a/tools/testing/selftests/bpf/progs/verifier_helper_restricted.c b/tools/testing/selftests/bpf/progs/verifier_helper_restricted.c
index 889c9b78b912..b3beb8f6dd19 100644
--- a/tools/testing/selftests/bpf/progs/verifier_helper_restricted.c
+++ b/tools/testing/selftests/bpf/progs/verifier_helper_restricted.c
@@ -10,6 +10,18 @@ struct val {
 	struct bpf_spin_lock l;
 };
 
+struct bpf_res_spin_lock {
+	__u32 val;
+};
+
+struct res_val {
+	int cnt;
+	struct bpf_res_spin_lock l;
+};
+
+extern int bpf_res_spin_lock(struct bpf_res_spin_lock *lock) __ksym;
+extern void bpf_res_spin_unlock(struct bpf_res_spin_lock *lock) __ksym;
+
 struct {
 	__uint(type, BPF_MAP_TYPE_ARRAY);
 	__uint(max_entries, 1);
@@ -17,6 +29,28 @@ struct {
 	__type(value, struct val);
 } map_spin_lock SEC(".maps");
 
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, int);
+	__type(value, struct res_val);
+} map_res_spin_lock SEC(".maps");
+
+static __always_inline int use_res_spin_lock(void)
+{
+	struct res_val *val;
+	int key = 0;
+
+	val = bpf_map_lookup_elem(&map_res_spin_lock, &key);
+	if (!val)
+		return 0;
+	if (bpf_res_spin_lock(&val->l))
+		return 0;
+	val->cnt++;
+	bpf_res_spin_unlock(&val->l);
+	return 0;
+}
+
 SEC("kprobe")
 __description("bpf_ktime_get_coarse_ns is forbidden in BPF_PROG_TYPE_KPROBE")
 __failure __msg("program of this type cannot use helper bpf_ktime_get_coarse_ns")
@@ -165,4 +199,36 @@ l0_%=:	exit;						\
 	: __clobber_all);
 }
 
+SEC("kprobe")
+__description("bpf_res_spin_lock is allowed in BPF_PROG_TYPE_KPROBE")
+__success
+int res_spin_lock_bpf_prog_type_kprobe(void *ctx)
+{
+	return use_res_spin_lock();
+}
+
+SEC("tracepoint")
+__description("bpf_res_spin_lock is allowed in BPF_PROG_TYPE_TRACEPOINT")
+__success
+int res_spin_lock_bpf_prog_type_tracepoint(void *ctx)
+{
+	return use_res_spin_lock();
+}
+
+SEC("perf_event")
+__description("bpf_res_spin_lock is allowed in BPF_PROG_TYPE_PERF_EVENT")
+__success
+int res_spin_lock_bpf_prog_type_perf_event(void *ctx)
+{
+	return use_res_spin_lock();
+}
+
+SEC("raw_tracepoint")
+__description("bpf_res_spin_lock is allowed in BPF_PROG_TYPE_RAW_TRACEPOINT")
+__success
+int res_spin_lock_bpf_prog_type_raw_tracepoint(void *ctx)
+{
+	return use_res_spin_lock();
+}
+
 char _license[] SEC("license") = "GPL";
-- 
2.53.0


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

end of thread, other threads:[~2026-07-24 20:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-24 20:16 [PATCH bpf-next v1 0/2] Open up res_spin_lock() in all contexts Kumar Kartikeya Dwivedi
2026-07-24 20:16 ` [PATCH bpf-next v1 1/2] bpf: Allow bpf_res_spin_lock() " Kumar Kartikeya Dwivedi
2026-07-24 20:16 ` [PATCH bpf-next v1 2/2] selftests/bpf: Test res spin locks in tracing programs Kumar Kartikeya Dwivedi

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