Linux PCI subsystem development
 help / color / mirror / Atom feed
From: "Krzysztof Wilczyński" <kwilczynski@kernel.org>
To: Bjorn Helgaas <bhelgaas@google.com>
Cc: "Bjorn Helgaas" <helgaas@kernel.org>,
	"Manivannan Sadhasivam" <mani@kernel.org>,
	"Lorenzo Pieralisi" <lpieralisi@kernel.org>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	"Lukas Wunner" <lukas@wunner.de>,
	"Shuan He" <heshuan@bytedance.com>,
	linux-pci@vger.kernel.org
Subject: Re: [PATCH v3] PCI/proc: Fix race between pci_proc_init() and pci_bus_add_device()
Date: Fri, 12 Jun 2026 00:01:52 +0900	[thread overview]
Message-ID: <20260611143710.GA375273@rocinante> (raw)
In-Reply-To: <20260606212045.GA2380983@rocinante>

Hello,

[...]
> > 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().
> > 
> > Closes: https://lore.kernel.org/linux-pci/20250702155112.40124-2-heshuan@bytedance.com/
> 
> 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

      reply	other threads:[~2026-06-11 15:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-06 20:30 [PATCH v3] PCI/proc: Fix race between pci_proc_init() and pci_bus_add_device() Krzysztof Wilczyński
2026-06-06 21:23 ` Krzysztof Wilczyński
2026-06-11 15:01   ` Krzysztof Wilczyński [this message]

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=20260611143710.GA375273@rocinante \
    --to=kwilczynski@kernel.org \
    --cc=bhelgaas@google.com \
    --cc=helgaas@kernel.org \
    --cc=heshuan@bytedance.com \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux-pci@vger.kernel.org \
    --cc=lpieralisi@kernel.org \
    --cc=lukas@wunner.de \
    --cc=mani@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