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 12/13] selftests/resctrl: Add a test for control ID allocation and reuse
Date: Mon, 30 Nov 2026 10:29:28 +0800 [thread overview]
Message-ID: <20261130022929.71748-13-cuiyunhui@bytedance.com> (raw)
In-Reply-To: <20261130022929.71748-1-cuiyunhui@bytedance.com>
A control group that reads the root policy may either share the root
entry or own a private entry with the same value. Add a CTRL_ID test that
distinguishes controller-local and identity modes with a root policy
update, checks that same-value writes allocate private entries, releases
and reuses an entry after exhaustion, and verifies that a reused direct
ID does not retain the previous policy.
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
.../testing/selftests/resctrl/ctrl_id_test.c | 380 ++++++++++++++++++
tools/testing/selftests/resctrl/resctrl.h | 1 +
.../testing/selftests/resctrl/resctrl_tests.c | 1 +
3 files changed, 382 insertions(+)
create mode 100644 tools/testing/selftests/resctrl/ctrl_id_test.c
diff --git a/tools/testing/selftests/resctrl/ctrl_id_test.c b/tools/testing/selftests/resctrl/ctrl_id_test.c
new file mode 100644
index 0000000000000..e9b26ff2d6b5e
--- /dev/null
+++ b/tools/testing/selftests/resctrl/ctrl_id_test.c
@@ -0,0 +1,380 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test the relationship between resctrl control groups and hardware entries.
+ */
+#include "resctrl.h"
+
+#include <fcntl.h>
+
+#define CTRL_ID_MAX_GROUPS 64
+#define CTRL_ID_NAME_LEN 64
+#define CTRL_ID_VALUE_LEN 64
+
+struct ctrl_id_group {
+ char name[CTRL_ID_NAME_LEN];
+ bool created;
+};
+
+struct ctrl_id_ctx {
+ struct ctrl_id_group groups[CTRL_ID_MAX_GROUPS];
+ char root_value[CTRL_ID_VALUE_LEN];
+ const char *resource;
+ int domain_id;
+ bool root_changed;
+};
+
+static struct ctrl_id_ctx ctrl_id_ctx;
+
+static int ctrl_id_schemata_path(char *path, size_t size, const char *group)
+{
+ int len;
+
+ if (group)
+ len = snprintf(path, size, "%s/%s/schemata", RESCTRL_PATH,
+ group);
+ else
+ len = snprintf(path, size, "%s/schemata", RESCTRL_PATH);
+
+ if (len < 0 || (size_t)len >= size)
+ return -ENAMETOOLONG;
+
+ return 0;
+}
+
+static int ctrl_id_read_policy(const char *group, char *value, size_t size)
+{
+ char path[PATH_MAX];
+ char *line = NULL;
+ size_t line_size = 0;
+ FILE *fp;
+ int ret;
+
+ ret = ctrl_id_schemata_path(path, sizeof(path), group);
+ if (ret)
+ return ret;
+
+ fp = fopen(path, "r");
+ if (!fp)
+ return -errno;
+
+ ret = -ENOENT;
+ while (getline(&line, &line_size, fp) >= 0) {
+ char *entry;
+ char *resource_line = line;
+ char *saveptr;
+ size_t resource_len;
+
+ while (isspace(*resource_line))
+ resource_line++;
+ resource_len = strlen(ctrl_id_ctx.resource);
+ if (strncmp(resource_line, ctrl_id_ctx.resource, resource_len) ||
+ resource_line[resource_len] != ':')
+ continue;
+
+ entry = strtok_r(resource_line + resource_len + 1, ";\n",
+ &saveptr);
+ while (entry) {
+ char current[CTRL_ID_VALUE_LEN];
+ unsigned int domain;
+ int len;
+
+ if (sscanf(entry, "%u=%63s", &domain, current) == 2 &&
+ domain == ctrl_id_ctx.domain_id) {
+ len = snprintf(value, size, "%s", current);
+ if (len < 0 || (size_t)len >= size)
+ ret = -ENOSPC;
+ else
+ ret = 0;
+ goto out;
+ }
+ entry = strtok_r(NULL, ";\n", &saveptr);
+ }
+ }
+
+out:
+ free(line);
+ fclose(fp);
+ return ret;
+}
+
+static int ctrl_id_write_policy(const char *group, const char *value)
+{
+ char path[PATH_MAX];
+ char schemata[256];
+ ssize_t written;
+ int fd;
+ int len;
+ int ret;
+
+ ret = ctrl_id_schemata_path(path, sizeof(path), group);
+ if (ret)
+ return ret;
+
+ len = snprintf(schemata, sizeof(schemata), "%s:%d=%s\n",
+ ctrl_id_ctx.resource, ctrl_id_ctx.domain_id, value);
+ if (len < 0 || len >= sizeof(schemata))
+ return -ENAMETOOLONG;
+
+ fd = open(path, O_WRONLY);
+ if (fd < 0)
+ return -errno;
+
+ written = write(fd, schemata, len);
+ ret = written < 0 ? -errno : 0;
+ close(fd);
+ if (!ret && written != len)
+ ret = -EIO;
+
+ return ret;
+}
+
+static int ctrl_id_create_group(unsigned int index)
+{
+ struct ctrl_id_group *group = &ctrl_id_ctx.groups[index];
+ char path[PATH_MAX];
+ int len;
+
+ len = snprintf(group->name, sizeof(group->name), "ctrl-id-%d-%u",
+ getpid(), index);
+ if (len < 0 || len >= sizeof(group->name))
+ return -ENAMETOOLONG;
+
+ len = snprintf(path, sizeof(path), "%s/%s", RESCTRL_PATH,
+ group->name);
+ if (len < 0 || len >= sizeof(path))
+ return -ENAMETOOLONG;
+
+ if (mkdir(path, 0755))
+ return -errno;
+
+ group->created = true;
+ return 0;
+}
+
+static int ctrl_id_remove_group(unsigned int index)
+{
+ struct ctrl_id_group *group = &ctrl_id_ctx.groups[index];
+ char path[PATH_MAX];
+ int len;
+
+ if (!group->created)
+ return 0;
+
+ len = snprintf(path, sizeof(path), "%s/%s", RESCTRL_PATH,
+ group->name);
+ if (len < 0 || len >= sizeof(path))
+ return -ENAMETOOLONG;
+
+ if (rmdir(path))
+ return -errno;
+
+ group->created = false;
+ return 0;
+}
+
+static void ctrl_id_cleanup(void)
+{
+ int i;
+
+ if (ctrl_id_ctx.root_changed) {
+ ctrl_id_write_policy(NULL, ctrl_id_ctx.root_value);
+ ctrl_id_ctx.root_changed = false;
+ }
+
+ for (i = CTRL_ID_MAX_GROUPS - 1; i >= 0; i--)
+ ctrl_id_remove_group(i);
+
+ memset(&ctrl_id_ctx, 0, sizeof(ctrl_id_ctx));
+}
+
+static int ctrl_id_changed_policy(const char *original, char *changed,
+ size_t size)
+{
+ unsigned int min_bits;
+ unsigned long candidate;
+ unsigned long mask;
+ unsigned int first;
+ char *end;
+ int len;
+
+ if (resource_info_unsigned_get(ctrl_id_ctx.resource, "min_cbm_bits",
+ &min_bits))
+ return -ENOENT;
+
+ errno = 0;
+ mask = strtoul(original, &end, 16);
+ if (errno || *end || !mask)
+ return -EINVAL;
+
+ first = __builtin_ctzl(mask);
+ if (min_bits >= sizeof(mask) * 8 - first)
+ return -ERANGE;
+
+ candidate = ((1UL << min_bits) - 1) << first;
+ if (candidate == mask || (candidate & mask) != candidate)
+ return -ERANGE;
+
+ len = snprintf(changed, size, "%lx", candidate);
+ if (len < 0 || (size_t)len >= size)
+ return -ENAMETOOLONG;
+
+ return 0;
+}
+
+static bool ctrl_id_feature_check(const struct resctrl_test *test)
+{
+ unsigned int num_closids;
+
+ if (!resctrl_resource_exists(test->resource))
+ return false;
+
+ if (resource_info_unsigned_get(test->resource, "num_closids",
+ &num_closids))
+ return false;
+
+ return num_closids >= 3;
+}
+
+static int ctrl_id_run_test(const struct resctrl_test *test,
+ const struct user_params *uparams)
+{
+ char changed[CTRL_ID_VALUE_LEN];
+ char current[CTRL_ID_VALUE_LEN];
+ unsigned int num_closids;
+ unsigned int group_count;
+ unsigned int pending = 0;
+ bool local_mapping;
+ int ret;
+ unsigned int i;
+
+ memset(&ctrl_id_ctx, 0, sizeof(ctrl_id_ctx));
+ ctrl_id_ctx.resource = test->resource;
+
+ ret = get_domain_id(test->resource, uparams->cpu,
+ &ctrl_id_ctx.domain_id);
+ if (ret)
+ return ret;
+
+ ret = resource_info_unsigned_get(test->resource, "num_closids",
+ &num_closids);
+ if (ret)
+ return ret;
+
+ ret = ctrl_id_read_policy(NULL, ctrl_id_ctx.root_value,
+ sizeof(ctrl_id_ctx.root_value));
+ if (ret)
+ return ret;
+
+ group_count = num_closids - 1;
+ if (group_count > CTRL_ID_MAX_GROUPS)
+ group_count = CTRL_ID_MAX_GROUPS;
+
+ for (i = 0; i < group_count; i++) {
+ ret = ctrl_id_create_group(i);
+ if (ret == -ENOSPC && i >= 2) {
+ group_count = i;
+ break;
+ }
+ if (ret)
+ return ret;
+ ret = ctrl_id_read_policy(ctrl_id_ctx.groups[i].name, current,
+ sizeof(current));
+ if (ret || strcmp(current, ctrl_id_ctx.root_value))
+ return ret ?: -EINVAL;
+ }
+
+ ret = ctrl_id_write_policy(ctrl_id_ctx.groups[0].name,
+ ctrl_id_ctx.root_value);
+ if (ret)
+ return ret;
+
+ ret = ctrl_id_changed_policy(ctrl_id_ctx.root_value, changed,
+ sizeof(changed));
+ if (ret)
+ return ret;
+
+ ctrl_id_ctx.root_changed = true;
+ ret = ctrl_id_write_policy(NULL, changed);
+ if (ret)
+ return ret;
+
+ ret = ctrl_id_read_policy(ctrl_id_ctx.groups[0].name, current,
+ sizeof(current));
+ if (ret || strcmp(current, ctrl_id_ctx.root_value))
+ return ret ?: -EINVAL;
+
+ ret = ctrl_id_read_policy(ctrl_id_ctx.groups[1].name, current,
+ sizeof(current));
+ if (ret)
+ return ret;
+ if (!strcmp(current, changed))
+ local_mapping = true;
+ else if (!strcmp(current, ctrl_id_ctx.root_value))
+ local_mapping = false;
+ else
+ return -EINVAL;
+ ksft_print_msg("Detected %s control ID mode\n",
+ local_mapping ? "controller-local" : "identity");
+
+ ret = ctrl_id_write_policy(NULL, ctrl_id_ctx.root_value);
+ if (ret)
+ return ret;
+ ctrl_id_ctx.root_changed = false;
+
+ if (!local_mapping) {
+ ret = ctrl_id_write_policy(ctrl_id_ctx.groups[0].name,
+ changed);
+ if (ret)
+ return ret;
+ ret = ctrl_id_remove_group(0);
+ if (ret)
+ return ret;
+ ret = ctrl_id_create_group(0);
+ if (ret)
+ return ret;
+ ret = ctrl_id_read_policy(ctrl_id_ctx.groups[0].name,
+ current, sizeof(current));
+ if (ret)
+ return ret;
+ return strcmp(current, ctrl_id_ctx.root_value) ? -EINVAL : 0;
+ }
+
+ for (i = 1; i < group_count; i++) {
+ ret = ctrl_id_write_policy(ctrl_id_ctx.groups[i].name,
+ ctrl_id_ctx.root_value);
+ if (ret == -ENOSPC) {
+ pending = i;
+ break;
+ }
+ if (ret)
+ return ret;
+ }
+
+ if (!pending) {
+ ksft_print_msg("Physical control entry exhaustion was not reached\n");
+ return 0;
+ }
+ ksft_print_msg("Physical entry exhaustion reached at test group %u\n",
+ pending);
+
+ ret = ctrl_id_remove_group(0);
+ if (ret)
+ return ret;
+
+ ret = ctrl_id_write_policy(ctrl_id_ctx.groups[pending].name,
+ ctrl_id_ctx.root_value);
+ if (ret)
+ return ret;
+ ksft_print_msg("Released entry reused by test group %u\n", pending);
+
+ return 0;
+}
+
+struct resctrl_test ctrl_id_test = {
+ .name = "CTRL_ID",
+ .group = "CTRL_ID",
+ .resource = "L3",
+ .feature_check = ctrl_id_feature_check,
+ .run_test = ctrl_id_run_test,
+ .cleanup = ctrl_id_cleanup,
+};
diff --git a/tools/testing/selftests/resctrl/resctrl.h b/tools/testing/selftests/resctrl/resctrl.h
index a7556cdae0de3..6b727b080e0b8 100644
--- a/tools/testing/selftests/resctrl/resctrl.h
+++ b/tools/testing/selftests/resctrl/resctrl.h
@@ -244,5 +244,6 @@ extern struct resctrl_test cmt_test;
extern struct resctrl_test l3_cat_test;
extern struct resctrl_test l3_noncont_cat_test;
extern struct resctrl_test l2_noncont_cat_test;
+extern struct resctrl_test ctrl_id_test;
#endif /* RESCTRL_H */
diff --git a/tools/testing/selftests/resctrl/resctrl_tests.c b/tools/testing/selftests/resctrl/resctrl_tests.c
index dbcd5eea9fbc9..c706912b5ae3d 100644
--- a/tools/testing/selftests/resctrl/resctrl_tests.c
+++ b/tools/testing/selftests/resctrl/resctrl_tests.c
@@ -21,6 +21,7 @@ static struct resctrl_test *resctrl_tests[] = {
&l3_cat_test,
&l3_noncont_cat_test,
&l2_noncont_cat_test,
+ &ctrl_id_test,
};
static unsigned int detect_vendor(void)
--
2.39.2 (Apple Git-143)
next prev parent reply other threads:[~2026-11-30 2:31 UTC|newest]
Thread overview: 21+ 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 ` [RFC PATCH 06/13] arm_mpam: Add an internal PARTID allocator Yunhui Cui
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 ` Yunhui Cui [this message]
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
2026-11-30 14:09 ` [External] " yunhui cui
2026-11-30 15:22 ` Ben Horgan
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-13-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®