public inbox for linux-arm-kernel@lists.infradead.org
 help / color / mirror / Atom feed
From: Zeng Heng <zengheng4@huawei.com>
To: <ben.horgan@arm.com>, <Dave.Martin@arm.com>,
	<james.morse@arm.com>, <tan.shaopeng@jp.fujitsu.com>,
	<reinette.chatre@intel.com>, <fenghuay@nvidia.com>,
	<tglx@kernel.org>, <will@kernel.org>, <hpa@zytor.com>,
	<bp@alien8.de>, <babu.moger@amd.com>,
	<dave.hansen@linux.intel.com>, <mingo@redhat.com>,
	<tony.luck@intel.com>, <gshan@redhat.com>,
	<catalin.marinas@arm.com>
Cc: <linux-arm-kernel@lists.infradead.org>, <x86@kernel.org>,
	<linux-kernel@vger.kernel.org>, <wangkefeng.wang@huawei.com>
Subject: [PATCH v8 next 08/10] arm_mpam: Implement dynamic reqPARTID allocation for monitoring groups
Date: Mon, 13 Apr 2026 16:54:03 +0800	[thread overview]
Message-ID: <20260413085405.1166412-9-zengheng4@huawei.com> (raw)
In-Reply-To: <20260413085405.1166412-1-zengheng4@huawei.com>

Replace static reqPARTID allocation with dynamic binding to maximize
the monitoring group utilization. Static allocation wastes resources when
control groups create fewer sub-groups than the pre-allocated limit.

Add a lookup table (reqpartid_map) to dynamically bind reqPARTIDs to
control groups needing extended monitoring capacity:

  * resctrl_arch_rmid_expand(): Find and bind a free reqPARTID to the
    specified closid when creating monitoring groups.

  * resctrl_arch_rmid_reclaim(): Unbind reqPARTID when all monitoring
    groups associated with pmg are freed, making it available for reuse.

Update conversion helpers for dynamic mapping:
  * req2intpartid() switches to lookup table for dynamic allocation.
  * Add partid2closid() and req_pmg2rmid() helpers.

Refactor __write_config() to iterate over all reqPARTIDs that match
by intPARTID, removing fixed per-closid slot assumption.

Signed-off-by: Zeng Heng <zengheng4@huawei.com>
---
 drivers/resctrl/mpam_devices.c  |  21 ++---
 drivers/resctrl/mpam_internal.h |   2 +
 drivers/resctrl/mpam_resctrl.c  | 141 +++++++++++++++++++++++++++++---
 include/linux/arm_mpam.h        |  17 ++++
 4 files changed, 157 insertions(+), 24 deletions(-)

diff --git a/drivers/resctrl/mpam_devices.c b/drivers/resctrl/mpam_devices.c
index 2aeff798a865..cf94b45b4f9e 100644
--- a/drivers/resctrl/mpam_devices.c
+++ b/drivers/resctrl/mpam_devices.c
@@ -1764,27 +1764,24 @@ struct mpam_write_config_arg {
 	u16 partid;
 };
 
