[RFC PATCH 06/13] arm_mpam: Add an internal PARTID allocator

From: Yunhui Cui

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


PARTID narrowing allows an MSC to accept more request PARTIDs than it
has internal configuration entries. A logical PARTID only needs its own
internal PARTID when it requires a private policy, and different MSCs
may choose different internal PARTIDs for the same logical PARTID.

Allocate internal PARTIDs at MSC scope because all RIS in an MSC share
one mapping table. Reserve internal PARTID 0 for the root policy and
check every MSC serving a component before making any reservation, so
running out of entries cannot leave a partial allocation.

Signed-off-by: Yunhui Cui <cuiyunhui@xxxxxxxxxxxxx>
---
drivers/resctrl/mpam_devices.c | 176 +++++++++++++++++++++++++++++++-
drivers/resctrl/mpam_internal.h | 9 +-
2 files changed, 181 insertions(+), 4 deletions(-)

diff --git a/drivers/resctrl/mpam_devices.c b/drivers/resctrl/mpam_devices.c
index 7cf4f4eece192..b4ee5a0354bfa 100644
--- a/drivers/resctrl/mpam_devices.c
+++ b/drivers/resctrl/mpam_devices.c
@@ -153,6 +153,7 @@ u16 mpam_partid_max;
u8 mpam_pmg_max;
static bool partid_max_init, partid_max_published;
static DEFINE_SPINLOCK(partid_max_lock);
+static DEFINE_MUTEX(partid_map_lock);

