xref: /linux/tools/testing/selftests/powerpc/pmu/ebb/lost_exception_test.c (revision 71ec70ea26e399eb8371fe9d2d5bf5dab09570fb)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2014, Michael Ellerman, IBM Corp.
4  */
5 
6 #include <sched.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/mman.h>
11 #include <time.h>
12 
13 #include "ebb.h"
14 
15 
16 /*
17  * Test that tries to trigger CPU_FTR_PMAO_BUG. Which is a hardware defect
18  * where an exception triggers but we context switch before it is delivered and
19  * lose the exception.
20  */
21 
22 static int test_body(void)
23 {
24 	int i, orig_period, max_period;
25 	struct event event;
26 	struct timespec ts = { .tv_sec = 0, .tv_nsec = 1 };
27 
28 	SKIP_IF(!ebb_is_supported());
29 
30 	/* We use PMC4 to make sure the kernel switches all counters correctly */
31 	event_init_named(&event, 0x40002, "instructions");
32 	event_leader_ebb_init(&event);
33 
34 	event.attr.exclude_kernel = 1;
35 	event.attr.exclude_hv = 1;
36 	event.attr.exclude_idle = 1;
37 
38 	FAIL_IF(event_open(&event));
39 
40 	ebb_enable_pmc_counting(4);
41 	setup_ebb_handler(standard_ebb_callee);
42 	ebb_global_enable();
43 	FAIL_IF(ebb_event_enable(&event));
44 
45 	/*
46 	 * We want a low sample period, but we also want to get out of the EBB
47 	 * handler without tripping up again.
48 	 *
49 	 * This value picked after much experimentation.
50 	 */
51 	orig_period = max_period = sample_period = 400;
52 
53 	mtspr(SPRN_PMC4, pmc_sample_period(sample_period));
54 
55 	while (ebb_state.stats.ebb_count < 1000000) {
56 		/*
57 		 * We are trying to get the EBB exception to race exactly with
58 		 * us entering the kernel to do the syscall. We then need the
59 		 * kernel to decide our timeslice is up and context switch to
60 		 * the other thread. When we come back our EBB will have been
61 		 * lost and we'll spin in this while loop forever.
62 		 *
63 		 * Use nanosleep(0) instead of sched_yield() to guarantee a
64 		 * context switch to the eat_cpu child regardless of the
65 		 * eligibility state. sched_yield() via yield_task_fair() may
66 		 * become a no-op when the task is ineligible (vruntime ahead
67 		 * of avg_vruntime), preventing the required context switch.
68 		 */
69 		for (i = 0; i < 100000; i++)
70 			nanosleep(&ts, NULL);
71 
72 		/* Change the sample period slightly to try and hit the race */
73 		if (sample_period >= (orig_period + 200))
74 			sample_period = orig_period;
75 		else
76 			sample_period++;
77 
78 		if (sample_period > max_period)
79 			max_period = sample_period;
80 	}
81 
82 	ebb_freeze_pmcs();
83 	ebb_global_disable();
84 
85 	mtspr(SPRN_PMC4, 0xdead);
86 
87 	dump_summary_ebb_state();
88 	dump_ebb_hw_state();
89 
90 	event_close(&event);
91 
92 	FAIL_IF(ebb_state.stats.ebb_count == 0);
93 
94 	/* We vary our sample period so we need extra fudge here */
95 	FAIL_IF(!ebb_check_count(4, orig_period, 2 * (max_period - orig_period)));
96 
97 	return 0;
98 }
99 
100 static int lost_exception(void)
101 {
102 	return eat_cpu(test_body);
103 }
104 
105 int main(void)
106 {
107 	test_harness_set_timeout(300);
108 	return test_harness(lost_exception, "lost_exception");
109 }
110