Support for nesting IOMMUs in VFIO
Subsystems (2)
v4 cover Support for nesting IOMMUs in VFIO
Hi all,
This is version four of the patches I originally 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
The main change since v3 is the mass simplification of the type1 IOMMU code based on discussions with Alex. We now unconditionally advertise support for nesting IOMMUs and postpone failure until group attach time.
Alex -- any chance you could queue the first two patches please? I'll deal with the arm-smmu patch for 3.19.
Feedback welcome,
Will
Will Deacon (3):
iommu: introduce domain attribute for nesting IOMMUs vfio/iommu_type1: add new VFIO_TYPE1_NESTING_IOMMU IOMMU type iommu/arm-smmu: add support for DOMAIN_ATTR_NESTING attribute
drivers/iommu/arm-smmu.c | 110 ++++++++++++++++++++++++++++++++-------- drivers/vfio/vfio_iommu_type1.c | 29 ++++++++--- include/linux/iommu.h | 1 + include/uapi/linux/vfio.h | 3 ++ 4 files changed, 117 insertions(+), 26 deletions(-)
Signature · 1 line
2.1.0
A message that is not in the archive (sent off-list or not yet received): 1411550485-32483-1-git-send-email-will.deacon-5wv7dgnIgG8@public.gmane.org
PATCH v4 1/3 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
@@ -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
A message that is not in the archive (sent off-list or not yet received): 1411550485-32483-2-git-send-email-will.deacon-5wv7dgnIgG8@public.gmane.org
Joerg Roedel
Acked-byRe: [PATCH v4 1/3] iommu: introduce domain attribute for nesting IOMMUs
Quoted 7 lines · Will Deacon
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.
Quoted 8 lines
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.Cc Joerg Roedel <[email protected]>Cc Alex Williamson <[email protected]>Signed-off-by Will Deacon <[email protected]>
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH v4 1/3] iommu: introduce domain attribute for nesting IOMMUs
Quoted 13 lines · Joerg Roedel
Quoted 12 lines · Will Deacon
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.
Quoted 5 lines
Quoted 3 lines
Cc Joerg Roedel <[email protected]>Cc Alex Williamson <[email protected]>Signed-off-by Will Deacon <[email protected]>Acked-by Joerg Roedel <[email protected]>
Thanks, Joerg.
Will
PATCH v4 2/3 vfio/iommu_type1: add new VFIO_TYPE1_NESTING_IOMMU IOMMU type
In reply to a message not in the archiveVFIO 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.
--- drivers/vfio/vfio_iommu_type1.c | 29 +++++++++++++++++++++++------ include/uapi/linux/vfio.h | 3 +++ 2 files changed, 26 insertions(+), 6 deletions(-)
drivers/vfio/vfio_iommu_type1.c +23 -6
@@ -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 {
@@ -671,7 +672,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,struct vfio_group *group, *g;struct vfio_domain *domain, *d;struct bus_type *bus =NULL ; -int ret; +int ret, attr =1 ;mutex_lock (&iommu->lock);
@@ -705,6 +706,13 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, goto out_free; } +if (iommu->nesting) { + 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 +827,25 @@ 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 : +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 +901,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
@@ -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
A message that is not in the archive (sent off-list or not yet received): 1411550485-32483-3-git-send-email-will.deacon-5wv7dgnIgG8@public.gmane.org
Re: [PATCH v4 2/3] vfio/iommu_type1: add new VFIO_TYPE1_NESTING_IOMMU IOMMU type
Quoted 48 lines · Will Deacon
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 theVFIO_TYPE1v2_IOMMUtype, but additionally sets theDOMAIN_ATTR_NESTINGattribute on its IOMMU domains.Cc Joerg Roedel <[email protected]>Cc Alex Williamson <[email protected]>Signed-off-by Will Deacon <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 29 +++++++++++++++++++++++------ include/uapi/linux/vfio.h | 3 +++ 2 files changed, 26 insertions(+), 6 deletions(-)drivers/vfio/vfio_iommu_type1.c +3
-2@@ -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 {@@ -671,7 +672,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,struct vfio_group *group, *g;struct vfio_domain *domain, *d;struct bus_type *bus =NULL ; -int ret; +int ret, attr =1 ;mutex_lock (&iommu->lock);
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 709
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +1
-0@@ -705,6 +706,13 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, goto out_free; } +if (iommu->nesting) {
nit, attr declaration could be moved here
Quoted 19 lines
drivers/vfio/vfio_iommu_type1.c +6
-3+ 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 +827,25 @@ 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 );
drivers/vfio/vfio_iommu_type1.c static void *vfio_iommu_type1_open(unsigned long arg) · line 842
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +9
-0+switch (arg) { +case VFIO_TYPE1_IOMMU : +break ; +case VFIO_TYPE1_NESTING_IOMMU : + iommu->nesting = true; +case VFIO_TYPE1v2_IOMMU: + iommu->v2 = true; +break ; +default :
Leaks iommu
Quoted 32 lines
drivers/vfio/vfio_iommu_type1.c +4
-1+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 +901,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@@ -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
A message that is not in the archive (sent off-list or not yet received): 1411582999.24563.110.camel-85EaTFmN5p//[email protected]
Re: [PATCH v4 2/3] vfio/iommu_type1: add new VFIO_TYPE1_NESTING_IOMMU IOMMU type
Hi Alex,
Quoted 16 lines · Alex Williamson
Quoted 15 lines · Will Deacon
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 theVFIO_TYPE1v2_IOMMUtype, but additionally sets theDOMAIN_ATTR_NESTINGattribute on its IOMMU domains.
[...]
Quoted 23 lines
Quoted 23 lines
drivers/vfio/vfio_iommu_type1.c +3
-2@@ -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 {@@ -671,7 +672,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,struct vfio_group *group, *g;struct vfio_domain *domain, *d;struct bus_type *bus =NULL ; -int ret; +int ret, attr =1 ;mutex_lock (&iommu->lock);
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 709
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +1
-0@@ -705,6 +706,13 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, goto out_free; } +if (iommu->nesting) {nit, attr declaration could be moved here
Sure.
Quoted 20 lines
Quoted 20 lines
drivers/vfio/vfio_iommu_type1.c +6
-3+ 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 +827,25 @@ 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 );
Quoted 11 lines
drivers/vfio/vfio_iommu_type1.c static void *vfio_iommu_type1_open(unsigned long arg) · line 842
Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c +9
-0+switch (arg) { +case VFIO_TYPE1_IOMMU : +break ; +case VFIO_TYPE1_NESTING_IOMMU : + iommu->nesting = true; +case VFIO_TYPE1v2_IOMMU: + iommu->v2 = true; +break ; +default :Leaks iommu
Yikes, sorry about that. Will fix.
Will
PATCH v4 3/3 iommu/arm-smmu: add support for DOMAIN_ATTR_NESTING attribute
In reply to a message not in the archiveWhen domains are set with the DOMAIN_ATTR_NESTING flag, we must ensure that we allocate them to stage-2 context banks if the hardware permits it.
This patch adds support for the attribute to the ARM SMMU driver, with the actual stage being determined depending on the features supported by the hardware.
--- drivers/iommu/arm-smmu.c | 110 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 90 insertions(+), 20 deletions(-)
drivers/iommu/arm-smmu.c +90 -20
@@ -404,9 +404,16 @@ struct arm_smmu_cfg {#define ARM_SMMU_CB_ASID (cfg) ((cfg)->cbndx)#define ARM_SMMU_CB_VMID (cfg) ((cfg)->cbndx + 1) +enum arm_smmu_domain_stage { +ARM_SMMU_DOMAIN_S1 =0 , +ARM_SMMU_DOMAIN_S2 , +ARM_SMMU_DOMAIN_NESTED , +}; +struct arm_smmu_domain {struct arm_smmu_device *smmu;struct arm_smmu_cfg cfg; +enum arm_smmu_domain_stage stage;spinlock_t lock; };
@@ -906,19 +913,46 @@ static int arm_smmu_init_domain_context(struct iommu_domain *domain,if (smmu_domain->smmu) goto out_unlock; -if (smmu->features &ARM_SMMU_FEAT_TRANS_NESTED ) { +/* +* Mapping the requested stage onto what we support is surprisingly +* complicated, mainly because the spec allows S1+S2 SMMUs without +* support for nested translation. That means we end up with the +* following table: +* +* Requested Supported Actual +* S1 N S1 +* S1 S1+S2 S1 +* S1 S2 S2 +* S1 S1 S1 +* N N N +* N S1+S2 S2 +* N S2 S2 +* N S1 S1 +* +* Note that you can't actually request stage-2 mappings. +*/ +if (!(smmu->features &ARM_SMMU_FEAT_TRANS_S1 )) + smmu_domain->stage =ARM_SMMU_DOMAIN_S2 ; +if (!(smmu->features &ARM_SMMU_FEAT_TRANS_S2 )) + smmu_domain->stage =ARM_SMMU_DOMAIN_S1 ; + +switch (smmu_domain->stage) { +case ARM_SMMU_DOMAIN_S1 : + cfg->cbar =CBAR_TYPE_S1_TRANS_S2_BYPASS ; + start = smmu->num_s2_context_banks; +break ; +case ARM_SMMU_DOMAIN_NESTED :/* * We will likely want to change this if/when KVM gets * involved. */ - cfg->cbar =CBAR_TYPE_S1_TRANS_S2_BYPASS ; - start = smmu->num_s2_context_banks; - }else if (smmu->features &ARM_SMMU_FEAT_TRANS_S1 ) { - cfg->cbar =CBAR_TYPE_S1_TRANS_S2_BYPASS ; - start = smmu->num_s2_context_banks; - }else { +case ARM_SMMU_DOMAIN_S2 : cfg->cbar =CBAR_TYPE_S2_TRANS ; start =0 ; +break ; +default : + ret = -EINVAL ; + goto out_unlock; } ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
@@ -1645,20 +1679,56 @@ static void arm_smmu_remove_device(struct device *dev)iommu_group_remove_device (dev); } +static int arm_smmu_domain_get_attr (struct iommu_domain *domain, +enum iommu_attr attr,void *data) +{ +struct arm_smmu_domain *smmu_domain = domain->priv; + +switch (attr) { +case DOMAIN_ATTR_NESTING : + *(int *)data = (smmu_domain->stage ==ARM_SMMU_DOMAIN_NESTED ); +return 0 ; +default : +return -ENODEV ; + } +} + +static int arm_smmu_domain_set_attr (struct iommu_domain *domain, +enum iommu_attr attr,void *data) +{ +struct arm_smmu_domain *smmu_domain = domain->priv; + +switch (attr) { +case DOMAIN_ATTR_NESTING : +if (smmu_domain->smmu) +return -EPERM ; +if (*(int *)data) + smmu_domain->stage =ARM_SMMU_DOMAIN_NESTED ; +else + smmu_domain->stage =ARM_SMMU_DOMAIN_S1 ; + +return 0 ; +default : +return -ENODEV ; + } +} +static const struct iommu_ops arm_smmu_ops = { - .domain_init = arm_smmu_domain_init, - .domain_destroy = arm_smmu_domain_destroy, - .attach_dev = arm_smmu_attach_dev, - .detach_dev = arm_smmu_detach_dev, - .map = arm_smmu_map, - .unmap = arm_smmu_unmap, - .iova_to_phys = arm_smmu_iova_to_phys, - .domain_has_cap = arm_smmu_domain_has_cap, - .add_device = arm_smmu_add_device, - .remove_device = arm_smmu_remove_device, - .pgsize_bitmap = (SECTION_SIZE | -ARM_SMMU_PTE_CONT_SIZE | -PAGE_SIZE ), + .domain_init = arm_smmu_domain_init, + .domain_destroy = arm_smmu_domain_destroy, + .attach_dev = arm_smmu_attach_dev, + .detach_dev = arm_smmu_detach_dev, + .map = arm_smmu_map, + .unmap = arm_smmu_unmap, + .iova_to_phys = arm_smmu_iova_to_phys, + .domain_has_cap = arm_smmu_domain_has_cap, + .add_device = arm_smmu_add_device, + .remove_device = arm_smmu_remove_device, + .domain_get_attr = arm_smmu_domain_get_attr, + .domain_set_attr = arm_smmu_domain_set_attr, + .pgsize_bitmap = (SECTION_SIZE | +ARM_SMMU_PTE_CONT_SIZE | +PAGE_SIZE ), };static void arm_smmu_device_reset (struct arm_smmu_device *smmu)
Signature · 1 line
2.1.0
Quoted 6 lines · Will Deacon
Hi all,
This is version four of the patches I originally posted here:
RFCv1: http://permalink.gmane.org/gmane.linux.kernel.iommu/5552
RFCv2: http://permalink.gmane.org/gmane.linux.kernel.iommu/5700
Quoted 8 lines
v3: http://permalink.gmane.org/gmane.linux.kernel.iommu/6230The main change since v3 is the mass simplification of the type1 IOMMU code based on discussions with Alex. We now unconditionally advertise support for nesting IOMMUs and postpone failure until group attach time.
Alex -- any chance you could queue the first two patches please? I'll deal with the arm-smmu patch for 3.19.
I'd need an ack from Joerg to pull in the first patch through vfio. Thanks,
Alex