From: David Jeffery <djeffery@redhat.com>
To: driver-core@lists.linux.dev,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Danilo Krummrich <dakr@kernel.org>
Cc: linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org,
linux-scsi@vger.kernel.org, Tarun Sahu <tarunsahu@google.com>,
Stuart Hayes <stuart.w.hayes@gmail.com>,
Laurence Oberman <loberman@redhat.com>,
Bjorn Helgaas <helgaas@kernel.org>,
kexec@lists.infradead.org, "Ewan Milne" <emilne@redhat.com>,
"John Meneghini" <jmeneghi@redhat.com>,
"Lombardi, Maurizio" <mlombard@redhat.com>,
"Bart Van Assche" <bvanassche@acm.org>,
"John Garry" <john.g.garry@oracle.com>,
"Jeremy Allison" <jallison@ciq.com>,
"Martin K . Petersen" <martin.petersen@oracle.com>,
"Pasha Tatashin" <tatashin@google.com>,
David Jeffery <djeffery@redhat.com>
Subject: [PATCH 2/9] driver core: prevent device_add() during system shutdown
Date: Wed, 2 Sep 2026 13:07:27 -0400 [thread overview]
Message-ID: <20260902170734.95504-3-djeffery@redhat.com> (raw)
In-Reply-To: <20260902170734.95504-1-djeffery@redhat.com>
From: Tarun Sahu <tarunsahu@google.com>
In Async device shutdown, device_kset->list lock is released to handle
asynchronisation and hold again to get entry from device_kset->list.
Which will leave window when device_add can try to add the device to
device_kset list and temper with ongoing shutdown process. New added
device can be async type or sync type and might also introduce new
dependency which can cause device_shutdown path to deadlock. S is
waiting C to finish but C is never scheduled as it was added recently
from device_add path. And C can only be scheduled when main loops
continue to reach to C which is waiting on S.
So, When a system enters shutdown (SYSTEM_HALT, SYSTEM_POWER_OFF, or
SYSTEM_RESTART), new devices should not be allowed to be added.
Adding system_state check (system_is_shutting_down()) to avoid
device_add incase of these states of the system. And use srcu so the
device shutdown operation can synchronize device_add and ensure any
device_add in progress is finished.
Signed-off-by: Tarun Sahu <tarunsahu@google.com>
Signed-off-by: David Jeffery <djeffery@redhat.com>
Tested-by: Laurence Oberman <loberman@redhat.com>
---
drivers/base/core.c | 30 +++++++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 83263e3fa5d4..bce555dd74f6 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -47,6 +47,22 @@ static bool fw_devlink_drv_reg_done;
static bool fw_devlink_best_effort;
static struct workqueue_struct *device_link_wq;
+/**
+ * system_is_shutting_down - Check if system state is not active.
+ *
+ * When system state is not active and in shutdown state, new devices
+ * should not be allowed to be added.
+ *
+ * If system_state is SYSTEM_HALT || SYSTEM_POWER_OFF || SYSTEM_RESTART
+ * this function will return true.
+ */
+static inline bool system_is_shutting_down(void)
+{
+ return system_state == SYSTEM_HALT ||
+ system_state == SYSTEM_POWER_OFF ||
+ system_state == SYSTEM_RESTART;
+}
+
/**
* __fwnode_link_add - Create a link between two fwnode_handles.
* @con: Consumer end of the link.
@@ -3614,6 +3630,9 @@ static int device_private_init(struct device *dev)
return 0;
}
+
+DEFINE_STATIC_SRCU(device_add_srcu);
+
/**
* device_add - add device to device hierarchy.
* @dev: device.
@@ -3647,13 +3666,20 @@ int device_add(struct device *dev)
struct device *parent;
struct kobject *kobj;
struct class_interface *class_intf;
- int error = -EINVAL;
+ int idx, error = -EINVAL;
struct kobject *glue_dir = NULL;
+ idx = srcu_read_lock(&device_add_srcu);
+
dev = get_device(dev);
if (!dev)
goto done;
+ if (unlikely(system_is_shutting_down())) {
+ error = -ESHUTDOWN;
+ goto done;
+ }
+
if (!dev->p) {
error = device_private_init(dev);
if (error)
@@ -3803,6 +3829,7 @@ int device_add(struct device *dev)
}
done:
put_device(dev);
+ srcu_read_unlock(&device_add_srcu, idx);
return error;
SysEntryError:
if (MAJOR(dev->devt))
@@ -4877,6 +4904,7 @@ void device_shutdown(void)
wait_for_device_probe();
device_block_probing();
+ synchronize_srcu(&device_add_srcu);
cpufreq_suspend();
--
2.55.0
next prev parent reply other threads:[~2026-09-02 17:09 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-02 17:07 [PATCH v21 0/9] shut down devices asynchronously David Jeffery
2026-09-02 17:07 ` [PATCH 1/9] driver core: rely on put_device to free dev->p David Jeffery
2026-09-02 17:16 ` sashiko-bot
2026-09-02 17:07 ` David Jeffery [this message]
2026-09-02 17:15 ` [PATCH 2/9] driver core: prevent device_add() during system shutdown sashiko-bot
2026-09-02 17:07 ` [PATCH 3/9] driver core: warn should device_move try to move a need_parent_lock device David Jeffery
2026-09-02 17:22 ` sashiko-bot
2026-09-02 17:07 ` [PATCH 4/9] driver core: separate function to shutdown one device David Jeffery
2026-09-02 17:13 ` sashiko-bot
2026-09-02 17:07 ` [PATCH 5/9] driver core: do not always lock parent in shutdown David Jeffery
2026-09-02 17:25 ` sashiko-bot
2026-09-02 17:07 ` [PATCH 6/9] driver core: async device shutdown infrastructure David Jeffery
2026-09-02 17:17 ` sashiko-bot
2026-09-02 17:07 ` [PATCH 7/9] PCI: Link a virtual function to its physical function David Jeffery
2026-09-02 17:17 ` sashiko-bot
2026-09-02 17:07 ` [PATCH 8/9] PCI: Enable async shutdown support David Jeffery
2026-09-02 17:24 ` sashiko-bot
2026-09-02 17:07 ` [PATCH 9/9] scsi: " David Jeffery
2026-09-02 17:26 ` sashiko-bot
-- strict thread matches above, loose matches on Subject: below --
2026-08-21 14:24 [PATCH v20 0/9] shut down devices asynchronously David Jeffery
2026-08-21 14:24 ` [PATCH 2/9] driver core: prevent device_add() during system shutdown David Jeffery
2026-08-21 14:43 ` sashiko-bot
2026-08-28 16:01 ` tarunsahu
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=20260902170734.95504-3-djeffery@redhat.com \
--to=djeffery@redhat.com \
--cc=bvanassche@acm.org \
--cc=dakr@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=emilne@redhat.com \
--cc=gregkh@linuxfoundation.org \
--cc=helgaas@kernel.org \
--cc=jallison@ciq.com \
--cc=jmeneghi@redhat.com \
--cc=john.g.garry@oracle.com \
--cc=kexec@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=loberman@redhat.com \
--cc=martin.petersen@oracle.com \
--cc=mlombard@redhat.com \
--cc=rafael@kernel.org \
--cc=stuart.w.hayes@gmail.com \
--cc=tarunsahu@google.com \
--cc=tatashin@google.com \
/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