vfio: support Shared Virtual Addressing
Subsystems (2)
v2 cover vfio: support Shared Virtual Addressing
Shared virtual address (SVA), a.k.a, Shared virtual memory (SVM) on Intel platforms allow address space sharing between device DMA and applications. SVA can reduce programming complexity and enhance security.
This series is intended to expose SVA capability to VMs. i.e. shared guest application address space with passthru devices. The whole SVA virtualization requires QEMU/VFIO/IOMMU changes. This series includes the VFIO changes, for QEMU and IOMMU changes, they are in separate series (listed in the "Related series").
The high-level architecture for SVA virtualization is as below:
.-------------. .---------------------------.
| 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
There are roughly three parts in this patchset which are corresponding to the basic vSVA support for PCI device assignment
1. vfio support for PASID allocation and free from VMs
2. vfio support for guest PASID binding from VMs
3. vfio support for IOMMU cache invalidation from VMs
The complete vSVA upstream patches are divided into three phases:
1. Common APIs and PCI device direct assignment
2. Page Request Services (PRS) support
3. Mediated device assignment
This RFC patchset is aiming for the phase 1, and works together with the VT-d driver[1] changes and QEMU changes[2]. Complete set for vSVA can be found in:
https://github.com/jacobpan/linux.git:siov_sva.
And this patchset doesn't include the patch to expose PASID capability to guest. This is expected to be in another series.
Related series:
[1] [PATCH v6 00/10] Nested Shared Virtual Address (SVA) VT-d support:
https://lkml.org/lkml/2019/10/22/953
<This series is based on this kernel series from Jacob Pan>
[2] [RFC v2 00/20] intel_iommu: expose Shared Virtual Addressing to VM from Yi Liu
Changelog:
- RFC v1 -> v2:
Dropped vfio: VFIO_IOMMU_ATTACH/DETACH_PASID_TABLE. RFC v1: https://patchwork.kernel.org/cover/11033699/
Liu Yi L (3):
vfio: VFIO_IOMMU_CACHE_INVALIDATE
vfio/type1: VFIO_IOMMU_PASID_REQUEST(alloc/free) vfio/type1: bind guest pasid (guest page tables) to host
drivers/vfio/vfio_iommu_type1.c | 305 ++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 82 +++++++++++ 2 files changed, 387 insertions(+)
Signature · 6 lines
2.7.4 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
RFC v2 1/3 vfio: VFIO_IOMMU_CACHE_INVALIDATE
From: Liu Yi L <[email protected]>
When the guest "owns" the stage 1 translation structures, the host IOMMU driver has no knowledge of caching structure updates unless the guest invalidation requests are trapped and passed down to the host.
This patch adds the VFIO_IOMMU_CACHE_INVALIDATE ioctl with aims at propagating guest stage1 IOMMU cache invalidations to the host.
--- drivers/vfio/vfio_iommu_type1.c | 55 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 13 ++++++++++ 2 files changed, 68 insertions(+)
drivers/vfio/vfio_iommu_type1.c +55 -0
@@ -124,6 +124,34 @@ struct vfio_regions {#define IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu) \ (!list_empty(&iommu->domain_list)) +struct domain_capsule { +struct iommu_domain *domain; +void *data; +}; + +/* iommu->lock must be held */ +static int +vfio_iommu_lookup_dev (struct vfio_iommu *iommu, +int (*fn)(struct device *dev,void *data), +void *data) +{ +struct domain_capsule dc = {.data = data}; +struct vfio_domain *d; +struct vfio_group *g; +int ret =0 ; + +list_for_each_entry (d, &iommu->domain_list, next) { + dc.domain = d->domain; +list_for_each_entry (g, &d->group_list, next) { + ret =iommu_group_for_each_dev (g->iommu_group, + &dc, fn); +if (ret) +break ; + } + } +return ret; +} +static int put_pfn (unsigned long pfn,int prot);/*
@@ -2211,6 +2239,15 @@ static int vfio_iommu_iova_build_caps(struct vfio_iommu *iommu,return ret; } +static int vfio_cache_inv_fn (struct device *dev,void *data) +{ +struct domain_capsule *dc = (struct domain_capsule *)data; +struct vfio_iommu_type1_cache_invalidate *ustruct = + (struct vfio_iommu_type1_cache_invalidate *)dc->data; + +return iommu_cache_invalidate (dc->domain, dev, &ustruct->info); +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg) {
@@ -2315,6 +2352,24 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,return copy_to_user ((void __user *)arg, &unmap, minsz) ? -EFAULT :0 ; + }else if (cmd ==VFIO_IOMMU_CACHE_INVALIDATE ) { +struct vfio_iommu_type1_cache_invalidate ustruct; +int ret; + + minsz = offsetofend(struct vfio_iommu_type1_cache_invalidate, + info); + +if (copy_from_user (&ustruct, (void __user *)arg, minsz)) +return -EFAULT ; + +if (ustruct.argsz < minsz || ustruct.flags) +return -EINVAL ; + +mutex_lock (&iommu->lock); + ret =vfio_iommu_lookup_dev (iommu, vfio_cache_inv_fn, + &ustruct); +mutex_unlock (&iommu->lock); +return ret; }return -ENOTTY ;
include/uapi/linux/vfio.h +13 -0
@@ -794,6 +794,19 @@ 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_CACHE_INVALIDATE - _IOWR(VFIO_TYPE, VFIO_BASE + 24, +* struct vfio_iommu_type1_cache_invalidate) +* +* Propagate guest IOMMU cache invalidation to the host. +*/ +struct vfio_iommu_type1_cache_invalidate { +__u32 argsz; +__u32 flags; +struct iommu_cache_invalidate_info info; +}; +#define VFIO_IOMMU_CACHE_INVALIDATE _IO(VFIO_TYPE, VFIO_BASE + 24) +/* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */ /*
Signature · 1 line
2.7.4
RE: [RFC v2 1/3] vfio: VFIO_IOMMU_CACHE_INVALIDATE
Quoted 32 lines
From: Liu, Yi L
Sent: Thursday, October 24, 2019 8:26 PMFrom: Liu Yi L <[email protected]>
When the guest "owns" the stage 1 translation structures, the host IOMMU driver has no knowledge of caching structure updates unless the guest invalidation requests are trapped and passed down to the host.
This patch adds the
VFIO_IOMMU_CACHE_INVALIDATEioctl with aims at propagating guest stage1 IOMMU cache invalidations to the host.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Eric Auger <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 55 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 13 ++++++++++ 2 files changed, 68 insertions(+)drivers/vfio/vfio_iommu_type1.c +0
-0@@ -124,6 +124,34 @@ struct vfio_regions {#define IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu) \ (!list_empty(&iommu->domain_list))
drivers/vfio/vfio_iommu_type1.c struct vfio_regions { · line 136
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+struct domain_capsule { +struct iommu_domain *domain; +void *data; +}; + +/* iommu->lock must be held */ +static int +vfio_iommu_lookup_dev (struct vfio_iommu *iommu, +int (*fn)(struct device *dev,void *data), +void *data)
'lookup' usually means find a device and then return. But the real purpose here is to loop all the devices within this container and then do something. Does it make more sense to be vfio_iommu_for_each_dev?
Quoted 40 lines
drivers/vfio/vfio_iommu_type1.c +18
-0+{ +struct domain_capsule dc = {.data = data}; +struct vfio_domain *d; +struct vfio_group *g; +int ret =0 ; + +list_for_each_entry (d, &iommu->domain_list, next) { + dc.domain = d->domain; +list_for_each_entry (g, &d->group_list, next) { + ret =iommu_group_for_each_dev (g- >iommu_group, + &dc, fn); +if (ret) +break ; + } + } +return ret; +} +static int put_pfn (unsigned long pfn,int prot);/*
drivers/vfio/vfio_iommu_type1.c +9
-0@@ -2211,6 +2239,15 @@ static int vfio_iommu_iova_build_caps(structvfio_iommu *iommu, return ret; } +static int vfio_cache_inv_fn (struct device *dev,void *data) +{ +struct domain_capsule *dc = (struct domain_capsule *)data; +struct vfio_iommu_type1_cache_invalidate *ustruct = + (struct vfio_iommu_type1_cache_invalidate *)dc->data; + +return iommu_cache_invalidate (dc->domain, dev, &ustruct->info); +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg){
drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void · line 2357
Quoted 6 lines
drivers/vfio/vfio_iommu_type1.c +2
-0@@ -2315,6 +2352,24 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, return copy_to_user((void __user *)arg, &unmap, minsz) ? -EFAULT :0 ; + }else if (cmd ==VFIO_IOMMU_CACHE_INVALIDATE ) { +struct vfio_iommu_type1_cache_invalidate ustruct;
it's weird to call a variable as struct.
Quoted 25 lines
drivers/vfio/vfio_iommu_type1.c +16
-0+int ret; + + minsz = offsetofend(struct vfio_iommu_type1_cache_invalidate, + info); + +if (copy_from_user (&ustruct, (void __user *)arg, minsz)) +return -EFAULT ; + +if (ustruct.argsz < minsz || ustruct.flags) +return -EINVAL ; + +mutex_lock (&iommu->lock); + ret =vfio_iommu_lookup_dev (iommu, vfio_cache_inv_fn, + &ustruct); +mutex_unlock (&iommu->lock); +return ret; }drivers/vfio/vfio_iommu_type1.c +0
-0return -ENOTTY ;include/uapi/linux/vfio.h +0
-0
include/uapi/linux/vfio.h struct vfio_iommu_type1_dma_unmap { · line 802
Quoted 9 lines
include/uapi/linux/vfio.h +5
-0@@ -794,6 +794,19 @@ 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_CACHE_INVALIDATE - _IOWR(VFIO_TYPE, VFIO_BASE + 24, +* struct vfio_iommu_type1_cache_invalidate) +* +* Propagate guest IOMMU cache invalidation to the host.
guest or first-level/stage-1? Ideally userspace application may also bind its own address space as stage-1 one day...
Quoted 15 lines
include/uapi/linux/vfio.h +9
-0+*/ +struct vfio_iommu_type1_cache_invalidate { +__u32 argsz; +__u32 flags; +struct iommu_cache_invalidate_info info; +}; +#define VFIO_IOMMU_CACHE_INVALIDATE _IO(VFIO_TYPE, VFIO_BASE + 24) +/* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */
/*
--
2.7.4
RE: [RFC v2 1/3] vfio: VFIO_IOMMU_CACHE_INVALIDATE
Hi Kevin,
Quoted 35 lines
From: Tian, Kevin
Sent: Friday, October 25, 2019 5:14 PM
To: Liu, Yi L <[email protected]>; [email protected];
Subject: RE: [RFC v2 1/3] vfio:VFIO_IOMMU_CACHE_INVALIDATEQuoted 30 lines
From: Liu, Yi L
Sent: Thursday, October 24, 2019 8:26 PMFrom: Liu Yi L <[email protected]>
When the guest "owns" the stage 1 translation structures, the host IOMMU driver has no knowledge of caching structure updates unless the guest invalidation requests are trapped and passed down to the host.
This patch adds the
VFIO_IOMMU_CACHE_INVALIDATEioctl with aims at propagating guest stage1 IOMMU cache invalidations to the host.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Eric Auger <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 55 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 13 ++++++++++ 2 files changed, 68 insertions(+)(quoted diff) +0
-0
b/drivers/vfio/vfio_iommu_type1.cindex 96fddc1d..cd8d3a5 100644drivers/vfio/vfio_iommu_type1.c +0
-0@@ -124,6 +124,34 @@ struct vfio_regions {#define IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu) \ (!list_empty(&iommu->domain_list))
Quoted 14 lines
drivers/vfio/vfio_iommu_type1.c struct vfio_regions { · line 136
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+struct domain_capsule { +struct iommu_domain *domain; +void *data; +}; + +/* iommu->lock must be held */ +static int +vfio_iommu_lookup_dev (struct vfio_iommu *iommu, +int (*fn)(struct device *dev,void *data), +void *data)'lookup' usually means find a device and then return. But the real purpose here is to loop all the devices within this container and then do something. Does it make more sense to be
vfio_iommu_for_each_dev?
yep, I can replace it.
Quoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +3
-0+{ +struct domain_capsule dc = {.data = data}; +struct vfio_domain *d;
[...]
Quoted 9 lines
2315,6 +2352,24 @@
drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void *iommu_data, · line 2356
Quoted 6 lines
static long vfio_iommu_type1_ioctl(void
*iommu_data,drivers/vfio/vfio_iommu_type1.c +2
-0return copy_to_user ((void __user *)arg, &unmap, minsz) ? -EFAULT :0 ; + }else if (cmd ==VFIO_IOMMU_CACHE_INVALIDATE ) { +struct vfio_iommu_type1_cache_invalidate ustruct;it's weird to call a variable as struct.
Will fix it.
Quoted 28 lines
Quoted 28 lines
drivers/vfio/vfio_iommu_type1.c +3
-0+int ret; + + minsz = offsetofend(struct
vfio_iommu_type1_cache_invalidate,drivers/vfio/vfio_iommu_type1.c +13
-0+ info); + +if (copy_from_user (&ustruct, (void __user *)arg, minsz)) +return -EFAULT ; + +if (ustruct.argsz < minsz || ustruct.flags) +return -EINVAL ; + +mutex_lock (&iommu->lock); + ret =vfio_iommu_lookup_dev (iommu, vfio_cache_inv_fn, + &ustruct); +mutex_unlock (&iommu->lock); +return ret; }return -ENOTTY ;include/uapi/linux/vfio.h +0
-0@@ -794,6 +794,19 @@ 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)
Quoted 9 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_dma_unmap { · line 801
Quoted 6 lines
include/uapi/linux/vfio.h +5
-0+/** +* VFIO_IOMMU_CACHE_INVALIDATE - _IOWR(VFIO_TYPE, VFIO_BASE + 24, +* struct vfio_iommu_type1_cache_invalidate) +* +* Propagate guest IOMMU cache invalidation to the host. guest or first-level/stage-1? Ideally userspace application may also bind its own address space as stage-1 one day...
Should be first-level/stage-1. Will correct it.
Thanks,
Yi Liu
Re: [RFC v2 1/3] vfio: VFIO_IOMMU_CACHE_INVALIDATE
Quoted 51 lines · Yi L
Hi Kevin,
Quoted 49 lines
From: Tian, Kevin
Sent: Friday, October 25, 2019 5:14 PM
To: Liu, Yi L <[email protected]>; [email protected];
Subject: RE: [RFC v2 1/3] vfio:VFIO_IOMMU_CACHE_INVALIDATEQuoted 30 lines
From: Liu, Yi L
Sent: Thursday, October 24, 2019 8:26 PMFrom: Liu Yi L <[email protected]>
When the guest "owns" the stage 1 translation structures, the host IOMMU driver has no knowledge of caching structure updates unless the guest invalidation requests are trapped and passed down to the host.
This patch adds the
VFIO_IOMMU_CACHE_INVALIDATEioctl with aims at propagating guest stage1 IOMMU cache invalidations to the host.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Eric Auger <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 55 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 13 ++++++++++ 2 files changed, 68 insertions(+)(quoted diff) +0
-0
b/drivers/vfio/vfio_iommu_type1.cindex 96fddc1d..cd8d3a5 100644drivers/vfio/vfio_iommu_type1.c +0
-0@@ -124,6 +124,34 @@ struct vfio_regions {#define IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu) \ (!list_empty(&iommu->domain_list))drivers/vfio/vfio_iommu_type1.c struct vfio_regions { · line 136
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+struct domain_capsule { +struct iommu_domain *domain; +void *data; +}; + +/* iommu->lock must be held */ +static int +vfio_iommu_lookup_dev (struct vfio_iommu *iommu, +int (*fn)(struct device *dev,void *data), +void *data)'lookup' usually means find a device and then return. But the real purpose here is to loop all the devices within this container and then do something. Does it make more sense to be
vfio_iommu_for_each_dev?
+1
Quoted 47 lines
yep, I can replace it.
Quoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +3
-0+{ +struct domain_capsule dc = {.data = data}; +struct vfio_domain *d;[...]
Quoted 9 lines
2315,6 +2352,24 @@
drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void *iommu_data, · line 2356
Quoted 6 lines
static long vfio_iommu_type1_ioctl(void
*iommu_data,drivers/vfio/vfio_iommu_type1.c +2
-0return copy_to_user ((void __user *)arg, &unmap, minsz) ? -EFAULT :0 ; + }else if (cmd ==VFIO_IOMMU_CACHE_INVALIDATE ) { +struct vfio_iommu_type1_cache_invalidate ustruct;it's weird to call a variable as struct.
Will fix it.
Quoted 28 lines
Quoted 28 lines
drivers/vfio/vfio_iommu_type1.c +3
-0+int ret; + + minsz = offsetofend(struct
vfio_iommu_type1_cache_invalidate,drivers/vfio/vfio_iommu_type1.c +13
-0+ info); + +if (copy_from_user (&ustruct, (void __user *)arg, minsz)) +return -EFAULT ; + +if (ustruct.argsz < minsz || ustruct.flags) +return -EINVAL ; + +mutex_lock (&iommu->lock); + ret =vfio_iommu_lookup_dev (iommu, vfio_cache_inv_fn, + &ustruct); +mutex_unlock (&iommu->lock); +return ret; }return -ENOTTY ;include/uapi/linux/vfio.h +0
-0@@ -794,6 +794,19 @@ 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)
Quoted 3 lines
Quoted 3 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_dma_unmap { · line 799
Quoted 3 lines
include/uapi/linux/vfio.h +2
-0+/** +* VFIO_IOMMU_CACHE_INVALIDATE - _IOWR(VFIO_TYPE, VFIO_BASE + 24,
What's going on with these ioctl numbers? AFAICT[1] we've used up through VFIO_BASE + 21, this jumps to 24, the next patch skips to 27, then the last patch fills in 28 & 29. Thanks,
Alex
[1] git grep -h VFIO_BASE | grep "VFIO_BASE +" | grep -e ^#define | \
awk '{print $NF}' | tr -d ')' | sort -u -nRE: [RFC v2 1/3] vfio: VFIO_IOMMU_CACHE_INVALIDATE
Quoted 109 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Wednesday, November 6, 2019 6:42 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 1/3] vfio:VFIO_IOMMU_CACHE_INVALIDATEQuoted 51 lines · Yi L
Hi Kevin,
Quoted 49 lines
From: Tian, Kevin
Sent: Friday, October 25, 2019 5:14 PM
To: Liu, Yi L <[email protected]>; [email protected];
Subject: RE: [RFC v2 1/3] vfio:VFIO_IOMMU_CACHE_INVALIDATEQuoted 30 lines
From: Liu, Yi L
Sent: Thursday, October 24, 2019 8:26 PMFrom: Liu Yi L <[email protected]>
When the guest "owns" the stage 1 translation structures, the host IOMMU driver has no knowledge of caching structure updates unless the guest invalidation requests are trapped and passed down to the host.
This patch adds the
VFIO_IOMMU_CACHE_INVALIDATEioctl with aims at propagating guest stage1 IOMMU cache invalidations to the host.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Eric Auger <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 55 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 13 ++++++++++ 2 files changed, 68 insertions(+)(quoted diff) +0
-0
b/drivers/vfio/vfio_iommu_type1.cindex 96fddc1d..cd8d3a5 100644drivers/vfio/vfio_iommu_type1.c +0
-0@@ -124,6 +124,34 @@ struct vfio_regions {#define IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu) \ (!list_empty(&iommu->domain_list))drivers/vfio/vfio_iommu_type1.c struct vfio_regions { · line 136
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+struct domain_capsule { +struct iommu_domain *domain; +void *data; +}; + +/* iommu->lock must be held */ +static int +vfio_iommu_lookup_dev (struct vfio_iommu *iommu, +int (*fn)(struct device *dev,void *data), +void *data)'lookup' usually means find a device and then return. But the real purpose here is to loop all the devices within this container and then do something. Does it make more sense to be
vfio_iommu_for_each_dev?+1
Quoted 47 lines
yep, I can replace it.
Quoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +3
-0+{ +struct domain_capsule dc = {.data = data}; +struct vfio_domain *d;[...]
Quoted 9 lines
2315,6 +2352,24 @@
drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void *iommu_data, · line 2356
Quoted 6 lines
static long vfio_iommu_type1_ioctl(void
*iommu_data,drivers/vfio/vfio_iommu_type1.c +2
-0return copy_to_user ((void __user *)arg, &unmap, minsz) ? -EFAULT :0 ; + }else if (cmd ==VFIO_IOMMU_CACHE_INVALIDATE ) { +struct vfio_iommu_type1_cache_invalidate ustruct;it's weird to call a variable as struct.
Will fix it.
Quoted 28 lines
Quoted 28 lines
drivers/vfio/vfio_iommu_type1.c +3
-0+int ret; + + minsz = offsetofend(struct
vfio_iommu_type1_cache_invalidate,drivers/vfio/vfio_iommu_type1.c +13
-0+ info); + +if (copy_from_user (&ustruct, (void __user *)arg, minsz)) +return -EFAULT ; + +if (ustruct.argsz < minsz || ustruct.flags) +return -EINVAL ; + +mutex_lock (&iommu->lock); + ret =vfio_iommu_lookup_dev (iommu, vfio_cache_inv_fn, + &ustruct); +mutex_unlock (&iommu->lock); +return ret; }return -ENOTTY ;include/uapi/linux/vfio.h +0
-0@@ -794,6 +794,19 @@ 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)
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_dma_unmap { · line 799
Quoted 3 lines
include/uapi/linux/vfio.h +2
-0+/** +* VFIO_IOMMU_CACHE_INVALIDATE - _IOWR(VFIO_TYPE, VFIO_BASE + 24, What's going on with these ioctl numbers? AFAICT[1] we've used up through
VFIO_BASE+ 21, this jumps to 24, the next patch skips to 27, then the last patch fills in 28 & 29. Thanks,
Hi Alex,
I rebase my patch to Eric's nested stage translation patches. His base also introduced IOCTLs. I should have made it better. I'll try to sync with Eric to serialize the IOCTLs.
[PATCH v6 00/22] SMMUv3 Nested Stage Setup by Eric Auger
https://lkml.org/lkml/2019/3/17/124
Thanks,
Yi Liu
Quoted 4 lines
Alex
[1] git grep -h VFIO_BASE | grep "VFIO_BASE +" | grep -e ^#define | \ awk '{print $NF}' | tr -d ')' | sort -u -n
Re: [RFC v2 1/3] vfio: VFIO_IOMMU_CACHE_INVALIDATE
Hi Yi,
Quoted 117 lines · Yi L
Quoted 109 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Wednesday, November 6, 2019 6:42 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 1/3] vfio:VFIO_IOMMU_CACHE_INVALIDATEQuoted 51 lines · Yi L
Hi Kevin,
Quoted 49 lines
From: Tian, Kevin
Sent: Friday, October 25, 2019 5:14 PM
To: Liu, Yi L <[email protected]>; [email protected];
Subject: RE: [RFC v2 1/3] vfio:VFIO_IOMMU_CACHE_INVALIDATEQuoted 30 lines
From: Liu, Yi L
Sent: Thursday, October 24, 2019 8:26 PMFrom: Liu Yi L <[email protected]>
When the guest "owns" the stage 1 translation structures, the host IOMMU driver has no knowledge of caching structure updates unless the guest invalidation requests are trapped and passed down to the host.
This patch adds the
VFIO_IOMMU_CACHE_INVALIDATEioctl with aims at propagating guest stage1 IOMMU cache invalidations to the host.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Eric Auger <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 55 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 13 ++++++++++ 2 files changed, 68 insertions(+)(quoted diff) +0
-0
b/drivers/vfio/vfio_iommu_type1.cindex 96fddc1d..cd8d3a5 100644drivers/vfio/vfio_iommu_type1.c +0
-0@@ -124,6 +124,34 @@ struct vfio_regions {#define IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu) \ (!list_empty(&iommu->domain_list))drivers/vfio/vfio_iommu_type1.c struct vfio_regions { · line 136
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+struct domain_capsule { +struct iommu_domain *domain; +void *data; +}; + +/* iommu->lock must be held */ +static int +vfio_iommu_lookup_dev (struct vfio_iommu *iommu, +int (*fn)(struct device *dev,void *data), +void *data)'lookup' usually means find a device and then return. But the real purpose here is to loop all the devices within this container and then do something. Does it make more sense to be
vfio_iommu_for_each_dev?+1
Quoted 47 lines
yep, I can replace it.
Quoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +3
-0+{ +struct domain_capsule dc = {.data = data}; +struct vfio_domain *d;[...]
Quoted 9 lines
2315,6 +2352,24 @@
drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void *iommu_data, · line 2356
Quoted 6 lines
static long vfio_iommu_type1_ioctl(void
*iommu_data,drivers/vfio/vfio_iommu_type1.c +2
-0return copy_to_user ((void __user *)arg, &unmap, minsz) ? -EFAULT :0 ; + }else if (cmd ==VFIO_IOMMU_CACHE_INVALIDATE ) { +struct vfio_iommu_type1_cache_invalidate ustruct;it's weird to call a variable as struct.
Will fix it.
Quoted 28 lines
Quoted 28 lines
drivers/vfio/vfio_iommu_type1.c +3
-0+int ret; + + minsz = offsetofend(struct
vfio_iommu_type1_cache_invalidate,drivers/vfio/vfio_iommu_type1.c +13
-0+ info); + +if (copy_from_user (&ustruct, (void __user *)arg, minsz)) +return -EFAULT ; + +if (ustruct.argsz < minsz || ustruct.flags) +return -EINVAL ; + +mutex_lock (&iommu->lock); + ret =vfio_iommu_lookup_dev (iommu, vfio_cache_inv_fn, + &ustruct); +mutex_unlock (&iommu->lock); +return ret; }return -ENOTTY ;include/uapi/linux/vfio.h +0
-0@@ -794,6 +794,19 @@ 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)Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_dma_unmap { · line 799
Quoted 3 lines
include/uapi/linux/vfio.h +2
-0+/** +* VFIO_IOMMU_CACHE_INVALIDATE - _IOWR(VFIO_TYPE, VFIO_BASE + 24, What's going on with these ioctl numbers? AFAICT[1] we've used up through
VFIO_BASE+ 21, this jumps to 24, the next patch skips to 27, then the last patch fills in 28 & 29. Thanks,
Quoted 7 lines
Hi Alex,
I rebase my patch to Eric's nested stage translation patches. His base also introduced IOCTLs. I should have made it better. I'll try to sync with Eric to serialize the IOCTLs.
[PATCH v6 00/22] SMMUv3 Nested Stage Setup by Eric Auger
https://lkml.org/lkml/2019/3/17/124
Feel free to choose your IOCTL numbers without taking care of my series. I will adapt to yours if my work gets unblocked at some point.
Thanks
Eric
Quoted 9 lines
Thanks,
Yi LiuQuoted 4 lines
Alex
[1] git grep -h VFIO_BASE | grep "VFIO_BASE +" | grep -e ^#define | \ awk '{print $NF}' | tr -d ')' | sort -u -n
kbuild test robot
build botRe: [RFC v2 1/3] vfio: VFIO_IOMMU_CACHE_INVALIDATE
In reply to Liu Yi L[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please…
Hi Liu,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on vfio/next]
[cannot apply to v5.4-rc4 next-20191025]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Liu-Yi-L/vfio-support-Shared-Virtual-Addressing/20191027-181542 base: https://github.com/awilliam/linux-vfio.git next config: sparc-allyesconfig (attached as .config) compiler: sparc64-linux-gcc (GCC) 7.4.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.4.0 make.cross ARCH=sparc
If you fix the issue, kindly add following tag
All errors (new ones prefixed by >>):
In file included from include/linux/vfio.h:16:0,
from drivers/vfio/virqfd.c:10:
>> include/uapi/linux/vfio.h:806:37: error: field 'info' has incomplete type
struct iommu_cache_invalidate_info info;
^~~~vim +/info +806 include/uapi/linux/vfio.h
796
797 /**
798 * VFIO_IOMMU_CACHE_INVALIDATE - _IOWR(VFIO_TYPE, VFIO_BASE + 24,
799 * struct vfio_iommu_type1_cache_invalidate)
800 *
801 * Propagate guest IOMMU cache invalidation to the host.
802 */
803 struct vfio_iommu_type1_cache_invalidate {
804 __u32 argsz;
805 __u32 flags;
> 806 struct iommu_cache_invalidate_info info;
807 };
808 #define VFIO_IOMMU_CACHE_INVALIDATE _IO(VFIO_TYPE, VFIO_BASE + 24)
809
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
RFC v2 2/3 vfio/type1: VFIO_IOMMU_PASID_REQUEST(alloc/free)
In reply to Liu Yi LThis patch adds VFIO_IOMMU_PASID_REQUEST ioctl which aims to passdown PASID allocation/free request from the virtual iommu. This is required to get PASID managed in system-wide.
--- drivers/vfio/vfio_iommu_type1.c | 114 ++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 25 +++++++++ 2 files changed, 139 insertions(+)
drivers/vfio/vfio_iommu_type1.c +114 -0
@@ -2248,6 +2248,83 @@ static int vfio_cache_inv_fn(struct device *dev, void *data)return iommu_cache_invalidate (dc->domain, dev, &ustruct->info); } +static int vfio_iommu_type1_pasid_alloc (struct vfio_iommu *iommu, +int min_pasid, +int max_pasid) +{ +int ret; +ioasid_t pasid; +struct mm_struct *mm =NULL ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + mm =get_task_mm (current); +/* Track ioasid allocation owner by mm */ + pasid =ioasid_alloc ((struct ioasid_set *)mm, min_pasid, + max_pasid,NULL ); +if (pasid ==INVALID_IOASID ) { + ret = -ENOSPC ; + goto out_unlock; + } + ret = pasid; +out_unlock: +mutex_unlock (&iommu->lock); +if (mm) +mmput (mm); +return ret; +} + +static int vfio_iommu_type1_pasid_free (struct vfio_iommu *iommu, +unsigned int pasid) +{ +struct mm_struct *mm =NULL ; +void *pdata; +int ret =0 ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + +/** +* REVISIT: +* There are two cases free could fail: +* 1. free pasid by non-owner, we use ioasid_set to track mm, if +* the set does not match, caller is not permitted to free. +* 2. free before unbind all devices, we can check if ioasid private +* data, if data != NULL, then fail to free. +*/ + mm =get_task_mm (current); + pdata =ioasid_find ((struct ioasid_set *)mm, pasid,NULL ); +if (IS_ERR (pdata)) { +if (pdata ==ERR_PTR (-ENOENT )) +pr_err ("PASID %u is not allocated\n" , pasid); +else if (pdata ==ERR_PTR (-EACCES )) +pr_err ("Free PASID %u by non-owner, denied" , pasid); +else +pr_err ("Error searching PASID %u\n" , pasid); + ret = -EPERM ; + goto out_unlock; + } +if (pdata) { +pr_debug ("Cannot free pasid %d with private data\n" , pasid); +/* Expect PASID has no private data if not bond */ + ret = -EBUSY ; + goto out_unlock; + } +ioasid_free (pasid); + +out_unlock: +if (mm) +mmput (mm); +mutex_unlock (&iommu->lock); +return ret; +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg) {
@@ -2370,6 +2447,43 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, &ustruct);mutex_unlock (&iommu->lock);return ret; + + }else if (cmd ==VFIO_IOMMU_PASID_REQUEST ) { +struct vfio_iommu_type1_pasid_request req; +int min_pasid, max_pasid, pasid; + + minsz = offsetofend(struct vfio_iommu_type1_pasid_request, + flag); + +if (copy_from_user (&req, (void __user *)arg, minsz)) +return -EFAULT ; + +if (req.argsz < minsz) +return -EINVAL ; + +switch (req.flag) { +/** +* TODO: min_pasid and max_pasid align with +* typedef unsigned int ioasid_t +*/ +case VFIO_IOMMU_PASID_ALLOC : +if (copy_from_user (&min_pasid, + (void __user *)arg + minsz,sizeof (min_pasid))) +return -EFAULT ; +if (copy_from_user (&max_pasid, + (void __user *)arg + minsz +sizeof (min_pasid), +sizeof (max_pasid))) +return -EFAULT ; +return vfio_iommu_type1_pasid_alloc (iommu, + min_pasid, max_pasid); +case VFIO_IOMMU_PASID_FREE : +if (copy_from_user (&pasid, + (void __user *)arg + minsz,sizeof (pasid))) +return -EFAULT ; +return vfio_iommu_type1_pasid_free (iommu, pasid); +default : +return -EINVAL ; + } }return -ENOTTY ;
include/uapi/linux/vfio.h +25 -0
@@ -807,6 +807,31 @@ struct vfio_iommu_type1_cache_invalidate { };#define VFIO_IOMMU_CACHE_INVALIDATE _IO(VFIO_TYPE, VFIO_BASE + 24) +/* +* @flag=VFIO_IOMMU_PASID_ALLOC, refer to the @min_pasid and @max_pasid fields +* @flag=VFIO_IOMMU_PASID_FREE, refer to @pasid field +*/ +struct vfio_iommu_type1_pasid_request { +__u32 argsz; +#define VFIO_IOMMU_PASID_ALLOC (1 << 0) +#define VFIO_IOMMU_PASID_FREE (1 << 1) +__u32 flag; +union { +struct { +int min_pasid; +int max_pasid; + }; +int pasid; + }; +}; + +/** +* VFIO_IOMMU_PASID_REQUEST - _IOWR(VFIO_TYPE, VFIO_BASE + 27, +* struct vfio_iommu_type1_pasid_request) +* +*/ +#define VFIO_IOMMU_PASID_REQUEST _IO(VFIO_TYPE, VFIO_BASE + 27) +/* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */ /*
Signature · 1 line
2.7.4
RE: [RFC v2 2/3] vfio/type1: VFIO_IOMMU_PASID_REQUEST(alloc/free)
Quoted 32 lines
From: Liu Yi L
Sent: Thursday, October 24, 2019 8:26 PMThis patch adds
VFIO_IOMMU_PASID_REQUESTioctl which aims to passdown PASID allocation/free request from the virtual iommu. This is required to get PASID managed in system-wide.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Yi Sun <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 114 ++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 25 +++++++++ 2 files changed, 139 insertions(+)drivers/vfio/vfio_iommu_type1.c +5
-0@@ -2248,6 +2248,83 @@ static int vfio_cache_inv_fn(struct device *dev, void *data)return iommu_cache_invalidate (dc->domain, dev, &ustruct->info); } +static int vfio_iommu_type1_pasid_alloc (struct vfio_iommu *iommu, +int min_pasid, +int max_pasid) +{ +int ret;
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, · line 2266
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+ioasid_t pasid; +struct mm_struct *mm =NULL ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + mm =get_task_mm (current); +/* Track ioasid allocation owner by mm */
below is purely allocation. Where does 'track' come to play?
Quoted 25 lines
drivers/vfio/vfio_iommu_type1.c +25
-0+ pasid =ioasid_alloc ((struct ioasid_set *)mm, min_pasid, + max_pasid,NULL ); +if (pasid ==INVALID_IOASID ) { + ret = -ENOSPC ; + goto out_unlock; + } + ret = pasid; +out_unlock: +mutex_unlock (&iommu->lock); +if (mm) +mmput (mm); +return ret; +} + +static int vfio_iommu_type1_pasid_free (struct vfio_iommu *iommu, +unsigned int pasid) +{ +struct mm_struct *mm =NULL ; +void *pdata; +int ret =0 ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock;
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2300
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+ } + +/** +* REVISIT: +* There are two cases free could fail: +* 1. free pasid by non-owner, we use ioasid_set to track mm, if +* the set does not match, caller is not permitted to free. +* 2. free before unbind all devices, we can check if ioasid private +* data, if data != NULL, then fail to free. +*/
Does REVISIT mean that above comment is the right way but the code doesn't follow yet, or the comment itself should be revisited?
should we have some notification mechanism, so the guy who holds the reference to the pasid can be notified to release its usage?
Quoted 123 lines
drivers/vfio/vfio_iommu_type1.c +27
-0+ mm =get_task_mm (current); + pdata =ioasid_find ((struct ioasid_set *)mm, pasid,NULL ); +if (IS_ERR (pdata)) { +if (pdata ==ERR_PTR (-ENOENT )) +pr_err ("PASID %u is not allocated\n" , pasid); +else if (pdata ==ERR_PTR (-EACCES )) +pr_err ("Free PASID %u by non-owner, denied" , pasid); +else +pr_err ("Error searching PASID %u\n" , pasid); + ret = -EPERM ; + goto out_unlock; + } +if (pdata) { +pr_debug ("Cannot free pasid %d with private data\n" , pasid); +/* Expect PASID has no private data if not bond */ + ret = -EBUSY ; + goto out_unlock; + } +ioasid_free (pasid); + +out_unlock: +if (mm) +mmput (mm); +mutex_unlock (&iommu->lock); +return ret; +} +static long vfio_iommu_type1_ioctl(void
*iommu_data,drivers/vfio/vfio_iommu_type1.c +25
-0unsigned int cmd, unsigned long arg) {@@ -2370,6 +2447,43 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, &ustruct);mutex_unlock (&iommu->lock);return ret; + + }else if (cmd ==VFIO_IOMMU_PASID_REQUEST ) { +struct vfio_iommu_type1_pasid_request req; +int min_pasid, max_pasid, pasid; + + minsz = offsetofend(struct vfio_iommu_type1_pasid_request, + flag); + +if (copy_from_user (&req, (void __user *)arg, minsz)) +return -EFAULT ; + +if (req.argsz < minsz) +return -EINVAL ; + +switch (req.flag) { +/** +* TODO: min_pasid and max_pasid align with +* typedef unsigned int ioasid_t +*/ +case VFIO_IOMMU_PASID_ALLOC : +if (copy_from_user (&min_pasid, + (void __user *)arg + minsz,sizeof (min_pasid))) +return -EFAULT ; + if (copy_from_user(&max_pasid, + (void __user *)arg + minsz +
sizeof(min_pasid),drivers/vfio/vfio_iommu_type1.c +9
-0+ sizeof(max_pasid))) +return -EFAULT ; +return vfio_iommu_type1_pasid_alloc (iommu, + min_pasid, max_pasid); +case VFIO_IOMMU_PASID_FREE : +if (copy_from_user (&pasid, + (void __user *)arg + minsz,sizeof (pasid))) +return -EFAULT ; + return vfio_iommu_type1_pasid_free(iommu,pasid);
drivers/vfio/vfio_iommu_type1.c +3
-0+default : +return -EINVAL ; + } }return -ENOTTY ;include/uapi/linux/vfio.h +3
-0@@ -807,6 +807,31 @@ struct vfio_iommu_type1_cache_invalidate { };#define VFIO_IOMMU_CACHE_INVALIDATE _IO(VFIO_TYPE, VFIO_BASE + 24) +/* +* @flag=VFIO_IOMMU_PASID_ALLOC, refer to the @min_pasid and @max_pasid fields
+ * @flag=VFIO_IOMMU_PASID_FREE, refer to @pasid field
+ */include/uapi/linux/vfio.h +22
-0+struct vfio_iommu_type1_pasid_request { +__u32 argsz; +#define VFIO_IOMMU_PASID_ALLOC (1 << 0) +#define VFIO_IOMMU_PASID_FREE (1 << 1) +__u32 flag; +union { +struct { +int min_pasid; +int max_pasid; + }; +int pasid; + }; +}; + +/** +* VFIO_IOMMU_PASID_REQUEST - _IOWR(VFIO_TYPE, VFIO_BASE + 27, +* struct vfio_iommu_type1_pasid_request) +* +*/ +#define VFIO_IOMMU_PASID_REQUEST _IO(VFIO_TYPE, VFIO_BASE + 27) +/* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */
/*
--
2.7.4
RE: [RFC v2 2/3] vfio/type1: VFIO_IOMMU_PASID_REQUEST(alloc/free)
Hi Kevin,
Quoted 30 lines
From: Tian, Kevin
Sent: Friday, October 25, 2019 6:06 PM
To: Liu, Yi L <[email protected]>; [email protected];
Subject: RE: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 25 lines
From: Liu Yi L
Sent: Thursday, October 24, 2019 8:26 PMThis patch adds
VFIO_IOMMU_PASID_REQUESTioctl which aims to passdown PASID allocation/free request from the virtual iommu. This is required to get PASID managed in system-wide.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Yi Sun <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 114 ++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 25 +++++++++ 2 files changed, 139 insertions(+)(quoted diff) +0
-0
b/drivers/vfio/vfio_iommu_type1.cindex cd8d3a5..3d73a7d 100644drivers/vfio/vfio_iommu_type1.c +0
-0@@ -2248,6 +2248,83 @@ static int vfio_cache_inv_fn(struct device *dev, void *data)return iommu_cache_invalidate (dc->domain, dev, &ustruct->info); }
Quoted 16 lines
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+static int vfio_iommu_type1_pasid_alloc (struct vfio_iommu *iommu, +int min_pasid, +int max_pasid) +{ +int ret;drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2265
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+ioasid_t pasid; +struct mm_struct *mm =NULL ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + mm =get_task_mm (current); +/* Track ioasid allocation owner by mm */ below is purely allocation. Where does 'track' come to play?
ioasid_set is kind of owner track. As allocation is separate with bind, here set the "owner" could be used to do sanity check when a pasid bind comes.
Quoted 30 lines
Quoted 30 lines
drivers/vfio/vfio_iommu_type1.c +30
-0+ pasid =ioasid_alloc ((struct ioasid_set *)mm, min_pasid, + max_pasid,NULL ); +if (pasid ==INVALID_IOASID ) { + ret = -ENOSPC ; + goto out_unlock; + } + ret = pasid; +out_unlock: +mutex_unlock (&iommu->lock); +if (mm) +mmput (mm); +return ret; +} + +static int vfio_iommu_type1_pasid_free (struct vfio_iommu *iommu, +unsigned int pasid) +{ +struct mm_struct *mm =NULL ; +void *pdata; +int ret =0 ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + +/** +* REVISIT: +* There are two cases free could fail:
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2300
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+* 1. free pasid by non-owner, we use ioasid_set to track mm, if +* the set does not match, caller is not permitted to free. +* 2. free before unbind all devices, we can check if ioasid private +* data, if data != NULL, then fail to free. +*/ Does REVISIT mean that above comment is the right way but the code doesn't follow yet, or the comment itself should be revisited?
Sorry, it's a mistake... should be removed. It's added in the development phase for remind. Will remove it.
Quoted 3 lines
should we have some notification mechanism, so the guy who holds the reference to the pasid can be notified to release its usage?
Do you mean the ioasid itself to provide such a notification mechanism?
Currently, we prevent pasid free before all user (iommu driver, guest) released their usage. This is achieved by checking the private data, in which there is a user_cnt of a pasid. e.g. struct intel_svm. A fresh guest pasid bind will allocate the private data. A second guest pasid bind will increase the user_cnt. guest pasid unbind decreases the user_cnt. The private data will be freed by the last guest pasid unbind. Do you think it is sufficient? or we may want to have a notification mechanism to allow such pasid free and keep the user updated?
Thanks,
Yi Liu
Re: [RFC v2 2/3] vfio/type1: VFIO_IOMMU_PASID_REQUEST(alloc/free)
In reply to Liu Yi LQuoted 28 lines · On Thu, 24 Oct 2019 08:26:22 -0400 Liu Yi L <[email protected]> wrote:
This patch adds
VFIO_IOMMU_PASID_REQUESTioctl which aims to passdown PASID allocation/free request from the virtual iommu. This is required to get PASID managed in system-wide.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Yi Sun <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 114 ++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 25 +++++++++ 2 files changed, 139 insertions(+)drivers/vfio/vfio_iommu_type1.c +7
-0@@ -2248,6 +2248,83 @@ static int vfio_cache_inv_fn(struct device *dev, void *data)return iommu_cache_invalidate (dc->domain, dev, &ustruct->info); } +static int vfio_iommu_type1_pasid_alloc (struct vfio_iommu *iommu, +int min_pasid, +int max_pasid) +{ +int ret; +ioasid_t pasid; +struct mm_struct *mm =NULL ;
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2267
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+ +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + mm =get_task_mm (current); +/* Track ioasid allocation owner by mm */ + pasid =ioasid_alloc ((struct ioasid_set *)mm, min_pasid, + max_pasid,NULL );
Are we sure we want to tie this to the task mm vs perhaps the vfio_iommu pointer?
Quoted 32 lines
drivers/vfio/vfio_iommu_type1.c +32
-0+if (pasid ==INVALID_IOASID ) { + ret = -ENOSPC ; + goto out_unlock; + } + ret = pasid; +out_unlock: +mutex_unlock (&iommu->lock); +if (mm) +mmput (mm); +return ret; +} + +static int vfio_iommu_type1_pasid_free (struct vfio_iommu *iommu, +unsigned int pasid) +{ +struct mm_struct *mm =NULL ; +void *pdata; +int ret =0 ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + +/** +* REVISIT: +* There are two cases free could fail: +* 1. free pasid by non-owner, we use ioasid_set to track mm, if +* the set does not match, caller is not permitted to free. +* 2. free before unbind all devices, we can check if ioasid private +* data, if data != NULL, then fail to free.
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2309
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+*/ + mm =get_task_mm (current); + pdata =ioasid_find ((struct ioasid_set *)mm, pasid,NULL ); +if (IS_ERR (pdata)) { +if (pdata ==ERR_PTR (-ENOENT )) +pr_err ("PASID %u is not allocated\n" , pasid); +else if (pdata ==ERR_PTR (-EACCES )) +pr_err ("Free PASID %u by non-owner, denied" , pasid); +else +pr_err ("Error searching PASID %u\n" , pasid);
This should be removed, errno is sufficient for the user, this just provides the user with a trivial DoS vector filling logs.
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2310
Quoted 1 line
drivers/vfio/vfio_iommu_type1.c +1
-0+ ret = -EPERM ;
But why not return PTR_ERR(pdata)?
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2319
Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c +9
-0+ goto out_unlock; + } +if (pdata) { +pr_debug ("Cannot free pasid %d with private data\n" , pasid); +/* Expect PASID has no private data if not bond */ + ret = -EBUSY ; + goto out_unlock; + } +ioasid_free (pasid);
We only ever get here with pasid == NULL?! Something is wrong. Should that be 'if (!pdata)'? (which also makes that pr_debug another DoS vector)
Quoted 20 lines
drivers/vfio/vfio_iommu_type1.c +13
-0+ +out_unlock: +if (mm) +mmput (mm); +mutex_unlock (&iommu->lock); +return ret; +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg) {@@ -2370,6 +2447,43 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, &ustruct);mutex_unlock (&iommu->lock);return ret; + + }else if (cmd ==VFIO_IOMMU_PASID_REQUEST ) { +struct vfio_iommu_type1_pasid_request req; +int min_pasid, max_pasid, pasid; +
drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void *iommu_data, · line 2464
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+ minsz = offsetofend(struct vfio_iommu_type1_pasid_request, + flag); + +if (copy_from_user (&req, (void __user *)arg, minsz)) +return -EFAULT ; + +if (req.argsz < minsz) +return -EINVAL ; + +switch (req.flag) {
This works, but it's strange. Let's make the code a little easier for the next flag bit that gets used so they don't need to rework this case statement. I'd suggest creating a VFIO_IOMMU_PASID_OPS_MASK that is the OR of the ALLOC/FREE options, test that no bits are set outside of that mask, then AND that mask as the switch arg with the code below.
Quoted 38 lines
drivers/vfio/vfio_iommu_type1.c +22
-0+/** +* TODO: min_pasid and max_pasid align with +* typedef unsigned int ioasid_t +*/ +case VFIO_IOMMU_PASID_ALLOC : +if (copy_from_user (&min_pasid, + (void __user *)arg + minsz,sizeof (min_pasid))) +return -EFAULT ; +if (copy_from_user (&max_pasid, + (void __user *)arg + minsz +sizeof (min_pasid), +sizeof (max_pasid))) +return -EFAULT ; +return vfio_iommu_type1_pasid_alloc (iommu, + min_pasid, max_pasid); +case VFIO_IOMMU_PASID_FREE : +if (copy_from_user (&pasid, + (void __user *)arg + minsz,sizeof (pasid))) +return -EFAULT ; +return vfio_iommu_type1_pasid_free (iommu, pasid); +default : +return -EINVAL ; + } }return -ENOTTY ;include/uapi/linux/vfio.h +5
-0@@ -807,6 +807,31 @@ struct vfio_iommu_type1_cache_invalidate { };#define VFIO_IOMMU_CACHE_INVALIDATE _IO(VFIO_TYPE, VFIO_BASE + 24) +/* +* @flag=VFIO_IOMMU_PASID_ALLOC, refer to the @min_pasid and @max_pasid fields +* @flag=VFIO_IOMMU_PASID_FREE, refer to @pasid field +*/ +struct vfio_iommu_type1_pasid_request {
include/uapi/linux/vfio.h struct vfio_iommu_type1_cache_invalidate { · line 824
Quoted 10 lines
include/uapi/linux/vfio.h +10
-0+__u32 argsz; +#define VFIO_IOMMU_PASID_ALLOC (1 << 0) +#define VFIO_IOMMU_PASID_FREE (1 << 1) +__u32 flag; +union { +struct { +int min_pasid; +int max_pasid; + }; +int pasid;
Perhaps:
struct {
u32 min;
u32 max;
} alloc_pasid;
u32 free_pasid;(note also the s/int/u32/)
Quoted 13 lines
include/uapi/linux/vfio.h +10
-0+ }; +}; + +/** +* VFIO_IOMMU_PASID_REQUEST - _IOWR(VFIO_TYPE, VFIO_BASE + 27, +* struct vfio_iommu_type1_pasid_request) +* +*/ +#define VFIO_IOMMU_PASID_REQUEST _IO(VFIO_TYPE, VFIO_BASE + 27) +/* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */ /*
RE: [RFC v2 2/3] vfio/type1: VFIO_IOMMU_PASID_REQUEST(alloc/free)
Quoted 43 lines
From: Alex Williamson <[email protected]>
Sent: Wednesday, November 6, 2019 7:36 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 10 lines · On Thu, 24 Oct 2019 08:26:22 -0400 Liu Yi L <[email protected]> wrote:
This patch adds
VFIO_IOMMU_PASID_REQUESTioctl which aims to passdown PASID allocation/free request from the virtual iommu. This is required to get PASID managed in system-wide.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Yi Sun <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 114++++++++++++++++++++++++++++++++++++++++
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void
Quoted 8 lines
include/uapi/linux/vfio.h | 25 +++++++++ 2 files changed, 139 insertions(+)drivers/vfio/vfio_iommu_type1.c +0
-0@@ -2248,6 +2248,83 @@ static int vfio_cache_inv_fn(struct device *dev, void*data)
Quoted 15 lines
drivers/vfio/vfio_iommu_type1.c +12
-0return iommu_cache_invalidate (dc->domain, dev, &ustruct->info); } +static int vfio_iommu_type1_pasid_alloc (struct vfio_iommu *iommu, +int min_pasid, +int max_pasid) +{ +int ret; +ioasid_t pasid; +struct mm_struct *mm =NULL ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock;
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2267
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+ } + mm =get_task_mm (current); +/* Track ioasid allocation owner by mm */ + pasid =ioasid_alloc ((struct ioasid_set *)mm, min_pasid, + max_pasid,NULL );Are we sure we want to tie this to the task mm vs perhaps the
vfio_iommupointer?
Here we want to have a kind of per-VM mark, which can be used to do ownership check on whether a pasid is held by a specific VM. This is very important to prevent across VM affect. vfio_iommu pointer is competent for vfio as vfio is both pasid alloc requester and pasid consumer. e.g. vfio requests pasid alloc from ioasid and also it will invoke bind_gpasid(). vfio can either check ownership before invoking bind_gpasid() or pass vfio_iommu pointer to iommu driver. But in future, there may be other modules which are just consumers of pasid. And they also want to do ownership check for a pasid. Then, it would be hard for them as they are not the pasid alloc requester. So here better to have a system wide structure to perform as the per-VM mark. task mm looks to be much competent.
Quoted 37 lines
Quoted 37 lines
drivers/vfio/vfio_iommu_type1.c +37
-0+if (pasid ==INVALID_IOASID ) { + ret = -ENOSPC ; + goto out_unlock; + } + ret = pasid; +out_unlock: +mutex_unlock (&iommu->lock); +if (mm) +mmput (mm); +return ret; +} + +static int vfio_iommu_type1_pasid_free (struct vfio_iommu *iommu, +unsigned int pasid) +{ +struct mm_struct *mm =NULL ; +void *pdata; +int ret =0 ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + +/** +* REVISIT: +* There are two cases free could fail: +* 1. free pasid by non-owner, we use ioasid_set to track mm, if +* the set does not match, caller is not permitted to free. +* 2. free before unbind all devices, we can check if ioasid private +* data, if data != NULL, then fail to free. +*/ + mm =get_task_mm (current); + pdata =ioasid_find ((struct ioasid_set *)mm, pasid,NULL ); +if (IS_ERR (pdata)) { +if (pdata ==ERR_PTR (-ENOENT ))
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2309
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+pr_err ("PASID %u is not allocated\n" , pasid); + else if (pdata == ERR_PTR(-EACCES)) + pr_err("Free PASID %u by non-owner, denied", pasid); + else + pr_err("Error searching PASID %u\n", pasid);This should be removed, errno is sufficient for the user, this just provides the user with a trivial DoS vector filling logs.
sure, will fix it. thanks.
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2310
Quoted 1 line
drivers/vfio/vfio_iommu_type1.c +1
-0+ ret = -EPERM ;But why not return
PTR_ERR(pdata)?
aha, would do it.
Quoted 11 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2319
Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c +9
-0+ goto out_unlock; + } +if (pdata) { +pr_debug ("Cannot free pasid %d with private data\n" , pasid); +/* Expect PASID has no private data if not bond */ + ret = -EBUSY ; + goto out_unlock; + } +ioasid_free (pasid);We only ever get here with pasid == NULL?!
I guess you meant only when pdata==NULL.
Quoted 3 lines
Something is wrong. Should
that be 'if (!pdata)'? (which also makes thatpr_debuganother DoS vector)
Oh, yes, just do it as below:
if (!pdata) {
ioasid_free(pasid);
ret = SUCCESS;
} else
ret = -EBUSY;Is it what you mean?
Quoted 28 lines
Quoted 28 lines
drivers/vfio/vfio_iommu_type1.c +21
-0+ +out_unlock: +if (mm) +mmput (mm); +mutex_unlock (&iommu->lock); +return ret; +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg) {@@ -2370,6 +2447,43 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, &ustruct);mutex_unlock (&iommu->lock);return ret; + + }else if (cmd ==VFIO_IOMMU_PASID_REQUEST ) { +struct vfio_iommu_type1_pasid_request req; +int min_pasid, max_pasid, pasid; + + minsz = offsetofend(struct vfio_iommu_type1_pasid_request, + flag); + +if (copy_from_user (&req, (void __user *)arg, minsz)) +return -EFAULT ; + +if (req.argsz < minsz) +return -EINVAL ;
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void *iommu_data, · line 2464
Quoted 2 lines
drivers/vfio/vfio_iommu_type1.c +2
-0+ +switch (req.flag) {This works, but it's strange. Let's make the code a little easier for the next flag bit that gets used so they don't need to rework this case statement. I'd suggest creating a
VFIO_IOMMU_PASID_OPS_MASKthat is the OR of the ALLOC/FREE options, test that no bits are set outside of that mask, then AND that mask as the switch arg with the code below.
Got it. Let me fix it in next version.
Quoted 52 lines
Quoted 30 lines
drivers/vfio/vfio_iommu_type1.c +22
-0+/** +* TODO: min_pasid and max_pasid align with +* typedef unsigned int ioasid_t +*/ +case VFIO_IOMMU_PASID_ALLOC : +if (copy_from_user (&min_pasid, + (void __user *)arg + minsz,sizeof (min_pasid))) +return -EFAULT ; +if (copy_from_user (&max_pasid, + (void __user *)arg + minsz +sizeof (min_pasid), +sizeof (max_pasid))) +return -EFAULT ; +return vfio_iommu_type1_pasid_alloc (iommu, + min_pasid, max_pasid); +case VFIO_IOMMU_PASID_FREE : +if (copy_from_user (&pasid, + (void __user *)arg + minsz,sizeof (pasid))) +return -EFAULT ; +return vfio_iommu_type1_pasid_free (iommu, pasid); +default : +return -EINVAL ; + } }return -ENOTTY ;include/uapi/linux/vfio.h +0
-0include/uapi/linux/vfio.h struct vfio_iommu_type1_cache_invalidate { · line 811
Quoted 5 lines
include/uapi/linux/vfio.h +2
-0@@ -807,6 +807,31 @@ struct vfio_iommu_type1_cache_invalidate { };#define VFIO_IOMMU_CACHE_INVALIDATE _IO(VFIO_TYPE, VFIO_BASE + 24) +/* +* @flag=VFIO_IOMMU_PASID_ALLOC, refer to the @min_pasid and @max_pasid fields
include/uapi/linux/vfio.h struct vfio_iommu_type1_cache_invalidate { · line 824
Quoted 13 lines
include/uapi/linux/vfio.h +13
-0+* @flag=VFIO_IOMMU_PASID_FREE, refer to @pasid field +*/ +struct vfio_iommu_type1_pasid_request { +__u32 argsz; +#define VFIO_IOMMU_PASID_ALLOC (1 << 0) +#define VFIO_IOMMU_PASID_FREE (1 << 1) +__u32 flag; +union { +struct { +int min_pasid; +int max_pasid; + }; +int pasid;Perhaps:
Quoted 7 lines
struct { u32 min; u32 max; } alloc_pasid; u32 free_pasid;(note also the s/int/u32/)
got it. will fix it in next version. Thanks.
Quoted 12 lines
Quoted 12 lines
include/uapi/linux/vfio.h +10
-0+ }; +}; + +/** +* VFIO_IOMMU_PASID_REQUEST - _IOWR(VFIO_TYPE, VFIO_BASE + 27, +* struct vfio_iommu_type1_pasid_request) +* +*/ +#define VFIO_IOMMU_PASID_REQUEST _IO(VFIO_TYPE, VFIO_BASE + 27) +/* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */
Quoted 1 line
include/uapi/linux/vfio.h struct vfio_iommu_type1_cache_invalidate { · line 837
Quoted 1 line
include/uapi/linux/vfio.h +0
-0/*
Regards,
Yi Liu
Re: [RFC v2 2/3] vfio/type1: VFIO_IOMMU_PASID_REQUEST(alloc/free)
Quoted 52 lines · Yi L
Quoted 43 lines
From: Alex Williamson <[email protected]>
Sent: Wednesday, November 6, 2019 7:36 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 10 lines · On Thu, 24 Oct 2019 08:26:22 -0400 Liu Yi L <[email protected]> wrote:
This patch adds
VFIO_IOMMU_PASID_REQUESTioctl which aims to passdown PASID allocation/free request from the virtual iommu. This is required to get PASID managed in system-wide.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Yi Sun <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 114++++++++++++++++++++++++++++++++++++++++
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void
Quoted 8 lines
include/uapi/linux/vfio.h | 25 +++++++++ 2 files changed, 139 insertions(+)drivers/vfio/vfio_iommu_type1.c +0
-0@@ -2248,6 +2248,83 @@ static int vfio_cache_inv_fn(struct device *dev, void*data)
Quoted 15 lines
drivers/vfio/vfio_iommu_type1.c +12
-0return iommu_cache_invalidate (dc->domain, dev, &ustruct->info); } +static int vfio_iommu_type1_pasid_alloc (struct vfio_iommu *iommu, +int min_pasid, +int max_pasid) +{ +int ret; +ioasid_t pasid; +struct mm_struct *mm =NULL ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock;Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2267
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+ } + mm =get_task_mm (current); +/* Track ioasid allocation owner by mm */ + pasid =ioasid_alloc ((struct ioasid_set *)mm, min_pasid, + max_pasid,NULL );Are we sure we want to tie this to the task mm vs perhaps the
vfio_iommupointer?
Quoted 12 lines
Here we want to have a kind of per-VM mark, which can be used to do ownership check on whether a pasid is held by a specific VM. This is very important to prevent across VM affect.
vfio_iommupointer is competent for vfio as vfio is both pasid alloc requester and pasid consumer. e.g. vfio requests pasid alloc from ioasid and also it will invokebind_gpasid(). vfio can either check ownership before invokingbind_gpasid()or passvfio_iommupointer to iommu driver. But in future, there may be other modules which are just consumers of pasid. And they also want to do ownership check for a pasid. Then, it would be hard for them as they are not the pasid alloc requester. So here better to have a system wide structure to perform as the per-VM mark. task mm looks to be much competent.
Ok, so it's intentional to have a VM-wide token. Elsewhere in the type1 code (vfio_dma_do_map) we record the task_struct per dma mapping so that we can get the task mm as needed. Would the task_struct pointer provide any advantage?
Also, an overall question, this provides userspace with pasid alloc and free ioctls, (1) what prevents a userspace process from consuming every available pasid, and (2) if the process exits or crashes without freeing pasids, how are they recovered aside from a reboot?
Quoted 7 lines
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2274
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c +7
-0+if (pasid ==INVALID_IOASID ) { + ret = -ENOSPC ; + goto out_unlock; + } + ret = pasid; +out_unlock: +mutex_unlock (&iommu->lock);
What does holding this lock protect? That the vfio_iommu remains backed by an iommu during this operation, even though we don't do anything to release allocated pasids when that iommu backing is removed?
Quoted 67 lines
Quoted 30 lines
Quoted 30 lines
drivers/vfio/vfio_iommu_type1.c +30
-0+if (mm) +mmput (mm); +return ret; +} + +static int vfio_iommu_type1_pasid_free (struct vfio_iommu *iommu, +unsigned int pasid) +{ +struct mm_struct *mm =NULL ; +void *pdata; +int ret =0 ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + +/** +* REVISIT: +* There are two cases free could fail: +* 1. free pasid by non-owner, we use ioasid_set to track mm, if +* the set does not match, caller is not permitted to free. +* 2. free before unbind all devices, we can check if ioasid private +* data, if data != NULL, then fail to free. +*/ + mm =get_task_mm (current); + pdata =ioasid_find ((struct ioasid_set *)mm, pasid,NULL ); +if (IS_ERR (pdata)) { +if (pdata ==ERR_PTR (-ENOENT ))Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2309
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+pr_err ("PASID %u is not allocated\n" , pasid); + else if (pdata == ERR_PTR(-EACCES)) + pr_err("Free PASID %u by non-owner, denied", pasid); + else + pr_err("Error searching PASID %u\n", pasid);This should be removed, errno is sufficient for the user, this just provides the user with a trivial DoS vector filling logs.
sure, will fix it. thanks.
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2310
Quoted 1 line
drivers/vfio/vfio_iommu_type1.c +1
-0+ ret = -EPERM ;But why not return
PTR_ERR(pdata)?aha, would do it.
Quoted 11 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2319
Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c +9
-0+ goto out_unlock; + } +if (pdata) { +pr_debug ("Cannot free pasid %d with private data\n" , pasid); +/* Expect PASID has no private data if not bond */ + ret = -EBUSY ; + goto out_unlock; + } +ioasid_free (pasid);We only ever get here with pasid == NULL?!
I guess you meant only when pdata==NULL.
Quoted 3 lines
Something is wrong. Should
that be 'if (!pdata)'? (which also makes thatpr_debuganother DoS vector)Oh, yes, just do it as below:
Quoted 7 lines
if (!pdata) { ioasid_free(pasid); ret = SUCCESS; } else ret = -EBUSY;Is it what you mean?
No, I think I was just confusing pdata and pasid, but I am still confused about testing pdata. We call ioasid_alloc() with private = NULL, and I don't see any of your patches calling ioasid_set_data() to change the private data after allocation, so how could this ever be set? Should this just be a BUG_ON(pdata) as the integrity of the system is in question should this state ever occur? Thanks,
Alex
Quoted 113 lines
Quoted 28 lines
Quoted 28 lines
drivers/vfio/vfio_iommu_type1.c +21
-0+ +out_unlock: +if (mm) +mmput (mm); +mutex_unlock (&iommu->lock); +return ret; +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg) {@@ -2370,6 +2447,43 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, &ustruct);mutex_unlock (&iommu->lock);return ret; + + }else if (cmd ==VFIO_IOMMU_PASID_REQUEST ) { +struct vfio_iommu_type1_pasid_request req; +int min_pasid, max_pasid, pasid; + + minsz = offsetofend(struct vfio_iommu_type1_pasid_request, + flag); + +if (copy_from_user (&req, (void __user *)arg, minsz)) +return -EFAULT ; + +if (req.argsz < minsz) +return -EINVAL ;Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void *iommu_data, · line 2464
Quoted 2 lines
drivers/vfio/vfio_iommu_type1.c +2
-0+ +switch (req.flag) {This works, but it's strange. Let's make the code a little easier for the next flag bit that gets used so they don't need to rework this case statement. I'd suggest creating a
VFIO_IOMMU_PASID_OPS_MASKthat is the OR of the ALLOC/FREE options, test that no bits are set outside of that mask, then AND that mask as the switch arg with the code below.Got it. Let me fix it in next version.
Quoted 52 lines
Quoted 30 lines
drivers/vfio/vfio_iommu_type1.c +22
-0+/** +* TODO: min_pasid and max_pasid align with +* typedef unsigned int ioasid_t +*/ +case VFIO_IOMMU_PASID_ALLOC : +if (copy_from_user (&min_pasid, + (void __user *)arg + minsz,sizeof (min_pasid))) +return -EFAULT ; +if (copy_from_user (&max_pasid, + (void __user *)arg + minsz +sizeof (min_pasid), +sizeof (max_pasid))) +return -EFAULT ; +return vfio_iommu_type1_pasid_alloc (iommu, + min_pasid, max_pasid); +case VFIO_IOMMU_PASID_FREE : +if (copy_from_user (&pasid, + (void __user *)arg + minsz,sizeof (pasid))) +return -EFAULT ; +return vfio_iommu_type1_pasid_free (iommu, pasid); +default : +return -EINVAL ; + } }return -ENOTTY ;include/uapi/linux/vfio.h +0
-0include/uapi/linux/vfio.h struct vfio_iommu_type1_cache_invalidate { · line 811
Quoted 5 lines
include/uapi/linux/vfio.h +2
-0@@ -807,6 +807,31 @@ struct vfio_iommu_type1_cache_invalidate { };#define VFIO_IOMMU_CACHE_INVALIDATE _IO(VFIO_TYPE, VFIO_BASE + 24) +/* +* @flag=VFIO_IOMMU_PASID_ALLOC, refer to the @min_pasid and @max_pasid fields
include/uapi/linux/vfio.h struct vfio_iommu_type1_cache_invalidate { · line 824
Quoted 13 lines
include/uapi/linux/vfio.h +13
-0+* @flag=VFIO_IOMMU_PASID_FREE, refer to @pasid field +*/ +struct vfio_iommu_type1_pasid_request { +__u32 argsz; +#define VFIO_IOMMU_PASID_ALLOC (1 << 0) +#define VFIO_IOMMU_PASID_FREE (1 << 1) +__u32 flag; +union { +struct { +int min_pasid; +int max_pasid; + }; +int pasid;Perhaps:
Quoted 7 lines
struct { u32 min; u32 max; } alloc_pasid; u32 free_pasid;(note also the s/int/u32/)
got it. will fix it in next version. Thanks.
Quoted 12 lines
Quoted 12 lines
include/uapi/linux/vfio.h +10
-0+ }; +}; + +/** +* VFIO_IOMMU_PASID_REQUEST - _IOWR(VFIO_TYPE, VFIO_BASE + 27, +* struct vfio_iommu_type1_pasid_request) +* +*/ +#define VFIO_IOMMU_PASID_REQUEST _IO(VFIO_TYPE, VFIO_BASE + 27) +/* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */
Quoted 4 lines
Quoted 1 line
include/uapi/linux/vfio.h struct vfio_iommu_type1_cache_invalidate { · line 837
Quoted 1 line
include/uapi/linux/vfio.h +0
-0/* Regards,
Yi Liu
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
RE: [RFC v2 2/3] vfio/type1: VFIO_IOMMU_PASID_REQUEST(alloc/free)
Quoted 75 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Friday, November 8, 2019 6:07 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 18 lines · Yi L
Quoted 18 lines
From: Alex Williamson <[email protected]>
Sent: Wednesday, November 6, 2019 7:36 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 10 lines · On Thu, 24 Oct 2019 08:26:22 -0400 Liu Yi L <[email protected]> wrote:
This patch adds
VFIO_IOMMU_PASID_REQUESTioctl which aims to passdown PASID allocation/free request from the virtual iommu. This is required to get PASID managed in system-wide.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Yi Sun <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 114Quoted 5 lines
Quoted 5 lines
++++++++++++++++++++++++++++++++++++++++
Review
Quoted 4 lines
include/uapi/linux/vfio.h | 25 +++++++++ 2 files changed, 139 insertions(+)(quoted diff) +0
-0
b/drivers/vfio/vfio_iommu_type1.cQuoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev,
Quoted 4 lines
index cd8d3a5..3d73a7d 100644
drivers/vfio/vfio_iommu_type1.c +0
-0@@ -2248,6 +2248,83 @@ static int vfio_cache_inv_fn(struct device *dev,void
Quoted 25 lines
Quoted 16 lines
*data)
Quoted 15 lines
drivers/vfio/vfio_iommu_type1.c +12
-0return iommu_cache_invalidate (dc->domain, dev, &ustruct->info); } +static int vfio_iommu_type1_pasid_alloc (struct vfio_iommu *iommu, +int min_pasid, +int max_pasid) +{ +int ret; +ioasid_t pasid; +struct mm_struct *mm =NULL ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock;Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2267
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+ } + mm =get_task_mm (current); +/* Track ioasid allocation owner by mm */ + pasid =ioasid_alloc ((struct ioasid_set *)mm, min_pasid, + max_pasid,NULL );Are we sure we want to tie this to the task mm vs perhaps the
vfio_iommupointer?Quoted 12 lines
Here we want to have a kind of per-VM mark, which can be used to do ownership check on whether a pasid is held by a specific VM. This is very important to prevent across VM affect.
vfio_iommupointer is competent for vfio as vfio is both pasid alloc requester and pasid consumer. e.g. vfio requests pasid alloc from ioasid and also it will invokebind_gpasid(). vfio can either check ownership before invokingbind_gpasid()or passvfio_iommupointer to iommu driver. But in future, there may be other modules which are just consumers of pasid. And they also want to do ownership check for a pasid. Then, it would be hard for them as they are not the pasid alloc requester. So here better to have a system wide structure to perform as the per-VM mark. task mm looks to be much competent.
Quoted 4 lines
Ok, so it's intentional to have a VM-wide token. Elsewhere in the type1 code (
vfio_dma_do_map) we record thetask_structper dma mapping so that we can get the task mm as needed. Would thetask_structpointer provide any advantage?
I think we may use task_struct pointer to make type1 code consistent. How do you think?
Quoted 4 lines
Also, an overall question, this provides userspace with pasid alloc and free ioctls, (1) what prevents a userspace process from consuming every available pasid, and (2) if the process exits or crashes without freeing pasids, how are they recovered aside from a reboot?
For question (1), I think we only need to take care about malicious userspace process. As vfio usage is under privilege mode, so we may be safe on it so far. However, we may need to introduce a kind of credit mechanism to protect it. I've thought it, but no good idea yet. Would be happy to hear from you.
For question (2), I think we need to reclaim the allocated pasids when the vfio container fd is released just like what vfio does to the domain mappings. I didn't add it yet. But I can add it in next version if you think it would make the pasid alloc/free be much sound.
Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2274
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c +7
-0+if (pasid ==INVALID_IOASID ) { + ret = -ENOSPC ; + goto out_unlock; + } + ret = pasid; +out_unlock: +mutex_unlock (&iommu->lock);What does holding this lock protect? That the
vfio_iommuremains backed by an iommu during this operation, even though we don't do anything to release allocated pasids when that iommu backing is removed?
yes, it is unnecessary to hold the lock here. At least for the operations in this patch. will remove it. :-)
Quoted 73 lines
Quoted 73 lines
Quoted 30 lines
Quoted 30 lines
drivers/vfio/vfio_iommu_type1.c +30
-0+if (mm) +mmput (mm); +return ret; +} + +static int vfio_iommu_type1_pasid_free (struct vfio_iommu *iommu, +unsigned int pasid) +{ +struct mm_struct *mm =NULL ; +void *pdata; +int ret =0 ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + +/** +* REVISIT: +* There are two cases free could fail: +* 1. free pasid by non-owner, we use ioasid_set to track mm, if +* the set does not match, caller is not permitted to free. +* 2. free before unbind all devices, we can check if ioasid private +* data, if data != NULL, then fail to free. +*/ + mm =get_task_mm (current); + pdata =ioasid_find ((struct ioasid_set *)mm, pasid,NULL ); +if (IS_ERR (pdata)) { +if (pdata ==ERR_PTR (-ENOENT ))Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2309
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+pr_err ("PASID %u is not allocated\n" , pasid); + else if (pdata == ERR_PTR(-EACCES)) + pr_err("Free PASID %u by non-owner, denied", pasid); + else + pr_err("Error searching PASID %u\n", pasid);This should be removed, errno is sufficient for the user, this just provides the user with a trivial DoS vector filling logs.
sure, will fix it. thanks.
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2310
Quoted 1 line
drivers/vfio/vfio_iommu_type1.c +1
-0+ ret = -EPERM ;But why not return
PTR_ERR(pdata)?aha, would do it.
Quoted 11 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2319
Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c +9
-0+ goto out_unlock; + } +if (pdata) { +pr_debug ("Cannot free pasid %d with private data\n" , pasid); +/* Expect PASID has no private data if not bond */ + ret = -EBUSY ; + goto out_unlock; + } +ioasid_free (pasid);We only ever get here with pasid == NULL?!
I guess you meant only when pdata==NULL.
Quoted 3 lines
Something is wrong. Should
that be 'if (!pdata)'? (which also makes thatpr_debuganother DoS vector)Oh, yes, just do it as below:
if (!pdata) { ioasid_free(pasid); ret = SUCCESS; } else ret = -EBUSY;
Quoted 8 lines
Quoted 1 line
Is it what you mean?
No, I think I was just confusing pdata and pasid, but I am still confused about testing pdata. We call
ioasid_alloc()with private = NULL, and I don't see any of your patches callingioasid_set_data()to change the private data after allocation, so how could this ever be set? Should this just be aBUG_ON(pdata)as the integrity of the system is in question should this state ever occur? Thanks,
ioasid_set_data() was called in one patch from Jacob's vSVA patchset. [PATCH v6 08/10] iommu/vt-d: Add bind guest PASID support
https://lkml.org/lkml/2019/10/22/946
The basic idea is to allocate pasid with private=NULL, and set it when the pasid is actually bind to a device (bind_gpasid()). Each bind_gpasid() will increase the ref_cnt in the private data, and each unbind_gpasid() will decrease the ref_cnt. So if bind/unbind_gpasid() is called in mirror, the private data should be null when comes to free operation. If not, vfio can believe that the pasid is still in use.
Quoted 1 line
Alex
Thanks,
Yi Liu
Quoted 119 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +4
-0+ +out_unlock: +if (mm) +mmput (mm);Quoted 8 lines
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c +4
-0+mutex_unlock (&iommu->lock); +return ret; +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg) {@@ -2370,6 +2447,43 @@ static long vfio_iommu_type1_ioctl(void
*iommu_data,Quoted 51 lines
Quoted 16 lines
Quoted 16 lines
drivers/vfio/vfio_iommu_type1.c +13
-0&ustruct);mutex_unlock (&iommu->lock);return ret; + + }else if (cmd ==VFIO_IOMMU_PASID_REQUEST ) { +struct vfio_iommu_type1_pasid_request req; +int min_pasid, max_pasid, pasid; + + minsz = offsetofend(struct vfio_iommu_type1_pasid_request, + flag); + +if (copy_from_user (&req, (void __user *)arg, minsz)) +return -EFAULT ; + +if (req.argsz < minsz) +return -EINVAL ;Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void · line 2464
Quoted 2 lines
drivers/vfio/vfio_iommu_type1.c +2
-0+ +switch (req.flag) {This works, but it's strange. Let's make the code a little easier for the next flag bit that gets used so they don't need to rework this case statement. I'd suggest creating a
VFIO_IOMMU_PASID_OPS_MASKthat is the OR of the ALLOC/FREE options, test that no bits are set outside of that mask, then AND that mask as the switch arg with the code below.Got it. Let me fix it in next version.
Quoted 24 lines
Quoted 24 lines
drivers/vfio/vfio_iommu_type1.c +22
-0+/** +* TODO: min_pasid and max_pasid align with +* typedef unsigned int ioasid_t +*/ +case VFIO_IOMMU_PASID_ALLOC : +if (copy_from_user (&min_pasid, + (void __user *)arg + minsz,sizeof (min_pasid))) +return -EFAULT ; +if (copy_from_user (&max_pasid, + (void __user *)arg + minsz +sizeof (min_pasid), +sizeof (max_pasid))) +return -EFAULT ; +return vfio_iommu_type1_pasid_alloc (iommu, + min_pasid, max_pasid); +case VFIO_IOMMU_PASID_FREE : +if (copy_from_user (&pasid, + (void __user *)arg + minsz,sizeof (pasid))) +return -EFAULT ; +return vfio_iommu_type1_pasid_free (iommu, pasid); +default : +return -EINVAL ; + } }Quoted 8 lines
Quoted 8 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_cache_invalidate { · line 808
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c +0
-0return -ENOTTY ;include/uapi/linux/vfio.h +0
-0@@ -807,6 +807,31 @@ struct vfio_iommu_type1_cache_invalidate { };#define VFIO_IOMMU_CACHE_INVALIDATE _IO(VFIO_TYPE, VFIO_BASE +24)
Quoted 46 lines
Quoted 20 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_cache_invalidate { · line 810
Quoted 3 lines
include/uapi/linux/vfio.h +2
-0+/* +* @flag=VFIO_IOMMU_PASID_ALLOC, refer to the @min_pasid and @max_pasid fields
include/uapi/linux/vfio.h struct vfio_iommu_type1_cache_invalidate { · line 824
Quoted 13 lines
include/uapi/linux/vfio.h +13
-0+* @flag=VFIO_IOMMU_PASID_FREE, refer to @pasid field +*/ +struct vfio_iommu_type1_pasid_request { +__u32 argsz; +#define VFIO_IOMMU_PASID_ALLOC (1 << 0) +#define VFIO_IOMMU_PASID_FREE (1 << 1) +__u32 flag; +union { +struct { +int min_pasid; +int max_pasid; + }; +int pasid;Perhaps:
Quoted 7 lines
struct { u32 min; u32 max; } alloc_pasid; u32 free_pasid;(note also the s/int/u32/)
got it. will fix it in next version. Thanks.
Quoted 12 lines
Quoted 12 lines
include/uapi/linux/vfio.h +10
-0+ }; +}; + +/** +* VFIO_IOMMU_PASID_REQUEST - _IOWR(VFIO_TYPE, VFIO_BASE + 27, +* struct vfio_iommu_type1_pasid_request) +* +*/ +#define VFIO_IOMMU_PASID_REQUEST _IO(VFIO_TYPE, VFIO_BASE + 27) +/* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */ Quoted 1 line
include/uapi/linux/vfio.h struct vfio_iommu_type1_cache_invalidate { · line 836
Quoted 1 line
include/uapi/linux/vfio.h +0
-0/* Regards,
Yi Liu
Re: [RFC v2 2/3] vfio/type1: VFIO_IOMMU_PASID_REQUEST(alloc/free)
Quoted 75 lines · Yi L
Quoted 75 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Friday, November 8, 2019 6:07 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 18 lines · Yi L
Quoted 18 lines
From: Alex Williamson <[email protected]>
Sent: Wednesday, November 6, 2019 7:36 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 10 lines · On Thu, 24 Oct 2019 08:26:22 -0400 Liu Yi L <[email protected]> wrote:
This patch adds
VFIO_IOMMU_PASID_REQUESTioctl which aims to passdown PASID allocation/free request from the virtual iommu. This is required to get PASID managed in system-wide.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Yi Sun <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 114Quoted 5 lines
Quoted 5 lines
++++++++++++++++++++++++++++++++++++++++
Review
Quoted 4 lines
include/uapi/linux/vfio.h | 25 +++++++++ 2 files changed, 139 insertions(+)(quoted diff) +0
-0
b/drivers/vfio/vfio_iommu_type1.cQuoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev,
Quoted 4 lines
index cd8d3a5..3d73a7d 100644
drivers/vfio/vfio_iommu_type1.c +0
-0@@ -2248,6 +2248,83 @@ static int vfio_cache_inv_fn(struct device *dev,void
Quoted 37 lines
Quoted 16 lines
*data)
Quoted 15 lines
drivers/vfio/vfio_iommu_type1.c +12
-0return iommu_cache_invalidate (dc->domain, dev, &ustruct->info); } +static int vfio_iommu_type1_pasid_alloc (struct vfio_iommu *iommu, +int min_pasid, +int max_pasid) +{ +int ret; +ioasid_t pasid; +struct mm_struct *mm =NULL ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock;Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2267
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+ } + mm =get_task_mm (current); +/* Track ioasid allocation owner by mm */ + pasid =ioasid_alloc ((struct ioasid_set *)mm, min_pasid, + max_pasid,NULL );Are we sure we want to tie this to the task mm vs perhaps the
vfio_iommupointer?Here we want to have a kind of per-VM mark, which can be used to do ownership check on whether a pasid is held by a specific VM. This is very important to prevent across VM affect.
vfio_iommupointer is competent for vfio as vfio is both pasid alloc requester and pasid consumer. e.g. vfio requests pasid alloc from ioasid and also it will invokebind_gpasid(). vfio can either check ownership before invokingbind_gpasid()or passvfio_iommupointer to iommu driver. But in future, there may be other modules which are just consumers of pasid. And they also want to do ownership check for a pasid. Then, it would be hard for them as they are not the pasid alloc requester. So here better to have a system wide structure to perform as the per-VM mark. task mm looks to be much competent.
Quoted 7 lines
Quoted 4 lines
Ok, so it's intentional to have a VM-wide token. Elsewhere in the type1 code (
vfio_dma_do_map) we record thetask_structper dma mapping so that we can get the task mm as needed. Would thetask_structpointer provide any advantage?I think we may use
task_structpointer to make type1 code consistent. How do you think?
If it has the same utility, sure.
Quoted 8 lines
Quoted 4 lines
Also, an overall question, this provides userspace with pasid alloc and free ioctls, (1) what prevents a userspace process from consuming every available pasid, and (2) if the process exits or crashes without freeing pasids, how are they recovered aside from a reboot?
For question (1), I think we only need to take care about malicious userspace process. As vfio usage is under privilege mode, so we may be safe on it so far.
No, where else do we ever make this assumption? vfio requires a privileged entity to configure the system for vfio, bind devices for user use, and grant those devices to the user, but the usage of the device is always assumed to be by an unprivileged user. It is absolutely not acceptable require a privileged user. It's vfio's responsibility to protect the system from the user.
Quoted 3 lines
However, we may need to introduce a kind of credit
mechanism to protect it. I've thought it, but no good idea yet. Would be happy to hear from you.
It's a limited system resource and it's unclear how many might reasonably used by a user. I don't have an easy answer.
Quoted 4 lines
For question (2), I think we need to reclaim the allocated pasids when the vfio container fd is released just like what vfio does to the domain mappings. I didn't add it yet. But I can add it in next version if you think it would make the pasid alloc/free be much sound.
Consider it required, the interface is susceptible to abuse without it.
Quoted 101 lines
Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2274
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c +7
-0+if (pasid ==INVALID_IOASID ) { + ret = -ENOSPC ; + goto out_unlock; + } + ret = pasid; +out_unlock: +mutex_unlock (&iommu->lock);What does holding this lock protect? That the
vfio_iommuremains backed by an iommu during this operation, even though we don't do anything to release allocated pasids when that iommu backing is removed?yes, it is unnecessary to hold the lock here. At least for the operations in this patch. will remove it. :-)
Quoted 73 lines
Quoted 73 lines
Quoted 30 lines
Quoted 30 lines
drivers/vfio/vfio_iommu_type1.c +30
-0+if (mm) +mmput (mm); +return ret; +} + +static int vfio_iommu_type1_pasid_free (struct vfio_iommu *iommu, +unsigned int pasid) +{ +struct mm_struct *mm =NULL ; +void *pdata; +int ret =0 ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + +/** +* REVISIT: +* There are two cases free could fail: +* 1. free pasid by non-owner, we use ioasid_set to track mm, if +* the set does not match, caller is not permitted to free. +* 2. free before unbind all devices, we can check if ioasid private +* data, if data != NULL, then fail to free. +*/ + mm =get_task_mm (current); + pdata =ioasid_find ((struct ioasid_set *)mm, pasid,NULL ); +if (IS_ERR (pdata)) { +if (pdata ==ERR_PTR (-ENOENT ))Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2309
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+pr_err ("PASID %u is not allocated\n" , pasid); + else if (pdata == ERR_PTR(-EACCES)) + pr_err("Free PASID %u by non-owner, denied", pasid); + else + pr_err("Error searching PASID %u\n", pasid);This should be removed, errno is sufficient for the user, this just provides the user with a trivial DoS vector filling logs.
sure, will fix it. thanks.
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2310
Quoted 1 line
drivers/vfio/vfio_iommu_type1.c +1
-0+ ret = -EPERM ;But why not return
PTR_ERR(pdata)?aha, would do it.
Quoted 11 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2319
Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c +9
-0+ goto out_unlock; + } +if (pdata) { +pr_debug ("Cannot free pasid %d with private data\n" , pasid); +/* Expect PASID has no private data if not bond */ + ret = -EBUSY ; + goto out_unlock; + } +ioasid_free (pasid);We only ever get here with pasid == NULL?!
I guess you meant only when pdata==NULL.
Quoted 3 lines
Something is wrong. Should
that be 'if (!pdata)'? (which also makes thatpr_debuganother DoS vector)Oh, yes, just do it as below:
if (!pdata) { ioasid_free(pasid); ret = SUCCESS; } else ret = -EBUSY;Quoted 8 lines
Quoted 1 line
Is it what you mean?
No, I think I was just confusing pdata and pasid, but I am still confused about testing pdata. We call
ioasid_alloc()with private = NULL, and I don't see any of your patches callingioasid_set_data()to change the private data after allocation, so how could this ever be set? Should this just be aBUG_ON(pdata)as the integrity of the system is in question should this state ever occur? Thanks,
ioasid_set_data()was called in one patch from Jacob's vSVA patchset. [PATCH v6 08/10] iommu/vt-d: Add bind guest PASID support
https://lkml.org/lkml/2019/10/22/946
Quoted 6 lines
The basic idea is to allocate pasid with private=NULL, and set it when the pasid is actually bind to a device (
bind_gpasid()). Eachbind_gpasid()will increase theref_cntin the private data, and eachunbind_gpasid()will decrease theref_cnt. So if bind/unbind_gpasid() is called in mirror, the private data should be null when comes to free operation. If not, vfio can believe that the pasid is still in use.
So this is another opportunity to leak pasids. What's a user supposed to do when their attempt to free a pasid fails? It invites leaks to allow this path to fail. Thanks,
Alex
RE: [RFC v2 2/3] vfio/type1: VFIO_IOMMU_PASID_REQUEST(alloc/free)
Quoted 89 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Friday, November 8, 2019 11:15 PM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 5 lines · Yi L
Quoted 5 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Friday, November 8, 2019 6:07 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 7 lines
Quoted 7 lines
Quoted 4 lines · Yi L
Quoted 4 lines
From: Alex Williamson <[email protected]>
Sent: Wednesday, November 6, 2019 7:36 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:
VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 68 lines
Quoted 67 lines
Quoted 14 lines
Quoted 14 lines
Quoted 10 lines · On Thu, 24 Oct 2019 08:26:22 -0400 Liu Yi L <[email protected]> wrote:
This patch adds
VFIO_IOMMU_PASID_REQUESTioctl which aims to passdown PASID allocation/free request from the virtual iommu. This is required to get PASID managed in system-wide.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Yi Sun <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 114Quoted 5 lines
Quoted 5 lines
++++++++++++++++++++++++++++++++++++++++
Review
Quoted 4 lines
include/uapi/linux/vfio.h | 25 +++++++++ 2 files changed, 139 insertions(+)(quoted diff) +0
-0
b/drivers/vfio/vfio_iommu_type1.cQuoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev,
Quoted 4 lines
index cd8d3a5..3d73a7d 100644
drivers/vfio/vfio_iommu_type1.c +0
-0@@ -2248,6 +2248,83 @@ static int vfio_cache_inv_fn(struct device *dev,void
Quoted 25 lines
Quoted 16 lines
*data)
Quoted 15 lines
drivers/vfio/vfio_iommu_type1.c +12
-0return iommu_cache_invalidate (dc->domain, dev, &ustruct->info); } +static int vfio_iommu_type1_pasid_alloc (struct vfio_iommu *iommu, +int min_pasid, +int max_pasid) +{ +int ret; +ioasid_t pasid; +struct mm_struct *mm =NULL ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock;Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2267
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+ } + mm =get_task_mm (current); +/* Track ioasid allocation owner by mm */ + pasid =ioasid_alloc ((struct ioasid_set *)mm, min_pasid, + max_pasid,NULL );Are we sure we want to tie this to the task mm vs perhaps the
vfio_iommupointer?Quoted 12 lines
Here we want to have a kind of per-VM mark, which can be used to do ownership check on whether a pasid is held by a specific VM. This is very important to prevent across VM affect.
vfio_iommupointer is competent for vfio as vfio is both pasid alloc requester and pasid consumer. e.g. vfio requests pasid alloc from ioasid and also it will invokebind_gpasid(). vfio can either check ownership before invokingbind_gpasid()or passvfio_iommupointer to iommu driver. But in future, there may be other modules which are just consumers of pasid. And they also want to do ownership check for a pasid. Then, it would be hard for them as they are not the pasid alloc requester. So here better to have a system wide structure to perform as the per-VM mark. task mm looks to be much competent.Ok, so it's intentional to have a VM-wide token. Elsewhere in the type1 code (
vfio_dma_do_map) we record thetask_structper dma mapping so that we can get the task mm as needed. Would thetask_structpointer provide any advantage?
Quoted 4 lines
Quoted 2 lines
I think we may use
task_structpointer to make type1 code consistent. How do you think?If it has the same utility, sure.
thanks, I'll make this change.
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines
Also, an overall question, this provides userspace with pasid alloc and free ioctls, (1) what prevents a userspace process from consuming every available pasid, and (2) if the process exits or crashes without freeing pasids, how are they recovered aside from a reboot?
For question (1), I think we only need to take care about malicious userspace process. As vfio usage is under privilege mode, so we may be safe on it so far.
Quoted 6 lines
No, where else do we ever make this assumption? vfio requires a privileged entity to configure the system for vfio, bind devices for user use, and grant those devices to the user, but the usage of the device is always assumed to be by an unprivileged user. It is absolutely not acceptable require a privileged user. It's vfio's responsibility to protect the system from the user.
My assumption is not precise here. sorry for it... Maybe to further check with you to better understand your point. I think the user (QEMU) of vfio needs to have a root permission. Thus it can open the vfio fds. At this point, the user is a privileged one. Also I guess that's why vfio can grant the user with the usage of VFIO_MAP/UNMAP to config mappings into iommu page tables. But I'm not quite sure when will the user be an unprivileged one.
Quoted 6 lines
Quoted 3 lines
However, we may need to introduce a kind of credit
mechanism to protect it. I've thought it, but no good idea yet. Would be happy to hear from you.It's a limited system resource and it's unclear how many might reasonably used by a user. I don't have an easy answer.
How about the below method? based on some offline chat with Jacob. a. some reasonable defaults for the initial per VM quota, e.g. 1000 per process
b. IOASID should be able to enforce per ioasid_set (it is kind of per VM mark) limit
Quoted 6 lines
Quoted 4 lines
For question (2), I think we need to reclaim the allocated pasids when the vfio container fd is released just like what vfio does to the domain mappings. I didn't add it yet. But I can add it in next version if you think it would make the pasid alloc/free be much sound.
Consider it required, the interface is susceptible to abuse without it.
sure, let me add it in next version.
Quoted 109 lines
Quoted 40 lines
Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2274
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c +7
-0+if (pasid ==INVALID_IOASID ) { + ret = -ENOSPC ; + goto out_unlock; + } + ret = pasid; +out_unlock: +mutex_unlock (&iommu->lock);What does holding this lock protect? That the
vfio_iommuremains backed by an iommu during this operation, even though we don't do anything to release allocated pasids when that iommu backing is removed?yes, it is unnecessary to hold the lock here. At least for the operations in this patch. will remove it. :-)
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
drivers/vfio/vfio_iommu_type1.c +25
-0+if (mm) +mmput (mm); +return ret; +} + +static int vfio_iommu_type1_pasid_free (struct vfio_iommu *iommu, +unsigned int pasid) +{ +struct mm_struct *mm =NULL ; +void *pdata; +int ret =0 ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + +/** +* REVISIT: +* There are two cases free could fail: +* 1. free pasid by non-owner, we use ioasid_set to track mm, if +* the set does not match, caller is not permitted to free. +* 2. free before unbind all devices, we can check if ioasid private +* data, if data != NULL, then fail to free. Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data)
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c +8
-0+*/ + mm =get_task_mm (current); + pdata =ioasid_find ((struct ioasid_set *)mm, pasid,NULL ); +if (IS_ERR (pdata)) { +if (pdata ==ERR_PTR (-ENOENT )) +pr_err ("PASID %u is not allocated\n" , pasid); +else if (pdata ==ERR_PTR (-EACCES )) + pr_err("Free PASID %u by non-owner, denied",pasid);
Quoted 53 lines
Quoted 40 lines
Quoted 40 lines
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2309
Quoted 2 lines
drivers/vfio/vfio_iommu_type1.c +2
-0+else +pr_err ("Error searching PASID %u\n" , pasid);This should be removed, errno is sufficient for the user, this just provides the user with a trivial DoS vector filling logs.
sure, will fix it. thanks.
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2310
Quoted 1 line
drivers/vfio/vfio_iommu_type1.c +1
-0+ ret = -EPERM ;But why not return
PTR_ERR(pdata)?aha, would do it.
Quoted 11 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, void *data) · line 2319
Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c +9
-0+ goto out_unlock; + } +if (pdata) { +pr_debug ("Cannot free pasid %d with private data\n" , pasid); +/* Expect PASID has no private data if not bond */ + ret = -EBUSY ; + goto out_unlock; + } +ioasid_free (pasid);We only ever get here with pasid == NULL?!
I guess you meant only when pdata==NULL.
Quoted 3 lines
Something is wrong. Should
that be 'if (!pdata)'? (which also makes thatpr_debuganother DoS vector)Oh, yes, just do it as below:
if (!pdata) { ioasid_free(pasid); ret = SUCCESS; } else ret = -EBUSY;Quoted 8 lines
Quoted 1 line
Is it what you mean?
No, I think I was just confusing pdata and pasid, but I am still confused about testing pdata. We call
ioasid_alloc()with private = NULL, and I don't see any of your patches callingioasid_set_data()to change the private data after allocation, so how could this ever be set? Should this just be aBUG_ON(pdata)as the integrity of the system is in question should this state ever occur? Thanks,
ioasid_set_data()was called in one patch from Jacob's vSVA patchset. [PATCH v6 08/10] iommu/vt-d: Add bind guest PASID support
https://lkml.org/lkml/2019/10/22/946Quoted 6 lines
The basic idea is to allocate pasid with private=NULL, and set it when the pasid is actually bind to a device (
bind_gpasid()). Eachbind_gpasid()will increase theref_cntin the private data, and eachunbind_gpasid()will decrease theref_cnt. So if bind/unbind_gpasid() is called in mirror, the private data should be null when comes to free operation. If not, vfio can believe that the pasid is still in use.
Quoted 3 lines
So this is another opportunity to leak pasids. What's a user supposed to do when their attempt to free a pasid fails? It invites leaks to allow this path to fail. Thanks,
Agreed, may no need to fail pasid free as it may leak pasid. How about always let free successful? If the ref_cnt is non-zero, notify the remaining users to release their reference.
Thanks,
Yi Liu
Re: [RFC v2 2/3] vfio/type1: VFIO_IOMMU_PASID_REQUEST(alloc/free)
Quoted 112 lines · Yi L
Quoted 89 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Friday, November 8, 2019 11:15 PM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 5 lines · Yi L
Quoted 5 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Friday, November 8, 2019 6:07 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 7 lines
Quoted 7 lines
Quoted 4 lines · Yi L
Quoted 4 lines
From: Alex Williamson <[email protected]>
Sent: Wednesday, November 6, 2019 7:36 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:
VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 68 lines
Quoted 67 lines
Quoted 14 lines
Quoted 14 lines
Quoted 10 lines · On Thu, 24 Oct 2019 08:26:22 -0400 Liu Yi L <[email protected]> wrote:
This patch adds
VFIO_IOMMU_PASID_REQUESTioctl which aims to passdown PASID allocation/free request from the virtual iommu. This is required to get PASID managed in system-wide.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Yi Sun <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 114Quoted 5 lines
Quoted 5 lines
++++++++++++++++++++++++++++++++++++++++
Review
Quoted 4 lines
include/uapi/linux/vfio.h | 25 +++++++++ 2 files changed, 139 insertions(+)(quoted diff) +0
-0
b/drivers/vfio/vfio_iommu_type1.cQuoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev,
Quoted 4 lines
index cd8d3a5..3d73a7d 100644
drivers/vfio/vfio_iommu_type1.c +0
-0@@ -2248,6 +2248,83 @@ static int vfio_cache_inv_fn(struct device *dev,void
Quoted 25 lines
Quoted 16 lines
*data)
Quoted 15 lines
drivers/vfio/vfio_iommu_type1.c +12
-0return iommu_cache_invalidate (dc->domain, dev, &ustruct->info); } +static int vfio_iommu_type1_pasid_alloc (struct vfio_iommu *iommu, +int min_pasid, +int max_pasid) +{ +int ret; +ioasid_t pasid; +struct mm_struct *mm =NULL ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock;Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, · line 2267
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+ } + mm =get_task_mm (current); +/* Track ioasid allocation owner by mm */ + pasid =ioasid_alloc ((struct ioasid_set *)mm, min_pasid, + max_pasid,NULL );Are we sure we want to tie this to the task mm vs perhaps the
vfio_iommupointer?Quoted 12 lines
Here we want to have a kind of per-VM mark, which can be used to do ownership check on whether a pasid is held by a specific VM. This is very important to prevent across VM affect.
vfio_iommupointer is competent for vfio as vfio is both pasid alloc requester and pasid consumer. e.g. vfio requests pasid alloc from ioasid and also it will invokebind_gpasid(). vfio can either check ownership before invokingbind_gpasid()or passvfio_iommupointer to iommu driver. But in future, there may be other modules which are just consumers of pasid. And they also want to do ownership check for a pasid. Then, it would be hard for them as they are not the pasid alloc requester. So here better to have a system wide structure to perform as the per-VM mark. task mm looks to be much competent.Ok, so it's intentional to have a VM-wide token. Elsewhere in the type1 code (
vfio_dma_do_map) we record thetask_structper dma mapping so that we can get the task mm as needed. Would thetask_structpointer provide any advantage?Quoted 4 lines
Quoted 2 lines
I think we may use
task_structpointer to make type1 code consistent. How do you think?If it has the same utility, sure.
thanks, I'll make this change.
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines
Also, an overall question, this provides userspace with pasid alloc and free ioctls, (1) what prevents a userspace process from consuming every available pasid, and (2) if the process exits or crashes without freeing pasids, how are they recovered aside from a reboot?
For question (1), I think we only need to take care about malicious userspace process. As vfio usage is under privilege mode, so we may be safe on it so far.
Quoted 6 lines
No, where else do we ever make this assumption? vfio requires a privileged entity to configure the system for vfio, bind devices for user use, and grant those devices to the user, but the usage of the device is always assumed to be by an unprivileged user. It is absolutely not acceptable require a privileged user. It's vfio's responsibility to protect the system from the user.
Quoted 7 lines
My assumption is not precise here. sorry for it... Maybe to further check with you to better understand your point. I think the user (QEMU) of vfio needs to have a root permission. Thus it can open the vfio fds. At this point, the user is a privileged one. Also I guess that's why vfio can grant the user with the usage of VFIO_MAP/UNMAP to config mappings into iommu page tables. But I'm not quite sure when will the user be an unprivileged one.
QEMU does NOT need to be run as root to use vfio. This is NOT the model libvirt follows. libvirt grants a user access to a device, or rather a set of one or more devices (ie. the group) via standard file permission access to the group file (/dev/vfio/$GROUP). Ownership of a device allows the user permission to make use of the IOMMU. The user's ability to create DMA mappings is restricted by their process locked memory limits, where libvirt elevates the user limit sufficient for the size of the VM. QEMU should never need to be run as root and doing so is entirely unacceptable from a security perspective. The only mode of vfio that requires elevated privilege for use is when making use of no-iommu, where we have no IOMMU protection or translation.
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
However, we may need to introduce a kind of credit
mechanism to protect it. I've thought it, but no good idea yet. Would be happy to hear from you.
Quoted 8 lines
Quoted 2 lines
It's a limited system resource and it's unclear how many might reasonably used by a user. I don't have an easy answer.
How about the below method? based on some offline chat with Jacob. a. some reasonable defaults for the initial per VM quota, e.g. 1000 per process
b. IOASID should be able to enforce perioasid_set(it is kind of per VM mark) limit
We support large numbers of assigned devices, how many IOASIDs might be reasonably used per device? Is the mm or the task still the correct "set" in this scenario? I don't have any better ideas than setting a limit, but it probably needs a kernel or module tunable, and it needs to match the scaling we expect to see when multiple devices are involved.
Quoted 118 lines
Quoted 6 lines
Quoted 4 lines
For question (2), I think we need to reclaim the allocated pasids when the vfio container fd is released just like what vfio does to the domain mappings. I didn't add it yet. But I can add it in next version if you think it would make the pasid alloc/free be much sound.
Consider it required, the interface is susceptible to abuse without it.
sure, let me add it in next version.
Quoted 109 lines
Quoted 40 lines
Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, · line 2274
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c +7
-0+if (pasid ==INVALID_IOASID ) { + ret = -ENOSPC ; + goto out_unlock; + } + ret = pasid; +out_unlock: +mutex_unlock (&iommu->lock);What does holding this lock protect? That the
vfio_iommuremains backed by an iommu during this operation, even though we don't do anything to release allocated pasids when that iommu backing is removed?yes, it is unnecessary to hold the lock here. At least for the operations in this patch. will remove it. :-)
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
drivers/vfio/vfio_iommu_type1.c +25
-0+if (mm) +mmput (mm); +return ret; +} + +static int vfio_iommu_type1_pasid_free (struct vfio_iommu *iommu, +unsigned int pasid) +{ +struct mm_struct *mm =NULL ; +void *pdata; +int ret =0 ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + +/** +* REVISIT: +* There are two cases free could fail: +* 1. free pasid by non-owner, we use ioasid_set to track mm, if +* the set does not match, caller is not permitted to free. +* 2. free before unbind all devices, we can check if ioasid private +* data, if data != NULL, then fail to free. Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, · line 2307
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c +8
-0+*/ + mm =get_task_mm (current); + pdata =ioasid_find ((struct ioasid_set *)mm, pasid,NULL ); +if (IS_ERR (pdata)) { +if (pdata ==ERR_PTR (-ENOENT )) +pr_err ("PASID %u is not allocated\n" , pasid); +else if (pdata ==ERR_PTR (-EACCES )) + pr_err("Free PASID %u by non-owner, denied",pasid);
Quoted 59 lines
Quoted 40 lines
Quoted 40 lines
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, · line 2309
Quoted 2 lines
drivers/vfio/vfio_iommu_type1.c +2
-0+else +pr_err ("Error searching PASID %u\n" , pasid);This should be removed, errno is sufficient for the user, this just provides the user with a trivial DoS vector filling logs.
sure, will fix it. thanks.
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, · line 2310
Quoted 1 line
drivers/vfio/vfio_iommu_type1.c +1
-0+ ret = -EPERM ;But why not return
PTR_ERR(pdata)?aha, would do it.
Quoted 11 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device *dev, · line 2319
Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c +9
-0+ goto out_unlock; + } +if (pdata) { +pr_debug ("Cannot free pasid %d with private data\n" , pasid); +/* Expect PASID has no private data if not bond */ + ret = -EBUSY ; + goto out_unlock; + } +ioasid_free (pasid);We only ever get here with pasid == NULL?!
I guess you meant only when pdata==NULL.
Quoted 3 lines
Something is wrong. Should
that be 'if (!pdata)'? (which also makes thatpr_debuganother DoS vector)Oh, yes, just do it as below:
if (!pdata) { ioasid_free(pasid); ret = SUCCESS; } else ret = -EBUSY;Quoted 8 lines
Quoted 1 line
Is it what you mean?
No, I think I was just confusing pdata and pasid, but I am still confused about testing pdata. We call
ioasid_alloc()with private = NULL, and I don't see any of your patches callingioasid_set_data()to change the private data after allocation, so how could this ever be set? Should this just be aBUG_ON(pdata)as the integrity of the system is in question should this state ever occur? Thanks,
ioasid_set_data()was called in one patch from Jacob's vSVA patchset. [PATCH v6 08/10] iommu/vt-d: Add bind guest PASID support
https://lkml.org/lkml/2019/10/22/946The basic idea is to allocate pasid with private=NULL, and set it when the pasid is actually bind to a device (
bind_gpasid()). Eachbind_gpasid()will increase theref_cntin the private data, and eachunbind_gpasid()will decrease theref_cnt. So if bind/unbind_gpasid() is called in mirror, the private data should be null when comes to free operation. If not, vfio can believe that the pasid is still in use.
Quoted 7 lines
Quoted 3 lines
So this is another opportunity to leak pasids. What's a user supposed to do when their attempt to free a pasid fails? It invites leaks to allow this path to fail. Thanks,
Agreed, may no need to fail pasid free as it may leak pasid. How about always let free successful? If the
ref_cntis non-zero, notify the remaining users to release their reference.
If a user frees an PASID, they've done their due diligence in indicating it's no longer used. The kernel should handle reclaiming it from that point. Thanks,
Alex
Re: [RFC v2 2/3] vfio/type1: VFIO_IOMMU_PASID_REQUEST(alloc/free)
Quoted 158 lines · On Wed, 13 Nov 2019 08:29:40 -0700 Alex Williamson…
Quoted 121 lines · Yi L
Quoted 97 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Friday, November 8, 2019 11:15 PM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 6 lines · Yi L
Quoted 6 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Friday, November 8, 2019 6:07 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 7 lines
Quoted 7 lines
Quoted 4 lines · Yi L
Quoted 4 lines
From: Alex Williamson <[email protected]>
Sent: Wednesday, November 6, 2019 7:36 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:
VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 74 lines
Quoted 73 lines
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines · On Thu, 24 Oct 2019 08:26:22 -0400 Liu Yi L <[email protected]> wrote:
This patch adds
VFIO_IOMMU_PASID_REQUESTioctl which aims to passdown PASID allocation/free request from the virtual iommu. This is required to get PASID managed in system-wide.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Yi Sun <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 114Quoted 5 lines
Quoted 5 lines
++++++++++++++++++++++++++++++++++++++++
Review
Quoted 4 lines
include/uapi/linux/vfio.h | 25 +++++++++ 2 files changed, 139 insertions(+)(quoted diff) +0
-0
b/drivers/vfio/vfio_iommu_type1.cQuoted 5 lines
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c static int · line 2248
Quoted 5 lines
index cd8d3a5..3d73a7d 100644
drivers/vfio/vfio_iommu_type1.c +0
-0@@ -2248,6 +2248,83 @@ static int vfio_cache_inv_fn(struct device *dev,void
Quoted 27 lines
Quoted 18 lines
*data)
Quoted 17 lines
drivers/vfio/vfio_iommu_type1.c +13
-0return iommu_cache_invalidate (dc->domain, dev, &ustruct->info); } +static int vfio_iommu_type1_pasid_alloc (struct vfio_iommu *iommu, +int min_pasid, +int max_pasid) +{ +int ret; +ioasid_t pasid; +struct mm_struct *mm =NULL ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + }Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int · line 2270
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +4
-0+ mm =get_task_mm (current); +/* Track ioasid allocation owner by mm */ + pasid =ioasid_alloc ((struct ioasid_set *)mm, min_pasid, + max_pasid,NULL );Are we sure we want to tie this to the task mm vs perhaps the
vfio_iommupointer?Quoted 14 lines
Here we want to have a kind of per-VM mark, which can be used to do ownership check on whether a pasid is held by a specific VM. This is very important to prevent across VM affect.
vfio_iommupointer is competent for vfio as vfio is both pasid alloc requester and pasid consumer. e.g. vfio requests pasid alloc from ioasid and also it will invokebind_gpasid(). vfio can either check ownership before invokingbind_gpasid()or passvfio_iommupointer to iommu driver. But in future, there may be other modules which are just consumers of pasid. And they also want to do ownership check for a pasid. Then, it would be hard for them as they are not the pasid alloc requester. So here better to have a system wide structure to perform as the per-VM mark. task mm looks to be much competent.Ok, so it's intentional to have a VM-wide token. Elsewhere in the type1 code (
vfio_dma_do_map) we record thetask_structper dma mapping so that we can get the task mm as needed. Would thetask_structpointer provide any advantage?Quoted 4 lines
Quoted 2 lines
I think we may use
task_structpointer to make type1 code consistent. How do you think?If it has the same utility, sure.
thanks, I'll make this change.
Quoted 10 lines
Quoted 9 lines
Quoted 5 lines
Also, an overall question, this provides userspace with pasid alloc and free ioctls, (1) what prevents a userspace process from consuming every available pasid, and (2) if the process exits or crashes without freeing pasids, how are they recovered aside from a reboot?
For question (1), I think we only need to take care about malicious userspace process. As vfio usage is under privilege mode, so we may be safe on it so far.
Quoted 6 lines
No, where else do we ever make this assumption? vfio requires a privileged entity to configure the system for vfio, bind devices for user use, and grant those devices to the user, but the usage of the device is always assumed to be by an unprivileged user. It is absolutely not acceptable require a privileged user. It's vfio's responsibility to protect the system from the user.
Quoted 7 lines
My assumption is not precise here. sorry for it... Maybe to further check with you to better understand your point. I think the user (QEMU) of vfio needs to have a root permission. Thus it can open the vfio fds. At this point, the user is a privileged one. Also I guess that's why vfio can grant the user with the usage of VFIO_MAP/UNMAP to config mappings into iommu page tables. But I'm not quite sure when will the user be an unprivileged one.
QEMU does NOT need to be run as root to use vfio. This is NOT the model libvirt follows. libvirt grants a user access to a device, or rather a set of one or more devices (ie. the group) via standard file permission access to the group file (
/dev/vfio/$GROUP). Ownership of a device allows the user permission to make use of the IOMMU. The user's ability to create DMA mappings is restricted by their process locked memory limits, where libvirt elevates the user limit sufficient for the size of the VM. QEMU should never need to be run as root and doing so is entirely unacceptable from a security perspective. The only mode of vfio that requires elevated privilege for use is when making use of no-iommu, where we have no IOMMU protection or translation.Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
However, we may need to introduce a kind of credit mechanism to protect it. I've thought it, but no good idea yet. Would be happy to hear from you.
Quoted 8 lines
Quoted 2 lines
It's a limited system resource and it's unclear how many might reasonably used by a user. I don't have an easy answer.
How about the below method? based on some offline chat with Jacob. a. some reasonable defaults for the initial per VM quota, e.g. 1000 per process
b. IOASID should be able to enforce perioasid_set(it is kind of per VM mark) limit
Quoted 7 lines
We support large numbers of assigned devices, how many IOASIDs might be reasonably used per device? Is the mm or the task still the correct "set" in this scenario? I don't have any better ideas than setting a limit, but it probably needs a kernel or module tunable, and it needs to match the scaling we expect to see when multiple devices are involved.
I think mm/task is still the correct set in that we try to prevent abuse based on mm not device. Or we need to have some notion of super container (Ashok proposed a while ago) that maps to a VM.
I am guessing you are suggesting the per mm quota should also be scaled against number of devices assigned. I think that is very reasonable. Perhaps we can do:
1. A tunable per iommu group PASID quota with a default of 1000, e.g. nr_pasid_per_group. Since we are dealing with nested translation and each device has its own second level so we pretty much have one device per group. Call it nr_pasid_per_group?
2. Limit number of PASIDs per VM with nr_pasid_per_group * nr_groups. Probably update the limit when group is added to a container with the same mm.
I guess we could also use a cgroup controller for PASIDs.
Quoted 138 lines
Quoted 138 lines
Quoted 8 lines
Quoted 5 lines
For question (2), I think we need to reclaim the allocated pasids when the vfio container fd is released just like what vfio does to the domain mappings. I didn't add it yet. But I can add it in next version if you think it would make the pasid alloc/free be much sound.
Consider it required, the interface is susceptible to abuse without it.
sure, let me add it in next version.
Quoted 126 lines
Quoted 48 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +4
-0+if (pasid ==INVALID_IOASID ) { + ret = -ENOSPC ; + goto out_unlock; + }Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c static int · line 2277
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c +3
-0+ ret = pasid; +out_unlock: +mutex_unlock (&iommu->lock);What does holding this lock protect? That the
vfio_iommuremains backed by an iommu during this operation, even though we don't do anything to release allocated pasids when that iommu backing is removed?yes, it is unnecessary to hold the lock here. At least for the operations in this patch. will remove it. :-)
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
drivers/vfio/vfio_iommu_type1.c +6
-0+if (mm) +mmput (mm); +return ret; +} + +static int vfio_iommu_type1_pasid_free(struct
vfio_iommu*iommu,drivers/vfio/vfio_iommu_type1.c +1
-0+unsigned int pasid) +{
drivers/vfio/vfio_iommu_type1.c +14
-0+struct mm_struct *mm =NULL ; +void *pdata; +int ret =0 ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + +/** +* REVISIT: +* There are two cases free could fail: +* 1. free pasid by non-owner, we use
ioasid_setto track mm, ifdrivers/vfio/vfio_iommu_type1.c +1
-0+* the set does not match, caller is not permitted to free.
drivers/vfio/vfio_iommu_type1.c +1
-0+* 2. free before unbind all devices, we can check if ioasid private
drivers/vfio/vfio_iommu_type1.c +4
-0+* data, if data != NULL, then fail to free. +*/ + mm =get_task_mm (current); + pdata = ioasid_find((struct ioasid_set *)mm,Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
pasid, NULL);
drivers/vfio/vfio_iommu_type1.c +3
-0+if (IS_ERR (pdata)) { +if (pdata ==ERR_PTR (-ENOENT )) + pr_err("PASID %u is notallocated\n", pasid);
drivers/vfio/vfio_iommu_type1.c +2
-0+else if (pdata ==ERR_PTR (-EACCES )) + pr_err("Free PASID %u bynon-owner, denied",
pasid);
Quoted 58 lines
Quoted 46 lines
Quoted 38 lines
Quoted 7 lines
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c +2
-0+else + pr_err("Error searching PASID%u\n", pasid);
This should be removed, errno is sufficient for the user, this just provides the user with a trivial DoS vector filling logs.
sure, will fix it. thanks.
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c
Quoted 1 line
drivers/vfio/vfio_iommu_type1.c +1
-0+ ret = -EPERM ;But why not return
PTR_ERR(pdata)?aha, would do it.
Quoted 5 lines
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +4
-0+ goto out_unlock; + } +if (pdata) { + pr_debug("Cannot free pasid %d withprivate data\n", pasid);
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c
Quoted 6 lines
drivers/vfio/vfio_iommu_type1.c +1
-0+/* Expect PASID has no private data if not bond */
drivers/vfio/vfio_iommu_type1.c +4
-0+ ret = -EBUSY ; + goto out_unlock; + } +ioasid_free (pasid);We only ever get here with pasid == NULL?!
I guess you meant only when pdata==NULL.
Quoted 3 lines
Something is wrong. Should
that be 'if (!pdata)'? (which also makes thatpr_debuganother DoS vector)Oh, yes, just do it as below:
Quoted 7 lines
if (!pdata) { ioasid_free(pasid); ret = SUCCESS; } else ret = -EBUSY;Is it what you mean?
Quoted 7 lines
No, I think I was just confusing pdata and pasid, but I am still confused about testing pdata. We call
ioasid_alloc()with private = NULL, and I don't see any of your patches callingioasid_set_data()to change the private data after allocation, so how could this ever be set? Should this just be aBUG_ON(pdata)as the integrity of the system is in question should this state ever occur? Thanks,
ioasid_set_data()was called in one patch from Jacob's vSVA patchset. [PATCH v6 08/10] iommu/vt-d: Add bind guest PASID support https://lkml.org/lkml/2019/10/22/946Quoted 7 lines
The basic idea is to allocate pasid with private=NULL, and set it when the pasid is actually bind to a device (
bind_gpasid()). Eachbind_gpasid()will increase theref_cntin the private data, and eachunbind_gpasid()will decrease theref_cnt. So if bind/unbind_gpasid() is called in mirror, the private data should be null when comes to free operation. If not, vfio can believe that the pasid is still in use.So this is another opportunity to leak pasids. What's a user supposed to do when their attempt to free a pasid fails? It invites leaks to allow this path to fail. Thanks,
Quoted 7 lines
Quoted 3 lines
Agreed, may no need to fail pasid free as it may leak pasid. How about always let free successful? If the
ref_cntis non-zero, notify the remaining users to release their reference.If a user frees an PASID, they've done their due diligence in indicating it's no longer used. The kernel should handle reclaiming it from that point. Thanks,
Yeah, I think we can add a atomic notifier for each PASID. Consumers such as IOMMU driver and KVM gets notified when IOASID is freed by VFIO. IOMMU driver can do the unbind and tear down.
In case of all the users already did unbind() before ioasid_free(), the free will proceed as usual.
Quoted 2 lines
Alex
RE: [RFC v2 2/3] vfio/type1: VFIO_IOMMU_PASID_REQUEST(alloc/free)
Hi Alex,
I think the major opens in this thread are the pasid allocation limit for each VM and pasid lifecycle management. For lifecycle management, we will free pasid when userspace requests and reclaim pasids when VM is crashed. For the pasid allocation limit, may have a tunable quota and scaled per assigned device number. If no apparent issue, we may prepare a version to see if it is workable.
Thanks,
Yi Liu
Quoted 352 lines
From: Jacob Pan [mailto:[email protected]]
Sent: Thursday, November 14, 2019 3:45 AM
To: Alex Williamson <[email protected]>
Cc: Liu, Yi L <[email protected]>; [email protected]; Tian, Kevin <[email protected]>; [email protected]; Raj, Ashok <[email protected]>; Tian, Jun J <[email protected]>; Sun, Yi Y <[email protected]>; jean- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 158 lines · On Wed, 13 Nov 2019 08:29:40 -0700 Alex Williamson…
Quoted 121 lines · Yi L
Quoted 97 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Friday, November 8, 2019 11:15 PM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 6 lines · Yi L
Quoted 6 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Friday, November 8, 2019 6:07 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 7 lines
Quoted 7 lines
Quoted 4 lines · Yi L
Quoted 4 lines
From: Alex Williamson <[email protected]>
Sent: Wednesday, November 6, 2019 7:36 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:
VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 74 lines
Quoted 73 lines
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines · On Thu, 24 Oct 2019 08:26:22 -0400 Liu Yi L <[email protected]> wrote:
This patch adds
VFIO_IOMMU_PASID_REQUESTioctl which aims to passdown PASID allocation/free request from the virtual iommu. This is required to get PASID managed in system-wide.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Yi Sun <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 114Quoted 5 lines
Quoted 5 lines
++++++++++++++++++++++++++++++++++++++++
Review
Quoted 4 lines
include/uapi/linux/vfio.h | 25 +++++++++ 2 files changed, 139 insertions(+)(quoted diff) +0
-0
b/drivers/vfio/vfio_iommu_type1.cQuoted 5 lines
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c static int · line 2248
Quoted 5 lines
index cd8d3a5..3d73a7d 100644
drivers/vfio/vfio_iommu_type1.c +0
-0@@ -2248,6 +2248,83 @@ static int vfio_cache_inv_fn(struct device *dev,void
Quoted 27 lines
Quoted 18 lines
*data)
Quoted 17 lines
drivers/vfio/vfio_iommu_type1.c +13
-0return iommu_cache_invalidate (dc->domain, dev, &ustruct->info); } +static int vfio_iommu_type1_pasid_alloc (struct vfio_iommu *iommu, +int min_pasid, +int max_pasid) +{ +int ret; +ioasid_t pasid; +struct mm_struct *mm =NULL ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + }Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int · line 2270
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +4
-0+ mm =get_task_mm (current); +/* Track ioasid allocation owner by mm */ + pasid =ioasid_alloc ((struct ioasid_set *)mm, min_pasid, + max_pasid,NULL );Are we sure we want to tie this to the task mm vs perhaps the
vfio_iommupointer?Quoted 14 lines
Here we want to have a kind of per-VM mark, which can be used to do ownership check on whether a pasid is held by a specific VM. This is very important to prevent across VM affect.
vfio_iommupointer is competent for vfio as vfio is both pasid alloc requester and pasid consumer. e.g. vfio requests pasid alloc from ioasid and also it will invokebind_gpasid(). vfio can either check ownership before invokingbind_gpasid()or passvfio_iommupointer to iommu driver. But in future, there may be other modules which are just consumers of pasid. And they also want to do ownership check for a pasid. Then, it would be hard for them as they are not the pasid alloc requester. So here better to have a system wide structure to perform as the per-VM mark. task mm looks to be much competent.Ok, so it's intentional to have a VM-wide token. Elsewhere in the type1 code (
vfio_dma_do_map) we record thetask_structper dma mapping so that we can get the task mm as needed. Would thetask_structpointer provide any advantage?Quoted 4 lines
Quoted 2 lines
I think we may use
task_structpointer to make type1 code consistent. How do you think?If it has the same utility, sure.
thanks, I'll make this change.
Quoted 10 lines
Quoted 9 lines
Quoted 5 lines
Also, an overall question, this provides userspace with pasid alloc and free ioctls, (1) what prevents a userspace process from consuming every available pasid, and (2) if the process exits or crashes without freeing pasids, how are they recovered aside from a reboot?
For question (1), I think we only need to take care about malicious userspace process. As vfio usage is under privilege mode, so we may be safe on it so far.
Quoted 6 lines
No, where else do we ever make this assumption? vfio requires a privileged entity to configure the system for vfio, bind devices for user use, and grant those devices to the user, but the usage of the device is always assumed to be by an unprivileged user. It is absolutely not acceptable require a privileged user. It's vfio's responsibility to protect the system from the user.
Quoted 7 lines
My assumption is not precise here. sorry for it... Maybe to further check with you to better understand your point. I think the user (QEMU) of vfio needs to have a root permission. Thus it can open the vfio fds. At this point, the user is a privileged one. Also I guess that's why vfio can grant the user with the usage of VFIO_MAP/UNMAP to config mappings into iommu page tables. But I'm not quite sure when will the user be an unprivileged one.
QEMU does NOT need to be run as root to use vfio. This is NOT the model libvirt follows. libvirt grants a user access to a device, or rather a set of one or more devices (ie. the group) via standard file permission access to the group file (
/dev/vfio/$GROUP). Ownership of a device allows the user permission to make use of the IOMMU. The user's ability to create DMA mappings is restricted by their process locked memory limits, where libvirt elevates the user limit sufficient for the size of the VM. QEMU should never need to be run as root and doing so is entirely unacceptable from a security perspective. The only mode of vfio that requires elevated privilege for use is when making use of no-iommu, where we have no IOMMU protection or translation.Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
However, we may need to introduce a kind of credit mechanism to protect it. I've thought it, but no good idea yet. Would be happy to hear from you.
Quoted 8 lines
Quoted 2 lines
It's a limited system resource and it's unclear how many might reasonably used by a user. I don't have an easy answer.
How about the below method? based on some offline chat with Jacob. a. some reasonable defaults for the initial per VM quota, e.g. 1000 per process
b. IOASID should be able to enforce perioasid_set(it is kind of per VM mark) limitQuoted 7 lines
We support large numbers of assigned devices, how many IOASIDs might be reasonably used per device? Is the mm or the task still the correct "set" in this scenario? I don't have any better ideas than setting a limit, but it probably needs a kernel or module tunable, and it needs to match the scaling we expect to see when multiple devices are involved.
I think mm/task is still the correct set in that we try to prevent abuse based on mm not device. Or we need to have some notion of super container (Ashok proposed a while ago) that maps to a VM.
I am guessing you are suggesting the per mm quota should also be scaled against number of devices assigned. I think that is very reasonable. Perhaps we can do:
1. A tunable per iommu group PASID quota with a default of 1000, e.g.
nr_pasid_per_group. Since we are dealing with nested translation and each device has its own second level so we pretty much have one device per group. Call itnr_pasid_per_group?2. Limit number of PASIDs per VM with
nr_pasid_per_group*nr_groups. Probably update the limit when group is added to a container with the same mm.I guess we could also use a cgroup controller for PASIDs.
Quoted 138 lines
Quoted 138 lines
Quoted 8 lines
Quoted 5 lines
For question (2), I think we need to reclaim the allocated pasids when the vfio container fd is released just like what vfio does to the domain mappings. I didn't add it yet. But I can add it in next version if you think it would make the pasid alloc/free be much sound.
Consider it required, the interface is susceptible to abuse without it.
sure, let me add it in next version.
Quoted 126 lines
Quoted 48 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +4
-0+if (pasid ==INVALID_IOASID ) { + ret = -ENOSPC ; + goto out_unlock; + }Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c static int · line 2277
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c +3
-0+ ret = pasid; +out_unlock: +mutex_unlock (&iommu->lock);What does holding this lock protect? That the
vfio_iommuremains backed by an iommu during this operation, even though we don't do anything to release allocated pasids when that iommu backing is removed?yes, it is unnecessary to hold the lock here. At least for the operations in this patch. will remove it. :-)
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
drivers/vfio/vfio_iommu_type1.c +6
-0+if (mm) +mmput (mm); +return ret; +} + +static int vfio_iommu_type1_pasid_free(struct
vfio_iommu*iommu,drivers/vfio/vfio_iommu_type1.c +1
-0+unsigned int pasid) +{
drivers/vfio/vfio_iommu_type1.c +14
-0+struct mm_struct *mm =NULL ; +void *pdata; +int ret =0 ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + +/** +* REVISIT: +* There are two cases free could fail: +* 1. free pasid by non-owner, we use
ioasid_setto track mm, ifdrivers/vfio/vfio_iommu_type1.c +1
-0+* the set does not match, caller is not permitted to free.
drivers/vfio/vfio_iommu_type1.c +1
-0+* 2. free before unbind all devices, we can check if ioasid private
drivers/vfio/vfio_iommu_type1.c +4
-0+* data, if data != NULL, then fail to free. +*/ + mm =get_task_mm (current); + pdata = ioasid_find((struct ioasid_set *)mm,Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
pasid, NULL);
drivers/vfio/vfio_iommu_type1.c +3
-0+if (IS_ERR (pdata)) { +if (pdata ==ERR_PTR (-ENOENT )) + pr_err("PASID %u is notallocated\n", pasid);
drivers/vfio/vfio_iommu_type1.c +2
-0+else if (pdata ==ERR_PTR (-EACCES )) + pr_err("Free PASID %u bynon-owner, denied",
pasid);
Quoted 58 lines
Quoted 46 lines
Quoted 38 lines
Quoted 7 lines
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c +2
-0+else + pr_err("Error searching PASID%u\n", pasid);
This should be removed, errno is sufficient for the user, this just provides the user with a trivial DoS vector filling logs.
sure, will fix it. thanks.
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c
Quoted 1 line
drivers/vfio/vfio_iommu_type1.c +1
-0+ ret = -EPERM ;But why not return
PTR_ERR(pdata)?aha, would do it.
Quoted 5 lines
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +4
-0+ goto out_unlock; + } +if (pdata) { + pr_debug("Cannot free pasid %d withprivate data\n", pasid);
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c
Quoted 6 lines
drivers/vfio/vfio_iommu_type1.c +1
-0+/* Expect PASID has no private data if not bond */
drivers/vfio/vfio_iommu_type1.c +4
-0+ ret = -EBUSY ; + goto out_unlock; + } +ioasid_free (pasid);We only ever get here with pasid == NULL?!
I guess you meant only when pdata==NULL.
Quoted 3 lines
Something is wrong. Should
that be 'if (!pdata)'? (which also makes thatpr_debuganother DoS vector)Oh, yes, just do it as below:
Quoted 7 lines
if (!pdata) { ioasid_free(pasid); ret = SUCCESS; } else ret = -EBUSY;Is it what you mean?
Quoted 7 lines
No, I think I was just confusing pdata and pasid, but I am still confused about testing pdata. We call
ioasid_alloc()with private = NULL, and I don't see any of your patches callingioasid_set_data()to change the private data after allocation, so how could this ever be set? Should this just be aBUG_ON(pdata)as the integrity of the system is in question should this state ever occur? Thanks,
ioasid_set_data()was called in one patch from Jacob's vSVA patchset. [PATCH v6 08/10] iommu/vt-d: Add bind guest PASID support https://lkml.org/lkml/2019/10/22/946Quoted 7 lines
The basic idea is to allocate pasid with private=NULL, and set it when the pasid is actually bind to a device (
bind_gpasid()). Eachbind_gpasid()will increase theref_cntin the private data, and eachunbind_gpasid()will decrease theref_cnt. So if bind/unbind_gpasid() is called in mirror, the private data should be null when comes to free operation. If not, vfio can believe that the pasid is still in use.So this is another opportunity to leak pasids. What's a user supposed to do when their attempt to free a pasid fails? It invites leaks to allow this path to fail. Thanks,
Quoted 7 lines
Quoted 3 lines
Agreed, may no need to fail pasid free as it may leak pasid. How about always let free successful? If the
ref_cntis non-zero, notify the remaining users to release their reference.If a user frees an PASID, they've done their due diligence in indicating it's no longer used. The kernel should handle reclaiming it from that point. Thanks,
Yeah, I think we can add a atomic notifier for each PASID. Consumers such as IOMMU driver and KVM gets notified when IOASID is freed by VFIO. IOMMU driver can do the unbind and tear down.
In case of all the users already did
unbind()beforeioasid_free(), the free will proceed as usual.Quoted 2 lines
Alex
RE: [RFC v2 2/3] vfio/type1: VFIO_IOMMU_PASID_REQUEST(alloc/free)
In reply to Alex WilliamsonQuoted 130 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Wednesday, November 13, 2019 11:30 PM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 5 lines · Yi L
Quoted 5 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Friday, November 8, 2019 11:15 PM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 7 lines
Quoted 7 lines
Quoted 4 lines · Yi L
Quoted 4 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Friday, November 8, 2019 6:07 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:
VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 27 lines
Quoted 27 lines
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines · Yi L
Quoted 4 lines
From: Alex Williamson <[email protected]>
Sent: Wednesday, November 6, 2019 7:36 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 2/3] vfio/type1:
VFIO_IOMMU_PASID_REQUEST(alloc/free)Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 10 lines · On Thu, 24 Oct 2019 08:26:22 -0400 Liu Yi L <[email protected]> wrote:
This patch adds
VFIO_IOMMU_PASID_REQUESTioctl which aims to passdown PASID allocation/free request from the virtual iommu. This is required to get PASID managed in system-wide.Cc Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Yi Sun <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 114++++++++++++++++++++++++++++++++++++++++
Quoted 3 lines
include/uapi/linux/vfio.h | 25 +++++++++ 2 files changed, 139 insertions(+)Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +0
-0
b/drivers/vfio/vfio_iommu_type1.cQuoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device
Quoted 4 lines
index cd8d3a5..3d73a7d 100644
drivers/vfio/vfio_iommu_type1.c +0
-0@@ -2248,6 +2248,83 @@ static int vfio_cache_inv_fn(struct device*dev,
Quoted 67 lines
Quoted 44 lines
Quoted 44 lines
Quoted 43 lines
void
Quoted 25 lines
Quoted 16 lines
*data)
Quoted 15 lines
drivers/vfio/vfio_iommu_type1.c +12
-0return iommu_cache_invalidate (dc->domain, dev, &ustruct->info); } +static int vfio_iommu_type1_pasid_alloc (struct vfio_iommu *iommu, +int min_pasid, +int max_pasid) +{ +int ret; +ioasid_t pasid; +struct mm_struct *mm =NULL ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock;Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device · line 2267
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+ } + mm =get_task_mm (current); +/* Track ioasid allocation owner by mm */ + pasid =ioasid_alloc ((struct ioasid_set *)mm, min_pasid, + max_pasid,NULL );Are we sure we want to tie this to the task mm vs perhaps the
vfio_iommupointer?Quoted 12 lines
Here we want to have a kind of per-VM mark, which can be used to do ownership check on whether a pasid is held by a specific VM. This is very important to prevent across VM affect.
vfio_iommupointer is competent for vfio as vfio is both pasid alloc requester and pasid consumer. e.g. vfio requests pasid alloc from ioasid and also it will invokebind_gpasid(). vfio can either check ownership before invokingbind_gpasid()or passvfio_iommupointer to iommu driver. But in future, there may be other modules which are just consumers of pasid. And they also want to do ownership check for a pasid. Then, it would be hard for them as they are not the pasid alloc requester. So here better to have a system wide structure to perform as the per-VM mark. task mm looks to be much competent.Ok, so it's intentional to have a VM-wide token. Elsewhere in the type1 code (
vfio_dma_do_map) we record thetask_structper dma mapping so that we can get the task mm as needed. Would thetask_structpointer provide any advantage?Quoted 4 lines
Quoted 2 lines
I think we may use
task_structpointer to make type1 code consistent. How do you think?If it has the same utility, sure.
thanks, I'll make this change.
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines
Also, an overall question, this provides userspace with pasid alloc and free ioctls, (1) what prevents a userspace process from consuming every available pasid, and (2) if the process exits or crashes without freeing pasids, how are they recovered aside from a reboot?
For question (1), I think we only need to take care about malicious userspace process. As vfio usage is under privilege mode, so we may be safe on it so far.
Quoted 6 lines
No, where else do we ever make this assumption? vfio requires a privileged entity to configure the system for vfio, bind devices for user use, and grant those devices to the user, but the usage of the device is always assumed to be by an unprivileged user. It is absolutely not acceptable require a privileged user. It's vfio's responsibility to protect the system from the user.
Quoted 7 lines
My assumption is not precise here. sorry for it... Maybe to further check with you to better understand your point. I think the user (QEMU) of vfio needs to have a root permission. Thus it can open the vfio fds. At this point, the user is a privileged one. Also I guess that's why vfio can grant the user with the usage of VFIO_MAP/UNMAP to config mappings into iommu page tables. But I'm not quite sure when will the user be an unprivileged one.
Quoted 11 lines
QEMU does NOT need to be run as root to use vfio. This is NOT the model libvirt follows. libvirt grants a user access to a device, or rather a set of one or more devices (ie. the group) via standard file permission access to the group file (
/dev/vfio/$GROUP). Ownership of a device allows the user permission to make use of the IOMMU. The user's ability to create DMA mappings is restricted by their process locked memory limits, where libvirt elevates the user limit sufficient for the size of the VM. QEMU should never need to be run as root and doing so is entirely unacceptable from a security perspective. The only mode of vfio that requires elevated privilege for use is when making use of no-iommu, where we have no IOMMU protection or translation.
got it. thanks for the detailed explanation.
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
However, we may need to introduce a kind of credit
mechanism to protect it. I've thought it, but no good idea yet. Would be happy to hear from you.Quoted 8 lines
Quoted 2 lines
It's a limited system resource and it's unclear how many might reasonably used by a user. I don't have an easy answer.
How about the below method? based on some offline chat with Jacob. a. some reasonable defaults for the initial per VM quota, e.g. 1000 per process
b. IOASID should be able to enforce perioasid_set(it is kind of per VM mark) limit
Quoted 6 lines
We support large numbers of assigned devices, how many IOASIDs might be reasonably used per device? Is the mm or the task still the correct "set" in this scenario? I don't have any better ideas than setting a limit, but it probably needs a kernel or module tunable, and it needs to match the scaling we expect to see when multiple devices are involved.
How about Jacob's proposal in his reply?
Quoted 122 lines
Quoted 122 lines
Quoted 6 lines
Quoted 4 lines
For question (2), I think we need to reclaim the allocated pasids when the vfio container fd is released just like what vfio does to the domain mappings. I didn't add it yet. But I can add it in next version if you think it would make the pasid alloc/free be much sound.
Consider it required, the interface is susceptible to abuse without it.
sure, let me add it in next version.
Quoted 112 lines
Quoted 40 lines
Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device · line 2274
Quoted 7 lines
drivers/vfio/vfio_iommu_type1.c +7
-0+if (pasid ==INVALID_IOASID ) { + ret = -ENOSPC ; + goto out_unlock; + } + ret = pasid; +out_unlock: +mutex_unlock (&iommu->lock);What does holding this lock protect? That the
vfio_iommuremains backed by an iommu during this operation, even though we don't do anything to release allocated pasids when that iommu backing is removed?yes, it is unnecessary to hold the lock here. At least for the operations in this patch. will remove it. :-)
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
drivers/vfio/vfio_iommu_type1.c +25
-0+if (mm) +mmput (mm); +return ret; +} + +static int vfio_iommu_type1_pasid_free (struct vfio_iommu *iommu, +unsigned int pasid) +{ +struct mm_struct *mm =NULL ; +void *pdata; +int ret =0 ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + +/** +* REVISIT: +* There are two cases free could fail: +* 1. free pasid by non-owner, we use ioasid_set to track mm, if +* the set does not match, caller is not permitted to free. +* 2. free before unbind all devices, we can check if ioasid private +* data, if data != NULL, then fail to free. Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device · line 2307
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c +8
-0+*/ + mm =get_task_mm (current); + pdata =ioasid_find ((struct ioasid_set *)mm, pasid,NULL ); +if (IS_ERR (pdata)) { +if (pdata ==ERR_PTR (-ENOENT )) +pr_err ("PASID %u is not allocated\n" , pasid); +else if (pdata ==ERR_PTR (-EACCES )) + pr_err("Free PASID %u by non-owner, denied",pasid);
Quoted 53 lines
Quoted 40 lines
Quoted 40 lines
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device · line 2309
Quoted 2 lines
drivers/vfio/vfio_iommu_type1.c +2
-0+else +pr_err ("Error searching PASID %u\n" , pasid);This should be removed, errno is sufficient for the user, this just provides the user with a trivial DoS vector filling logs.
sure, will fix it. thanks.
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device · line 2310
Quoted 1 line
drivers/vfio/vfio_iommu_type1.c +1
-0+ ret = -EPERM ;But why not return
PTR_ERR(pdata)?aha, would do it.
Quoted 11 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_cache_inv_fn(struct device · line 2319
Quoted 9 lines
drivers/vfio/vfio_iommu_type1.c +9
-0+ goto out_unlock; + } +if (pdata) { +pr_debug ("Cannot free pasid %d with private data\n" , pasid); +/* Expect PASID has no private data if not bond */ + ret = -EBUSY ; + goto out_unlock; + } +ioasid_free (pasid);We only ever get here with pasid == NULL?!
I guess you meant only when pdata==NULL.
Quoted 3 lines
Something is wrong. Should
that be 'if (!pdata)'? (which also makes thatpr_debuganother DoS vector)Oh, yes, just do it as below:
if (!pdata) { ioasid_free(pasid); ret = SUCCESS; } else ret = -EBUSY;Quoted 8 lines
Quoted 1 line
Is it what you mean?
No, I think I was just confusing pdata and pasid, but I am still confused about testing pdata. We call
ioasid_alloc()with private = NULL, and I don't see any of your patches callingioasid_set_data()to change the private data after allocation, so how could this ever be set? Should this just be aBUG_ON(pdata)as the integrity of the system is in question should this state ever occur? Thanks,
ioasid_set_data()was called in one patch from Jacob's vSVA patchset. [PATCH v6 08/10] iommu/vt-d: Add bind guest PASID support
https://lkml.org/lkml/2019/10/22/946Quoted 6 lines
The basic idea is to allocate pasid with private=NULL, and set it when the pasid is actually bind to a device (
bind_gpasid()). Eachbind_gpasid()will increase theref_cntin the private data, and eachunbind_gpasid()will decrease theref_cnt. So if bind/unbind_gpasid() is called in mirror, the private data should be null when comes to free operation. If not, vfio can believe that the pasid is still in use.So this is another opportunity to leak pasids. What's a user supposed to do when their attempt to free a pasid fails? It invites leaks to allow this path to fail. Thanks,
Quoted 7 lines
Quoted 3 lines
Agreed, may no need to fail pasid free as it may leak pasid. How about always let free successful? If the
ref_cntis non-zero, notify the remaining users to release their reference.If a user frees an PASID, they've done their due diligence in indicating it's no longer used. The kernel should handle reclaiming it from that point. Thanks,
Yes, I've aligned with Jacob offline. Will free PASID per requested, no fail. Jacob will help to add notifications in ioasid.
Quoted 1 line
Alex
Thanks,
Yi Liu
RFC v2 3/3 vfio/type1: bind guest pasid (guest page tables) to host
In reply to Liu Yi LThis patch adds vfio support to bind guest translation structure to host iommu. VFIO exposes iommu programming capability to user- space. Guest is a user-space application in host under KVM solution. For SVA usage in Virtual Machine, guest owns GVA->GPA translation structure. And this part should be passdown to host to enable nested translation (or say two stage translation). This patch reuses the VFIO_IOMMU_BIND proposal from Jean-Philippe Brucker, and adds new bind type for binding guest owned translation structure to host.
*) Add two new ioctls for VFIO containers.
- VFIO_IOMMU_BIND: for bind request from userspace, it could be bind a process to a pasid or bind a guest pasid to a device, this is indicated by type
- VFIO_IOMMU_UNBIND: for unbind request from userspace, it could be unbind a process to a pasid or unbind a guest pasid to a device, also indicated by type
- Bind type:
VFIO_IOMMU_BIND_PROCESS: user-space request to bind a process
to a device
VFIO_IOMMU_BIND_GUEST_PASID: bind guest owned translation
structure to host iommu. e.g. guest page table
*) Code logic in vfio_iommu_type1_ioctl() to handle VFIO_IOMMU_BIND/UNBIND
--- drivers/vfio/vfio_iommu_type1.c | 136 ++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 44 +++++++++++++ 2 files changed, 180 insertions(+)
drivers/vfio/vfio_iommu_type1.c +136 -0
@@ -2325,6 +2325,104 @@ static int vfio_iommu_type1_pasid_free(struct vfio_iommu *iommu,return ret; } +static int vfio_bind_gpasid_fn (struct device *dev,void *data) +{ +struct domain_capsule *dc = (struct domain_capsule *)data; +struct iommu_gpasid_bind_data *ustruct = + (struct iommu_gpasid_bind_data *) dc->data; + +return iommu_sva_bind_gpasid (dc->domain, dev, ustruct); +} + +static int vfio_unbind_gpasid_fn (struct device *dev,void *data) +{ +struct domain_capsule *dc = (struct domain_capsule *)data; +struct iommu_gpasid_bind_data *ustruct = + (struct iommu_gpasid_bind_data *) dc->data; + +return iommu_sva_unbind_gpasid (dc->domain, dev, + ustruct->hpasid); +} + +/* +* unbind specific gpasid, caller of this function requires hold +* vfio_iommu->lock +*/ +static long vfio_iommu_type1_do_guest_unbind (struct vfio_iommu *iommu, +struct iommu_gpasid_bind_data *gbind_data) +{ +return vfio_iommu_lookup_dev (iommu, vfio_unbind_gpasid_fn, gbind_data); +} + +static long vfio_iommu_type1_bind_gpasid (struct vfio_iommu *iommu, +void __user *arg, +struct vfio_iommu_type1_bind *bind) +{ +struct iommu_gpasid_bind_data gbind_data; +unsigned long minsz; +int ret =0 ; + + minsz =sizeof (*bind) +sizeof (gbind_data); +if (bind->argsz < minsz) +return -EINVAL ; + +if (copy_from_user (&gbind_data, arg,sizeof (gbind_data))) +return -EFAULT ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + + ret =vfio_iommu_lookup_dev (iommu, vfio_bind_gpasid_fn, &gbind_data); +/* +* If bind failed, it may not be a total failure. Some devices within +* the iommu group may have bind successfully. Although we don't enable +* pasid capability for non-singletion iommu groups, a unbind operation +* would be helpful to ensure no partial binding for an iommu group. +*/ +if (ret) +/* +* Undo all binds that already succeeded, no need to check the +* return value here since some device within the group has no +* successful bind when coming to this place switch. +*/ +vfio_iommu_type1_do_guest_unbind (iommu, &gbind_data); + +out_unlock: +mutex_unlock (&iommu->lock); +return ret; +} + +static long vfio_iommu_type1_unbind_gpasid (struct vfio_iommu *iommu, +void __user *arg, +struct vfio_iommu_type1_bind *bind) +{ +struct iommu_gpasid_bind_data gbind_data; +unsigned long minsz; +int ret =0 ; + + minsz =sizeof (*bind) +sizeof (gbind_data); +if (bind->argsz < minsz) +return -EINVAL ; + +if (copy_from_user (&gbind_data, arg,sizeof (gbind_data))) +return -EFAULT ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + + ret =vfio_iommu_type1_do_guest_unbind (iommu, &gbind_data); + +out_unlock: +mutex_unlock (&iommu->lock); +return ret; +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg) {
@@ -2484,6 +2582,44 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,default :return -EINVAL ; } + + }else if (cmd ==VFIO_IOMMU_BIND ) { +struct vfio_iommu_type1_bind bind; + + minsz = offsetofend(struct vfio_iommu_type1_bind, bind_type); + +if (copy_from_user (&bind, (void __user *)arg, minsz)) +return -EFAULT ; + +if (bind.argsz < minsz) +return -EINVAL ; + +switch (bind.bind_type) { +case VFIO_IOMMU_BIND_GUEST_PASID : +return vfio_iommu_type1_bind_gpasid (iommu, + (void __user *)(arg + minsz), &bind); +default : +return -EINVAL ; + } + + }else if (cmd ==VFIO_IOMMU_UNBIND ) { +struct vfio_iommu_type1_bind bind; + + minsz = offsetofend(struct vfio_iommu_type1_bind, bind_type); + +if (copy_from_user (&bind, (void __user *)arg, minsz)) +return -EFAULT ; + +if (bind.argsz < minsz) +return -EINVAL ; + +switch (bind.bind_type) { +case VFIO_IOMMU_BIND_GUEST_PASID : +return vfio_iommu_type1_unbind_gpasid (iommu, + (void __user *)(arg + minsz), &bind); +default : +return -EINVAL ; + } }return -ENOTTY ;
include/uapi/linux/vfio.h +44 -0
50 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to host
Quoted 113 lines · On Thu, 24 Oct 2019 08:26:23 -0400 Liu Yi L <[email protected]> wrote:
This patch adds vfio support to bind guest translation structure to host iommu. VFIO exposes iommu programming capability to user- space. Guest is a user-space application in host under KVM solution. For SVA usage in Virtual Machine, guest owns
GVA->GPAtranslation structure. And this part should be passdown to host to enable nested translation (or say two stage translation). This patch reuses theVFIO_IOMMU_BINDproposal from Jean-Philippe Brucker, and adds new bind type for binding guest owned translation structure to host.*) Add two new ioctls for VFIO containers.
-
VFIO_IOMMU_BIND: for bind request from userspace, it could be bind a process to a pasid or bind a guest pasid to a device, this is indicated by type
-VFIO_IOMMU_UNBIND: for unbind request from userspace, it could be unbind a process to a pasid or unbind a guest pasid to a device, also indicated by type
- Bind type:
VFIO_IOMMU_BIND_PROCESS: user-space request to bind a process
to a device
VFIO_IOMMU_BIND_GUEST_PASID: bind guest owned translation
structure to host iommu. e.g. guest page table*) Code logic in
vfio_iommu_type1_ioctl()to handle VFIO_IOMMU_BIND/UNBINDCc Kevin Tian <[email protected]>Signed-off-by Jean-Philippe Brucker <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 136 ++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 44 +++++++++++++ 2 files changed, 180 insertions(+)drivers/vfio/vfio_iommu_type1.c +71
-0@@ -2325,6 +2325,104 @@ static int vfio_iommu_type1_pasid_free(struct vfio_iommu *iommu,return ret; } +static int vfio_bind_gpasid_fn (struct device *dev,void *data) +{ +struct domain_capsule *dc = (struct domain_capsule *)data; +struct iommu_gpasid_bind_data *ustruct = + (struct iommu_gpasid_bind_data *) dc->data; + +return iommu_sva_bind_gpasid (dc->domain, dev, ustruct); +} + +static int vfio_unbind_gpasid_fn (struct device *dev,void *data) +{ +struct domain_capsule *dc = (struct domain_capsule *)data; +struct iommu_gpasid_bind_data *ustruct = + (struct iommu_gpasid_bind_data *) dc->data; + +return iommu_sva_unbind_gpasid (dc->domain, dev, + ustruct->hpasid); +} + +/* +* unbind specific gpasid, caller of this function requires hold +* vfio_iommu->lock +*/ +static long vfio_iommu_type1_do_guest_unbind (struct vfio_iommu *iommu, +struct iommu_gpasid_bind_data *gbind_data) +{ +return vfio_iommu_lookup_dev (iommu, vfio_unbind_gpasid_fn, gbind_data); +} + +static long vfio_iommu_type1_bind_gpasid (struct vfio_iommu *iommu, +void __user *arg, +struct vfio_iommu_type1_bind *bind) +{ +struct iommu_gpasid_bind_data gbind_data; +unsigned long minsz; +int ret =0 ; + + minsz =sizeof (*bind) +sizeof (gbind_data); +if (bind->argsz < minsz) +return -EINVAL ; + +if (copy_from_user (&gbind_data, arg,sizeof (gbind_data))) +return -EFAULT ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + + ret =vfio_iommu_lookup_dev (iommu, vfio_bind_gpasid_fn, &gbind_data); +/* +* If bind failed, it may not be a total failure. Some devices within +* the iommu group may have bind successfully. Although we don't enable +* pasid capability for non-singletion iommu groups, a unbind operation +* would be helpful to ensure no partial binding for an iommu group. +*/ +if (ret) +/* +* Undo all binds that already succeeded, no need to check the +* return value here since some device within the group has no +* successful bind when coming to this place switch. +*/ +vfio_iommu_type1_do_guest_unbind (iommu, &gbind_data); + +out_unlock: +mutex_unlock (&iommu->lock); +return ret; +} + +static long vfio_iommu_type1_unbind_gpasid(struct vfio_iommu *iommu,
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_pasid_free(struct vfio_iommu *iommu, · line 2408
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+void __user *arg, + struct vfio_iommu_type1_bind *bind) +{ + struct iommu_gpasid_bind_data gbind_data; +unsigned long minsz; +int ret =0 ; + + minsz =sizeof (*bind) +sizeof (gbind_data); +if (bind->argsz < minsz) +return -EINVAL ;
But gbind_data can change size if new vendor specific data is added to the union, so kernel updates break existing userspace. Fail.
Quoted 30 lines
drivers/vfio/vfio_iommu_type1.c +23
-0+ +if (copy_from_user (&gbind_data, arg,sizeof (gbind_data))) +return -EFAULT ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + + ret =vfio_iommu_type1_do_guest_unbind (iommu, &gbind_data); + +out_unlock: +mutex_unlock (&iommu->lock); +return ret; +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg) {@@ -2484,6 +2582,44 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,default :return -EINVAL ; } + + }else if (cmd ==VFIO_IOMMU_BIND ) { +struct vfio_iommu_type1_bind bind; + + minsz = offsetofend(struct vfio_iommu_type1_bind, bind_type); +
drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void *iommu_data, · line 2600
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10
-0+if (copy_from_user (&bind, (void __user *)arg, minsz)) +return -EFAULT ; + +if (bind.argsz < minsz) +return -EINVAL ; + +switch (bind.bind_type) { +case VFIO_IOMMU_BIND_GUEST_PASID : +return vfio_iommu_type1_bind_gpasid (iommu, + (void __user *)(arg + minsz), &bind);
Why are we defining BIND_PROCESS if it's not supported? How does the user learn it's not supported?
Quoted 38 lines
drivers/vfio/vfio_iommu_type1.c +22
-0+default : +return -EINVAL ; + } + + }else if (cmd ==VFIO_IOMMU_UNBIND ) { +struct vfio_iommu_type1_bind bind; + + minsz = offsetofend(struct vfio_iommu_type1_bind, bind_type); + +if (copy_from_user (&bind, (void __user *)arg, minsz)) +return -EFAULT ; + +if (bind.argsz < minsz) +return -EINVAL ; + +switch (bind.bind_type) { +case VFIO_IOMMU_BIND_GUEST_PASID : +return vfio_iommu_type1_unbind_gpasid (iommu, + (void __user *)(arg + minsz), &bind); +default : +return -EINVAL ; + } }return -ENOTTY ;include/uapi/linux/vfio.h +5
-0@@ -832,6 +832,50 @@ struct vfio_iommu_type1_pasid_request {*/ #define VFIO_IOMMU_PASID_REQUEST _IO(VFIO_TYPE, VFIO_BASE + 27) +enum vfio_iommu_bind_type { +VFIO_IOMMU_BIND_PROCESS , +VFIO_IOMMU_BIND_GUEST_PASID , +}; +
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request { · line 849
Quoted 10 lines
include/uapi/linux/vfio.h +10
-0+/* +* Supported types: +* - VFIO_IOMMU_BIND_GUEST_PASID: bind guest pasid, which invoked +* by guest, it takes iommu_gpasid_bind_data in data. +*/ +struct vfio_iommu_type1_bind { +__u32 argsz; +enum vfio_iommu_bind_type bind_type; +__u8 data[]; +};
I don't think enum defines a compiler invariant data size. We can't use it for a kernel/user interface. Also why no flags field as is essentially standard for every vfio ioctl? Couldn't we specify process/guest-pasid with flags? For that matter couldn't we specify bind/unbind using a single ioctl? I think that would be more consistent with the pasid alloc/free ioctl in the previous patch.
Why are we appending opaque data to the end of the structure when clearly we expect a struct iommu_gpasid_bind_data? That bind data structure expects a format (ex. IOMMU_PASID_FORMAT_INTEL_VTD). How does a user determine what formats are accepted from within the vfio API (or even outside of the vfio API)?
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request { · line 852
Quoted 3 lines
include/uapi/linux/vfio.h +3
-0+ +/* +* VFIO_IOMMU_BIND - _IOWR(VFIO_TYPE, VFIO_BASE + 28, struct vfio_iommu_bind)
^
The semantics appear to just be _IOW, nothing is written back to the userspace buffer on return.
Quoted 4 lines
include/uapi/linux/vfio.h +4
-0+* +* Manage address spaces of devices in this container. Initially a TYPE1 +* container can only have one address space, managed with +* VFIO_IOMMU_MAP/UNMAP_DMA.
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request { · line 866
Quoted 10 lines
include/uapi/linux/vfio.h +10
-0+* +* An IOMMU of type VFIO_TYPE1_NESTING_IOMMU can be managed by both MAP/UNMAP +* and BIND ioctls at the same time. MAP/UNMAP acts on the stage-2 (host) page +* tables, and BIND manages the stage-1 (guest) page tables. Other types of +* IOMMU may allow MAP/UNMAP and BIND to coexist, where MAP/UNMAP controls +* non-PASID traffic and BIND controls PASID traffic. But this depends on the +* underlying IOMMU architecture and isn't guaranteed. +* +* Availability of this feature depends on the device, its bus, the underlying +* IOMMU and the CPU architecture.
And the user discovers this is available by...? There's no probe here, are they left only to setup a VM to the point of trying to use this before they fail the ioctl? Could VFIO_IOMMU_GET_INFO fill this gap? Thanks,
Alex
Quoted 5 lines
include/uapi/linux/vfio.h +5
-0+* +* returns: 0 on success, -errno on failure. +*/ +#define VFIO_IOMMU_BIND _IO(VFIO_TYPE, VFIO_BASE + 28) +
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request { · line 881
Quoted 10 lines
include/uapi/linux/vfio.h +7
-0+/* +* VFIO_IOMMU_UNBIND - _IOWR(VFIO_TYPE, VFIO_BASE + 29, struct vfio_iommu_bind) +* +* Undo what was done by the corresponding VFIO_IOMMU_BIND ioctl. +*/ +#define VFIO_IOMMU_UNBIND _IO(VFIO_TYPE, VFIO_BASE + 29) +/* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */ /*
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
RE: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to host
Quoted 128 lines
From: Alex Williamson < [email protected] >
Sent: Friday, November 8, 2019 7:21 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to hostQuoted 23 lines · On Thu, 24 Oct 2019 08:26:23 -0400 Liu Yi L <[email protected]> wrote:
This patch adds vfio support to bind guest translation structure to host iommu. VFIO exposes iommu programming capability to user- space. Guest is a user-space application in host under KVM solution. For SVA usage in Virtual Machine, guest owns
GVA->GPAtranslation structure. And this part should be passdown to host to enable nested translation (or say two stage translation). This patch reuses theVFIO_IOMMU_BINDproposal from Jean-Philippe Brucker, and adds new bind type for binding guest owned translation structure to host.*) Add two new ioctls for VFIO containers.
-
VFIO_IOMMU_BIND: for bind request from userspace, it could be bind a process to a pasid or bind a guest pasid to a device, this is indicated by type
-VFIO_IOMMU_UNBIND: for unbind request from userspace, it could be unbind a process to a pasid or unbind a guest pasid to a device, also indicated by type
- Bind type:
VFIO_IOMMU_BIND_PROCESS: user-space request to bind a process
to a device
VFIO_IOMMU_BIND_GUEST_PASID: bind guest owned translation
structure to host iommu. e.g. guest page tableQuoted 8 lines
*) Code logic in
vfio_iommu_type1_ioctl()to handle VFIO_IOMMU_BIND/UNBINDCc Kevin Tian <[email protected]>Signed-off-by Jean-Philippe Brucker <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 136++++++++++++++++++++++++++++++++++++++++
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_pasid_free(struct
Quoted 8 lines
include/uapi/linux/vfio.h | 44 +++++++++++++ 2 files changed, 180 insertions(+)drivers/vfio/vfio_iommu_type1.c +0
-0@@ -2325,6 +2325,104 @@ static int vfio_iommu_type1_pasid_free(struct
vfio_iommu*iommu,Quoted 79 lines
drivers/vfio/vfio_iommu_type1.c +76
-0return ret; } +static int vfio_bind_gpasid_fn (struct device *dev,void *data) +{ +struct domain_capsule *dc = (struct domain_capsule *)data; +struct iommu_gpasid_bind_data *ustruct = + (struct iommu_gpasid_bind_data *) dc->data; + +return iommu_sva_bind_gpasid (dc->domain, dev, ustruct); +} + +static int vfio_unbind_gpasid_fn (struct device *dev,void *data) +{ +struct domain_capsule *dc = (struct domain_capsule *)data; +struct iommu_gpasid_bind_data *ustruct = + (struct iommu_gpasid_bind_data *) dc->data; + +return iommu_sva_unbind_gpasid (dc->domain, dev, + ustruct->hpasid); +} + +/* +* unbind specific gpasid, caller of this function requires hold +* vfio_iommu->lock +*/ +static long vfio_iommu_type1_do_guest_unbind (struct vfio_iommu *iommu, +struct iommu_gpasid_bind_data *gbind_data) +{ +return vfio_iommu_lookup_dev (iommu, vfio_unbind_gpasid_fn, gbind_data); +} + +static long vfio_iommu_type1_bind_gpasid (struct vfio_iommu *iommu, +void __user *arg, +struct vfio_iommu_type1_bind *bind) +{ +struct iommu_gpasid_bind_data gbind_data; +unsigned long minsz; +int ret =0 ; + + minsz =sizeof (*bind) +sizeof (gbind_data); +if (bind->argsz < minsz) +return -EINVAL ; + +if (copy_from_user (&gbind_data, arg,sizeof (gbind_data))) +return -EFAULT ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + + ret =vfio_iommu_lookup_dev (iommu, vfio_bind_gpasid_fn, &gbind_data); +/* +* If bind failed, it may not be a total failure. Some devices within +* the iommu group may have bind successfully. Although we don't enable +* pasid capability for non-singletion iommu groups, a unbind operation +* would be helpful to ensure no partial binding for an iommu group. +*/ +if (ret) +/* +* Undo all binds that already succeeded, no need to check the +* return value here since some device within the group has no +* successful bind when coming to this place switch. +*/ +vfio_iommu_type1_do_guest_unbind (iommu, &gbind_data); + +out_unlock: +mutex_unlock (&iommu->lock); +return ret; +} + +static long vfio_iommu_type1_unbind_gpasid (struct vfio_iommu *iommu, +void __user *arg, +struct vfio_iommu_type1_bind *bind) +{ +struct iommu_gpasid_bind_data gbind_data; +unsigned long minsz;
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_pasid_free(struct vfio_iommu *iommu, · line 2367
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+int ret =0 ; + + minsz =sizeof (*bind) +sizeof (gbind_data); +if (bind->argsz < minsz) +return -EINVAL ;But
gbind_datacan change size if new vendor specific data is added to the union, so kernel updates break existing userspace. Fail.
yes, we have a version field in struct iommu_gpasid_bind_data. How about doing sanity check per versions? kernel knows the gbind_data size of specific versions. Does it make sense? If yes, I'll also apply it to the other sanity check in this series to avoid userspace fail after kernel update.
Quoted 35 lines
Quoted 35 lines
drivers/vfio/vfio_iommu_type1.c +28
-0+ +if (copy_from_user (&gbind_data, arg,sizeof (gbind_data))) +return -EFAULT ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + + ret =vfio_iommu_type1_do_guest_unbind (iommu, &gbind_data); + +out_unlock: +mutex_unlock (&iommu->lock); +return ret; +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg) {@@ -2484,6 +2582,44 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,default :return -EINVAL ; } + + }else if (cmd ==VFIO_IOMMU_BIND ) { +struct vfio_iommu_type1_bind bind; + + minsz = offsetofend(struct vfio_iommu_type1_bind, bind_type); + +if (copy_from_user (&bind, (void __user *)arg, minsz)) +return -EFAULT ; + +if (bind.argsz < minsz) +return -EINVAL ;
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void *iommu_data, · line 2600
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+ +switch (bind.bind_type) { +case VFIO_IOMMU_BIND_GUEST_PASID : +return vfio_iommu_type1_bind_gpasid (iommu, + (void __user *)(arg + minsz), &bind);Why are we defining
BIND_PROCESSif it's not supported? How does the user learn it's not supported?
I think I should drop it so far since I only add BIND_GUEST_PASID. I think Jean Philippe may need it in his native SVA enabling patchset. For the way to let user learn it, may be using VFIO_IOMMU_GET_INFO as you mentioned below?
Quoted 46 lines
Quoted 46 lines
drivers/vfio/vfio_iommu_type1.c +22
-0+default : +return -EINVAL ; + } + + }else if (cmd ==VFIO_IOMMU_UNBIND ) { +struct vfio_iommu_type1_bind bind; + + minsz = offsetofend(struct vfio_iommu_type1_bind, bind_type); + +if (copy_from_user (&bind, (void __user *)arg, minsz)) +return -EFAULT ; + +if (bind.argsz < minsz) +return -EINVAL ; + +switch (bind.bind_type) { +case VFIO_IOMMU_BIND_GUEST_PASID : +return vfio_iommu_type1_unbind_gpasid (iommu, + (void __user *)(arg + minsz), &bind); +default : +return -EINVAL ; + } }return -ENOTTY ;include/uapi/linux/vfio.h +12
-0@@ -832,6 +832,50 @@ struct vfio_iommu_type1_pasid_request {*/ #define VFIO_IOMMU_PASID_REQUEST _IO(VFIO_TYPE, VFIO_BASE + 27) +enum vfio_iommu_bind_type { +VFIO_IOMMU_BIND_PROCESS , +VFIO_IOMMU_BIND_GUEST_PASID , +}; + +/* +* Supported types: +* - VFIO_IOMMU_BIND_GUEST_PASID: bind guest pasid, which invoked +* by guest, it takes iommu_gpasid_bind_data in data. +*/ +struct vfio_iommu_type1_bind { +__u32 argsz;
Quoted 8 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request { · line 849
Quoted 3 lines
include/uapi/linux/vfio.h +3
-0+enum vfio_iommu_bind_type bind_type; +__u8 data[]; +};I don't think enum defines a compiler invariant data size. We can't use it for a kernel/user interface. Also why no flags field as is essentially standard for every vfio ioctl? Couldn't we specify process/guest-pasid with flags?
I remember there is an early comment in community which pointed out that using flags potentially allows to config multiples types in one IOCTL. Regards to it, defining explicit emums avoids it. But I agree with you, it makes variant size. I'll fix it if this matter more.
Quoted 3 lines
For that matter couldn't we specify
bind/unbind using a single ioctl? I think that would be more consistent with the pasid alloc/free ioctl in the previous patch.
yes, let me make it in next version.
Quoted 2 lines
Why are we appending opaque data to the end of the structure when clearly we expect a struct
iommu_gpasid_bind_data?
This is due to the intention to support BIND_GUEST_PASID and BIND_PROCESS with a single IOCTL. Maybe we can use a separate IOCTL for BIND_PROCESS. what's your opinion here?
Quoted 4 lines
That bind data
structure expects a format (ex.IOMMU_PASID_FORMAT_INTEL_VTD). How does a user determine what formats are accepted from within the vfio API (or even outside of the vfio API)?
The info is provided by vIOMMU emulator (e.g. virtual VT-d). The vSVA patch from Jacob has a sanity check on it.
https://lkml.org/lkml/2019/10/28/873
Quoted 7 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request { · line 852
Quoted 3 lines
include/uapi/linux/vfio.h +3
-0+ +/* +* VFIO_IOMMU_BIND - _IOWR(VFIO_TYPE, VFIO_BASE + 28, struct
vfio_iommu_bind)^The semantics appear to just be
_IOW, nothing is written back to the userspace buffer on return.
will fix it. thanks.
Quoted 17 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request {
Quoted 6 lines
include/uapi/linux/vfio.h +6
-0+* +* Manage address spaces of devices in this container. Initially a TYPE1 +* container can only have one address space, managed with +* VFIO_IOMMU_MAP/UNMAP_DMA. +* +* An IOMMU of type VFIO_TYPE1_NESTING_IOMMU can be managed by both MAP/UNMAP
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request {
Quoted 3 lines
include/uapi/linux/vfio.h +3
-0+* and BIND ioctls at the same time. MAP/UNMAP acts on the stage-2 (host) page +* tables, and BIND manages the stage-1 (guest) page tables. Other types of +* IOMMU may allow MAP/UNMAP and BIND to coexist, where MAP/UNMAP controls
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request { · line 866
Quoted 5 lines
include/uapi/linux/vfio.h +5
-0+* non-PASID traffic and BIND controls PASID traffic. But this depends on the +* underlying IOMMU architecture and isn't guaranteed. +* +* Availability of this feature depends on the device, its bus, the underlying +* IOMMU and the CPU architecture.
Quoted 4 lines
And the user discovers this is available by...? There's no probe here, are they left only to setup a VM to the point of trying to use this before they fail the ioctl? Could
VFIO_IOMMU_GET_INFOfill this gap? Thanks,
I think VFIO_IOMMU_GET_INFO could help. let me extend it to fill this gap if you agree.
Quoted 1 line
Alex
Thanks,
Yi Liu
Quoted 9 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request {
Quoted 8 lines
include/uapi/linux/vfio.h +7
-0+* +* returns: 0 on success, -errno on failure. +*/ +#define VFIO_IOMMU_BIND _IO(VFIO_TYPE, VFIO_BASE + 28) + +/* +* VFIO_IOMMU_UNBIND - _IOWR(VFIO_TYPE, VFIO_BASE + 29, struct
vfio_iommu_bind)
Quoted 8 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request { · line 881
Quoted 8 lines
include/uapi/linux/vfio.h +5
-0+* +* Undo what was done by the corresponding VFIO_IOMMU_BIND ioctl. +*/ +#define VFIO_IOMMU_UNBIND _IO(VFIO_TYPE, VFIO_BASE + 29) +/* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */ /*
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to host
Quoted 137 lines · Yi L
Quoted 128 lines
From: Alex Williamson < [email protected] >
Sent: Friday, November 8, 2019 7:21 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to hostQuoted 23 lines · On Thu, 24 Oct 2019 08:26:23 -0400 Liu Yi L <[email protected]> wrote:
This patch adds vfio support to bind guest translation structure to host iommu. VFIO exposes iommu programming capability to user- space. Guest is a user-space application in host under KVM solution. For SVA usage in Virtual Machine, guest owns
GVA->GPAtranslation structure. And this part should be passdown to host to enable nested translation (or say two stage translation). This patch reuses theVFIO_IOMMU_BINDproposal from Jean-Philippe Brucker, and adds new bind type for binding guest owned translation structure to host.*) Add two new ioctls for VFIO containers.
-
VFIO_IOMMU_BIND: for bind request from userspace, it could be bind a process to a pasid or bind a guest pasid to a device, this is indicated by type
-VFIO_IOMMU_UNBIND: for unbind request from userspace, it could be unbind a process to a pasid or unbind a guest pasid to a device, also indicated by type
- Bind type:
VFIO_IOMMU_BIND_PROCESS: user-space request to bind a process
to a device
VFIO_IOMMU_BIND_GUEST_PASID: bind guest owned translation
structure to host iommu. e.g. guest page tableQuoted 8 lines
*) Code logic in
vfio_iommu_type1_ioctl()to handle VFIO_IOMMU_BIND/UNBINDCc Kevin Tian <[email protected]>Signed-off-by Jean-Philippe Brucker <[email protected]>Signed-off-by Liu Yi L <[email protected]>Signed-off-by Jacob Pan <[email protected]>--- drivers/vfio/vfio_iommu_type1.c | 136++++++++++++++++++++++++++++++++++++++++
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_pasid_free(struct
Quoted 8 lines
include/uapi/linux/vfio.h | 44 +++++++++++++ 2 files changed, 180 insertions(+)drivers/vfio/vfio_iommu_type1.c +0
-0@@ -2325,6 +2325,104 @@ static int vfio_iommu_type1_pasid_free(struct
vfio_iommu*iommu,Quoted 79 lines
drivers/vfio/vfio_iommu_type1.c +76
-0return ret; } +static int vfio_bind_gpasid_fn (struct device *dev,void *data) +{ +struct domain_capsule *dc = (struct domain_capsule *)data; +struct iommu_gpasid_bind_data *ustruct = + (struct iommu_gpasid_bind_data *) dc->data; + +return iommu_sva_bind_gpasid (dc->domain, dev, ustruct); +} + +static int vfio_unbind_gpasid_fn (struct device *dev,void *data) +{ +struct domain_capsule *dc = (struct domain_capsule *)data; +struct iommu_gpasid_bind_data *ustruct = + (struct iommu_gpasid_bind_data *) dc->data; + +return iommu_sva_unbind_gpasid (dc->domain, dev, + ustruct->hpasid); +} + +/* +* unbind specific gpasid, caller of this function requires hold +* vfio_iommu->lock +*/ +static long vfio_iommu_type1_do_guest_unbind (struct vfio_iommu *iommu, +struct iommu_gpasid_bind_data *gbind_data) +{ +return vfio_iommu_lookup_dev (iommu, vfio_unbind_gpasid_fn, gbind_data); +} + +static long vfio_iommu_type1_bind_gpasid (struct vfio_iommu *iommu, +void __user *arg, +struct vfio_iommu_type1_bind *bind) +{ +struct iommu_gpasid_bind_data gbind_data; +unsigned long minsz; +int ret =0 ; + + minsz =sizeof (*bind) +sizeof (gbind_data); +if (bind->argsz < minsz) +return -EINVAL ; + +if (copy_from_user (&gbind_data, arg,sizeof (gbind_data))) +return -EFAULT ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + + ret =vfio_iommu_lookup_dev (iommu, vfio_bind_gpasid_fn, &gbind_data); +/* +* If bind failed, it may not be a total failure. Some devices within +* the iommu group may have bind successfully. Although we don't enable +* pasid capability for non-singletion iommu groups, a unbind operation +* would be helpful to ensure no partial binding for an iommu group. +*/ +if (ret) +/* +* Undo all binds that already succeeded, no need to check the +* return value here since some device within the group has no +* successful bind when coming to this place switch. +*/ +vfio_iommu_type1_do_guest_unbind (iommu, &gbind_data); + +out_unlock: +mutex_unlock (&iommu->lock); +return ret; +} + +static long vfio_iommu_type1_unbind_gpasid (struct vfio_iommu *iommu, +void __user *arg, +struct vfio_iommu_type1_bind *bind) +{ +struct iommu_gpasid_bind_data gbind_data; +unsigned long minsz;Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_pasid_free(struct vfio_iommu *iommu, · line 2367
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+int ret =0 ; + + minsz =sizeof (*bind) +sizeof (gbind_data); +if (bind->argsz < minsz) +return -EINVAL ;But
gbind_datacan change size if new vendor specific data is added to the union, so kernel updates break existing userspace. Fail.
Quoted 5 lines
yes, we have a version field in struct
iommu_gpasid_bind_data. How about doing sanity check per versions? kernel knows thegbind_datasize of specific versions. Does it make sense? If yes, I'll also apply it to the other sanity check in this series to avoid userspace fail after kernel update.
Has it already been decided that the version field will be updated for every addition to the union? It seems there are two options, either the version definition includes the possible contents of the union, which means we need to support multiple versions concurrently in the kernel to maintain compatibility with userspace and follow deprecation protocols for removing that support, or we need to consider version to be the general form of the structure and interpret the format field to determine necessary length to copy from the user.
Quoted 113 lines
Quoted 35 lines
Quoted 35 lines
drivers/vfio/vfio_iommu_type1.c +28
-0+ +if (copy_from_user (&gbind_data, arg,sizeof (gbind_data))) +return -EFAULT ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + + ret =vfio_iommu_type1_do_guest_unbind (iommu, &gbind_data); + +out_unlock: +mutex_unlock (&iommu->lock); +return ret; +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg) {@@ -2484,6 +2582,44 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,default :return -EINVAL ; } + + }else if (cmd ==VFIO_IOMMU_BIND ) { +struct vfio_iommu_type1_bind bind; + + minsz = offsetofend(struct vfio_iommu_type1_bind, bind_type); + +if (copy_from_user (&bind, (void __user *)arg, minsz)) +return -EFAULT ; + +if (bind.argsz < minsz) +return -EINVAL ;Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void *iommu_data, · line 2600
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+ +switch (bind.bind_type) { +case VFIO_IOMMU_BIND_GUEST_PASID : +return vfio_iommu_type1_bind_gpasid (iommu, + (void __user *)(arg + minsz), &bind);Why are we defining
BIND_PROCESSif it's not supported? How does the user learn it's not supported?I think I should drop it so far since I only add
BIND_GUEST_PASID. I think Jean Philippe may need it in his native SVA enabling patchset. For the way to let user learn it, may be usingVFIO_IOMMU_GET_INFOas you mentioned below?Quoted 46 lines
Quoted 46 lines
drivers/vfio/vfio_iommu_type1.c +22
-0+default : +return -EINVAL ; + } + + }else if (cmd ==VFIO_IOMMU_UNBIND ) { +struct vfio_iommu_type1_bind bind; + + minsz = offsetofend(struct vfio_iommu_type1_bind, bind_type); + +if (copy_from_user (&bind, (void __user *)arg, minsz)) +return -EFAULT ; + +if (bind.argsz < minsz) +return -EINVAL ; + +switch (bind.bind_type) { +case VFIO_IOMMU_BIND_GUEST_PASID : +return vfio_iommu_type1_unbind_gpasid (iommu, + (void __user *)(arg + minsz), &bind); +default : +return -EINVAL ; + } }return -ENOTTY ;include/uapi/linux/vfio.h +12
-0@@ -832,6 +832,50 @@ struct vfio_iommu_type1_pasid_request {*/ #define VFIO_IOMMU_PASID_REQUEST _IO(VFIO_TYPE, VFIO_BASE + 27) +enum vfio_iommu_bind_type { +VFIO_IOMMU_BIND_PROCESS , +VFIO_IOMMU_BIND_GUEST_PASID , +}; + +/* +* Supported types: +* - VFIO_IOMMU_BIND_GUEST_PASID: bind guest pasid, which invoked +* by guest, it takes iommu_gpasid_bind_data in data. +*/ +struct vfio_iommu_type1_bind { +__u32 argsz;Quoted 8 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request { · line 849
Quoted 3 lines
include/uapi/linux/vfio.h +3
-0+enum vfio_iommu_bind_type bind_type; +__u8 data[]; +};I don't think enum defines a compiler invariant data size. We can't use it for a kernel/user interface. Also why no flags field as is essentially standard for every vfio ioctl? Couldn't we specify process/guest-pasid with flags?
I remember there is an early comment in community which pointed out that using flags potentially allows to config multiples types in one IOCTL. Regards to it, defining explicit emums avoids it. But I agree with you, it makes variant size. I'll fix it if this matter more.
Quoted 3 lines
For that matter couldn't we specify
bind/unbind using a single ioctl? I think that would be more consistent with the pasid alloc/free ioctl in the previous patch.
Quoted 8 lines
yes, let me make it in next version.
Quoted 2 lines
Why are we appending opaque data to the end of the structure when clearly we expect a struct
iommu_gpasid_bind_data?This is due to the intention to support
BIND_GUEST_PASIDandBIND_PROCESSwith a single IOCTL. Maybe we can use a separate IOCTL forBIND_PROCESS. what's your opinion here?
If the ioctls have similar purpose and form, then re-using a single ioctl might make sense, but BIND_PROCESS is only a place-holder in this series, which is not acceptable. A dual purpose ioctl does not preclude that we could also use a union for the data field to make the structure well specified.
Quoted 8 lines
Quoted 4 lines
That bind data
structure expects a format (ex.IOMMU_PASID_FORMAT_INTEL_VTD). How does a user determine what formats are accepted from within the vfio API (or even outside of the vfio API)?The info is provided by vIOMMU emulator (e.g. virtual VT-d). The vSVA patch from Jacob has a sanity check on it.
https://lkml.org/lkml/2019/10/28/873
The vIOMMU emulator runs at a layer above vfio. How does the vIOMMU emulator know that the vfio interface supports virtual VT-d? IMO, it's not acceptable that the user simply assume that an Intel host platform supports VT-d. For example, consider what happens when we need to define IOMMU_PASID_FORMAT_INTEL_VTDv2. How would the user learn that VTDv2 is supported and the original VTD format is not supported?
Quoted 27 lines
Quoted 7 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request { · line 852
Quoted 3 lines
include/uapi/linux/vfio.h +3
-0+ +/* +* VFIO_IOMMU_BIND - _IOWR(VFIO_TYPE, VFIO_BASE + 28, struct
vfio_iommu_bind)^The semantics appear to just be
_IOW, nothing is written back to the userspace buffer on return.will fix it. thanks.
Quoted 17 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request {
Quoted 6 lines
include/uapi/linux/vfio.h +6
-0+* +* Manage address spaces of devices in this container. Initially a TYPE1 +* container can only have one address space, managed with +* VFIO_IOMMU_MAP/UNMAP_DMA. +* +* An IOMMU of type VFIO_TYPE1_NESTING_IOMMU can be managed by both MAP/UNMAP
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request {
Quoted 3 lines
include/uapi/linux/vfio.h +3
-0+* and BIND ioctls at the same time. MAP/UNMAP acts on the stage-2 (host) page +* tables, and BIND manages the stage-1 (guest) page tables. Other types of +* IOMMU may allow MAP/UNMAP and BIND to coexist, where MAP/UNMAP controls
Quoted 5 lines
include/uapi/linux/vfio.h +5
-0+* non-PASID traffic and BIND controls PASID traffic. But this depends on the +* underlying IOMMU architecture and isn't guaranteed. +* +* Availability of this feature depends on the device, its bus, the underlying +* IOMMU and the CPU architecture.
Quoted 7 lines
Quoted 4 lines
And the user discovers this is available by...? There's no probe here, are they left only to setup a VM to the point of trying to use this before they fail the ioctl? Could
VFIO_IOMMU_GET_INFOfill this gap? Thanks,I think
VFIO_IOMMU_GET_INFOcould help. let me extend it to fill this gap if you agree.
It's a start. Thanks,
Alex
RE: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to host
Quoted 42 lines
From: Alex Williamson <[email protected]>
Sent: Wednesday, November 13, 2019 1:26 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to hostQuoted 31 lines · Yi L
Quoted 31 lines
From: Alex Williamson < [email protected] >
Sent: Friday, November 8, 2019 7:21 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to hostQuoted 23 lines · On Thu, 24 Oct 2019 08:26:23 -0400 Liu Yi L <[email protected]> wrote:
This patch adds vfio support to bind guest translation structure to host iommu. VFIO exposes iommu programming capability to user- space. Guest is a user-space application in host under KVM solution. For SVA usage in Virtual Machine, guest owns
GVA->GPAtranslation structure. And this part should be passdown to host to enable nested translation (or say two stage translation). This patch reuses theVFIO_IOMMU_BINDproposal from Jean-Philippe Brucker, and adds new bind type for binding guest owned translation structure to host.*) Add two new ioctls for VFIO containers.
-
VFIO_IOMMU_BIND: for bind request from userspace, it could be bind a process to a pasid or bind a guest pasid to a device, this is indicated by type
-VFIO_IOMMU_UNBIND: for unbind request from userspace, it could be unbind a process to a pasid or unbind a guest pasid to a device, also indicated by type
- Bind type:
VFIO_IOMMU_BIND_PROCESS: user-space request to bind a process
to a device
VFIO_IOMMU_BIND_GUEST_PASID: bind guest owned translation
structure to host iommu. e.g. guest page tableQuoted 1 line
Quoted 1 line
Quoted 1 line
*) Code logic in
vfio_iommu_type1_ioctl()to handleVFIO_IOMMU_BIND/UNBIND
Quoted 1 line
Quoted 1 line
Quoted 1 line
[...]
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_pasid_free(struct vfio_iommu *iommu, · line 2408
Quoted 11 lines
drivers/vfio/vfio_iommu_type1.c +11
-0+static long vfio_iommu_type1_unbind_gpasid (struct vfio_iommu *iommu, +void __user *arg, +struct vfio_iommu_type1_bind *bind) +{ +struct iommu_gpasid_bind_data gbind_data; +unsigned long minsz; +int ret =0 ; + + minsz =sizeof (*bind) +sizeof (gbind_data); +if (bind->argsz < minsz) +return -EINVAL ;But
gbind_datacan change size if new vendor specific data is added to the union, so kernel updates break existing userspace. Fail.
Quoted 8 lines
Quoted 5 lines
yes, we have a version field in struct
iommu_gpasid_bind_data. How about doing sanity check per versions? kernel knows thegbind_datasize of specific versions. Does it make sense? If yes, I'll also apply it to the other sanity check in this series to avoid userspace fail after kernel update.Has it already been decided that the version field will be updated for every addition to the union?
No, just my proposal. Jacob may help to explain the purpose of version field. But if we may be too "frequent" for an uapi version number updating if we inc version for each change in the union part. I may vote for the second option from you below.
Quoted 7 lines
It seems there are two options, either
the version definition includes the possible contents of the union, which means we need to support multiple versions concurrently in the kernel to maintain compatibility with userspace and follow deprecation protocols for removing that support, or we need to consider version to be the general form of the structure and interpret the format field to determine necessary length to copy from the user.
As I mentioned above, may be better to let @version field only over the general fields and let format to cover the possible changes in union. e.g. IOMMU_PASID_FORMAT_INTEL_VTD2 may means version 2 of Intel VT-d bind. But either way, I think we need to let kernel maintain multiple versions to support compatible userspace. e.g. may have multiple versions iommu_gpasid_bind_data_vtd struct in the union part.
Quoted 125 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
drivers/vfio/vfio_iommu_type1.c +13
-0+ +if (copy_from_user (&gbind_data, arg,sizeof (gbind_data))) +return -EFAULT ; + +mutex_lock (&iommu->lock); +if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER (iommu)) { + ret = -EINVAL ; + goto out_unlock; + } + + ret =vfio_iommu_type1_do_guest_unbind (iommu, &gbind_data); + +out_unlock:Quoted 8 lines
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c +4
-0+mutex_unlock (&iommu->lock); +return ret; +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg) {@@ -2484,6 +2582,44 @@ static long vfio_iommu_type1_ioctl(void
*iommu_data,Quoted 62 lines
Quoted 14 lines
Quoted 14 lines
drivers/vfio/vfio_iommu_type1.c +11
-0default :return -EINVAL ; } + + }else if (cmd ==VFIO_IOMMU_BIND ) { +struct vfio_iommu_type1_bind bind; + + minsz = offsetofend(struct vfio_iommu_type1_bind, bind_type); + +if (copy_from_user (&bind, (void __user *)arg, minsz)) +return -EFAULT ; + +if (bind.argsz < minsz) +return -EINVAL ;Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static long vfio_iommu_type1_ioctl(void *iommu_data, · line 2600
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+ +switch (bind.bind_type) { +case VFIO_IOMMU_BIND_GUEST_PASID : +return vfio_iommu_type1_bind_gpasid (iommu, + (void __user *)(arg + minsz), &bind);Why are we defining
BIND_PROCESSif it's not supported? How does the user learn it's not supported?I think I should drop it so far since I only add
BIND_GUEST_PASID. I think Jean Philippe may need it in his native SVA enabling patchset. For the way to let user learn it, may be usingVFIO_IOMMU_GET_INFOas you mentioned below?Quoted 34 lines
Quoted 34 lines
drivers/vfio/vfio_iommu_type1.c +22
-0+default : +return -EINVAL ; + } + + }else if (cmd ==VFIO_IOMMU_UNBIND ) { +struct vfio_iommu_type1_bind bind; + + minsz = offsetofend(struct vfio_iommu_type1_bind, bind_type); + +if (copy_from_user (&bind, (void __user *)arg, minsz)) +return -EFAULT ; + +if (bind.argsz < minsz) +return -EINVAL ; + +switch (bind.bind_type) { +case VFIO_IOMMU_BIND_GUEST_PASID : +return vfio_iommu_type1_unbind_gpasid (iommu, + (void __user *)(arg + minsz), &bind); +default : +return -EINVAL ; + } }return -ENOTTY ;include/uapi/linux/vfio.h +0
-0@@ -832,6 +832,50 @@ struct vfio_iommu_type1_pasid_request {*/ #define VFIO_IOMMU_PASID_REQUEST _IO(VFIO_TYPE, VFIO_BASE + 27)Quoted 8 lines
Quoted 8 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request {
Quoted 8 lines
include/uapi/linux/vfio.h +8
-0+enum vfio_iommu_bind_type { +VFIO_IOMMU_BIND_PROCESS , +VFIO_IOMMU_BIND_GUEST_PASID , +}; + +/* +* Supported types: +* - VFIO_IOMMU_BIND_GUEST_PASID: bind guest pasid, which invoked
Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
include/uapi/linux/vfio.h +4
-0+* by guest, it takes iommu_gpasid_bind_data in data. +*/ +struct vfio_iommu_type1_bind { +__u32 argsz;Quoted 8 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request { · line 849
Quoted 3 lines
include/uapi/linux/vfio.h +3
-0+enum vfio_iommu_bind_type bind_type; +__u8 data[]; +};I don't think enum defines a compiler invariant data size. We can't use it for a kernel/user interface. Also why no flags field as is essentially standard for every vfio ioctl? Couldn't we specify process/guest-pasid with flags?
I remember there is an early comment in community which pointed out that using flags potentially allows to config multiples types in one IOCTL. Regards to it, defining explicit emums avoids it. But I agree with you, it makes variant size. I'll fix it if this matter more.
Quoted 3 lines
For that matter couldn't we specify
bind/unbind using a single ioctl? I think that would be more consistent with the pasid alloc/free ioctl in the previous patch.Quoted 8 lines
yes, let me make it in next version.
Quoted 2 lines
Why are we appending opaque data to the end of the structure when clearly we expect a struct
iommu_gpasid_bind_data?This is due to the intention to support
BIND_GUEST_PASIDandBIND_PROCESSwith a single IOCTL. Maybe we can use a separate IOCTL forBIND_PROCESS. what's your opinion here?
Quoted 5 lines
If the ioctls have similar purpose and form, then re-using a single ioctl might make sense, but
BIND_PROCESSis only a place-holder in this series, which is not acceptable. A dual purpose ioctl does not preclude that we could also use a union for the data field to make the structure well specified.
yes, BIND_PROCESS is only a place-holder here. From kernel p.o.v., both BIND_GUEST_PASID and BIND_PROCESS are bind requests from userspace. So the purposes are aligned. Below is the content the @data[] field supposed to convey for BIND_PROCESS. If we use union, it would leave space for extending it to support BIND_PROCESS. If only data[], it is a little bit confusing why we define it in such manner if BIND_PROCESS is included in this series. Please feel free let me know which one suits better.
+struct vfio_iommu_type1_bind_process {
+ __u32 flags;
+#define VFIO_IOMMU_BIND_PID (1 << 0)
+ __u32 pasid;
+ __s32 pid;
+};
https://patchwork.kernel.org/patch/10394927/
Quoted 10 lines
Quoted 2 lines
Quoted 2 lines
That bind data
structure expects a format (ex.IOMMU_PASID_FORMAT_INTEL_VTD). Howdoes
Quoted 6 lines
Quoted 2 lines
a user determine what formats are accepted from within the vfio API (or even outside of the vfio API)?
The info is provided by vIOMMU emulator (e.g. virtual VT-d). The vSVA patch from Jacob has a sanity check on it.
https://lkml.org/lkml/2019/10/28/873
Quoted 6 lines
The vIOMMU emulator runs at a layer above vfio. How does the vIOMMU emulator know that the vfio interface supports virtual VT-d? IMO, it's not acceptable that the user simply assume that an Intel host platform supports VT-d. For example, consider what happens when we need to define
IOMMU_PASID_FORMAT_INTEL_VTDv2. How would the user learn that VTDv2 is supported and the original VTD format is not supported?
I guess this may be another info VFIO_IOMMU_GET_INFO should provide. It makes sense that vfio be aware of what platform it is running on. right? After vfio gets the info, may let vfio fill in the format info. Is it the correct direction?
Quoted 35 lines
Quoted 9 lines
Quoted 8 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request { · line 852
Quoted 4 lines
include/uapi/linux/vfio.h +3
-0+ +/* +* VFIO_IOMMU_BIND - _IOWR(VFIO_TYPE, VFIO_BASE + 28, struct
vfio_iommu_bind)^The semantics appear to just be
_IOW, nothing is written back to the userspace buffer on return.Quoted 8 lines
will fix it. thanks.
Quoted 6 lines
Quoted 6 lines
include/uapi/linux/vfio.h +6
-0+* +* Manage address spaces of devices in this container. Initially a TYPE1 +* container can only have one address space, managed with +* VFIO_IOMMU_MAP/UNMAP_DMA. +* +* An IOMMU of type VFIO_TYPE1_NESTING_IOMMU can be managed by both
Quoted 2 lines
Quoted 2 lines
MAP/UNMAP
Quoted 1 line
include/uapi/linux/vfio.h +1
-0+* and BIND ioctls at the same time. MAP/UNMAP acts on the stage-2 (host) page
Quoted 14 lines
Quoted 13 lines
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request { · line 861
Quoted 2 lines
include/uapi/linux/vfio.h +2
-0+* tables, and BIND manages the stage-1 (guest) page tables. Other types of +* IOMMU may allow MAP/UNMAP and BIND to coexist, where MAP/UNMAP controls
include/uapi/linux/vfio.h struct vfio_iommu_type1_pasid_request { · line 866
Quoted 5 lines
include/uapi/linux/vfio.h +5
-0+* non-PASID traffic and BIND controls PASID traffic. But this depends on the +* underlying IOMMU architecture and isn't guaranteed. +* +* Availability of this feature depends on the device, its bus, the underlying +* IOMMU and the CPU architecture. And the user discovers this is available by...? There's no probe here, are they left only to setup a VM to the point of trying to use this before they fail the ioctl? Could
VFIO_IOMMU_GET_INFOfill this gap? Thanks,
Quoted 4 lines
Quoted 2 lines
I think
VFIO_IOMMU_GET_INFOcould help. let me extend it to fill this gap if you agree.It's a start. Thanks,
Got it. will show the code in next version. Thanks for your patient review.
Quoted 1 line
Alex
Regards,
Yi Liu
Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to host
Quoted 43 lines · Yi L
Quoted 42 lines
From: Alex Williamson <[email protected]>
Sent: Wednesday, November 13, 2019 1:26 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to hostQuoted 31 lines · Yi L
Quoted 31 lines
From: Alex Williamson < [email protected] >
Sent: Friday, November 8, 2019 7:21 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to hostQuoted 23 lines · On Thu, 24 Oct 2019 08:26:23 -0400 Liu Yi L <[email protected]> wrote:
This patch adds vfio support to bind guest translation structure to host iommu. VFIO exposes iommu programming capability to user- space. Guest is a user-space application in host under KVM solution. For SVA usage in Virtual Machine, guest owns
GVA->GPAtranslation structure. And this part should be passdown to host to enable nested translation (or say two stage translation). This patch reuses theVFIO_IOMMU_BINDproposal from Jean-Philippe Brucker, and adds new bind type for binding guest owned translation structure to host.*) Add two new ioctls for VFIO containers.
-
VFIO_IOMMU_BIND: for bind request from userspace, it could be bind a process to a pasid or bind a guest pasid to a device, this is indicated by type
-VFIO_IOMMU_UNBIND: for unbind request from userspace, it could be unbind a process to a pasid or unbind a guest pasid to a device, also indicated by type
- Bind type:
VFIO_IOMMU_BIND_PROCESS: user-space request to bind a process
to a device
VFIO_IOMMU_BIND_GUEST_PASID: bind guest owned translation
structure to host iommu. e.g. guest page tableQuoted 1 line
Quoted 1 line
Quoted 1 line
*) Code logic in
vfio_iommu_type1_ioctl()to handleVFIO_IOMMU_BIND/UNBIND
Quoted 1 line
Quoted 1 line
Quoted 1 line
[...]
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/vfio/vfio_iommu_type1.c +6
-0+static long vfio_iommu_type1_unbind_gpasid (struct vfio_iommu *iommu, +void __user *arg, +struct vfio_iommu_type1_bind *bind) +{ +struct iommu_gpasid_bind_data gbind_data; +unsigned long minsz;Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_pasid_free(struct vfio_iommu *iommu, · line 2367
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +5
-0+int ret =0 ; + + minsz =sizeof (*bind) +sizeof (gbind_data); +if (bind->argsz < minsz) +return -EINVAL ;But
gbind_datacan change size if new vendor specific data is added to the union, so kernel updates break existing userspace. Fail.
I guess we could take minsz up to the vendor-specific data, copy @format, and then check the size of vendor-specific data?
Quoted 23 lines
Quoted 9 lines
Quoted 6 lines
yes, we have a version field in struct
iommu_gpasid_bind_data. How about doing sanity check per versions? kernel knows thegbind_datasize of specific versions. Does it make sense? If yes, I'll also apply it to the other sanity check in this series to avoid userspace fail after kernel update.Has it already been decided that the version field will be updated for every addition to the union?
No, just my proposal. Jacob may help to explain the purpose of version field. But if we may be too "frequent" for an uapi version number updating if we inc version for each change in the union part. I may vote for the second option from you below.
Quoted 7 lines
It seems there are two options, either
the version definition includes the possible contents of the union, which means we need to support multiple versions concurrently in the kernel to maintain compatibility with userspace and follow deprecation protocols for removing that support, or we need to consider version to be the general form of the structure and interpret the format field to determine necessary length to copy from the user.
Quoted 6 lines
As I mentioned above, may be better to let @version field only over the general fields and let format to cover the possible changes in union. e.g.
IOMMU_PASID_FORMAT_INTEL_VTD2may means version 2 of Intel VT-d bind. But either way, I think we need to let kernel maintain multiple versions to support compatible userspace. e.g. may have multiple versionsiommu_gpasid_bind_data_vtdstruct in the union part.
I couldn't find where the @version field originated in our old discussions, but I believe our plan for allowing future extensions was:
* Add new vendor-specific data by introducing a new format (IOMMU_PASID_FORMAT_INTEL_VTD2, IOMMU_PASID_FORMAT_ARM_SMMUV2...), and extend the union.
* Add a new common field, if it fits in the existing padding bytes, by adding a flag (IOMMU_SVA_GPASID_*).
* Add a new common field, if it doesn't fit in the current padding bytes, or completely change the structure layout, by introducing a new version (IOMMU_GPASID_BIND_VERSION_2). In that case the kernel has to handle both new and old structure versions. It would have both iommu_gpasid_bind_data and iommu_gpasid_bind_data_v2 structs.
I think iommu_cache_invalidate_info and iommu_page_response use the same scheme. iommu_fault is a bit more complicated because it's kernel->userspace and requires some negotiation:
https://lore.kernel.org/linux-iommu/[email protected]/
[...]
Quoted 20 lines
Quoted 5 lines
If the ioctls have similar purpose and form, then re-using a single ioctl might make sense, but
BIND_PROCESSis only a place-holder in this series, which is not acceptable. A dual purpose ioctl does not preclude that we could also use a union for the data field to make the structure well specified.yes,
BIND_PROCESSis only a place-holder here. From kernel p.o.v., bothBIND_GUEST_PASIDandBIND_PROCESSare bind requests from userspace. So the purposes are aligned. Below is the content the @data[] field supposed to convey forBIND_PROCESS. If we use union, it would leave space for extending it to supportBIND_PROCESS. If only data[], it is a little bit confusing why we define it in such manner ifBIND_PROCESSis included in this series. Please feel free let me know which one suits better.(quoted diff) +6
-0+struct vfio_iommu_type1_bind_process { + __u32 flags; +#define VFIO_IOMMU_BIND_PID (1 << 0) + __u32 pasid; + __s32 pid; +};
Quoted 1 line
Note that I don't plan to upstream BIND_PROCESS at the moment. It was useful for testing but I don't know of anyone actually needing it.
Quoted 17 lines
Quoted 10 lines
Quoted 2 lines
Quoted 2 lines
That bind data
structure expects a format (ex.IOMMU_PASID_FORMAT_INTEL_VTD). Howdoes
Quoted 6 lines
Quoted 2 lines
a user determine what formats are accepted from within the vfio API (or even outside of the vfio API)?
The info is provided by vIOMMU emulator (e.g. virtual VT-d). The vSVA patch from Jacob has a sanity check on it.
https://lkml.org/lkml/2019/10/28/873Quoted 6 lines
The vIOMMU emulator runs at a layer above vfio. How does the vIOMMU emulator know that the vfio interface supports virtual VT-d? IMO, it's not acceptable that the user simply assume that an Intel host platform supports VT-d. For example, consider what happens when we need to define
IOMMU_PASID_FORMAT_INTEL_VTDv2. How would the user learn that VTDv2 is supported and the original VTD format is not supported?
Quoted 4 lines
I guess this may be another info
VFIO_IOMMU_GET_INFOshould provide. It makes sense that vfio be aware of what platform it is running on. right? After vfio gets the info, may let vfio fill in the format info. Is it the correct direction?
I thought you were planning to put that information in sysfs? We last discussed this over a year ago so I don't remember where we left it. I know Alex isn't keen on putting in sysfs what can be communicated through VFIO, but it is a convenient way to describe IOMMU features:
http://www.linux-arm.org/git?p=linux-jpb.git;a=commitdiff;h=665370d5b5e0022c24b2d2b57975ef6fe7b40870;hp=7ce780d838889b53f5e04ba5d444520621261eda
My problem with GET_INFO was that it could be difficult to extend, and to describe things like variable-size list of supported page table formats, but I guess the new info capabilities make this easier.
Thanks,
Jean
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
RE: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to host
Quoted 67 lines
From: Jean-Philippe Brucker [mailto:[email protected]]
Sent: Wednesday, November 13, 2019 6:29 PM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to hostQuoted 5 lines · Yi L
Quoted 5 lines
From: Alex Williamson <[email protected]>
Sent: Wednesday, November 13, 2019 1:26 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to hostQuoted 7 lines
Quoted 7 lines
Quoted 4 lines · Yi L
Quoted 4 lines
From: Alex Williamson < [email protected] >
Sent: Friday, November 8, 2019 7:21 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) tohost
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines · On Thu, 24 Oct 2019 08:26:23 -0400 Liu Yi L <[email protected]> wrote:
This patch adds vfio support to bind guest translation structure to host iommu. VFIO exposes iommu programming capability to user- space. Guest is a user-space application in host under KVM solution. For SVA usage in Virtual Machine, guest owns
GVA->GPAtranslation structure. And this part should be passdown to host to enable nested translation (or say two stage translation). This patch reuses theVFIO_IOMMU_BINDproposal from Jean-Philippe Brucker, and adds new bind type for binding guest owned translation structure to host.*) Add two new ioctls for VFIO containers.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
-
VFIO_IOMMU_BIND: for bind request from userspace, it could be bind a process to a pasid or bind a guest pasid to a device, this is indicated by type
-VFIO_IOMMU_UNBIND: for unbind request from userspace, it could be unbind a process to a pasid or unbind a guest pasid to a device, also indicated by type
- Bind type:
VFIO_IOMMU_BIND_PROCESS: user-space request to bind aprocess
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
to a device
VFIO_IOMMU_BIND_GUEST_PASID: bind guest owned translation
structure to host iommu. e.g. guest page table*) Code logic in
vfio_iommu_type1_ioctl()to handleVFIO_IOMMU_BIND/UNBIND
Quoted 1 line
Quoted 1 line
Quoted 1 line
[...]
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_pasid_free(struct vfio_iommu *iommu,
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c +3
-0+static long vfio_iommu_type1_unbind_gpasid (struct vfio_iommu *iommu, +void __user *arg, + struct vfio_iommu_type1_bind*bind)
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_pasid_free(struct vfio_iommu *iommu, · line 2367
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c +8
-0+{ +struct iommu_gpasid_bind_data gbind_data; +unsigned long minsz; +int ret =0 ; + + minsz =sizeof (*bind) +sizeof (gbind_data); +if (bind->argsz < minsz) +return -EINVAL ;But
gbind_datacan change size if new vendor specific data is added to the union, so kernel updates break existing userspace. Fail.
Quoted 2 lines
I guess we could take minsz up to the vendor-specific data, copy @format, and then check the size of vendor-specific data?
Agreed.
Quoted 48 lines
Quoted 24 lines
Quoted 10 lines
Quoted 7 lines
yes, we have a version field in struct
iommu_gpasid_bind_data. How about doing sanity check per versions? kernel knows thegbind_datasize of specific versions. Does it make sense? If yes, I'll also apply it to the other sanity check in this series to avoid userspace fail after kernel update.Has it already been decided that the version field will be updated for every addition to the union?
No, just my proposal. Jacob may help to explain the purpose of version field. But if we may be too "frequent" for an uapi version number updating if we inc version for each change in the union part. I may vote for the second option from you below.
Quoted 7 lines
It seems there are two options, either
the version definition includes the possible contents of the union, which means we need to support multiple versions concurrently in the kernel to maintain compatibility with userspace and follow deprecation protocols for removing that support, or we need to consider version to be the general form of the structure and interpret the format field to determine necessary length to copy from the user.Quoted 6 lines
As I mentioned above, may be better to let @version field only over the general fields and let format to cover the possible changes in union. e.g.
IOMMU_PASID_FORMAT_INTEL_VTD2may means version 2 of Intel VT-d bind. But either way, I think we need to let kernel maintain multiple versions to support compatible userspace. e.g. may have multiple versionsiommu_gpasid_bind_data_vtdstruct in the union part.I couldn't find where the @version field originated in our old discussions, but I believe our plan for allowing future extensions was:
* Add new vendor-specific data by introducing a new format (
IOMMU_PASID_FORMAT_INTEL_VTD2,IOMMU_PASID_FORMAT_ARM_SMMUV2...), and
extend the union.* Add a new common field, if it fits in the existing padding bytes, by adding a flag (IOMMU_SVA_GPASID_*).
* Add a new common field, if it doesn't fit in the current padding bytes, or completely change the structure layout, by introducing a new version (
IOMMU_GPASID_BIND_VERSION_2). In that case the kernel has to handle both new and old structure versions. It would have bothiommu_gpasid_bind_dataandiommu_gpasid_bind_data_v2structs.
Quoted 5 lines
I think
iommu_cache_invalidate_infoandiommu_page_responseuse the same scheme.iommu_faultis a bit more complicated because it'skernel->userspaceand requires some negotiation:
https://lore.kernel.org/linux-iommu/77405d39-81a4-d9a8-5d35-
[email protected]/
Thanks for the excellent recap.
Quoted 15 lines
[...]
Quoted 14 lines
Quoted 5 lines
If the ioctls have similar purpose and form, then re-using a single ioctl might make sense, but
BIND_PROCESSis only a place-holder in this series, which is not acceptable. A dual purpose ioctl does not preclude that we could also use a union for the data field to make the structure well specified.yes,
BIND_PROCESSis only a place-holder here. From kernel p.o.v., bothBIND_GUEST_PASIDandBIND_PROCESSare bind requests from userspace. So the purposes are aligned. Below is the content the @data[] field supposed to convey forBIND_PROCESS. If we use union, it would leave space for extending it to supportBIND_PROCESS. If only data[], it is a little bit confusing why we define it in such manner ifBIND_PROCESSis included in this series. Please feel free let me know which one suits better.
Quoted 10 lines
Quoted 7 lines
(quoted diff) +6
-0+struct vfio_iommu_type1_bind_process { + __u32 flags; +#define VFIO_IOMMU_BIND_PID (1 << 0) + __u32 pasid; + __s32 pid; +};Note that I don't plan to upstream
BIND_PROCESSat the moment. It was useful for testing but I don't know of anyone actually needing it.
yes, you told me during KVM forum. But if we want to share IOCTL, may need to leave a place for you to extend. If @data[] is not good, then may use union.
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 2 lines
Quoted 2 lines
That bind data
structure expects a format (ex.IOMMU_PASID_FORMAT_INTEL_VTD). Howdoes
Quoted 6 lines
Quoted 2 lines
a user determine what formats are accepted from within the vfio API (or even outside of the vfio API)?
The info is provided by vIOMMU emulator (e.g. virtual VT-d). The vSVA patch from Jacob has a sanity check on it.
https://lkml.org/lkml/2019/10/28/873The vIOMMU emulator runs at a layer above vfio. How does the vIOMMU emulator know that the vfio interface supports virtual VT-d? IMO, it's not acceptable that the user simply assume that an Intel host platform supports VT-d. For example, consider what happens when we need to define
IOMMU_PASID_FORMAT_INTEL_VTDv2. How would the user learn that VTDv2 is supported and the original VTD format is not supported?
Quoted 7 lines
Quoted 4 lines
I guess this may be another info
VFIO_IOMMU_GET_INFOshould provide. It makes sense that vfio be aware of what platform it is running on. right? After vfio gets the info, may let vfio fill in the format info. Is it the correct direction?I thought you were planning to put that information in sysfs? We last discussed this over a year ago so I don't remember where we left it. I
yes, we did have such discussion to do hardware iommu capability query via sysfs. If only want to let vIOMMU learn what format it should use, then GET_INFO may be enough. e.g. vfio just asks its backed iommu driver. hey, do you support nested translation? what format do you prefer? But I'm open on it.
Quoted 9 lines
know Alex isn't keen on putting in sysfs what can be communicated through VFIO, but it is a convenient way to describe IOMMU features:
http://www.linux-arm.org/git?p=linux-
jpb.git;a=commitdiff;h=665370d5b5e0022c24b2d2b57975ef6fe7b40870;hp=7ce780d838889b53f5e04ba5d444520621261edaMy problem with
GET_INFOwas that it could be difficult to extend, and to describe things like variable-size list of supported page table formats, but I guess the new info capabilities make this easier.
yeah, you also need to make the info generic if want to extend something. As I said, I'm open with it. Please feel free let me know if you've got other ideas.
Regards,
Yi Liu
Quoted 2 lines
Thanks,
Jean
RE: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to host
In reply to Jean-Philippe BruckerHi Alex,
Thanks for the review. Here I'd like to conclude the major opens in this thread and see if we can get some agreements to prepare a new version.
a) IOCTLs for BIND_GPASID and BIND_PROCESS, share a single IOCTL or two separate IOCTLs?
Yi: It may be helpful to have separate IOCTLs. The bind data conveyed for BIND_GPASID and BIND_PROCESS are totally different, and the struct iommu_gpasid_bind_data has vendor specific data and may even have more versions in future. To better maintain it, I guess separate IOCTLs for the two bind types would be better. The structure for BIND_GPASID is as below:
struct vfio_iommu_type1_bind {
__u32 argsz;
struct iommu_gpasid_bind_data bind_data;
};b) how kernel-space learns the number of bytes to be copied (a.k.a. the usage of @version field and @format field of struct iommu_gpasid_bind_data)
Yi: Jean has an excellent recap in prior reply on the plan of future extensions regards to @version field and @format field. Based on the plan, kernel space needs to parse the @version field and @format field to get the length of the current BIND_GPASID request. Also kernel needs to maintain the new and old structure versions. Follow specific deprecation policy in future.
c) how can vIOMMU emulator know that the vfio interface supports to config dual stage translation for vIOMMU?
Yi: may do it via VFIO_IOMMU_GET_INFO.
d) how can vIOMMU emulator know what @version and @format should be set in struct iommu_gpasid_bind_data?
Yi: currently, we have two ways. First one, may do it via VFIO_IOMMU_GET_INFO. This is a natural idea as here @version and @format are used in vfio apis. It makes sense to let vfio to provide related info to vIOMMU emulator after checking with vendor specific iommu driver. Also, there is idea to do it via sysfs (/sys/class/iommu/dmar#) as we have plan to do IOMMU capability sync between vIOMMU and pIOMMU via sysfs. I have two concern on this option. Current iommu sysfs only provides vendor specific hardware infos. I'm not sure if it is good to expose infos defined in IOMMU generic layer via iommu sysfs. If this concern is not a big thing, I'm fine with both options.
Thoughts? Would also be happy to know more from you guys.
Regards,
Yi Liu
Quoted 185 lines
From: Jean-Philippe Brucker [mailto:[email protected]]
Sent: Wednesday, November 13, 2019 6:29 PM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to hostQuoted 5 lines · Yi L
Quoted 5 lines
From: Alex Williamson <[email protected]>
Sent: Wednesday, November 13, 2019 1:26 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to hostQuoted 7 lines
Quoted 7 lines
Quoted 4 lines · Yi L
Quoted 4 lines
From: Alex Williamson < [email protected] >
Sent: Friday, November 8, 2019 7:21 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) tohost
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines · On Thu, 24 Oct 2019 08:26:23 -0400 Liu Yi L <[email protected]> wrote:
This patch adds vfio support to bind guest translation structure to host iommu. VFIO exposes iommu programming capability to user- space. Guest is a user-space application in host under KVM solution. For SVA usage in Virtual Machine, guest owns
GVA->GPAtranslation structure. And this part should be passdown to host to enable nested translation (or say two stage translation). This patch reuses theVFIO_IOMMU_BINDproposal from Jean-Philippe Brucker, and adds new bind type for binding guest owned translation structure to host.*) Add two new ioctls for VFIO containers.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
-
VFIO_IOMMU_BIND: for bind request from userspace, it could be bind a process to a pasid or bind a guest pasid to a device, this is indicated by type
-VFIO_IOMMU_UNBIND: for unbind request from userspace, it could be unbind a process to a pasid or unbind a guest pasid to a device, also indicated by type
- Bind type:
VFIO_IOMMU_BIND_PROCESS: user-space request to bind aprocess
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
to a device
VFIO_IOMMU_BIND_GUEST_PASID: bind guest owned translation
structure to host iommu. e.g. guest page table*) Code logic in
vfio_iommu_type1_ioctl()to handleVFIO_IOMMU_BIND/UNBIND
Quoted 1 line
Quoted 1 line
Quoted 1 line
[...]
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_pasid_free(struct vfio_iommu *iommu,
Quoted 3 lines
drivers/vfio/vfio_iommu_type1.c +3
-0+static long vfio_iommu_type1_unbind_gpasid (struct vfio_iommu *iommu, +void __user *arg, + struct vfio_iommu_type1_bind*bind)
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_pasid_free(struct vfio_iommu *iommu, · line 2367
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c +8
-0+{ +struct iommu_gpasid_bind_data gbind_data; +unsigned long minsz; +int ret =0 ; + + minsz =sizeof (*bind) +sizeof (gbind_data); +if (bind->argsz < minsz) +return -EINVAL ;But
gbind_datacan change size if new vendor specific data is added to the union, so kernel updates break existing userspace. Fail.I guess we could take minsz up to the vendor-specific data, copy @format, and then check the size of vendor-specific data?
Quoted 23 lines
Quoted 9 lines
Quoted 6 lines
yes, we have a version field in struct
iommu_gpasid_bind_data. How about doing sanity check per versions? kernel knows thegbind_datasize of specific versions. Does it make sense? If yes, I'll also apply it to the other sanity check in this series to avoid userspace fail after kernel update.Has it already been decided that the version field will be updated for every addition to the union?
No, just my proposal. Jacob may help to explain the purpose of version field. But if we may be too "frequent" for an uapi version number updating if we inc version for each change in the union part. I may vote for the second option from you below.
Quoted 7 lines
It seems there are two options, either
the version definition includes the possible contents of the union, which means we need to support multiple versions concurrently in the kernel to maintain compatibility with userspace and follow deprecation protocols for removing that support, or we need to consider version to be the general form of the structure and interpret the format field to determine necessary length to copy from the user.Quoted 6 lines
As I mentioned above, may be better to let @version field only over the general fields and let format to cover the possible changes in union. e.g.
IOMMU_PASID_FORMAT_INTEL_VTD2may means version 2 of Intel VT-d bind. But either way, I think we need to let kernel maintain multiple versions to support compatible userspace. e.g. may have multiple versionsiommu_gpasid_bind_data_vtdstruct in the union part.I couldn't find where the @version field originated in our old discussions, but I believe our plan for allowing future extensions was:
* Add new vendor-specific data by introducing a new format (
IOMMU_PASID_FORMAT_INTEL_VTD2,IOMMU_PASID_FORMAT_ARM_SMMUV2...), and
extend the union.* Add a new common field, if it fits in the existing padding bytes, by adding a flag (IOMMU_SVA_GPASID_*).
* Add a new common field, if it doesn't fit in the current padding bytes, or completely change the structure layout, by introducing a new version (
IOMMU_GPASID_BIND_VERSION_2). In that case the kernel has to handle both new and old structure versions. It would have bothiommu_gpasid_bind_dataandiommu_gpasid_bind_data_v2structs.I think
iommu_cache_invalidate_infoandiommu_page_responseuse the same scheme.iommu_faultis a bit more complicated because it'skernel->userspaceand requires some negotiation:
https://lore.kernel.org/linux-iommu/77405d39-81a4-d9a8-5d35-
[email protected]/[...]
Quoted 20 lines
Quoted 5 lines
If the ioctls have similar purpose and form, then re-using a single ioctl might make sense, but
BIND_PROCESSis only a place-holder in this series, which is not acceptable. A dual purpose ioctl does not preclude that we could also use a union for the data field to make the structure well specified.yes,
BIND_PROCESSis only a place-holder here. From kernel p.o.v., bothBIND_GUEST_PASIDandBIND_PROCESSare bind requests from userspace. So the purposes are aligned. Below is the content the @data[] field supposed to convey forBIND_PROCESS. If we use union, it would leave space for extending it to supportBIND_PROCESS. If only data[], it is a little bit confusing why we define it in such manner ifBIND_PROCESSis included in this series. Please feel free let me know which one suits better.(quoted diff) +6
-0+struct vfio_iommu_type1_bind_process { + __u32 flags; +#define VFIO_IOMMU_BIND_PID (1 << 0) + __u32 pasid; + __s32 pid; +};Quoted 1 line
Note that I don't plan to upstream
BIND_PROCESSat the moment. It was useful for testing but I don't know of anyone actually needing it.Quoted 17 lines
Quoted 10 lines
Quoted 2 lines
Quoted 2 lines
That bind data
structure expects a format (ex.IOMMU_PASID_FORMAT_INTEL_VTD). Howdoes
Quoted 6 lines
Quoted 2 lines
a user determine what formats are accepted from within the vfio API (or even outside of the vfio API)?
The info is provided by vIOMMU emulator (e.g. virtual VT-d). The vSVA patch from Jacob has a sanity check on it.
https://lkml.org/lkml/2019/10/28/873Quoted 6 lines
The vIOMMU emulator runs at a layer above vfio. How does the vIOMMU emulator know that the vfio interface supports virtual VT-d? IMO, it's not acceptable that the user simply assume that an Intel host platform supports VT-d. For example, consider what happens when we need to define
IOMMU_PASID_FORMAT_INTEL_VTDv2. How would the user learn that VTDv2 is supported and the original VTD format is not supported?Quoted 4 lines
I guess this may be another info
VFIO_IOMMU_GET_INFOshould provide. It makes sense that vfio be aware of what platform it is running on. right? After vfio gets the info, may let vfio fill in the format info. Is it the correct direction?I thought you were planning to put that information in sysfs? We last discussed this over a year ago so I don't remember where we left it. I know Alex isn't keen on putting in sysfs what can be communicated through VFIO, but it is a convenient way to describe IOMMU features:
http://www.linux-arm.org/git?p=linux-
jpb.git;a=commitdiff;h=665370d5b5e0022c24b2d2b57975ef6fe7b40870;hp=7ce780d838889b53f5e04ba5d444520621261edaMy problem with
GET_INFOwas that it could be difficult to extend, and to describe things like variable-size list of supported page table formats, but I guess the new info capabilities make this easier.Thanks,
Jean
Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to host
Quoted 14 lines · Yi L
Hi Alex,
Thanks for the review. Here I'd like to conclude the major opens in this thread and see if we can get some agreements to prepare a new version.
a) IOCTLs for
BIND_GPASIDandBIND_PROCESS, share a single IOCTL or two separate IOCTLs?
Yi: It may be helpful to have separate IOCTLs. The bind data conveyed forBIND_GPASIDandBIND_PROCESSare totally different, and the structiommu_gpasid_bind_datahas vendor specific data and may even have more versions in future. To better maintain it, I guess separate IOCTLs for the two bind types would be better. The structure forBIND_GPASIDis as below:
Quoted 4 lines
struct vfio_iommu_type1_bind {
__u32 argsz;
struct iommu_gpasid_bind_data bind_data;
};We've been rather successful at extending ioctls in vfio and I'm generally opposed to rampant ioctl proliferation. If we added @flags to the above struct (as pretty much the standard for vfio ioctls), then we could use it to describe the type of binding to perform and therefore the type of data provided. I think my major complaint here was that we were defining PROCESS but not implementing it. We can design the ioctl to enable it, but not define it until it's implemented.
Quoted 9 lines
b) how kernel-space learns the number of bytes to be copied (a.k.a. the usage of @version field and @format field of struct
iommu_gpasid_bind_data)
Yi: Jean has an excellent recap in prior reply on the plan of future extensions regards to @version field and @format field. Based on the plan, kernel space needs to parse the @version field and @format field to get the length of the currentBIND_GPASIDrequest. Also kernel needs to maintain the new and old structure versions. Follow specific deprecation policy in future.
Yes, it seems reasonable, so from the struct above (plus @flags) we could determine we have struct iommu_gpasid_bind_data as the payload and read that using @version and @format as outlined.
Quoted 3 lines
c) how can vIOMMU emulator know that the vfio interface supports to config dual stage translation for vIOMMU?
Yi: may do it viaVFIO_IOMMU_GET_INFO.
Yes please.
Quoted 4 lines
d) how can vIOMMU emulator know what @version and @format should be set in struct
iommu_gpasid_bind_data?
Yi: currently, we have two ways. First one, may do it viaVFIO_IOMMU_GET_INFO. This is a natural idea as here @version and @format
Quoted 8 lines
are used in vfio apis. It makes sense to let vfio to provide related info to vIOMMU emulator after checking with vendor specific iommu driver. Also, there is idea to do it via sysfs (/sys/class/iommu/dmar#) as we have plan to do IOMMU capability sync between vIOMMU and pIOMMU via sysfs. I have two concern on this option. Current iommu sysfs only provides vendor specific hardware infos. I'm not sure if it is good to expose infos defined in IOMMU generic layer via iommu sysfs. If this concern is not a big thing, I'm fine with both options.
This seems like the same issue we had with IOMMU reserved regions, I'd prefer that a user can figure out how to interact with the vfio interface through the vfio interface. Forcing the user to poke around in sysfs requires the user to have read permissions to sysfs in places they otherwise wouldn't need. Thanks,
Alex
Quoted 186 lines
Quoted 185 lines
From: Jean-Philippe Brucker [mailto:[email protected]]
Sent: Wednesday, November 13, 2019 6:29 PM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to hostQuoted 5 lines · Yi L
Quoted 5 lines
From: Alex Williamson <[email protected]>
Sent: Wednesday, November 13, 2019 1:26 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to hostQuoted 7 lines
Quoted 7 lines
Quoted 4 lines · Yi L
Quoted 4 lines
From: Alex Williamson < [email protected] >
Sent: Friday, November 8, 2019 7:21 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) tohost
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines · On Thu, 24 Oct 2019 08:26:23 -0400 Liu Yi L <[email protected]> wrote:
This patch adds vfio support to bind guest translation structure to host iommu. VFIO exposes iommu programming capability to user- space. Guest is a user-space application in host under KVM solution. For SVA usage in Virtual Machine, guest owns
GVA->GPAtranslation structure. And this part should be passdown to host to enable nested translation (or say two stage translation). This patch reuses theVFIO_IOMMU_BINDproposal from Jean-Philippe Brucker, and adds new bind type for binding guest owned translation structure to host.*) Add two new ioctls for VFIO containers.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
-
VFIO_IOMMU_BIND: for bind request from userspace, it could be bind a process to a pasid or bind a guest pasid to a device, this is indicated by type
-VFIO_IOMMU_UNBIND: for unbind request from userspace, it could be unbind a process to a pasid or unbind a guest pasid to a device, also indicated by type
- Bind type:
VFIO_IOMMU_BIND_PROCESS: user-space request to bind aprocess
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
to a device
VFIO_IOMMU_BIND_GUEST_PASID: bind guest owned translation
structure to host iommu. e.g. guest page table*) Code logic in
vfio_iommu_type1_ioctl()to handleVFIO_IOMMU_BIND/UNBIND
Quoted 1 line
Quoted 1 line
Quoted 1 line
[...]
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+static long vfio_iommu_type1_unbind_gpasid(struct vfio_iommu *iommu, + void __user *arg, + struct vfio_iommu_type1_bind*bind)
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+{ + struct iommu_gpasid_bind_data gbind_data; + unsigned long minsz; + int ret = 0; + + minsz = sizeof(*bind) + sizeof(gbind_data); + if (bind->argsz < minsz) + return -EINVAL;But
gbind_datacan change size if new vendor specific data is added to the union, so kernel updates break existing userspace. Fail.I guess we could take minsz up to the vendor-specific data, copy @format, and then check the size of vendor-specific data?
Quoted 23 lines
Quoted 9 lines
Quoted 6 lines
yes, we have a version field in struct
iommu_gpasid_bind_data. How about doing sanity check per versions? kernel knows thegbind_datasize of specific versions. Does it make sense? If yes, I'll also apply it to the other sanity check in this series to avoid userspace fail after kernel update.Has it already been decided that the version field will be updated for every addition to the union?
No, just my proposal. Jacob may help to explain the purpose of version field. But if we may be too "frequent" for an uapi version number updating if we inc version for each change in the union part. I may vote for the second option from you below.
Quoted 7 lines
It seems there are two options, either
the version definition includes the possible contents of the union, which means we need to support multiple versions concurrently in the kernel to maintain compatibility with userspace and follow deprecation protocols for removing that support, or we need to consider version to be the general form of the structure and interpret the format field to determine necessary length to copy from the user.Quoted 6 lines
As I mentioned above, may be better to let @version field only over the general fields and let format to cover the possible changes in union. e.g.
IOMMU_PASID_FORMAT_INTEL_VTD2may means version 2 of Intel VT-d bind. But either way, I think we need to let kernel maintain multiple versions to support compatible userspace. e.g. may have multiple versionsiommu_gpasid_bind_data_vtdstruct in the union part.I couldn't find where the @version field originated in our old discussions, but I believe our plan for allowing future extensions was:
* Add new vendor-specific data by introducing a new format (
IOMMU_PASID_FORMAT_INTEL_VTD2,IOMMU_PASID_FORMAT_ARM_SMMUV2...), and
extend the union.* Add a new common field, if it fits in the existing padding bytes, by adding a flag (IOMMU_SVA_GPASID_*).
* Add a new common field, if it doesn't fit in the current padding bytes, or completely change the structure layout, by introducing a new version (
IOMMU_GPASID_BIND_VERSION_2). In that case the kernel has to handle both new and old structure versions. It would have bothiommu_gpasid_bind_dataandiommu_gpasid_bind_data_v2structs.I think
iommu_cache_invalidate_infoandiommu_page_responseuse the same scheme.iommu_faultis a bit more complicated because it'skernel->userspaceand requires some negotiation:
https://lore.kernel.org/linux-iommu/77405d39-81a4-d9a8-5d35-
[email protected]/[...]
Quoted 20 lines
Quoted 5 lines
If the ioctls have similar purpose and form, then re-using a single ioctl might make sense, but
BIND_PROCESSis only a place-holder in this series, which is not acceptable. A dual purpose ioctl does not preclude that we could also use a union for the data field to make the structure well specified.yes,
BIND_PROCESSis only a place-holder here. From kernel p.o.v., bothBIND_GUEST_PASIDandBIND_PROCESSare bind requests from userspace. So the purposes are aligned. Below is the content the @data[] field supposed to convey forBIND_PROCESS. If we use union, it would leave space for extending it to supportBIND_PROCESS. If only data[], it is a little bit confusing why we define it in such manner ifBIND_PROCESSis included in this series. Please feel free let me know which one suits better.(quoted diff) +6
-0+struct vfio_iommu_type1_bind_process { + __u32 flags; +#define VFIO_IOMMU_BIND_PID (1 << 0) + __u32 pasid; + __s32 pid; +};Quoted 1 line
Note that I don't plan to upstream
BIND_PROCESSat the moment. It was useful for testing but I don't know of anyone actually needing it.Quoted 17 lines
Quoted 10 lines
Quoted 2 lines
Quoted 2 lines
That bind data
structure expects a format (ex.IOMMU_PASID_FORMAT_INTEL_VTD). Howdoes
Quoted 6 lines
Quoted 2 lines
a user determine what formats are accepted from within the vfio API (or even outside of the vfio API)?
The info is provided by vIOMMU emulator (e.g. virtual VT-d). The vSVA patch from Jacob has a sanity check on it.
https://lkml.org/lkml/2019/10/28/873Quoted 6 lines
The vIOMMU emulator runs at a layer above vfio. How does the vIOMMU emulator know that the vfio interface supports virtual VT-d? IMO, it's not acceptable that the user simply assume that an Intel host platform supports VT-d. For example, consider what happens when we need to define
IOMMU_PASID_FORMAT_INTEL_VTDv2. How would the user learn that VTDv2 is supported and the original VTD format is not supported?Quoted 4 lines
I guess this may be another info
VFIO_IOMMU_GET_INFOshould provide. It makes sense that vfio be aware of what platform it is running on. right? After vfio gets the info, may let vfio fill in the format info. Is it the correct direction?I thought you were planning to put that information in sysfs? We last discussed this over a year ago so I don't remember where we left it. I know Alex isn't keen on putting in sysfs what can be communicated through VFIO, but it is a convenient way to describe IOMMU features:
http://www.linux-arm.org/git?p=linux-
jpb.git;a=commitdiff;h=665370d5b5e0022c24b2d2b57975ef6fe7b40870;hp=7ce780d838889b53f5e04ba5d444520621261edaMy problem with
GET_INFOwas that it could be difficult to extend, and to describe things like variable-size list of supported page table formats, but I guess the new info capabilities make this easier.Thanks,
Jean
RE: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to host
Quoted 28 lines
From: Alex Williamson [mailto:[email protected]]
Sent: Tuesday, December 3, 2019 8:12 AM
To: Liu, Yi L <[email protected]>
Subject: Re: [RFC v2 3/3] vfio/type1: bind guest pasid (guest page tables) to hostQuoted 14 lines · Yi L
Hi Alex,
Thanks for the review. Here I'd like to conclude the major opens in this thread and see if we can get some agreements to prepare a new version.
a) IOCTLs for
BIND_GPASIDandBIND_PROCESS, share a single IOCTL or two separate IOCTLs?
Yi: It may be helpful to have separate IOCTLs. The bind data conveyed forBIND_GPASIDandBIND_PROCESSare totally different, and the structiommu_gpasid_bind_datahas vendor specific data and may even have more versions in future. To better maintain it, I guess separate IOCTLs for the two bind types would be better. The structure forBIND_GPASIDis as below:Quoted 4 lines
struct vfio_iommu_type1_bind { __u32 argsz; struct iommu_gpasid_bind_data bind_data; };
Quoted 7 lines
We've been rather successful at extending ioctls in vfio and I'm generally opposed to rampant ioctl proliferation. If we added @flags to the above struct (as pretty much the standard for vfio ioctls), then we could use it to describe the type of binding to perform and therefore the type of data provided. I think my major complaint here was that we were defining PROCESS but not implementing it. We can design the ioctl to enable it, but not define it until it's implemented.
sure, so I'll pull back the @flags field. BTW. Regards to the payloads, what would be preferred? @data[] or a wrapper structure like below?
union {
struct iommu_gpasid_bind_data bind_gpasid;
}bind_data;Quoted 10 lines
Quoted 9 lines
b) how kernel-space learns the number of bytes to be copied (a.k.a. the usage of @version field and @format field of struct
iommu_gpasid_bind_data)
Yi: Jean has an excellent recap in prior reply on the plan of future extensions regards to @version field and @format field. Based on the plan, kernel space needs to parse the @version field and @format field to get the length of the currentBIND_GPASIDrequest. Also kernel needs to maintain the new and old structure versions. Follow specific deprecation policy in future.
Quoted 3 lines
Yes, it seems reasonable, so from the struct above (plus @flags) we could determine we have struct
iommu_gpasid_bind_dataas the payload and read that using @version and @format as outlined.
sure, thanks.
Quoted 5 lines
Quoted 3 lines
c) how can vIOMMU emulator know that the vfio interface supports to config dual stage translation for vIOMMU?
Yi: may do it viaVFIO_IOMMU_GET_INFO.Yes please.
got it.
Quoted 13 lines
Quoted 4 lines
d) how can vIOMMU emulator know what @version and @format should be set in struct
iommu_gpasid_bind_data?
Yi: currently, we have two ways. First one, may do it viaVFIO_IOMMU_GET_INFO. This is a natural idea as here @version and @formatQuoted 8 lines
are used in vfio apis. It makes sense to let vfio to provide related info to vIOMMU emulator after checking with vendor specific iommu driver. Also, there is idea to do it via sysfs (/sys/class/iommu/dmar#) as we have plan to do IOMMU capability sync between vIOMMU and pIOMMU via sysfs. I have two concern on this option. Current iommu sysfs only provides vendor specific hardware infos. I'm not sure if it is good to expose infos defined in IOMMU generic layer via iommu sysfs. If this concern is not a big thing, I'm fine with both options.
Quoted 5 lines
This seems like the same issue we had with IOMMU reserved regions, I'd prefer that a user can figure out how to interact with the vfio interface through the vfio interface. Forcing the user to poke around in sysfs requires the user to have read permissions to sysfs in places they otherwise wouldn't need. Thanks,
thanks, let me prepare a new version.
Regards,
Yi Liu
Quoted 1 line
Alex
Quoted 65 lines
From: Liu Yi L
Sent: Thursday, October 24, 2019 8:26 PMShared virtual address (SVA), a.k.a, Shared virtual memory (SVM) on Intel platforms allow address space sharing between device DMA and applications.
SVA can reduce programming complexity and enhance security. This series is intended to expose SVA capability to VMs. i.e. shared guest application address space with passthru devices. The whole SVA virtualization
requires QEMU/VFIO/IOMMU changes. This series includes the VFIO changes, for
QEMU and IOMMU changes, they are in separate series (listed in the "Related
series").The high-level architecture for SVA virtualization is as below:
.-------------. .---------------------------. | 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 tablesThere are roughly three parts in this patchset which are corresponding to the basic vSVA support for PCI device assignment
1. vfio support for PASID allocation and free from VMs
2. vfio support for guest PASID binding from VMs
3. vfio support for IOMMU cache invalidation from VMsThe complete vSVA upstream patches are divided into three phases:
1. Common APIs and PCI device direct assignment
2. Page Request Services (PRS) support
3. Mediated device assignmentThis RFC patchset is aiming for the phase 1, and works together with the VT-d driver[1] changes and QEMU changes[2]. Complete set for vSVA can be
found in:
https://github.com/jacobpan/linux.git:siov_sva.And this patchset doesn't include the patch to expose PASID capability to guest. This is expected to be in another series.
Quoted 7 lines
Related series:
[1] [PATCH v6 00/10] Nested Shared Virtual Address (SVA) VT-d support:
https://lkml.org/lkml/2019/10/22/953
<This series is based on this kernel series from Jacob Pan>[2] [RFC v2 00/20]
intel_iommu: expose Shared Virtual Addressing to VM from Yi Liu
there is no link, and should be [RFC v2 00/22]
Quoted 23 lines
Changelog:
- RFC v1 -> v2:
Dropped vfio: VFIO_IOMMU_ATTACH/DETACH_PASID_TABLE. RFC v1: https://patchwork.kernel.org/cover/11033699/Liu Yi L (3):
vfio:VFIO_IOMMU_CACHE_INVALIDATE
vfio/type1:VFIO_IOMMU_PASID_REQUEST(alloc/free)vfio/type1: bind guest pasid (guest page tables) to hostdrivers/vfio/vfio_iommu_type1.c | 305 ++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 82 +++++++++++ 2 files changed, 387 insertions(+)--
2.7.4_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Hi Kevin,
Quoted 20 lines
From: Tian, Kevin
Sent: Friday, October 25, 2019 4:59 PM
To: Liu, Yi L <[email protected]>; [email protected];
Subject: RE: [RFC v2 0/3] vfio: support Shared Virtual AddressingQuoted 15 lines
From: Liu Yi L
Sent: Thursday, October 24, 2019 8:26 PMShared virtual address (SVA), a.k.a, Shared virtual memory (SVM) on Intel platforms allow address space sharing between device DMA and applications.
SVA can reduce programming complexity and enhance security. This series is intended to expose SVA capability to VMs. i.e. shared guest application address space with passthru devices. The whole SVA virtualization requires QEMU/VFIO/IOMMU changes. This series includes the VFIO changes, for QEMU and IOMMU changes, they are in separate series (listed in the "Related series").The high-level architecture for SVA virtualization is as below:
[...]
Quoted 10 lines
Quoted 8 lines
Related series:
[1] [PATCH v6 00/10] Nested Shared Virtual Address (SVA) VT-d support:
https://lkml.org/lkml/2019/10/22/953
<This series is based on this kernel series from Jacob Pan>[2] [RFC v2 00/20]
intel_iommu: expose Shared Virtual Addressing to VM from Yi Liuthere is no link, and should be [RFC v2 00/22]
The link is not generated at the time this series is sent out. Yeah, should be [RFC v2 0/22]. Thanks for spotting it.
Regards,
Yi Liu