Skip to contentSkip to conversation

Support for nesting IOMMUs in VFIO

PATCH Applied Will Deacon 2 participants 4 messages Sep 25, 2014 – Sep 30, 2014 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v5 cover Support for nesting IOMMUs in VFIO

Hello,

This is version five of the patches I previously posted here:

RFCv1: http://permalink.gmane.org/gmane.linux.kernel.iommu/5552
RFCv2: http://permalink.gmane.org/gmane.linux.kernel.iommu/5700

     v3: http://permalink.gmane.org/gmane.linux.kernel.iommu/6230
     v4: http://permalink.gmane.org/gmane.linux.kernel.iommu/6590

Changes since v4 are:

- Move temporary attr variable into local scope (suggested by Alex)
- Fix memory leak of iommu on open failure
- Added Joerg's ack to the second patch
- Dropped the arm-smmu patch remove the series, as I'll push this separately

Thanks,

Will

Will Deacon (2):
iommu: introduce domain attribute for nesting IOMMUs vfio/iommu_type1: add new VFIO_TYPE1_NESTING_IOMMU IOMMU type

 drivers/vfio/vfio_iommu_type1.c | 30 +++++++++++++++++++++++++-----
 include/linux/iommu.h           |  1 +
 include/uapi/linux/vfio.h       |  3 +++
 3 files changed, 29 insertions(+), 5 deletions(-)
Signature · 1 line
2.1.0

A message that is not in the archive (sent off-list or not yet received): 1411667101-26197-1-git-send-email-will.deacon-5wv7dgnIgG8@public.gmane.org

#2 lore raw

PATCH v5 1/2 iommu: introduce domain attribute for nesting IOMMUs

Some IOMMUs, such as the ARM SMMU, support two stages of translation. The idea behind such a scheme is to allow a guest operating system to use the IOMMU for DMA mappings in the first stage of translation, with the hypervisor then installing mappings in the second stage to provide isolation of the DMA to the physical range assigned to that virtual machine.

In order to allow IOMMU domains to be used for second-stage translation, this patch adds a new iommu_attr (IOMMU_ATTR_NESTING) for setting second-stage domains prior to device attach. The attribute can also be queried to see if a domain is actually making use of nesting.

---
 include/linux/iommu.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/iommu.h +1 -0
index 20f9a527922a..7b02bcc85b9e 100644
@@ -80,6 +80,7 @@ enum iommu_attr {
 	DOMAIN_ATTR_FSL_PAMU_STASH,
 	DOMAIN_ATTR_FSL_PAMU_ENABLE,
 	DOMAIN_ATTR_FSL_PAMUV1,
+	DOMAIN_ATTR_NESTING,	/* two stages of translation */
 	DOMAIN_ATTR_MAX,
 };
 
Signature · 1 line
2.1.0
#3 lore raw

PATCH v5 2/2 vfio/iommu_type1: add new VFIO_TYPE1_NESTING_IOMMU IOMMU type

In reply to a message not in the archive

VFIO allows devices to be safely handed off to userspace by putting them behind an IOMMU configured to ensure DMA and interrupt isolation. This enables userspace KVM clients, such as kvmtool and qemu, to further map the device into a virtual machine.

With IOMMUs such as the ARM SMMU, it is then possible to provide SMMU translation services to the guest operating system, which are nested with the existing translation installed by VFIO. However, enabling this feature means that the IOMMU driver must be informed that the VFIO domain is being created for the purposes of nested translation.

This patch adds a new IOMMU type (VFIO_TYPE1_NESTING_IOMMU) to the VFIO type-1 driver. The new IOMMU type acts identically to the VFIO_TYPE1v2_IOMMU type, but additionally sets the DOMAIN_ATTR_NESTING attribute on its IOMMU domains.

Cc Joerg Roedel <[email protected]>
Cc Alex Williamson <[email protected]>
---
 drivers/vfio/vfio_iommu_type1.c | 30 +++++++++++++++++++++++++-----
 include/uapi/linux/vfio.h       |  3 +++
 2 files changed, 28 insertions(+), 5 deletions(-)
