All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yi Liu <yi.l.liu@intel.com>
To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com,
	kevin.tian@intel.com, robin.murphy@arm.com,
	baolu.lu@linux.intel.com
Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com,
	kvm@vger.kernel.org, mjrosato@linux.ibm.com,
	chao.p.peng@linux.intel.com, yi.l.liu@intel.com,
	yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com,
	shameerali.kolothum.thodi@huawei.com, lulu@redhat.com,
	suravee.suthikulpanit@amd.com, iommu@lists.linux.dev,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	zhenzhong.duan@intel.com, joao.m.martins@oracle.com
Subject: [RFC 3/7] iommufd: Add iommufd_device_bind_pasid()
Date: Mon,  9 Oct 2023 01:51:19 -0700	[thread overview]
Message-ID: <20231009085123.463179-4-yi.l.liu@intel.com> (raw)
In-Reply-To: <20231009085123.463179-1-yi.l.liu@intel.com>

From: Kevin Tian <kevin.tian@intel.com>

Intel SIOV allows creating virtual devices of which the vRID is
represented by a pasid of a physical device. So such device can be
bound to an iommufd as physical device does and then later be attached
to an IOAS/hwpt using that pasid.

Binding a virtual device has different security contract comparing
to binding a physical device. There is no DMA ownership claim per
pasid since the parent device is already claimed by the parent driver
including the entire pasid space. With that we simply store the pasid
in the object once it passes other checks.

Signed-off-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
---
 drivers/iommu/iommufd/device.c          | 72 ++++++++++++++++++++++++-
 drivers/iommu/iommufd/iommufd_private.h |  7 +++
 include/linux/iommufd.h                 |  3 ++
 3 files changed, 80 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 9dd76d92b7f6..35c1419ee96b 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -5,6 +5,7 @@
 #include <linux/slab.h>
 #include <linux/iommu.h>
 #include <uapi/linux/iommufd.h>
+#include <linux/msi.h>
 #include "../iommu-priv.h"
 
 #include "io_pagetable.h"
@@ -139,8 +140,10 @@ void iommufd_device_destroy(struct iommufd_object *obj)
 	WARN_ON(!xa_empty(&idev->pasid_hwpts));
 	if (idev->has_user_data)
 		dev_iommu_ops(idev->dev)->unset_dev_user_data(idev->dev);
-	iommu_device_release_dma_owner(idev->dev);
-	iommufd_put_group(idev->igroup);
+	if (idev->igroup) {
+		iommu_device_release_dma_owner(idev->dev);
+		iommufd_put_group(idev->igroup);
+	}
 	if (!iommufd_selftest_is_mock_dev(idev->dev))
 		iommufd_ctx_put(idev->ictx);
 }
@@ -257,6 +260,71 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
 }
 EXPORT_SYMBOL_NS_GPL(iommufd_device_bind, IOMMUFD);
 
+/**
+ * iommufd_device_bind_pasid - Bind a virtual device to an iommu fd
+ * @ictx: iommufd file descriptor
+ * @dev: Pointer to the parent physical device struct
+ * @pasid: the pasid value representing vRID of this virtual device
+ * @id: Output ID number to return to userspace for this device
+ *
+ * The virtual device always tags its DMA with the provided pasid.
+ * A successful bind allows the pasid to be used in other iommufd
+ * operations e.g. attach/detach and returns struct iommufd_device
+ * pointer, otherwise returns error pointer.
+ *
+ * There is no ownership check per pasid. A driver using this API
+ * must already claim the DMA ownership over the parent device and
+ * the pasid is allocated by the driver itself.
+ *
+ * PASID is a device capability so unlike iommufd_device_bind() it
+ * has no iommu group associated.
+ *
+ * The caller must undo this with iommufd_device_unbind()
+ */
+struct iommufd_device *iommufd_device_bind_pasid(struct iommufd_ctx *ictx,
+						 struct device *dev,
+						 u32 pasid, u32 *id)
+{
+	struct iommufd_device *idev;
+	int rc;
+
+	/*
+	 * iommufd always sets IOMMU_CACHE because we offer no way for userspace
+	 * to restore cache coherency.
+	 */
+	if (!device_iommu_capable(dev, IOMMU_CAP_CACHE_COHERENCY))
+		return ERR_PTR(-EINVAL);
+
+	/*
+	 * No iommu supports pasid-granular msi message today. Here we
+	 * just check whether the parent device can do safe interrupts.
+	 * Isolation between virtual devices within the parent device
+	 * relies on the parent driver to enforce.
+	 */
+	if (!iommufd_selftest_is_mock_dev(dev) &&
+	    !msi_device_has_isolated_msi(dev)) {
+		rc = iommufd_allow_unsafe_interrupts(dev);
+		if (rc)
+			return ERR_PTR(rc);
+	}
+
+	idev = iommufd_alloc_device(ictx, dev);
+	if (IS_ERR(idev))
+		return idev;
+	idev->default_pasid = pasid;
+
+	/*
+	 * If the caller fails after this success it must call
+	 * iommufd_unbind_device() which is safe since we hold this refcount.
+	 * This also means the device is a leaf in the graph and no other
+	 * object can take a reference on it.
+	 */
+	iommufd_object_finalize(ictx, &idev->obj);
+	*id = idev->obj.id;
+	return idev;
+}
+EXPORT_SYMBOL_NS_GPL(iommufd_device_bind_pasid, IOMMUFD);
+
 /**
  * iommufd_ctx_has_group - True if any device within the group is bound
  *                         to the ictx
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index 06ebee4c87c5..7b3405fd6fd3 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -332,10 +332,17 @@ struct iommufd_group {
 struct iommufd_device {
 	struct iommufd_object obj;
 	struct iommufd_ctx *ictx;
+	/* valid if this is a physical device */
 	struct iommufd_group *igroup;
 	struct list_head group_item;
 	/* always the physical device */
 	struct device *dev;
