Skip to contentSkip to conversation

device_schedule_reprobe(): core helper and conversions

PATCH Superseded Daniel Golle 2 participants 7 messages Aug 21 – Sep 10

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover device_schedule_reprobe(): core helper and conversions

Three in-tree drivers (iwlwifi, hci_h5, btintel_pcie) schedule a deferred re-probe of their own device from a work item whose work function lives in module text. The hand-rolled copies share two bug
classes: the work function ends with module_put(THIS_MODULE), racing a concurrent rmmod freeing the module text (the race module_put_and_kthread_exit() exists to close for kthreads), and nothing synchronizes the deferred detach against device_shutdown() or an administrative unbind.

Patch 1 moves the deferred work into the driver core. device_schedule_reprobe() runs builtin code, so no module reference is needed; it checks under a single __device_driver_lock() hold that the device is still bound to the driver that scheduled the re-probe, and skips the detach once device_shutdown() has reached the device (a new one-bit shutdown_done flag in struct device_private). It pins both the device and its parent for the lifetime of the work and re-locks the parent across the attach on buses that require it. Patches 2 and 3 are mechanical conversions; patch 4 (btintel_pcie) also retires that driver's remove()-from-own-work contract.

This is a cross-tree series. Each patch applies cleanly on the tree that owns it:

  patch 1 (driver core)      -> driver-core
  patch 2 (iwlwifi)          -> wireless-next
  patches 3, 4 (bluetooth)   -> bluetooth-next

It is based on bluetooth-next so patches 3 and 4 apply there; patch 1 applies equally on driver-core and patch 2 on wireless-next (verified against all three trees). Patches 2-4 depend on the helper from patch 1, so they need it in their tree -- or an immutable branch/tag of it -- before they build.

As Hans put it on the RFC: the tree already has device_reprobe() with 15 callers, so this just adds a way to run one from a worker safely rather than having each open-coded copy reimplement the locking.

History: this was first floated as an RFC (v1) [1]. In parallel the helper rode along in the mxl862xx DSA firmware-update series, whose review [2] caught a parent use-after-free (the work locks the parent, but only the child was pinned) and a missing parent lock across device_attach() on need_parent_lock buses -- both fixed here. net-next is closed and the mxl862xx feature waits for the next window, so the helper is submitted on its own now; mxl862xx is going to use it once it lands.

Testing: Hans de Goede reviewed and tested patches 1 and 3 on hci_h5 hardware that hits the resume re-probe path. Patches 1-3 were also runtime-tested (backported to 7.0.11) on Intel AX101 with PROVE_LOCKING and DEBUG_OBJECTS_WORK, driving iwlwifi's crash escalation into the re-probe path: normal detach+rebind, an unbind racing a pending re-probe (not undone), rmmod with a re-probe pending (now succeeds instead of EBUSY), and reboot with a re-probe pending; no lockdep or debugobjects reports. The helper was exercised further by the mxl862xx v11/v12 hardware testing. btintel_pcie (patch 4) is compile-tested only.

Changes since v2 [3]:
- patch 1: use dev_err_probe() for the re-probe error path so a re-probe deferred at resume no longer logs a spurious error; pick up Hans de Goede's Tested-by/Reviewed-by
- patch 3: pick up Hans de Goede's Tested-by/Reviewed-by
- patch 4: rebased onto bluetooth-next, whose btintel_pcie has four dump workers rather than the single one the v2 mainline snapshot had
- base the series on bluetooth-next instead of v7.2-rc7 so the bluetooth patches apply for their maintainers

