1
0
mirror of https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git synced 2026-01-11 17:10:13 +00:00

Crypto library fix for v6.19-rc4

Fix the kunit_run_irq_test() function (which I recently added for the
 CRC and crypto tests) to be less timing-dependent. This fixes flakiness
 in the polyval kunit test suite.
 -----BEGIN PGP SIGNATURE-----
 
 iIoEABYIADIWIQSacvsUNc7UX4ntmEPzXCl4vpKOKwUCaVgMCxQcZWJpZ2dlcnNA
 a2VybmVsLm9yZwAKCRDzXCl4vpKOK46IAPwOtyn1r+t5tS4OWeyDYkCzNBamG4Xj
 Rhxlf2BX0jtHYQD/WulHkAdfhT/WYZJeGb9UvFvdZMsgbq4liLKJGbFn1Q4=
 =1kSJ
 -----END PGP SIGNATURE-----

Merge tag 'libcrypto-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux

Pull crypto library fix from Eric Biggers:
 "Fix the kunit_run_irq_test() function (which I recently added for the
  CRC and crypto tests) to be less timing-dependent.

  This fixes flakiness in the polyval kunit test suite"

* tag 'libcrypto-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux:
  kunit: Enforce task execution in {soft,hard}irq contexts
This commit is contained in:
Linus Torvalds 2026-01-02 12:28:24 -08:00
commit dec1ecf2c7

View File

