On Tue, Oct 14, 2025 at 12:37:16AM +0200, Christian Marangi wrote:
+ Krzysztof Wilczyński, Shuan He (since they were involved in similar discussion before)
In some specific scenario it's possible that the pci_create_resource_files() gets called multiple times and the created entry actually gets wrongly deleted with extreme case of having a NULL pointer dereference when the PCI is removed.
This mainly happen due to bad timing where the PCI bus is adding PCI devices and at the same time the sysfs code is adding the entry causing double execution of the pci_create_resource_files function and kernel WARNING.
To be more precise there is a race between the late_initcall of pci-sysfs with pci_sysfs_init and PCI bus.c pci_bus_add_device that also call pci_create_sysfs_dev_files.
With correct amount of ""luck"" (or better say bad luck) pci_create_sysfs_dev_files in bus.c might be called with pci_sysfs_init is executing the loop.
This has been reported multiple times and on multiple system, like imx6 system, ipq806x systems...
Yes. More recently on the RISC-V platform: https://lore.kernel.org/linux-pci/20250702155112.40124-1-heshuan@bytedance.c...
To address this, imlement multiple improvement to the implementation:
- Add a bool to pci_dev to flag when sysfs entry are created (sysfs_init)
- Implement a simple completion to wait pci_sysfs_init execution.
- Permit additional call of pci_create_sysfs_dev_files only after pci_sysfs_init has finished.
With such logic in place, we address al kind of timing problem with minimal change to any driver.
We do have the same issue with pci_proc_attach_device() as well. I submitted a dumb series [1] that removed both pci_create_sysfs_dev_files() and pci_proc_attach_device() calls from their _init() calls, but I was pointed out that they are required for PCI_ROM_RESOURCE.
Then it was suggested that making the sysfs resource files static would be the proper solution (not sure what's about proc). Krzysztof had some work on this topic earlier and had plans to revive it, but I guess he didn't get much time so far.
Krzysztof, if you do not mind, could you please share your previous work so that someone else can try to extend it if you are busy?
- Mani
[1] https://lore.kernel.org/linux-pci/20250723111124.13694-1-manivannan.sadhasiv...