bpf: Reject bpf_timer for PREEMPT_RT
Subsystems (4)
v3 cover bpf: Reject bpf_timer for PREEMPT_RT
While running './test_progs -t timer' to validate the test case from "selftests/bpf: Introduce experimental bpf_in_interrupt()"[0] for PREEMPT_RT, I encountered a kernel warning:
BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:48
To address this, reject bpf_timer usage in the verifier when PREEMPT_RT is enabled, and skip the corresponding timer selftests.
Changes:
v2 -> v3:
* Drop skipping test case 'timer_interrupt'.
* Address comments from Alexei:
* Respin targeting bpf tree.
* Trim commit log.
v1 -> v2:
* Skip test case 'timer_interrupt'.
Links:
[0] https://lore.kernel.org/bpf/[email protected]/
Leon Hwang (2):
bpf: Reject bpf_timer for PREEMPT_RT
selftests/bpf: Skip timer cases when bpf_timer is not supported
kernel/bpf/verifier.c | 4 ++++ tools/testing/selftests/bpf/prog_tests/free_timer.c | 4 ++++ tools/testing/selftests/bpf/prog_tests/timer.c | 4 ++++ tools/testing/selftests/bpf/prog_tests/timer_crash.c | 4 ++++ tools/testing/selftests/bpf/prog_tests/timer_lockup.c | 4 ++++ tools/testing/selftests/bpf/prog_tests/timer_mim.c | 4 ++++ 6 files changed, 24 insertions(+)
--
2.50.1
PATCH v3 1/2 bpf: Reject bpf_timer for PREEMPT_RT
When enable CONFIG_PREEMPT_RT, the kernel will warn when run timer selftests by './test_progs -t timer':
BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:48
In order to avoid such warning, reject bpf_timer in verifier when PREEMPT_RT is enabled.
--- kernel/bpf/verifier.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/bpf/verifier.c +4 -0
@@ -8547,6 +8547,10 @@ static int process_timer_func(struct bpf_verifier_env *env, int regno,verifier_bug (env,"Two map pointers in a timer helper" );return -EFAULT ; } +if (IS_ENABLED (CONFIG_PREEMPT_RT )) { +verbose (env,"bpf_timer cannot be used for PREEMPT_RT.\n" ); +return -EOPNOTSUPP ; + } meta->map_uid = reg->map_uid; meta->map_ptr = map;return 0 ;
Signature · 1 line
2.50.1
PATCH v3 2/2 selftests/bpf: Skip timer cases when bpf_timer is not supported
In reply to Leon HwangWhen enable CONFIG_PREEMPT_RT, verifier will reject bpf_timer with returning -EOPNOTSUPP.
Therefore, skip test cases when errno is EOPNOTSUPP.
cd tools/testing/selftests/bpf
./test_progs -t timer
125 free_timer:SKIP 456 timer:SKIP 457/1 timer_crash/array:SKIP 457/2 timer_crash/hash:SKIP 457 timer_crash:SKIP 458 timer_lockup:SKIP 459 timer_mim:SKIP
Summary: 5/0 PASSED, 6 SKIPPED, 0 FAILED
--- tools/testing/selftests/bpf/prog_tests/free_timer.c | 4 ++++ tools/testing/selftests/bpf/prog_tests/timer.c | 4 ++++ tools/testing/selftests/bpf/prog_tests/timer_crash.c | 4 ++++ tools/testing/selftests/bpf/prog_tests/timer_lockup.c | 4 ++++ tools/testing/selftests/bpf/prog_tests/timer_mim.c | 4 ++++ 5 files changed, 20 insertions(+)
tools/testing/selftests/bpf/prog_tests/free_timer.c +4 -0
@@ -124,6 +124,10 @@ void test_free_timer(void)int err; skel =free_timer__open_and_load (); +if (!skel && errno ==EOPNOTSUPP ) { +test__skip (); +return ; + }if (!ASSERT_OK_PTR (skel,"open_load" ))return ;
tools/testing/selftests/bpf/prog_tests/timer.c +4 -0
@@ -86,6 +86,10 @@ void serial_test_timer(void)int err; timer_skel =timer__open_and_load (); +if (!timer_skel && errno ==EOPNOTSUPP ) { +test__skip (); +return ; + }if (!ASSERT_OK_PTR (timer_skel,"timer_skel_load" ))return ;
tools/testing/selftests/bpf/prog_tests/timer_crash.c +4 -0
@@ -12,6 +12,10 @@ static void test_timer_crash_mode(int mode)struct timer_crash *skel; skel =timer_crash__open_and_load (); +if (!skel && errno ==EOPNOTSUPP ) { +test__skip (); +return ; + }if (!ASSERT_OK_PTR (skel,"timer_crash__open_and_load" ))return ; skel->bss->pid =getpid ();
tools/testing/selftests/bpf/prog_tests/timer_lockup.c +4 -0
@@ -59,6 +59,10 @@ void test_timer_lockup(void) } skel =timer_lockup__open_and_load (); +if (!skel && errno ==EOPNOTSUPP ) { +test__skip (); +return ; + }if (!ASSERT_OK_PTR (skel,"timer_lockup__open_and_load" ))return ;
tools/testing/selftests/bpf/prog_tests/timer_mim.c +4 -0
@@ -65,6 +65,10 @@ void serial_test_timer_mim(void) goto cleanup; timer_skel =timer_mim__open_and_load (); +if (!timer_skel && errno ==EOPNOTSUPP ) { +test__skip (); +return ; + }if (!ASSERT_OK_PTR (timer_skel,"timer_skel_load" )) goto cleanup;
Signature · 1 line
2.50.1
This series was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:
Quoted 10 lines · On Wed, 10 Sep 2025 20:57:38 +0800 you wrote:
While running './test_progs -t timer' to validate the test case from "selftests/bpf: Introduce experimental bpf_in_interrupt()"[0] for
PREEMPT_RT, I encountered a kernel warning:BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:48To address this, reject
bpf_timerusage in the verifier whenPREEMPT_RTis enabled, and skip the corresponding timer selftests.[...]
Here is the summary with links:
- [bpf,v3,1/2] bpf: Reject bpf_timer for PREEMPT_RT
https://git.kernel.org/bpf/bpf/c/e25ddfb388c8 - [bpf,v3,2/2] selftests/bpf: Skip timer cases when bpf_timer is not supported https://git.kernel.org/bpf/bpf/c/fbdd61c94bcb
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html