xref: /linux/lib/test_csd_lock.c (revision 486b257357c23743d9729ea715136d105fff461e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Keep one CPU from answering an IPI, so that the CSD-lock debug code in
4  * kernel/smp.c has a stall to report.
5  *
6  * Copyright (c) 2026 Meta Platforms, Inc. and affiliates
7  * Copyright (c) 2026 Breno Leitao <leitao@debian.org>
8  *
9  * The target either spins with interrupts disabled, which leaves it idle as
10  * far as the debug code can tell and gets the IPI re-sent, or spins inside a
11  * CSD handler, which does not.  The recovery message differs between the two.
12  *
13  * Loading the module runs one stall, then fails the load with -EAGAIN so
14  * that nothing is left loaded afterwards:
15  *
16  *	echo 500 > /sys/module/smp/parameters/csd_lock_timeout
17  *	modprobe test_csd_lock stall_ms=1000 in_handler=0
18  *
19  * csd_lock_timeout has to be below stall_ms for the stall to be reported at
20  * all, and the report has to come out before the CPU answers, so leave it
21  * some room.
22  */
23 
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 
26 #include <linux/completion.h>
27 #include <linux/cpu.h>
28 #include <linux/cpumask.h>
29 #include <linux/ktime.h>
30 #include <linux/module.h>
31 #include <linux/smp.h>
32 #include <linux/workqueue.h>
33 
34 #define STALL_MS_MAX	10000
35 
36 static unsigned int stall_ms = 1000;
37 module_param(stall_ms, uint, 0444);
38 MODULE_PARM_DESC(stall_ms, "Time the target CPU ignores the IPI, in milliseconds.");
39 
40 static int stall_cpu = -1;
41 module_param(stall_cpu, int, 0444);
42 MODULE_PARM_DESC(stall_cpu, "CPU to stall, or -1 for the first online one.");
43 
44 static bool in_handler;
45 module_param(in_handler, bool, 0444);
46 MODULE_PARM_DESC(in_handler, "Stall inside a CSD handler instead of with interrupts disabled.");
47 
48 static int target_cpu;
49 static bool target_stalling;
50 static bool hog_launched;
51 static struct work_struct irqoff_work;
52 static struct work_struct sender_work;
53 static call_single_data_t hog_csd;
54 static DECLARE_COMPLETION(hog_done);
55 
56 static void csd_test_nop(void *unused)
57 {
58 }
59 
60 static void csd_test_spin(void)
61 {
62 	u64 end = ktime_get_mono_fast_ns() + (u64)stall_ms * NSEC_PER_MSEC;
63 
64 	while (ktime_get_mono_fast_ns() < end)
65 		cpu_relax();
66 }
67 
68 /* Nothing is running for the target while interrupts are off, so it gets a new IPI. */
69 static void csd_test_irqoff_fn(struct work_struct *work)
70 {
71 	local_irq_disable();
72 	/* Pairs with the load in csd_test_sender_fn(), which waits for this. */
73 	smp_store_release(&target_stalling, true);
74 	csd_test_spin();
75 	local_irq_enable();
76 }
77 
78 /* Here cur_csd stays set on the target, which suppresses the re-send. */
79 static void csd_test_hog_fn(void *unused)
80 {
81 	/* Pairs with the load in csd_test_sender_fn(), which waits for this. */
82 	smp_store_release(&target_stalling, true);
83 	csd_test_spin();
84 	complete(&hog_done);
85 }
86 
87 /*
88  * Start the stall from here rather than from module init, so that however
89  * long this work item waits to be scheduled comes off before the target
90  * stops answering, not out of the middle of the stall.
91  */
92 static void csd_test_sender_fn(struct work_struct *work)
93 {
94 	u64 deadline, ts;
95 	int err;
96 
97 	if (in_handler) {
98 		hog_csd.func = csd_test_hog_fn;
99 		err = smp_call_function_single_async(target_cpu, &hog_csd);
100 		if (err) {
101 			pr_err("cannot queue the CSD handler on CPU%d: %d\n", target_cpu, err);
102 			return;
103 		}
104 	} else {
105 		queue_work_on(target_cpu, system_highpri_wq, &irqoff_work);
106 	}
107 	WRITE_ONCE(hog_launched, true);
108 
109 	deadline = ktime_get_mono_fast_ns() + (u64)STALL_MS_MAX * NSEC_PER_MSEC;
110 	/* Pairs with the store in the stall functions: send once it is stuck. */
111 	while (!smp_load_acquire(&target_stalling)) {
112 		if (ktime_get_mono_fast_ns() > deadline) {
113 			pr_err("CPU%d never stopped answering\n", target_cpu);
114 			return;
115 		}
116 		cpu_relax();
117 	}
118 
119 	ts = ktime_get_mono_fast_ns();
120 	smp_call_function_single(target_cpu, csd_test_nop, NULL, 1);
121 	pr_info("CPU%d answered after %llu ns\n", target_cpu,
122 		ktime_get_mono_fast_ns() - ts);
123 }
124 
125 static int __init test_csd_lock_init(void)
126 {
127 	int sender_cpu;
128 	int ret = 0;
129 
130 	if (!stall_ms || stall_ms > STALL_MS_MAX) {
131 		pr_err("stall_ms must be between 1 and %d\n", STALL_MS_MAX);
132 		return -EINVAL;
133 	}
134 
135 	INIT_WORK(&irqoff_work, csd_test_irqoff_fn);
136 	INIT_WORK(&sender_work, csd_test_sender_fn);
137 
138 	cpus_read_lock();
139 
140 	target_cpu = stall_cpu < 0 ? cpumask_first(cpu_online_mask) : stall_cpu;
141 	sender_cpu = nr_cpu_ids;
142 	if (target_cpu < nr_cpu_ids && cpu_online(target_cpu))
143 		sender_cpu = cpumask_any_but(cpu_online_mask, target_cpu);
144 	if (sender_cpu >= nr_cpu_ids) {
145 		pr_err("need CPU%d and one other CPU online\n", target_cpu);
146 		ret = -EINVAL;
147 		goto unlock;
148 	}
149 
150 	pr_info("stalling CPU%d for %u ms %s, IPI from CPU%d\n", target_cpu, stall_ms,
151 		in_handler ? "inside a CSD handler" : "with interrupts disabled", sender_cpu);
152 
153 	queue_work_on(sender_cpu, system_highpri_wq, &sender_work);
154 	flush_work(&sender_work);
155 	flush_work(&irqoff_work);
156 
157 	/* The CSD has to be idle again before this module goes away. */
158 	if (in_handler && READ_ONCE(hog_launched) &&
159 	    !wait_for_completion_timeout(&hog_done, msecs_to_jiffies(2 * STALL_MS_MAX)))
160 		pr_err("CSD handler on CPU%d never finished\n", target_cpu);
161 
162 	/* The stall is over and there is nothing left to hold, so go away. */
163 	ret = -EAGAIN;
164 unlock:
165 	cpus_read_unlock();
166 
167 	return ret;
168 }
169 module_init(test_csd_lock_init);
170 
171 MODULE_LICENSE("GPL");
172 MODULE_AUTHOR("Breno Leitao <leitao@debian.org>");
173 MODULE_DESCRIPTION("Test module to stall a CPU on a CSD lock");
174