Skip to contentSkip to conversation

resctrl: Add perf PMU for resctrl monitoring

PATCH In review Jonathan Perry 2 participants 12 messages Oct 16, 2025 – Oct 17, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover resctrl: Add perf PMU for resctrl monitoring

Expose resctrl monitoring data via a lightweight perf PMU.

Background: The kernel's initial cache-monitoring interface shipped via perf (commit 4afbb24ce5e7, 2015). That approach tied monitoring to tasks and cgroups. Later, cache control was designed around the resctrl filesystem to better match hardware semantics, and the incompatible perf CQM code was removed (commit c39a0e2c8850, 2017). This series implements a thin, generic perf PMU that _is_ compatible with resctrl.

Motivation: perf support enables measuring cache occupancy and memory bandwidth metrics on hrtimer (high resolution timer) interrupts via eBPF. Compared with polling from userspace, hrtimer-based reads remove scheduling jitter and context switch overhead. Further, PMU reads can be parallel, since the PMU read path need not lock resctrl's rdtgroup_mutex. Parallelization and reduced jitter enable more accurate snapshots of cache occupancy and memory bandwidth. [1] has more details on the motivation and design.

Design: The "resctrl" PMU is a small adapter on top of resctrl's monitoring path:
- Event selection uses attr.config to pass an open mon_data fd (e.g. mon_L3_00/llc_occupancy).
- Events must be CPU-bound within the file's domain. Perf is responsible the read executes on the bound CPU.
- Event init resolves and pins the rdtgroup, prepares struct rmid_read via mon_event_setup_read(), and validates the bound CPU is in the file's domain CPU mask.
- Sampling is not supported; reads match the mon_data file contents.
- If the rdtgroup is deleted, reads return 0.

Includes a new selftest (tools/testing/selftests/resctrl/pmu_test.c) to validate the PMU event init path, and adds PMU testing to existing CMT tests.

Example usage (see Documentation/filesystems/resctrl.rst):
Open a monitoring file and pass its fd in perf_event_attr.config, with attr.type set to the resctrl PMU type.

The patches are based on top of v6.18-rc1 (commit 3a8660878839).

[1] https://www.youtube.com/watch?v=4BGhAMJdZTc

Jonathan Perry (8):
resctrl: Pin rdtgroup for mon_data file lifetime resctrl/mon: Split RMID read init from execution resctrl/mon: Select cpumask before invoking mon_event_read() resctrl/mon: Create mon_event_setup_read() helper
resctrl: Propagate CPU mask validation error via rr->err resctrl/pmu: Introduce skeleton PMU and selftests resctrl/pmu: Use mon_event_setup_read() and validate CPU resctrl/pmu: Implement .read via direct RMID read; add LLC selftest

 Documentation/filesystems/resctrl.rst         |  64 ++++
 fs/resctrl/Makefile                           |   2 +-
 fs/resctrl/ctrlmondata.c                      | 118 ++++---
 fs/resctrl/internal.h                         |  24 +-
 fs/resctrl/monitor.c                          |   8 +-
 fs/resctrl/pmu.c                              | 217 +++++++++++++
 fs/resctrl/rdtgroup.c                         | 131 +++++++-
 tools/testing/selftests/resctrl/cache.c       |  94 +++++-
 tools/testing/selftests/resctrl/cmt_test.c    |  17 +-
 tools/testing/selftests/resctrl/pmu_test.c    | 292 ++++++++++++++++++
 tools/testing/selftests/resctrl/pmu_utils.c   |  32 ++
 tools/testing/selftests/resctrl/resctrl.h     |   4 +
 .../testing/selftests/resctrl/resctrl_tests.c |   1 +
 13 files changed, 948 insertions(+), 56 deletions(-)
 create mode 100644 fs/resctrl/pmu.c
 create mode 100644 tools/testing/selftests/resctrl/pmu_test.c
 create mode 100644 tools/testing/selftests/resctrl/pmu_utils.c
#1 lore raw

PATCH 1/8 resctrl: Pin rdtgroup for mon_data file lifetime

Add .open and .release handlers to mon_data kernfs files so a monitoring file holds a reference to its rdtgroup for the file's lifetime. Store the rdtgroup in of->priv on open and drop it on release. Provide rdtgroup_get()/rdtgroup_put() helpers.

This lets code that only has an open monitoring fd (e.g. the resctrl PMU event_init path) safely resolve the rdtgroup without having a kernfs active reference.

No functional change intended.

---
 fs/resctrl/internal.h |  2 ++
 fs/resctrl/rdtgroup.c | 62 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
fs/resctrl/internal.h +2 -0
index cf1fd82dc5a9..63fb4d6c21a7 100644
@@ -360,6 +360,8 @@ void resctrl_mon_resource_exit(void);
 void mon_event_count(void *info);
 
 int rdtgroup_mondata_show(struct seq_file *m, void *arg);
+int rdtgroup_mondata_open(struct kernfs_open_file *of);
+void rdtgroup_mondata_release(struct kernfs_open_file *of);
 
 void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
 		    struct rdt_mon_domain *d, struct rdtgroup *rdtgrp,
fs/resctrl/rdtgroup.c +62 -0

92 lines not on this page · raw message

#2 lore raw

PATCH 2/8 resctrl/mon: Split RMID read init from execution

In reply to Jonathan Perry

Introduce rmid_read_init() to fill struct rmid_read (resource, domain, rdtgroup, event id, flags, ci). Change mon_event_read() to accept a prepared rmid_read and a CPU mask.

Update callers to use rmid_read_init() + mon_event_read().

