From nobody Wed Apr 8 03:07:10 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5E134C433FE for ; Fri, 21 Oct 2022 13:12:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230334AbiJUNMz (ORCPT ); Fri, 21 Oct 2022 09:12:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230254AbiJUNMi (ORCPT ); Fri, 21 Oct 2022 09:12:38 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2D56526DB1A for ; Fri, 21 Oct 2022 06:12:34 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A13C11042; Fri, 21 Oct 2022 06:12:39 -0700 (PDT) Received: from merodach.members.linode.com (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id CD25E3F792; Fri, 21 Oct 2022 06:12:28 -0700 (PDT) From: James Morse To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: Fenghua Yu , Reinette Chatre , Thomas Gleixner , Ingo Molnar , Borislav Petkov , H Peter Anvin , Babu Moger , James Morse , shameerali.kolothum.thodi@huawei.com, D Scott Phillips OS , carl@os.amperecomputing.com, lcherian@marvell.com, bobo.shaobowang@huawei.com, tan.shaopeng@fujitsu.com, Jamie Iles , Xin Hao , xingxin.hx@openanolis.org, baolin.wang@linux.alibaba.com, peternewman@google.com Subject: [PATCH 02/18] x86/resctrl: Access per-rmid structures by index Date: Fri, 21 Oct 2022 13:11:48 +0000 Message-Id: <20221021131204.5581-3-james.morse@arm.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20221021131204.5581-1-james.morse@arm.com> References: <20221021131204.5581-1-james.morse@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Because of the differences between Intel RDT/AMD QoS and Arm's MPAM monitors, RMID values on arm64 are not unique unless the CLOSID is also included. Bitmaps like rmid_busy_llc need to be sized by the number of unique entries for this resource. Add helpers to encode/decode the CLOSID and RMID to an index. The domain's busy_rmid_llc and the rmid_ptrs[] array are then sized by index. On x86, this is always just the RMID. This gives resctrl a unique value it can use to store monitor values, and allows MPAM to decode the closid when reading the hardware counters. Signed-off-by: James Morse --- arch/x86/include/asm/resctrl.h | 17 ++++++ arch/x86/kernel/cpu/resctrl/internal.h | 2 + arch/x86/kernel/cpu/resctrl/monitor.c | 75 +++++++++++++++++--------- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 7 +-- 4 files changed, 72 insertions(+), 29 deletions(-) diff --git a/arch/x86/include/asm/resctrl.h b/arch/x86/include/asm/resctrl.h index d24b04ebf950..523eabfa3193 100644 --- a/arch/x86/include/asm/resctrl.h +++ b/arch/x86/include/asm/resctrl.h @@ -96,6 +96,23 @@ static inline void resctrl_sched_in(void) __resctrl_sched_in(); } =20 +static inline u32 resctrl_arch_system_num_rmid_idx(void) +{ + /* RMID are independent numbers for x86. num_rmid_idx=3D=3Dnum_rmid */ + return boot_cpu_data.x86_cache_max_rmid + 1; +} + +static inline void resctrl_arch_rmid_idx_decode(u32 idx, u32 *closid, u32 = *rmid) +{ + *rmid =3D idx; + *closid =3D ~0; +} + +static inline u32 resctrl_arch_rmid_idx_encode(u32 closid, u32 rmid) +{ + return rmid; +} + void resctrl_cpu_detect(struct cpuinfo_x86 *c); =20 #else diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/r= esctrl/internal.h index 4b243ba88882..cb94c3e3fe36 100644 --- a/arch/x86/kernel/cpu/resctrl/internal.h +++ b/arch/x86/kernel/cpu/resctrl/internal.h @@ -8,6 +8,8 @@ #include #include =20 +#include + #define MSR_IA32_L3_QOS_CFG 0xc81 #define MSR_IA32_L2_QOS_CFG 0xc82 #define MSR_IA32_L3_CBM_BASE 0xc90 diff --git a/arch/x86/kernel/cpu/resctrl/monitor.c b/arch/x86/kernel/cpu/re= sctrl/monitor.c index f1f66c9942a5..c95d259476d4 100644 --- a/arch/x86/kernel/cpu/resctrl/monitor.c +++ b/arch/x86/kernel/cpu/resctrl/monitor.c @@ -137,11 +137,24 @@ static inline u64 get_corrected_mbm_count(u32 rmid, u= nsigned long val) return val; } =20 -static inline struct rmid_entry *__rmid_entry(u32 closid, u32 rmid) +/* + * x86 and arm64 differ in their handling of monitoring. + * x86's RMID are an independent number, there is one RMID '1'. + * arm64's PMG extend the PARTID/CLOSID space, there is one RMID '1' for e= ach + * CLOSID. The RMID is no longer unique. + * To account for this, resctrl uses an index. On x86 this is just the RMI= D, + * on arm64 it encodes the CLOSID and RMID. This gives a unique number. + * + * The domain's rmid_busy_llc and rmid_ptrs are sized by index. The arch c= ode + * must accept an attempt to read every index. + */ +static inline struct rmid_entry *__rmid_entry(u32 idx) { struct rmid_entry *entry; + u32 closid, rmid; =20 - entry =3D &rmid_ptrs[rmid]; + entry =3D &rmid_ptrs[idx]; + resctrl_arch_rmid_idx_decode(idx, &closid, &rmid); WARN_ON(entry->rmid !=3D rmid); =20 return entry; @@ -238,8 +251,9 @@ int resctrl_arch_rmid_read(struct rdt_resource *r, stru= ct rdt_domain *d, void __check_limbo(struct rdt_domain *d, bool force_free) { struct rdt_resource *r =3D &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl; + u32 idx_limit =3D resctrl_arch_system_num_rmid_idx(); struct rmid_entry *entry; - u32 crmid =3D 1, nrmid; + u32 idx, cur_idx =3D 1; bool rmid_dirty; u64 val =3D 0; =20 @@ -250,12 +264,11 @@ void __check_limbo(struct rdt_domain *d, bool force_f= ree) * RMID and move it to the free list when the counter reaches 0. */ for (;;) { - nrmid =3D find_next_bit(d->rmid_busy_llc, r->num_rmid, crmid); - if (nrmid >=3D r->num_rmid) + idx =3D find_next_bit(d->rmid_busy_llc, idx_limit, cur_idx); + if (idx >=3D idx_limit) break; =20 - entry =3D __rmid_entry(~0, nrmid); // temporary - + entry =3D __rmid_entry(idx); if (resctrl_arch_rmid_read(r, d, entry->closid, entry->rmid, QOS_L3_OCCUP_EVENT_ID, &val)) { rmid_dirty =3D true; @@ -264,19 +277,21 @@ void __check_limbo(struct rdt_domain *d, bool force_f= ree) } =20 if (force_free || !rmid_dirty) { - clear_bit(entry->rmid, d->rmid_busy_llc); + clear_bit(idx, d->rmid_busy_llc); if (!--entry->busy) { rmid_limbo_count--; list_add_tail(&entry->list, &rmid_free_lru); } } - crmid =3D nrmid + 1; + cur_idx =3D idx + 1; } } =20 bool has_busy_rmid(struct rdt_resource *r, struct rdt_domain *d) { - return find_first_bit(d->rmid_busy_llc, r->num_rmid) !=3D r->num_rmid; + u32 idx_limit =3D resctrl_arch_system_num_rmid_idx(); + + return find_first_bit(d->rmid_busy_llc, idx_limit) !=3D idx_limit; } =20 /* @@ -306,6 +321,9 @@ static void add_rmid_to_limbo(struct rmid_entry *entry) struct rdt_domain *d; int cpu, err; u64 val =3D 0; + u32 idx; + + idx =3D resctrl_arch_rmid_idx_encode(entry->closid, entry->rmid); =20 entry->busy =3D 0; cpu =3D get_cpu(); @@ -325,7 +343,7 @@ static void add_rmid_to_limbo(struct rmid_entry *entry) */ if (!has_busy_rmid(r, d)) cqm_setup_limbo_handler(d, CQM_LIMBOCHECK_INTERVAL); - set_bit(entry->rmid, d->rmid_busy_llc); + set_bit(idx, d->rmid_busy_llc); entry->busy++; } put_cpu(); @@ -338,14 +356,16 @@ static void add_rmid_to_limbo(struct rmid_entry *entr= y) =20 void free_rmid(u32 closid, u32 rmid) { + u32 idx =3D resctrl_arch_rmid_idx_encode(closid, rmid); struct rmid_entry *entry; =20 - if (!rmid) - return; - lockdep_assert_held(&rdtgroup_mutex); =20 - entry =3D __rmid_entry(closid, rmid); + /* do not allow the default rmid to be free'd */ + if (!idx) + return; + + entry =3D __rmid_entry(idx); =20 if (is_llc_occupancy_enabled()) add_rmid_to_limbo(entry); @@ -355,6 +375,7 @@ void free_rmid(u32 closid, u32 rmid) =20 static int __mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr) { + u32 idx =3D resctrl_arch_rmid_idx_encode(closid, rmid); struct mbm_state *m; u64 tval =3D 0; =20 @@ -371,10 +392,10 @@ static int __mon_event_count(u32 closid, u32 rmid, st= ruct rmid_read *rr) rr->val +=3D tval; return 0; case QOS_L3_MBM_TOTAL_EVENT_ID: - m =3D &rr->d->mbm_total[rmid]; + m =3D &rr->d->mbm_total[idx]; break; case QOS_L3_MBM_LOCAL_EVENT_ID: - m =3D &rr->d->mbm_local[rmid]; + m =3D &rr->d->mbm_local[idx]; break; default: /* @@ -407,7 +428,8 @@ static int __mon_event_count(u32 closid, u32 rmid, stru= ct rmid_read *rr) */ static void mbm_bw_count(u32 closid, u32 rmid, struct rmid_read *rr) { - struct mbm_state *m =3D &rr->d->mbm_local[rmid]; + u32 idx =3D resctrl_arch_rmid_idx_encode(closid, rmid); + struct mbm_state *m =3D &rr->d->mbm_local[idx]; u64 cur_bw, bytes, cur_bytes; =20 cur_bytes =3D rr->val; @@ -497,7 +519,7 @@ static void update_mba_bw(struct rdtgroup *rgrp, struct= rdt_domain *dom_mbm) { u32 closid, rmid, cur_msr_val, new_msr_val; struct mbm_state *pmbm_data, *cmbm_data; - u32 cur_bw, delta_bw, user_bw; + u32 cur_bw, delta_bw, user_bw, idx; struct rdt_resource *r_mba; struct rdt_domain *dom_mba; struct list_head *head; @@ -510,7 +532,8 @@ static void update_mba_bw(struct rdtgroup *rgrp, struct= rdt_domain *dom_mbm) =20 closid =3D rgrp->closid; rmid =3D rgrp->mon.rmid; - pmbm_data =3D &dom_mbm->mbm_local[rmid]; + idx =3D resctrl_arch_rmid_idx_encode(closid, rmid); + pmbm_data =3D &dom_mbm->mbm_local[idx]; =20 dom_mba =3D get_domain_from_cpu(smp_processor_id(), r_mba); if (!dom_mba) { @@ -693,19 +716,19 @@ void mbm_setup_overflow_handler(struct rdt_domain *do= m, unsigned long delay_ms) =20 static int dom_data_init(struct rdt_resource *r) { + u32 nr_idx =3D resctrl_arch_system_num_rmid_idx(); struct rmid_entry *entry =3D NULL; - int i, nr_rmids; + int i; =20 - nr_rmids =3D r->num_rmid; - rmid_ptrs =3D kcalloc(nr_rmids, sizeof(struct rmid_entry), GFP_KERNEL); + rmid_ptrs =3D kcalloc(nr_idx, sizeof(struct rmid_entry), GFP_KERNEL); if (!rmid_ptrs) return -ENOMEM; =20 - for (i =3D 0; i < nr_rmids; i++) { + for (i =3D 0; i < nr_idx; i++) { entry =3D &rmid_ptrs[i]; INIT_LIST_HEAD(&entry->list); =20 - entry->rmid =3D i; + resctrl_arch_rmid_idx_decode(i, &entry->closid, &entry->rmid); list_add_tail(&entry->list, &rmid_free_lru); } =20 @@ -714,7 +737,7 @@ static int dom_data_init(struct rdt_resource *r) * default_rdtgroup control group, which will be setup later. See * rdtgroup_setup_root(). */ - entry =3D __rmid_entry(0, 0); + entry =3D __rmid_entry(resctrl_arch_rmid_idx_encode(0, 0)); list_del(&entry->list); =20 return 0; diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/r= esctrl/rdtgroup.c index f3b739c52e42..9ce4746778f4 100644 --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c @@ -3320,16 +3320,17 @@ void resctrl_offline_domain(struct rdt_resource *r,= struct rdt_domain *d) =20 static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domai= n *d) { + u32 idx_limit =3D resctrl_arch_system_num_rmid_idx(); size_t tsize; =20 if (is_llc_occupancy_enabled()) { - d->rmid_busy_llc =3D bitmap_zalloc(r->num_rmid, GFP_KERNEL); + d->rmid_busy_llc =3D bitmap_zalloc(idx_limit, GFP_KERNEL); if (!d->rmid_busy_llc) return -ENOMEM; } if (is_mbm_total_enabled()) { tsize =3D sizeof(*d->mbm_total); - d->mbm_total =3D kcalloc(r->num_rmid, tsize, GFP_KERNEL); + d->mbm_total =3D kcalloc(idx_limit, tsize, GFP_KERNEL); if (!d->mbm_total) { bitmap_free(d->rmid_busy_llc); return -ENOMEM; @@ -3337,7 +3338,7 @@ static int domain_setup_mon_state(struct rdt_resource= *r, struct rdt_domain *d) } if (is_mbm_local_enabled()) { tsize =3D sizeof(*d->mbm_local); - d->mbm_local =3D kcalloc(r->num_rmid, tsize, GFP_KERNEL); + d->mbm_local =3D kcalloc(idx_limit, tsize, GFP_KERNEL); if (!d->mbm_local) { bitmap_free(d->rmid_busy_llc); kfree(d->mbm_total); --=20 2.30.2