selftests/bpf: Test non-sleepable kfunc context

A sleepable kfunc call can be rejected because the program itself is
not sleepable or because a sleepable program is inside an active
critical section.

Exercise the two causes separately. Call a sleepable kfunc directly
from a non-sleepable tracing program, then call it from a sleepable
tracing program inside an RCU read-side critical section. Assert both
the legacy verifier messages and the structured reasons.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/2e42a1a2bf45f4d2aba7495bdc9f147558055740e2f3c8b9dae255f6c57fc13c@mail.kernel.org/
Link: https://lore.kernel.org/r/20260924170646.2366016-6-memxor@gmail.com
Link: https://lore.kernel.org/bpf/20260926133048.2962553-2-memxor@gmail.com
This commit is contained in:
Kumar Kartikeya Dwivedi 2026-09-26 15:30:43 +02:00 committed by Daniel Borkmann
parent acff58e305
commit 8295510622

View File

@ -6,6 +6,8 @@
#include "bpf_experimental.h"
extern int bpf_copy_from_user_str(void *dst, u32 dst__sz, const void *unsafe_ptr__ign, u64 flags) __weak __ksym;
extern void bpf_rcu_read_lock(void) __ksym;
extern void bpf_rcu_read_unlock(void) __ksym;
SEC("?tc")
__failure __msg("BPF_EXIT instruction in main prog cannot be used inside bpf_preempt_disable-ed region")
@ -179,6 +181,30 @@ int preempt_sleepable_kfunc(void *ctx)
return 0;
}
SEC("?fentry/" SYS_PREFIX "sys_getpgid")
__failure __msg("program must be sleepable to call sleepable kfunc bpf_copy_from_user_str")
__msg("cannot be used in non-sleepable program")
int non_sleepable_kfunc(void *ctx)
{
u32 data;
bpf_copy_from_user_str(&data, sizeof(data), NULL, 0);
return 0;
}
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
__failure __msg("kernel func bpf_copy_from_user_str is sleepable within rcu_read_lock region")
__msg("cannot be used in RCU read lock region")
int sleepable_kfunc_in_rcu(void *ctx)
{
u32 data;
bpf_rcu_read_lock();
bpf_copy_from_user_str(&data, sizeof(data), NULL, 0);
bpf_rcu_read_unlock();
return 0;
}
int __noinline preempt_global_subprog(void)
{
preempt_balance_subprog();