Skip to content

Commit 9e7a4d9

Browse files
sinkapAlexei Starovoitov
authored andcommitted
bpf: Allow LSM programs to use bpf spin locks
Usage of spin locks was not allowed for tracing programs due to insufficient preemption checks. The verifier does not currently prevent LSM programs from using spin locks, but the helpers are not exposed via bpf_lsm_func_proto. Based on the discussion in [1], non-sleepable LSM programs should be able to use bpf_spin_{lock, unlock}. Sleepable LSM programs can be preempted which means that allowng spin locks will need more work (disabling preemption and the verifier ensuring that no sleepable helpers are called when a spin lock is held). [1]: https://lore.kernel.org/bpf/[email protected]/T/#md601a053229287659071600d3483523f752cd2fb Signed-off-by: KP Singh <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Song Liu <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent c6bde95 commit 9e7a4d9

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

kernel/bpf/bpf_lsm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5959
return &bpf_sk_storage_get_proto;
6060
case BPF_FUNC_sk_storage_delete:
6161
return &bpf_sk_storage_delete_proto;
62+
case BPF_FUNC_spin_lock:
63+
return &bpf_spin_lock_proto;
64+
case BPF_FUNC_spin_unlock:
65+
return &bpf_spin_unlock_proto;
6266
default:
6367
return tracing_prog_func_proto(func_id, prog);
6468
}

kernel/bpf/verifier.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9719,11 +9719,21 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env,
97199719
verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n");
97209720
}
97219721

9722-
if ((is_tracing_prog_type(prog_type) ||
9723-
prog_type == BPF_PROG_TYPE_SOCKET_FILTER) &&
9724-
map_value_has_spin_lock(map)) {
9725-
verbose(env, "tracing progs cannot use bpf_spin_lock yet\n");
9726-
return -EINVAL;
9722+
if (map_value_has_spin_lock(map)) {
9723+
if (prog_type == BPF_PROG_TYPE_SOCKET_FILTER) {
9724+
verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n");
9725+
return -EINVAL;
9726+
}
9727+
9728+
if (is_tracing_prog_type(prog_type)) {
9729+
verbose(env, "tracing progs cannot use bpf_spin_lock yet\n");
9730+
return -EINVAL;
9731+
}
9732+
9733+
if (prog->aux->sleepable) {
9734+
verbose(env, "sleepable progs cannot use bpf_spin_lock yet\n");
9735+
return -EINVAL;
9736+
}
97279737
}
97289738

97299739
if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) &&

0 commit comments

Comments
 (0)