Changes since the RFC (v1) [1]:
- pin the parent device across the deferred work and re-lock it across device_attach() on need_parent_lock buses (found reviewing the mxl862xx posting [2])
- schedule the work on system_dfl_wq instead of the deprecated system_unbound_wq
- thread the series properly (the RFC's patches were unthreaded)
- drop RFC status

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/

Daniel Golle (4):
driver core: add device_schedule_reprobe()
wifi: iwlwifi: use device_schedule_reprobe()
Bluetooth: hci_h5: use device_schedule_reprobe()
Bluetooth: btintel_pcie: use device_schedule_reprobe() after reset

 drivers/base/base.h                           |   5 +
 drivers/base/core.c                           |   3 +
 drivers/base/dd.c                             | 102 ++++++++++++++++++
 drivers/bluetooth/btintel_pcie.c              |  49 +++++----
 drivers/bluetooth/hci_h5.c                    |  43 ++------
 .../net/wireless/intel/iwlwifi/iwl-trans.c    |  40 +------
 include/linux/device.h                        |   2 +
 7 files changed, 146 insertions(+), 98 deletions(-)

base-commit: 486f8908aa587ab2a213bbef39311743e4f8f57a

Signature · 1 line
2.55.0
#1 lore raw

PATCH v3 1/4 driver core: add device_schedule_reprobe()

Three in-tree drivers schedule a deferred re-probe of their own device from a work item whose work function lives in module text: iwlwifi (iwl_trans_schedule_reprobe(), firmware crash recovery when a lighter restart is not sufficient), hci_h5 (h5_btrtl_resume(), RTL devices lose their firmware state over suspend) and btintel_pcie (synchronous device_reprobe() from its own reset work, with a hand-rolled locking contract spanning several comments).

Two bug classes affect the hand-rolled implementations:

1. The work function ends with put_device(); kfree(); module_put(THIS_MODULE); in module text. After the atomic decrement a concurrent rmmod can free the module text before the function epilogue has finished executing. This is exactly the race module_put_and_kthread_exit() exists to close for kthreads; there is no work-item equivalent.

2. There is no synchronization between the deferred device_reprobe() and device_shutdown() or a driver unbind. The drivers do not check any bound state before calling device_reprobe(), so a stale re-probe can undo an administrative unbind, and the detach half can run against a device whose ->shutdown() callback has already run. The core already blocks the attach half during shutdown (device_shutdown() calls device_block_probing() before any callback, and really_probe() honors defer_all_probes), but nothing blocks the detach half. For drivers which clear their drvdata in ->shutdown() so that a subsequent ->remove() becomes a no-op this escalates to use-after-free of driver state which other subsystem structures still reference.

Both classes disappear when the driver core owns the deferred work. Add device_schedule_reprobe(), which schedules a detach and re-probe of a device after a caller-specified delay:

- The work function is builtin text, so callers do not need to hold a module reference. If the driver module is unloaded before the work runs, driver_unregister() has already unbound the device, the bound driver no longer matches the driver recorded at scheduling time and the work does nothing.

- The recorded driver pointer is only ever compared, never dereferenced, so it may legitimately point to freed memory.

- The bound-state check and __device_release_driver() run under a single __device_driver_lock() hold, the same lock dance device_release_driver_internal() uses. This closes the check-vs-detach TOCTOU that drivers cannot close themselves, because device_reprobe() takes the device lock internally.

- Both @dev and its parent are pinned for the lifetime of the work. __device_driver_lock() and the attach half lock the parent, and an unregister of @dev drops @dev's reference to the parent, so without a reference of our own the parent could be freed before the work runs.

- A new shutdown_done flag in struct device_private, set under the device lock once device_shutdown() reaches a device, suppresses the detach half during shutdown. It occupies a spare bit in an existing byte, mirroring how kill_device() sets the dead flag.

- The attach half is plain device_attach(), which already honors both the dead flag and defer_all_probes: a re-probe landing during system suspend detaches immediately and the probe is deferred until device_restore_probing() at resume time. The detach half deliberately does not check defer_all_probes so that a re-probe scheduled before suspend is not silently dropped. The parent is re-locked across device_attach() on buses that set need_parent_lock, mirroring bus_rescan_devices_helper().

One pre-existing window remains: __device_release_driver() transiently drops the locks while consumer device links are busy, so for devices with busy consumers a ->shutdown() can still interleave in the middle of the release. That window exists identically for every unbind path in the kernel, sysfs unbind included, and is not made worse by this helper.

---
v3: use dev_err_probe() for the re-probe error path, so a re-probe
deferred at resume no longer logs a spurious error (Hans de Goede)
v2: pin the parent across the deferred work and re-lock it across
device_attach() on need_parent_lock buses; schedule on system_dfl_wq rather than the deprecated system_unbound_wq (found reviewing the mxl862xx v12 posting)
v1: initial RFC

 drivers/base/base.h    |   5 ++
 drivers/base/core.c    |   3 ++
 drivers/base/dd.c      | 102 +++++++++++++++++++++++++++++++++++++++++
 include/linux/device.h |   2 +
 4 files changed, 112 insertions(+)
drivers/base/base.h +5 -0
index a5b7abc10ff0..6234e37de7e9 100644
@@ -106,6 +106,10 @@ struct driver_private {
  * @dead: This device is currently either in the process of or has been
  *	  removed from the system. Any asynchronous events scheduled for this
  *	  device should exit without taking any action.
+ * @shutdown_done: Set once device_shutdown() has reached this device, under
+ *	  the device lock, before any shutdown callback runs. Read under the
+ *	  device lock. A deferred re-probe scheduled with
+ *	  device_schedule_reprobe() must not detach the device anymore.
  *
  * Nothing outside of the driver core should ever touch these fields.
  */
@@ -120,6 +124,7 @@ struct device_private {
 	char *deferred_probe_reason;
 	struct device *device;
 	u8 dead:1;
+	u8 shutdown_done:1;
 };
 #define to_device_private_parent(obj)	\
 	container_of(obj, struct device_private, knode_parent)
drivers/base/core.c +3 -0
index 4d026682944f..8a7dbe4e8362 100644
@@ -4906,6 +4906,9 @@ void device_shutdown(void)
 			device_lock(parent);
 		device_lock(dev);
 
+		if (dev->p)
+			dev->p->shutdown_done = true;
+
 		/* Don't allow any more runtime suspends */
 		pm_runtime_get_noresume(dev);
 		pm_runtime_barrier(dev);
drivers/base/dd.c +102 -0

105 lines not on this page · raw message

include/linux/device.h +2 -0
index 7b2baffdd2f5..1cdd40a6a48d 100644
@@ -1312,6 +1312,8 @@ int  __must_check device_attach(struct device *dev);
 int __must_check driver_attach(const struct device_driver *drv);
 void device_initial_probe(struct device *dev);
 int __must_check device_reprobe(struct device *dev);
+int __must_check device_schedule_reprobe(struct device *dev,
+					 unsigned int delay_ms);
 
 bool device_is_bound(struct device *dev);
 
Signature · 1 line
2.55.0
#2 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1149424

---Test result---

Test Summary:

CheckPatch                    PASS      3.98 seconds
VerifyFixes                   PASS      0.14 seconds
VerifySignedoff               PASS      0.14 seconds
GitLint                       PASS      1.36 seconds
SubjectPrefix                 FAIL      0.56 seconds
BuildKernel                   PASS      27.88 seconds
CheckAllWarning               PASS      30.35 seconds
CheckSparse                   PASS      29.30 seconds
BuildKernel32                 PASS      26.40 seconds
CheckKernelLLVM               SKIP      0.00 seconds
TestRunnerSetup               PASS      502.81 seconds
TestRunner_l2cap-tester       PASS      65.90 seconds
TestRunner_iso-tester         PASS      83.60 seconds
TestRunner_bnep-tester        PASS      19.21 seconds
TestRunner_mgmt-tester        FAIL      221.72 seconds
TestRunner_rfcomm-tester      PASS      25.46 seconds
TestRunner_sco-tester         PASS      36.60 seconds
TestRunner_ioctl-tester       PASS      26.30 seconds
TestRunner_mesh-tester        FAIL      26.05 seconds
TestRunner_smp-tester         PASS      23.47 seconds
TestRunner_userchan-tester    PASS      20.58 seconds
TestRunner_6lowpan-tester     PASS      23.63 seconds
IncrementalBuild              PASS      30.46 seconds

Details
##############################
Test: SubjectPrefix - FAIL
Desc: Check subject contains "Bluetooth" prefix Output:
"Bluetooth: " prefix is not specified in the subject "Bluetooth: " prefix is not specified in the subject
##############################
Test: CheckKernelLLVM - SKIP
Desc: Build kernel with LLVM + context analysis Output:
Clang not found
##############################
Test: TestRunner_mgmt-tester - FAIL
Desc: Run mgmt-tester with test-runner Output:
Total: 501, Passed: 496 (99.0%), Failed: 1, Not Run: 4

Failed Test Cases
Read Exp Feature - Success Failed 0.255 seconds
##############################
Test: TestRunner_mesh-tester - FAIL
Desc: Run mesh-tester with test-runner Output:
Total: 10, Passed: 8 (80.0%), Failed: 2, Not Run: 0

Failed Test Cases

Mesh - Send cancel - 1                               Timed out    1.908 seconds
Mesh - Send cancel - 2                               Timed out    1.988 seconds

https://github.com/bluez/bluetooth-next/pull/627

---
Regards,
Linux Bluetooth

#3 lore raw
In reply to Daniel Golle

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1149424/

---Test result---

Test Summary:

CheckPatch                    PASS      3.48 seconds
VerifyFixes                   PASS      0.10 seconds
VerifySignedoff               PASS      0.10 seconds
GitLint                       PASS      1.24 seconds
SubjectPrefix                 FAIL      0.39 seconds
BuildKernel                   PASS      29.55 seconds
CheckAllWarning               PASS      33.66 seconds
CheckSparse                   PASS      36.63 seconds
BuildKernel32                 PASS      30.00 seconds
CheckKernelLLVM               PASS      43.82 seconds
TestRunnerSetup               PASS      775.59 seconds
TestRunner_l2cap-tester       PASS      44.07 seconds
TestRunner_iso-tester         PASS      93.05 seconds
TestRunner_bnep-tester        PASS      12.56 seconds
TestRunner_mgmt-tester        FAIL      145.83 seconds
TestRunner_rfcomm-tester      PASS      16.05 seconds
TestRunner_sco-tester         PASS      23.41 seconds
TestRunner_ioctl-tester       PASS      17.22 seconds
TestRunner_mesh-tester        FAIL      18.17 seconds
TestRunner_smp-tester         PASS      14.88 seconds
TestRunner_userchan-tester    PASS      12.96 seconds
TestRunner_6lowpan-tester     PASS      16.38 seconds
IncrementalBuild              PASS      39.00 seconds

Details
##############################
Test: SubjectPrefix - FAIL
Desc: Check subject contains "Bluetooth" prefix Output:
"Bluetooth: " prefix is not specified in the subject "Bluetooth: " prefix is not specified in the subject
##############################
Test: TestRunner_mgmt-tester - FAIL
Desc: Run mgmt-tester with test-runner Output:
Total: 501, Passed: 496 (99.0%), Failed: 1, Not Run: 4

Failed Test Cases
Read Exp Feature - Success Failed 0.156 seconds
##############################
Test: TestRunner_mesh-tester - FAIL
Desc: Run mesh-tester with test-runner Output:
Total: 10, Passed: 8 (80.0%), Failed: 2, Not Run: 0

Failed Test Cases

Mesh - Send cancel - 1                               Timed out    2.116 seconds
Mesh - Send cancel - 2                               Timed out    1.996 seconds

https://github.com/bluez/bluetooth-next/pull/740

---
Regards,
Linux Bluetooth

#4 lore raw

PATCH v3 2/4 wifi: iwlwifi: use device_schedule_reprobe()

In reply to Daniel Golle

iwl_trans_schedule_reprobe() open-codes a deferred re-probe: it takes a module reference, allocates a work item, and the work function calls device_reprobe() and then ends with put_device(); kfree(); module_put(THIS_MODULE); in module text. That final module_put() is
racy: once the reference count is decremented a concurrent rmmod can free the module text before the work function's epilogue has finished executing. The work also does not synchronize against shutdown or unbind, so a stale re-probe could undo an administrative unbind or detach a device whose ->shutdown() callback has already run.

Convert to the new device_schedule_reprobe() helper, whose work function is builtin text and which skips the re-probe when the device was removed, shutdown reached it, or it is no longer bound to the driver that scheduled the re-probe. Both call sites keep their delays (IWL_TRANS_TOP_FOLLOWER_WAIT for the TOP follower case, 0 for the escalated firmware error case).

Behavioral changes:

- A pending re-probe no longer pins the module: rmmod with a re-probe pending now succeeds immediately and the re-probe becomes a no-op, instead of rmmod failing with EBUSY. The "Module is being unloaded - abort" path disappears together with the try_module_get().

- A re-probe scheduled before a system shutdown or before an administrative unbind no longer detaches and rebinds the device afterwards.

---
v3: no changes
v2: no changes
v1: initial RFC

 .../net/wireless/intel/iwlwifi/iwl-trans.c    | 40 +------------------
 1 file changed, 2 insertions(+), 38 deletions(-)
drivers/net/wireless/intel/iwlwifi/iwl-trans.c +2 -38
index 73aae1125042..5ae734cb9027 100644
@@ -78,47 +78,11 @@ void iwl_trans_free_restart_list(void)
 	}
 }
 
