kunit: irq: Ensure timer doesn't fire too frequently
Subsystems (3)
PATCH 1/1 kunit: irq: Ensure timer doesn't fire too frequently
Fix a bug where kunit_run_irq_test() could hang if the system is too slow. This was noticed with the crypto library tests in certain VMs.
Specifically, if kunit_irq_test_timer_func() and the associated hrtimer code took over 5us to run, then the CPU would spend all its time executing that code in hardirq context. As a result, the task executing kunit_run_irq_test() never had a chance to run, exit the loop, and cancel the timer.
To fix it, make kunit_irq_test_timer_func() increase the timer interval when the other contexts aren't having a chance to run.
---
This patch applies to v7.0-rc1 and is targeting libcrypto-fixes
include/kunit/run-in-irq-context.h | 44 +++++++++++++++++++----------- 1 file changed, 28 insertions(+), 16 deletions(-)
include/kunit/run-in-irq-context.h +28 -16
@@ -10,36 +10,47 @@#include <kunit/test.h> #include <linux/timekeeping.h> #include <linux/hrtimer.h> #include <linux/workqueue.h> -#define KUNIT_IRQ_TEST_HRTIMER_INTERVAL us_to_ktime(5) -struct kunit_irq_test_state {bool (*func)(void *test_specific_state);void *test_specific_state;bool task_func_reported_failure;bool hardirq_func_reported_failure;bool softirq_func_reported_failure; +atomic_t task_func_calls;atomic_t hardirq_func_calls;atomic_t softirq_func_calls; +ktime_t interval;struct hrtimer timer;struct work_struct bh_work; };static enum hrtimer_restart kunit_irq_test_timer_func (struct hrtimer *timer) {struct kunit_irq_test_state *state =container_of (timer,typeof (*state), timer); +int task_calls, hardirq_calls, softirq_calls;WARN_ON_ONCE (!in_hardirq ()); -atomic_inc (&state->hardirq_func_calls); + task_calls =atomic_read (&state->task_func_calls); + hardirq_calls =atomic_inc_return (&state->hardirq_func_calls); + softirq_calls =atomic_read (&state->softirq_func_calls); + +/* +* If the timer is firing too often for the softirq or task to ever have +* a chance to run, increase the timer interval. This is needed on very +* slow systems. +*/ +if (hardirq_calls >=20 && (softirq_calls ==0 || task_calls ==0 )) + state->interval =ktime_add_ns (state->interval,250 );if (!state->func (state->test_specific_state)) state->hardirq_func_reported_failure = true; -hrtimer_forward_now (&state->timer,KUNIT_IRQ_TEST_HRTIMER_INTERVAL ); +hrtimer_forward_now (&state->timer, state->interval);queue_work (system_bh_wq, &state->bh_work);return HRTIMER_RESTART ; }static void kunit_irq_test_bh_work_func (struct work_struct *work)
@@ -84,14 +95,18 @@ static inline void kunit_run_irq_test(struct kunit *test, bool (*func)(void *), void *test_specific_state) {struct kunit_irq_test_state state = { .func = func, .test_specific_state = test_specific_state, +/* +* Start with a 5us timer interval. If the system can't keep +* up, kunit_irq_test_timer_func() will increase it. +*/ + .interval =us_to_ktime (5 ), };unsigned long end_jiffies; -int hardirq_calls, softirq_calls; -bool allctx = false; +int task_calls, hardirq_calls, softirq_calls;/* * Set up a hrtimer (the way we access hardirq context) and a work * struct for the BH workqueue (the way we access softirq context). */
@@ -102,25 +117,22 @@ static inline void kunit_run_irq_test(struct kunit *test, bool (*func)(void *),/* * Run for up to max_iterations (including at least one task, softirq, * and hardirq), or 1 second, whichever comes first. */ end_jiffies = jiffies +HZ ; -hrtimer_start (&state.timer,KUNIT_IRQ_TEST_HRTIMER_INTERVAL , -HRTIMER_MODE_REL_HARD ); -for (int task_calls =0 , calls =0 ; - ((calls < max_iterations) || !allctx) && - !time_after (jiffies, end_jiffies); - task_calls++) { +hrtimer_start (&state.timer, state.interval,HRTIMER_MODE_REL_HARD ); +do {if (!func (test_specific_state)) state.task_func_reported_failure = true; + task_calls =atomic_inc_return (&state.task_func_calls); hardirq_calls =atomic_read (&state.hardirq_func_calls); softirq_calls =atomic_read (&state.softirq_func_calls); - calls = task_calls + hardirq_calls + softirq_calls; - allctx = (task_calls >0 ) && (hardirq_calls >0 ) && - (softirq_calls >0 ); - } + }while ((task_calls + hardirq_calls + softirq_calls < max_iterations || + (task_calls ==0 || hardirq_calls ==0 || + softirq_calls ==0 )) && + !time_after (jiffies, end_jiffies));/* Cancel the timer and work. */ hrtimer_cancel (&state.timer);flush_work (&state.bh_work);
base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f
Signature · 1 line
2.53.0
Le 24/02/2026 à 11:37, 'Eric Biggers' via KUnit Development a écrit :
Quoted 9 lines
Fix a bug where
kunit_run_irq_test()could hang if the system is too slow. This was noticed with the crypto library tests in certain VMs.Specifically, if
kunit_irq_test_timer_func()and the associated hrtimer code took over 5us to run, then the CPU would spend all its time executing that code in hardirq context. As a result, the task executingkunit_run_irq_test()never had a chance to run, exit the loop, and cancel the timer.
Quoted 7 lines
To fix it, make
kunit_irq_test_timer_func()increase the timer interval when the other contexts aren't having a chance to run.Fixes 950a81224e8b ("lib/crypto: tests: Add hash-test-template.h and gen-hash-testvecs.py")Signed-off-by Eric Biggers <[email protected]>---
Looks good to me, thanks!
Cheers,
-- David
Quoted 118 lines
This patch applies to v7.0-rc1 and is targeting libcrypto-fixes
include/kunit/run-in-irq-context.h | 44 +++++++++++++++++++----------- 1 file changed, 28 insertions(+), 16 deletions(-)include/kunit/run-in-irq-context.h +28
-16@@ -10,36 +10,47 @@#include <kunit/test.h> #include <linux/timekeeping.h> #include <linux/hrtimer.h> #include <linux/workqueue.h> -#define KUNIT_IRQ_TEST_HRTIMER_INTERVAL us_to_ktime(5) -struct kunit_irq_test_state {bool (*func)(void *test_specific_state);void *test_specific_state;bool task_func_reported_failure;bool hardirq_func_reported_failure;bool softirq_func_reported_failure; +atomic_t task_func_calls;atomic_t hardirq_func_calls;atomic_t softirq_func_calls; +ktime_t interval;struct hrtimer timer;struct work_struct bh_work; };static enum hrtimer_restart kunit_irq_test_timer_func (struct hrtimer *timer) {struct kunit_irq_test_state *state =container_of (timer,typeof (*state), timer); +int task_calls, hardirq_calls, softirq_calls;WARN_ON_ONCE (!in_hardirq ()); -atomic_inc (&state->hardirq_func_calls); + task_calls =atomic_read (&state->task_func_calls); + hardirq_calls =atomic_inc_return (&state->hardirq_func_calls); + softirq_calls =atomic_read (&state->softirq_func_calls); + +/* +* If the timer is firing too often for the softirq or task to ever have +* a chance to run, increase the timer interval. This is needed on very +* slow systems. +*/ +if (hardirq_calls >=20 && (softirq_calls ==0 || task_calls ==0 )) + state->interval =ktime_add_ns (state->interval,250 );if (!state->func (state->test_specific_state)) state->hardirq_func_reported_failure = true; -hrtimer_forward_now (&state->timer,KUNIT_IRQ_TEST_HRTIMER_INTERVAL ); +hrtimer_forward_now (&state->timer, state->interval);queue_work (system_bh_wq, &state->bh_work);return HRTIMER_RESTART ; }static void kunit_irq_test_bh_work_func (struct work_struct *work)@@ -84,14 +95,18 @@ static inline void kunit_run_irq_test(struct kunit *test, bool (*func)(void *), void *test_specific_state) {struct kunit_irq_test_state state = { .func = func, .test_specific_state = test_specific_state, +/* +* Start with a 5us timer interval. If the system can't keep +* up, kunit_irq_test_timer_func() will increase it. +*/ + .interval =us_to_ktime (5 ), };unsigned long end_jiffies; -int hardirq_calls, softirq_calls; -bool allctx = false; +int task_calls, hardirq_calls, softirq_calls;/* * Set up a hrtimer (the way we access hardirq context) and a work * struct for the BH workqueue (the way we access softirq context). */ @@ -102,25 +117,22 @@ static inline void kunit_run_irq_test(struct kunit *test, bool (*func)(void *),/* * Run for up to max_iterations (including at least one task, softirq, * and hardirq), or 1 second, whichever comes first. */ end_jiffies = jiffies +HZ ; -hrtimer_start (&state.timer,KUNIT_IRQ_TEST_HRTIMER_INTERVAL , -HRTIMER_MODE_REL_HARD ); -for (int task_calls =0 , calls =0 ; - ((calls < max_iterations) || !allctx) && - !time_after (jiffies, end_jiffies); - task_calls++) { +hrtimer_start (&state.timer, state.interval,HRTIMER_MODE_REL_HARD ); +do {if (!func (test_specific_state)) state.task_func_reported_failure = true; + task_calls =atomic_inc_return (&state.task_func_calls); hardirq_calls =atomic_read (&state.hardirq_func_calls); softirq_calls =atomic_read (&state.softirq_func_calls); - calls = task_calls + hardirq_calls + softirq_calls; - allctx = (task_calls >0 ) && (hardirq_calls >0 ) && - (softirq_calls >0 ); - } + }while ((task_calls + hardirq_calls + softirq_calls < max_iterations || + (task_calls ==0 || hardirq_calls ==0 || + softirq_calls ==0 )) && + !time_after (jiffies, end_jiffies));/* Cancel the timer and work. */ hrtimer_cancel (&state.timer);flush_work (&state.bh_work);base-commit:
6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f
Eric Biggers
AppliedQuoted 15 lines · Eric Biggers
Fix a bug where
kunit_run_irq_test()could hang if the system is too slow. This was noticed with the crypto library tests in certain VMs.Specifically, if
kunit_irq_test_timer_func()and the associated hrtimer code took over 5us to run, then the CPU would spend all its time executing that code in hardirq context. As a result, the task executingkunit_run_irq_test()never had a chance to run, exit the loop, and cancel the timer.To fix it, make
kunit_irq_test_timer_func()increase the timer interval when the other contexts aren't having a chance to run.Fixes 950a81224e8b ("lib/crypto: tests: Add hash-test-template.h and gen-hash-testvecs.py")Signed-off-by Eric Biggers <[email protected]>
Quoted 6 lines
---
This patch applies to v7.0-rc1 and is targeting libcrypto-fixes
include/kunit/run-in-irq-context.h | 44 +++++++++++++++++++----------- 1 file changed, 28 insertions(+), 16 deletions(-)
Applied to https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git/log/?h=libcrypto-fixes
(Additional reviews always appreciated, of course)
- Eric