Skip to contentSkip to conversation

vfio: expose virtual Shared Virtual Addressing to VMs

PATCH In review Liu Yi L 7 participants 37 messages Jun 11, 2020 – Jun 23, 2020

Subsystems (4)
lore
Share & tools
lore

Liu Yi L

author
#0 lore raw

v2 cover vfio: expose virtual Shared Virtual Addressing to VMs

Shared Virtual Addressing (SVA), a.k.a, Shared Virtual Memory (SVM) on Intel platforms allows address space sharing between device DMA and applications. SVA can reduce programming complexity and enhance security.

This VFIO series is intended to expose SVA usage to VMs. i.e. Sharing guest application address space with passthru devices. This is called vSVA in this series. The whole vSVA enabling requires QEMU/VFIO/IOMMU changes. For IOMMU and QEMU changes, they are in separate series (listed in the "Related series").

The high-level architecture for SVA virtualization is as below, the key design of vSVA support is to utilize the dual-stage IOMMU translation ( also known as IOMMU nesting translation) capability in host IOMMU.

    .-------------.  .---------------------------.
    |   vIOMMU    |  | Guest process CR3, FL only|
    |             |  '---------------------------'

.----------------/
| PASID Entry |--- PASID cache flush -
'-------------' |
| | V
| | CR3 in GPA
'-------------'
Guest

------| Shadow |--------------------------|--------
      v        v                          v
Host
    .-------------.  .----------------------.
    |   pIOMMU    |  | Bind FL for GVA-GPA  |
    |             |  '----------------------'
    .----------------/  |
    | PASID Entry |     V (Nested xlate)
    '----------------\.------------------------------.
    |             |   |SL for GPA-HPA, default domain|
    |             |   '------------------------------'
    '-------------'

Where:
- FL = First level/stage one page tables
- SL = Second level/stage two page tables

Patch Overview:
1. a refactor to vfio_iommu_type1 ioctl (patch 0001)
2. reports IOMMU nesting info to userspace ( patch 0002, 0003 and 0015)
3. vfio support for PASID allocation and free for VMs (patch 0004, 0005, 0006)
4. vfio support for binding guest page table to host (patch 0007, 0008, 0009, 0010)
5. vfio support for IOMMU cache invalidation from VMs (patch 0011)
6. vfio support for vSVA usage on IOMMU-backed mdevs (patch 0012)
7. expose PASID capability to VM (patch 0013)
8. add doc for VFIO dual stage control (patch 0014)

The complete vSVA kernel upstream patches are divided into three phases:
1. Common APIs and PCI device direct assignment
2. IOMMU-backed Mediated Device assignment
3. Page Request Services (PRS) support

This patchset is aiming for the phase 1 and phase 2, and based on Jacob's below series.
[PATCH v13 0/8] Nested Shared Virtual Address (SVA) VT-d support - merged
https://lkml.org/lkml/2020/5/13/1582

[PATCH v2 0/3] IOMMU user API enhancement - wip
https://lkml.org/lkml/2020/6/11/5

[PATCH 00/10] IOASID extensions for guest SVA - wip
https://lkml.org/lkml/2020/3/25/874

The latest IOASID code added below new interface for itertate all PASIDs of an ioasid_set. The implementation is not sent out yet as Jacob needs some cleanup, it can be found in branch vsva-linux-5.7-rc4-v2.
int ioasid_set_for_each_ioasid(int sid, void (*fn)(ioasid_t id, void *data), void *data);

Complete set for current vSVA can be found in below branch. This branch also includes some extra modifications to IOASID core code and vt-d iommu driver cleanup patches.
https://github.com/luxis1999/linux-vsva.git:vsva-linux-5.7-rc4-v2

The corresponding QEMU patch series is included in below branch:
https://github.com/luxis1999/qemu.git:vsva_5.7_rc4_qemu_rfcv6

Regards,
Yi Liu

Changelog:
- Patch v1 -> Patch v2:
a) Refactor vfio_iommu_type1_ioctl() per suggestion from Christoph
Hellwig.
b) Re-sequence the patch series for better bisect support.
c) Report IOMMU nesting cap info in detail instead of a format in
v1.
d) Enforce one group per nesting type container for vfio iommu type1
driver.
e) Build the vfio_mm related code from vfio.c to be a separate
vfio_pasid.ko.
f) Add PASID ownership check in IOMMU driver.
g) Adopted to latest IOMMU UAPI design. Removed IOMMU UAPI version
check. Added iommu_gpasid_unbind_data for unbind requests from
userspace.
h) Define a single ioctl:VFIO_IOMMU_NESTING_OP for bind/unbind_gtbl
and cahce_invld.
i) Document dual stage control in vfio.rst.
Patch v1: https://lore.kernel.org/linux-iommu/[email protected]/

- RFC v3 -> Patch v1:
a) Address comments to the PASID request(alloc/free) path
b) Report PASID alloc/free availabitiy to user-space
c) Add a vfio_iommu_type1 parameter to support pasid quota tuning
d) Adjusted to latest ioasid code implementation. e.g. remove the
code for tracking the allocated PASIDs as latest ioasid code
will track it, VFIO could use ioasid_free_set() to free all
PASIDs.
RFC v3: https://lore.kernel.org/linux-iommu/[email protected]/

- RFC v2 -> v3:
a) Refine the whole patchset to fit the roughly parts in this series
b) Adds complete vfio PASID management framework. e.g. pasid alloc,
free, reclaim in VM crash/down and per-VM PASID quota to prevent
PASID abuse.
c) Adds IOMMU uAPI version check and page table format check to ensure
version compatibility and hardware compatibility.
d) Adds vSVA vfio support for IOMMU-backed mdevs.
RFC v2: https://lore.kernel.org/linux-iommu/[email protected]/

- RFC v1 -> v2:
Dropped vfio: VFIO_IOMMU_ATTACH/DETACH_PASID_TABLE.
RFC v1: https://lore.kernel.org/linux-iommu/[email protected]/

Eric Auger (1):
vfio: Document dual stage control

Liu Yi L (13):
vfio/type1: Refactor vfio_iommu_type1_ioctl()
iommu: Report domain nesting info
vfio/type1: Report iommu nesting info to userspace
vfio: Add PASID allocation/free support iommu/vt-d: Support setting ioasid set to domain vfio/type1: Add VFIO_IOMMU_PASID_REQUEST (alloc/free) iommu/uapi: Add iommu_gpasid_unbind_data iommu/vt-d: Check ownership for PASIDs from user-space vfio/type1: Support binding guest page tables to PASID vfio/type1: Allow invalidating first-level/stage IOMMU cache vfio/type1: Add vSVA support for IOMMU-backed mdevs vfio/pci: Expose PCIe PASID capability to guest iommu/vt-d: Support reporting nesting capability info

Yi Sun (1):
iommu: Pass domain and unbind_data to sva_unbind_gpasid()

 Documentation/driver-api/vfio.rst  |  64 ++++
 drivers/iommu/intel-iommu.c        | 107 ++++++-
 drivers/iommu/intel-svm.c          |  20 +-
 drivers/iommu/iommu.c              |   4 +-
 drivers/vfio/Kconfig               |   6 +
 drivers/vfio/Makefile              |   1 +
 drivers/vfio/pci/vfio_pci_config.c |   2 +-
 drivers/vfio/vfio_iommu_type1.c    | 614 ++++++++++++++++++++++++++++++++-----
 drivers/vfio/vfio_pasid.c          | 191 ++++++++++++
 include/linux/intel-iommu.h        |  23 +-
 include/linux/iommu.h              |  10 +-
 include/linux/vfio.h               |  54 ++++
 include/uapi/linux/iommu.h         |  47 +++
 include/uapi/linux/vfio.h          |  78 +++++
 14 files changed, 1134 insertions(+), 87 deletions(-)
 create mode 100644 drivers/vfio/vfio_pasid.c
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v2 1/15 vfio/type1: Refactor vfio_iommu_type1_ioctl()

This patch refactors the vfio_iommu_type1_ioctl() to use switch instead of if-else, and each cmd got a helper function.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>
---
 drivers/vfio/vfio_iommu_type1.c | 183 +++++++++++++++++++++++-----------------
 1 file changed, 105 insertions(+), 78 deletions(-)
drivers/vfio/vfio_iommu_type1.c +105 -78
index cc1d647..402aad3 100644
@@ -2106,6 +2106,23 @@ static int vfio_domains_have_iommu_cache(struct vfio_iommu *iommu)
 	return ret;
 }
 
+static int vfio_iommu_type1_check_extension(struct vfio_iommu *iommu,
+					    unsigned long arg)
+{
+	switch (arg) {
+	case VFIO_TYPE1_IOMMU:
+	case VFIO_TYPE1v2_IOMMU:
+	case VFIO_TYPE1_NESTING_IOMMU:
+		return 1;
+	case VFIO_DMA_CC_IOMMU:
+		if (!iommu)
+			return 0;
+		return vfio_domains_have_iommu_cache(iommu);
+	default:
+		return 0;
+	}
+}
+
 static int vfio_iommu_iova_add_cap(struct vfio_info_cap *caps,
 		 struct vfio_iommu_type1_info_cap_iova_range *cap_iovas,
 		 size_t size)
@@ -2173,110 +2190,120 @@ static int vfio_iommu_iova_build_caps(struct vfio_iommu *iommu,
 	return ret;
 }
 
-static long vfio_iommu_type1_ioctl(void *iommu_data,
-				   unsigned int cmd, unsigned long arg)
+static int vfio_iommu_type1_get_info(struct vfio_iommu *iommu,
+				     unsigned long arg)
 {
-	struct vfio_iommu *iommu = iommu_data;
+	struct vfio_iommu_type1_info info;
 	unsigned long minsz;
+	struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
+	unsigned long capsz;
+	int ret;
 
-	if (cmd == VFIO_CHECK_EXTENSION) {
-		switch (arg) {
-		case VFIO_TYPE1_IOMMU:
-		case VFIO_TYPE1v2_IOMMU:
-		case VFIO_TYPE1_NESTING_IOMMU:
-			return 1;
-		case VFIO_DMA_CC_IOMMU:
-			if (!iommu)
-				return 0;
-			return vfio_domains_have_iommu_cache(iommu);
-		default:
-			return 0;
-		}
-	} else if (cmd == VFIO_IOMMU_GET_INFO) {
-		struct vfio_iommu_type1_info info;
-		struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
-		unsigned long capsz;
-		int ret;
-
-		minsz = offsetofend(struct vfio_iommu_type1_info, iova_pgsizes);
+	minsz = offsetofend(struct vfio_iommu_type1_info, iova_pgsizes);
 
-		/* For backward compatibility, cannot require this */
-		capsz = offsetofend(struct vfio_iommu_type1_info, cap_offset);
+	/* For backward compatibility, cannot require this */
+	capsz = offsetofend(struct vfio_iommu_type1_info, cap_offset);
 
-		if (copy_from_user(&info, (void __user *)arg, minsz))
-			return -EFAULT;
+	if (copy_from_user(&info, (void __user *)arg, minsz))
+		return -EFAULT;
 
-		if (info.argsz < minsz)
-			return -EINVAL;
+	if (info.argsz < minsz)
+		return -EINVAL;
 
-		if (info.argsz >= capsz) {
-			minsz = capsz;
-			info.cap_offset = 0; /* output, no-recopy necessary */
-		}
+	if (info.argsz >= capsz) {
+		minsz = capsz;
+		info.cap_offset = 0; /* output, no-recopy necessary */
+	}
 
-		info.flags = VFIO_IOMMU_INFO_PGSIZES;
+	info.flags = VFIO_IOMMU_INFO_PGSIZES;
 
-		info.iova_pgsizes = vfio_pgsize_bitmap(iommu);
+	info.iova_pgsizes = vfio_pgsize_bitmap(iommu);
 
-		ret = vfio_iommu_iova_build_caps(iommu, &caps);
-		if (ret)
-			return ret;
+	ret = vfio_iommu_iova_build_caps(iommu, &caps);
+	if (ret)
+		return ret;
 
-		if (caps.size) {
-			info.flags |= VFIO_IOMMU_INFO_CAPS;
+	if (caps.size) {
+		info.flags |= VFIO_IOMMU_INFO_CAPS;
 
-			if (info.argsz < sizeof(info) + caps.size) {
-				info.argsz = sizeof(info) + caps.size;
-			} else {
-				vfio_info_cap_shift(&caps, sizeof(info));
-				if (copy_to_user((void __user *)arg +
-						sizeof(info), caps.buf,
-						caps.size)) {
-					kfree(caps.buf);
-					return -EFAULT;
-				}
-				info.cap_offset = sizeof(info);
+		if (info.argsz < sizeof(info) + caps.size) {
+			info.argsz = sizeof(info) + caps.size;
+		} else {
+			vfio_info_cap_shift(&caps, sizeof(info));
+			if (copy_to_user((void __user *)arg +
+					sizeof(info), caps.buf,
+					caps.size)) {
+				kfree(caps.buf);
+				return -EFAULT;
 			}
-
-			kfree(caps.buf);
+			info.cap_offset = sizeof(info);
 		}
 
-		return copy_to_user((void __user *)arg, &info, minsz) ?
-			-EFAULT : 0;
+		kfree(caps.buf);
+	}
 
-	} else if (cmd == VFIO_IOMMU_MAP_DMA) {
-		struct vfio_iommu_type1_dma_map map;
-		uint32_t mask = VFIO_DMA_MAP_FLAG_READ |
-				VFIO_DMA_MAP_FLAG_WRITE;
+	return copy_to_user((void __user *)arg, &info, minsz) ?
+		-EFAULT : 0;
 
-		minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
+}
 
-		if (copy_from_user(&map, (void __user *)arg, minsz))
-			return -EFAULT;
+static int vfio_iommu_type1_map_dma(struct vfio_iommu *iommu,
+				    unsigned long arg)
+{
+	struct vfio_iommu_type1_dma_map map;
+	unsigned long minsz;
+	uint32_t mask = VFIO_DMA_MAP_FLAG_READ |
+			VFIO_DMA_MAP_FLAG_WRITE;
 
-		if (map.argsz < minsz || map.flags & ~mask)
-			return -EINVAL;
+	minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
 
-		return vfio_dma_do_map(iommu, &map);
+	if (copy_from_user(&map, (void __user *)arg, minsz))
+		return -EFAULT;
 
-	} else if (cmd == VFIO_IOMMU_UNMAP_DMA) {
-		struct vfio_iommu_type1_dma_unmap unmap;
-		long ret;
+	if (map.argsz < minsz || map.flags & ~mask)
+		return -EINVAL;
 
-		minsz = offsetofend(struct vfio_iommu_type1_dma_unmap, size);
+	return vfio_dma_do_map(iommu, &map);
+}
 
-		if (copy_from_user(&unmap, (void __user *)arg, minsz))
-			return -EFAULT;
+static int vfio_iommu_type1_unmap_dma(struct vfio_iommu *iommu,
+				    unsigned long arg)
+{
+	struct vfio_iommu_type1_dma_unmap unmap;
+	unsigned long minsz;
+	long ret;
 
-		if (unmap.argsz < minsz || unmap.flags)
-			return -EINVAL;
+	minsz = offsetofend(struct vfio_iommu_type1_dma_unmap, size);
 
-		ret = vfio_dma_do_unmap(iommu, &unmap);
-		if (ret)
-			return ret;
+	if (copy_from_user(&unmap, (void __user *)arg, minsz))
+		return -EFAULT;
+
+	if (unmap.argsz < minsz || unmap.flags)
+		return -EINVAL;
+
+	ret = vfio_dma_do_unmap(iommu, &unmap);
+	if (ret)
+		return ret;
+
+	return copy_to_user((void __user *)arg, &unmap, minsz) ?
+		-EFAULT : 0;
+
+}
+
+static long vfio_iommu_type1_ioctl(void *iommu_data,
+				   unsigned int cmd, unsigned long arg)
+{
+	struct vfio_iommu *iommu = iommu_data;
 
-		return copy_to_user((void __user *)arg, &unmap, minsz) ?
-			-EFAULT : 0;
+	switch (cmd) {
+	case VFIO_CHECK_EXTENSION:
+		return vfio_iommu_type1_check_extension(iommu, arg);
+	case VFIO_IOMMU_GET_INFO:
+		return vfio_iommu_type1_get_info(iommu, arg);
+	case VFIO_IOMMU_MAP_DMA:
+		return vfio_iommu_type1_map_dma(iommu, arg);
+	case VFIO_IOMMU_UNMAP_DMA:
+		return vfio_iommu_type1_unmap_dma(iommu, arg);
 	}
 
 	return -ENOTTY;
Signature · 1 line
2.7.4
author
#2 lore raw

PATCH v2 2/15 iommu: Report domain nesting info

In reply to Liu Yi L

IOMMUs that support nesting translation needs report the capability info to userspace, e.g. the format of first level/stage paging structures.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>

---
@Jean, Eric: as nesting was introduced for ARM, but looks like no actual user of it. right? So I'm wondering if we can reuse DOMAIN_ATTR_NESTING to retrieve nesting info? how about your opinions?

 include/linux/iommu.h      |  1 +
 include/uapi/linux/iommu.h | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
include/linux/iommu.h +1 -0
index 78a26ae..f6e4b49 100644
@@ -126,6 +126,7 @@ enum iommu_attr {
 	DOMAIN_ATTR_FSL_PAMUV1,
 	DOMAIN_ATTR_NESTING,	/* two stages of translation */
 	DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
+	DOMAIN_ATTR_NESTING_INFO,
 	DOMAIN_ATTR_MAX,
 };
 
include/uapi/linux/iommu.h +34 -0
index 303f148..02eac73 100644
@@ -332,4 +332,38 @@ struct iommu_gpasid_bind_data {
 	};
 };
 
+struct iommu_nesting_info {
+	__u32	size;
+	__u32	format;
+	__u32	features;
+#define IOMMU_NESTING_FEAT_SYSWIDE_PASID	(1 << 0)
+#define IOMMU_NESTING_FEAT_BIND_PGTBL		(1 << 1)
+#define IOMMU_NESTING_FEAT_CACHE_INVLD		(1 << 2)
+	__u32	flags;
+	__u8	data[];
+};
+
+/*
+ * @flags:	VT-d specific flags. Currently reserved for future
+ *		extension.
+ * @addr_width:	The output addr width of first level/stage translation
+ * @pasid_bits:	Maximum supported PASID bits, 0 represents no PASID
+ *		support.
+ * @cap_reg:	Describe basic capabilities as defined in VT-d capability
+ *		register.
+ * @cap_mask:	Mark valid capability bits in @cap_reg.
+ * @ecap_reg:	Describe the extended capabilities as defined in VT-d
+ *		extended capability register.
+ * @ecap_mask:	Mark the valid capability bits in @ecap_reg.
+ */
+struct iommu_nesting_info_vtd {
+	__u32	flags;
+	__u16	addr_width;
+	__u16	pasid_bits;
+	__u64	cap_reg;
+	__u64	cap_mask;
+	__u64	ecap_reg;
+	__u64	ecap_mask;
+};
+
 #endif /* _UAPI_IOMMU_H */
Signature · 1 line
2.7.4
#3 lore raw

Re: [PATCH v2 02/15] iommu: Report domain nesting info

Quoted 54 lines · On Thu, 11 Jun 2020 05:15:21 -0700 Liu Yi L <[email protected]> wrote:

IOMMUs that support nesting translation needs report the capability info to userspace, e.g. the format of first level/stage paging structures.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>

---
@Jean, Eric: as nesting was introduced for ARM, but looks like no actual user of it. right? So I'm wondering if we can reuse DOMAIN_ATTR_NESTING to retrieve nesting info? how about your opinions?

 include/linux/iommu.h      |  1 +
 include/uapi/linux/iommu.h | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
include/linux/iommu.h +1 -0
index 78a26ae..f6e4b49 100644
@@ -126,6 +126,7 @@ enum iommu_attr {
 	DOMAIN_ATTR_FSL_PAMUV1,
 	DOMAIN_ATTR_NESTING,	/* two stages of translation */
 	DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
+	DOMAIN_ATTR_NESTING_INFO,
 	DOMAIN_ATTR_MAX,
 };
 
include/uapi/linux/iommu.h +13 -0
index 303f148..02eac73 100644
@@ -332,4 +332,38 @@ struct iommu_gpasid_bind_data {
 	};
 };
 
+struct iommu_nesting_info {
+	__u32	size;
+	__u32	format;
+	__u32	features;
+#define IOMMU_NESTING_FEAT_SYSWIDE_PASID	(1 << 0)
+#define IOMMU_NESTING_FEAT_BIND_PGTBL		(1 << 1)
+#define IOMMU_NESTING_FEAT_CACHE_INVLD		(1 << 2)
+	__u32	flags;
+	__u8	data[];
+};
+
+/*
+ * @flags:	VT-d specific flags. Currently reserved for future

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 357

Quoted 10 lines
include/uapi/linux/iommu.h +10 -0
+ *		extension.
+ * @addr_width:	The output addr width of first level/stage translation
+ * @pasid_bits:	Maximum supported PASID bits, 0 represents no PASID
+ *		support.
+ * @cap_reg:	Describe basic capabilities as defined in VT-d capability
+ *		register.
+ * @cap_mask:	Mark valid capability bits in @cap_reg.
+ * @ecap_reg:	Describe the extended capabilities as defined in VT-d
+ *		extended capability register.
+ * @ecap_mask:	Mark the valid capability bits in @ecap_reg.

Please explain this a little further, why do we need to tell userspace about cap/ecap register bits that aren't valid through this interface? Thanks,

Alex

Quoted 12 lines
include/uapi/linux/iommu.h +11 -0
+ */
+struct iommu_nesting_info_vtd {
+	__u32	flags;
+	__u16	addr_width;
+	__u16	pasid_bits;
+	__u64	cap_reg;
+	__u64	cap_mask;
+	__u64	ecap_reg;
+	__u64	ecap_mask;
+};
+
 #endif /* _UAPI_IOMMU_H */
author
#4 lore raw

RE: [PATCH v2 02/15] iommu: Report domain nesting info

Hi Alex,

Quoted 66 lines

From: Alex Williamson <[email protected]>
Sent: Friday, June 12, 2020 3:30 AM

Quoted 60 lines · On Thu, 11 Jun 2020 05:15:21 -0700 Liu Yi L <[email protected]> wrote:

IOMMUs that support nesting translation needs report the capability info to userspace, e.g. the format of first level/stage paging structures.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>

---
@Jean, Eric: as nesting was introduced for ARM, but looks like no actual user of it. right? So I'm wondering if we can reuse DOMAIN_ATTR_NESTING to retrieve nesting info? how about your opinions?

 include/linux/iommu.h      |  1 +
 include/uapi/linux/iommu.h | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
include/linux/iommu.h index renamed from include/linux/iommu.h +0 -0

78a26ae..f6e4b49 100644

include/linux/iommu.h +1 -0
@@ -126,6 +126,7 @@ enum iommu_attr {
 	DOMAIN_ATTR_FSL_PAMUV1,
 	DOMAIN_ATTR_NESTING,	/* two stages of translation */
 	DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
+	DOMAIN_ATTR_NESTING_INFO,
 	DOMAIN_ATTR_MAX,
 };
 
include/uapi/linux/iommu.h +19 -0
index 303f148..02eac73 100644
@@ -332,4 +332,38 @@ struct iommu_gpasid_bind_data {
 	};
 };
 
+struct iommu_nesting_info {
+	__u32	size;
+	__u32	format;
+	__u32	features;
+#define IOMMU_NESTING_FEAT_SYSWIDE_PASID	(1 << 0)
+#define IOMMU_NESTING_FEAT_BIND_PGTBL		(1 << 1)
+#define IOMMU_NESTING_FEAT_CACHE_INVLD		(1 << 2)
+	__u32	flags;
+	__u8	data[];
+};
+
+/*
+ * @flags:	VT-d specific flags. Currently reserved for future
+ *		extension.
+ * @addr_width:	The output addr width of first level/stage translation
+ * @pasid_bits:	Maximum supported PASID bits, 0 represents no PASID
+ *		support.
+ * @cap_reg:	Describe basic capabilities as defined in VT-d capability
+ *		register.
Quoted 8 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 357

Quoted 4 lines
include/uapi/linux/iommu.h +4 -0
+ * @cap_mask:	Mark valid capability bits in @cap_reg.
+ * @ecap_reg:	Describe the extended capabilities as defined in VT-d
+ *		extended capability register.
+ * @ecap_mask:	Mark the valid capability bits in @ecap_reg.

Please explain this a little further, why do we need to tell userspace about cap/ecap register bits that aren't valid through this interface? Thanks,

we only want to tell userspace about the bits marked in the cap/ecap_mask. cap/ecap_mask is kind of white-list of the cap/ecap register. userspace should only care about the bits in the white-list, for other bits, it should ignore.

Regards,
Yi Liu

Quoted 15 lines

Alex

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 369

Quoted 12 lines
include/uapi/linux/iommu.h +11 -0
+ */
+struct iommu_nesting_info_vtd {
+	__u32	flags;
+	__u16	addr_width;
+	__u16	pasid_bits;
+	__u64	cap_reg;
+	__u64	cap_mask;
+	__u64	ecap_reg;
+	__u64	ecap_mask;
+};
+
 #endif /* _UAPI_IOMMU_H */

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#5 lore raw

RE: [PATCH v2 02/15] iommu: Report domain nesting info

Quoted 84 lines

From: Liu, Yi L <[email protected]>
Sent: Friday, June 12, 2020 5:05 PM

Hi Alex,

Quoted 22 lines

From: Alex Williamson <[email protected]>
Sent: Friday, June 12, 2020 3:30 AM

Quoted 16 lines · On Thu, 11 Jun 2020 05:15:21 -0700 Liu Yi L <[email protected]> wrote:

IOMMUs that support nesting translation needs report the capability info to userspace, e.g. the format of first level/stage paging structures.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>

---
@Jean, Eric: as nesting was introduced for ARM, but looks like no actual user of it. right? So I'm wondering if we can reuse DOMAIN_ATTR_NESTING to retrieve nesting info? how about your

opinions?

Quoted 3 lines
Quoted 3 lines
 include/linux/iommu.h      |  1 +
 include/uapi/linux/iommu.h | 34

++++++++++++++++++++++++++++++++++

Quoted 22 lines
Quoted 22 lines
 2 files changed, 35 insertions(+)
include/linux/iommu.h index renamed from include/linux/iommu.h +0 -0

78a26ae..f6e4b49 100644

include/linux/iommu.h +1 -0
@@ -126,6 +126,7 @@ enum iommu_attr {
 	DOMAIN_ATTR_FSL_PAMUV1,
 	DOMAIN_ATTR_NESTING,	/* two stages of translation */
 	DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
+	DOMAIN_ATTR_NESTING_INFO,
 	DOMAIN_ATTR_MAX,
 };
 
include/uapi/linux/iommu.h +0 -0
index 303f148..02eac73 100644
@@ -332,4 +332,38 @@ struct iommu_gpasid_bind_data {
 	};
 };
 
