[RFC PATCH 07/13] arm_mpam: Add request-to-internal PARTID mapping

From: Yunhui Cui

Date: Sun Nov 29 2026 - 21:31:11 EST


PARTID narrowing separates the request PARTID carried by a request from
the internal PARTID containing its policy. A newly allocated internal
entry must be configured on every RIS in the MSC before requests are
mapped to it, otherwise requests may observe an uninitialized or partial
policy.

Add the mapping lifecycle for allocated internal PARTIDs. Publish the
request-to-internal mapping only after configuration succeeds, map the
request PARTID back to the root entry before releasing an entry, and
keep new allocations pending until all MSC updates complete so a failure
can undo mappings that were already published.

Signed-off-by: Yunhui Cui <cuiyunhui@xxxxxxxxxxxxx>
---
drivers/resctrl/mpam_devices.c | 262 ++++++++++++++++++++++++++++++--
drivers/resctrl/mpam_internal.h | 10 ++
2 files changed, 261 insertions(+), 11 deletions(-)

diff --git a/drivers/resctrl/mpam_devices.c b/drivers/resctrl/mpam_devices.c
index b4ee5a0354bfa..f2abb69914959 100644
--- a/drivers/resctrl/mpam_devices.c
+++ b/drivers/resctrl/mpam_devices.c
@@ -1838,31 +1838,43 @@ static u16 mpam_wa_t241_calc_min_from_max(struct mpam_props *props,
}

/* Called via IPI. Call while holding an SRCU reference */
-static int mpam_reprogram_ris_partid(struct mpam_msc_ris *ris, u16 partid,
- struct mpam_config *cfg)
+static int __mpam_reprogram_ris_partid(struct mpam_msc_ris *ris,
+ u16 logical_partid,
+ struct mpam_config *cfg,
+ bool use_internal)
{
u16 cmax = MPAMCFG_CMAX_CMAX;
struct mpam_msc *msc = ris->vmsc->msc;
struct mpam_props *rprops = &ris->props;
+ u16 intpartid = logical_partid;
int ret;

guard(mutex)(&msc->part_sel_lock);
- ret = __mpam_part_sel(ris->ris_idx, partid, msc);
- if (ret)
- return ret;
+ if (use_internal) {
+ if (!test_bit(logical_partid, msc->partid_has_entry))
+ return -EINVAL;
+ intpartid = msc->partid_to_intpartid[logical_partid];
+ ret = __mpam_intpart_sel(ris->ris_idx, intpartid, msc);
+ if (ret)
+ return ret;
+ } else {
+ ret = __mpam_part_sel(ris->ris_idx, logical_partid, msc);
+ if (ret)
+ return ret;
+ }

- if (mpam_has_feature(mpam_feat_partid_nrw, rprops)) {
+ if (!use_internal && mpam_has_feature(mpam_feat_partid_nrw, rprops)) {
/* Update the intpartid mapping */
ret = mpam_write_partsel_reg(msc, INTPARTID,
- MPAMCFG_INTPARTID_INTERNAL | partid);
+ MPAMCFG_INTPARTID_INTERNAL | logical_partid);
if (ret)
return ret;

/*
- * Then switch to the 'internal' partid to update the
+ * Then switch to the 'internal' logical_partid to update the
* configuration.
*/
- ret = __mpam_intpart_sel(ris->ris_idx, partid, msc);
+ ret = __mpam_intpart_sel(ris->ris_idx, logical_partid, msc);
if (ret)
return ret;
}
@@ -1972,11 +1984,24 @@ static int mpam_reprogram_ris_partid(struct mpam_msc_ris *ris, u16 partid,
return ret;
}

- mpam_quirk_post_config_change(ris, partid, cfg);
+ mpam_quirk_post_config_change(ris, intpartid, cfg);

return 0;
}

+static int mpam_reprogram_ris_partid(struct mpam_msc_ris *ris, u16 partid,
+ struct mpam_config *cfg)
+{
+ return __mpam_reprogram_ris_partid(ris, partid, cfg, false);
+}
+
+static int mpam_reprogram_ris_mapped_partid(struct mpam_msc_ris *ris,
+ u16 logical_partid,
+ struct mpam_config *cfg)
+{
+ return __mpam_reprogram_ris_partid(ris, logical_partid, cfg, true);
+}
+
/* Call with msc cfg_lock held */
static int mpam_restore_mbwu_state(void *_ris)
{
@@ -2158,6 +2183,14 @@ static int __write_config(void *arg)
&c->comp->cfg[c->partid]);
}

+static int __write_internal_config(void *arg)
+{
+ struct mpam_write_config_arg *c = arg;
+
+ return mpam_reprogram_ris_mapped_partid(c->ris, c->partid,
+ &c->comp->cfg[c->partid]);
+}
+
static void mpam_reprogram_msc(struct mpam_msc *msc)
{
bool reset;
@@ -2958,6 +2991,7 @@ static void mpam_allocate_partid_entries(struct mpam_component *comp,
__set_bit(intpartid, msc->intpartid_allocated);
msc->partid_to_intpartid[logical_partid] = intpartid;
__set_bit(logical_partid, msc->partid_has_entry);
+ __set_bit(logical_partid, msc->partid_pending);
}
}
}
@@ -2980,6 +3014,205 @@ int mpam_prepare_partid_entries(struct mpam_component *comp,
return 0;
}