This prepares reuse from contexts that pre-select the CPU (e.g. the perf PMU) without duplicating initialization logic.

No functional change intended.

---
 fs/resctrl/ctrlmondata.c | 40 ++++++++++++++++++++++------------------
 fs/resctrl/internal.h    |  5 +++--
 fs/resctrl/rdtgroup.c    |  6 ++++--
 3 files changed, 29 insertions(+), 22 deletions(-)
fs/resctrl/ctrlmondata.c +22 -18
index 0d0ef54fc4de..82f8ad2b3053 100644
@@ -546,28 +546,31 @@ struct rdt_domain_hdr *resctrl_find_domain(struct list_head *h, int id,
 	return NULL;
 }
 
-void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
+void rmid_read_init(struct rmid_read *rr, struct rdt_resource *r,
 		    struct rdt_mon_domain *d, struct rdtgroup *rdtgrp,
-		    cpumask_t *cpumask, int evtid, int first)
+		    int evtid, int first, struct cacheinfo *ci)
 {
-	int cpu;
-
-	/* When picking a CPU from cpu_mask, ensure it can't race with cpuhp */
-	lockdep_assert_cpus_held();
-
-	/*
-	 * Setup the parameters to pass to mon_event_count() to read the data.
-	 */
+	memset(rr, 0, sizeof(*rr));
 	rr->rgrp = rdtgrp;
 	rr->evtid = evtid;
 	rr->r = r;
 	rr->d = d;
 	rr->first = first;
+	rr->ci = ci;
 	if (resctrl_arch_mbm_cntr_assign_enabled(r) &&
-	    resctrl_is_mbm_event(evtid)) {
+	    resctrl_is_mbm_event(evtid))
 		rr->is_mbm_cntr = true;
-	} else {
-		rr->arch_mon_ctx = resctrl_arch_mon_ctx_alloc(r, evtid);
+}
+
+void mon_event_read(struct rmid_read *rr, cpumask_t *cpumask)
+{
+	int cpu;
+
+	/* When picking a CPU from cpu_mask, ensure it can't race with cpuhp */
+	lockdep_assert_cpus_held();
+
+	if (!rr->is_mbm_cntr) {
+		rr->arch_mon_ctx = resctrl_arch_mon_ctx_alloc(rr->r, rr->evtid);
 		if (IS_ERR(rr->arch_mon_ctx)) {
 			rr->err = -EINVAL;
 			return;
@@ -588,7 +591,7 @@ void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
 		smp_call_on_cpu(cpu, smp_mon_event_count, rr, false);
 
 	if (rr->arch_mon_ctx)
-		resctrl_arch_mon_ctx_free(r, evtid, rr->arch_mon_ctx);
+		resctrl_arch_mon_ctx_free(rr->r, rr->evtid, rr->arch_mon_ctx);
 }
 
 int rdtgroup_mondata_show(struct seq_file *m, void *arg)
@@ -635,9 +638,9 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
 				ci = get_cpu_cacheinfo_level(cpu, RESCTRL_L3_CACHE);
 				if (!ci)
 					continue;
-				rr.ci = ci;
-				mon_event_read(&rr, r, NULL, rdtgrp,
-					       &ci->shared_cpu_map, evtid, false);
+				rmid_read_init(&rr, r, NULL, rdtgrp,
+					       evtid, false, ci);
+				mon_event_read(&rr, &ci->shared_cpu_map);
 				goto checkresult;
 			}
 		}
@@ -654,7 +657,8 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
 			goto out;
 		}
 		d = container_of(hdr, struct rdt_mon_domain, hdr);
-		mon_event_read(&rr, r, d, rdtgrp, &d->hdr.cpu_mask, evtid, false);
+		rmid_read_init(&rr, r, d, rdtgrp, evtid, false, NULL);
+		mon_event_read(&rr, &d->hdr.cpu_mask);
 	}
 
 checkresult:
fs/resctrl/internal.h +3 -2
index 63fb4d6c21a7..dcc0b7bea3ac 100644
@@ -363,9 +363,10 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg);
 int rdtgroup_mondata_open(struct kernfs_open_file *of);
 void rdtgroup_mondata_release(struct kernfs_open_file *of);
 
-void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
+void rmid_read_init(struct rmid_read *rr, struct rdt_resource *r,
 		    struct rdt_mon_domain *d, struct rdtgroup *rdtgrp,
-		    cpumask_t *cpumask, int evtid, int first);
+		    int evtid, int first, struct cacheinfo *ci);
+void mon_event_read(struct rmid_read *rr, cpumask_t *cpumask);
 
 int resctrl_mon_resource_init(void);
 
fs/resctrl/rdtgroup.c +4 -2
index 17b61dcfad07..34337abe5345 100644
@@ -3235,8 +3235,10 @@ static int mon_add_all_files(struct kernfs_node *kn, struct rdt_mon_domain *d,
 		if (ret)
 			return ret;
 
-		if (!do_sum && resctrl_is_mbm_event(mevt->evtid))
-			mon_event_read(&rr, r, d, prgrp, &d->hdr.cpu_mask, mevt->evtid, true);
+		if (!do_sum && resctrl_is_mbm_event(mevt->evtid)) {
+			rmid_read_init(&rr, r, d, prgrp, mevt->evtid, true, NULL);
+			mon_event_read(&rr, &d->hdr.cpu_mask);
+		}
 	}
 
 	return 0;
#3 lore raw

PATCH 3/8 resctrl/mon: Select cpumask before invoking mon_event_read()

In reply to Jonathan Perry

Refactor rdtgroup_mondata_show() to pick the appropriate CPU mask first and then call mon_event_read() once.