drivers/vfio/vfio_iommu_type1.c +25 -5
index 0734fbe5b651..583ccdb2c58f 100644
@@ -57,7 +57,8 @@ struct vfio_iommu {
 	struct list_head	domain_list;
 	struct mutex		lock;
 	struct rb_root		dma_list;
-	bool v2;
+	bool			v2;
+	bool			nesting;
 };
 
 struct vfio_domain {
@@ -705,6 +706,15 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
 		goto out_free;
 	}
 
+	if (iommu->nesting) {
+		int attr = 1;
+
+		ret = iommu_domain_set_attr(domain->domain, DOMAIN_ATTR_NESTING,
+					    &attr);
+		if (ret)
+			goto out_domain;
+	}
+
 	ret = iommu_attach_group(domain->domain, iommu_group);
 	if (ret)
 		goto out_domain;
@@ -819,17 +829,26 @@ static void *vfio_iommu_type1_open(unsigned long arg)
 {
 	struct vfio_iommu *iommu;
 
-	if (arg != VFIO_TYPE1_IOMMU && arg != VFIO_TYPE1v2_IOMMU)
-		return ERR_PTR(-EINVAL);
-
 	iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
 	if (!iommu)
 		return ERR_PTR(-ENOMEM);
 
+	switch (arg) {
+	case VFIO_TYPE1_IOMMU:
+		break;
+	case VFIO_TYPE1_NESTING_IOMMU:
+		iommu->nesting = true;
+	case VFIO_TYPE1v2_IOMMU:
+		iommu->v2 = true;
+		break;
+	default:
+		kfree(iommu);
+		return ERR_PTR(-EINVAL);
+	}
+
 	INIT_LIST_HEAD(&iommu->domain_list);
 	iommu->dma_list = RB_ROOT;
 	mutex_init(&iommu->lock);
-	iommu->v2 = (arg == VFIO_TYPE1v2_IOMMU);
 
 	return iommu;
 }
@@ -885,6 +904,7 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,
 		switch (arg) {
 		case VFIO_TYPE1_IOMMU:
 		case VFIO_TYPE1v2_IOMMU:
+		case VFIO_TYPE1_NESTING_IOMMU:
 			return 1;
 		case VFIO_DMA_CC_IOMMU:
 			if (!iommu)
include/uapi/linux/vfio.h +3 -0
index 6612974c64bf..29715d27548f 100644
@@ -33,6 +33,9 @@
 /* Check if EEH is supported */
 #define VFIO_EEH			5
 
+/* Two-stage IOMMU */
+#define VFIO_TYPE1_NESTING_IOMMU	6	/* Implies v2 */
+
 /*
  * The IOCTL interface is designed for extensibility by embedding the
  * structure length (argsz) and flags into structures passed between
Signature · 1 line
2.1.0
#4 lore raw
In reply to a message not in the archive
Quoted 22 lines · Will Deacon

Hello,

This is version five of the patches I previously posted here:

RFCv1: http://permalink.gmane.org/gmane.linux.kernel.iommu/5552
RFCv2: http://permalink.gmane.org/gmane.linux.kernel.iommu/5700

     v3: http://permalink.gmane.org/gmane.linux.kernel.iommu/6230
     v4: http://permalink.gmane.org/gmane.linux.kernel.iommu/6590

Changes since v4 are:

- Move temporary attr variable into local scope (suggested by Alex)
- Fix memory leak of iommu on open failure
- Added Joerg's ack to the second patch
- Dropped the arm-smmu patch remove the series, as I'll push this separately

Thanks,

Will

Quoted 9 lines

Will Deacon (2):
iommu: introduce domain attribute for nesting IOMMUs vfio/iommu_type1: add new VFIO_TYPE1_NESTING_IOMMU IOMMU type

 drivers/vfio/vfio_iommu_type1.c | 30 +++++++++++++++++++++++++-----
 include/linux/iommu.h           |  1 +
 include/uapi/linux/vfio.h       |  3 +++
 3 files changed, 29 insertions(+), 5 deletions(-)

Applied to linux-vfio.git/next for 3.18. Thanks!

Alex