mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yunhui Cui <cuiyunhui@bytedance.com>
To: tony.luck@intel.com, reinette.chatre@intel.com,
	ben.horgan@arm.com, Dave.Martin@arm.com, james.morse@arm.com,
	babu.moger@amd.com, fustini@kernel.org, tglx@kernel.org,
	mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com,
	hpa@zytor.com, fenghuay@nvidia.com, shuah@kernel.org,
	corbet@lwn.net, skhan@linuxfoundation.org, rdunlap@infradead.org,
	x86@kernel.org, linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-riscv@lists.infradead.org, linux-kselftest@vger.kernel.org,
	kunit-dev@googlegroups.com, linux-doc@vger.kernel.org
Cc: Yunhui Cui <cuiyunhui@bytedance.com>
Subject: [RFC PATCH 10/13] arm_mpam: Map CDP data and code defaults separately
Date: Mon, 30 Nov 2026 10:29:26 +0800	[thread overview]
Message-ID: <20261130022929.71748-11-cuiyunhui@bytedance.com> (raw)
In-Reply-To: <20261130022929.71748-1-cuiyunhui@bytedance.com>

PARTID mapping initially sends all unconfigured request PARTIDs to a
single root entry, but CDP requires separate default policies for data
and code. Allocate a code-root entry and map unconfigured even and odd
request PARTIDs to the data and code roots. Before disabling CDP or
rolling back a failed enable, restore all default mappings to the data
root before releasing the code-root entry; retain it if restoration
fails because hardware may still select it.

Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
 drivers/resctrl/mpam_devices.c  | 179 +++++++++++++++++++++++++++++---
 drivers/resctrl/mpam_internal.h |   1 +
 drivers/resctrl/mpam_resctrl.c  |  16 +--
 3 files changed, 175 insertions(+), 21 deletions(-)

diff --git a/drivers/resctrl/mpam_devices.c b/drivers/resctrl/mpam_devices.c
index 4925b61be162c..f67bcda8209c9 100644
--- a/drivers/resctrl/mpam_devices.c
+++ b/drivers/resctrl/mpam_devices.c
@@ -155,9 +155,15 @@ static bool partid_max_init, partid_max_published;
 static DEFINE_SPINLOCK(partid_max_lock);
 static DEFINE_MUTEX(partid_map_lock);
 static bool partid_narrowing_enabled;
+static unsigned int default_partid_count = 1;
 static bool partid_mapping_failed;
 static void mpam_fail_partid_mapping(void);
 
