From: Lu Baolu <baolu.lu@linux.intel.com>
To: Jason Gunthorpe <jgg@ziepe.ca>, Kevin Tian <kevin.tian@intel.com>,
Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>,
Robin Murphy <robin.murphy@arm.com>,
Jean-Philippe Brucker <jean-philippe@linaro.org>,
Nicolin Chen <nicolinc@nvidia.com>, Yi Liu <yi.l.liu@intel.com>,
Jacob Pan <jacob.jun.pan@linux.intel.com>,
Joel Granados <j.granados@samsung.com>
Cc: iommu@lists.linux.dev, virtualization@lists.linux-foundation.org,
linux-kernel@vger.kernel.org, Lu Baolu <baolu.lu@linux.intel.com>
Subject: [PATCH v8 04/10] iommu: Extend domain attach group with handle support
Date: Tue, 2 Jul 2024 14:34:38 +0800 [thread overview]
Message-ID: <20240702063444.105814-5-baolu.lu@linux.intel.com> (raw)
In-Reply-To: <20240702063444.105814-1-baolu.lu@linux.intel.com>
Unlike the SVA case where each PASID of a device has an SVA domain
attached to it, the I/O page faults are handled by the fault handler
of the SVA domain. The I/O page faults for a user page table might
be handled by the domain attached to RID or the domain attached to
the PASID, depending on whether the PASID table is managed by user
space or kernel. As a result, there is a need for the domain attach
group interfaces to have attach handle support. The attach handle
will be forwarded to the fault handler of the user domain.
Add some variants of the domain attaching group interfaces so that they
could support the attach handle and export them for use in IOMMUFD.
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
---
drivers/iommu/iommu-priv.h | 8 +++
drivers/iommu/iommu.c | 103 +++++++++++++++++++++++++++++++++++++
2 files changed, 111 insertions(+)
diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h
index f1536a5ebb0d..c37801c32f33 100644
--- a/drivers/iommu/iommu-priv.h
+++ b/drivers/iommu/iommu-priv.h
@@ -31,4 +31,12 @@ void iommu_device_unregister_bus(struct iommu_device *iommu,
struct iommu_attach_handle *iommu_attach_handle_get(struct iommu_group *group,
ioasid_t pasid,
unsigned int type);
+int iommu_attach_group_handle(struct iommu_domain *domain,
+ struct iommu_group *group,
+ struct iommu_attach_handle *handle);
+void iommu_detach_group_handle(struct iommu_domain *domain,
+ struct iommu_group *group);
+int iommu_replace_group_handle(struct iommu_group *group,
+ struct iommu_domain *new_domain,
+ struct iommu_attach_handle *handle);
#endif /* __LINUX_IOMMU_PRIV_H */
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 5a7e874abb36..8484285fbaa8 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -3478,3 +3478,106 @@ iommu_attach_handle_get(struct iommu_group *group, ioasid_t pasid, unsigned int
return handle;
}
EXPORT_SYMBOL_NS_GPL(iommu_attach_handle_get, IOMMUFD_INTERNAL);
+
+/**
+ * iommu_attach_group_handle - Attach an IOMMU domain to an IOMMU group
+ * @domain: IOMMU domain to attach
+ * @group: IOMMU group that will be attached
+ * @handle: attach handle
+ *
+ * Returns 0 on success and error code on failure.
+ *
+ * This is a variant of iommu_attach_group(). It allows the caller to provide
+ * an attach handle and use it when the domain is attached. This is currently
+ * used by IOMMUFD to deliver the I/O page faults.
+ */
+int iommu_attach_group_handle(struct iommu_domain *domain,
+ struct iommu_group *group,
+ struct iommu_attach_handle *handle)
+{
+ int ret;
+
+ if (handle)
+ handle->domain = domain;
+
+ mutex_lock(&group->mutex);
+ ret = xa_insert(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL);
+ if (ret)
+ goto err_unlock;
+
+ ret = __iommu_attach_group(domain, group);
+ if (ret)
+ goto err_erase;
+ mutex_unlock(&group->mutex);
+
+ return 0;
+err_erase:
+ xa_erase(&group->pasid_array, IOMMU_NO_PASID);
+err_unlock:
+ mutex_unlock(&group->mutex);
+ return ret;
+}
+EXPORT_SYMBOL_NS_GPL(iommu_attach_group_handle, IOMMUFD_INTERNAL);
+
+/**
+ * iommu_detach_group_handle - Detach an IOMMU domain from an IOMMU group
+ * @domain: IOMMU domain to attach
+ * @group: IOMMU group that will be attached
+ *
+ * Detach the specified IOMMU domain from the specified IOMMU group.
+ * It must be used in conjunction with iommu_attach_group_handle().
+ */
+void iommu_detach_group_handle(struct iommu_domain *domain,
+ struct iommu_group *group)
+{
+ mutex_lock(&group->mutex);
+ __iommu_group_set_core_domain(group);
+ xa_erase(&group->pasid_array, IOMMU_NO_PASID);
+ mutex_unlock(&group->mutex);
+}
+EXPORT_SYMBOL_NS_GPL(iommu_detach_group_handle, IOMMUFD_INTERNAL);
+
+/**
+ * iommu_replace_group_handle - replace the domain that a group is attached to
+ * @group: IOMMU group that will be attached to the new domain
+ * @new_domain: new IOMMU domain to replace with
+ * @handle: attach handle
+ *
+ * This is a variant of iommu_group_replace_domain(). It allows the caller to
+ * provide an attach handle for the new domain and use it when the domain is
+ * attached.
+ */
+int iommu_replace_group_handle(struct iommu_group *group,
+ struct iommu_domain *new_domain,
+ struct iommu_attach_handle *handle)
+{
+ void *curr;
+ int ret;
+
+ if (!new_domain)
+ return -EINVAL;
+
+ mutex_lock(&group->mutex);
+ if (handle) {
+ ret = xa_reserve(&group->pasid_array, IOMMU_NO_PASID, GFP_KERNEL);
+ if (ret)
+ goto err_unlock;
+ }
+
+ ret = __iommu_group_set_domain(group, new_domain);
+ if (ret)
+ goto err_release;
+
+ curr = xa_store(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL);
+ WARN_ON(xa_is_err(curr));
+
+ mutex_unlock(&group->mutex);
+
+ return 0;
+err_release:
+ xa_release(&group->pasid_array, IOMMU_NO_PASID);
+err_unlock:
+ mutex_unlock(&group->mutex);
+ return ret;
+}
+EXPORT_SYMBOL_NS_GPL(iommu_replace_group_handle, IOMMUFD_INTERNAL);
--
2.34.1
next prev parent reply other threads:[~2024-07-02 6:37 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-02 6:34 [PATCH v8 00/10] IOMMUFD: Deliver IO page faults to user space Lu Baolu
2024-07-02 6:34 ` [PATCH v8 01/10] iommu: Introduce domain attachment handle Lu Baolu
2024-07-02 6:34 ` [PATCH v8 02/10] iommu: Remove sva handle list Lu Baolu
2024-07-02 6:34 ` [PATCH v8 03/10] iommu: Add attach handle to struct iopf_group Lu Baolu
2024-07-02 6:34 ` Lu Baolu [this message]
2024-07-02 6:34 ` [PATCH v8 05/10] iommufd: Add fault and response message definitions Lu Baolu
2024-07-02 6:34 ` [PATCH v8 06/10] iommufd: Add iommufd fault object Lu Baolu
2024-07-03 23:06 ` Nicolin Chen
2024-07-04 2:59 ` Baolu Lu
2024-07-04 5:36 ` Nicolin Chen
2024-07-04 6:37 ` Tian, Kevin
2024-07-04 7:32 ` Baolu Lu
2024-07-04 23:18 ` Nicolin Chen
2024-07-05 0:49 ` Tian, Kevin
2024-07-08 16:22 ` Jason Gunthorpe
2024-07-08 16:29 ` Jason Gunthorpe
2024-07-08 18:36 ` Nicolin Chen
2024-07-09 17:00 ` Jason Gunthorpe
2024-07-09 17:33 ` Nicolin Chen
2024-07-12 13:00 ` Jason Gunthorpe
2024-07-02 6:34 ` [PATCH v8 07/10] iommufd: Fault-capable hwpt attach/detach/replace Lu Baolu
2024-10-15 3:19 ` Zhangfei Gao
2024-10-15 12:54 ` Jason Gunthorpe
2024-10-16 1:58 ` Zhangfei Gao
2024-10-16 15:25 ` Jason Gunthorpe
2024-10-17 1:44 ` Zhangfei Gao
2024-10-17 12:05 ` Jason Gunthorpe
2024-10-17 12:35 ` Zhangfei Gao
2024-10-17 12:58 ` Shameerali Kolothum Thodi
2024-10-17 13:08 ` Jason Gunthorpe
2024-10-18 1:58 ` Baolu Lu
2024-10-18 2:45 ` Zhangfei Gao
2024-10-27 14:12 ` Zhangfei Gao
2024-10-27 14:26 ` Baolu Lu
2024-10-28 9:56 ` Zhangfei Gao
2024-10-28 11:17 ` Baolu Lu
2024-10-18 14:33 ` Jason Gunthorpe
2024-10-18 13:53 ` Jason Gunthorpe
2024-10-22 14:30 ` Zhangfei Gao
2024-10-22 14:52 ` Jason Gunthorpe
2024-10-23 10:22 ` Zhangfei Gao
2024-07-02 6:34 ` [PATCH v8 08/10] iommufd: Associate fault object with iommufd_hw_pgtable Lu Baolu
2024-07-02 6:34 ` [PATCH v8 09/10] iommufd/selftest: Add IOPF support for mock device Lu Baolu
2024-07-02 6:34 ` [PATCH v8 10/10] iommufd/selftest: Add coverage for IOPF test Lu Baolu
2024-07-04 14:18 ` [PATCH v8 00/10] IOMMUFD: Deliver IO page faults to user space Will Deacon
2024-07-09 17:23 ` Jason Gunthorpe
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=20240702063444.105814-5-baolu.lu@linux.intel.com \
--to=baolu.lu@linux.intel.com \
--cc=iommu@lists.linux.dev \
--cc=j.granados@samsung.com \
--cc=jacob.jun.pan@linux.intel.com \
--cc=jean-philippe@linaro.org \
--cc=jgg@ziepe.ca \
--cc=joro@8bytes.org \
--cc=kevin.tian@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=nicolinc@nvidia.com \
--cc=robin.murphy@arm.com \
--cc=virtualization@lists.linux-foundation.org \
--cc=will@kernel.org \
--cc=yi.l.liu@intel.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;
as well as URLs for NNTP newsgroup(s).