Quoted 16 lines
Quoted 6 lines
include/uapi/linux/iommu.h +6 -0
+struct iommu_nesting_info {
+	__u32	size;
+	__u32	format;
+	__u32	features;
+#define IOMMU_NESTING_FEAT_SYSWIDE_PASID	(1 << 0)
+#define IOMMU_NESTING_FEAT_BIND_PGTBL		(1 << 1)

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data {

Quoted 10 lines
include/uapi/linux/iommu.h +10 -0
+#define IOMMU_NESTING_FEAT_CACHE_INVLD		(1 << 2)
+	__u32	flags;
+	__u8	data[];
+};
+
+/*
+ * @flags:	VT-d specific flags. Currently reserved for future
+ *		extension.
+ * @addr_width:	The output addr width of first level/stage translation
+ * @pasid_bits:	Maximum supported PASID bits, 0 represents no

PASID

Quoted 2 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 352

Quoted 2 lines
include/uapi/linux/iommu.h +2 -0
+ *		support.
+ * @cap_reg:	Describe basic capabilities as defined in VT-d

capability

Quoted 9 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 357

Quoted 5 lines
include/uapi/linux/iommu.h +5 -0
+ *		register.
+ * @cap_mask:	Mark valid capability bits in @cap_reg.
+ * @ecap_reg:	Describe the extended capabilities as defined in VT-d
+ *		extended capability register.
+ * @ecap_mask:	Mark the valid capability bits in @ecap_reg.

Please explain this a little further, why do we need to tell userspace about cap/ecap register bits that aren't valid through this interface? Thanks,

Quoted 7 lines

we only want to tell userspace about the bits marked in the cap/ecap_mask. cap/ecap_mask is kind of white-list of the cap/ecap register. userspace should
only care about the bits in the white-list, for other bits, it should ignore.

Regards,
Yi Liu

For invalid bits if kernel just clears them then do we still need additional mask bits to explicitly mark them out? I guess this might be the point that Alex asked...

Quoted 4 lines
Quoted 4 lines

Alex

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
include/uapi/linux/iommu.h +4 -0
+ */
+struct iommu_nesting_info_vtd {
+	__u32	flags;
+	__u16	addr_width;
Quoted 8 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 369

Quoted 8 lines
include/uapi/linux/iommu.h +7 -0
+	__u16	pasid_bits;
+	__u64	cap_reg;
+	__u64	cap_mask;
+	__u64	ecap_reg;
+	__u64	ecap_mask;
+};
+
 #endif /* _UAPI_IOMMU_H */

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

author
#6 lore raw

RE: [PATCH v2 02/15] iommu: Report domain nesting info

Hi Kevin,

Quoted 94 lines

From: Tian, Kevin <[email protected]>
Sent: Monday, June 15, 2020 9:23 AM

Quoted 13 lines

From: Liu, Yi L <[email protected]>
Sent: Friday, June 12, 2020 5:05 PM

Hi Alex,

Quoted 8 lines

From: Alex Williamson <[email protected]>
Sent: Friday, June 12, 2020 3:30 AM

Quoted 2 lines · On Thu, 11 Jun 2020 05:15:21 -0700 Liu Yi L <[email protected]> wrote:

IOMMUs that support nesting translation needs report the capability info to userspace, e.g. the format of first level/stage paging

structures.

Quoted 41 lines
Quoted 14 lines
Quoted 10 lines
Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>
Quoted 4 lines

---
@Jean, Eric: as nesting was introduced for ARM, but looks like no actual user of it. right? So I'm wondering if we can reuse DOMAIN_ATTR_NESTING to retrieve nesting info? how about your

opinions?

Quoted 3 lines
Quoted 3 lines
 include/linux/iommu.h      |  1 +
 include/uapi/linux/iommu.h | 34

++++++++++++++++++++++++++++++++++

Quoted 22 lines
Quoted 22 lines
 2 files changed, 35 insertions(+)
include/linux/iommu.h index renamed from include/linux/iommu.h +0 -0

78a26ae..f6e4b49 100644

include/linux/iommu.h +1 -0
@@ -126,6 +126,7 @@ enum iommu_attr {
 	DOMAIN_ATTR_FSL_PAMUV1,
 	DOMAIN_ATTR_NESTING,	/* two stages of translation */
 	DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
+	DOMAIN_ATTR_NESTING_INFO,
 	DOMAIN_ATTR_MAX,
 };
 
(quoted diff) +0 -0

b/include/uapi/linux/iommu.h index 303f148..02eac73 100644

include/uapi/linux/iommu.h +0 -0
@@ -332,4 +332,38 @@ struct iommu_gpasid_bind_data {
 	};
 };
 
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
include/uapi/linux/iommu.h +7 -0
+struct iommu_nesting_info {
+	__u32	size;
+	__u32	format;
+	__u32	features;
+#define IOMMU_NESTING_FEAT_SYSWIDE_PASID	(1 << 0)
+#define IOMMU_NESTING_FEAT_BIND_PGTBL		(1 << 1)
+#define IOMMU_NESTING_FEAT_CACHE_INVLD		(1 << 2)
Quoted 8 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data {

Quoted 8 lines
include/uapi/linux/iommu.h +8 -0
+	__u32	flags;
+	__u8	data[];
+};
+
+/*
+ * @flags:	VT-d specific flags. Currently reserved for future
+ *		extension.
+ * @addr_width:	The output addr width of first level/stage

translation

Quoted 20 lines
Quoted 1 line

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 350

Quoted 1 line
include/uapi/linux/iommu.h +1 -0
+ * @pasid_bits:	Maximum supported PASID bits, 0 represents no

PASID

Quoted 2 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 352

Quoted 2 lines
include/uapi/linux/iommu.h +2 -0
+ *		support.
+ * @cap_reg:	Describe basic capabilities as defined in VT-d

capability

Quoted 9 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 357

Quoted 5 lines
include/uapi/linux/iommu.h +5 -0
+ *		register.
+ * @cap_mask:	Mark valid capability bits in @cap_reg.
+ * @ecap_reg:	Describe the extended capabilities as defined in VT-d
+ *		extended capability register.
+ * @ecap_mask:	Mark the valid capability bits in @ecap_reg.

Please explain this a little further, why do we need to tell userspace about cap/ecap register bits that aren't valid through this interface? Thanks,

we only want to tell userspace about the bits marked in the cap/ecap_mask. cap/ecap_mask is kind of white-list of the cap/ecap register. userspace should only care about the bits in the white-list, for other bits, it should ignore.

Quoted 5 lines
Quoted 2 lines

Regards,
Yi Liu

For invalid bits if kernel just clears them then do we still need additional mask bits to explicitly mark them out? I guess this might be the point that Alex asked...

For invalid bits, kernel will clear them. But I think the mask bits is still necessary. The mask bits tells user space the bits related to nesting. Without it, user space may have no idea about it.

Maybe talk about QEMU usage of the cap/ecap bits would help. QEMU vIOMMU decides cap/ecap bits according to QEMU cmdline. But not all of them are compatible with hardware support. Especially, vIOMMU built on nesting. So needs to sync the cap/ecap bits with host side. Based on the mask bits, QEMU can compare the cap/ecap bits configured by QEMU cmdline with the cap/ecap bits reported by this interface. This comparation is limited to the nesting related bits in cap/ecap, the other bits are not included and can use the configuration by QEMU cmdline.

The link below show the current Intel vIOMMU usage on the cap/ecap bits. For each assigned device, vIOMMU will compare the nesting related bits in cap/ecap and mask out the bits which hardware doesn't support. After the machine is intilized, the vIOMMU cap/ecap bits are determined. If user hot-plug devices to VM, vIOMMU will fail it if the hardware cap/ecap bits behind hot-plug device are not compatible with determined vIOMMU cap/ecap bits.

https://www.spinics.net/lists/kvm/msg218294.html

Regards,
Yi Liu

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

Alex

Quoted 12 lines
include/uapi/linux/iommu.h +11 -0
+ */
+struct iommu_nesting_info_vtd {
+	__u32	flags;
+	__u16	addr_width;
+	__u16	pasid_bits;
+	__u64	cap_reg;
+	__u64	cap_mask;
+	__u64	ecap_reg;
+	__u64	ecap_mask;
+};
+
 #endif /* _UAPI_IOMMU_H */
#7 lore raw

RE: [PATCH v2 02/15] iommu: Report domain nesting info

Quoted 106 lines

From: Liu, Yi L <[email protected]>
Sent: Monday, June 15, 2020 2:05 PM

Hi Kevin,

Quoted 58 lines

From: Tian, Kevin <[email protected]>
Sent: Monday, June 15, 2020 9:23 AM

Quoted 13 lines

From: Liu, Yi L <[email protected]>
Sent: Friday, June 12, 2020 5:05 PM

Hi Alex,

Quoted 8 lines

From: Alex Williamson <[email protected]>
Sent: Friday, June 12, 2020 3:30 AM

Quoted 2 lines · On Thu, 11 Jun 2020 05:15:21 -0700 Liu Yi L <[email protected]> wrote:

IOMMUs that support nesting translation needs report the capability info to userspace, e.g. the format of first level/stage paging

structures.

Quoted 41 lines
Quoted 14 lines
Quoted 10 lines
Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>
Quoted 4 lines

---
@Jean, Eric: as nesting was introduced for ARM, but looks like no actual user of it. right? So I'm wondering if we can reuse DOMAIN_ATTR_NESTING to retrieve nesting info? how about your

opinions?

Quoted 3 lines
Quoted 3 lines
 include/linux/iommu.h      |  1 +
 include/uapi/linux/iommu.h | 34

++++++++++++++++++++++++++++++++++

Quoted 22 lines
Quoted 22 lines
 2 files changed, 35 insertions(+)
include/linux/iommu.h index renamed from include/linux/iommu.h +0 -0

78a26ae..f6e4b49 100644

include/linux/iommu.h +1 -0
@@ -126,6 +126,7 @@ enum iommu_attr {
 	DOMAIN_ATTR_FSL_PAMUV1,
 	DOMAIN_ATTR_NESTING,	/* two stages of translation */
 	DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
+	DOMAIN_ATTR_NESTING_INFO,
 	DOMAIN_ATTR_MAX,
 };
 
(quoted diff) +0 -0

b/include/uapi/linux/iommu.h index 303f148..02eac73 100644

include/uapi/linux/iommu.h +0 -0
@@ -332,4 +332,38 @@ struct iommu_gpasid_bind_data {
 	};
 };
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data {

Quoted 7 lines
include/uapi/linux/iommu.h +7 -0
+struct iommu_nesting_info {
+	__u32	size;
+	__u32	format;
+	__u32	features;
+#define IOMMU_NESTING_FEAT_SYSWIDE_PASID	(1 << 0)
+#define IOMMU_NESTING_FEAT_BIND_PGTBL		(1 << 1)
+#define IOMMU_NESTING_FEAT_CACHE_INVLD		(1 <<

2)

Quoted 10 lines
Quoted 8 lines
Quoted 8 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data {

Quoted 8 lines
include/uapi/linux/iommu.h +8 -0
+	__u32	flags;
+	__u8	data[];
+};
+
+/*
+ * @flags:	VT-d specific flags. Currently reserved for future
+ *		extension.
+ * @addr_width:	The output addr width of first level/stage

translation

Quoted 1 line
Quoted 1 line
Quoted 1 line
include/uapi/linux/iommu.h +1 -0
+ * @pasid_bits:	Maximum supported PASID bits, 0 represents

no

Quoted 4 lines
Quoted 4 lines

PASID

Quoted 2 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 352

Quoted 2 lines
include/uapi/linux/iommu.h +2 -0
+ *		support.
+ * @cap_reg:	Describe basic capabilities as defined in VT-d

capability

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 357

Quoted 5 lines
include/uapi/linux/iommu.h +5 -0
+ *		register.
+ * @cap_mask:	Mark valid capability bits in @cap_reg.
+ * @ecap_reg:	Describe the extended capabilities as defined in VT-d
+ *		extended capability register.
+ * @ecap_mask:	Mark the valid capability bits in @ecap_reg.

Please explain this a little further, why do we need to tell userspace about cap/ecap register bits that aren't valid through this

interface?

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

Thanks,

we only want to tell userspace about the bits marked in the

cap/ecap_mask.

Quoted 7 lines
Quoted 6 lines

cap/ecap_mask is kind of white-list of the cap/ecap register. userspace should only care about the bits in the white-list, for other bits, it should ignore.

Regards,
Yi Liu

Quoted 7 lines
Quoted 1 line

For invalid bits if kernel just clears them then do we still need additional

mask bits

Quoted 1 line

to explicitly mark them out? I guess this might be the point that Alex asked...

For invalid bits, kernel will clear them. But I think the mask bits is still necessary. The mask bits tells user space the bits related to nesting. Without it, user space may have no idea about it.

userspace should know which bit is related to nesting and then should check that bit explicitly...

Quoted 10 lines

Maybe talk about QEMU usage of the cap/ecap bits would help. QEMU vIOMMU
decides cap/ecap bits according to QEMU cmdline. But not all of them are compatible with hardware support. Especially, vIOMMU built on nesting. So needs to sync the cap/ecap bits with host side. Based on the mask bits, QEMU can compare the cap/ecap bits configured by QEMU cmdline with the cap/ecap bits reported by this interface. This comparation is limited to the nesting related bits in cap/ecap, the other bits are not included and can use the configuration by QEMU cmdline.

I didn't get this explanation. Based on patch [15/15], nesting capabilities are defined as:
+/* Nesting Support Capability Alignment */
+#define VTD_CAP_FL1GP (1ULL << 56)
+#define VTD_CAP_FL5LP (1ULL << 60)
+#define VTD_ECAP_PRS (1ULL << 29)
+#define VTD_ECAP_ERS (1ULL << 30)
+#define VTD_ECAP_SRS (1ULL << 31)
+#define VTD_ECAP_EAFS (1ULL << 34)
+#define VTD_ECAP_PASID (1ULL << 40)

When Qemu gets an cmdline option it knows which bit out of above list should be checked against hardware capability. Then just do the check bit-by-bit. Why do we need mask bit in uapi to tell which bits are valid? Unless 0/1 doesn't represent validity of some bit. Do we have such example?

Quoted 31 lines

The link below show the current Intel vIOMMU usage on the cap/ecap bits. For each assigned device, vIOMMU will compare the nesting related bits in cap/ecap and mask out the bits which hardware doesn't support. After the machine is intilized, the vIOMMU cap/ecap bits are determined. If user hot-plug devices to VM, vIOMMU will fail it if the hardware cap/ecap bits behind hot-plug device are not compatible with determined vIOMMU cap/ecap
bits.

https://www.spinics.net/lists/kvm/msg218294.html

Regards,
Yi Liu

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

Alex

Quoted 12 lines
include/uapi/linux/iommu.h +11 -0
+ */
+struct iommu_nesting_info_vtd {
+	__u32	flags;
+	__u16	addr_width;
+	__u16	pasid_bits;
+	__u64	cap_reg;
+	__u64	cap_mask;
+	__u64	ecap_reg;
+	__u64	ecap_mask;
+};
+
 #endif /* _UAPI_IOMMU_H */
author
#8 lore raw

RE: [PATCH v2 02/15] iommu: Report domain nesting info

Quoted 112 lines

From: Tian, Kevin <[email protected]>
Sent: Tuesday, June 16, 2020 9:56 AM

Quoted 80 lines

From: Liu, Yi L <[email protected]>
Sent: Monday, June 15, 2020 2:05 PM

Hi Kevin,

Quoted 58 lines

From: Tian, Kevin <[email protected]>
Sent: Monday, June 15, 2020 9:23 AM

Quoted 13 lines

From: Liu, Yi L <[email protected]>
Sent: Friday, June 12, 2020 5:05 PM

Hi Alex,

Quoted 8 lines

From: Alex Williamson <[email protected]>
Sent: Friday, June 12, 2020 3:30 AM

Quoted 2 lines · On Thu, 11 Jun 2020 05:15:21 -0700 Liu Yi L <[email protected]> wrote:

IOMMUs that support nesting translation needs report the capability info to userspace, e.g. the format of first level/stage paging

structures.

Quoted 41 lines
Quoted 14 lines
Quoted 10 lines
Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>
Quoted 4 lines

---
@Jean, Eric: as nesting was introduced for ARM, but looks like no actual user of it. right? So I'm wondering if we can reuse DOMAIN_ATTR_NESTING to retrieve nesting info? how about your

opinions?

Quoted 3 lines
Quoted 3 lines
 include/linux/iommu.h      |  1 +
 include/uapi/linux/iommu.h | 34

++++++++++++++++++++++++++++++++++

Quoted 22 lines
Quoted 22 lines
 2 files changed, 35 insertions(+)
include/linux/iommu.h index renamed from include/linux/iommu.h +0 -0

78a26ae..f6e4b49 100644

include/linux/iommu.h +1 -0
@@ -126,6 +126,7 @@ enum iommu_attr {
 	DOMAIN_ATTR_FSL_PAMUV1,
 	DOMAIN_ATTR_NESTING,	/* two stages of translation */
 	DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
+	DOMAIN_ATTR_NESTING_INFO,
 	DOMAIN_ATTR_MAX,
 };
 
(quoted diff) +0 -0

b/include/uapi/linux/iommu.h index 303f148..02eac73 100644

include/uapi/linux/iommu.h +0 -0
@@ -332,4 +332,38 @@ struct iommu_gpasid_bind_data {
 	};
 };
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data {

Quoted 7 lines
include/uapi/linux/iommu.h +7 -0
+struct iommu_nesting_info {
+	__u32	size;
+	__u32	format;
+	__u32	features;
+#define IOMMU_NESTING_FEAT_SYSWIDE_PASID	(1 << 0)
+#define IOMMU_NESTING_FEAT_BIND_PGTBL		(1 << 1)
+#define IOMMU_NESTING_FEAT_CACHE_INVLD		(1 <<

2)

Quoted 9 lines
Quoted 8 lines
Quoted 8 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data {

Quoted 8 lines
include/uapi/linux/iommu.h +8 -0
+	__u32	flags;
+	__u8	data[];
+};
+
+/*
+ * @flags:	VT-d specific flags. Currently reserved for future
+ *		extension.
+ * @addr_width:	The output addr width of first level/stage

translation

Quoted 9 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 350

Quoted 1 line
include/uapi/linux/iommu.h +1 -0
+ * @pasid_bits:	Maximum supported PASID bits, 0 represents

no

Quoted 7 lines
Quoted 7 lines

PASID

Quoted 2 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 352

Quoted 2 lines
include/uapi/linux/iommu.h +2 -0
+ *		support.
+ * @cap_reg:	Describe basic capabilities as defined in VT-d

capability

Quoted 3 lines
Quoted 3 lines
include/uapi/linux/iommu.h +3 -0
+ *		register.
+ * @cap_mask:	Mark valid capability bits in @cap_reg.
+ * @ecap_reg:	Describe the extended capabilities as defined in

VT-d

Quoted 19 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 357

Quoted 2 lines
include/uapi/linux/iommu.h +2 -0
+ *		extended capability register.
+ * @ecap_mask:	Mark the valid capability bits in @ecap_reg.

Please explain this a little further, why do we need to tell userspace about cap/ecap register bits that aren't valid through this

interface?

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

Thanks,

we only want to tell userspace about the bits marked in the

cap/ecap_mask.

Quoted 8 lines
Quoted 6 lines

cap/ecap_mask is kind of white-list of the cap/ecap register. userspace should only care about the bits in the white-list, for other bits, it should ignore.

Regards,
Yi Liu

For invalid bits if kernel just clears them then do we still need additional

mask bits

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

to explicitly mark them out? I guess this might be the point that Alex asked...

For invalid bits, kernel will clear them. But I think the mask bits is still necessary. The mask bits tells user space the bits related to nesting. Without it, user space may have no idea about it.

userspace should know which bit is related to nesting and then should check that bit explicitly...

ok, so userspace could get such info by the understanding of spec, right? if user space could get it, then I think it's uncessary to have cap/ecap mask bits.

Quoted 22 lines
Quoted 10 lines

Maybe talk about QEMU usage of the cap/ecap bits would help. QEMU vIOMMU
decides cap/ecap bits according to QEMU cmdline. But not all of them are compatible with hardware support. Especially, vIOMMU built on nesting. So needs to sync the cap/ecap bits with host side. Based on the mask bits, QEMU can compare the cap/ecap bits configured by QEMU cmdline with the cap/ecap bits reported by this interface. This comparation is limited to the nesting related bits in cap/ecap, the other bits are not included and can use the configuration by QEMU cmdline.

I didn't get this explanation. Based on patch [15/15], nesting capabilities are defined as:

(quoted diff) +8 -0
+/* Nesting Support Capability Alignment */
+#define VTD_CAP_FL1GP		(1ULL << 56)
+#define VTD_CAP_FL5LP		(1ULL << 60)
+#define VTD_ECAP_PRS		(1ULL << 29)
+#define VTD_ECAP_ERS		(1ULL << 30)
+#define VTD_ECAP_SRS		(1ULL << 31)
+#define VTD_ECAP_EAFS		(1ULL << 34)
+#define VTD_ECAP_PASID		(1ULL << 40)
Quoted 4 lines

When Qemu gets an cmdline option it knows which bit out of above list should be checked against hardware capability. Then just do the check bit-by-bit. Why do we need mask bit in uapi to tell which bits are valid?

as above reply, if userspace has the check list for the cap/ecap bits, then it's not necessary to use mask bit.

Quoted 2 lines

Unless 0/1 doesn't represent validity of some bit. Do we have such example?

yes, like the pasid bits. it's 20 bits. but we already got pasid_bits in the iommu_nesting_info_vtd structure. so it's not covered in the ecap_bits.

Regards,
Yi Liu

Quoted 31 lines
Quoted 31 lines

The link below show the current Intel vIOMMU usage on the cap/ecap bits. For each assigned device, vIOMMU will compare the nesting related bits in cap/ecap and mask out the bits which hardware doesn't support. After the machine is intilized, the vIOMMU cap/ecap bits are determined. If user hot-plug devices to VM, vIOMMU will fail it if the hardware cap/ecap bits behind hot-plug device are not compatible with determined vIOMMU cap/ecap
bits.

https://www.spinics.net/lists/kvm/msg218294.html

Regards,
Yi Liu

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

Alex

Quoted 12 lines
include/uapi/linux/iommu.h +11 -0
+ */
+struct iommu_nesting_info_vtd {
+	__u32	flags;
+	__u16	addr_width;
+	__u16	pasid_bits;
+	__u64	cap_reg;
+	__u64	cap_mask;
+	__u64	ecap_reg;
+	__u64	ecap_mask;
+};
+
 #endif /* _UAPI_IOMMU_H */
#9 lore raw

Re: [PATCH v2 02/15] iommu: Report domain nesting info

In reply to Liu Yi L

[+ Will and Robin]

Hi Yi,

Quoted 12 lines · Liu Yi L

IOMMUs that support nesting translation needs report the capability info to userspace, e.g. the format of first level/stage paging structures.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>
Quoted 4 lines

---
@Jean, Eric: as nesting was introduced for ARM, but looks like no actual user of it. right? So I'm wondering if we can reuse DOMAIN_ATTR_NESTING to retrieve nesting info? how about your opinions?

Sure, I think we could rework the getters for DOMAIN_ATTR_NESTING since they aren't used, but we do need to keep the setters as is.

Before attaching a domain, VFIO sets DOMAIN_ATTR_NESTING if userspace requested a VFIO_TYPE1_NESTING_IOMMU container. This is necessary for the SMMU driver to know how to attach later, but at that point we don't know whether the SMMU does support nesting (since the domain isn't attached to any endpoint). During attach, the SMMU driver adapts to the SMMU's capabilities, and may well fallback to one stage if the SMMU doesn't support nesting.

