* [RFC PATCH v2 0/4] resctrl: Assign registered devices to resource groups
@ 2026-11-16 7:13 Zhanpeng Zhang
2026-11-16 7:13 ` [RFC PATCH v2 1/4] resctrl: Add registered device assignment Zhanpeng Zhang
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Zhanpeng Zhang @ 2026-11-16 7:13 UTC (permalink / raw)
To: joro, palmer, tony.luck, reinette.chatre, tomasz.jeznach
Cc: fustini, jgg, james.morse, ben.horgan, xiaqinxin, will,
robin.murphy, pjw, aou, alex, Dave.Martin, babu.moger,
kevin.tian, corbet, skhan, shuah, randy.dunlap, cuiyunhui,
yuanzhu, iommu, linux-riscv, linux-kernel, linux-doc,
linux-kselftest, x86
This RFC lets resctrl groups include devices as well as CPU tasks. It
uses the RISC-V IOMMU QoS ID extension as the first provider and builds
on Drew Fustini's CBQRI resctrl v12 series [1] on Linux v7.4-rc1.
RISC-V QoS tags requests with a resource control ID (RCID) and a
monitoring counter ID (MCID). On successful translations the IOMMU
supplies the device context's IDs to the I/O bridge for device requests.
Those IDs also tag IOMMU accesses to context-specific metadata, such as
page tables. IOMMU accesses to the device directory, queues and
IOMMU-generated MSIs use the per-IOMMU iommu_qosid register. When the
IOMMU is in DDTP Bare mode, device requests use that register too.
The IOMMU supplies the tags; downstream controllers apply the policy.
In v1, the unit of assignment was an IOMMU group. In v2, the driver
responsible for tagging explicitly registers supported devices with
resctrl. Here that is the RISC-V IOMMU driver, so endpoint drivers such
as NVMe do not need to change. Common resctrl code handles token
lookup and group membership. When a device is assigned, resctrl calls
the driver's set_ids() callback to program its device context. The
devices file remains separate from tasks.
resctrl control group
schemata ---------> CBQRI controller: resource policy
tasks ---------> CPU: task IDs
devices ---------> IOMMU: device context IDs
For example, for an independently tagged PCI device:
mkdir /sys/fs/resctrl/p1
echo pci/0000:00:03.0 > /sys/fs/resctrl/p1/devices
echo pci/0000:00:03.0 > /sys/fs/resctrl/devices
rmdir /sys/fs/resctrl/p1
The devices file appears only if at least one device is registered at
mount time. A device being present in sysfs does not make it assignable.
The IOMMU driver excludes shared hardware contexts and requires known QoS
ID limits before registering a device. It probes those limits only with
DDTP set to Off and all queues disabled and idle, and rejects out-of-range
IDs before changing a device context.
The separate qosid/{rcid,mcid} attributes under the IOMMU class device
configure the global register independently of resctrl.
The v12 dependency provides cache capacity allocation, not monitoring.
Nonzero device MCIDs require a monitoring-capable dependency.
Changes since RFC v1:
* Assign devices rather than IOMMU groups, as Jason and Qinxin
suggested [2]. Drop the IOMMU core group lookup and walk helpers.
* Build on James's driver callback suggestion [2] with explicit
device registration. Handle token lookup in common resctrl rather
than in each architecture, as Drew suggested [3].
* Keep devices separate from tasks so the existing ABI stays
unchanged [4]. Following Drew's runtime visibility suggestion [5],
check for registered devices at mount time.
* Split the global sysfs setting into separate rcid and mcid files
[6], and introduce qosid_lock in the patch that first uses it [7].
Thanks to Drew for both reviews.
* Rebase on CBQRI v12, check implemented QoS ID widths before device
assignment, and test task and device membership in the same
control group.
Feedback would be particularly helpful on:
* The provider registration and lifetime contract, including how to
represent devices that share a tagging context.
* The bus/name token versus using an existing sysfs identity.
* The scope of protection after a command queue failure. If an update
leaves the hardware state uncertain, the current code prevents ID
reuse by blocking group removal and future mounts, including for
groups unrelated to the failed update. Would reserving only the
affected old and target IDs be sufficient?
PASID assignment and recovery after an uncertain hardware update are
outside this RFC.
[1] https://lore.kernel.org/linux-riscv/20261114-dfustini-atl-sc-cbqri-dt-v12-0-6ef1f627072d@kernel.org/
[2] https://lore.kernel.org/all/arKx__kOPe4B5gDW@agluck-desk3/T/
[3] https://lore.kernel.org/all/an4CRRXt44%2BPhHgy@x1/
[4] https://lore.kernel.org/all/7cf591e8-e182-447f-9808-d8e328d28f79@arm.com/#t
[5] https://lore.kernel.org/all/anvYGuNF3MC%2F%2FQ94@x1/
[6] https://lore.kernel.org/all/andiub47zsUsL4qh@x1/
[7] https://lore.kernel.org/all/andiWrKhz5rLPVuq@x1/
Zhanpeng Zhang (4):
resctrl: Add registered device assignment
iommu/riscv: Register independently tagged devices with resctrl
iommu/riscv: Expose global QoS IDs in sysfs
selftests/resctrl: Exercise device assignment and IOMMU global IDs
.../ABI/testing/sysfs-class-iommu-riscv-iommu | 27 ++
.../filesystems/resctrl-devices-prototype.rst | 43 ++
MAINTAINERS | 8 +
drivers/iommu/riscv/iommu-bits.h | 8 +
drivers/iommu/riscv/iommu.c | 398 +++++++++++++++++-
drivers/iommu/riscv/iommu.h | 16 +
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 +++
tools/testing/selftests/resctrl/Makefile | 2 +
tools/testing/selftests/resctrl/devices.sh | 205 +++++++++
.../testing/selftests/resctrl/iommu_qosid.sh | 119 ++++++
14 files changed, 1143 insertions(+), 7 deletions(-)
create mode 100644 Documentation/ABI/testing/sysfs-class-iommu-riscv-iommu
create mode 100644 Documentation/filesystems/resctrl-devices-prototype.rst
create mode 100644 fs/resctrl/devices.c
create mode 100644 include/linux/resctrl_device.h
create mode 100755 tools/testing/selftests/resctrl/devices.sh
create mode 100755 tools/testing/selftests/resctrl/iommu_qosid.sh
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
prerequisite-patch-id: 492fe81aa4f3c885ac5bb48252def2fdc5527275
prerequisite-patch-id: b4f0bd4bf660c92dbf4ca0c7d871c1550b66df0d
prerequisite-patch-id: a2e36f0788f8e11c708d162dce653351dd99c4f0
prerequisite-patch-id: d1d359c5cf89b2b5018d64ca8f320892f6dbcca2
prerequisite-patch-id: 5b7fea6f1ad8f2137ffe45681d56b83e17f7cb2a
--
2.50.1 (Apple Git-155)
^ permalink raw reply [flat|nested] 6+ messages in thread
* [RFC PATCH v2 1/4] resctrl: Add registered device assignment
2026-11-16 7:13 [RFC PATCH v2 0/4] resctrl: Assign registered devices to resource groups Zhanpeng Zhang
@ 2026-11-16 7:13 ` Zhanpeng Zhang
2026-11-16 7:13 ` [RFC PATCH v2 2/4] iommu/riscv: Register independently tagged devices with resctrl Zhanpeng Zhang
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Zhanpeng Zhang @ 2026-11-16 7:13 UTC (permalink / raw)
To: joro, palmer, tony.luck, reinette.chatre, tomasz.jeznach
Cc: fustini, jgg, james.morse, ben.horgan, xiaqinxin, will,
robin.murphy, pjw, aou, alex, Dave.Martin, babu.moger,
kevin.tian, corbet, skhan, shuah, randy.dunlap, cuiyunhui,
yuanzhu, iommu, linux-riscv, linux-kernel, linux-doc,
linux-kselftest, x86
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@bytedance.com>
---
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)
^ permalink raw reply [flat|nested] 6+ messages in thread
* [RFC PATCH v2 2/4] iommu/riscv: Register independently tagged devices with resctrl
2026-11-16 7:13 [RFC PATCH v2 0/4] resctrl: Assign registered devices to resource groups Zhanpeng Zhang
2026-11-16 7:13 ` [RFC PATCH v2 1/4] resctrl: Add registered device assignment Zhanpeng Zhang
@ 2026-11-16 7:13 ` Zhanpeng Zhang
2026-11-16 7:13 ` [RFC PATCH v2 3/4] iommu/riscv: Expose global QoS IDs in sysfs Zhanpeng Zhang
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Zhanpeng Zhang @ 2026-11-16 7:13 UTC (permalink / raw)
To: joro, palmer, tony.luck, reinette.chatre, tomasz.jeznach
Cc: fustini, jgg, james.morse, ben.horgan, xiaqinxin, will,
robin.murphy, pjw, aou, alex, Dave.Martin, babu.moger,
kevin.tian, corbet, skhan, shuah, randy.dunlap, cuiyunhui,
yuanzhu, iommu, linux-riscv, linux-kernel, linux-doc,
linux-kselftest, x86
Register independently tagged devices after default-domain setup and
retain their IDs in driver-private state across domain changes. Program
all firmware IDs under the context-update lock and report command
completion failures to resctrl.
The DC.ta field size does not describe the implemented QoS ID widths.
Probe the low bits of the global write-any, read-legal fields with DDTP
OFF and all queues disabled and idle. Preserve and verify restoration of
the original register. If the widths cannot be obtained safely, do not
register devices with resctrl. Reject IDs outside the probed range before
changing any device context.
Track exclusive device-context ownership without changing IOMMU core.
Discovering an alias resets and unregisters any previous QoS owner before
marking the context shared. Shared contexts are excluded from independent
QoS assignment until IOMMU reprobe, without failing ordinary DMA probing.
Serialize registration, alias discovery and release so an old owner
cannot become independently assignable again after its context is shared.
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
---
drivers/iommu/riscv/iommu-bits.h | 8 +
drivers/iommu/riscv/iommu.c | 269 ++++++++++++++++++++++++++++++-
drivers/iommu/riscv/iommu.h | 16 ++
3 files changed, 289 insertions(+), 4 deletions(-)
diff --git a/drivers/iommu/riscv/iommu-bits.h b/drivers/iommu/riscv/iommu-bits.h
index f2ef9bd3cde9..9dce98c5239c 100644
--- a/drivers/iommu/riscv/iommu-bits.h
+++ b/drivers/iommu/riscv/iommu-bits.h
@@ -63,6 +63,7 @@
#define RISCV_IOMMU_CAPABILITIES_PD8 BIT_ULL(38)
#define RISCV_IOMMU_CAPABILITIES_PD17 BIT_ULL(39)
#define RISCV_IOMMU_CAPABILITIES_PD20 BIT_ULL(40)
+#define RISCV_IOMMU_CAPABILITIES_QOSID BIT_ULL(41)
#define RISCV_IOMMU_CAPABILITIES_NL BIT_ULL(42)
#define RISCV_IOMMU_CAPABILITIES_S BIT_ULL(43)
@@ -274,6 +275,11 @@ enum riscv_iommu_hpmevent_id {
#define RISCV_IOMMU_TR_RESPONSE_SZ BIT_ULL(9)
#define RISCV_IOMMU_TR_RESPONSE_PPN RISCV_IOMMU_PPN_FIELD
+/* IOMMU global QoS IDs (32 bits). */
+#define RISCV_IOMMU_REG_IOMMU_QOSID 0x0270
+#define RISCV_IOMMU_IOMMU_QOSID_RCID GENMASK(11, 0)
+#define RISCV_IOMMU_IOMMU_QOSID_MCID GENMASK(27, 16)
+
/* 5.27 Interrupt cause to vector (64bits) */
#define RISCV_IOMMU_REG_ICVEC 0x02F8
#define RISCV_IOMMU_ICVEC_CIV GENMASK_ULL(3, 0)
@@ -371,6 +377,8 @@ enum riscv_iommu_dc_iohgatp_modes {
/* Translation attributes fields */
#define RISCV_IOMMU_DC_TA_PSCID GENMASK_ULL(31, 12)
+#define RISCV_IOMMU_DC_TA_RCID GENMASK_ULL(51, 40)
+#define RISCV_IOMMU_DC_TA_MCID GENMASK_ULL(63, 52)
/* First-stage context fields */
#define RISCV_IOMMU_DC_FSC_PPN RISCV_IOMMU_ATP_PPN_FIELD
diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c
index fbdca69c436e..dd2ebb25312d 100644
--- a/drivers/iommu/riscv/iommu.c
+++ b/drivers/iommu/riscv/iommu.c
@@ -22,6 +22,7 @@
#include <linux/iopoll.h>
#include <linux/kernel.h>
#include <linux/pci.h>
+#include <linux/resctrl_device.h>
#include <linux/generic_pt/iommu.h>
#include "../iommu-pages.h"
@@ -472,6 +473,7 @@ static int riscv_iommu_queue_send(struct riscv_iommu_queue *queue,
err_busy:
/* Report the failure and continue; full RAS recovery is not implemented. */
+ WRITE_ONCE(queue->iommu->qos_cmd_failed, true);
dev_err_once(queue->iommu->dev, "Hardware error: command enqueue failed\n");
return ret;
}
@@ -484,13 +486,22 @@ static int riscv_iommu_queue_send(struct riscv_iommu_queue *queue,
static irqreturn_t riscv_iommu_cmdq_process(int irq, void *data)
{
const struct riscv_iommu_queue *queue = (struct riscv_iommu_queue *)data;
+ unsigned long flags;
unsigned int ctrl;
/* Clear MF/CQ errors, complete error recovery to be implemented. */
+ spin_lock_irqsave(&queue->iommu->cmdq_error_lock, flags);
ctrl = riscv_iommu_readl(queue->iommu, queue->qcr);
if (ctrl & (RISCV_IOMMU_CQCSR_CQMF | RISCV_IOMMU_CQCSR_CMD_TO |
RISCV_IOMMU_CQCSR_CMD_ILL | RISCV_IOMMU_CQCSR_FENCE_W_IP)) {
+ if (ctrl & (RISCV_IOMMU_CQCSR_CQMF | RISCV_IOMMU_CQCSR_CMD_TO |
+ RISCV_IOMMU_CQCSR_CMD_ILL))
+ WRITE_ONCE(queue->iommu->qos_cmd_failed, true);
riscv_iommu_writel(queue->iommu, queue->qcr, ctrl);
+ }
+ spin_unlock_irqrestore(&queue->iommu->cmdq_error_lock, flags);
+ if (ctrl & (RISCV_IOMMU_CQCSR_CQMF | RISCV_IOMMU_CQCSR_CMD_TO |
+ RISCV_IOMMU_CQCSR_CMD_ILL | RISCV_IOMMU_CQCSR_FENCE_W_IP)) {
dev_warn(queue->iommu->dev,
"Queue #%u error; fault:%d timeout:%d illegal:%d fence_w_ip:%d\n",
queue->qid,
@@ -851,8 +862,16 @@ PT_IOMMU_CHECK_DOMAIN(struct riscv_iommu_domain, riscvpt.iommu, domain);
/* Private IOMMU data for managed devices, dev_iommu_priv_* */
struct riscv_iommu_info {
struct riscv_iommu_domain *domain;
+ /* Retained across domain changes, under the IOMMU's qosid_lock. */
+ struct resctrl_device_ids qos_ids;
};
+static u64 riscv_iommu_qos_ta(struct resctrl_device_ids ids)
+{
+ return FIELD_PREP(RISCV_IOMMU_DC_TA_RCID, ids.closid) |
+ FIELD_PREP(RISCV_IOMMU_DC_TA_MCID, ids.rmid);
+}
+
/*
* Linkage between an iommu_domain and attached devices.
*
@@ -1191,12 +1210,14 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
struct device *dev, u64 fsc, u64 ta)
{
struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
+ struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
struct riscv_iommu_dc *dc;
struct riscv_iommu_command cmd;
bool sync_required = false;
u64 tc;
int i;
+ mutex_lock(&iommu->qosid_lock);
for (i = 0; i < fwspec->num_ids; i++) {
dc = riscv_iommu_get_dc(iommu, fwspec->ids[i]);
tc = READ_ONCE(dc->tc);
@@ -1230,7 +1251,8 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
tc |= ta & RISCV_IOMMU_DC_TC_V;
WRITE_ONCE(dc->fsc, fsc);
- WRITE_ONCE(dc->ta, ta & RISCV_IOMMU_PC_TA_PSCID);
+ WRITE_ONCE(dc->ta, (ta & RISCV_IOMMU_PC_TA_PSCID) |
+ riscv_iommu_qos_ta(info->qos_ids));
/* Update device context, write TC.V as the last step. */
dma_wmb();
WRITE_ONCE(dc->tc, tc);
@@ -1247,6 +1269,7 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
}
riscv_iommu_cmd_sync(iommu, RISCV_IOMMU_IOTINVAL_TIMEOUT);
+ mutex_unlock(&iommu->qosid_lock);
}
/*
@@ -1460,6 +1483,38 @@ static int riscv_iommu_of_xlate(struct device *dev, const struct of_phandle_args
return iommu_fwspec_add_ids(dev, args->args, 1);
}
+/* Shared DIDs stay ineligible for independent QoS until IOMMU reprobe. */
+#define RISCV_IOMMU_SHARED_DC xa_mk_value(1)
+
+static int riscv_iommu_claim_qos_context(struct riscv_iommu_device *iommu,
+ struct device *dev, u32 did)
+{
+ struct device *owner;
+ int ret = 0;
+
+ lockdep_assert_held(&iommu->qos_registry_lock);
+ mutex_lock(&iommu->qosid_lock);
+ owner = xa_load(&iommu->dc_owners, did);
+ mutex_unlock(&iommu->qosid_lock);
+
+ /*
+ * Reset and remove the old assignment before an alias can use this DC.
+ * The registry lock keeps owner and its driver state alive here. Never
+ * take the resctrl mutex while holding qosid_lock.
+ */
+ if (owner && owner != dev && !xa_is_value(owner))
+ resctrl_device_unregister(owner);
+
+ mutex_lock(&iommu->qosid_lock);
+ if (!owner)
+ ret = xa_insert(&iommu->dc_owners, did, dev, GFP_KERNEL);
+ else if (owner != dev)
+ ret = xa_err(xa_store(&iommu->dc_owners, did,
+ RISCV_IOMMU_SHARED_DC, GFP_KERNEL));
+ mutex_unlock(&iommu->qosid_lock);
+ return ret;
+}
+
static struct iommu_device *riscv_iommu_probe_device(struct device *dev)
{
struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
@@ -1467,7 +1522,7 @@ static struct iommu_device *riscv_iommu_probe_device(struct device *dev)
struct riscv_iommu_info *info;
struct riscv_iommu_dc *dc;
u64 tc;
- int i;
+ int i, ret;
if (!fwspec || !fwspec->iommu_fwnode->dev || !fwspec->num_ids)
return ERR_PTR(-ENODEV);
@@ -1486,31 +1541,174 @@ static struct iommu_device *riscv_iommu_probe_device(struct device *dev)
info = kzalloc_obj(*info);
if (!info)
return ERR_PTR(-ENOMEM);
+
+ mutex_lock(&iommu->qos_registry_lock);
+ /* Shared contexts disable independent QoS, not ordinary DMA probing. */
+ if (IS_ENABLED(CONFIG_RISCV_CBQRI_RESCTRL_FS) &&
+ iommu->qos_ids_valid) {
+ for (i = 0; i < fwspec->num_ids; i++) {
+ ret = riscv_iommu_claim_qos_context(iommu, dev,
+ fwspec->ids[i]);
+ if (ret)
+ goto err_claims;
+ }
+ }
/*
* Allocate and pre-configure device context entries in
* the device directory. Do not mark the context valid yet.
*/
+ mutex_lock(&iommu->qosid_lock);
tc = 0;
for (i = 0; i < fwspec->num_ids; i++) {
dc = riscv_iommu_get_dc(iommu, fwspec->ids[i]);
if (!dc) {
- kfree(info);
- return ERR_PTR(-ENODEV);
+ ret = -ENODEV;
+ goto err_contexts;
}
if (READ_ONCE(dc->tc) & RISCV_IOMMU_DC_TC_V)
dev_warn(dev, "already attached to IOMMU device directory\n");
WRITE_ONCE(dc->tc, tc);
+ WRITE_ONCE(dc->ta, 0);
}
+ mutex_unlock(&iommu->qosid_lock);
dev_iommu_priv_set(dev, info);
+ mutex_unlock(&iommu->qos_registry_lock);
return &iommu->iommu;
+
+err_contexts:
+ mutex_unlock(&iommu->qosid_lock);
+err_claims:
+ mutex_lock(&iommu->qosid_lock);
+ for (i = 0; i < fwspec->num_ids; i++)
+ if (xa_load(&iommu->dc_owners, fwspec->ids[i]) == dev)
+ xa_erase(&iommu->dc_owners, fwspec->ids[i]);
+ mutex_unlock(&iommu->qosid_lock);
+ mutex_unlock(&iommu->qos_registry_lock);
+ kfree(info);
+ return ERR_PTR(ret);
+}
+
+/* Unlike the legacy void sync helper, QoS must report incomplete updates. */
+static int riscv_iommu_qos_sync(struct riscv_iommu_device *iommu)
+{
+ struct riscv_iommu_command cmd;
+ u32 errors = RISCV_IOMMU_CQCSR_CQMF | RISCV_IOMMU_CQCSR_CMD_TO |
+ RISCV_IOMMU_CQCSR_CMD_ILL;
+ unsigned long flags;
+ unsigned int prod;
+ int ret;
+
+ riscv_iommu_cmd_iofence(&cmd);
+ ret = riscv_iommu_queue_send(&iommu->cmdq, &cmd, sizeof(cmd), &prod);
+ if (!ret)
+ ret = riscv_iommu_queue_wait(&iommu->cmdq, prod,
+ RISCV_IOMMU_QUEUE_TIMEOUT);
+ /* The IRQ must not clear CSR errors between these two checks. */
+ spin_lock_irqsave(&iommu->cmdq_error_lock, flags);
+ if (ret || READ_ONCE(iommu->qos_cmd_failed) ||
+ (riscv_iommu_readl(iommu, iommu->cmdq.qcr) & errors)) {
+ WRITE_ONCE(iommu->qos_cmd_failed, true);
+ ret = -EIO;
+ }
+ spin_unlock_irqrestore(&iommu->cmdq_error_lock, flags);
+ return ret;
+}
+
+static int riscv_iommu_set_device_ids(struct device *dev,
+ struct resctrl_device_ids ids)
+{
+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
+ struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
+ struct riscv_iommu_device *iommu = dev_to_iommu(dev);
+ struct riscv_iommu_command cmd;
+ struct riscv_iommu_dc *dc;
+ u64 mask = RISCV_IOMMU_DC_TA_RCID | RISCV_IOMMU_DC_TA_MCID;
+ int i, ret = 0;
+
+ if (!iommu->qos_ids_valid)
+ return -EOPNOTSUPP;
+ if (ids.closid > iommu->qos_rcid_max ||
+ ids.rmid > iommu->qos_mcid_max)
+ return -ERANGE;
+
+ mutex_lock(&iommu->qosid_lock);
+ if (READ_ONCE(iommu->qos_cmd_failed)) {
+ ret = -EIO;
+ goto out;
+ }
+ /* Validate the complete device before changing any of its contexts. */
+ for (i = 0; i < fwspec->num_ids; i++) {
+ if (xa_load(&iommu->dc_owners, fwspec->ids[i]) != dev ||
+ !riscv_iommu_get_dc(iommu, fwspec->ids[i])) {
+ ret = -ENODEV;
+ goto out;
+ }
+ }
+ for (i = 0; i < fwspec->num_ids; i++) {
+ dc = riscv_iommu_get_dc(iommu, fwspec->ids[i]);
+ WRITE_ONCE(dc->ta, (READ_ONCE(dc->ta) & ~mask) |
+ riscv_iommu_qos_ta(ids));
+ dma_wmb();
+ riscv_iommu_cmd_iodir_inval_ddt(&cmd);
+ riscv_iommu_cmd_iodir_set_did(&cmd, fwspec->ids[i]);
+ riscv_iommu_cmd_send(iommu, &cmd);
+ riscv_iommu_iodir_iotinval(iommu, false, dc->iohgatp, dc, NULL);
+ }
+ ret = riscv_iommu_qos_sync(iommu);
+ if (!ret)
+ info->qos_ids = ids;
+out:
+ mutex_unlock(&iommu->qosid_lock);
+ return ret;
+}
+
+static const struct resctrl_device_ops riscv_iommu_resctrl_ops = {
+ .set_ids = riscv_iommu_set_device_ids,
+};
+
+static void riscv_iommu_probe_finalize(struct device *dev)
+{
+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
+ struct riscv_iommu_device *iommu = dev_to_iommu(dev);
+ int i, ret;
+
+ if (!IS_ENABLED(CONFIG_RISCV_CBQRI_RESCTRL_FS) ||
+ !iommu->qos_ids_valid ||
+ iommu->ddt_mode <= RISCV_IOMMU_DDTP_IOMMU_MODE_BARE)
+ return;
+
+ mutex_lock(&iommu->qos_registry_lock);
+ for (i = 0; i < fwspec->num_ids; i++) {
+ if (xa_load(&iommu->dc_owners, fwspec->ids[i]) != dev) {
+ dev_dbg(dev, "shared device context; independent QoS unavailable\n");
+ goto out;
+ }
+ }
+ ret = resctrl_device_register(dev, &riscv_iommu_resctrl_ops);
+ if (ret)
+ dev_warn(dev, "resctrl registration failed: %d\n", ret);
+out:
+ mutex_unlock(&iommu->qos_registry_lock);
}
static void riscv_iommu_release_device(struct device *dev)
{
struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
+ struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
+ struct riscv_iommu_device *iommu = dev_to_iommu(dev);
+ int i;
+ /* Do not take qosid_lock before draining resctrl callbacks. */
+ mutex_lock(&iommu->qos_registry_lock);
+ resctrl_device_unregister(dev);
+ mutex_lock(&iommu->qosid_lock);
+ for (i = 0; i < fwspec->num_ids; i++)
+ if (xa_load(&iommu->dc_owners, fwspec->ids[i]) == dev)
+ xa_erase(&iommu->dc_owners, fwspec->ids[i]);
+ mutex_unlock(&iommu->qosid_lock);
+ mutex_unlock(&iommu->qos_registry_lock);
kfree_rcu_mightsleep(info);
}
@@ -1523,9 +1721,62 @@ static const struct iommu_ops riscv_iommu_ops = {
.domain_alloc_paging = riscv_iommu_alloc_paging_domain,
.device_group = riscv_iommu_device_group,
.probe_device = riscv_iommu_probe_device,
+ .probe_finalize = riscv_iommu_probe_finalize,
.release_device = riscv_iommu_release_device,
};
+/* Probe implemented low bits without changing the other ID or reserved bits. */
+static int riscv_iommu_probe_qosid_field(struct riscv_iommu_device *iommu,
+ u32 saved, u32 mask)
+{
+ u32 value, limit;
+
+ riscv_iommu_writel(iommu, RISCV_IOMMU_REG_IOMMU_QOSID, saved | mask);
+ value = riscv_iommu_readl(iommu, RISCV_IOMMU_REG_IOMMU_QOSID);
+ if ((value & ~mask) != (saved & ~mask))
+ return -EIO;
+ limit = (value & mask) >> __ffs(mask);
+ /* A width mask consists of contiguous low bits; zero permits only ID 0. */
+ if (limit & (limit + 1))
+ return -EINVAL;
+ return limit;
+}
+
+static void riscv_iommu_probe_qosid_limits(struct riscv_iommu_device *iommu)
+{
+ u32 busy = RISCV_IOMMU_QUEUE_ENABLE | RISCV_IOMMU_QUEUE_ACTIVE |
+ RISCV_IOMMU_QUEUE_BUSY;
+ u32 saved, restored;
+ u64 ddtp;
+ int rcid, mcid;
+
+ /* Never retag live DMA or queue traffic, including firmware handover. */
+ ddtp = riscv_iommu_readq(iommu, RISCV_IOMMU_REG_DDTP);
+ if ((ddtp & (RISCV_IOMMU_DDTP_IOMMU_MODE | RISCV_IOMMU_DDTP_BUSY)) ||
+ ((riscv_iommu_readl(iommu, RISCV_IOMMU_REG_CQCSR) |
+ riscv_iommu_readl(iommu, RISCV_IOMMU_REG_FQCSR) |
+ riscv_iommu_readl(iommu, RISCV_IOMMU_REG_PQCSR)) & busy))
+ goto unavailable;
+
+ saved = riscv_iommu_readl(iommu, RISCV_IOMMU_REG_IOMMU_QOSID);
+ rcid = riscv_iommu_probe_qosid_field(iommu, saved,
+ RISCV_IOMMU_IOMMU_QOSID_RCID);
+ mcid = riscv_iommu_probe_qosid_field(iommu, saved,
+ RISCV_IOMMU_IOMMU_QOSID_MCID);
+ riscv_iommu_writel(iommu, RISCV_IOMMU_REG_IOMMU_QOSID, saved);
+ restored = riscv_iommu_readl(iommu, RISCV_IOMMU_REG_IOMMU_QOSID);
+ if (rcid < 0 || mcid < 0 || restored != saved)
+ goto unavailable;
+
+ iommu->qos_rcid_max = rcid;
+ iommu->qos_mcid_max = mcid;
+ iommu->qos_ids_valid = true;
+ return;
+
+unavailable:
+ dev_warn(iommu->dev, "QoS ID limits unavailable; device QoS disabled\n");
+}
+
static int riscv_iommu_init_check(struct riscv_iommu_device *iommu)
{
u64 ddtp;
@@ -1559,6 +1810,10 @@ static int riscv_iommu_init_check(struct riscv_iommu_device *iommu)
return -EINVAL;
}
+ if (IS_ENABLED(CONFIG_RISCV_CBQRI_RESCTRL_FS) &&
+ (iommu->caps & RISCV_IOMMU_CAPABILITIES_QOSID))
+ riscv_iommu_probe_qosid_limits(iommu);
+
/*
* Distribute interrupt vectors, always use first vector for CIV.
* At least one interrupt is required. Read back and verify.
@@ -1583,6 +1838,7 @@ static int riscv_iommu_init_check(struct riscv_iommu_device *iommu)
void riscv_iommu_remove(struct riscv_iommu_device *iommu)
{
iommu_device_unregister(&iommu->iommu);
+ xa_destroy(&iommu->dc_owners);
iommu_device_sysfs_remove(&iommu->iommu);
riscv_iommu_iodir_set_mode(iommu, RISCV_IOMMU_DDTP_IOMMU_MODE_OFF);
riscv_iommu_queue_disable(&iommu->cmdq);
@@ -1596,6 +1852,10 @@ int riscv_iommu_init(struct riscv_iommu_device *iommu)
RISCV_IOMMU_QUEUE_INIT(&iommu->cmdq, CQ);
raw_spin_lock_init(&iommu->cmdq.lock);
RISCV_IOMMU_QUEUE_INIT(&iommu->fltq, FQ);
+ mutex_init(&iommu->qosid_lock);
+ mutex_init(&iommu->qos_registry_lock);
+ spin_lock_init(&iommu->cmdq_error_lock);
+ xa_init(&iommu->dc_owners);
rc = riscv_iommu_init_check(iommu);
if (rc)
@@ -1657,6 +1917,7 @@ int riscv_iommu_init(struct riscv_iommu_device *iommu)
err_queue_disable:
riscv_iommu_queue_disable(&iommu->fltq);
riscv_iommu_queue_disable(&iommu->cmdq);
+ xa_destroy(&iommu->dc_owners);
return rc;
}
diff --git a/drivers/iommu/riscv/iommu.h b/drivers/iommu/riscv/iommu.h
index 5676001548cc..72bb536190d1 100644
--- a/drivers/iommu/riscv/iommu.h
+++ b/drivers/iommu/riscv/iommu.h
@@ -15,6 +15,9 @@
#include <linux/spinlock.h>
#include <linux/types.h>
#include <linux/iopoll.h>
+#include <linux/mutex.h>
+#include <linux/spinlock.h>
+#include <linux/xarray.h>
#include "iommu-bits.h"
@@ -62,6 +65,19 @@ struct riscv_iommu_device {
unsigned int ddt_mode;
dma_addr_t ddt_phys;
u64 *ddt_root;
+
+ /* Serialize resctrl registration against DC sharing and device release. */
+ struct mutex qos_registry_lock;
+ /* Serialize DC updates and track independently owned device IDs. */
+ struct mutex qosid_lock;
+ struct xarray dc_owners;
+ /* Serialize CQ error acknowledgement with QoS completion checks. */
+ spinlock_t cmdq_error_lock;
+ bool qos_cmd_failed;
+ /* Inclusive ID limits, valid only after a quiescent WARL probe. */
+ u16 qos_rcid_max;
+ u16 qos_mcid_max;
+ bool qos_ids_valid;
};
int riscv_iommu_init(struct riscv_iommu_device *iommu);
--
2.50.1 (Apple Git-155)
^ permalink raw reply [flat|nested] 6+ messages in thread
* [RFC PATCH v2 3/4] iommu/riscv: Expose global QoS IDs in sysfs
2026-11-16 7:13 [RFC PATCH v2 0/4] resctrl: Assign registered devices to resource groups Zhanpeng Zhang
2026-11-16 7:13 ` [RFC PATCH v2 1/4] resctrl: Add registered device assignment Zhanpeng Zhang
2026-11-16 7:13 ` [RFC PATCH v2 2/4] iommu/riscv: Register independently tagged devices with resctrl Zhanpeng Zhang
@ 2026-11-16 7:13 ` Zhanpeng Zhang
2026-11-16 7:13 ` [RFC PATCH v2 4/4] selftests/resctrl: Exercise device assignment and IOMMU global IDs Zhanpeng Zhang
2026-11-23 7:03 ` [RFC PATCH v2 0/4] resctrl: Assign registered devices to resource groups Drew Fustini
4 siblings, 0 replies; 6+ messages in thread
From: Zhanpeng Zhang @ 2026-11-16 7:13 UTC (permalink / raw)
To: joro, palmer, tony.luck, reinette.chatre, tomasz.jeznach
Cc: fustini, jgg, james.morse, ben.horgan, xiaqinxin, will,
robin.murphy, pjw, aou, alex, Dave.Martin, babu.moger,
kevin.tian, corbet, skhan, shuah, randy.dunlap, cuiyunhui,
yuanzhu, iommu, linux-riscv, linux-kernel, linux-doc,
linux-kselftest, x86
Expose the per-IOMMU global RCID and MCID as separate attributes in the
IOMMU class device's qosid group. These IDs label IOMMU-originated
directory and queue accesses, and device requests in DDTP BARE mode.
Initialize the global IDs to zero before enabling translation. Preserve
the other field and reserved bits on each write, serialize updates, and
reject values that the WARL fields do not retain. Keep this interface
independent of resctrl device assignment and available without resctrl.
Document the attributes and add a maintainer entry for the new ABI and
device-assignment files.
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
---
.../ABI/testing/sysfs-class-iommu-riscv-iommu | 27 ++++
MAINTAINERS | 7 +
drivers/iommu/riscv/iommu.c | 135 +++++++++++++++++-
3 files changed, 164 insertions(+), 5 deletions(-)
create mode 100644 Documentation/ABI/testing/sysfs-class-iommu-riscv-iommu
diff --git a/Documentation/ABI/testing/sysfs-class-iommu-riscv-iommu b/Documentation/ABI/testing/sysfs-class-iommu-riscv-iommu
new file mode 100644
index 000000000000..4658cbcd625b
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-class-iommu-riscv-iommu
@@ -0,0 +1,27 @@
+What: /sys/class/iommu/<iommu>/qosid/rcid
+What: /sys/class/iommu/<iommu>/qosid/mcid
+Date: October 2026
+Contact: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
+Description:
+ The global default QoS IDs for a RISC-V IOMMU. These files
+ are present only when the IOMMU reports the QOSID capability.
+ Reading rcid or mcid returns the corresponding iommu_qosid
+ register field as a decimal number followed by a newline.
+
+ Writing a decimal number updates only the corresponding
+ field, preserving the other ID and reserved/WPRI bits. The
+ two writes are independent, not an atomic pair update.
+ Malformed input fails with EINVAL. Values outside
+ the 12-bit field range fail with ERANGE. If WARL readback
+ does not retain a value, the old register value is restored
+ and the write fails with ERANGE. Initialization sets both
+ IDs to zero.
+
+ These IDs tag IOMMU-originated DDT, CQ, FQ, PQ and MSI
+ accesses. In DDTP BARE mode they also tag device-originated
+ requests. In translated modes, device-specific requests
+ instead use the device context's QoS IDs.
+
+ This interface does not allocate resctrl IDs or configure
+ downstream resource controllers. The administrator must
+ choose IDs appropriate for the platform's QoS policy.
diff --git a/MAINTAINERS b/MAINTAINERS
index df051086b5dd..e14fddd07882 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -24072,6 +24072,13 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/iommu/linux.git
F: Documentation/devicetree/bindings/iommu/riscv,iommu.yaml
F: drivers/iommu/riscv/
+RISC-V IOMMU QoS
+M: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
+L: iommu@lists.linux.dev
+L: linux-riscv@lists.infradead.org
+S: Maintained
+F: Documentation/ABI/testing/sysfs-class-iommu-riscv-iommu
+
RISC-V MICROCHIP SUPPORT
M: Conor Dooley <conor.dooley@microchip.com>
R: Valentina Fernandez <valentina.fernandezalanis@microchip.com>
diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c
index dd2ebb25312d..b1b069bb2ad8 100644
--- a/drivers/iommu/riscv/iommu.c
+++ b/drivers/iommu/riscv/iommu.c
@@ -23,6 +23,7 @@
#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/resctrl_device.h>
+#include <linux/string.h>
#include <linux/generic_pt/iommu.h>
#include "../iommu-pages.h"
@@ -1777,9 +1778,124 @@ static void riscv_iommu_probe_qosid_limits(struct riscv_iommu_device *iommu)
dev_warn(iommu->dev, "QoS ID limits unavailable; device QoS disabled\n");
}
+static int riscv_iommu_update_qosid(struct riscv_iommu_device *iommu,
+ u32 mask, u32 value)
+{
+ u32 old_qosid, qosid;
+ int ret = 0;
+
+ mutex_lock(&iommu->qosid_lock);
+ old_qosid = riscv_iommu_readl(iommu, RISCV_IOMMU_REG_IOMMU_QOSID);
+ qosid = (old_qosid & ~mask) | value;
+ riscv_iommu_writel(iommu, RISCV_IOMMU_REG_IOMMU_QOSID, qosid);
+
+ /* Reject values that the WARL fields do not retain. */
+ qosid = riscv_iommu_readl(iommu, RISCV_IOMMU_REG_IOMMU_QOSID);
+ if ((qosid & mask) != value) {
+ riscv_iommu_writel(iommu, RISCV_IOMMU_REG_IOMMU_QOSID,
+ old_qosid);
+ riscv_iommu_readl(iommu, RISCV_IOMMU_REG_IOMMU_QOSID);
+ ret = -ERANGE;
+ }
+ mutex_unlock(&iommu->qosid_lock);
+ return ret;
+}
+
+static ssize_t riscv_iommu_qosid_show(struct device *dev, char *buf, bool rcid)
+{
+ struct iommu_device *idev = dev_to_iommu_device(dev);
+ struct riscv_iommu_device *iommu;
+ u32 qosid;
+
+ if (!idev)
+ return -ENODEV;
+ iommu = container_of(idev, struct riscv_iommu_device, iommu);
+ mutex_lock(&iommu->qosid_lock);
+ qosid = riscv_iommu_readl(iommu, RISCV_IOMMU_REG_IOMMU_QOSID);
+ mutex_unlock(&iommu->qosid_lock);
+ return sysfs_emit(buf, "%u\n", rcid ?
+ (u32)FIELD_GET(RISCV_IOMMU_IOMMU_QOSID_RCID, qosid) :
+ (u32)FIELD_GET(RISCV_IOMMU_IOMMU_QOSID_MCID, qosid));
+}
+
+static ssize_t riscv_iommu_qosid_store(struct device *dev, const char *buf,
+ size_t count, bool rcid)
+{
+ struct iommu_device *idev = dev_to_iommu_device(dev);
+ struct riscv_iommu_device *iommu;
+ u32 mask, value;
+ int ret;
+
+ if (!idev)
+ return -ENODEV;
+ if (memchr(buf, '\0', count))
+ return -EINVAL;
+ ret = kstrtou32(buf, 10, &value);
+ if (ret)
+ return ret;
+ if (rcid) {
+ if (value > FIELD_MAX(RISCV_IOMMU_IOMMU_QOSID_RCID))
+ return -ERANGE;
+ mask = RISCV_IOMMU_IOMMU_QOSID_RCID;
+ value = FIELD_PREP(RISCV_IOMMU_IOMMU_QOSID_RCID, value);
+ } else {
+ if (value > FIELD_MAX(RISCV_IOMMU_IOMMU_QOSID_MCID))
+ return -ERANGE;
+ mask = RISCV_IOMMU_IOMMU_QOSID_MCID;
+ value = FIELD_PREP(RISCV_IOMMU_IOMMU_QOSID_MCID, value);
+ }
+
+ iommu = container_of(idev, struct riscv_iommu_device, iommu);
+ ret = riscv_iommu_update_qosid(iommu, mask, value);
+ return ret ? ret : count;
+}
+
+static ssize_t rcid_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ return riscv_iommu_qosid_show(dev, buf, true);
+}
+
+static ssize_t rcid_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ return riscv_iommu_qosid_store(dev, buf, count, true);
+}
+static DEVICE_ATTR_RW(rcid);
+
+static ssize_t mcid_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ return riscv_iommu_qosid_show(dev, buf, false);
+}
+
+static ssize_t mcid_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ return riscv_iommu_qosid_store(dev, buf, count, false);
+}
+static DEVICE_ATTR_RW(mcid);
+
+static struct attribute *riscv_iommu_attrs[] = {
+ &dev_attr_rcid.attr,
+ &dev_attr_mcid.attr,
+ NULL,
+};
+
+static const struct attribute_group riscv_iommu_group = {
+ .name = "qosid",
+ .attrs = riscv_iommu_attrs,
+};
+
+static const struct attribute_group *riscv_iommu_groups[] = {
+ &riscv_iommu_group,
+ NULL,
+};
+
static int riscv_iommu_init_check(struct riscv_iommu_device *iommu)
{
u64 ddtp;
+ int ret;
/*
* Make sure the IOMMU is switched off or in pass-through mode during
@@ -1810,9 +1926,15 @@ static int riscv_iommu_init_check(struct riscv_iommu_device *iommu)
return -EINVAL;
}
- if (IS_ENABLED(CONFIG_RISCV_CBQRI_RESCTRL_FS) &&
- (iommu->caps & RISCV_IOMMU_CAPABILITIES_QOSID))
- riscv_iommu_probe_qosid_limits(iommu);
+ if (iommu->caps & RISCV_IOMMU_CAPABILITIES_QOSID) {
+ if (IS_ENABLED(CONFIG_RISCV_CBQRI_RESCTRL_FS))
+ riscv_iommu_probe_qosid_limits(iommu);
+ ret = riscv_iommu_update_qosid(iommu,
+ RISCV_IOMMU_IOMMU_QOSID_RCID |
+ RISCV_IOMMU_IOMMU_QOSID_MCID, 0);
+ if (ret)
+ return ret;
+ }
/*
* Distribute interrupt vectors, always use first vector for CIV.
@@ -1847,6 +1969,7 @@ void riscv_iommu_remove(struct riscv_iommu_device *iommu)
int riscv_iommu_init(struct riscv_iommu_device *iommu)
{
+ const struct attribute_group **sysfs_groups = NULL;
int rc;
RISCV_IOMMU_QUEUE_INIT(&iommu->cmdq, CQ);
@@ -1887,8 +2010,10 @@ int riscv_iommu_init(struct riscv_iommu_device *iommu)
if (rc)
goto err_queue_disable;
- rc = iommu_device_sysfs_add(&iommu->iommu, NULL, NULL, "riscv-iommu@%s",
- dev_name(iommu->dev));
+ if (iommu->caps & RISCV_IOMMU_CAPABILITIES_QOSID)
+ sysfs_groups = riscv_iommu_groups;
+ rc = iommu_device_sysfs_add(&iommu->iommu, NULL, sysfs_groups,
+ "riscv-iommu@%s", dev_name(iommu->dev));
if (rc) {
dev_err_probe(iommu->dev, rc, "cannot register sysfs interface\n");
goto err_iodir_off;
--
2.50.1 (Apple Git-155)
^ permalink raw reply [flat|nested] 6+ messages in thread
* [RFC PATCH v2 4/4] selftests/resctrl: Exercise device assignment and IOMMU global IDs
2026-11-16 7:13 [RFC PATCH v2 0/4] resctrl: Assign registered devices to resource groups Zhanpeng Zhang
` (2 preceding siblings ...)
2026-11-16 7:13 ` [RFC PATCH v2 3/4] iommu/riscv: Expose global QoS IDs in sysfs Zhanpeng Zhang
@ 2026-11-16 7:13 ` Zhanpeng Zhang
2026-11-23 7:03 ` [RFC PATCH v2 0/4] resctrl: Assign registered devices to resource groups Drew Fustini
4 siblings, 0 replies; 6+ messages in thread
From: Zhanpeng Zhang @ 2026-11-16 7:13 UTC (permalink / raw)
To: joro, palmer, tony.luck, reinette.chatre, tomasz.jeznach
Cc: fustini, jgg, james.morse, ben.horgan, xiaqinxin, will,
robin.murphy, pjw, aou, alex, Dave.Martin, babu.moger,
kevin.tian, corbet, skhan, shuah, randy.dunlap, cuiyunhui,
yuanzhu, iommu, linux-riscv, linux-kernel, linux-doc,
linux-kselftest, x86
Add opt-in tests with an explicitly selected disposable device or IOMMU.
Cover assignment, moves, idempotence, malformed tokens, occupied-group
removal, global register readback and restoration. Include monitor-group
checks only when explicitly requested on a monitoring-capable dependency.
Check that a task and a registered device can share a control group with
a non-default cache mask while leaving the default group's policy intact.
Document the assignment interface and how to run both selftests. These
tests do not verify downstream traffic tags or resource isolation.
The shell selftests were largely drafted with a large language model
(LLM) using the test scenarios and expected results as input.
Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>
---
.../filesystems/resctrl-devices-prototype.rst | 43 ++++
MAINTAINERS | 1 +
tools/testing/selftests/resctrl/Makefile | 2 +
tools/testing/selftests/resctrl/devices.sh | 205 ++++++++++++++++++
.../testing/selftests/resctrl/iommu_qosid.sh | 119 ++++++++++
5 files changed, 370 insertions(+)
create mode 100644 Documentation/filesystems/resctrl-devices-prototype.rst
create mode 100755 tools/testing/selftests/resctrl/devices.sh
create mode 100755 tools/testing/selftests/resctrl/iommu_qosid.sh
diff --git a/Documentation/filesystems/resctrl-devices-prototype.rst b/Documentation/filesystems/resctrl-devices-prototype.rst
new file mode 100644
index 000000000000..7e6eedb9bf79
--- /dev/null
+++ b/Documentation/filesystems/resctrl-devices-prototype.rst
@@ -0,0 +1,43 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+Registered resctrl devices (prototype)
+=====================================
+
+Write one registered ``<bus>/<device-name>`` token to a group's
+``devices`` file to move the device there. Reading the file lists that
+group's devices; the root file lists only the default group.
+``tasks`` is unchanged.
+
+Run as root, using a test device listed in the default group::
+
+ mount -t resctrl resctrl /sys/fs/resctrl
+ cat /sys/fs/resctrl/devices
+ mkdir /sys/fs/resctrl/p1
+ echo pci/0000:00:03.0 > /sys/fs/resctrl/p1/devices
+ echo pci/0000:00:03.0 > /sys/fs/resctrl/devices
+ rmdir /sys/fs/resctrl/p1
+
+Return devices to the default group before removing their group.
+``devices`` appears only if a device was registered at mount time.
+The RISC-V IOMMU driver does not register devices with shared contexts
+for independent assignment.
+
+Selftests
+---------
+
+Use an explicitly selected disposable device in the default group::
+
+ RESCTRL_DEVICE=pci/0000:00:03.0 sh tools/testing/selftests/resctrl/devices.sh
+
+This covers device moves, invalid writes, group removal and task/device
+co-assignment with a non-default cache mask. Set ``RESCTRL_TEST_MONITOR=1``
+only with a monitoring-capable dependency.
+
+For the separate global-ID test, substitute the test IOMMU's name::
+
+ IOMMU_QOSID_DIR='/sys/class/iommu/<iommu>/qosid' \
+ sh tools/testing/selftests/resctrl/iommu_qosid.sh
+
+The global-ID test restores the original values. Each test ID defaults
+to 1; override ``IOMMU_QOSID_RCID`` and ``IOMMU_QOSID_MCID`` if needed.
+Neither test measures traffic tags or resource isolation.
diff --git a/MAINTAINERS b/MAINTAINERS
index e14fddd07882..eef4695d089a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -23466,6 +23466,7 @@ R: Babu Moger <babu.moger@amd.com>
L: linux-kernel@vger.kernel.org
S: Supported
P: Documentation/process/maintainer-tip.rst
+F: Documentation/filesystems/resctrl-devices-prototype.rst
F: Documentation/filesystems/resctrl.rst
F: arch/x86/include/asm/resctrl.h
F: arch/x86/kernel/cpu/resctrl/
diff --git a/tools/testing/selftests/resctrl/Makefile b/tools/testing/selftests/resctrl/Makefile
index 984534cfbf1b..73646aec1284 100644
--- a/tools/testing/selftests/resctrl/Makefile
+++ b/tools/testing/selftests/resctrl/Makefile
@@ -4,6 +4,8 @@ CFLAGS = -g -Wall -O2 -D_FORTIFY_SOURCE=2
CFLAGS += $(KHDR_INCLUDES)
TEST_GEN_PROGS := resctrl_tests
+TEST_PROGS += devices.sh
+TEST_PROGS += iommu_qosid.sh
LOCAL_HDRS += $(wildcard *.h)
diff --git a/tools/testing/selftests/resctrl/devices.sh b/tools/testing/selftests/resctrl/devices.sh
new file mode 100755
index 000000000000..12176d6a3779
--- /dev/null
+++ b/tools/testing/selftests/resctrl/devices.sh
@@ -0,0 +1,205 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+# Explicitly select a disposable device; never choose a storage device for you.
+
+root=${RESCTRL_MOUNT:-/sys/fs/resctrl}
+token=${RESCTRL_DEVICE:-}
+count=0
+failed=0
+g1="$root/device-test-$$-a"
+g2="$root/device-test-$$-b"
+mon="$g2/mon_groups/device-test-$$"
+test_pid=
+
+echo 'TAP version 13'
+skip()
+{
+ echo "1..0 # SKIP $*"
+ exit 4
+}
+[ "$(id -u)" = 0 ] || skip 'root required'
+[ -n "$token" ] || skip 'set RESCTRL_DEVICE=bus/name for a test device'
+[ -f "$root/devices" ] || skip 'no registered resctrl device interface'
+grep -Fxq "$token" "$root/devices" || skip 'device is not in the default group'
+
+stop_test_task()
+{
+ [ -n "$test_pid" ] || return 0
+ kill "$test_pid" 2>/dev/null || :
+ wait "$test_pid" 2>/dev/null || :
+ test_pid=
+}
+
+cleanup()
+{
+ stop_test_task
+ if ! echo "$token" > "$root/devices"; then
+ echo '# ERROR: cannot restore device to default group' >&2
+ failed=1
+ return
+ fi
+ rmdir "$mon" "$g1" "$g2" 2>/dev/null || :
+}
+
+finish()
+{
+ ret=$?
+ trap - EXIT HUP INT TERM
+ cleanup
+ echo "1..$count"
+ [ "$ret" = 0 ] || failed=1
+ exit "$failed"
+}
+trap finish EXIT
+trap 'exit 1' HUP INT TERM
+
+check()
+{
+ name=$1
+ shift
+ count=$((count + 1))
+ if "$@"; then
+ echo "ok $count - $name"
+ else
+ echo "not ok $count - $name"
+ failed=1
+ fi
+}
+move()
+{
+ echo "$token" > "$1/devices" &&
+ grep -Fxq "$token" "$1/devices"
+}
+reject()
+{
+ if echo "$1" > "$g1/devices" 2>/dev/null; then
+ return 1
+ fi
+ grep -Fxq "$token" "$g2/devices"
+}
+nul()
+{
+ if printf '%s\000ignored\n' "$token" > "$g1/devices" 2>/dev/null; then
+ return 1
+ fi
+ grep -Fxq "$token" "$g2/devices"
+}
+busy()
+{
+ ! rmdir "$g2" 2>/dev/null && [ -d "$g2" ]
+}
+busy_mon()
+{
+ ! rmdir "$mon" 2>/dev/null && [ -d "$mon" ]
+}
+mode_busy()
+{
+ if echo exclusive > "$g2/mode" 2>/dev/null; then
+ return 1
+ fi
+ # Inspect the reason before another group read clears last_cmd_status.
+ grep -Fq 'Move assigned devices' "$root/info/last_cmd_status" &&
+ [ "$(cat "$g2/mode")" = shareable ]
+}
+empty()
+{
+ contents=$(cat "$1") || return 1
+ [ -z "$contents" ]
+}
+
+cache_entry()
+{
+ awk -F '[:=;]' -v res="$resource" '
+ $1 ~ ("^[[:space:]]*" res "$") && NF >= 3 {
+ print $2 "=" $3
+ exit
+ }' "$g1/schemata"
+}
+
+find_cache_mask()
+{
+ for resource in L3 L2; do
+ full=$(cat "$root/info/$resource/cbm_mask" 2>/dev/null) || continue
+ min=$(cat "$root/info/$resource/min_cbm_bits") || continue
+ initial=$(cache_entry)
+ [ -n "$initial" ] || continue
+
+ # Use a contiguous subset distinct from the new group's initial mask.
+ mask=$((0x$full >> 1))
+ [ "$mask" -gt 0 ] &&
+ [ "$((mask & (mask + 1)))" -eq 0 ] &&
+ [ "$((mask & 0x$full))" -eq "$mask" ] &&
+ [ "$mask" -ge "$(((1 << min) - 1))" ] &&
+ [ "$mask" -ne "$((0x${initial#*=}))" ] || continue
+ cache_setting=$(printf '%s=%x' "${initial%=*}" "$mask")
+ return 0
+ done
+ return 1
+}
+
+task_and_device()
+{
+ default_schema=$(cat "$root/schemata") || return 1
+ echo "$resource:$cache_setting" > "$g1/schemata" || return 1
+ sleep 60 &
+ test_pid=$!
+ result=1
+ if echo "$test_pid" > "$g1/tasks" &&
+ grep -Fxq "$test_pid" "$g1/tasks" &&
+ grep -Fxq "$token" "$root/devices" &&
+ move "$g1" &&
+ ! grep -Fxq "$token" "$root/devices" &&
+ grep -Fxq "$test_pid" "$g1/tasks" &&
+ [ "$(cache_entry)" = "$cache_setting" ] &&
+ [ "$(cat "$root/schemata")" = "$default_schema" ]; then
+ echo "# PID $test_pid and $token share $resource:$cache_setting"
+ result=0
+ fi
+ stop_test_task
+ move "$root" && [ "$result" -eq 0 ]
+}
+
+check 'create first control group' mkdir "$g1"
+[ -d "$g1" ] || exit 1
+check 'create second control group' mkdir "$g2"
+[ -d "$g2" ] || exit 1
+if find_cache_mask; then
+ check 'task and device share a control group with a non-default cache mask' task_and_device
+else
+ count=$((count + 1))
+ echo "ok $count - task and device share a control group # SKIP no alternate cache mask"
+fi
+check 'assign device to a control group' move "$g1"
+check 'repeat the same assignment' move "$g1"
+check 'move device to another control group' move "$g2"
+check 'old group no longer lists device' empty "$g1/devices"
+check 'reject unknown device without moving the member' reject 'missing/device'
+check 'reject old IOMMU group token' reject 'iommu_group:0'
+check 'reject multiple tokens' reject "$token,$token"
+check 'reject empty token' reject ''
+check 'reject multiple lines' reject "$token
+$token"
+check 'reject embedded NUL without moving the member' nul
+check 'reject removing a group with devices' busy
+check 'reject exclusive mode with devices before testing cache masks' mode_busy
+
+# Monitoring requires a real monitoring-capable dependency, not fabricated RMIDs.
+if [ "${RESCTRL_TEST_MONITOR:-0}" = 1 ]; then
+ check 'create monitor group' mkdir "$mon"
+ if [ -d "$mon" ]; then
+ check 'assign device to monitor group' move "$mon"
+ check 'control group no longer lists monitored device' empty "$g2/devices"
+ check 'reject removing monitor group with device' busy_mon
+ check 'reject removing parent with monitored device' busy
+ check 'reject exclusive parent with monitored device' mode_busy
+ check 'restore device to control group' move "$g2"
+ check 'remove empty monitor group' rmdir "$mon"
+ fi
+else
+ count=$((count + 1))
+ echo "ok $count - monitor-group assignment # SKIP set RESCTRL_TEST_MONITOR=1"
+fi
+check 'move back to default group' move "$root"
+check 'remove empty first group' rmdir "$g1"
+check 'remove empty second group' rmdir "$g2"
+exit "$failed"
diff --git a/tools/testing/selftests/resctrl/iommu_qosid.sh b/tools/testing/selftests/resctrl/iommu_qosid.sh
new file mode 100755
index 000000000000..b10f89721dc2
--- /dev/null
+++ b/tools/testing/selftests/resctrl/iommu_qosid.sh
@@ -0,0 +1,119 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+# Opt in: global IDs affect IOMMU traffic, not just a disposable endpoint.
+
+dir=${IOMMU_QOSID_DIR:-}
+rcid=${IOMMU_QOSID_RCID:-1}
+mcid=${IOMMU_QOSID_MCID:-1}
+count=0
+failed=0
+
+echo 'TAP version 13'
+skip()
+{
+ echo "1..0 # SKIP $*"
+ exit 4
+}
+[ "$(id -u)" = 0 ] || skip 'root required'
+[ -n "$dir" ] || skip 'set IOMMU_QOSID_DIR for a test IOMMU'
+for field in rcid mcid; do
+ [ -r "$dir/$field" ] && [ -w "$dir/$field" ] ||
+ skip 'qosid interface unavailable'
+done
+case "$rcid:$mcid" in
+ *[!0-9:]* | :* | *:) skip 'select decimal test IDs supported by the IOMMU' ;;
+ 0* | *:0*) skip 'select nonzero IDs without leading zeros' ;;
+esac
+[ "$rcid" -gt 0 ] && [ "$rcid" -le 4095 ] &&
+ [ "$mcid" -gt 0 ] && [ "$mcid" -le 4095 ] ||
+ skip 'select nonzero 12-bit test IDs supported by the IOMMU'
+original_rcid=$(cat "$dir/rcid") || exit 1
+original_mcid=$(cat "$dir/mcid") || exit 1
+
+snapshot()
+{
+ current_rcid=$(cat "$dir/rcid") || return 1
+ current_mcid=$(cat "$dir/mcid") || return 1
+ printf '%s:%s\n' "$current_rcid" "$current_mcid"
+}
+restore()
+{
+ restored=0
+ echo "$original_rcid" > "$dir/rcid" || restored=1
+ echo "$original_mcid" > "$dir/mcid" || restored=1
+ [ "$(snapshot)" = "$original_rcid:$original_mcid" ] || restored=1
+ return "$restored"
+}
+
+finish()
+{
+ ret=$?
+ trap - EXIT HUP INT TERM
+ if ! restore; then
+ echo '# ERROR: failed to restore global QoS IDs' >&2
+ failed=1
+ fi
+ echo "1..$count"
+ [ "$ret" = 0 ] || failed=1
+ exit "$failed"
+}
+trap finish EXIT
+trap 'exit 1' HUP INT TERM
+
+check()
+{
+ name=$1
+ shift
+ count=$((count + 1))
+ if "$@"; then
+ echo "ok $count - $name"
+ else
+ echo "not ok $count - $name"
+ failed=1
+ fi
+}
+write_rcid()
+{
+ other=$(cat "$dir/mcid") || return 1
+ echo "$1" > "$dir/rcid" && [ "$(snapshot)" = "$1:$other" ]
+}
+write_mcid()
+{
+ other=$(cat "$dir/rcid") || return 1
+ echo "$1" > "$dir/mcid" && [ "$(snapshot)" = "$other:$1" ]
+}
+reject()
+{
+ before=$(snapshot) || return 1
+ if echo "$2" > "$dir/$1" 2>/dev/null; then
+ return 1
+ fi
+ [ "$(snapshot)" = "$before" ]
+}
+nul()
+{
+ before=$(snapshot) || return 1
+ if printf '0\000ignored\n' > "$dir/$1" 2>/dev/null; then
+ return 1
+ fi
+ [ "$(snapshot)" = "$before" ]
+}
+
+check 'write RCID zero without changing MCID' write_rcid 0
+check 'write MCID zero without changing RCID' write_mcid 0
+check 'write nonzero RCID without changing MCID' write_rcid "$rcid"
+check 'write nonzero MCID without changing RCID' write_mcid "$mcid"
+check 'change RCID while preserving nonzero MCID' write_rcid 0
+check 'restore nonzero RCID while preserving MCID' write_rcid "$rcid"
+check 'change MCID while preserving nonzero RCID' write_mcid 0
+check 'reject malformed RCID and preserve IDs' reject rcid bad
+check 'reject malformed MCID and preserve IDs' reject mcid bad
+check 'reject trailing input and preserve IDs' reject rcid '0 1'
+check 'reject out-of-range RCID and preserve IDs' reject rcid 4096
+check 'reject out-of-range MCID and preserve IDs' reject mcid 4096
+check 'reject negative ID and preserve IDs' reject mcid -1
+check 'reject integer overflow and preserve IDs' reject rcid 4294967296
+check 'reject embedded NUL in RCID and preserve IDs' nul rcid
+check 'reject embedded NUL in MCID and preserve IDs' nul mcid
+check 'restore original global IDs' restore
+exit "$failed"
--
2.50.1 (Apple Git-155)
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [RFC PATCH v2 0/4] resctrl: Assign registered devices to resource groups
2026-11-16 7:13 [RFC PATCH v2 0/4] resctrl: Assign registered devices to resource groups Zhanpeng Zhang
` (3 preceding siblings ...)
2026-11-16 7:13 ` [RFC PATCH v2 4/4] selftests/resctrl: Exercise device assignment and IOMMU global IDs Zhanpeng Zhang
@ 2026-11-23 7:03 ` Drew Fustini
4 siblings, 0 replies; 6+ messages in thread
From: Drew Fustini @ 2026-11-23 7:03 UTC (permalink / raw)
To: Zhanpeng Zhang
Cc: joro, palmer, tony.luck, reinette.chatre, tomasz.jeznach, jgg,
james.morse, ben.horgan, xiaqinxin, will, robin.murphy, pjw, aou,
alex, Dave.Martin, babu.moger, kevin.tian, corbet, skhan, shuah,
randy.dunlap, cuiyunhui, yuanzhu, iommu, linux-riscv,
linux-kernel, linux-doc, linux-kselftest, x86
On Mon, Nov 16, 2026 at 03:13:39PM +0800, Zhanpeng Zhang wrote:
> This RFC lets resctrl groups include devices as well as CPU tasks. It
> uses the RISC-V IOMMU QoS ID extension as the first provider and builds
> on Drew Fustini's CBQRI resctrl v12 series [1] on Linux v7.4-rc1.
It does not seem like riscv-iommu in qemu models qosid. Are you testing
this on a hardware implementation?
> Feedback would be particularly helpful on:
>
> * The provider registration and lifetime contract, including how to
> represent devices that share a tagging context.
> * The bus/name token versus using an existing sysfs identity.
> * The scope of protection after a command queue failure. If an update
> leaves the hardware state uncertain, the current code prevents ID
> reuse by blocking group removal and future mounts, including for
> groups unrelated to the failed update. Would reserving only the
> affected old and target IDs be sufficient?
It seems like blocking future mounts would be too much since resctrl
should still be able to handle cpu tasks.
Thanks,
Drew
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-23 7:03 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 7:13 [RFC PATCH v2 0/4] resctrl: Assign registered devices to resource groups Zhanpeng Zhang
2026-11-16 7:13 ` [RFC PATCH v2 1/4] resctrl: Add registered device assignment Zhanpeng Zhang
2026-11-16 7:13 ` [RFC PATCH v2 2/4] iommu/riscv: Register independently tagged devices with resctrl Zhanpeng Zhang
2026-11-16 7:13 ` [RFC PATCH v2 3/4] iommu/riscv: Expose global QoS IDs in sysfs Zhanpeng Zhang
2026-11-16 7:13 ` [RFC PATCH v2 4/4] selftests/resctrl: Exercise device assignment and IOMMU global IDs Zhanpeng Zhang
2026-11-23 7:03 ` [RFC PATCH v2 0/4] resctrl: Assign registered devices to resource groups Drew Fustini
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®