Lines Matching +full:test +full:- +full:cpu

1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit test for hw_breakpoint constraints accounting logic.
8 #include <kunit/test.h>
15 #define TEST_REQUIRES_BP_SLOTS(test, slots) \ argument
18 kunit_skip((test), "Requires breakpoint slots: %d > %d", slots, \
23 #define TEST_EXPECT_NOSPC(expr) KUNIT_EXPECT_EQ(test, -ENOSPC, PTR_ERR(expr))
31 static struct perf_event *register_test_bp(int cpu, struct task_struct *tsk, int idx) in register_test_bp() argument
42 return perf_event_create_kernel_counter(&attr, cpu, tsk, NULL, NULL); in register_test_bp()
65 static void fill_one_bp_slot(struct kunit *test, int *id, int cpu, struct task_struct *tsk) in fill_one_bp_slot() argument
67 struct perf_event *bp = register_test_bp(cpu, tsk, *id); in fill_one_bp_slot()
69 KUNIT_ASSERT_NOT_NULL(test, bp); in fill_one_bp_slot()
70 KUNIT_ASSERT_FALSE(test, IS_ERR(bp)); in fill_one_bp_slot()
71 KUNIT_ASSERT_NULL(test, test_bps[*id]); in fill_one_bp_slot()
76 * Fills up the given @cpu/@tsk with breakpoints, only leaving @skip slots free.
80 static bool fill_bp_slots(struct kunit *test, int *id, int cpu, struct task_struct *tsk, int skip) in fill_bp_slots() argument
82 for (int i = 0; i < get_test_bp_slots() - skip; ++i) in fill_bp_slots()
83 fill_one_bp_slot(test, id, cpu, tsk); in fill_bp_slots()
93 static struct task_struct *get_other_task(struct kunit *test) in get_other_task() argument
101 KUNIT_ASSERT_FALSE(test, IS_ERR(tsk)); in get_other_task()
108 int cpu; in get_test_cpu() local
112 for_each_online_cpu(cpu) { in get_test_cpu()
113 if (num-- <= 0) in get_test_cpu()
117 return cpu; in get_test_cpu()
120 /* ===== Test cases ===== */
122 static void test_one_cpu(struct kunit *test) in test_one_cpu() argument
126 fill_bp_slots(test, &idx, get_test_cpu(0), NULL, 0); in test_one_cpu()
127 TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx)); in test_one_cpu()
131 static void test_many_cpus(struct kunit *test) in test_many_cpus() argument
134 int cpu; in test_many_cpus() local
136 /* Test that CPUs are independent. */ in test_many_cpus()
137 for_each_online_cpu(cpu) { in test_many_cpus()
138 bool do_continue = fill_bp_slots(test, &idx, cpu, NULL, 0); in test_many_cpus()
140 TEST_EXPECT_NOSPC(register_test_bp(cpu, NULL, idx)); in test_many_cpus()
146 static void test_one_task_on_all_cpus(struct kunit *test) in test_one_task_on_all_cpus() argument
150 fill_bp_slots(test, &idx, -1, current, 0); in test_one_task_on_all_cpus()
151 TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx)); in test_one_task_on_all_cpus()
154 /* Remove one and adding back CPU-target should work. */ in test_one_task_on_all_cpus()
156 fill_one_bp_slot(test, &idx, get_test_cpu(0), NULL); in test_one_task_on_all_cpus()
159 static void test_two_tasks_on_all_cpus(struct kunit *test) in test_two_tasks_on_all_cpus() argument
163 /* Test that tasks are independent. */ in test_two_tasks_on_all_cpus()
164 fill_bp_slots(test, &idx, -1, current, 0); in test_two_tasks_on_all_cpus()
165 fill_bp_slots(test, &idx, -1, get_other_task(test), 0); in test_two_tasks_on_all_cpus()
167 TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx)); in test_two_tasks_on_all_cpus()
168 TEST_EXPECT_NOSPC(register_test_bp(-1, get_other_task(test), idx)); in test_two_tasks_on_all_cpus()
170 TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), get_other_task(test), idx)); in test_two_tasks_on_all_cpus()
172 /* Remove one from first task and adding back CPU-target should not work. */ in test_two_tasks_on_all_cpus()
177 static void test_one_task_on_one_cpu(struct kunit *test) in test_one_task_on_one_cpu() argument
181 fill_bp_slots(test, &idx, get_test_cpu(0), current, 0); in test_one_task_on_one_cpu()
182 TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx)); in test_one_task_on_one_cpu()
186 * Remove one and adding back CPU-target should work; this case is in test_one_task_on_one_cpu()
187 * special vs. above because the task's constraints are CPU-dependent. in test_one_task_on_one_cpu()
190 fill_one_bp_slot(test, &idx, get_test_cpu(0), NULL); in test_one_task_on_one_cpu()
193 static void test_one_task_mixed(struct kunit *test) in test_one_task_mixed() argument
197 TEST_REQUIRES_BP_SLOTS(test, 3); in test_one_task_mixed()
199 fill_one_bp_slot(test, &idx, get_test_cpu(0), current); in test_one_task_mixed()
200 fill_bp_slots(test, &idx, -1, current, 1); in test_one_task_mixed()
201 TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx)); in test_one_task_mixed()
205 /* Transition from CPU-dependent pinned count to CPU-independent. */ in test_one_task_mixed()
208 fill_one_bp_slot(test, &idx, get_test_cpu(0), NULL); in test_one_task_mixed()
209 fill_one_bp_slot(test, &idx, get_test_cpu(0), NULL); in test_one_task_mixed()
213 static void test_two_tasks_on_one_cpu(struct kunit *test) in test_two_tasks_on_one_cpu() argument
217 fill_bp_slots(test, &idx, get_test_cpu(0), current, 0); in test_two_tasks_on_one_cpu()
218 fill_bp_slots(test, &idx, get_test_cpu(0), get_other_task(test), 0); in test_two_tasks_on_one_cpu()
220 TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx)); in test_two_tasks_on_one_cpu()
221 TEST_EXPECT_NOSPC(register_test_bp(-1, get_other_task(test), idx)); in test_two_tasks_on_one_cpu()
223 TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), get_other_task(test), idx)); in test_two_tasks_on_one_cpu()
225 /* Can still create breakpoints on some other CPU. */ in test_two_tasks_on_one_cpu()
226 fill_bp_slots(test, &idx, get_test_cpu(1), NULL, 0); in test_two_tasks_on_one_cpu()
229 static void test_two_tasks_on_one_all_cpus(struct kunit *test) in test_two_tasks_on_one_all_cpus() argument
233 fill_bp_slots(test, &idx, get_test_cpu(0), current, 0); in test_two_tasks_on_one_all_cpus()
234 fill_bp_slots(test, &idx, -1, get_other_task(test), 0); in test_two_tasks_on_one_all_cpus()
236 TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx)); in test_two_tasks_on_one_all_cpus()
237 TEST_EXPECT_NOSPC(register_test_bp(-1, get_other_task(test), idx)); in test_two_tasks_on_one_all_cpus()
239 TEST_EXPECT_NOSPC(register_test_bp(get_test_cpu(0), get_other_task(test), idx)); in test_two_tasks_on_one_all_cpus()
241 /* Cannot create breakpoints on some other CPU either. */ in test_two_tasks_on_one_all_cpus()
245 static void test_task_on_all_and_one_cpu(struct kunit *test) in test_task_on_all_and_one_cpu() argument
250 TEST_REQUIRES_BP_SLOTS(test, 3); in test_task_on_all_and_one_cpu()
252 fill_bp_slots(test, &idx, -1, current, 2); in test_task_on_all_and_one_cpu()
253 /* Transitioning from only all CPU breakpoints to mixed. */ in test_task_on_all_and_one_cpu()
255 fill_one_bp_slot(test, &idx, get_test_cpu(0), current); in test_task_on_all_and_one_cpu()
256 fill_one_bp_slot(test, &idx, -1, current); in test_task_on_all_and_one_cpu()
258 TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx)); in test_task_on_all_and_one_cpu()
262 /* We should still be able to use up another CPU's slots. */ in test_task_on_all_and_one_cpu()
264 fill_one_bp_slot(test, &idx, get_test_cpu(1), NULL); in test_task_on_all_and_one_cpu()
269 /* Still have a CPU target breakpoint in get_test_cpu(1). */ in test_task_on_all_and_one_cpu()
270 TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx)); in test_task_on_all_and_one_cpu()
273 fill_one_bp_slot(test, &idx, -1, current); in test_task_on_all_and_one_cpu()
275 TEST_EXPECT_NOSPC(register_test_bp(-1, current, idx)); in test_task_on_all_and_one_cpu()
294 static int test_init(struct kunit *test) in test_init() argument
296 /* Most test cases want 2 distinct CPUs. */ in test_init()
298 kunit_skip(test, "not enough cpus"); in test_init()
302 kunit_skip(test, "hw breakpoint already in use"); in test_init()
307 static void test_exit(struct kunit *test) in test_exit() argument
320 KUNIT_EXPECT_FALSE(test, hw_breakpoint_is_used()); in test_exit()