Open up res_spin_lock() in all contexts
Subsystems (4)
Kumar Kartikeya Dwivedi
maintainercover Open up res_spin_lock() in all contexts
Spin off first patch with added tests from [0]. See commits for details.
[0]: https://lore.kernel.org/bpf/[email protected]
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
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerPATCH 1/2 bpf: Allow bpf_res_spin_lock() in all contexts
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.
--- kernel/bpf/verifier.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/bpf/verifier.c +2 -0
@@ -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 ;
Signature · 1 line
2.53.0
Kumar Kartikeya Dwivedi
maintainerPATCH 2/2 selftests/bpf: Test res spin locks in tracing programs
In reply to Kumar Kartikeya DwivediExercise 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.
--- .../bpf/progs/verifier_helper_restricted.c | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+)
tools/testing/selftests/bpf/progs/verifier_helper_restricted.c +66 -0
@@ -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";
Signature · 1 line
2.53.0
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 9 lines · On Fri, 24 Jul 2026 22:16:30 +0200 you wrote:
Spin off first patch with added tests from [0]. See commits for details.
[0]: https://lore.kernel.org/bpf/[email protected]
Kumar Kartikeya Dwivedi (2):
bpf: Allowbpf_res_spin_lock()in all contexts selftests/bpf: Test res spin locks in tracing programs[...]
Here is the summary with links:
- [bpf-next,v1,1/2] bpf: Allow bpf_res_spin_lock() in all contexts
https://git.kernel.org/bpf/bpf-next/c/59b9731addc7
- [bpf-next,v1,2/2] selftests/bpf: Test res spin locks in tracing programs (no matching commit)
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html