Skip to contentSkip to conversation

PCI: Prevent workqueue code nesting in pci_call_probe()

PATCH Superseded Waiman Long 2 participants 2 messages May 22 PCI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: Prevent workqueue code nesting in pci_call_probe()

pci_call_probe() can be called recursively. If the recursive calls are done indirectly via workqueue kworker, a lockdep recursive warning can be produced.

There are older commits that tries to prevent that. One example is commit 12c3156f10c5 ("PCI: Avoid unnecessary CPU switch when calling driver .probe() method") which prevents work_on_cpu() recursion when the current device is a virtual function and the physical device has been probed. However, there are still other cases where workqueue code nesting is possible leading to a lockdep recursive locking warning like the following stack trace on a 4-socket Skylake server.

  <TASK>
    :
  work_on_cpu_key()
  pci_call_probe()
  pci_device_probe()
  really_probe()
  __driver_probe_device()
  driver_probe_device()
  __device_attach_driver()
  bus_for_each_drv()
  __device_attach()
  pci_bus_add_device()
  pci_bus_add_devices()
  vmd_enable_domain()
  vmd_probe()
  local_pci_probe()
  work_for_cpu_fn()
  process_one_work()
  worker_thread()
    :
  </TASK>

Fix that by adding a new wq_kworker() helper to check if the current task is likely a workqueue kworker. If so, call local_pci_probe() directly instead of calling into workqueue code recursively.

---
 drivers/pci/pci-driver.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/pci/pci-driver.c +11 -2
index e3f59001785a..3c63098f6fde 100644
@@ -370,6 +370,14 @@ static bool pci_physfn_is_probed(struct pci_dev *dev)
 #endif
 }
 
+/*
+ * Return true if current task is a workqueue kworker
+ */
+static bool wq_kworker(void)
+{
+	return (current->flags & PF_KTHREAD) && strstr(current->comm, "kworker");
+}
+
 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
 			  const struct pci_device_id *id)
 {
@@ -387,10 +395,11 @@ static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
 	cpu_hotplug_disable();
 	/*
 	 * Prevent nesting work_on_cpu() for the case where a Virtual Function
-	 * device is probed from work_on_cpu() of the Physical device.
+	 * device is probed from work_on_cpu() of the Physical device or when
+	 * the current task is a workqueue kworker.
 	 */
 	if (node < 0 || node >= MAX_NUMNODES || !node_online(node) ||
-	    pci_physfn_is_probed(dev)) {
+	    pci_physfn_is_probed(dev) || wq_kworker()) {
 		error = local_pci_probe(&ddi);
 	} else {
 		struct pci_probe_arg arg = { .ddi = &ddi };
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Bypassing the workqueue context switch for tasks identified as kworkers enables a deterministic…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Bypassing the workqueue context switch for tasks identified as kworkers enables a deterministic kernel stack overflow through unbounded recursion when probing nested PCI topologies.
- [Low] The wq_kworker() helper uses an inefficient and brittle string search (strstr) on current->comm instead of the standard task flag.
--

commit ccb28447ef5f5ccb4af61ff521cadbec5879d906
Author: Waiman Long <[email protected]>

PCI: Prevent workqueue code nesting in pci_call_probe()

This commit attempts to prevent lockdep recursive warnings caused by nested workqueue calls in pci_call_probe(). It introduces a helper to check if the current task is a kworker and, if so, executes local_pci_probe() synchronously instead of recursively calling into workqueue code.

Quoted 4 lines
drivers/pci/pci-driver.c +0 -0
index d10ece0889f0f..268ac59ad49a1 100644

[ ... ]

drivers/pci/pci-driver.c static bool pci_physfn_is_probed(struct pci_dev *dev) · line 372

Quoted 11 lines
drivers/pci/pci-driver.c +7 -0
@@ -363,6 +363,14 @@ static bool pci_physfn_is_probed(struct pci_dev *dev)
 #endif
 }
 
+/*
+ * Return true if current task is a workqueue kworker
+ */
+static bool wq_kworker(void)
+{
+	return (current->flags & PF_KTHREAD) && strstr(current->comm, "kworker");
+}

[Severity: Low]
Is there a reason to use a string comparison on current->comm instead of checking current->flags & PF_WQ_WORKER?

Other kernel threads might coincidentally contain "kworker" in their name, leading to false positives that skip queue_work_on() and defeat the NUMA optimization in pci_call_probe().

Quoted 6 lines
drivers/pci/pci-driver.c +1 -0
+
 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
 			  const struct pci_device_id *id)
 {
@@ -380,10 +388,11 @@ static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
 	cpu_hotplug_disable();

drivers/pci/pci-driver.c static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev, · line 396

Quoted 10 lines
drivers/pci/pci-driver.c +3 -2
 	/*
 	 * Prevent nesting work_on_cpu() for the case where a Virtual Function
-	 * device is probed from work_on_cpu() of the Physical device.
+	 * device is probed from work_on_cpu() of the Physical device or when
+	 * the current task is a workqueue kworker.
 	 */
 	if (node < 0 || node >= MAX_NUMNODES || !node_online(node) ||
-	    pci_physfn_is_probed(dev)) {
+	    pci_physfn_is_probed(dev) || wq_kworker()) {
 		error = local_pci_probe(&ddi);

[Severity: High]
Does bypassing the workqueue context switch here expose us to a kernel stack overflow when probing nested PCI topologies?

Because the initial probe runs in a kworker, wq_kworker() will evaluate to true, causing any nested probes to execute synchronously on the same stack.

If a deeply nested PCIe topology is presented (e.g., via Thunderbolt with chained VMD endpoints), could the recursive synchronous calls to pci_call_probe() exhaust the 16KB kernel stack and trigger a system panic?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1