VFIO should check after attaching that the nesting attribute held, by calling iommu_domain_get_attr(NESTING). At the moment it does not, and since your 03/15 patch does that with additional info, I agree with reusing DOMAIN_ATTR_NESTING instead of adding DOMAIN_ATTR_NESTING_INFO.

However it requires changing the get_attr(NESTING) implementations in both SMMU drivers as a precursor of this series, to avoid breaking VFIO_TYPE1_NESTING_IOMMU on Arm. Since we haven't yet defined the nesting_info structs for SMMUv2 and v3, I suppose we could return an empty struct iommu_nesting_info for now?

Quoted 22 lines
 include/linux/iommu.h      |  1 +
 include/uapi/linux/iommu.h | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
include/linux/iommu.h +1 -0
index 78a26ae..f6e4b49 100644
@@ -126,6 +126,7 @@ enum iommu_attr {
 	DOMAIN_ATTR_FSL_PAMUV1,
 	DOMAIN_ATTR_NESTING,	/* two stages of translation */
 	DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
+	DOMAIN_ATTR_NESTING_INFO,
 	DOMAIN_ATTR_MAX,
 };
 
include/uapi/linux/iommu.h +0 -0
index 303f148..02eac73 100644

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 337

Quoted 6 lines
include/uapi/linux/iommu.h +3 -0
@@ -332,4 +332,38 @@ struct iommu_gpasid_bind_data {
 	};
 };
 
+struct iommu_nesting_info {
+	__u32	size;
+	__u32	format;

What goes into format? And flags? This structure needs some documentation.

Thanks,
Jean

Quoted 35 lines
include/uapi/linux/iommu.h +31 -0
+	__u32	features;
+#define IOMMU_NESTING_FEAT_SYSWIDE_PASID	(1 << 0)
+#define IOMMU_NESTING_FEAT_BIND_PGTBL		(1 << 1)
+#define IOMMU_NESTING_FEAT_CACHE_INVLD		(1 << 2)
+	__u32	flags;
+	__u8	data[];
+};
+
+/*
+ * @flags:	VT-d specific flags. Currently reserved for future
+ *		extension.
+ * @addr_width:	The output addr width of first level/stage translation
+ * @pasid_bits:	Maximum supported PASID bits, 0 represents no PASID
+ *		support.
+ * @cap_reg:	Describe basic capabilities as defined in VT-d capability
+ *		register.
+ * @cap_mask:	Mark valid capability bits in @cap_reg.
+ * @ecap_reg:	Describe the extended capabilities as defined in VT-d
+ *		extended capability register.
+ * @ecap_mask:	Mark the valid capability bits in @ecap_reg.
+ */
+struct iommu_nesting_info_vtd {
+	__u32	flags;
+	__u16	addr_width;
+	__u16	pasid_bits;
+	__u64	cap_reg;
+	__u64	cap_mask;
+	__u64	ecap_reg;
+	__u64	ecap_mask;
+};
+
 #endif /* _UAPI_IOMMU_H */
Signature · 1 line
2.7.4

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

author
#10 lore raw

RE: [PATCH v2 02/15] iommu: Report domain nesting info

Hi Jean,

Quoted 28 lines

From: Jean-Philippe Brucker < [email protected]>
Sent: Wednesday, June 17, 2020 10:39 PM

[+ Will and Robin]

Hi Yi,

Quoted 12 lines · Liu Yi L

IOMMUs that support nesting translation needs report the capability info to userspace, e.g. the format of first level/stage paging structures.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>
Quoted 4 lines

---
@Jean, Eric: as nesting was introduced for ARM, but looks like no actual user of it. right? So I'm wondering if we can reuse DOMAIN_ATTR_NESTING to retrieve nesting info? how about your opinions?

Sure, I think we could rework the getters for DOMAIN_ATTR_NESTING since they aren't used, but we do need to keep the setters as is.

Quoted 6 lines

Before attaching a domain, VFIO sets DOMAIN_ATTR_NESTING if userspace requested a VFIO_TYPE1_NESTING_IOMMU container. This is necessary for the SMMU driver to know how to attach later, but at that point we don't know whether the SMMU does support nesting (since the domain isn't attached to any endpoint). During attach, the SMMU driver adapts to the SMMU's capabilities, and may well fallback to one stage if the SMMU doesn't support nesting.

got you. so even VFIO sets DOMAIN_ATTR_NESTING successfully, it doesn't mean the nesting will be used. yeah, it's a little bit different with VT-d side. intel iommu driver will fail ATT_NESTING setting if it found not all iommu units in the system are nesting capable.

Quoted 10 lines

VFIO should check after attaching that the nesting attribute held, by calling iommu_domain_get_attr(NESTING). At the moment it does not, and since your 03/15 patch does that with additional info, I agree with reusing DOMAIN_ATTR_NESTING instead of adding DOMAIN_ATTR_NESTING_INFO.

However it requires changing the get_attr(NESTING) implementations in both SMMU drivers as a precursor of this series, to avoid breaking VFIO_TYPE1_NESTING_IOMMU on Arm. Since we haven't yet defined the nesting_info structs for SMMUv2 and v3, I suppose we could return an empty struct iommu_nesting_info for now?

got you. I think it works. So far, I didn't see any getter for ATTR_NESTING, once SMMU drivers return empty struct iommu_nesting_info, VFIO won't fail. will do it when switching to reuse ATTR_NESTING for getting nesting info.

Quoted 25 lines
Quoted 25 lines
 include/linux/iommu.h      |  1 +
 include/uapi/linux/iommu.h | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
include/linux/iommu.h index renamed from include/linux/iommu.h +0 -0

78a26ae..f6e4b49 100644

include/linux/iommu.h +1 -0
@@ -126,6 +126,7 @@ enum iommu_attr {
 	DOMAIN_ATTR_FSL_PAMUV1,
 	DOMAIN_ATTR_NESTING,	/* two stages of translation */
 	DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
+	DOMAIN_ATTR_NESTING_INFO,
 	DOMAIN_ATTR_MAX,
 };
 
include/uapi/linux/iommu.h +0 -0
index 303f148..02eac73 100644
@@ -332,4 +332,38 @@ struct iommu_gpasid_bind_data {
 	};
 };
 
Quoted 5 lines

include/uapi/linux/iommu.h struct iommu_gpasid_bind_data { · line 337

Quoted 3 lines
include/uapi/linux/iommu.h +3 -0
+struct iommu_nesting_info {
+	__u32	size;
+	__u32	format;

What goes into format? And flags? This structure needs some documentation.

format will be the same with the definition of @format in iommu_gpasid_bind_data. flags is reserved for future extension. will add description in next version. :-)

struct iommu_gpasid_bind_data {
        __u32 argsz;
#define IOMMU_GPASID_BIND_VERSION_1     1
        __u32 version;
#define IOMMU_PASID_FORMAT_INTEL_VTD    1
        __u32 format;
#define IOMMU_SVA_GPASID_VAL    (1 << 0) /* guest PASID valid */
        __u64 flags;
        __u64 gpgd;
        __u64 hpasid;
        __u64 gpasid;
        __u32 addr_width;
        __u8  padding[12];
        /* Vendor specific data */
        union {
                struct iommu_gpasid_bind_data_vtd vtd;
        } vendor;
};

Regards,
Yi Liu

Quoted 38 lines

Thanks,
Jean

Quoted 35 lines
include/uapi/linux/iommu.h +31 -1
+	__u32	features;
+#define IOMMU_NESTING_FEAT_SYSWIDE_PASID	(1 << 0)
+#define IOMMU_NESTING_FEAT_BIND_PGTBL		(1 << 1)
+#define IOMMU_NESTING_FEAT_CACHE_INVLD		(1 << 2)
+	__u32	flags;
+	__u8	data[];
+};
+
+/*
+ * @flags:	VT-d specific flags. Currently reserved for future
+ *		extension.
+ * @addr_width:	The output addr width of first level/stage translation
+ * @pasid_bits:	Maximum supported PASID bits, 0 represents no PASID
+ *		support.
+ * @cap_reg:	Describe basic capabilities as defined in VT-d capability
+ *		register.
+ * @cap_mask:	Mark valid capability bits in @cap_reg.
+ * @ecap_reg:	Describe the extended capabilities as defined in VT-d
+ *		extended capability register.
+ * @ecap_mask:	Mark the valid capability bits in @ecap_reg.
+ */
+struct iommu_nesting_info_vtd {
+	__u32	flags;
+	__u16	addr_width;
+	__u16	pasid_bits;
+	__u64	cap_reg;
+	__u64	cap_mask;
+	__u64	ecap_reg;
+	__u64	ecap_mask;
+};
+
 #endif /* _UAPI_IOMMU_H */
--

2.7.4

author
#11 lore raw

PATCH v2 3/15 vfio/type1: Report iommu nesting info to userspace

In reply to Liu Yi L

This patch exports iommu nesting capability info to user space through VFIO. User space is expected to check this info for supported uAPIs (e.g. PASID alloc/free, bind page table, and cache invalidation) and the vendor specific format information for first level/stage page table that will be bound to.

The nesting info is available only after the nesting iommu type is set for a container. Current implementation imposes one limitation - one nesting container should include at most one group. The philosophy of vfio container is having all groups/devices within the container share the same IOMMU context. When vSVA is enabled, one IOMMU context could include one 2nd-level address space and multiple 1st-level address spaces. While the 2nd-leve address space is reasonably sharable by multiple groups , blindly sharing 1st-level address spaces across all groups within the container might instead break the guest expectation. In the future sub/ super container concept might be introduced to allow partial address space sharing within an IOMMU context. But for now let's go with this restriction by requiring singleton container for using nesting iommu features. Below link has the related discussion about this decision.

https://lkml.org/lkml/2020/5/15/1028

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>
---
 drivers/vfio/vfio_iommu_type1.c | 73 +++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/vfio.h       |  9 +++++
 2 files changed, 82 insertions(+)