-static u32 get_num_reqpartid_per_intpartid(void)
-{
-	return (mpam_partid_max + 1) / (mpam_intpartid_max + 1);
-}
-
 static int __write_config(void *arg)
 {
 	int closid_num = resctrl_arch_get_num_closid(NULL);
 	struct mpam_write_config_arg *c = arg;
-	u32 reqpartid, req_idx;
+	u32 reqpartid;
 
 	/* c->partid should be within the range of intPARTIDs */
 	WARN_ON_ONCE(c->partid >= closid_num);
 
-	/* Synchronize the configuration to each sub-monitoring group. */
-	for (req_idx = 0; req_idx < get_num_reqpartid_per_intpartid();
-	     req_idx++) {
-		reqpartid = req_idx * closid_num + c->partid;
+	mpam_reprogram_ris_partid(c->ris, c->partid,
+				 &c->comp->cfg[c->partid]);
 
-		mpam_reprogram_ris_partid(c->ris, reqpartid,
-					 &c->comp->cfg[c->partid]);
+	/* Synchronize the configuration to each sub-monitoring group. */
+	for (reqpartid = closid_num;
+	     reqpartid < get_num_reqpartid(); reqpartid++) {
+		if (req2intpartid(reqpartid) == c->partid)
+			mpam_reprogram_ris_partid(c->ris, reqpartid,
+						 &c->comp->cfg[c->partid]);
 	}
 
 	return 0;
diff --git a/drivers/resctrl/mpam_internal.h b/drivers/resctrl/mpam_internal.h
index 790a90a5ccd9..16ce968344d0 100644
--- a/drivers/resctrl/mpam_internal.h
+++ b/drivers/resctrl/mpam_internal.h
@@ -473,6 +473,8 @@ void mpam_msmon_reset_mbwu(struct mpam_component *comp, struct mon_cfg *ctx);
 int mpam_get_cpumask_from_cache_id(unsigned long cache_id, u32 cache_level,
 				   cpumask_t *affinity);
 
+u32 get_num_reqpartid(void);
+
 #ifdef CONFIG_RESCTRL_FS
 int mpam_resctrl_setup(void);
 void mpam_resctrl_exit(void);
diff --git a/drivers/resctrl/mpam_resctrl.c b/drivers/resctrl/mpam_resctrl.c
index 9d0a7a4dffd1..2762462d80e5 100644
--- a/drivers/resctrl/mpam_resctrl.c
+++ b/drivers/resctrl/mpam_resctrl.c
@@ -268,7 +268,7 @@ u32 resctrl_arch_get_num_closid(struct rdt_resource *ignored)
  * The first call occurs in update_rmid_limits(), ensuring the
  * prerequisite initialization is complete.
  */
-static u32 get_num_reqpartid(void)
+u32 get_num_reqpartid(void)
 {
 	struct mpam_resctrl_res *res;
 	struct mpam_props *cprops;
@@ -318,9 +318,34 @@ static u8 rmid2pmg(u32 rmid)
 	return rmid % (mpam_pmg_max + 1);
 }
 
+static u32 req_pmg2rmid(u32 reqpartid, u8 pmg)
+{
+	if (cdp_enabled)
+		reqpartid >>= 1;
+
+	return reqpartid * (mpam_pmg_max + 1) + pmg;
+}
+
+static u32 *reqpartid_map;
+
 u16 req2intpartid(u16 reqpartid)
 {
-	return reqpartid % (mpam_intpartid_max + 1);
+	/*
+	 * Directly return intPartid in case that mpam_reset_ris() access
+	 * NULL pointer.
+	 */
+	if (reqpartid < (mpam_intpartid_max + 1))
+		return reqpartid;
+
+	return reqpartid_map[reqpartid];
+}
+
+static u32 partid2closid(u32 partid)
+{
+	if (cdp_enabled)
+		partid >>= 1;
+
+	return partid;
 }
 
 /*
@@ -334,12 +359,12 @@ u16 req2intpartid(u16 reqpartid)
 u32 resctrl_arch_rmid_idx_encode(u32 closid, u32 rmid)
 {
 	u32 reqpartid = rmid2reqpartid(rmid);
-	u32 intpartid = req2intpartid(reqpartid);
 
-	if (cdp_enabled)
-		intpartid >>= 1;
+	/* When enable CDP mode, needs to filter invalid rmid entry out */
+	if (reqpartid >= get_num_reqpartid())
+		return U32_MAX;
 
-	if (closid != intpartid)
+	if (closid != partid2closid(req2intpartid(reqpartid)))
 		return U32_MAX;
 
 	return rmid;
@@ -352,11 +377,9 @@ void resctrl_arch_rmid_idx_decode(u32 idx, u32 *closid, u32 *rmid)
 
 	if (rmid)
 		*rmid = idx;
-	if (closid) {
-		if (cdp_enabled)
-			intpartid >>= 1;
-		*closid = intpartid;
-	}
+
+	if (closid)
+		*closid = partid2closid(intpartid);
 }
 
 void resctrl_arch_sched_in(struct task_struct *tsk)
@@ -1665,6 +1688,93 @@ void mpam_resctrl_offline_cpu(unsigned int cpu)
 	}
 }
 