No functional change intended.

---
 fs/resctrl/ctrlmondata.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
fs/resctrl/ctrlmondata.c +7 -5
index 82f8ad2b3053..f28328c49479 100644
@@ -607,6 +607,7 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
 	struct rdt_resource *r;
 	struct cacheinfo *ci;
 	struct mon_data *md;
+	cpumask_t *cpumask;
 
 	rdtgrp = rdtgroup_kn_lock_live(of->kn);
 	if (!rdtgrp) {
@@ -639,9 +640,9 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
 				if (!ci)
 					continue;
 				rmid_read_init(&rr, r, NULL, rdtgrp,
-					       evtid, false, ci);
-				mon_event_read(&rr, &ci->shared_cpu_map);
-				goto checkresult;
+						     evtid, false, ci);
+				cpumask = &ci->shared_cpu_map;
+				goto perform;
 			}
 		}
 		ret = -ENOENT;
@@ -658,10 +659,11 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
 		}
 		d = container_of(hdr, struct rdt_mon_domain, hdr);
 		rmid_read_init(&rr, r, d, rdtgrp, evtid, false, NULL);
-		mon_event_read(&rr, &d->hdr.cpu_mask);
+		cpumask = &d->hdr.cpu_mask;
 	}
 
-checkresult:
+perform:
+	mon_event_read(&rr, cpumask);
 
 	/*
 	 * -ENOENT is a special case, set only when "mbm_event" counter assignment
#4 lore raw

PATCH 4/8 resctrl/mon: Create mon_event_setup_read() helper

In reply to Jonathan Perry

Refactor the selection of monitored event from the kernfs seq_show handler to a helper function. This provides a single setup path that the resctrl PMU will reuse.

Add mon_event_setup_read() to encapsulate domain lookup, rmid_read_init(), and selection of the valid CPU mask for the read. Rework rdtgroup_mondata_show() to call the helper before reading.

No functional change intended.

---
 fs/resctrl/ctrlmondata.c | 71 ++++++++++++++++++++++------------------
 fs/resctrl/internal.h    |  2 ++
 2 files changed, 41 insertions(+), 32 deletions(-)
fs/resctrl/ctrlmondata.c +39 -32
index f28328c49479..d1e4cf6f2128 100644
@@ -594,32 +594,16 @@ void mon_event_read(struct rmid_read *rr, cpumask_t *cpumask)
 		resctrl_arch_mon_ctx_free(rr->r, rr->evtid, rr->arch_mon_ctx);
 }
 
-int rdtgroup_mondata_show(struct seq_file *m, void *arg)
+int mon_event_setup_read(struct rmid_read *rr, cpumask_t **cpumask,
+			 struct mon_data *md, struct rdtgroup *rdtgrp)
 {
-	struct kernfs_open_file *of = m->private;
 	enum resctrl_res_level resid;
 	enum resctrl_event_id evtid;
 	struct rdt_domain_hdr *hdr;
-	struct rmid_read rr = {0};
 	struct rdt_mon_domain *d;
-	struct rdtgroup *rdtgrp;
-	int domid, cpu, ret = 0;
 	struct rdt_resource *r;
 	struct cacheinfo *ci;
-	struct mon_data *md;
-	cpumask_t *cpumask;
-
-	rdtgrp = rdtgroup_kn_lock_live(of->kn);
-	if (!rdtgrp) {
-		ret = -ENOENT;
-		goto out;
-	}
-
-	md = of->kn->priv;
-	if (WARN_ON_ONCE(!md)) {
-		ret = -EIO;
-		goto out;
-	}
+	int domid, cpu;
 
 	resid = md->rid;
 	domid = md->domid;
@@ -639,30 +623,53 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
 				ci = get_cpu_cacheinfo_level(cpu, RESCTRL_L3_CACHE);
 				if (!ci)
 					continue;
-				rmid_read_init(&rr, r, NULL, rdtgrp,
-						     evtid, false, ci);
-				cpumask = &ci->shared_cpu_map;
-				goto perform;
+				rmid_read_init(rr, r, NULL, rdtgrp,
+					       evtid, false, ci);
+				*cpumask = &ci->shared_cpu_map;
+				return 0;
 			}
 		}
-		ret = -ENOENT;
-		goto out;
+		return -ENOENT;
 	} else {
 		/*
 		 * This file provides data from a single domain. Search
 		 * the resource to find the domain with "domid".
 		 */
 		hdr = resctrl_find_domain(&r->mon_domains, domid, NULL);
-		if (!hdr || WARN_ON_ONCE(hdr->type != RESCTRL_MON_DOMAIN)) {
-			ret = -ENOENT;
-			goto out;
-		}
+		if (!hdr || WARN_ON_ONCE(hdr->type != RESCTRL_MON_DOMAIN))
+			return -ENOENT;
+
 		d = container_of(hdr, struct rdt_mon_domain, hdr);
-		rmid_read_init(&rr, r, d, rdtgrp, evtid, false, NULL);
-		cpumask = &d->hdr.cpu_mask;
+		rmid_read_init(rr, r, d, rdtgrp, evtid, false, NULL);
+		*cpumask = &d->hdr.cpu_mask;
+		return 0;
 	}
+}
 