drivers/vfio/vfio_iommu_type1.c +73 -0
index 402aad3..22432cf 100644
@@ -71,6 +71,7 @@ struct vfio_iommu {
 	unsigned int		dma_avail;
 	bool			v2;
 	bool			nesting;
+	struct iommu_nesting_info *nesting_info;
 };
 
 struct vfio_domain {
@@ -125,6 +126,9 @@ struct vfio_regions {
 #define IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu)	\
 					(!list_empty(&iommu->domain_list))
 
+#define IS_DOMAIN_IN_CONTAINER(iommu)	((iommu->external_domain) || \
+					 (!list_empty(&iommu->domain_list)))
+
 static int put_pfn(unsigned long pfn, int prot);
 
 /*
@@ -1641,6 +1645,12 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
 		}
 	}
 
+	/* Nesting type container can include only one group */
+	if (iommu->nesting && IS_DOMAIN_IN_CONTAINER(iommu)) {
+		mutex_unlock(&iommu->lock);
+		return -EINVAL;
+	}
+
 	group = kzalloc(sizeof(*group), GFP_KERNEL);
 	domain = kzalloc(sizeof(*domain), GFP_KERNEL);
 	if (!group || !domain) {
@@ -1700,6 +1710,36 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
 	if (ret)
 		goto out_domain;
 
+	/* Nesting cap info is available only after attaching */
+	if (iommu->nesting) {
+		struct iommu_nesting_info tmp;
+		struct iommu_nesting_info *info;
+
+		/* First get the size of vendor specific nesting info */
+		ret = iommu_domain_get_attr(domain->domain,
+					    DOMAIN_ATTR_NESTING_INFO,
+					    &tmp);
+		if (ret)
+			goto out_detach;
+
+		info = kzalloc(tmp.size, GFP_KERNEL);
+		if (!info) {
+			ret = -ENOMEM;
+			goto out_detach;
+		}
+
+		/* Now get the nesting info */
+		info->size = tmp.size;
+		ret = iommu_domain_get_attr(domain->domain,
+					    DOMAIN_ATTR_NESTING_INFO,
+					    info);
+		if (ret) {
+			kfree(info);
+			goto out_detach;
+		}
+		iommu->nesting_info = info;
+	}
+
 	/* Get aperture info */
 	iommu_domain_get_attr(domain->domain, DOMAIN_ATTR_GEOMETRY, &geo);
 
@@ -1801,6 +1841,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
 	return 0;
 
 out_detach:
+	kfree(iommu->nesting_info);
 	vfio_iommu_detach_group(domain, group);
 out_domain:
 	iommu_domain_free(domain->domain);
@@ -1998,6 +2039,8 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,
 					vfio_iommu_unmap_unpin_all(iommu);
 				else
 					vfio_iommu_unmap_unpin_reaccount(iommu);
+
+				kfree(iommu->nesting_info);
 			}
 			iommu_domain_free(domain->domain);
 			list_del(&domain->next);
@@ -2190,6 +2233,30 @@ static int vfio_iommu_iova_build_caps(struct vfio_iommu *iommu,
 	return ret;
 }
 
+static int vfio_iommu_info_add_nesting_cap(struct vfio_iommu *iommu,
+					   struct vfio_info_cap *caps)
+{
+	struct vfio_info_cap_header *header;
+	struct vfio_iommu_type1_info_cap_nesting *nesting_cap;
+	size_t size;
+
+	size = sizeof(*nesting_cap) + iommu->nesting_info->size;
+
+	header = vfio_info_cap_add(caps, size,
+				   VFIO_IOMMU_TYPE1_INFO_CAP_NESTING, 1);
+	if (IS_ERR(header))
+		return PTR_ERR(header);
+
+	nesting_cap = container_of(header,
+				   struct vfio_iommu_type1_info_cap_nesting,
+				   header);
+
+	memcpy(&nesting_cap->info, iommu->nesting_info,
+	       iommu->nesting_info->size);
+
+	return 0;
+}
+
 static int vfio_iommu_type1_get_info(struct vfio_iommu *iommu,
 				     unsigned long arg)
 {
@@ -2223,6 +2290,12 @@ static int vfio_iommu_type1_get_info(struct vfio_iommu *iommu,
 	if (ret)
 		return ret;
 
+	if (iommu->nesting_info) {
+		ret = vfio_iommu_info_add_nesting_cap(iommu, &caps);
+		if (ret)
+			return ret;
+	}
+
 	if (caps.size) {
 		info.flags |= VFIO_IOMMU_INFO_CAPS;
 
include/uapi/linux/vfio.h +9 -0
index 015516b..26e3dce 100644
@@ -14,6 +14,7 @@
 
 #include <linux/types.h>
 #include <linux/ioctl.h>
+#include <linux/iommu.h>
 
 #define VFIO_API_VERSION	0
 
@@ -785,6 +786,14 @@ struct vfio_iommu_type1_info_cap_iova_range {
 	struct	vfio_iova_range iova_ranges[];
 };
 
+#define VFIO_IOMMU_TYPE1_INFO_CAP_NESTING  2
+
+struct vfio_iommu_type1_info_cap_nesting {
+	struct	vfio_info_cap_header header;
+	__u32	flags;
+	__u8	info[];
+};
+
 #define VFIO_IOMMU_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 12)
 
 /**
Signature · 6 lines
2.7.4

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author
#12 lore raw

PATCH v2 4/15 vfio: Add PASID allocation/free support

In reply to Liu Yi L

Shared Virtual Addressing (a.k.a Shared Virtual Memory) allows sharing multiple process virtual address spaces with the device for simplified programming model. PASID is used to tag an virtual address space in DMA requests and to identify the related translation structure in IOMMU. When a PASID-capable device is assigned to a VM, we want the same capability of using PASID to tag guest process virtual address spaces to achieve virtual SVA (vSVA).

PASID management for guest is vendor specific. Some vendors (e.g. Intel VT-d) requires system-wide managed PASIDs cross all devices, regardless of whether a device is used by host or assigned to guest. Other vendors (e.g. ARM SMMU) may allow PASIDs managed per-device thus could be fully delegated to the guest for assigned devices.

For system-wide managed PASIDs, this patch introduces a vfio module to handle explicit PASID alloc/free requests from guest. Allocated PASIDs are associated to a process (or, mm_struct) in IOASID core. A vfio_mm object is introduced to track mm_struct. Multiple VFIO containers within a process share the same vfio_mm object.

A quota mechanism is provided to prevent malicious user from exhausting available PASIDs. Currently the quota is a global parameter applied to all VFIO devices. In the future per-device quota might be supported too.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>

---
v1 -> v2:
*) added in v2, split from the pasid alloc/free support of v1

 drivers/vfio/Kconfig      |   5 ++
 drivers/vfio/Makefile     |   1 +
 drivers/vfio/vfio_pasid.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/vfio.h      |  28 +++++++++
 4 files changed, 185 insertions(+)
 create mode 100644 drivers/vfio/vfio_pasid.c
drivers/vfio/Kconfig +5 -0
index fd17db9..3d8a108 100644
@@ -19,6 +19,11 @@ config VFIO_VIRQFD
 	depends on VFIO && EVENTFD
 	default n
 
+config VFIO_PASID
+	tristate
+	depends on IOASID && VFIO
+	default n
+
 menuconfig VFIO
 	tristate "VFIO Non-Privileged userspace driver framework"
 	depends on IOMMU_API
drivers/vfio/Makefile +1 -0
index de67c47..bb836a3 100644
@@ -3,6 +3,7 @@ vfio_virqfd-y := virqfd.o
 
 obj-$(CONFIG_VFIO) += vfio.o
 obj-$(CONFIG_VFIO_VIRQFD) += vfio_virqfd.o
+obj-$(CONFIG_VFIO_PASID) += vfio_pasid.o
 obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o
 obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o
 obj-$(CONFIG_VFIO_SPAPR_EEH) += vfio_spapr_eeh.o
drivers/vfio/vfio_pasid.c new file +151 -0

151 lines not on this page · raw message

include/linux/vfio.h +28 -0
index 5d92ee1..104418a 100644
@@ -95,6 +95,34 @@ extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
 extern void vfio_unregister_iommu_driver(
 				const struct vfio_iommu_driver_ops *ops);
 
+struct vfio_mm;
+#if IS_ENABLED(CONFIG_VFIO_PASID)
+extern struct vfio_mm *vfio_mm_get_from_task(struct task_struct *task);
+extern void vfio_mm_put(struct vfio_mm *vmm);
+extern int vfio_pasid_alloc(struct vfio_mm *vmm, int min, int max);
+extern void vfio_pasid_free_range(struct vfio_mm *vmm,
+					ioasid_t min, ioasid_t max);
+#else
+static inline struct vfio_mm *vfio_mm_get_from_task(struct task_struct *task)
+{
+	return NULL;
+}
+
+static inline void vfio_mm_put(struct vfio_mm *vmm)
+{
+}
+
+static inline int vfio_pasid_alloc(struct vfio_mm *vmm, int min, int max)
+{
+	return -ENOTTY;
+}
+
+static inline void vfio_pasid_free_range(struct vfio_mm *vmm,
+					  ioasid_t min, ioasid_t max)
+{
+}
+#endif /* CONFIG_VFIO_PASID */
+
 /*
  * External user API
  */
Signature · 1 line
2.7.4
author
#13 lore raw

PATCH v2 5/15 iommu/vt-d: Support setting ioasid set to domain

In reply to Liu Yi L

From IOMMU p.o.v., PASIDs allocated and managed by external components (e.g. VFIO) will be passed in for gpasid_bind/unbind operation. IOMMU needs some knowledge to check the PASID ownership, hence add an interface for those components to tell the PASID owner.

In latest kernel design, PASID ownership is managed by IOASID set where the PASID is allocated from. This patch adds support for setting ioasid set ID to the domains used for nesting/vSVA. Subsequent SVA operations on the PASID will be checked against its IOASID set for proper ownership.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>
---
 drivers/iommu/intel-iommu.c | 16 ++++++++++++++++
 include/linux/intel-iommu.h |  4 ++++
 include/linux/iommu.h       |  1 +
 3 files changed, 21 insertions(+)
drivers/iommu/intel-iommu.c +16 -0
index 5628e4b..2d59a5d 100644
@@ -1788,6 +1788,7 @@ static struct dmar_domain *alloc_domain(int flags)
 	if (first_level_by_default())
 		domain->flags |= DOMAIN_FLAG_USE_FIRST_LEVEL;
 	domain->has_iotlb_device = false;
+	domain->ioasid_sid = INVALID_IOASID_SET;
 	INIT_LIST_HEAD(&domain->devices);
 
 	return domain;
@@ -6035,6 +6036,21 @@ intel_iommu_domain_set_attr(struct iommu_domain *domain,
 		}
 		spin_unlock_irqrestore(&device_domain_lock, flags);
 		break;
+	case DOMAIN_ATTR_IOASID_SID:
+		if (!(dmar_domain->flags & DOMAIN_FLAG_NESTING_MODE)) {
+			ret = -ENODEV;
+			break;
+		}
+		if ((dmar_domain->ioasid_sid != INVALID_IOASID_SET) &&
+		    (dmar_domain->ioasid_sid != (*(int *) data))) {
+			pr_warn_ratelimited("multi ioasid_set (%d:%d) setting",
+					    dmar_domain->ioasid_sid,
+					    (*(int *) data));
+			ret = -EBUSY;
+			break;
+		}
+		dmar_domain->ioasid_sid = *(int *) data;
+		break;
 	default:
 		ret = -EINVAL;
 		break;
include/linux/intel-iommu.h +4 -0
index 1e02624..29d1c6f 100644
@@ -548,6 +548,10 @@ struct dmar_domain {
 					   2 == 1GiB, 3 == 512GiB, 4 == 1TiB */
 	u64		max_addr;	/* maximum mapped address */
 
+	int		ioasid_sid;	/*
+					 * the ioasid set which tracks all
+					 * PASIDs used by the domain.
+					 */
 	int		default_pasid;	/*
 					 * The default pasid used for non-SVM
 					 * traffic on mediated devices.
include/linux/iommu.h +1 -0
index f6e4b49..57c46ae 100644
@@ -127,6 +127,7 @@ enum iommu_attr {
 	DOMAIN_ATTR_NESTING,	/* two stages of translation */
 	DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
 	DOMAIN_ATTR_NESTING_INFO,
+	DOMAIN_ATTR_IOASID_SID,
 	DOMAIN_ATTR_MAX,
 };
 
Signature · 6 lines
2.7.4

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author
#14 lore raw

PATCH v2 6/15 vfio/type1: Add VFIO_IOMMU_PASID_REQUEST (alloc/free)

In reply to Liu Yi L

This patch allows user space to request PASID allocation/free, e.g. when serving the request from the guest.

PASIDs that are not freed by userspace are automatically freed when the IOASID set is destroyed when process exits.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>

---
v1 -> v2:
*) move the vfio_mm related code to be a seprate module
*) use a single structure for alloc/free, could support a range of PASIDs *) fetch vfio_mm at group_attach time instead of at iommu driver open time

 drivers/vfio/Kconfig            |  1 +
 drivers/vfio/vfio_iommu_type1.c | 96 ++++++++++++++++++++++++++++++++++++++++-
 drivers/vfio/vfio_pasid.c       | 10 +++++
 include/linux/vfio.h            |  6 +++
 include/uapi/linux/vfio.h       | 36 ++++++++++++++++
 5 files changed, 147 insertions(+), 2 deletions(-)
drivers/vfio/Kconfig +1 -0
index 3d8a108..95d90c6 100644
@@ -2,6 +2,7 @@
 config VFIO_IOMMU_TYPE1
 	tristate
 	depends on VFIO
+	select VFIO_PASID if (X86)
 	default n
 
 config VFIO_IOMMU_SPAPR_TCE
drivers/vfio/vfio_iommu_type1.c +94 -2

138 lines not on this page · raw message

drivers/vfio/vfio_pasid.c +10 -0
index dd5b6d1..2ea9f1a 100644
@@ -54,6 +54,7 @@ void vfio_mm_put(struct vfio_mm *vmm)
 {
 	kref_put_mutex(&vmm->kref, vfio_mm_release, &vfio_pasid.vfio_mm_lock);
 }
+EXPORT_SYMBOL_GPL(vfio_mm_put);
 
 static void vfio_mm_get(struct vfio_mm *vmm)
 {
@@ -103,6 +104,13 @@ struct vfio_mm *vfio_mm_get_from_task(struct task_struct *task)
 	mmput(mm);
 	return vmm;
 }
+EXPORT_SYMBOL_GPL(vfio_mm_get_from_task);
+
+int vfio_mm_ioasid_sid(struct vfio_mm *vmm)
+{
+	return vmm->ioasid_sid;
+}
+EXPORT_SYMBOL_GPL(vfio_mm_ioasid_sid);
 
 int vfio_pasid_alloc(struct vfio_mm *vmm, int min, int max)
 {
@@ -112,6 +120,7 @@ int vfio_pasid_alloc(struct vfio_mm *vmm, int min, int max)
 
 	return (pasid == INVALID_IOASID) ? -ENOSPC : pasid;
 }
+EXPORT_SYMBOL_GPL(vfio_pasid_alloc);
 
 void vfio_pasid_free_range(struct vfio_mm *vmm,
 			    ioasid_t min, ioasid_t max)
@@ -129,6 +138,7 @@ void vfio_pasid_free_range(struct vfio_mm *vmm,
 	for (; pasid <= max; pasid++)
 		ioasid_free(pasid);
 }
+EXPORT_SYMBOL_GPL(vfio_pasid_free_range);
 
 static int __init vfio_pasid_init(void)
 {
include/linux/vfio.h +6 -0
index 104418a..76da56e 100644
@@ -99,6 +99,7 @@ struct vfio_mm;
 #if IS_ENABLED(CONFIG_VFIO_PASID)
 extern struct vfio_mm *vfio_mm_get_from_task(struct task_struct *task);
 extern void vfio_mm_put(struct vfio_mm *vmm);
+int vfio_mm_ioasid_sid(struct vfio_mm *vmm);
 extern int vfio_pasid_alloc(struct vfio_mm *vmm, int min, int max);
 extern void vfio_pasid_free_range(struct vfio_mm *vmm,
 					ioasid_t min, ioasid_t max);
@@ -112,6 +113,11 @@ static inline void vfio_mm_put(struct vfio_mm *vmm)
 {
 }
 
+static inline int vfio_mm_ioasid_sid(struct vfio_mm *vmm)
+{
+	return -ENOTTY;
+}
+
 static inline int vfio_pasid_alloc(struct vfio_mm *vmm, int min, int max)
 {
 	return -ENOTTY;
include/uapi/linux/vfio.h +36 -0
index 26e3dce..24d1992 100644
@@ -840,6 +840,42 @@ struct vfio_iommu_type1_dma_unmap {
 #define VFIO_IOMMU_ENABLE	_IO(VFIO_TYPE, VFIO_BASE + 15)
 #define VFIO_IOMMU_DISABLE	_IO(VFIO_TYPE, VFIO_BASE + 16)
 
+/**
+ * VFIO_IOMMU_PASID_REQUEST - _IOWR(VFIO_TYPE, VFIO_BASE + 22,
+ *				struct vfio_iommu_type1_pasid_request)
+ *
+ * PASID (Processor Address Space ID) is a PCIe concept for tagging
+ * address spaces in DMA requests. When system-wide PASID allocation
+ * is required by underlying iommu driver (e.g. Intel VT-d), this
+ * provides an interface for userspace to request pasid alloc/free
+ * for its assigned devices. Userspace should check the availability
+ * of this API through VFIO_IOMMU_GET_INFO.
+ *
+ * @flags=VFIO_IOMMU_ALLOC_PASID, allocate a single PASID within @range.
+ * @flags=VFIO_IOMMU_FREE_PASID, free the PASIDs within @range.
+ * @range is [min, max], which means both @min and @max are inclusive.
+ * ALLOC_PASID and FREE_PASID are mutually exclusive.
+ *
+ * returns: allocated PASID value on success, -errno on failure for
+ *	     ALLOC_PASID;
+ *	     0 for FREE_PASID operation;
+ */
+struct vfio_iommu_type1_pasid_request {
+	__u32	argsz;
+#define VFIO_IOMMU_ALLOC_PASID	(1 << 0)
+#define VFIO_IOMMU_FREE_PASID	(1 << 1)
+	__u32	flags;
+	struct {
+		__u32	min;
+		__u32	max;
+	} range;
+};
+
+#define VFIO_PASID_REQUEST_MASK	(VFIO_IOMMU_ALLOC_PASID | \
+					 VFIO_IOMMU_FREE_PASID)
+
+#define VFIO_IOMMU_PASID_REQUEST	_IO(VFIO_TYPE, VFIO_BASE + 22)
+
 /* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */
 
 /*
Signature · 1 line
2.7.4
author
#15 lore raw

PATCH v2 7/15 iommu/uapi: Add iommu_gpasid_unbind_data

In reply to Liu Yi L

Existing iommu_gpasid_bind_data is used for binding guest page tables to a specified PASID. While for unwind it, a unbind_data structure is needed.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>
---
 include/uapi/linux/iommu.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/uapi/linux/iommu.h +13 -0
index 02eac73..46a7c57 100644
@@ -332,6 +332,19 @@ struct iommu_gpasid_bind_data {
 	};
 };
 
+/**
+ * struct iommu_gpasid_unbind_data - Information about device and guest PASID
+ *				     unbinding
+ * @argsz:	User filled size of this data
+ * @flags:	Additional information on guest unbind request
+ * @pasid:	Process address space ID used for the guest mm in host IOMMU
+ */
+struct iommu_gpasid_unbind_data {
+	__u32 argsz;
+	__u64 flags;
+	__u64 pasid;
+};
+
 struct iommu_nesting_info {
 	__u32	size;
 	__u32	format;
Signature · 6 lines
2.7.4

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author
#16 lore raw

PATCH v2 8/15 iommu: Pass domain and unbind_data to sva_unbind_gpasid()

In reply to Liu Yi L

From: Yi Sun <[email protected]>

Current interface is good enough for SVA virtualization on an assigned physical PCI device, but when it comes to mediated devices, a physical device may attached with multiple aux-domains. So this interface needs to pass in domain info. Then the iommu driver is able to know which domain will be used for the 2nd stage translation of the nesting mode.

This patch passes @domain per the above reason. This interface is supposed to serve the unbind request from user-space, should pass in unbind_data as well.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>
---
 drivers/iommu/intel-svm.c   | 14 ++++++++++++--
 drivers/iommu/iommu.c       |  4 ++--
 include/linux/intel-iommu.h |  3 ++-
 include/linux/iommu.h       |  8 +++++---
 4 files changed, 21 insertions(+), 8 deletions(-)
drivers/iommu/intel-svm.c +12 -2
index 6272da6..bf55e2f 100644
@@ -445,16 +445,26 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
 	return ret;
 }
 
-int intel_svm_unbind_gpasid(struct device *dev, int pasid)
+int intel_svm_unbind_gpasid(struct iommu_domain *domain,
+			    struct device *dev,
+			    struct iommu_gpasid_unbind_data *data)
 {
 	struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
 	struct intel_svm_dev *sdev;
 	struct intel_svm *svm;
 	int ret = -EINVAL;
+	unsigned long minsz;
+	int pasid;
+
+	if (WARN_ON(!iommu) || !data)
+		return -EINVAL;
 
-	if (WARN_ON(!iommu))
+	minsz = offsetofend(struct iommu_gpasid_unbind_data, pasid);
+	if (data->argsz < minsz || data->flags)
 		return -EINVAL;
 
+	pasid = data->pasid;
+
 	mutex_lock(&pasid_mutex);
 	svm = ioasid_find(INVALID_IOASID_SET, pasid, NULL);
 	if (!svm) {
drivers/iommu/iommu.c +2 -2
index 374b34f..57aac03 100644
@@ -1955,12 +1955,12 @@ int iommu_sva_bind_gpasid(struct iommu_domain *domain,
 EXPORT_SYMBOL_GPL(iommu_sva_bind_gpasid);
 
 int iommu_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
-			     ioasid_t pasid)
+			    struct iommu_gpasid_unbind_data *data)
 {
 	if (unlikely(!domain->ops->sva_unbind_gpasid))
 		return -ENODEV;
 
-	return domain->ops->sva_unbind_gpasid(dev, pasid);
+	return domain->ops->sva_unbind_gpasid(domain, dev, data);
 }
 EXPORT_SYMBOL_GPL(iommu_sva_unbind_gpasid);
 
include/linux/intel-iommu.h +2 -1
index 29d1c6f..0b238c3 100644
@@ -737,7 +737,8 @@ extern int intel_svm_finish_prq(struct intel_iommu *iommu);
 
 int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
 			  struct iommu_gpasid_bind_data *data);
-int intel_svm_unbind_gpasid(struct device *dev, int pasid);
+int intel_svm_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
+			    struct iommu_gpasid_unbind_data *data);
 struct iommu_sva *intel_svm_bind(struct device *dev, struct mm_struct *mm,
 				 void *drvdata);
 void intel_svm_unbind(struct iommu_sva *handle);
include/linux/iommu.h +5 -3
index 57c46ae..a19f063 100644
@@ -325,7 +325,8 @@ struct iommu_ops {
 	int (*sva_bind_gpasid)(struct iommu_domain *domain,
 			struct device *dev, struct iommu_gpasid_bind_data *data);
 
-	int (*sva_unbind_gpasid)(struct device *dev, int pasid);
+	int (*sva_unbind_gpasid)(struct iommu_domain *domain,
+		struct device *dev, struct iommu_gpasid_unbind_data *data);
 
 	int (*def_domain_type)(struct device *dev);
 
@@ -459,7 +460,7 @@ extern int iommu_cache_invalidate(struct iommu_domain *domain,
 extern int iommu_sva_bind_gpasid(struct iommu_domain *domain,
 		struct device *dev, struct iommu_gpasid_bind_data *data);
 extern int iommu_sva_unbind_gpasid(struct iommu_domain *domain,
-				struct device *dev, ioasid_t pasid);
+		struct device *dev, struct iommu_gpasid_unbind_data *data);
 extern struct iommu_domain *iommu_get_domain_for_dev(struct device *dev);
 extern struct iommu_domain *iommu_get_dma_domain(struct device *dev);
 extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
@@ -1084,7 +1085,8 @@ static inline int iommu_sva_bind_gpasid(struct iommu_domain *domain,
 }
 
 static inline int iommu_sva_unbind_gpasid(struct iommu_domain *domain,
-					   struct device *dev, int pasid)
+					  struct device *dev,
+					  struct iommu_gpasid_unbind_data *data)
 {
 	return -ENODEV;
 }
Signature · 1 line
2.7.4
author
#17 lore raw

PATCH v2 9/15 iommu/vt-d: Check ownership for PASIDs from user-space

In reply to Liu Yi L

When an IOMMU domain with nesting attribute is used for guest SVA, a system-wide PASID is allocated for binding with the device and the domain. For security reason, we need to check the PASID passsed from user-space. e.g. page table bind/unbind and PASID related cache invalidation.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>
---
 drivers/iommu/intel-iommu.c | 10 ++++++++++
 drivers/iommu/intel-svm.c   |  6 ++++--
 2 files changed, 14 insertions(+), 2 deletions(-)
drivers/iommu/intel-iommu.c +10 -0
index 2d59a5d..25650ac 100644
@@ -5433,6 +5433,7 @@ intel_iommu_sva_invalidate(struct iommu_domain *domain, struct device *dev,
 		int granu = 0;
 		u64 pasid = 0;
 		u64 addr = 0;
+		void *pdata;
 
 		granu = to_vtd_granularity(cache_type, inv_info->granularity);
 		if (granu == -EINVAL) {
@@ -5452,6 +5453,15 @@ intel_iommu_sva_invalidate(struct iommu_domain *domain, struct device *dev,
 			 (inv_info->addr_info.flags & IOMMU_INV_ADDR_FLAGS_PASID))
 			pasid = inv_info->addr_info.pasid;
 
+		pdata = ioasid_find(dmar_domain->ioasid_sid, pasid, NULL);
+		if (!pdata) {
+			ret = -EINVAL;
+			goto out_unlock;
+		} else if (IS_ERR(pdata)) {
+			ret = PTR_ERR(pdata);
+			goto out_unlock;
+		}
+
 		switch (BIT(cache_type)) {
 		case IOMMU_CACHE_INV_TYPE_IOTLB:
 			/* HW will ignore LSB bits based on address mask */
drivers/iommu/intel-svm.c +4 -2
index bf55e2f..49059c1 100644
@@ -332,7 +332,7 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
 	dmar_domain = to_dmar_domain(domain);
 
 	mutex_lock(&pasid_mutex);
-	svm = ioasid_find(INVALID_IOASID_SET, data->hpasid, NULL);
+	svm = ioasid_find(dmar_domain->ioasid_sid, data->hpasid, NULL);
 	if (IS_ERR(svm)) {
 		ret = PTR_ERR(svm);
 		goto out;
@@ -450,6 +450,7 @@ int intel_svm_unbind_gpasid(struct iommu_domain *domain,
 			    struct iommu_gpasid_unbind_data *data)
 {
 	struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
+	struct dmar_domain *dmar_domain;
 	struct intel_svm_dev *sdev;
 	struct intel_svm *svm;
 	int ret = -EINVAL;
@@ -464,9 +465,10 @@ int intel_svm_unbind_gpasid(struct iommu_domain *domain,
 		return -EINVAL;
 
 	pasid = data->pasid;
+	dmar_domain = to_dmar_domain(domain);
 
 	mutex_lock(&pasid_mutex);
-	svm = ioasid_find(INVALID_IOASID_SET, pasid, NULL);
+	svm = ioasid_find(dmar_domain->ioasid_sid, pasid, NULL);
 	if (!svm) {
 		ret = -EINVAL;
 		goto out;
Signature · 6 lines
2.7.4

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author
#18 lore raw

PATCH v2 10/15 vfio/type1: Support binding guest page tables to PASID

In reply to Liu Yi L

Nesting translation allows two-levels/stages page tables, with 1st level for guest translations (e.g. GVA->GPA), 2nd level for host translations (e.g. GPA->HPA). This patch adds interface for binding guest page tables to a PASID. This PASID must have been allocated to user space before the binding request.

CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>

---
v1 -> v2:
*) rename subject from "vfio/type1: Bind guest page tables to host" *) remove VFIO_IOMMU_BIND, introduce VFIO_IOMMU_NESTING_OP to support bind/
unbind guet page table
*) replaced vfio_iommu_for_each_dev() with a group level loop since this
series enforces one group per container w/ nesting type as start.
*) rename vfio_bind/unbind_gpasid_fn() to vfio_dev_bind/unbind_gpasid_fn() *) vfio_dev_unbind_gpasid() always successful
*) use vfio_mm->pasid_lock to avoid race between PASID free and page table
bind/unbind

Cc Kevin Tian <[email protected]>
 drivers/vfio/vfio_iommu_type1.c | 191 ++++++++++++++++++++++++++++++++++++++++
 drivers/vfio/vfio_pasid.c       |  30 +++++++
 include/linux/vfio.h            |  20 +++++
 include/uapi/linux/vfio.h       |  30 +++++++
 4 files changed, 271 insertions(+)
drivers/vfio/vfio_iommu_type1.c +191 -0
index a7b3a83..f1468a0 100644
@@ -130,6 +130,33 @@ struct vfio_regions {
 #define IS_DOMAIN_IN_CONTAINER(iommu)	((iommu->external_domain) || \
 					 (!list_empty(&iommu->domain_list)))
 
+struct domain_capsule {
+	struct vfio_group *group;
+	struct iommu_domain *domain;
+	void *data;
+};
+
+/* iommu->lock must be held */
+static struct vfio_group *vfio_find_nesting_group(struct vfio_iommu *iommu)
+{
+	struct vfio_domain *d;
+	struct vfio_group *g, *group = NULL;
+
+	if (!iommu->nesting_info)
+		return NULL;
+
+	/* only support singleton container with nesting type */
+	list_for_each_entry(d, &iommu->domain_list, next) {
+		list_for_each_entry(g, &d->group_list, next) {
+			if (!group) {
+				group = g;
+				break;
+			}
+		}
+	}
+	return group;
+}
+
 static int put_pfn(unsigned long pfn, int prot);
 
 /*
@@ -2014,6 +2041,39 @@ static int vfio_iommu_resv_refresh(struct vfio_iommu *iommu,
 	return ret;
 }
 
+static int vfio_dev_bind_gpasid_fn(struct device *dev, void *data)
+{
+	struct domain_capsule *dc = (struct domain_capsule *)data;
+	struct iommu_gpasid_bind_data *bind_data =
+		(struct iommu_gpasid_bind_data *) dc->data;
+
+	return iommu_sva_bind_gpasid(dc->domain, dev, bind_data);
+}
+
+static int vfio_dev_unbind_gpasid_fn(struct device *dev, void *data)
+{
+	struct domain_capsule *dc = (struct domain_capsule *)data;
+	struct iommu_gpasid_unbind_data *unbind_data =
+		(struct iommu_gpasid_unbind_data *) dc->data;
+
+	iommu_sva_unbind_gpasid(dc->domain, dev, unbind_data);
+	return 0;
+}
+
+static void vfio_group_unbind_gpasid_fn(ioasid_t pasid, void *data)
+{
+	struct domain_capsule *dc = (struct domain_capsule *) data;
+	struct iommu_gpasid_unbind_data unbind_data;
+
+	unbind_data.argsz = sizeof(unbind_data);
+	unbind_data.flags = 0;
+	unbind_data.pasid = pasid;
+
+	dc->data = &unbind_data;
+	iommu_group_for_each_dev(dc->group->iommu_group,
+				 dc, vfio_dev_unbind_gpasid_fn);
+}
+
 static void vfio_iommu_type1_detach_group(void *iommu_data,
 					  struct iommu_group *iommu_group)
 {
@@ -2055,6 +2115,21 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,
 		if (!group)
 			continue;
 
+		if (iommu->nesting_info && iommu->vmm &&
+		    (iommu->nesting_info->features &
+					IOMMU_NESTING_FEAT_BIND_PGTBL)) {
+			struct domain_capsule dc = { .group = group,
+						     .domain = domain->domain,
+						     .data = NULL };
+
+			/*
+			 * Unbind page tables bound with system wide PASIDs
+			 * which are allocated to user space.
+			 */
+			vfio_mm_for_each_pasid(iommu->vmm, &dc,
+					       vfio_group_unbind_gpasid_fn);
+		}
+
 		vfio_iommu_detach_group(domain, group);
 		list_del(&group->next);
 		kfree(group);
@@ -2453,6 +2528,120 @@ static int vfio_iommu_type1_pasid_request(struct vfio_iommu *iommu,
 	}
 }
 
+static long vfio_iommu_handle_pgtbl_op(struct vfio_iommu *iommu,
+				       bool is_bind, void *data)
+{
+	struct iommu_nesting_info *info;
+	struct domain_capsule dc = { .data = data };
+	struct vfio_group *group;
+	struct vfio_domain *domain;
+	int ret;
+
+	mutex_lock(&iommu->lock);
+
+	info = iommu->nesting_info;
+	if (!info || !(info->features & IOMMU_NESTING_FEAT_BIND_PGTBL)) {
+		ret = -ENOTSUPP;
+		goto out_unlock_iommu;
+	}
+
+	if (!iommu->vmm) {
+		ret = -EINVAL;
+		goto out_unlock_iommu;
+	}
+
+	group = vfio_find_nesting_group(iommu);
+	if (!group) {
+		ret = -EINVAL;
+		goto out_unlock_iommu;
+	}
+
+	domain = list_first_entry(&iommu->domain_list,
+				      struct vfio_domain, next);
+	dc.group = group;
+	dc.domain = domain->domain;
+
+	/* Avoid race with other containers within the same process */
+	vfio_mm_pasid_lock(iommu->vmm);
+
+	if (is_bind) {
+		ret = iommu_group_for_each_dev(group->iommu_group, &dc,
+					       vfio_dev_bind_gpasid_fn);
+		if (ret)
+			iommu_group_for_each_dev(group->iommu_group, &dc,
+						 vfio_dev_unbind_gpasid_fn);
+	} else {
+		iommu_group_for_each_dev(group->iommu_group,
+					 &dc, vfio_dev_unbind_gpasid_fn);
+		ret = 0;
+	}
+
+	vfio_mm_pasid_unlock(iommu->vmm);
+out_unlock_iommu:
+	mutex_unlock(&iommu->lock);
+	return ret;
+}
+
+static long vfio_iommu_type1_nesting_op(struct vfio_iommu *iommu,
+					unsigned long arg)
+{
+	struct vfio_iommu_type1_nesting_op hdr;
+	unsigned int minsz;
+	u8 *data = NULL;
+	size_t data_size;
+	int ret;
+
+	minsz = offsetofend(struct vfio_iommu_type1_nesting_op, flags);
+
+	if (copy_from_user(&hdr, (void __user *)arg, minsz))
+		return -EFAULT;
+
+	if (hdr.argsz < minsz || hdr.flags & ~VFIO_NESTING_OP_MASK)
+		return -EINVAL;
+
+	/* Get the current IOMMU UAPI data size */
+	switch (hdr.flags & VFIO_NESTING_OP_MASK) {
+	case VFIO_IOMMU_NESTING_OP_BIND_PGTBL:
+		data_size = sizeof(struct iommu_gpasid_bind_data);
+		break;
+	case VFIO_IOMMU_NESTING_OP_UNBIND_PGTBL:
+		data_size = sizeof(struct iommu_gpasid_unbind_data);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if ((hdr.argsz - minsz) > data_size) {
+		/* User data > current kernel */
+		return -E2BIG;
+	}
+
+	data = kzalloc(data_size, GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	if (copy_from_user(data, (void __user *)(arg + minsz),
+			   hdr.argsz - minsz)) {
+		ret = -EFAULT;
+		goto out_free;
+	}
+
+	switch (hdr.flags & VFIO_NESTING_OP_MASK) {
+	case VFIO_IOMMU_NESTING_OP_BIND_PGTBL:
+		ret = vfio_iommu_handle_pgtbl_op(iommu, true, data);
+		break;
+	case VFIO_IOMMU_NESTING_OP_UNBIND_PGTBL:
+		ret = vfio_iommu_handle_pgtbl_op(iommu, false, data);
+		break;
+	default:
+		ret = -EINVAL;
+	}
+
+out_free:
+	kfree(data);
+	return ret;
+}
+
 static long vfio_iommu_type1_ioctl(void *iommu_data,
 				   unsigned int cmd, unsigned long arg)
 {
@@ -2469,6 +2658,8 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,
 		return vfio_iommu_type1_unmap_dma(iommu, arg);
 	case VFIO_IOMMU_PASID_REQUEST:
 		return vfio_iommu_type1_pasid_request(iommu, arg);
+	case VFIO_IOMMU_NESTING_OP:
+		return vfio_iommu_type1_nesting_op(iommu, arg);
 	}
 
 	return -ENOTTY;
drivers/vfio/vfio_pasid.c +30 -0
index 2ea9f1a..20f1e72 100644
@@ -30,6 +30,7 @@ struct vfio_mm {
 	struct kref		kref;
 	struct vfio_mm_token	token;
 	int			ioasid_sid;
+	struct mutex		pasid_lock;
 	int			pasid_quota;
 	struct list_head	next;
 };
@@ -97,6 +98,7 @@ struct vfio_mm *vfio_mm_get_from_task(struct task_struct *task)
 	kref_init(&vmm->kref);
 	vmm->token.val = (unsigned long long) mm;
 	vmm->pasid_quota = pasid_quota;
+	mutex_init(&vmm->pasid_lock);
 
 	list_add(&vmm->next, &vfio_pasid.vfio_mm_list);
 out:
@@ -134,12 +136,40 @@ void vfio_pasid_free_range(struct vfio_mm *vmm,
 	 * IOASID core will notify PASID users (e.g. IOMMU driver) to
 	 * teardown necessary structures depending on the to-be-freed
 	 * PASID.
+	 * Hold pasid_lock to avoid race with PASID usages like bind/
+	 * unbind page tables to requested PASID.
 	 */
+	mutex_lock(&vmm->pasid_lock);
 	for (; pasid <= max; pasid++)
 		ioasid_free(pasid);
+	mutex_unlock(&vmm->pasid_lock);
 }
 EXPORT_SYMBOL_GPL(vfio_pasid_free_range);
 
+int vfio_mm_for_each_pasid(struct vfio_mm *vmm, void *data,
+			   void (*fn)(ioasid_t id, void *data))
+{
+	int ret;
+
+	mutex_lock(&vmm->pasid_lock);
+	ret = ioasid_set_for_each_ioasid(vmm->ioasid_sid, fn, data);
+	mutex_unlock(&vmm->pasid_lock);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(vfio_mm_for_each_pasid);
+
+void vfio_mm_pasid_lock(struct vfio_mm *vmm)
+{
+	mutex_lock(&vmm->pasid_lock);
+}
+EXPORT_SYMBOL_GPL(vfio_mm_pasid_lock);
+
+void vfio_mm_pasid_unlock(struct vfio_mm *vmm)
+{
+	mutex_unlock(&vmm->pasid_lock);
+}
+EXPORT_SYMBOL_GPL(vfio_mm_pasid_unlock);
+
 static int __init vfio_pasid_init(void)
 {
 	mutex_init(&vfio_pasid.vfio_mm_lock);
include/linux/vfio.h +20 -0
index 76da56e..66495de 100644
@@ -103,6 +103,11 @@ int vfio_mm_ioasid_sid(struct vfio_mm *vmm);
 extern int vfio_pasid_alloc(struct vfio_mm *vmm, int min, int max);
 extern void vfio_pasid_free_range(struct vfio_mm *vmm,
 					ioasid_t min, ioasid_t max);
+extern int vfio_mm_for_each_pasid(struct vfio_mm *vmm, void *data,
+				  void (*fn)(ioasid_t id, void *data));
+extern void vfio_mm_pasid_lock(struct vfio_mm *vmm);
+extern void vfio_mm_pasid_unlock(struct vfio_mm *vmm);
+
 #else
 static inline struct vfio_mm *vfio_mm_get_from_task(struct task_struct *task)
 {
@@ -127,6 +132,21 @@ static inline void vfio_pasid_free_range(struct vfio_mm *vmm,
 					  ioasid_t min, ioasid_t max)
 {
 }
+
+static inline int vfio_mm_for_each_pasid(struct vfio_mm *vmm, void *data,
+					 void (*fn)(ioasid_t id, void *data))
+{
+	return -ENOTTY;
+}
+
+static inline void vfio_mm_pasid_lock(struct vfio_mm *vmm)
+{
+}
+
+static inline void vfio_mm_pasid_unlock(struct vfio_mm *vmm)
+{
+}
+
 #endif /* CONFIG_VFIO_PASID */
 
 /*
include/uapi/linux/vfio.h +30 -0
index 24d1992..e4aa466 100644
@@ -876,6 +876,36 @@ struct vfio_iommu_type1_pasid_request {
 
 #define VFIO_IOMMU_PASID_REQUEST	_IO(VFIO_TYPE, VFIO_BASE + 22)
 
+/**
+ * VFIO_IOMMU_NESTING_OP - _IOW(VFIO_TYPE, VFIO_BASE + 23,
+ *				struct vfio_iommu_type1_nesting_op)
+ *
+ * This interface allows user space to utilize the nesting IOMMU
+ * capabilities as reported through VFIO_IOMMU_GET_INFO.
+ *
+ * @data[] types defined for each op:
+ * +=================+===============================================+
+ * | NESTING OP      |                  @data[]                      |
+ * +=================+===============================================+
+ * | BIND_PGTBL      |      struct iommu_gpasid_bind_data            |
+ * +-----------------+-----------------------------------------------+
+ * | UNBIND_PGTBL    |      struct iommu_gpasid_unbind_data          |
+ * +-----------------+-----------------------------------------------+
+ *
+ * returns: 0 on success, -errno on failure.
+ */
+struct vfio_iommu_type1_nesting_op {
+	__u32	argsz;
+	__u32	flags;
+#define VFIO_NESTING_OP_MASK	(0xffff) /* lower 16-bits for op */
+	__u8	data[];
+};
+
+#define VFIO_IOMMU_NESTING_OP_BIND_PGTBL	(0)
+#define VFIO_IOMMU_NESTING_OP_UNBIND_PGTBL	(1)
+
+#define VFIO_IOMMU_NESTING_OP		_IO(VFIO_TYPE, VFIO_BASE + 23)
+
 /* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */
 
 /*
Signature · 1 line
2.7.4
author
#19 lore raw

PATCH v2 11/15 vfio/type1: Allow invalidating first-level/stage IOMMU cache

In reply to Liu Yi L

This patch provides an interface allowing the userspace to invalidate IOMMU cache for first-level page table. It is required when the first level IOMMU page table is not managed by the host kernel in the nested translation setup.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>

---
v1 -> v2:
*) rename from "vfio/type1: Flush stage-1 IOMMU cache for nesting type" *) rename vfio_cache_inv_fn() to vfio_dev_cache_invalidate_fn() *) vfio_dev_cache_inv_fn() always successful
*) remove VFIO_IOMMU_CACHE_INVALIDATE, and reuse VFIO_IOMMU_NESTING_OP

 drivers/vfio/vfio_iommu_type1.c | 59 +++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/vfio.h       |  3 +++
 2 files changed, 62 insertions(+)
drivers/vfio/vfio_iommu_type1.c +59 -0
index f1468a0..c233c8e 100644
@@ -2582,6 +2582,54 @@ static long vfio_iommu_handle_pgtbl_op(struct vfio_iommu *iommu,
 	return ret;
 }
 
+static int vfio_dev_cache_invalidate_fn(struct device *dev, void *data)
+{
+	struct domain_capsule *dc = (struct domain_capsule *)data;
+	struct iommu_cache_invalidate_info *cache_info =
+		(struct iommu_cache_invalidate_info *) dc->data;
+
+	iommu_cache_invalidate(dc->domain, dev, cache_info);
+	return 0;
+}
+
+static long vfio_iommu_invalidate_cache(struct vfio_iommu *iommu,
+			struct iommu_cache_invalidate_info *cache_info)
+{
+	struct domain_capsule dc = { .data = cache_info };
+	struct vfio_group *group;
+	struct vfio_domain *domain;
+	int ret = 0;
+	struct iommu_nesting_info *info;
+
+	mutex_lock(&iommu->lock);
+	/*
+	 * Cache invalidation is required for any nesting IOMMU,
+	 * so no need to check system-wide PASID support.
+	 */
+	info = iommu->nesting_info;
+	if (!info || !(info->features & IOMMU_NESTING_FEAT_CACHE_INVLD)) {
+		ret = -ENOTSUPP;
+		goto out_unlock;
+	}
+
+	group = vfio_find_nesting_group(iommu);
+	if (!group) {
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+
+	domain = list_first_entry(&iommu->domain_list,
+				      struct vfio_domain, next);
+	dc.group = group;
+	dc.domain = domain->domain;
+	iommu_group_for_each_dev(group->iommu_group, &dc,
+				 vfio_dev_cache_invalidate_fn);
+
+out_unlock:
+	mutex_unlock(&iommu->lock);
+	return ret;
+}
+
 static long vfio_iommu_type1_nesting_op(struct vfio_iommu *iommu,
 					unsigned long arg)
 {
@@ -2607,6 +2655,9 @@ static long vfio_iommu_type1_nesting_op(struct vfio_iommu *iommu,
 	case VFIO_IOMMU_NESTING_OP_UNBIND_PGTBL:
 		data_size = sizeof(struct iommu_gpasid_unbind_data);
 		break;
+	case VFIO_IOMMU_NESTING_OP_CACHE_INVLD:
+		data_size = sizeof(struct iommu_cache_invalidate_info);
+		break;
 	default:
 		return -EINVAL;
 	}
@@ -2633,6 +2684,14 @@ static long vfio_iommu_type1_nesting_op(struct vfio_iommu *iommu,
 	case VFIO_IOMMU_NESTING_OP_UNBIND_PGTBL:
 		ret = vfio_iommu_handle_pgtbl_op(iommu, false, data);
 		break;
+	case VFIO_IOMMU_NESTING_OP_CACHE_INVLD:
+	{
+		struct iommu_cache_invalidate_info *cache_info =
+				(struct iommu_cache_invalidate_info *)data;
+
+		ret = vfio_iommu_invalidate_cache(iommu, cache_info);
+		break;
+	}
 	default:
 		ret = -EINVAL;
 	}
include/uapi/linux/vfio.h +3 -0
index e4aa466..9a011d6 100644
@@ -891,6 +891,8 @@ struct vfio_iommu_type1_pasid_request {
  * +-----------------+-----------------------------------------------+
  * | UNBIND_PGTBL    |      struct iommu_gpasid_unbind_data          |
  * +-----------------+-----------------------------------------------+
+ * | CACHE_INVLD     |      struct iommu_cache_invalidate_info       |
+ * +-----------------+-----------------------------------------------+
  *
  * returns: 0 on success, -errno on failure.
  */
@@ -903,6 +905,7 @@ struct vfio_iommu_type1_nesting_op {
 
 #define VFIO_IOMMU_NESTING_OP_BIND_PGTBL	(0)
 #define VFIO_IOMMU_NESTING_OP_UNBIND_PGTBL	(1)
+#define VFIO_IOMMU_NESTING_OP_CACHE_INVLD	(2)
 
 #define VFIO_IOMMU_NESTING_OP		_IO(VFIO_TYPE, VFIO_BASE + 23)
 
Signature · 1 line
2.7.4
author
#20 lore raw

PATCH v2 12/15 vfio/type1: Add vSVA support for IOMMU-backed mdevs

In reply to Liu Yi L

Recent years, mediated device pass-through framework (e.g. vfio-mdev) is used to achieve flexible device sharing across domains (e.g. VMs). Also there are hardware assisted mediated pass-through solutions from platform vendors. e.g. Intel VT-d scalable mode which supports Intel Scalable I/O Virtualization technology. Such mdevs are called IOMMU- backed mdevs as there are IOMMU enforced DMA isolation for such mdevs. In kernel, IOMMU-backed mdevs are exposed to IOMMU layer by aux-domain concept, which means mdevs are protected by an iommu domain which is auxiliary to the domain that the kernel driver primarily uses for DMA API. Details can be found in the KVM presentation as below:

https://events19.linuxfoundation.org/wp-content/uploads/2017/12/\
Hardware-Assisted-Mediated-Pass-Through-with-VFIO-Kevin-Tian-Intel.pdf

This patch extends NESTING_IOMMU ops to IOMMU-backed mdev devices. The main requirement is to use the auxiliary domain associated with mdev.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
CC Jun Tian <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>

---
v1 -> v2:
*) check the iommu_device to ensure the handling mdev is IOMMU-backed

 drivers/vfio/vfio_iommu_type1.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
drivers/vfio/vfio_iommu_type1.c +27 -3
index c233c8e..bcd7935 100644
@@ -2041,13 +2041,27 @@ static int vfio_iommu_resv_refresh(struct vfio_iommu *iommu,
 	return ret;
 }
 
+static struct device *vfio_get_iommu_device(struct vfio_group *group,
+					    struct device *dev)
+{
+	if (group->mdev_group)
+		return vfio_mdev_get_iommu_device(dev);
+	else
+		return dev;
+}
+
 static int vfio_dev_bind_gpasid_fn(struct device *dev, void *data)
 {
 	struct domain_capsule *dc = (struct domain_capsule *)data;
 	struct iommu_gpasid_bind_data *bind_data =
 		(struct iommu_gpasid_bind_data *) dc->data;
+	struct device *iommu_device;
+
+	iommu_device = vfio_get_iommu_device(dc->group, dev);
+	if (!iommu_device)
+		return -EINVAL;
 
-	return iommu_sva_bind_gpasid(dc->domain, dev, bind_data);
+	return iommu_sva_bind_gpasid(dc->domain, iommu_device, bind_data);
 }
 
 static int vfio_dev_unbind_gpasid_fn(struct device *dev, void *data)
@@ -2055,8 +2069,13 @@ static int vfio_dev_unbind_gpasid_fn(struct device *dev, void *data)
 	struct domain_capsule *dc = (struct domain_capsule *)data;
 	struct iommu_gpasid_unbind_data *unbind_data =
 		(struct iommu_gpasid_unbind_data *) dc->data;
+	struct device *iommu_device;
+
+	iommu_device = vfio_get_iommu_device(dc->group, dev);
+	if (!iommu_device)
+		return 0;
 
-	iommu_sva_unbind_gpasid(dc->domain, dev, unbind_data);
+	iommu_sva_unbind_gpasid(dc->domain, iommu_device, unbind_data);
 	return 0;
 }
 
@@ -2587,8 +2606,13 @@ static int vfio_dev_cache_invalidate_fn(struct device *dev, void *data)
 	struct domain_capsule *dc = (struct domain_capsule *)data;
 	struct iommu_cache_invalidate_info *cache_info =
 		(struct iommu_cache_invalidate_info *) dc->data;
+	struct device *iommu_device;
+
+	iommu_device = vfio_get_iommu_device(dc->group, dev);
+	if (!iommu_device)
+		return -EINVAL;
 
-	iommu_cache_invalidate(dc->domain, dev, cache_info);
+	iommu_cache_invalidate(dc->domain, iommu_device, cache_info);
 	return 0;
 }
 
Signature · 6 lines
2.7.4

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author
#21 lore raw

PATCH v2 13/15 vfio/pci: Expose PCIe PASID capability to guest

In reply to Liu Yi L

This patch exposes PCIe PASID capability to guest for assigned devices. Existing vfio_pci driver hides it from guest by setting the capability length as 0 in pci_ext_cap_length[].

And this patch only exposes PASID capability for devices which has PCIe PASID extended struture in its configuration space. So VFs, will will not see PASID capability on VFs as VF doesn't implement PASID extended structure in its configuration space. For VF, it is a TODO in future. Related discussion can be found in below link:

https://lkml.org/lkml/2020/4/7/693

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>

---
v1 -> v2:
*) added in v2, but it was sent in a separate patchseries before

 drivers/vfio/pci/vfio_pci_config.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vfio/pci/vfio_pci_config.c +1 -1
index 90c0b80..4b9af99 100644
@@ -95,7 +95,7 @@ static const u16 pci_ext_cap_length[PCI_EXT_CAP_ID_MAX + 1] = {
 	[PCI_EXT_CAP_ID_LTR]	=	PCI_EXT_CAP_LTR_SIZEOF,
 	[PCI_EXT_CAP_ID_SECPCI]	=	0,	/* not yet */
 	[PCI_EXT_CAP_ID_PMUX]	=	0,	/* not yet */
-	[PCI_EXT_CAP_ID_PASID]	=	0,	/* not yet */
+	[PCI_EXT_CAP_ID_PASID]	=	PCI_EXT_CAP_PASID_SIZEOF,
 };
 
 /*
Signature · 1 line
2.7.4
author
#22 lore raw

PATCH v2 14/15 vfio: Document dual stage control

In reply to Liu Yi L

From: Eric Auger <[email protected]>

The VFIO API was enhanced to support nested stage control: a bunch of new iotcls and usage guideline.

Let's document the process to follow to set up nested mode.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>

---
v1 -> v2:
*) new in v2, compared with Eric's original version, pasid table bind
and fault reporting is removed as this series doesn't cover them. Original version from Eric.
https://lkml.org/lkml/2020/3/20/700

 Documentation/driver-api/vfio.rst | 64 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
Documentation/driver-api/vfio.rst +64 -0
index f1a4d3c..06224bd 100644
@@ -239,6 +239,70 @@ group and can access them as follows::
 	/* Gratuitous device reset and go... */
 	ioctl(device, VFIO_DEVICE_RESET);
 
+IOMMU Dual Stage Control
+------------------------
+
+Some IOMMUs support 2 stages/levels of translation. Stage corresponds to
+the ARM terminology while level corresponds to Intel's VTD terminology.
+In the following text we use either without distinction.
+
+This is useful when the guest is exposed with a virtual IOMMU and some
+devices are assigned to the guest through VFIO. Then the guest OS can use
+stage 1 (GIOVA -> GPA or GVA->GPA), while the hypervisor uses stage 2 for
+VM isolation (GPA -> HPA).
+
+Under dual stage translation, the guest gets ownership of the stage 1 page
+tables and also owns stage 1 configuration structures. The hypervisor owns
+the root configuration structure (for security reason), including stage 2
+configuration. This works as long configuration structures and page table
+format are compatible between the virtual IOMMU and the physical IOMMU.
+
+Assuming the HW supports it, this nested mode is selected by choosing the
+VFIO_TYPE1_NESTING_IOMMU type through:
+
+    ioctl(container, VFIO_SET_IOMMU, VFIO_TYPE1_NESTING_IOMMU);
+
+This forces the hypervisor to use the stage 2, leaving stage 1 available
+for guest usage. The guest stage 1 format depends on IOMMU vendor, and
+it is the same with the nesting configuration method. User space should
+check the format and configuration method after setting nesting type by
+using:
+
+    ioctl(container->fd, VFIO_IOMMU_GET_INFO, &nesting_info);
+
+Details can be found in Documentation/userspace-api/iommu.rst. For Intel
+VT-d, each stage 1 page table is bound to host by:
+
+    nesting_op->flags = VFIO_IOMMU_NESTING_OP_BIND_PGTBL;
+    memcpy(&nesting_op->data, &bind_data, sizeof(bind_data));
+    ioctl(container->fd, VFIO_IOMMU_NESTING_OP, nesting_op);
+
+As mentioned above, guest OS may use stage 1 for GIOVA->GPA or GVA->GPA.
+GVA->GPA page tables are available when PASID (Process Address Space ID)
+is exposed to guest. e.g. guest with PASID-capable devices assigned. For
+such page table binding, the bind_data should include PASID info, which
+is allocated by guest itself or by host. This depends on hardware vendor
+e.g. Intel VT-d requires to allocate PASID from host. This requirement is
+available by VFIO_IOMMU_GET_INFO. User space could allocate PASID from
+host by:
+
+    req.flags = VFIO_IOMMU_ALLOC_PASID;
+    ioctl(container, VFIO_IOMMU_PASID_REQUEST, &req);
+
+With first stage/level page table bound to host, it allows to combine the
+guest stage 1 translation along with the hypervisor stage 2 translation to
+get final address.
+
+When the guest invalidates stage 1 related caches, invalidations must be
+forwarded to the host through
+
+    nesting_op->flags = VFIO_IOMMU_NESTING_OP_CACHE_INVLD;
+    memcpy(&nesting_op->data, &inv_data, sizeof(inv_data));
+    ioctl(container->fd, VFIO_IOMMU_NESTING_OP, nesting_op);
+
+Those invalidations can happen at various granularity levels, page, context,
+...
+
 VFIO User API
 -------------------------------------------------------------------------------
 
Signature · 6 lines
2.7.4

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#23 lore raw

Re: [PATCH v2 14/15] vfio: Document dual stage control

Quoted 40 lines · Liu Yi L

From: Eric Auger <[email protected]>

The VFIO API was enhanced to support nested stage control: a bunch of new iotcls and usage guideline.

Let's document the process to follow to set up nested mode.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>

---
v1 -> v2:
*) new in v2, compared with Eric's original version, pasid table bind
and fault reporting is removed as this series doesn't cover them. Original version from Eric.
https://lkml.org/lkml/2020/3/20/700

 Documentation/driver-api/vfio.rst | 64 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
Documentation/driver-api/vfio.rst +6 -0
index f1a4d3c..06224bd 100644
@@ -239,6 +239,70 @@ group and can access them as follows::
 	/* Gratuitous device reset and go... */
 	ioctl(device, VFIO_DEVICE_RESET);
 
+IOMMU Dual Stage Control
+------------------------
+
+Some IOMMUs support 2 stages/levels of translation. Stage corresponds to
+the ARM terminology while level corresponds to Intel's VTD terminology.
+In the following text we use either without distinction.

Documentation/driver-api/vfio.rst group and can access them as follows:: · line 257

Quoted 10 lines
Documentation/driver-api/vfio.rst +10 -0
+
+This is useful when the guest is exposed with a virtual IOMMU and some
+devices are assigned to the guest through VFIO. Then the guest OS can use
+stage 1 (GIOVA -> GPA or GVA->GPA), while the hypervisor uses stage 2 for
+VM isolation (GPA -> HPA).
+
+Under dual stage translation, the guest gets ownership of the stage 1 page
+tables and also owns stage 1 configuration structures. The hypervisor owns
+the root configuration structure (for security reason), including stage 2
+configuration. This works as long configuration structures and page table

s/as long configuration/as long as configuration/

Documentation/driver-api/vfio.rst group and can access them as follows:: · line 258

Quoted 1 line
Documentation/driver-api/vfio.rst +1 -0
+format are compatible between the virtual IOMMU and the physical IOMMU.

s/format/formats/

Quoted 22 lines
Documentation/driver-api/vfio.rst +22 -0
+
+Assuming the HW supports it, this nested mode is selected by choosing the
+VFIO_TYPE1_NESTING_IOMMU type through:
+
+    ioctl(container, VFIO_SET_IOMMU, VFIO_TYPE1_NESTING_IOMMU);
+
+This forces the hypervisor to use the stage 2, leaving stage 1 available
+for guest usage. The guest stage 1 format depends on IOMMU vendor, and
+it is the same with the nesting configuration method. User space should
+check the format and configuration method after setting nesting type by
+using:
+
+    ioctl(container->fd, VFIO_IOMMU_GET_INFO, &nesting_info);
+
+Details can be found in Documentation/userspace-api/iommu.rst. For Intel
+VT-d, each stage 1 page table is bound to host by:
+
+    nesting_op->flags = VFIO_IOMMU_NESTING_OP_BIND_PGTBL;
+    memcpy(&nesting_op->data, &bind_data, sizeof(bind_data));
+    ioctl(container->fd, VFIO_IOMMU_NESTING_OP, nesting_op);
+
+As mentioned above, guest OS may use stage 1 for GIOVA->GPA or GVA->GPA.

Documentation/driver-api/vfio.rst group and can access them as follows:: · line 290

Quoted 10 lines
Documentation/driver-api/vfio.rst +10 -0
+GVA->GPA page tables are available when PASID (Process Address Space ID)
+is exposed to guest. e.g. guest with PASID-capable devices assigned. For
+such page table binding, the bind_data should include PASID info, which
+is allocated by guest itself or by host. This depends on hardware vendor
+e.g. Intel VT-d requires to allocate PASID from host. This requirement is
+available by VFIO_IOMMU_GET_INFO. User space could allocate PASID from
+host by:
+
+    req.flags = VFIO_IOMMU_ALLOC_PASID;
+    ioctl(container, VFIO_IOMMU_PASID_REQUEST, &req);

It is not clear how the userspace application determines whether PASIDs must be allocated from the host via VFIO_IOMMU_PASID_REQUEST or if the guest itself can allocate PASIDs. The text mentions VFIO_IOMMU_GET_INFO but what exactly should the userspace application check?

author
#24 lore raw

RE: [PATCH v2 14/15] vfio: Document dual stage control

Quoted 51 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Monday, June 15, 2020 5:41 PM

Quoted 47 lines · Liu Yi L

From: Eric Auger <[email protected]>

The VFIO API was enhanced to support nested stage control: a bunch of new iotcls and usage guideline.

Let's document the process to follow to set up nested mode.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>

---
v1 -> v2:
*) new in v2, compared with Eric's original version, pasid table bind
and fault reporting is removed as this series doesn't cover them. Original version from Eric.
https://lkml.org/lkml/2020/3/20/700

 Documentation/driver-api/vfio.rst | 64
+++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
Documentation/driver-api/vfio.rst +11 -0
index f1a4d3c..06224bd 100644
@@ -239,6 +239,70 @@ group and can access them as follows::
 	/* Gratuitous device reset and go... */
 	ioctl(device, VFIO_DEVICE_RESET);
 
+IOMMU Dual Stage Control
+------------------------
+
+Some IOMMUs support 2 stages/levels of translation. Stage corresponds
+to the ARM terminology while level corresponds to Intel's VTD terminology.
+In the following text we use either without distinction.
+
+This is useful when the guest is exposed with a virtual IOMMU and
+some devices are assigned to the guest through VFIO. Then the guest
+OS can use stage 1 (GIOVA -> GPA or GVA->GPA), while the hypervisor
+uses stage 2 for VM isolation (GPA -> HPA).
Quoted 8 lines

Documentation/driver-api/vfio.rst group and can access them as follows:: · line 258

Quoted 6 lines
Documentation/driver-api/vfio.rst +6 -0
+
+Under dual stage translation, the guest gets ownership of the stage 1
+page tables and also owns stage 1 configuration structures. The
+hypervisor owns the root configuration structure (for security
+reason), including stage 2 configuration. This works as long
+configuration structures and page table

s/as long configuration/as long as configuration/

got it.

Quoted 4 lines

Documentation/driver-api/vfio.rst group and can access them as follows:: · line 258

Quoted 2 lines
Documentation/driver-api/vfio.rst +1 -0
+format are compatible between the virtual IOMMU and the physical IOMMU.

s/format/formats/

I see.

Quoted 31 lines
Quoted 31 lines
Documentation/driver-api/vfio.rst +31 -0
+
+Assuming the HW supports it, this nested mode is selected by choosing
+the VFIO_TYPE1_NESTING_IOMMU type through:
+
+    ioctl(container, VFIO_SET_IOMMU, VFIO_TYPE1_NESTING_IOMMU);
+
+This forces the hypervisor to use the stage 2, leaving stage 1
+available for guest usage. The guest stage 1 format depends on IOMMU
+vendor, and it is the same with the nesting configuration method.
+User space should check the format and configuration method after
+setting nesting type by
+using:
+
+    ioctl(container->fd, VFIO_IOMMU_GET_INFO, &nesting_info);
+
+Details can be found in Documentation/userspace-api/iommu.rst. For
+Intel VT-d, each stage 1 page table is bound to host by:
+
+    nesting_op->flags = VFIO_IOMMU_NESTING_OP_BIND_PGTBL;
+    memcpy(&nesting_op->data, &bind_data, sizeof(bind_data));
+    ioctl(container->fd, VFIO_IOMMU_NESTING_OP, nesting_op);
+
+As mentioned above, guest OS may use stage 1 for GIOVA->GPA or GVA->GPA.
+GVA->GPA page tables are available when PASID (Process Address Space
+GVA->ID)
+is exposed to guest. e.g. guest with PASID-capable devices assigned.
+For such page table binding, the bind_data should include PASID info,
+which is allocated by guest itself or by host. This depends on
+hardware vendor e.g. Intel VT-d requires to allocate PASID from host.
+This requirement is available by VFIO_IOMMU_GET_INFO. User space
+could allocate PASID from host by:
Quoted 8 lines

Documentation/driver-api/vfio.rst group and can access them as follows:: · line 290

Quoted 3 lines
Documentation/driver-api/vfio.rst +3 -0
+
+    req.flags = VFIO_IOMMU_ALLOC_PASID;
+    ioctl(container, VFIO_IOMMU_PASID_REQUEST, &req);

It is not clear how the userspace application determines whether PASIDs must be allocated from the host via VFIO_IOMMU_PASID_REQUEST or if the guest itself can allocate PASIDs. The text mentions VFIO_IOMMU_GET_INFO but what exactly should the userspace application check?

For VT-d, spec 3.0 introduced Virtual Cmd interface for PASID allocation, guest request PASID from host if it detects the interface. Application should check the IOMMU_NESTING_FEAT_SYSWIDE_PASID setting in the below info reported by VFIO_IOMMU_GET_INFO. And virtual VT-d should not report SVA related capabilities to guest if SYSWIDE_PASID is not supported by kernel.

+struct iommu_nesting_info {
+ __u32 size;
+ __u32 format;
+ __u32 features;
+#define IOMMU_NESTING_FEAT_SYSWIDE_PASID (1 << 0)
+#define IOMMU_NESTING_FEAT_BIND_PGTBL (1 << 1)
+#define IOMMU_NESTING_FEAT_CACHE_INVLD (1 << 2)
+ __u32 flags;
+ __u8 data[];
+};
https://lore.kernel.org/linux-iommu/[email protected]/

Regards,
Yi Liu

#25 lore raw

Re: [PATCH v2 14/15] vfio: Document dual stage control

Quoted 126 lines · Yi L
Quoted 51 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Monday, June 15, 2020 5:41 PM

Quoted 47 lines · Liu Yi L

From: Eric Auger <[email protected]>

The VFIO API was enhanced to support nested stage control: a bunch of new iotcls and usage guideline.

Let's document the process to follow to set up nested mode.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>

---
v1 -> v2:
*) new in v2, compared with Eric's original version, pasid table bind
and fault reporting is removed as this series doesn't cover them. Original version from Eric.
https://lkml.org/lkml/2020/3/20/700

 Documentation/driver-api/vfio.rst | 64
+++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
Documentation/driver-api/vfio.rst +11 -0
index f1a4d3c..06224bd 100644
@@ -239,6 +239,70 @@ group and can access them as follows::
 	/* Gratuitous device reset and go... */
 	ioctl(device, VFIO_DEVICE_RESET);
 
+IOMMU Dual Stage Control
+------------------------
+
+Some IOMMUs support 2 stages/levels of translation. Stage corresponds
+to the ARM terminology while level corresponds to Intel's VTD terminology.
+In the following text we use either without distinction.
+
+This is useful when the guest is exposed with a virtual IOMMU and
+some devices are assigned to the guest through VFIO. Then the guest
+OS can use stage 1 (GIOVA -> GPA or GVA->GPA), while the hypervisor
+uses stage 2 for VM isolation (GPA -> HPA).
Quoted 8 lines

Documentation/driver-api/vfio.rst group and can access them as follows:: · line 258

Quoted 6 lines
Documentation/driver-api/vfio.rst +6 -0
+
+Under dual stage translation, the guest gets ownership of the stage 1
+page tables and also owns stage 1 configuration structures. The
+hypervisor owns the root configuration structure (for security
+reason), including stage 2 configuration. This works as long
+configuration structures and page table

s/as long configuration/as long as configuration/

got it.

Quoted 4 lines

Documentation/driver-api/vfio.rst group and can access them as follows:: · line 258

Quoted 2 lines
Documentation/driver-api/vfio.rst +1 -0
+format are compatible between the virtual IOMMU and the physical IOMMU.

s/format/formats/

I see.

Quoted 31 lines
Quoted 31 lines
Documentation/driver-api/vfio.rst +31 -0
+
+Assuming the HW supports it, this nested mode is selected by choosing
+the VFIO_TYPE1_NESTING_IOMMU type through:
+
+    ioctl(container, VFIO_SET_IOMMU, VFIO_TYPE1_NESTING_IOMMU);
+
+This forces the hypervisor to use the stage 2, leaving stage 1
+available for guest usage. The guest stage 1 format depends on IOMMU
+vendor, and it is the same with the nesting configuration method.
+User space should check the format and configuration method after
+setting nesting type by
+using:
+
+    ioctl(container->fd, VFIO_IOMMU_GET_INFO, &nesting_info);
+
+Details can be found in Documentation/userspace-api/iommu.rst. For
+Intel VT-d, each stage 1 page table is bound to host by:
+
+    nesting_op->flags = VFIO_IOMMU_NESTING_OP_BIND_PGTBL;
+    memcpy(&nesting_op->data, &bind_data, sizeof(bind_data));
+    ioctl(container->fd, VFIO_IOMMU_NESTING_OP, nesting_op);
+
+As mentioned above, guest OS may use stage 1 for GIOVA->GPA or GVA->GPA.
+GVA->GPA page tables are available when PASID (Process Address Space
+GVA->ID)
+is exposed to guest. e.g. guest with PASID-capable devices assigned.
+For such page table binding, the bind_data should include PASID info,
+which is allocated by guest itself or by host. This depends on
+hardware vendor e.g. Intel VT-d requires to allocate PASID from host.
+This requirement is available by VFIO_IOMMU_GET_INFO. User space
+could allocate PASID from host by:
Quoted 8 lines

Documentation/driver-api/vfio.rst group and can access them as follows:: · line 290

Quoted 3 lines
Documentation/driver-api/vfio.rst +3 -0
+
+    req.flags = VFIO_IOMMU_ALLOC_PASID;
+    ioctl(container, VFIO_IOMMU_PASID_REQUEST, &req);

It is not clear how the userspace application determines whether PASIDs must be allocated from the host via VFIO_IOMMU_PASID_REQUEST or if the guest itself can allocate PASIDs. The text mentions VFIO_IOMMU_GET_INFO but what exactly should the userspace application check?

For VT-d, spec 3.0 introduced Virtual Cmd interface for PASID allocation, guest request PASID from host if it detects the interface. Application should check the IOMMU_NESTING_FEAT_SYSWIDE_PASID setting in the below info reported by VFIO_IOMMU_GET_INFO. And virtual VT-d should not report SVA related capabilities to guest if SYSWIDE_PASID is not supported by kernel.

(quoted diff) +10 -0
+struct iommu_nesting_info {
+	__u32	size;
+	__u32	format;
+	__u32	features;
+#define IOMMU_NESTING_FEAT_SYSWIDE_PASID	(1 << 0)
+#define IOMMU_NESTING_FEAT_BIND_PGTBL		(1 << 1)
+#define IOMMU_NESTING_FEAT_CACHE_INVLD		(1 << 2)
+	__u32	flags;
+	__u8	data[];
+};
Quoted 1 line

https://lore.kernel.org/linux-iommu/[email protected]/

I see. Is it possible to add this information into this patch or at least a reference so readers know where to find out exactly how to do this?

Stefan

author
#26 lore raw

RE: [PATCH v2 14/15] vfio: Document dual stage control

Quoted 134 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Monday, June 22, 2020 8:51 PM

Quoted 60 lines · Yi L
Quoted 51 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Monday, June 15, 2020 5:41 PM

Quoted 47 lines · Liu Yi L

From: Eric Auger <[email protected]>

The VFIO API was enhanced to support nested stage control: a bunch of new iotcls and usage guideline.

Let's document the process to follow to set up nested mode.

Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>

---
v1 -> v2:
*) new in v2, compared with Eric's original version, pasid table bind
and fault reporting is removed as this series doesn't cover them. Original version from Eric.
https://lkml.org/lkml/2020/3/20/700

 Documentation/driver-api/vfio.rst | 64
+++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
Documentation/driver-api/vfio.rst +11 -0
index f1a4d3c..06224bd 100644
@@ -239,6 +239,70 @@ group and can access them as follows::
 	/* Gratuitous device reset and go... */
 	ioctl(device, VFIO_DEVICE_RESET);
 
+IOMMU Dual Stage Control
+------------------------
+
+Some IOMMUs support 2 stages/levels of translation. Stage corresponds
+to the ARM terminology while level corresponds to Intel's VTD terminology.
+In the following text we use either without distinction.
+
+This is useful when the guest is exposed with a virtual IOMMU and
+some devices are assigned to the guest through VFIO. Then the guest
+OS can use stage 1 (GIOVA -> GPA or GVA->GPA), while the hypervisor
+uses stage 2 for VM isolation (GPA -> HPA).
Quoted 8 lines

Documentation/driver-api/vfio.rst group and can access them as follows:: · line 258

Quoted 6 lines
Documentation/driver-api/vfio.rst +6 -0
+
+Under dual stage translation, the guest gets ownership of the stage 1
+page tables and also owns stage 1 configuration structures. The
+hypervisor owns the root configuration structure (for security
+reason), including stage 2 configuration. This works as long
+configuration structures and page table

s/as long configuration/as long as configuration/

Quoted 46 lines

got it.

Quoted 4 lines

Documentation/driver-api/vfio.rst group and can access them as follows:: · line 258

Quoted 2 lines
Documentation/driver-api/vfio.rst +1 -0
+format are compatible between the virtual IOMMU and the physical IOMMU.

s/format/formats/

I see.

Quoted 37 lines
Quoted 24 lines
Documentation/driver-api/vfio.rst +24 -0
+
+Assuming the HW supports it, this nested mode is selected by choosing
+the VFIO_TYPE1_NESTING_IOMMU type through:
+
+    ioctl(container, VFIO_SET_IOMMU, VFIO_TYPE1_NESTING_IOMMU);
+
+This forces the hypervisor to use the stage 2, leaving stage 1
+available for guest usage. The guest stage 1 format depends on IOMMU
+vendor, and it is the same with the nesting configuration method.
+User space should check the format and configuration method after
+setting nesting type by
+using:
+
+    ioctl(container->fd, VFIO_IOMMU_GET_INFO, &nesting_info);
+
+Details can be found in Documentation/userspace-api/iommu.rst. For
+Intel VT-d, each stage 1 page table is bound to host by:
+
+    nesting_op->flags = VFIO_IOMMU_NESTING_OP_BIND_PGTBL;
+    memcpy(&nesting_op->data, &bind_data, sizeof(bind_data));
+    ioctl(container->fd, VFIO_IOMMU_NESTING_OP, nesting_op);
+
+As mentioned above, guest OS may use stage 1 for GIOVA->GPA or GVA->GPA.
+GVA->GPA page tables are available when PASID (Process Address Space

Documentation/driver-api/vfio.rst group and can access them as follows:: · line 290

Quoted 10 lines
Documentation/driver-api/vfio.rst +10 -0
+GVA->ID)
+is exposed to guest. e.g. guest with PASID-capable devices assigned.
+For such page table binding, the bind_data should include PASID info,
+which is allocated by guest itself or by host. This depends on
+hardware vendor e.g. Intel VT-d requires to allocate PASID from host.
+This requirement is available by VFIO_IOMMU_GET_INFO. User space
+could allocate PASID from host by:
+
+    req.flags = VFIO_IOMMU_ALLOC_PASID;
+    ioctl(container, VFIO_IOMMU_PASID_REQUEST, &req);

It is not clear how the userspace application determines whether PASIDs must be allocated from the host via VFIO_IOMMU_PASID_REQUEST or if the guest itself

can

Quoted 20 lines
Quoted 2 lines

allocate PASIDs. The text mentions VFIO_IOMMU_GET_INFO but what exactly should the userspace application check?

For VT-d, spec 3.0 introduced Virtual Cmd interface for PASID allocation, guest request PASID from host if it detects the interface. Application should check the IOMMU_NESTING_FEAT_SYSWIDE_PASID setting in the below info reported by VFIO_IOMMU_GET_INFO. And virtual VT-d should not report SVA related capabilities to guest if SYSWIDE_PASID is not supported by kernel.

(quoted diff) +10 -0
+struct iommu_nesting_info {
+	__u32	size;
+	__u32	format;
+	__u32	features;
+#define IOMMU_NESTING_FEAT_SYSWIDE_PASID	(1 << 0)
+#define IOMMU_NESTING_FEAT_BIND_PGTBL		(1 << 1)
+#define IOMMU_NESTING_FEAT_CACHE_INVLD		(1 << 2)
+	__u32	flags;
+	__u8	data[];
+};
Quoted 1 line

https://lore.kernel.org/linux-iommu/1591877734-66527-3-git-send-email-

[email protected]/

Quoted 3 lines

I see. Is it possible to add this information into this patch or at least a reference so readers know where to find out exactly how to do this?

oh, yes. this would help a lot. will add it.

Regards,
Yi Liu

Quoted 1 line

Stefan

author
#27 lore raw

PATCH v2 15/15 iommu/vt-d: Support reporting nesting capability info

In reply to Liu Yi L
Cc Kevin Tian <[email protected]>
CC Jacob Pan <[email protected]>
Cc Alex Williamson <[email protected]>
Cc Eric Auger <[email protected]>
Cc Jean-Philippe Brucker <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Lu Baolu <[email protected]>
---
 drivers/iommu/intel-iommu.c | 81 +++++++++++++++++++++++++++++++++++++++++++--
 include/linux/intel-iommu.h | 16 +++++++++
 2 files changed, 95 insertions(+), 2 deletions(-)
drivers/iommu/intel-iommu.c +79 -2
index 25650ac..5415dc7 100644
@@ -5655,12 +5655,16 @@ static inline bool iommu_pasid_support(void)
 static inline bool nested_mode_support(void)
 {
 	struct dmar_drhd_unit *drhd;
-	struct intel_iommu *iommu;
+	struct intel_iommu *iommu, *prev = NULL;
 	bool ret = true;
 
 	rcu_read_lock();
 	for_each_active_iommu(iommu, drhd) {
-		if (!sm_supported(iommu) || !ecap_nest(iommu->ecap)) {
+		if (!prev)
+			prev = iommu;
+		if (!sm_supported(iommu) || !ecap_nest(iommu->ecap) ||
+		    (VTD_CAP_MASK & (iommu->cap ^ prev->cap)) ||
+		    (VTD_ECAP_MASK & (iommu->ecap ^ prev->ecap))) {
 			ret = false;
 			break;
 		}
@@ -6069,11 +6073,84 @@ intel_iommu_domain_set_attr(struct iommu_domain *domain,
 	return ret;
 }
 
+static int intel_iommu_get_nesting_info(struct iommu_domain *domain,
+					struct iommu_nesting_info *info)
+{
+	struct dmar_domain *dmar_domain = to_dmar_domain(domain);
+	u64 cap = VTD_CAP_MASK, ecap = VTD_ECAP_MASK;
+	struct device_domain_info *domain_info;
+	struct iommu_nesting_info_vtd vtd;
+	unsigned long flags;
+	u32 size;
+
+	if ((domain->type != IOMMU_DOMAIN_UNMANAGED) ||
+	    !(dmar_domain->flags & DOMAIN_FLAG_NESTING_MODE))
+		return -ENODEV;
+
+	if (!info)
+		return -EINVAL;
+
+	size = sizeof(struct iommu_nesting_info) +
+		sizeof(struct iommu_nesting_info_vtd);
+	/*
+	 * if provided buffer size is not equal to the size, should
+	 * return 0 and also the expected buffer size to caller.
+	 */
+	if (info->size != size) {
+		info->size = size;
+		return 0;
+	}
+
+	spin_lock_irqsave(&device_domain_lock, flags);
+	/*
+	 * arbitrary select the first domain_info as all nesting
+	 * related capabilities should be consistent across iommu
+	 * units.
+	 */
+	domain_info = list_first_entry(&dmar_domain->devices,
+				      struct device_domain_info, link);
+	cap &= domain_info->iommu->cap;
+	ecap &= domain_info->iommu->ecap;
+	spin_unlock_irqrestore(&device_domain_lock, flags);
+
+	info->format = IOMMU_PASID_FORMAT_INTEL_VTD;
+	info->features = IOMMU_NESTING_FEAT_SYSWIDE_PASID |
+			 IOMMU_NESTING_FEAT_BIND_PGTBL |
+			 IOMMU_NESTING_FEAT_CACHE_INVLD;
+	vtd.flags = 0;
+	vtd.addr_width = dmar_domain->gaw;
+	vtd.pasid_bits = ilog2(intel_pasid_max_id);
+	vtd.cap_reg = cap;
+	vtd.cap_mask = VTD_CAP_MASK;
+	vtd.ecap_reg = ecap;
+	vtd.ecap_mask = VTD_ECAP_MASK;
+
+	memcpy(info->data, &vtd, sizeof(vtd));
+	return 0;
+}
+
+static int intel_iommu_domain_get_attr(struct iommu_domain *domain,
+				       enum iommu_attr attr, void *data)
+{
+	switch (attr) {
+	case DOMAIN_ATTR_NESTING_INFO:
+	{
+		struct iommu_nesting_info *info =
+				(struct iommu_nesting_info *) data;
+
+		return intel_iommu_get_nesting_info(domain, info);
+	}
+	default:
+		return -ENODEV;
+	}
+}
+
 const struct iommu_ops intel_iommu_ops = {
 	.capable		= intel_iommu_capable,
 	.domain_alloc		= intel_iommu_domain_alloc,
 	.domain_free		= intel_iommu_domain_free,
 	.domain_set_attr	= intel_iommu_domain_set_attr,
+	.domain_get_attr	= intel_iommu_domain_get_attr,
 	.attach_dev		= intel_iommu_attach_device,
 	.detach_dev		= intel_iommu_detach_device,
 	.aux_attach_dev		= intel_iommu_aux_attach_device,
include/linux/intel-iommu.h +16 -0
index 0b238c3..be48f4e 100644
@@ -196,6 +196,22 @@
 #define ecap_max_handle_mask(e) ((e >> 20) & 0xf)
 #define ecap_sc_support(e)	((e >> 7) & 0x1) /* Snooping Control */
 
+/* Nesting Support Capability Alignment */
+#define VTD_CAP_FL1GP		(1ULL << 56)
+#define VTD_CAP_FL5LP		(1ULL << 60)
+#define VTD_ECAP_PRS		(1ULL << 29)
+#define VTD_ECAP_ERS		(1ULL << 30)
+#define VTD_ECAP_SRS		(1ULL << 31)
+#define VTD_ECAP_EAFS		(1ULL << 34)
+#define VTD_ECAP_PASID		(1ULL << 40)
+
+/* Only capabilities marked in below MASKs are reported */
+#define VTD_CAP_MASK		(VTD_CAP_FL1GP | VTD_CAP_FL5LP)
+
+#define VTD_ECAP_MASK		(VTD_ECAP_PRS | VTD_ECAP_ERS | \
+				 VTD_ECAP_SRS | VTD_ECAP_EAFS | \
+				 VTD_ECAP_PASID)
+
 /* Virtual command interface capability */
 #define vccap_pasid(v)		(((v) & DMA_VCS_PAS)) /* PASID allocation */
 
Signature · 6 lines
2.7.4

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#28 lore raw
In reply to Liu Yi L
Quoted 37 lines · Liu Yi L

Shared Virtual Addressing (SVA), a.k.a, Shared Virtual Memory (SVM) on Intel platforms allows address space sharing between device DMA and applications. SVA can reduce programming complexity and enhance security.

This VFIO series is intended to expose SVA usage to VMs. i.e. Sharing guest application address space with passthru devices. This is called vSVA in this series. The whole vSVA enabling requires QEMU/VFIO/IOMMU changes. For IOMMU and QEMU changes, they are in separate series (listed in the "Related series").

The high-level architecture for SVA virtualization is as below, the key design of vSVA support is to utilize the dual-stage IOMMU translation ( also known as IOMMU nesting translation) capability in host IOMMU.

    .-------------.  .---------------------------.
    |   vIOMMU    |  | Guest process CR3, FL only|
    |             |  '---------------------------'

.----------------/
| PASID Entry |--- PASID cache flush -
'-------------' |
| | V
| | CR3 in GPA
'-------------'
Guest

------| Shadow |--------------------------|--------
      v        v                          v
Host
    .-------------.  .----------------------.
    |   pIOMMU    |  | Bind FL for GVA-GPA  |
    |             |  '----------------------'
    .----------------/  |
    | PASID Entry |     V (Nested xlate)
    '----------------\.------------------------------.
    |             |   |SL for GPA-HPA, default domain|
    |             |   '------------------------------'
    '-------------'
Quoted 3 lines

Where:
- FL = First level/stage one page tables
- SL = Second level/stage two page tables

Hi,
Looks like an interesting feature!

To check I understand this feature: can applications now pass virtual addresses to devices instead of translating to IOVAs?

If yes, can guest applications restrict the vSVA address space so the device only has access to certain regions?

On one hand replacing IOVA translation with virtual addresses simplifies the application programming model, but does it give up isolation if the device can now access all application memory?

Thanks,
Stefan

author
#29 lore raw
Quoted 45 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Monday, June 15, 2020 6:02 PM

Quoted 37 lines · Liu Yi L

Shared Virtual Addressing (SVA), a.k.a, Shared Virtual Memory (SVM) on Intel platforms allows address space sharing between device DMA and applications. SVA can reduce programming complexity and enhance security.

This VFIO series is intended to expose SVA usage to VMs. i.e. Sharing guest application address space with passthru devices. This is called vSVA in this series. The whole vSVA enabling requires QEMU/VFIO/IOMMU changes. For IOMMU and QEMU changes, they are in separate series (listed in the "Related series").

The high-level architecture for SVA virtualization is as below, the key design of vSVA support is to utilize the dual-stage IOMMU translation ( also known as IOMMU nesting translation) capability in host IOMMU.

    .-------------.  .---------------------------.
    |   vIOMMU    |  | Guest process CR3, FL only|
    |             |  '---------------------------'

.----------------/
| PASID Entry |--- PASID cache flush -
'-------------' |
| | V
| | CR3 in GPA
'-------------'
Guest

------| Shadow |--------------------------|--------
      v        v                          v
Host
    .-------------.  .----------------------.
    |   pIOMMU    |  | Bind FL for GVA-GPA  |
    |             |  '----------------------'
    .----------------/  |
    | PASID Entry |     V (Nested xlate)
    '----------------\.------------------------------.
    |             |   |SL for GPA-HPA, default domain|
    |             |   '------------------------------'
    '-------------'
Quoted 3 lines

Where:
- FL = First level/stage one page tables
- SL = Second level/stage two page tables

Quoted 2 lines

Hi,
Looks like an interesting feature!

thanks for the interest. Stefan :-)

Quoted 2 lines

To check I understand this feature: can applications now pass virtual addresses to devices instead of translating to IOVAs?

yes, application could pass virtual addresses to device directly. As long as the virtual address is mapped in cpu page table, then IOMMU would get it translated to physical address.

Quoted 2 lines

If yes, can guest applications restrict the vSVA address space so the device only has access to certain regions?

do you mean restrict the access of certain virtual address regions of guest application ? or certain guest memory? :-)

Quoted 3 lines

On one hand replacing IOVA translation with virtual addresses simplifies the application programming model, but does it give up isolation if the device can now access all application memory?

yeah, you are right, SVA simplifies application programming model. And today, we do allow access all application memory by SVA. this is also another benefit of SVA. e.g. say an accelerator gets a copy of data from a buffer written by cpu. If there is some other data which is directed by a pointer (a virtual address) within the data got from memory, accelerator could do another DMA to fetch it without cpu's involvement.

Regards,
Yi Liu

Quoted 2 lines

Thanks,
Stefan

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#30 lore raw
Quoted 57 lines · Yi L
Quoted 45 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Monday, June 15, 2020 6:02 PM

Quoted 37 lines · Liu Yi L

Shared Virtual Addressing (SVA), a.k.a, Shared Virtual Memory (SVM) on Intel platforms allows address space sharing between device DMA and applications. SVA can reduce programming complexity and enhance security.

This VFIO series is intended to expose SVA usage to VMs. i.e. Sharing guest application address space with passthru devices. This is called vSVA in this series. The whole vSVA enabling requires QEMU/VFIO/IOMMU changes. For IOMMU and QEMU changes, they are in separate series (listed in the "Related series").

The high-level architecture for SVA virtualization is as below, the key design of vSVA support is to utilize the dual-stage IOMMU translation ( also known as IOMMU nesting translation) capability in host IOMMU.

    .-------------.  .---------------------------.
    |   vIOMMU    |  | Guest process CR3, FL only|
    |             |  '---------------------------'

.----------------/
| PASID Entry |--- PASID cache flush -
'-------------' |
| | V
| | CR3 in GPA
'-------------'
Guest

------| Shadow |--------------------------|--------
      v        v                          v
Host
    .-------------.  .----------------------.
    |   pIOMMU    |  | Bind FL for GVA-GPA  |
    |             |  '----------------------'
    .----------------/  |
    | PASID Entry |     V (Nested xlate)
    '----------------\.------------------------------.
    |             |   |SL for GPA-HPA, default domain|
    |             |   '------------------------------'
    '-------------'
Quoted 3 lines

Where:
- FL = First level/stage one page tables
- SL = Second level/stage two page tables

Quoted 2 lines

Hi,
Looks like an interesting feature!

thanks for the interest. Stefan :-)

Quoted 2 lines

To check I understand this feature: can applications now pass virtual addresses to devices instead of translating to IOVAs?

yes, application could pass virtual addresses to device directly. As long as the virtual address is mapped in cpu page table, then IOMMU would get it translated to physical address.

Quoted 5 lines
Quoted 2 lines

If yes, can guest applications restrict the vSVA address space so the device only has access to certain regions?

do you mean restrict the access of certain virtual address regions of guest application ? or certain guest memory? :-)