+static int reqpartid_init(void)
+{
+	int req_num, idx;
+
+	req_num = get_num_reqpartid();
+	reqpartid_map = kcalloc(req_num, sizeof(u32), GFP_KERNEL);
+	if (!reqpartid_map)
+		return -ENOMEM;
+
+	for (idx = 0; idx < req_num; idx++)
+		reqpartid_map[idx] = idx;
+
+	return 0;
+}
+
+static void reqpartid_exit(void)
+{
+	kfree(reqpartid_map);
+}
+
+static void update_rmid_entries_for_reqpartid(u32 reqpartid)
+{
+	int pmg;
+	u32 intpartid = reqpartid_map[reqpartid];
+	u32 closid = partid2closid(intpartid);
+
+	for (pmg = 0; pmg <= mpam_pmg_max; pmg++)
+		rmid_entry_reassign_closid(closid, req_pmg2rmid(reqpartid, pmg));
+}
+
+int resctrl_arch_rmid_expand(u32 closid)
+{
+	int i;
+
+	for (i = resctrl_arch_get_num_closid(NULL);
+	     i < get_num_reqpartid(); i++) {
+
+		/* Here means the reqpartid 'i' is free. */
+		if (reqpartid_map[i] >= resctrl_arch_get_num_closid(NULL)) {
+			if (cdp_enabled) {
+				reqpartid_map[i] = resctrl_get_config_index(closid, CDP_DATA);
+				/*
+				 * Reqpartids are always allocated in
+				 * pairs, never out-of-bounds access.
+				 */
+				reqpartid_map[i + 1] = resctrl_get_config_index(closid, CDP_CODE);
+			} else {
+				reqpartid_map[i] = resctrl_get_config_index(closid, CDP_NONE);
+			}
+			update_rmid_entries_for_reqpartid(i);
+			return i;
+		}
+	}
+
+	return -ENOSPC;
+}
+
+void resctrl_arch_rmid_reclaim(u32 closid, u32 rmid)
+{
+	int pmg;
+	u32 intpartid;
+	int reqpartid = rmid2reqpartid(rmid);
+
+	if (reqpartid < resctrl_arch_get_num_closid(NULL))
+		return;
+
+	if (cdp_enabled)
+		intpartid = resctrl_get_config_index(closid, CDP_DATA);
+	else
+		intpartid = resctrl_get_config_index(closid, CDP_NONE);
+
+	WARN_ON_ONCE(intpartid != req2intpartid(reqpartid));
+
+	for (pmg = 0; pmg <= mpam_pmg_max; pmg++) {
+		if (rmid_is_occupied(closid, req_pmg2rmid(reqpartid, pmg)))
+			break;
+	}
+
+	if (pmg > mpam_pmg_max) {
+		reqpartid_map[reqpartid] = reqpartid;
+		if (cdp_enabled)
+			reqpartid_map[reqpartid + 1] = reqpartid + 1;
+
+		update_rmid_entries_for_reqpartid(reqpartid);
+	}
+}
+
 int mpam_resctrl_setup(void)
 {
 	int err = 0;
@@ -1720,10 +1830,16 @@ int mpam_resctrl_setup(void)
 		return -EOPNOTSUPP;
 	}
 
-	err = resctrl_init();
+	err = reqpartid_init();
 	if (err)
 		return err;
 
+	err = resctrl_init();
+	if (err) {
+		reqpartid_exit();
+		return err;
+	}
+
 	WRITE_ONCE(resctrl_enabled, true);
 
 	return 0;
@@ -1741,6 +1857,7 @@ void mpam_resctrl_exit(void)
 
 	WRITE_ONCE(resctrl_enabled, false);
 	resctrl_exit();