-struct iwl_trans_reprobe {
-	struct device *dev;
-	struct delayed_work work;
-};
-
-static void iwl_trans_reprobe_wk(struct work_struct *wk)
-{
-	struct iwl_trans_reprobe *reprobe;
-
-	reprobe = container_of(wk, typeof(*reprobe), work.work);
-
-	if (device_reprobe(reprobe->dev))
-		dev_err(reprobe->dev, "reprobe failed!\n");
-	put_device(reprobe->dev);
-	kfree(reprobe);
-	module_put(THIS_MODULE);
-}
-
 static void iwl_trans_schedule_reprobe(struct iwl_trans *trans,
 				       unsigned int delay_ms)
 {
-	struct iwl_trans_reprobe *reprobe;
-
-	/*
-	 * get a module reference to avoid doing this while unloading
-	 * anyway and to avoid scheduling a work with code that's
-	 * being removed.
-	 */
-	if (!try_module_get(THIS_MODULE)) {
-		IWL_ERR(trans, "Module is being unloaded - abort\n");
-		return;
-	}
-
-	reprobe = kzalloc_obj(*reprobe);
-	if (!reprobe) {
-		module_put(THIS_MODULE);
-		return;
-	}
-	reprobe->dev = get_device(trans->dev);
-	INIT_DELAYED_WORK(&reprobe->work, iwl_trans_reprobe_wk);
-	schedule_delayed_work(&reprobe->work, msecs_to_jiffies(delay_ms));
+	if (device_schedule_reprobe(trans->dev, delay_ms))
+		IWL_ERR(trans, "Could not schedule reprobe\n");
 }
 
 #define IWL_TRANS_RESET_OK_TIME	7 /* seconds */
