Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F177752089
D61769.1795200951.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None
D61769.1795200951.diff
View Options
diff --git a/sys/dev/hwpmc/hwpmc_mod.c b/sys/dev/hwpmc/hwpmc_mod.c
--- a/sys/dev/hwpmc/hwpmc_mod.c
+++ b/sys/dev/hwpmc/hwpmc_mod.c
@@ -1171,6 +1171,43 @@
* this process, remove the process structure from its hash table. If
* 'flags' contains PMC_FLAG_REMOVE, then free the process structure.
*/
+/*
+ * Move to each CPU where a thread of 'p' runs. The scheduler then
+ * switches that thread out, and the switch-out removes its PMCs from
+ * the CPU. A detached PMC can otherwise stay on the row of a remote
+ * CPU after a new PMC takes the row of 'p'.
+ */
+static void
+pmc_switch_out_running_threads(struct proc *p)
+{
+ struct pmc_binding pb;
+ struct thread *td;
+ cpuset_t cpus;
+ int cpu;
+
+ sx_assert(&pmc_sx, SX_XLOCKED);
+ CPU_ZERO(&cpus);
+ PROC_LOCK(p);
+ FOREACH_THREAD_IN_PROC(p, td) {
+ if (td == curthread)
+ continue;
+ thread_lock(td);
+ if (TD_IS_RUNNING(td))
+ CPU_SET(td->td_oncpu, &cpus);
+ thread_unlock(td);
+ }
+ PROC_UNLOCK(p);
+ if (CPU_EMPTY(&cpus))
+ return;
+
+ pmc_save_cpu_binding(&pb);
+ CPU_FOREACH_ISSET(cpu, &cpus) {
+ if (pmc_cpu_is_active(cpu))
+ pmc_select_cpu(cpu);
+ }
+ pmc_restore_cpu_binding(&pb);
+}
+
static int
pmc_detach_one_process(struct proc *p, struct pmc *pm, int flags)
{
@@ -1210,6 +1247,10 @@
pmc_unlink_target_process(pm, pp);
+ /* Remove the PMC from the CPUs where 'p' still runs. */
+ if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm)))
+ pmc_switch_out_running_threads(p);
+
/* Issue a detach entry if a log file is configured */
if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE)
pmclog_process_pmcdetach(pm, p->p_pid);
diff --git a/tests/sys/pmc/pmc_detach_test.c b/tests/sys/pmc/pmc_detach_test.c
--- a/tests/sys/pmc/pmc_detach_test.c
+++ b/tests/sys/pmc/pmc_detach_test.c
@@ -47,10 +47,14 @@
*/
#include <sys/types.h>
+#include <sys/cpuset.h>
+#include <sys/wait.h>
#include <errno.h>
#include <pmc.h>
#include <pthread.h>
+#include <signal.h>
+#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
@@ -68,6 +72,7 @@
NULL
};
+static const char *counting_event; /* set by alloc_counting_pmc() */
static volatile int spin_stop;
static void *
@@ -103,8 +108,10 @@
for (i = 0; counting_events[i] != NULL; i++) {
id = PMC_ID_INVALID;
if (pmc_allocate(counting_events[i], PMC_MODE_TC, 0,
- PMC_CPU_ANY, &id, 0) == 0)
+ PMC_CPU_ANY, &id, 0) == 0) {
+ counting_event = counting_events[i];
return (id);
+ }
}
atf_tc_skip("no allocatable process-mode counting event "
"(no vPMU?)");
@@ -169,10 +176,153 @@
pthread_join(th[i], NULL);
}
+/* Return a CPU this process may use other than 'other', or skip. */
+static int
+pick_cpu(int other)
+{
+ cpuset_t set;
+ int cpu;
+
+ ATF_REQUIRE(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
+ sizeof(set), &set) == 0);
+ for (cpu = CPU_SETSIZE - 1; cpu >= 0; cpu--) {
+ if (CPU_ISSET(cpu, &set) && cpu != other)
+ return (cpu);
+ }
+ atf_tc_skip("needs two CPUs");
+}
+
+/* Return 0 or the error number. */
+static int
+set_cpu(pid_t pid, int cpu)
+{
+ cpuset_t set;
+
+ CPU_ZERO(&set);
+ CPU_SET(cpu, &set);
+ if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, pid,
+ sizeof(set), &set) != 0)
+ return (errno);
+ return (0);
+}
+
+/*
+ * Owner of PMC B. Allocate B with the event of A, attach it to 'target'
+ * and start it. Send the row and the error, then hold B until the
+ * parent closes 'hold'.
+ */
+static void
+run_second_owner(pid_t target, int result, int hold, int unused[2])
+{
+ int reply[2];
+ pmc_id_t b;
+ char c;
+
+ /* Close the parent ends: a read of 'hold' then sees EOF. */
+ (void)close(unused[0]);
+ (void)close(unused[1]);
+ reply[0] = reply[1] = 0;
+ if (pmc_allocate(counting_event, PMC_MODE_TC, 0, PMC_CPU_ANY, &b,
+ 0) != 0)
+ reply[1] = errno;
+ else {
+ reply[0] = PMC_ID_TO_ROWINDEX(b);
+ if (pmc_attach(b, target) != 0 || pmc_start(b) != 0)
+ reply[1] = errno;
+ }
+ (void)write(result, reply, sizeof(reply));
+ (void)read(hold, &c, 1);
+ _exit(0);
+}
+
+ATF_TC(detach_running_target_frees_row);
+ATF_TC_HEAD(detach_running_target_frees_row, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Detaching a PMC from a target that runs on another CPU removes "
+ "the PMC from that CPU before another PMC takes its row");
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(detach_running_target_frees_row, tc)
+{
+ pmc_id_t a;
+ pid_t other, owner, target;
+ int cpu, error, hold[2], parent_ends[2], reply[2], result[2], status;
+
+ a = alloc_counting_pmc();
+ cpu = pick_cpu(-1);
+ ATF_REQUIRE(set_cpu(getpid(), pick_cpu(cpu)) == 0);
+ ATF_REQUIRE(pipe(hold) == 0 && pipe(result) == 0);
+ reply[0] = reply[1] = 0;
+ owner = -1;
+ /* A second target keeps A attached after the detach. */
+ ATF_REQUIRE((other = fork()) >= 0);
+ if (other == 0) {
+ for (;;)
+ (void)pause();
+ }
+ ATF_REQUIRE((target = fork()) >= 0);
+ if (target == 0) {
+ volatile uint64_t sink = 0;
+
+ /* Spin, and never block. */
+ for (;;)
+ sink++;
+ }
+
+ /* Kill the children before any check can stop the test. */
+ error = set_cpu(target, cpu);
+ if (error == 0) {
+ /* Let the target reach its CPU and run there. */
+ (void)usleep(200000);
+ if (pmc_attach(a, other) != 0 || pmc_attach(a, target) != 0 ||
+ pmc_start(a) != 0)
+ error = errno;
+ }
+ if (error == 0) {
+ /* Let the target switch in with A. */
+ (void)usleep(100000);
+ if (pmc_detach(a, target) != 0)
+ error = errno;
+ }
+ parent_ends[0] = result[0];
+ parent_ends[1] = hold[1];
+ if (error == 0 && (owner = fork()) == 0)
+ run_second_owner(target, result[1], hold[0], parent_ends);
+ if (owner < 0 && error == 0)
+ error = errno;
+ /* A dead owner then gives EOF. */
+ (void)close(result[1]);
+ if (owner > 0 && read(result[0], reply, sizeof(reply)) !=
+ sizeof(reply))
+ error = EIO;
+ if (error == 0)
+ (void)usleep(100000);
+ /*
+ * Without the fix, A stays on the row of the target CPU. The exit
+ * of the target then finds A where it expects B.
+ */
+ (void)kill(target, SIGKILL);
+ (void)kill(other, SIGKILL);
+ (void)close(hold[1]);
+ ATF_REQUIRE(waitpid(target, &status, 0) == target);
+ ATF_REQUIRE(waitpid(other, &status, 0) == other);
+ if (owner > 0)
+ ATF_REQUIRE(waitpid(owner, &status, 0) == owner);
+ ATF_REQUIRE_MSG(error == 0, "setup: %s", strerror(error));
+ ATF_REQUIRE_MSG(reply[1] == 0, "PMC B: %s", strerror(reply[1]));
+ (void)pmc_stop(a);
+ ATF_CHECK(pmc_release(a) == 0);
+ if (reply[0] != PMC_ID_TO_ROWINDEX(a))
+ atf_tc_skip("PMC B used row %d, not row %d", reply[0],
+ PMC_ID_TO_ROWINDEX(a));
+}
+
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, detach_live_self);
ATF_TP_ADD_TC(tp, detach_live_multithread);
+ ATF_TP_ADD_TC(tp, detach_running_target_frees_row);
return (atf_no_error());
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Nov 20, 6:55 PM (16 h, 42 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41873950
Default Alt Text
D61769.1795200951.diff (6 KB)
Attached To
Mode
D61769: hwpmc: remove a detached PMC from CPUs where the target runs
Attached
Detach File
Event Timeline