+	/*
+	 * valid if this is a virtual device which gains pasid-granular
+	 * DMA isolation in IOMMU. The default pasid is used when attaching
+	 * this device to a IOAS/hwpt.
+	 */
+	u32 default_pasid;
 	struct xarray pasid_hwpts;
 	bool enforce_cache_coherency;
 	bool has_user_data;
diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
index 0b007c376306..402320d6eba1 100644
--- a/include/linux/iommufd.h
+++ b/include/linux/iommufd.h
@@ -20,6 +20,9 @@ struct iommu_group;
 
 struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
 					   struct device *dev, u32 *id);
+struct iommufd_device *iommufd_device_bind_pasid(struct iommufd_ctx *ictx,
+						 struct device *dev,
+						 u32 pasid, u32 *id);
 void iommufd_device_unbind(struct iommufd_device *idev);
 
 int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id);
-- 
2.34.1


  parent reply	other threads:[~2023-10-09  8:52 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-09  8:51 [RFC 0/7] Add SIOV virtual device support Yi Liu
2023-10-09  8:51 ` [RFC 1/7] iommufd: Handle unsafe interrupts in a separate function Yi Liu
2023-10-09  8:51 ` [RFC 2/7] iommufd: Introduce iommufd_alloc_device() Yi Liu
2023-10-09  8:51 ` Yi Liu [this message]
2023-10-10  8:19   ` [RFC 3/7] iommufd: Add iommufd_device_bind_pasid() Tian, Kevin
2023-11-08  7:45     ` Yi Liu
2023-11-08  8:46       ` Tian, Kevin
2023-11-08  9:03         ` Yi Liu
2023-10-09  8:51 ` [RFC 4/7] iommufd: Support attach/replace for SIOV virtual device {dev, pasid} Yi Liu
2023-10-10  8:24   ` Tian, Kevin
2023-11-09  8:21     ` Yi Liu
2023-10-09  8:51 ` [RFC 5/7] iommufd/selftest: Extend IOMMU_TEST_OP_MOCK_DOMAIN to pass in pasid Yi Liu
2023-10-10  8:25   ` Tian, Kevin
2023-10-09  8:51 ` [RFC 6/7] iommufd/selftest: Add test coverage for SIOV virtual device Yi Liu
2023-10-10  8:30   ` Tian, Kevin
2023-11-09  7:48     ` Yi Liu
2023-10-09  8:51 ` [RFC 7/7] vfio: Add vfio_register_pasid_iommu_dev() Yi Liu
2023-10-10  8:33   ` Tian, Kevin
2023-11-09  8:20     ` Yi Liu
2023-11-16  5:35   ` Cao, Yahui
2023-11-17  6:31     ` Yi Liu
     [not found]   ` <99115148-d0e3-4920-aed6-669ae45aa2fe@intel.com>
2023-11-17  6:30     ` Yi Liu
2023-10-09 13:21 ` [RFC 0/7] Add SIOV virtual device support Jason Gunthorpe
2023-10-09 23:33   ` Tian, Kevin
2023-11-22  3:59   ` Cao, Yahui

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=20231009085123.463179-4-yi.l.liu@intel.com \
    --to=yi.l.liu@intel.com \
    --cc=alex.williamson@redhat.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=chao.p.peng@linux.intel.com \
    --cc=cohuck@redhat.com \
    --cc=eric.auger@redhat.com \
    --cc=iommu@lists.linux.dev \
    --cc=jasowang@redhat.com \
    --cc=jgg@nvidia.com \
    --cc=joao.m.martins@oracle.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=lulu@redhat.com \
    --cc=mjrosato@linux.ibm.com \
    --cc=nicolinc@nvidia.com \
    --cc=peterx@redhat.com \
    --cc=robin.murphy@arm.com \
    --cc=shameerali.kolothum.thodi@huawei.com \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=yi.y.sun@linux.intel.com \
    --cc=zhenzhong.duan@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.