Signature · 1 line
2.55.0
#5 lore raw

PATCH v3 3/4 Bluetooth: hci_h5: use device_schedule_reprobe()

In reply to Daniel Golle

h5_btrtl_resume() open-codes a deferred re-probe for RTL devices that lose their firmware state over suspend: it takes a module reference, allocates a work item, and the work function calls device_reprobe() and then ends with put_device(); kfree(); module_put(THIS_MODULE); in module text. That final module_put() is racy: once the reference count is decremented a concurrent rmmod can free the module text before the work function's epilogue has finished executing. The work also does not synchronize against shutdown or unbind, so a stale re-probe could undo an administrative unbind or detach a device whose ->shutdown() callback has already run.

Convert to the new device_schedule_reprobe() helper, whose work function is builtin text and which skips the re-probe when the device was removed, shutdown reached it, or it is no longer bound to the driver that scheduled the re-probe.

The old worker suppressed its error message for -EPROBE_DEFER; the helper needs no equivalent because its attach half is device_attach(), which folds probe deferral into the deferred-probe machinery silently.

Behavioral changes:

- A pending re-probe no longer pins the module: rmmod with a re-probe pending now succeeds immediately and the re-probe becomes a no-op, instead of rmmod failing with EBUSY.

- A re-probe scheduled before a system shutdown or before an administrative unbind no longer detaches and rebinds the device afterwards.

