From nobody Tue Apr 7 00:46:13 2026 Received: from canpmsgout04.his.huawei.com (canpmsgout04.his.huawei.com [113.46.200.219]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF6953CB2C3 for ; Tue, 17 Mar 2026 13:21:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.219 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773753721; cv=none; b=e0VghvvkEoTcoHJz3/J3AJInipzy2JyJ9eUYevFjNhW+ixxH4xlhYWvc2Ch9XyjnC4CMLplQoVMjRyikCgw4pwhecSZ+da1gFcHFejNSdaKXEV6wnF2LjNliUBsIARyOepBDLbNs/BzPV8QBK3nNaEPNbf4c0krE0R5RQmj3a2A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773753721; c=relaxed/simple; bh=Oh8bLHdSqT3lIzvtzc4SB/N5rqyyJ9J2wTKSxIHcdLs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ihUkHfLhlieLrDk/FlDEn6E55Au6QXb+QANb2Ii+lz9agmR1vx3/+lNKHvAl7w2o1BHClNzUVbj9ZWiBvkLK/KEyed/AG1y346vn5hY8shanFxLpLqzRsGMRj+RvfoBz6Y7Qiawxp+nCEXfL2wVy+3YbR2uGHkqtQY4qyUqZzes= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=h-partners.com; dkim=pass (1024-bit key) header.d=h-partners.com header.i=@h-partners.com header.b=GTXD9PNZ; arc=none smtp.client-ip=113.46.200.219 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=h-partners.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=h-partners.com header.i=@h-partners.com header.b="GTXD9PNZ" dkim-signature: v=1; a=rsa-sha256; d=h-partners.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=qbfGnpDPrfirGZE9+//FmnYi/LSQeqd94n1FMvxAG4E=; b=GTXD9PNZmCUPTRqKPbfHAuaytU16UJNCsWTySBGzAgbEmHk12RLrdaoJi6bYvzKLhAp5XIihA VcDi96H+Pkt2Acf0G/drhcgPZQ2ubByCgETVDom1jD4M/+EMok97l3R0qvTZOL4b9z9AiTbxPJl 2gXcfdX4JKMJs4rC4l97+JA= Received: from mail.maildlp.com (unknown [172.19.162.223]) by canpmsgout04.his.huawei.com (SkyGuard) with ESMTPS id 4fZstb03j5z1prKL; Tue, 17 Mar 2026 21:16:55 +0800 (CST) Received: from kwepemf100008.china.huawei.com (unknown [7.202.181.222]) by mail.maildlp.com (Postfix) with ESMTPS id 4D00B40561; Tue, 17 Mar 2026 21:21:56 +0800 (CST) Received: from huawei.com (10.50.87.109) by kwepemf100008.china.huawei.com (7.202.181.222) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.36; Tue, 17 Mar 2026 21:21:55 +0800 From: Zeng Heng To: , , , , CC: , , , , , , , , , Subject: [PATCH v3 7/9] arm_mpam: Implement dynamic reqPARTID allocation for monitoring groups Date: Tue, 17 Mar 2026 21:21:39 +0800 Message-ID: <20260317132141.1272506-8-zengheng4@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260317132141.1272506-1-zengheng4@huawei.com> References: <20260317132141.1272506-1-zengheng4@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: kwepems500002.china.huawei.com (7.221.188.17) To kwepemf100008.china.huawei.com (7.202.181.222) Content-Type: text/plain; charset="utf-8" Replace static reqPARTID allocation with dynamic binding to maximize 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 --- drivers/resctrl/mpam_devices.c | 21 ++--- drivers/resctrl/mpam_internal.h | 1 + drivers/resctrl/mpam_resctrl.c | 141 +++++++++++++++++++++++++++++--- include/linux/arm_mpam.h | 17 ++++ 4 files changed, 156 insertions(+), 24 deletions(-) diff --git a/drivers/resctrl/mpam_devices.c b/drivers/resctrl/mpam_devices.c index d98f81621be1..20ad06f66b88 100644 --- a/drivers/resctrl/mpam_devices.c +++ b/drivers/resctrl/mpam_devices.c @@ -1770,27 +1770,24 @@ struct mpam_write_config_arg { u16 partid; }; =20 -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 =3D resctrl_arch_get_num_closid(NULL); struct mpam_write_config_arg *c =3D arg; - u32 reqpartid, req_idx; + u32 reqpartid; =20 /* c->partid should be within the range of intPARTIDs */ WARN_ON_ONCE(c->partid >=3D closid_num); =20 - /* Synchronize the configuration to each sub-monitoring group. */ - for (req_idx =3D 0; req_idx < get_num_reqpartid_per_intpartid(); - req_idx++) { - reqpartid =3D req_idx * closid_num + c->partid; + mpam_reprogram_ris_partid(c->ris, c->partid, + &c->comp->cfg[c->partid]); =20 - mpam_reprogram_ris_partid(c->ris, reqpartid, - &c->comp->cfg[c->partid]); + /* Synchronize the configuration to each sub-monitoring group. */ + for (reqpartid =3D closid_num; + reqpartid < get_num_reqpartid(); reqpartid++) { + if (req2intpartid(reqpartid) =3D=3D c->partid) + mpam_reprogram_ris_partid(c->ris, reqpartid, + &c->comp->cfg[c->partid]); } =20 return 0; diff --git a/drivers/resctrl/mpam_internal.h b/drivers/resctrl/mpam_interna= l.h index 9069e2314d77..91f6d5f14a9e 100644 --- a/drivers/resctrl/mpam_internal.h +++ b/drivers/resctrl/mpam_internal.h @@ -488,6 +488,7 @@ int mpam_get_cpumask_from_cache_id(unsigned long cache_= id, u32 cache_level, cpumask_t *affinity); =20 u16 req2intpartid(u16 reqpartid); +u32 get_num_reqpartid(void); =20 #ifdef CONFIG_RESCTRL_FS int mpam_resctrl_setup(void); diff --git a/drivers/resctrl/mpam_resctrl.c b/drivers/resctrl/mpam_resctrl.c index 725583d7bd07..a91c0e22ef6b 100644 --- a/drivers/resctrl/mpam_resctrl.c +++ b/drivers/resctrl/mpam_resctrl.c @@ -251,7 +251,7 @@ u32 resctrl_arch_get_num_closid(struct rdt_resource *ig= nored) * The first call occurs in mpam_resctrl_pick_counters(), ensuring the * prerequisite initialization is complete. */ -static u32 get_num_reqpartid(void) +u32 get_num_reqpartid(void) { struct mpam_resctrl_res *res; struct rdt_resource *r_mba; @@ -296,9 +296,36 @@ static u8 rmid2pmg(u32 rmid) return rmid % (mpam_pmg_max + 1); } =20 +static u32 req_pmg2rmid(u32 reqpartid, u8 pmg) +{ + if (cdp_enabled) + reqpartid >>=3D 1; + + return reqpartid * (mpam_pmg_max + 1) + pmg; +} + +static u32 *reqpartid_map; + u16 req2intpartid(u16 reqpartid) { - return reqpartid % (mpam_intpartid_max + 1); + WARN_ON_ONCE(reqpartid >=3D get_num_reqpartid()); + + /* + * 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 >>=3D 1; + + return partid; } =20 /* @@ -312,12 +339,12 @@ u16 req2intpartid(u16 reqpartid) u32 resctrl_arch_rmid_idx_encode(u32 closid, u32 rmid) { u32 reqpartid =3D rmid2reqpartid(rmid); - u32 intpartid =3D req2intpartid(reqpartid); =20 - if (cdp_enabled) - intpartid >>=3D 1; + /* When enable CDP mode, needs to filter invalid rmid entry out */ + if (reqpartid >=3D get_num_reqpartid()) + return U32_MAX; =20 - if (closid !=3D intpartid) + if (closid !=3D partid2closid(req2intpartid(reqpartid))) return U32_MAX; =20 return rmid; @@ -330,11 +357,9 @@ void resctrl_arch_rmid_idx_decode(u32 idx, u32 *closid= , u32 *rmid) =20 if (rmid) *rmid =3D idx; - if (closid) { - if (cdp_enabled) - intpartid >>=3D 1; - *closid =3D intpartid; - } + + if (closid) + *closid =3D partid2closid(intpartid); } =20 void resctrl_arch_sched_in(struct task_struct *tsk) @@ -1822,6 +1847,91 @@ void mpam_resctrl_offline_cpu(unsigned int cpu) } } =20 +static int reqpartid_init(void) +{ + int req_num, idx; + + req_num =3D get_num_reqpartid(); + reqpartid_map =3D kcalloc(req_num, sizeof(u32), GFP_KERNEL); + if (!reqpartid_map) + return -ENOMEM; + + for (idx =3D 0; idx < req_num; idx++) + reqpartid_map[idx] =3D idx; + + return 0; +} + +static void reqpartid_exit(void) +{ + kfree(reqpartid_map); +} + +static void update_rmid_entries_for_reqpartid(u32 reqpartid) +{ + int pmg; + u32 intpartid =3D reqpartid_map[reqpartid]; + u32 closid =3D partid2closid(intpartid); + + for (pmg =3D 0; pmg <=3D mpam_pmg_max; pmg++) + rmid_entry_reassign_closid(closid, req_pmg2rmid(reqpartid, pmg)); +} + +int resctrl_arch_rmid_expand(u32 closid) +{ + int i; + + for (i =3D resctrl_arch_get_num_closid(NULL); + i < get_num_reqpartid(); i++) { + /* + * If reqpartid_map[i] > mpam_intpartid_max, + * it means the reqpartid 'i' is free. + */ + if (reqpartid_map[i] >=3D resctrl_arch_get_num_closid(NULL)) { + if (cdp_enabled) { + reqpartid_map[i] =3D resctrl_get_config_index(closid, CDP_DATA); + reqpartid_map[i + 1] =3D resctrl_get_config_index(closid, CDP_CODE); + } else { + reqpartid_map[i] =3D 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 =3D rmid2reqpartid(rmid); + + if (reqpartid < resctrl_arch_get_num_closid(NULL)) + return; + + if (cdp_enabled) + intpartid =3D resctrl_get_config_index(closid, CDP_DATA); + else + intpartid =3D resctrl_get_config_index(closid, CDP_NONE); + + WARN_ON_ONCE(intpartid !=3D req2intpartid(reqpartid)); + + for (pmg =3D 0; pmg <=3D mpam_pmg_max; pmg++) { + if (rmid_is_occupied(closid, req_pmg2rmid(reqpartid, pmg))) + break; + } + + if (pmg > mpam_pmg_max) { + reqpartid_map[reqpartid] =3D reqpartid; + if (cdp_enabled) + reqpartid_map[reqpartid + 1] =3D reqpartid + 1; + + update_rmid_entries_for_reqpartid(reqpartid); + } +} + int mpam_resctrl_setup(void) { int err =3D 0; @@ -1877,10 +1987,16 @@ int mpam_resctrl_setup(void) return -EOPNOTSUPP; } =20 - err =3D resctrl_init(); + err =3D reqpartid_init(); if (err) return err; =20 + err =3D resctrl_init(); + if (err) { + reqpartid_exit(); + return err; + } + WRITE_ONCE(resctrl_enabled, true); =20 return 0; @@ -1898,6 +2014,7 @@ void mpam_resctrl_exit(void) =20 WRITE_ONCE(resctrl_enabled, false); resctrl_exit(); + reqpartid_exit(); } =20 static void mpam_resctrl_teardown_mon(struct mpam_resctrl_mon *mon, struct= mpam_class *class) 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, u= 32 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 specifie= d 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 specif= ied 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); --=20 2.25.1