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 06/13] arm_mpam: Add an internal PARTID allocator
Date: Mon, 30 Nov 2026 10:29:22 +0800	[thread overview]
Message-ID: <20261130022929.71748-7-cuiyunhui@bytedance.com> (raw)
In-Reply-To: <20261130022929.71748-1-cuiyunhui@bytedance.com>

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@bytedance.com>
---
 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)


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

Thread overview: 19+ 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 ` Yunhui Cui [this message]
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 ` [RFC PATCH 10/13] arm_mpam: Map CDP data and code defaults separately Yunhui Cui
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

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-7-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®