[RFC PATCH v2 1/4] resctrl: Add registered device assignment

From: Zhanpeng Zhang

Date: Mon Nov 16 2026 - 02:14:10 EST


Keep requester discovery and hardware programming in providers. Common
resctrl owns a bus/name registry and resource-group membership, with one
sleepable set_ids callback per provider. Keep filesystem integration small
and separate from the registry implementation.

Snapshot runtime availability at mount, serialize assignment with
unregister, reject removal of assigned groups, and quarantine ID reuse
after an uncertain hardware update. Recovery and dynamic creation of the
first devices file are intentionally outside this RFC.

Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@xxxxxxxxxxxxx>
---
fs/resctrl/Makefile | 2 +-
fs/resctrl/devices.c | 233 +++++++++++++++++++++++++++++++++
fs/resctrl/internal.h | 8 ++
fs/resctrl/rdtgroup.c | 28 ++++
include/linux/resctrl_device.h | 53 ++++++++
5 files changed, 323 insertions(+), 1 deletion(-)
create mode 100644 fs/resctrl/devices.c
create mode 100644 include/linux/resctrl_device.h

diff --git a/fs/resctrl/Makefile b/fs/resctrl/Makefile
index e67f34d2236a..1cd663c3309a 100644
--- a/fs/resctrl/Makefile
+++ b/fs/resctrl/Makefile
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
-obj-$(CONFIG_RESCTRL_FS) += rdtgroup.o ctrlmondata.o monitor.o
+obj-$(CONFIG_RESCTRL_FS) += rdtgroup.o ctrlmondata.o monitor.o devices.o
obj-$(CONFIG_RESCTRL_FS_PSEUDO_LOCK) += pseudo_lock.o