- A re-probe racing the next suspend now detaches immediately while the probe is deferred until the following resume by the defer_all_probes machinery, instead of probing mid-suspend.

---
v3: picked up Hans de Goede's Tested-by/Reviewed-by
v2: also correct the stale device_reprobe() reference in the
h5_btrtl_open() comment
v1: initial RFC

 drivers/bluetooth/hci_h5.c | 43 ++++++--------------------------------
 1 file changed, 6 insertions(+), 37 deletions(-)
drivers/bluetooth/hci_h5.c +6 -37
index b1999e14aade..3fde1d5a5ae9 100644
@@ -990,7 +990,7 @@ static int h5_btrtl_setup(struct h5 *h5)
 static void h5_btrtl_open(struct h5 *h5)
 {
 	/*
-	 * Since h5_btrtl_resume() does a device_reprobe() the suspend handling
+	 * Since h5_btrtl_resume() schedules a device re-probe the suspend handling
 	 * done by the hci_suspend_notifier is not necessary; it actually causes
 	 * delays and a bunch of errors to get logged, so disable it.
 	 */
@@ -1049,46 +1049,15 @@ static int h5_btrtl_suspend(struct h5 *h5)
 	return 0;
 }
 
-struct h5_btrtl_reprobe {
-	struct device *dev;
-	struct work_struct work;
-};
-
-static void h5_btrtl_reprobe_worker(struct work_struct *work)
-{
-	struct h5_btrtl_reprobe *reprobe =
-		container_of(work, struct h5_btrtl_reprobe, work);
-	int ret;
-
-	ret = device_reprobe(reprobe->dev);
-	if (ret && ret != -EPROBE_DEFER)
-		dev_err(reprobe->dev, "Reprobe error %d\n", ret);
-
-	put_device(reprobe->dev);
-	kfree(reprobe);
-	module_put(THIS_MODULE);
-}
-
 static int h5_btrtl_resume(struct h5 *h5)
 {
-	if (test_bit(H5_WAKEUP_DISABLE, &h5->flags)) {
-		struct h5_btrtl_reprobe *reprobe;
-
-		reprobe = kzalloc_obj(*reprobe);
-		if (!reprobe)
-			return -ENOMEM;
-
-		__module_get(THIS_MODULE);
+	if (test_bit(H5_WAKEUP_DISABLE, &h5->flags))
+		return device_schedule_reprobe(&h5->hu->serdev->dev, 0);
 
-		INIT_WORK(&reprobe->work, h5_btrtl_reprobe_worker);
-		reprobe->dev = get_device(&h5->hu->serdev->dev);
-		queue_work(system_long_wq, &reprobe->work);
-	} else {
-		gpiod_set_value_cansleep(h5->device_wake_gpio, 1);
+	gpiod_set_value_cansleep(h5->device_wake_gpio, 1);
 
-		if (test_bit(H5_HW_FLOW_CONTROL, &h5->flags))
-			serdev_device_set_flow_control(h5->hu->serdev, true);
-	}
+	if (test_bit(H5_HW_FLOW_CONTROL, &h5->flags))
+		serdev_device_set_flow_control(h5->hu->serdev, true);
 
 	return 0;
 }