+	reqpartid_exit();
 }
 
 /*
diff --git a/include/linux/arm_mpam.h b/include/linux/arm_mpam.h
index f92a36187a52..d45422965907 100644
--- a/include/linux/arm_mpam.h
+++ b/include/linux/arm_mpam.h
@@ -59,6 +59,23 @@ void resctrl_arch_set_cpu_default_closid_rmid(int cpu, u32 closid, u32 rmid);
 void resctrl_arch_sched_in(struct task_struct *tsk);
 bool resctrl_arch_match_closid(struct task_struct *tsk, u32 closid);
 bool resctrl_arch_match_rmid(struct task_struct *tsk, u32 closid, u32 rmid);
+
+/**
+ * resctrl_arch_rmid_expand() - Expand the RMID resources for the specified closid.
+ * @closid:    closid that matches the rmid.
+ *
+ * Return:
+ * 0 on success, or -ENOSPC etc on error.
+ */
+int resctrl_arch_rmid_expand(u32 closid);
+
+/**
+ * resctrl_arch_rmid_reclaim() - Reclaim the rmid resources for the specified closid.
+ * @closid:    closid that matches the rmid.
+ * @rmid:      Reclaim the rmid specified.
+ */
+void resctrl_arch_rmid_reclaim(u32 closid, u32 rmid);
+
 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);
-- 
2.25.1



  parent reply	other threads:[~2026-04-13  8:55 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-13  8:53 [PATCH v8 next 00/10] arm_mpam: Introduce Narrow-PARTID feature Zeng Heng
2026-04-13  8:53 ` [PATCH v8 next 01/10] fs/resctrl: Fix MPAM Partid parsing errors by preserving CDP state during umount Zeng Heng
2026-04-13  8:53 ` [PATCH v8 next 02/10] arm_mpam: Add intPARTID and reqPARTID support for Narrow-PARTID feature Zeng Heng
2026-04-13  8:53 ` [PATCH v8 next 03/10] arm_mpam: Disable reqPARTID expansion when Narrow-PARTID is unavailable Zeng Heng
2026-04-13  8:53 ` [PATCH v8 next 04/10] arm_mpam: Refactor rmid to reqPARTID/PMG mapping Zeng Heng
2026-04-13  8:54 ` [PATCH v8 next 05/10] arm_mpam: Propagate control group config to sub-monitoring groups Zeng Heng
2026-04-13  8:54 ` [PATCH v8 next 06/10] arm_mpam: Add boot parameter to limit mpam_intpartid_max Zeng Heng
2026-04-13  8:54 ` [PATCH v8 next 07/10] fs/resctrl: Add rmid_entry state helpers Zeng Heng
2026-04-13  8:54 ` Zeng Heng [this message]
2026-04-13  8:54 ` [PATCH v8 next 09/10] fs/resctrl: Wire up rmid expansion and reclaim functions Zeng Heng
2026-04-13  8:54 ` [PATCH v8 next 10/10] arm_mpam: Add mpam_sync_config() for dynamic rmid expansion Zeng Heng
2026-04-16  6:29 ` [PATCH v8 next 00/10] arm_mpam: Introduce Narrow-PARTID feature Shaopeng Tan (Fujitsu)
2026-04-20  7:31 ` Zeng Heng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260413085405.1166412-9-zengheng4@huawei.com \
    --to=zengheng4@huawei.com \
    --cc=Dave.Martin@arm.com \
    --cc=babu.moger@amd.com \
    --cc=ben.horgan@arm.com \
    --cc=bp@alien8.de \
    --cc=catalin.marinas@arm.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=fenghuay@nvidia.com \
    --cc=gshan@redhat.com \
    --cc=hpa@zytor.com \
    --cc=james.morse@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=reinette.chatre@intel.com \
    --cc=tan.shaopeng@jp.fujitsu.com \
    --cc=tglx@kernel.org \
    --cc=tony.luck@intel.com \
    --cc=wangkefeng.wang@huawei.com \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox