#ifndef _KUNIT_RUN_IN_IRQ_CONTEXT_H
#define _KUNIT_RUN_IN_IRQ_CONTEXT_H
#include <kunit/test.h>
#include <linux/timekeeping.h>
#include <linux/hrtimer.h>
#include <linux/workqueue.h>
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());
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 (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, 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)
{
struct kunit_irq_test_state *state =
container_of(work, typeof(*state), bh_work);
WARN_ON_ONCE(!in_serving_softirq());
atomic_inc(&state->softirq_func_calls);
if (!state->func(state->test_specific_state))
state->softirq_func_reported_failure = true;
}
static inline void kunit_run_irq_test(struct kunit *test, bool (*func)(void *),
int max_iterations,
void *test_specific_state)
{
struct kunit_irq_test_state state = {
.func = func,
.test_specific_state = test_specific_state,
.interval = us_to_ktime(5),
};
unsigned long end_jiffies;
int task_calls, hardirq_calls, softirq_calls;
hrtimer_setup_on_stack(&state.timer, kunit_irq_test_timer_func,
CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
INIT_WORK_ONSTACK(&state.bh_work, kunit_irq_test_bh_work_func);
end_jiffies = jiffies + HZ;
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);
} while ((task_calls + hardirq_calls + softirq_calls < max_iterations ||
(task_calls == 0 || hardirq_calls == 0 ||
softirq_calls == 0)) &&
!time_after(jiffies, end_jiffies));
hrtimer_cancel(&state.timer);
flush_work(&state.bh_work);
KUNIT_EXPECT_GT_MSG(test, atomic_read(&state.hardirq_func_calls), 0,
"Timer function was not called");
KUNIT_EXPECT_GT_MSG(test, atomic_read(&state.softirq_func_calls), 0,
"BH work function was not called");
KUNIT_EXPECT_FALSE_MSG(test, state.task_func_reported_failure,
"Failure reported from task context");
KUNIT_EXPECT_FALSE_MSG(test, state.hardirq_func_reported_failure,
"Failure reported from hardirq context");
KUNIT_EXPECT_FALSE_MSG(test, state.softirq_func_reported_failure,
"Failure reported from softirq context");
}
#endif