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