1724c299cSMarco Elver // SPDX-License-Identifier: GPL-2.0
2724c299cSMarco Elver /*
3724c299cSMarco Elver * KUnit test for hw_breakpoint constraints accounting logic.
4724c299cSMarco Elver *
5724c299cSMarco Elver * Copyright (C) 2022, Google LLC.
6724c299cSMarco Elver */
7724c299cSMarco Elver
8724c299cSMarco Elver #include <kunit/test.h>
9724c299cSMarco Elver #include <linux/cpumask.h>
10724c299cSMarco Elver #include <linux/hw_breakpoint.h>
11724c299cSMarco Elver #include <linux/kthread.h>
12724c299cSMarco Elver #include <linux/perf_event.h>
13724c299cSMarco Elver #include <asm/hw_breakpoint.h>
14724c299cSMarco Elver
15724c299cSMarco Elver #define TEST_REQUIRES_BP_SLOTS(test, slots) \
16724c299cSMarco Elver do { \
17724c299cSMarco Elver if ((slots) > get_test_bp_slots()) { \
18724c299cSMarco Elver kunit_skip((test), "Requires breakpoint slots: %d > %d", slots, \
19724c299cSMarco Elver get_test_bp_slots()); \
20724c299cSMarco Elver } \
21724c299cSMarco Elver } while (0)
22724c299cSMarco Elver
23724c299cSMarco Elver #define TEST_EXPECT_NOSPC(expr) KUNIT_EXPECT_EQ(test, -ENOSPC, PTR_ERR(expr))
24724c299cSMarco Elver
25724c299cSMarco Elver #define MAX_TEST_BREAKPOINTS 512
26724c299cSMarco Elver
27724c299cSMarco Elver static char break_vars[MAX_TEST_BREAKPOINTS];
28724c299cSMarco Elver static struct perf_event *test_bps[MAX_TEST_BREAKPOINTS];
29724c299cSMarco Elver static struct task_struct *__other_task;
30724c299cSMarco Elver
register_test_bp(int cpu,struct task_struct * tsk,int idx)31724c299cSMarco Elver static struct perf_event *register_test_bp(int cpu, struct task_struct *tsk, int idx)
32724c299cSMarco Elver {
33724c299cSMarco Elver struct perf_event_attr attr = {};
34724c299cSMarco Elver
35724c299cSMarco Elver if (WARN_ON(idx < 0 || idx >= MAX_TEST_BREAKPOINTS))
36724c299cSMarco Elver return NULL;
37724c299cSMarco Elver
38724c299cSMarco Elver hw_breakpoint_init(&attr);
39724c299cSMarco Elver attr.bp_addr = (unsigned long)&break_vars[idx];
40724c299cSMarco Elver attr.bp_len = HW_BREAKPOINT_LEN_1;
41724c299cSMarco Elver attr.bp_type = HW_BREAKPOINT_RW;
42724c299cSMarco Elver return perf_event_create_kernel_counter(&attr, cpu, tsk, NULL, NULL);
43724c299cSMarco Elver }
44724c299cSMarco Elver
unregister_test_bp(struct perf_event ** bp)45724c299cSMarco Elver static void unregister_test_bp(struct perf_event **bp)
46724c299cSMarco Elver {
47724c299cSMarco Elver if (WARN_ON(IS_ERR(*bp)))
48724c299cSMarco Elver return;
49724c299cSMarco Elver if (WARN_ON(!*bp))
50724c299cSMarco Elver return;
51724c299cSMarco Elver unregister_hw_breakpoint(*bp);
52724c299cSMarco Elver *bp = NULL;
53724c299cSMarco Elver }
54724c299cSMarco Elver
get_test_bp_slots(void)55724c299cSMarco Elver static int get_test_bp_slots(void)
56724c299cSMarco Elver {
57724c299cSMarco Elver static int slots;
58724c299cSMarco Elver
59724c299cSMarco Elver if (!slots)
60724c299cSMarco Elver slots = hw_breakpoint_slots(TYPE_DATA);
61724c299cSMarco Elver
62724c299cSMarco Elver return slots;
63724c299cSMarco Elver }
64724c299cSMarco Elver
fill_one_bp_slot(struct kunit * test,int * id,int cpu,struct task_struct * tsk)65724c299cSMarco Elver static void fill_one_bp_slot(struct kunit *test, int *id, int cpu, struct task_struct *tsk)
66724c299cSMarco Elver {
67724c299cSMarco Elver struct perf_event *bp = register_test_bp(cpu, tsk, *id);
68724c299cSMarco Elver
69724c299cSMarco Elver KUNIT_ASSERT_NOT_NULL(test, bp);
70724c299cSMarco Elver KUNIT_ASSERT_FALSE(test, IS_ERR(bp));
71724c299cSMarco Elver KUNIT_ASSERT_NULL(test, test_bps[*id]);
72724c299cSMarco Elver test_bps[(*id)++] = bp;
73724c299cSMarco Elver }
74724c299cSMarco Elver
75724c299cSMarco Elver /*
76724c299cSMarco Elver * Fills up the given @cpu/@tsk with breakpoints, only leaving @skip slots free.
77724c299cSMarco Elver *
78724c299cSMarco Elver * Returns true if this can be called again, continuing at @id.
79724c299cSMarco Elver */
fill_bp_slots(struct kunit * test,int * id,int cpu,struct task_struct * tsk,int skip)80724c299cSMarco Elver static bool fill_bp_slots(struct kunit *test, int *id, int cpu, struct task_struct *tsk, int skip)
81724c299cSMarco Elver {
82724c299cSMarco Elver for (int i = 0; i < get_test_bp_slots() - skip; ++i)
83724c299cSMarco Elver fill_one_bp_slot(test, id, cpu, tsk);
84724c299cSMarco Elver
85724c299cSMarco Elver return *id + get_test_bp_slots() <= MAX_TEST_BREAKPOINTS;
86724c299cSMarco Elver }
87724c299cSMarco Elver
dummy_kthread(void * arg)88724c299cSMarco Elver static int dummy_kthread(void *arg)
89724c299cSMarco Elver {
90724c299cSMarco Elver return 0;
91724c299cSMarco Elver }
92724c299cSMarco Elver
get_other_task(struct kunit * test)93724c299cSMarco Elver static struct task_struct *get_other_task(struct kunit *test)
94724c299cSMarco Elver {
95724c299cSMarco Elver struct task_struct *tsk;
96724c299cSMarco Elver
97724c299cSMarco Elver if (__other_task)
98724c299cSMarco Elver return __other_task;
99724c299cSMarco Elver
100724c299cSMarco Elver tsk = kthread_create(dummy_kthread, NULL, "hw_breakpoint_dummy_task");
101724c299cSMarco Elver KUNIT_ASSERT_FALSE(test, IS_ERR(tsk));
102724c299cSMarco Elver __other_task = tsk;
103724c299cSMarco Elver return __other_task;
104724c299cSMarco Elver }
105724c299cSMarco Elver
get_test_cpu(int num)106724c299cSMarco Elver static int get_test_cpu(int num)
107724c299cSMarco Elver {
108724c299cSMarco Elver int cpu;
109724c299cSMarco Elver
110724c299cSMarco Elver WARN_ON(num < 0);
111724c299cSMarco Elver
112724c299cSMarco Elver for_each_online_cpu(cpu) {
113724c299cSMarco Elver if (num-- <= 0)
114724c299cSMarco Elver break;
115724c299cSMarco Elver }
116724c299cSMarco Elver
117724c299cSMarco Elver return cpu;
118724c299cSMarco Elver }
119724c299cSMarco Elver
120724c299cSMarco Elver /* ===== Test cases ===== */
121724c299cSMarco Elver
test_one_cpu(struct kunit * test)122724c299cSMarco Elver static void test_one_cpu(struct kunit *test)
123724c299cSMarco Elver {
124724c299cSMarco Elver int idx = 0;
125724c299cSMarco Elver
126724c299cSMarco Elver fill_bp_slots(test, &idx, get_test_cpu(0), NULL, 0);
127724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx));
128724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), NULL, idx));
129724c299cSMarco Elver }
130724c299cSMarco Elver
test_many_cpus(struct kunit * test)131724c299cSMarco Elver static void test_many_cpus(struct kunit *test)
132724c299cSMarco Elver {
133724c299cSMarco Elver int idx = 0;
134724c299cSMarco Elver int cpu;
135724c299cSMarco Elver
136724c299cSMarco Elver /* Test that CPUs are independent. */
137724c299cSMarco Elver for_each_online_cpu(cpu) {
138724c299cSMarco Elver bool do_continue = fill_bp_slots(test, &idx, cpu, NULL, 0);
139724c299cSMarco Elver
140724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(cpu, NULL, idx));
141724c299cSMarco Elver if (!do_continue)
142724c299cSMarco Elver break;
143724c299cSMarco Elver }
144724c299cSMarco Elver }
145724c299cSMarco Elver
test_one_task_on_all_cpus(struct kunit * test)146724c299cSMarco Elver static void test_one_task_on_all_cpus(struct kunit *test)
147724c299cSMarco Elver {
148724c299cSMarco Elver int idx = 0;
149724c299cSMarco Elver
150724c299cSMarco Elver fill_bp_slots(test, &idx, -1, current, 0);
151724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx));
152724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), current, idx));
153724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), NULL, idx));
154724c299cSMarco Elver /* Remove one and adding back CPU-target should work. */
155724c299cSMarco Elver unregister_test_bp(&test_bps[0]);
156724c299cSMarco Elver fill_one_bp_slot(test, &idx, get_test_cpu(0), NULL);
157724c299cSMarco Elver }
158724c299cSMarco Elver
test_two_tasks_on_all_cpus(struct kunit * test)159724c299cSMarco Elver static void test_two_tasks_on_all_cpus(struct kunit *test)
160724c299cSMarco Elver {
161724c299cSMarco Elver int idx = 0;
162724c299cSMarco Elver
163724c299cSMarco Elver /* Test that tasks are independent. */
164724c299cSMarco Elver fill_bp_slots(test, &idx, -1, current, 0);
165724c299cSMarco Elver fill_bp_slots(test, &idx, -1, get_other_task(test), 0);
166724c299cSMarco Elver
167724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx));
168724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(-1, get_other_task(test), idx));
169724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), current, idx));
170724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), get_other_task(test), idx));
171724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), NULL, idx));
172724c299cSMarco Elver /* Remove one from first task and adding back CPU-target should not work. */
173724c299cSMarco Elver unregister_test_bp(&test_bps[0]);
174724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), NULL, idx));
175724c299cSMarco Elver }
176724c299cSMarco Elver
test_one_task_on_one_cpu(struct kunit * test)177724c299cSMarco Elver static void test_one_task_on_one_cpu(struct kunit *test)
178724c299cSMarco Elver {
179724c299cSMarco Elver int idx = 0;
180724c299cSMarco Elver
181724c299cSMarco Elver fill_bp_slots(test, &idx, get_test_cpu(0), current, 0);
182724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx));
183724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), current, idx));
184724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), NULL, idx));
185724c299cSMarco Elver /*
186724c299cSMarco Elver * Remove one and adding back CPU-target should work; this case is
187724c299cSMarco Elver * special vs. above because the task's constraints are CPU-dependent.
188724c299cSMarco Elver */
189724c299cSMarco Elver unregister_test_bp(&test_bps[0]);
190724c299cSMarco Elver fill_one_bp_slot(test, &idx, get_test_cpu(0), NULL);
191724c299cSMarco Elver }
192724c299cSMarco Elver
test_one_task_mixed(struct kunit * test)193724c299cSMarco Elver static void test_one_task_mixed(struct kunit *test)
194724c299cSMarco Elver {
195724c299cSMarco Elver int idx = 0;
196724c299cSMarco Elver
197724c299cSMarco Elver TEST_REQUIRES_BP_SLOTS(test, 3);
198724c299cSMarco Elver
199724c299cSMarco Elver fill_one_bp_slot(test, &idx, get_test_cpu(0), current);
200724c299cSMarco Elver fill_bp_slots(test, &idx, -1, current, 1);
201724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx));
202724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), current, idx));
203724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), NULL, idx));
204724c299cSMarco Elver
205724c299cSMarco Elver /* Transition from CPU-dependent pinned count to CPU-independent. */
206724c299cSMarco Elver unregister_test_bp(&test_bps[0]);
207724c299cSMarco Elver unregister_test_bp(&test_bps[1]);
208724c299cSMarco Elver fill_one_bp_slot(test, &idx, get_test_cpu(0), NULL);
209724c299cSMarco Elver fill_one_bp_slot(test, &idx, get_test_cpu(0), NULL);
210724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), NULL, idx));
211724c299cSMarco Elver }
212724c299cSMarco Elver
test_two_tasks_on_one_cpu(struct kunit * test)213724c299cSMarco Elver static void test_two_tasks_on_one_cpu(struct kunit *test)
214724c299cSMarco Elver {
215724c299cSMarco Elver int idx = 0;
216724c299cSMarco Elver
217724c299cSMarco Elver fill_bp_slots(test, &idx, get_test_cpu(0), current, 0);
218724c299cSMarco Elver fill_bp_slots(test, &idx, get_test_cpu(0), get_other_task(test), 0);
219724c299cSMarco Elver
220724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx));
221724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(-1, get_other_task(test), idx));
222724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), current, idx));
223724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), get_other_task(test), idx));
224724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), NULL, idx));
225724c299cSMarco Elver /* Can still create breakpoints on some other CPU. */
226724c299cSMarco Elver fill_bp_slots(test, &idx, get_test_cpu(1), NULL, 0);
227724c299cSMarco Elver }
228724c299cSMarco Elver
test_two_tasks_on_one_all_cpus(struct kunit * test)229724c299cSMarco Elver static void test_two_tasks_on_one_all_cpus(struct kunit *test)
230724c299cSMarco Elver {
231724c299cSMarco Elver int idx = 0;
232724c299cSMarco Elver
233724c299cSMarco Elver fill_bp_slots(test, &idx, get_test_cpu(0), current, 0);
234724c299cSMarco Elver fill_bp_slots(test, &idx, -1, get_other_task(test), 0);
235724c299cSMarco Elver
236724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx));
237724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(-1, get_other_task(test), idx));
238724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), current, idx));
239724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), get_other_task(test), idx));
240724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), NULL, idx));
241724c299cSMarco Elver /* Cannot create breakpoints on some other CPU either. */
242724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(1), NULL, idx));
243724c299cSMarco Elver }
244724c299cSMarco Elver
test_task_on_all_and_one_cpu(struct kunit * test)245724c299cSMarco Elver static void test_task_on_all_and_one_cpu(struct kunit *test)
246724c299cSMarco Elver {
247724c299cSMarco Elver int tsk_on_cpu_idx, cpu_idx;
248724c299cSMarco Elver int idx = 0;
249724c299cSMarco Elver
250724c299cSMarco Elver TEST_REQUIRES_BP_SLOTS(test, 3);
251724c299cSMarco Elver
252724c299cSMarco Elver fill_bp_slots(test, &idx, -1, current, 2);
253724c299cSMarco Elver /* Transitioning from only all CPU breakpoints to mixed. */
254724c299cSMarco Elver tsk_on_cpu_idx = idx;
255724c299cSMarco Elver fill_one_bp_slot(test, &idx, get_test_cpu(0), current);
256724c299cSMarco Elver fill_one_bp_slot(test, &idx, -1, current);
257724c299cSMarco Elver
258724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx));
259724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), current, idx));
260724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), NULL, idx));
261724c299cSMarco Elver
262724c299cSMarco Elver /* We should still be able to use up another CPU's slots. */
263724c299cSMarco Elver cpu_idx = idx;
264724c299cSMarco Elver fill_one_bp_slot(test, &idx, get_test_cpu(1), NULL);
265724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(1), NULL, idx));
266724c299cSMarco Elver
267724c299cSMarco Elver /* Transitioning back to task target on all CPUs. */
268724c299cSMarco Elver unregister_test_bp(&test_bps[tsk_on_cpu_idx]);
269724c299cSMarco Elver /* Still have a CPU target breakpoint in get_test_cpu(1). */
270724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx));
271724c299cSMarco Elver /* Remove it and try again. */
272724c299cSMarco Elver unregister_test_bp(&test_bps[cpu_idx]);
273724c299cSMarco Elver fill_one_bp_slot(test, &idx, -1, current);
274724c299cSMarco Elver
275724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx));
276724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), current, idx));
277724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), NULL, idx));
278724c299cSMarco Elver TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(1), NULL, idx));
279724c299cSMarco Elver }
280724c299cSMarco Elver
281724c299cSMarco Elver static struct kunit_case hw_breakpoint_test_cases[] = {
282724c299cSMarco Elver KUNIT_CASE(test_one_cpu),
283724c299cSMarco Elver KUNIT_CASE(test_many_cpus),
284724c299cSMarco Elver KUNIT_CASE(test_one_task_on_all_cpus),
285724c299cSMarco Elver KUNIT_CASE(test_two_tasks_on_all_cpus),
286724c299cSMarco Elver KUNIT_CASE(test_one_task_on_one_cpu),
287724c299cSMarco Elver KUNIT_CASE(test_one_task_mixed),
288724c299cSMarco Elver KUNIT_CASE(test_two_tasks_on_one_cpu),
289724c299cSMarco Elver KUNIT_CASE(test_two_tasks_on_one_all_cpus),
290724c299cSMarco Elver KUNIT_CASE(test_task_on_all_and_one_cpu),
291724c299cSMarco Elver {},
292724c299cSMarco Elver };
293724c299cSMarco Elver
test_init(struct kunit * test)294724c299cSMarco Elver static int test_init(struct kunit *test)
295724c299cSMarco Elver {
296724c299cSMarco Elver /* Most test cases want 2 distinct CPUs. */
297c5b81449SMarco Elver if (num_online_cpus() < 2)
298*4b18cb3fSDavid Gow kunit_skip(test, "not enough cpus");
299c5b81449SMarco Elver
300c5b81449SMarco Elver /* Want the system to not use breakpoints elsewhere. */
301c5b81449SMarco Elver if (hw_breakpoint_is_used())
302*4b18cb3fSDavid Gow kunit_skip(test, "hw breakpoint already in use");
303c5b81449SMarco Elver
304c5b81449SMarco Elver return 0;
305724c299cSMarco Elver }
306724c299cSMarco Elver
test_exit(struct kunit * test)307724c299cSMarco Elver static void test_exit(struct kunit *test)
308724c299cSMarco Elver {
309724c299cSMarco Elver for (int i = 0; i < MAX_TEST_BREAKPOINTS; ++i) {
310724c299cSMarco Elver if (test_bps[i])
311724c299cSMarco Elver unregister_test_bp(&test_bps[i]);
312724c299cSMarco Elver }
313724c299cSMarco Elver
314724c299cSMarco Elver if (__other_task) {
315724c299cSMarco Elver kthread_stop(__other_task);
316724c299cSMarco Elver __other_task = NULL;
317724c299cSMarco Elver }
318c5b81449SMarco Elver
319c5b81449SMarco Elver /* Verify that internal state agrees that no breakpoints are in use. */
320c5b81449SMarco Elver KUNIT_EXPECT_FALSE(test, hw_breakpoint_is_used());
321724c299cSMarco Elver }
322724c299cSMarco Elver
323724c299cSMarco Elver static struct kunit_suite hw_breakpoint_test_suite = {
324724c299cSMarco Elver .name = "hw_breakpoint",
325724c299cSMarco Elver .test_cases = hw_breakpoint_test_cases,
326724c299cSMarco Elver .init = test_init,
327724c299cSMarco Elver .exit = test_exit,
328724c299cSMarco Elver };
329724c299cSMarco Elver
330724c299cSMarco Elver kunit_test_suites(&hw_breakpoint_test_suite);
331724c299cSMarco Elver
332724c299cSMarco Elver MODULE_AUTHOR("Marco Elver <elver@google.com>");
333