From: Ulf Hansson <ulf.hansson@oss.qualcomm.com>
To: Danilo Krummrich <dakr@kernel.org>,
Johan Hovold <johan@kernel.org>,
Saravana Kannan <saravanak@kernel.org>,
"Rafael J . Wysocki" <rafael@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
driver-core@lists.linux.dev
Cc: Ulf Hansson <ulf.hansson@oss.qualcomm.com>,
linux-pm@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH v2] driver core: Fix potential use-after-free for devs in deferred_sync list
Date: Thu, 3 Dec 2026 17:51:32 +0100 [thread overview]
Message-ID: <20261203165132.455351-1-ulf.hansson@oss.qualcomm.com> (raw)
In case sync_state has been temporarily paused, a supplier device may be put
on the deferred_sync list to be handled when sync_state is resumed. The
device could be removed before that happens, which would lead to a
use-after-free.
Fix this by holding a reference to the device while it's on deferred_sync
list. Let's drop the reference when it's removed from the list in
device_links_supplier_sync_state_resume() or from
device_links_driver_cleanup().
Now as dev->links.defer_sync is shared between deferred_sync and the
transient lists used to call sync_state, the two cases can't easily be
distinguished when deciding who owns the reference for the device. Let's
therefore add a separate list node, links.sync_list, for the transient
lists, so each node and its reference have a single owner.
Note that, the put_device() calls are done outside device_links_lock to
prevent a potential deadlock, in case it triggers the release path to run
for the device.
Fixes: fc5a251d0fd7 ("driver core: Add sync_state driver/bus callback")
Cc: stable@vger.kernel.org
Signed-off-by: Ulf Hansson <ulf.hansson@oss.qualcomm.com>
---
Changes in v2:
- Move the calls to put_device() outside of device_links_lock. Sashiko.
---
drivers/base/core.c | 44 ++++++++++++++++++++++++++++--------------
include/linux/device.h | 2 ++
2 files changed, 31 insertions(+), 15 deletions(-)
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 44f79f427b6b..1c394714d6b1 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -1219,11 +1219,11 @@ static void __device_links_queue_sync_state(struct device *dev,
*/
dev_set_state_synced(dev);
- if (WARN_ON(!list_empty(&dev->links.defer_sync)))
+ if (WARN_ON(!list_empty(&dev->links.sync_list)))
return;
get_device(dev);
- list_add_tail(&dev->links.defer_sync, list);
+ list_add_tail(&dev->links.sync_list, list);
}
/**
@@ -1241,8 +1241,8 @@ static void device_links_flush_sync_list(struct list_head *list,
{
struct device *dev, *tmp;
- list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
- list_del_init(&dev->links.defer_sync);
+ list_for_each_entry_safe(dev, tmp, list, links.sync_list) {
+ list_del_init(&dev->links.sync_list);
if (dev != dont_lock_dev)
device_lock(dev);
@@ -1265,7 +1265,7 @@ void device_links_supplier_sync_state_pause(void)
void device_links_supplier_sync_state_resume(void)
{
- struct device *dev, *tmp;
+ struct device *dev;
LIST_HEAD(sync_list);
device_links_write_lock();
@@ -1274,16 +1274,20 @@ void device_links_supplier_sync_state_resume(void)
goto out;
}
defer_sync_state_count--;
- if (defer_sync_state_count)
- goto out;
- list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
- /*
- * Delete from deferred_sync list before queuing it to
- * sync_list because defer_sync is used for both lists.
- */
+ /* Stop if sync_state got paused while unlocked. */
+ while (!defer_sync_state_count) {
+ dev = list_first_entry_or_null(&deferred_sync, struct device,
+ links.defer_sync);
+ if (!dev)
+ break;
+
list_del_init(&dev->links.defer_sync);
__device_links_queue_sync_state(dev, &sync_list);
+
+ device_links_write_unlock();
+ put_device(dev);
+ device_links_write_lock();
}
out:
device_links_write_unlock();
@@ -1300,8 +1304,10 @@ late_initcall(sync_state_resume_initcall);
static void __device_links_supplier_defer_sync(struct device *sup)
{
- if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
+ if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup)) {
+ get_device(sup);
list_add_tail(&sup->links.defer_sync, &deferred_sync);
+ }
}
static void device_link_drop_managed(struct device_link *link)
@@ -1565,6 +1571,7 @@ void device_links_no_driver(struct device *dev)
void device_links_driver_cleanup(struct device *dev)
{
struct device_link *link, *ln;
+ bool put = false;
device_links_write_lock();
@@ -1587,10 +1594,16 @@ void device_links_driver_cleanup(struct device *dev)
WRITE_ONCE(link->status, DL_STATE_DORMANT);
}
- list_del_init(&dev->links.defer_sync);
+ if (!list_empty(&dev->links.defer_sync)) {
+ list_del_init(&dev->links.defer_sync);
+ put = true;
+ }
__device_links_no_driver(dev);
device_links_write_unlock();
+
+ if (put)
+ put_device(dev);
}
/**
@@ -1867,7 +1880,7 @@ static int fw_devlink_dev_sync_state(struct device *dev, void *data)
dev_warn(sup, "Timed out. Forcing sync_state()\n");
dev_set_state_synced(sup);
get_device(sup);
- list_add_tail(&sup->links.defer_sync, data);
+ list_add_tail(&sup->links.sync_list, data);
return 0;
}
@@ -3249,6 +3262,7 @@ void device_initialize(struct device *dev)
INIT_LIST_HEAD(&dev->links.consumers);
INIT_LIST_HEAD(&dev->links.suppliers);
INIT_LIST_HEAD(&dev->links.defer_sync);
+ INIT_LIST_HEAD(&dev->links.sync_list);
dev->links.status = DL_DEV_NO_DRIVER;
dev_assign_dma_coherent(dev, dma_default_coherent);
swiotlb_dev_init(dev);
diff --git a/include/linux/device.h b/include/linux/device.h
index 6b082c638d45..ba6879502b60 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -485,12 +485,14 @@ enum device_removable {
* @suppliers: List of links to supplier devices.
* @consumers: List of links to consumer devices.
* @defer_sync: Hook to global list of devices that have deferred sync_state.
+ * @sync_list: Hook to a transient list of devices queued for sync_state.
* @status: Driver status information.
*/
struct dev_links_info {
struct list_head suppliers;
struct list_head consumers;
struct list_head defer_sync;
+ struct list_head sync_list;
enum dl_dev_state status;
};
--
2.43.0
next reply other threads:[~2026-12-03 16:51 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 16:51 Ulf Hansson [this message]
2026-12-03 18:45 ` Rafael J. Wysocki (Intel)
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=20261203165132.455351-1-ulf.hansson@oss.qualcomm.com \
--to=ulf.hansson@oss.qualcomm.com \
--cc=dakr@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=johan@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=rafael@kernel.org \
--cc=saravanak@kernel.org \
--cc=stable@vger.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®