KVM PCIe/MSI passthrough on ARM/ARM64: kernel part 3/3: vfio changes
Subsystems (4)
v12 cover KVM PCIe/MSI passthrough on ARM/ARM64: kernel part 3/3: vfio changes
This series allows the user-space to register a reserved IOVA domain. This completes the kernel integration of the whole functionality on top of the 2 previous parts (v11).
We reuse the VFIO DMA MAP ioctl with a new flag to bridge to the msi-iommu API. The need for provisioning such MSI IOVA range is reported through capability chain, using VFIO_IOMMU_TYPE1_INFO_CAP_MSI_GEOMETRY.
vfio_iommu_type1 checks if the MSI mapping is safe when attaching the vfio group to the container (allow_unsafe_interrupts modality). This is done in a very coarse way, looking at all the registered doorbells and returning assignment is safe wrt IRQ if all the doorbells are safe.
More details & context can be found at:
http://www.linaro.org/blog/core-dump/kvm-pciemsi-passthrough-armarm64/
Best Regards
Eric
Testing:
- functional on ARM64 AMD Overdrive HW (single GICv2m frame) with Intel I350T2 (SR-IOV capable, igb/igbvf) and Intel 82574L (e1000e)
- functional on Cavium ThunderX (ARM GICv3 ITS) with Intel 82574L (e1000e)
References:
[1] [RFC 0/2] VFIO: Add virtual MSI doorbell support
(https://lkml.org/lkml/2015/7/24/135) [2] [RFC PATCH 0/6] vfio: Add interface to map MSI pages (https://lists.cs.columbia.edu/pipermail/kvmarm/2015-September/016607.html) [3] [PATCH v2 0/3] Introduce MSI hardware mapping for VFIO (http://permalink.gmane.org/gmane.comp.emulators.kvm.arm.devel/3858)
Git: complete series available at
https://github.com/eauger/linux/tree/v4.7-rc7-passthrough-v12
previous: https://github.com/eauger/linux/tree/v4.7-rc7-passthrough-v11
the above branch includes a temporary patch to work around a ThunderX pci bus reset crash (which I think unrelated to this series):
"vfio: pci: HACK! workaround thunderx pci_try_reset_bus crash" Do not take this one for other platforms.
History:
v11 -> v12:
- no functional change. Only adapt to renamings done in PART II
v10 -> v11:
no change to this series, just incremented for consistency
v9 -> v10:
Took into account Alex' comments:
- split "vfio/type1: vfio_find_dma accepting a type argument" into 2 patches
- properly implement replay of MSI DMA slots (by setting the aperture on the new domain); fixes the assignment of several devices
- rework user api for vfio_iommu_type1_info capability chains (cap_offset at the end of the struct and fix padding issues)
- VFIO_IOVA_RESERVED renamed into VFIO_IOVA_MSI_RESERVED
- explicit dma->type setting to VFIO_IOVA_USER
v8 -> v9:
- report MSI geometry through capability chain (last patch only); with the current limitation that an arbitrary number of 16 page requirement is reported. To be improved later on.
v7 -> v8:
- use renamed msi-iommu API
- VFIO only responsible for setting the IOVA aperture
- use new DOMAIN_ATTR_MSI_GEOMETRY iommu domain attribute
v6 -> v7:
- vfio_find_dma now accepts a dma_type argument.
- should have recovered the capability to unmap the whole user IOVA range
- remove computation of nb IOVA pages -> will post a separate RFC for that while respinning the QEMU part
RFC v5 -> patch v6:
- split to ease the review process
RFC v4 -> RFC v5:
- take into account Thomas' comments on MSI related patches
- split "msi: IOMMU map the doorbell address when needed"
- increase readability and add comments
- fix style issues
- split "iommu: Add DOMAIN_ATTR_MSI_MAPPING attribute"
- platform ITS now advertises IOMMU_CAP_INTR_REMAP
- fix compilation issue with CONFIG_IOMMU API unset
- arm-smmu-v3 now advertises DOMAIN_ATTR_MSI_MAPPING
RFC v3 -> v4:
- Move doorbell mapping/unmapping in msi.c
- fix ref count issue on set_affinity: in case of a change in the address the previous address is decremented
- doorbell map/unmap now is done on msi composition. Should allow the use case for platform MSI controllers
- create dma-reserved-iommu.h/c exposing/implementing a new API dedicated to reserved IOVA management (looking like dma-iommu glue)
- series reordering to ease the review:
- first part is related to IOMMU
- second related to MSI sub-system
- third related to VFIO (except arm-smmu IOMMU_CAP_INTR_REMAP removal)
- expose the number of requested IOVA pages through VFIO_IOMMU_GET_INFO [this partially addresses Marc's comments on iommu_get/put_single_reserved size/alignment problematic - which I did not ignore - but I don't know how much I can do at the moment]
RFC v2 -> RFC v3:
- should fix wrong handling of some CONFIG combinations:
CONFIG_IOVA, CONFIG_IOMMU_API, CONFIG_PCI_MSI_IRQ_DOMAIN
- fix MSI_FLAG_IRQ_REMAPPING setting in GICv3 ITS (although not tested)
PATCH v1 -> RFC v2:
- reverted to RFC since it looks more reasonable ;-) the code is split between VFIO, IOMMU, MSI controller and I am not sure I did the right choices. Also API need to be further discussed.
- iova API usage in arm-smmu.c.
- MSI controller natively programs the MSI addr with either the PA or IOVA. This is not done anymore in vfio-pci driver as suggested by Alex.
- check irq remapping capability of the group
RFC v1 [2] -> PATCH v1:
- use the existing dma map/unmap ioctl interface with a flag to register a reserved IOVA range. Use the legacy Rb to store this special vfio_dma.
- a single reserved IOVA contiguous region now is allowed
- use of an RB tree indexed by PA to store allocated reserved slots
- use of a vfio_domain iova_domain to manage iova allocation within the window provided by the userspace
- vfio alloc_map/unmap_free take a vfio_group handle
- vfio_group handle is cached in vfio_pci_device
- add ref counting to bindings
- user modality enabled at the end of the series
Eric Auger (8):
vfio: introduce a vfio_dma type field
vfio/type1: vfio_find_dma accepting a type argument vfio/type1: implement recursive vfio_find_dma_from_node vfio/type1: handle unmap/unpin and replay for VFIO_IOVA_RESERVED slots
vfio: allow reserved msi iova registration
vfio/type1: check doorbell safety
iommu/arm-smmu: do not advertise IOMMU_CAP_INTR_REMAP vfio/type1: return MSI geometry through VFIO_IOMMU_GET_INFO capability
chains
drivers/iommu/arm-smmu-v3.c | 3 +- drivers/iommu/arm-smmu.c | 3 +- drivers/vfio/vfio_iommu_type1.c | 246 +++++++++++++++++++++++++++++++++++++--- include/uapi/linux/vfio.h | 42 ++++++- 4 files changed, 276 insertions(+), 18 deletions(-)
Signature · 1 line
1.9.1
PATCH v12 1/8 vfio: introduce a vfio_dma type field
We introduce a vfio_dma type since we will need to discriminate different types of dma slots:
- VFIO_IOVA_USER: IOVA region used to map user vaddr
- VFIO_IOVA_RESERVED_MSI: IOVA region reserved to map MSI doorbells
---
v9 -> v10:
- renamed VFIO_IOVA_RESERVED into VFIO_IOVA_RESERVED_MSI
- explicitly set type to VFIO_IOVA_USER on dma_map
v6 -> v7:
- add VFIO_IOVA_ANY
- do not introduce yet any VFIO_IOVA_RESERVED handling
--- drivers/vfio/vfio_iommu_type1.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/vfio/vfio_iommu_type1.c +8 -0
@@ -53,6 +53,12 @@ module_param_named(disable_hugepages,MODULE_PARM_DESC (disable_hugepages,"Disable VFIO IOMMU support for IOMMU hugepages." ); +enum vfio_iova_type { +VFIO_IOVA_USER =0 ,/* standard IOVA used to map user vaddr */ +VFIO_IOVA_RESERVED_MSI ,/* reserved to map MSI doorbells */ +VFIO_IOVA_ANY ,/* matches any IOVA type */ +}; +struct vfio_iommu {struct list_head domain_list;struct mutex lock;
@@ -75,6 +81,7 @@ struct vfio_dma {unsigned long vaddr;/* Process virtual addr */ size_t size;/* Map size (bytes) */ int prot;/* IOMMU_READ/WRITE */ +enum vfio_iova_type type;/* type of IOVA */ };struct vfio_group {
@@ -607,6 +614,7 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, dma->iova = iova; dma->vaddr = vaddr; dma->prot = prot; + dma->type =VFIO_IOVA_USER ;/* Insert zero-sized and grow as we map chunks of it */ vfio_link_dma (iommu, dma);
Signature · 1 line
1.9.1
PATCH v12 2/8 vfio/type1: vfio_find_dma accepting a type argument
In reply to Eric AugerIn our RB-tree we get prepared to insert slots of different types (USER and RESERVED). It becomes useful to be able to search for dma slots of a specific type or any type.
This patch introduces vfio_find_dma_from_node which starts the search from a given node and stops on the first node that matches the @start and @size parameters. If this node also matches the @type parameter, the node is returned else NULL is returned.
At the moment we only have USER SLOTS so the type will always match.
In a separate patch, this function will be enhanced to pursue the search recursively in case a node with a different type is encountered.
--- drivers/vfio/vfio_iommu_type1.c | 53 +++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 10 deletions(-)
drivers/vfio/vfio_iommu_type1.c +43 -10
@@ -94,25 +94,56 @@ struct vfio_group {* into DMA'ble space using the IOMMU */ -static struct vfio_dma *vfio_find_dma (struct vfio_iommu *iommu, -dma_addr_t start,size_t size) +/** +* vfio_find_dma_from_node: looks for a dma slot intersecting a window +* from a given rb tree node +* @top: top rb tree node where the search starts (including this node) +* @start: window start +* @size: window size +* @type: window type +*/ +static struct vfio_dma *vfio_find_dma_from_node (struct rb_node *top, +dma_addr_t start,size_t size, +enum vfio_iova_type type) { -struct rb_node *node = iommu->dma_list.rb_node; +struct rb_node *node = top; +struct vfio_dma *dma;while (node) { - struct vfio_dma *dma = rb_entry(node, struct vfio_dma, node); - + dma = rb_entry(node, struct vfio_dma, node);if (start + size <= dma->iova) node = node->rb_left;else if (start >= dma->iova + dma->size) node = node->rb_right;else -return dma; +break ; } +if (!node) +return NULL ; + +/* a dma slot intersects our window, check the type also matches */ +if (type ==VFIO_IOVA_ANY || dma->type == type) +return dma;return NULL ; } +/** +* vfio_find_dma: find a dma slot intersecting a given window +* @iommu: vfio iommu handle +* @start: window base iova +* @size: window size +* @type: window type +*/ +static struct vfio_dma *vfio_find_dma (struct vfio_iommu *iommu, +dma_addr_t start,size_t size, +enum vfio_iova_type type) +{ +struct rb_node *top_node = iommu->dma_list.rb_node; + +return vfio_find_dma_from_node (top_node, start, size, type); +} +static void vfio_link_dma (struct vfio_iommu *iommu,struct vfio_dma *new) {struct rb_node **link = &iommu->dma_list.rb_node, *parent =NULL ;
@@ -484,19 +515,21 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,* mappings within the range. */ if (iommu->v2) { - dma =vfio_find_dma (iommu, unmap->iova,0 ); + dma =vfio_find_dma (iommu, unmap->iova,0 ,VFIO_IOVA_USER );if (dma && dma->iova != unmap->iova) { ret = -EINVAL ; goto unlock; } - dma =vfio_find_dma (iommu, unmap->iova + unmap->size -1 ,0 ); + dma =vfio_find_dma (iommu, unmap->iova + unmap->size -1 ,0 , +VFIO_IOVA_USER );if (dma && dma->iova + dma->size != unmap->iova + unmap->size) { ret = -EINVAL ; goto unlock; } } -while ((dma =vfio_find_dma (iommu, unmap->iova, unmap->size))) { +while ((dma =vfio_find_dma (iommu, unmap->iova, unmap->size, +VFIO_IOVA_USER ))) {if (!iommu->v2 && unmap->iova > dma->iova)break ; unmapped += dma->size;
@@ -600,7 +633,7 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,mutex_lock (&iommu->lock); -if (vfio_find_dma (iommu, iova, size)) { +if (vfio_find_dma (iommu, iova, size,VFIO_IOVA_ANY )) {mutex_unlock (&iommu->lock);return -EEXIST ; }
Signature · 1 line
1.9.1
PATCH v12 3/8 vfio/type1: implement recursive vfio_find_dma_from_node
In reply to Eric AugerThis patch handles the case where a node is encountered, matching @start and @size arguments but not matching the @type argument. In that case, we need to skip that node and pursue the search in the node's leaves. In case @start is inferior to the node's base, we resume the search on the left leaf. If the recursive search on the left leaves did not produce any match, we search the right leaves recursively.
---
v10: creation
--- drivers/vfio/vfio_iommu_type1.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/vfio/vfio_iommu_type1.c +11 -1
@@ -125,7 +125,17 @@ static struct vfio_dma *vfio_find_dma_from_node(struct rb_node *top,if (type ==VFIO_IOVA_ANY || dma->type == type)return dma; -return NULL ; +/* restart 2 searches skipping the current node */ +if (start < dma->iova) { + dma =vfio_find_dma_from_node (node->rb_left, start, + size, type); +if (dma) +return dma; + } +if (start + size > dma->iova + dma->size) + dma =vfio_find_dma_from_node (node->rb_right, start, + size, type); +return dma; }/**
Signature · 1 line
1.9.1
PATCH v12 4/8 vfio/type1: handle unmap/unpin and replay for VFIO_IOVA_RESERVED slots
In reply to Eric AugerBefore allowing the end-user to create VFIO_IOVA_RESERVED dma slots, let's implement the expected behavior for removal and replay.
As opposed to user dma slots, reserved IOVAs are not systematically bound to PAs and PAs are not pinned. VFIO just initializes the IOVA "aperture". IOVAs are allocated outside of the VFIO framework, by the MSI layer which is responsible to free and unmap them. The MSI mapping resources are freeed by the IOMMU driver on domain destruction.
On the creation of a new domain, the "replay" of a reserved slot simply needs to set the MSI aperture on the new domain.
---
v9 -> v10:
- replay of a reserved slot sets the MSI aperture on the new domain
- use VFIO_IOVA_RESERVED_MSI enum value instead of VFIO_IOVA_RESERVED
v7 -> v8:
- do no destroy anything anymore, just bypass unmap/unpin and iommu_map on replay
--- drivers/vfio/vfio_iommu_type1.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/vfio/vfio_iommu_type1.c +10 -1
@@ -36,6 +36,7 @@#include <linux/uaccess.h> #include <linux/vfio.h> #include <linux/workqueue.h> +#include <linux/msi-iommu.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"
@@ -387,7 +388,7 @@ static void vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma)struct vfio_domain *domain, *d;long unlocked =0 ; -if (!dma->size) +if (!dma->size || dma->type !=VFIO_IOVA_USER )return ;/* * We use the IOMMU to track the physical addresses, otherwise we'd
@@ -724,6 +725,14 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu, dma = rb_entry(n, struct vfio_dma, node); iova = dma->iova; +if (dma->type ==VFIO_IOVA_RESERVED_MSI ) { + ret =iommu_msi_set_aperture (domain->domain, + dma->iova, + iova + dma->size -1 ); +WARN_ON (ret); +continue ; + } +while (iova < dma->iova + dma->size) {phys_addr_t phys =iommu_iova_to_phys (d->domain, iova);size_t size;
Signature · 1 line
1.9.1
PATCH v12 5/8 vfio: allow reserved msi iova registration
In reply to Eric AugerThe user is allowed to register a reserved MSI IOVA range by using the DMA MAP API and setting the new flag: VFIO_DMA_MAP_FLAG_MSI_RESERVED_IOVA. This region is stored in the vfio_dma rb tree. At that point the iova range is not mapped to any target address yet. The host kernel will use those iova when needed, typically when MSIs are allocated.
---
v9 -> v10
- use VFIO_IOVA_RESERVED_MSI enum value
v7 -> v8:
- use iommu_msi_set_aperture function. There is no notion of unregistration anymore since the reserved msi slot remains until the container gets closed.
v6 -> v7:
- use iommu_free_reserved_iova_domain
- convey prot attributes downto dma-reserved-iommu iova domain creation
- reserved bindings teardown now performed on iommu domain destruction
- rename VFIO_DMA_MAP_FLAG_MSI_RESERVED_IOVA into VFIO_DMA_MAP_FLAG_RESERVED_MSI_IOVA
- change title
- pass the protection attribute to dma-reserved-iommu API
v3 -> v4:
- use iommu_alloc/free_reserved_iova_domain exported by dma-reserved-iommu
- protect vfio_register_reserved_iova_range implementation with CONFIG_IOMMU_DMA_RESERVED
- handle unregistration by user-space and on vfio_iommu_type1 release
v1 -> v2:
- set returned value according to alloc_reserved_iova_domain result
- free the iova domains in case any error occurs
RFC v1 -> v1:
- takes into account Alex comments, based on
[RFC PATCH 1/6] vfio: Add interface for add/del reserved iova region:
- use the existing dma map/unmap ioctl interface with a flag to register a reserved IOVA range. A single reserved iova region is allowed.
--- drivers/vfio/vfio_iommu_type1.c | 77 ++++++++++++++++++++++++++++++++++++++++- include/uapi/linux/vfio.h | 10 +++++- 2 files changed, 85 insertions(+), 2 deletions(-)
drivers/vfio/vfio_iommu_type1.c +76 -1
@@ -442,6 +442,20 @@ static void vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma)vfio_lock_acct (-unlocked); } +static int vfio_set_msi_aperture (struct vfio_iommu *iommu, +dma_addr_t iova,size_t size) +{ +struct vfio_domain *d; +int ret =0 ; + +list_for_each_entry (d, &iommu->domain_list, next) { + ret =iommu_msi_set_aperture (d->domain, iova, iova + size -1 ); +if (ret) +break ; + } +return ret; +} +static void vfio_remove_dma (struct vfio_iommu *iommu,struct vfio_dma *dma) {vfio_unmap_unpin (iommu, dma);
@@ -691,6 +705,63 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,return ret; } +static int vfio_register_msi_range (struct vfio_iommu *iommu, +struct vfio_iommu_type1_dma_map *map) +{ +dma_addr_t iova = map->iova; +size_t size = map->size; +int ret =0 ; +struct vfio_dma *dma; +unsigned long order; +uint64_t mask; + +/* Verify that none of our __u64 fields overflow */ +if (map->size != size || map->iova != iova) +return -EINVAL ; + + order =__ffs (vfio_pgsize_bitmap (iommu)); + mask = ((uint64_t )1 << order) -1 ; + +WARN_ON (mask &PAGE_MASK ); + +if (!size || (size | iova) & mask) +return -EINVAL ; + +/* Don't allow IOVA address wrap */ +if (iova + size -1 < iova) +return -EINVAL ; + +mutex_lock (&iommu->lock); + +if (vfio_find_dma (iommu, iova, size,VFIO_IOVA_ANY )) { + ret = -EEXIST ; + goto unlock; + } + + dma =kzalloc (sizeof (*dma),GFP_KERNEL ); +if (!dma) { + ret = -ENOMEM ; + goto unlock; + } + + dma->iova = iova; + dma->size = size; + dma->type =VFIO_IOVA_RESERVED_MSI ; + + ret =vfio_set_msi_aperture (iommu, iova, size); +if (ret) + goto free_unlock; + +vfio_link_dma (iommu, dma); + goto unlock; + +free_unlock: +kfree (dma); +unlock: +mutex_unlock (&iommu->lock); +return ret; +} +static int vfio_bus_type (struct device *dev,void *data) {struct bus_type **bus = data;
@@ -1065,7 +1136,8 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, }else if (cmd ==VFIO_IOMMU_MAP_DMA ) {struct vfio_iommu_type1_dma_map map;uint32_t mask =VFIO_DMA_MAP_FLAG_READ | -VFIO_DMA_MAP_FLAG_WRITE ; +VFIO_DMA_MAP_FLAG_WRITE | +VFIO_DMA_MAP_FLAG_RESERVED_MSI_IOVA ; minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
@@ -1075,6 +1147,9 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,if (map.argsz < minsz || map.flags & ~mask)return -EINVAL ; +if (map.flags &VFIO_DMA_MAP_FLAG_RESERVED_MSI_IOVA ) +return vfio_register_msi_range (iommu, &map); +return vfio_dma_do_map (iommu, &map); }else if (cmd ==VFIO_IOMMU_UNMAP_DMA ) {
include/uapi/linux/vfio.h +9 -1
@@ -498,12 +498,19 @@ struct vfio_iommu_type1_info {* * Map process virtual addresses to IO virtual addresses using the * provided struct vfio_dma_map. Caller sets argsz. READ &/ WRITE required. +* +* In case RESERVED_MSI_IOVA flag is set, the API only aims at registering an +* IOVA region that will be used on some platforms to map the host MSI frames. +* In that specific case, vaddr is ignored. Once registered, an MSI reserved +* IOVA region stays until the container is closed. */ struct vfio_iommu_type1_dma_map {__u32 argsz;__u32 flags;#define VFIO_DMA_MAP_FLAG_READ (1 << 0)/* readable from device */ #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1)/* writable from device */ +/* reserved iova for MSI vectors*/ +#define VFIO_DMA_MAP_FLAG_RESERVED_MSI_IOVA (1 << 2)__u64 vaddr;/* Process virtual address */ __u64 iova;/* IO virtual address */ __u64 size;/* Size of mapping (bytes) */
@@ -519,7 +526,8 @@ struct vfio_iommu_type1_dma_map {* Caller sets argsz. The actual unmapped size is returned in the size * field. No guarantee is made to the user that arbitrary unmaps of iova * or size different from those used in the original mapping call will -* succeed. +* succeed. Once registered, an MSI region cannot be unmapped and stays +* until the container is closed. */ struct vfio_iommu_type1_dma_unmap {__u32 argsz;
Signature · 1 line
1.9.1
PATCH v12 6/8 vfio/type1: check doorbell safety
In reply to Eric AugerOn x86 IRQ remapping is abstracted by the IOMMU. On ARM this is abstracted by the msi controller.
Since we currently have no way to detect whether the MSI controller is upstream or downstream to the IOMMU we rely on the MSI doorbell information registered by the interrupt controllers. In case at least one doorbell does not implement proper isolation, we state the assignment is unsafe with regard to interrupts. This is a coase assessment but should allow to wait for a better system description.
At this point ARM sMMU still advertises IOMMU_CAP_INTR_REMAP. This is removed in next patch.
---
v9 -> v10:
- coarse safety assessment based on MSI doorbell info
v3 -> v4:
- rename vfio_msi_parent_irq_remapping_capable into vfio_safe_irq_domain and irq_remapping into safe_irq_domains
v2 -> v3:
- protect vfio_msi_parent_irq_remapping_capable with CONFIG_GENERIC_MSI_IRQ_DOMAIN
--- drivers/vfio/vfio_iommu_type1.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/vfio/vfio_iommu_type1.c +8 -1
@@ -37,6 +37,7 @@#include <linux/vfio.h> #include <linux/workqueue.h> #include <linux/msi-iommu.h> +#include <linux/msi-doorbell.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"
@@ -922,8 +923,14 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,INIT_LIST_HEAD (&domain->group_list);list_add (&group->next, &domain->group_list); +/* +* to advertise safe interrupts either the IOMMU or the MSI controllers +* must support IRQ remapping (aka. interrupt translation), property +* exposed by their registered doorbells +*/ if (!allow_unsafe_interrupts && - !iommu_capable (bus,IOMMU_CAP_INTR_REMAP )) { + (!iommu_capable (bus,IOMMU_CAP_INTR_REMAP ) && + !msi_doorbell_safe ())) {pr_warn ("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n" , __func__); ret = -EPERM ;
Signature · 1 line
1.9.1
PATCH v12 7/8 iommu/arm-smmu: do not advertise IOMMU_CAP_INTR_REMAP
In reply to Eric AugerDo not advertise IOMMU_CAP_INTR_REMAP for arm-smmu(-v3). Indeed the irq_remapping capability is abstracted on irqchip side for ARM as opposed to Intel IOMMU featuring IRQ remapping HW.
So to check IRQ remapping capability, the msi domain needs to be checked instead.
This commit affects platform and PCIe device assignment use cases on any platform featuring an unsafe MSI controller (currently the ARM GICv2m). For those platforms the VFIO module must be loaded with allow_unsafe_interrupts set to 1.
---
v9 -> v10:
- reword the commit message (allow_unsafe_interrupts)
--- drivers/iommu/arm-smmu-v3.c | 3 ++- drivers/iommu/arm-smmu.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-)
drivers/iommu/arm-smmu-v3.c +2 -1
@@ -1387,7 +1387,8 @@ static bool arm_smmu_capable(enum iommu_cap cap)case IOMMU_CAP_CACHE_COHERENCY :return true;case IOMMU_CAP_INTR_REMAP : -return true;/* MSIs are just memory writes */ +/* interrupt translation handled at MSI controller level */ +return false;case IOMMU_CAP_NOEXEC :return true;default :
drivers/iommu/arm-smmu.c +2 -1
@@ -1380,7 +1380,8 @@ static bool arm_smmu_capable(enum iommu_cap cap)*/ return true;case IOMMU_CAP_INTR_REMAP : -return true;/* MSIs are just memory writes */ +/* interrupt translation handled at MSI controller level */ +return false;case IOMMU_CAP_NOEXEC :return true;default :
Signature · 1 line
1.9.1
PATCH v12 8/8 vfio/type1: return MSI geometry through VFIO_IOMMU_GET_INFO capability chains
In reply to Eric AugerThis patch allows the user-space to retrieve the MSI geometry. The implementation is based on capability chains, now also added to VFIO_IOMMU_GET_INFO.
The returned info comprise:
- whether the MSI IOVA are constrained to a reserved range (x86 case) and in the positive, the start/end of the aperture,
- or whether the IOVA aperture need to be set by the userspace. In that case, the size and alignment of the IOVA window to be provided are returned.
In case the userspace must provide the IOVA aperture, we currently report a size/alignment based on all the doorbells registered by the host kernel. This may exceed the actual needs.
---
v11 -> v11:
- msi_doorbell_pages was renamed msi_doorbell_calc_pages
v9 -> v10:
- move cap_offset after iova_pgsizes
- replace __u64 alignment by __u32 order
- introduce __u32 flags in vfio_iommu_type1_info_cap_msi_geometry and fix alignment
- call msi-doorbell API to compute the size/alignment
v8 -> v9:
- use iommu_msi_supported flag instead of programmable
- replace IOMMU_INFO_REQUIRE_MSI_MAP flag by a more sophisticated capability chain, reporting the MSI geometry
v7 -> v8:
- use iommu_domain_msi_geometry
v6 -> v7:
- remove the computation of the number of IOVA pages to be provisionned. This number depends on the domain/group/device topology which can dynamically change. Let's rely instead rely on an arbitrary max depending on the system
v4 -> v5:
- move msi_info and ret declaration within the conditional code
v3 -> v4:
- replace former vfio_domains_require_msi_mapping by
more complex computation of MSI mapping requirements, especially the number of pages to be provided by the user-space.
- reword patch title
RFC v1 -> v1:
- derived from
[RFC PATCH 3/6] vfio: Extend iommu-info to return MSIs automap state
- renamed allow_msi_reconfig into require_msi_mapping
- fixed VFIO_IOMMU_GET_INFO
--- drivers/vfio/vfio_iommu_type1.c | 78 ++++++++++++++++++++++++++++++++++++++++- include/uapi/linux/vfio.h | 32 ++++++++++++++++- 2 files changed, 108 insertions(+), 2 deletions(-)
drivers/vfio/vfio_iommu_type1.c +77 -1
@@ -38,6 +38,8 @@#include <linux/workqueue.h> #include <linux/msi-iommu.h> #include <linux/msi-doorbell.h> +#include <linux/irqdomain.h> +#include <linux/msi.h> #define DRIVER_VERSION "0.2"#define DRIVER_AUTHOR "Alex Williamson <[email protected]>"
@@ -1103,6 +1105,55 @@ static int vfio_domains_have_iommu_cache(struct vfio_iommu *iommu)return ret; } +static int compute_msi_geometry_caps (struct vfio_iommu *iommu, +struct vfio_info_cap *caps) +{ +struct vfio_iommu_type1_info_cap_msi_geometry *vfio_msi_geometry; +unsigned long order =__ffs (vfio_pgsize_bitmap (iommu)); +struct iommu_domain_msi_geometry msi_geometry; +struct vfio_info_cap_header *header; +struct vfio_domain *d; +bool reserved; +size_t size; + +mutex_lock (&iommu->lock); +/* All domains have same require_msi_map property, pick first */ + d = list_first_entry(&iommu->domain_list, struct vfio_domain, next); +iommu_domain_get_attr (d->domain,DOMAIN_ATTR_MSI_GEOMETRY , + &msi_geometry); + reserved = !msi_geometry.iommu_msi_supported; + +mutex_unlock (&iommu->lock); + + size =sizeof (*vfio_msi_geometry); + header =vfio_info_cap_add (caps, size, +VFIO_IOMMU_TYPE1_INFO_CAP_MSI_GEOMETRY ,1 ); + +if (IS_ERR (header)) +return PTR_ERR (header); + + vfio_msi_geometry =container_of (header, + struct vfio_iommu_type1_info_cap_msi_geometry, + header); + + vfio_msi_geometry->flags = reserved; +if (reserved) { + vfio_msi_geometry->aperture_start = msi_geometry.aperture_start; + vfio_msi_geometry->aperture_end = msi_geometry.aperture_end; +return 0 ; + } + + vfio_msi_geometry->order = order; +/* +* we compute a system-wide requirement based on all the registered +* doorbells +*/ + vfio_msi_geometry->size = +msi_doorbell_calc_pages (order) * ((uint64_t )1 << order); + +return 0 ; +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg) {
@@ -1124,8 +1175,10 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, } }else if (cmd ==VFIO_IOMMU_GET_INFO ) {struct vfio_iommu_type1_info info; +struct vfio_info_cap caps = { .buf =NULL , .size =0 }; +int ret; - minsz = offsetofend(struct vfio_iommu_type1_info, iova_pgsizes); + minsz = offsetofend(struct vfio_iommu_type1_info, cap_offset);if (copy_from_user (&info, (void __user *)arg, minsz))return -EFAULT ;
@@ -1137,6 +1190,29 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, info.iova_pgsizes =vfio_pgsize_bitmap (iommu); + ret =compute_msi_geometry_caps (iommu, &caps); +if (ret) +return ret; + +if (caps.size) { + info.flags |=VFIO_IOMMU_INFO_CAPS ; +if (info.argsz <sizeof (info) + caps.size) { + info.argsz =sizeof (info) + caps.size; + info.cap_offset =0 ; + }else { +vfio_info_cap_shift (&caps,sizeof (info)); +if (copy_to_user ((void __user *)arg + +sizeof (info), caps.buf, + caps.size)) { +kfree (caps.buf); +return -EFAULT ; + } + info.cap_offset =sizeof (info); + } + +kfree (caps.buf); + } +return copy_to_user ((void __user *)arg, &info, minsz) ? -EFAULT :0 ;
include/uapi/linux/vfio.h +31 -1
@@ -488,7 +488,35 @@ struct vfio_iommu_type1_info {__u32 argsz;__u32 flags;#define VFIO_IOMMU_INFO_PGSIZES (1 << 0)/* supported page sizes info */ -__u64 iova_pgsizes;/* Bitmap of supported page sizes */ +#define VFIO_IOMMU_INFO_CAPS (1 << 1)/* Info supports caps */ +__u64 iova_pgsizes;/* Bitmap of supported page sizes */ +__u32 __resv; +__u32 cap_offset;/* Offset within info struct of first cap */ +}; + +#define VFIO_IOMMU_TYPE1_INFO_CAP_MSI_GEOMETRY 1 + +/* +* The MSI geometry capability allows to report the MSI IOVA geometry: +* - either the MSI IOVAs are constrained within a reserved IOVA aperture +* whose boundaries are given by [@aperture_start, @aperture_end]. +* this is typically the case on x86 host. The userspace is not allowed +* to map userspace memory at IOVAs intersecting this range using +* VFIO_IOMMU_MAP_DMA. +* - or the MSI IOVAs are not requested to belong to any reserved range; +* in that case the userspace must provide an IOVA window characterized by +* @size and @alignment using VFIO_IOMMU_MAP_DMA with RESERVED_MSI_IOVA flag. +*/ +struct vfio_iommu_type1_info_cap_msi_geometry { +struct vfio_info_cap_header header; +__u32 flags; +#define VFIO_IOMMU_MSI_GEOMETRY_RESERVED (1 << 0)/* reserved geometry */ +/* not reserved */ +__u32 order;/* iommu page order used for aperture alignment*/ +__u64 size;/* IOVA aperture size (bytes) the userspace must provide */ +/* reserved */ +__u64 aperture_start; +__u64 aperture_end; };#define VFIO_IOMMU_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 12)
@@ -503,6 +531,8 @@ struct vfio_iommu_type1_info {* IOVA region that will be used on some platforms to map the host MSI frames. * In that specific case, vaddr is ignored. Once registered, an MSI reserved * IOVA region stays until the container is closed. +* The requirement for provisioning such reserved IOVA range can be checked by +* checking the VFIO_IOMMU_TYPE1_INFO_CAP_MSI_GEOMETRY capability. */ struct vfio_iommu_type1_dma_map {__u32 argsz;
Signature · 1 line
1.9.1
kbuild test robot
build botRe: [PATCH v12 6/8] vfio/type1: check doorbell safety
In reply to Eric Augerreproduce:
Hi Eric,
[auto build test ERROR on vfio/next]
[also build test ERROR on v4.7]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Eric-Auger/KVM-PCIe-MSI-passthrough-on-ARM-ARM64-kernel-part-3-3-vfio-changes/20160803-122924 base: https://github.com/awilliam/linux-vfio.git next config: x86_64-randconfig-s1-08031247 (attached as .config) compiler: gcc-4.4 (Debian 4.4.7-8) 4.4.7 reproduce: # save the attached .config to linux build tree make ARCH=x86_64
All errors (new ones prefixed by >>):
drivers/vfio/vfio_iommu_type1.c:39:29: error: linux/msi-iommu.h: No such file or directory
>> drivers/vfio/vfio_iommu_type1.c:40:32: error: linux/msi-doorbell.h: No such file or directory drivers/vfio/vfio_iommu_type1.c: In function 'vfio_set_msi_aperture': drivers/vfio/vfio_iommu_type1.c:453: error: implicit declaration of function 'iommu_msi_set_aperture' drivers/vfio/vfio_iommu_type1.c: In function 'vfio_iommu_type1_attach_group': >> drivers/vfio/vfio_iommu_type1.c:933: error: implicit declaration of function 'msi_doorbell_safe'
At top level:
cc1: warning: unrecognized command line option "-Wno-maybe-uninitialized"
vim +40 drivers/vfio/vfio_iommu_type1.c
33 #include <linux/rbtree.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/uaccess.h>
37 #include <linux/vfio.h>
38 #include <linux/workqueue.h>
> 39 #include <linux/msi-iommu.h>
> 40 #include <linux/msi-doorbell.h>
41
42 #define DRIVER_VERSION "0.2"
43 #define DRIVER_AUTHOR "Alex Williamson <[email protected]>"---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
kbuild test robot
build botRe: [PATCH v12 8/8] vfio/type1: return MSI geometry through VFIO_IOMMU_GET_INFO capability chains
In reply to Eric Augerreproduce:
Hi Eric,
[auto build test ERROR on vfio/next]
[also build test ERROR on v4.7]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Eric-Auger/KVM-PCIe-MSI-passthrough-on-ARM-ARM64-kernel-part-3-3-vfio-changes/20160803-122924 base: https://github.com/awilliam/linux-vfio.git next config: x86_64-randconfig-s1-08031247 (attached as .config) compiler: gcc-4.4 (Debian 4.4.7-8) 4.4.7 reproduce: # save the attached .config to linux build tree make ARCH=x86_64
All errors (new ones prefixed by >>):
drivers/vfio/vfio_iommu_type1.c:39:29: error: linux/msi-iommu.h: No such file or directory drivers/vfio/vfio_iommu_type1.c:40:32: error: linux/msi-doorbell.h: No such file or directory drivers/vfio/vfio_iommu_type1.c: In function 'vfio_set_msi_aperture':
drivers/vfio/vfio_iommu_type1.c:455: error: implicit declaration of function 'iommu_msi_set_aperture' drivers/vfio/vfio_iommu_type1.c: In function 'vfio_iommu_type1_attach_group':
drivers/vfio/vfio_iommu_type1.c:935: error: implicit declaration of function 'msi_doorbell_safe' drivers/vfio/vfio_iommu_type1.c: In function 'compute_msi_geometry_caps':
drivers/vfio/vfio_iommu_type1.c:1113: error: storage size of 'msi_geometry' isn't known drivers/vfio/vfio_iommu_type1.c:1122: error: 'DOMAIN_ATTR_MSI_GEOMETRY' undeclared (first use in this function) drivers/vfio/vfio_iommu_type1.c:1122: error: (Each undeclared identifier is reported only once drivers/vfio/vfio_iommu_type1.c:1122: error: for each function it appears in.)
>> drivers/vfio/vfio_iommu_type1.c:1152: error: implicit declaration of function 'msi_doorbell_calc_pages'
drivers/vfio/vfio_iommu_type1.c:1113: warning: unused variable 'msi_geometry' At top level:
cc1: warning: unrecognized command line option "-Wno-maybe-uninitialized"
vim +/msi_doorbell_calc_pages +1152 drivers/vfio/vfio_iommu_type1.c
1146 vfio_msi_geometry->order = order;
1147 /*
1148 * we compute a system-wide requirement based on all the registered
1149 * doorbells
1150 */
1151 vfio_msi_geometry->size =
> 1152 msi_doorbell_calc_pages(order) * ((uint64_t) 1 << order);
1153
1154 return 0;
1155 }
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Hi Eric,
I have tested these patches in a VFIO PCI scenario (using the ITS emulation) on a NXP LS2080 board. It worked fine with one e1000 card assigned to the guest. However, when I tried to assign two cards to the guest I got a crash. I narrowed down the problem to this code:
drivers/vfio/vfio_iommu_type1.c, vfio_iommu_type1_attach_group function
/*
* Try to match an existing compatible domain. We don't want to
* preclude an IOMMU driver supporting multiple bus_types and being
* able to include different bus_types in the same IOMMU domain, so
* we test whether the domains use the same iommu_ops rather than
* testing if they're on the same bus_type.
*/
list_for_each_entry(d, &iommu->domain_list, next) {
if (d->domain->ops == domain->domain->ops &&
d->prot == domain->prot) {
iommu_detach_group(domain->domain, iommu_group);
if (!iommu_attach_group(d->domain, iommu_group)) {
list_add(&group->next, &d->group_list);
iommu_domain_free(domain->domain);
kfree(domain);
mutex_unlock(&iommu->lock);
return 0;
} ret = iommu_attach_group(domain->domain, iommu_group);
if (ret)
goto out_domain;
}
}The iommu_domain_free function eventually calls iommu_put_dma_cookie which calls put_iova_domain. This function (put_iova_domain) tries to acquire some spinlocks which were not yet initialized. They will be initialized later when the first DMA mapping will be performed. Because the spinlock was not yet initialized, it crashes when attempting to acquire it.
With the following fix I was able to successfully assign two cards to the guest:
drivers/iommu/iova.c +3 -0
@@ -457,6 +457,9 @@ void put_iova_domain(struct iova_domain *iovad)struct rb_node *node;unsigned long flags; +if (!iovad->start_pfn) +return ; +free_iova_rcaches (iovad);spin_lock_irqsave (&iovad->iova_rbtree_lock, flags); node =rb_first (&iovad->rbroot);
However, I am not sure if this is the right fix.
Thanks,
Diana
Quoted 150 lines · Eric Auger
This series allows the user-space to register a reserved IOVA domain. This completes the kernel integration of the whole functionality on top of the 2 previous parts (v11).
We reuse the VFIO DMA MAP ioctl with a new flag to bridge to the msi-iommu API. The need for provisioning such MSI IOVA range is reported through capability chain, using
VFIO_IOMMU_TYPE1_INFO_CAP_MSI_GEOMETRY.
vfio_iommu_type1checks if the MSI mapping is safe when attaching the vfio group to the container (allow_unsafe_interruptsmodality). This is done in a very coarse way, looking at all the registered doorbells and returning assignment is safe wrt IRQ if all the doorbells are safe.More details & context can be found at:
http://www.linaro.org/blog/core-dump/kvm-pciemsi-passthrough-armarm64/Best Regards
Eric
Testing:
- functional on ARM64 AMD Overdrive HW (single GICv2m frame) with Intel I350T2 (SR-IOV capable, igb/igbvf) and Intel 82574L (e1000e)
- functional on Cavium ThunderX (ARM GICv3 ITS) with Intel 82574L (e1000e)References:
[1] [RFC 0/2] VFIO: Add virtual MSI doorbell support(https://lkml.org/lkml/2015/7/24/135) [2] [RFC PATCH 0/6] vfio: Add interface to map MSI pages (https://lists.cs.columbia.edu/pipermail/kvmarm/2015-September/016607.html) [3] [PATCH v2 0/3] Introduce MSI hardware mapping for VFIO (http://permalink.gmane.org/gmane.comp.emulators.kvm.arm.devel/3858)Git: complete series available at
https://github.com/eauger/linux/tree/v4.7-rc7-passthrough-v12
previous: https://github.com/eauger/linux/tree/v4.7-rc7-passthrough-v11the above branch includes a temporary patch to work around a ThunderX pci bus reset crash (which I think unrelated to this series):
"vfio: pci: HACK! workaround thunderxpci_try_reset_buscrash" Do not take this one for other platforms.History:
v11 -> v12:
- no functional change. Only adapt to renamings done in PART IIv10 -> v11:
no change to this series, just incremented for consistencyv9 -> v10:
Took into account Alex' comments:
- split "vfio/type1:vfio_find_dmaaccepting a type argument" into 2 patches
- properly implement replay of MSI DMA slots (by setting the aperture on the new domain); fixes the assignment of several devices
- rework user api forvfio_iommu_type1_infocapability chains (cap_offsetat the end of the struct and fix padding issues)
-VFIO_IOVA_RESERVEDrenamed intoVFIO_IOVA_MSI_RESERVED
- explicitdma->typesetting toVFIO_IOVA_USERv8 -> v9:
- report MSI geometry through capability chain (last patch only); with the current limitation that an arbitrary number of 16 page requirement is reported. To be improved later on.v7 -> v8:
- use renamed msi-iommu API
- VFIO only responsible for setting the IOVA aperture
- use newDOMAIN_ATTR_MSI_GEOMETRYiommu domain attributev6 -> v7:
-vfio_find_dmanow accepts adma_typeargument.
- should have recovered the capability to unmap the whole user IOVA range
- remove computation of nb IOVA pages -> will post a separate RFC for that while respinning the QEMU partRFC v5 -> patch v6:
- split to ease the review processRFC v4 -> RFC v5:
- take into account Thomas' comments on MSI related patches
- split "msi: IOMMU map the doorbell address when needed"
- increase readability and add comments
- fix style issues
- split "iommu: AddDOMAIN_ATTR_MSI_MAPPINGattribute"
- platform ITS now advertisesIOMMU_CAP_INTR_REMAP
- fix compilation issue withCONFIG_IOMMUAPI unset
- arm-smmu-v3 now advertisesDOMAIN_ATTR_MSI_MAPPINGRFC v3 -> v4:
- Move doorbell mapping/unmapping inmsi.c
- fix ref count issue onset_affinity: in case of a change in the address the previous address is decremented
- doorbell map/unmap now is done on msi composition. Should allow the use case for platform MSI controllers
- create dma-reserved-iommu.h/c exposing/implementing a new API dedicated to reserved IOVA management (looking like dma-iommu glue)
- series reordering to ease the review:
- first part is related to IOMMU
- second related to MSI sub-system
- third related to VFIO (except arm-smmuIOMMU_CAP_INTR_REMAPremoval)
- expose the number of requested IOVA pages throughVFIO_IOMMU_GET_INFO[this partially addresses Marc's comments on iommu_get/put_single_reserved size/alignment problematic - which I did not ignore - but I don't know how much I can do at the moment]RFC v2 -> RFC v3:
- should fix wrong handling of some CONFIG combinations:
CONFIG_IOVA,CONFIG_IOMMU_API,CONFIG_PCI_MSI_IRQ_DOMAIN
- fixMSI_FLAG_IRQ_REMAPPINGsetting in GICv3 ITS (although not tested)PATCH v1 -> RFC v2:
- reverted to RFC since it looks more reasonable ;-) the code is split between VFIO, IOMMU, MSI controller and I am not sure I did the right choices. Also API need to be further discussed.
- iova API usage inarm-smmu.c.
- MSI controller natively programs the MSI addr with either the PA or IOVA. This is not done anymore in vfio-pci driver as suggested by Alex.
- check irq remapping capability of the groupRFC v1 [2] -> PATCH v1:
- use the existing dma map/unmap ioctl interface with a flag to register a reserved IOVA range. Use the legacy Rb to store this specialvfio_dma.
- a single reserved IOVA contiguous region now is allowed
- use of an RB tree indexed by PA to store allocated reserved slots
- use of avfio_domainiova_domainto manage iova allocation within the window provided by the userspace
- vfio alloc_map/unmap_free take avfio_grouphandle
-vfio_grouphandle is cached invfio_pci_device
- add ref counting to bindings
- user modality enabled at the end of the seriesEric Auger (8):
vfio: introduce avfio_dmatype field
vfio/type1:vfio_find_dmaaccepting a type argument vfio/type1: implement recursivevfio_find_dma_from_nodevfio/type1: handle unmap/unpin and replay forVFIO_IOVA_RESERVEDslots
vfio: allow reserved msi iova registration
vfio/type1: check doorbell safety
iommu/arm-smmu: do not advertiseIOMMU_CAP_INTR_REMAPvfio/type1: return MSI geometry throughVFIO_IOMMU_GET_INFOcapability
chainsdrivers/iommu/arm-smmu-v3.c | 3 +- drivers/iommu/arm-smmu.c | 3 +- drivers/vfio/vfio_iommu_type1.c | 246 +++++++++++++++++++++++++++++++++++++--- include/uapi/linux/vfio.h | 42 ++++++- 4 files changed, 276 insertions(+), 18 deletions(-)
Hi Diana,
Quoted 59 lines · Diana Madalina Craciun
Hi Eric,
I have tested these patches in a VFIO PCI scenario (using the ITS emulation) on a NXP LS2080 board. It worked fine with one e1000 card assigned to the guest. However, when I tried to assign two cards to the guest I got a crash. I narrowed down the problem to this code:
drivers/vfio/vfio_iommu_type1.c,vfio_iommu_type1_attach_groupfunction/* * Try to match an existing compatible domain. We don't want to * preclude an IOMMU driver supporting multiple bus_types and being * able to include different bus_types in the same IOMMU domain, so * we test whether the domains use the same iommu_ops rather than * testing if they're on the same bus_type. */ list_for_each_entry(d, &iommu->domain_list, next) { if (d->domain->ops == domain->domain->ops && d->prot == domain->prot) { iommu_detach_group(domain->domain, iommu_group); if (!iommu_attach_group(d->domain, iommu_group)) { list_add(&group->next, &d->group_list); iommu_domain_free(domain->domain); kfree(domain); mutex_unlock(&iommu->lock); return 0; }ret = iommu_attach_group(domain->domain, iommu_group); if (ret) goto out_domain; } }The
iommu_domain_freefunction eventually callsiommu_put_dma_cookiewhich callsput_iova_domain. This function (put_iova_domain) tries to acquire some spinlocks which were not yet initialized. They will be initialized later when the first DMA mapping will be performed. Because the spinlock was not yet initialized, it crashes when attempting to acquire it.With the following fix I was able to successfully assign two cards to the guest:
drivers/iommu/iova.c +3
-0@@ -457,6 +457,9 @@ void put_iova_domain(struct iova_domain *iovad)struct rb_node *node;unsigned long flags; +if (!iovad->start_pfn) +return ; +free_iova_rcaches (iovad);spin_lock_irqsave (&iovad->iova_rbtree_lock, flags); node =rb_first (&iovad->rbroot);
Quoted 1 line
However, I am not sure if this is the right fix.
Thanks for testing. Sorry for the time you spent narrowing the above issue. The problem is known and fixed by Robin's patch sent on iommu list. The patch can be found on my branch - similar fix as the one you did ;-) -:
iommu/dma: Don't put uninitialised IOVA domains
Thanks!
Eric
Quoted 158 lines
Thanks,
Diana
Quoted 150 lines · Eric Auger
This series allows the user-space to register a reserved IOVA domain. This completes the kernel integration of the whole functionality on top of the 2 previous parts (v11).
We reuse the VFIO DMA MAP ioctl with a new flag to bridge to the msi-iommu API. The need for provisioning such MSI IOVA range is reported through capability chain, using
VFIO_IOMMU_TYPE1_INFO_CAP_MSI_GEOMETRY.
vfio_iommu_type1checks if the MSI mapping is safe when attaching the vfio group to the container (allow_unsafe_interruptsmodality). This is done in a very coarse way, looking at all the registered doorbells and returning assignment is safe wrt IRQ if all the doorbells are safe.More details & context can be found at:
http://www.linaro.org/blog/core-dump/kvm-pciemsi-passthrough-armarm64/Best Regards
Eric
Testing:
- functional on ARM64 AMD Overdrive HW (single GICv2m frame) with Intel I350T2 (SR-IOV capable, igb/igbvf) and Intel 82574L (e1000e)
- functional on Cavium ThunderX (ARM GICv3 ITS) with Intel 82574L (e1000e)References:
[1] [RFC 0/2] VFIO: Add virtual MSI doorbell support(https://lkml.org/lkml/2015/7/24/135) [2] [RFC PATCH 0/6] vfio: Add interface to map MSI pages (https://lists.cs.columbia.edu/pipermail/kvmarm/2015-September/016607.html) [3] [PATCH v2 0/3] Introduce MSI hardware mapping for VFIO (http://permalink.gmane.org/gmane.comp.emulators.kvm.arm.devel/3858)Git: complete series available at
https://github.com/eauger/linux/tree/v4.7-rc7-passthrough-v12
previous: https://github.com/eauger/linux/tree/v4.7-rc7-passthrough-v11the above branch includes a temporary patch to work around a ThunderX pci bus reset crash (which I think unrelated to this series):
"vfio: pci: HACK! workaround thunderxpci_try_reset_buscrash" Do not take this one for other platforms.History:
v11 -> v12:
- no functional change. Only adapt to renamings done in PART IIv10 -> v11:
no change to this series, just incremented for consistencyv9 -> v10:
Took into account Alex' comments:
- split "vfio/type1:vfio_find_dmaaccepting a type argument" into 2 patches
- properly implement replay of MSI DMA slots (by setting the aperture on the new domain); fixes the assignment of several devices
- rework user api forvfio_iommu_type1_infocapability chains (cap_offsetat the end of the struct and fix padding issues)
-VFIO_IOVA_RESERVEDrenamed intoVFIO_IOVA_MSI_RESERVED
- explicitdma->typesetting toVFIO_IOVA_USERv8 -> v9:
- report MSI geometry through capability chain (last patch only); with the current limitation that an arbitrary number of 16 page requirement is reported. To be improved later on.v7 -> v8:
- use renamed msi-iommu API
- VFIO only responsible for setting the IOVA aperture
- use newDOMAIN_ATTR_MSI_GEOMETRYiommu domain attributev6 -> v7:
-vfio_find_dmanow accepts adma_typeargument.
- should have recovered the capability to unmap the whole user IOVA range
- remove computation of nb IOVA pages -> will post a separate RFC for that while respinning the QEMU partRFC v5 -> patch v6:
- split to ease the review processRFC v4 -> RFC v5:
- take into account Thomas' comments on MSI related patches
- split "msi: IOMMU map the doorbell address when needed"
- increase readability and add comments
- fix style issues
- split "iommu: AddDOMAIN_ATTR_MSI_MAPPINGattribute"
- platform ITS now advertisesIOMMU_CAP_INTR_REMAP
- fix compilation issue withCONFIG_IOMMUAPI unset
- arm-smmu-v3 now advertisesDOMAIN_ATTR_MSI_MAPPINGRFC v3 -> v4:
- Move doorbell mapping/unmapping inmsi.c
- fix ref count issue onset_affinity: in case of a change in the address the previous address is decremented
- doorbell map/unmap now is done on msi composition. Should allow the use case for platform MSI controllers
- create dma-reserved-iommu.h/c exposing/implementing a new API dedicated to reserved IOVA management (looking like dma-iommu glue)
- series reordering to ease the review:
- first part is related to IOMMU
- second related to MSI sub-system
- third related to VFIO (except arm-smmuIOMMU_CAP_INTR_REMAPremoval)
- expose the number of requested IOVA pages throughVFIO_IOMMU_GET_INFO[this partially addresses Marc's comments on iommu_get/put_single_reserved size/alignment problematic - which I did not ignore - but I don't know how much I can do at the moment]RFC v2 -> RFC v3:
- should fix wrong handling of some CONFIG combinations:
CONFIG_IOVA,CONFIG_IOMMU_API,CONFIG_PCI_MSI_IRQ_DOMAIN
- fixMSI_FLAG_IRQ_REMAPPINGsetting in GICv3 ITS (although not tested)PATCH v1 -> RFC v2:
- reverted to RFC since it looks more reasonable ;-) the code is split between VFIO, IOMMU, MSI controller and I am not sure I did the right choices. Also API need to be further discussed.
- iova API usage inarm-smmu.c.
- MSI controller natively programs the MSI addr with either the PA or IOVA. This is not done anymore in vfio-pci driver as suggested by Alex.
- check irq remapping capability of the groupRFC v1 [2] -> PATCH v1:
- use the existing dma map/unmap ioctl interface with a flag to register a reserved IOVA range. Use the legacy Rb to store this specialvfio_dma.
- a single reserved IOVA contiguous region now is allowed
- use of an RB tree indexed by PA to store allocated reserved slots
- use of avfio_domainiova_domainto manage iova allocation within the window provided by the userspace
- vfio alloc_map/unmap_free take avfio_grouphandle
-vfio_grouphandle is cached invfio_pci_device
- add ref counting to bindings
- user modality enabled at the end of the seriesEric Auger (8):
vfio: introduce avfio_dmatype field
vfio/type1:vfio_find_dmaaccepting a type argument vfio/type1: implement recursivevfio_find_dma_from_nodevfio/type1: handle unmap/unpin and replay forVFIO_IOVA_RESERVEDslots
vfio: allow reserved msi iova registration
vfio/type1: check doorbell safety
iommu/arm-smmu: do not advertiseIOMMU_CAP_INTR_REMAPvfio/type1: return MSI geometry throughVFIO_IOMMU_GET_INFOcapability
chainsdrivers/iommu/arm-smmu-v3.c | 3 +- drivers/iommu/arm-smmu.c | 3 +- drivers/vfio/vfio_iommu_type1.c | 246 +++++++++++++++++++++++++++++++++++++--- include/uapi/linux/vfio.h | 42 ++++++- 4 files changed, 276 insertions(+), 18 deletions(-)