[RFC PATCH 10/13] arm_mpam: Map CDP data and code defaults separately
From: Yunhui Cui
Date: Sun Nov 29 2026 - 21:31:38 EST
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@xxxxxxxxxxxxx>
---
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)