From: Keith Busch <kbusch@meta.com>
To: <linux-pci@vger.kernel.org>, <bhelgaas@google.com>
Cc: <lukas@wunner.de>, Keith Busch <kbusch@kernel.org>,
Jonathan Cameron <Jonathan.Cameron@huawei.com>
Subject: [PATCHv3 1/5] pci: make pci_stop_dev concurrent safe
Date: Tue, 22 Oct 2024 15:48:47 -0700 [thread overview]
Message-ID: <20241022224851.340648-2-kbusch@meta.com> (raw)
In-Reply-To: <20241022224851.340648-1-kbusch@meta.com>
From: Keith Busch <kbusch@kernel.org>
Use the atomic ADDED flag to safely ensure concurrent callers can't
attempt to stop the device multiple times. Callers should currently all
be holding the pci_rescan_remove_lock, so there shouldn't be an existing
race. But that global lock can cause lock dependency issues, so this is
preparing to reduce reliance on that lock by using the existing existing
atomic bit ops.
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Keith Busch <kbusch@kernel.org>
---
drivers/pci/bus.c | 2 +-
drivers/pci/pci.h | 11 +++++++++--
drivers/pci/remove.c | 20 +++++++++-----------
3 files changed, 19 insertions(+), 14 deletions(-)
diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c
index e0a2441be6d32..aec08e81abff7 100644
--- a/drivers/pci/bus.c
+++ b/drivers/pci/bus.c
@@ -358,7 +358,7 @@ void pci_bus_add_device(struct pci_dev *dev)
if (retval < 0 && retval != -EPROBE_DEFER)
pci_warn(dev, "device attach failed (%d)\n", retval);
- pci_dev_assign_added(dev, true);
+ pci_dev_assign_added(dev);
if (dev_of_node(&dev->dev) && pci_is_bridge(dev)) {
retval = of_platform_populate(dev_of_node(&dev->dev), NULL, NULL,
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index d89fdbf04f363..c4cceec006d0d 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -470,9 +470,16 @@ static inline int pci_dev_set_disconnected(struct pci_dev *dev, void *unused)
#define PCI_DPC_RECOVERED 1
#define PCI_DPC_RECOVERING 2
-static inline void pci_dev_assign_added(struct pci_dev *dev, bool added)
+static inline void pci_dev_assign_added(struct pci_dev *dev)
{
- assign_bit(PCI_DEV_ADDED, &dev->priv_flags, added);
+ smp_mb__before_atomic();
+ set_bit(PCI_DEV_ADDED, &dev->priv_flags);
+ smp_mb__after_atomic();
+}
+
+static inline bool pci_dev_test_and_clear_added(struct pci_dev *dev)
+{
+ return test_and_clear_bit(PCI_DEV_ADDED, &dev->priv_flags);
}
static inline bool pci_dev_is_added(const struct pci_dev *dev)
diff --git a/drivers/pci/remove.c b/drivers/pci/remove.c
index e4ce1145aa3ee..e0d4402ec3391 100644
--- a/drivers/pci/remove.c
+++ b/drivers/pci/remove.c
@@ -31,18 +31,16 @@ static int pci_pwrctl_unregister(struct device *dev, void *data)
static void pci_stop_dev(struct pci_dev *dev)
{
- pci_pme_active(dev, false);
-
- if (pci_dev_is_added(dev)) {
- device_for_each_child(dev->dev.parent, dev_of_node(&dev->dev),
- pci_pwrctl_unregister);
- device_release_driver(&dev->dev);
- pci_proc_detach_device(dev);
- pci_remove_sysfs_dev_files(dev);
- of_pci_remove_node(dev);
+ if (!pci_dev_test_and_clear_added(dev))
+ return;
- pci_dev_assign_added(dev, false);
- }
+ pci_pme_active(dev, false);
+ device_for_each_child(dev->dev.parent, dev_of_node(&dev->dev),
+ pci_pwrctl_unregister);
+ device_release_driver(&dev->dev);
+ pci_proc_detach_device(dev);
+ pci_remove_sysfs_dev_files(dev);
+ of_pci_remove_node(dev);
}
static void pci_destroy_dev(struct pci_dev *dev)
--
2.43.5
next prev parent reply other threads:[~2024-10-22 22:49 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-22 22:48 [PATCHv3 0/5] pci cleanup/prep patches Keith Busch
2024-10-22 22:48 ` Keith Busch [this message]
2024-11-07 14:06 ` [PATCHv3 1/5] pci: make pci_stop_dev concurrent safe Lukas Wunner
2024-11-07 15:56 ` Keith Busch
2024-11-11 7:20 ` Lukas Wunner
2024-10-22 22:48 ` [PATCHv3 2/5] pci: make pci_destroy_dev " Keith Busch
2024-10-22 22:48 ` [PATCHv3 3/5] pci: move the walk bus lock to where its needed Keith Busch
2024-10-22 22:48 ` [PATCHv3 4/5] pci: walk bus recursively Keith Busch
2024-11-11 8:21 ` Lukas Wunner
2024-10-22 22:48 ` [PATCHv3 5/5] pci: unexport pci_walk_bus_locked Keith Busch
2024-10-23 21:43 ` Bjorn Helgaas
2024-10-23 22:00 ` Keith Busch
2024-10-23 22:07 ` Bjorn Helgaas
2024-10-23 22:20 ` [PATCHv3 0/5] pci cleanup/prep patches Bjorn Helgaas
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=20241022224851.340648-2-kbusch@meta.com \
--to=kbusch@meta.com \
--cc=Jonathan.Cameron@huawei.com \
--cc=bhelgaas@google.com \
--cc=kbusch@kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=lukas@wunner.de \
/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