From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=113.46.200.221 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796367206; b=yxPsofVO3WZaURb+4HPo/uLHYcr07fw+DxJqXf4NAxGxpYKnwAK2bm1IDWXhY1HXeCza kuFpw39IHHU/zyfvvoBRi7c4W0/tpGi+u+8qtBL25gCh++iCpsdQkPm9MuZb5bxLQwDj0 iGd2Rs4CcB01y9+rKenAL2w0ipGP2zu8NqTyOPHzieeQSQzucSlWum0AwUKBgwAeMasEq d73JBVMEumPn6N0HqPbK6XSyn6ReovWIE1TCyRTWDLFSPMslWGjsmNfaZz/exTvBEvhO+ bLTEttki2qR/HzIa7AZVjqpgSMHQhyzsn5rsYCIIL7wUxN1YBc1x7Xl7u4sXrd+p4cA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796367206; h=DMARC-Filter:dkim-signature:Received:Received:Received:Message-ID: Date:MIME-Version:User-Agent:Subject:To:References:From:In-Reply-To: Content-Type:Content-Transfer-Encoding:X-Originating-IP: X-ClientProxiedBy; bh=zKmJmHpezjskM9NZLpDaldoDCXBOY205QI6nys5BVdI=; b=zisTdYUTfZlfWdUlHksCkvcrMn2DeriK1zrt/Dh1BihiXvRfVdsp320Pd5eZBFUVGaUy 6q5CYCXZYystjj99NaezStK5G2n4/Tt3FcOHfxqAYX7IkQVgqaUVOT6GgoFycv6pNsdXx xpmX3k1fjM77Au/HgIw6X2i31K980zC9TZfMaCHROYFgKDw4RjtZ4RyDhueh+wti1KIB6 7HsDXKmZskOjSu5E3gk0U96aXaEzWEN0KxMtJXFNIqmqIQlQs4AEG0v5FNMV6azyBCpOq lC7ohDfDRH0ulPlWI19LauJluyGMkGZh7Lc2/xhy9ryPYePNORG3m6qCMU+q6pFO53w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass header.d=huawei.com header.i=@huawei.com header.a=rsa-sha256 header.s=dkim header.b=IdtQiUgq; arc=none smtp.remote-ip=113.46.200.221 Received: from canpmsgout06.his.huawei.com (canpmsgout06.his.huawei.com [113.46.200.221]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 57643606A322 for ; Fri, 4 Dec 2026 06:53:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 57643606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=huawei.com header.i=@huawei.com header.a=rsa-sha256 header.s=dkim header.b=IdtQiUgq dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=zKmJmHpezjskM9NZLpDaldoDCXBOY205QI6nys5BVdI=; b=IdtQiUgqBZel6rS+I06K+bOT/ccsXVoaDbOHmcJoUahtHzbtbZfp4V4qfPZecu2zYkBl5jqJa MLMZyZke0KlQPQuFsqSC1WDMOGFUKJnDPeDOgeueiZ6xyhh7KULWtNf+7mTodLzFxcNCtsBb3Fj XqcQUlGIYr6cPEJdJUC9/U8= Received: from mail.maildlp.com (unknown [172.19.162.144]) by canpmsgout06.his.huawei.com (SkyGuard) with ESMTPS id 4jghxP4kr8zRhQX; Fri, 4 Dec 2026 14:51:53 +0800 (CST) Received: from kwepemk200008.china.huawei.com (unknown [7.202.194.74]) by mail.maildlp.com (Postfix) with ESMTPS id 8A09040538; Fri, 4 Dec 2026 14:53:19 +0800 (CST) Received: from [10.67.110.68] (10.67.110.68) by kwepemk200008.china.huawei.com (7.202.194.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Fri, 4 Dec 2026 14:53:18 +0800 Message-ID: <86e8de33-5699-4cfb-860b-0377fd52f390@huawei.com> Date: Fri, 4 Dec 2026 14:53:18 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4] arm_mpam: resctrl: Use the domain's counter ID To: Ben Horgan , , , , , , , , References: <20261124074552.1701376-1-ruanjinjie@huawei.com> <69a5e5d5-359a-48e3-8473-22741f7ec42b@arm.com> From: Jinjie Ruan In-Reply-To: <69a5e5d5-359a-48e3-8473-22741f7ec42b@arm.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-ClientProxiedBy: kwepems100002.china.huawei.com (7.221.188.206) To kwepemk200008.china.huawei.com (7.202.194.74) 在 2026/12/3 17:26, Ben Horgan 写道: > Hi Jinjie, Hi Ben, > > On 24/11/2026 07:45, Jinjie Ruan wrote: >> resctrl assigns MBM counters per domain: mbm_L3_assignments takes a >> domain ID, a group can have a counter in one domain and none in >> another, and resctrl_arch_cntr_read() and resctrl_arch_reset_cntr() >> are given the counter to use. >> >> The driver ignores that counter and looks the monitor up in >> mbwu_idx_to_mon[], indexed by CLOSID and RMID. >> resctrl_arch_config_cntr() fills that array without looking at the >> domain, so each group has one monitor for the whole resource. >> Whichever assignment is written last selects the monitor the group >> reads in every domain. >> >> For example, with the counter ID each group uses for mbm_total_bytes >> on each domain and "_" for none: >> >> domain 0 domain 1 >> mount, mkdir g1 default=0 g1=1 default=0 g1=1 >> unassign both on domain 1 default=0 g1=1 default=_ g1=_ >> assign g1 on domain 1 default=0 g1=1 default=_ g1=0 >> >> g1 is assigned the lowest free counter on domain 1, counter 0. That >> assignment is the last one written for g1, so it selects the monitor >> g1 reads in every domain. On domain 0, g1 uses counter 1, but reads >> the monitor of counter 0: >> >> counter whose monitor is read on domain 0 >> default g1 >> expected 0 1 >> actual 0 0 > > I'd like to assess how serious a problem this is. Have you seen this on real hardware? The only system that I have > access to with exposed bandwidth counters and multiple domains only has a single counter on each domain and so rather > than seeing incorrect counts I see a counter being unexpectedly Unassigned. Yes, and I reproduced exactly that on hardware. The driver keeps one monitor per group for the whole resource, so the last mbm_L3_assignments write for a group selects the monitor that group reads in every domain. What that costs depends on how many counters there are. ## Plenty of counters I have 256 per domain, with the counter each group uses for mbm_total_bytes on each domain and "_" for none: domain 0 domain 1 mount, mkdir g1 default=0 g1=1 default=0 g1=1 unassign both on domain 1 default=0 g1=1 default=_ g1=_ assign g1 on domain 1 default=0 g1=1 default=_ g1=0 g1 gets counter 0 on domain 1, and that assignment is the last one written for g1, so it selects the monitor g1 reads everywhere. On domain 0 g1 owns counter 1, but reads the monitor of counter 0: counter whose monitor is read on domain 0 default g1 expected 0 1 actual 0 0 kprobe on mpam_msmon_read shows it: both groups end up on the same monitor instance, and only the domain written last reads the right counter. The numbers are still correct, even though two groups now share a monitor instance. The read path puts the reader's own CLOSID into the filter before it touches the counter: resctrl_arch_cntr_read(..., closid, rmid, cntr_id, ...) __read_mon(...): cfg = (struct mon_cfg) { .mon = mon_idx, /* possibly wrong */ .match_pmg = true, .partid = closid, /* the reader */ .pmg = rmid }; mpam_msmon_read(mon_comp, &cfg, ...) __ris_msmon_read_locked(m): /* under mon_sel lock */ mpam_mon_sel_lock_held(msc); write MSMON_CFG_MON_SEL = cfg->mon gen_msmon_ctl_flt_vals(): *flt_val = FIELD_PREP(MSMON_CFG_x_FLT_PARTID, ctx->partid); write MSMON_CFG_MBWU_FLT = flt_val read MSMON_MBWU So .mon may be the wrong counter, but the filter is always the reader's own PARTID/PMG, and the whole sequence is serialised by the per-MSC monitor select lock. Measured: g1 streaming on domain 1 while default is idle gives g1 d1=21881, default d1=236. So with plenty of counters this is a bookkeeping problem: the assignment tells you one thing and the driver uses another, but the value is yours. ## A single counter I limited num_mbm_cntrs to 2 per domain and left default on counter 0, so only one is free: domain 0 domain 1 mount, mkdir g1 default=0 g1=1 default=_ g1=_ g1 has counter 1 on domain 0 and none on domain 1: g1: mbm_total_bytes:0=e;1=_;2=_;3=_ g1 d0 read: g1's own traffic g1 d1 read: Unassigned Now make g1 give up domain 0 and take domain 1 instead -- the last write moves g1's monitor to counter 1, and domain 0 has no entry left: g1: mbm_total_bytes:0=_;1=e;2=_;3=_ g1 d0 read: Unassigned /* g1 gave this up */ g1 d1 read: Unassigned /* but this one was granted */ kprobe on resctrl_arch_config_cntr shows the domain 1 assignment did succeed: cfg: closid=0x1 cntr_id=0x1 assign=0x1 The counter exists and mbm_L3_assignments says =e, yet the read comes back Unassigned. Writing the same assignment a second time makes it readable. That is the state you are describing. With one counter per domain the array can only ever hold one answer, so any group whose last write was on a different domain reads Unassigned where it holds a counter. Thanks, Jinjie > > Thanks, > > BEn > >> >> That is the default group's monitor. >> >> Translate the counter ID with assigned_counters[], which maps a counter >> ID to a monitor of the class and is valid in every domain. >> >> mbwu_idx_to_mon[] is left alone: resctrl_arch_config_cntr() still fills >> it, but the read and reset paths no longer consult it, and a future >> default mbm_assign_mode may use the array, so removing it can be >> considered separately. Unassigning a counter now resets its monitor, >> where before the lookup made the reset a no-op; the monitor is reset >> again when the counter is next assigned. >> >> Fixes: a5ff6ef2d73e ("arm_mpam: resctrl: Add resctrl_arch_cntr_read() & resctrl_arch_reset_cntr()") >> Suggested-by: Ben Horgan >> Signed-off-by: Jinjie Ruan >> Cc: >> --- >> v4: >> - Rename: mon_for_cntr() -> mon_idx_from_cntr_id(), and drop the comment. (Ben) >> - Add is_cntr parameter to make read_mon_cdp_safe() and >> reset_mon_cdp_safe() the same pattern. (Ben) >> - v3: https://lore.kernel.org/all/20261118125417.671859-1-ruanjinjie@huawei.com/ >> v3: >> - Keep mbwu_idx_to_mon[] and remove the cleanup. (Ben) >> - Split out as a mpam seperate patch. >> - Remove unnessary stable version. >> - v2: https://lore.kernel.org/all/20261113083506.4129060-2-ruanjinjie@huawei.com/ >> --- >> drivers/resctrl/mpam_resctrl.c | 49 ++++++++++++++++++++++++++-------- >> 1 file changed, 38 insertions(+), 11 deletions(-) >> >> diff --git a/drivers/resctrl/mpam_resctrl.c b/drivers/resctrl/mpam_resctrl.c >> index 8e9d5c68c2c1..a81adb1b1bfc 100644 >> --- a/drivers/resctrl/mpam_resctrl.c >> +++ b/drivers/resctrl/mpam_resctrl.c >> @@ -463,6 +463,14 @@ void resctrl_arch_mon_ctx_free(struct rdt_resource *r, >> resctrl_arch_mon_ctx_free_no_wait(evtid, mon_idx); >> } >> >> +static int mon_idx_from_cntr_id(struct mpam_resctrl_mon *mon, int cntr_id, >> + enum resctrl_conf_type cdp_type) >> +{ >> + u32 idx = resctrl_get_config_index(cntr_id, cdp_type); >> + >> + return mon->assigned_counters[idx]; >> +} >> + >> static int __read_mon(struct mpam_resctrl_mon *mon, struct mpam_component *mon_comp, >> enum mpam_device_features mon_type, >> int mon_idx, >> @@ -500,19 +508,25 @@ static int __read_mon(struct mpam_resctrl_mon *mon, struct mpam_component *mon_c >> } >> >> static int read_mon_cdp_safe(struct mpam_resctrl_mon *mon, struct mpam_component *mon_comp, >> - enum mpam_device_features mon_type, >> + enum mpam_device_features mon_type, bool is_cntr, >> int mon_idx, u32 closid, u32 rmid, u64 *val) >> { >> if (cdp_enabled) { >> + int code_idx = mon_idx, data_idx = mon_idx; >> u64 code_val = 0, data_val = 0; >> int err; >> >> - err = __read_mon(mon, mon_comp, mon_type, mon_idx, >> + if (is_cntr) { >> + code_idx = mon_idx_from_cntr_id(mon, mon_idx, CDP_CODE); >> + data_idx = mon_idx_from_cntr_id(mon, mon_idx, CDP_DATA); >> + } >> + >> + err = __read_mon(mon, mon_comp, mon_type, code_idx, >> CDP_CODE, closid, rmid, &code_val); >> if (err) >> return err; >> >> - err = __read_mon(mon, mon_comp, mon_type, mon_idx, >> + err = __read_mon(mon, mon_comp, mon_type, data_idx, >> CDP_DATA, closid, rmid, &data_val); >> if (err) >> return err; >> @@ -521,6 +535,9 @@ static int read_mon_cdp_safe(struct mpam_resctrl_mon *mon, struct mpam_component >> return 0; >> } >> >> + if (is_cntr) >> + mon_idx = mon_idx_from_cntr_id(mon, mon_idx, CDP_NONE); >> + >> return __read_mon(mon, mon_comp, mon_type, mon_idx, >> CDP_NONE, closid, rmid, val); >> } >> @@ -552,13 +569,13 @@ int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_domain_hdr *hdr, >> >> mon_type = mpam_feat_msmon_csu; >> >> - return read_mon_cdp_safe(mon, mon_comp, mon_type, mon_idx, >> + return read_mon_cdp_safe(mon, mon_comp, mon_type, false, mon_idx, >> closid, rmid, val); >> } >> >> /* MBWU counters when in ABMC mode */ >> int resctrl_arch_cntr_read(struct rdt_resource *r, struct rdt_l3_mon_domain *d, >> - u32 closid, u32 rmid, int mon_idx, >> + u32 closid, u32 rmid, int cntr_id, >> enum resctrl_event_id eventid, u64 *val) >> { >> struct mpam_resctrl_mon *mon = &mpam_resctrl_counters[eventid]; >> @@ -574,8 +591,8 @@ int resctrl_arch_cntr_read(struct rdt_resource *r, struct rdt_l3_mon_domain *d, >> l3_dom = container_of(d, struct mpam_resctrl_dom, resctrl_mon_dom); >> mon_comp = l3_dom->mon_comp[eventid]; >> >> - return read_mon_cdp_safe(mon, mon_comp, mpam_feat_msmon_mbwu, >> - USE_PRE_ALLOCATED, closid, rmid, val); >> + return read_mon_cdp_safe(mon, mon_comp, mpam_feat_msmon_mbwu, true, >> + cntr_id, closid, rmid, val); >> } >> >> static void __reset_mon(struct mpam_resctrl_mon *mon, struct mpam_component *mon_comp, >> @@ -603,12 +620,22 @@ static void __reset_mon(struct mpam_resctrl_mon *mon, struct mpam_component *mon >> } >> >> static void reset_mon_cdp_safe(struct mpam_resctrl_mon *mon, struct mpam_component *mon_comp, >> - int mon_idx, u32 closid, u32 rmid) >> + bool is_cntr, int mon_idx, u32 closid, u32 rmid) >> { >> if (cdp_enabled) { >> - __reset_mon(mon, mon_comp, mon_idx, CDP_CODE, closid, rmid); >> - __reset_mon(mon, mon_comp, mon_idx, CDP_DATA, closid, rmid); >> + int code_idx = mon_idx, data_idx = mon_idx; >> + >> + if (is_cntr) { >> + code_idx = mon_idx_from_cntr_id(mon, mon_idx, CDP_CODE); >> + data_idx = mon_idx_from_cntr_id(mon, mon_idx, CDP_DATA); >> + } >> + >> + __reset_mon(mon, mon_comp, code_idx, CDP_CODE, closid, rmid); >> + __reset_mon(mon, mon_comp, data_idx, CDP_DATA, closid, rmid); >> } else { >> + if (is_cntr) >> + mon_idx = mon_idx_from_cntr_id(mon, mon_idx, CDP_NONE); >> + >> __reset_mon(mon, mon_comp, mon_idx, CDP_NONE, closid, rmid); >> } >> } >> @@ -631,7 +658,7 @@ void resctrl_arch_reset_cntr(struct rdt_resource *r, struct rdt_l3_mon_domain *d >> l3_dom = container_of(d, struct mpam_resctrl_dom, resctrl_mon_dom); >> mon_comp = l3_dom->mon_comp[eventid]; >> >> - reset_mon_cdp_safe(mon, mon_comp, USE_PRE_ALLOCATED, closid, rmid); >> + reset_mon_cdp_safe(mon, mon_comp, true, cntr_id, closid, rmid); >> } >> >> /* >