Your reply below answered my question. I was wondering if applications can protect parts of their virtual memory space that should not be accessed by the device. It makes sense that there is a trade-off to simplify the programming model and performance might also be better if the application doesn't need to DMA map/unmap buffers frequently.

Stefan

#31 lore raw
In reply to Stefan Hajnoczi
Quoted 52 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Monday, June 15, 2020 6:02 PM

Quoted 3 lines · Liu Yi L

Shared Virtual Addressing (SVA), a.k.a, Shared Virtual Memory (SVM) on Intel platforms allows address space sharing between device DMA and applications. SVA can reduce programming complexity and enhance

security.

Quoted 34 lines

This VFIO series is intended to expose SVA usage to VMs. i.e. Sharing guest application address space with passthru devices. This is called vSVA in this series. The whole vSVA enabling requires QEMU/VFIO/IOMMU changes. For IOMMU and QEMU changes, they are in separate series (listed in the "Related series").

The high-level architecture for SVA virtualization is as below, the key design of vSVA support is to utilize the dual-stage IOMMU translation ( also known as IOMMU nesting translation) capability in host IOMMU.

    .-------------.  .---------------------------.
    |   vIOMMU    |  | Guest process CR3, FL only|
    |             |  '---------------------------'

.----------------/
| PASID Entry |--- PASID cache flush -
'-------------' |
| | V
| | CR3 in GPA
'-------------'
Guest