Signature · 1 line
2.55.0
#6 lore raw

PATCH v3 4/4 Bluetooth: btintel_pcie: use device_schedule_reprobe() after reset

In reply to Daniel Golle

btintel_pcie re-probes its own device synchronously from its own reset work via device_reprobe(), both after an FLR and after an ACPI PLDR. The synchronous call runs .remove() from inside the reset work, which requires a hand-rolled correctness contract spanning several comments:
.remove() must skip draining the very reset work it is running from (the current_work() check), the reset must use pci_try_reset_function() to dodge a device_lock ABBA against .remove(), and both reset paths must not touch 'data' after the reprobe call because .remove() has freed it.

Convert the two BT self re-probes to device_schedule_reprobe(). The driver core's builtin work item now triggers .remove(), never the reset work itself, so the current_work() check in .remove() is dead and is removed: disable_work_sync(&data->reset_work) now also guarantees the reset work has fully returned before 'data' is freed. The Wi-Fi sibling re-probe in the PLDR path is left untouched; the sibling is a different device re-probed from a bounded context and has neither of the bug classes the helper addresses.

Safety of the window between the reset work returning and the deferred detach running, during which 'data' now stays alive:

- hci callbacks: send_frame fails with -ENODEV while BTINTEL_PCIE_RECOVERY_IN_PROGRESS is set, and that bit is only cleared by a fresh probe. New reset requests coalesce into the in-flight one via the same bit. open/close are no-ops.

