run-in-irq-context.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Helper function for testing code in interrupt contexts
  4. *
  5. * Copyright 2025 Google LLC
  6. */
  7. #ifndef _KUNIT_RUN_IN_IRQ_CONTEXT_H
  8. #define _KUNIT_RUN_IN_IRQ_CONTEXT_H
  9. #include <kunit/test.h>
  10. #include <linux/timekeeping.h>
  11. #include <linux/hrtimer.h>
  12. #include <linux/workqueue.h>
  13. struct kunit_irq_test_state {
  14. bool (*func)(void *test_specific_state);
  15. void *test_specific_state;
  16. bool task_func_reported_failure;
  17. bool hardirq_func_reported_failure;
  18. bool softirq_func_reported_failure;
  19. atomic_t task_func_calls;
  20. atomic_t hardirq_func_calls;
  21. atomic_t softirq_func_calls;
  22. ktime_t interval;
  23. struct hrtimer timer;
  24. struct work_struct bh_work;
  25. };
  26. static enum hrtimer_restart kunit_irq_test_timer_func(struct hrtimer *timer)
  27. {
  28. struct kunit_irq_test_state *state =
  29. container_of(timer, typeof(*state), timer);
  30. int task_calls, hardirq_calls, softirq_calls;
  31. WARN_ON_ONCE(!in_hardirq());
  32. task_calls = atomic_read(&state->task_func_calls);
  33. hardirq_calls = atomic_inc_return(&state->hardirq_func_calls);
  34. softirq_calls = atomic_read(&state->softirq_func_calls);
  35. /*
  36. * If the timer is firing too often for the softirq or task to ever have
  37. * a chance to run, increase the timer interval. This is needed on very
  38. * slow systems.
  39. */
  40. if (hardirq_calls >= 20 && (softirq_calls == 0 || task_calls == 0))
  41. state->interval = ktime_add_ns(state->interval, 250);
  42. if (!state->func(state->test_specific_state))
  43. state->hardirq_func_reported_failure = true;
  44. hrtimer_forward_now(&state->timer, state->interval);
  45. queue_work(system_bh_wq, &state->bh_work);
  46. return HRTIMER_RESTART;
  47. }
  48. static void kunit_irq_test_bh_work_func(struct work_struct *work)
  49. {
  50. struct kunit_irq_test_state *state =
  51. container_of(work, typeof(*state), bh_work);
  52. WARN_ON_ONCE(!in_serving_softirq());
  53. atomic_inc(&state->softirq_func_calls);
  54. if (!state->func(state->test_specific_state))
  55. state->softirq_func_reported_failure = true;
  56. }
  57. /*
  58. * Helper function which repeatedly runs the given @func in task, softirq, and
  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,
  69. * typically due to the FPU or vector registers already being in-use in kernel
  70. * mode. These code paths aren't covered when the test code is executed only by
  71. * the KUnit test runner thread in task context. The reason for the concurrency
  72. * is because merely using hardirq context is not sufficient to reach a fallback
  73. * code path on some architectures; the hardirq actually has to occur while the
  74. * FPU or vector unit was already in-use in kernel mode.
  75. *
  76. * Another purpose of this testing is to detect issues with the architecture's
  77. * irq_fpu_usable() and kernel_fpu_begin/end() or equivalent functions,
  78. * especially in softirq context when the softirq may have interrupted a task
  79. * already using kernel-mode FPU or vector (if the arch didn't prevent that).
  80. * Crypto functions are often executed in softirqs, so this is important.
  81. */
  82. static inline void kunit_run_irq_test(struct kunit *test, bool (*func)(void *),
  83. int max_iterations,
  84. void *test_specific_state)
  85. {
  86. struct kunit_irq_test_state state = {
  87. .func = func,
  88. .test_specific_state = test_specific_state,
  89. /*
  90. * Start with a 5us timer interval. If the system can't keep
  91. * up, kunit_irq_test_timer_func() will increase it.
  92. */
  93. .interval = us_to_ktime(5),
  94. };
  95. unsigned long end_jiffies;
  96. int task_calls, hardirq_calls, softirq_calls;
  97. /*
  98. * Set up a hrtimer (the way we access hardirq context) and a work
  99. * struct for the BH workqueue (the way we access softirq context).
  100. */
  101. hrtimer_setup_on_stack(&state.timer, kunit_irq_test_timer_func,
  102. CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
  103. INIT_WORK_ONSTACK(&state.bh_work, kunit_irq_test_bh_work_func);
  104. /*
  105. * Run for up to max_iterations (including at least one task, softirq,
  106. * and hardirq), or 1 second, whichever comes first.
  107. */
  108. end_jiffies = jiffies + HZ;
  109. hrtimer_start(&state.timer, state.interval, HRTIMER_MODE_REL_HARD);
  110. do {
  111. if (!func(test_specific_state))
  112. state.task_func_reported_failure = true;
  113. task_calls = atomic_inc_return(&state.task_func_calls);
  114. hardirq_calls = atomic_read(&state.hardirq_func_calls);
  115. softirq_calls = atomic_read(&state.softirq_func_calls);
  116. } while ((task_calls + hardirq_calls + softirq_calls < max_iterations ||
  117. (task_calls == 0 || hardirq_calls == 0 ||
  118. softirq_calls == 0)) &&
  119. !time_after(jiffies, end_jiffies));
  120. /* Cancel the timer and work. */
  121. hrtimer_cancel(&state.timer);
  122. flush_work(&state.bh_work);
  123. /* Sanity check: the timer and BH functions should have been run. */
  124. KUNIT_EXPECT_GT_MSG(test, atomic_read(&state.hardirq_func_calls), 0,
  125. "Timer function was not called");
  126. KUNIT_EXPECT_GT_MSG(test, atomic_read(&state.softirq_func_calls), 0,
  127. "BH work function was not called");
  128. /* Check for failure reported from any context. */
  129. KUNIT_EXPECT_FALSE_MSG(test, state.task_func_reported_failure,
  130. "Failure reported from task context");
  131. KUNIT_EXPECT_FALSE_MSG(test, state.hardirq_func_reported_failure,
  132. "Failure reported from hardirq context");
  133. KUNIT_EXPECT_FALSE_MSG(test, state.softirq_func_reported_failure,
  134. "Failure reported from softirq context");
  135. }
  136. #endif /* _KUNIT_RUN_IN_IRQ_CONTEXT_H */