-perform:
+int rdtgroup_mondata_show(struct seq_file *m, void *arg)
+{
+	struct kernfs_open_file *of = m->private;
+	struct rmid_read rr = {0};
+	struct rdtgroup *rdtgrp;
+	int ret = 0;
+	struct mon_data *md;
+	cpumask_t *cpumask;
+
+	rdtgrp = rdtgroup_kn_lock_live(of->kn);
+	if (!rdtgrp) {
+		ret = -ENOENT;
+		goto out;
+	}
+
+	md = of->kn->priv;
+	if (WARN_ON_ONCE(!md)) {
+		ret = -EIO;
+		goto out;
+	}
+
+	ret = mon_event_setup_read(&rr, &cpumask, md, rdtgrp);
+	if (ret)
+		goto out;
 	mon_event_read(&rr, cpumask);
 
 	/*
fs/resctrl/internal.h +2 -0
index dcc0b7bea3ac..486cbca8d0ec 100644
@@ -366,6 +366,8 @@ void rdtgroup_mondata_release(struct kernfs_open_file *of);
 void rmid_read_init(struct rmid_read *rr, struct rdt_resource *r,
 		    struct rdt_mon_domain *d, struct rdtgroup *rdtgrp,
 		    int evtid, int first, struct cacheinfo *ci);
+int mon_event_setup_read(struct rmid_read *rr, cpumask_t **cpumask,
+			 struct mon_data *md, struct rdtgroup *rdtgrp);
 void mon_event_read(struct rmid_read *rr, cpumask_t *cpumask);
 
 int resctrl_mon_resource_init(void);
#5 lore raw

PATCH 5/8 resctrl: Propagate CPU mask validation error via rr->err

In reply to Jonathan Perry

When __mon_event_count() rejects a CPU because it is not in the domain's mask (or does not match the cacheinfo domain), it returned -EINVAL but did not set rr->err. mon_event_count() then discarded the return value, which made failures harder to diagnose.

Set rr->err = -EINVAL before returning in both validation checks so the error is visible to callers and can trigger WARN_ONCE() in the PMU .read path.

---
 fs/resctrl/monitor.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/resctrl/monitor.c +6 -2
index 4076336fbba6..4d19c2ec823f 100644
@@ -445,8 +445,10 @@ static int __mon_event_count(struct rdtgroup *rdtgrp, struct rmid_read *rr)
 
 	if (rr->d) {
 		/* Reading a single domain, must be on a CPU in that domain. */
-		if (!cpumask_test_cpu(cpu, &rr->d->hdr.cpu_mask))
+		if (!cpumask_test_cpu(cpu, &rr->d->hdr.cpu_mask)) {
+			rr->err = -EINVAL;
 			return -EINVAL;
+		}
 		if (rr->is_mbm_cntr)
 			rr->err = resctrl_arch_cntr_read(rr->r, rr->d, closid, rmid, cntr_id,
 							 rr->evtid, &tval);
@@ -462,8 +464,10 @@ static int __mon_event_count(struct rdtgroup *rdtgrp, struct rmid_read *rr)
 	}
 
 	/* Summing domains that share a cache, must be on a CPU for that cache. */