- interrupts: the reset work masks all interrupt causes and synchronizes the IRQs before the reset, the dump workers stay disabled (disable count >= 1) until .remove(), rx_work is flushed, and the freshly reset device raises no traffic until the next probe re-initialises it. The same exposure already exists today in the window between pci_try_reset_function() and the synchronous re-probe; the conversion only lengthens it.

- work disable counts: the success-path contract is unchanged in substance. The reset work's disable_work_sync() calls stay unbalanced on success, .remove() disables again, and the fresh probe re-INIT_WORKs the dump workers with disable count 0.

pci_lock_rescan_remove() now only covers the reset itself, no longer the re-probe. Hot-removal between the reset and the deferred re-probe is handled by the helper's dead-device check.

If scheduling the re-probe fails in the FLR path, the error is returned so the reset work re-enables the dump workers, matching the existing FLR failure handling; unlike before, 'data' is still alive in that case. The PLDR path keeps logging only, as before.

---
v3: rebased onto bluetooth-next, whose btintel_pcie has four dump
workers (coredump/hwexp/fwtrigger/mbox) rather than the single one the mainline snapshot v2 targeted
v2: reconciled to the current tree and adapted the commit message
v1: initial RFC

 drivers/bluetooth/btintel_pcie.c | 49 +++++++++++++++++---------------
 1 file changed, 26 insertions(+), 23 deletions(-)
