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

Changed files
+33 -20
include
+33 -20
include/kunit/run-in-irq-context.h
··· 20 20 bool task_func_reported_failure; 21 21 bool hardirq_func_reported_failure; 22 22 bool softirq_func_reported_failure; 23 - unsigned long hardirq_func_calls; 24 - unsigned long softirq_func_calls; 23 + atomic_t hardirq_func_calls; 24 + atomic_t softirq_func_calls; 25 25 struct hrtimer timer; 26 26 struct work_struct bh_work; 27 27 }; ··· 32 32 container_of(timer, typeof(*state), timer); 33 33 34 34 WARN_ON_ONCE(!in_hardirq()); 35 - state->hardirq_func_calls++; 35 + atomic_inc(&state->hardirq_func_calls); 36 36 37 37 if (!state->func(state->test_specific_state)) 38 38 state->hardirq_func_reported_failure = true; ··· 48 48 container_of(work, typeof(*state), bh_work); 49 49 50 50 WARN_ON_ONCE(!in_serving_softirq()); 51 - state->softirq_func_calls++; 51 + atomic_inc(&state->softirq_func_calls); 52 52 53 53 if (!state->func(state->test_specific_state)) 54 54 state->softirq_func_reported_failure = true; ··· 59 59 * hardirq context concurrently, and reports a failure to KUnit if any 60 60 * invocation of @func in any context returns false. @func is passed 61 61 * @test_specific_state as its argument. At most 3 invocations of @func will 62 - * run concurrently: one in each of task, softirq, and hardirq context. 62 + * run concurrently: one in each of task, softirq, and hardirq context. @func 63 + * will continue running until either @max_iterations calls have been made (so 64 + * long as at least one each runs in task, softirq, and hardirq contexts), or 65 + * one second has passed. 63 66 * 64 67 * The main purpose of this interrupt context testing is to validate fallback 65 68 * code paths that run in contexts where the normal code path cannot be used, ··· 88 85 .test_specific_state = test_specific_state, 89 86 }; 90 87 unsigned long end_jiffies; 88 + int hardirq_calls, softirq_calls; 89 + bool allctx = false; 91 90 92 91 /* 93 92 * Set up a hrtimer (the way we access hardirq context) and a work ··· 99 94 CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); 100 95 INIT_WORK_ONSTACK(&state.bh_work, kunit_irq_test_bh_work_func); 101 96 102 - /* Run for up to max_iterations or 1 second, whichever comes first. */ 97 + /* 98 + * Run for up to max_iterations (including at least one task, softirq, 99 + * and hardirq), or 1 second, whichever comes first. 100 + */ 103 101 end_jiffies = jiffies + HZ; 104 102 hrtimer_start(&state.timer, KUNIT_IRQ_TEST_HRTIMER_INTERVAL, 105 103 HRTIMER_MODE_REL_HARD); 106 - for (int i = 0; i < max_iterations && !time_after(jiffies, end_jiffies); 107 - i++) { 104 + for (int task_calls = 0, calls = 0; 105 + ((calls < max_iterations) || !allctx) && 106 + !time_after(jiffies, end_jiffies); 107 + task_calls++) { 108 108 if (!func(test_specific_state)) 109 109 state.task_func_reported_failure = true; 110 + 111 + hardirq_calls = atomic_read(&state.hardirq_func_calls); 112 + softirq_calls = atomic_read(&state.softirq_func_calls); 113 + calls = task_calls + hardirq_calls + softirq_calls; 114 + allctx = (task_calls > 0) && (hardirq_calls > 0) && 115 + (softirq_calls > 0); 110 116 } 111 117 112 118 /* Cancel the timer and work. */ ··· 125 109 flush_work(&state.bh_work); 126 110 127 111 /* Sanity check: the timer and BH functions should have been run. */ 128 - KUNIT_EXPECT_GT_MSG(test, state.hardirq_func_calls, 0, 112 + KUNIT_EXPECT_GT_MSG(test, atomic_read(&state.hardirq_func_calls), 0, 129 113 "Timer function was not called"); 130 - KUNIT_EXPECT_GT_MSG(test, state.softirq_func_calls, 0, 114 + KUNIT_EXPECT_GT_MSG(test, atomic_read(&state.softirq_func_calls), 0, 131 115 "BH work function was not called"); 132 116 133 - /* Check for incorrect hash values reported from any context. */ 134 - KUNIT_EXPECT_FALSE_MSG( 135 - test, state.task_func_reported_failure, 136 - "Incorrect hash values reported from task context"); 137 - KUNIT_EXPECT_FALSE_MSG( 138 - test, state.hardirq_func_reported_failure, 139 - "Incorrect hash values reported from hardirq context"); 140 - KUNIT_EXPECT_FALSE_MSG( 141 - test, state.softirq_func_reported_failure, 142 - "Incorrect hash values reported from softirq context"); 117 + /* Check for failure reported from any context. */ 118 + KUNIT_EXPECT_FALSE_MSG(test, state.task_func_reported_failure, 119 + "Failure reported from task context"); 120 + KUNIT_EXPECT_FALSE_MSG(test, state.hardirq_func_reported_failure, 121 + "Failure reported from hardirq context"); 122 + KUNIT_EXPECT_FALSE_MSG(test, state.softirq_func_reported_failure, 123 + "Failure reported from softirq context"); 143 124 } 144 125 145 126 #endif /* _KUNIT_RUN_IN_IRQ_CONTEXT_H */