From: Pavol Sakac <sakacpav@amazon.de>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J . Wysocki" <rafael@kernel.org>,
Danilo Krummrich <dakr@kernel.org>, Tejun Heo <tj@kernel.org>
Cc: <linux-kernel@vger.kernel.org>, <driver-core@lists.linux.dev>,
"David Woodhouse" <dwmw@amazon.co.uk>,
Pasha Tatashin <pasha.tatashin@soleen.com>,
Mike Rapoport <rppt@kernel.org>,
Pratyush Yadav <pratyush@kernel.org>,
"David Matlack" <dmatlack@google.com>,
Samiullah Khawaja <skhawaja@google.com>,
Alexander Graf <graf@amazon.com>, <linux-mm@kvack.org>,
<kexec@lists.infradead.org>, <nh-open-source@amazon.com>
Subject: [RFC PATCH 09/14] iommu: lazy-populate iommu_group reserved_regions/type attrs
Date: Thu, 2 Jul 2026 19:51:09 +0200 [thread overview]
Message-ID: <20260702175114.24659-5-sakacpav@amazon.de> (raw)
In-Reply-To: <20260702175114.24659-1-sakacpav@amazon.de>
iommu_group_alloc() eagerly creates two attribute files -
reserved_regions and type - that are read only on the VFIO /
iommufd ioctl path (VFIO_IOMMU_GET_INFO, iommufd type query).
On systems with thousands of SR-IOV VFs each VF typically gets
its own iommu_group, so deferring these two attrs eliminates two
kernfs nodes per group at boot.
iommu_group_ktype is the first non-device_ktype consumer of the
populate mechanism: wire iommu_group_populate_one /
iommu_group_populate_all and call kernfs_set_lazy() on the
group's kernfs node immediately after kobject_init_and_add().
See the in-diff comment at the kernfs_set_lazy() site for the
ordering rationale (must precede kobject_create_and_add("devices")
to interact correctly with kernfs_inc_rev()).
Both callbacks fast-path on iommu_group_sysfs_populated() so
re-entry after the directory is fully populated is cheap and
cannot fire sysfs_warn_dup().
The name attribute is created on demand by iommu_group_set_name()
on a separate API path and is not handled by these callbacks;
groups without a set name return -ENOENT on lookup of name.
No userspace ABI change. A selftest for this deferral is added
later in the series under tools/testing/selftests/sysfs-lazy/.
Cc: Joerg Roedel <joro@8bytes.org>
Cc: Will Deacon <will@kernel.org>
Cc: Robin Murphy <robin.murphy@arm.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Kevin Tian <kevin.tian@intel.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: iommu@lists.linux.dev
Cc: linux-kselftest@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-api@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Danilo Krummrich <dakr@kernel.org>
Cc: driver-core@lists.linux.dev
Assisted-by: Claude:claude-opus-4.7
Signed-off-by: Pavol Sakac <sakacpav@amazon.de>
---
drivers/iommu/iommu.c | 132 +++++++++++++++++++++++++++++++++++++-----
1 file changed, 119 insertions(+), 13 deletions(-)
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 61c12ba782066..7a8dd43f0a09a 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -21,6 +21,7 @@
#include <linux/iommufd.h>
#include <linux/idr.h>
#include <linux/err.h>
+#include <linux/kernfs.h>
#include <linux/pci.h>
#include <linux/pci-ats.h>
#include <linux/bitops.h>
@@ -71,8 +72,22 @@ struct iommu_group {
struct list_head entry;
unsigned int owner_cnt;
void *owner;
+ /* Embedded lazy-sysfs state (every iommu_group is lazy). */
+ struct sysfs_lazy_state sysfs_lazy;
};
+static inline bool iommu_group_sysfs_populated(const struct iommu_group *group)
+{
+ return READ_ONCE(group->sysfs_lazy.populated);
+}
+
+static inline void
+iommu_group_sysfs_set_populated(struct iommu_group *group)
+{
+ lockdep_assert_held(&group->sysfs_lazy.lock);
+ WRITE_ONCE(group->sysfs_lazy.populated, true);
+}
+
struct group_device {
struct list_head list;
struct device *dev;
@@ -1033,9 +1048,99 @@ static void iommu_group_release(struct kobject *kobj)
kfree(group);
}
+/* Lazy iommu_group attrs (excludes "name", managed separately). */
+static const struct {
+ const char *name;
+ struct iommu_group_attribute *attr;
+} iommu_group_lazy_attrs[] = {
+ { "reserved_regions", &iommu_group_attr_reserved_regions },
+ { "type", &iommu_group_attr_type },
+};
+
+static int iommu_group_populate_one(struct kobject *kobj, const char *name)
+{
+ struct iommu_group *group = to_iommu_group(kobj);
+ size_t i;
+ int ret = -ENOENT;
+ bool name_present = false;
+
+ /* Fast path: directory fully populated; kernfs has authoritative state. */
+ if (iommu_group_sysfs_populated(group))
+ return -ENOENT;
+
+ mutex_lock(&group->sysfs_lazy.lock);
+ /* Re-check under the lock against a concurrent populate_all. */
+ if (iommu_group_sysfs_populated(group)) {
+ ret = -ENOENT;
+ goto out;
+ }
+
+ /* Materialise the whole table (2 entries); cheaper than per-name dispatch. */
+ for (i = 0; i < ARRAY_SIZE(iommu_group_lazy_attrs); i++) {
+ struct iommu_group_attribute *attr =
+ iommu_group_lazy_attrs[i].attr;
+ int rc;
+
+ if (sysfs_kn_exists(&group->kobj, attr->attr.name))
+ rc = 0;
+ else
+ rc = iommu_group_create_file(group, attr);
+
+ if (!strcmp(name, iommu_group_lazy_attrs[i].name)) {
+ name_present = true;
+ ret = rc;
+ } else if (rc) {
+ pr_warn("group %d: lazy-create %s failed: %d\n",
+ group->id,
+ iommu_group_lazy_attrs[i].name, rc);
+ }
+ }
+ iommu_group_sysfs_set_populated(group);
+
+out:
+ mutex_unlock(&group->sysfs_lazy.lock);
+ return name_present ? ret : -ENOENT;
+}
+
+static void iommu_group_populate_all(struct kobject *kobj)
+{
+ struct iommu_group *group = to_iommu_group(kobj);
+ size_t i;
+ int ret;
+
+ /* Fast path: directory already fully populated. */
+ if (iommu_group_sysfs_populated(group))
+ return;
+
+ mutex_lock(&group->sysfs_lazy.lock);
+ if (iommu_group_sysfs_populated(group))
+ goto out;
+
+ for (i = 0; i < ARRAY_SIZE(iommu_group_lazy_attrs); i++) {
+ struct iommu_group_attribute *attr =
+ iommu_group_lazy_attrs[i].attr;
+
+ /* Existence check absorbs the populate_one race. */
+ if (sysfs_kn_exists(&group->kobj, attr->attr.name))
+ continue;
+
+ ret = iommu_group_create_file(group, attr);
+ if (ret)
+ pr_warn("group %d: lazy-create %s failed: %d\n",
+ group->id,
+ iommu_group_lazy_attrs[i].name, ret);
+ }
+
+ iommu_group_sysfs_set_populated(group);
+out:
+ mutex_unlock(&group->sysfs_lazy.lock);
+}
+
static const struct kobj_type iommu_group_ktype = {
.sysfs_ops = &iommu_group_sysfs_ops,
.release = iommu_group_release,
+ .populate = iommu_group_populate_one,
+ .populate_all = iommu_group_populate_all,
};
/**
@@ -1060,6 +1165,7 @@ struct iommu_group *iommu_group_alloc(void)
group->kobj.kset = iommu_group_kset;
mutex_init(&group->mutex);
+ mutex_init(&group->sysfs_lazy.lock);
INIT_LIST_HEAD(&group->devices);
INIT_LIST_HEAD(&group->entry);
xa_init(&group->pasid_array);
@@ -1078,6 +1184,19 @@ struct iommu_group *iommu_group_alloc(void)
return ERR_PTR(ret);
}
+ /*
+ * Defer reserved_regions and type - both are read only on
+ * VFIO/iommufd ioctl paths, so let the populate callbacks
+ * materialise them on demand.
+ *
+ * KERNFS_LAZY MUST be set before kobject_create_and_add("devices"),
+ * which calls kernfs_inc_rev() on this kobject's directory and
+ * would otherwise leave a stale negative dentry cached for a
+ * missing reserved_regions/type child. WARN_ON because
+ * iommu_group_alloc() controls all group kobjects directly.
+ */
+ WARN_ON(kernfs_set_lazy(group->kobj.sd));
+
group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
if (!group->devices_kobj) {
kobject_put(&group->kobj); /* triggers .release & free */
@@ -1091,19 +1210,6 @@ struct iommu_group *iommu_group_alloc(void)
*/
kobject_put(&group->kobj);
- ret = iommu_group_create_file(group,
- &iommu_group_attr_reserved_regions);
- if (ret) {
- kobject_put(group->devices_kobj);
- return ERR_PTR(ret);
- }
-
- ret = iommu_group_create_file(group, &iommu_group_attr_type);
- if (ret) {
- kobject_put(group->devices_kobj);
- return ERR_PTR(ret);
- }
-
pr_debug("Allocated group %d\n", group->id);
return group;
--
2.47.3
Amazon Web Services Development Center Germany GmbH
Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christof Hellmis, Andreas Stieger
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
next prev parent reply other threads:[~2026-07-02 17:53 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-02 17:40 [RFC PATCH 00/14] driver core: defer per-VF sysfs creation for fast SR-IOV bring-up Pavol Sakac
2026-07-02 17:40 ` [RFC PATCH 01/14] kernfs: add populate callbacks and KERNFS_LAZY flag for lazy dir population Pavol Sakac
2026-07-02 17:40 ` [RFC PATCH 02/14] sysfs: add existence-check helpers for lazy populate races Pavol Sakac
2026-07-02 17:40 ` [RFC PATCH 03/14] sysfs: introduce sysfs_kf_syscall_ops dispatching to kobj_type Pavol Sakac
2026-07-02 17:40 ` [RFC PATCH 04/14] driver core: add struct sysfs_lazy_state and device_set_sysfs_lazy() Pavol Sakac
2026-07-02 17:51 ` [RFC PATCH 05/14] driver core: add struct device_sysfs_entry and walker Pavol Sakac
2026-07-02 17:51 ` [RFC PATCH 06/14] driver core: wire device_ktype populate to walker Pavol Sakac
2026-07-02 17:51 ` [RFC PATCH 07/14] driver core: migrate device sysfs to device_sysfs_entry table Pavol Sakac
2026-07-02 17:51 ` [RFC PATCH 08/14] PCI/sysfs: migrate to device_sysfs_entry, defer physfn symlink Pavol Sakac
2026-07-02 17:51 ` Pavol Sakac [this message]
2026-07-10 14:19 ` [RFC PATCH 09/14] iommu: lazy-populate iommu_group reserved_regions/type attrs Greg Kroah-Hartman
2026-07-02 17:51 ` [RFC PATCH 10/14] PCI/IOV: opt SR-IOV VFs into sysfs_lazy Pavol Sakac
2026-07-02 17:51 ` [RFC PATCH 11/14] vfio: opt vfio-dev and VFIO group devices " Pavol Sakac
2026-07-02 17:51 ` [RFC PATCH 12/14] driver core: test: add KUnit tests for device_sysfs_apply Pavol Sakac
2026-07-02 17:51 ` [RFC PATCH 13/14] Documentation: add lazy sysfs initialisation and device_sysfs_entry docs Pavol Sakac
2026-07-02 17:51 ` [RFC PATCH 14/14] selftests: sysfs-lazy: add tests for lazy sysfs initialization Pavol Sakac
2026-07-10 14:16 ` [RFC PATCH 00/14] driver core: defer per-VF sysfs creation for fast SR-IOV bring-up Greg Kroah-Hartman
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=20260702175114.24659-5-sakacpav@amazon.de \
--to=sakacpav@amazon.de \
--cc=dakr@kernel.org \
--cc=dmatlack@google.com \
--cc=driver-core@lists.linux.dev \
--cc=dwmw@amazon.co.uk \
--cc=graf@amazon.com \
--cc=gregkh@linuxfoundation.org \
--cc=kexec@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nh-open-source@amazon.com \
--cc=pasha.tatashin@soleen.com \
--cc=pratyush@kernel.org \
--cc=rafael@kernel.org \
--cc=rppt@kernel.org \
--cc=skhawaja@google.com \
--cc=tj@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