driver core: add device_schedule_reprobe()
Subsystems (4)
RFC PATCH 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.
- 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.
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 | 82 ++++++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 2 ++ 4 files changed, 92 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
@@ -4910,6 +4910,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 +82 -0
85 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
RE: device_schedule_reprobe(): core helper and conversions
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=1143724
---Test result---
Test Summary:
CheckPatch PASS 2.79 seconds VerifyFixes PASS 0.07 seconds VerifySignedoff PASS 0.06 seconds GitLint PASS 0.77 seconds SubjectPrefix FAIL 0.25 seconds BuildKernel PASS 27.53 seconds CheckAllWarning PASS 30.70 seconds CheckSparse PASS 30.35 seconds BuildKernel32 PASS 27.17 seconds CheckKernelLLVM SKIP 0.00 seconds TestRunnerSetup PASS 502.65 seconds TestRunner_l2cap-tester PASS 63.14 seconds TestRunner_iso-tester PASS 85.31 seconds TestRunner_bnep-tester PASS 19.27 seconds TestRunner_mgmt-tester FAIL 219.15 seconds TestRunner_rfcomm-tester PASS 25.44 seconds TestRunner_sco-tester PASS 31.90 seconds TestRunner_ioctl-tester PASS 27.55 seconds TestRunner_mesh-tester FAIL 26.88 seconds TestRunner_smp-tester PASS 23.87 seconds TestRunner_userchan-tester PASS 20.01 seconds TestRunner_6lowpan-tester PASS 23.47 seconds IncrementalBuild PASS 30.37 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.252 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.786 seconds Mesh - Send cancel - 2 Timed out 1.986 seconds
https://github.com/bluez/bluetooth-next/pull/567
---
Regards,
Linux Bluetooth
Hi,
drivers/base/dd.c void driver_detach(const struct device_driver *drv) · line 1516
Quoted 2 lines
drivers/base/dd.c +2
-0+queue_delayed_work (system_unbound_wq, &rp->work, +msecs_to_jiffies (delay_ms));
Please note that system_unbound_wq is deprecated. The new unbound workqueue is system_dfl_wq.
Thanks!