virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
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 v5 6/9] iommufd: Fault-capable hwpt attach/detach/replace
Date: Tue, 30 Apr 2024 22:57:07 +0800	[thread overview]
Message-ID: <20240430145710.68112-7-baolu.lu@linux.intel.com> (raw)
In-Reply-To: <20240430145710.68112-1-baolu.lu@linux.intel.com>

Add iopf-capable hw page table attach/detach/replace helpers. The pointer
to iommufd_device is stored in the domain attachment handle, so that it
can be echo'ed back in the iopf_group.

The iopf-capable hw page tables can only be attached to devices that
support the IOMMU_DEV_FEAT_IOPF feature. On the first attachment of an
iopf-capable hw_pagetable to the device, the IOPF feature is enabled on
the device. Similarly, after the last iopf-capable hwpt is detached from
the device, the IOPF feature is disabled on the device.

The current implementation allows a replacement between iopf-capable and
non-iopf-capable hw page tables. This matches the nested translation use
case, where a parent domain is attached by default and can then be
replaced with a nested user domain with iopf support.

Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
---
 drivers/iommu/iommufd/iommufd_private.h |  10 ++
 drivers/iommu/iommufd/device.c          |  15 ++-
 drivers/iommu/iommufd/fault.c           | 118 ++++++++++++++++++++++++
 3 files changed, 140 insertions(+), 3 deletions(-)

diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index acb89bbf9f8e..18c8d7d38dcd 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -293,6 +293,7 @@ int iommufd_check_iova_range(struct io_pagetable *iopt,
 struct iommufd_hw_pagetable {
 	struct iommufd_object obj;
 	struct iommu_domain *domain;
+	struct iommufd_fault *fault;
 };
 
 struct iommufd_hwpt_paging {
@@ -396,6 +397,7 @@ struct iommufd_device {
 	/* always the physical device */
 	struct device *dev;
 	bool enforce_cache_coherency;
+	bool iopf_enabled;
 	/* outstanding faults awaiting response indexed by fault group id */
 	struct xarray faults;
 };
@@ -450,6 +452,14 @@ struct iommufd_fault {
 int iommufd_fault_alloc(struct iommufd_ucmd *ucmd);
 void iommufd_fault_destroy(struct iommufd_object *obj);
 
+int iommufd_fault_domain_attach_dev(struct iommufd_hw_pagetable *hwpt,
+				    struct iommufd_device *idev);
+void iommufd_fault_domain_detach_dev(struct iommufd_hw_pagetable *hwpt,
+				     struct iommufd_device *idev);
+int iommufd_fault_domain_replace_dev(struct iommufd_device *idev,
+				     struct iommufd_hw_pagetable *hwpt,
+				     struct iommufd_hw_pagetable *old);
+
 #ifdef CONFIG_IOMMUFD_TEST
 int iommufd_test(struct iommufd_ucmd *ucmd);
 void iommufd_selftest_destroy(struct iommufd_object *obj);
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 22e22969363a..2bee3f399ec7 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -377,7 +377,10 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
 	 * attachment.
 	 */
 	if (list_empty(&idev->igroup->device_list)) {
-		rc = iommu_attach_group(hwpt->domain, idev->igroup->group);
+		if (hwpt->fault)
+			rc = iommufd_fault_domain_attach_dev(hwpt, idev);
+		else
+			rc = iommu_attach_group(hwpt->domain, idev->igroup->group);
 		if (rc)
 			goto err_unresv;
 		idev->igroup->hwpt = hwpt;
@@ -403,7 +406,10 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev)
 	mutex_lock(&idev->igroup->lock);
 	list_del(&idev->group_item);
 	if (list_empty(&idev->igroup->device_list)) {
-		iommu_detach_group(hwpt->domain, idev->igroup->group);
+		if (hwpt->fault)
+			iommufd_fault_domain_detach_dev(hwpt, idev);
+		else
+			iommu_detach_group(hwpt->domain, idev->igroup->group);
 		idev->igroup->hwpt = NULL;
 	}
 	if (hwpt_is_paging(hwpt))
@@ -498,7 +504,10 @@ iommufd_device_do_replace(struct iommufd_device *idev,
 			goto err_unlock;
 	}
 
-	rc = iommu_group_replace_domain(igroup->group, hwpt->domain);
+	if (old_hwpt->fault || hwpt->fault)
+		rc = iommufd_fault_domain_replace_dev(idev, hwpt, old_hwpt);
+	else
+		rc = iommu_group_replace_domain(igroup->group, hwpt->domain);
 	if (rc)
 		goto err_unresv;
 
diff --git a/drivers/iommu/iommufd/fault.c b/drivers/iommu/iommufd/fault.c
index 13125c0feecb..6357229bf3b4 100644
--- a/drivers/iommu/iommufd/fault.c
+++ b/drivers/iommu/iommufd/fault.c
@@ -15,6 +15,124 @@
 #include "../iommu-priv.h"
 #include "iommufd_private.h"
 
+static int iommufd_fault_iopf_enable(struct iommufd_device *idev)
+{
+	int ret;
+
+	if (idev->iopf_enabled)
+		return 0;
+
+	ret = iommu_dev_enable_feature(idev->dev, IOMMU_DEV_FEAT_IOPF);
+	if (ret)
+		return ret;
+
+	idev->iopf_enabled = true;
+
+	return 0;
+}
+
+static void iommufd_fault_iopf_disable(struct iommufd_device *idev)
+{
+	if (!idev->iopf_enabled)
+		return;
+
+	iommu_dev_disable_feature(idev->dev, IOMMU_DEV_FEAT_IOPF);
+	idev->iopf_enabled = false;
+}
+
+int iommufd_fault_domain_attach_dev(struct iommufd_hw_pagetable *hwpt,
+				    struct iommufd_device *idev)
+{
+	struct iommu_attach_handle *handle;
+	int ret;
+
+	if (!hwpt->fault)
+		return -EINVAL;
+
+	ret = iommufd_fault_iopf_enable(idev);
+	if (ret)
+		return ret;
+
+	ret = iommu_attach_group(hwpt->domain, idev->igroup->group);
+	if (ret) {
+		iommufd_fault_iopf_disable(idev);
+		return ret;
+	}
+
+	handle = iommu_attach_handle_get(idev->igroup->group, IOMMU_NO_PASID, 0);
+	handle->idev = idev;
+
+	return 0;
+}
+
+static void iommufd_auto_response_faults(struct iommufd_hw_pagetable *hwpt,
+					 struct iommufd_device *idev)
+{
+	struct iommufd_fault *fault = hwpt->fault;
+	struct iopf_group *group, *next;
+	unsigned long index;
+
+	if (!fault)
+		return;
+
+	mutex_lock(&fault->mutex);
+	list_for_each_entry_safe(group, next, &fault->deliver, node) {
+		if (group->domain != hwpt->domain ||
+		    group->fault_param->dev != idev->dev)
+			continue;
+		list_del(&group->node);
+		iopf_group_response(group, IOMMU_PAGE_RESP_INVALID);
+		iopf_free_group(group);
+	}
+
+	xa_for_each(&idev->faults, index, group) {
+		if (group->domain != hwpt->domain)
+			continue;
+		xa_erase(&idev->faults, index);
+		iopf_group_response(group, IOMMU_PAGE_RESP_INVALID);
+		iopf_free_group(group);
+	}
+	mutex_unlock(&fault->mutex);
+}
+
+void iommufd_fault_domain_detach_dev(struct iommufd_hw_pagetable *hwpt,
+				     struct iommufd_device *idev)
+{
+	iommu_detach_group(hwpt->domain, idev->igroup->group);
+	iommufd_fault_iopf_disable(idev);
+	iommufd_auto_response_faults(hwpt, idev);
+}
+
+int iommufd_fault_domain_replace_dev(struct iommufd_device *idev,
+				     struct iommufd_hw_pagetable *hwpt,
+				     struct iommufd_hw_pagetable *old)
+{
+	struct iommu_attach_handle *handle;
+	int ret;
+
+	if (hwpt->fault)
+		ret = iommufd_fault_iopf_enable(idev);
+	else
+		iommufd_fault_iopf_disable(idev);
+
+	ret = iommu_group_replace_domain(idev->igroup->group, hwpt->domain);
+	if (ret)
+		goto out_cleanup;
+
+	iommufd_auto_response_faults(old, idev);
+	handle = iommu_attach_handle_get(idev->igroup->group, IOMMU_NO_PASID, 0);
+	handle->idev = idev;
+
+	return 0;
+out_cleanup:
+	if (old->fault)
+		ret = iommufd_fault_iopf_enable(idev);
+	else
+		iommufd_fault_iopf_disable(idev);
+
+	return ret;
+}
+
 void iommufd_fault_destroy(struct iommufd_object *obj)
 {
 	struct iommufd_fault *fault = container_of(obj, struct iommufd_fault, obj);
-- 
2.34.1


  parent reply	other threads:[~2024-04-30 14:59 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-30 14:57 [PATCH v5 0/9] IOMMUFD: Deliver IO page faults to user space Lu Baolu
2024-04-30 14:57 ` [PATCH v5 1/9] iommu: Introduce domain attachment handle Lu Baolu
2024-05-15  7:17   ` Tian, Kevin
2024-05-19 10:07     ` Baolu Lu
2024-04-30 14:57 ` [PATCH v5 2/9] iommu: Replace sva_iommu with iommu_attach_handle Lu Baolu
2024-05-07 23:58   ` Jason Gunthorpe
2024-05-15  7:21   ` Tian, Kevin
2024-05-19 10:14     ` Baolu Lu
2024-05-20  3:18       ` Tian, Kevin
2024-04-30 14:57 ` [PATCH v5 3/9] iommu: Add attachment handle to struct iopf_group Lu Baolu
2024-05-08  0:04   ` Jason Gunthorpe
2024-05-10  3:14     ` Baolu Lu
2024-05-10 13:38       ` Jason Gunthorpe
2024-05-10 14:30         ` Baolu Lu
2024-05-10 16:28           ` Jason Gunthorpe
2024-05-15  7:28             ` Tian, Kevin
2024-05-15  7:31   ` Tian, Kevin
2024-05-19 14:03     ` Baolu Lu
2024-05-20  3:20       ` Tian, Kevin
2024-04-30 14:57 ` [PATCH v5 4/9] iommufd: Add fault and response message definitions Lu Baolu
2024-05-15  7:43   ` Tian, Kevin
2024-05-19 14:37     ` Baolu Lu
2024-05-20  3:24       ` Tian, Kevin
2024-05-20  3:33         ` Baolu Lu
2024-05-20  4:59           ` Tian, Kevin
2024-05-24 14:15             ` Jason Gunthorpe
2024-05-27  1:27               ` Tian, Kevin
2024-04-30 14:57 ` [PATCH v5 5/9] iommufd: Add iommufd fault object Lu Baolu
2024-05-08  0:11   ` Jason Gunthorpe
2024-05-08 10:05     ` Baolu Lu
2024-05-15  7:57       ` Tian, Kevin
2024-05-20  0:41         ` Baolu Lu
2024-05-20  3:26           ` Tian, Kevin
2024-05-20  3:28             ` Baolu Lu
2024-05-08  0:22   ` Jason Gunthorpe
2024-05-10  9:13     ` Baolu Lu
2024-05-15  8:37   ` Tian, Kevin
2024-05-20  1:15     ` Baolu Lu
2024-05-20  1:24     ` Baolu Lu
2024-05-24 14:16       ` Jason Gunthorpe
2024-05-20  1:33     ` Baolu Lu
2024-05-20  3:33       ` Tian, Kevin
2024-05-20  1:38     ` Baolu Lu
2024-04-30 14:57 ` Lu Baolu [this message]
2024-05-08  0:18   ` [PATCH v5 6/9] iommufd: Fault-capable hwpt attach/detach/replace Jason Gunthorpe
2024-05-10  3:20     ` Baolu Lu
2024-05-10 13:39       ` Jason Gunthorpe
2024-05-15  8:43   ` Tian, Kevin
2024-05-20  2:10     ` Baolu Lu
2024-05-20  3:35       ` Tian, Kevin
2024-05-20  3:55         ` Baolu Lu
2024-04-30 14:57 ` [PATCH v5 7/9] iommufd: Associate fault object with iommufd_hw_pgtable Lu Baolu
2024-05-08  0:25   ` Jason Gunthorpe
2024-05-10  3:23     ` Baolu Lu
2024-05-15  8:50   ` Tian, Kevin
2024-05-20  2:18     ` Baolu Lu
2024-05-20  3:39       ` Tian, Kevin
2024-05-20  4:00         ` Baolu Lu
2024-05-24 14:24         ` Jason Gunthorpe
2024-05-27  1:33           ` Tian, Kevin
2024-05-27  3:16             ` Baolu Lu
2024-04-30 14:57 ` [PATCH v5 8/9] iommufd/selftest: Add IOPF support for mock device Lu Baolu
2024-04-30 14:57 ` [PATCH v5 9/9] iommufd/selftest: Add coverage for IOPF test 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=20240430145710.68112-7-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).