From: Jason Gunthorpe <jgg@nvidia.com>
To: Lu Baolu <baolu.lu@linux.intel.com>,
David Woodhouse <dwmw2@infradead.org>,
iommu@lists.linux.dev, Joerg Roedel <joro@8bytes.org>,
Len Brown <lenb@kernel.org>,
linux-acpi@vger.kernel.org,
"Rafael J. Wysocki" <rafael@kernel.org>,
Robin Murphy <robin.murphy@arm.com>,
Will Deacon <will@kernel.org>
Cc: Kevin Tian <kevin.tian@intel.com>, Nicolin Chen <nicolinc@nvidia.com>
Subject: [PATCH v2 04/10] iommu: Simplify the __iommu_group_remove_device() flow
Date: Fri, 19 May 2023 15:42:13 -0300 [thread overview]
Message-ID: <4-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> (raw)
In-Reply-To: <0-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com>
Instead of returning the struct group_device and then later freeing it, do
the entire free under the group->mutex and defer only putting the
iommu_group.
It is safe to remove the sysfs_links and free memory while holding that
mutex.
Move the sanity assert of the group status into
__iommu_group_free_device().
The next patch will improve upon this and consolidate the group put and
the mutex into __iommu_group_remove_device().
__iommu_group_free_device() is close to being the paired undo of
iommu_group_add_device(), following patches will improve on that.
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/iommu.c | 83 ++++++++++++++++++++-----------------------
1 file changed, 39 insertions(+), 44 deletions(-)
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 6177e01ced67ab..a87e2df5ce1238 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -470,32 +470,8 @@ int iommu_probe_device(struct device *dev)
}
-/*
- * Remove a device from a group's device list and return the group device
- * if successful.
- */
-static struct group_device *
-__iommu_group_remove_device(struct iommu_group *group, struct device *dev)
-{
- struct group_device *device;
-
- lockdep_assert_held(&group->mutex);
- for_each_group_device(group, device) {
- if (device->dev == dev) {
- list_del(&device->list);
- return device;
- }
- }
-
- return NULL;
-}
-
-/*
- * Release a device from its group and decrements the iommu group reference
- * count.
- */
-static void __iommu_group_release_device(struct iommu_group *group,
- struct group_device *grp_dev)
+static void __iommu_group_free_device(struct iommu_group *group,
+ struct group_device *grp_dev)
{
struct device *dev = grp_dev->dev;
@@ -504,16 +480,45 @@ static void __iommu_group_release_device(struct iommu_group *group,
trace_remove_device_from_group(group->id, dev);
+ /*
+ * If the group has become empty then ownership must have been
+ * released, and the current domain must be set back to NULL or
+ * the default domain.
+ */
+ if (list_empty(&group->devices))
+ WARN_ON(group->owner_cnt ||
+ group->domain != group->default_domain);
+
kfree(grp_dev->name);
kfree(grp_dev);
dev->iommu_group = NULL;
- iommu_group_put(group);
+}
+
+/*
+ * Remove the iommu_group from the struct device. The attached group must be put
+ * by the caller after releaseing the group->mutex.
+ */
+static void __iommu_group_remove_device(struct device *dev)
+{
+ struct iommu_group *group = dev->iommu_group;
+ struct group_device *device;
+
+ lockdep_assert_held(&group->mutex);
+ for_each_group_device(group, device) {
+ if (device->dev != dev)
+ continue;
+
+ list_del(&device->list);
+ __iommu_group_free_device(group, device);
+ /* Caller must put iommu_group */
+ return;
+ }
+ WARN(true, "Corrupted iommu_group device_list");
}
static void iommu_release_device(struct device *dev)
{
struct iommu_group *group = dev->iommu_group;
- struct group_device *device;
const struct iommu_ops *ops;
if (!dev->iommu || !group)
@@ -522,16 +527,7 @@ static void iommu_release_device(struct device *dev)
iommu_device_unlink(dev->iommu->iommu_dev, dev);
mutex_lock(&group->mutex);
- device = __iommu_group_remove_device(group, dev);
-
- /*
- * If the group has become empty then ownership must have been released,
- * and the current domain must be set back to NULL or the default
- * domain.
- */
- if (list_empty(&group->devices))
- WARN_ON(group->owner_cnt ||
- group->domain != group->default_domain);
+ __iommu_group_remove_device(dev);
/*
* release_device() must stop using any attached domain on the device.
@@ -547,8 +543,8 @@ static void iommu_release_device(struct device *dev)
ops->release_device(dev);
mutex_unlock(&group->mutex);
- if (device)
- __iommu_group_release_device(group, device);
+ /* Pairs with the get in iommu_group_add_device() */
+ iommu_group_put(group);
module_put(ops->owner);
dev_iommu_free(dev);
@@ -1107,7 +1103,6 @@ EXPORT_SYMBOL_GPL(iommu_group_add_device);
void iommu_group_remove_device(struct device *dev)
{
struct iommu_group *group = dev->iommu_group;
- struct group_device *device;
if (!group)
return;
@@ -1115,11 +1110,11 @@ void iommu_group_remove_device(struct device *dev)
dev_info(dev, "Removing from iommu group %d\n", group->id);
mutex_lock(&group->mutex);
- device = __iommu_group_remove_device(group, dev);
+ __iommu_group_remove_device(dev);
mutex_unlock(&group->mutex);
- if (device)
- __iommu_group_release_device(group, device);
+ /* Pairs with the get in iommu_group_add_device() */
+ iommu_group_put(group);
}
EXPORT_SYMBOL_GPL(iommu_group_remove_device);
--
2.40.1
next prev parent reply other threads:[~2023-05-19 18:42 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-19 18:42 [PATCH v2 00/10] Consolidate the probe_device path Jason Gunthorpe
2023-05-19 18:42 ` [PATCH v2 01/10] iommu: Have __iommu_probe_device() check for already probed devices Jason Gunthorpe
2023-05-21 8:17 ` Baolu Lu
2023-05-22 12:18 ` Rafael J. Wysocki
2023-05-19 18:42 ` [PATCH v2 02/10] iommu: Use iommu_group_ref_get/put() for dev->iommu_group Jason Gunthorpe
2023-05-21 8:18 ` Baolu Lu
2023-05-19 18:42 ` [PATCH v2 03/10] iommu: Inline iommu_group_get_for_dev() into __iommu_probe_device() Jason Gunthorpe
2023-05-21 8:19 ` Baolu Lu
2023-06-02 17:17 ` Jason Gunthorpe
2023-05-19 18:42 ` Jason Gunthorpe [this message]
2023-05-21 9:08 ` [PATCH v2 04/10] iommu: Simplify the __iommu_group_remove_device() flow Baolu Lu
2023-05-22 8:35 ` Niklas Schnelle
2023-05-29 19:33 ` Jason Gunthorpe
2023-05-19 18:42 ` [PATCH v2 05/10] iommu: Add iommu_init/deinit_device() paired functions Jason Gunthorpe
2023-05-21 11:09 ` Baolu Lu
2023-05-21 11:31 ` Baolu Lu
2023-05-22 2:31 ` Baolu Lu
2023-06-02 17:20 ` Jason Gunthorpe
2023-05-19 18:42 ` [PATCH v2 06/10] iommu: Move the iommu driver sysfs setup into iommu_init/deinit_device() Jason Gunthorpe
2023-05-22 0:51 ` Baolu Lu
2023-05-19 18:42 ` [PATCH v2 07/10] iommu: Do not export iommu_device_link/unlink() Jason Gunthorpe
2023-05-22 0:52 ` Baolu Lu
2023-05-19 18:42 ` [PATCH v2 08/10] iommu: Always destroy the iommu_group during iommu_release_device() Jason Gunthorpe
2023-05-22 1:43 ` Baolu Lu
2023-05-19 18:42 ` [PATCH v2 09/10] iommu: Split iommu_group_add_device() Jason Gunthorpe
2023-05-22 2:34 ` Baolu Lu
2023-05-25 5:37 ` Tian, Kevin
2023-05-19 18:42 ` [PATCH v2 10/10] iommu: Avoid locking/unlocking for iommu_probe_device() Jason Gunthorpe
2023-05-22 2:39 ` Baolu Lu
2023-05-25 5:37 ` Tian, Kevin
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=4-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com \
--to=jgg@nvidia.com \
--cc=baolu.lu@linux.intel.com \
--cc=dwmw2@infradead.org \
--cc=iommu@lists.linux.dev \
--cc=joro@8bytes.org \
--cc=kevin.tian@intel.com \
--cc=lenb@kernel.org \
--cc=linux-acpi@vger.kernel.org \
--cc=nicolinc@nvidia.com \
--cc=rafael@kernel.org \
--cc=robin.murphy@arm.com \
--cc=will@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