------| Shadow |--------------------------|--------
      v        v                          v
Host
    .-------------.  .----------------------.
    |   pIOMMU    |  | Bind FL for GVA-GPA  |
    |             |  '----------------------'
    .----------------/  |
    | PASID Entry |     V (Nested xlate)
    '----------------\.------------------------------.
    |             |   |SL for GPA-HPA, default domain|
    |             |   '------------------------------'
    '-------------'
Quoted 3 lines

Where:
- FL = First level/stage one page tables
- SL = Second level/stage two page tables

Hi,
Looks like an interesting feature!

To check I understand this feature: can applications now pass virtual addresses to devices instead of translating to IOVAs?

Quoted 7 lines

If yes, can guest applications restrict the vSVA address space so the device only has access to certain regions?

On one hand replacing IOVA translation with virtual addresses simplifies the application programming model, but does it give up isolation if the device can now access all application memory?

with SVA each application is allocated with a unique PASID to tag its virtual address space. The device that claims SVA support must guarantee that one application can only program the device to access its own virtual address space (i.e. all DMAs triggered by this application are tagged with the application's PASID, and are translated by IOMMU's PASID-granular page table). So, isolation is not sacrificed in SVA.

Thanks
Kevin
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#32 lore raw
Quoted 60 lines · Kevin
Quoted 52 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Monday, June 15, 2020 6:02 PM

