xref: /linux/include/kunit/run-in-irq-context.h (revision e2fffe1d958b3660bc4e07e6542d97b6cc168826)
1*b41dc83fSEric Biggers /* SPDX-License-Identifier: GPL-2.0-or-later */
2*b41dc83fSEric Biggers /*
3*b41dc83fSEric Biggers  * Helper function for testing code in interrupt contexts
4*b41dc83fSEric Biggers  *
5*b41dc83fSEric Biggers  * Copyright 2025 Google LLC
6*b41dc83fSEric Biggers  */
7*b41dc83fSEric Biggers #ifndef _KUNIT_RUN_IN_IRQ_CONTEXT_H
8*b41dc83fSEric Biggers #define _KUNIT_RUN_IN_IRQ_CONTEXT_H
9*b41dc83fSEric Biggers 
10*b41dc83fSEric Biggers #include <kunit/test.h>
11*b41dc83fSEric Biggers #include <linux/timekeeping.h>
12*b41dc83fSEric Biggers #include <linux/hrtimer.h>
13*b41dc83fSEric Biggers #include <linux/workqueue.h>
14*b41dc83fSEric Biggers 
15*b41dc83fSEric Biggers #define KUNIT_IRQ_TEST_HRTIMER_INTERVAL us_to_ktime(5)
16*b41dc83fSEric Biggers 
17*b41dc83fSEric Biggers struct kunit_irq_test_state {
18*b41dc83fSEric Biggers 	bool (*func)(void *test_specific_state);
19*b41dc83fSEric Biggers 	void *test_specific_state;
20*b41dc83fSEric Biggers 	bool task_func_reported_failure;
21*b41dc83fSEric Biggers 	bool hardirq_func_reported_failure;
22*b41dc83fSEric Biggers 	bool softirq_func_reported_failure;
23*b41dc83fSEric Biggers 	unsigned long hardirq_func_calls;
24*b41dc83fSEric Biggers 	unsigned long softirq_func_calls;
25*b41dc83fSEric Biggers 	struct hrtimer timer;
26*b41dc83fSEric Biggers 	struct work_struct bh_work;
27*b41dc83fSEric Biggers };
28*b41dc83fSEric Biggers 
kunit_irq_test_timer_func(struct hrtimer * timer)29*b41dc83fSEric Biggers static enum hrtimer_restart kunit_irq_test_timer_func(struct hrtimer *timer)
30*b41dc83fSEric Biggers {
31*b41dc83fSEric Biggers 	struct kunit_irq_test_state *state =
32*b41dc83fSEric Biggers 		container_of(timer, typeof(*state), timer);
33*b41dc83fSEric Biggers 
34*b41dc83fSEric Biggers 	WARN_ON_ONCE(!in_hardirq());
35*b41dc83fSEric Biggers 	state->hardirq_func_calls++;
36*b41dc83fSEric Biggers 
37*b41dc83fSEric Biggers 	if (!state->func(state->test_specific_state))
38*b41dc83fSEric Biggers 		state->hardirq_func_reported_failure = true;
39*b41dc83fSEric Biggers 
40*b41dc83fSEric Biggers 	hrtimer_forward_now(&state->timer, KUNIT_IRQ_TEST_HRTIMER_INTERVAL);
41*b41dc83fSEric Biggers 	queue_work(system_bh_wq, &state->bh_work);
42*b41dc83fSEric Biggers 	return HRTIMER_RESTART;
43*b41dc83fSEric Biggers }
44*b41dc83fSEric Biggers 
kunit_irq_test_bh_work_func(struct work_struct * work)45*b41dc83fSEric Biggers static void kunit_irq_test_bh_work_func(struct work_struct *work)
46*b41dc83fSEric Biggers {
47*b41dc83fSEric Biggers 	struct kunit_irq_test_state *state =
48*b41dc83fSEric Biggers 		container_of(work, typeof(*state), bh_work);
49*b41dc83fSEric Biggers 
50*b41dc83fSEric Biggers 	WARN_ON_ONCE(!in_serving_softirq());
51*b41dc83fSEric Biggers 	state->softirq_func_calls++;
52*b41dc83fSEric Biggers 
53*b41dc83fSEric Biggers 	if (!state->func(state->test_specific_state))
54*b41dc83fSEric Biggers 		state->softirq_func_reported_failure = true;
55*b41dc83fSEric Biggers }
56*b41dc83fSEric Biggers 
57*b41dc83fSEric Biggers /*
58*b41dc83fSEric Biggers  * Helper function which repeatedly runs the given @func in task, softirq, and
59*b41dc83fSEric Biggers  * hardirq context concurrently, and reports a failure to KUnit if any
60*b41dc83fSEric Biggers  * invocation of @func in any context returns false.  @func is passed
61*b41dc83fSEric Biggers  * @test_specific_state as its argument.  At most 3 invocations of @func will
62*b41dc83fSEric Biggers  * run concurrently: one in each of task, softirq, and hardirq context.
63*b41dc83fSEric Biggers  *
64*b41dc83fSEric Biggers  * The main purpose of this interrupt context testing is to validate fallback
65*b41dc83fSEric Biggers  * code paths that run in contexts where the normal code path cannot be used,
66*b41dc83fSEric Biggers  * typically due to the FPU or vector registers already being in-use in kernel
67*b41dc83fSEric Biggers  * mode.  These code paths aren't covered when the test code is executed only by
68*b41dc83fSEric Biggers  * the KUnit test runner thread in task context.  The reason for the concurrency
69*b41dc83fSEric Biggers  * is because merely using hardirq context is not sufficient to reach a fallback
70*b41dc83fSEric Biggers  * code path on some architectures; the hardirq actually has to occur while the
71*b41dc83fSEric Biggers  * FPU or vector unit was already in-use in kernel mode.
72*b41dc83fSEric Biggers  *
73*b41dc83fSEric Biggers  * Another purpose of this testing is to detect issues with the architecture's
74*b41dc83fSEric Biggers  * irq_fpu_usable() and kernel_fpu_begin/end() or equivalent functions,
75*b41dc83fSEric Biggers  * especially in softirq context when the softirq may have interrupted a task
76*b41dc83fSEric Biggers  * already using kernel-mode FPU or vector (if the arch didn't prevent that).
77*b41dc83fSEric Biggers  * Crypto functions are often executed in softirqs, so this is important.
78*b41dc83fSEric Biggers  */
kunit_run_irq_test(struct kunit * test,bool (* func)(void *),int max_iterations,void * test_specific_state)79*b41dc83fSEric Biggers static inline void kunit_run_irq_test(struct kunit *test, bool (*func)(void *),
80*b41dc83fSEric Biggers 				      int max_iterations,
81*b41dc83fSEric Biggers 				      void *test_specific_state)
82*b41dc83fSEric Biggers {
83*b41dc83fSEric Biggers 	struct kunit_irq_test_state state = {
84*b41dc83fSEric Biggers 		.func = func,
85*b41dc83fSEric Biggers 		.test_specific_state = test_specific_state,
86*b41dc83fSEric Biggers 	};
87*b41dc83fSEric Biggers 	unsigned long end_jiffies;
88*b41dc83fSEric Biggers 
89*b41dc83fSEric Biggers 	/*
90*b41dc83fSEric Biggers 	 * Set up a hrtimer (the way we access hardirq context) and a work
91*b41dc83fSEric Biggers 	 * struct for the BH workqueue (the way we access softirq context).
92*b41dc83fSEric Biggers 	 */
93*b41dc83fSEric Biggers 	hrtimer_setup_on_stack(&state.timer, kunit_irq_test_timer_func,
94*b41dc83fSEric Biggers 			       CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
95*b41dc83fSEric Biggers 	INIT_WORK_ONSTACK(&state.bh_work, kunit_irq_test_bh_work_func);
96*b41dc83fSEric Biggers 
97*b41dc83fSEric Biggers 	/* Run for up to max_iterations or 1 second, whichever comes first. */
98*b41dc83fSEric Biggers 	end_jiffies = jiffies + HZ;
99*b41dc83fSEric Biggers 	hrtimer_start(&state.timer, KUNIT_IRQ_TEST_HRTIMER_INTERVAL,
100*b41dc83fSEric Biggers 		      HRTIMER_MODE_REL_HARD);
101*b41dc83fSEric Biggers 	for (int i = 0; i < max_iterations && !time_after(jiffies, end_jiffies);
102*b41dc83fSEric Biggers 	     i++) {
103*b41dc83fSEric Biggers 		if (!func(test_specific_state))
104*b41dc83fSEric Biggers 			state.task_func_reported_failure = true;
105*b41dc83fSEric Biggers 	}
106*b41dc83fSEric Biggers 
107*b41dc83fSEric Biggers 	/* Cancel the timer and work. */
108*b41dc83fSEric Biggers 	hrtimer_cancel(&state.timer);
109*b41dc83fSEric Biggers 	flush_work(&state.bh_work);
110*b41dc83fSEric Biggers 
111*b41dc83fSEric Biggers 	/* Sanity check: the timer and BH functions should have been run. */
112*b41dc83fSEric Biggers 	KUNIT_EXPECT_GT_MSG(test, state.hardirq_func_calls, 0,
113*b41dc83fSEric Biggers 			    "Timer function was not called");
114*b41dc83fSEric Biggers 	KUNIT_EXPECT_GT_MSG(test, state.softirq_func_calls, 0,
115*b41dc83fSEric Biggers 			    "BH work function was not called");
116*b41dc83fSEric Biggers 
117*b41dc83fSEric Biggers 	/* Check for incorrect hash values reported from any context. */
118*b41dc83fSEric Biggers 	KUNIT_EXPECT_FALSE_MSG(
119*b41dc83fSEric Biggers 		test, state.task_func_reported_failure,
120*b41dc83fSEric Biggers 		"Incorrect hash values reported from task context");
121*b41dc83fSEric Biggers 	KUNIT_EXPECT_FALSE_MSG(
122*b41dc83fSEric Biggers 		test, state.hardirq_func_reported_failure,
123*b41dc83fSEric Biggers 		"Incorrect hash values reported from hardirq context");
124*b41dc83fSEric Biggers 	KUNIT_EXPECT_FALSE_MSG(
125*b41dc83fSEric Biggers 		test, state.softirq_func_reported_failure,
126*b41dc83fSEric Biggers 		"Incorrect hash values reported from softirq context");
127*b41dc83fSEric Biggers }
128*b41dc83fSEric Biggers 
129*b41dc83fSEric Biggers #endif /* _KUNIT_RUN_IN_IRQ_CONTEXT_H */
130