@ -20,8 +20,8 @@ struct kunit_irq_test_state {
bool task_func_reported_failure; bool task_func_reported_failure;
bool hardirq_func_reported_failure; bool hardirq_func_reported_failure;
bool softirq_func_reported_failure; bool softirq_func_reported_failure;
unsigned long hardirq_func_calls; atomic_t hardirq_func_calls;
unsigned long softirq_func_calls; atomic_t softirq_func_calls;
struct hrtimer timer; struct hrtimer timer;
struct work_struct bh_work; struct work_struct bh_work;
}; };
@ -32,7 +32,7 @@ static enum hrtimer_restart kunit_irq_test_timer_func(struct hrtimer *timer)
container_of(timer, typeof(*state), timer); container_of(timer, typeof(*state), timer);
WARN_ON_ONCE(!in_hardirq()); WARN_ON_ONCE(!in_hardirq());
state->hardirq_func_calls++; atomic_inc(&state->hardirq_func_calls);
if (!state->func(state->test_specific_state)) if (!state->func(state->test_specific_state))
state->hardirq_func_reported_failure = true; state->hardirq_func_reported_failure = true;
@ -48,7 +48,7 @@ static void kunit_irq_test_bh_work_func(struct work_struct *work)
container_of(work, typeof(*state), bh_work); container_of(work, typeof(*state), bh_work);
WARN_ON_ONCE(!in_serving_softirq()); WARN_ON_ONCE(!in_serving_softirq());
state->softirq_func_calls++; atomic_inc(&state->softirq_func_calls);
if (!state->func(state->test_specific_state)) if (!state->func(state->test_specific_state))
state->softirq_func_reported_failure = true; state->softirq_func_reported_failure = true;
@ -59,7 +59,10 @@ static void kunit_irq_test_bh_work_func(struct work_struct *work)
* hardirq context concurrently, and reports a failure to KUnit if any * hardirq context concurrently, and reports a failure to KUnit if any
* invocation of @func in any context returns false. @func is passed * invocation of @func in any context returns false. @func is passed
* @test_specific_state as its argument. At most 3 invocations of @func will * @test_specific_state as its argument. At most 3 invocations of @func will
* run concurrently: one in each of task, softirq, and hardirq context. * run concurrently: one in each of task, softirq, and hardirq context. @func
* will continue running until either @max_iterations calls have been made (so
* long as at least one each runs in task, softirq, and hardirq contexts), or
* one second has passed.
* *
* The main purpose of this interrupt context testing is to validate fallback * The main purpose of this interrupt context testing is to validate fallback
* code paths that run in contexts where the normal code path cannot be used, * code paths that run in contexts where the normal code path cannot be used,
@ -85,6 +88,8 @@ static inline void kunit_run_irq_test(struct kunit *test, bool (*func)(void *),
.test_specific_state = test_specific_state, .test_specific_state = test_specific_state,
}; };
unsigned long end_jiffies; unsigned long end_jiffies;
int hardirq_calls, softirq_calls;
bool allctx = false;
/* /*
* Set up a hrtimer (the way we access hardirq context) and a work * Set up a hrtimer (the way we access hardirq context) and a work
@ -94,14 +99,25 @@ static inline void kunit_run_irq_test(struct kunit *test, bool (*func)(void *),
CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
INIT_WORK_ONSTACK(&state.bh_work, kunit_irq_test_bh_work_func); INIT_WORK_ONSTACK(&state.bh_work, kunit_irq_test_bh_work_func);
/* Run for up to max_iterations or 1 second, whichever comes first. */ /*
* Run for up to max_iterations (including at least one task, softirq,
* and hardirq), or 1 second, whichever comes first.
*/
end_jiffies = jiffies + HZ; end_jiffies = jiffies + HZ;
hrtimer_start(&state.timer, KUNIT_IRQ_TEST_HRTIMER_INTERVAL, hrtimer_start(&state.timer, KUNIT_IRQ_TEST_HRTIMER_INTERVAL,
HRTIMER_MODE_REL_HARD); HRTIMER_MODE_REL_HARD);
for (int i = 0; i < max_iterations && !time_after(jiffies, end_jiffies); for (int task_calls = 0, calls = 0;
i++) { ((calls < max_iterations) || !allctx) &&
!time_after(jiffies, end_jiffies);
task_calls++) {
if (!func(test_specific_state)) if (!func(test_specific_state))
state.task_func_reported_failure = true; state.task_func_reported_failure = true;
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);
} }
/* Cancel the timer and work. */ /* Cancel the timer and work. */
@ -109,21 +125,18 @@ static inline void kunit_run_irq_test(struct kunit *test, bool (*func)(void *),
flush_work(&state.bh_work); flush_work(&state.bh_work);
/* Sanity check: the timer and BH functions should have been run. */ /* Sanity check: the timer and BH functions should have been run. */
KUNIT_EXPECT_GT_MSG(test, state.hardirq_func_calls, 0, KUNIT_EXPECT_GT_MSG(test, atomic_read(&state.hardirq_func_calls), 0,
"Timer function was not called"); "Timer function was not called");
KUNIT_EXPECT_GT_MSG(test, state.softirq_func_calls, 0, KUNIT_EXPECT_GT_MSG(test, atomic_read(&state.softirq_func_calls), 0,
"BH work function was not called"); "BH work function was not called");
/* Check for incorrect hash values reported from any context. */ /* Check for failure reported from any context. */
KUNIT_EXPECT_FALSE_MSG( KUNIT_EXPECT_FALSE_MSG(test, state.task_func_reported_failure,
test, state.task_func_reported_failure, "Failure reported from task context");
"Incorrect hash values reported from task context"); KUNIT_EXPECT_FALSE_MSG(test, state.hardirq_func_reported_failure,
KUNIT_EXPECT_FALSE_MSG( "Failure reported from hardirq context");
test, state.hardirq_func_reported_failure, KUNIT_EXPECT_FALSE_MSG(test, state.softirq_func_reported_failure,
"Incorrect hash values reported from hardirq context"); "Failure reported from softirq context");
KUNIT_EXPECT_FALSE_MSG(
test, state.softirq_func_reported_failure,
"Incorrect hash values reported from softirq context");
} }
#endif /* _KUNIT_RUN_IN_IRQ_CONTEXT_H */ #endif /* _KUNIT_RUN_IN_IRQ_CONTEXT_H */