Quoted 3 lines · Liu Yi L

Shared Virtual Addressing (SVA), a.k.a, Shared Virtual Memory (SVM) on Intel platforms allows address space sharing between device DMA and applications. SVA can reduce programming complexity and enhance

security.

Quoted 34 lines

This VFIO series is intended to expose SVA usage to VMs. i.e. Sharing guest application address space with passthru devices. This is called vSVA in this series. The whole vSVA enabling requires QEMU/VFIO/IOMMU changes. For IOMMU and QEMU changes, they are in separate series (listed in the "Related series").

The high-level architecture for SVA virtualization is as below, the key design of vSVA support is to utilize the dual-stage IOMMU translation ( also known as IOMMU nesting translation) capability in host IOMMU.

    .-------------.  .---------------------------.
    |   vIOMMU    |  | Guest process CR3, FL only|
    |             |  '---------------------------'

.----------------/
| PASID Entry |--- PASID cache flush -
'-------------' |
| | V
| | CR3 in GPA
'-------------'
Guest

------| Shadow |--------------------------|--------
      v        v                          v
Host
    .-------------.  .----------------------.
    |   pIOMMU    |  | Bind FL for GVA-GPA  |
    |             |  '----------------------'
    .----------------/  |
    | PASID Entry |     V (Nested xlate)
    '----------------\.------------------------------.
    |             |   |SL for GPA-HPA, default domain|
    |             |   '------------------------------'
    '-------------'
