[PATCH 12/12] arm_mpam: kunit: Test the capped groups and the hard limit

From: Jinjie Ruan

Date: Sun Nov 29 2026 - 22:25:46 EST


The default counter assignment mode caps the CLOSIDs and RMIDs to the
number of bandwidth counters, and only keeps the hard limit if every
resource in the class implements it. Neither behaviour is tested.

Add a table-driven test for mpam_resctrl_cap_closid_rmid(), and walk
every combination of the mbw_max feature and the hard limit flag, for
aliasing and non-aliasing resources, to check what the merge keeps.

Signed-off-by: Jinjie Ruan <ruanjinjie@xxxxxxxxxx>
---
drivers/resctrl/test_mpam_devices.c | 45 ++++++++++++++++++++++++
drivers/resctrl/test_mpam_resctrl.c | 54 +++++++++++++++++++++++++++++
2 files changed, 99 insertions(+)

diff --git a/drivers/resctrl/test_mpam_devices.c b/drivers/resctrl/test_mpam_devices.c
index 31871f519729..fc197372ed53 100644
--- a/drivers/resctrl/test_mpam_devices.c
+++ b/drivers/resctrl/test_mpam_devices.c
@@ -25,6 +25,50 @@ static void test__props_mismatch(struct kunit *test)
KUNIT_EXPECT_EQ(test, memcmp(&parent, &child, sizeof(child)), 0);
}

+/*
+ * __props_mismatch() keeps has_hardlim only where every resource implements
+ * the hard limit. Walk every combination of the feature and the flag on the
+ * two sides, for aliasing and non-aliasing resources.
+ */
+static void test__props_mismatch_hardlim(struct kunit *test)
+{
+ int i;
+
+ for (i = 0; i < 32; i++) {
+ bool alias = i & BIT(0);
+ bool has_p = i & BIT(1);
+ bool has_c = i & BIT(2);
+ bool hard_p = i & BIT(3);
+ bool hard_c = i & BIT(4);
+ struct mpam_props parent = { 0 };
+ struct mpam_props child = { 0 };
+ bool expect_feat, expect_hard;
+
+ if (has_p)
+ mpam_set_feature(mpam_feat_mbw_max, &parent);
+ if (has_c)
+ mpam_set_feature(mpam_feat_mbw_max, &child);
+ parent.has_hardlim = hard_p;
+ child.has_hardlim = hard_c;
+
+ __props_mismatch(&parent, &child, alias);
+
+ /* Aliasing resources take either, the others need both. */
+ expect_feat = alias ? (has_p || has_c) : (has_p && has_c);
+ if (has_p && has_c)
+ expect_hard = hard_p && hard_c;
+ else
+ expect_hard = has_c ? hard_c : hard_p;
+
+ KUNIT_EXPECT_EQ_MSG(test, mpam_has_feature(mpam_feat_mbw_max, &parent),
+ expect_feat, "case %d", i);
+ /* The flag only has a meaning together with the feature. */
+ if (expect_feat)
+ KUNIT_EXPECT_EQ_MSG(test, parent.has_hardlim, expect_hard,
+ "case %d", i);
+ }
+}
+
static struct list_head fake_classes_list;
static struct mpam_class fake_class = { 0 };
static struct mpam_component fake_comp1 = { 0 };
@@ -386,6 +430,7 @@ static struct kunit_case mpam_devices_test_cases[] = {
KUNIT_CASE(test_mpam_reset_msc_bitmap),
KUNIT_CASE(test_mpam_enable_merge_features),
KUNIT_CASE(test__props_mismatch),
+ KUNIT_CASE(test__props_mismatch_hardlim),
{}
};

diff --git a/drivers/resctrl/test_mpam_resctrl.c b/drivers/resctrl/test_mpam_resctrl.c
index b93d6ad87e43..c39316f67c47 100644
--- a/drivers/resctrl/test_mpam_resctrl.c
+++ b/drivers/resctrl/test_mpam_resctrl.c
@@ -296,7 +296,61 @@ static void test_percent_to_max_rounding(struct kunit *test)
KUNIT_EXPECT_LE(test, 4 * num_rounded_up, 3 * total);
}

+struct cap_case {
+ u32 num_closid; /* hardware limits that come in */
+ u32 num_rmid;
+ u32 num_mon; /* monitors to measure with */
+ u32 exp_closid;
+ u32 exp_rmid;
+};
+
+/*
+ * The groups of the capped space must each have a monitor, so
+ * exp_closid * exp_rmid <= num_mon, and no CLOSID is left without an RMID.
+ */
+static const struct cap_case cap_cases[] = {
+ /* One monitor: only the default group of the default CLOSID. */
+ { 32, 2, 1, 1, 1 },
+ /* Fewer monitors than CLOSIDs: each CLOSID keeps its default group. */
+ { 32, 2, 2, 2, 1 },
+ { 32, 2, 3, 3, 1 },
+ { 32, 1, 7, 7, 1 },
+ { 32, 1, 8, 8, 1 },
+ /* RMIDs shrink first: all CLOSIDs, then as many RMIDs as fit. */
+ { 32, 4, 64, 32, 2 },
+ { 32, 4, 96, 32, 3 },
+ { 32, 4, 128, 32, 4 },
+ /* The monitors cover everything, so nothing is capped. */
+ { 32, 4, 1000, 32, 4 },
+ { 4, 2, 8, 4, 2 },
+ /* A single CLOSID and PMG. */
+ { 1, 1, 8, 1, 1 },
+};
+
+static void test_cap_closid_rmid(struct kunit *test)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(cap_cases); i++) {
+ const struct cap_case *c = &cap_cases[i];
+ u32 num_closid = c->num_closid;
+ u32 num_rmid = c->num_rmid;
+
+ mpam_resctrl_cap_closid_rmid(c->num_mon, &num_closid, &num_rmid);
+
+ KUNIT_EXPECT_EQ_MSG(test, num_closid, c->exp_closid,
+ "case %d: %u closids, %u rmids, %u monitors",
+ i, c->num_closid, c->num_rmid, c->num_mon);
+ KUNIT_EXPECT_EQ_MSG(test, num_rmid, c->exp_rmid,
+ "case %d: %u closids, %u rmids, %u monitors",
+ i, c->num_closid, c->num_rmid, c->num_mon);
+ KUNIT_EXPECT_LE_MSG(test, num_closid * num_rmid, c->num_mon,
+ "case %d: more groups than monitors", i);
+ }
+}
+
static struct kunit_case mpam_resctrl_test_cases[] = {
+ KUNIT_CASE(test_cap_closid_rmid),
KUNIT_CASE(test_get_mba_granularity),
KUNIT_CASE_PARAM(test_mbw_max_to_percent, test_percent_value_gen_params),
KUNIT_CASE_PARAM(test_percent_to_mbw_max, test_percent_value_gen_params),
--
2.34.1