+static u16 mpam_default_partid(u16 partid, bool cdp)
+{
+	return cdp ? partid & 1 : 0;
+}
+
 /*
  * mpam is enabled once all devices have been probed from CPU online callbacks,
  * scheduled via this work_struct. If access to an MSC depends on a CPU that
@@ -2280,10 +2286,12 @@ static void mpam_reprogram_msc(struct mpam_msc *msc)
 
 	if (msc->partid_to_intpartid) {
 		for (u32 partid = 0; partid <= mpam_partid_max; partid++) {
+			bool cdp = READ_ONCE(default_partid_count) == 2;
+			u16 root = mpam_default_partid(partid, cdp);
 			struct mpam_partid_map_arg map_arg = {
 				.msc = msc,
 				.partid = partid,
-				.intpartid = msc->partid_to_intpartid[0],
+				.intpartid = msc->partid_to_intpartid[root],
 			};
 
 			if (test_bit(partid, msc->partid_has_entry))
@@ -2978,18 +2986,6 @@ bool mpam_has_partid_narrowing(void)
 	return partid_narrowing_enabled;
 }
 
-bool mpam_partid_mapping_failed(void)
-{
-	return READ_ONCE(partid_mapping_failed);
-}
-
-static void mpam_fail_partid_mapping(void)
-{
-	WRITE_ONCE(partid_mapping_failed, true);
-	mpam_disable_reason = "Failed to restore MPAM controller state.";
-	schedule_work(&mpam_broken_work);
-}
-
 static bool __mpam_component_has_partid_entries(struct mpam_component *comp,
 						u16 logical_partid)
 {
@@ -3293,6 +3289,159 @@ int mpam_release_partid(struct mpam_component *comp, u16 logical_partid,
 	return __mpam_unmap_partid(comp, logical_partid, root_partid, false);
 }
 
+bool mpam_partid_mapping_failed(void)
+{
+	return READ_ONCE(partid_mapping_failed);
+}
+
+static int mpam_update_default_msc_mappings(struct mpam_msc *msc)
+{
+	u32 partid;
+
+	if (!msc->partid_to_intpartid)
+		return 0;
+
+	for (partid = 0; partid <= mpam_partid_max; partid++) {
+		bool cdp = READ_ONCE(default_partid_count) == 2;
+		u16 root = mpam_default_partid(partid, cdp);
+		u16 intpartid;
+		int ret;
+
+		if (partid >= default_partid_count &&
+		    test_bit(partid, msc->partid_has_entry))
+			continue;
+		intpartid = msc->partid_to_intpartid[root];
+		ret = mpam_set_msc_partid_map(msc, partid, intpartid);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
+static int mpam_update_default_partid_mappings(void)
+{
+	struct mpam_msc *msc;
+
+	guard(srcu)(&mpam_srcu);
+	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
+				 srcu_read_lock_held(&mpam_srcu)) {
+		int ret = mpam_update_default_msc_mappings(msc);
+
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
+static int mpam_remove_code_root(bool pending_only)
+{
+	struct mpam_class *class;
+	struct mpam_component *comp;
+	int error = 0;
+
+	list_for_each_entry(class, &mpam_classes, classes_list) {
+		list_for_each_entry(comp, &class->components, class_list) {
+			int ret;
+
+			if (!comp->uses_partid_narrowing)
+				continue;
+			if (pending_only)
+				ret = mpam_abort_partid(comp, 1, 0);
+			else
+				ret = mpam_release_partid(comp, 1, 0);
+			if (ret && !error)
+				error = ret;
+		}
+	}
+	return error;
+}
+
+static void mpam_fail_partid_mapping(void)
+{
+	WRITE_ONCE(partid_mapping_failed, true);
+	mpam_disable_reason = "Failed to restore MPAM controller state.";
+	schedule_work(&mpam_broken_work);
+}
+
+/*
+ * CDP has separate data and code defaults. Reserve the code root before
+ * publishing the odd-PARTID default mappings. Callers have moved all tasks
+ * and CPUs back to the data root before removing the code root.
+ */
+int mpam_set_cdp_mapping(bool enable)
+{
+	struct mpam_class *class;
+	struct mpam_component *comp;
+	unsigned int count = enable ? 2 : 1;
+	u16 code_partid = 1;
+	int ret, rollback_ret;
+
+	lockdep_assert_cpus_held();
+	if (mpam_partid_mapping_failed())
+		return -EIO;
+	if (!partid_narrowing_enabled || count == default_partid_count)
+		return 0;
+
+	if (!enable) {
+		WRITE_ONCE(default_partid_count, 1);
+		ret = mpam_update_default_partid_mappings();
+		if (!ret)
+			ret = mpam_remove_code_root(false);
+		if (ret)
+			mpam_fail_partid_mapping();
+		return ret;
+	}
+
+	list_for_each_entry(class, &mpam_classes, classes_list) {
+		list_for_each_entry(comp, &class->components, class_list) {
+			if (!comp->uses_partid_narrowing)
+				continue;
+			if (mpam_component_has_partid_entries(comp, 1))
+				return -EBUSY;
+		}
+	}
+	list_for_each_entry(class, &mpam_classes, classes_list) {
+		list_for_each_entry(comp, &class->components, class_list) {
+			if (!comp->uses_partid_narrowing)
+				continue;
+			ret = mpam_prepare_partid_entries(comp, &code_partid, 1);
+			if (ret)
+				goto abort;
+			ret = mpam_configure_pending_partid(comp, 1, 0);
+			if (ret)
+				goto abort;
+		}
+	}
+	list_for_each_entry(class, &mpam_classes, classes_list) {
+		list_for_each_entry(comp, &class->components, class_list) {
+			if (!comp->uses_partid_narrowing)
+				continue;
+			ret = mpam_commit_partid(comp, 1);
+			if (ret)
+				goto abort;
+		}
+	}
+
+	WRITE_ONCE(default_partid_count, 2);
+	ret = mpam_update_default_partid_mappings();
+	if (ret)
+		goto abort;
+	list_for_each_entry(class, &mpam_classes, classes_list) {
+		list_for_each_entry(comp, &class->components, class_list)
+			mpam_finish_partid(comp, 1);
+	}
+	return 0;
+
+abort:
+	WRITE_ONCE(default_partid_count, 1);
+	rollback_ret = mpam_update_default_partid_mappings();
+	if (!rollback_ret)
+		rollback_ret = mpam_remove_code_root(true);
+	if (rollback_ret)
+		mpam_fail_partid_mapping();
+	return ret;
+}
+
 static bool mpam_component_shares_direct_msc(struct mpam_component *comp)
 {
 	struct mpam_vmsc *vmsc;
@@ -3378,7 +3527,9 @@ static int mpam_init_msc_partid_mappings(struct mpam_msc *msc)
 		return ret;
 
 	for (partid = 0; partid <= mpam_partid_max; partid++) {
-		u16 intpartid = msc->partid_to_intpartid[0];
+		bool cdp = READ_ONCE(default_partid_count) == 2;
+		u16 root = mpam_default_partid(partid, cdp);
+		u16 intpartid = msc->partid_to_intpartid[root];
 
 		ret = mpam_set_msc_partid_map(msc, partid, intpartid);
 		if (ret)
diff --git a/drivers/resctrl/mpam_internal.h b/drivers/resctrl/mpam_internal.h
index d0c501ab94aae..8c5a5e5f9312b 100644
--- a/drivers/resctrl/mpam_internal.h
+++ b/drivers/resctrl/mpam_internal.h
@@ -523,6 +523,7 @@ int mpam_apply_config(struct mpam_component *comp, u16 logical_partid,
 		      struct mpam_config *cfg);
 bool mpam_component_uses_partid_narrowing(struct mpam_component *comp);
 bool mpam_has_partid_narrowing(void);
+int mpam_set_cdp_mapping(bool enable);
 bool mpam_partid_mapping_failed(void);
 bool mpam_component_has_partid_entries(struct mpam_component *comp,
 				       u16 logical_partid);
diff --git a/drivers/resctrl/mpam_resctrl.c b/drivers/resctrl/mpam_resctrl.c
index 528159a48da4b..c470594807953 100644
--- a/drivers/resctrl/mpam_resctrl.c
+++ b/drivers/resctrl/mpam_resctrl.c
@@ -196,7 +196,7 @@ int resctrl_arch_set_cdp_enabled(enum resctrl_res_level rid, bool enable)
 	u32 partid_i = RESCTRL_RESERVED_CLOSID, partid_d = RESCTRL_RESERVED_CLOSID;
 	struct mpam_resctrl_res *res = &mpam_resctrl_controls[RDT_RESOURCE_L3];
 	struct rdt_resource *l3 = &res->resctrl_res;
-	int cpu;
+	int cpu, ret;
 
 	if (!IS_ENABLED(CONFIG_EXPERT) && enable) {
 		/*
@@ -206,8 +206,13 @@ int resctrl_arch_set_cdp_enabled(enum resctrl_res_level rid, bool enable)
 		pr_warn("CDP not supported\n");
 		return -EOPNOTSUPP;
 	}
-	if (enable && mpam_has_partid_narrowing())
-		return -EOPNOTSUPP;
+	if (enable) {
+		if (mpam_partid_max < 1)
+			return -EINVAL;
+		ret = mpam_set_cdp_mapping(true);
+		if (ret)
+			return ret;
+	}
 
 	if (enable)
 		pr_warn("CDP is an expert feature and may cause MPAM to malfunction.\n");
@@ -241,9 +246,6 @@ int resctrl_arch_set_cdp_enabled(enum resctrl_res_level rid, bool enable)
 		mpam_resctrl_controls[RDT_RESOURCE_MBA].resctrl_res.alloc_capable = true;
 
 	if (enable) {
-		if (mpam_partid_max < 1)
-			return -EINVAL;
-
 		partid_d = resctrl_get_config_index(RESCTRL_RESERVED_CLOSID, CDP_DATA);
 		partid_i = resctrl_get_config_index(RESCTRL_RESERVED_CLOSID, CDP_CODE);
 	}
@@ -258,7 +260,7 @@ int resctrl_arch_set_cdp_enabled(enum resctrl_res_level rid, bool enable)
 		mpam_set_cpu_defaults(cpu, partid_d, partid_i, 0, 0);
 	on_each_cpu(resctrl_arch_sync_cpu_closid_rmid, NULL, 1);
 
-	return 0;
+	return enable ? 0 : mpam_set_cdp_mapping(false);
 }
 
 static bool mpam_resctrl_hide_cdp(enum resctrl_res_level rid)
-- 
2.39.2 (Apple Git-143)


  parent reply	other threads:[~2026-11-30  2:31 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  2:29 [RFC PATCH 00/13] resctrl: Introduce a framework for logical control IDs Yunhui Cui
2026-11-30  2:29 ` [RFC PATCH 01/13] resctrl: Introduce interfaces " Yunhui Cui
2026-11-30  2:29 ` [RFC PATCH 02/13] resctrl: Add prepare, commit and abort hooks for control updates Yunhui Cui
2026-11-30  2:29 ` [RFC PATCH 03/13] resctrl: Add a release hook before logical control ID reuse Yunhui Cui
2026-11-30  2:29 ` [RFC PATCH 04/13] arm_mpam: Record PARTID narrowing capacity per MSC Yunhui Cui
2026-11-30  2:29 ` [RFC PATCH 05/13] arm_mpam: Select PARTID narrowing per component Yunhui Cui
2026-11-30  2:29 ` [RFC PATCH 06/13] arm_mpam: Add an internal PARTID allocator Yunhui Cui
2026-11-30  2:29 ` [RFC PATCH 07/13] arm_mpam: Add request-to-internal PARTID mapping Yunhui Cui
2026-11-30  2:29 ` [RFC PATCH 08/13] arm_mpam: Apply PARTID narrowing to resctrl control groups Yunhui Cui
2026-11-30  2:29 ` [RFC PATCH 09/13] arm_mpam: Separate request and internal PARTID limits Yunhui Cui
2026-11-30  2:29 ` Yunhui Cui [this message]
2026-11-30  2:29 ` [RFC PATCH 11/13] arm_mpam: Add kunit tests for PARTID narrowing Yunhui Cui
2026-11-30  2:29 ` [RFC PATCH 12/13] selftests/resctrl: Add a test for control ID allocation and reuse Yunhui Cui
2026-11-30  2:29 ` [RFC PATCH 13/13] docs/resctrl: Document logical control IDs and hardware entries Yunhui Cui
2026-11-30  6:15 ` [RFC PATCH 00/13] resctrl: Introduce a framework for logical control IDs Jinjie Ruan
2026-11-30  6:56   ` [External] " yunhui cui
2026-11-30  7:29     ` Jinjie Ruan
2026-11-30 11:22   ` Ben Horgan
2026-11-30 12:09     ` Jinjie Ruan
2026-11-30 14:09     ` [External] " yunhui cui
2026-11-30 15:22       ` Ben Horgan

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=20261130022929.71748-11-cuiyunhui@bytedance.com \
    --to=cuiyunhui@bytedance.com \
    --cc=Dave.Martin@arm.com \
    --cc=babu.moger@amd.com \
    --cc=ben.horgan@arm.com \
    --cc=bp@alien8.de \
    --cc=corbet@lwn.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=fenghuay@nvidia.com \
    --cc=fustini@kernel.org \
    --cc=hpa@zytor.com \
    --cc=james.morse@arm.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=mingo@redhat.com \
    --cc=rdunlap@infradead.org \
    --cc=reinette.chatre@intel.com \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=tglx@kernel.org \
    --cc=tony.luck@intel.com \
    --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

all inboxes | Powered by JetHome®