Quoted 3 lines

Where:
- FL = First level/stage one page tables
- SL = Second level/stage two page tables

Hi,
Looks like an interesting feature!

To check I understand this feature: can applications now pass virtual addresses to devices instead of translating to IOVAs?

Quoted 7 lines

If yes, can guest applications restrict the vSVA address space so the device only has access to certain regions?

On one hand replacing IOVA translation with virtual addresses simplifies the application programming model, but does it give up isolation if the device can now access all application memory?

Quoted 6 lines

with SVA each application is allocated with a unique PASID to tag its virtual address space. The device that claims SVA support must guarantee that one application can only program the device to access its own virtual address space (i.e. all DMAs triggered by this application are tagged with the application's PASID, and are translated by IOMMU's PASID-granular page table). So, isolation is not sacrificed in SVA.

Isolation between applications is preserved but there is no isolation between the device and the application itself. The application needs to trust the device.

Examples:

1. The device can snoop secret data from readable pages in the application's virtual memory space.

2. The device can gain arbitrary execution on the CPU by overwriting control flow addresses (e.g. function pointers, stack return addresses) in writable pages.

Stefan

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#33 lore raw
Quoted 4 lines · Stefan Hajnoczi

Isolation between applications is preserved but there is no isolation between the device and the application itself. The application needs to trust the device.

Quoted 8 lines

Examples:

1. The device can snoop secret data from readable pages in the application's virtual memory space.

2. The device can gain arbitrary execution on the CPU by overwriting control flow addresses (e.g. function pointers, stack return addresses) in writable pages.

To me, SVA seems to be that "middle layer" of secure where it's not as safe as VFIO_IOMMU_MAP_DMA which has buffer level granularity of control (but of course we pay overhead on buffer setups and on-the-fly translations), however it's far better than DMA with no IOMMU which can ruin the whole host/guest, because after all we do a lot of isolations as process based.

IMHO it's the same as when we see a VM (or the QEMU process) as a whole along with the guest code. In some cases we don't care if the guest did some bad things to mess up with its own QEMU process. It is still ideal if we can even stop the guest from doing so, but when it's not easy to do it the ideal way, we just lower the requirement to not spread the influence to the host and other VMs.

Thanks,

Signature · 1 line
Peter Xu
#34 lore raw
Quoted 20 lines · Peter Xu
Quoted 4 lines · Stefan Hajnoczi

Isolation between applications is preserved but there is no isolation between the device and the application itself. The application needs to trust the device.

Quoted 8 lines

Examples:

1. The device can snoop secret data from readable pages in the application's virtual memory space.

2. The device can gain arbitrary execution on the CPU by overwriting control flow addresses (e.g. function pointers, stack return addresses) in writable pages.

To me, SVA seems to be that "middle layer" of secure where it's not as safe as VFIO_IOMMU_MAP_DMA which has buffer level granularity of control (but of course we pay overhead on buffer setups and on-the-fly translations), however it's far better than DMA with no IOMMU which can ruin the whole host/guest, because after all we do a lot of isolations as process based.

Quoted 6 lines

IMHO it's the same as when we see a VM (or the QEMU process) as a whole along with the guest code. In some cases we don't care if the guest did some bad things to mess up with its own QEMU process. It is still ideal if we can even stop the guest from doing so, but when it's not easy to do it the ideal way, we just lower the requirement to not spread the influence to the host and other VMs.

Makes sense.

Stefan

#35 lore raw
In reply to Stefan Hajnoczi
Quoted 68 lines · Stefan Hajnoczi
Quoted 60 lines · Kevin
Quoted 52 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Monday, June 15, 2020 6:02 PM

Quoted 3 lines · Liu Yi L

Shared Virtual Addressing (SVA), a.k.a, Shared Virtual Memory (SVM) on Intel platforms allows address space sharing between device DMA and applications. SVA can reduce programming complexity and enhance

security.

Quoted 34 lines

This VFIO series is intended to expose SVA usage to VMs. i.e. Sharing guest application address space with passthru devices. This is called vSVA in this series. The whole vSVA enabling requires QEMU/VFIO/IOMMU changes. For IOMMU and QEMU changes, they are in separate series (listed in the "Related series").

The high-level architecture for SVA virtualization is as below, the key design of vSVA support is to utilize the dual-stage IOMMU translation ( also known as IOMMU nesting translation) capability in host IOMMU.

    .-------------.  .---------------------------.
    |   vIOMMU    |  | Guest process CR3, FL only|
    |             |  '---------------------------'

.----------------/
| PASID Entry |--- PASID cache flush -
'-------------' |
| | V
| | CR3 in GPA
'-------------'
Guest

------| Shadow |--------------------------|--------
      v        v                          v
Host
    .-------------.  .----------------------.
    |   pIOMMU    |  | Bind FL for GVA-GPA  |
    |             |  '----------------------'
    .----------------/  |
    | PASID Entry |     V (Nested xlate)
    '----------------\.------------------------------.
    |             |   |SL for GPA-HPA, default domain|
    |             |   '------------------------------'
    '-------------'
Quoted 3 lines

Where:
- FL = First level/stage one page tables
- SL = Second level/stage two page tables

Hi,
Looks like an interesting feature!

To check I understand this feature: can applications now pass virtual addresses to devices instead of translating to IOVAs?

Quoted 7 lines

If yes, can guest applications restrict the vSVA address space so the device only has access to certain regions?

On one hand replacing IOVA translation with virtual addresses simplifies the application programming model, but does it give up isolation if the device can now access all application memory?

Quoted 6 lines

with SVA each application is allocated with a unique PASID to tag its virtual address space. The device that claims SVA support must guarantee that one application can only program the device to access its own virtual address space (i.e. all DMAs triggered by this application are tagged with the application's PASID, and are translated by IOMMU's PASID-granular page table). So, isolation is not sacrificed in SVA.

Quoted 3 lines

Isolation between applications is preserved but there is no isolation between the device and the application itself. The application needs to trust the device.

Right. With all convenience comes security trust. With SVA there is an expectation that the device has the required security boundaries properly implemented. FWIW, what is our guarantee today that VF's are secure from one another or even its own PF? They can also generate transactions with any of its peer id's and there is nothing an IOMMU can do today. Other than rely on ACS. Even BusMaster enable can be ignored and devices (malicious or otherwise) can generate after the BM=0. With SVM you get the benefits of

* Not having to register regions
* Don't need to pin application space for DMA.

Quoted 5 lines

Examples:

1. The device can snoop secret data from readable pages in the application's virtual memory space.

Aren't there other security technologies that can address this?

Quoted 4 lines

2. The device can gain arbitrary execution on the CPU by overwriting control flow addresses (e.g. function pointers, stack return addresses) in writable pages.

I suppose technology like CET might be able to guard. The general expectation is code pages and anything that needs to be protected should be mapped nor writable.

Cheers,
Ashok
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#36 lore raw
Quoted 81 lines · Ashok
Quoted 68 lines · Stefan Hajnoczi
Quoted 60 lines · Kevin
Quoted 52 lines

From: Stefan Hajnoczi <[email protected]>
Sent: Monday, June 15, 2020 6:02 PM

Quoted 3 lines · Liu Yi L

Shared Virtual Addressing (SVA), a.k.a, Shared Virtual Memory (SVM) on Intel platforms allows address space sharing between device DMA and applications. SVA can reduce programming complexity and enhance

security.

Quoted 34 lines

This VFIO series is intended to expose SVA usage to VMs. i.e. Sharing guest application address space with passthru devices. This is called vSVA in this series. The whole vSVA enabling requires QEMU/VFIO/IOMMU changes. For IOMMU and QEMU changes, they are in separate series (listed in the "Related series").

The high-level architecture for SVA virtualization is as below, the key design of vSVA support is to utilize the dual-stage IOMMU translation ( also known as IOMMU nesting translation) capability in host IOMMU.

    .-------------.  .---------------------------.
    |   vIOMMU    |  | Guest process CR3, FL only|
    |             |  '---------------------------'

.----------------/
| PASID Entry |--- PASID cache flush -
'-------------' |
| | V
| | CR3 in GPA
'-------------'
Guest

------| Shadow |--------------------------|--------
      v        v                          v
Host
    .-------------.  .----------------------.
    |   pIOMMU    |  | Bind FL for GVA-GPA  |
    |             |  '----------------------'
    .----------------/  |
    | PASID Entry |     V (Nested xlate)
    '----------------\.------------------------------.
    |             |   |SL for GPA-HPA, default domain|
    |             |   '------------------------------'
    '-------------'
Quoted 3 lines

Where:
- FL = First level/stage one page tables
- SL = Second level/stage two page tables

Hi,
Looks like an interesting feature!

To check I understand this feature: can applications now pass virtual addresses to devices instead of translating to IOVAs?

Quoted 7 lines

If yes, can guest applications restrict the vSVA address space so the device only has access to certain regions?

On one hand replacing IOVA translation with virtual addresses simplifies the application programming model, but does it give up isolation if the device can now access all application memory?

Quoted 6 lines

with SVA each application is allocated with a unique PASID to tag its virtual address space. The device that claims SVA support must guarantee that one application can only program the device to access its own virtual address space (i.e. all DMAs triggered by this application are tagged with the application's PASID, and are translated by IOMMU's PASID-granular page table). So, isolation is not sacrificed in SVA.

Quoted 3 lines

Isolation between applications is preserved but there is no isolation between the device and the application itself. The application needs to trust the device.

Right. With all convenience comes security trust. With SVA there is an expectation that the device has the required security boundaries properly implemented. FWIW, what is our guarantee today that VF's are secure from one another or even its own PF? They can also generate transactions with any of its peer id's and there is nothing an IOMMU can do today. Other than rely on ACS. Even BusMaster enable can be ignored and devices (malicious or otherwise) can generate after the BM=0. With SVM you get the benefits of

Quoted 2 lines

* Not having to register regions
* Don't need to pin application space for DMA.

As along as the security model is clearly documented users can decide whether or not SVA meets their requirements. I just wanted to clarify what the security model is.

Quoted 8 lines
Quoted 6 lines

Examples:

1. The device can snoop secret data from readable pages in the application's virtual memory space.

Aren't there other security technologies that can address this?

Maybe the IOMMU could enforce Memory Protection Keys? Imagine each device is assigned a subset of memory protection keys and the IOMMU checks them on each device access. This would allow the application to mark certain pages off-limits to the device but the IOMMU could still walk the full process page table (no need to construct a special device page table for the IOMMU).

Quoted 8 lines
Quoted 4 lines

2. The device can gain arbitrary execution on the CPU by overwriting control flow addresses (e.g. function pointers, stack return addresses) in writable pages.

I suppose technology like CET might be able to guard. The general expectation is code pages and anything that needs to be protected should be mapped nor writable.

Function pointers are a common exception to this. They are often located in writable heap or stack pages.

There might also be dynamic linker memory structures that are easy to hijack.

Stefan