[RFC PATCH 10/14] PCI/IOV: opt SR-IOV VFs into sysfs_lazy
Pavol Sakac
sakacpav at amazon.de
Thu Jul 2 10:51:10 PDT 2026
First behaviour change in the lazy-sysfs series. Call
device_set_sysfs_lazy() on every SR-IOV Virtual Function before
pci_device_add() so the VF kobj directory is marked KERNFS_LAZY and
attribute files materialise on first userspace access.
Place the opt-in in pci_iov_scan_device() (immediately after
pci_setup_device()), which is the single allocation/cleanup site
for a VF struct (Shay Drory, commit 04d50d953ab4 ("PCI: Fix NULL
dereference in SR-IOV VF creation error path")). Any -ENOMEM from
device_set_sysfs_lazy() rolls back through that one site rather
than leaking the VF struct, the physfn ref, and the bus ref.
Add an early-return guard at the top of pci_create_sysfs_dev_files()
so lazy VFs skip eager resource-file creation; the guard covers
both pci_bus_add and the late pci_sysfs_init iterator.
pci_remove_sysfs_dev_files() stays unguarded - its per-entry remove
callbacks are idempotent.
Marking VFs lazy defers the bulk of each VF's sysfs population to
first access. The cost avoided is per VF and scales with the number
of VFs, so on hosts that create a large number of VFs it removes a
correspondingly large amount of up-front kernfs node allocation at
probe. The nodes are deferred, not dropped: reading a VF's
attributes materialises the same tree as before. Quantified results
are deferred to the cover letter.
No userspace ABI change: all files eventually materialize.
Cc: Bjorn Helgaas <bhelgaas at google.com>
Cc: linux-pci at vger.kernel.org
Cc: linux-kernel at vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh at linuxfoundation.org>
Cc: Danilo Krummrich <dakr at kernel.org>
Cc: driver-core at lists.linux.dev
Cc: linux-api at vger.kernel.org
Assisted-by: Claude:claude-opus-4.7
Signed-off-by: Pavol Sakac <sakacpav at amazon.de>
---
drivers/pci/iov.c | 19 +++++++++++++------
drivers/pci/pci-sysfs.c | 9 +++++++++
2 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
index ec2fca8209593..5537c3df6f45b 100644
--- a/drivers/pci/iov.c
+++ b/drivers/pci/iov.c
@@ -328,14 +328,21 @@ static struct pci_dev *pci_iov_scan_device(struct pci_dev *dev, int id,
pci_read_vf_config_common(virtfn);
rc = pci_setup_device(virtfn);
- if (rc) {
- pci_dev_put(dev);
- pci_bus_put(virtfn->bus);
- kfree(virtfn);
- return ERR_PTR(rc);
- }
+ if (rc)
+ goto err_free;
+
+ /* Single-site -ENOMEM rollback (kfree + pci_dev_put/pci_bus_put). */
+ rc = device_set_sysfs_lazy(&virtfn->dev);
+ if (rc)
+ goto err_free;
return virtfn;
+
+err_free:
+ pci_dev_put(dev);
+ pci_bus_put(virtfn->bus);
+ kfree(virtfn);
+ return ERR_PTR(rc);
}
int pci_iov_add_virtfn(struct pci_dev *dev, int id)
diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 5a779ebaee326..8bf3837204693 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -1777,6 +1777,9 @@ int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
if (!sysfs_initialized)
return -EACCES;
+ if (device_is_sysfs_lazy(&pdev->dev))
+ return 0; /* lazy: entries materialize on first access */
+
return device_sysfs_apply(&pdev->dev, pci_sysfs_entries,
DEV_SYSFS_ADD_ALL, NULL);
}
@@ -1790,6 +1793,12 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
if (!sysfs_initialized)
return;
+ /*
+ * No sysfs_lazy early-return here: populate_one/populate_all may
+ * have materialized resource files on first access, so the
+ * per-entry remove() callbacks must run unconditionally. They
+ * are idempotent either way.
+ */
device_sysfs_apply(&pdev->dev, pci_sysfs_entries,
DEV_SYSFS_REMOVE_ALL, NULL);
}
--
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
More information about the kexec
mailing list