From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J . Wysocki" <rafael@kernel.org>,
Danilo Krummrich <dakr@kernel.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
driver-core@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [PATCH v2] driver core: fix race between bus_remove_driver() and dev_driver_uevent()
Date: Sun, 22 Nov 2026 17:26:04 -0800 [thread overview]
Message-ID: <20261123012604.3882115-1-dmitry.torokhov@gmail.com> (raw)
When a device driver is being removed, bus_remove_driver() removes the
driver from the bus klist before detaching it from bound devices. If
dev_driver_uevent() runs concurrently on another CPU, it may acquire the
bus klist lock after the driver has been removed from the list, read the
still-valid driver pointer from the device, and access the driver's name
while bus_remove_driver() proceeds to free the driver structure.
To close this race window, acquire and immediately release the bus
drivers klist lock after all devices have been detached. This ensures
that any in-flight uevent generation that observed the driver pointer
before device detachment completed finishes reading the driver name
before the driver structure is destroyed.
Fixes: 18daa52418e7 ("driver core: fix potential NULL pointer dereference in dev_uevent()")
Assisted-by: LLM
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
v2:
- Updated the comment above dev_driver_uevent() in drivers/base/core.c
to explain the new synchronization with bus_remove_driver(), per
Danilo Krummrich's suggestion.
Extremely unlikely to hit so I would not worry about backporting to
stable.
drivers/base/bus.c | 9 +++++++++
drivers/base/core.c | 9 ++++++---
2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index 396097fdb316..e7616d0ca854 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -849,6 +849,15 @@ void bus_remove_driver(struct device_driver *drv)
driver_remove_file(drv, &driver_attr_uevent);
klist_remove(&drv->p->knode_bus);
driver_detach(drv);
+
+ /*
+ * Ensure that any concurrent dev_driver_uevent() that observed
+ * dev->driver before driver_detach() cleared it, finishes
+ * accessing drv->name.
+ */
+ scoped_guard(spinlock, &sp->klist_drivers.k_lock) {
+ }
+
module_remove_driver(drv);
kobject_put(&drv->p->kobj);
diff --git a/drivers/base/core.c b/drivers/base/core.c
index cbe05197d4a0..cc6a1016c251 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -2705,9 +2705,12 @@ static const char *dev_uevent_name(const struct kobject *kobj)
* risky as driver pointer is transitioning to NULL, so READ_ONCE() should
* be used to make sure we are dealing with the same pointer, and to
* ensure that driver structure is not going to disappear from under us
- * we take bus' drivers klist lock. The assumption that only registered
- * driver can be bound to a device, and to unregister a driver bus code
- * will take the same lock.
+ * we take the bus' drivers klist lock. During driver unregistration,
+ * bus_remove_driver() acquires and releases this lock after detaching
+ * all devices and clearing their driver pointers. This waits for readers
+ * that observed the old pointer to finish accessing the driver name
+ * before the driver can be freed. Readers acquiring the lock afterward
+ * cannot observe the removed driver.
*/
static void dev_driver_uevent(const struct device *dev, struct kobj_uevent_env *env)
{
--
2.56.1.750.gf70dc682cf-goog
reply other threads:[~2026-11-23 1:26 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261123012604.3882115-1-dmitry.torokhov@gmail.com \
--to=dmitry.torokhov@gmail.com \
--cc=dakr@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mhiramat@kernel.org \
--cc=rafael@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
all inboxes | Powered by JetHome®