[PATCH 06/12] x86,fs/resctrl: Let the architecture refuse RMIDs

Jinjie Ruan ruanjinjie at huawei.com
Sun Nov 29 19:24:41 PST 2026


resctrl allocates an RMID by walking a free list over the whole rmid_idx
space. An architecture that assigns the counters from a shared pool caps
the monitoring groups to the number of counters while the default
assignment mode is used, and needs a way to tell resctrl which RMIDs are
beyond the cap. The rmid_idx space itself cannot change: rmid_ptrs[] is
set up on the first mount, and RMIDs in limbo outlive an unmount.

Add resctrl_arch_rmid_valid() and skip the RMIDs the architecture
refuses in resctrl_find_free_rmid().

resctrl_find_cleanest_closid() only considers the CLOSIDs that can
allocate one of the remaining RMIDs, so a CLOSID whose allowed RMIDs
are all in limbo does not hide another one that has a free RMID. When
no CLOSID can, the first unusable one is returned, and alloc_rmid()
then reports why the RMID allocation failed. A CLOSID with no RMID in
limbo has all its RMIDs on the free list, so it is taken without
scanning the list.

The hook is a no-op on x86 and, for now, on MPAM.

Signed-off-by: Jinjie Ruan <ruanjinjie at huawei.com>
---
 arch/x86/include/asm/resctrl.h |  6 ++++++
 fs/resctrl/monitor.c           | 29 +++++++++++++++++++++++------
 include/linux/arm_mpam.h       |  6 ++++++
 3 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/arch/x86/include/asm/resctrl.h b/arch/x86/include/asm/resctrl.h
index 8f6edcdcfd87..0cd5e17857a4 100644
--- a/arch/x86/include/asm/resctrl.h
+++ b/arch/x86/include/asm/resctrl.h
@@ -181,6 +181,12 @@ static inline u32 resctrl_arch_rmid_idx_encode(u32 ignored, u32 rmid)
 	return rmid;
 }
 
+/* x86 can use every RMID */
+static inline bool resctrl_arch_rmid_valid(u32 closid, u32 rmid)
+{
+	return true;
+}
+
 /* x86 can always read an rmid, nothing needs allocating */
 struct rdt_resource;
 static inline void *resctrl_arch_mon_ctx_alloc(struct rdt_resource *r,
diff --git a/fs/resctrl/monitor.c b/fs/resctrl/monitor.c
index 6da838a41200..b9896a0df35b 100644
--- a/fs/resctrl/monitor.c
+++ b/fs/resctrl/monitor.c
@@ -216,12 +216,13 @@ static struct rmid_entry *resctrl_find_free_rmid(u32 closid)
 		 * to be if it were used with this CLOSID.
 		 * If the CLOSID is irrelevant on this architecture, the two
 		 * index values are always the same on every entry and thus the
-		 * very first entry will be returned.
+		 * very first entry will be returned. Skip the RMIDs the
+		 * architecture does not allow.
 		 */
 		itr_idx = resctrl_arch_rmid_idx_encode(itr->closid, itr->rmid);
 		cmp_idx = resctrl_arch_rmid_idx_encode(closid, itr->rmid);
 
-		if (itr_idx == cmp_idx)
+		if (itr_idx == cmp_idx && resctrl_arch_rmid_valid(closid, itr->rmid))
 			return itr;
 	}
 
@@ -235,7 +236,9 @@ static struct rmid_entry *resctrl_find_free_rmid(u32 closid)
  *
  * MPAM's equivalent of RMID are per-CLOSID, meaning a freshly allocated CLOSID
  * may not be able to allocate clean RMID. To avoid this the allocator will
- * choose the CLOSID with the most clean RMID.
+ * choose the CLOSID with the most clean RMID. A CLOSID the architecture
+ * cannot allocate an RMID for is skipped: some of its RMIDs may be refused,
+ * and the rest may all be in limbo.
  *
  * When the CLOSID and RMID are independent numbers, the first free CLOSID will
  * be returned.
@@ -245,6 +248,7 @@ static struct rmid_entry *resctrl_find_free_rmid(u32 closid)
 int resctrl_find_cleanest_closid(void)
 {
 	u32 cleanest_closid = ~0;
+	u32 unusable_closid = ~0;
 	int i = 0;
 
 	lockdep_assert_held(&rdtgroup_mutex);
@@ -262,6 +266,16 @@ int resctrl_find_cleanest_closid(void)
 		if (num_dirty == 0)
 			return i;
 
+		/*
+		 * Leave a CLOSID that cannot allocate for last so that
+		 * alloc_rmid() can still report why the RMID allocation failed.
+		 */
+		if (IS_ERR(resctrl_find_free_rmid(i))) {
+			if (unusable_closid == ~0)
+				unusable_closid = i;
+			continue;
+		}
+
 		if (cleanest_closid == ~0)
 			cleanest_closid = i;
 
@@ -269,10 +283,13 @@ int resctrl_find_cleanest_closid(void)
 			cleanest_closid = i;
 	}
 
-	if (cleanest_closid == ~0)
-		return -ENOSPC;
+	if (cleanest_closid != ~0)
+		return cleanest_closid;
+
+	if (unusable_closid != ~0)
+		return unusable_closid;
 
-	return cleanest_closid;
+	return -ENOSPC;
 }
 
 /*
diff --git a/include/linux/arm_mpam.h b/include/linux/arm_mpam.h
index 002f56e15362..76c192302bc6 100644
--- a/include/linux/arm_mpam.h
+++ b/include/linux/arm_mpam.h
@@ -63,6 +63,12 @@ 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;
+}
+
 struct rdt_resource;
 void *resctrl_arch_mon_ctx_alloc(struct rdt_resource *r, enum resctrl_event_id evtid);
 void resctrl_arch_mon_ctx_free(struct rdt_resource *r, enum resctrl_event_id evtid, void *ctx);
-- 
2.34.1




More information about the linux-arm-kernel mailing list