linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] PCI: Remove redudant calls to pci_create_sysfs_dev_files() and pci_proc_attach_device()
@ 2025-07-23 11:11 Manivannan Sadhasivam
  2025-07-23 12:55 ` Lukas Wunner
  0 siblings, 1 reply; 5+ messages in thread
From: Manivannan Sadhasivam @ 2025-07-23 11:11 UTC (permalink / raw)
  To: bhelgaas; +Cc: linux-pci, linux-kernel, Manivannan Sadhasivam, Shuan He

Both pci_create_sysfs_dev_files() and pci_proc_attach_device() are called
from pci_bus_add_device(). Calling these APIs from other places is prone to
a race condition as nothing prevents the callers from racing against
each other.

Moreover, the proper place to create SYSFS and PROCFS entries is during
the 'pci_dev' creation. So there is no real need to call these APIs
elsewhere.

Hence, remove the calls from pci_sysfs_init() and pci_proc_init().

Reported-by: Shuan He <heshuan@bytedance.com>
Closes: https://lore.kernel.org/linux-pci/20250702155112.40124-1-heshuan@bytedance.com
Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com>
---
 drivers/pci/pci-sysfs.c | 9 ---------
 drivers/pci/proc.c      | 3 ---
 2 files changed, 12 deletions(-)

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 268c69daa4d5..8e712c14e6ea 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -1676,18 +1676,9 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
 
 static int __init pci_sysfs_init(void)
 {
-	struct pci_dev *pdev = NULL;
 	struct pci_bus *pbus = NULL;
-	int retval;
 
 	sysfs_initialized = 1;
-	for_each_pci_dev(pdev) {
-		retval = pci_create_sysfs_dev_files(pdev);
-		if (retval) {
-			pci_dev_put(pdev);
-			return retval;
-		}
-	}
 
 	while ((pbus = pci_find_next_bus(pbus)))
 		pci_create_legacy_files(pbus);
diff --git a/drivers/pci/proc.c b/drivers/pci/proc.c
index 9348a0fb8084..b78286afe18e 100644
--- a/drivers/pci/proc.c
+++ b/drivers/pci/proc.c
@@ -463,13 +463,10 @@ int pci_proc_detach_bus(struct pci_bus *bus)
 
 static int __init pci_proc_init(void)
 {
-	struct pci_dev *dev = NULL;
 	proc_bus_pci_dir = proc_mkdir("bus/pci", NULL);
 	proc_create_seq("devices", 0, proc_bus_pci_dir,
 		    &proc_bus_pci_devices_op);
 	proc_initialized = 1;
-	for_each_pci_dev(dev)
-		pci_proc_attach_device(dev);
 
 	return 0;
 }
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] PCI: Remove redudant calls to pci_create_sysfs_dev_files() and pci_proc_attach_device()
  2025-07-23 11:11 [PATCH] PCI: Remove redudant calls to pci_create_sysfs_dev_files() and pci_proc_attach_device() Manivannan Sadhasivam
@ 2025-07-23 12:55 ` Lukas Wunner
  2025-07-23 13:40   ` Manivannan Sadhasivam
  0 siblings, 1 reply; 5+ messages in thread
From: Lukas Wunner @ 2025-07-23 12:55 UTC (permalink / raw)
  To: Manivannan Sadhasivam
  Cc: bhelgaas, linux-pci, linux-kernel, Shuan He, kwilczynski

On Wed, Jul 23, 2025 at 04:41:24PM +0530, Manivannan Sadhasivam wrote:
> Both pci_create_sysfs_dev_files() and pci_proc_attach_device() are called
> from pci_bus_add_device(). Calling these APIs from other places is prone to
> a race condition as nothing prevents the callers from racing against
> each other.
> 
> Moreover, the proper place to create SYSFS and PROCFS entries is during
> the 'pci_dev' creation. So there is no real need to call these APIs
> elsewhere.

The raison d'être for the call to pci_create_sysfs_dev_files() in
pci_sysfs_init() is that PCI_ROM_RESOURCEs may appear after device
enumeration but before the late_initcall stage:

https://lore.kernel.org/r/20231019200110.GA1410324@bhelgaas/

Your patch will regress those platforms.

The proper solution is to make the resource files in sysfs static
and call sysfs_update_group() from pci_sysfs_init().

Krzysztof has an old branch where he started working on this:

https://github.com/kwilczynski/linux/commits/kwilczynski/sysfs-static-resource-attributes/

Thanks,

Lukas

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] PCI: Remove redudant calls to pci_create_sysfs_dev_files() and pci_proc_attach_device()
  2025-07-23 12:55 ` Lukas Wunner
@ 2025-07-23 13:40   ` Manivannan Sadhasivam
       [not found]     ` <CAKmKDK=dOZp1a_syV1fjdo2gjEJX=C21A_mDsMqZVZrKLjf46A@mail.gmail.com>
  0 siblings, 1 reply; 5+ messages in thread
From: Manivannan Sadhasivam @ 2025-07-23 13:40 UTC (permalink / raw)
  To: Lukas Wunner
  Cc: Manivannan Sadhasivam, bhelgaas, linux-pci, linux-kernel,
	Shuan He, kwilczynski