drivers/bluetooth/btintel_pcie.c +26 -23
index baa621b3fef9..29be05d41429 100644
@@ -3012,7 +3012,7 @@ static void btintel_pcie_perform_pldr(struct btintel_pcie_data *data)
 	 * BT needs pci_save_state()/pci_restore_state() because the BT driver
 	 * is still partially attached when the _PRR runs (it hasn't been unbound yet).
 	 * The PCI device needs to remain minimally functional so that
-	 * device_reprobe(&pdev->dev) can work afterward
+	 * the deferred re-probe of the BT device can work afterward
 	 */
 	ret = btintel_pcie_acpi_reset_method(data);
 
@@ -3023,14 +3023,16 @@ static void btintel_pcie_perform_pldr(struct btintel_pcie_data *data)
 	}
 
 	if (!ret) {
-		if (device_reprobe(&pdev->dev))
-			BT_ERR("BT reprobe failed for BDF:%s", pci_name(pdev));
+		if (device_schedule_reprobe(&pdev->dev, 0))
+			BT_ERR("BT reprobe scheduling failed for BDF:%s",
+			       pci_name(pdev));
 	}
 }
 
 /*
- * Issue a Function Level Reset and hand teardown/re-init off to the PCI
- * core via device_reprobe(), mirroring the PLDR path's contract.
+ * Issue a Function Level Reset and hand teardown/re-init off to the
+ * driver core via device_schedule_reprobe(), mirroring the PLDR path's
+ * contract.
  *
  * Caller must hold pci_lock_rescan_remove() and must have already
  * disabled interrupts and drained both rx_work and coredump_work.
@@ -3052,14 +3054,12 @@ static int btintel_pcie_perform_flr(struct btintel_pcie_data *data)
 		return err;
 	}
 
-	/* device_reprobe() always detaches the driver first (running
-	 * .remove(), which frees 'data'); any re-probe failure leaves the
-	 * device unbound but 'data' is already gone, so just log it.
-	 */
-	if (device_reprobe(&pdev->dev))
-		BT_ERR("BT reprobe failed for BDF:%s", pci_name(pdev));
+	err = device_schedule_reprobe(&pdev->dev, 0);
+	if (err)
+		BT_ERR("BT reprobe scheduling failed for BDF:%s",
+		       pci_name(pdev));
 
-	return 0;
+	return err;
 }
 
 static void btintel_pcie_reset_work(struct work_struct *wk)
@@ -3090,11 +3090,15 @@ static void btintel_pcie_reset_work(struct work_struct *wk)
 
 	bt_dev_dbg(data->hdev, "Release bluetooth interface");
 
-	/* Both reset paths follow the same contract: on success they
-	 * destroy 'data' via device_reprobe() (a fresh probe re-INIT_WORKs
-	 * the dump workers with disable count 0), so enable_work() must
-	 * NOT be called on the success path. Only the FLR path can fail
-	 * with 'data' still alive, in which case we balance the
+	/* Both reset paths follow the same contract: on success the
+	 * deferred re-probe scheduled with device_schedule_reprobe()
+	 * destroys 'data' by re-running .probe() (which re-INIT_WORKs the
+	 * dump workers with disable count 0), so enable_work() must NOT be
+	 * called on the success path. 'data' stays alive until the deferred
+	 * detach runs; in this window new activity is fenced by
+	 * BTINTEL_PCIE_RECOVERY_IN_PROGRESS, the masked interrupts and the
+	 * disabled dump workers. Only the FLR path can fail with no
+	 * re-probe scheduled, in which case we balance the
 	 * disable_work_sync() calls above so a later successful reset is
 	 * not permanently blocked.
 	 *
@@ -3460,13 +3464,12 @@ static void btintel_pcie_remove(struct pci_dev *pdev)
 	disable_work_sync(&data->fwtrigger_work);
 	disable_work_sync(&data->mbox_work);
 
-	/* Cancel pending reset work. Skip only when remove() is called from
-	 * within the reset work itself (PLDR device_reprobe path) to avoid
-	 * deadlock. current_work() returns the work_struct of the caller if
-	 * we are in a workqueue context.
+	/* The deferred re-probe triggers .remove() from the driver core's
+	 * work item, never from reset_work itself, so this no longer runs
+	 * nested in reset_work; disable_work_sync() also guarantees the
+	 * reset work has fully returned before 'data' is freed.
 	 */
-	if (current_work() != &data->reset_work)
-		disable_work_sync(&data->reset_work);
+	disable_work_sync(&data->reset_work);
 
 	btintel_pcie_disable_interrupts(data);
 
Signature · 1 line
2.55.0