1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2026 Alexander Leidinger <netchild@FreeBSD.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other advertising materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * Regression tests for detaching a process-mode PMC from a target that
31 * still has it loaded on hardware. The runcount reference for a live
32 * PMC is dropped only by the context-switch-out reclaim, which the
33 * scheduler gates on P_HWPMC; detaching used to clear P_HWPMC without
34 * draining the reference, so the following release spun forever in
35 * pmc_wait_for_pmc_idle() - a panic ("waiting too long for pmc to be
36 * free") on an INVARIANTS kernel, an unkillable loop holding the hwpmc
37 * lock otherwise.
38 *
39 * The failure is deterministic: attaching a counting PMC to the current
40 * (running) thread and detaching it before release always left the
41 * reference live. On a kernel that carries the fix these tests complete
42 * immediately; run against a kernel that lacks it they would wedge, so
43 * (like any hwpmc regression test) they ship alongside the fix.
44 *
45 * They need an allocatable process-mode counting event, and skip where
46 * none is available (hwpmc(4) not loaded, or a VM without a vPMU).
47 */
48
49 #include <sys/types.h>
50
51 #include <errno.h>
52 #include <pmc.h>
53 #include <pthread.h>
54 #include <stdio.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 #include <atf-c.h>
59
60 static const char *counting_events[] = {
61 "instructions",
62 "cycles",
63 "branches",
64 "unhalted-core-cycles",
65 "inst_retired.any",
66 "cpu_clk_unhalted.thread",
67 "ls_not_halted_cyc",
68 NULL
69 };
70
71 static volatile int spin_stop;
72
73 static void *
spinner(void * arg __unused)74 spinner(void *arg __unused)
75 {
76 volatile unsigned long s = 0;
77
78 while (spin_stop == 0)
79 s += 1;
80 return (NULL);
81 }
82
83 static void
burn_cpu(void)84 burn_cpu(void)
85 {
86 volatile unsigned long s = 0;
87 int i;
88
89 for (i = 0; i < 1000000; i++)
90 s += i;
91 }
92
93 /* Allocate the first available process-mode counting PMC, or skip. */
94 static pmc_id_t
alloc_counting_pmc(void)95 alloc_counting_pmc(void)
96 {
97 pmc_id_t id;
98 int i;
99
100 if (pmc_init() != 0)
101 atf_tc_skip("hwpmc(4) is not available: %s", strerror(errno));
102
103 for (i = 0; counting_events[i] != NULL; i++) {
104 id = PMC_ID_INVALID;
105 if (pmc_allocate(counting_events[i], PMC_MODE_TC, 0,
106 PMC_CPU_ANY, &id, 0) == 0)
107 return (id);
108 }
109 atf_tc_skip("no allocatable process-mode counting event "
110 "(no vPMU?)");
111 return (PMC_ID_INVALID); /* not reached */
112 }
113
114 ATF_TC(detach_live_self);
ATF_TC_HEAD(detach_live_self,tc)115 ATF_TC_HEAD(detach_live_self, tc)
116 {
117 atf_tc_set_md_var(tc, "descr",
118 "Detaching a running counting PMC from the current thread and "
119 "then releasing it does not leak its runcount reference");
120 atf_tc_set_md_var(tc, "require.user", "root");
121 }
ATF_TC_BODY(detach_live_self,tc)122 ATF_TC_BODY(detach_live_self, tc)
123 {
124 pmc_id_t id;
125
126 id = alloc_counting_pmc();
127
128 ATF_REQUIRE(pmc_attach(id, 0) == 0); /* 0 == current process */
129 ATF_REQUIRE(pmc_start(id) == 0);
130 burn_cpu(); /* make the counter live */
131
132 /* Detach while still loaded on this CPU, then release. */
133 ATF_REQUIRE(pmc_detach(id, 0) == 0);
134 ATF_REQUIRE(pmc_release(id) == 0); /* wedged before the fix */
135 }
136
137 ATF_TC(detach_live_multithread);
ATF_TC_HEAD(detach_live_multithread,tc)138 ATF_TC_HEAD(detach_live_multithread, tc)
139 {
140 atf_tc_set_md_var(tc, "descr",
141 "Detaching a running process-scope counting PMC drains the "
142 "references held by the process' other threads");
143 atf_tc_set_md_var(tc, "require.user", "root");
144 }
ATF_TC_BODY(detach_live_multithread,tc)145 ATF_TC_BODY(detach_live_multithread, tc)
146 {
147 pthread_t th[3];
148 pmc_id_t id;
149 int i, n;
150
151 id = alloc_counting_pmc();
152
153 /* Sibling threads keep the process-scope PMC live on other CPUs. */
154 spin_stop = 0;
155 for (n = 0; n < 3; n++)
156 ATF_REQUIRE(pthread_create(&th[n], NULL, spinner, NULL) == 0);
157
158 ATF_REQUIRE(pmc_attach(id, 0) == 0);
159 ATF_REQUIRE(pmc_start(id) == 0);
160 burn_cpu();
161 /* Let the siblings be scheduled and pick up the PMC. */
162 usleep(200 * 1000);
163
164 ATF_REQUIRE(pmc_detach(id, 0) == 0);
165 ATF_REQUIRE(pmc_release(id) == 0); /* must drain the siblings */
166
167 spin_stop = 1;
168 for (i = 0; i < n; i++)
169 pthread_join(th[i], NULL);
170 }
171
ATF_TP_ADD_TCS(tp)172 ATF_TP_ADD_TCS(tp)
173 {
174 ATF_TP_ADD_TC(tp, detach_live_self);
175 ATF_TP_ADD_TC(tp, detach_live_multithread);
176
177 return (atf_no_error());
178 }
179