+struct mpam_partid_map_arg {
+ struct mpam_msc *msc;
+ u16 partid;
+ u16 intpartid;
+};
+
+static int __mpam_set_partid_map(void *_arg)
+{
+ struct mpam_partid_map_arg *arg = _arg;
+ struct mpam_msc *msc = arg->msc;
+ u32 partsel;
+ int ret;
+
+ guard(mutex)(&msc->part_sel_lock);
+
+ /* MPAMCFG_INTPARTID is selected by PARTID, but not by RIS. */
+ partsel = FIELD_PREP(MPAMCFG_PART_SEL_PARTID_SEL, arg->partid);
+ ret = __mpam_part_sel_raw(partsel, msc);
+ if (ret)
+ return ret;
+
+ return mpam_write_partsel_reg(msc, INTPARTID,
+ MPAMCFG_INTPARTID_INTERNAL | arg->intpartid);
+}
+
+static int mpam_set_msc_partid_map(struct mpam_msc *msc, u16 logical_partid,
+ u16 intpartid)
+{
+ struct mpam_partid_map_arg arg = {
+ .msc = msc,
+ .partid = logical_partid,
+ .intpartid = intpartid,
+ };
+
+ guard(mutex)(&msc->cfg_lock);
+
+ return mpam_touch_msc(msc, __mpam_set_partid_map, &arg);
+}
+
+static void mpam_copy_partid_config(struct mpam_config *dst,
+ const struct mpam_config *src)
+{
+ bitmap_copy(dst->features, src->features, MPAM_FEATURE_LAST);
+ dst->cpbm = src->cpbm;
+ dst->mbw_pbm = src->mbw_pbm;
+ dst->mbw_max = src->mbw_max;
+}
+
+static int mpam_configure_msc_partid(struct mpam_msc *msc, u16 logical_partid,
+ u16 root_partid)
+{
+ struct mpam_msc_ris *ris;
+ int ret;
+
+ guard(mutex)(&msc->cfg_lock);
+ list_for_each_entry_srcu(ris, &msc->ris, msc_list,
+ srcu_read_lock_held(&mpam_srcu)) {
+ struct mpam_component *comp = ris->vmsc->comp;
+ struct mpam_write_config_arg arg = {
+ .ris = ris,
+ .comp = comp,
+ .partid = logical_partid,
+ };
+
+ mpam_copy_partid_config(&comp->cfg[logical_partid], &comp->cfg[root_partid]);
+
+ ret = mpam_touch_msc(msc, __write_internal_config, &arg);
+ if (ret)
+ return ret;
+ ris->in_reset_state = false;
+ }
+
+ return 0;
+}
+
+int mpam_configure_pending_partid(struct mpam_component *comp,
+ u16 logical_partid, u16 root_partid)
+{
+ struct mpam_vmsc *vmsc;
+ int ret;
+
+ guard(mutex)(&partid_map_lock);
+ guard(srcu)(&mpam_srcu);
+
+ if (!comp->uses_partid_narrowing || !logical_partid)
+ return 0;
+ if (!__mpam_component_has_partid_entries(comp, logical_partid))
+ return -EINVAL;
+
+ list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
+ srcu_read_lock_held(&mpam_srcu)) {
+ struct mpam_msc *msc = vmsc->msc;
+
+ if (!test_bit(logical_partid, msc->partid_pending))
+ continue;
+ ret = mpam_configure_msc_partid(msc, logical_partid, root_partid);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
+int mpam_commit_partid(struct mpam_component *comp, u16 logical_partid)
+{
+ struct mpam_vmsc *vmsc;
+ int ret;
+
+ guard(mutex)(&partid_map_lock);
+ guard(srcu)(&mpam_srcu);
+
+ if (!comp->uses_partid_narrowing || !logical_partid)
+ return 0;
+
+ list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
+ srcu_read_lock_held(&mpam_srcu)) {
+ struct mpam_msc *msc = vmsc->msc;
+ u16 intpartid;
+
+ if (!test_bit(logical_partid, msc->partid_pending))
+ continue;
+ intpartid = msc->partid_to_intpartid[logical_partid];
+ ret = mpam_set_msc_partid_map(msc, logical_partid, intpartid);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
+void mpam_finish_partid(struct mpam_component *comp, u16 logical_partid)
+{
+ struct mpam_vmsc *vmsc;
+
+ guard(mutex)(&partid_map_lock);
+
+ if (!comp->uses_partid_narrowing || !logical_partid)
+ return;
+
+ list_for_each_entry(vmsc, &comp->vmsc, comp_list)
+ __clear_bit(logical_partid, vmsc->msc->partid_pending);
+}
+
+static int __mpam_unmap_partid(struct mpam_component *comp, u16 logical_partid,
+ u16 root_partid, bool pending_only)
+{
+ struct mpam_vmsc *vmsc;
+ int ret;
+
+ if (!comp->uses_partid_narrowing || !logical_partid)
+ return 0;
+
+ guard(mutex)(&partid_map_lock);
+ guard(srcu)(&mpam_srcu);
+
+ list_for_each_entry_srcu(vmsc, &comp->vmsc, comp_list,
+ srcu_read_lock_held(&mpam_srcu)) {
+ struct mpam_msc *msc = vmsc->msc;
+ u16 intpartid;
+
+ if (!test_bit(logical_partid, msc->partid_has_entry) ||
+ (pending_only && !test_bit(logical_partid, msc->partid_pending)))
+ continue;
+ intpartid = msc->partid_to_intpartid[root_partid];
+ ret = mpam_set_msc_partid_map(msc, logical_partid, intpartid);
+ if (ret)
+ return ret;
+ }
+
+ list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
+ struct mpam_msc *msc = vmsc->msc;
+ u16 intpartid;
+
+ if (!test_bit(logical_partid, msc->partid_has_entry) ||
+ (pending_only && !test_bit(logical_partid, msc->partid_pending)))
+ continue;
+ intpartid = msc->partid_to_intpartid[logical_partid];
+ msc->partid_to_intpartid[logical_partid] =
+ msc->partid_to_intpartid[root_partid];
+ __clear_bit(logical_partid, msc->partid_pending);
+ __clear_bit(logical_partid, msc->partid_has_entry);
+ __clear_bit(intpartid, msc->intpartid_allocated);
+ }
+
+ return 0;
+}
+
+int mpam_abort_partid(struct mpam_component *comp, u16 logical_partid,
+ u16 root_partid)
+{
+ return __mpam_unmap_partid(comp, logical_partid, root_partid, true);
+}
+
+int mpam_release_partid(struct mpam_component *comp, u16 logical_partid,
+ u16 root_partid)
+{
+ return __mpam_unmap_partid(comp, logical_partid, root_partid, false);
+}
+
static bool mpam_component_shares_direct_msc(struct mpam_component *comp)
{
struct mpam_vmsc *vmsc;
@@ -3318,7 +3551,7 @@ static int mpam_alloc_msc_partid_state(struct mpam_msc *msc)
struct device *dev = &msc->pdev->dev;
size_t nr_partids = mpam_partid_max + 1;
unsigned long *intpartids, *entries;
-
+ unsigned long *pending;
u16 *map;

if (msc->partid_to_intpartid)
@@ -3334,14 +3567,21 @@ static int mpam_alloc_msc_partid_state(struct mpam_msc *msc)
if (!entries)
goto out_free_intpartids;

+ pending = devm_bitmap_zalloc(dev, nr_partids, GFP_KERNEL);
+ if (!pending)
+ goto out_free_entries;
+
__set_bit(0, intpartids);
__set_bit(0, entries);
msc->partid_to_intpartid = map;
msc->intpartid_allocated = intpartids;
msc->partid_has_entry = entries;
+ msc->partid_pending = pending;

return 0;

+out_free_entries:
+ devm_kfree(dev, entries);
out_free_intpartids:
devm_kfree(dev, intpartids);
out_free_map:
diff --git a/drivers/resctrl/mpam_internal.h b/drivers/resctrl/mpam_internal.h
index f45a129689301..6055cf52972a5 100644
--- a/drivers/resctrl/mpam_internal.h
+++ b/drivers/resctrl/mpam_internal.h
@@ -98,6 +98,8 @@ struct mpam_msc {
u16 *partid_to_intpartid;
unsigned long *intpartid_allocated;
unsigned long *partid_has_entry;
+ /* New reservations stay pending until the entire update finishes. */
+ unsigned long *partid_pending;
u8 pmg_max;
unsigned long ris_idxs;
u32 ris_max;
@@ -524,6 +526,14 @@ bool mpam_component_has_partid_entries(struct mpam_component *comp,
u16 logical_partid);
int mpam_prepare_partid_entries(struct mpam_component *comp,
const u16 *logical_partids, size_t nr_partids);
+int mpam_configure_pending_partid(struct mpam_component *comp,
+ u16 logical_partid, u16 root_partid);
+int mpam_commit_partid(struct mpam_component *comp, u16 logical_partid);
+void mpam_finish_partid(struct mpam_component *comp, u16 logical_partid);
+int mpam_abort_partid(struct mpam_component *comp, u16 logical_partid,
+ u16 root_partid);
+int mpam_release_partid(struct mpam_component *comp, u16 logical_partid,
+ u16 root_partid);

int mpam_msmon_read(struct mpam_component *comp, struct mon_cfg *ctx,
enum mpam_device_features, u64 *val);
--
2.39.2 (Apple Git-143)