PCI/proc: Fix race between pci_proc_init() and pci_bus_add_device()
PATCH v3 1/1 PCI/proc: Fix race between pci_proc_init() and pci_bus_add_device()
pci_proc_attach_device() creates procfs entries for PCI devices and is called from pci_bus_add_device(). It lazily creates the per-bus procfs directory (bus->procdir) via proc_mkdir() on first use, and returns early if proc_initialized is not yet set.
On x86 with ACPI, PCI enumeration occurs at subsys_initcall, before pci_proc_init() sets proc_initialized at device_initcall. The for_each_pci_dev() loop in pci_proc_init() then creates procfs entries for these already-enumerated devices, but runs without holding pci_rescan_remove_lock.
On ARM64 with devicetree, PCI host bridges probe at device_initcall. With async probing enabled, pci_bus_add_device() can run concurrently with pci_proc_init(), and both may call pci_proc_attach_device() for the same device or for different devices on the same bus. As pci_host_probe() holds pci_rescan_remove_lock while pci_proc_init() does not, there is no serialisation between the two paths.
When two threads concurrently call pci_proc_attach_device() for devices on the same bus, both observe bus->procdir as NULL and both call proc_mkdir(). The proc filesystem serialises directory creation internally, so only one caller succeeds. The other receives NULL (duplicate entry) and unconditionally stores it to bus->procdir, corrupting the valid pointer set by the first caller.
Thus, extract the bus procfs directory creation from pci_proc_attach_device() into a new pci_proc_attach_bus() function, and call it from the two bus creation paths: pci_register_host_bridge() for root buses and pci_alloc_child_bus() for child buses. These are the only two callers of pci_alloc_bus(), so for buses created after proc_initialized is set, bus->procdir is in place before any device can be added to the bus.
Therefore, by the time pci_proc_attach_device() runs on these buses, bus->procdir is already set and the racy proc_mkdir() call is never reached.
For buses created before pci_proc_init() sets proc_initialized (the common x86 ACPI case), the bus creation hooks return early. As such, add a fallback call to pci_proc_attach_bus() from pci_proc_attach_device() to handle these pre-init buses.
Additionally, wrap the for_each_pci_dev() loop in pci_proc_init() with pci_lock_rescan_remove() to serialise against concurrent PCI bus operations, add an early return in pci_proc_attach_device() when dev->procent is already set to make the function idempotent, and clear bus->procdir in pci_proc_detach_bus() to prevent use of a dangling pointer after proc_remove().
---
Changes in v3:
https://lore.kernel.org/linux-pci/[email protected]/
- Used local variables in pci_proc_attach_bus() and pci_proc_attach_device() to ensure that bus->procdir and dev->procent are only assigned on success. If either the proc_mkdir() or proc_create_data() returns NULL, the struct field will not see a new assignment, preventing a valid pointer from being overwritten. This was reported by Sashiko, see:
https://sashiko.dev/#/patchset/20260606170146.673140-1-kwilczynski%40kernel.org?part=1
Changes in v2:
https://lore.kernel.org/linux-pci/[email protected]/
- Extracted bus->procdir creation into new pci_proc_attach_bus() function, addressing the suggestion to move proc_mkdir() to a bus creation path, as per Bjorn Helgaas' feedback.
- Added bus->procdir = NULL in pci_proc_detach_bus() for symmetry and to prevent dangling pointer after proc_remove().
drivers/pci/pci.h | 6 ++-- drivers/pci/probe.c | 6 ++++ drivers/pci/proc.c | 68 ++++++++++++++++++++++++++++++++------------- 3 files changed, 58 insertions(+), 22 deletions(-)
drivers/pci/pci.h +4 -2
@@ -343,13 +343,15 @@ void pci_allocate_vc_save_buffers(struct pci_dev *dev);/* PCI /proc functions */ #ifdef CONFIG_PROC_FS +int pci_proc_attach_bus (struct pci_bus *bus); +int pci_proc_detach_bus (struct pci_bus *bus);int pci_proc_attach_device (struct pci_dev *dev);int pci_proc_detach_device (struct pci_dev *dev); -int pci_proc_detach_bus (struct pci_bus *bus);#else +static inline int pci_proc_attach_bus (struct pci_bus *bus) {return 0 ; } +static inline int pci_proc_detach_bus (struct pci_bus *bus) {return 0 ; }static inline int pci_proc_attach_device (struct pci_dev *dev) {return 0 ; }static inline int pci_proc_detach_device (struct pci_dev *dev) {return 0 ; } -static inline int pci_proc_detach_bus (struct pci_bus *bus) {return 0 ; }#endif /* Functions for PCI Hotplug drivers to use */
drivers/pci/probe.c +6 -0
@@ -1071,6 +1071,9 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)/* Create legacy_io and legacy_mem files for this bus */ pci_create_legacy_files (bus); +/* Create procfs directory for this bus */ +pci_proc_attach_bus (bus); +if (parent)dev_info (parent,"PCI host bridge to bus %s\n" , name);else
@@ -1279,6 +1282,9 @@ static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,/* Create legacy_io and legacy_mem files for this bus */ pci_create_legacy_files (child); +/* Create procfs directory for this bus */ +pci_proc_attach_bus (child); +return child; }
drivers/pci/proc.c +48 -20
100 lines not on this page · raw message
Signature · 1 line
2.54.0
Krzysztof Wilczyński
AppliedHello,
Quoted 42 lines
pci_proc_attach_device()creates procfs entries for PCI devices and is called frompci_bus_add_device(). It lazily creates the per-bus procfs directory (bus->procdir) viaproc_mkdir()on first use, and returns early ifproc_initializedis not yet set.On x86 with ACPI, PCI enumeration occurs at
subsys_initcall, beforepci_proc_init()setsproc_initializedatdevice_initcall. Thefor_each_pci_dev()loop inpci_proc_init()then creates procfs entries for these already-enumerated devices, but runs without holdingpci_rescan_remove_lock.On ARM64 with devicetree, PCI host bridges probe at
device_initcall. With async probing enabled,pci_bus_add_device()can run concurrently withpci_proc_init(), and both may callpci_proc_attach_device()for the same device or for different devices on the same bus. Aspci_host_probe()holdspci_rescan_remove_lockwhilepci_proc_init()does not, there is no serialisation between the two paths.When two threads concurrently call
pci_proc_attach_device()for devices on the same bus, both observebus->procdiras NULL and both callproc_mkdir(). The proc filesystem serialises directory creation internally, so only one caller succeeds. The other receives NULL (duplicate entry) and unconditionally stores it tobus->procdir, corrupting the valid pointer set by the first caller.Thus, extract the bus procfs directory creation from
pci_proc_attach_device()into a newpci_proc_attach_bus()function, and call it from the two bus creation paths:pci_register_host_bridge()for root buses andpci_alloc_child_bus()for child buses. These are the only two callers ofpci_alloc_bus(), so for buses created afterproc_initializedis set,bus->procdiris in place before any device can be added to the bus.Therefore, by the time
pci_proc_attach_device()runs on these buses,bus->procdiris already set and the racyproc_mkdir()call is never reached.For buses created before
pci_proc_init()setsproc_initialized(the common x86 ACPI case), the bus creation hooks return early. As such, add a fallback call topci_proc_attach_bus()frompci_proc_attach_device()to handle these pre-init buses.
Quoted 8 lines
Additionally, wrap the
for_each_pci_dev()loop inpci_proc_init()withpci_lock_rescan_remove()to serialise against concurrent PCI bus operations, add an early return inpci_proc_attach_device()whendev->procentis already set to make the function idempotent, and clearbus->procdirinpci_proc_detach_bus()to prevent use of a dangling pointer afterproc_remove().
Applied to procfs, as I want this to have some soak time via linux-next, plus 0-day bot will test it for me, too.
Lorenzo, do you think you could re-test this on your hardware platform?
Shuan, any chance for you to test this again on your RISC-V based platform?
Thank you!
Krzysztof
Hello,
[...]
Quoted 24 lines
Quoted 24 lines
Thus, extract the bus procfs directory creation from
pci_proc_attach_device()into a newpci_proc_attach_bus()function, and call it from the two bus creation paths:pci_register_host_bridge()for root buses andpci_alloc_child_bus()for child buses. These are the only two callers ofpci_alloc_bus(), so for buses created afterproc_initializedis set,bus->procdiris in place before any device can be added to the bus.Therefore, by the time
pci_proc_attach_device()runs on these buses,bus->procdiris already set and the racyproc_mkdir()call is never reached.For buses created before
pci_proc_init()setsproc_initialized(the common x86 ACPI case), the bus creation hooks return early. As such, add a fallback call topci_proc_attach_bus()frompci_proc_attach_device()to handle these pre-init buses.Additionally, wrap the
for_each_pci_dev()loop inpci_proc_init()withpci_lock_rescan_remove()to serialise against concurrent PCI bus operations, add an early return inpci_proc_attach_device()whendev->procentis already set to make the function idempotent, and clearbus->procdirinpci_proc_detach_bus()to prevent use of a dangling pointer afterproc_remove().
Quoted 4 lines
Quoted 1 line
Applied to procfs, as I want this to have some soak time via linux-next, plus 0-day bot will test it for me, too.
I will be sending a v4 shortly, which aims to solve this problem with a different approach as moving procfs directories to the bus creation time has an unwelcome side effect of leaving empty directories for post-init creation time, such as remove/rescan or when devices are added via a hotplug, etc.
Thank you!
Krzysztof