[PATCH 07/12] arm_mpam: resctrl: Support the default counter assignment mode
From: Jinjie Ruan
Date: Sun Nov 29 2026 - 22:25:25 EST
MPAM's bandwidth counters are a shared pool: the "mbm_event" assignment
mode takes a counter from the pool when a monitoring group is created,
and reads fail with -ENOENT once the pool is empty, which resctrl
reports as "Unavailable".
resctrl also has a default assignment mode, where every monitoring group
is expected to have a counter, as x86 systems without assignable
counters always provide. x86 systems with assignable counters can
switch between the modes with the mbm_assign_mode file.
Let MPAM switch too: the driver reports the mode as configurable and
implements resctrl_arch_mbm_cntr_assign_set(). Switching to the default
mode gives every group one of the counters allocated at init: the
CLOSIDs and the RMIDs per CLOSID are capped to the number of counters,
shrinking the RMIDs first and keeping one per CLOSID for the default
monitor group, so that every group in the space has a monitor. The map
the reads and resets go through is filled for the whole capped space,
and cleared when the mode changes, as the "mbm_event" mode fills it
when a counter is assigned.
While the default mode is used, resctrl_arch_get_num_closid() and the
RMID count report the capped values, and resctrl_arch_rmid_valid()
refuses the RMIDs above the cap.
resctrl_arch_rmid_read() and resctrl_arch_reset_rmid() read and reset
the bandwidth counter of the (closid, rmid) pair through the map,
instead of only the occupancy counters.
Groups that exist when the mode is switched and are beyond the cap
cannot be read until the mode is switched back. Everything else is
unchanged: the mode at boot is still "mbm_event", and the assignment
interfaces only work there.
A single bandwidth counter leaves the default group as the only one
that can be measured, which the driver warns about when the mode is
switched.
Document the mode and its lower numbers in the resctrl documentation.
Suggested-by: Ben Horgan <ben.horgan@xxxxxxx>
Signed-off-by: Jinjie Ruan <ruanjinjie@xxxxxxxxxx>
---
Documentation/filesystems/resctrl.rst | 7 ++
drivers/resctrl/mpam_resctrl.c | 154 +++++++++++++++++++++++---
include/linux/arm_mpam.h | 6 +-
3 files changed, 144 insertions(+), 23 deletions(-)
diff --git a/Documentation/filesystems/resctrl.rst b/Documentation/filesystems/resctrl.rst
index 2750f2eabf70..5bec3d2197ac 100644
--- a/Documentation/filesystems/resctrl.rst
+++ b/Documentation/filesystems/resctrl.rst
@@ -385,6 +385,13 @@ with the following files:
In default mode, resctrl assumes there is a hardware counter for each
event within every CTRL_MON and MON group.
+ On an architecture that assigns the counters from a shared pool, such
+ as MPAM, switching to the default mode gives every monitoring group
+ one of the counters. The number of CLOSIDs and of RMIDs is then
+ limited to the number of counters: "num_rmids" reports the limited
+ number, "num_closids" keeps the value from the mount, and monitoring
+ groups beyond them cannot be read until the mode is switched back.
+
* To enable "mbm_event" counter assignment mode:
::
diff --git a/drivers/resctrl/mpam_resctrl.c b/drivers/resctrl/mpam_resctrl.c
index dd5927a16b24..3ae3e9253a02 100644
--- a/drivers/resctrl/mpam_resctrl.c
+++ b/drivers/resctrl/mpam_resctrl.c
@@ -62,6 +62,20 @@ static DEFINE_MUTEX(domain_list_lock);
*/
static bool cdp_enabled;
+/*
+ * The CLOSIDs and the RMIDs per CLOSID resctrl can use while
+ * the default counter assignment mode is used.
+ */
+static u32 mpam_capped_num_closid;
+static u32 mpam_capped_num_rmid;
+
+/*
+ * True while the L3 resource uses the "mbm_event" counter assignment mode.
+ * The mode at boot is "mbm_event"; on a system without the counters the
+ * hook is gated by mbm_cntr_assignable instead.
+ */
+static bool mbm_cntr_assign_enabled = true;
+
/*
* We use cacheinfo to discover the size of the caches and their id. cacheinfo
* populates this from a device_initcall(). mpam_resctrl_setup() must wait.
@@ -114,19 +128,10 @@ void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_l3_mon_domai
{
}
-void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_l3_mon_domain *d,
- u32 closid, u32 rmid, enum resctrl_event_id eventid)
-{
-}
-
bool resctrl_arch_mbm_cntr_assign_enabled(struct rdt_resource *r)
{
- return (r == &mpam_resctrl_controls[RDT_RESOURCE_L3].resctrl_res);
-}
-
-int resctrl_arch_mbm_cntr_assign_set(struct rdt_resource *r, bool enable)
-{
- return -EINVAL;
+ return mbm_cntr_assign_enabled && r->mon.mbm_cntr_assignable &&
+ (r == &mpam_resctrl_controls[RDT_RESOURCE_L3].resctrl_res);
}
int resctrl_arch_io_alloc_enable(struct rdt_resource *r, bool enable)
@@ -191,6 +196,22 @@ static void mpam_resctrl_monitor_sync_abmc_vals(struct rdt_resource *l3)
*/
}
+/* CDP halves the CLOSIDs, and the default assignment mode caps both. */
+static void mpam_resctrl_set_num_rmid(void)
+{
+ struct rdt_resource *l3 = &mpam_resctrl_controls[RDT_RESOURCE_L3].resctrl_res;
+ u32 num_rmid = mbm_cntr_assign_enabled ? mpam_pmg_max + 1 : mpam_capped_num_rmid;
+ u32 num_closid = 1;
+
+ if (resctrl_arch_alloc_capable()) {
+ num_closid = resctrl_arch_get_num_closid(NULL);
+ if (cdp_enabled)
+ num_closid /= 2;
+ }
+
+ l3->mon.num_rmid = num_rmid * num_closid;
+}
+
int resctrl_arch_set_cdp_enabled(enum resctrl_res_level rid, bool enable)
{
u32 partid_i = RESCTRL_RESERVED_CLOSID, partid_d = RESCTRL_RESERVED_CLOSID;
@@ -271,7 +292,7 @@ static bool mpam_resctrl_hide_cdp(enum resctrl_res_level rid)
*/
u32 resctrl_arch_get_num_closid(struct rdt_resource *ignored)
{
- return mpam_partid_max + 1;
+ return mbm_cntr_assign_enabled ? mpam_partid_max + 1 : mpam_capped_num_closid;
}
u32 resctrl_arch_system_num_rmid_idx(void)
@@ -290,6 +311,15 @@ void resctrl_arch_rmid_idx_decode(u32 idx, u32 *closid, u32 *rmid)
*rmid = idx % (mpam_pmg_max + 1);
}
+/*
+ * The CLOSIDs are capped by resctrl_arch_get_num_closid(), and every CLOSID
+ * has the same RMIDs, so @closid does not matter.
+ */
+bool resctrl_arch_rmid_valid(u32 closid, u32 rmid)
+{
+ return mbm_cntr_assign_enabled || rmid < mpam_capped_num_rmid;
+}
+
void resctrl_arch_sched_in(struct task_struct *tsk)
{
lockdep_assert_preemption_disabled();
@@ -510,7 +540,10 @@ static int read_mon_cdp_safe(struct mpam_resctrl_mon *mon, struct mpam_component
CDP_NONE, closid, rmid, val);
}
-/* MBWU when not in ABMC mode (not supported), and CSU counters. */
+/*
+ * CSU counters, and MBWU counters through the per-group mapping while the
+ * default assignment mode is used.
+ */
int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_domain_hdr *hdr,
u32 closid, u32 rmid, enum resctrl_event_id eventid,
void *arch_priv, u64 *val, void *arch_mon_ctx)
@@ -532,10 +565,10 @@ int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_domain_hdr *hdr,
l3_dom = container_of(hdr, struct mpam_resctrl_dom, resctrl_mon_dom.hdr);
mon_comp = l3_dom->mon_comp[eventid];
- if (eventid != QOS_L3_OCCUP_EVENT_ID)
- return -EINVAL;
-
- mon_type = mpam_feat_msmon_csu;
+ if (eventid == QOS_L3_OCCUP_EVENT_ID)
+ mon_type = mpam_feat_msmon_csu;
+ else
+ mon_type = mpam_feat_msmon_mbwu;
return read_mon_cdp_safe(mon, mon_comp, mon_type, mon_idx,
closid, rmid, val);
@@ -598,6 +631,25 @@ static void reset_mon_cdp_safe(struct mpam_resctrl_mon *mon, struct mpam_compone
}
}
+void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_l3_mon_domain *d,
+ u32 closid, u32 rmid, enum resctrl_event_id eventid)
+{
+ struct mpam_resctrl_mon *mon = &mpam_resctrl_counters[eventid];
+ struct mpam_resctrl_dom *l3_dom;
+ struct mpam_component *mon_comp;
+
+ if (!mpam_is_enabled())
+ return;
+
+ if (eventid == QOS_L3_OCCUP_EVENT_ID || !mon->class)
+ return;
+
+ l3_dom = container_of(d, struct mpam_resctrl_dom, resctrl_mon_dom);
+ mon_comp = l3_dom->mon_comp[eventid];
+
+ __reset_mon(mon, mon_comp, USE_PRE_ALLOCATED, CDP_NONE, closid, rmid);
+}
+
/* Reset an assigned counter */
void resctrl_arch_reset_cntr(struct rdt_resource *r, struct rdt_l3_mon_domain *d,
u32 closid, u32 rmid, int cntr_id,
@@ -1229,6 +1281,72 @@ static void mpam_resctrl_pick_counters(void)
}
}
+static void mpam_resctrl_map_mbwu_mons(bool default_mode)
+{
+ struct mpam_resctrl_mon *mon = &mpam_resctrl_counters[QOS_L3_MBM_TOTAL_EVENT_ID];
+ u32 num_grp = mpam_capped_num_closid * mpam_capped_num_rmid;
+ u32 num_idx = resctrl_arch_system_num_rmid_idx();
+
+ if (!mon->mbwu_idx_to_mon || !mon->assigned_counters)
+ return;
+
+ memset(mon->mbwu_idx_to_mon, -1, num_idx * sizeof(*mon->mbwu_idx_to_mon));
+
+ if (!default_mode)
+ return;
+
+ for (u32 i = 0; i < num_grp; i++) {
+ u32 closid = i / mpam_capped_num_rmid;
+ u32 rmid = i % mpam_capped_num_rmid;
+ int mbwu_idx = resctrl_arch_rmid_idx_encode(closid, rmid);
+
+ mon->mbwu_idx_to_mon[mbwu_idx] = mon->assigned_counters[i];
+ }
+}
+
+static void mpam_resctrl_cap_closid_rmid(u32 num_mon, u32 *num_closid,
+ u32 *num_rmid)
+{
+ *num_closid = min(*num_closid, num_mon);
+ *num_rmid = clamp(num_mon / *num_closid, 1U, *num_rmid);
+}
+
+int resctrl_arch_mbm_cntr_assign_set(struct rdt_resource *r, bool enable)
+{
+ struct mpam_resctrl_mon *mon = &mpam_resctrl_counters[QOS_L3_MBM_TOTAL_EVENT_ID];
+ struct rdt_resource *l3 = &mpam_resctrl_controls[RDT_RESOURCE_L3].resctrl_res;
+ u32 num_closid = mpam_partid_max + 1;
+ u32 num_rmid = mpam_pmg_max + 1;
+
+ lockdep_assert_cpus_held();
+
+ if (r != l3 || !r->mon.mbm_cntr_assignable)
+ return -EINVAL;
+
+ if (enable == mbm_cntr_assign_enabled)
+ return 0;
+
+ if (!enable) {
+ if (mon->class) {
+ mpam_resctrl_cap_closid_rmid(mon->class->props.num_mbwu_mon,
+ &num_closid, &num_rmid);
+
+ if (mon->class->props.num_mbwu_mon < 2)
+ pr_warn("MPAM: only %u bandwidth counter, so only the default group can be measured\n",
+ mon->class->props.num_mbwu_mon);
+ }
+
+ mpam_capped_num_closid = num_closid;
+ mpam_capped_num_rmid = num_rmid;
+ }
+
+ mpam_resctrl_map_mbwu_mons(!enable);
+ mbm_cntr_assign_enabled = enable;
+ mpam_resctrl_set_num_rmid();
+
+ return 0;
+}
+
static void __config_cntr(struct mpam_resctrl_mon *mon, u32 cntr_id,
enum resctrl_conf_type cdp_type, u32 closid, u32 rmid,
bool assign)
@@ -1383,7 +1501,7 @@ static int mpam_resctrl_monitor_init_abmc(struct mpam_resctrl_mon *mon)
l3->mon.mbm_cntr_assignable = true;
l3->mon.mbm_assign_on_mkdir = true;
l3->mon.mbm_cntr_configurable = false;
- l3->mon.mbm_cntr_assign_fixed = true;
+ l3->mon.mbm_cntr_assign_fixed = false;
mpam_resctrl_monitor_sync_abmc_vals(l3);
diff --git a/include/linux/arm_mpam.h b/include/linux/arm_mpam.h
index 76c192302bc6..eb535bb8c104 100644
--- a/include/linux/arm_mpam.h
+++ b/include/linux/arm_mpam.h
@@ -63,11 +63,7 @@ u32 resctrl_arch_rmid_idx_encode(u32 closid, u32 rmid);
void resctrl_arch_rmid_idx_decode(u32 idx, u32 *closid, u32 *rmid);
u32 resctrl_arch_system_num_rmid_idx(void);
-/* MPAM does not limit the RMIDs yet. */
-static inline bool resctrl_arch_rmid_valid(u32 closid, u32 rmid)
-{
- return true;
-}
+bool resctrl_arch_rmid_valid(u32 closid, u32 rmid);
struct rdt_resource;
void *resctrl_arch_mon_ctx_alloc(struct rdt_resource *r, enum resctrl_event_id evtid);
--
2.34.1