On Wed, Jul 23, 2025 at 02:55:28PM GMT, Lukas Wunner wrote:
> On Wed, Jul 23, 2025 at 04:41:24PM +0530, Manivannan Sadhasivam wrote:
> > Both pci_create_sysfs_dev_files() and pci_proc_attach_device() are called
> > from pci_bus_add_device(). Calling these APIs from other places is prone to
> > a race condition as nothing prevents the callers from racing against
> > each other.
> > 
> > Moreover, the proper place to create SYSFS and PROCFS entries is during
> > the 'pci_dev' creation. So there is no real need to call these APIs
> > elsewhere.
> 
> The raison d'être for the call to pci_create_sysfs_dev_files() in
> pci_sysfs_init() is that PCI_ROM_RESOURCEs may appear after device
> enumeration but before the late_initcall stage:
> 
> https://lore.kernel.org/r/20231019200110.GA1410324@bhelgaas/
> 
> Your patch will regress those platforms.
> 

Ok, thanks for the pointer. I was not aware of this issue.

> The proper solution is to make the resource files in sysfs static
> and call sysfs_update_group() from pci_sysfs_init().
> 
> Krzysztof has an old branch where he started working on this:
> 
> https://github.com/kwilczynski/linux/commits/kwilczynski/sysfs-static-resource-attributes/
> 

I had a chat with Krzysztof offline and he promised me that he will revive this
effort (thanks Krzysztof!).

- Mani

-- 
மணிவண்ணன் சதாசிவம்

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [External] Re: [PATCH] PCI: Remove redudant calls to pci_create_sysfs_dev_files() and pci_proc_attach_device()
       [not found]     ` <CAKmKDK=dOZp1a_syV1fjdo2gjEJX=C21A_mDsMqZVZrKLjf46A@mail.gmail.com>
@ 2025-08-07 14:14       ` Krzysztof Wilczyński
       [not found]         ` <CAKmKDKmCfUgj+ZAjH-pKeaDu2xz6j1tbD7kHMUWbCRFxYAPenA@mail.gmail.com>
  0 siblings, 1 reply; 5+ messages in thread
From: Krzysztof Wilczyński @ 2025-08-07 14:14 UTC (permalink / raw)
  To: Shuan He
  Cc: Manivannan Sadhasivam, Lukas Wunner, Manivannan Sadhasivam,
	bhelgaas, linux-pci, linux-kernel

Hello,

[...]
> Or we should wait Krzysztof's work got finished first, then
> make further moves?

What "moves" do you plan to make here?  I would say... please be a little
patient.  My day job is keeping me busy at the moment. Nevertheless, I plan
to get to this very soon.

[...]
> > https://github.com/kwilczynski/linux/commits/kwilczynski/sysfs-static-resource-attributes/

This is not the latest version, please don't use it for anything (this has
been since removed).

Thank you!

P.S. Try not to top post when replying.

	Krzysztof

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [External] Re: [PATCH] PCI: Remove redudant calls to pci_create_sysfs_dev_files() and pci_proc_attach_device()
       [not found]         ` <CAKmKDKmCfUgj+ZAjH-pKeaDu2xz6j1tbD7kHMUWbCRFxYAPenA@mail.gmail.com>
@ 2025-08-07 15:14           ` Krzysztof Wilczyński
  0 siblings, 0 replies; 5+ messages in thread
From: Krzysztof Wilczyński @ 2025-08-07 15:14 UTC (permalink / raw)
  To: Shuan He
  Cc: Manivannan Sadhasivam, Lukas Wunner, Manivannan Sadhasivam,
	bhelgaas, linux-pci, linux-kernel

Hello,

> Very sorry to bring any disturbance.

Nothing to apologise for.  If anything, I am sorry for being slow with
getting this done causing you and others some grief when this breaks.

> > What "moves" do you plan to make here?  I would say... please be a little
> > patient.  My day job is keeping me busy at the moment. Nevertheless, I
> plan
> > to get to this very soon.
> Thanks in advance for your efforts. Please do this in your early
> convenience then.

Are you having an issue with this, actually?  If so, what platform and
kernel version?  If you don't mind sharing - it's nice to document more
cases where this is causing issues...

> Plus, no worries, "moves" here didn't mean anything.

So, no dance moves? :-)

> https://github.com/kwilczynski/linux/commits/kwilczynski/sysfs-static-resource-attributes/
> > This is not the latest version, please don't use it for anything (this has
> > been since removed).
> Got it. Thanks.
> 
> > P.S. Try not to top post when replying.
> Sorry again. :(

No worries.  Not a big issue :)  It has to do with readability, as when top
and bottom posts mix up, then it's hard to follow the conversation (plus
the ettiquete of posting on the Linux-related mailing lists applies here,
too).

Thank you!

	Krzysztof

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-08-07 15:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-23 11:11 [PATCH] PCI: Remove redudant calls to pci_create_sysfs_dev_files() and pci_proc_attach_device() Manivannan Sadhasivam
2025-07-23 12:55 ` Lukas Wunner
2025-07-23 13:40   ` Manivannan Sadhasivam
     [not found]     ` <CAKmKDK=dOZp1a_syV1fjdo2gjEJX=C21A_mDsMqZVZrKLjf46A@mail.gmail.com>
2025-08-07 14:14       ` [External] " Krzysztof Wilczyński
     [not found]         ` <CAKmKDKmCfUgj+ZAjH-pKeaDu2xz6j1tbD7kHMUWbCRFxYAPenA@mail.gmail.com>
2025-08-07 15:14           ` Krzysztof Wilczyński

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).