From: Lu Baolu <baolu.lu@linux.intel.com>
To: iommu@lists.linux.dev
Cc: Joerg Roedel <joro@8bytes.org>, Jason Gunthorpe <jgg@nvidia.com>,
Christoph Hellwig <hch@infradead.org>,
Kevin Tian <kevin.tian@intel.com>, Will Deacon <will@kernel.org>,
Robin Murphy <robin.murphy@arm.com>,
linux-kernel@vger.kernel.org, Lu Baolu <baolu.lu@linux.intel.com>
Subject: [PATCH 1/4] iommu: Add dev_iommu->ops_rwsem
Date: Mon, 13 Feb 2023 15:49:38 +0800 [thread overview]
Message-ID: <20230213074941.919324-2-baolu.lu@linux.intel.com> (raw)
In-Reply-To: <20230213074941.919324-1-baolu.lu@linux.intel.com>
Add a RW semaphore to make sure that iommu_ops of a device is consistent
in any non-driver-oriented path, such as a store operation on the iommu
group sysfs node.
Add a pair of helpers to freeze and unfreeze the iommu ops of all devices
in an iommu group, and use them in iommu_group_store_type().
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
---
include/linux/iommu.h | 3 +++
drivers/iommu/iommu.c | 53 ++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 55 insertions(+), 1 deletion(-)
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index 3589d1b8f922..a4204e1bfef3 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -402,6 +402,8 @@ struct iommu_fault_param {
* @fwspec: IOMMU fwspec data
* @iommu_dev: IOMMU device this device is linked to
* @priv: IOMMU Driver private data
+ * @ops_rwsem: RW semaphore to synchronize between device release
+ * path and the sysfs interfaces.
* @max_pasids: number of PASIDs this device can consume
* @attach_deferred: the dma domain attachment is deferred
*
@@ -415,6 +417,7 @@ struct dev_iommu {
struct iommu_fwspec *fwspec;
struct iommu_device *iommu_dev;
void *priv;
+ struct rw_semaphore ops_rwsem;
u32 max_pasids;
u32 attach_deferred:1;
};
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 5f1dc9aaba52..4f71dcd2621b 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -267,6 +267,7 @@ static struct dev_iommu *dev_iommu_get(struct device *dev)
return NULL;
mutex_init(¶m->lock);
+ init_rwsem(¶m->ops_rwsem);
dev->iommu = param;
return param;
}
@@ -461,12 +462,19 @@ void iommu_release_device(struct device *dev)
iommu_device_unlink(dev->iommu->iommu_dev, dev);
+ /*
+ * The device's iommu_ops will be released in .release_device
+ * callback. Hold ops_rwsem to avoid use after release.
+ */
+ down_write(&dev->iommu->ops_rwsem);
ops = dev_iommu_ops(dev);
if (ops->release_device)
ops->release_device(dev);
+ module_put(ops->owner);
+ dev->iommu->iommu_dev = NULL;
+ up_write(&dev->iommu->ops_rwsem);
iommu_group_remove_device(dev);
- module_put(ops->owner);
dev_iommu_free(dev);
}
@@ -2911,6 +2919,46 @@ static int iommu_change_dev_def_domain(struct iommu_group *group,
return ret;
}
+static int iommu_group_freeze_dev_ops(struct iommu_group *group)
+{
+ struct group_device *device;
+ struct device *dev;
+
+ mutex_lock(&group->mutex);
+ list_for_each_entry(device, &group->devices, list) {
+ dev = device->dev;
+ down_read(&dev->iommu->ops_rwsem);
+ /* .release_device has been called. */
+ if (!dev->iommu->iommu_dev) {
+ up_read(&dev->iommu->ops_rwsem);
+ goto restore_out;
+ }
+ }
+ mutex_unlock(&group->mutex);
+
+ return 0;
+
+restore_out:
+ list_for_each_entry(device, &group->devices, list) {
+ if (device->dev == dev)
+ break;
+ up_read(&device->dev->iommu->ops_rwsem);
+ }
+ mutex_unlock(&group->mutex);
+
+ return -EINVAL;
+}
+
+static void iommu_group_unfreeze_dev_ops(struct iommu_group *group)
+{
+ struct group_device *device;
+
+ mutex_lock(&group->mutex);
+ list_for_each_entry(device, &group->devices, list)
+ up_read(&device->dev->iommu->ops_rwsem);
+ mutex_unlock(&group->mutex);
+}
+
/*
* Changing the default domain through sysfs requires the users to unbind the
* drivers from the devices in the iommu group, except for a DMA -> DMA-FQ
@@ -2988,6 +3036,8 @@ static ssize_t iommu_group_store_type(struct iommu_group *group,
*/
mutex_unlock(&group->mutex);
+ iommu_group_freeze_dev_ops(group);
+
/* Check if the device in the group still has a driver bound to it */
device_lock(dev);
if (device_is_bound(dev) && !(req_type == IOMMU_DOMAIN_DMA_FQ &&
@@ -3002,6 +3052,7 @@ static ssize_t iommu_group_store_type(struct iommu_group *group,
out:
device_unlock(dev);
+ iommu_group_unfreeze_dev_ops(group);
put_device(dev);
return ret;
--
2.34.1
next prev parent reply other threads:[~2023-02-13 7:58 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-13 7:49 [PATCH 0/4] iommu: Extend changing default domain to normal group Lu Baolu
2023-02-13 7:49 ` Lu Baolu [this message]
2023-02-13 14:16 ` [PATCH 1/4] iommu: Add dev_iommu->ops_rwsem Jason Gunthorpe
2023-02-15 5:34 ` Baolu Lu
2023-02-15 11:24 ` Robin Murphy
2023-02-16 0:40 ` Baolu Lu
2023-02-13 7:49 ` [PATCH 2/4] iommu: Use group ownership to avoid driver attachment Lu Baolu
2023-02-13 14:19 ` Jason Gunthorpe
2023-02-15 5:51 ` Baolu Lu
2023-02-15 6:56 ` Tian, Kevin
2023-02-15 7:28 ` Baolu Lu
2023-02-15 11:09 ` Robin Murphy
2023-02-16 0:42 ` Baolu Lu
2023-02-15 12:56 ` Jason Gunthorpe
2023-02-16 0:36 ` Baolu Lu
2023-02-13 7:49 ` [PATCH 3/4] iommu: Remove unnecessary device_lock() Lu Baolu
2023-02-13 7:49 ` [PATCH 4/4] iommu: Cleanup iommu_change_dev_def_domain() Lu Baolu
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=20230213074941.919324-2-baolu.lu@linux.intel.com \
--to=baolu.lu@linux.intel.com \
--cc=hch@infradead.org \
--cc=iommu@lists.linux.dev \
--cc=jgg@nvidia.com \
--cc=joro@8bytes.org \
--cc=kevin.tian@intel.com \
--cc=linux-kernel@vger.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