# To allow define_trace.h's recursive include:
diff --git a/fs/resctrl/devices.c b/fs/resctrl/devices.c
new file mode 100644
index 000000000000..f98d375e6934
--- /dev/null
+++ b/fs/resctrl/devices.c
@@ -0,0 +1,233 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Registered, independently tagged devices assigned to resctrl groups. */
+#include <linux/capability.h>
+#include <linux/device.h>
+#include <linux/resctrl_device.h>
+#include <linux/slab.h>
+
+#include "internal.h"
+
+/* All registry, ownership and callback access is under rdtgroup_mutex. */
+struct resctrl_device {
+ struct list_head list;
+ struct device *dev;
+ const struct resctrl_device_ops *ops;
+ struct rdtgroup *group; /* NULL denotes the default group. */
+ char *token; /* Stable bus/name identity; not a sysfs pathname. */
+};
+
+static LIST_HEAD(resctrl_devices);
+/* No recovery protocol yet: never recycle IDs after an uncertain update. */
+static bool devices_quarantined;
+/* File visibility is a mount-time capability snapshot. */
+static bool devices_enabled;
+
+static struct resctrl_device *resctrl_device_find(struct device *dev)
+{
+ struct resctrl_device *entry;
+
+ lockdep_assert_held(&rdtgroup_mutex);
+ list_for_each_entry(entry, &resctrl_devices, list)
+ if (entry->dev == dev)
+ return entry;
+ return NULL;
+}
+
+int resctrl_device_register(struct device *dev,
+ const struct resctrl_device_ops *ops)
+{
+ struct resctrl_device *entry, *other;
+ int ret = 0;
+
+ if (!dev->bus || !ops || !ops->set_ids ||
+ strpbrk(dev->bus->name, "/ \t\n\r\v\f,") ||
+ strpbrk(dev_name(dev), "/ \t\n\r\v\f,"))
+ return -EINVAL;
+
+ entry = kzalloc_obj(*entry);
+ if (!entry)
+ return -ENOMEM;
+ entry->token = kasprintf(GFP_KERNEL, "%s/%s", dev->bus->name,
+ dev_name(dev));
+ if (!entry->token) {
+ kfree(entry);
+ return -ENOMEM;
+ }
+ entry->dev = get_device(dev);
+ entry->ops = ops;
+
+ mutex_lock(&rdtgroup_mutex);
+ list_for_each_entry(other, &resctrl_devices, list) {
+ if (other->dev == dev || !strcmp(other->token, entry->token)) {
+ ret = -EEXIST;
+ goto out;
+ }
+ }
+ list_add_tail(&entry->list, &resctrl_devices);
+out:
+ mutex_unlock(&rdtgroup_mutex);
+ if (ret) {
+ put_device(entry->dev);
+ kfree(entry->token);
+ kfree(entry);
+ }
+ return ret;
+}
+EXPORT_SYMBOL_GPL(resctrl_device_register);
+
+void resctrl_device_unregister(struct device *dev)
+{
+ struct resctrl_device *entry;
+
+ mutex_lock(&rdtgroup_mutex);
+ entry = resctrl_device_find(dev);
+ if (!entry)
+ goto out;
+
+ if (entry->ops->set_ids(dev, (struct resctrl_device_ids){})) {
+ devices_quarantined = true;
+ dev_err(dev, "resctrl: reset failed; ID reuse disabled\n");
+ }
+ list_del(&entry->list);
+ put_device(entry->dev);
+ kfree(entry->token);
+ kfree(entry);
+out:
+ mutex_unlock(&rdtgroup_mutex);
+}
+EXPORT_SYMBOL_GPL(resctrl_device_unregister);
+
+bool resctrl_devices_visible(void)
+{
+ lockdep_assert_held(&rdtgroup_mutex);
+ return devices_enabled;
+}
+
+int resctrl_devices_prepare_mount(void)
+{
+ lockdep_assert_held(&rdtgroup_mutex);
+ if (devices_quarantined)
+ return -EIO;
+ devices_enabled = !list_empty(&resctrl_devices);
+ return 0;
+}
+
+/* Must precede freeing any resource group or its IDs. */
+void resctrl_devices_unmount(void)
+{
+ struct resctrl_device *entry;
+
+ lockdep_assert_held(&rdtgroup_mutex);
+ list_for_each_entry(entry, &resctrl_devices, list) {
+ if (entry->group &&
+ entry->ops->set_ids(entry->dev,
+ (struct resctrl_device_ids){})) {
+ devices_quarantined = true;
+ dev_err(entry->dev, "resctrl: unmount reset failed\n");
+ }
+ entry->group = NULL;
+ }
+ devices_enabled = false;
+}
+
+/* Includes monitor children when the caller changes/removes a CTRL group. */
+int resctrl_devices_check_group(struct rdtgroup *group)
+{
+ struct resctrl_device *entry;
+
+ lockdep_assert_held(&rdtgroup_mutex);
+ if (devices_quarantined) {
+ rdt_last_cmd_puts("Device QoS state uncertain; reboot required\n");
+ return -EIO;
+ }
+ list_for_each_entry(entry, &resctrl_devices, list) {
+ if (entry->group == group ||
+ (entry->group && entry->group->type == RDTMON_GROUP &&
+ entry->group->mon.parent == group)) {
+ rdt_last_cmd_puts("Move assigned devices to the default group first\n");
+ return -EBUSY;
+ }
+ }
+ return 0;
+}
+
+int resctrl_devices_show(struct kernfs_open_file *of, struct seq_file *s,
+ void *v)
+{
+ struct rdtgroup *group = rdtgroup_kn_lock_live(of->kn);
+ struct resctrl_device *entry;
+ int ret = 0;
+
+ if (!group) {
+ ret = -ENOENT;
+ goto out;
+ }
+ if (devices_quarantined) {
+ ret = -EIO;
+ goto out;
+ }
+ if (group == &rdtgroup_default)
+ group = NULL;
+ list_for_each_entry(entry, &resctrl_devices, list)
+ if (entry->group == group)
+ seq_printf(s, "%s\n", entry->token);
+out:
+ rdtgroup_kn_unlock(of->kn);
+ return ret;
+}
+
+ssize_t resctrl_devices_write(struct kernfs_open_file *of, char *buf,
+ size_t nbytes, loff_t off)
+{
+ struct resctrl_device_ids ids;
+ struct resctrl_device *entry;
+ struct rdtgroup *group;
+ char *token;
+ int ret = -ENODEV;
+
+ if (!capable(CAP_SYS_ADMIN))
+ return -EPERM;
+ group = rdtgroup_kn_lock_live(of->kn);
+ if (!group) {
+ ret = -ENOENT;
+ goto out;
+ }
+ if (off || memchr(buf, '\0', nbytes)) {
+ ret = -EINVAL;
+ goto out;
+ }
+ token = strim(buf);
+ if (devices_quarantined) {
+ ret = -EIO;
+ goto out;
+ }
+ if (group->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
+ group->mode == RDT_MODE_PSEUDO_LOCKED ||
+ (group->type == RDTMON_GROUP &&
+ group->mon.parent->mode != RDT_MODE_SHAREABLE)) {
+ ret = -EBUSY;
+ goto out;
+ }
+ /* One registered identity per write, with no path or list parsing. */
+ if (!*token || strpbrk(token, " \t\n\r\v\f,")) {
+ ret = -EINVAL;
+ goto out;
+ }
+ ids.closid = group->closid;
+ ids.rmid = group->mon.rmid;
+ list_for_each_entry(entry, &resctrl_devices, list) {
+ if (strcmp(entry->token, token))
+ continue;
+ ret = entry->ops->set_ids(entry->dev, ids);
+ if (!ret)
+ entry->group = group == &rdtgroup_default ? NULL : group;
+ else if (ret == -EIO || ret == -ETIMEDOUT)
+ devices_quarantined = true;
+ break;
+ }
+out:
+ if (ret)
+ rdt_last_cmd_printf("Device assignment failed: %d\n", ret);
+ rdtgroup_kn_unlock(of->kn);
+ return ret ? ret : nbytes;
+}
diff --git a/fs/resctrl/internal.h b/fs/resctrl/internal.h
index e62a277dee85..4bebd998bdb7 100644
--- a/fs/resctrl/internal.h
+++ b/fs/resctrl/internal.h
@@ -335,6 +335,14 @@ __printf(1, 2)
void rdt_last_cmd_printf(const char *fmt, ...);

struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn);
+bool resctrl_devices_visible(void);
+int resctrl_devices_prepare_mount(void);
+void resctrl_devices_unmount(void);
+int resctrl_devices_check_group(struct rdtgroup *group);
+int resctrl_devices_show(struct kernfs_open_file *of, struct seq_file *s,
+ void *v);
+ssize_t resctrl_devices_write(struct kernfs_open_file *of, char *buf,
+ size_t nbytes, loff_t off);
void rdtgroup_kn_unlock(struct kernfs_node *kn);
bool info_kn_lock(struct kernfs_node *kn);
void info_kn_unlock(struct kernfs_node *kn);
diff --git a/fs/resctrl/rdtgroup.c b/fs/resctrl/rdtgroup.c
index 57ab090072c2..4d3417273934 100644
--- a/fs/resctrl/rdtgroup.c
+++ b/fs/resctrl/rdtgroup.c
@@ -1560,6 +1560,9 @@ static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,
}
rdtgrp->mode = RDT_MODE_SHAREABLE;
} else if (!strcmp(buf, "exclusive")) {
+ ret = resctrl_devices_check_group(rdtgrp);
+ if (ret)
+ goto out;
if (!rdtgroup_mode_test_exclusive(rdtgrp)) {
ret = -EINVAL;
goto out;
@@ -1572,6 +1575,9 @@ static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,
rdtgrp->mode = RDT_MODE_EXCLUSIVE;
} else if (IS_ENABLED(CONFIG_RESCTRL_FS_PSEUDO_LOCK) &&
!strcmp(buf, "pseudo-locksetup")) {
+ ret = resctrl_devices_check_group(rdtgrp);
+ if (ret)
+ goto out;
ret = rdtgroup_locksetup_enter(rdtgrp);
if (ret)
goto out;
@@ -2158,6 +2164,14 @@ static struct rftype res_common_files[] = {
.seq_show = rdtgroup_tasks_show,
.fflags = RFTYPE_BASE,
},
+ {
+ .name = "devices",
+ .mode = 0600,
+ .kf_ops = &rdtgroup_kf_single_ops,
+ .write = resctrl_devices_write,
+ .seq_show = resctrl_devices_show,
+ .fflags = RFTYPE_BASE,
+ },
{
.name = "mon_hw_id",
.mode = 0444,
@@ -2225,6 +2239,8 @@ static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
fflags |= RFTYPE_DEBUG;

for (rft = rfts; rft < rfts + len; rft++) {
+ if (!strcmp(rft->name, "devices") && !resctrl_devices_visible())
+ continue;
if (rft->fflags && ((fflags & rft->fflags) == rft->fflags)) {
ret = rdtgroup_add_file(kn, rft);
if (ret)
@@ -3132,6 +3148,7 @@ static void resctrl_fs_teardown(void)
if (!rdtgroup_default.kn)
return;

+ resctrl_devices_unmount();
rmdir_all_sub();
rdtgroup_unassign_cntrs(&rdtgroup_default);
mon_put_kn_priv();
@@ -3192,6 +3209,9 @@ static int rdt_get_tree(struct fs_context *fc)
ret = -EBUSY;
goto out;
}
+ ret = resctrl_devices_prepare_mount();
+ if (ret)
+ goto out;

ret = setup_rmid_lru_list();
if (ret)
@@ -4278,6 +4298,9 @@ static int rdtgroup_rmdir(struct kernfs_node *kn)
goto out;
}
parent_kn = rdt_kn_parent(kn);
+ ret = resctrl_devices_check_group(rdtgrp);
+ if (ret)
+ goto out;

/*
* If the rdtgroup is a ctrl_mon group and parent directory
@@ -4394,6 +4417,11 @@ static int rdtgroup_rename(struct kernfs_node *kn,
ret = -EPERM;
goto out;
}
+ if (rdtgrp->mon.parent != new_prdtgrp) {
+ ret = resctrl_devices_check_group(rdtgrp);
+ if (ret)
+ goto out;
+ }

/*
* If the MON group is monitoring CPUs, the CPUs must be assigned to the
diff --git a/include/linux/resctrl_device.h b/include/linux/resctrl_device.h
new file mode 100644
index 000000000000..a39f674739d6
--- /dev/null
+++ b/include/linux/resctrl_device.h
@@ -0,0 +1,53 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_RESCTRL_DEVICE_H
+#define _LINUX_RESCTRL_DEVICE_H
+
+#include <linux/errno.h>
+#include <linux/kconfig.h>
+#include <linux/types.h>
+
+struct device;
+
+/* Resource-group IDs, interpreted by the architecture's request tagger. */
+struct resctrl_device_ids {
+ u32 closid;
+ u32 rmid;
+};
+
+/**
+ * struct resctrl_device_ops - program one independently tagged requester
+ * @set_ids: Complete the update before returning zero. Validation errors must
+ * leave the old IDs intact. Return -EIO if hardware state is uncertain;
+ * resctrl then prevents ID reuse. Called in sleepable context, serialized
+ * with assignment and unregister. Must not call back into resctrl.
+ */
+struct resctrl_device_ops {
+ int (*set_ids)(struct device *dev, struct resctrl_device_ids ids);
+};
+
+#ifdef CONFIG_RESCTRL_FS
+/*
+ * Register only after hardware and driver-private state are ready. The bus and
+ * device names must remain stable until unregister. Initial IDs must be zero.
+ * The driver must own the tag fields exclusively, including across reattach.
+ */
+int resctrl_device_register(struct device *dev,
+ const struct resctrl_device_ops *ops);
+
+/*
+ * Remove the requester and drain callbacks BEFORE freeing driver state.
+ * This also resets its IDs. A failed reset quarantines resctrl until reboot,
+ * rather than recycling IDs which the device might still be using.
+ */
+void resctrl_device_unregister(struct device *dev);
+#else
+static inline int
+resctrl_device_register(struct device *dev, const struct resctrl_device_ops *ops)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline void resctrl_device_unregister(struct device *dev) { }
+#endif
+
+#endif
--
2.50.1 (Apple Git-155)