-	if (!cpumask_test_cpu(cpu, &rr->ci->shared_cpu_map))
+	if (!cpumask_test_cpu(cpu, &rr->ci->shared_cpu_map)) {
+		rr->err = -EINVAL;
 		return -EINVAL;
+	}
 
 	/*
 	 * Legacy files must report the sum of an event across all
#6 lore raw

PATCH 6/8 resctrl/pmu: Introduce skeleton PMU and selftests

In reply to Jonathan Perry

Register a read-only "resctrl" PMU and implement minimal perf hooks (event_init, add, del, start, stop, read, destroy). The PMU accepts a resctrl monitoring file descriptor via attr.config, resolves the rdtgroup, and pins it for the event's lifetime.

Call PMU init/exit in resctrl_init()/resctrl_exit().

Add a selftest to exercise PMU registration and verify that only allowed monitoring files can be opened via perf.

---
 fs/resctrl/Makefile                           |   2 +-
 fs/resctrl/internal.h                         |  12 ++
 fs/resctrl/pmu.c                              | 139 ++++++++++++
 fs/resctrl/rdtgroup.c                         |  53 +++++
 tools/testing/selftests/resctrl/pmu_test.c    | 202 ++++++++++++++++++
 tools/testing/selftests/resctrl/resctrl.h     |   1 +
 .../testing/selftests/resctrl/resctrl_tests.c |   1 +
 7 files changed, 409 insertions(+), 1 deletion(-)
 create mode 100644 fs/resctrl/pmu.c
 create mode 100644 tools/testing/selftests/resctrl/pmu_test.c
fs/resctrl/Makefile +1 -1
index e67f34d2236a..f738b0165ccc 100644
@@ -1,5 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
-obj-$(CONFIG_RESCTRL_FS)		+= rdtgroup.o ctrlmondata.o monitor.o
+obj-$(CONFIG_RESCTRL_FS)		+= rdtgroup.o ctrlmondata.o monitor.o pmu.o
 obj-$(CONFIG_RESCTRL_FS_PSEUDO_LOCK)	+= pseudo_lock.o
 
 # To allow define_trace.h's recursive include:
fs/resctrl/internal.h +12 -0
index 486cbca8d0ec..b42c625569a8 100644
@@ -4,6 +4,7 @@
 
 #include <linux/resctrl.h>
 #include <linux/kernfs.h>
+#include <linux/fs.h>
 #include <linux/fs_context.h>
 #include <linux/tick.h>
 
@@ -362,6 +363,17 @@ void mon_event_count(void *info);
 int rdtgroup_mondata_show(struct seq_file *m, void *arg);
 int rdtgroup_mondata_open(struct kernfs_open_file *of);
 void rdtgroup_mondata_release(struct kernfs_open_file *of);
+void rdtgroup_get(struct rdtgroup *rdtgrp);
+void rdtgroup_put(struct rdtgroup *rdtgrp);
+
+/* PMU support */
+/*
+ * Get rdtgroup from a resctrl monitoring file and take a reference.
+ * Returns a valid pointer with an extra reference on success, or ERR_PTR on failure.
+ */
+struct rdtgroup *rdtgroup_get_from_file(struct file *file);
+int resctrl_pmu_init(void);
+void resctrl_pmu_exit(void);
 
 void rmid_read_init(struct rmid_read *rr, struct rdt_resource *r,
 		    struct rdt_mon_domain *d, struct rdtgroup *rdtgrp,
fs/resctrl/pmu.c new file +139 -0

139 lines not on this page · raw message

fs/resctrl/rdtgroup.c +53 -0

71 lines not on this page · raw message

tools/testing/selftests/resctrl/pmu_test.c new file +202 -0

202 lines not on this page · raw message

tools/testing/selftests/resctrl/resctrl.h +1 -0
index cd3adfc14969..5b0e6074eaba 100644
@@ -244,5 +244,6 @@ extern struct resctrl_test cmt_test;
 extern struct resctrl_test l3_cat_test;
 extern struct resctrl_test l3_noncont_cat_test;
 extern struct resctrl_test l2_noncont_cat_test;
+extern struct resctrl_test pmu_test;
 
 #endif /* RESCTRL_H */
tools/testing/selftests/resctrl/resctrl_tests.c +1 -0
index 5154ffd821c4..11ba9000e015 100644
@@ -21,6 +21,7 @@ static struct resctrl_test *resctrl_tests[] = {
 	&l3_cat_test,
 	&l3_noncont_cat_test,
 	&l2_noncont_cat_test,
+	&pmu_test,
 };
 
 static int detect_vendor(void)
#7 lore raw

PATCH 7/8 resctrl/pmu: Use mon_event_setup_read() and validate CPU

In reply to Jonathan Perry

During event_init, extract mon_data from the monitoring file and call mon_event_setup_read() to prepare rmid_read and the valid CPU mask for that file. Require a CPU-bound event and verify the bound CPU is in the mask.

Store the prepared rmid_read and CPU mask in the event private data along with the pinned rdtgroup.

Split the helper that gets the pinned rdtgroup in two, so event_init can get the mon_data from kernfs_open_file:
- rdtgroup_get_mondata_open_file() gets kernfs_open_file from file
- rdtgroup_get_from_mondata_file() gets pinned rdtgroup from kernfs_open_file

Extend the selftest to test CPU validation and verify that pid-bound events are rejected.

---
 fs/resctrl/internal.h                      |   2 +
 fs/resctrl/pmu.c                           |  59 +++++++-
 fs/resctrl/rdtgroup.c                      |  24 ++-
 tools/testing/selftests/resctrl/pmu_test.c | 164 ++++++++++++++++++---
 4 files changed, 214 insertions(+), 35 deletions(-)
fs/resctrl/internal.h +2 -0
index b42c625569a8..8cc3a3747c2f 100644
@@ -365,6 +365,8 @@ int rdtgroup_mondata_open(struct kernfs_open_file *of);
 void rdtgroup_mondata_release(struct kernfs_open_file *of);
 void rdtgroup_get(struct rdtgroup *rdtgrp);
 void rdtgroup_put(struct rdtgroup *rdtgrp);
+struct kernfs_open_file *rdtgroup_get_mondata_open_file(struct file *file);
+struct rdtgroup *rdtgroup_get_from_mondata_file(struct kernfs_open_file *of);
 
 /* PMU support */
 /*
fs/resctrl/pmu.c +55 -4

97 lines not on this page · raw message

fs/resctrl/rdtgroup.c +17 -7
index 4f4139edafbf..ed7d9feccd94 100644
@@ -3429,19 +3429,16 @@ void rdtgroup_mondata_release(struct kernfs_open_file *of)
 }
 
 /*
- * rdtgroup_get_from_file - Resolve rdtgroup from a resctrl mon data file
+ * Resolve kernfs_open_file from a resctrl mon data file.
  * @file: struct file opened on a resctrl monitoring data file
  *
  * Validate that @file belongs to resctrl and refers to a monitoring data
- * file (kf_mondata_ops). Then, using the kernfs_open_file stored in the
- * seq_file, safely fetch the rdtgroup that was pinned at open time and take
- * an additional rdtgroup reference for the caller under rdtgroup_mutex.
+ * file (kf_mondata_ops).
  *
- * Returns: rdtgroup* with an extra reference on success; ERR_PTR on failure.
+ * Returns: kernfs_open_file* on success; ERR_PTR on failure.
  */
-struct rdtgroup *rdtgroup_get_from_file(struct file *file)
+struct kernfs_open_file *rdtgroup_get_mondata_open_file(struct file *file)
 {
-	struct rdtgroup *rdtgrp = NULL;
 	struct kernfs_open_file *of;
 	struct seq_file *seq;
 	struct inode *inode;
@@ -3464,6 +3461,19 @@ struct rdtgroup *rdtgroup_get_from_file(struct file *file)
 	if (!of || !of->kn || of->kn->attr.ops != &kf_mondata_ops)
 		return ERR_PTR(-EINVAL);
 
+	return of;
+}
+
+/*
+ * Get rdtgroup from a resctrl mon data open file.
+ * @of: kernfs_open_file opened on a resctrl monitoring data file
+ *
+ * Returns: rdtgroup* with an extra reference on success; ERR_PTR on failure.
+ */
+struct rdtgroup *rdtgroup_get_from_mondata_file(struct kernfs_open_file *of)
+{
+	struct rdtgroup *rdtgrp = NULL;
+
 	/* Hold rdtgroup_mutex to prevent race with release callback */
 	guard(mutex)(&rdtgroup_mutex);
 
tools/testing/selftests/resctrl/pmu_test.c +140 -24

205 lines not on this page · raw message

#8 lore raw

PATCH 8/8 resctrl/pmu: Implement .read via direct RMID read; add LLC selftest

In reply to Jonathan Perry

Implement reads of monitored values in resctrl_event_update().

If the rdtgroup was deleted, read zero. On RMID read errors, emit a WARN_ONCE and read zero.

Introduce mon_event_read_this_cpu() to call mon_event_count() directly, with no IPI, as perf infra would ensure .read runs on the bound CPU.

Augment the existing LLC occupancy selftest to also test the PMU.

Document PMU usage in resctrl.rst.

---
 Documentation/filesystems/resctrl.rst       | 64 ++++++++++++++
 fs/resctrl/ctrlmondata.c                    | 17 ++++
 fs/resctrl/internal.h                       |  1 +
 fs/resctrl/pmu.c                            | 31 ++++++-
 tools/testing/selftests/resctrl/cache.c     | 94 ++++++++++++++++++++-
 tools/testing/selftests/resctrl/cmt_test.c  | 17 +++-
 tools/testing/selftests/resctrl/pmu_test.c  | 28 +-----
 tools/testing/selftests/resctrl/pmu_utils.c | 32 +++++++
 tools/testing/selftests/resctrl/resctrl.h   |  3 +
 9 files changed, 253 insertions(+), 34 deletions(-)
 create mode 100644 tools/testing/selftests/resctrl/pmu_utils.c
Documentation/filesystems/resctrl.rst +64 -0
index b7f35b07876a..8f91ba7d622b 100644
@@ -628,6 +628,70 @@ Resource monitoring rules
    "mon_data" group.
 
 
+Perf PMU access (resctrl PMU)
+=============================
+
+resctrl registers a perf PMU named "resctrl", which provides read-only access
+to the same monitoring values exposed via the "mon_data" files. The PMU
+enables access from eBPF and allows parallelizing reads as it does not lock
+the system-wide `rdtgroup_mutex`.
+
+Selection and usage
+-------------------
+- Event selection is performed by passing the file descriptor of a
+  resctrl monitoring file, for example
+  "mon_data/mon_L3_00/llc_occupancy", in the `perf_event_attr.config`
+  field when calling perf_event_open().
+- The perf event must be CPU-bound (pid = -1 and cpu >= 0).
+- The chosen CPU must be valid for the domain represented by the
+  monitoring file:
+
+  - For a domain-specific file such as "mon_L3_00/...", choose a CPU that
+    belongs to that domain.
+  - For files that provide a sum across domains that share the same L3
+    cache instance (for example on SNC systems), choose a CPU that
+    shares that L3 cache instance. See the "Cache IDs" section for the
+    concepts and mapping.
+- Exclude flags must be zero. perf_event_open() fails if any exclude
+  flags are set.
+
+Semantics
+---------
+- The values from the resctrl PMU match the values what would be
+  read from the corresponding "mon_data" file at the time of the read.
+- Sampling is not supported. The PMU provides counts that can be read
+  on demand; there are no periodic interrupts or per-context filtering
+  semantics.
+- It is safe to read a perf event whose underlying resctrl group has been
+  deleted. However, the returned values are unspecified: the current
+  implementation returns zeros, but this may change in the future.
+
+Discovering the PMU and example
+-------------------------------
+- The PMU type is exposed at
+  "/sys/bus/event_source/devices/resctrl/type" and must be placed in
+  `perf_event_attr.type`.
+- A minimal example of opening a resctrl PMU event by passing a
+  monitoring file descriptor in `config`::
+
+      int pmu_type = read_int("/sys/bus/event_source/devices/resctrl/type");
+      int mon_fd = open("/sys/fs/resctrl/mon_data/mon_L3_00/llc_occupancy", O_RDONLY);
+      struct perf_event_attr pe = { 0 };
+
+      pe.type = pmu_type;
+      pe.size = sizeof(pe);
+      pe.config = mon_fd;  /* select event via resctrl file descriptor */
+      pe.disabled = 1;
+
+      int cpu = /* a CPU in the L3_00 domain (see Cache IDs) */;
+      int fd = perf_event_open(&pe, -1 /* pid */, cpu /* cpu */, -1, 0);
+
+      ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
+      uint64_t val;
+      read(fd, &val, sizeof(val));
+      ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
+
+
 Notes on cache occupancy monitoring and control
 ===============================================
 When moving a task from one group to another you should remember that
fs/resctrl/ctrlmondata.c +17 -0
index d1e4cf6f2128..02f8f256c680 100644
@@ -594,6 +594,23 @@ void mon_event_read(struct rmid_read *rr, cpumask_t *cpumask)
 		resctrl_arch_mon_ctx_free(rr->r, rr->evtid, rr->arch_mon_ctx);
 }
 
+void mon_event_read_this_cpu(struct rmid_read *rr)
+{
+	/* Ensure we're not in a CPU hotplug race */
+	lockdep_assert_cpus_held();
+
+	rr->arch_mon_ctx = resctrl_arch_mon_ctx_alloc(rr->r, rr->evtid);
+	if (IS_ERR(rr->arch_mon_ctx)) {
+		rr->err = -EINVAL;
+		return;
+	}
+
+	/* Direct call on current CPU - no IPI needed */
+	mon_event_count(rr);
+
+	resctrl_arch_mon_ctx_free(rr->r, rr->evtid, rr->arch_mon_ctx);
+}
+
 int mon_event_setup_read(struct rmid_read *rr, cpumask_t **cpumask,
 			 struct mon_data *md, struct rdtgroup *rdtgrp)
 {
fs/resctrl/internal.h +1 -0
index 8cc3a3747c2f..c4adb6eaf101 100644
@@ -383,6 +383,7 @@ void rmid_read_init(struct rmid_read *rr, struct rdt_resource *r,
 int mon_event_setup_read(struct rmid_read *rr, cpumask_t **cpumask,
 			 struct mon_data *md, struct rdtgroup *rdtgrp);
 void mon_event_read(struct rmid_read *rr, cpumask_t *cpumask);
+void mon_event_read_this_cpu(struct rmid_read *rr);
 
 int resctrl_mon_resource_init(void);
 
fs/resctrl/pmu.c +29 -2
index bdca0b3a5b0b..e6f5f13f29d2 100644
@@ -132,8 +132,35 @@ static void resctrl_event_destroy(struct perf_event *event)
 
 static void resctrl_event_update(struct perf_event *event)
 {
-	/* Currently just a stub - would read actual cache occupancy here */
-	local64_set(&event->hw.prev_count, 0);
+	struct resctrl_pmu_event *resctrl_event = event->pmu_private;
+	struct rdtgroup *rdtgrp = resctrl_event->rdtgrp;
+	struct rmid_read rr;
+	u64 value = 0;
+
+	/* Check if rdtgroup has been deleted */
+	if (rdtgrp->flags & RDT_DELETED) {
+		local64_set(&event->count, 0);
+		return;
+	}
+
+	/* Setup rmid_read structure with current parameters */
+	rr = resctrl_event->rr;
+	rr.val = 0;
+	rr.err = 0;
+
+	/* Take cpus read lock only around the actual RMID read */
+	cpus_read_lock();
+	mon_event_read_this_cpu(&rr);
+	cpus_read_unlock();
+
+	/* Update counter value based on read result */
+	if (!rr.err)
+		value = rr.val;
+	else
+		WARN_ONCE(1, "resctrl PMU: RMID read error (err=%d) for closid=%u, rmid=%u, evtid=%d\n",
+			  rr.err, rdtgrp->closid, rdtgrp->mon.rmid, rr.evtid);
+
+	local64_set(&event->count, value);
 }
 
 static void resctrl_event_start(struct perf_event *event, int flags)
tools/testing/selftests/resctrl/cache.c +91 -3

128 lines not on this page · raw message

tools/testing/selftests/resctrl/cmt_test.c +15 -2
index d09e693dc739..28250903bbf0 100644
@@ -78,6 +78,7 @@ static int check_results(struct resctrl_val_param *param, size_t span, int no_of
 {
 	char *token_array[8], temp[512];
 	unsigned long sum_llc_occu_resc = 0;
+	unsigned long sum_llc_occu_pmu = 0;
 	int runs = 0;
 	FILE *fp;
 
@@ -100,12 +101,24 @@ static int check_results(struct resctrl_val_param *param, size_t span, int no_of
 
 		/* Field 3 is llc occ resc value */
 		sum_llc_occu_resc += strtoul(token_array[3], NULL, 0);
+
+		/* Field 5: llc occupancy from PMU */
+		sum_llc_occu_pmu += strtoul(token_array[5], NULL, 0);
 		runs++;
 	}
 	fclose(fp);
 
-	return show_results_info(sum_llc_occu_resc, no_of_bits, span,
-				 MAX_DIFF, MAX_DIFF_PERCENT, runs, true);
+	/* Filesystem-based results */
+	ksft_print_msg("CMT (resctrl fs):\n");
+	int ret_fs = show_results_info(sum_llc_occu_resc, no_of_bits, span,
+				       MAX_DIFF, MAX_DIFF_PERCENT, runs, true);
+
+	/* PMU-based results */
+	ksft_print_msg("CMT (PMU):\n");
+	int ret_pmu = show_results_info(sum_llc_occu_pmu, no_of_bits, span,
+					MAX_DIFF, MAX_DIFF_PERCENT, runs, true);
+
+	return ret_fs || ret_pmu;
 }
 
 static void cmt_test_cleanup(void)
tools/testing/selftests/resctrl/pmu_test.c +1 -27
index fb3eec721e43..e4d75a8c0a6c 100644
@@ -16,32 +16,6 @@
 
 #define RESCTRL_PMU_NAME "resctrl"
 
-static int find_pmu_type(const char *pmu_name)
-{
-	char path[256];
-	FILE *file;
-	int type;
-
-	snprintf(path, sizeof(path), "/sys/bus/event_source/devices/%s/type",
-		 pmu_name);
-
-	file = fopen(path, "r");
-	if (!file) {
-		ksft_print_msg("Failed to open %s: %s\n", path,
-			       strerror(errno));
-		return -1;
-	}
-
-	if (fscanf(file, "%d", &type) != 1) {
-		ksft_print_msg("Failed to read PMU type from %s\n", path);
-		fclose(file);
-		return -1;
-	}
-
-	fclose(file);
-	return type;
-}
-
 static bool is_allowed_file(const char *filename)
 {
 	const char *base;
@@ -288,7 +262,7 @@ static int pmu_run_test(const struct resctrl_test *test,
 	ksft_print_msg("Testing resctrl PMU file access safety\n");
 
 	/* Find the resctrl PMU type */
-	pmu_type = find_pmu_type(RESCTRL_PMU_NAME);
+	pmu_type = resctrl_find_pmu_type(RESCTRL_PMU_NAME);
 	if (pmu_type < 0) {
 		ksft_print_msg("Resctrl PMU not found - PMU is not registered?\n");
 		return -1;
tools/testing/selftests/resctrl/pmu_utils.c new file +32 -0
new file mode 100644
index 000000000000..2d65d8b6e9e3
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "resctrl.h"
+#include <stdio.h>
+
+int resctrl_find_pmu_type(const char *pmu_name)
+{
+	char path[256];
+	FILE *file;
+	int type;
+
+	if (!pmu_name)
+		return -1;
+
+	snprintf(path, sizeof(path), "/sys/bus/event_source/devices/%s/type",
+		 pmu_name);
+
+	file = fopen(path, "r");
+	if (!file) {
+		ksft_print_msg("Failed to open %s: %s\n", path, strerror(errno));
+		return -1;
+	}
+
+	if (fscanf(file, "%d", &type) != 1) {
+		ksft_print_msg("Failed to read PMU type from %s\n", path);
+		fclose(file);
+		return -1;
+	}
+
+	fclose(file);
+	return type;
+}
tools/testing/selftests/resctrl/resctrl.h +3 -0
index 5b0e6074eaba..d1d2891081cf 100644
@@ -205,6 +205,9 @@ void signal_handler_unregister(void);
 unsigned int count_bits(unsigned long n);
 int snc_kernel_support(void);
 
+/* PMU utilities */
+int resctrl_find_pmu_type(const char *pmu_name);
+
 void perf_event_attr_initialize(struct perf_event_attr *pea, __u64 config);
 void perf_event_initialize_read_format(struct perf_event_read *pe_read);
 int perf_open(struct perf_event_attr *pea, pid_t pid, int cpu_no);
maintainer
#9 lore raw
In reply to Jonathan Perry
Quoted 8 lines · Jonathan Perry

Motivation: perf support enables measuring cache occupancy and memory bandwidth metrics on hrtimer (high resolution timer) interrupts via eBPF. Compared with polling from userspace, hrtimer-based reads remove scheduling jitter and context switch overhead. Further, PMU reads can be parallel, since the PMU read path need not lock resctrl's rdtgroup_mutex. Parallelization and reduced jitter enable more accurate snapshots of cache occupancy and memory bandwidth. [1] has more details on the motivation and design.

This parallel read without rdtgroup_mutex looks worrying.

The h/w counters have limited width (24-bits on older Intel CPUs, 32-bits on AMD and Intel >= Icelake). So resctrl takes the raw value and in get_corrected_val() figures the increment since the previous read of the MSR to figure out how much to add to the running per-RMID count of "chunks".

That's all inherently full of races. If perf does this at the same time that resctrl does, then things will be corrupted sooner or later.

You might fix it with a per-RMID spinlock in "struct arch_mbm_state"?

-Tony

maintainer
#10 lore raw
Quoted 17 lines
Quoted 8 lines

Motivation: perf support enables measuring cache occupancy and memory bandwidth metrics on hrtimer (high resolution timer) interrupts via eBPF. Compared with polling from userspace, hrtimer-based reads remove scheduling jitter and context switch overhead. Further, PMU reads can be parallel, since the PMU read path need not lock resctrl's rdtgroup_mutex. Parallelization and reduced jitter enable more accurate snapshots of cache occupancy and memory bandwidth. [1] has more details on the motivation and design.

This parallel read without rdtgroup_mutex looks worrying.

The h/w counters have limited width (24-bits on older Intel CPUs, 32-bits on AMD and Intel >= Icelake). So resctrl takes the raw value and in get_corrected_val() figures the increment since the previous read of the MSR to figure out how much to add to the running per-RMID count of "chunks".

Quoted 5 lines

That's all inherently full of races. If perf does this at the same time that resctrl does, then things will be corrupted sooner or later.

You might fix it with a per-RMID spinlock in "struct arch_mbm_state"?

That might be too fine a locking granularity. You'd probably be fine with little contention with a lock in "struct rdt_mon_domain".

-Tony

#11 lore raw
Quoted 17 lines
Quoted 17 lines
Quoted 8 lines

Motivation: perf support enables measuring cache occupancy and memory bandwidth metrics on hrtimer (high resolution timer) interrupts via eBPF. Compared with polling from userspace, hrtimer-based reads remove scheduling jitter and context switch overhead. Further, PMU reads can be parallel, since the PMU read path need not lock resctrl's rdtgroup_mutex. Parallelization and reduced jitter enable more accurate snapshots of cache occupancy and memory bandwidth. [1] has more details on the motivation and design.

This parallel read without rdtgroup_mutex looks worrying.

The h/w counters have limited width (24-bits on older Intel CPUs, 32-bits on AMD and Intel >= Icelake). So resctrl takes the raw value and in get_corrected_val() figures the increment since the previous read of the MSR to figure out how much to add to the running per-RMID count of "chunks".

Quoted 8 lines
Quoted 5 lines

That's all inherently full of races. If perf does this at the same time that resctrl does, then things will be corrupted sooner or later.

You might fix it with a per-RMID spinlock in "struct arch_mbm_state"?

That might be too fine a locking granularity. You'd probably be fine with little contention with a lock in "struct rdt_mon_domain".

Good catch. Thank you Tony!

We might be able to solve the issue similarly to what adding a per-RMID spinlock in "struct arch_mbm_state" would do, but with only a memory barrier (no spinlock). I'll look further into it.

-Jonathan