Page MenuHomeFreeBSD

D60549.1796133973.diff
No OneTemporary

Authored By
Unknown
Size
33 KB
Referenced Files
None
Subscribers
None

D60549.1796133973.diff

diff --git a/tests/sys/pmc/Makefile b/tests/sys/pmc/Makefile
--- a/tests/sys/pmc/Makefile
+++ b/tests/sys/pmc/Makefile
@@ -16,6 +16,7 @@
ATF_TESTS_SH+= pmc_cli_test
.if ${MACHINE_ARCH} == "amd64"
+ATF_TESTS_C+= pmc_amdiommu_test
ATF_TESTS_C+= pmc_exterr_test
ATF_TESTS_C+= pmc_mapfile_test
ATF_TESTS_C+= pmc_perf_test
@@ -28,6 +29,7 @@
.endif
TEST_METADATA.pmc_exterr_test+= required_user="root"
+TEST_METADATA.pmc_amdiommu_test+= is_exclusive="true"
TEST_METADATA.pmc_callchain_ur_test+= is_exclusive="true"
TEST_METADATA.pmc_cli_test+= is_exclusive="true"
TEST_METADATA.pmc_pidtrack_test+= is_exclusive="true"
@@ -39,6 +41,7 @@
TEST_METADATA.pmcstat_tsc_test+= required_kmods="hwpmc"
LIBADD+= pmc
+LIBADD.pmc_amdiommu_test+= cam
LIBADD.pmc_detach_test+= pthread
LIBADD.pmc_unload_test+= memstat
diff --git a/tests/sys/pmc/pmc_amdiommu_test.c b/tests/sys/pmc/pmc_amdiommu_test.c
new file mode 100644
--- /dev/null
+++ b/tests/sys/pmc/pmc_amdiommu_test.c
@@ -0,0 +1,1073 @@
+/*
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/*
+ * Tests for the AMD IOMMU performance counter PMC class, PMC_CLASS_AMDIOMMU
+ * (pmc.amdiommu(3)). The class exists only on amd64 kernels with options
+ * IOMMU, on machines whose IOMMU implements the performance counter
+ * extension (EFR[PCSup]) and whose IOMMU driver is enabled
+ * (hw.amdiommu.enable=1); elsewhere every test skips.
+ *
+ * The DMA tests read a raw CAM disk: the first one that qualifies in
+ * kern.disks, or in the space-separated amdiommu_disk configuration
+ * variable when it is set. They find the IOMMU unit and requester ID of
+ * the disk controller from its %iommu property, and skip unless the disk
+ * DMA goes through a context of that unit (dev.amdiommu.N.dma=1, set with
+ * hw.iommu.dma=1). With hw.busdma.default=bounce the context is
+ * identity-mapped, a device table entry without page tables (mode 0);
+ * the EPYC 9654 still counts its DMA as translated, with IOMMU TLB hits.
+ * Once the DMA goes through a context, a counter that does not count it
+ * fails the test. Select the disks as in
+ *
+ * kyua -v 'test_suites.FreeBSD.amdiommu_disk=nda1 da0' test \
+ * pmc_amdiommu_test
+ *
+ * dev.amdiommu.N.dma=1 does not prove that the controller got a context;
+ * if creating it failed, the DMA tests fail. The requester ID comes from
+ * %iommu, which is the PCI requester; if an IVHD alias entry makes the
+ * IOMMU see another ID, iommu_pmc_devid_filter fails. Whether the context
+ * is identity-mapped is read from the busdma tunables of the controller,
+ * which may differ from its requester behind a PCIe-to-PCI bridge.
+ *
+ * A kernel that mishandles counter ownership or row indexes may panic in
+ * iommu_pmc_multicpu_alloc, iommu_pmc_non_sc_rejected or
+ * iommu_pmc_kill_running_owner (KASSERTs with INVARIANTS) rather than
+ * fail them.
+ *
+ * The counters count system-wide (PMC_MODE_SC) and allocating them needs
+ * the PRIV_PMC_SYSTEM privilege. Tests that need free counters run
+ * exclusively. Units whose counter banks firmware has locked have no
+ * usable counters and are passed over.
+ */
+
+#include <sys/param.h>
+#include <sys/ioctl.h>
+#include <sys/module.h>
+#include <sys/pciio.h>
+#include <sys/pmc.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
+
+#include <camlib.h>
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <kenv.h>
+#include <pmc.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#define TRANS_TOTAL "AMDIOMMU-MEM_TRANS_TOTAL"
+#define MAX_UNITS 256 /* unit= takes 0..255 */
+
+static void
+head_common(atf_tc_t *tc, const char *descr, bool exclusive)
+{
+ atf_tc_set_md_var(tc, "descr", descr);
+ atf_tc_set_md_var(tc, "require.arch", "amd64");
+ atf_tc_set_md_var(tc, "require.kmods", "hwpmc");
+ atf_tc_set_md_var(tc, "require.user", "root");
+ if (exclusive)
+ atf_tc_set_md_var(tc, "is.exclusive", "true");
+}
+
+/*
+ * Skip unless hwpmc(4) exports PMC_CLASS_AMDIOMMU. Store the class
+ * information in *cip if it is not NULL.
+ */
+static void
+require_amdiommu(struct pmc_classinfo *cip)
+{
+ const struct pmc_cpuinfo *ci;
+ unsigned int i;
+
+ if (pmc_init() != 0)
+ atf_tc_skip("hwpmc(4) is not available: %s", strerror(errno));
+ if (pmc_cpuinfo(&ci) != 0)
+ atf_tc_skip("pmc_cpuinfo() failed: %s", strerror(errno));
+ for (i = 0; i < ci->pm_nclass; i++) {
+ if (ci->pm_classes[i].pm_class == PMC_CLASS_AMDIOMMU) {
+ if (cip != NULL)
+ *cip = ci->pm_classes[i];
+ return;
+ }
+ }
+ atf_tc_skip("no AMD IOMMU performance counters");
+}
+
+/* Allocate a system-wide PMC on CPU 0; return 0 or the errno. */
+static int
+alloc_iommu_sc(const char *spec, pmc_id_t *idp)
+{
+ *idp = PMC_ID_INVALID;
+ if (pmc_allocate(spec, PMC_MODE_SC, 0, 0, idp, 0) != 0)
+ return (errno);
+ return (0);
+}
+
+/*
+ * Allocate MEM_TRANS_TOTAL PMCs on CPU 0 until an allocation fails. As
+ * each allocation takes the first free counter, unit by unit, together
+ * they cover every unit. Trying one more than the class has rows catches
+ * a class that hands out more PMCs than it has counters. Returns the
+ * count and the PMC ids in *idsp; *errp is the errno of the allocation
+ * that failed.
+ */
+static int
+alloc_iommu_sc_all(pmc_id_t **idsp, int *errp)
+{
+ struct pmc_classinfo ci;
+ pmc_id_t *ids;
+ int n;
+
+ require_amdiommu(&ci);
+ ids = calloc(ci.pm_num + 1, sizeof(*ids));
+ ATF_REQUIRE(ids != NULL);
+ *errp = 0;
+ for (n = 0; n <= (int)ci.pm_num; n++) {
+ if (pmc_allocate(TRANS_TOTAL, PMC_MODE_SC, 0, 0, &ids[n],
+ 0) != 0) {
+ *errp = errno;
+ break;
+ }
+ }
+ *idsp = ids;
+ return (n);
+}
+
+static void
+release_all(pmc_id_t *ids, int n)
+{
+ int i;
+
+ for (i = 0; i < n; i++) {
+ (void)pmc_stop(ids[i]);
+ (void)pmc_release(ids[i]);
+ }
+ free(ids);
+}
+
+/*
+ * Scan the AMDIOMMU rows, named AMDIOMMU<unit>-<bank>-<counter> and grouped
+ * by unit. Store the units in ascending order in units[] and, for each,
+ * in nmine[] the number of its rows owned by this process. Returns the
+ * number of units; *nrowsp is the number of rows.
+ */
+static int
+scan_rows(int *units, int *nmine, int *nrowsp)
+{
+ struct pmc_pmcinfo *ppi;
+ struct pmc_info *pi;
+ int i, n, npmc, u;
+
+ npmc = pmc_npmc(0);
+ ATF_REQUIRE(npmc > 0);
+ ATF_REQUIRE(pmc_pmcinfo(0, &ppi) == 0);
+ n = 0;
+ *nrowsp = 0;
+ for (i = 0; i < npmc; i++) {
+ pi = &ppi->pm_pmcs[i];
+ if (pi->pm_class != PMC_CLASS_AMDIOMMU)
+ continue;
+ ATF_REQUIRE_MSG(sscanf(pi->pm_name, "AMDIOMMU%d-", &u) == 1,
+ "row name \"%s\"", pi->pm_name);
+ (*nrowsp)++;
+ if (n == 0 || units[n - 1] != u) {
+ ATF_REQUIRE(n < MAX_UNITS);
+ units[n] = u;
+ nmine[n++] = 0;
+ }
+ if (pi->pm_ownerpid == getpid())
+ nmine[n - 1]++;
+ }
+ free(ppi);
+ ATF_REQUIRE_MSG(n > 0, "no AMDIOMMU rows in pmc_pmcinfo()");
+ return (n);
+}
+
+/*
+ * The units with at least min usable counters: those that got that many
+ * of the counters when all were allocated. Firmware-locked banks are not
+ * used, so a unit may have fewer.
+ */
+static int
+usable_units(int *units, int min)
+{
+ int nmine[MAX_UNITS], all[MAX_UNITS];
+ pmc_id_t *ids;
+ int err, i, n, nall, nrows, nu;
+
+ n = alloc_iommu_sc_all(&ids, &err);
+ nall = scan_rows(all, nmine, &nrows);
+ release_all(ids, n);
+ nu = 0;
+ for (i = 0; i < nall; i++) {
+ if (nmine[i] >= min)
+ units[nu++] = all[i];
+ else
+ printf("unit %d: %d usable counters\n", all[i],
+ nmine[i]);
+ }
+ return (nu);
+}
+
+/* Highest CPU other than 0 that hwpmc(4) reports as active, or -1. */
+static int
+other_active_cpu(void)
+{
+ struct pmc_pmcinfo *ppi;
+ int cpu;
+
+ for (cpu = pmc_ncpu() - 1; cpu > 0; cpu--) {
+ if (pmc_pmcinfo(cpu, &ppi) == 0) {
+ free(ppi);
+ return (cpu);
+ }
+ }
+ return (-1);
+}
+
+/* A raw disk whose DMA an IOMMU unit translates. */
+struct iommu_disk {
+ int fd;
+ int unit; /* amdiommu unit */
+ int rid; /* requester ID of the disk controller */
+ bool idmap; /* the context is identity-mapped */
+};
+
+/* Parse the "unit=amdiommuN rid=0xR" %iommu property of a device. */
+static int
+iommu_prop(const char *driver, int num, int *unitp, int *ridp)
+{
+ char key[128], val[128];
+ size_t len;
+
+ snprintf(key, sizeof(key), "dev.%s.%d.%%iommu", driver, num);
+ len = sizeof(val) - 1;
+ if (sysctlbyname(key, val, &len, NULL, 0) != 0)
+ return (-1);
+ val[len] = '\0';
+ if (sscanf(val, "unit=amdiommu%d rid=%x", unitp, ridp) != 2)
+ return (-1);
+ return (0);
+}
+
+/*
+ * Does busdma(9) give a device an identity-mapped context, as it does for
+ * "bounce" in hw.busdma.pciD.B.S.F or, without that, hw.busdma.default?
+ */
+static bool
+busdma_bounce(const char *driver, int num)
+{
+ char key[128], loc[256], val[32], *p;
+ size_t len;
+ u_int bus, dom, func, slot;
+
+ snprintf(key, sizeof(key), "dev.%s.%d.%%location", driver, num);
+ len = sizeof(loc) - 1;
+ if (sysctlbyname(key, loc, &len, NULL, 0) == 0) {
+ loc[len] = '\0';
+ if ((p = strstr(loc, "dbsf=pci")) != NULL &&
+ sscanf(p, "dbsf=pci%u:%u:%u:%u", &dom, &bus, &slot,
+ &func) == 4) {
+ snprintf(key, sizeof(key), "hw.busdma.pci%u.%u.%u.%u",
+ dom, bus, slot, func);
+ if (kenv(KENV_GET, key, val, sizeof(val)) > 0 &&
+ (strcmp(val, "bounce") == 0 ||
+ strcmp(val, "iommu") == 0 ||
+ strcmp(val, "dmar") == 0))
+ return (strcmp(val, "bounce") == 0);
+ }
+ }
+ return (kenv(KENV_GET, "hw.busdma.default", val, sizeof(val)) > 0 &&
+ strcmp(val, "bounce") == 0);
+}
+
+/*
+ * Find the IOMMU unit and requester ID of the controller of a CAM disk.
+ * The SIM may be a channel below the PCI function (ahcich), so walk up
+ * the parents until a device has an %iommu property. On failure, say why
+ * in why[].
+ */
+static bool
+disk_iommu(const char *path, int *unitp, int *ridp, bool *idmapp, char *why,
+ size_t size)
+{
+ struct cam_device *cam;
+ char driver[DEV_IDLEN + 1], key[128], parent[128], *p;
+ size_t len;
+ int depth, num;
+
+ /* pass(4) needs write access for the inquiry; no data is written. */
+ cam = cam_open_device(path, O_RDWR);
+ if (cam == NULL) {
+ snprintf(why, size, "%s is not a CAM disk", path);
+ return (false);
+ }
+ strlcpy(driver, cam->sim_name, sizeof(driver));
+ num = cam->sim_unit_number;
+ cam_close_device(cam);
+
+ snprintf(why, size, "the controller of %s has no AMD IOMMU context",
+ path);
+ for (depth = 0; depth < 8; depth++) {
+ if (iommu_prop(driver, num, unitp, ridp) == 0) {
+ *idmapp = busdma_bounce(driver, num);
+ return (true);
+ }
+ snprintf(key, sizeof(key), "dev.%s.%d.%%parent", driver, num);
+ len = sizeof(parent) - 1;
+ if (sysctlbyname(key, parent, &len, NULL, 0) != 0)
+ return (false);
+ parent[len] = '\0';
+ for (p = parent + strlen(parent);
+ p > parent && isdigit((unsigned char)p[-1]); p--)
+ ;
+ if (p == parent || *p == '\0')
+ return (false);
+ num = atoi(p);
+ *p = '\0';
+ strlcpy(driver, parent, sizeof(driver));
+ }
+ return (false);
+}
+
+/*
+ * Open the first raw disk, of the amdiommu_disk configuration variable or
+ * else of kern.disks, whose DMA goes through a context of an IOMMU unit
+ * with at least ncounters usable counters, and skip if there is none.
+ * From then on, a counter that does not count the DMA of the disk is a
+ * failure.
+ */
+static void
+open_iommu_disk(const atf_tc_t *tc, struct iommu_disk *d, int ncounters)
+{
+ char key[64], path[80], why[160], *disk, *disks, *p;
+ size_t len;
+ int dma, i, nu, units[MAX_UNITS];
+
+ require_amdiommu(NULL);
+ if (atf_tc_has_config_var(tc, "amdiommu_disk")) {
+ disks = strdup(atf_tc_get_config_var(tc, "amdiommu_disk"));
+ ATF_REQUIRE(disks != NULL);
+ } else {
+ if (sysctlbyname("kern.disks", NULL, &len, NULL, 0) != 0)
+ atf_tc_skip("no kern.disks");
+ disks = malloc(len + 1);
+ ATF_REQUIRE(disks != NULL);
+ ATF_REQUIRE(sysctlbyname("kern.disks", disks, &len, NULL,
+ 0) == 0);
+ disks[len] = '\0';
+ }
+ nu = usable_units(units, ncounters);
+ strlcpy(why, "no raw disk to make DMA with", sizeof(why));
+ d->fd = -1;
+ p = disks;
+ while (d->fd < 0 && (disk = strsep(&p, " ")) != NULL) {
+ if (*disk == '\0' || strncmp(disk, "cd", 2) == 0 ||
+ strncmp(disk, "md", 2) == 0)
+ continue;
+ snprintf(path, sizeof(path), "/dev/%s", disk);
+ if (!disk_iommu(path, &d->unit, &d->rid, &d->idmap, why,
+ sizeof(why))) {
+ printf("%s\n", why);
+ continue;
+ }
+ snprintf(key, sizeof(key), "dev.amdiommu.%d.dma", d->unit);
+ dma = 0;
+ len = sizeof(dma);
+ if (sysctlbyname(key, &dma, &len, NULL, 0) != 0 ||
+ len != sizeof(dma) || dma != 1) {
+ snprintf(why, sizeof(why),
+ "%s is not 1; boot with hw.iommu.dma=1", key);
+ printf("%s\n", why);
+ continue;
+ }
+ for (i = 0; i < nu && units[i] != d->unit; i++)
+ ;
+ if (i == nu) {
+ snprintf(why, sizeof(why),
+ "amdiommu%d has fewer than %d usable counters",
+ d->unit, ncounters);
+ printf("%s\n", why);
+ continue;
+ }
+ d->fd = open(path, O_RDONLY);
+ if (d->fd < 0) {
+ snprintf(why, sizeof(why), "%s: %s", path,
+ strerror(errno));
+ printf("%s\n", why);
+ }
+ }
+ free(disks);
+ if (d->fd < 0)
+ atf_tc_skip("%s", why);
+ printf("%s: amdiommu%d, requester %#x%s\n", path, d->unit, d->rid,
+ d->idmap ? ", identity-mapped" : "");
+}
+
+/* Read 4 MB from the raw disk, which bypasses the buffer cache. */
+static void
+drive_dma(const struct iommu_disk *d)
+{
+ static char buf[65536];
+ ssize_t n;
+ int i;
+
+ for (i = 0; i < 64; i++) {
+ n = pread(d->fd, buf, sizeof(buf), (off_t)i * sizeof(buf));
+ ATF_REQUIRE_MSG(n == (ssize_t)sizeof(buf),
+ "disk read %d returned %zd", i, n);
+ }
+}
+
+/* Allocate a system-wide PMC on CPU 0 for a formatted spec. */
+static pmc_id_t __printflike(1, 2)
+alloc_pmcf(const char *fmt, ...)
+{
+ char spec[128];
+ va_list ap;
+ pmc_id_t id;
+ int rc;
+
+ va_start(ap, fmt);
+ vsnprintf(spec, sizeof(spec), fmt, ap);
+ va_end(ap);
+ rc = alloc_iommu_sc(spec, &id);
+ ATF_REQUIRE_MSG(rc == 0, "%s: %s", spec, strerror(rc));
+ return (id);
+}
+
+/*
+ * A requester ID that no PCI device uses, directly or as the alias the
+ * IOMMU sees, for checking that a requester ID filter rejects.
+ */
+static int
+unused_rid(void)
+{
+ static bool used[UINT16_MAX + 1];
+ struct pci_conf conf[64];
+ struct pci_conf_io pc;
+ int fd, i, rid, tries, unit;
+
+ fd = open("/dev/pci", O_RDONLY);
+ ATF_REQUIRE_MSG(fd >= 0, "/dev/pci: %s", strerror(errno));
+ /* Start over, as pciconf(8) does, if the device list changes. */
+ for (tries = 0;; tries++) {
+ ATF_REQUIRE_MSG(tries < 10,
+ "the PCI device list keeps changing");
+ memset(used, 0, sizeof(used));
+ memset(&pc, 0, sizeof(pc));
+ pc.match_buf_len = sizeof(conf);
+ pc.matches = conf;
+ do {
+ ATF_REQUIRE(ioctl(fd, PCIOCGETCONF, &pc) == 0);
+ if (pc.status == PCI_GETCONF_LIST_CHANGED)
+ break;
+ ATF_REQUIRE_MSG(pc.status == PCI_GETCONF_LAST_DEVICE ||
+ pc.status == PCI_GETCONF_MORE_DEVS,
+ "PCIOCGETCONF status %d", pc.status);
+ /*
+ * Requester IDs are per PCI domain; ignoring the
+ * domain only marks more of them as used.
+ */
+ for (i = 0; i < (int)pc.num_matches; i++) {
+ used[conf[i].pc_sel.pc_bus << 8 |
+ conf[i].pc_sel.pc_dev << 3 |
+ conf[i].pc_sel.pc_func] = true;
+ if (conf[i].pd_name[0] != '\0' &&
+ iommu_prop(conf[i].pd_name,
+ conf[i].pd_unit, &unit, &rid) == 0 &&
+ rid <= UINT16_MAX)
+ used[rid] = true;
+ }
+ } while (pc.status == PCI_GETCONF_MORE_DEVS);
+ if (pc.status == PCI_GETCONF_LAST_DEVICE)
+ break;
+ }
+ ATF_REQUIRE(close(fd) == 0);
+ for (rid = UINT16_MAX; rid > 0 && used[rid]; rid--)
+ ;
+ ATF_REQUIRE_MSG(!used[rid], "every requester ID is in use");
+ return (rid);
+}
+
+ATF_TC(iommu_pmc_class_present);
+ATF_TC_HEAD(iommu_pmc_class_present, tc)
+{
+ head_common(tc, "The AMDIOMMU class advertises system-wide read, "
+ "write and qualifier capabilities, and neither interrupts nor "
+ "per-domain allocation", false);
+}
+ATF_TC_BODY(iommu_pmc_class_present, tc)
+{
+ struct pmc_classinfo ci;
+ uint32_t want;
+
+ require_amdiommu(&ci);
+ want = PMC_CAP_SYSTEM | PMC_CAP_READ | PMC_CAP_WRITE |
+ PMC_CAP_SYSWIDE | PMC_CAP_QUALIFIER;
+ ATF_CHECK_EQ_MSG(want, ci.pm_caps & want, "caps %#x", ci.pm_caps);
+ ATF_CHECK_EQ_MSG(0, ci.pm_caps & (PMC_CAP_INTERRUPT |
+ PMC_CAP_DOMWIDE), "caps %#x", ci.pm_caps);
+}
+
+ATF_TC(iommu_pmc_topology);
+ATF_TC_HEAD(iommu_pmc_topology, tc)
+{
+ head_common(tc, "The AMDIOMMU class has rows, 48-bit wide, named "
+ "after their unit", false);
+}
+ATF_TC_BODY(iommu_pmc_topology, tc)
+{
+ struct pmc_classinfo ci;
+ int nmine[MAX_UNITS], units[MAX_UNITS];
+ int nrows;
+
+ require_amdiommu(&ci);
+ ATF_CHECK_MSG(ci.pm_num > 0, "no rows");
+ ATF_CHECK_EQ_MSG(48, ci.pm_width, "width %u", ci.pm_width);
+ (void)scan_rows(units, nmine, &nrows);
+ ATF_CHECK_EQ_MSG((int)ci.pm_num, nrows, "%u rows in the class, %d "
+ "in pmc_pmcinfo()", ci.pm_num, nrows);
+}
+
+ATF_TC(iommu_pmc_alloc_free);
+ATF_TC_HEAD(iommu_pmc_alloc_free, tc)
+{
+ head_common(tc, "Every AMDIOMMU event can be allocated and released, "
+ "twice, without leaking counters", true);
+}
+ATF_TC_BODY(iommu_pmc_alloc_free, tc)
+{
+ const char **names;
+ char spec[96];
+ pmc_id_t id;
+ uint32_t caps;
+ int i, nevents, pass, rc;
+
+ require_amdiommu(NULL);
+ ATF_REQUIRE(pmc_event_names_of_class(PMC_CLASS_AMDIOMMU, &names,
+ &nevents) == 0);
+ ATF_CHECK_EQ_MSG(24, nevents, "%d events", nevents);
+
+ /* A release that leaks the counter makes the second pass fail. */
+ for (pass = 0; pass < 2; pass++) {
+ for (i = 0; i < nevents; i++) {
+ snprintf(spec, sizeof(spec), "AMDIOMMU-%s", names[i]);
+ rc = alloc_iommu_sc(spec, &id);
+ ATF_REQUIRE_MSG(rc == 0, "pass %d: %s: %s", pass,
+ spec, strerror(rc));
+ /* pmcstat(8) allocates SYSWIDE PMCs on one CPU. */
+ ATF_REQUIRE(pmc_capabilities(id, &caps) == 0);
+ ATF_CHECK_MSG((caps & PMC_CAP_SYSWIDE) != 0,
+ "%s: caps %#x", spec, caps);
+ ATF_REQUIRE_MSG(pmc_release(id) == 0,
+ "pass %d: release %s: %s", pass, spec,
+ strerror(errno));
+ }
+ }
+ free(names);
+}
+
+ATF_TC(iommu_pmc_count_dma);
+ATF_TC_HEAD(iommu_pmc_count_dma, tc)
+{
+ head_common(tc, "MEM_TRANS_TOTAL counters never go backwards, and the "
+ "counter of the unit that translates the DMA of a raw disk "
+ "advances while the disk is read", true);
+}
+ATF_TC_BODY(iommu_pmc_count_dma, tc)
+{
+ struct iommu_disk d;
+ pmc_id_t *ids, id;
+ pmc_value_t *after, *before, v0, v1;
+ int err, i, n;
+
+ open_iommu_disk(tc, &d, 2);
+ id = alloc_pmcf(TRANS_TOTAL ",unit=%d", d.unit);
+ /* The other counters too, to check that none goes backwards. */
+ n = alloc_iommu_sc_all(&ids, &err);
+ before = calloc(n + 1, sizeof(*before));
+ after = calloc(n + 1, sizeof(*after));
+ ATF_REQUIRE(before != NULL && after != NULL);
+
+ ATF_REQUIRE(pmc_start(id) == 0);
+ for (i = 0; i < n; i++)
+ ATF_REQUIRE(pmc_start(ids[i]) == 0);
+ ATF_REQUIRE(pmc_read(id, &v0) == 0);
+ for (i = 0; i < n; i++)
+ ATF_REQUIRE(pmc_read(ids[i], &before[i]) == 0);
+ drive_dma(&d);
+ for (i = 0; i < n; i++)
+ ATF_REQUIRE(pmc_read(ids[i], &after[i]) == 0);
+ ATF_REQUIRE(pmc_read(id, &v1) == 0);
+ release_all(ids, n);
+ (void)pmc_stop(id);
+ ATF_REQUIRE(pmc_release(id) == 0);
+ ATF_REQUIRE(close(d.fd) == 0);
+
+ for (i = 0; i < n; i++)
+ ATF_CHECK_MSG(after[i] >= before[i],
+ "counter %d went backwards: %ju -> %ju", i,
+ (uintmax_t)before[i], (uintmax_t)after[i]);
+ free(before);
+ free(after);
+ ATF_CHECK_MSG(v1 > v0, "amdiommu%d did not count the disk DMA: "
+ "%ju -> %ju", d.unit, (uintmax_t)v0, (uintmax_t)v1);
+}
+
+ATF_TC(iommu_pmc_tlb_hit_miss);
+ATF_TC_HEAD(iommu_pmc_tlb_hit_miss, tc)
+{
+ head_common(tc, "The IOMMU TLB hit and miss counters can be held at "
+ "once on the unit that translates the DMA of a raw disk, and some "
+ "of them advance while the disk is read", true);
+}
+ATF_TC_BODY(iommu_pmc_tlb_hit_miss, tc)
+{
+ /* Lookups may be counted at the PTE or at the PDE level. */
+ static const char *events[] = {
+ "AMDIOMMU-MEM_IOMMU_TLB_PTE_HIT",
+ "AMDIOMMU-MEM_IOMMU_TLB_PTE_MIS",
+ "AMDIOMMU-MEM_IOMMU_TLB_PDE_HIT",
+ "AMDIOMMU-MEM_IOMMU_TLB_PDE_MIS",
+ };
+ struct iommu_disk d;
+ pmc_id_t id[nitems(events)];
+ pmc_value_t v0[nitems(events)], v1[nitems(events)];
+ size_t i;
+ bool moved;
+
+ open_iommu_disk(tc, &d, nitems(events));
+ for (i = 0; i < nitems(events); i++)
+ id[i] = alloc_pmcf("%s,unit=%d", events[i], d.unit);
+ for (i = 0; i < nitems(events); i++)
+ ATF_REQUIRE(pmc_start(id[i]) == 0);
+ for (i = 0; i < nitems(events); i++)
+ ATF_REQUIRE(pmc_read(id[i], &v0[i]) == 0);
+ drive_dma(&d);
+ for (i = 0; i < nitems(events); i++)
+ ATF_REQUIRE(pmc_read(id[i], &v1[i]) == 0);
+ for (i = 0; i < nitems(events); i++) {
+ (void)pmc_stop(id[i]);
+ ATF_REQUIRE(pmc_release(id[i]) == 0);
+ }
+ ATF_REQUIRE(close(d.fd) == 0);
+
+ moved = false;
+ for (i = 0; i < nitems(events); i++) {
+ ATF_REQUIRE_MSG(v1[i] >= v0[i], "%s went backwards",
+ events[i]);
+ if (v1[i] > v0[i])
+ moved = true;
+ }
+ /*
+ * An identity-mapped context has no page tables. The EPYC 9654
+ * still counts IOMMU TLB hits for it, but other parts may not.
+ */
+ if (!moved && d.idmap)
+ atf_tc_skip("amdiommu%d counted no IOMMU TLB lookup for the "
+ "identity-mapped context of the disk", d.unit);
+ ATF_CHECK_MSG(moved, "amdiommu%d: no IOMMU TLB hit or miss for the "
+ "disk DMA", d.unit);
+}
+
+ATF_TC(iommu_pmc_stop_read_order);
+ATF_TC_HEAD(iommu_pmc_stop_read_order, tc)
+{
+ head_common(tc, "A counter read after stop returns its final count, "
+ "does not change while stopped, and counts again after a restart",
+ true);
+}
+ATF_TC_BODY(iommu_pmc_stop_read_order, tc)
+{
+ struct iommu_disk d;
+ pmc_id_t id;
+ pmc_value_t after, first, frozen, resumed, running, stopped;
+
+ open_iommu_disk(tc, &d, 2);
+ id = alloc_pmcf(TRANS_TOTAL ",unit=%d", d.unit);
+ ATF_REQUIRE(pmc_start(id) == 0);
+ ATF_REQUIRE(pmc_read(id, &first) == 0);
+ drive_dma(&d);
+ ATF_REQUIRE(pmc_read(id, &running) == 0);
+ ATF_REQUIRE_MSG(running > first, "amdiommu%d did not count the disk "
+ "DMA", d.unit);
+
+ /*
+ * The MI layer stops a PMC and only then reads it, and some parts
+ * power-gate a counter whose event is deselected, so the class must
+ * return the count saved at stop, and load it again at restart.
+ */
+ ATF_REQUIRE(pmc_stop(id) == 0);
+ ATF_REQUIRE(pmc_read(id, &stopped) == 0);
+ drive_dma(&d);
+ ATF_REQUIRE(pmc_read(id, &frozen) == 0);
+ ATF_REQUIRE(pmc_start(id) == 0);
+ ATF_REQUIRE(pmc_read(id, &resumed) == 0);
+ drive_dma(&d);
+ ATF_REQUIRE(pmc_read(id, &after) == 0);
+ (void)pmc_stop(id);
+ ATF_REQUIRE(pmc_release(id) == 0);
+ ATF_REQUIRE(close(d.fd) == 0);
+
+ ATF_CHECK_MSG(stopped >= running, "count lost at stop: %ju -> %ju",
+ (uintmax_t)running, (uintmax_t)stopped);
+ ATF_CHECK_MSG(frozen == stopped, "stopped counter changed: %ju -> %ju",
+ (uintmax_t)stopped, (uintmax_t)frozen);
+ ATF_CHECK_MSG(resumed >= stopped, "restart lost the count: %ju -> %ju",
+ (uintmax_t)stopped, (uintmax_t)resumed);
+ ATF_CHECK_MSG(after > resumed, "no counting after the restart");
+}
+
+ATF_TC(iommu_pmc_multicpu_alloc);
+ATF_TC_HEAD(iommu_pmc_multicpu_alloc, tc)
+{
+ head_common(tc, "PMCs allocated at the same time on two CPUs get "
+ "different counters and release cleanly", true);
+}
+ATF_TC_BODY(iommu_pmc_multicpu_alloc, tc)
+{
+ pmc_id_t id0, idn;
+ int cpu, rc;
+
+ require_amdiommu(NULL);
+ cpu = other_active_cpu();
+ if (cpu < 0)
+ atf_tc_skip("needs a second active CPU");
+
+ ATF_REQUIRE_MSG(pmc_allocate(TRANS_TOTAL, PMC_MODE_SC, 0, 0, &id0,
+ 0) == 0, "allocation on CPU 0: %s", strerror(errno));
+ rc = pmc_allocate(TRANS_TOTAL, PMC_MODE_SC, 0, cpu, &idn, 0);
+ ATF_CHECK_MSG(rc == 0, "allocation on CPU %d: %s", cpu,
+ strerror(errno));
+ if (rc == 0) {
+ ATF_CHECK_MSG(PMC_ID_TO_ROWINDEX(idn) !=
+ PMC_ID_TO_ROWINDEX(id0), "both PMCs got row %u",
+ PMC_ID_TO_ROWINDEX(id0));
+ ATF_CHECK(pmc_release(idn) == 0);
+ }
+ ATF_CHECK(pmc_release(id0) == 0);
+}
+
+ATF_TC(iommu_pmc_non_sc_rejected);
+ATF_TC_HEAD(iommu_pmc_non_sc_rejected, tc)
+{
+ head_common(tc, "Process-mode AMDIOMMU PMCs are rejected by libpmc, "
+ "and with EINVAL by hwpmc(4) when libpmc is bypassed", false);
+}
+ATF_TC_BODY(iommu_pmc_non_sc_rejected, tc)
+{
+ struct module_stat ms;
+ struct pmc_op_pmcallocate pa;
+ struct pmc_op_simple ps;
+ int modid, rc;
+
+ require_amdiommu(NULL);
+
+ rc = pmc_allocate(TRANS_TOTAL, PMC_MODE_TC, 0, PMC_CPU_ANY,
+ &ps.pm_pmcid, 0);
+ if (rc == 0)
+ (void)pmc_release(ps.pm_pmcid);
+ ATF_CHECK_MSG(rc != 0, "libpmc accepted a PMC_MODE_TC request");
+
+ /* Find the hwpmc(4) system call the way pmc_init() does. */
+ modid = modfind(PMC_MODULE_NAME);
+ ATF_REQUIRE_MSG(modid >= 0, "modfind(%s): %s", PMC_MODULE_NAME,
+ strerror(errno));
+ ms.version = sizeof(ms);
+ ATF_REQUIRE(modstat(modid, &ms) == 0);
+
+ memset(&pa, 0, sizeof(pa));
+ pa.pm_caps = PMC_CAP_READ | PMC_CAP_WRITE;
+ pa.pm_cpu = PMC_CPU_ANY;
+ pa.pm_class = PMC_CLASS_AMDIOMMU;
+ pa.pm_ev = PMC_EV_AMDIOMMU_MEM_TRANS_TOTAL;
+ pa.pm_mode = PMC_MODE_TC;
+ rc = syscall(ms.data.intval, PMC_OP_PMCALLOCATE, &pa);
+ if (rc == 0) {
+ ps.pm_pmcid = pa.pm_pmcid;
+ (void)syscall(ms.data.intval, PMC_OP_PMCRELEASE, &ps);
+ }
+ ATF_CHECK_MSG(rc != 0 && errno == EINVAL,
+ "PMC_MODE_TC allocation: rc %d, errno %d", rc, errno);
+}
+
+ATF_TC(iommu_pmc_exclusive_counters);
+ATF_TC_HEAD(iommu_pmc_exclusive_counters, tc)
+{
+ head_common(tc, "No more PMCs than counters can be allocated, and "
+ "once all are held an allocation on another CPU fails", true);
+}
+ATF_TC_BODY(iommu_pmc_exclusive_counters, tc)
+{
+ struct pmc_classinfo ci;
+ pmc_id_t *ids, extra;
+ int cpu, err, err2, n, rc;
+
+ require_amdiommu(&ci);
+ cpu = other_active_cpu();
+ if (cpu < 0)
+ atf_tc_skip("needs a second active CPU");
+
+ n = alloc_iommu_sc_all(&ids, &err);
+ rc = pmc_allocate(TRANS_TOTAL, PMC_MODE_SC, 0, cpu, &extra, 0);
+ err2 = errno;
+ if (rc == 0)
+ (void)pmc_release(extra);
+ release_all(ids, n);
+ ATF_REQUIRE_MSG(n > 0, "allocation failed: %s", strerror(err));
+ ATF_CHECK_MSG(n <= (int)ci.pm_num, "%d PMCs allocated on %u counters",
+ n, ci.pm_num);
+ ATF_CHECK_MSG(rc != 0,
+ "allocation on CPU %d succeeded with all %d counters held", cpu,
+ n);
+ ATF_CHECK_MSG(rc == 0 || err2 == EINVAL, "allocation on CPU %d: %s",
+ cpu, strerror(err2));
+}
+
+ATF_TC(iommu_pmc_kill_running_owner);
+ATF_TC_HEAD(iommu_pmc_kill_running_owner, tc)
+{
+ head_common(tc, "The counters of a process killed while counting "
+ "are released and can be allocated again", true);
+}
+ATF_TC_BODY(iommu_pmc_kill_running_owner, tc)
+{
+ struct pmc_classinfo ci;
+ pmc_id_t *ids;
+ pid_t pid;
+ ssize_t rc;
+ int err, fds[2], n, n2, status;
+
+ require_amdiommu(&ci);
+ ids = calloc(ci.pm_num, sizeof(*ids));
+ ATF_REQUIRE(ids != NULL);
+ ATF_REQUIRE(pipe(fds) == 0);
+ pid = fork();
+ ATF_REQUIRE(pid >= 0);
+ if (pid == 0) {
+ /* Report the number of running counters, or -1. */
+ (void)close(fds[0]);
+ for (n = 0; n < (int)ci.pm_num; n++) {
+ if (pmc_allocate(TRANS_TOTAL, PMC_MODE_SC, 0, 0,
+ &ids[n], 0) != 0)
+ break;
+ if (pmc_start(ids[n]) != 0) {
+ n = -1;
+ break;
+ }
+ }
+ if (write(fds[1], &n, sizeof(n)) != sizeof(n) || n <= 0)
+ _exit(1);
+ for (;;)
+ pause();
+ }
+ free(ids);
+
+ (void)close(fds[1]);
+ n = 0;
+ rc = read(fds[0], &n, sizeof(n));
+ (void)close(fds[0]);
+ (void)kill(pid, SIGKILL);
+ ATF_REQUIRE(waitpid(pid, &status, 0) == pid);
+ ATF_REQUIRE_MSG(rc == sizeof(n), "the child died before reporting");
+ ATF_REQUIRE_MSG(n >= 0, "pmc_start() failed in the child");
+ if (n == 0)
+ atf_tc_skip("no free counter for the child");
+ ATF_REQUIRE_MSG(WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL,
+ "child status %#x", status);
+
+ n2 = alloc_iommu_sc_all(&ids, &err);
+ release_all(ids, n2);
+ ATF_CHECK_MSG(n2 >= n,
+ "the killed child held %d counters, only %d allocatable after", n,
+ n2);
+}
+
+ATF_TC(iommu_pmc_qualifiers);
+ATF_TC_HEAD(iommu_pmc_qualifiers, tc)
+{
+ head_common(tc, "Valid qualifiers are accepted, with case-insensitive "
+ "keywords; empty, signed, out of range, repeated and unknown ones "
+ "are rejected", true);
+}
+ATF_TC_BODY(iommu_pmc_qualifiers, tc)
+{
+ static const struct {
+ const char *q;
+ bool ok;
+ } cases[] = {
+ { "devid=0x500", true }, { "devid=1280", true },
+ { "devid=0xffff", true }, { "domain=0xffff", true },
+ { "pasid=0xfffff", true }, { "DEVID=1", true },
+ { "devid=0x500,domain=4", true },
+ { "devid=0x10000", false }, { "domain=0x10000", false },
+ { "pasid=0x100000", false }, { "unit=256", false },
+ { "devid=", false }, { "domain=", false },
+ { "devid=-1", false }, { "devid=+1", false },
+ { "devid=abc", false }, { "devid=5x", false },
+ { "devid=1,devid=2", false }, { "devid=1,", false },
+ { "", false }, { "foo=1", false },
+ };
+ char spec[128];
+ pmc_id_t id;
+ size_t i;
+ int rc;
+
+ require_amdiommu(NULL);
+ for (i = 0; i < nitems(cases); i++) {
+ snprintf(spec, sizeof(spec), TRANS_TOTAL ",%s", cases[i].q);
+ rc = alloc_iommu_sc(spec, &id);
+ if (rc == 0)
+ (void)pmc_release(id);
+ if (cases[i].ok)
+ ATF_CHECK_MSG(rc == 0, "\"%s\" rejected (%s)",
+ cases[i].q, strerror(rc));
+ else
+ ATF_CHECK_MSG(rc != 0, "\"%s\" accepted", cases[i].q);
+ }
+}
+
+ATF_TC(iommu_pmc_unit_select);
+ATF_TC_HEAD(iommu_pmc_unit_select, tc)
+{
+ head_common(tc, "unit=N allocates a counter of IOMMU unit N, and a "
+ "unit without usable counters is rejected", true);
+}
+ATF_TC_BODY(iommu_pmc_unit_select, tc)
+{
+ struct pmc_pmcinfo *ppi;
+ int units[MAX_UNITS];
+ char prefix[32], spec[96];
+ pmc_id_t id;
+ int i, j, missing, npmc, nu, rc;
+ bool found;
+
+ require_amdiommu(NULL);
+ nu = usable_units(units, 1);
+ if (nu == 0)
+ atf_tc_skip("no unit has a usable counter");
+ npmc = pmc_npmc(0);
+ for (i = 0; i < nu; i++) {
+ snprintf(spec, sizeof(spec), TRANS_TOTAL ",unit=%d", units[i]);
+ rc = alloc_iommu_sc(spec, &id);
+ ATF_REQUIRE_MSG(rc == 0, "%s: %s", spec, strerror(rc));
+ ATF_REQUIRE(pmc_pmcinfo(0, &ppi) == 0);
+ snprintf(prefix, sizeof(prefix), "AMDIOMMU%d-", units[i]);
+ found = false;
+ for (j = 0; j < npmc; j++) {
+ if (ppi->pm_pmcs[j].pm_class == PMC_CLASS_AMDIOMMU &&
+ ppi->pm_pmcs[j].pm_ownerpid == getpid() &&
+ strncmp(ppi->pm_pmcs[j].pm_name, prefix,
+ strlen(prefix)) == 0)
+ found = true;
+ }
+ free(ppi);
+ (void)pmc_release(id);
+ ATF_CHECK_MSG(found, "%s did not allocate a %s counter", spec,
+ prefix);
+ }
+
+ /* The lowest unit number without usable counters. */
+ missing = 0;
+ for (i = 0; i < nu && units[i] == missing; i++)
+ missing++;
+ snprintf(spec, sizeof(spec), TRANS_TOTAL ",unit=%d", missing);
+ if (alloc_iommu_sc(spec, &id) == 0) {
+ (void)pmc_release(id);
+ atf_tc_fail("%s accepted, but unit %d has no usable counters",
+ spec, missing);
+ }
+}
+
+ATF_TC(iommu_pmc_devid_filter);
+ATF_TC_HEAD(iommu_pmc_devid_filter, tc)
+{
+ head_common(tc, "A requester ID filter counts the DMA of a raw disk's "
+ "controller, no more than an unfiltered counter does, and counts "
+ "nothing for a requester ID that no device uses", true);
+}
+ATF_TC_BODY(iommu_pmc_devid_filter, tc)
+{
+ struct iommu_disk d;
+ pmc_id_t all, match, other;
+ pmc_value_t a0, a1, m0, m1, o0, o1;
+ int none;
+
+ open_iommu_disk(tc, &d, 2);
+ none = unused_rid();
+
+ /* The filtered window lies within the unfiltered one. */
+ all = alloc_pmcf(TRANS_TOTAL ",unit=%d", d.unit);
+ match = alloc_pmcf(TRANS_TOTAL ",unit=%d,devid=%#x", d.unit,
+ d.rid);
+ ATF_REQUIRE(pmc_start(all) == 0);
+ ATF_REQUIRE(pmc_start(match) == 0);
+ ATF_REQUIRE(pmc_read(all, &a0) == 0);
+ ATF_REQUIRE(pmc_read(match, &m0) == 0);
+ drive_dma(&d);
+ ATF_REQUIRE(pmc_read(match, &m1) == 0);
+ ATF_REQUIRE(pmc_read(all, &a1) == 0);
+ (void)pmc_stop(match);
+ (void)pmc_stop(all);
+ ATF_REQUIRE(pmc_release(match) == 0);
+ ATF_REQUIRE(pmc_release(all) == 0);
+ ATF_CHECK_MSG(m1 > m0, "devid=%#x counted nothing (an IVHD alias "
+ "for the controller?)", d.rid);
+ ATF_CHECK_MSG(m1 - m0 <= a1 - a0, "devid=%#x counted %ju, more than "
+ "the unfiltered %ju", d.rid, (uintmax_t)(m1 - m0),
+ (uintmax_t)(a1 - a0));
+
+ /* In one window, the disk requester counts and an unused one not. */
+ match = alloc_pmcf(TRANS_TOTAL ",unit=%d,devid=%#x", d.unit,
+ d.rid);
+ other = alloc_pmcf(TRANS_TOTAL ",unit=%d,devid=%#x", d.unit,
+ none);
+ ATF_REQUIRE(pmc_start(match) == 0);
+ ATF_REQUIRE(pmc_start(other) == 0);
+ ATF_REQUIRE(pmc_read(match, &m0) == 0);
+ ATF_REQUIRE(pmc_read(other, &o0) == 0);
+ drive_dma(&d);
+ ATF_REQUIRE(pmc_read(other, &o1) == 0);
+ ATF_REQUIRE(pmc_read(match, &m1) == 0);
+ (void)pmc_stop(other);
+ (void)pmc_stop(match);
+ ATF_REQUIRE(pmc_release(other) == 0);
+ ATF_REQUIRE(pmc_release(match) == 0);
+ ATF_REQUIRE(close(d.fd) == 0);
+ ATF_CHECK_MSG(m1 > m0, "devid=%#x counted nothing", d.rid);
+ ATF_CHECK_MSG(o1 == o0, "devid=%#x, used by no device, counted %ju",
+ none, (uintmax_t)(o1 - o0));
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, iommu_pmc_class_present);
+ ATF_TP_ADD_TC(tp, iommu_pmc_topology);
+ ATF_TP_ADD_TC(tp, iommu_pmc_alloc_free);
+ ATF_TP_ADD_TC(tp, iommu_pmc_count_dma);
+ ATF_TP_ADD_TC(tp, iommu_pmc_tlb_hit_miss);
+ ATF_TP_ADD_TC(tp, iommu_pmc_stop_read_order);
+ ATF_TP_ADD_TC(tp, iommu_pmc_multicpu_alloc);
+ ATF_TP_ADD_TC(tp, iommu_pmc_non_sc_rejected);
+ ATF_TP_ADD_TC(tp, iommu_pmc_exclusive_counters);
+ ATF_TP_ADD_TC(tp, iommu_pmc_kill_running_owner);
+ ATF_TP_ADD_TC(tp, iommu_pmc_qualifiers);
+ ATF_TP_ADD_TC(tp, iommu_pmc_unit_select);
+ ATF_TP_ADD_TC(tp, iommu_pmc_devid_filter);
+
+ return (atf_no_error());
+}

File Metadata

Mime Type
text/plain
Expires
Tue, Dec 1, 2:06 PM (23 h, 19 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42125844
Default Alt Text
D60549.1796133973.diff (33 KB)

Event Timeline