iommu.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
From: Nicolin Chen <nicolinc@nvidia.com>
To: <jgg@nvidia.com>, <kevin.tian@intel.com>, <joro@8bytes.org>,
	<will@kernel.org>, <robin.murphy@arm.com>,
	<alex.williamson@redhat.com>, <shuah@kernel.org>
Cc: <yi.l.liu@intel.com>, <linux-kernel@vger.kernel.org>,
	<iommu@lists.linux.dev>, <kvm@vger.kernel.org>,
	<linux-kselftest@vger.kernel.org>, <mjrosato@linux.ibm.com>,
	<farman@linux.ibm.com>
Subject: [PATCH v5 2/4] iommufd: Add iommufd_access_replace() API
Date: Thu, 23 Mar 2023 01:33:03 -0700	[thread overview]
Message-ID: <2c3fa7a21373ee10af7cc9f8c370945a08341930.1679559476.git.nicolinc@nvidia.com> (raw)
In-Reply-To: <cover.1679559476.git.nicolinc@nvidia.com>

Extract all common procedure, between the iommufd_access_attach() API and
the new iommufd_access_replace() API, to an iommufd_access_change_pt()
helper.

Add iommufd_access_replace() function for VFIO emulated pathway to use.

Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
 drivers/iommu/iommufd/device.c          | 60 ++++++++++++++++++++-----
 include/linux/iommufd.h                 |  1 +
 tools/testing/selftests/iommu/iommfd*.c |  0
 3 files changed, 50 insertions(+), 11 deletions(-)
 create mode 100644 tools/testing/selftests/iommu/iommfd*.c

diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 012e85fc6d77..3b39b76fbc3b 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -746,39 +746,77 @@ void iommufd_access_detach(struct iommufd_access *access)
 }
 EXPORT_SYMBOL_NS_GPL(iommufd_access_detach, IOMMUFD);
 
-int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id)
+static struct iommufd_ioas *
+iommufd_access_change_pt(struct iommufd_access *access, u32 ioas_id)
 {
 	struct iommufd_ioas *new_ioas;
 	struct iommufd_object *obj;
 	int rc = 0;
 
+	lockdep_assert_held(&access->ioas_lock);
+
+	obj = iommufd_get_object(access->ictx, ioas_id, IOMMUFD_OBJ_IOAS);
+	if (IS_ERR(obj))
+		return (struct iommufd_ioas *)obj;
+	new_ioas = container_of(obj, struct iommufd_ioas, obj);
+
+	rc = iopt_add_access(&new_ioas->iopt, access);
+	if (rc) {
+		iommufd_put_object(obj);
+		return ERR_PTR(rc);
+	}
+	iommufd_ref_to_users(obj);
+	return new_ioas;
+}
+
+int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id)
+{
+	struct iommufd_ioas *new_ioas;
+
 	mutex_lock(&access->ioas_lock);
 	if (access->ioas != NULL && access->ioas->obj.id != ioas_id) {
 		mutex_unlock(&access->ioas_lock);
 		return -EINVAL;
 	}
 
-	obj = iommufd_get_object(access->ictx, ioas_id, IOMMUFD_OBJ_IOAS);
-	if (IS_ERR(obj)) {
+	new_ioas = iommufd_access_change_pt(access, ioas_id);
+	if (IS_ERR(new_ioas)) {
 		mutex_unlock(&access->ioas_lock);
-		return PTR_ERR(obj);
+		return PTR_ERR(new_ioas);
 	}
-	new_ioas = container_of(obj, struct iommufd_ioas, obj);
+	access->ioas = new_ioas;
+	access->ioas_unpin = new_ioas;
+	mutex_unlock(&access->ioas_lock);
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(iommufd_access_attach, IOMMUFD);
 
-	rc = iopt_add_access(&new_ioas->iopt, access);
-	if (rc) {
+int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id)
+{
+	struct iommufd_ioas *new_ioas;
+
+	mutex_lock(&access->ioas_lock);
+	if (!access->ioas) {
 		mutex_unlock(&access->ioas_lock);
-		iommufd_put_object(obj);
-		return rc;
+		return -ENOENT;
+	}
+	if (access->ioas->obj.id == ioas_id) {
+		mutex_unlock(&access->ioas_lock);
+		return 0;
 	}
-	iommufd_ref_to_users(obj);
 
+	new_ioas = iommufd_access_change_pt(access, ioas_id);
+	if (IS_ERR(new_ioas)) {
+		mutex_unlock(&access->ioas_lock);
+		return PTR_ERR(new_ioas);
+	}
+	__iommufd_access_detach(access);
 	access->ioas = new_ioas;
 	access->ioas_unpin = new_ioas;
 	mutex_unlock(&access->ioas_lock);
 	return 0;
 }
-EXPORT_SYMBOL_NS_GPL(iommufd_access_attach, IOMMUFD);
+EXPORT_SYMBOL_NS_GPL(iommufd_access_replace, IOMMUFD);
 
 /**
  * iommufd_access_notify_unmap - Notify users of an iopt to stop using it
diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
index 7c79f9b33246..cd7ae5a625fc 100644
--- a/include/linux/iommufd.h
+++ b/include/linux/iommufd.h
@@ -46,6 +46,7 @@ iommufd_access_create(struct iommufd_ctx *ictx,
 		      const struct iommufd_access_ops *ops, void *data, u32 *id);
 void iommufd_access_destroy(struct iommufd_access *access);
 int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id);
+int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id);
 void iommufd_access_detach(struct iommufd_access *access);
 
 void iommufd_ctx_get(struct iommufd_ctx *ictx);
diff --git a/tools/testing/selftests/iommu/iommfd*.c b/tools/testing/selftests/iommu/iommfd*.c
new file mode 100644
index 000000000000..e69de29bb2d1
-- 
2.40.0


  parent reply	other threads:[~2023-03-23  8:33 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-23  8:33 [PATCH v5 0/4] cover-letter: Add IO page table replacement support Nicolin Chen
2023-03-23  8:33 ` [PATCH v5 1/4] vfio: Do not allow !ops->dma_unmap in vfio_pin/unpin_pages() Nicolin Chen
2023-03-23  8:33 ` Nicolin Chen [this message]
2023-03-24  3:02   ` [PATCH v5 2/4] iommufd: Add iommufd_access_replace() API Tian, Kevin
2023-03-24 17:02     ` Nicolin Chen
2023-03-23  8:33 ` [PATCH v5 3/4] iommufd/selftest: Add IOMMU_TEST_OP_ACCESS_REPLACE_IOAS coverage Nicolin Chen
2023-03-23  8:33 ` [PATCH v5 4/4] vfio: Support IO page table replacement Nicolin Chen
2023-03-24  3:04   ` 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=2c3fa7a21373ee10af7cc9f8c370945a08341930.1679559476.git.nicolinc@nvidia.com \
    --to=nicolinc@nvidia.com \
    --cc=alex.williamson@redhat.com \
    --cc=farman@linux.ibm.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@nvidia.com \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mjrosato@linux.ibm.com \
    --cc=robin.murphy@arm.com \
    --cc=shuah@kernel.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).