device_schedule_reprobe(): core helper and conversions
Subsystems (3)
v2 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, replacing the current_work() dance with the deferred re-probe; it changes more and can be dropped without affecting patches 1-3.
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 of the open-coded copies reimplement the locking.
This was first floated as an RFC [1]. In parallel the helper rode along in the mxl862xx DSA firmware-update series as its patch 3, and the v11 [2] and v12 [3] postings put it through further review: an automated review of v12 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 are 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 converts to it once it lands.
Testing: Patches 1-3 were runtime-tested (backported to 7.0.11) on Intel AX101 hardware 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 (the unbind is 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 same helper was exercised by the mxl862xx v11/v12 hardware testing across repeated devlink firmware-flash and re-probe cycles, including a re-probe pending across unbind, rmmod and reboot. hci_h5 and btintel_pcie are compile-tested only.
Changes since the RFC (v1) [1]:
- rebased from net-next onto v7.2-rc7
- pin the parent device across the deferred work and re-lock it across device_attach() on need_parent_lock buses (found by the automated review of the mxl862xx v12 posting)
- 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 | 99 +++++++++++++++++++ drivers/bluetooth/btintel_pcie.c | 51 +++++----- drivers/bluetooth/hci_h5.c | 43 ++------ .../net/wireless/intel/iwlwifi/iwl-trans.c | 40 +------- include/linux/device.h | 2 + 7 files changed, 144 insertions(+), 99 deletions(-)
base-commit: db2ddb87143519e20a95aa36c60b36107b736a58
Signature · 1 line
2.55.0
PATCH v2 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.
--- drivers/base/base.h | 5 +++ drivers/base/core.c | 3 ++ drivers/base/dd.c | 99 ++++++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 2 + 4 files changed, 109 insertions(+)
drivers/base/base.h +5 -0
@@ -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
@@ -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 +99 -0
102 lines not on this page · raw message
include/linux/device.h +2 -0
@@ -1314,6 +1314,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
This is an automated email and please do not reply to this email.
Dear Submitter,
Thank you for submitting the patches to the linux bluetooth mailing list.
While preparing the CI tests, the patches you submitted couldn't be applied to the current HEAD of the repository.
----- Output -----
error: patch failed: drivers/bluetooth/btintel_pcie.c:2657
error: drivers/bluetooth/btintel_pcie.c: patch does not apply
hint: Use 'git am --show-current-patch' to see the failed patch
Please resolve the issue and submit the patches again.
---
Regards,
Linux Bluetooth
Re: [PATCH v2 1/4] driver core: add device_schedule_reprobe()
In reply to Daniel GolleHi Daniel,
Quoted 156 lines · Daniel Golle
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) andbtintel_pcie(synchronousdevice_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 racemodule_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()anddevice_shutdown()or a driver unbind. The drivers do not check any bound state before callingdevice_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()callsdevice_block_probing()before any callback, andreally_probe()honorsdefer_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 dancedevice_release_driver_internal()uses. This closes the check-vs-detach TOCTOU that drivers cannot close themselves, becausedevice_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_doneflag in structdevice_private, set under the device lock oncedevice_shutdown()reaches a device, suppresses the detach half during shutdown. It occupies a spare bit in an existing byte, mirroring howkill_device()sets the dead flag.- The attach half is plain
device_attach(), which already honors both the dead flag anddefer_all_probes: a re-probe landing during system suspend detaches immediately and the probe is deferred untildevice_restore_probing()at resume time. The detach half deliberately does not checkdefer_all_probesso that a re-probe scheduled before suspend is not silently dropped. The parent is re-locked acrossdevice_attach()on buses that setneed_parent_lock, mirroringbus_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.Signed-off-by Daniel Golle <[email protected]>--- drivers/base/base.h | 5 +++ drivers/base/core.c | 3 ++ drivers/base/dd.c | 99 ++++++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 2 + 4 files changed, 109 insertions(+)drivers/base/base.h +5
-0@@ -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@@ -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 +27
-0@@ -1436,3 +1436,102 @@ void driver_detach(const struct device_driver *drv)put_device (dev); } } + +struct device_reprobe { +struct delayed_work work; +struct device *dev; +struct device *parent; +const struct device_driver *drv; +}; + +static void device_reprobe_work_fn (struct work_struct *work) +{ + struct device_reprobe *rp = container_of(work, struct device_reprobe, + work.work); +struct device *dev = rp->dev; +struct device *parent = rp->parent; +bool detached = false; + +__device_driver_lock (dev, parent); +/* +* rp->drv is only ever compared, never dereferenced: the driver it +* points to may have been unregistered and freed by now. +*/ +if (!dev->p->dead && !dev->p->shutdown_done && + dev->driver && dev->driver == rp->drv) { +__device_release_driver (dev, parent); + detached = true; + } +__device_driver_unlock (dev, parent);
drivers/base/dd.c void driver_detach(const struct device_driver *drv) · line 1475
Quoted 10 lines
drivers/base/dd.c +10
-0+ +if (detached) { +/* +* device_attach() must run with the parent locked on buses +* that require it, mirroring bus_rescan_devices_helper(). +*/ +if (parent && dev->bus->need_parent_lock) +device_lock (parent); +if (device_attach (dev) <0 ) +dev_err (dev,"re-probe failed, device left unbound\n" );
Testing suspend/resume with a hci_h5 BT HCI which needs to be re-probed at resume has shown that this may fail with -EPROBE_DEFER when run during resume.
The probe does get successfully retried later and then everything works, but this failure caused the dev_err() to log a spurious error.
So this should be switched to using dev_err_probe(), e.g. squash in this:
drivers/base/dd.c +4 -2
@@ -1451,6 +1451,7 @@ static void device_reprobe_work_fn(struct work_struct *work)struct device *dev = rp->dev;struct device *parent = rp->parent;bool detached = false; +int ret;__device_driver_lock (dev, parent);/*
@@ -1471,8 +1472,9 @@ static void device_reprobe_work_fn(struct work_struct *work)*/ if (parent && dev->bus->need_parent_lock)device_lock (parent); -if (device_attach (dev) <0 ) -dev_err (dev,"re-probe failed, device left unbound\n" ); + ret =device_attach (dev); +if (ret <0 ) +dev_err_probe (dev, ret,"re-probe failed, device left unbound\n" );if (parent && dev->bus->need_parent_lock)device_unlock (parent); }
With that fixed this looks good to me:
Regards,
Hans
Quoted 75 lines
drivers/base/dd.c +62
-0+if (parent && dev->bus->need_parent_lock) +device_unlock (parent); + } + +put_device (dev); +put_device (parent); +kfree (rp); +} + +/** +* device_schedule_reprobe - schedule a deferred detach and re-probe +* @dev: device to detach and re-probe +* @delay_ms: delay in milliseconds before the re-probe runs +* +* Schedule a detach and re-probe of @dev after @delay_ms milliseconds. +* The re-probe is skipped if, by the time the scheduled work runs, the +* device has been removed, the system shutdown sequence has reached the +* device, or @dev is no longer bound to the driver that was bound at +* scheduling time. In particular an administrative unbind is never +* undone by a stale re-probe. +* +* The work function is built-in text, so the bound driver may call this +* from its own code without holding a module reference. If the driver +* module is unloaded before the work runs, driver unregistration unbinds +* @dev first and the scheduled work does nothing. +* +* Multiple pending re-probes for the same device are individually safe; +* a caller that wants at most one pending re-probe must gate scheduling +* itself. +* +* May only be called from process context. +* +* Returns: 0 on success, -EINVAL if @dev is not a registered device +* bound to a driver, -ENOMEM on allocation failure. +*/ +int device_schedule_reprobe (struct device *dev,unsigned int delay_ms) +{ +struct device_reprobe *rp; + +if (!dev->bus || !dev->p || !device_is_registered (dev)) +return -EINVAL ; +if (!dev->driver) +return -EINVAL ; + + rp =kzalloc_obj (*rp); +if (!rp) +return -ENOMEM ; + + rp->dev =get_device (dev); +/* +* Pin the parent too: the work locks it, and an unregister of @dev +* would otherwise drop the last reference before the work runs. +*/ + rp->parent =get_device (dev->parent); + rp->drv =READ_ONCE (dev->driver); +INIT_DELAYED_WORK (&rp->work, device_reprobe_work_fn); +queue_delayed_work (system_dfl_wq, &rp->work, +msecs_to_jiffies (delay_ms)); + +return 0 ; +} +EXPORT_SYMBOL_GPL (device_schedule_reprobe);include/linux/device.h +2
-0@@ -1314,6 +1314,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);
PATCH v2 2/4 wifi: iwlwifi: use device_schedule_reprobe()
In reply to Daniel Golleiwl_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.
--- .../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
@@ -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
PATCH v2 3/4 Bluetooth: hci_h5: use device_schedule_reprobe()
In reply to Daniel Golleh5_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.
--- drivers/bluetooth/hci_h5.c | 43 ++++++-------------------------------- 1 file changed, 6 insertions(+), 37 deletions(-)
drivers/bluetooth/hci_h5.c +6 -37
@@ -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. */
@@ -1047,46 +1047,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
Re: [PATCH v2 3/4] Bluetooth: hci_h5: use device_schedule_reprobe()
Hi,
Quoted 31 lines · 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 callsdevice_reprobe()and then ends withput_device();kfree();module_put(THIS_MODULE); in module text. That finalmodule_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.
Quoted 5 lines
- A re-probe racing the next suspend now detaches immediately while the probe is deferred until the following resume by the
defer_all_probesmachinery, instead of probing mid-suspend.Signed-off-by Daniel Golle <[email protected]>
Thanks, this looks good to me and works as advertised on a device where this code path gets hit:
Regards,
Hans
Quoted 69 lines
--- drivers/bluetooth/hci_h5.c | 43 ++++++-------------------------------- 1 file changed, 6 insertions(+), 37 deletions(-)drivers/bluetooth/hci_h5.c +6
-37@@ -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. */ @@ -1047,46 +1047,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 ; }
PATCH v2 4/4 Bluetooth: btintel_pcie: use device_schedule_reprobe() after reset
In reply to Daniel Gollebtintel_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 coredump worker stays 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() call stays unbalanced on success, .remove() disables again, and the fresh probe re-INIT_WORKs the coredump worker 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 coredump worker, matching the existing FLR failure handling; unlike before, 'data' is still alive in that case. The PLDR path keeps logging only, as before.
--- drivers/bluetooth/btintel_pcie.c | 51 +++++++++++++++++--------------- 1 file changed, 27 insertions(+), 24 deletions(-)
drivers/bluetooth/btintel_pcie.c +27 -24
@@ -2582,7 +2582,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);
@@ -2593,14 +2593,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.
@@ -2622,14 +2624,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)
@@ -2657,11 +2657,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 coredump_work 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 +* coredump_work 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 coredump_work. Only the FLR path can fail with no +* re-probe scheduled, in which case we balance the * disable_work_sync() above so a later successful reset is not * permanently blocked. *
@@ -2941,19 +2945,18 @@ static void btintel_pcie_remove(struct pci_dev *pdev) }/* Permanently block coredump triggers and drain the worker before -* tearing down. Must run before cancel_work_sync(&reset_work) so +* tearing down. Must run before disable_work_sync(&reset_work) so * the disable counter stays >= 1 even after reset_work()'s * balanced enable_work() (counter 2 -> 1, never reaching 0). */ disable_work_sync (&data->coredump_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