/*
* mpam is enabled once all devices have been probed from CPU online callbacks,
@@ -2853,6 +2854,132 @@ bool mpam_component_uses_partid_narrowing(struct mpam_component *comp)
return comp->uses_partid_narrowing;
}

+static bool __mpam_component_has_partid_entries(struct mpam_component *comp,
+ u16 logical_partid)
+{
+ struct mpam_vmsc *vmsc;
+
+ if (!comp->uses_partid_narrowing || logical_partid == 0)
+ return true;
+
+ list_for_each_entry(vmsc, &comp->vmsc, comp_list)
+ if (!test_bit(logical_partid, vmsc->msc->partid_has_entry))
+ return false;
+
+ return true;
+}
+
+bool mpam_component_has_partid_entries(struct mpam_component *comp,
+ u16 logical_partid)
+{
+ guard(mutex)(&partid_map_lock);
+
+ return __mpam_component_has_partid_entries(comp, logical_partid);
+}
+
+static bool mpam_partid_seen(const u16 *logical_partids, size_t nr_partids,
+ u16 logical_partid)
+{
+ size_t i;
+
+ for (i = 0; i < nr_partids; i++) {
+ if (logical_partids[i] == logical_partid)
+ return true;
+ }
+
+ return false;
+}
+
+static unsigned int mpam_count_new_partid_entries(struct mpam_msc *msc,
+ const u16 *logical_partids,
+ size_t nr_partids)
+{
+ unsigned int needed = 0;
+ size_t i;
+
+ for (i = 0; i < nr_partids; i++) {
+ u16 logical_partid = logical_partids[i];
+
+ if (!logical_partid ||
+ test_bit(logical_partid, msc->partid_has_entry) ||
+ mpam_partid_seen(logical_partids, i, logical_partid))
+ continue;
+ needed++;
+ }
+
+ return needed;
+}
+
+static int mpam_preflight_partid_entries(struct mpam_component *comp,
+ const u16 *logical_partids,
+ size_t nr_partids)
+{
+ struct mpam_vmsc *vmsc;
+
+ lockdep_assert_held(&partid_map_lock);
+
+ list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
+ struct mpam_msc *msc = vmsc->msc;
+ unsigned int free, needed;
+
+ needed = mpam_count_new_partid_entries(msc, logical_partids,
+ nr_partids);
+ free = msc->nr_intpartids -
+ bitmap_weight(msc->intpartid_allocated,
+ msc->nr_intpartids);
+ if (free < needed)
+ return -ENOSPC;
+ }
+
+ return 0;
+}
+
+static void mpam_allocate_partid_entries(struct mpam_component *comp,
+ const u16 *logical_partids,
+ size_t nr_partids)
+{
+ struct mpam_vmsc *vmsc;
+ size_t i;
+
+ lockdep_assert_held(&partid_map_lock);
+
+ list_for_each_entry(vmsc, &comp->vmsc, comp_list) {
+ struct mpam_msc *msc = vmsc->msc;
+
+ for (i = 0; i < nr_partids; i++) {
+ u16 logical_partid = logical_partids[i];
+ u16 intpartid;
+
+ if (!logical_partid ||
+ test_bit(logical_partid, msc->partid_has_entry))
+ continue;
+ intpartid = find_next_zero_bit(msc->intpartid_allocated,
+ msc->nr_intpartids, 1);
+ __set_bit(intpartid, msc->intpartid_allocated);
+ msc->partid_to_intpartid[logical_partid] = intpartid;
+ __set_bit(logical_partid, msc->partid_has_entry);
+ }
+ }
+}
+
+int mpam_prepare_partid_entries(struct mpam_component *comp,
+ const u16 *logical_partids, size_t nr_partids)
+{
+ int ret;
+
+ guard(mutex)(&partid_map_lock);
+
+ if (!comp->uses_partid_narrowing)
+ return 0;
+
+ ret = mpam_preflight_partid_entries(comp, logical_partids, nr_partids);
+ if (ret)
+ return ret;
+ mpam_allocate_partid_entries(comp, logical_partids, nr_partids);
+
+ return 0;
+}
+
static bool mpam_component_shares_direct_msc(struct mpam_component *comp)
{
struct mpam_vmsc *vmsc;
@@ -3186,6 +3313,42 @@ static void mpam_reset_component_cfg(struct mpam_component *comp)
}
}

+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;
+
+ u16 *map;
+
+ if (msc->partid_to_intpartid)
+ return 0;
+
+ map = devm_kcalloc(dev, nr_partids, sizeof(*map), GFP_KERNEL);
+ if (!map)
+ return -ENOMEM;
+ intpartids = devm_bitmap_zalloc(dev, msc->nr_intpartids, GFP_KERNEL);
+ if (!intpartids)
+ goto out_free_map;
+ entries = devm_bitmap_zalloc(dev, nr_partids, GFP_KERNEL);
+ if (!entries)
+ goto out_free_intpartids;
+
+ __set_bit(0, intpartids);
+ __set_bit(0, entries);
+ msc->partid_to_intpartid = map;
+ msc->intpartid_allocated = intpartids;
+ msc->partid_has_entry = entries;
+
+ return 0;
+
+out_free_intpartids:
+ devm_kfree(dev, intpartids);
+out_free_map:
+ devm_kfree(dev, map);
+ return -ENOMEM;
+}
+
static int __allocate_component_cfg(struct mpam_component *comp)
{
struct mpam_vmsc *vmsc;
@@ -3212,10 +3375,17 @@ static int __allocate_component_cfg(struct mpam_component *comp)
struct mpam_msc_ris *ris;
struct msmon_mbwu_state *mbwu_state;

- if (!vmsc->props.num_mbwu_mon)
- continue;
-
msc = vmsc->msc;
+
+ if (comp->uses_partid_narrowing) {
+ int ret = mpam_alloc_msc_partid_state(msc);
+
+ if (ret) {
+ __destroy_component_cfg(comp);
+ return ret;
+ }
+ }
+
list_for_each_entry(ris, &vmsc->ris, vmsc_list) {
if (!ris->props.num_mbwu_mon)
continue;
diff --git a/drivers/resctrl/mpam_internal.h b/drivers/resctrl/mpam_internal.h
index 25744cefe43f3..f45a129689301 100644
--- a/drivers/resctrl/mpam_internal.h
+++ b/drivers/resctrl/mpam_internal.h
@@ -95,6 +95,9 @@ struct mpam_msc {
bool probed;
u16 partid_max;
u32 nr_intpartids;
+ u16 *partid_to_intpartid;
+ unsigned long *intpartid_allocated;
+ unsigned long *partid_has_entry;
u8 pmg_max;
unsigned long ris_idxs;
u32 ris_max;
@@ -514,9 +517,13 @@ void mpam_fb_disable_mpam(int err, int mpam_fb_err);
/* Reset all the RIS in a class under cpus_read_lock() */
int mpam_reset_class_locked(struct mpam_class *class);

-int mpam_apply_config(struct mpam_component *comp, u16 partid,
+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_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_msmon_read(struct mpam_component *comp, struct mon_cfg *ctx,
enum mpam_device_features, u64 *val);
--
2.39.2 (Apple Git-143)