From nobody Tue Dec 16 14:50:57 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A76F330149B for ; Fri, 5 Dec 2025 22:01:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764972092; cv=none; b=Ocro6jT6QdG5qknU9KjxMWHYdytOpy1RPQQDOQSlET5HKbhG+qTk96GuOKuo3WlRKj+Kv5NX6CGv9Y0nout4cok7c2Z0wudhwtNLI4v4hPjI98+JnrQeGpHggNjAtM8GqP4JHeKuhBpcDOUd4SqbQGA/jIWxLy9yRYw4J/0yMWs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764972092; c=relaxed/simple; bh=ujDXU87fvjq7qsS1RSy+zAmyBIzLnNPPC+5zWV02rhk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dtIPmepOnU6fJ01cK+OGT3pMchsk6hbCK/QHCYnj4J1ienMZ4kcSu2t5CeotBPzHHLNyqtwhwnJchuqeDsuDyp5SLIagWNFDt6h1RQOq5J6eYtot/P+dhSVbngTa2JiDyEU5a0u5V8IgTa1eXa2XokmxpIB2Xq8tXHz+ZeXi2wc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com 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 EB6F21AC1; Fri, 5 Dec 2025 14:01:22 -0800 (PST) Received: from merodach.members.linode.com (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 89EBB3F740; Fri, 5 Dec 2025 14:01:26 -0800 (PST) From: James Morse To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: James Morse , D Scott Phillips OS , carl@os.amperecomputing.com, lcherian@marvell.com, bobo.shaobowang@huawei.com, tan.shaopeng@fujitsu.com, baolin.wang@linux.alibaba.com, Jamie Iles , Xin Hao , peternewman@google.com, dfustini@baylibre.com, amitsinght@marvell.com, David Hildenbrand , Dave Martin , Koba Ko , Shanker Donthineni , fenghuay@nvidia.com, baisheng.gao@unisoc.com, Jonathan Cameron , Gavin Shan , Ben Horgan , rohit.mathew@arm.com, reinette.chatre@intel.com, Punit Agrawal Subject: [RFC PATCH 30/38] arm_mpam: resctrl: Call resctrl_exit() in the event of errors Date: Fri, 5 Dec 2025 21:58:53 +0000 Message-Id: <20251205215901.17772-31-james.morse@arm.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20251205215901.17772-1-james.morse@arm.com> References: <20251205215901.17772-1-james.morse@arm.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 Content-Type: text/plain; charset="utf-8" All of MPAMs errors indicate a software bug, e.g. an out-of-bounds partid has been generated. When this happens, the mpam driver is disabled. If resctrl_init() succeeded, also call resctrl_exit() to remove resctrl. mpam_devices.c calls mpam_resctrl_teardown_class() when a class becomes incomplete, and can no longer be used by resctrl. If resctrl was using this class, then resctrl_exit() is called. This in turn removes the kernfs hierarchy from the filesystem and free()s memory that was allocated by resctrl. Signed-off-by: James Morse --- drivers/resctrl/mpam_devices.c | 32 +++++++++++-- drivers/resctrl/mpam_internal.h | 4 ++ drivers/resctrl/mpam_resctrl.c | 80 +++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 4 deletions(-) diff --git a/drivers/resctrl/mpam_devices.c b/drivers/resctrl/mpam_devices.c index fccebfd980d8..1334093fc03e 100644 --- a/drivers/resctrl/mpam_devices.c +++ b/drivers/resctrl/mpam_devices.c @@ -73,6 +73,14 @@ static DECLARE_WORK(mpam_broken_work, &mpam_disable); /* When mpam is disabled, the printed reason to aid debugging */ static char *mpam_disable_reason; =20 +/* + * Whether resctrl has been setup. Used by cpuhp in preference to + * mpam_is_enabled(). The disable call after an error interrupt makes + * mpam_is_enabled() false before the cpuhp callbacks are made. + * Reads/writes should hold mpam_cpuhp_state_lock, (or be cpuhp callbacks). + */ +static bool mpam_resctrl_enabled; + /* * An MSC is a physical container for controls and monitors, each identifi= ed by * their RIS index. These share a base-address, interrupts and some MMIO @@ -1627,7 +1635,7 @@ static int mpam_cpu_online(unsigned int cpu) mpam_reprogram_msc(msc); } =20 - if (mpam_is_enabled()) + if (mpam_resctrl_enabled) mpam_resctrl_online_cpu(cpu); =20 return 0; @@ -1673,7 +1681,7 @@ static int mpam_cpu_offline(unsigned int cpu) { struct mpam_msc *msc; =20 - if (mpam_is_enabled()) + if (mpam_resctrl_enabled) mpam_resctrl_offline_cpu(cpu); =20 guard(srcu)(&mpam_srcu); @@ -2535,6 +2543,7 @@ static void mpam_enable_once(void) } =20 static_branch_enable(&mpam_enabled); + mpam_resctrl_enabled =3D true; mpam_register_cpuhp_callbacks(mpam_cpu_online, mpam_cpu_offline, "mpam:online"); =20 @@ -2594,24 +2603,39 @@ void mpam_reset_class(struct mpam_class *class) void mpam_disable(struct work_struct *ignored) { int idx; + bool do_resctrl_exit; struct mpam_class *class; struct mpam_msc *msc, *tmp; =20 + if (mpam_is_enabled()) + static_branch_disable(&mpam_enabled); + mutex_lock(&mpam_cpuhp_state_lock); if (mpam_cpuhp_state) { cpuhp_remove_state(mpam_cpuhp_state); mpam_cpuhp_state =3D 0; } + + /* + * Removing the cpuhp state called mpam_cpu_offline() and told resctrl + * all the CPUs are offline. + */ + do_resctrl_exit =3D mpam_resctrl_enabled; + mpam_resctrl_enabled =3D false; mutex_unlock(&mpam_cpuhp_state_lock); =20 - static_branch_disable(&mpam_enabled); + if (do_resctrl_exit) + mpam_resctrl_exit(); =20 mpam_unregister_irqs(); =20 idx =3D srcu_read_lock(&mpam_srcu); list_for_each_entry_srcu(class, &mpam_classes, classes_list, - srcu_read_lock_held(&mpam_srcu)) + srcu_read_lock_held(&mpam_srcu)) { mpam_reset_class(class); + if (do_resctrl_exit) + mpam_resctrl_teardown_class(class); + } srcu_read_unlock(&mpam_srcu, idx); =20 mutex_lock(&mpam_list_lock); diff --git a/drivers/resctrl/mpam_internal.h b/drivers/resctrl/mpam_interna= l.h index 3a68ebd498fa..b13d5e55e701 100644 --- a/drivers/resctrl/mpam_internal.h +++ b/drivers/resctrl/mpam_internal.h @@ -450,12 +450,16 @@ int mpam_get_cpumask_from_cache_id(unsigned long cach= e_id, u32 cache_level, =20 #ifdef CONFIG_RESCTRL_FS int mpam_resctrl_setup(void); +void mpam_resctrl_exit(void); int mpam_resctrl_online_cpu(unsigned int cpu); void mpam_resctrl_offline_cpu(unsigned int cpu); +void mpam_resctrl_teardown_class(struct mpam_class *class); #else static inline int mpam_resctrl_setup(void) { return 0; } +static inline void mpam_resctrl_exit(void) { } static inline int mpam_resctrl_online_cpu(unsigned int cpu) { return 0; } static inline void mpam_resctrl_offline_cpu(unsigned int cpu) { } +static inline void mpam_resctrl_teardown_class(struct mpam_class *class) {= } #endif /* CONFIG_RESCTRL_FS */ =20 /* diff --git a/drivers/resctrl/mpam_resctrl.c b/drivers/resctrl/mpam_resctrl.c index eb3caee45470..506063bd3348 100644 --- a/drivers/resctrl/mpam_resctrl.c +++ b/drivers/resctrl/mpam_resctrl.c @@ -52,6 +52,12 @@ static bool exposed_mon_capable; */ static bool cdp_enabled; =20 +/* + * If resctrl_init() succeeded, resctrl_exit() can be used to remove suppo= rt + * for the filesystem in the event of an error. + */ +static bool resctrl_enabled; + /* * L3 local/total may come from different classes - what is the number of = MBWU * 'on L3'? @@ -310,6 +316,9 @@ static int resctrl_arch_mon_ctx_alloc_no_wait(enum resc= trl_event_id evtid) { struct mpam_resctrl_mon *mon =3D &mpam_resctrl_counters[evtid]; =20 + if (!mpam_is_enabled()) + return -EINVAL; + if (!mon->class) return -EINVAL; =20 @@ -352,6 +361,9 @@ static void resctrl_arch_mon_ctx_free_no_wait(enum resc= trl_event_id evtid, { struct mpam_resctrl_mon *mon =3D &mpam_resctrl_counters[evtid]; =20 + if (!mpam_is_enabled()) + return; + if (!mon->class) return; =20 @@ -449,6 +461,9 @@ int resctrl_arch_rmid_read(struct rdt_resource *r, stru= ct rdt_mon_domain *d, =20 resctrl_arch_rmid_read_context_check(); =20 + if (!mpam_is_enabled()) + return -EINVAL; + if (eventid >=3D QOS_NUM_EVENTS || !mon->class) return -EINVAL; =20 @@ -1343,6 +1358,9 @@ int resctrl_arch_update_one(struct rdt_resource *r, s= truct rdt_ctrl_domain *d, lockdep_assert_cpus_held(); lockdep_assert_irqs_enabled(); =20 + if (!mpam_is_enabled()) + return -EINVAL; + /* * No need to check the CPU as mpam_apply_config() doesn't care, and * resctrl_arch_update_domains() relies on this. @@ -1408,6 +1426,9 @@ int resctrl_arch_update_domains(struct rdt_resource *= r, u32 closid) lockdep_assert_cpus_held(); lockdep_assert_irqs_enabled(); =20 + if (!mpam_is_enabled()) + return -EINVAL; + list_for_each_entry(d, &r->ctrl_domains, hdr.list) { for (t =3D 0; t < CDP_NUM_TYPES; t++) { cfg =3D &d->staged_config[t]; @@ -1769,11 +1790,70 @@ int mpam_resctrl_setup(void) } =20 err =3D resctrl_init(); + if (!err) + WRITE_ONCE(resctrl_enabled, true); } =20 return err; } =20 +void mpam_resctrl_exit(void) +{ + if (!READ_ONCE(resctrl_enabled)) + return; + + WRITE_ONCE(resctrl_enabled, false); + resctrl_exit(); +} + +static void mpam_resctrl_teardown_mon(struct mpam_resctrl_mon *mon, struct= mpam_class *class) +{ + u32 num_mbwu_mon =3D l3_num_allocated_mbwu; + + if (!mon->mbwu_idx_to_mon) + return; + + if (mon->assigned_counters) { + __free_mbwu_mon(class, mon->assigned_counters, num_mbwu_mon); + mon->assigned_counters =3D NULL; + kfree(mon->mbwu_idx_to_mon); + } else { + __free_mbwu_mon(class, mon->mbwu_idx_to_mon, num_mbwu_mon); + } + mon->mbwu_idx_to_mon =3D NULL; +} + +/* + * The driver is detaching an MSC from this class, if resctrl was using it, + * pull on resctrl_exit(). + */ +void mpam_resctrl_teardown_class(struct mpam_class *class) +{ + int i; + struct mpam_resctrl_res *res; + struct mpam_resctrl_mon *mon; + + might_sleep(); + + for (i =3D 0; i < RDT_NUM_RESOURCES; i++) { + res =3D &mpam_resctrl_controls[i]; + if (res->class =3D=3D class) { + res->class =3D NULL; + break; + } + } + for (i =3D 0; i < QOS_NUM_EVENTS; i++) { + mon =3D &mpam_resctrl_counters[i]; + if (mon->class =3D=3D class) { + mon->class =3D NULL; + + mpam_resctrl_teardown_mon(mon, class); + + break; + } + } +} + #ifdef CONFIG_MPAM_KUNIT_TEST #include "test_mpam_resctrl.c" #endif --=20 2.39.5