vfio: VFIO Driver core framework
Subsystems (5)
RFC PATCH 1/1 vfio: VFIO Driver core framework
VFIO provides a secure, IOMMU based interface for user space drivers, including device assignment to virtual machines. This provides the base management of IOMMU groups, devices, and IOMMU objects. See Documentation/vfio.txt included in this patch for user and kernel API description.
Note, this implements the new API discussed at KVM Forum 2011, as represented by the drvier version 0.2. It's hoped that this provides a modular enough interface to support PCI and non-PCI userspace drivers across various architectures and IOMMU implementations.
---
Fingers crossed, this is the last RFC for VFIO, but we need the iommu group support before this can go upstream
(http://lkml.indiana.edu/hypermail/linux/kernel/1110.2/02303.html),
hoping this helps push that along.
Since the last posting, this version completely modularizes the device backends and better defines the APIs between the core VFIO code and the device backends. I expect that we might also adopt a modular IOMMU interface as iommu_ops learns about different types of hardware. Also many, many cleanups. Check the complete git history for details:
git://github.com/awilliam/linux-vfio.git vfio-ng
(matching qemu tree: git://github.com/awilliam/qemu-vfio.git)
This version, along with the supporting VFIO PCI backend can be found here:
git://github.com/awilliam/linux-vfio.git vfio-next-20111103
I've held off on implementing a kernel->user signaling mechanism for now since the previous netlink version produced too many gag reflexes. It's easy enough to set a bit in the group flags too indicate such support in the future, so I think we can move ahead without it.
Appreciate any feedback or suggestions. Thanks,
Alex
Documentation/ioctl/ioctl-number.txt | 1 Documentation/vfio.txt | 304 +++++++++ MAINTAINERS | 8 drivers/Kconfig | 2 drivers/Makefile | 1 drivers/vfio/Kconfig | 8 drivers/vfio/Makefile | 3 drivers/vfio/vfio_iommu.c | 530 ++++++++++++++++ drivers/vfio/vfio_main.c | 1151 ++++++++++++++++++++++++++++++++++ drivers/vfio/vfio_private.h | 34 + include/linux/vfio.h | 155 +++++ 11 files changed, 2197 insertions(+), 0 deletions(-) create mode 100644 Documentation/vfio.txt create mode 100644 drivers/vfio/Kconfig create mode 100644 drivers/vfio/Makefile create mode 100644 drivers/vfio/vfio_iommu.c create mode 100644 drivers/vfio/vfio_main.c create mode 100644 drivers/vfio/vfio_private.h create mode 100644 include/linux/vfio.h
Documentation/ioctl/ioctl-number.txt +1 -0
@@ -88,6 +88,7 @@ Code Seq#(hex) Include File Comments and kernel/power/user.c '8' all SNP8023 advanced NIC card <mailto:[email protected]> +';' 64-76 linux/vfio.h '@' 00-0F linux/radeonfb.h conflict! '@' 00-0F drivers/video/aty/aty128fb.c conflict! 'A' 00-1F linux/apm_bios.h conflict!
Documentation/vfio.txt new file +304 -0
304 lines not on this page · raw message
MAINTAINERS +8 -0
@@ -7106,6 +7106,14 @@ S: Maintained F: Documentation/filesystems/vfat.txt F: fs/fat/ +VFIO DRIVER +M: Alex Williamson <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/vfio.txt +F: drivers/vfio/ +F: include/linux/vfio.h + VIDEOBUF2 FRAMEWORK M: Pawel Osciak <[email protected]> M: Marek Szyprowski <[email protected]>
drivers/Kconfig +2 -0
@@ -112,6 +112,8 @@ source "drivers/auxdisplay/Kconfig" source "drivers/uio/Kconfig" +source "drivers/vfio/Kconfig" + source "drivers/vlynq/Kconfig" source "drivers/virtio/Kconfig"
drivers/Makefile +1 -0
@@ -58,6 +58,7 @@ obj-$(CONFIG_ATM) += atm/ obj-$(CONFIG_FUSION) += message/ obj-y += firewire/ obj-$(CONFIG_UIO) += uio/ +obj-$(CONFIG_VFIO) += vfio/ obj-y += cdrom/ obj-y += auxdisplay/ obj-$(CONFIG_PCCARD) += pcmcia/
drivers/vfio/Kconfig new file +8 -0
@@ -0,0 +1,8 @@ +menuconfig VFIO + tristate "VFIO Non-Privileged userspace driver framework" + depends on IOMMU_API + help + VFIO provides a framework for secure userspace device drivers. + See Documentation/vfio.txt for more details. + + If you don't know what to do here, say N.
drivers/vfio/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +vfio-y := vfio_main.o vfio_iommu.o + +obj-$(CONFIG_VFIO) := vfio.o
drivers/vfio/vfio_iommu.c new file +530 -0
530 lines not on this page · raw message
drivers/vfio/vfio_main.c new file +1151 -0
1,151 lines not on this page · raw message
drivers/vfio/vfio_private.h new file +34 -0
@@ -0,0 +1,34 @@ +/* +* Copyright (C) 2011 Red Hat, Inc. All rights reserved. +* Author: Alex Williamson <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* Derived from original vfio: +* Copyright 2010 Cisco Systems, Inc. All rights reserved. +* Author: Tom Lyon, [email protected] +*/ + +#include <linux/list.h> +#include <linux/mutex.h> + +#ifndef VFIO_PRIVATE_H +#define VFIO_PRIVATE_H + +struct vfio_iommu { +struct iommu_domain *domain; +struct bus_type *bus; +struct mutex dgate; +struct list_head dm_list; +struct mm_struct *mm; +struct list_head group_list; +int refcnt; +bool cache; +}; + +extern int vfio_release_iommu (struct vfio_iommu *iommu); +extern void vfio_iommu_unmapall (struct vfio_iommu *iommu); + +#endif /* VFIO_PRIVATE_H */
include/linux/vfio.h new file +155 -0
155 lines not on this page · raw message
I'm going to send out chunks of comments as I go over this stuff. Below I've covered the documentation file and vfio_iommu.c. More comments coming soon...
Quoted 14 lines · Alex Williamson
VFIO provides a secure, IOMMU based interface for user space drivers, including device assignment to virtual machines. This provides the base management of IOMMU groups, devices, and IOMMU objects. See
Documentation/vfio.txtincluded in this patch for user and kernel API description.Note, this implements the new API discussed at KVM Forum 2011, as represented by the drvier version 0.2. It's hoped that this provides a modular enough interface to support PCI and non-PCI userspace drivers across various architectures and IOMMU implementations.
Signed-off-by Alex Williamson <[email protected]>---
<snip>
Documentation/vfio.txt line 38
Quoted 9 lines
Documentation/vfio.txt new file +8
-1+ +Groups, Devices, IOMMUs, oh my +----------------------------------------------------------------------------- -- + +A fundamental component of VFIO is the notion of IOMMU groups. IOMMUs +can't always distinguish transactions from each individual device in +the system. Sometimes this is because of the IOMMU design, such as with +PEs, other times it's caused by the I/O topology, for instance a
Can you define this acronym the first time you use it, i.e.
+ PEs (partitionable endpoints), ...
Documentation/vfio.txt line 40
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+PCIe-to-PCI bridge masking all devices behind it. We call the sets of +devices created by these restictions IOMMU groups (or just "groups" for
restrictions
Documentation/vfio.txt line 43
Quoted 3 lines
Documentation/vfio.txt new file +3
-0+this document). + +The IOMMU cannot distiguish transactions between the individual devices
distinguish
Quoted 4 lines
Documentation/vfio.txt new file +4
-0+within the group, therefore the group is the basic unit of ownership for +a userspace process. Because of this, groups are also the primary +interface to both devices and IOMMU domains in VFIO. +
<snip>
Documentation/vfio.txt line 101
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+file descriptor referencing the same internal IOMMU object from either +X or Y). Merged groups can be dissolved either explictly with UNMERGE
explicitly
<snip>
Review
Quoted 2 lines
(quoted diff) +2
-0+ +Device tree devices also invlude ioctls for further defining the
include
<snip>
Quoted 5 lines
drivers/vfio/vfio_iommu.c new file +0
-0
<snip>
Quoted 5 lines
drivers/vfio/vfio_iommu.c new file +5
-0+static struct dma_map_page *vfio_find_dma (struct vfio_iommu *iommu, +dma_addr_t start,size_t size) +{ +struct list_head *pos; +struct dma_map_page *mlp;
drivers/vfio/vfio_iommu.c line 276
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+ +list_for_each (pos, &iommu->dm_list) { + mlp = list_entry(pos, struct dma_map_page, list); +if (ranges_overlap (mlp->daddr,NPAGE_TO_SIZE (mlp->npage), + start, size)) +return mlp; + } +return NULL ; +} +
This function below should be static.
Quoted 13 lines
drivers/vfio/vfio_iommu.c new file +13
-0+int vfio_remove_dma_overlap (struct vfio_iommu *iommu,dma_addr_t start, +size_t size,struct dma_map_page *mlp) +{ +struct dma_map_page *split; +int npage_lo, npage_hi; + +/* Existing dma region is completely covered, unmap all */ +if (start <= mlp->daddr && + start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +vfio_dma_unmap (iommu, mlp->daddr, mlp->npage, mlp->rdwr); +list_del (&mlp->list); + npage_lo = mlp->npage; +kfree (mlp);
drivers/vfio/vfio_iommu.c line 299
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+return npage_lo; + } + +/* Overlap low address of existing range */ +if (start <= mlp->daddr) { +size_t overlap; + + overlap = start + size - mlp->daddr; + npage_lo = overlap >>PAGE_SHIFT ; + npage_hi = mlp->npage - npage_lo;
npage_hi not used.. Delete this line ^
Quoted 31 lines
drivers/vfio/vfio_iommu.c new file +31
-0+ +vfio_dma_unmap (iommu, mlp->daddr, npage_lo, mlp->rdwr); + mlp->daddr += overlap; + mlp->vaddr += overlap; + mlp->npage -= npage_lo; +return npage_lo; + } + +/* Overlap high address of existing range */ +if (start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +size_t overlap; + + overlap = mlp->daddr +NPAGE_TO_SIZE (mlp->npage) - start; + npage_hi = overlap >>PAGE_SHIFT ; + npage_lo = mlp->npage - npage_hi; + +vfio_dma_unmap (iommu, start, npage_hi, mlp->rdwr); + mlp->npage -= npage_hi; +return npage_hi; + } + +/* Split existing */ + npage_lo = (start - mlp->daddr) >>PAGE_SHIFT ; + npage_hi = mlp->npage - (size >>PAGE_SHIFT ) - npage_lo; + + split =kzalloc (sizeof *split,GFP_KERNEL ); +if (!split) +return -ENOMEM ; + +vfio_dma_unmap (iommu, start, size >>PAGE_SHIFT , mlp->rdwr); +
drivers/vfio/vfio_iommu.c line 340
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+ mlp->npage = npage_lo; + + split->npage = npage_hi; + split->daddr = start + size; + split->vaddr = mlp->vaddr +NPAGE_TO_SIZE (npage_lo) + size; + split->rdwr = mlp->rdwr; +list_add (&split->list, &iommu->dm_list); +return size >>PAGE_SHIFT ; +} +
Function should be static.
Quoted 13 lines
drivers/vfio/vfio_iommu.c new file +13
-0+int vfio_dma_unmap_dm (struct vfio_iommu *iommu,struct vfio_dma_map *dmp) +{ +int ret =0 ; +size_t npage = dmp->size >>PAGE_SHIFT ; +struct list_head *pos, *n; + +if (dmp->dmaaddr & ~PAGE_MASK ) +return -EINVAL ; +if (dmp->size & ~PAGE_MASK ) +return -EINVAL ; + +mutex_lock (&iommu->dgate); +
drivers/vfio/vfio_iommu.c line 363
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+list_for_each_safe (pos, n, &iommu->dm_list) { +struct dma_map_page *mlp; + + mlp = list_entry(pos, struct dma_map_page, list); +if (ranges_overlap (mlp->daddr,NPAGE_TO_SIZE (mlp->npage), + dmp->dmaaddr, dmp->size)) { + ret =vfio_remove_dma_overlap (iommu, dmp->dmaaddr, + dmp->size, mlp); +if (ret >0 ) + npage -=NPAGE_TO_SIZE (ret);
Why NPAGE_TO_SIZE here?
drivers/vfio/vfio_iommu.c line 371
Quoted 8 lines
drivers/vfio/vfio_iommu.c new file +8
-0+if (ret <0 || npage ==0 ) +break ; + } + } +mutex_unlock (&iommu->dgate); +return ret >0 ?0 : ret; +} +
Function should be static.
Quoted 5 lines
drivers/vfio/vfio_iommu.c new file +5
-0+int vfio_dma_map_dm (struct vfio_iommu *iommu,struct vfio_dma_map *dmp) +{ +int npage; +struct dma_map_page *mlp, *mmlp =NULL ; +dma_addr_t daddr = dmp->dmaaddr;
Quoted 33 lines · Aaron Fabbri
I'm going to send out chunks of comments as I go over this stuff. Below I've covered the documentation file and
vfio_iommu.c. More comments coming soon...Quoted 14 lines · Alex Williamson
VFIO provides a secure, IOMMU based interface for user space drivers, including device assignment to virtual machines. This provides the base management of IOMMU groups, devices, and IOMMU objects. See
Documentation/vfio.txtincluded in this patch for user and kernel API description.Note, this implements the new API discussed at KVM Forum 2011, as represented by the drvier version 0.2. It's hoped that this provides a modular enough interface to support PCI and non-PCI userspace drivers across various architectures and IOMMU implementations.
Signed-off-by Alex Williamson <[email protected]>---
<snip>
Documentation/vfio.txt line 38
Quoted 9 lines
Documentation/vfio.txt new file +8
-1+ +Groups, Devices, IOMMUs, oh my +----------------------------------------------------------------------------- -- + +A fundamental component of VFIO is the notion of IOMMU groups. IOMMUs +can't always distinguish transactions from each individual device in +the system. Sometimes this is because of the IOMMU design, such as with +PEs, other times it's caused by the I/O topology, for instance aCan you define this acronym the first time you use it, i.e.
Quoted 1 line
+ PEs (partitionable endpoints), ...
It was actually up in the <snip>:
... POWER systems with Partitionable Endpoints (PEs) ...
I tried to make sure I defined them, but let me know if anything else is missing/non-obvious.
Quoted 4 lines
Documentation/vfio.txt line 40
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+PCIe-to-PCI bridge masking all devices behind it. We call the sets of +devices created by these restictions IOMMU groups (or just "groups" forrestrictions
Ugh, lost w/o a spell checker. Fixed all these.
Quoted 15 lines
Quoted 5 lines
drivers/vfio/vfio_iommu.c new file +0
-0<snip>
Quoted 9 lines
drivers/vfio/vfio_iommu.c new file +9
-0+static struct dma_map_page *vfio_find_dma (struct vfio_iommu *iommu, +dma_addr_t start,size_t size) +{ +struct list_head *pos; +struct dma_map_page *mlp; + +list_for_each (pos, &iommu->dm_list) { + mlp = list_entry(pos, struct dma_map_page, list); + if (ranges_overlap(mlp->daddr, NPAGE_TO_SIZE(mlp->npage),
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 276
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ start, size)) +return mlp; + } +return NULL ; +} +This function below should be static.
Fixed
Quoted 17 lines
Quoted 17 lines
drivers/vfio/vfio_iommu.c new file +17
-0+int vfio_remove_dma_overlap (struct vfio_iommu *iommu,dma_addr_t start, +size_t size,struct dma_map_page *mlp) +{ +struct dma_map_page *split; +int npage_lo, npage_hi; + +/* Existing dma region is completely covered, unmap all */ +if (start <= mlp->daddr && + start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +vfio_dma_unmap (iommu, mlp->daddr, mlp->npage, mlp->rdwr); +list_del (&mlp->list); + npage_lo = mlp->npage; +kfree (mlp); +return npage_lo; + } + +/* Overlap low address of existing range */
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 299
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+if (start <= mlp->daddr) { +size_t overlap; + + overlap = start + size - mlp->daddr; + npage_lo = overlap >>PAGE_SHIFT ; + npage_hi = mlp->npage - npage_lo;
npage_hinot used.. Delete this line ^
Yep, and npage_lo in the next block. I was setting them just for symmetry, but they can be removed now.
Quoted 35 lines
Quoted 35 lines
drivers/vfio/vfio_iommu.c new file +35
-0+ +vfio_dma_unmap (iommu, mlp->daddr, npage_lo, mlp->rdwr); + mlp->daddr += overlap; + mlp->vaddr += overlap; + mlp->npage -= npage_lo; +return npage_lo; + } + +/* Overlap high address of existing range */ +if (start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +size_t overlap; + + overlap = mlp->daddr +NPAGE_TO_SIZE (mlp->npage) - start; + npage_hi = overlap >>PAGE_SHIFT ; + npage_lo = mlp->npage - npage_hi; + +vfio_dma_unmap (iommu, start, npage_hi, mlp->rdwr); + mlp->npage -= npage_hi; +return npage_hi; + } + +/* Split existing */ + npage_lo = (start - mlp->daddr) >>PAGE_SHIFT ; + npage_hi = mlp->npage - (size >>PAGE_SHIFT ) - npage_lo; + + split =kzalloc (sizeof *split,GFP_KERNEL ); +if (!split) +return -ENOMEM ; + +vfio_dma_unmap (iommu, start, size >>PAGE_SHIFT , mlp->rdwr); + + mlp->npage = npage_lo; + + split->npage = npage_hi; + split->daddr = start + size;
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 340
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ split->vaddr = mlp->vaddr +NPAGE_TO_SIZE (npage_lo) + size; + split->rdwr = mlp->rdwr; +list_add (&split->list, &iommu->dm_list); +return size >>PAGE_SHIFT ; +} +Function should be static.
Fixed
Quoted 17 lines
Quoted 17 lines
drivers/vfio/vfio_iommu.c new file +17
-0+int vfio_dma_unmap_dm (struct vfio_iommu *iommu,struct vfio_dma_map *dmp) +{ +int ret =0 ; +size_t npage = dmp->size >>PAGE_SHIFT ; +struct list_head *pos, *n; + +if (dmp->dmaaddr & ~PAGE_MASK ) +return -EINVAL ; +if (dmp->size & ~PAGE_MASK ) +return -EINVAL ; + +mutex_lock (&iommu->dgate); + +list_for_each_safe (pos, n, &iommu->dm_list) { +struct dma_map_page *mlp; + + mlp = list_entry(pos, struct dma_map_page, list);
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 363
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+if (ranges_overlap (mlp->daddr,NPAGE_TO_SIZE (mlp->npage), + dmp->dmaaddr, dmp->size)) { + ret =vfio_remove_dma_overlap (iommu, dmp->dmaaddr, + dmp->size, mlp); +if (ret >0 ) + npage -=NPAGE_TO_SIZE (ret);Why
NPAGE_TO_SIZEhere?
Looks like a bug, I'll change and test.
Quoted 10 lines
drivers/vfio/vfio_iommu.c line 371
Quoted 8 lines
drivers/vfio/vfio_iommu.c new file +8
-0+if (ret <0 || npage ==0 ) +break ; + } + } +mutex_unlock (&iommu->dgate); +return ret >0 ?0 : ret; +} +Function should be static.
Fixed.
Quoted 6 lines
drivers/vfio/vfio_iommu.c line 376
Quoted 5 lines
drivers/vfio/vfio_iommu.c new file +5
-0+int vfio_dma_map_dm (struct vfio_iommu *iommu,struct vfio_dma_map *dmp) +{ +int npage; +struct dma_map_page *mlp, *mmlp =NULL ; +dma_addr_t daddr = dmp->dmaaddr;
Thanks!
Alex
I'm going to send out chunks of comments as I go over this stuff. Below I've covered the documentation file and vfio_iommu.c. More comments coming soon...
Quoted 14 lines · Alex Williamson
VFIO provides a secure, IOMMU based interface for user space drivers, including device assignment to virtual machines. This provides the base management of IOMMU groups, devices, and IOMMU objects. See
Documentation/vfio.txtincluded in this patch for user and kernel API description.Note, this implements the new API discussed at KVM Forum 2011, as represented by the drvier version 0.2. It's hoped that this provides a modular enough interface to support PCI and non-PCI userspace drivers across various architectures and IOMMU implementations.
Signed-off-by Alex Williamson <[email protected]>---
<snip>
Documentation/vfio.txt line 38
Quoted 9 lines
Documentation/vfio.txt new file +8
-1+ +Groups, Devices, IOMMUs, oh my +----------------------------------------------------------------------------- -- + +A fundamental component of VFIO is the notion of IOMMU groups. IOMMUs +can't always distinguish transactions from each individual device in +the system. Sometimes this is because of the IOMMU design, such as with +PEs, other times it's caused by the I/O topology, for instance a
Can you define this acronym the first time you use it, i.e.
+ PEs (partitionable endpoints), ...
Documentation/vfio.txt line 40
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+PCIe-to-PCI bridge masking all devices behind it. We call the sets of +devices created by these restictions IOMMU groups (or just "groups" for
restrictions
Documentation/vfio.txt line 43
Quoted 3 lines
Documentation/vfio.txt new file +3
-0+this document). + +The IOMMU cannot distiguish transactions between the individual devices
distinguish
Quoted 4 lines
Documentation/vfio.txt new file +4
-0+within the group, therefore the group is the basic unit of ownership for +a userspace process. Because of this, groups are also the primary +interface to both devices and IOMMU domains in VFIO. +
<snip>
Documentation/vfio.txt line 101
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+file descriptor referencing the same internal IOMMU object from either +X or Y). Merged groups can be dissolved either explictly with UNMERGE
explicitly
<snip>
Review
Quoted 2 lines
(quoted diff) +2
-0+ +Device tree devices also invlude ioctls for further defining the
include
<snip>
Quoted 5 lines
drivers/vfio/vfio_iommu.c new file +0
-0
<snip>
Quoted 5 lines
drivers/vfio/vfio_iommu.c new file +5
-0+static struct dma_map_page *vfio_find_dma (struct vfio_iommu *iommu, +dma_addr_t start,size_t size) +{ +struct list_head *pos; +struct dma_map_page *mlp;
drivers/vfio/vfio_iommu.c line 276
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+ +list_for_each (pos, &iommu->dm_list) { + mlp = list_entry(pos, struct dma_map_page, list); +if (ranges_overlap (mlp->daddr,NPAGE_TO_SIZE (mlp->npage), + start, size)) +return mlp; + } +return NULL ; +} +
This function below should be static.
Quoted 13 lines
drivers/vfio/vfio_iommu.c new file +13
-0+int vfio_remove_dma_overlap (struct vfio_iommu *iommu,dma_addr_t start, +size_t size,struct dma_map_page *mlp) +{ +struct dma_map_page *split; +int npage_lo, npage_hi; + +/* Existing dma region is completely covered, unmap all */ +if (start <= mlp->daddr && + start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +vfio_dma_unmap (iommu, mlp->daddr, mlp->npage, mlp->rdwr); +list_del (&mlp->list); + npage_lo = mlp->npage; +kfree (mlp);
drivers/vfio/vfio_iommu.c line 299
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+return npage_lo; + } + +/* Overlap low address of existing range */ +if (start <= mlp->daddr) { +size_t overlap; + + overlap = start + size - mlp->daddr; + npage_lo = overlap >>PAGE_SHIFT ; + npage_hi = mlp->npage - npage_lo;
npage_hi not used.. Delete this line ^
Quoted 31 lines
drivers/vfio/vfio_iommu.c new file +31
-0+ +vfio_dma_unmap (iommu, mlp->daddr, npage_lo, mlp->rdwr); + mlp->daddr += overlap; + mlp->vaddr += overlap; + mlp->npage -= npage_lo; +return npage_lo; + } + +/* Overlap high address of existing range */ +if (start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +size_t overlap; + + overlap = mlp->daddr +NPAGE_TO_SIZE (mlp->npage) - start; + npage_hi = overlap >>PAGE_SHIFT ; + npage_lo = mlp->npage - npage_hi; + +vfio_dma_unmap (iommu, start, npage_hi, mlp->rdwr); + mlp->npage -= npage_hi; +return npage_hi; + } + +/* Split existing */ + npage_lo = (start - mlp->daddr) >>PAGE_SHIFT ; + npage_hi = mlp->npage - (size >>PAGE_SHIFT ) - npage_lo; + + split =kzalloc (sizeof *split,GFP_KERNEL ); +if (!split) +return -ENOMEM ; + +vfio_dma_unmap (iommu, start, size >>PAGE_SHIFT , mlp->rdwr); +
drivers/vfio/vfio_iommu.c line 340
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+ mlp->npage = npage_lo; + + split->npage = npage_hi; + split->daddr = start + size; + split->vaddr = mlp->vaddr +NPAGE_TO_SIZE (npage_lo) + size; + split->rdwr = mlp->rdwr; +list_add (&split->list, &iommu->dm_list); +return size >>PAGE_SHIFT ; +} +
Function should be static.
Quoted 13 lines
drivers/vfio/vfio_iommu.c new file +13
-0+int vfio_dma_unmap_dm (struct vfio_iommu *iommu,struct vfio_dma_map *dmp) +{ +int ret =0 ; +size_t npage = dmp->size >>PAGE_SHIFT ; +struct list_head *pos, *n; + +if (dmp->dmaaddr & ~PAGE_MASK ) +return -EINVAL ; +if (dmp->size & ~PAGE_MASK ) +return -EINVAL ; + +mutex_lock (&iommu->dgate); +
drivers/vfio/vfio_iommu.c line 363
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+list_for_each_safe (pos, n, &iommu->dm_list) { +struct dma_map_page *mlp; + + mlp = list_entry(pos, struct dma_map_page, list); +if (ranges_overlap (mlp->daddr,NPAGE_TO_SIZE (mlp->npage), + dmp->dmaaddr, dmp->size)) { + ret =vfio_remove_dma_overlap (iommu, dmp->dmaaddr, + dmp->size, mlp); +if (ret >0 ) + npage -=NPAGE_TO_SIZE (ret);
Why NPAGE_TO_SIZE here?
drivers/vfio/vfio_iommu.c line 371
Quoted 8 lines
drivers/vfio/vfio_iommu.c new file +8
-0+if (ret <0 || npage ==0 ) +break ; + } + } +mutex_unlock (&iommu->dgate); +return ret >0 ?0 : ret; +} +
Function should be static.
Quoted 5 lines
drivers/vfio/vfio_iommu.c new file +5
-0+int vfio_dma_map_dm (struct vfio_iommu *iommu,struct vfio_dma_map *dmp) +{ +int npage; +struct dma_map_page *mlp, *mmlp =NULL ; +dma_addr_t daddr = dmp->dmaaddr;
I have not gone through the all patch yet, but here are my first comments/questions about the code in vfio_main.c (and pci/vfio_pci.c).
Quoted 10 lines
-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Thursday, November 03, 2011 1:12 PM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Christian Benvenuti (benve); Aaron Fabbri (aafabbri); [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected] foundation.org; [email protected]
Subject: [RFC PATCH] vfio: VFIO Driver core framework
<snip>
Quoted 57 lines
drivers/vfio/vfio_main.c new file +7
-0@@ -0,0 +1,1151 @@ +/* +* VFIO framework +* +* Copyright (C) 2011 Red Hat, Inc. All rights reserved. +* Author: Alex Williamson <[email protected]> +* +* This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Derived from original vfio:
+ * Copyright 2010 Cisco Systems, Inc. All rights reserved.
+ * Author: Tom Lyon, [email protected]
+ */drivers/vfio/vfio_main.c new file +27
-0+ +#include <linux/cdev.h> +#include <linux/compat.h> +#include <linux/device.h> +#include <linux/file.h> +#include <linux/anon_inodes.h> +#include <linux/fs.h> +#include <linux/idr.h> +#include <linux/iommu.h> +#include <linux/mm.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/uaccess.h> +#include <linux/vfio.h> +#include <linux/wait.h> + +#include "vfio_private.h" + +#define DRIVER_VERSION "0.2" +#define DRIVER_AUTHOR "Alex Williamson <[email protected]>" +#define DRIVER_DESC "VFIO - User Level meta-driver" + +static int allow_unsafe_intrs; +module_param (allow_unsafe_intrs, int,0 ); +MODULE_PARM_DESC(allow_unsafe_intrs, + "Allow use of IOMMUs which do not support interruptremapping");
drivers/vfio/vfio_main.c new file +8
-0+ +static struct vfio { + dev_t devt; + struct cdev cdev; + struct list_head group_list; + struct mutex lock; + struct kref kref; + struct class *class;
drivers/vfio/vfio_main.c line 59
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+struct idr idr; +wait_queue_head_t release_q; +} vfio; + +static const struct file_operations vfio_group_fops; +extern const struct file_operations vfio_iommu_fops; + +struct vfio_group { +dev_t devt; +unsigned int groupid;
This groupid is returned by the device_group callback you recently added with a separate (not yet in tree) IOMMU patch.
Is it correct to say that the scope of this ID is the bus the iommu belongs too (but you use it as if it was global)?
I believe there is nothing right now to ensure the uniqueness of such ID across bus types (assuming there will be other bus drivers in the future besides vfio-pci).
If that's the case, the vfio.group_list global list and the __vfio_lookup_dev routine should be changed to account for the bus too?
Ops, I just saw the error msg in vfio_group_add_dev about the group id conflict. Is that warning related to what I mentioned above?
drivers/vfio/vfio_main.c line 71
Quoted 12 lines
drivers/vfio/vfio_main.c new file +12
-0+struct bus_type *bus; +struct vfio_iommu *iommu; +struct list_head device_list; +struct list_head iommu_next; +struct list_head group_next; +int refcnt; +}; + +struct vfio_device { +struct device *dev; +const struct vfio_device_ops *ops; +struct vfio_iommu *iommu;
I wonder if you need to have the 'iommu' field here. vfio_device.iommu is always set and reset together with vfio_group.iommu.
Given that a vfio_device instance is always linked to a vfio_group instance, do we need this duplication? Is this duplication there because you do not want the double dereference device->group->iommu?
Quoted 51 lines
drivers/vfio/vfio_main.c new file +51
-0+struct vfio_group *group; +struct list_head device_next; +bool attached; +int refcnt; +void *device_data; +}; + +/* +* Helper functions called under vfio.lock +*/ + +/* Return true if any devices within a group are opened */ +static bool __vfio_group_devs_inuse (struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next); +if (device->refcnt) +return true; + } +return false; +} + +/* Return true if any of the groups attached to an iommu are opened. +* We can only tear apart merged groups when nothing is left open. */ +static bool __vfio_iommu_groups_inuse (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group, iommu_next); +if (group->refcnt) +return true; + } +return false; +} + +/* An iommu is "in use" if it has a file descriptor open or if any of +* the groups assigned to the iommu have devices open. */ +static bool __vfio_iommu_inuse (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (iommu->refcnt) +return true; +
drivers/vfio/vfio_main.c line 132
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group, iommu_next); + +if (__vfio_group_devs_inuse (group)) +return true; + } +return false; +}
I looked at how you take care of ref counts ...
This is how the tree of vfio_iommu/vfio_group/vfio_device data Structures is organized (I'll use just iommu/group/dev to make the graph smaller):
iommu
/ \
/ \
group ... group
/ \ / \ / \ / \
dev .. dev dev .. dev
This is how you get a file descriptor for the three kind of objects:
- group : open /dev/vfio/xxx for group xxx
- iommu : group ioctl VFIO_GROUP_GET_IOMMU_FD
- device: group ioctl VFIO_GROUP_GET_DEVICE_FD
Given the above topology, I would assume that:
(1) an iommu is 'inuse' if : a) iommu refcnt > 0, or
b) any of its groups is 'inuse'
(2) a group is 'inuse' if : a) group refcnt > 0, or
b) any of its devices is 'inuse'
(3) a device is 'inuse' if : a) device refcnt > 0
You have coded the 'inuse' logic with these three routines:
__vfio_iommu_inuse, which implements (1) above
and
__vfio_iommu_groups_inuse
__vfio_group_devs_inuse
which are used by __vfio_iommu_inuse.
Why don't you check the group refcnt in __vfio_iommu_groups_inuse?
Would it make sense (and the code more readable) to structure the nested refcnt/inuse check like this?
(The numbers (1)(2)(3) refer to the three 'inuse' conditions above)
(1)__vfio_iommu_inuse
|
+-> check iommu refcnt
+-> __vfio_iommu_groups_inuse
|
+->LOOP: (2)__vfio_iommu_group_inuse<--MISSING
|
+-> check group refcnt<--MISSING
+-> __vfio_group_devs_inuse()
|
+-> LOOP: (3)__vfio_group_dev_inuse<--MISSING
|
+-> check device refcntdrivers/vfio/vfio_main.c line 144
Quoted 11 lines
drivers/vfio/vfio_main.c new file +11
-0+static void __vfio_group_set_iommu (struct vfio_group *group, +struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (group->iommu) +list_del (&group->iommu_next); +if (iommu) +list_add (&group->iommu_next, &iommu->group_list); + + group->iommu = iommu;
If you remove the vfio_device.iommu field (as suggested above in a previous Comment), the block below would not be needed anymore.
Quoted 108 lines
drivers/vfio/vfio_main.c new file +70
-0+list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next); + device->iommu = iommu; + } +} + +static void __vfio_iommu_detach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +BUG_ON (!iommu->domain && device->attached); + +if (!iommu->domain || !device->attached) +return ; + +iommu_detach_device (iommu->domain, device->dev); + device->attached = false; +} + +static void __vfio_iommu_detach_group (struct vfio_iommu *iommu, +struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next); +__vfio_iommu_detach_dev (iommu, device); + } +} + +static int __vfio_iommu_attach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +int ret; + +BUG_ON (device->attached); + +if (!iommu || !iommu->domain) +return -EINVAL ; + + ret =iommu_attach_device (iommu->domain, device->dev); +if (!ret) + device->attached = true; + +return ret; +} + +static int __vfio_iommu_attach_group (struct vfio_iommu *iommu, +struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; +int ret; + + device = list_entry(pos, struct vfio_device, device_next); + ret =__vfio_iommu_attach_dev (iommu, device); +if (ret) { +__vfio_iommu_detach_group (iommu, group); +return ret; + } + } +return 0 ; +} + +/* The iommu is viable, ie. ready to be configured, when all the devices
+ * for all the groups attached to the iommu are bound to their vfio device
+ * drivers (ex. vfio-pci). This sets thedevice_dataprivate data pointer. */drivers/vfio/vfio_main.c new file +33
-0+static bool __vfio_iommu_viable (struct vfio_iommu *iommu) +{ +struct list_head *gpos, *dpos; + +list_for_each (gpos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(gpos, struct vfio_group, iommu_next); + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + device =list_entry (dpos, + struct vfio_device, device_next); + +if (!device->device_data) +return false; + } + } +return true; +} + +static void __vfio_close_iommu (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (!iommu->domain) +return ; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(pos, struct vfio_group, iommu_next); + +__vfio_iommu_detach_group (iommu, group); + }
drivers/vfio/vfio_main.c line 260
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+ +vfio_iommu_unmapall (iommu); + +iommu_domain_free (iommu->domain); + iommu->domain =NULL ; + iommu->mm =NULL ; +} + +/* Open the IOMMU. This gates all access to the iommu or device file +* descriptors and sets current->mm as the exclusive user. */
Given the fn vfio_group_open (ie, 1st object, 2nd operation), I would have called this one __vfio_iommu_open (instead of __vfio_open_iommu).
Is it named __vfio_open_iommu to avoid a conflict with the namespace in vfio_iommu.c?
Quoted 240 lines
drivers/vfio/vfio_main.c new file +40
-0+static int __vfio_open_iommu (struct vfio_iommu *iommu) +{ +struct list_head *pos; +int ret; + +if (!__vfio_iommu_viable (iommu)) +return -EBUSY ; + +if (iommu->domain) +return -EINVAL ; + + iommu->domain =iommu_domain_alloc (iommu->bus); +if (!iommu->domain) +return -EFAULT ; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(pos, struct vfio_group, iommu_next); + + ret =__vfio_iommu_attach_group (iommu, group); +if (ret) { +__vfio_close_iommu (iommu); +return ret; + } + } + +if (!allow_unsafe_intrs && + !iommu_domain_has_cap (iommu->domain,IOMMU_CAP_INTR_REMAP )) { +__vfio_close_iommu (iommu); +return -EFAULT ; + } + + iommu->cache = (iommu_domain_has_cap (iommu->domain, +IOMMU_CAP_CACHE_COHERENCY ) !=0 ); + iommu->mm = current->mm; + +return 0 ; +} + +/* Actively try to tear down the iommu and merged groups. If there are no
+ * open iommu or device fds, we close the iommu. If we close the iommu and
+ * there are also no open group fds, we can futher dissolve the group to
+ * iommu association and free the iommu data structure. */drivers/vfio/vfio_main.c new file +15
-0+static int __vfio_try_dissolve_iommu (struct vfio_iommu *iommu) +{ + +if (__vfio_iommu_inuse (iommu)) +return -EBUSY ; + +__vfio_close_iommu (iommu); + +if (!__vfio_iommu_groups_inuse (iommu)) { +struct list_head *pos, *ppos; + +list_for_each_safe (pos, ppos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group,
iommu_next);drivers/vfio/vfio_main.c new file +64
-0+__vfio_group_set_iommu (group,NULL ); + } + + +kfree (iommu); + } + +return 0 ; +} + +static struct vfio_device *__vfio_lookup_dev (struct device *dev) +{ +struct list_head *gpos; +unsigned int groupid; + +if (iommu_device_group (dev, &groupid)) +return NULL ; + +list_for_each (gpos, &vfio.group_list) { +struct vfio_group *group; +struct list_head *dpos; + + group = list_entry(gpos, struct vfio_group, group_next); + +if (group->groupid != groupid) +continue ; + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + + device =list_entry (dpos, + struct vfio_device, device_next); + +if (device->dev == dev) +return device; + } + } +return NULL ; +} + +/* All release paths simply decrement the refcnt, attempt to teardown +* the iommu and merged groups, and wakeup anything that might be +* waiting if we successfully dissolve anything. */ +static int vfio_do_release (int *refcnt,struct vfio_iommu *iommu) +{ +bool wake; + +mutex_lock (&vfio.lock); + + (*refcnt)--; + wake = (__vfio_try_dissolve_iommu (iommu) ==0 ); + +mutex_unlock (&vfio.lock); + +if (wake) +wake_up (&vfio.release_q); + +return 0 ; +} + +/* +* Device fops - passthrough to vfio device driver w/ device_data +*/ +static int vfio_device_release(struct inode *inode, struct file*filep)
drivers/vfio/vfio_main.c new file +27
-0+{ +struct vfio_device *device = filep->private_data; + +vfio_do_release (&device->refcnt, device->iommu); + + device->ops->put (device->device_data); + +return 0 ; +} + +static long vfio_device_unl_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ +struct vfio_device *device = filep->private_data; + +return device->ops->ioctl (device->device_data, cmd, arg); +} + +static ssize_t vfio_device_read (struct file *filep,char __user *buf, +size_t count,loff_t *ppos) +{ +struct vfio_device *device = filep->private_data; + +return device->ops->read (device->device_data, buf, count, ppos); +} + +static ssize_t vfio_device_write(struct file *filep, const char __user*buf,
drivers/vfio/vfio_main.c new file +8
-0+ size_t count, loff_t *ppos) +{ + struct vfio_device *device = filep->private_data; + +return device->ops->write (device->device_data, buf, count, ppos); +} + +static int vfio_device_mmap(struct file *filep, struct vm_area_struct*vma)
drivers/vfio/vfio_main.c new file +76
-0+{ +struct vfio_device *device = filep->private_data; + +return device->ops->mmap (device->device_data, vma); +} + +#ifdef CONFIG_COMPAT +static long vfio_device_compat_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ + arg = (unsigned long )compat_ptr (arg); +return vfio_device_unl_ioctl (filep, cmd, arg); +} +#endif /* CONFIG_COMPAT */ + +const struct file_operations vfio_device_fops = { + .owner =THIS_MODULE , + .release = vfio_device_release, + .read = vfio_device_read, + .write = vfio_device_write, + .unlocked_ioctl = vfio_device_unl_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = vfio_device_compat_ioctl, +#endif + .mmap = vfio_device_mmap, +}; + +/* +* Group fops +*/ +static int vfio_group_open (struct inode *inode,struct file *filep) +{ +struct vfio_group *group; +int ret =0 ; + +mutex_lock (&vfio.lock); + + group =idr_find (&vfio.idr,iminor (inode)); + +if (!group) { + ret = -ENODEV ; + goto out; + } + + filep->private_data = group; + +if (!group->iommu) { +struct vfio_iommu *iommu; + + iommu =kzalloc (sizeof (*iommu),GFP_KERNEL ); +if (!iommu) { + ret = -ENOMEM ; + goto out; + } +INIT_LIST_HEAD (&iommu->group_list); +INIT_LIST_HEAD (&iommu->dm_list); +mutex_init (&iommu->dgate); + iommu->bus = group->bus; +__vfio_group_set_iommu (group, iommu); + } + group->refcnt++; + +out: +mutex_unlock (&vfio.lock); + +return ret; +} + +static int vfio_group_release (struct inode *inode,struct file *filep) +{ +struct vfio_group *group = filep->private_data; + +return vfio_do_release (&group->refcnt, group->iommu); +} + +/* Attempt to merge the group pointed to by fd into group. The merge-
Quoted 5 lines
ee
+ * group must not have an iommu or any devices open because we cannot
+ * maintain that context across the merge. The merge-er group can be
+ * in use. */
+static intvfio_group_merge(struct vfio_group *group, int fd)
The documentation in vfio.txt explains clearly the logic implemented by the merge/unmerge group ioctls.
However, what you are doing is not merging groups, but rather adding/removing groups to/from iommus (and creating flat lists of groups).
For example, when you do
merge(A,B)
you actually mean to say "merge B to the list of groups assigned to the same iommu as group A".
For the same reason, you do not really need to provide the group you want to unmerge from, which means that instead of
unmerge(A,B)
you would just need
unmerge(B)
I understand the reason why it is not a real merge/unmerge (ie, to keep the original groups so that you can unmerge later) ... however I just wonder if it wouldn't be more natural to implement the VFIO_IOMMU_ADD_GROUP/DEL_GROUP iommu ioctls instead? (the relationships between the data structure would remain the same)
I guess you already discarded this option for some reasons, right? What was the reason?
Quoted 303 lines
drivers/vfio/vfio_main.c new file +65
-0+{ +struct vfio_group *new; +struct vfio_iommu *old_iommu; +struct file *file; +int ret =0 ; +bool opened = false; + +mutex_lock (&vfio.lock); + + file =fget (fd); +if (!file) { + ret = -EBADF ; + goto out_noput; + } + +/* Sanity check, is this really our fd? */ +if (file->f_op != &vfio_group_fops) { + ret = -EINVAL ; + goto out; + } + + new = file->private_data; + +if (!new || new == group || !new->iommu || + new->iommu->domain || new->bus != group->bus) { + ret = -EINVAL ; + goto out; + } + +/* We need to attach all the devices to each domain separately +* in order to validate that the capabilities match for both. */ + ret =__vfio_open_iommu (new->iommu); +if (ret) + goto out; + +if (!group->iommu->domain) { + ret =__vfio_open_iommu (group->iommu); +if (ret) + goto out; + opened = true; + } + +/* If cache coherency doesn't match we'd potentialy need to +* remap existing iommu mappings in the merge-er domain. +* Poor return to bother trying to allow this currently. */ +if (iommu_domain_has_cap (group->iommu->domain, +IOMMU_CAP_CACHE_COHERENCY ) != +iommu_domain_has_cap (new->iommu->domain, +IOMMU_CAP_CACHE_COHERENCY )) { +__vfio_close_iommu (new->iommu); +if (opened) +__vfio_close_iommu (group->iommu); + ret = -EINVAL ; + goto out; + } + +/* Close the iommu for the merge-ee and attach all its devices +* to the merge-er iommu. */ +__vfio_close_iommu (new->iommu); + + ret =__vfio_iommu_attach_group (group->iommu, new); +if (ret) + goto out; + +/* set_iommu unlinks new from the iommu, so save a pointer to it */
drivers/vfio/vfio_main.c new file +94
-0+ old_iommu = new->iommu; +__vfio_group_set_iommu (new, group->iommu); +kfree (old_iommu); + +out: +fput (file); +out_noput: +mutex_unlock (&vfio.lock); +return ret; +} + +/* Unmerge the group pointed to by fd from group. */ +static int vfio_group_unmerge (struct vfio_group *group,int fd) +{ +struct vfio_group *new; +struct vfio_iommu *new_iommu; +struct file *file; +int ret =0 ; + +/* Since the merge-out group is already opened, it needs to +* have an iommu struct associated with it. */ + new_iommu =kzalloc (sizeof (*new_iommu),GFP_KERNEL ); +if (!new_iommu) +return -ENOMEM ; + +INIT_LIST_HEAD (&new_iommu->group_list); +INIT_LIST_HEAD (&new_iommu->dm_list); +mutex_init (&new_iommu->dgate); + new_iommu->bus = group->bus; + +mutex_lock (&vfio.lock); + + file =fget (fd); +if (!file) { + ret = -EBADF ; + goto out_noput; + } + +/* Sanity check, is this really our fd? */ +if (file->f_op != &vfio_group_fops) { + ret = -EINVAL ; + goto out; + } + + new = file->private_data; +if (!new || new == group || new->iommu != group->iommu) { + ret = -EINVAL ; + goto out; + } + +/* We can't merge-out a group with devices still in use. */ +if (__vfio_group_devs_inuse (new)) { + ret = -EBUSY ; + goto out; + } + +__vfio_iommu_detach_group (group->iommu, new); +__vfio_group_set_iommu (new, new_iommu); + +out: +fput (file); +out_noput: +if (ret) +kfree (new_iommu); +mutex_unlock (&vfio.lock); +return ret; +} + +/* Get a new iommu file descriptor. This will open the iommu, setting +* the current->mm ownership if it's not already set. */ +static int vfio_group_get_iommu_fd (struct vfio_group *group) +{ +int ret =0 ; + +mutex_lock (&vfio.lock); + +if (!group->iommu->domain) { + ret =__vfio_open_iommu (group->iommu); +if (ret) + goto out; + } + + ret =anon_inode_getfd ("[vfio-iommu]" , &vfio_iommu_fops, + group->iommu,O_RDWR ); +if (ret <0 ) + goto out; + + group->iommu->refcnt++; +out: +mutex_unlock (&vfio.lock); +return ret; +} + +/* Get a new device file descriptor. This will open the iommu, setting
+ * thecurrent->mmownership if it's not already set. It's difficult to
+ * specify the requirements for matching a user supplied buffer to a
+ * device, so we use a vfio driver callback to test for a match. For
+ * PCI,dev_name(dev)is unique, but other drivers may require including
+ * a parent device string. */
+static intvfio_group_get_device_fd(struct vfio_group *group, char *buf)drivers/vfio/vfio_main.c new file +133
-0+{ +struct vfio_iommu *iommu = group->iommu; +struct list_head *gpos; +int ret = -ENODEV ; + +mutex_lock (&vfio.lock); + +if (!iommu->domain) { + ret =__vfio_open_iommu (iommu); +if (ret) + goto out; + } + +list_for_each (gpos, &iommu->group_list) { +struct list_head *dpos; + + group = list_entry(gpos, struct vfio_group, iommu_next); + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + + device =list_entry (dpos, + struct vfio_device, device_next); + +if (device->ops->match (device->dev, buf)) { +struct file *file; + +if (device->ops->get (device->device_data)) { + ret = -EFAULT ; + goto out; + } + +/* We can't use anon_inode_getfd(), like above +* because we need to modify the f_mode flags +* directly to allow more than just ioctls */ + ret =get_unused_fd (); +if (ret <0 ) { + device->ops->put (device->device_data); + goto out; + } + + file =anon_inode_getfile ("[vfio-device]" , + &vfio_device_fops, + device,O_RDWR ); +if (IS_ERR (file)) { +put_unused_fd (ret); + ret =PTR_ERR (file); + device->ops->put (device->device_data); + goto out; + } + +/* Todo: add an anon_inode interface to do +* this. Appears to be missing by lack of +* need rather than explicitly prevented. +* Now there's need. */ + file->f_mode |= (FMODE_LSEEK | +FMODE_PREAD | +FMODE_PWRITE ); + +fd_install (ret, file); + + device->refcnt++; + goto out; + } + } + } +out: +mutex_unlock (&vfio.lock); +return ret; +} + +static long vfio_group_unl_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ +struct vfio_group *group = filep->private_data; + +if (cmd ==VFIO_GROUP_GET_FLAGS ) { +u64 flags =0 ; + +mutex_lock (&vfio.lock); +if (__vfio_iommu_viable (group->iommu)) + flags |=VFIO_GROUP_FLAGS_VIABLE ; +mutex_unlock (&vfio.lock); + +if (group->iommu->mm) + flags |=VFIO_GROUP_FLAGS_MM_LOCKED ; + +return put_user (flags, (u64 __user *)arg); + } + +/* Below commands are restricted once the mm is set */ +if (group->iommu->mm && group->iommu->mm != current->mm) +return -EPERM ; +if (cmd ==VFIO_GROUP_MERGE || cmd ==VFIO_GROUP_UNMERGE ) { +int fd; + +if (get_user (fd, (int __user *)arg)) +return -EFAULT ; +if (fd <0 ) +return -EINVAL ; + +if (cmd ==VFIO_GROUP_MERGE ) +return vfio_group_merge (group, fd); +else +return vfio_group_unmerge (group, fd); + }else if (cmd ==VFIO_GROUP_GET_IOMMU_FD ) { +return vfio_group_get_iommu_fd (group); + }else if (cmd ==VFIO_GROUP_GET_DEVICE_FD ) { +char *buf; +int ret; + + buf =strndup_user ((const char __user *)arg,PAGE_SIZE ); +if (IS_ERR (buf)) +return PTR_ERR (buf); + + ret =vfio_group_get_device_fd (group, buf); +kfree (buf); +return ret; + } + +return -ENOSYS ; +} + +#ifdef CONFIG_COMPAT +static long vfio_group_compat_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ + arg = (unsigned long )compat_ptr (arg); +return vfio_group_unl_ioctl (filep, cmd, arg); +} +#endif /* CONFIG_COMPAT */ + +static const struct file_operations vfio_group_fops = {
drivers/vfio/vfio_main.c line 806
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+ .owner = THIS_MODULE, + .open = vfio_group_open, + .release = vfio_group_release, + .unlocked_ioctl = vfio_group_unl_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = vfio_group_compat_ioctl, +#endif +}; + +/* iommu fd release hook */
Given vfio_device_release and
vfio_group_release (ie, 1st object, 2nd operation), I was
going to suggest renaming the fn below to vfio_iommu_release, but then I saw the latter name being already used in vfio_iommu.c ... a bit confusing but I guess it's ok then.
Quoted 95 lines
drivers/vfio/vfio_main.c new file +11
-0+int vfio_release_iommu (struct vfio_iommu *iommu) +{ +return vfio_do_release (&iommu->refcnt, iommu); +} + +/* +* VFIO driver API +*/ + +/* Add a new device to the vfio framework with associated vfio driver +* callbacks. This is the entry point for vfio drivers to register devices. */
+intvfio_group_add_dev(struct device *dev, const struct vfio_device_ops *ops)drivers/vfio/vfio_main.c new file +59
-0+{ + struct list_head *pos; + struct vfio_group *group = NULL; + struct vfio_device *device = NULL; + unsigned int groupid; + int ret = 0; + bool new_group = false; + + if (!ops) + return -EINVAL; + + if (iommu_device_group(dev, &groupid)) + return -ENODEV; + + mutex_lock(&vfio.lock); + + list_for_each(pos, &vfio.group_list) { + group = list_entry(pos, struct vfio_group, group_next); + if (group->groupid == groupid) + break; + group = NULL; + } + + if (!group) { + int minor; + + if (unlikely(idr_pre_get(&vfio.idr, GFP_KERNEL) == 0)) { + ret = -ENOMEM; + goto out; + } + + group = kzalloc(sizeof(*group), GFP_KERNEL); + if (!group) { + ret = -ENOMEM; + goto out; + } + + group->groupid = groupid; + INIT_LIST_HEAD(&group->device_list); + + ret = idr_get_new(&vfio.idr, group, &minor); + if (ret == 0 && minor > MINORMASK) { + idr_remove(&vfio.idr, minor); + kfree(group); + ret = -ENOSPC; + goto out; + } + + group->devt = MKDEV(MAJOR(vfio.devt), minor); + device_create(vfio.class, NULL, group->devt, + group, "%u", groupid); + + group->bus = dev->bus; + list_add(&group->group_next, &vfio.group_list); + new_group = true; + } else { + if (group->bus != dev->bus) { + printk(KERN_WARNING + "Error: IOMMU group ID conflict. Group ID %u"
drivers/vfio/vfio_main.c new file +20
-0+"on both bus %s and %s\n" , groupid, + group->bus->name, dev->bus->name); + ret = -EFAULT ; + goto out; + } + +list_for_each (pos, &group->device_list) { + device =list_entry (pos, + struct vfio_device, device_next); +if (device->dev == dev) +break ; + device =NULL ; + } + } + + if (!device) { + if (__vfio_group_devs_inuse(group) || + (group->iommu && group->iommu->refcnt)) { + printk(KERN_WARNING + "Adding device %s to group %u while group isalready in use!!\n",
drivers/vfio/vfio_main.c line 899
Quoted 2 lines
drivers/vfio/vfio_main.c new file +2
-0+ dev_name(dev), group->groupid); +/* XXX How to prevent other drivers from claiming? */
Here we are adding a device (not yet assigned to a vfio bus) to a group that is already in use.
Given that it would not be acceptable for this device to get assigned to a non vfio driver, why not forcing such assignment here then? I am not sure though what the best way to do it would be. What about something like this:
- when the bus vfio-pci processes the BUS_NOTIFY_ADD_DEVICE notification it assigns to the device a PCI ID that will make sure the vfio-pci's probe routine will be invoked (and no other driver can therefore claim the device). That PCI ID would have to be added to the vfio_pci_driver's id_table (it would be the exception to the "only dynamic IDs" rule). Too hackish?
Quoted 9 lines
drivers/vfio/vfio_main.c new file +9
-0+ } + + device =kzalloc (sizeof (*device),GFP_KERNEL ); +if (!device) { +/* If we just created this group, tear it down */ +if (new_group) { +list_del (&group->group_next); +device_destroy (vfio.class, group->devt); +idr_remove (&vfio.idr,MINOR (group->devt));
drivers/vfio/vfio_main.c line 918
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+kfree (group); + } + ret = -ENOMEM ; + goto out; + } + +list_add (&device->device_next, &group->device_list); + device->dev = dev; + device->ops = ops; + device->iommu = group->iommu;/* NULL if new */
Shouldn't you check the return code of __vfio_iommu_attach_dev?
drivers/vfio/vfio_main.c line 927
Quoted 9 lines
drivers/vfio/vfio_main.c new file +9
-0+__vfio_iommu_attach_dev (group->iommu, device); + } +out: +mutex_unlock (&vfio.lock); +return ret; +} +EXPORT_SYMBOL_GPL (vfio_group_add_dev); + +/* Remove a device from the vfio framework */
This fn below does not return any error code. Ok ...
However, there are a number of errors case that you test, for example
- device that does not belong to any group (according to iommu API)
- device that belongs to a group but that does not appear in the list of devices of the vfio_group structure.
Are the above two errors checks just paranoia or are those errors actually possible? If they were possible, shouldn't we generate a warning (most probably it would be a bug in the code)?
Quoted 111 lines
drivers/vfio/vfio_main.c new file +64
-0+void vfio_group_del_dev (struct device *dev) +{ +struct list_head *pos; +struct vfio_group *group =NULL ; +struct vfio_device *device =NULL ; +unsigned int groupid; + +if (iommu_device_group (dev, &groupid)) +return ; + +mutex_lock (&vfio.lock); + +list_for_each (pos, &vfio.group_list) { + group = list_entry(pos, struct vfio_group, group_next); +if (group->groupid == groupid) +break ; + group =NULL ; + } + +if (!group) + goto out; + +list_for_each (pos, &group->device_list) { + device = list_entry(pos, struct vfio_device, device_next); +if (device->dev == dev) +break ; + device =NULL ; + } + +if (!device) + goto out; + +BUG_ON (device->refcnt); + +if (device->attached) +__vfio_iommu_detach_dev (group->iommu, device); + +list_del (&device->device_next); +kfree (device); + +/* If this was the only device in the group, remove the group. +* Note that we intentionally unmerge empty groups here if the +* group fd isn't opened. */ +if (list_empty (&group->device_list) && group->refcnt ==0 ) { +struct vfio_iommu *iommu = group->iommu; + +if (iommu) { +__vfio_group_set_iommu (group,NULL ); +__vfio_try_dissolve_iommu (iommu); + } + +device_destroy (vfio.class, group->devt); +idr_remove (&vfio.idr,MINOR (group->devt)); +list_del (&group->group_next); +kfree (group); + } +out: +mutex_unlock (&vfio.lock); +} +EXPORT_SYMBOL_GPL (vfio_group_del_dev); + +/* When a device is bound to a vfio device driver (ex. vfio-pci), this +* entry point is used to mark the device usable (viable). The vfio +* device driver associates a private device_data struct with the device
+ * here, which will later be return forvfio_device_fopscallbacks. */drivers/vfio/vfio_main.c new file +23
-0+int vfio_bind_dev (struct device *dev,void *device_data) +{ +struct vfio_device *device; +int ret = -EINVAL ; + +BUG_ON (!device_data); + +mutex_lock (&vfio.lock); + + device =__vfio_lookup_dev (dev); + +BUG_ON (!device); + + ret =dev_set_drvdata (dev, device); +if (!ret) + device->device_data = device_data; + +mutex_unlock (&vfio.lock); +return ret; +} +EXPORT_SYMBOL_GPL (vfio_bind_dev); + +/* A device is only removeable if the iommu for the group is not in use. */
drivers/vfio/vfio_main.c new file +14
-0+static bool vfio_device_removeable (struct vfio_device *device) +{ +bool ret = true; + +mutex_lock (&vfio.lock); + +if (device->iommu &&__vfio_iommu_inuse (device->iommu)) + ret = false; + +mutex_unlock (&vfio.lock); +return ret; +} + +/* Notify vfio that a device is being unbound from the vfio device driver
+ * and return the device privatedevice_datapointer. If the group is
+ * in use, we need to block or take other measures to make it safe for
+ * the device to be removed from the iommu. */drivers/vfio/vfio_main.c new file +3
-0+void *vfio_unbind_dev (struct device *dev) +{ +struct vfio_device *device =dev_get_drvdata (dev);
drivers/vfio/vfio_main.c line 1045
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+void *device_data; + +BUG_ON (!device); + +again: +if (!vfio_device_removeable (device)) { +/* XXX signal for all devices in group to be removed or +* resort to killing the process holding the device fds. +* For now just block waiting for releases to wake us. */ +wait_event (vfio.release_q,vfio_device_removeable (device));
Any new idea/proposal on how to handle this situation?
The last one I remember was to leave the soft/hard/etc timeout handling in userspace and implement it as a sort of policy. Is that one still the most likely candidate solution to handle this situation?
Quoted 317 lines
drivers/vfio/vfio_main.c new file +5
-0+ } + +mutex_lock (&vfio.lock); + +/* Need to re-check that the device is still removeable under lock. */
drivers/vfio/vfio_main.c new file +101
-0+if (device->iommu &&__vfio_iommu_inuse (device->iommu)) { +mutex_unlock (&vfio.lock); + goto again; + } + + device_data = device->device_data; + + device->device_data =NULL ; +dev_set_drvdata (dev,NULL ); + +mutex_unlock (&vfio.lock); +return device_data; +} +EXPORT_SYMBOL_GPL (vfio_unbind_dev); + +/* +* Module/class support +*/ +static void vfio_class_release (struct kref *kref) +{ +class_destroy (vfio.class); + vfio.class =NULL ; +} + +static char *vfio_devnode (struct device *dev,mode_t *mode) +{ +return kasprintf (GFP_KERNEL ,"vfio/%s" ,dev_name (dev)); +} + +static int __init vfio_init (void ) +{ +int ret; + +idr_init (&vfio.idr); +mutex_init (&vfio.lock); +INIT_LIST_HEAD (&vfio.group_list); +init_waitqueue_head (&vfio.release_q); + +kref_init (&vfio.kref); + vfio.class =class_create (THIS_MODULE ,"vfio" ); +if (IS_ERR (vfio.class)) { + ret =PTR_ERR (vfio.class); + goto err_class; + } + + vfio.class->devnode = vfio_devnode; + +/* FIXME - how many minors to allocate... all of them! */ + ret =alloc_chrdev_region (&vfio.devt,0 ,MINORMASK ,"vfio" ); +if (ret) + goto err_chrdev; + +cdev_init (&vfio.cdev, &vfio_group_fops); + ret =cdev_add (&vfio.cdev, vfio.devt,MINORMASK ); +if (ret) + goto err_cdev; + +pr_info (DRIVER_DESC " version: " DRIVER_VERSION "\n" ); + +return 0 ; + +err_cdev: +unregister_chrdev_region (vfio.devt,MINORMASK ); +err_chrdev: +kref_put (&vfio.kref, vfio_class_release); +err_class: +return ret; +} + +static void __exit vfio_cleanup(void) +{ +struct list_head *gpos, *gppos; + +list_for_each_safe (gpos, gppos, &vfio.group_list) { +struct vfio_group *group; +struct list_head *dpos, *dppos; + + group = list_entry(gpos, struct vfio_group, group_next); + +list_for_each_safe (dpos, dppos, &group->device_list) { +struct vfio_device *device; + + device =list_entry (dpos, + struct vfio_device, device_next); +vfio_group_del_dev (device->dev); + } + } + +idr_destroy (&vfio.idr); +cdev_del (&vfio.cdev); +unregister_chrdev_region (vfio.devt,MINORMASK ); +kref_put (&vfio.kref, vfio_class_release); +} + +module_init (vfio_init); +module_exit (vfio_cleanup); + +MODULE_VERSION (DRIVER_VERSION ); +MODULE_LICENSE ("GPL v2" ); +MODULE_AUTHOR (DRIVER_AUTHOR ); +MODULE_DESCRIPTION (DRIVER_DESC );drivers/vfio/vfio_private.h new file +5
-0@@ -0,0 +1,34 @@ +/* +* Copyright (C) 2011 Red Hat, Inc. All rights reserved. +* Author: Alex Williamson <[email protected]> +* +* This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Derived from original vfio:
+ * Copyright 2010 Cisco Systems, Inc. All rights reserved.
+ * Author: Tom Lyon, [email protected]
+ */drivers/vfio/vfio_private.h new file +22
-0+ +#include <linux/list.h> +#include <linux/mutex.h> + +#ifndef VFIO_PRIVATE_H +#define VFIO_PRIVATE_H + +struct vfio_iommu { +struct iommu_domain *domain; +struct bus_type *bus; +struct mutex dgate; +struct list_head dm_list; +struct mm_struct *mm; +struct list_head group_list; +int refcnt; +bool cache; +}; + +extern int vfio_release_iommu (struct vfio_iommu *iommu); +extern void vfio_iommu_unmapall (struct vfio_iommu *iommu); + +#endif /* VFIO_PRIVATE_H */ include/linux/vfio.h new file +5
-0@@ -0,0 +1,155 @@ +/* +* Copyright 2010 Cisco Systems, Inc. All rights reserved. +* Author: Tom Lyon, [email protected] +* +* This program is free software; you may redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Portions derived fromdrivers/uio/uio.c:
+ * Copyright(C) 2005, Benedikt Spranger <[email protected]>
+ * Copyright(C) 2005, Thomas Gleixner <[email protected]>
+ * Copyright(C) 2006, Hans J. Koch <[email protected]>
+ * Copyright(C) 2006, Greg Kroah-Hartman <[email protected]>
+ *
+ * Portions derived fromdrivers/uio/uio_pci_generic.c:
+ * Copyright (C) 2009 Red Hat, Inc.
+ * Author: Michael S. Tsirkin <[email protected]>
+ */include/linux/vfio.h new file +55
-0+#include <linux/types.h> + +#ifndef VFIO_H +#define VFIO_H + +#ifdef __KERNEL__ + +struct vfio_device_ops { + bool (*match)(struct device *, char *); + int (*get)(void *); + void (*put)(void *); + ssize_t (*read)(void *, char __user *, + size_t, loff_t *); + ssize_t (*write)(void *, const char __user *, + size_t, loff_t *); + long (*ioctl)(void *, unsigned int, unsigned long); + int (*mmap)(void *, struct vm_area_struct *); +}; + +extern int vfio_group_add_dev(struct device *device, + const struct vfio_device_ops *ops); +extern void vfio_group_del_dev(struct device *device); +extern int vfio_bind_dev(struct device *device, void *device_data); +extern void *vfio_unbind_dev(struct device *device); + +#endif /* __KERNEL__ */ + +/* + * VFIO driver - allow mapping and use of certain devices + * in unprivileged user processes. (If IOMMU is present) + * Especially useful for Virtual Function parts of SR-IOV devices + */ + + +/* Kernel & User level defines for ioctls */ + +#define VFIO_GROUP_GET_FLAGS _IOR(';', 100, __u64) + #define VFIO_GROUP_FLAGS_VIABLE (1 << 0) + #define VFIO_GROUP_FLAGS_MM_LOCKED (1 << 1) +#define VFIO_GROUP_MERGE _IOW(';', 101, int) +#define VFIO_GROUP_UNMERGE _IOW(';', 102, int) +#define VFIO_GROUP_GET_IOMMU_FD _IO(';', 103) +#define VFIO_GROUP_GET_DEVICE_FD _IOW(';', 104, char *) + +/* + * Structure for DMA mapping of user buffers + * vaddr, dmaaddr, and size must all be page aligned + */ +struct vfio_dma_map { + __u64 len; /* length of structure */ + __u64 vaddr; /* process virtual addr */ + __u64 dmaaddr; /* desired and/or returned dma address */ + __u64 size; /* size in bytes */ + __u64 flags; +#define VFIO_DMA_MAP_FLAG_WRITE (1 << 0) /* req writeable DMAmem */
include/linux/vfio.h new file +6
-0+}; + +#define VFIO_IOMMU_GET_FLAGS _IOR(';', 105, __u64) +/* Does the IOMMU support mapping any IOVA to any virtual address? */ +#define VFIO_IOMMU_FLAGS_MAP_ANY (1 << 0) +#define VFIO_IOMMU_MAP_DMA _IOWR(';', 106, struct
vfio_dma_map)
+#defineVFIO_IOMMU_UNMAP_DMA_IOWR(';', 107, structvfio_dma_map)include/linux/vfio.h new file +20
-0+ +#define VFIO_DEVICE_GET_FLAGS _IOR(';', 108, __u64) +#define VFIO_DEVICE_FLAGS_PCI (1 << 0) +#define VFIO_DEVICE_FLAGS_DT (1 << 1) +#define VFIO_DEVICE_FLAGS_RESET (1 << 2) +#define VFIO_DEVICE_GET_NUM_REGIONS _IOR(';', 109, int) + +struct vfio_region_info { +__u32 len;/* length of structure */ +__u32 index;/* region number */ +__u64 size;/* size in bytes of region */ +__u64 offset;/* start offset of region */ +__u64 flags; +#define VFIO_REGION_INFO_FLAG_MMAP (1 << 0) +#define VFIO_REGION_INFO_FLAG_RO (1 << 1) +#define VFIO_REGION_INFO_FLAG_PHYS_VALID (1 << 2) +__u64 phys;/* physical address of region */ +}; + +#define VFIO_DEVICE_GET_REGION_INFO _IOWR(';', 110, struct
vfio_region_info)include/linux/vfio.h new file +12
-0+ +#define VFIO_DEVICE_GET_NUM_IRQS _IOR(';', 111, int) + +struct vfio_irq_info { +__u32 len;/* length of structure */ +__u32 index;/* IRQ number */ +__u32 count;/* number of individual IRQs */ +__u32 flags; +#define VFIO_IRQ_INFO_FLAG_LEVEL (1 << 0) +}; + +#define VFIO_DEVICE_GET_IRQ_INFO _IOWR(';', 112, struct
vfio_irq_info)include/linux/vfio.h new file +2
-0+ +/* Set IRQ eventfds, arg[0] = index, arg[1] = count, arg[2-n] = eventfds */
include/linux/vfio.h new file +19
-0+#define VFIO_DEVICE_SET_IRQ_EVENTFDS _IOW(';', 113, int) + +/* Unmask IRQ index, arg[0] = index */ +#define VFIO_DEVICE_UNMASK_IRQ _IOW(';', 114, int) + +/* Set unmask eventfd, arg[0] = index, arg[1] = eventfd */ +#define VFIO_DEVICE_SET_UNMASK_IRQ_EVENTFD _IOW(';', 115, int) + +#define VFIO_DEVICE_RESET _IO(';', 116) + +struct vfio_dtpath { +__u32 len;/* length of structure */ +__u32 index; +__u64 flags; +#define VFIO_DTPATH_FLAGS_REGION (1 << 0) +#define VFIO_DTPATH_FLAGS_IRQ (1 << 1) +char *path; +}; +#define VFIO_DEVICE_GET_DTPATH _IOWR(';', 117, struct
vfio_dtpath)include/linux/vfio.h new file +11
-0+ +struct vfio_dtindex { +__u32 len;/* length of structure */ +__u32 index; +__u32 prop_type; +__u32 prop_index; +__u64 flags; +#define VFIO_DTINDEX_FLAGS_REGION (1 << 0) +#define VFIO_DTINDEX_FLAGS_IRQ (1 << 1) +}; +#define VFIO_DEVICE_GET_DTINDEX _IOWR(';', 118, struct
vfio_dtindex)
include/linux/vfio.h line 155
Quoted 2 lines
include/linux/vfio.h new file +2
-0+ +#endif /* VFIO_H */
/Chris
Quoted 3 lines · Christian Benvenuti (benve)
I have not gone through the all patch yet, but here are my first comments/questions about the code in
vfio_main.c(andpci/vfio_pci.c).
Thanks! Comments inline...
Quoted 84 lines
Quoted 10 lines
-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Thursday, November 03, 2011 1:12 PM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Christian Benvenuti (benve); Aaron Fabbri (aafabbri); [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected] foundation.org; [email protected]
Subject: [RFC PATCH] vfio: VFIO Driver core framework<snip>
Quoted 57 lines
drivers/vfio/vfio_main.c new file +7
-0@@ -0,0 +1,1151 @@ +/* +* VFIO framework +* +* Copyright (C) 2011 Red Hat, Inc. All rights reserved. +* Author: Alex Williamson <[email protected]> +* +* This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Derived from original vfio:
+ * Copyright 2010 Cisco Systems, Inc. All rights reserved.
+ * Author: Tom Lyon, [email protected]
+ */drivers/vfio/vfio_main.c new file +27
-0+ +#include <linux/cdev.h> +#include <linux/compat.h> +#include <linux/device.h> +#include <linux/file.h> +#include <linux/anon_inodes.h> +#include <linux/fs.h> +#include <linux/idr.h> +#include <linux/iommu.h> +#include <linux/mm.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/uaccess.h> +#include <linux/vfio.h> +#include <linux/wait.h> + +#include "vfio_private.h" + +#define DRIVER_VERSION "0.2" +#define DRIVER_AUTHOR "Alex Williamson <[email protected]>" +#define DRIVER_DESC "VFIO - User Level meta-driver" + +static int allow_unsafe_intrs; +module_param (allow_unsafe_intrs, int,0 ); +MODULE_PARM_DESC(allow_unsafe_intrs, + "Allow use of IOMMUs which do not support interruptremapping");
drivers/vfio/vfio_main.c new file +8
-0+ +static struct vfio { + dev_t devt; + struct cdev cdev; + struct list_head group_list; + struct mutex lock; + struct kref kref; + struct class *class;drivers/vfio/vfio_main.c line 59
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+struct idr idr; +wait_queue_head_t release_q; +} vfio; + +static const struct file_operations vfio_group_fops; +extern const struct file_operations vfio_iommu_fops; + +struct vfio_group { +dev_t devt; +unsigned int groupid;This groupid is returned by the
device_groupcallback you recently added with a separate (not yet in tree) IOMMU patch.
Is it correct to say that the scope of this ID is the bus the iommu
Quoted 8 lines
belongs too (but you use it as if it was global)?
I believe there is nothing right now to ensure the uniqueness of such ID across bus types (assuming there will be other bus drivers in the future besides vfio-pci).
If that's the case, the vfio.group_list global list and the__vfio_lookup_devroutine should be changed to account for the bus too?
Ops, I just saw the error msg invfio_group_add_devabout the group id conflict. Is that warning related to what I mentioned above?
Yeah, this is a concern, but I can't think of a system where we would manifest a collision. The IOMMU driver is expected to provide unique groupids for all devices below them, but we could imagine a system that implements two different bus_types, each with a different IOMMU driver and we have no coordination between them. Perhaps since we have iommu_ops per bus, we should also expose the bus in the vfio group path, ie. /dev/vfio/%s/%u, dev->bus->name, iommu_device_group(dev,..). This means userspace would need to do a readlink of the subsystem entry where it finds the iommu_group to find the vfio group. Reasonable?
Quoted 11 lines
Quoted 11 lines
drivers/vfio/vfio_main.c new file +11
-0+struct bus_type *bus; +struct vfio_iommu *iommu; +struct list_head device_list; +struct list_head iommu_next; +struct list_head group_next; +int refcnt; +}; + +struct vfio_device { +struct device *dev; +const struct vfio_device_ops *ops;
Quoted 8 lines
drivers/vfio/vfio_main.c
Quoted 1 line
drivers/vfio/vfio_main.c new file +1
-0+struct vfio_iommu *iommu;I wonder if you need to have the 'iommu' field here. vfio_device.iommu is always set and reset together with vfio_group.iommu.
Given that avfio_deviceinstance is always linked to avfio_groupinstance, do we need this duplication? Is this duplication there because you do not want the double dereferencedevice->group->iommu?
I think that was my initial goal in duplicating the pointer on the device. I believe I was also at one point passing a vfio_device around and needed the pointer. We seem to be getting along fine w/o that and I don't see any performance sensitive paths from getting from the device to iommu, so I'll see about removing it.
Quoted 84 lines
Quoted 51 lines
drivers/vfio/vfio_main.c new file +51
-0+struct vfio_group *group; +struct list_head device_next; +bool attached; +int refcnt; +void *device_data; +}; + +/* +* Helper functions called under vfio.lock +*/ + +/* Return true if any devices within a group are opened */ +static bool __vfio_group_devs_inuse (struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next); +if (device->refcnt) +return true; + } +return false; +} + +/* Return true if any of the groups attached to an iommu are opened. +* We can only tear apart merged groups when nothing is left open. */ +static bool __vfio_iommu_groups_inuse (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group, iommu_next); +if (group->refcnt) +return true; + } +return false; +} + +/* An iommu is "in use" if it has a file descriptor open or if any of +* the groups assigned to the iommu have devices open. */ +static bool __vfio_iommu_inuse (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (iommu->refcnt) +return true; +drivers/vfio/vfio_main.c line 132
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group, iommu_next); + +if (__vfio_group_devs_inuse (group)) +return true; + } +return false; +}I looked at how you take care of ref counts ...
This is how the tree of vfio_iommu/vfio_group/vfio_device data Structures is organized (I'll use just iommu/group/dev to make the graph smaller):
iommu / \ / \ group ... group / \ / \/ \ / \
dev .. dev dev .. devThis is how you get a file descriptor for the three kind of objects:
- group : open
/dev/vfio/xxxfor group xxx
- iommu : group ioctlVFIO_GROUP_GET_IOMMU_FD
- device: group ioctlVFIO_GROUP_GET_DEVICE_FDGiven the above topology, I would assume that:
Quoted 7 lines
(1) an iommu is 'inuse' if : a) iommu refcnt > 0, or
b) any of its groups is 'inuse'(2) a group is 'inuse' if : a) group refcnt > 0, or
b) any of its devices is 'inuse'(3) a device is 'inuse' if : a) device refcnt > 0
(2) is a bit debatable. I've wrestled with this one for a while. The vfio_iommu serves two purposes. First, it is the object we use for managing iommu domains, which includes allocating domains and attaching devices to domains. Groups objects aren't involved here, they just manage the set of devices. The second role is to manage merged groups, because whether or not groups can be merged is a function of iommu domain compatibility.
So if we look at "is the iommu in use?" ie. can I destroy the mapping context, detach devices and free the domain, the reference count on the group is irrelevant. The user has to have a device or iommu file descriptor opened somewhere, across the group or merged group, for that context to be maintained. A reasonable requirement, I think.
However, if we ask "is the group in use?" ie. can I not only destroy the mappings above, but also automatically tear apart merged groups, then I think we need to look at the group refcnt.
There's also a symmetry factor, the group is a benign entry point to device access. It's only when device or iommu access is granted that the group gains any real power. Therefore, shouldn't that power also be removed when those access points are closed?
Quoted 6 lines
You have coded the 'inuse' logic with these three routines:
__vfio_iommu_inuse, which implements (1) aboveand
__vfio_iommu_groups_inuse
Implements (2.a)
Quoted 1 line
__vfio_group_devs_inuse
Implements (2.b)
Quoted 2 lines
which are used by
__vfio_iommu_inuse.
Why don't you check the group refcnt in__vfio_iommu_groups_inuse?
Hopefully explained above, but open for discussion.
Quoted 4 lines
Would it make sense (and the code more readable) to structure the nested refcnt/inuse check like this?
(The numbers (1)(2)(3) refer to the three 'inuse' conditions above)
Quoted 13 lines
(1)__vfio_iommu_inuse
|
+-> check iommu refcnt
+-> __vfio_iommu_groups_inuse
|
+->LOOP: (2)__vfio_iommu_group_inuse<--MISSING
|
+-> check group refcnt<--MISSING
+-> __vfio_group_devs_inuse()
|
+-> LOOP: (3)__vfio_group_dev_inuse<--MISSING
|
+-> check device refcntWe currently do:
(1)__vfio_iommu_inuse
|
+-> check iommu refcnt
+-> __vfio_group_devs_inuse
|
+->LOOP: (2.b)__vfio_group_devs_inuse
|
+-> LOOP: (3) check device refcntIf that passes, the iommu context can be dissolved and we follow up with:
__vfio_iommu_groups_inuse
|
+-> LOOP: (2.a)__vfio_iommu_groups_inuse
|
+-> check group refcntIf that passes, groups can also be umerged.
Is this right?
Quoted 6 lines
Quoted 6 lines
drivers/vfio/vfio_main.c new file +6
-0+static void __vfio_group_set_iommu (struct vfio_group *group, +struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (group->iommu)
Quoted 8 lines
drivers/vfio/vfio_main.c line 144
Quoted 5 lines
drivers/vfio/vfio_main.c new file +5
-0+list_del (&group->iommu_next); +if (iommu) +list_add (&group->iommu_next, &iommu->group_list); + + group->iommu = iommu;If you remove the vfio_device.iommu field (as suggested above in a previous Comment), the block below would not be needed anymore.
Yep, I'll try removing that and see how it plays out.
Quoted 114 lines
Quoted 114 lines
drivers/vfio/vfio_main.c new file +70
-0+list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next); + device->iommu = iommu; + } +} + +static void __vfio_iommu_detach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +BUG_ON (!iommu->domain && device->attached); + +if (!iommu->domain || !device->attached) +return ; + +iommu_detach_device (iommu->domain, device->dev); + device->attached = false; +} + +static void __vfio_iommu_detach_group (struct vfio_iommu *iommu, +struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next); +__vfio_iommu_detach_dev (iommu, device); + } +} + +static int __vfio_iommu_attach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +int ret; + +BUG_ON (device->attached); + +if (!iommu || !iommu->domain) +return -EINVAL ; + + ret =iommu_attach_device (iommu->domain, device->dev); +if (!ret) + device->attached = true; + +return ret; +} + +static int __vfio_iommu_attach_group (struct vfio_iommu *iommu, +struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; +int ret; + + device = list_entry(pos, struct vfio_device, device_next); + ret =__vfio_iommu_attach_dev (iommu, device); +if (ret) { +__vfio_iommu_detach_group (iommu, group); +return ret; + } + } +return 0 ; +} + +/* The iommu is viable, ie. ready to be configured, when all the devices
+ * for all the groups attached to the iommu are bound to their vfio device
+ * drivers (ex. vfio-pci). This sets thedevice_dataprivate data pointer. */drivers/vfio/vfio_main.c new file +39
-0+static bool __vfio_iommu_viable (struct vfio_iommu *iommu) +{ +struct list_head *gpos, *dpos; + +list_for_each (gpos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(gpos, struct vfio_group, iommu_next); + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + device =list_entry (dpos, + struct vfio_device, device_next); + +if (!device->device_data) +return false; + } + } +return true; +} + +static void __vfio_close_iommu (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (!iommu->domain) +return ; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(pos, struct vfio_group, iommu_next); + +__vfio_iommu_detach_group (iommu, group); + } + +vfio_iommu_unmapall (iommu); + +iommu_domain_free (iommu->domain); + iommu->domain =NULL ; + iommu->mm =NULL ;
Quoted 8 lines
drivers/vfio/vfio_main.c line 260
Quoted 4 lines
drivers/vfio/vfio_main.c new file +4
-0+} + +/* Open the IOMMU. This gates all access to the iommu or device file +* descriptors and sets current->mm as the exclusive user. */ Given the fn
vfio_group_open(ie, 1st object, 2nd operation), I would have called this one__vfio_iommu_open(instead of__vfio_open_iommu).
Is it named__vfio_open_iommuto avoid a conflict with the namespace invfio_iommu.c?
I would have expected that too, I'll look at renaming these.
Quoted 252 lines
Quoted 240 lines
drivers/vfio/vfio_main.c new file +40
-0+static int __vfio_open_iommu (struct vfio_iommu *iommu) +{ +struct list_head *pos; +int ret; + +if (!__vfio_iommu_viable (iommu)) +return -EBUSY ; + +if (iommu->domain) +return -EINVAL ; + + iommu->domain =iommu_domain_alloc (iommu->bus); +if (!iommu->domain) +return -EFAULT ; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(pos, struct vfio_group, iommu_next); + + ret =__vfio_iommu_attach_group (iommu, group); +if (ret) { +__vfio_close_iommu (iommu); +return ret; + } + } + +if (!allow_unsafe_intrs && + !iommu_domain_has_cap (iommu->domain,IOMMU_CAP_INTR_REMAP )) { +__vfio_close_iommu (iommu); +return -EFAULT ; + } + + iommu->cache = (iommu_domain_has_cap (iommu->domain, +IOMMU_CAP_CACHE_COHERENCY ) !=0 ); + iommu->mm = current->mm; + +return 0 ; +} + +/* Actively try to tear down the iommu and merged groups. If there are no
+ * open iommu or device fds, we close the iommu. If we close the iommu and
+ * there are also no open group fds, we can futher dissolve the group to
+ * iommu association and free the iommu data structure. */drivers/vfio/vfio_main.c new file +15
-0+static int __vfio_try_dissolve_iommu (struct vfio_iommu *iommu) +{ + +if (__vfio_iommu_inuse (iommu)) +return -EBUSY ; + +__vfio_close_iommu (iommu); + +if (!__vfio_iommu_groups_inuse (iommu)) { +struct list_head *pos, *ppos; + +list_for_each_safe (pos, ppos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group,
iommu_next);drivers/vfio/vfio_main.c new file +64
-0+__vfio_group_set_iommu (group,NULL ); + } + + +kfree (iommu); + } + +return 0 ; +} + +static struct vfio_device *__vfio_lookup_dev (struct device *dev) +{ +struct list_head *gpos; +unsigned int groupid; + +if (iommu_device_group (dev, &groupid)) +return NULL ; + +list_for_each (gpos, &vfio.group_list) { +struct vfio_group *group; +struct list_head *dpos; + + group = list_entry(gpos, struct vfio_group, group_next); + +if (group->groupid != groupid) +continue ; + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + + device =list_entry (dpos, + struct vfio_device, device_next); + +if (device->dev == dev) +return device; + } + } +return NULL ; +} + +/* All release paths simply decrement the refcnt, attempt to teardown +* the iommu and merged groups, and wakeup anything that might be +* waiting if we successfully dissolve anything. */ +static int vfio_do_release (int *refcnt,struct vfio_iommu *iommu) +{ +bool wake; + +mutex_lock (&vfio.lock); + + (*refcnt)--; + wake = (__vfio_try_dissolve_iommu (iommu) ==0 ); + +mutex_unlock (&vfio.lock); + +if (wake) +wake_up (&vfio.release_q); + +return 0 ; +} + +/* +* Device fops - passthrough to vfio device driver w/ device_data +*/ +static int vfio_device_release(struct inode *inode, struct file*filep)
drivers/vfio/vfio_main.c new file +27
-0+{ +struct vfio_device *device = filep->private_data; + +vfio_do_release (&device->refcnt, device->iommu); + + device->ops->put (device->device_data); + +return 0 ; +} + +static long vfio_device_unl_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ +struct vfio_device *device = filep->private_data; + +return device->ops->ioctl (device->device_data, cmd, arg); +} + +static ssize_t vfio_device_read (struct file *filep,char __user *buf, +size_t count,loff_t *ppos) +{ +struct vfio_device *device = filep->private_data; + +return device->ops->read (device->device_data, buf, count, ppos); +} + +static ssize_t vfio_device_write(struct file *filep, const char __user*buf,
drivers/vfio/vfio_main.c new file +8
-0+ size_t count, loff_t *ppos) +{ + struct vfio_device *device = filep->private_data; + +return device->ops->write (device->device_data, buf, count, ppos); +} + +static int vfio_device_mmap(struct file *filep, struct vm_area_struct*vma)
drivers/vfio/vfio_main.c new file +76
-0+{ +struct vfio_device *device = filep->private_data; + +return device->ops->mmap (device->device_data, vma); +} + +#ifdef CONFIG_COMPAT +static long vfio_device_compat_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ + arg = (unsigned long )compat_ptr (arg); +return vfio_device_unl_ioctl (filep, cmd, arg); +} +#endif /* CONFIG_COMPAT */ + +const struct file_operations vfio_device_fops = { + .owner =THIS_MODULE , + .release = vfio_device_release, + .read = vfio_device_read, + .write = vfio_device_write, + .unlocked_ioctl = vfio_device_unl_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = vfio_device_compat_ioctl, +#endif + .mmap = vfio_device_mmap, +}; + +/* +* Group fops +*/ +static int vfio_group_open (struct inode *inode,struct file *filep) +{ +struct vfio_group *group; +int ret =0 ; + +mutex_lock (&vfio.lock); + + group =idr_find (&vfio.idr,iminor (inode)); + +if (!group) { + ret = -ENODEV ; + goto out; + } + + filep->private_data = group; + +if (!group->iommu) { +struct vfio_iommu *iommu; + + iommu =kzalloc (sizeof (*iommu),GFP_KERNEL ); +if (!iommu) { + ret = -ENOMEM ; + goto out; + } +INIT_LIST_HEAD (&iommu->group_list); +INIT_LIST_HEAD (&iommu->dm_list); +mutex_init (&iommu->dgate); + iommu->bus = group->bus; +__vfio_group_set_iommu (group, iommu); + } + group->refcnt++; + +out: +mutex_unlock (&vfio.lock); + +return ret; +} + +static int vfio_group_release (struct inode *inode,struct file *filep) +{ +struct vfio_group *group = filep->private_data; + +return vfio_do_release (&group->refcnt, group->iommu); +} + +/* Attempt to merge the group pointed to by fd into group. The merge- Quoted 5 lines
ee
+ * group must not have an iommu or any devices open because we cannot
+ * maintain that context across the merge. The merge-er group can be
+ * in use. */
+static intvfio_group_merge(struct vfio_group *group, int fd)The documentation in vfio.txt explains clearly the logic implemented by the merge/unmerge group ioctls.
However, what you are doing is not merging groups, but rather adding/removing groups to/from iommus (and creating flat lists of groups).
For example, when you do
Quoted 4 lines
merge(A,B)you actually mean to say "merge B to the list of groups assigned to the same iommu as group A".
It's actually a little more than that. After you've merged B into A, you can close the file descriptor for B and access all of the devices for the merged group from A.
Quoted 8 lines
For the same reason, you do not really need to provide the group you want to unmerge from, which means that instead of
unmerge(A,B)you would just need
unmerge(B)
Good point, we can avoid the awkward reference via file descriptor for the unmerge.
Quoted 2 lines
I understand the reason why it is not a real merge/unmerge (ie, to keep the original groups so that you can unmerge later)
Right, we still need to have visibility of the groups comprising the merged group, but the abstraction provided to the user seems to be deeper than you're thinking.
Quoted 6 lines
... however I just wonder if
it wouldn't be more natural to implement the VFIO_IOMMU_ADD_GROUP/DEL_GROUP iommu ioctls instead? (the relationships between the data structure would remain the same)
I guess you already discarded this option for some reasons, right? What was the reason?
It's a possibility, I'm not sure it was discussed or really what advantage it provides. It seems like we'd logically lose the ability to access devices from other groups, whether that's good or bad, I don't know. I think the notion of "merge" promotes the idea that the groups are peers and an iommu_add/del feels a bit more hierarchical.
Quoted 311 lines
Quoted 311 lines
drivers/vfio/vfio_main.c new file +65
-0+{ +struct vfio_group *new; +struct vfio_iommu *old_iommu; +struct file *file; +int ret =0 ; +bool opened = false; + +mutex_lock (&vfio.lock); + + file =fget (fd); +if (!file) { + ret = -EBADF ; + goto out_noput; + } + +/* Sanity check, is this really our fd? */ +if (file->f_op != &vfio_group_fops) { + ret = -EINVAL ; + goto out; + } + + new = file->private_data; + +if (!new || new == group || !new->iommu || + new->iommu->domain || new->bus != group->bus) { + ret = -EINVAL ; + goto out; + } + +/* We need to attach all the devices to each domain separately +* in order to validate that the capabilities match for both. */ + ret =__vfio_open_iommu (new->iommu); +if (ret) + goto out; + +if (!group->iommu->domain) { + ret =__vfio_open_iommu (group->iommu); +if (ret) + goto out; + opened = true; + } + +/* If cache coherency doesn't match we'd potentialy need to +* remap existing iommu mappings in the merge-er domain. +* Poor return to bother trying to allow this currently. */ +if (iommu_domain_has_cap (group->iommu->domain, +IOMMU_CAP_CACHE_COHERENCY ) != +iommu_domain_has_cap (new->iommu->domain, +IOMMU_CAP_CACHE_COHERENCY )) { +__vfio_close_iommu (new->iommu); +if (opened) +__vfio_close_iommu (group->iommu); + ret = -EINVAL ; + goto out; + } + +/* Close the iommu for the merge-ee and attach all its devices +* to the merge-er iommu. */ +__vfio_close_iommu (new->iommu); + + ret =__vfio_iommu_attach_group (group->iommu, new); +if (ret) + goto out; + +/* set_iommu unlinks new from the iommu, so save a pointer to it */
drivers/vfio/vfio_main.c new file +94
-0+ old_iommu = new->iommu; +__vfio_group_set_iommu (new, group->iommu); +kfree (old_iommu); + +out: +fput (file); +out_noput: +mutex_unlock (&vfio.lock); +return ret; +} + +/* Unmerge the group pointed to by fd from group. */ +static int vfio_group_unmerge (struct vfio_group *group,int fd) +{ +struct vfio_group *new; +struct vfio_iommu *new_iommu; +struct file *file; +int ret =0 ; + +/* Since the merge-out group is already opened, it needs to +* have an iommu struct associated with it. */ + new_iommu =kzalloc (sizeof (*new_iommu),GFP_KERNEL ); +if (!new_iommu) +return -ENOMEM ; + +INIT_LIST_HEAD (&new_iommu->group_list); +INIT_LIST_HEAD (&new_iommu->dm_list); +mutex_init (&new_iommu->dgate); + new_iommu->bus = group->bus; + +mutex_lock (&vfio.lock); + + file =fget (fd); +if (!file) { + ret = -EBADF ; + goto out_noput; + } + +/* Sanity check, is this really our fd? */ +if (file->f_op != &vfio_group_fops) { + ret = -EINVAL ; + goto out; + } + + new = file->private_data; +if (!new || new == group || new->iommu != group->iommu) { + ret = -EINVAL ; + goto out; + } + +/* We can't merge-out a group with devices still in use. */ +if (__vfio_group_devs_inuse (new)) { + ret = -EBUSY ; + goto out; + } + +__vfio_iommu_detach_group (group->iommu, new); +__vfio_group_set_iommu (new, new_iommu); + +out: +fput (file); +out_noput: +if (ret) +kfree (new_iommu); +mutex_unlock (&vfio.lock); +return ret; +} + +/* Get a new iommu file descriptor. This will open the iommu, setting +* the current->mm ownership if it's not already set. */ +static int vfio_group_get_iommu_fd (struct vfio_group *group) +{ +int ret =0 ; + +mutex_lock (&vfio.lock); + +if (!group->iommu->domain) { + ret =__vfio_open_iommu (group->iommu); +if (ret) + goto out; + } + + ret =anon_inode_getfd ("[vfio-iommu]" , &vfio_iommu_fops, + group->iommu,O_RDWR ); +if (ret <0 ) + goto out; + + group->iommu->refcnt++; +out: +mutex_unlock (&vfio.lock); +return ret; +} + +/* Get a new device file descriptor. This will open the iommu, setting
+ * thecurrent->mmownership if it's not already set. It's difficult to
+ * specify the requirements for matching a user supplied buffer to a
+ * device, so we use a vfio driver callback to test for a match. For
+ * PCI,dev_name(dev)is unique, but other drivers may require including
+ * a parent device string. */
+static intvfio_group_get_device_fd(struct vfio_group *group, char *buf)drivers/vfio/vfio_main.c new file +141
-0+{ +struct vfio_iommu *iommu = group->iommu; +struct list_head *gpos; +int ret = -ENODEV ; + +mutex_lock (&vfio.lock); + +if (!iommu->domain) { + ret =__vfio_open_iommu (iommu); +if (ret) + goto out; + } + +list_for_each (gpos, &iommu->group_list) { +struct list_head *dpos; + + group = list_entry(gpos, struct vfio_group, iommu_next); + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + + device =list_entry (dpos, + struct vfio_device, device_next); + +if (device->ops->match (device->dev, buf)) { +struct file *file; + +if (device->ops->get (device->device_data)) { + ret = -EFAULT ; + goto out; + } + +/* We can't use anon_inode_getfd(), like above +* because we need to modify the f_mode flags +* directly to allow more than just ioctls */ + ret =get_unused_fd (); +if (ret <0 ) { + device->ops->put (device->device_data); + goto out; + } + + file =anon_inode_getfile ("[vfio-device]" , + &vfio_device_fops, + device,O_RDWR ); +if (IS_ERR (file)) { +put_unused_fd (ret); + ret =PTR_ERR (file); + device->ops->put (device->device_data); + goto out; + } + +/* Todo: add an anon_inode interface to do +* this. Appears to be missing by lack of +* need rather than explicitly prevented. +* Now there's need. */ + file->f_mode |= (FMODE_LSEEK | +FMODE_PREAD | +FMODE_PWRITE ); + +fd_install (ret, file); + + device->refcnt++; + goto out; + } + } + } +out: +mutex_unlock (&vfio.lock); +return ret; +} + +static long vfio_group_unl_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ +struct vfio_group *group = filep->private_data; + +if (cmd ==VFIO_GROUP_GET_FLAGS ) { +u64 flags =0 ; + +mutex_lock (&vfio.lock); +if (__vfio_iommu_viable (group->iommu)) + flags |=VFIO_GROUP_FLAGS_VIABLE ; +mutex_unlock (&vfio.lock); + +if (group->iommu->mm) + flags |=VFIO_GROUP_FLAGS_MM_LOCKED ; + +return put_user (flags, (u64 __user *)arg); + } + +/* Below commands are restricted once the mm is set */ +if (group->iommu->mm && group->iommu->mm != current->mm) +return -EPERM ; +if (cmd ==VFIO_GROUP_MERGE || cmd ==VFIO_GROUP_UNMERGE ) { +int fd; + +if (get_user (fd, (int __user *)arg)) +return -EFAULT ; +if (fd <0 ) +return -EINVAL ; + +if (cmd ==VFIO_GROUP_MERGE ) +return vfio_group_merge (group, fd); +else +return vfio_group_unmerge (group, fd); + }else if (cmd ==VFIO_GROUP_GET_IOMMU_FD ) { +return vfio_group_get_iommu_fd (group); + }else if (cmd ==VFIO_GROUP_GET_DEVICE_FD ) { +char *buf; +int ret; + + buf =strndup_user ((const char __user *)arg,PAGE_SIZE ); +if (IS_ERR (buf)) +return PTR_ERR (buf); + + ret =vfio_group_get_device_fd (group, buf); +kfree (buf); +return ret; + } + +return -ENOSYS ; +} + +#ifdef CONFIG_COMPAT +static long vfio_group_compat_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ + arg = (unsigned long )compat_ptr (arg); +return vfio_group_unl_ioctl (filep, cmd, arg); +} +#endif /* CONFIG_COMPAT */ + +static const struct file_operations vfio_group_fops = { + .owner =THIS_MODULE , + .open = vfio_group_open, + .release = vfio_group_release, + .unlocked_ioctl = vfio_group_unl_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = vfio_group_compat_ioctl, +#endif +};
Quoted 8 lines
drivers/vfio/vfio_main.c line 806
Quoted 2 lines
drivers/vfio/vfio_main.c new file +2
-0+ +/* iommu fd release hook */ Given
vfio_device_releaseand
vfio_group_release(ie, 1st object, 2nd operation), I was
going to suggest renaming the fn below tovfio_iommu_release, but then I saw the latter name being already used invfio_iommu.c... a bit confusing but I guess it's ok then.
Right, this one was definitely because of naming collision.
Quoted 94 lines
Quoted 94 lines
drivers/vfio/vfio_main.c new file +11
-0+int vfio_release_iommu (struct vfio_iommu *iommu) +{ +return vfio_do_release (&iommu->refcnt, iommu); +} + +/* +* VFIO driver API +*/ + +/* Add a new device to the vfio framework with associated vfio driver +* callbacks. This is the entry point for vfio drivers to register devices. */
+intvfio_group_add_dev(struct device *dev, const struct vfio_device_ops *ops)drivers/vfio/vfio_main.c new file +59
-0+{ + struct list_head *pos; + struct vfio_group *group = NULL; + struct vfio_device *device = NULL; + unsigned int groupid; + int ret = 0; + bool new_group = false; + + if (!ops) + return -EINVAL; + + if (iommu_device_group(dev, &groupid)) + return -ENODEV; + + mutex_lock(&vfio.lock); + + list_for_each(pos, &vfio.group_list) { + group = list_entry(pos, struct vfio_group, group_next); + if (group->groupid == groupid) + break; + group = NULL; + } + + if (!group) { + int minor; + + if (unlikely(idr_pre_get(&vfio.idr, GFP_KERNEL) == 0)) { + ret = -ENOMEM; + goto out; + } + + group = kzalloc(sizeof(*group), GFP_KERNEL); + if (!group) { + ret = -ENOMEM; + goto out; + } + + group->groupid = groupid; + INIT_LIST_HEAD(&group->device_list); + + ret = idr_get_new(&vfio.idr, group, &minor); + if (ret == 0 && minor > MINORMASK) { + idr_remove(&vfio.idr, minor); + kfree(group); + ret = -ENOSPC; + goto out; + } + + group->devt = MKDEV(MAJOR(vfio.devt), minor); + device_create(vfio.class, NULL, group->devt, + group, "%u", groupid); + + group->bus = dev->bus; + list_add(&group->group_next, &vfio.group_list); + new_group = true; + } else { + if (group->bus != dev->bus) { + printk(KERN_WARNING + "Error: IOMMU group ID conflict. Group ID %u"
drivers/vfio/vfio_main.c new file +20
-0+"on both bus %s and %s\n" , groupid, + group->bus->name, dev->bus->name); + ret = -EFAULT ; + goto out; + } + +list_for_each (pos, &group->device_list) { + device =list_entry (pos, + struct vfio_device, device_next); +if (device->dev == dev) +break ; + device =NULL ; + } + } + + if (!device) { + if (__vfio_group_devs_inuse(group) || + (group->iommu && group->iommu->refcnt)) { + printk(KERN_WARNING + "Adding device %s to group %u while group is
Quoted 8 lines
drivers/vfio/vfio_main.c line 899
Quoted 3 lines
already in use!!\n",
drivers/vfio/vfio_main.c new file +2
-0+ dev_name(dev), group->groupid); +/* XXX How to prevent other drivers from claiming? */ Here we are adding a device (not yet assigned to a vfio bus) to a group that is already in use.
Given that it would not be acceptable for this device to get assigned to a non vfio driver, why not forcing such assignment here then?
Exactly, I just don't know the mechanics of how to make that happen and was hoping for suggestions...
Quoted 9 lines
I am not sure though what the best way to do it would be. What about something like this:
- when the bus vfio-pci processes the
BUS_NOTIFY_ADD_DEVICEnotification it assigns to the device a PCI ID that will make sure the vfio-pci's probe routine will be invoked (and no other driver can therefore claim the device). That PCI ID would have to be added to the vfio_pci_driver'sid_table(it would be the exception to the "only dynamic IDs" rule). Too hackish?
Presumably some other driver also has the ID in it's id_table, how do we make sure we win?
Quoted 13 lines
Quoted 13 lines
drivers/vfio/vfio_main.c new file +13
-0+ } + + device =kzalloc (sizeof (*device),GFP_KERNEL ); +if (!device) { +/* If we just created this group, tear it down */ +if (new_group) { +list_del (&group->group_next); +device_destroy (vfio.class, group->devt); +idr_remove (&vfio.idr,MINOR (group->devt)); +kfree (group); + } + ret = -ENOMEM ; + goto out;
Quoted 8 lines
drivers/vfio/vfio_main.c line 918
Quoted 6 lines
drivers/vfio/vfio_main.c new file +6
-0+ } + +list_add (&device->device_next, &group->device_list); + device->dev = dev; + device->ops = ops; + device->iommu = group->iommu;/* NULL if new */ Shouldn't you check the return code of
__vfio_iommu_attach_dev?
Yep, looks like I did this because the expected use case has a NULL iommu here, so I need to distiguish that error from an actual iommu_attach_device() error.
Quoted 10 lines
drivers/vfio/vfio_main.c line 927
Quoted 9 lines
drivers/vfio/vfio_main.c new file +9
-0+__vfio_iommu_attach_dev (group->iommu, device); + } +out: +mutex_unlock (&vfio.lock); +return ret; +} +EXPORT_SYMBOL_GPL (vfio_group_add_dev); + +/* Remove a device from the vfio framework */
Quoted 8 lines
This fn below does not return any error code. Ok ...
However, there are a number of errors case that you test, for example
- device that does not belong to any group (according to iommu API)
- device that belongs to a group but that does not appear in the list of devices of thevfio_groupstructure.
Are the above two errors checks just paranoia or are those errors actually possible? If they were possible, shouldn't we generate a warning (most probably it would be a bug in the code)?
They're all vfio-bus driver bugs of some sort, so it's just a matter of how much we want to scream about them. I'll comments on each below.
Quoted 9 lines
drivers/vfio/vfio_main.c line 936
Quoted 9 lines
drivers/vfio/vfio_main.c new file +9
-0+void vfio_group_del_dev (struct device *dev) +{ +struct list_head *pos; +struct vfio_group *group =NULL ; +struct vfio_device *device =NULL ; +unsigned int groupid; + +if (iommu_device_group (dev, &groupid)) +return ;
Here the bus driver is probably just sitting on a notifier list for their bus_type and a device is getting removed. Unless we want to require the bus driver to track everything it's attempted to add and whether it worked, we can just ignore this.
Quoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_main.c new file +4
-0+ +mutex_lock (&vfio.lock); + +list_for_each (pos, &vfio.group_list) {
Quoted 8 lines
drivers/vfio/vfio_main.c line 948
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+ group = list_entry(pos, struct vfio_group, group_next); +if (group->groupid == groupid) +break ; + group =NULL ; + } + +if (!group) + goto out;
We don't even have a group for the device, we could BUG_ON here. The bus driver failed to tell us about something that was then removed.
Quoted 10 lines
drivers/vfio/vfio_main.c line 958
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+ +list_for_each (pos, &group->device_list) { + device = list_entry(pos, struct vfio_device, device_next); +if (device->dev == dev) +break ; + device =NULL ; + } + +if (!device) + goto out;
Same here.
Quoted 87 lines
Quoted 87 lines
drivers/vfio/vfio_main.c new file +33
-0+ +BUG_ON (device->refcnt); + +if (device->attached) +__vfio_iommu_detach_dev (group->iommu, device); + +list_del (&device->device_next); +kfree (device); + +/* If this was the only device in the group, remove the group. +* Note that we intentionally unmerge empty groups here if the +* group fd isn't opened. */ +if (list_empty (&group->device_list) && group->refcnt ==0 ) { +struct vfio_iommu *iommu = group->iommu; + +if (iommu) { +__vfio_group_set_iommu (group,NULL ); +__vfio_try_dissolve_iommu (iommu); + } + +device_destroy (vfio.class, group->devt); +idr_remove (&vfio.idr,MINOR (group->devt)); +list_del (&group->group_next); +kfree (group); + } +out: +mutex_unlock (&vfio.lock); +} +EXPORT_SYMBOL_GPL (vfio_group_del_dev); + +/* When a device is bound to a vfio device driver (ex. vfio-pci), this +* entry point is used to mark the device usable (viable). The vfio +* device driver associates a private device_data struct with the device
+ * here, which will later be return forvfio_device_fopscallbacks. */drivers/vfio/vfio_main.c new file +23
-0+int vfio_bind_dev (struct device *dev,void *device_data) +{ +struct vfio_device *device; +int ret = -EINVAL ; + +BUG_ON (!device_data); + +mutex_lock (&vfio.lock); + + device =__vfio_lookup_dev (dev); + +BUG_ON (!device); + + ret =dev_set_drvdata (dev, device); +if (!ret) + device->device_data = device_data; + +mutex_unlock (&vfio.lock); +return ret; +} +EXPORT_SYMBOL_GPL (vfio_bind_dev); + +/* A device is only removeable if the iommu for the group is not in use. */
drivers/vfio/vfio_main.c new file +14
-0+static bool vfio_device_removeable (struct vfio_device *device) +{ +bool ret = true; + +mutex_lock (&vfio.lock); + +if (device->iommu &&__vfio_iommu_inuse (device->iommu)) + ret = false; + +mutex_unlock (&vfio.lock); +return ret; +} + +/* Notify vfio that a device is being unbound from the vfio device driver
+ * and return the device privatedevice_datapointer. If the group is
+ * in use, we need to block or take other measures to make it safe for
+ * the device to be removed from the iommu. */drivers/vfio/vfio_main.c new file +10
-0+void *vfio_unbind_dev (struct device *dev) +{ +struct vfio_device *device =dev_get_drvdata (dev); +void *device_data; + +BUG_ON (!device); + +again: +if (!vfio_device_removeable (device)) { +/* XXX signal for all devices in group to be removed or
Quoted 8 lines
drivers/vfio/vfio_main.c line 1045
Quoted 3 lines
drivers/vfio/vfio_main.c new file +3
-0+* resort to killing the process holding the device fds. +* For now just block waiting for releases to wake us. */ +wait_event (vfio.release_q,vfio_device_removeable (device));Any new idea/proposal on how to handle this situation?
The last one I remember was to leave the soft/hard/etc timeout handling in userspace and implement it as a sort of policy. Is that one still the most likely candidate solution to handle this situation?
I haven't heard any new proposals. I think we need the hard timeout handling in the kernel. We can't leave it to userspace to decide they get to keep the device. We could have this tunable via an ioctl, but I don't see how we wouldn't require CAP_SYS_ADMIN (or similar) to tweak it. I was intending to re-implement the netlink interface to signal the removal, but expect to get allergic reactions to that.
Thanks for the comments!
Alex
Comments inline...
Quoted 116 lines
-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Wednesday, November 09, 2011 10:03 AM
To: Christian Benvenuti (benve)
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Aaron Fabbri (aafabbri); [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [RFC PATCH] vfio: VFIO Driver core frameworkQuoted 3 lines · Christian Benvenuti (benve)
I have not gone through the all patch yet, but here are my first comments/questions about the code in
vfio_main.c(andpci/vfio_pci.c).Thanks! Comments inline...
Quoted 13 lines
Quoted 10 lines
-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Thursday, November 03, 2011 1:12 PM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Christian Benvenuti (benve); Aaron Fabbri (aafabbri); [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected] foundation.org; [email protected]
Subject: [RFC PATCH] vfio: VFIO Driver core framework<snip>
Quoted 15 lines
Quoted 13 lines
drivers/vfio/vfio_main.c new file +7
-0@@ -0,0 +1,1151 @@ +/* +* VFIO framework +* +* Copyright (C) 2011 Red Hat, Inc. All rights reserved. +* Author: Alex Williamson <[email protected]> +* +* This program is free software; you can redistribute it and/or Quoted 2 lines
modify
+ * it under the terms of the GNU General Public License version 2as
Quoted 19 lines
Quoted 19 lines
+ * published by the Free Software Foundation.
+ *
+ * Derived from original vfio:
+ * Copyright 2010 Cisco Systems, Inc. All rights reserved.
+ * Author: Tom Lyon, [email protected]
+ */drivers/vfio/vfio_main.c new file +13
-0+ +#include <linux/cdev.h> +#include <linux/compat.h> +#include <linux/device.h> +#include <linux/file.h> +#include <linux/anon_inodes.h> +#include <linux/fs.h> +#include <linux/idr.h> +#include <linux/iommu.h> +#include <linux/mm.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/string.h> Quoted 8 lines
drivers/vfio/vfio_main.c
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+#include <linux/uaccess.h> +#include <linux/vfio.h> +#include <linux/wait.h> + +#include "vfio_private.h" + +#define DRIVER_VERSION "0.2" +#define DRIVER_AUTHOR "Alex WilliamsonQuoted 19 lines
Quoted 19 lines
drivers/vfio/vfio_main.c new file +6
-0+#define DRIVER_DESC "VFIO - User Level meta-driver" + +static int allow_unsafe_intrs; +module_param (allow_unsafe_intrs, int,0 ); +MODULE_PARM_DESC(allow_unsafe_intrs, + "Allow use of IOMMUs which do not support interruptremapping");
drivers/vfio/vfio_main.c new file +12
-0+ +static struct vfio { +dev_t devt; +struct cdev cdev; +struct list_head group_list; +struct mutex lock; +struct kref kref; +struct class *class; +struct idr idr; +wait_queue_head_t release_q; +} vfio; +Quoted 8 lines
drivers/vfio/vfio_main.c line 59
Quoted 6 lines
drivers/vfio/vfio_main.c new file +6
-0+static const struct file_operations vfio_group_fops; +extern const struct file_operations vfio_iommu_fops; + +struct vfio_group { +dev_t devt; +unsigned int groupid;This groupid is returned by the
device_groupcallback you recentlyadded
Quoted 7 lines
with a separate (not yet in tree) IOMMU patch.
Is it correct to say that the scope of this ID is the bus the iommu belongs too (but you use it as if it was global)?
I believe there is nothing right now to ensure the uniqueness of such ID across bus types (assuming there will be other bus drivers in the future besides vfio-pci).
If that's the case, the vfio.group_list global list and the
__vfio_lookup_devQuoted 2 lines
routine should be changed to account for the bus too? Ops, I just saw the error msg in
vfio_group_add_devabout the groupid conflict.
Quoted 1 line
Is that warning related to what I mentioned above?
Quoted 11 lines
Yeah, this is a concern, but I can't think of a system where we would manifest a collision. The IOMMU driver is expected to provide unique groupids for all devices below them, but we could imagine a system that implements two different
bus_types, each with a different IOMMU driver and we have no coordination between them. Perhaps since we haveiommu_opsper bus, we should also expose the bus in the vfio group path,
ie. /dev/vfio/%s/%u,dev->bus->name,iommu_device_group(dev,..). This means userspace would need to do a readlink of the subsystem entry where
it finds theiommu_groupto find the vfio group. Reasonable?
Most probably we won't see use cases with multiple buses anytime soon, but this scheme you proposed (with the per-bus subdir) looks good to me.
Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
drivers/vfio/vfio_main.c new file +11
-0+struct bus_type *bus; +struct vfio_iommu *iommu; +struct list_head device_list; +struct list_head iommu_next; +struct list_head group_next; +int refcnt; +}; + +struct vfio_device { +struct device *dev; +const struct vfio_device_ops *ops;Quoted 8 lines
drivers/vfio/vfio_main.c
Quoted 1 line
drivers/vfio/vfio_main.c new file +1
-0+struct vfio_iommu *iommu;I wonder if you need to have the 'iommu' field here. vfio_device.iommu is always set and reset together with vfio_group.iommu.
Given that avfio_deviceinstance is always linked to avfio_groupinstance, do we need this duplication? Is this duplication there because you do not want the double dereferencedevice->group->iommu?
Quoted 6 lines
I think that was my initial goal in duplicating the pointer on the device. I believe I was also at one point passing a
vfio_devicearound and needed the pointer. We seem to be getting along fine w/o that and I
don't see any performance sensitive paths from getting from the device to iommu, so I'll see about removing it.
I guess you can add it back later if there will be need for it.
Right now, since you always init/deinit both at the same time, this would simplify the code and make it more unlikely to use an out-of-sync pointer.
Quoted 103 lines
Quoted 19 lines
Quoted 19 lines
drivers/vfio/vfio_main.c new file +19
-0+struct vfio_group *group; +struct list_head device_next; +bool attached; +int refcnt; +void *device_data; +}; + +/* +* Helper functions called under vfio.lock +*/ + +/* Return true if any devices within a group are opened */ +static bool __vfio_group_devs_inuse (struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; +Quoted 8 lines
drivers/vfio/vfio_main.c
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+ device = list_entry(pos, struct vfio_device, device_next); +if (device->refcnt) +return true; + } +return false; +} + +/* Return true if any of the groups attached to an iommu are opened.
Quoted 1 line
Quoted 1 line
+ * We can only tear apart merged groups when nothing is left open.
*/
Quoted 7 lines
Quoted 7 lines
drivers/vfio/vfio_main.c new file +7
-0+static bool __vfio_iommu_groups_inuse (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; +Quoted 8 lines
drivers/vfio/vfio_main.c
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+ group = list_entry(pos, struct vfio_group, iommu_next); +if (group->refcnt) +return true; + } +return false; +} + +/* An iommu is "in use" if it has a file descriptor open or if any of
Quoted 41 lines
Quoted 8 lines
+ * the groups assigned to the iommu have devices open. */
drivers/vfio/vfio_main.c new file +7
-0+static bool __vfio_iommu_inuse (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (iommu->refcnt) +return true; +drivers/vfio/vfio_main.c line 132
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group, iommu_next); + +if (__vfio_group_devs_inuse (group)) +return true; + } +return false; +}I looked at how you take care of ref counts ...
This is how the tree of vfio_iommu/vfio_group/vfio_device data Structures is organized (I'll use just iommu/group/dev to make the graph smaller):
iommu / \ / \ group ... group / \ / \/ \ / \
dev .. dev dev .. devThis is how you get a file descriptor for the three kind of objects:
- group : open
/dev/vfio/xxxfor group xxx
- iommu : group ioctlVFIO_GROUP_GET_IOMMU_FD
- device: group ioctlVFIO_GROUP_GET_DEVICE_FDGiven the above topology, I would assume that:
Quoted 7 lines
(1) an iommu is 'inuse' if : a) iommu refcnt > 0, or
b) any of its groups is 'inuse'(2) a group is 'inuse' if : a) group refcnt > 0, or
b) any of its devices is 'inuse'(3) a device is 'inuse' if : a) device refcnt > 0
(2) is a bit debatable. I've wrestled with this one for a while. The
vfio_iommuserves two purposes. First, it is the object we use for managing iommu domains, which includes allocating domains and attaching devices to domains. Groups objects aren't involved here, they just manage the set of devices. The second role is to manage merged groups, because whether or not groups can be merged is a function of iommu domain compatibility.
Quoted 5 lines
So if we look at "is the iommu in use?" ie. can I destroy the mapping context, detach devices and free the domain, the reference count on the group is irrelevant. The user has to have a device or iommu file descriptor opened somewhere, across the group or merged group, for that context to be maintained. A reasonable requirement, I think.
OK, then if you close all devices and the iommu, keeping the group open Would not protect the iommu domain mapping. This means that if you (or A management application) need to close all devices+iommu and reopen right away again the same devices+iommu you may get a failure on the iommu domain creation (supposing the system goes out of resources). Is this just a very unlikely scenario?
I guess in this case you would simply have to avoid releasing the iommu fd, right?
Quoted 4 lines
However, if we ask "is the group in use?" ie. can I not only destroy the
mappings above, but also automatically tear apart merged groups, then I think we need to look at the group refcnt.
Correct.
Quoted 6 lines
There's also a symmetry factor, the group is a benign entry point to device access. It's only when device or iommu access is granted that the group gains any real power. Therefore, shouldn't that power also be
removed when those access points are closed?
Quoted 8 lines
Quoted 6 lines
You have coded the 'inuse' logic with these three routines:
__vfio_iommu_inuse, which implements (1) aboveand
__vfio_iommu_groups_inuseImplements (2.a)
Yes, but for al groups at once.
Quoted 3 lines
Quoted 1 line
__vfio_group_devs_inuseImplements (2.b)
Yes
Quoted 43 lines
Quoted 2 lines
which are used by
__vfio_iommu_inuse.
Why don't you check the group refcnt in__vfio_iommu_groups_inuse?Hopefully explained above, but open for discussion.
Quoted 4 lines
Would it make sense (and the code more readable) to structure the nested refcnt/inuse check like this?
(The numbers (1)(2)(3) refer to the three 'inuse' conditions above)Quoted 13 lines
(1)__vfio_iommu_inuse | +-> check iommu refcnt +-> __vfio_iommu_groups_inuse | +->LOOP: (2)__vfio_iommu_group_inuse<--MISSING | +-> check group refcnt<--MISSING +-> __vfio_group_devs_inuse() | +-> LOOP: (3)__vfio_group_dev_inuse<--MISSING | +-> check device refcntWe currently do:
(1)__vfio_iommu_inuse | +-> check iommu refcnt +-> __vfio_group_devs_inuse | +->LOOP: (2.b)__vfio_group_devs_inuse | +-> LOOP: (3) check device refcntIf that passes, the iommu context can be dissolved and we follow up with:
__vfio_iommu_groups_inuse | +-> LOOP: (2.a)__vfio_iommu_groups_inuse | +-> check group refcnt
Quoted 3 lines
If that passes, groups can also be umerged.
Is this right?
Yes, assuming we stick to the "benign" role of groups you described above.
Quoted 414 lines
Quoted 5 lines
Quoted 5 lines
drivers/vfio/vfio_main.c new file +5
-0+static void __vfio_group_set_iommu (struct vfio_group *group, +struct vfio_iommu *iommu) +{ +struct list_head *pos; +Quoted 8 lines
drivers/vfio/vfio_main.c line 144
Quoted 6 lines
drivers/vfio/vfio_main.c new file +6
-0+if (group->iommu) +list_del (&group->iommu_next); +if (iommu) +list_add (&group->iommu_next, &iommu->group_list); + + group->iommu = iommu;If you remove the vfio_device.iommu field (as suggested above in a
previous
Quoted 1 line
Comment), the block below would not be needed anymore.
Yep, I'll try removing that and see how it plays out.
Quoted 64 lines
Quoted 64 lines
drivers/vfio/vfio_main.c new file +64
-0+list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next); + device->iommu = iommu; + } +} + +static void __vfio_iommu_detach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +BUG_ON (!iommu->domain && device->attached); + +if (!iommu->domain || !device->attached) +return ; + +iommu_detach_device (iommu->domain, device->dev); + device->attached = false; +} + +static void __vfio_iommu_detach_group (struct vfio_iommu *iommu, +struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next); +__vfio_iommu_detach_dev (iommu, device); + } +} + +static int __vfio_iommu_attach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +int ret; + +BUG_ON (device->attached); + +if (!iommu || !iommu->domain) +return -EINVAL ; + + ret =iommu_attach_device (iommu->domain, device->dev); +if (!ret) + device->attached = true; + +return ret; +} + +static int __vfio_iommu_attach_group (struct vfio_iommu *iommu, +struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; +int ret; + + device = list_entry(pos, struct vfio_device, device_next); + ret =__vfio_iommu_attach_dev (iommu, device); +if (ret) { +__vfio_iommu_detach_group (iommu, group); +return ret;Quoted 8 lines
Quoted 8 lines
drivers/vfio/vfio_main.c new file +6
-0+ } + } +return 0 ; +} + +/* The iommu is viable, ie. ready to be configured, when all the devices
+ * for all the groups attached to the iommu are bound to theirvfio
Quoted 37 lines
Quoted 37 lines
device
+ * drivers (ex. vfio-pci). This sets thedevice_dataprivate data pointer. */drivers/vfio/vfio_main.c new file +34
-0+static bool __vfio_iommu_viable (struct vfio_iommu *iommu) +{ +struct list_head *gpos, *dpos; + +list_for_each (gpos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(gpos, struct vfio_group, iommu_next); + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + device =list_entry (dpos, + struct vfio_device, device_next); + +if (!device->device_data) +return false; + } + } +return true; +} + +static void __vfio_close_iommu (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (!iommu->domain) +return ; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(pos, struct vfio_group, iommu_next); + +__vfio_iommu_detach_group (iommu, group); + } +Quoted 8 lines
drivers/vfio/vfio_main.c
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+vfio_iommu_unmapall (iommu); + +iommu_domain_free (iommu->domain); + iommu->domain =NULL ; + iommu->mm =NULL ; +} + +/* Open the IOMMU. This gates all access to the iommu or device file
Quoted 3 lines
Quoted 1 line
+ * descriptors and sets
current->mmas the exclusive user. */Given the fn
vfio_group_open(ie, 1st object, 2nd operation), Iwould have
Quoted 2 lines
called this one
__vfio_iommu_open(instead of__vfio_open_iommu). Is it named__vfio_open_iommuto avoid a conflict with the namespacein
vfio_iommu.c?I would have expected that too, I'll look at renaming these.
Quoted 32 lines
Quoted 32 lines
drivers/vfio/vfio_main.c new file +32
-0+static int __vfio_open_iommu (struct vfio_iommu *iommu) +{ +struct list_head *pos; +int ret; + +if (!__vfio_iommu_viable (iommu)) +return -EBUSY ; + +if (iommu->domain) +return -EINVAL ; + + iommu->domain =iommu_domain_alloc (iommu->bus); +if (!iommu->domain) +return -EFAULT ; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(pos, struct vfio_group, iommu_next); + + ret =__vfio_iommu_attach_group (iommu, group); +if (ret) { +__vfio_close_iommu (iommu); +return ret; + } + } + +if (!allow_unsafe_intrs && + !iommu_domain_has_cap (iommu->domain,IOMMU_CAP_INTR_REMAP )) { +__vfio_close_iommu (iommu); +return -EFAULT ; + } +Quoted 8 lines
drivers/vfio/vfio_main.c
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+ iommu->cache = (iommu_domain_has_cap (iommu->domain, +IOMMU_CAP_CACHE_COHERENCY ) !=0 ); + iommu->mm = current->mm; + +return 0 ; +} + +/* Actively try to tear down the iommu and merged groups. If there
Quoted 4 lines
Quoted 4 lines
are no
+ * open iommu or device fds, we close the iommu. If we close the iommu and
+ * there are also no open group fds, we can futher dissolve thegroup
Quoted 51 lines
Quoted 51 lines
to
+ * iommu association and free the iommu data structure. */drivers/vfio/vfio_main.c new file +15
-0+static int __vfio_try_dissolve_iommu (struct vfio_iommu *iommu) +{ + +if (__vfio_iommu_inuse (iommu)) +return -EBUSY ; + +__vfio_close_iommu (iommu); + +if (!__vfio_iommu_groups_inuse (iommu)) { +struct list_head *pos, *ppos; + +list_for_each_safe (pos, ppos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group,
iommu_next);drivers/vfio/vfio_main.c new file +33
-0+__vfio_group_set_iommu (group,NULL ); + } + + +kfree (iommu); + } + +return 0 ; +} + +static struct vfio_device *__vfio_lookup_dev (struct device *dev) +{ +struct list_head *gpos; +unsigned int groupid; + +if (iommu_device_group (dev, &groupid)) +return NULL ; + +list_for_each (gpos, &vfio.group_list) { +struct vfio_group *group; +struct list_head *dpos; + + group = list_entry(gpos, struct vfio_group, group_next); + +if (group->groupid != groupid) +continue ; + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + + device =list_entry (dpos, + struct vfio_device, device_next); +Quoted 8 lines
drivers/vfio/vfio_main.c
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+if (device->dev == dev) +return device; + } + } +return NULL ; +} + +/* All release paths simply decrement the refcnt, attempt to teardown
Quoted 35 lines
Quoted 35 lines
+ * the iommu and merged groups, and wakeup anything that might be
+ * waiting if we successfully dissolve anything. */drivers/vfio/vfio_main.c new file +21
-0+static int vfio_do_release (int *refcnt,struct vfio_iommu *iommu) +{ +bool wake; + +mutex_lock (&vfio.lock); + + (*refcnt)--; + wake = (__vfio_try_dissolve_iommu (iommu) ==0 ); + +mutex_unlock (&vfio.lock); + +if (wake) +wake_up (&vfio.release_q); + +return 0 ; +} + +/* +* Device fops - passthrough to vfio device driver w/ device_data +*/ +static int vfio_device_release(struct inode *inode, struct file*filep)
drivers/vfio/vfio_main.c new file +11
-0+{ +struct vfio_device *device = filep->private_data; + +vfio_do_release (&device->refcnt, device->iommu); + + device->ops->put (device->device_data); + +return 0 ; +} + +static long vfio_device_unl_ioctl(struct file *filep,Quoted 8 lines
drivers/vfio/vfio_main.c
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+ unsigned int cmd, unsigned long arg) +{ +struct vfio_device *device = filep->private_data; + +return device->ops->ioctl (device->device_data, cmd, arg); +} + +static ssize_t vfio_device_read(struct file *filep, char __user*buf,
Quoted 8 lines
drivers/vfio/vfio_main.c
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+ size_t count, loff_t *ppos) +{ + struct vfio_device *device = filep->private_data; + +return device->ops->read (device->device_data, buf, count, ppos); +} + +static ssize_t vfio_device_write(struct file *filep, const char
__userQuoted 9 lines
drivers/vfio/vfio_main.c
Quoted 9 lines
*buf,
drivers/vfio/vfio_main.c new file +8
-0+ size_t count, loff_t *ppos) +{ + struct vfio_device *device = filep->private_data; + +return device->ops->write (device->device_data, buf, count, ppos); +} + +static int vfio_device_mmap(struct file *filep, struct
vm_area_structQuoted 24 lines
Quoted 24 lines
*vma)
drivers/vfio/vfio_main.c new file +23
-0+{ +struct vfio_device *device = filep->private_data; + +return device->ops->mmap (device->device_data, vma); +} + +#ifdef CONFIG_COMPAT +static long vfio_device_compat_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ + arg = (unsigned long )compat_ptr (arg); +return vfio_device_unl_ioctl (filep, cmd, arg); +} +#endif /* CONFIG_COMPAT */ + +const struct file_operations vfio_device_fops = { + .owner =THIS_MODULE , + .release = vfio_device_release, + .read = vfio_device_read, + .write = vfio_device_write, + .unlocked_ioctl = vfio_device_unl_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = vfio_device_compat_ioctl,Quoted 8 lines
drivers/vfio/vfio_main.c
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+#endif + .mmap = vfio_device_mmap, +}; + +/* +* Group fops +*/ +static int vfio_group_open(struct inode *inode, struct file*filep)
Quoted 30 lines
Quoted 30 lines
drivers/vfio/vfio_main.c new file +30
-0+{ +struct vfio_group *group; +int ret =0 ; + +mutex_lock (&vfio.lock); + + group =idr_find (&vfio.idr,iminor (inode)); + +if (!group) { + ret = -ENODEV ; + goto out; + } + + filep->private_data = group; + +if (!group->iommu) { +struct vfio_iommu *iommu; + + iommu =kzalloc (sizeof (*iommu),GFP_KERNEL ); +if (!iommu) { + ret = -ENOMEM ; + goto out; + } +INIT_LIST_HEAD (&iommu->group_list); +INIT_LIST_HEAD (&iommu->dm_list); +mutex_init (&iommu->dgate); + iommu->bus = group->bus; +__vfio_group_set_iommu (group, iommu); + } + group->refcnt++;Quoted 8 lines
drivers/vfio/vfio_main.c
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+ +out: +mutex_unlock (&vfio.lock); + +return ret; +} + +static int vfio_group_release(struct inode *inode, struct file*filep)
Quoted 7 lines
drivers/vfio/vfio_main.c
Quoted 7 lines
drivers/vfio/vfio_main.c new file +7
-0+{ +struct vfio_group *group = filep->private_data; + +return vfio_do_release (&group->refcnt, group->iommu); +} + +/* Attempt to merge the group pointed to by fd into group. The merge-
Quoted 2 lines
Quoted 2 lines
ee
+ * group must not have an iommu or any devices open because wecannot
Quoted 1 line
Quoted 1 line
+ * maintain that context across the merge. The merge-er group can
be
Quoted 4 lines
Quoted 2 lines
+ * in use. */
+static intvfio_group_merge(struct vfio_group *group, int fd)The documentation in vfio.txt explains clearly the logic implemented
by
Quoted 2 lines
the merge/unmerge group ioctls.
However, what you are doing is not merging groups, but ratheradding/removing
Quoted 5 lines
groups to/from iommus (and creating flat lists of groups). For example, when you do
merge(A,B)
Quoted 7 lines
Quoted 1 line
you actually mean to say "merge B to the list of groups assigned to
the
Quoted 1 line
same iommu as group A".
It's actually a little more than that. After you've merged B into A, you can close the file descriptor for B and access all of the devices for the merged group from A.
It is actually more...
Scenario 1:
create_grp(A)
create_grp(B)
...
merge_grp(A,B)
create_grp(C)
merge_grp(C,B) ... this works, right?
Scenario 2:
create_grp(A)
create_grp(B)
fd_x = get_dev_fd(B,x)
...
merge_grp(A,B)
create_grp(C)
merge_grp(A,C)
fd_x = get_dev_fd(C,x)
Those two examples seems to suggest me more of a list-abstraction than a merge abstraction. However, if it fits into the agreed syntax/logic it is ok, as long as we document it properly.
Quoted 28 lines
Quoted 1 line
For the same reason, you do not really need to provide the group you
want
Quoted 7 lines
to unmerge from, which means that instead of
unmerge(A,B)you would just need
unmerge(B)
Good point, we can avoid the awkward reference via file descriptor for the unmerge.
Quoted 1 line
I understand the reason why it is not a real merge/unmerge (ie, to
keep the
Quoted 1 line
original groups so that you can unmerge later)
Right, we still need to have visibility of the groups comprising the merged group, but the abstraction provided to the user seems to be deeper than you're thinking.
Quoted 2 lines
... however I just wonder if
it wouldn't be more natural to implement theVFIO_IOMMU_ADD_GROUP/DEL_GROUP
Quoted 1 line
iommu ioctls instead? (the relationships between the data structure
would
Quoted 2 lines
remain the same)
I guess you already discarded this option for some reasons, right?
Quoted 7 lines
What was
Quoted 1 line
the reason?
It's a possibility, I'm not sure it was discussed or really what advantage it provides. It seems like we'd logically lose the ability to
access devices from other groups,
What is the real (immediate) benefit of this capability?
Quoted 3 lines
whether that's good or bad, I don't know. I think the notion of "merge" promotes the idea that the groups
are peers and an iommu_add/del feels a bit more hierarchical.
I agree.
Quoted 441 lines
Quoted 127 lines
Quoted 127 lines
drivers/vfio/vfio_main.c new file +65
-0+{ +struct vfio_group *new; +struct vfio_iommu *old_iommu; +struct file *file; +int ret =0 ; +bool opened = false; + +mutex_lock (&vfio.lock); + + file =fget (fd); +if (!file) { + ret = -EBADF ; + goto out_noput; + } + +/* Sanity check, is this really our fd? */ +if (file->f_op != &vfio_group_fops) { + ret = -EINVAL ; + goto out; + } + + new = file->private_data; + +if (!new || new == group || !new->iommu || + new->iommu->domain || new->bus != group->bus) { + ret = -EINVAL ; + goto out; + } + +/* We need to attach all the devices to each domain separately +* in order to validate that the capabilities match for both. */ + ret =__vfio_open_iommu (new->iommu); +if (ret) + goto out; + +if (!group->iommu->domain) { + ret =__vfio_open_iommu (group->iommu); +if (ret) + goto out; + opened = true; + } + +/* If cache coherency doesn't match we'd potentialy need to +* remap existing iommu mappings in the merge-er domain. +* Poor return to bother trying to allow this currently. */ +if (iommu_domain_has_cap (group->iommu->domain, +IOMMU_CAP_CACHE_COHERENCY ) != +iommu_domain_has_cap (new->iommu->domain, +IOMMU_CAP_CACHE_COHERENCY )) { +__vfio_close_iommu (new->iommu); +if (opened) +__vfio_close_iommu (group->iommu); + ret = -EINVAL ; + goto out; + } + +/* Close the iommu for the merge-ee and attach all its devices +* to the merge-er iommu. */ +__vfio_close_iommu (new->iommu); + + ret =__vfio_iommu_attach_group (group->iommu, new); +if (ret) + goto out; + +/* set_iommu unlinks new from the iommu, so save a pointer to it */
drivers/vfio/vfio_main.c new file +61
-0+ old_iommu = new->iommu; +__vfio_group_set_iommu (new, group->iommu); +kfree (old_iommu); + +out: +fput (file); +out_noput: +mutex_unlock (&vfio.lock); +return ret; +} + +/* Unmerge the group pointed to by fd from group. */ +static int vfio_group_unmerge (struct vfio_group *group,int fd) +{ +struct vfio_group *new; +struct vfio_iommu *new_iommu; +struct file *file; +int ret =0 ; + +/* Since the merge-out group is already opened, it needs to +* have an iommu struct associated with it. */ + new_iommu =kzalloc (sizeof (*new_iommu),GFP_KERNEL ); +if (!new_iommu) +return -ENOMEM ; + +INIT_LIST_HEAD (&new_iommu->group_list); +INIT_LIST_HEAD (&new_iommu->dm_list); +mutex_init (&new_iommu->dgate); + new_iommu->bus = group->bus; + +mutex_lock (&vfio.lock); + + file =fget (fd); +if (!file) { + ret = -EBADF ; + goto out_noput; + } + +/* Sanity check, is this really our fd? */ +if (file->f_op != &vfio_group_fops) { + ret = -EINVAL ; + goto out; + } + + new = file->private_data; +if (!new || new == group || new->iommu != group->iommu) { + ret = -EINVAL ; + goto out; + } + +/* We can't merge-out a group with devices still in use. */ +if (__vfio_group_devs_inuse (new)) { + ret = -EBUSY ; + goto out; + } + +__vfio_iommu_detach_group (group->iommu, new); +__vfio_group_set_iommu (new, new_iommu); + +out: +fput (file);Quoted 8 lines
drivers/vfio/vfio_main.c
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+out_noput: +if (ret) +kfree (new_iommu); +mutex_unlock (&vfio.lock); +return ret; +} + +/* Get a new iommu file descriptor. This will open the iommu, setting
Quoted 19 lines
Quoted 19 lines
+ * the
current->mmownership if it's not already set. */drivers/vfio/vfio_main.c new file +18
-0+static int vfio_group_get_iommu_fd (struct vfio_group *group) +{ +int ret =0 ; + +mutex_lock (&vfio.lock); + +if (!group->iommu->domain) { + ret =__vfio_open_iommu (group->iommu); +if (ret) + goto out; + } + + ret =anon_inode_getfd ("[vfio-iommu]" , &vfio_iommu_fops, + group->iommu,O_RDWR ); +if (ret <0 ) + goto out; + + group->iommu->refcnt++;Quoted 8 lines
Quoted 8 lines
drivers/vfio/vfio_main.c new file +6
-0+out: +mutex_unlock (&vfio.lock); +return ret; +} + +/* Get a new device file descriptor. This will open the iommu, setting
+ * thecurrent->mmownership if it's not already set. It'sdifficult
Quoted 2 lines
Quoted 2 lines
to
+ * specify the requirements for matching a user supplied buffer toa
Quoted 1 line
Quoted 1 line
+ * device, so we use a vfio driver callback to test for a match.
For
Quoted 146 lines
Quoted 146 lines
+ * PCI,
dev_name(dev)is unique, but other drivers may require including
+ * a parent device string. */
+static intvfio_group_get_device_fd(struct vfio_group *group, char *buf)drivers/vfio/vfio_main.c new file +141
-0+{ +struct vfio_iommu *iommu = group->iommu; +struct list_head *gpos; +int ret = -ENODEV ; + +mutex_lock (&vfio.lock); + +if (!iommu->domain) { + ret =__vfio_open_iommu (iommu); +if (ret) + goto out; + } + +list_for_each (gpos, &iommu->group_list) { +struct list_head *dpos; + + group = list_entry(gpos, struct vfio_group, iommu_next); + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + + device =list_entry (dpos, + struct vfio_device, device_next); + +if (device->ops->match (device->dev, buf)) { +struct file *file; + +if (device->ops->get (device->device_data)) { + ret = -EFAULT ; + goto out; + } + +/* We can't use anon_inode_getfd(), like above +* because we need to modify the f_mode flags +* directly to allow more than just ioctls */ + ret =get_unused_fd (); +if (ret <0 ) { + device->ops->put (device->device_data); + goto out; + } + + file =anon_inode_getfile ("[vfio-device]" , + &vfio_device_fops, + device,O_RDWR ); +if (IS_ERR (file)) { +put_unused_fd (ret); + ret =PTR_ERR (file); + device->ops->put (device->device_data); + goto out; + } + +/* Todo: add an anon_inode interface to do +* this. Appears to be missing by lack of +* need rather than explicitly prevented. +* Now there's need. */ + file->f_mode |= (FMODE_LSEEK | +FMODE_PREAD | +FMODE_PWRITE ); + +fd_install (ret, file); + + device->refcnt++; + goto out; + } + } + } +out: +mutex_unlock (&vfio.lock); +return ret; +} + +static long vfio_group_unl_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ +struct vfio_group *group = filep->private_data; + +if (cmd ==VFIO_GROUP_GET_FLAGS ) { +u64 flags =0 ; + +mutex_lock (&vfio.lock); +if (__vfio_iommu_viable (group->iommu)) + flags |=VFIO_GROUP_FLAGS_VIABLE ; +mutex_unlock (&vfio.lock); + +if (group->iommu->mm) + flags |=VFIO_GROUP_FLAGS_MM_LOCKED ; + +return put_user (flags, (u64 __user *)arg); + } + +/* Below commands are restricted once the mm is set */ +if (group->iommu->mm && group->iommu->mm != current->mm) +return -EPERM ; +if (cmd ==VFIO_GROUP_MERGE || cmd ==VFIO_GROUP_UNMERGE ) { +int fd; + +if (get_user (fd, (int __user *)arg)) +return -EFAULT ; +if (fd <0 ) +return -EINVAL ; + +if (cmd ==VFIO_GROUP_MERGE ) +return vfio_group_merge (group, fd); +else +return vfio_group_unmerge (group, fd); + }else if (cmd ==VFIO_GROUP_GET_IOMMU_FD ) { +return vfio_group_get_iommu_fd (group); + }else if (cmd ==VFIO_GROUP_GET_DEVICE_FD ) { +char *buf; +int ret; + + buf =strndup_user ((const char __user *)arg,PAGE_SIZE ); +if (IS_ERR (buf)) +return PTR_ERR (buf); + + ret =vfio_group_get_device_fd (group, buf); +kfree (buf); +return ret; + } + +return -ENOSYS ; +} + +#ifdef CONFIG_COMPAT +static long vfio_group_compat_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ + arg = (unsigned long )compat_ptr (arg); +return vfio_group_unl_ioctl (filep, cmd, arg); +} +#endif /* CONFIG_COMPAT */ + +static const struct file_operations vfio_group_fops = { + .owner =THIS_MODULE , + .open = vfio_group_open, + .release = vfio_group_release, + .unlocked_ioctl = vfio_group_unl_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = vfio_group_compat_ioctl, +#endif +};Quoted 8 lines
drivers/vfio/vfio_main.c line 806
Quoted 2 lines
drivers/vfio/vfio_main.c new file +2
-0+ +/* iommu fd release hook */ Given
vfio_device_releaseand
vfio_group_release(ie, 1st object, 2nd operation), I was
going to suggest renaming the fn below tovfio_iommu_release, but then I saw the latter name being already used invfio_iommu.c... a bit confusing but I guess it's ok then.Right, this one was definitely because of naming collision.
Quoted 10 lines
drivers/vfio/vfio_main.c
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+int vfio_release_iommu (struct vfio_iommu *iommu) +{ +return vfio_do_release (&iommu->refcnt, iommu); +} + +/* +* VFIO driver API +*/ + +/* Add a new device to the vfio framework with associated vfio driver
Quoted 1 line
Quoted 1 line
+ * callbacks. This is the entry point for vfio drivers to
register
Quoted 80 lines
Quoted 80 lines
devices. */
+intvfio_group_add_dev(struct device *dev, const struct vfio_device_ops *ops)drivers/vfio/vfio_main.c new file +59
-0+{ + struct list_head *pos; + struct vfio_group *group = NULL; + struct vfio_device *device = NULL; + unsigned int groupid; + int ret = 0; + bool new_group = false; + + if (!ops) + return -EINVAL; + + if (iommu_device_group(dev, &groupid)) + return -ENODEV; + + mutex_lock(&vfio.lock); + + list_for_each(pos, &vfio.group_list) { + group = list_entry(pos, struct vfio_group, group_next); + if (group->groupid == groupid) + break; + group = NULL; + } + + if (!group) { + int minor; + + if (unlikely(idr_pre_get(&vfio.idr, GFP_KERNEL) == 0)) { + ret = -ENOMEM; + goto out; + } + + group = kzalloc(sizeof(*group), GFP_KERNEL); + if (!group) { + ret = -ENOMEM; + goto out; + } + + group->groupid = groupid; + INIT_LIST_HEAD(&group->device_list); + + ret = idr_get_new(&vfio.idr, group, &minor); + if (ret == 0 && minor > MINORMASK) { + idr_remove(&vfio.idr, minor); + kfree(group); + ret = -ENOSPC; + goto out; + } + + group->devt = MKDEV(MAJOR(vfio.devt), minor); + device_create(vfio.class, NULL, group->devt, + group, "%u", groupid); + + group->bus = dev->bus; + list_add(&group->group_next, &vfio.group_list); + new_group = true; + } else { + if (group->bus != dev->bus) { + printk(KERN_WARNING + "Error: IOMMU group ID conflict. Group ID %u"
drivers/vfio/vfio_main.c new file +17
-0+"on both bus %s and %s\n" , groupid, + group->bus->name, dev->bus->name); + ret = -EFAULT ; + goto out; + } + +list_for_each (pos, &group->device_list) { + device =list_entry (pos, + struct vfio_device, device_next); +if (device->dev == dev) +break ; + device =NULL ; + } + } + +if (!device) { + if (__vfio_group_devs_inuse(group) ||Quoted 8 lines
drivers/vfio/vfio_main.c line 899
Quoted 6 lines
drivers/vfio/vfio_main.c new file +3
-0+ (group->iommu && group->iommu->refcnt)) { + printk(KERN_WARNING + "Adding device %s to group %u while group isalready in use!!\n",
drivers/vfio/vfio_main.c new file +2
-0+ dev_name(dev), group->groupid); +/* XXX How to prevent other drivers from claiming? */ Here we are adding a device (not yet assigned to a vfio bus) to a
group
Quoted 3 lines
that is already in use.
Given that it would not be acceptable for this device to get assigned to a non vfio driver, why not forcing such assignment here then?Exactly, I just don't know the mechanics of how to make that happen and was hoping for suggestions...
Quoted 6 lines
I am not sure though what the best way to do it would be. What about something like this:
- when the bus vfio-pci processes the
BUS_NOTIFY_ADD_DEVICEnotification it assigns to the device a PCI ID that will make sure the vfio-pci's probe routine will be invoked (and no other driver
Quoted 7 lines
can
Quoted 3 lines
therefore claim the device). That PCI ID would have to be added to the vfio_pci_driver's
id_table(it would be the exception to the "only dynamic IDs" rule). Too hackish?Presumably some other driver also has the ID in it's
id_table, how do we make sure we win?
By mangling such ID (when processing the BUS_NOTIFY_ADD_DEVICE notification) to match against a 'fake' ID registered in the vfio-pci table (it would be like a sort of driver redirect/divert). The vfio-pci's probe routine would restore the original ID (we do not want to confuse userspace). This is hackish, I agree.
What about this:
- When vfio-pci processes the BUS_NOTIFY_ADD_DEVICE notification it can pre-initialize the driver pointer (via an API). We would then need to change the match/probe PCI mechanism too: for example, the PCI core will have to check and honor such pre-driver-initialization when present (and give it higher priority over the match callbacks).
How to do this? For example, when vfio_group_add_dev is invoked, it checks whether the device is getting added to an already existent group where the other devices (well, you would need to check just one of the devices in the group) are already assigned to vfio-pci, and in such a case it pre-initialize the driver to vfio-pci.
NOTE: By "preinit" I mean "save into the device a reference to a driver before
the 'match' callbacks".
This would be the timeline:
|
+-> new device gets added to (PCI) bus | +-> PCI: send BUS_NOTIFIER_ADD_DEVICE notification | +-> VFIO:vfio_pci_device_notifier | | | +-> BUS_NOTIFIER_ADD_DEVICE: vfio_group_add_dev | | | +->iommu_device_group(dev,&groupid) | +->group = <search groupid in vfio.group_list> | +->if (group && group_is_vfio(group)) | | <preinit device driver to vfio-pci> | ... | +-> PCI: xxx | | | +-> if (!device_driver_is_preinit(dev)) | | probe=<search driver's probe callback using 'match'> | | else | | probe=<get it from preint driver config> | | (+fallback to 'match' if preinit driver disappeared?) | | | +-> rc = probe(...) | | | ...
v
...
Of course, what if multiple drivers decide to preinit the device ?
One way to make it cleaner would be to:
- have the PCI layer export an API that allows (for example) the bus notification callbacks (like vfio_pci_device_notifier) to preinit a driver
- make such API reject calls on devices that already have a preinit driver.
- make VFIO detect the case where vfio_pci_device_notifier can not preinit the driver (to vfio-pci) for the new device (because already preinited) and raise an error/warning.
Would this look a bit cleaner?
Quoted 49 lines
Quoted 13 lines
Quoted 13 lines
drivers/vfio/vfio_main.c new file +13
-0+ } + + device =kzalloc (sizeof (*device),GFP_KERNEL ); +if (!device) { +/* If we just created this group, tear it down */ +if (new_group) { +list_del (&group->group_next); +device_destroy (vfio.class, group->devt); +idr_remove (&vfio.idr,MINOR (group->devt)); +kfree (group); + } + ret = -ENOMEM ; + goto out;Quoted 8 lines
drivers/vfio/vfio_main.c line 918
Quoted 6 lines
drivers/vfio/vfio_main.c new file +6
-0+ } + +list_add (&device->device_next, &group->device_list); + device->dev = dev; + device->ops = ops; + device->iommu = group->iommu;/* NULL if new */ Shouldn't you check the return code of
__vfio_iommu_attach_dev?Yep, looks like I did this because the expected use case has a NULL iommu here, so I need to distiguish that error from an actual
iommu_attach_device()error.Quoted 8 lines
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+__vfio_iommu_attach_dev (group->iommu, device); + } +out: +mutex_unlock (&vfio.lock); +return ret; +} +EXPORT_SYMBOL_GPL (vfio_group_add_dev); +Quoted 8 lines
Quoted 1 line
+/* Remove a device from the vfio framework */
This fn below does not return any error code. Ok ... However, there are a number of errors case that you test, for example
- device that does not belong to any group (according to iommu API)
- device that belongs to a group but that does not appear in the list of devices of thevfio_groupstructure.
Are the above two errors checks just paranoia or are those errorsactually possible?
Quoted 2 lines
If they were possible, shouldn't we generate a warning (most probably it would be a bug in the code)?
They're all vfio-bus driver bugs of some sort, so it's just a matter of how much we want to scream about them. I'll comments on each below.
Quoted 14 lines
Quoted 9 lines
drivers/vfio/vfio_main.c line 936
Quoted 9 lines
drivers/vfio/vfio_main.c new file +9
-0+void vfio_group_del_dev (struct device *dev) +{ +struct list_head *pos; +struct vfio_group *group =NULL ; +struct vfio_device *device =NULL ; +unsigned int groupid; + +if (iommu_device_group (dev, &groupid)) +return ;Here the bus driver is probably just sitting on a notifier list for their
bus_typeand a device is getting removed. Unless we want to require the bus driver to track everything it's attempted to add and whether it worked, we can just ignore this.
OK, I see what you mean. If vfio_group_add_dev fails for some reasons we do not keep track of it. Right?
Would it make sense to add one special group to vfio.group_list (or better On a separate field of the vfio structure) whose goal
would be just that: keep track of those devices that failed to be added to the VFIO framework (can it help for debugging too?)?
Quoted 133 lines
Quoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_main.c new file +4
-0+ +mutex_lock (&vfio.lock); + +list_for_each (pos, &vfio.group_list) {Quoted 8 lines
drivers/vfio/vfio_main.c line 948
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+ group = list_entry(pos, struct vfio_group, group_next); +if (group->groupid == groupid) +break ; + group =NULL ; + } + +if (!group) + goto out;We don't even have a group for the device, we could
BUG_ONhere. The bus driver failed to tell us about something that was then removed.Quoted 10 lines
drivers/vfio/vfio_main.c line 958
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+ +list_for_each (pos, &group->device_list) { + device = list_entry(pos, struct vfio_device, device_next); +if (device->dev == dev) +break ; + device =NULL ; + } + +if (!device) + goto out;Same here.
Quoted 23 lines
Quoted 23 lines
drivers/vfio/vfio_main.c new file +23
-0+ +BUG_ON (device->refcnt); + +if (device->attached) +__vfio_iommu_detach_dev (group->iommu, device); + +list_del (&device->device_next); +kfree (device); + +/* If this was the only device in the group, remove the group. +* Note that we intentionally unmerge empty groups here if the +* group fd isn't opened. */ +if (list_empty (&group->device_list) && group->refcnt ==0 ) { +struct vfio_iommu *iommu = group->iommu; + +if (iommu) { +__vfio_group_set_iommu (group,NULL ); +__vfio_try_dissolve_iommu (iommu); + } + +device_destroy (vfio.class, group->devt); +idr_remove (&vfio.idr,MINOR (group->devt)); +list_del (&group->group_next);Quoted 8 lines
drivers/vfio/vfio_main.c
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+kfree (group); + } +out: +mutex_unlock (&vfio.lock); +} +EXPORT_SYMBOL_GPL (vfio_group_del_dev); + +/* When a device is bound to a vfio device driver (ex. vfio-pci), this
Quoted 1 line
Quoted 1 line
+ * entry point is used to mark the device usable (viable). The
vfio
Quoted 3 lines
Quoted 3 lines
+ * device driver associates a private
device_datastruct with the device
+ * here, which will later be return forvfio_device_fopscallbacks. */
Quoted 15 lines
Quoted 15 lines
drivers/vfio/vfio_main.c new file +15
-0+int vfio_bind_dev (struct device *dev,void *device_data) +{ +struct vfio_device *device; +int ret = -EINVAL ; + +BUG_ON (!device_data); + +mutex_lock (&vfio.lock); + + device =__vfio_lookup_dev (dev); + +BUG_ON (!device); + + ret =dev_set_drvdata (dev, device); +if (!ret)Quoted 8 lines
drivers/vfio/vfio_main.c
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+ device->device_data = device_data; + +mutex_unlock (&vfio.lock); +return ret; +} +EXPORT_SYMBOL_GPL (vfio_bind_dev); + +/* A device is only removeable if the iommu for the group is not in
Quoted 9 lines
Quoted 9 lines
use. */
drivers/vfio/vfio_main.c new file +8
-0+static bool vfio_device_removeable (struct vfio_device *device) +{ +bool ret = true; + +mutex_lock (&vfio.lock); + +if (device->iommu &&__vfio_iommu_inuse (device->iommu)) + ret = false;Quoted 8 lines
Quoted 8 lines
drivers/vfio/vfio_main.c new file +6
-0+ +mutex_unlock (&vfio.lock); +return ret; +} + +/* Notify vfio that a device is being unbound from the vfio device driver
+ * and return the device privatedevice_datapointer. If thegroup is
Quoted 1 line
Quoted 1 line
+ * in use, we need to block or take other measures to make it safe
for
Quoted 9 lines
Quoted 9 lines
+ * the device to be removed from the iommu. */
drivers/vfio/vfio_main.c new file +8
-0+void *vfio_unbind_dev (struct device *dev) +{ +struct vfio_device *device =dev_get_drvdata (dev); +void *device_data; + +BUG_ON (!device); + +again:Quoted 8 lines
drivers/vfio/vfio_main.c line 1045
Quoted 5 lines
drivers/vfio/vfio_main.c new file +5
-0+if (!vfio_device_removeable (device)) { +/* XXX signal for all devices in group to be removed or +* resort to killing the process holding the device fds. +* For now just block waiting for releases to wake us. */ +wait_event (vfio.release_q,vfio_device_removeable (device));Any new idea/proposal on how to handle this situation? The last one I remember was to leave the soft/hard/etc timeout
handling in
Quoted 1 line
userspace and implement it as a sort of policy. Is that one still the
most
Quoted 1 line
likely candidate solution to handle this situation?
Quoted 7 lines
I haven't heard any new proposals. I think we need the hard timeout handling in the kernel. We can't leave it to userspace to decide they get to keep the device. We could have this tunable via an ioctl, but I don't see how we wouldn't require
CAP_SYS_ADMIN(or similar) to tweak it. I was intending to re-implement the netlink interface to signal the
removal, but expect to get allergic reactions to that.
(I personally like the async netlink signaling, but I am OK with an ioctl based mechanism if it provides the same flexibility)
What would be a reasonable hard timeout?
/Chris
Alex Williamson
NAKOn Wed, 2011-11-09 at 15:08 -0600, Christian Benvenuti (benve) wrote:
<snip>
Quoted 32 lines
Quoted 20 lines
Quoted 6 lines
drivers/vfio/vfio_main.c line 59
Quoted 4 lines
drivers/vfio/vfio_main.c new file +4
-0+ +struct vfio_group { +dev_t devt; +unsigned int groupid;This groupid is returned by the
device_groupcallback you recentlyadded
Quoted 7 lines
with a separate (not yet in tree) IOMMU patch.
Is it correct to say that the scope of this ID is the bus the iommu belongs too (but you use it as if it was global)?
I believe there is nothing right now to ensure the uniqueness of such ID across bus types (assuming there will be other bus drivers in the future besides vfio-pci).
If that's the case, the vfio.group_list global list and the
__vfio_lookup_devQuoted 2 lines
routine should be changed to account for the bus too? Ops, I just saw the error msg in
vfio_group_add_devabout the groupid conflict.
Quoted 1 line
Is that warning related to what I mentioned above?
Quoted 11 lines
Yeah, this is a concern, but I can't think of a system where we would manifest a collision. The IOMMU driver is expected to provide unique groupids for all devices below them, but we could imagine a system that implements two different
bus_types, each with a different IOMMU driver and we have no coordination between them. Perhaps since we haveiommu_opsper bus, we should also expose the bus in the vfio group path,
ie. /dev/vfio/%s/%u,dev->bus->name,iommu_device_group(dev,..). This means userspace would need to do a readlink of the subsystem entry where
it finds theiommu_groupto find the vfio group. Reasonable?
Quoted 2 lines
Most probably we won't see use cases with multiple buses anytime soon, but this scheme you proposed (with the per-bus subdir) looks good to me.
Ok, I think that's easier than any scheme of trying to organize globally unique groupids instead of just bus_type unique. That makes group objects internally matched by the {groupid, bus} pair.
<snip>
Quoted 45 lines
Quoted 39 lines
Quoted 23 lines
I looked at how you take care of ref counts ...
This is how the tree of vfio_iommu/vfio_group/vfio_device data Structures is organized (I'll use just iommu/group/dev to make the graph smaller):
iommu / \ / \ group ... group / \ / \/ \ / \
dev .. dev dev .. devThis is how you get a file descriptor for the three kind of objects:
- group : open
/dev/vfio/xxxfor group xxx
- iommu : group ioctlVFIO_GROUP_GET_IOMMU_FD
- device: group ioctlVFIO_GROUP_GET_DEVICE_FDGiven the above topology, I would assume that:
Quoted 7 lines
(1) an iommu is 'inuse' if : a) iommu refcnt > 0, or
b) any of its groups is 'inuse'(2) a group is 'inuse' if : a) group refcnt > 0, or
b) any of its devices is 'inuse'(3) a device is 'inuse' if : a) device refcnt > 0
(2) is a bit debatable. I've wrestled with this one for a while. The
vfio_iommuserves two purposes. First, it is the object we use for managing iommu domains, which includes allocating domains and attaching devices to domains. Groups objects aren't involved here, they just manage the set of devices. The second role is to manage merged groups, because whether or not groups can be merged is a function of iommu domain compatibility.Quoted 5 lines
So if we look at "is the iommu in use?" ie. can I destroy the mapping context, detach devices and free the domain, the reference count on the group is irrelevant. The user has to have a device or iommu file descriptor opened somewhere, across the group or merged group, for that context to be maintained. A reasonable requirement, I think.
Quoted 6 lines
OK, then if you close all devices and the iommu, keeping the group open Would not protect the iommu domain mapping. This means that if you (or A management application) need to close all devices+iommu and reopen right away again the same devices+iommu you may get a failure on the iommu domain creation (supposing the system goes out of resources). Is this just a very unlikely scenario?
Can you think of a use case that would require such? I can't.
Quoted 2 lines
I guess in this case you would simply have to avoid releasing the iommu fd, right?
Right. We could also debate whether we should drop all iommu mappings when the iommu refcnt goes to zero. We don't currently do that, but it might make sense.
Quoted 16 lines
Quoted 5 lines
However, if we ask "is the group in use?" ie. can I not only destroy the
mappings above, but also automatically tear apart merged groups, then I think we need to look at the group refcnt.Correct.
Quoted 8 lines
There's also a symmetry factor, the group is a benign entry point to device access. It's only when device or iommu access is granted that the group gains any real power. Therefore, shouldn't that power also be
removed when those access points are closed?Quoted 2 lines
You have coded the 'inuse' logic with these three routines:
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
__vfio_iommu_inuse, which implements (1) aboveand
__vfio_iommu_groups_inuseImplements (2.a)
Yes, but for al groups at once.
Right
Quoted 49 lines
Quoted 3 lines
Quoted 1 line
__vfio_group_devs_inuseImplements (2.b)
Yes
Quoted 43 lines
Quoted 2 lines
which are used by
__vfio_iommu_inuse.
Why don't you check the group refcnt in__vfio_iommu_groups_inuse?Hopefully explained above, but open for discussion.
Quoted 4 lines
Would it make sense (and the code more readable) to structure the nested refcnt/inuse check like this?
(The numbers (1)(2)(3) refer to the three 'inuse' conditions above)Quoted 13 lines
(1)__vfio_iommu_inuse | +-> check iommu refcnt +-> __vfio_iommu_groups_inuse | +->LOOP: (2)__vfio_iommu_group_inuse<--MISSING | +-> check group refcnt<--MISSING +-> __vfio_group_devs_inuse() | +-> LOOP: (3)__vfio_group_dev_inuse<--MISSING | +-> check device refcntWe currently do:
(1)__vfio_iommu_inuse | +-> check iommu refcnt +-> __vfio_group_devs_inuse | +->LOOP: (2.b)__vfio_group_devs_inuse | +-> LOOP: (3) check device refcntIf that passes, the iommu context can be dissolved and we follow up with:
__vfio_iommu_groups_inuse | +-> LOOP: (2.a)__vfio_iommu_groups_inuse | +-> check group refcnt
Quoted 6 lines
Quoted 3 lines
If that passes, groups can also be umerged.
Is this right?
Yes, assuming we stick to the "benign" role of groups you described above.
Ok, no change then. Thanks for looking at that so closely.
<snip>
Quoted 22 lines
Quoted 12 lines
Quoted 3 lines
Quoted 1 line
+static int
vfio_group_merge(struct vfio_group *group, int fd)The documentation in vfio.txt explains clearly the logic implemented
by
Quoted 2 lines
the merge/unmerge group ioctls.
However, what you are doing is not merging groups, but ratheradding/removing
Quoted 5 lines
groups to/from iommus (and creating flat lists of groups). For example, when you do
merge(A,B)Quoted 7 lines
Quoted 1 line
you actually mean to say "merge B to the list of groups assigned to
the
Quoted 1 line
same iommu as group A".
It's actually a little more than that. After you've merged B into A, you can close the file descriptor for B and access all of the devices for the merged group from A.
It is actually more...
Quoted 8 lines
Scenario 1:
create_grp(A)
create_grp(B)
...
merge_grp(A,B)
create_grp(C)
merge_grp(C,B)... this works, right?
No, but merge_grp(B,C) does. I currently require that the incoming group has no open device or iommu file descriptors and is a singular group. The device/iommu is a hard requirement since we'll be changing the iommu context and can't leave an attack window. The singular group is an implementation detail. Given the iommu/device requirement, it's just as easy for userspace to tear apart the group and pass each individually.
Quoted 7 lines
Scenario 2:
create_grp(A)
create_grp(B)
fd_x=get_dev_fd(B,x)
...
merge_grp(A,B)
NAK, fails no open device test. Again, merge_grp(B,A) is supported.
Quoted 2 lines
create_grp(C)
merge_grp(A,C)
Yep, this works.
Quoted 1 line
fd_x=get_dev_fd(C,x)
Yep, and if x is they same in both cases, you'll get 2 different file descriptors backed by the same device.
Quoted 3 lines
Those two examples seems to suggest me more of a list-abstraction than a merge abstraction. However, if it fits into the agreed syntax/logic it is ok, as long as we document it properly.
Can you suggest documentation changes that would make this more clear?
Quoted 29 lines
Quoted 29 lines
Quoted 1 line
For the same reason, you do not really need to provide the group you
want
Quoted 7 lines
to unmerge from, which means that instead of
unmerge(A,B)you would just need
unmerge(B)
Good point, we can avoid the awkward reference via file descriptor for the unmerge.
Quoted 1 line
I understand the reason why it is not a real merge/unmerge (ie, to
keep the
Quoted 1 line
original groups so that you can unmerge later)
Right, we still need to have visibility of the groups comprising the merged group, but the abstraction provided to the user seems to be deeper than you're thinking.
Quoted 2 lines
... however I just wonder if
it wouldn't be more natural to implement theVFIO_IOMMU_ADD_GROUP/DEL_GROUP
Quoted 1 line
iommu ioctls instead? (the relationships between the data structure
would
Quoted 2 lines
remain the same)
I guess you already discarded this option for some reasons, right?What was
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
the reason?
It's a possibility, I'm not sure it was discussed or really what advantage it provides. It seems like we'd logically lose the ability to
access devices from other groups,What is the real (immediate) benefit of this capability?
Mostly convenience, but also promotes the peer idea where merged groups simply create a "super" group that can access the iommu and all the devices of the member groups. On x86 we expect that merging groups will always succeed and groups will typically have a single device, so a driver could merge them all together, throw away all the extra group file descriptors and manage the whole super group via a single group fd.
Quoted 5 lines
Quoted 3 lines
whether that's good or bad, I don't know. I think the notion of "merge" promotes the idea that the groups
are peers and an iommu_add/del feels a bit more hierarchical.I agree.
<snip>
Quoted 37 lines
Quoted 24 lines
Quoted 10 lines
drivers/vfio/vfio_main.c line 899
Quoted 8 lines
drivers/vfio/vfio_main.c new file +5
-0+ if (!device) { + if (__vfio_group_devs_inuse(group) || + (group->iommu && group->iommu->refcnt)) { + printk(KERN_WARNING + "Adding device %s to group %u while group isalready in use!!\n",
drivers/vfio/vfio_main.c new file +2
-0+ dev_name(dev), group->groupid); +/* XXX How to prevent other drivers from claiming? */ Here we are adding a device (not yet assigned to a vfio bus) to a
group
Quoted 3 lines
that is already in use.
Given that it would not be acceptable for this device to get assigned to a non vfio driver, why not forcing such assignment here then?Exactly, I just don't know the mechanics of how to make that happen and was hoping for suggestions...
Quoted 6 lines
I am not sure though what the best way to do it would be. What about something like this:
- when the bus vfio-pci processes the
BUS_NOTIFY_ADD_DEVICEnotification it assigns to the device a PCI ID that will make sure the vfio-pci's probe routine will be invoked (and no other driverQuoted 7 lines
can
Quoted 3 lines
therefore claim the device). That PCI ID would have to be added to the vfio_pci_driver's
id_table(it would be the exception to the "only dynamic IDs" rule). Too hackish?Presumably some other driver also has the ID in it's
id_table, how do we make sure we win?By mangling such ID (when processing the
BUS_NOTIFY_ADD_DEVICEnotification) to match against a 'fake' ID registered in the vfio-pci table (it would be like a sort of driver redirect/divert). The vfio-pci's probe routine would restore the original ID (we do not want to confuse userspace). This is hackish, I agree.
Quoted 11 lines
What about this:
- When vfio-pci processes theBUS_NOTIFY_ADD_DEVICEnotification it can pre-initialize the driver pointer (via an API). We would then need to change the match/probe PCI mechanism too: for example, the PCI core will have to check and honor such pre-driver-initialization when present (and give it higher priority over the match callbacks).
How to do this? For example, whenvfio_group_add_devis invoked, it checks whether the device is getting added to an already existent group where the other devices (well, you would need to check just one of the devices in the group) are already assigned to vfio-pci, and in such a case it pre-initialize the driver to vfio-pci.
It's ok to make a group "non-viable", we only want to intervene if the iommu is inuse (iommu or device refcnt > 0).
Quoted 32 lines
NOTE: By "preinit" I mean "save into the device a reference to a driver before
the 'match' callbacks".This would be the timeline:
|
+-> new device gets added to (PCI) bus | +-> PCI: send BUS_NOTIFIER_ADD_DEVICE notification | +-> VFIO:vfio_pci_device_notifier | | | +-> BUS_NOTIFIER_ADD_DEVICE: vfio_group_add_dev | | | +->iommu_device_group(dev,&groupid) | +->group = <search groupid in vfio.group_list> | +->if (group && group_is_vfio(group)) | | <preinit device driver to vfio-pci> | ... | +-> PCI: xxx | | | +-> if (!device_driver_is_preinit(dev)) | | probe=<search driver's probe callback using 'match'> | | else | | probe=<get it from preint driver config> | | (+fallback to 'match' if preinit driver disappeared?) | | | +-> rc = probe(...) | | | ...
Quoted 4 lines
v
...Of course, what if multiple drivers decide to preinit the device ?
Yep, we'd have to have a policy to BUG_ON if the preinit driver is already set.
Quoted 10 lines
One way to make it cleaner would be to:
- have the PCI layer export an API that allows (for example) the bus notification callbacks (likevfio_pci_device_notifier) to preinit a driver
- make such API reject calls on devices that already have a preinit driver.
- make VFIO detect the case wherevfio_pci_device_notifiercan not preinit the driver (to vfio-pci) for the new device (because already preinited) and raise an error/warning.Would this look a bit cleaner?
It looks like there might already be infrastructure that we can set dev->driver and call the driver probe() function, so maybe we're only in trouble if dev->driver is already set when we get the bus add notification. I just wasn't sure if that was entirely kosher. I'll have to try that and figure out how to test it; fake hotplug maybe.
<snip>
Quoted 28 lines
Quoted 13 lines
Quoted 6 lines
This fn below does not return any error code. Ok ... However, there are a number of errors case that you test, for example
- device that does not belong to any group (according to iommu API)
- device that belongs to a group but that does not appear in the list of devices of thevfio_groupstructure.
Are the above two errors checks just paranoia or are those errorsactually possible?
Quoted 2 lines
If they were possible, shouldn't we generate a warning (most probably it would be a bug in the code)?
They're all vfio-bus driver bugs of some sort, so it's just a matter of how much we want to scream about them. I'll comments on each below.
Quoted 14 lines
Quoted 9 lines
drivers/vfio/vfio_main.c line 936
Quoted 9 lines
drivers/vfio/vfio_main.c new file +9
-0+void vfio_group_del_dev (struct device *dev) +{ +struct list_head *pos; +struct vfio_group *group =NULL ; +struct vfio_device *device =NULL ; +unsigned int groupid; + +if (iommu_device_group (dev, &groupid)) +return ;Here the bus driver is probably just sitting on a notifier list for their
bus_typeand a device is getting removed. Unless we want to require the bus driver to track everything it's attempted to add and whether it worked, we can just ignore this.
Quoted 2 lines
OK, I see what you mean. If
vfio_group_add_devfails for some reasons we do not keep track of it. Right?
The primary thing I'm thinking of here is not vfio_group_add_dev() failing for "some reason", but specifically failing because the device doesn't have a groupid, ie. it's not behind an iommu. In that case it's just a random device that can't be used by vfio.
Quoted 4 lines
Would it make sense to add one special group to vfio.group_list (or better On a separate field of the vfio structure) whose goal
would be just that: keep track of those devices that failed to be added to the VFIO framework (can it help for debugging too?)?
For the above case, no, we shouldn't need to track those. But it does seem like there's a gap for devices that fail vfio_group_add_dev() for other reasons. I don't think we want a special group for them, because that isolates them from other devices that are potentially in the same group. I think instead what we want to do is set a taint flag on the group. We can do a BUG_ON not being able to allocate a group, then a WARN_ON if we fail elsewhere and mark the group tainted so it's effectively never viable.
<snip>
Quoted 21 lines
Quoted 13 lines
Quoted 8 lines
drivers/vfio/vfio_main.c line 1045
Quoted 5 lines
drivers/vfio/vfio_main.c new file +5
-0+if (!vfio_device_removeable (device)) { +/* XXX signal for all devices in group to be removed or +* resort to killing the process holding the device fds. +* For now just block waiting for releases to wake us. */ +wait_event (vfio.release_q,vfio_device_removeable (device));Any new idea/proposal on how to handle this situation? The last one I remember was to leave the soft/hard/etc timeout
handling in
Quoted 1 line
userspace and implement it as a sort of policy. Is that one still the
most
Quoted 1 line
likely candidate solution to handle this situation?
Quoted 7 lines
I haven't heard any new proposals. I think we need the hard timeout handling in the kernel. We can't leave it to userspace to decide they get to keep the device. We could have this tunable via an ioctl, but I don't see how we wouldn't require
CAP_SYS_ADMIN(or similar) to tweak it. I was intending to re-implement the netlink interface to signal the
removal, but expect to get allergic reactions to that.
Quoted 4 lines
(I personally like the async netlink signaling, but I am OK with an ioctl based mechanism if it provides the same flexibility)
What would be a reasonable hard timeout?
I think we were looking at 10s of seconds in the old vfio code. Tough call though. Could potentially provide a module_param override so an admin that trusts their users could set long/infinite timeout. Thanks,
Alex
I have not gone through the all patch yet, but here are my first comments/questions about the code in vfio_main.c (and pci/vfio_pci.c).
Quoted 10 lines
-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Thursday, November 03, 2011 1:12 PM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Christian Benvenuti (benve); Aaron Fabbri (aafabbri); [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected] foundation.org; [email protected]
Subject: [RFC PATCH] vfio: VFIO Driver core framework
<snip>
Quoted 57 lines
drivers/vfio/vfio_main.c new file +7
-0@@ -0,0 +1,1151 @@ +/* +* VFIO framework +* +* Copyright (C) 2011 Red Hat, Inc. All rights reserved. +* Author: Alex Williamson <[email protected]> +* +* This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Derived from original vfio:
+ * Copyright 2010 Cisco Systems, Inc. All rights reserved.
+ * Author: Tom Lyon, [email protected]
+ */drivers/vfio/vfio_main.c new file +27
-0+ +#include <linux/cdev.h> +#include <linux/compat.h> +#include <linux/device.h> +#include <linux/file.h> +#include <linux/anon_inodes.h> +#include <linux/fs.h> +#include <linux/idr.h> +#include <linux/iommu.h> +#include <linux/mm.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/uaccess.h> +#include <linux/vfio.h> +#include <linux/wait.h> + +#include "vfio_private.h" + +#define DRIVER_VERSION "0.2" +#define DRIVER_AUTHOR "Alex Williamson <[email protected]>" +#define DRIVER_DESC "VFIO - User Level meta-driver" + +static int allow_unsafe_intrs; +module_param (allow_unsafe_intrs, int,0 ); +MODULE_PARM_DESC(allow_unsafe_intrs, + "Allow use of IOMMUs which do not support interruptremapping");
drivers/vfio/vfio_main.c new file +8
-0+ +static struct vfio { + dev_t devt; + struct cdev cdev; + struct list_head group_list; + struct mutex lock; + struct kref kref; + struct class *class;
drivers/vfio/vfio_main.c line 59
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+struct idr idr; +wait_queue_head_t release_q; +} vfio; + +static const struct file_operations vfio_group_fops; +extern const struct file_operations vfio_iommu_fops; + +struct vfio_group { +dev_t devt; +unsigned int groupid;
This groupid is returned by the device_group callback you recently added with a separate (not yet in tree) IOMMU patch.
Is it correct to say that the scope of this ID is the bus the iommu belongs too (but you use it as if it was global)?
I believe there is nothing right now to ensure the uniqueness of such ID across bus types (assuming there will be other bus drivers in the future besides vfio-pci).
If that's the case, the vfio.group_list global list and the __vfio_lookup_dev routine should be changed to account for the bus too?
Ops, I just saw the error msg in vfio_group_add_dev about the group id conflict. Is that warning related to what I mentioned above?
drivers/vfio/vfio_main.c line 71
Quoted 12 lines
drivers/vfio/vfio_main.c new file +12
-0+struct bus_type *bus; +struct vfio_iommu *iommu; +struct list_head device_list; +struct list_head iommu_next; +struct list_head group_next; +int refcnt; +}; + +struct vfio_device { +struct device *dev; +const struct vfio_device_ops *ops; +struct vfio_iommu *iommu;
I wonder if you need to have the 'iommu' field here. vfio_device.iommu is always set and reset together with vfio_group.iommu.
Given that a vfio_device instance is always linked to a vfio_group instance, do we need this duplication? Is this duplication there because you do not want the double dereference device->group->iommu?
Quoted 51 lines
drivers/vfio/vfio_main.c new file +51
-0+struct vfio_group *group; +struct list_head device_next; +bool attached; +int refcnt; +void *device_data; +}; + +/* +* Helper functions called under vfio.lock +*/ + +/* Return true if any devices within a group are opened */ +static bool __vfio_group_devs_inuse (struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next); +if (device->refcnt) +return true; + } +return false; +} + +/* Return true if any of the groups attached to an iommu are opened. +* We can only tear apart merged groups when nothing is left open. */ +static bool __vfio_iommu_groups_inuse (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group, iommu_next); +if (group->refcnt) +return true; + } +return false; +} + +/* An iommu is "in use" if it has a file descriptor open or if any of +* the groups assigned to the iommu have devices open. */ +static bool __vfio_iommu_inuse (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (iommu->refcnt) +return true; +
drivers/vfio/vfio_main.c line 132
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group, iommu_next); + +if (__vfio_group_devs_inuse (group)) +return true; + } +return false; +}
I looked at how you take care of ref counts ...
This is how the tree of vfio_iommu/vfio_group/vfio_device data Structures is organized (I'll use just iommu/group/dev to make the graph smaller):
iommu
/ \
/ \
group ... group
/ \ / \ / \ / \
dev .. dev dev .. dev
This is how you get a file descriptor for the three kind of objects:
- group : open /dev/vfio/xxx for group xxx
- iommu : group ioctl VFIO_GROUP_GET_IOMMU_FD
- device: group ioctl VFIO_GROUP_GET_DEVICE_FD
Given the above topology, I would assume that:
(1) an iommu is 'inuse' if : a) iommu refcnt > 0, or
b) any of its groups is 'inuse'
(2) a group is 'inuse' if : a) group refcnt > 0, or
b) any of its devices is 'inuse'
(3) a device is 'inuse' if : a) device refcnt > 0
You have coded the 'inuse' logic with these three routines:
__vfio_iommu_inuse, which implements (1) above
and
__vfio_iommu_groups_inuse
__vfio_group_devs_inuse
which are used by __vfio_iommu_inuse.
Why don't you check the group refcnt in __vfio_iommu_groups_inuse?
Would it make sense (and the code more readable) to structure the nested refcnt/inuse check like this?
(The numbers (1)(2)(3) refer to the three 'inuse' conditions above)
(1)__vfio_iommu_inuse
|
+-> check iommu refcnt
+-> __vfio_iommu_groups_inuse
|
+->LOOP: (2)__vfio_iommu_group_inuse<--MISSING
|
+-> check group refcnt<--MISSING
+-> __vfio_group_devs_inuse()
|
+-> LOOP: (3)__vfio_group_dev_inuse<--MISSING
|
+-> check device refcntdrivers/vfio/vfio_main.c line 144
Quoted 11 lines
drivers/vfio/vfio_main.c new file +11
-0+static void __vfio_group_set_iommu (struct vfio_group *group, +struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (group->iommu) +list_del (&group->iommu_next); +if (iommu) +list_add (&group->iommu_next, &iommu->group_list); + + group->iommu = iommu;
If you remove the vfio_device.iommu field (as suggested above in a previous Comment), the block below would not be needed anymore.
Quoted 108 lines
drivers/vfio/vfio_main.c new file +70
-0+list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next); + device->iommu = iommu; + } +} + +static void __vfio_iommu_detach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +BUG_ON (!iommu->domain && device->attached); + +if (!iommu->domain || !device->attached) +return ; + +iommu_detach_device (iommu->domain, device->dev); + device->attached = false; +} + +static void __vfio_iommu_detach_group (struct vfio_iommu *iommu, +struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next); +__vfio_iommu_detach_dev (iommu, device); + } +} + +static int __vfio_iommu_attach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +int ret; + +BUG_ON (device->attached); + +if (!iommu || !iommu->domain) +return -EINVAL ; + + ret =iommu_attach_device (iommu->domain, device->dev); +if (!ret) + device->attached = true; + +return ret; +} + +static int __vfio_iommu_attach_group (struct vfio_iommu *iommu, +struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; +int ret; + + device = list_entry(pos, struct vfio_device, device_next); + ret =__vfio_iommu_attach_dev (iommu, device); +if (ret) { +__vfio_iommu_detach_group (iommu, group); +return ret; + } + } +return 0 ; +} + +/* The iommu is viable, ie. ready to be configured, when all the devices
+ * for all the groups attached to the iommu are bound to their vfio device
+ * drivers (ex. vfio-pci). This sets thedevice_dataprivate data pointer. */drivers/vfio/vfio_main.c new file +33
-0+static bool __vfio_iommu_viable (struct vfio_iommu *iommu) +{ +struct list_head *gpos, *dpos; + +list_for_each (gpos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(gpos, struct vfio_group, iommu_next); + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + device =list_entry (dpos, + struct vfio_device, device_next); + +if (!device->device_data) +return false; + } + } +return true; +} + +static void __vfio_close_iommu (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (!iommu->domain) +return ; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(pos, struct vfio_group, iommu_next); + +__vfio_iommu_detach_group (iommu, group); + }
drivers/vfio/vfio_main.c line 260
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+ +vfio_iommu_unmapall (iommu); + +iommu_domain_free (iommu->domain); + iommu->domain =NULL ; + iommu->mm =NULL ; +} + +/* Open the IOMMU. This gates all access to the iommu or device file +* descriptors and sets current->mm as the exclusive user. */
Given the fn vfio_group_open (ie, 1st object, 2nd operation), I would have called this one __vfio_iommu_open (instead of __vfio_open_iommu).
Is it named __vfio_open_iommu to avoid a conflict with the namespace in vfio_iommu.c?
Quoted 240 lines
drivers/vfio/vfio_main.c new file +40
-0+static int __vfio_open_iommu (struct vfio_iommu *iommu) +{ +struct list_head *pos; +int ret; + +if (!__vfio_iommu_viable (iommu)) +return -EBUSY ; + +if (iommu->domain) +return -EINVAL ; + + iommu->domain =iommu_domain_alloc (iommu->bus); +if (!iommu->domain) +return -EFAULT ; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(pos, struct vfio_group, iommu_next); + + ret =__vfio_iommu_attach_group (iommu, group); +if (ret) { +__vfio_close_iommu (iommu); +return ret; + } + } + +if (!allow_unsafe_intrs && + !iommu_domain_has_cap (iommu->domain,IOMMU_CAP_INTR_REMAP )) { +__vfio_close_iommu (iommu); +return -EFAULT ; + } + + iommu->cache = (iommu_domain_has_cap (iommu->domain, +IOMMU_CAP_CACHE_COHERENCY ) !=0 ); + iommu->mm = current->mm; + +return 0 ; +} + +/* Actively try to tear down the iommu and merged groups. If there are no
+ * open iommu or device fds, we close the iommu. If we close the iommu and
+ * there are also no open group fds, we can futher dissolve the group to
+ * iommu association and free the iommu data structure. */drivers/vfio/vfio_main.c new file +15
-0+static int __vfio_try_dissolve_iommu (struct vfio_iommu *iommu) +{ + +if (__vfio_iommu_inuse (iommu)) +return -EBUSY ; + +__vfio_close_iommu (iommu); + +if (!__vfio_iommu_groups_inuse (iommu)) { +struct list_head *pos, *ppos; + +list_for_each_safe (pos, ppos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group,
iommu_next);drivers/vfio/vfio_main.c new file +64
-0+__vfio_group_set_iommu (group,NULL ); + } + + +kfree (iommu); + } + +return 0 ; +} + +static struct vfio_device *__vfio_lookup_dev (struct device *dev) +{ +struct list_head *gpos; +unsigned int groupid; + +if (iommu_device_group (dev, &groupid)) +return NULL ; + +list_for_each (gpos, &vfio.group_list) { +struct vfio_group *group; +struct list_head *dpos; + + group = list_entry(gpos, struct vfio_group, group_next); + +if (group->groupid != groupid) +continue ; + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + + device =list_entry (dpos, + struct vfio_device, device_next); + +if (device->dev == dev) +return device; + } + } +return NULL ; +} + +/* All release paths simply decrement the refcnt, attempt to teardown +* the iommu and merged groups, and wakeup anything that might be +* waiting if we successfully dissolve anything. */ +static int vfio_do_release (int *refcnt,struct vfio_iommu *iommu) +{ +bool wake; + +mutex_lock (&vfio.lock); + + (*refcnt)--; + wake = (__vfio_try_dissolve_iommu (iommu) ==0 ); + +mutex_unlock (&vfio.lock); + +if (wake) +wake_up (&vfio.release_q); + +return 0 ; +} + +/* +* Device fops - passthrough to vfio device driver w/ device_data +*/ +static int vfio_device_release(struct inode *inode, struct file*filep)
drivers/vfio/vfio_main.c new file +27
-0+{ +struct vfio_device *device = filep->private_data; + +vfio_do_release (&device->refcnt, device->iommu); + + device->ops->put (device->device_data); + +return 0 ; +} + +static long vfio_device_unl_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ +struct vfio_device *device = filep->private_data; + +return device->ops->ioctl (device->device_data, cmd, arg); +} + +static ssize_t vfio_device_read (struct file *filep,char __user *buf, +size_t count,loff_t *ppos) +{ +struct vfio_device *device = filep->private_data; + +return device->ops->read (device->device_data, buf, count, ppos); +} + +static ssize_t vfio_device_write(struct file *filep, const char __user*buf,
drivers/vfio/vfio_main.c new file +8
-0+ size_t count, loff_t *ppos) +{ + struct vfio_device *device = filep->private_data; + +return device->ops->write (device->device_data, buf, count, ppos); +} + +static int vfio_device_mmap(struct file *filep, struct vm_area_struct*vma)
drivers/vfio/vfio_main.c new file +76
-0+{ +struct vfio_device *device = filep->private_data; + +return device->ops->mmap (device->device_data, vma); +} + +#ifdef CONFIG_COMPAT +static long vfio_device_compat_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ + arg = (unsigned long )compat_ptr (arg); +return vfio_device_unl_ioctl (filep, cmd, arg); +} +#endif /* CONFIG_COMPAT */ + +const struct file_operations vfio_device_fops = { + .owner =THIS_MODULE , + .release = vfio_device_release, + .read = vfio_device_read, + .write = vfio_device_write, + .unlocked_ioctl = vfio_device_unl_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = vfio_device_compat_ioctl, +#endif + .mmap = vfio_device_mmap, +}; + +/* +* Group fops +*/ +static int vfio_group_open (struct inode *inode,struct file *filep) +{ +struct vfio_group *group; +int ret =0 ; + +mutex_lock (&vfio.lock); + + group =idr_find (&vfio.idr,iminor (inode)); + +if (!group) { + ret = -ENODEV ; + goto out; + } + + filep->private_data = group; + +if (!group->iommu) { +struct vfio_iommu *iommu; + + iommu =kzalloc (sizeof (*iommu),GFP_KERNEL ); +if (!iommu) { + ret = -ENOMEM ; + goto out; + } +INIT_LIST_HEAD (&iommu->group_list); +INIT_LIST_HEAD (&iommu->dm_list); +mutex_init (&iommu->dgate); + iommu->bus = group->bus; +__vfio_group_set_iommu (group, iommu); + } + group->refcnt++; + +out: +mutex_unlock (&vfio.lock); + +return ret; +} + +static int vfio_group_release (struct inode *inode,struct file *filep) +{ +struct vfio_group *group = filep->private_data; + +return vfio_do_release (&group->refcnt, group->iommu); +} + +/* Attempt to merge the group pointed to by fd into group. The merge-
Quoted 5 lines
ee
+ * group must not have an iommu or any devices open because we cannot
+ * maintain that context across the merge. The merge-er group can be
+ * in use. */
+static intvfio_group_merge(struct vfio_group *group, int fd)
The documentation in vfio.txt explains clearly the logic implemented by the merge/unmerge group ioctls.
However, what you are doing is not merging groups, but rather adding/removing groups to/from iommus (and creating flat lists of groups).
For example, when you do
merge(A,B)
you actually mean to say "merge B to the list of groups assigned to the same iommu as group A".
For the same reason, you do not really need to provide the group you want to unmerge from, which means that instead of
unmerge(A,B)
you would just need
unmerge(B)
I understand the reason why it is not a real merge/unmerge (ie, to keep the original groups so that you can unmerge later) ... however I just wonder if it wouldn't be more natural to implement the VFIO_IOMMU_ADD_GROUP/DEL_GROUP iommu ioctls instead? (the relationships between the data structure would remain the same)
I guess you already discarded this option for some reasons, right? What was the reason?
Quoted 303 lines
drivers/vfio/vfio_main.c new file +65
-0+{ +struct vfio_group *new; +struct vfio_iommu *old_iommu; +struct file *file; +int ret =0 ; +bool opened = false; + +mutex_lock (&vfio.lock); + + file =fget (fd); +if (!file) { + ret = -EBADF ; + goto out_noput; + } + +/* Sanity check, is this really our fd? */ +if (file->f_op != &vfio_group_fops) { + ret = -EINVAL ; + goto out; + } + + new = file->private_data; + +if (!new || new == group || !new->iommu || + new->iommu->domain || new->bus != group->bus) { + ret = -EINVAL ; + goto out; + } + +/* We need to attach all the devices to each domain separately +* in order to validate that the capabilities match for both. */ + ret =__vfio_open_iommu (new->iommu); +if (ret) + goto out; + +if (!group->iommu->domain) { + ret =__vfio_open_iommu (group->iommu); +if (ret) + goto out; + opened = true; + } + +/* If cache coherency doesn't match we'd potentialy need to +* remap existing iommu mappings in the merge-er domain. +* Poor return to bother trying to allow this currently. */ +if (iommu_domain_has_cap (group->iommu->domain, +IOMMU_CAP_CACHE_COHERENCY ) != +iommu_domain_has_cap (new->iommu->domain, +IOMMU_CAP_CACHE_COHERENCY )) { +__vfio_close_iommu (new->iommu); +if (opened) +__vfio_close_iommu (group->iommu); + ret = -EINVAL ; + goto out; + } + +/* Close the iommu for the merge-ee and attach all its devices +* to the merge-er iommu. */ +__vfio_close_iommu (new->iommu); + + ret =__vfio_iommu_attach_group (group->iommu, new); +if (ret) + goto out; + +/* set_iommu unlinks new from the iommu, so save a pointer to it */
drivers/vfio/vfio_main.c new file +94
-0+ old_iommu = new->iommu; +__vfio_group_set_iommu (new, group->iommu); +kfree (old_iommu); + +out: +fput (file); +out_noput: +mutex_unlock (&vfio.lock); +return ret; +} + +/* Unmerge the group pointed to by fd from group. */ +static int vfio_group_unmerge (struct vfio_group *group,int fd) +{ +struct vfio_group *new; +struct vfio_iommu *new_iommu; +struct file *file; +int ret =0 ; + +/* Since the merge-out group is already opened, it needs to +* have an iommu struct associated with it. */ + new_iommu =kzalloc (sizeof (*new_iommu),GFP_KERNEL ); +if (!new_iommu) +return -ENOMEM ; + +INIT_LIST_HEAD (&new_iommu->group_list); +INIT_LIST_HEAD (&new_iommu->dm_list); +mutex_init (&new_iommu->dgate); + new_iommu->bus = group->bus; + +mutex_lock (&vfio.lock); + + file =fget (fd); +if (!file) { + ret = -EBADF ; + goto out_noput; + } + +/* Sanity check, is this really our fd? */ +if (file->f_op != &vfio_group_fops) { + ret = -EINVAL ; + goto out; + } + + new = file->private_data; +if (!new || new == group || new->iommu != group->iommu) { + ret = -EINVAL ; + goto out; + } + +/* We can't merge-out a group with devices still in use. */ +if (__vfio_group_devs_inuse (new)) { + ret = -EBUSY ; + goto out; + } + +__vfio_iommu_detach_group (group->iommu, new); +__vfio_group_set_iommu (new, new_iommu); + +out: +fput (file); +out_noput: +if (ret) +kfree (new_iommu); +mutex_unlock (&vfio.lock); +return ret; +} + +/* Get a new iommu file descriptor. This will open the iommu, setting +* the current->mm ownership if it's not already set. */ +static int vfio_group_get_iommu_fd (struct vfio_group *group) +{ +int ret =0 ; + +mutex_lock (&vfio.lock); + +if (!group->iommu->domain) { + ret =__vfio_open_iommu (group->iommu); +if (ret) + goto out; + } + + ret =anon_inode_getfd ("[vfio-iommu]" , &vfio_iommu_fops, + group->iommu,O_RDWR ); +if (ret <0 ) + goto out; + + group->iommu->refcnt++; +out: +mutex_unlock (&vfio.lock); +return ret; +} + +/* Get a new device file descriptor. This will open the iommu, setting
+ * thecurrent->mmownership if it's not already set. It's difficult to
+ * specify the requirements for matching a user supplied buffer to a
+ * device, so we use a vfio driver callback to test for a match. For
+ * PCI,dev_name(dev)is unique, but other drivers may require including
+ * a parent device string. */
+static intvfio_group_get_device_fd(struct vfio_group *group, char *buf)drivers/vfio/vfio_main.c new file +133
-0+{ +struct vfio_iommu *iommu = group->iommu; +struct list_head *gpos; +int ret = -ENODEV ; + +mutex_lock (&vfio.lock); + +if (!iommu->domain) { + ret =__vfio_open_iommu (iommu); +if (ret) + goto out; + } + +list_for_each (gpos, &iommu->group_list) { +struct list_head *dpos; + + group = list_entry(gpos, struct vfio_group, iommu_next); + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + + device =list_entry (dpos, + struct vfio_device, device_next); + +if (device->ops->match (device->dev, buf)) { +struct file *file; + +if (device->ops->get (device->device_data)) { + ret = -EFAULT ; + goto out; + } + +/* We can't use anon_inode_getfd(), like above +* because we need to modify the f_mode flags +* directly to allow more than just ioctls */ + ret =get_unused_fd (); +if (ret <0 ) { + device->ops->put (device->device_data); + goto out; + } + + file =anon_inode_getfile ("[vfio-device]" , + &vfio_device_fops, + device,O_RDWR ); +if (IS_ERR (file)) { +put_unused_fd (ret); + ret =PTR_ERR (file); + device->ops->put (device->device_data); + goto out; + } + +/* Todo: add an anon_inode interface to do +* this. Appears to be missing by lack of +* need rather than explicitly prevented. +* Now there's need. */ + file->f_mode |= (FMODE_LSEEK | +FMODE_PREAD | +FMODE_PWRITE ); + +fd_install (ret, file); + + device->refcnt++; + goto out; + } + } + } +out: +mutex_unlock (&vfio.lock); +return ret; +} + +static long vfio_group_unl_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ +struct vfio_group *group = filep->private_data; + +if (cmd ==VFIO_GROUP_GET_FLAGS ) { +u64 flags =0 ; + +mutex_lock (&vfio.lock); +if (__vfio_iommu_viable (group->iommu)) + flags |=VFIO_GROUP_FLAGS_VIABLE ; +mutex_unlock (&vfio.lock); + +if (group->iommu->mm) + flags |=VFIO_GROUP_FLAGS_MM_LOCKED ; + +return put_user (flags, (u64 __user *)arg); + } + +/* Below commands are restricted once the mm is set */ +if (group->iommu->mm && group->iommu->mm != current->mm) +return -EPERM ; +if (cmd ==VFIO_GROUP_MERGE || cmd ==VFIO_GROUP_UNMERGE ) { +int fd; + +if (get_user (fd, (int __user *)arg)) +return -EFAULT ; +if (fd <0 ) +return -EINVAL ; + +if (cmd ==VFIO_GROUP_MERGE ) +return vfio_group_merge (group, fd); +else +return vfio_group_unmerge (group, fd); + }else if (cmd ==VFIO_GROUP_GET_IOMMU_FD ) { +return vfio_group_get_iommu_fd (group); + }else if (cmd ==VFIO_GROUP_GET_DEVICE_FD ) { +char *buf; +int ret; + + buf =strndup_user ((const char __user *)arg,PAGE_SIZE ); +if (IS_ERR (buf)) +return PTR_ERR (buf); + + ret =vfio_group_get_device_fd (group, buf); +kfree (buf); +return ret; + } + +return -ENOSYS ; +} + +#ifdef CONFIG_COMPAT +static long vfio_group_compat_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ + arg = (unsigned long )compat_ptr (arg); +return vfio_group_unl_ioctl (filep, cmd, arg); +} +#endif /* CONFIG_COMPAT */ + +static const struct file_operations vfio_group_fops = {
drivers/vfio/vfio_main.c line 806
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+ .owner = THIS_MODULE, + .open = vfio_group_open, + .release = vfio_group_release, + .unlocked_ioctl = vfio_group_unl_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = vfio_group_compat_ioctl, +#endif +}; + +/* iommu fd release hook */
Given vfio_device_release and
vfio_group_release (ie, 1st object, 2nd operation), I was
going to suggest renaming the fn below to vfio_iommu_release, but then I saw the latter name being already used in vfio_iommu.c ... a bit confusing but I guess it's ok then.
Quoted 95 lines
drivers/vfio/vfio_main.c new file +11
-0+int vfio_release_iommu (struct vfio_iommu *iommu) +{ +return vfio_do_release (&iommu->refcnt, iommu); +} + +/* +* VFIO driver API +*/ + +/* Add a new device to the vfio framework with associated vfio driver +* callbacks. This is the entry point for vfio drivers to register devices. */
+intvfio_group_add_dev(struct device *dev, const struct vfio_device_ops *ops)drivers/vfio/vfio_main.c new file +59
-0+{ + struct list_head *pos; + struct vfio_group *group = NULL; + struct vfio_device *device = NULL; + unsigned int groupid; + int ret = 0; + bool new_group = false; + + if (!ops) + return -EINVAL; + + if (iommu_device_group(dev, &groupid)) + return -ENODEV; + + mutex_lock(&vfio.lock); + + list_for_each(pos, &vfio.group_list) { + group = list_entry(pos, struct vfio_group, group_next); + if (group->groupid == groupid) + break; + group = NULL; + } + + if (!group) { + int minor; + + if (unlikely(idr_pre_get(&vfio.idr, GFP_KERNEL) == 0)) { + ret = -ENOMEM; + goto out; + } + + group = kzalloc(sizeof(*group), GFP_KERNEL); + if (!group) { + ret = -ENOMEM; + goto out; + } + + group->groupid = groupid; + INIT_LIST_HEAD(&group->device_list); + + ret = idr_get_new(&vfio.idr, group, &minor); + if (ret == 0 && minor > MINORMASK) { + idr_remove(&vfio.idr, minor); + kfree(group); + ret = -ENOSPC; + goto out; + } + + group->devt = MKDEV(MAJOR(vfio.devt), minor); + device_create(vfio.class, NULL, group->devt, + group, "%u", groupid); + + group->bus = dev->bus; + list_add(&group->group_next, &vfio.group_list); + new_group = true; + } else { + if (group->bus != dev->bus) { + printk(KERN_WARNING + "Error: IOMMU group ID conflict. Group ID %u"
drivers/vfio/vfio_main.c new file +20
-0+"on both bus %s and %s\n" , groupid, + group->bus->name, dev->bus->name); + ret = -EFAULT ; + goto out; + } + +list_for_each (pos, &group->device_list) { + device =list_entry (pos, + struct vfio_device, device_next); +if (device->dev == dev) +break ; + device =NULL ; + } + } + + if (!device) { + if (__vfio_group_devs_inuse(group) || + (group->iommu && group->iommu->refcnt)) { + printk(KERN_WARNING + "Adding device %s to group %u while group isalready in use!!\n",
drivers/vfio/vfio_main.c line 899
Quoted 2 lines
drivers/vfio/vfio_main.c new file +2
-0+ dev_name(dev), group->groupid); +/* XXX How to prevent other drivers from claiming? */
Here we are adding a device (not yet assigned to a vfio bus) to a group that is already in use.
Given that it would not be acceptable for this device to get assigned to a non vfio driver, why not forcing such assignment here then? I am not sure though what the best way to do it would be. What about something like this:
- when the bus vfio-pci processes the BUS_NOTIFY_ADD_DEVICE notification it assigns to the device a PCI ID that will make sure the vfio-pci's probe routine will be invoked (and no other driver can therefore claim the device). That PCI ID would have to be added to the vfio_pci_driver's id_table (it would be the exception to the "only dynamic IDs" rule). Too hackish?
Quoted 9 lines
drivers/vfio/vfio_main.c new file +9
-0+ } + + device =kzalloc (sizeof (*device),GFP_KERNEL ); +if (!device) { +/* If we just created this group, tear it down */ +if (new_group) { +list_del (&group->group_next); +device_destroy (vfio.class, group->devt); +idr_remove (&vfio.idr,MINOR (group->devt));
drivers/vfio/vfio_main.c line 918
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+kfree (group); + } + ret = -ENOMEM ; + goto out; + } + +list_add (&device->device_next, &group->device_list); + device->dev = dev; + device->ops = ops; + device->iommu = group->iommu;/* NULL if new */
Shouldn't you check the return code of __vfio_iommu_attach_dev?
drivers/vfio/vfio_main.c line 927
Quoted 9 lines
drivers/vfio/vfio_main.c new file +9
-0+__vfio_iommu_attach_dev (group->iommu, device); + } +out: +mutex_unlock (&vfio.lock); +return ret; +} +EXPORT_SYMBOL_GPL (vfio_group_add_dev); + +/* Remove a device from the vfio framework */
This fn below does not return any error code. Ok ...
However, there are a number of errors case that you test, for example
- device that does not belong to any group (according to iommu API)
- device that belongs to a group but that does not appear in the list of devices of the vfio_group structure.
Are the above two errors checks just paranoia or are those errors actually possible? If they were possible, shouldn't we generate a warning (most probably it would be a bug in the code)?
Quoted 111 lines
drivers/vfio/vfio_main.c new file +64
-0+void vfio_group_del_dev (struct device *dev) +{ +struct list_head *pos; +struct vfio_group *group =NULL ; +struct vfio_device *device =NULL ; +unsigned int groupid; + +if (iommu_device_group (dev, &groupid)) +return ; + +mutex_lock (&vfio.lock); + +list_for_each (pos, &vfio.group_list) { + group = list_entry(pos, struct vfio_group, group_next); +if (group->groupid == groupid) +break ; + group =NULL ; + } + +if (!group) + goto out; + +list_for_each (pos, &group->device_list) { + device = list_entry(pos, struct vfio_device, device_next); +if (device->dev == dev) +break ; + device =NULL ; + } + +if (!device) + goto out; + +BUG_ON (device->refcnt); + +if (device->attached) +__vfio_iommu_detach_dev (group->iommu, device); + +list_del (&device->device_next); +kfree (device); + +/* If this was the only device in the group, remove the group. +* Note that we intentionally unmerge empty groups here if the +* group fd isn't opened. */ +if (list_empty (&group->device_list) && group->refcnt ==0 ) { +struct vfio_iommu *iommu = group->iommu; + +if (iommu) { +__vfio_group_set_iommu (group,NULL ); +__vfio_try_dissolve_iommu (iommu); + } + +device_destroy (vfio.class, group->devt); +idr_remove (&vfio.idr,MINOR (group->devt)); +list_del (&group->group_next); +kfree (group); + } +out: +mutex_unlock (&vfio.lock); +} +EXPORT_SYMBOL_GPL (vfio_group_del_dev); + +/* When a device is bound to a vfio device driver (ex. vfio-pci), this +* entry point is used to mark the device usable (viable). The vfio +* device driver associates a private device_data struct with the device
+ * here, which will later be return forvfio_device_fopscallbacks. */drivers/vfio/vfio_main.c new file +23
-0+int vfio_bind_dev (struct device *dev,void *device_data) +{ +struct vfio_device *device; +int ret = -EINVAL ; + +BUG_ON (!device_data); + +mutex_lock (&vfio.lock); + + device =__vfio_lookup_dev (dev); + +BUG_ON (!device); + + ret =dev_set_drvdata (dev, device); +if (!ret) + device->device_data = device_data; + +mutex_unlock (&vfio.lock); +return ret; +} +EXPORT_SYMBOL_GPL (vfio_bind_dev); + +/* A device is only removeable if the iommu for the group is not in use. */
drivers/vfio/vfio_main.c new file +14
-0+static bool vfio_device_removeable (struct vfio_device *device) +{ +bool ret = true; + +mutex_lock (&vfio.lock); + +if (device->iommu &&__vfio_iommu_inuse (device->iommu)) + ret = false; + +mutex_unlock (&vfio.lock); +return ret; +} + +/* Notify vfio that a device is being unbound from the vfio device driver
+ * and return the device privatedevice_datapointer. If the group is
+ * in use, we need to block or take other measures to make it safe for
+ * the device to be removed from the iommu. */drivers/vfio/vfio_main.c new file +3
-0+void *vfio_unbind_dev (struct device *dev) +{ +struct vfio_device *device =dev_get_drvdata (dev);
drivers/vfio/vfio_main.c line 1045
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+void *device_data; + +BUG_ON (!device); + +again: +if (!vfio_device_removeable (device)) { +/* XXX signal for all devices in group to be removed or +* resort to killing the process holding the device fds. +* For now just block waiting for releases to wake us. */ +wait_event (vfio.release_q,vfio_device_removeable (device));
Any new idea/proposal on how to handle this situation?
The last one I remember was to leave the soft/hard/etc timeout handling in userspace and implement it as a sort of policy. Is that one still the most likely candidate solution to handle this situation?
Quoted 317 lines
drivers/vfio/vfio_main.c new file +5
-0+ } + +mutex_lock (&vfio.lock); + +/* Need to re-check that the device is still removeable under lock. */
drivers/vfio/vfio_main.c new file +101
-0+if (device->iommu &&__vfio_iommu_inuse (device->iommu)) { +mutex_unlock (&vfio.lock); + goto again; + } + + device_data = device->device_data; + + device->device_data =NULL ; +dev_set_drvdata (dev,NULL ); + +mutex_unlock (&vfio.lock); +return device_data; +} +EXPORT_SYMBOL_GPL (vfio_unbind_dev); + +/* +* Module/class support +*/ +static void vfio_class_release (struct kref *kref) +{ +class_destroy (vfio.class); + vfio.class =NULL ; +} + +static char *vfio_devnode (struct device *dev,mode_t *mode) +{ +return kasprintf (GFP_KERNEL ,"vfio/%s" ,dev_name (dev)); +} + +static int __init vfio_init (void ) +{ +int ret; + +idr_init (&vfio.idr); +mutex_init (&vfio.lock); +INIT_LIST_HEAD (&vfio.group_list); +init_waitqueue_head (&vfio.release_q); + +kref_init (&vfio.kref); + vfio.class =class_create (THIS_MODULE ,"vfio" ); +if (IS_ERR (vfio.class)) { + ret =PTR_ERR (vfio.class); + goto err_class; + } + + vfio.class->devnode = vfio_devnode; + +/* FIXME - how many minors to allocate... all of them! */ + ret =alloc_chrdev_region (&vfio.devt,0 ,MINORMASK ,"vfio" ); +if (ret) + goto err_chrdev; + +cdev_init (&vfio.cdev, &vfio_group_fops); + ret =cdev_add (&vfio.cdev, vfio.devt,MINORMASK ); +if (ret) + goto err_cdev; + +pr_info (DRIVER_DESC " version: " DRIVER_VERSION "\n" ); + +return 0 ; + +err_cdev: +unregister_chrdev_region (vfio.devt,MINORMASK ); +err_chrdev: +kref_put (&vfio.kref, vfio_class_release); +err_class: +return ret; +} + +static void __exit vfio_cleanup(void) +{ +struct list_head *gpos, *gppos; + +list_for_each_safe (gpos, gppos, &vfio.group_list) { +struct vfio_group *group; +struct list_head *dpos, *dppos; + + group = list_entry(gpos, struct vfio_group, group_next); + +list_for_each_safe (dpos, dppos, &group->device_list) { +struct vfio_device *device; + + device =list_entry (dpos, + struct vfio_device, device_next); +vfio_group_del_dev (device->dev); + } + } + +idr_destroy (&vfio.idr); +cdev_del (&vfio.cdev); +unregister_chrdev_region (vfio.devt,MINORMASK ); +kref_put (&vfio.kref, vfio_class_release); +} + +module_init (vfio_init); +module_exit (vfio_cleanup); + +MODULE_VERSION (DRIVER_VERSION ); +MODULE_LICENSE ("GPL v2" ); +MODULE_AUTHOR (DRIVER_AUTHOR ); +MODULE_DESCRIPTION (DRIVER_DESC );drivers/vfio/vfio_private.h new file +5
-0@@ -0,0 +1,34 @@ +/* +* Copyright (C) 2011 Red Hat, Inc. All rights reserved. +* Author: Alex Williamson <[email protected]> +* +* This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Derived from original vfio:
+ * Copyright 2010 Cisco Systems, Inc. All rights reserved.
+ * Author: Tom Lyon, [email protected]
+ */drivers/vfio/vfio_private.h new file +22
-0+ +#include <linux/list.h> +#include <linux/mutex.h> + +#ifndef VFIO_PRIVATE_H +#define VFIO_PRIVATE_H + +struct vfio_iommu { +struct iommu_domain *domain; +struct bus_type *bus; +struct mutex dgate; +struct list_head dm_list; +struct mm_struct *mm; +struct list_head group_list; +int refcnt; +bool cache; +}; + +extern int vfio_release_iommu (struct vfio_iommu *iommu); +extern void vfio_iommu_unmapall (struct vfio_iommu *iommu); + +#endif /* VFIO_PRIVATE_H */ include/linux/vfio.h new file +5
-0@@ -0,0 +1,155 @@ +/* +* Copyright 2010 Cisco Systems, Inc. All rights reserved. +* Author: Tom Lyon, [email protected] +* +* This program is free software; you may redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Portions derived fromdrivers/uio/uio.c:
+ * Copyright(C) 2005, Benedikt Spranger <[email protected]>
+ * Copyright(C) 2005, Thomas Gleixner <[email protected]>
+ * Copyright(C) 2006, Hans J. Koch <[email protected]>
+ * Copyright(C) 2006, Greg Kroah-Hartman <[email protected]>
+ *
+ * Portions derived fromdrivers/uio/uio_pci_generic.c:
+ * Copyright (C) 2009 Red Hat, Inc.
+ * Author: Michael S. Tsirkin <[email protected]>
+ */include/linux/vfio.h new file +55
-0+#include <linux/types.h> + +#ifndef VFIO_H +#define VFIO_H + +#ifdef __KERNEL__ + +struct vfio_device_ops { + bool (*match)(struct device *, char *); + int (*get)(void *); + void (*put)(void *); + ssize_t (*read)(void *, char __user *, + size_t, loff_t *); + ssize_t (*write)(void *, const char __user *, + size_t, loff_t *); + long (*ioctl)(void *, unsigned int, unsigned long); + int (*mmap)(void *, struct vm_area_struct *); +}; + +extern int vfio_group_add_dev(struct device *device, + const struct vfio_device_ops *ops); +extern void vfio_group_del_dev(struct device *device); +extern int vfio_bind_dev(struct device *device, void *device_data); +extern void *vfio_unbind_dev(struct device *device); + +#endif /* __KERNEL__ */ + +/* + * VFIO driver - allow mapping and use of certain devices + * in unprivileged user processes. (If IOMMU is present) + * Especially useful for Virtual Function parts of SR-IOV devices + */ + + +/* Kernel & User level defines for ioctls */ + +#define VFIO_GROUP_GET_FLAGS _IOR(';', 100, __u64) + #define VFIO_GROUP_FLAGS_VIABLE (1 << 0) + #define VFIO_GROUP_FLAGS_MM_LOCKED (1 << 1) +#define VFIO_GROUP_MERGE _IOW(';', 101, int) +#define VFIO_GROUP_UNMERGE _IOW(';', 102, int) +#define VFIO_GROUP_GET_IOMMU_FD _IO(';', 103) +#define VFIO_GROUP_GET_DEVICE_FD _IOW(';', 104, char *) + +/* + * Structure for DMA mapping of user buffers + * vaddr, dmaaddr, and size must all be page aligned + */ +struct vfio_dma_map { + __u64 len; /* length of structure */ + __u64 vaddr; /* process virtual addr */ + __u64 dmaaddr; /* desired and/or returned dma address */ + __u64 size; /* size in bytes */ + __u64 flags; +#define VFIO_DMA_MAP_FLAG_WRITE (1 << 0) /* req writeable DMAmem */
include/linux/vfio.h new file +6
-0+}; + +#define VFIO_IOMMU_GET_FLAGS _IOR(';', 105, __u64) +/* Does the IOMMU support mapping any IOVA to any virtual address? */ +#define VFIO_IOMMU_FLAGS_MAP_ANY (1 << 0) +#define VFIO_IOMMU_MAP_DMA _IOWR(';', 106, struct
vfio_dma_map)
+#defineVFIO_IOMMU_UNMAP_DMA_IOWR(';', 107, structvfio_dma_map)include/linux/vfio.h new file +20
-0+ +#define VFIO_DEVICE_GET_FLAGS _IOR(';', 108, __u64) +#define VFIO_DEVICE_FLAGS_PCI (1 << 0) +#define VFIO_DEVICE_FLAGS_DT (1 << 1) +#define VFIO_DEVICE_FLAGS_RESET (1 << 2) +#define VFIO_DEVICE_GET_NUM_REGIONS _IOR(';', 109, int) + +struct vfio_region_info { +__u32 len;/* length of structure */ +__u32 index;/* region number */ +__u64 size;/* size in bytes of region */ +__u64 offset;/* start offset of region */ +__u64 flags; +#define VFIO_REGION_INFO_FLAG_MMAP (1 << 0) +#define VFIO_REGION_INFO_FLAG_RO (1 << 1) +#define VFIO_REGION_INFO_FLAG_PHYS_VALID (1 << 2) +__u64 phys;/* physical address of region */ +}; + +#define VFIO_DEVICE_GET_REGION_INFO _IOWR(';', 110, struct
vfio_region_info)include/linux/vfio.h new file +12
-0+ +#define VFIO_DEVICE_GET_NUM_IRQS _IOR(';', 111, int) + +struct vfio_irq_info { +__u32 len;/* length of structure */ +__u32 index;/* IRQ number */ +__u32 count;/* number of individual IRQs */ +__u32 flags; +#define VFIO_IRQ_INFO_FLAG_LEVEL (1 << 0) +}; + +#define VFIO_DEVICE_GET_IRQ_INFO _IOWR(';', 112, struct
vfio_irq_info)include/linux/vfio.h new file +2
-0+ +/* Set IRQ eventfds, arg[0] = index, arg[1] = count, arg[2-n] = eventfds */
include/linux/vfio.h new file +19
-0+#define VFIO_DEVICE_SET_IRQ_EVENTFDS _IOW(';', 113, int) + +/* Unmask IRQ index, arg[0] = index */ +#define VFIO_DEVICE_UNMASK_IRQ _IOW(';', 114, int) + +/* Set unmask eventfd, arg[0] = index, arg[1] = eventfd */ +#define VFIO_DEVICE_SET_UNMASK_IRQ_EVENTFD _IOW(';', 115, int) + +#define VFIO_DEVICE_RESET _IO(';', 116) + +struct vfio_dtpath { +__u32 len;/* length of structure */ +__u32 index; +__u64 flags; +#define VFIO_DTPATH_FLAGS_REGION (1 << 0) +#define VFIO_DTPATH_FLAGS_IRQ (1 << 1) +char *path; +}; +#define VFIO_DEVICE_GET_DTPATH _IOWR(';', 117, struct
vfio_dtpath)include/linux/vfio.h new file +11
-0+ +struct vfio_dtindex { +__u32 len;/* length of structure */ +__u32 index; +__u32 prop_type; +__u32 prop_index; +__u64 flags; +#define VFIO_DTINDEX_FLAGS_REGION (1 << 0) +#define VFIO_DTINDEX_FLAGS_IRQ (1 << 1) +}; +#define VFIO_DEVICE_GET_DTINDEX _IOWR(';', 118, struct
vfio_dtindex)
include/linux/vfio.h line 155
Quoted 2 lines
include/linux/vfio.h new file +2
-0+ +#endif /* VFIO_H */
/Chris
SGVyZSBhcmUgZmV3IG1pbm9yIGNvbW1lbnRzIG9uIHZmaW9faW9tbXUuYyAuLi4NCg0KPiBkaWZm IC0tZ2l0IGEvZHJpdmVycy92ZmlvL3ZmaW9faW9tbXUuYyBiL2RyaXZlcnMvdmZpby92ZmlvX2lv bW11LmMNCj4gbmV3IGZpbGUgbW9kZSAxMDA2NDQNCj4gaW5kZXggMDAwMDAwMC4uMDI5ZGFlMw0K PiAtLS0gL2Rldi9udWxsDQo+ICsrKyBiL2RyaXZlcnMvdmZpby92ZmlvX2lvbW11LmMNCj4gQEAg LTAsMCArMSw1MzAgQEANCj4gKy8qDQo+ICsgKiBWRklPOiBJT01NVSBETUEgbWFwcGluZyBzdXBw b3J0DQo+ICsgKg0KPiArICogQ29weXJpZ2h0IChDKSAyMDExIFJlZCBIYXQsIEluYy4gIEFsbCBy aWdodHMgcmVzZXJ2ZWQuDQo+ICsgKiAgICAgQXV0aG9yOiBBbGV4IFdpbGxpYW1zb24gPGFsZXgu d2lsbGlhbXNvbkByZWRoYXQuY29tPg0KPiArICoNCj4gKyAqIFRoaXMgcHJvZ3JhbSBpcyBmcmVl IHNvZnR3YXJlOyB5b3UgY2FuIHJlZGlzdHJpYnV0ZSBpdCBhbmQvb3INCj4gbW9kaWZ5DQo+ICsg KiBpdCB1bmRlciB0aGUgdGVybXMgb2YgdGhlIEdOVSBHZW5lcmFsIFB1YmxpYyBMaWNlbnNlIHZl cnNpb24gMiBhcw0KPiArICogcHVibGlzaGVkIGJ5IHRoZSBGcmVlIFNvZnR3YXJlIEZvdW5kYXRp b24uDQo+ICsgKg0KPiArICogRGVyaXZlZCBmcm9tIG9yaWdpbmFsIHZmaW86DQo+ICsgKiBDb3B5 cmlnaHQgMjAxMCBDaXNjbyBTeXN0ZW1zLCBJbmMuICBBbGwgcmlnaHRzIHJlc2VydmVkLg0KPiAr ICogQXV0aG9yOiBUb20gTHlvbiwgcHVnc0BjaXNjby5jb20NCj4gKyAqLw0KPiArDQo+ICsjaW5j bHVkZSA8bGludXgvY29tcGF0Lmg+DQo+ICsjaW5jbHVkZSA8bGludXgvZGV2aWNlLmg+DQo+ICsj aW5jbHVkZSA8bGludXgvZnMuaD4NCj4gKyNpbmNsdWRlIDxsaW51eC9pb21tdS5oPg0KPiArI2lu Y2x1ZGUgPGxpbnV4L21vZHVsZS5oPg0KPiArI2luY2x1ZGUgPGxpbnV4L21tLmg+DQo+ICsjaW5j bHVkZSA8bGludXgvc2NoZWQuaD4NCj4gKyNpbmNsdWRlIDxsaW51eC9zbGFiLmg+DQo+ICsjaW5j bHVkZSA8bGludXgvdWFjY2Vzcy5oPg0KPiArI2luY2x1ZGUgPGxpbnV4L3ZmaW8uaD4NCj4gKyNp bmNsdWRlIDxsaW51eC93b3JrcXVldWUuaD4NCj4gKw0KPiArI2luY2x1ZGUgInZmaW9fcHJpdmF0 ZS5oIg0KDQpEb2Vzbid0IHRoZSAnZG1hXycgIHByZWZpeCBiZWxvbmcgdG8gdGhlIGdlbmVyaWMg RE1BIGNvZGU/DQoNCj4gK3N0cnVjdCBkbWFfbWFwX3BhZ2Ugew0KPiArCXN0cnVjdCBsaXN0X2hl YWQJbGlzdDsNCj4gKwlkbWFfYWRkcl90CQlkYWRkcjsNCj4gKwl1bnNpZ25lZCBsb25nCQl2YWRk cjsNCj4gKwlpbnQJCQlucGFnZTsNCj4gKwlpbnQJCQlyZHdyOw0KPiArfTsNCj4gKw0KPiArLyoN Cj4gKyAqIFRoaXMgY29kZSBoYW5kbGVzIG1hcHBpbmcgYW5kIHVubWFwcGluZyBvZiB1c2VyIGRh dGEgYnVmZmVycw0KPiArICogaW50byBETUEnYmxlIHNwYWNlIHVzaW5nIHRoZSBJT01NVQ0KPiAr ICovDQo+ICsNCj4gKyNkZWZpbmUgTlBBR0VfVE9fU0laRShucGFnZSkJKChzaXplX3QpKG5wYWdl KSA8PCBQQUdFX1NISUZUKQ0KPiArDQo+ICtzdHJ1Y3Qgdndvcmsgew0KPiArCXN0cnVjdCBtbV9z dHJ1Y3QJKm1tOw0KPiArCWludAkJCW5wYWdlOw0KPiArCXN0cnVjdCB3b3JrX3N0cnVjdAl3b3Jr Ow0KPiArfTsNCj4gKw0KPiArLyogZGVsYXllZCBkZWNyZW1lbnQgZm9yIGxvY2tlZF92bSAqLw0K PiArc3RhdGljIHZvaWQgdmZpb19sb2NrX2FjY3RfYmcoc3RydWN0IHdvcmtfc3RydWN0ICp3b3Jr KQ0KPiArew0KPiArCXN0cnVjdCB2d29yayAqdndvcmsgPSBjb250YWluZXJfb2Yod29yaywgc3Ry dWN0IHZ3b3JrLCB3b3JrKTsNCj4gKwlzdHJ1Y3QgbW1fc3RydWN0ICptbTsNCj4gKw0KPiArCW1t ID0gdndvcmstPm1tOw0KPiArCWRvd25fd3JpdGUoJm1tLT5tbWFwX3NlbSk7DQo+ICsJbW0tPmxv Y2tlZF92bSArPSB2d29yay0+bnBhZ2U7DQo+ICsJdXBfd3JpdGUoJm1tLT5tbWFwX3NlbSk7DQo+ ICsJbW1wdXQobW0pOwkJLyogdW5yZWYgbW0gKi8NCj4gKwlrZnJlZSh2d29yayk7DQo+ICt9DQo+ ICsNCj4gK3N0YXRpYyB2b2lkIHZmaW9fbG9ja19hY2N0KGludCBucGFnZSkNCj4gK3sNCj4gKwlz dHJ1Y3QgdndvcmsgKnZ3b3JrOw0KPiArCXN0cnVjdCBtbV9zdHJ1Y3QgKm1tOw0KPiArDQo+ICsJ aWYgKCFjdXJyZW50LT5tbSkgew0KPiArCQkvKiBwcm9jZXNzIGV4aXRlZCAqLw0KPiArCQlyZXR1 cm47DQo+ICsJfQ0KPiArCWlmIChkb3duX3dyaXRlX3RyeWxvY2soJmN1cnJlbnQtPm1tLT5tbWFw X3NlbSkpIHsNCj4gKwkJY3VycmVudC0+bW0tPmxvY2tlZF92bSArPSBucGFnZTsNCj4gKwkJdXBf d3JpdGUoJmN1cnJlbnQtPm1tLT5tbWFwX3NlbSk7DQo+ICsJCXJldHVybjsNCj4gKwl9DQo+ICsJ LyoNCj4gKwkgKiBDb3VsZG4ndCBnZXQgbW1hcF9zZW0gbG9jaywgc28gbXVzdCBzZXR1cCB0byBk ZWNyZW1lbnQNCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg ICAgICAgIF5eXl5eXl5eXg0KDQpJbmNyZW1lbnQ/DQoNCj4gKwkgKiBtbS0+bG9ja2VkX3ZtIGxh dGVyLiBJZiBsb2NrZWRfdm0gd2VyZSBhdG9taWMsIHdlIHdvdWxkbid0DQo+ICsJICogbmVlZCB0 aGlzIHNpbGxpbmVzcw0KPiArCSAqLw0KPiArCXZ3b3JrID0ga21hbGxvYyhzaXplb2Yoc3RydWN0 IHZ3b3JrKSwgR0ZQX0tFUk5FTCk7DQo+ICsJaWYgKCF2d29yaykNCj4gKwkJcmV0dXJuOw0KPiAr CW1tID0gZ2V0X3Rhc2tfbW0oY3VycmVudCk7CS8qIHRha2UgcmVmIG1tICovDQo+ICsJaWYgKCFt bSkgew0KPiArCQlrZnJlZSh2d29yayk7DQo+ICsJCXJldHVybjsNCj4gKwl9DQo+ICsJSU5JVF9X T1JLKCZ2d29yay0+d29yaywgdmZpb19sb2NrX2FjY3RfYmcpOw0KPiArCXZ3b3JrLT5tbSA9IG1t Ow0KPiArCXZ3b3JrLT5ucGFnZSA9IG5wYWdlOw0KPiArCXNjaGVkdWxlX3dvcmsoJnZ3b3JrLT53 b3JrKTsNCj4gK30NCj4gKw0KPiArLyogU29tZSBtYXBwaW5ncyBhcmVuJ3QgYmFja2VkIGJ5IGEg c3RydWN0IHBhZ2UsIGZvciBleGFtcGxlIGFuIG1tYXAnZA0KPiArICogTU1JTyByYW5nZSBmb3Ig b3VyIG93biBvciBhbm90aGVyIGRldmljZS4gIFRoZXNlIHVzZSBhIGRpZmZlcmVudA0KPiArICog cGZuIGNvbnZlcnNpb24gYW5kIHNob3VsZG4ndCBiZSB0cmFja2VkIGFzIGxvY2tlZCBwYWdlcy4g Ki8NCj4gK3N0YXRpYyBpbnQgaXNfaW52YWxpZF9yZXNlcnZlZF9wZm4odW5zaWduZWQgbG9uZyBw Zm4pDQo+ICt7DQo+ICsJaWYgKHBmbl92YWxpZChwZm4pKSB7DQo+ICsJCWludCByZXNlcnZlZDsN Cj4gKwkJc3RydWN0IHBhZ2UgKnRhaWwgPSBwZm5fdG9fcGFnZShwZm4pOw0KPiArCQlzdHJ1Y3Qg cGFnZSAqaGVhZCA9IGNvbXBvdW5kX3RyYW5zX2hlYWQodGFpbCk7DQo+ICsJCXJlc2VydmVkID0g UGFnZVJlc2VydmVkKGhlYWQpOw0KPiArCQlpZiAoaGVhZCAhPSB0YWlsKSB7DQo+ICsJCQkvKiAi aGVhZCIgaXMgbm90IGEgZGFuZ2xpbmcgcG9pbnRlcg0KPiArCQkJICogKGNvbXBvdW5kX3RyYW5z X2hlYWQgdGFrZXMgY2FyZSBvZiB0aGF0KQ0KPiArCQkJICogYnV0IHRoZSBodWdlcGFnZSBtYXkg aGF2ZSBiZWVuIHNwbGl0DQo+ICsJCQkgKiBmcm9tIHVuZGVyIHVzIChhbmQgd2UgbWF5IG5vdCBo b2xkIGENCj4gKwkJCSAqIHJlZmVyZW5jZSBjb3VudCBvbiB0aGUgaGVhZCBwYWdlIHNvIGl0IGNh bg0KPiArCQkJICogYmUgcmV1c2VkIGJlZm9yZSB3ZSBydW4gUGFnZVJlZmVyZW5jZWQpLCBzbw0K PiArCQkJICogd2UndmUgdG8gY2hlY2sgUGFnZVRhaWwgYmVmb3JlIHJldHVybmluZw0KPiArCQkJ ICogd2hhdCB3ZSBqdXN0IHJlYWQuDQo+ICsJCQkgKi8NCj4gKwkJCXNtcF9ybWIoKTsNCj4gKwkJ CWlmIChQYWdlVGFpbCh0YWlsKSkNCj4gKwkJCQlyZXR1cm4gcmVzZXJ2ZWQ7DQo+ICsJCX0NCj4g KwkJcmV0dXJuIFBhZ2VSZXNlcnZlZCh0YWlsKTsNCj4gKwl9DQo+ICsNCj4gKwlyZXR1cm4gdHJ1 ZTsNCj4gK30NCj4gKw0KPiArc3RhdGljIGludCBwdXRfcGZuKHVuc2lnbmVkIGxvbmcgcGZuLCBp bnQgcmR3cikNCj4gK3sNCj4gKwlpZiAoIWlzX2ludmFsaWRfcmVzZXJ2ZWRfcGZuKHBmbikpIHsN Cj4gKwkJc3RydWN0IHBhZ2UgKnBhZ2UgPSBwZm5fdG9fcGFnZShwZm4pOw0KPiArCQlpZiAocmR3 cikNCj4gKwkJCVNldFBhZ2VEaXJ0eShwYWdlKTsNCj4gKwkJcHV0X3BhZ2UocGFnZSk7DQo+ICsJ CXJldHVybiAxOw0KPiArCX0NCj4gKwlyZXR1cm4gMDsNCj4gK30NCj4gKw0KPiArLyogVW5tYXAg RE1BIHJlZ2lvbiAqLw0KPiArLyogZGdhdGUgbXVzdCBiZSBoZWxkICovDQo+ICtzdGF0aWMgaW50 IF9fdmZpb19kbWFfdW5tYXAoc3RydWN0IHZmaW9faW9tbXUgKmlvbW11LCB1bnNpZ25lZCBsb25n DQo+IGlvdmEsDQo+ICsJCQkgICAgaW50IG5wYWdlLCBpbnQgcmR3cikNCj4gK3sNCj4gKwlpbnQg aSwgdW5sb2NrZWQgPSAwOw0KPiArDQo+ICsJZm9yIChpID0gMDsgaSA8IG5wYWdlOyBpKyssIGlv dmEgKz0gUEFHRV9TSVpFKSB7DQo+ICsJCXVuc2lnbmVkIGxvbmcgcGZuOw0KPiArDQo+ICsJCXBm biA9IGlvbW11X2lvdmFfdG9fcGh5cyhpb21tdS0+ZG9tYWluLCBpb3ZhKSA+Pg0KPiBQQUdFX1NI SUZUOw0KPiArCQlpZiAocGZuKSB7DQo+ICsJCQlpb21tdV91bm1hcChpb21tdS0+ZG9tYWluLCBp b3ZhLCAwKTsNCj4gKwkJCXVubG9ja2VkICs9IHB1dF9wZm4ocGZuLCByZHdyKTsNCj4gKwkJfQ0K PiArCX0NCj4gKwlyZXR1cm4gdW5sb2NrZWQ7DQo+ICt9DQo+ICsNCj4gK3N0YXRpYyB2b2lkIHZm aW9fZG1hX3VubWFwKHN0cnVjdCB2ZmlvX2lvbW11ICppb21tdSwgdW5zaWduZWQgbG9uZw0KPiBp b3ZhLA0KPiArCQkJICAgdW5zaWduZWQgbG9uZyBucGFnZSwgaW50IHJkd3IpDQo+ICt7DQo+ICsJ aW50IHVubG9ja2VkOw0KPiArDQo+ICsJdW5sb2NrZWQgPSBfX3ZmaW9fZG1hX3VubWFwKGlvbW11 LCBpb3ZhLCBucGFnZSwgcmR3cik7DQo+ICsJdmZpb19sb2NrX2FjY3QoLXVubG9ja2VkKTsNCj4g K30NCj4gKw0KPiArLyogVW5tYXAgQUxMIERNQSByZWdpb25zICovDQo+ICt2b2lkIHZmaW9faW9t bXVfdW5tYXBhbGwoc3RydWN0IHZmaW9faW9tbXUgKmlvbW11KQ0KPiArew0KPiArCXN0cnVjdCBs aXN0X2hlYWQgKnBvcywgKnBvczI7DQo+ICsJc3RydWN0IGRtYV9tYXBfcGFnZSAqbWxwOw0KPiAr DQo+ICsJbXV0ZXhfbG9jaygmaW9tbXUtPmRnYXRlKTsNCj4gKwlsaXN0X2Zvcl9lYWNoX3NhZmUo cG9zLCBwb3MyLCAmaW9tbXUtPmRtX2xpc3QpIHsNCj4gKwkJbWxwID0gbGlzdF9lbnRyeShwb3Ms IHN0cnVjdCBkbWFfbWFwX3BhZ2UsIGxpc3QpOw0KPiArCQl2ZmlvX2RtYV91bm1hcChpb21tdSwg bWxwLT5kYWRkciwgbWxwLT5ucGFnZSwgbWxwLT5yZHdyKTsNCj4gKwkJbGlzdF9kZWwoJm1scC0+ bGlzdCk7DQo+ICsJCWtmcmVlKG1scCk7DQo+ICsJfQ0KPiArCW11dGV4X3VubG9jaygmaW9tbXUt PmRnYXRlKTsNCj4gK30NCj4gKw0KPiArc3RhdGljIGludCB2YWRkcl9nZXRfcGZuKHVuc2lnbmVk IGxvbmcgdmFkZHIsIGludCByZHdyLCB1bnNpZ25lZCBsb25nDQo+ICpwZm4pDQo+ICt7DQo+ICsJ c3RydWN0IHBhZ2UgKnBhZ2VbMV07DQo+ICsJc3RydWN0IHZtX2FyZWFfc3RydWN0ICp2bWE7DQo+ ICsJaW50IHJldCA9IC1FRkFVTFQ7DQo+ICsNCj4gKwlpZiAoZ2V0X3VzZXJfcGFnZXNfZmFzdCh2 YWRkciwgMSwgcmR3ciwgcGFnZSkgPT0gMSkgew0KPiArCQkqcGZuID0gcGFnZV90b19wZm4ocGFn ZVswXSk7DQo+ICsJCXJldHVybiAwOw0KPiArCX0NCj4gKw0KPiArCWRvd25fcmVhZCgmY3VycmVu dC0+bW0tPm1tYXBfc2VtKTsNCj4gKw0KPiArCXZtYSA9IGZpbmRfdm1hX2ludGVyc2VjdGlvbihj dXJyZW50LT5tbSwgdmFkZHIsIHZhZGRyICsgMSk7DQo+ICsNCj4gKwlpZiAodm1hICYmIHZtYS0+ dm1fZmxhZ3MgJiBWTV9QRk5NQVApIHsNCj4gKwkJKnBmbiA9ICgodmFkZHIgLSB2bWEtPnZtX3N0 YXJ0KSA+PiBQQUdFX1NISUZUKSArIHZtYS0NCj4gPnZtX3Bnb2ZmOw0KPiArCQlpZiAoaXNfaW52 YWxpZF9yZXNlcnZlZF9wZm4oKnBmbikpDQo+ICsJCQlyZXQgPSAwOw0KPiArCX0NCj4gKw0KPiAr CXVwX3JlYWQoJmN1cnJlbnQtPm1tLT5tbWFwX3NlbSk7DQo+ICsNCj4gKwlyZXR1cm4gcmV0Ow0K PiArfQ0KPiArDQo+ICsvKiBNYXAgRE1BIHJlZ2lvbiAqLw0KPiArLyogZGdhdGUgbXVzdCBiZSBo ZWxkICovDQo+ICtzdGF0aWMgaW50IHZmaW9fZG1hX21hcChzdHJ1Y3QgdmZpb19pb21tdSAqaW9t bXUsIHVuc2lnbmVkIGxvbmcgaW92YSwNCj4gKwkJCXVuc2lnbmVkIGxvbmcgdmFkZHIsIGludCBu cGFnZSwgaW50IHJkd3IpDQo+ICt7DQo+ICsJdW5zaWduZWQgbG9uZyBzdGFydCA9IGlvdmE7DQo+ ICsJaW50IGksIHJldCwgbG9ja2VkID0gMCwgcHJvdCA9IElPTU1VX1JFQUQ7DQo+ICsNCj4gKwkv KiBWZXJpZnkgcGFnZXMgYXJlIG5vdCBhbHJlYWR5IG1hcHBlZCAqLw0KPiArCWZvciAoaSA9IDA7 IGkgPCBucGFnZTsgaSsrLCBpb3ZhICs9IFBBR0VfU0laRSkNCj4gKwkJaWYgKGlvbW11X2lvdmFf dG9fcGh5cyhpb21tdS0+ZG9tYWluLCBpb3ZhKSkNCj4gKwkJCXJldHVybiAtRUJVU1k7DQo+ICsN Cj4gKwlpb3ZhID0gc3RhcnQ7DQo+ICsNCj4gKwlpZiAocmR3cikNCj4gKwkJcHJvdCB8PSBJT01N VV9XUklURTsNCj4gKwlpZiAoaW9tbXUtPmNhY2hlKQ0KPiArCQlwcm90IHw9IElPTU1VX0NBQ0hF Ow0KPiArDQo+ICsJZm9yIChpID0gMDsgaSA8IG5wYWdlOyBpKyssIGlvdmEgKz0gUEFHRV9TSVpF LCB2YWRkciArPQ0KPiBQQUdFX1NJWkUpIHsNCj4gKwkJdW5zaWduZWQgbG9uZyBwZm4gPSAwOw0K PiArDQo+ICsJCXJldCA9IHZhZGRyX2dldF9wZm4odmFkZHIsIHJkd3IsICZwZm4pOw0KPiArCQlp ZiAocmV0KSB7DQo+ICsJCQlfX3ZmaW9fZG1hX3VubWFwKGlvbW11LCBzdGFydCwgaSwgcmR3cik7 DQo+ICsJCQlyZXR1cm4gcmV0Ow0KPiArCQl9DQo+ICsNCj4gKwkJLyogT25seSBhZGQgYWN0dWFs IGxvY2tlZCBwYWdlcyB0byBhY2NvdW50aW5nICovDQo+ICsJCWlmICghaXNfaW52YWxpZF9yZXNl cnZlZF9wZm4ocGZuKSkNCj4gKwkJCWxvY2tlZCsrOw0KPiArDQo+ICsJCXJldCA9IGlvbW11X21h cChpb21tdS0+ZG9tYWluLCBpb3ZhLA0KPiArCQkJCShwaHlzX2FkZHJfdClwZm4gPDwgUEFHRV9T SElGVCwgMCwgcHJvdCk7DQo+ICsJCWlmIChyZXQpIHsNCj4gKwkJCS8qIEJhY2sgb3V0IG1hcHBp bmdzIG9uIGVycm9yICovDQo+ICsJCQlwdXRfcGZuKHBmbiwgcmR3cik7DQo+ICsJCQlfX3ZmaW9f ZG1hX3VubWFwKGlvbW11LCBzdGFydCwgaSwgcmR3cik7DQo+ICsJCQlyZXR1cm4gcmV0Ow0KPiAr CQl9DQo+ICsJfQ0KPiArCXZmaW9fbG9ja19hY2N0KGxvY2tlZCk7DQo+ICsJcmV0dXJuIDA7DQo+ ICt9DQo+ICsNCj4gK3N0YXRpYyBpbmxpbmUgaW50IHJhbmdlc19vdmVybGFwKHVuc2lnbmVkIGxv bmcgc3RhcnQxLCBzaXplX3Qgc2l6ZTEsDQo+ICsJCQkJIHVuc2lnbmVkIGxvbmcgc3RhcnQyLCBz aXplX3Qgc2l6ZTIpDQo+ICt7DQo+ICsJcmV0dXJuICEoc3RhcnQxICsgc2l6ZTEgPD0gc3RhcnQy IHx8IHN0YXJ0MiArIHNpemUyIDw9IHN0YXJ0MSk7DQo+ICt9DQo+ICsNCj4gK3N0YXRpYyBzdHJ1 Y3QgZG1hX21hcF9wYWdlICp2ZmlvX2ZpbmRfZG1hKHN0cnVjdCB2ZmlvX2lvbW11ICppb21tdSwN Cj4gKwkJCQkJICBkbWFfYWRkcl90IHN0YXJ0LCBzaXplX3Qgc2l6ZSkNCj4gK3sNCj4gKwlzdHJ1 Y3QgbGlzdF9oZWFkICpwb3M7DQo+ICsJc3RydWN0IGRtYV9tYXBfcGFnZSAqbWxwOw0KPiArDQo+ ICsJbGlzdF9mb3JfZWFjaChwb3MsICZpb21tdS0+ZG1fbGlzdCkgew0KPiArCQltbHAgPSBsaXN0 X2VudHJ5KHBvcywgc3RydWN0IGRtYV9tYXBfcGFnZSwgbGlzdCk7DQo+ICsJCWlmIChyYW5nZXNf b3ZlcmxhcChtbHAtPmRhZGRyLCBOUEFHRV9UT19TSVpFKG1scC0+bnBhZ2UpLA0KPiArCQkJCSAg IHN0YXJ0LCBzaXplKSkNCj4gKwkJCXJldHVybiBtbHA7DQo+ICsJfQ0KPiArCXJldHVybiBOVUxM Ow0KPiArfQ0KPiArDQo+ICtpbnQgdmZpb19yZW1vdmVfZG1hX292ZXJsYXAoc3RydWN0IHZmaW9f aW9tbXUgKmlvbW11LCBkbWFfYWRkcl90DQo+IHN0YXJ0LA0KPiArCQkJICAgIHNpemVfdCBzaXpl LCBzdHJ1Y3QgZG1hX21hcF9wYWdlICptbHApDQo+ICt7DQo+ICsJc3RydWN0IGRtYV9tYXBfcGFn ZSAqc3BsaXQ7DQo+ICsJaW50IG5wYWdlX2xvLCBucGFnZV9oaTsNCj4gKw0KPiArCS8qIEV4aXN0 aW5nIGRtYSByZWdpb24gaXMgY29tcGxldGVseSBjb3ZlcmVkLCB1bm1hcCBhbGwgKi8NCg0KVGhp cyB3b3Jrcy4gSG93ZXZlciwgZ2l2ZW4gaG93IHZmaW9fZG1hX21hcF9kbSBpbXBsZW1lbnRzIHRo ZSBtZXJnaW5nDQpsb2dpYywgSSB0aGluayBpdCBpcyBpbXBvc3NpYmxlIHRvIGhhdmUNCg0KICAg IChzdGFydCA8IG1scC0+ZGFkZHIgJiYNCiAgICAgc3RhcnQgKyBzaXplID4gbWxwLT5kYWRkciAr IE5QQUdFX1RPX1NJWkUobWxwLT5ucGFnZSkpDQoNCg0KPiArCWlmIChzdGFydCA8PSBtbHAtPmRh ZGRyICYmDQo+ICsJICAgIHN0YXJ0ICsgc2l6ZSA+PSBtbHAtPmRhZGRyICsgTlBBR0VfVE9fU0la RShtbHAtPm5wYWdlKSkgew0KPiArCQl2ZmlvX2RtYV91bm1hcChpb21tdSwgbWxwLT5kYWRkciwg bWxwLT5ucGFnZSwgbWxwLT5yZHdyKTsNCj4gKwkJbGlzdF9kZWwoJm1scC0+bGlzdCk7DQo+ICsJ CW5wYWdlX2xvID0gbWxwLT5ucGFnZTsNCj4gKwkJa2ZyZWUobWxwKTsNCj4gKwkJcmV0dXJuIG5w YWdlX2xvOw0KPiArCX0NCj4gKw0KPiArCS8qIE92ZXJsYXAgbG93IGFkZHJlc3Mgb2YgZXhpc3Rp bmcgcmFuZ2UgKi8NCg0KU2FtZSBhcyBhYm92ZSAoaWUsICc8JyBpcyBpbXBvc3NpYmxlKQ0KDQo+ ICsJaWYgKHN0YXJ0IDw9IG1scC0+ZGFkZHIpIHsNCj4gKwkJc2l6ZV90IG92ZXJsYXA7DQo+ICsN Cj4gKwkJb3ZlcmxhcCA9IHN0YXJ0ICsgc2l6ZSAtIG1scC0+ZGFkZHI7DQo+ICsJCW5wYWdlX2xv ID0gb3ZlcmxhcCA+PiBQQUdFX1NISUZUOw0KPiArCQlucGFnZV9oaSA9IG1scC0+bnBhZ2UgLSBu cGFnZV9sbzsNCj4gKw0KPiArCQl2ZmlvX2RtYV91bm1hcChpb21tdSwgbWxwLT5kYWRkciwgbnBh Z2VfbG8sIG1scC0+cmR3cik7DQo+ICsJCW1scC0+ZGFkZHIgKz0gb3ZlcmxhcDsNCj4gKwkJbWxw LT52YWRkciArPSBvdmVybGFwOw0KPiArCQltbHAtPm5wYWdlIC09IG5wYWdlX2xvOw0KPiArCQly ZXR1cm4gbnBhZ2VfbG87DQo+ICsJfQ0KDQpTYW1lIGFzIGFib3ZlIChpZSwgJz4nIGlzIGltcG9z c2libGUpLg0KDQo+ICsJLyogT3ZlcmxhcCBoaWdoIGFkZHJlc3Mgb2YgZXhpc3RpbmcgcmFuZ2Ug Ki8NCj4gKwlpZiAoc3RhcnQgKyBzaXplID49IG1scC0+ZGFkZHIgKyBOUEFHRV9UT19TSVpFKG1s cC0+bnBhZ2UpKSB7DQo+ICsJCXNpemVfdCBvdmVybGFwOw0KPiArDQo+ICsJCW92ZXJsYXAgPSBt bHAtPmRhZGRyICsgTlBBR0VfVE9fU0laRShtbHAtPm5wYWdlKSAtIHN0YXJ0Ow0KPiArCQlucGFn ZV9oaSA9IG92ZXJsYXAgPj4gUEFHRV9TSElGVDsNCj4gKwkJbnBhZ2VfbG8gPSBtbHAtPm5wYWdl IC0gbnBhZ2VfaGk7DQo+ICsNCj4gKwkJdmZpb19kbWFfdW5tYXAoaW9tbXUsIHN0YXJ0LCBucGFn ZV9oaSwgbWxwLT5yZHdyKTsNCj4gKwkJbWxwLT5ucGFnZSAtPSBucGFnZV9oaTsNCj4gKwkJcmV0 dXJuIG5wYWdlX2hpOw0KPiArCX0NCj4gKw0KPiArCS8qIFNwbGl0IGV4aXN0aW5nICovDQo+ICsJ bnBhZ2VfbG8gPSAoc3RhcnQgLSBtbHAtPmRhZGRyKSA+PiBQQUdFX1NISUZUOw0KPiArCW5wYWdl X2hpID0gbWxwLT5ucGFnZSAtIChzaXplID4+IFBBR0VfU0hJRlQpIC0gbnBhZ2VfbG87DQo+ICsN Cj4gKwlzcGxpdCA9IGt6YWxsb2Moc2l6ZW9mICpzcGxpdCwgR0ZQX0tFUk5FTCk7DQo+ICsJaWYg KCFzcGxpdCkNCj4gKwkJcmV0dXJuIC1FTk9NRU07DQo+ICsNCj4gKwl2ZmlvX2RtYV91bm1hcChp b21tdSwgc3RhcnQsIHNpemUgPj4gUEFHRV9TSElGVCwgbWxwLT5yZHdyKTsNCj4gKw0KPiArCW1s cC0+bnBhZ2UgPSBucGFnZV9sbzsNCj4gKw0KPiArCXNwbGl0LT5ucGFnZSA9IG5wYWdlX2hpOw0K PiArCXNwbGl0LT5kYWRkciA9IHN0YXJ0ICsgc2l6ZTsNCj4gKwlzcGxpdC0+dmFkZHIgPSBtbHAt PnZhZGRyICsgTlBBR0VfVE9fU0laRShucGFnZV9sbykgKyBzaXplOw0KPiArCXNwbGl0LT5yZHdy ID0gbWxwLT5yZHdyOw0KPiArCWxpc3RfYWRkKCZzcGxpdC0+bGlzdCwgJmlvbW11LT5kbV9saXN0 KTsNCj4gKwlyZXR1cm4gc2l6ZSA+PiBQQUdFX1NISUZUOw0KPiArfQ0KPiArDQo+ICtpbnQgdmZp b19kbWFfdW5tYXBfZG0oc3RydWN0IHZmaW9faW9tbXUgKmlvbW11LCBzdHJ1Y3QgdmZpb19kbWFf bWFwDQo+ICpkbXApDQo+ICt7DQo+ICsJaW50IHJldCA9IDA7DQo+ICsJc2l6ZV90IG5wYWdlID0g ZG1wLT5zaXplID4+IFBBR0VfU0hJRlQ7DQo+ICsJc3RydWN0IGxpc3RfaGVhZCAqcG9zLCAqbjsN Cj4gKw0KPiArCWlmIChkbXAtPmRtYWFkZHIgJiB+UEFHRV9NQVNLKQ0KPiArCQlyZXR1cm4gLUVJ TlZBTDsNCj4gKwlpZiAoZG1wLT5zaXplICYgflBBR0VfTUFTSykNCj4gKwkJcmV0dXJuIC1FSU5W QUw7DQo+ICsNCj4gKwltdXRleF9sb2NrKCZpb21tdS0+ZGdhdGUpOw0KPiArDQo+ICsJbGlzdF9m b3JfZWFjaF9zYWZlKHBvcywgbiwgJmlvbW11LT5kbV9saXN0KSB7DQo+ICsJCXN0cnVjdCBkbWFf bWFwX3BhZ2UgKm1scDsNCj4gKw0KPiArCQltbHAgPSBsaXN0X2VudHJ5KHBvcywgc3RydWN0IGRt YV9tYXBfcGFnZSwgbGlzdCk7DQo+ICsJCWlmIChyYW5nZXNfb3ZlcmxhcChtbHAtPmRhZGRyLCBO UEFHRV9UT19TSVpFKG1scC0+bnBhZ2UpLA0KPiArCQkJCSAgIGRtcC0+ZG1hYWRkciwgZG1wLT5z aXplKSkgew0KPiArCQkJcmV0ID0gdmZpb19yZW1vdmVfZG1hX292ZXJsYXAoaW9tbXUsIGRtcC0+ ZG1hYWRkciwNCj4gKwkJCQkJCSAgICAgIGRtcC0+c2l6ZSwgbWxwKTsNCj4gKwkJCWlmIChyZXQg PiAwKQ0KPiArCQkJCW5wYWdlIC09IE5QQUdFX1RPX1NJWkUocmV0KTsNCj4gKwkJCWlmIChyZXQg PCAwIHx8IG5wYWdlID09IDApDQo+ICsJCQkJYnJlYWs7DQo+ICsJCX0NCj4gKwl9DQo+ICsJbXV0 ZXhfdW5sb2NrKCZpb21tdS0+ZGdhdGUpOw0KPiArCXJldHVybiByZXQgPiAwID8gMCA6IHJldDsN Cj4gK30NCj4gKw0KPiAraW50IHZmaW9fZG1hX21hcF9kbShzdHJ1Y3QgdmZpb19pb21tdSAqaW9t bXUsIHN0cnVjdCB2ZmlvX2RtYV9tYXANCj4gKmRtcCkNCj4gK3sNCj4gKwlpbnQgbnBhZ2U7DQo+ ICsJc3RydWN0IGRtYV9tYXBfcGFnZSAqbWxwLCAqbW1scCA9IE5VTEw7DQo+ICsJZG1hX2FkZHJf dCBkYWRkciA9IGRtcC0+ZG1hYWRkcjsNCj4gKwl1bnNpZ25lZCBsb25nIGxvY2tlZCwgbG9ja19s aW1pdCwgdmFkZHIgPSBkbXAtPnZhZGRyOw0KPiArCXNpemVfdCBzaXplID0gZG1wLT5zaXplOw0K PiArCWludCByZXQgPSAwLCByZHdyID0gZG1wLT5mbGFncyAmIFZGSU9fRE1BX01BUF9GTEFHX1dS SVRFOw0KPiArDQo+ICsJaWYgKHZhZGRyICYgKFBBR0VfU0laRS0xKSkNCj4gKwkJcmV0dXJuIC1F SU5WQUw7DQo+ICsJaWYgKGRhZGRyICYgKFBBR0VfU0laRS0xKSkNCj4gKwkJcmV0dXJuIC1FSU5W QUw7DQo+ICsJaWYgKHNpemUgJiAoUEFHRV9TSVpFLTEpKQ0KPiArCQlyZXR1cm4gLUVJTlZBTDsN Cj4gKw0KPiArCW5wYWdlID0gc2l6ZSA+PiBQQUdFX1NISUZUOw0KPiArCWlmICghbnBhZ2UpDQo+ ICsJCXJldHVybiAtRUlOVkFMOw0KPiArDQo+ICsJaWYgKCFpb21tdSkNCj4gKwkJcmV0dXJuIC1F SU5WQUw7DQo+ICsNCj4gKwltdXRleF9sb2NrKCZpb21tdS0+ZGdhdGUpOw0KPiArDQo+ICsJaWYg KHZmaW9fZmluZF9kbWEoaW9tbXUsIGRhZGRyLCBzaXplKSkgew0KPiArCQlyZXQgPSAtRUJVU1k7 DQo+ICsJCWdvdG8gb3V0X2xvY2s7DQo+ICsJfQ0KPiArDQo+ICsJLyogYWNjb3VudCBmb3IgbG9j a2VkIHBhZ2VzICovDQo+ICsJbG9ja2VkID0gY3VycmVudC0+bW0tPmxvY2tlZF92bSArIG5wYWdl Ow0KPiArCWxvY2tfbGltaXQgPSBybGltaXQoUkxJTUlUX01FTUxPQ0spID4+IFBBR0VfU0hJRlQ7 DQo+ICsJaWYgKGxvY2tlZCA+IGxvY2tfbGltaXQgJiYgIWNhcGFibGUoQ0FQX0lQQ19MT0NLKSkg ew0KPiArCQlwcmludGsoS0VSTl9XQVJOSU5HICIlczogUkxJTUlUX01FTUxPQ0sgKCVsZCkgZXhj ZWVkZWRcbiIsDQo+ICsJCQlfX2Z1bmNfXywgcmxpbWl0KFJMSU1JVF9NRU1MT0NLKSk7DQo+ICsJ CXJldCA9IC1FTk9NRU07DQo+ICsJCWdvdG8gb3V0X2xvY2s7DQo+ICsJfQ0KPiArDQo+ICsJcmV0 ID0gdmZpb19kbWFfbWFwKGlvbW11LCBkYWRkciwgdmFkZHIsIG5wYWdlLCByZHdyKTsNCj4gKwlp ZiAocmV0KQ0KPiArCQlnb3RvIG91dF9sb2NrOw0KPiArDQo+ICsJLyogQ2hlY2sgaWYgd2UgYWJ1 dCBhIHJlZ2lvbiBiZWxvdyAqLw0KDQpJcyAhZGFkZHIgcG9zc2libGU/DQoNCj4gKwlpZiAoZGFk ZHIpIHsNCj4gKwkJbWxwID0gdmZpb19maW5kX2RtYShpb21tdSwgZGFkZHIgLSAxLCAxKTsNCj4g KwkJaWYgKG1scCAmJiBtbHAtPnJkd3IgPT0gcmR3ciAmJg0KPiArCQkgICAgbWxwLT52YWRkciAr IE5QQUdFX1RPX1NJWkUobWxwLT5ucGFnZSkgPT0gdmFkZHIpIHsNCj4gKw0KPiArCQkJbWxwLT5u cGFnZSArPSBucGFnZTsNCj4gKwkJCWRhZGRyID0gbWxwLT5kYWRkcjsNCj4gKwkJCXZhZGRyID0g bWxwLT52YWRkcjsNCj4gKwkJCW5wYWdlID0gbWxwLT5ucGFnZTsNCj4gKwkJCXNpemUgPSBOUEFH RV9UT19TSVpFKG5wYWdlKTsNCj4gKw0KPiArCQkJbW1scCA9IG1scDsNCj4gKwkJfQ0KPiArCX0N Cg0KSXMgIShkYWRkciArIHNpemUpIHBvc3NpYmxlPw0KDQo+ICsJaWYgKGRhZGRyICsgc2l6ZSkg ew0KPiArCQltbHAgPSB2ZmlvX2ZpbmRfZG1hKGlvbW11LCBkYWRkciArIHNpemUsIDEpOw0KPiAr CQlpZiAobWxwICYmIG1scC0+cmR3ciA9PSByZHdyICYmIG1scC0+dmFkZHIgPT0gdmFkZHIgKyBz aXplKQ0KPiB7DQo+ICsNCj4gKwkJCW1scC0+bnBhZ2UgKz0gbnBhZ2U7DQo+ICsJCQltbHAtPmRh ZGRyID0gZGFkZHI7DQo+ICsJCQltbHAtPnZhZGRyID0gdmFkZHI7DQo+ICsNCj4gKwkJCS8qIElm IG1lcmdlZCBhYm92ZSBhbmQgYmVsb3csIHJlbW92ZSBwcmV2aW91c2x5DQo+ICsJCQkgKiBtZXJn ZWQgZW50cnkuICBOZXcgZW50cnkgY292ZXJzIGl0LiAgKi8NCj4gKwkJCWlmIChtbWxwKSB7DQo+ ICsJCQkJbGlzdF9kZWwoJm1tbHAtPmxpc3QpOw0KPiArCQkJCWtmcmVlKG1tbHApOw0KPiArCQkJ fQ0KPiArCQkJbW1scCA9IG1scDsNCj4gKwkJfQ0KPiArCX0NCj4gKw0KPiArCWlmICghbW1scCkg ew0KPiArCQltbHAgPSBremFsbG9jKHNpemVvZiAqbWxwLCBHRlBfS0VSTkVMKTsNCj4gKwkJaWYg KCFtbHApIHsNCj4gKwkJCXJldCA9IC1FTk9NRU07DQo+ICsJCQl2ZmlvX2RtYV91bm1hcChpb21t dSwgZGFkZHIsIG5wYWdlLCByZHdyKTsNCj4gKwkJCWdvdG8gb3V0X2xvY2s7DQo+ICsJCX0NCj4g Kw0KPiArCQltbHAtPm5wYWdlID0gbnBhZ2U7DQo+ICsJCW1scC0+ZGFkZHIgPSBkYWRkcjsNCj4g KwkJbWxwLT52YWRkciA9IHZhZGRyOw0KPiArCQltbHAtPnJkd3IgPSByZHdyOw0KPiArCQlsaXN0 X2FkZCgmbWxwLT5saXN0LCAmaW9tbXUtPmRtX2xpc3QpOw0KPiArCX0NCj4gKw0KPiArb3V0X2xv Y2s6DQo+ICsJbXV0ZXhfdW5sb2NrKCZpb21tdS0+ZGdhdGUpOw0KPiArCXJldHVybiByZXQ7DQo+ ICt9DQo+ICsNCj4gK3N0YXRpYyBpbnQgdmZpb19pb21tdV9yZWxlYXNlKHN0cnVjdCBpbm9kZSAq aW5vZGUsIHN0cnVjdCBmaWxlICpmaWxlcCkNCj4gK3sNCj4gKwlzdHJ1Y3QgdmZpb19pb21tdSAq aW9tbXUgPSBmaWxlcC0+cHJpdmF0ZV9kYXRhOw0KPiArDQo+ICsJdmZpb19yZWxlYXNlX2lvbW11 KGlvbW11KTsNCj4gKwlyZXR1cm4gMDsNCj4gK30NCj4gKw0KPiArc3RhdGljIGxvbmcgdmZpb19p b21tdV91bmxfaW9jdGwoc3RydWN0IGZpbGUgKmZpbGVwLA0KPiArCQkJCSB1bnNpZ25lZCBpbnQg Y21kLCB1bnNpZ25lZCBsb25nIGFyZykNCj4gK3sNCj4gKwlzdHJ1Y3QgdmZpb19pb21tdSAqaW9t bXUgPSBmaWxlcC0+cHJpdmF0ZV9kYXRhOw0KPiArCWludCByZXQgPSAtRU5PU1lTOw0KDQpBbnkg cmVhc29uIGZvciBub3QgdXNpbmcgInN3aXRjaCIgPw0KDQo+ICsgICAgICAgIGlmIChjbWQgPT0g VkZJT19JT01NVV9HRVRfRkxBR1MpIHsNCj4gKyAgICAgICAgICAgICAgICB1NjQgZmxhZ3MgPSBW RklPX0lPTU1VX0ZMQUdTX01BUF9BTlk7DQo+ICsNCj4gKyAgICAgICAgICAgICAgICByZXQgPSBw dXRfdXNlcihmbGFncywgKHU2NCBfX3VzZXIgKilhcmcpOw0KPiArDQo+ICsgICAgICAgIH0gZWxz ZSBpZiAoY21kID09IFZGSU9fSU9NTVVfTUFQX0RNQSkgew0KPiArCQlzdHJ1Y3QgdmZpb19kbWFf bWFwIGRtOw0KPiArDQo+ICsJCWlmIChjb3B5X2Zyb21fdXNlcigmZG0sICh2b2lkIF9fdXNlciAq KWFyZywgc2l6ZW9mIGRtKSkNCj4gKwkJCXJldHVybiAtRUZBVUxUOw0KDQpXaGF0IGRvZXMgdGhl ICJfZG0iIHN1ZmZpeCBzdGFuZCBmb3I/DQoNCj4gKwkJcmV0ID0gdmZpb19kbWFfbWFwX2RtKGlv bW11LCAmZG0pOw0KPiArDQo+ICsJCWlmICghcmV0ICYmIGNvcHlfdG9fdXNlcigodm9pZCBfX3Vz ZXIgKilhcmcsICZkbSwgc2l6ZW9mDQo+IGRtKSkNCj4gKwkJCXJldCA9IC1FRkFVTFQ7DQo+ICsN Cj4gKwl9IGVsc2UgaWYgKGNtZCA9PSBWRklPX0lPTU1VX1VOTUFQX0RNQSkgew0KPiArCQlzdHJ1 Y3QgdmZpb19kbWFfbWFwIGRtOw0KPiArDQo+ICsJCWlmIChjb3B5X2Zyb21fdXNlcigmZG0sICh2 b2lkIF9fdXNlciAqKWFyZywgc2l6ZW9mIGRtKSkNCj4gKwkJCXJldHVybiAtRUZBVUxUOw0KPiAr DQo+ICsJCXJldCA9IHZmaW9fZG1hX3VubWFwX2RtKGlvbW11LCAmZG0pOw0KPiArDQo+ICsJCWlm ICghcmV0ICYmIGNvcHlfdG9fdXNlcigodm9pZCBfX3VzZXIgKilhcmcsICZkbSwgc2l6ZW9mDQo+ IGRtKSkNCj4gKwkJCXJldCA9IC1FRkFVTFQ7DQo+ICsJfQ0KPiArCXJldHVybiByZXQ7DQo+ICt9 DQo+ICsNCj4gKyNpZmRlZiBDT05GSUdfQ09NUEFUDQo+ICtzdGF0aWMgbG9uZyB2ZmlvX2lvbW11 X2NvbXBhdF9pb2N0bChzdHJ1Y3QgZmlsZSAqZmlsZXAsDQo+ICsJCQkJICAgIHVuc2lnbmVkIGlu dCBjbWQsIHVuc2lnbmVkIGxvbmcgYXJnKQ0KPiArew0KPiArCWFyZyA9ICh1bnNpZ25lZCBsb25n KWNvbXBhdF9wdHIoYXJnKTsNCj4gKwlyZXR1cm4gdmZpb19pb21tdV91bmxfaW9jdGwoZmlsZXAs IGNtZCwgYXJnKTsNCj4gK30NCj4gKyNlbmRpZgkvKiBDT05GSUdfQ09NUEFUICovDQo+ICsNCj4g K2NvbnN0IHN0cnVjdCBmaWxlX29wZXJhdGlvbnMgdmZpb19pb21tdV9mb3BzID0gew0KPiArCS5v d25lcgkJPSBUSElTX01PRFVMRSwNCj4gKwkucmVsZWFzZQk9IHZmaW9faW9tbXVfcmVsZWFzZSwN Cj4gKwkudW5sb2NrZWRfaW9jdGwJPSB2ZmlvX2lvbW11X3VubF9pb2N0bCwNCj4gKyNpZmRlZiBD T05GSUdfQ09NUEFUDQo+ICsJLmNvbXBhdF9pb2N0bAk9IHZmaW9faW9tbXVfY29tcGF0X2lvY3Rs LA0KPiArI2VuZGlmDQo+ICt9Ow0KDQovQ2hyaXMNCg0K
Quoted 1 line · Christian Benvenuti (benve)
Here are few minor comments on
vfio_iommu.c...
Sorry, I've been poking sticks at trying to figure out a clean way to solve the force vfio driver attach problem.
Quoted 5 lines
Quoted 5 lines
drivers/vfio/vfio_iommu.c new file +0
-0
<snip>
Quoted 4 lines
drivers/vfio/vfio_iommu.c
Quoted 2 lines
drivers/vfio/vfio_iommu.c new file +2
-0+ +#include "vfio_private.h" Doesn't the '
dma_' prefix belong to the generic DMA code?
Sure, we could these more vfio-centric.
Quoted 53 lines
Quoted 41 lines
drivers/vfio/vfio_iommu.c new file +41
-0+struct dma_map_page { +struct list_head list; +dma_addr_t daddr; +unsigned long vaddr; +int npage; +int rdwr; +}; + +/* +* This code handles mapping and unmapping of user data buffers +* into DMA'ble space using the IOMMU +*/ + +#define NPAGE_TO_SIZE (npage) ((size_t)(npage) << PAGE_SHIFT) + +struct vwork { +struct mm_struct *mm; +int npage; +struct work_struct work; +}; + +/* delayed decrement for locked_vm */ +static void vfio_lock_acct_bg (struct work_struct *work) +{ + struct vwork *vwork = container_of(work, struct vwork, work); +struct mm_struct *mm; + + mm = vwork->mm; +down_write (&mm->mmap_sem); + mm->locked_vm += vwork->npage; +up_write (&mm->mmap_sem); +mmput (mm);/* unref mm */ +kfree (vwork); +} + +static void vfio_lock_acct (int npage) +{ +struct vwork *vwork; +struct mm_struct *mm; + +if (!current->mm) {drivers/vfio/vfio_iommu.c line 80
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+/* process exited */ +return ; + } +if (down_write_trylock (¤t->mm->mmap_sem)) { + current->mm->locked_vm += npage; +up_write (¤t->mm->mmap_sem); +return ; + } +/* +* Couldn't get mmap_sem lock, so must setup to decrement ^^^^^^^^^
Quoted 1 line
Increment?
Yep
<snip>
Quoted 12 lines
drivers/vfio/vfio_iommu.c line 283
Quoted 8 lines
+int vfio_remove_dma_overlap(struct
vfio_iommu*iommu,dma_addr_tstart,drivers/vfio/vfio_iommu.c new file +6
-0+ size_t size, struct dma_map_page *mlp) +{ + struct dma_map_page *split; +int npage_lo, npage_hi; + +/* Existing dma region is completely covered, unmap all */ This works. However, given how
vfio_dma_map_dmimplements the merging logic, I think it is impossible to have
Quoted 2 lines
(start < mlp->daddr &&
start + size > mlp->daddr + NPAGE_TO_SIZE(mlp->npage))It's quite possible. This allows userspace to create a sparse mapping, then blow it all away with a single unmap from 0 to ~0.
Quoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu.c new file +4
-0+if (start <= mlp->daddr && + start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +vfio_dma_unmap (iommu, mlp->daddr, mlp->npage, mlp->rdwr); +list_del (&mlp->list);
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 293
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ npage_lo = mlp->npage; +kfree (mlp); +return npage_lo; + } + +/* Overlap low address of existing range */ Same as above (ie, '<' is impossible)
existing: |<--- A --->| |<--- B --->| unmap: |<--- C --->|
Maybe not good practice from userspace, but we shouldn't count on userspace to be well behaved.
Quoted 7 lines
Quoted 7 lines
drivers/vfio/vfio_iommu.c new file +7
-0+if (start <= mlp->daddr) { +size_t overlap; + + overlap = start + size - mlp->daddr; + npage_lo = overlap >>PAGE_SHIFT ; + npage_hi = mlp->npage - npage_lo; +
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 306
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+vfio_dma_unmap (iommu, mlp->daddr, npage_lo, mlp->rdwr); + mlp->daddr += overlap; + mlp->vaddr += overlap; + mlp->npage -= npage_lo; +return npage_lo; + }Same as above (ie, '>' is impossible).
Same example as above.
Quoted 12 lines
Quoted 12 lines
drivers/vfio/vfio_iommu.c new file +12
-0+/* Overlap high address of existing range */ +if (start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +size_t overlap; + + overlap = mlp->daddr +NPAGE_TO_SIZE (mlp->npage) - start; + npage_hi = overlap >>PAGE_SHIFT ; + npage_lo = mlp->npage - npage_hi; + +vfio_dma_unmap (iommu, start, npage_hi, mlp->rdwr); + mlp->npage -= npage_hi; +return npage_hi; + }
<snip>
Quoted 40 lines
Quoted 40 lines
+int
vfio_dma_map_dm(struct vfio_iommu *iommu, struct vfio_dma_map *dmp)drivers/vfio/vfio_iommu.c new file +38
-0+{ +int npage; +struct dma_map_page *mlp, *mmlp =NULL ; +dma_addr_t daddr = dmp->dmaaddr; +unsigned long locked, lock_limit, vaddr = dmp->vaddr; +size_t size = dmp->size; +int ret =0 , rdwr = dmp->flags &VFIO_DMA_MAP_FLAG_WRITE ; + +if (vaddr & (PAGE_SIZE -1 )) +return -EINVAL ; +if (daddr & (PAGE_SIZE -1 )) +return -EINVAL ; +if (size & (PAGE_SIZE -1 )) +return -EINVAL ; + + npage = size >>PAGE_SHIFT ; +if (!npage) +return -EINVAL ; + +if (!iommu) +return -EINVAL ; + +mutex_lock (&iommu->dgate); + +if (vfio_find_dma (iommu, daddr, size)) { + ret = -EBUSY ; + goto out_lock; + } + +/* account for locked pages */ + locked = current->mm->locked_vm + npage; + lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; +if (locked > lock_limit && !capable (CAP_IPC_LOCK )) { +printk (KERN_WARNING "%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , + __func__,rlimit (RLIMIT_MEMLOCK )); + ret = -ENOMEM ; + goto out_lock; + }
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 416
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ + ret =vfio_dma_map (iommu, daddr, vaddr, npage, rdwr); +if (ret) + goto out_lock; + +/* Check if we abut a region below */ Is !daddr possible?
Sure, an IOVA of 0x0. There's no region below if we start at zero.
Quoted 8 lines
Quoted 8 lines
drivers/vfio/vfio_iommu.c new file +8
-0+if (daddr) { + mlp =vfio_find_dma (iommu, daddr -1 ,1 ); +if (mlp && mlp->rdwr == rdwr && + mlp->vaddr +NPAGE_TO_SIZE (mlp->npage) == vaddr) { + + mlp->npage += npage; + daddr = mlp->daddr; + vaddr = mlp->vaddr;
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 430
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ npage = mlp->npage; + size =NPAGE_TO_SIZE (npage); + + mmlp = mlp; + } + }Is !(daddr + size) possible?
Same, there's no region above if this region goes to the top of the address space, ie. 0xffffffff_fffff000 + 0x1000
Hmm, wonder if I'm missing a check for wrapping.
Quoted 46 lines
Quoted 46 lines
drivers/vfio/vfio_iommu.c new file +3
-0+if (daddr + size) { + mlp =vfio_find_dma (iommu, daddr + size,1 ); +if (mlp && mlp->rdwr == rdwr && mlp->vaddr == vaddr + size){
drivers/vfio/vfio_iommu.c new file +42
-0+ + mlp->npage += npage; + mlp->daddr = daddr; + mlp->vaddr = vaddr; + +/* If merged above and below, remove previously +* merged entry. New entry covers it. */ +if (mmlp) { +list_del (&mmlp->list); +kfree (mmlp); + } + mmlp = mlp; + } + } + +if (!mmlp) { + mlp =kzalloc (sizeof *mlp,GFP_KERNEL ); +if (!mlp) { + ret = -ENOMEM ; +vfio_dma_unmap (iommu, daddr, npage, rdwr); + goto out_lock; + } + + mlp->npage = npage; + mlp->daddr = daddr; + mlp->vaddr = vaddr; + mlp->rdwr = rdwr; +list_add (&mlp->list, &iommu->dm_list); + } + +out_lock: +mutex_unlock (&iommu->dgate); +return ret; +} + +static int vfio_iommu_release (struct inode *inode,struct file *filep) +{ +struct vfio_iommu *iommu = filep->private_data; + +vfio_release_iommu (iommu); +return 0 ; +}
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 482
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ +static long vfio_iommu_unl_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ +struct vfio_iommu *iommu = filep->private_data; +int ret = -ENOSYS ;Any reason for not using "switch" ?
It got ugly in vfio_main, so I decided to be consistent w/ it in the driver and use if/else here too. I don't like the aesthetics of extra {}s to declare variables within a switch, nor do I like declaring all the variables for each case for the whole function. Personal quirk.
Quoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu.c new file +4
-0+if (cmd ==VFIO_IOMMU_GET_FLAGS ) { +u64 flags =VFIO_IOMMU_FLAGS_MAP_ANY ; + + ret =put_user (flags, (u64 __user *)arg);
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 493
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ + }else if (cmd ==VFIO_IOMMU_MAP_DMA ) { +struct vfio_dma_map dm; + +if (copy_from_user (&dm, (void __user *)arg,sizeof dm)) +return -EFAULT ;What does the "
_dm" suffix stand for?
Inherited from Tom, but I figure _dma_map_dm = action(dma map), object(dm), which is a vfio_Dma_Map.
Thanks,
Alex
Quoted 11 lines
-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Friday, November 11, 2011 10:04 AM
To: Christian Benvenuti (benve)
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Aaron Fabbri (aafabbri); [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [RFC PATCH] vfio: VFIO Driver core framework
Quoted 5 lines
Quoted 1 line · Christian Benvenuti (benve)
Here are few minor comments on
vfio_iommu.c...Sorry, I've been poking sticks at trying to figure out a clean way to solve the force vfio driver attach problem.
Attach o detach?
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
drivers/vfio/vfio_iommu.c new file +0
-0<snip>
Quoted 6 lines
Quoted 4 lines
drivers/vfio/vfio_iommu.c
Quoted 2 lines
drivers/vfio/vfio_iommu.c new file +2
-0+ +#include "vfio_private.h" Doesn't the '
dma_' prefix belong to the generic DMA code?Sure, we could these more vfio-centric.
Like vfio_dma_map_page?
Quoted 71 lines
Quoted 54 lines
Quoted 42 lines
drivers/vfio/vfio_iommu.c new file +41
-0+struct dma_map_page { +struct list_head list; +dma_addr_t daddr; +unsigned long vaddr; +int npage; +int rdwr; +}; + +/* +* This code handles mapping and unmapping of user data buffers +* into DMA'ble space using the IOMMU +*/ + +#define NPAGE_TO_SIZE (npage) ((size_t)(npage) << PAGE_SHIFT) + +struct vwork { +struct mm_struct *mm; +int npage; +struct work_struct work; +}; + +/* delayed decrement for locked_vm */ +static void vfio_lock_acct_bg (struct work_struct *work) +{ + struct vwork *vwork = container_of(work, struct vwork, work); +struct mm_struct *mm; + + mm = vwork->mm; +down_write (&mm->mmap_sem); + mm->locked_vm += vwork->npage; +up_write (&mm->mmap_sem); +mmput (mm);/* unref mm */ +kfree (vwork); +} + +static void vfio_lock_acct (int npage) +{ +struct vwork *vwork; +struct mm_struct *mm; + +if (!current->mm) {drivers/vfio/vfio_iommu.c line 80
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+/* process exited */ +return ; + } +if (down_write_trylock (¤t->mm->mmap_sem)) { + current->mm->locked_vm += npage; +up_write (¤t->mm->mmap_sem); +return ; + } +/* +* Couldn't get mmap_sem lock, so must setup to decrement ^^^^^^^^^Quoted 1 line
Increment?
Yep
<snip>
Quoted 12 lines
drivers/vfio/vfio_iommu.c line 283
Quoted 8 lines
+int vfio_remove_dma_overlap(struct
vfio_iommu*iommu,dma_addr_tstart,drivers/vfio/vfio_iommu.c new file +6
-0+ size_t size, struct dma_map_page *mlp) +{ + struct dma_map_page *split; +int npage_lo, npage_hi; + +/* Existing dma region is completely covered, unmap all */ This works. However, given how
vfio_dma_map_dmimplements the merging logic, I think it is impossible to have
Quoted 5 lines
Quoted 2 lines
(start < mlp->daddr && start + size > mlp->daddr + NPAGE_TO_SIZE(mlp->npage))It's quite possible. This allows userspace to create a sparse mapping, then blow it all away with a single unmap from 0 to ~0.
I would prefer the user to use exact ranges in the unmap operations because it would make it easier to detect bugs/leaks in the map/unmap logic used by the callers.
My assumptions are that:
- the user always keeps track of the mappings
- the user either unmaps one specific mapping or 'all of them'. The 'all of them' case would also take care of those cases where the user does _not_ keep track of mappings and simply uses the "unmap from 0 to ~0" each time.
Because of this you could still provide an exact map/unmap logic and allow such "unmap from 0 to ~0" by making the latter a special case.
However, if we want to allow any arbitrary/inexact unmap request, then OK.
Quoted 197 lines
Quoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu.c new file +4
-0+if (start <= mlp->daddr && + start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +vfio_dma_unmap (iommu, mlp->daddr, mlp->npage, mlp->rdwr); +list_del (&mlp->list);Quoted 8 lines
drivers/vfio/vfio_iommu.c line 293
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ npage_lo = mlp->npage; +kfree (mlp); +return npage_lo; + } + +/* Overlap low address of existing range */ Same as above (ie, '<' is impossible)
existing: |<--- A --->| |<--- B --->| unmap: |<--- C --->|Maybe not good practice from userspace, but we shouldn't count on userspace to be well behaved.
Quoted 7 lines
Quoted 7 lines
drivers/vfio/vfio_iommu.c new file +7
-0+if (start <= mlp->daddr) { +size_t overlap; + + overlap = start + size - mlp->daddr; + npage_lo = overlap >>PAGE_SHIFT ; + npage_hi = mlp->npage - npage_lo; +Quoted 8 lines
drivers/vfio/vfio_iommu.c line 306
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+vfio_dma_unmap (iommu, mlp->daddr, npage_lo, mlp->rdwr); + mlp->daddr += overlap; + mlp->vaddr += overlap; + mlp->npage -= npage_lo; +return npage_lo; + }Same as above (ie, '>' is impossible).
Same example as above.
Quoted 12 lines
Quoted 12 lines
drivers/vfio/vfio_iommu.c new file +12
-0+/* Overlap high address of existing range */ +if (start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +size_t overlap; + + overlap = mlp->daddr +NPAGE_TO_SIZE (mlp->npage) - start; + npage_hi = overlap >>PAGE_SHIFT ; + npage_lo = mlp->npage - npage_hi; + +vfio_dma_unmap (iommu, start, npage_hi, mlp->rdwr); + mlp->npage -= npage_hi; +return npage_hi; + }<snip>
Quoted 40 lines
Quoted 40 lines
+int
vfio_dma_map_dm(struct vfio_iommu *iommu, struct vfio_dma_map *dmp)drivers/vfio/vfio_iommu.c new file +38
-0+{ +int npage; +struct dma_map_page *mlp, *mmlp =NULL ; +dma_addr_t daddr = dmp->dmaaddr; +unsigned long locked, lock_limit, vaddr = dmp->vaddr; +size_t size = dmp->size; +int ret =0 , rdwr = dmp->flags &VFIO_DMA_MAP_FLAG_WRITE ; + +if (vaddr & (PAGE_SIZE -1 )) +return -EINVAL ; +if (daddr & (PAGE_SIZE -1 )) +return -EINVAL ; +if (size & (PAGE_SIZE -1 )) +return -EINVAL ; + + npage = size >>PAGE_SHIFT ; +if (!npage) +return -EINVAL ; + +if (!iommu) +return -EINVAL ; + +mutex_lock (&iommu->dgate); + +if (vfio_find_dma (iommu, daddr, size)) { + ret = -EBUSY ; + goto out_lock; + } + +/* account for locked pages */ + locked = current->mm->locked_vm + npage; + lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; +if (locked > lock_limit && !capable (CAP_IPC_LOCK )) { +printk (KERN_WARNING "%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , + __func__,rlimit (RLIMIT_MEMLOCK )); + ret = -ENOMEM ; + goto out_lock; + }Quoted 8 lines
drivers/vfio/vfio_iommu.c line 416
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ + ret =vfio_dma_map (iommu, daddr, vaddr, npage, rdwr); +if (ret) + goto out_lock; + +/* Check if we abut a region below */ Is !daddr possible?
Sure, an IOVA of
0x0. There's no region below if we start at zero.Quoted 8 lines
Quoted 8 lines
drivers/vfio/vfio_iommu.c new file +8
-0+if (daddr) { + mlp =vfio_find_dma (iommu, daddr -1 ,1 ); +if (mlp && mlp->rdwr == rdwr && + mlp->vaddr +NPAGE_TO_SIZE (mlp->npage) == vaddr) { + + mlp->npage += npage; + daddr = mlp->daddr; + vaddr = mlp->vaddr;Quoted 8 lines
drivers/vfio/vfio_iommu.c line 430
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ npage = mlp->npage; + size =NPAGE_TO_SIZE (npage); + + mmlp = mlp; + } + }Is !(daddr + size) possible?
Same, there's no region above if this region goes to the top of the address space, ie. 0xffffffff_fffff000 +
0x1000Hmm, wonder if I'm missing a check for wrapping.
Quoted 32 lines
Quoted 32 lines
drivers/vfio/vfio_iommu.c new file +3
-0+if (daddr + size) { + mlp =vfio_find_dma (iommu, daddr + size,1 ); +if (mlp && mlp->rdwr == rdwr && mlp->vaddr == vaddr + size){
drivers/vfio/vfio_iommu.c new file +28
-0+ + mlp->npage += npage; + mlp->daddr = daddr; + mlp->vaddr = vaddr; + +/* If merged above and below, remove previously +* merged entry. New entry covers it. */ +if (mmlp) { +list_del (&mmlp->list); +kfree (mmlp); + } + mmlp = mlp; + } + } + +if (!mmlp) { + mlp =kzalloc (sizeof *mlp,GFP_KERNEL ); +if (!mlp) { + ret = -ENOMEM ; +vfio_dma_unmap (iommu, daddr, npage, rdwr); + goto out_lock; + } + + mlp->npage = npage; + mlp->daddr = daddr; + mlp->vaddr = vaddr; + mlp->rdwr = rdwr; +list_add (&mlp->list, &iommu->dm_list);Quoted 8 lines
drivers/vfio/vfio_iommu.c
Quoted 8 lines
drivers/vfio/vfio_iommu.c new file +8
-0+ } + +out_lock: +mutex_unlock (&iommu->dgate); +return ret; +} + +static int vfio_iommu_release(struct inode *inode, struct file*filep)
Quoted 6 lines
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+{ +struct vfio_iommu *iommu = filep->private_data; + +vfio_release_iommu (iommu); +return 0 ; +}Quoted 8 lines
drivers/vfio/vfio_iommu.c line 482
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ +static long vfio_iommu_unl_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ +struct vfio_iommu *iommu = filep->private_data; +int ret = -ENOSYS ;Any reason for not using "switch" ?
It got ugly in
vfio_main, so I decided to be consistent w/ it in the driver and use if/else here too. I don't like the aesthetics of extra {}s to declare variables within a switch, nor do I like declaring all the variables for each case for the whole function. Personal quirk.Quoted 4 lines
Quoted 4 lines
drivers/vfio/vfio_iommu.c new file +4
-0+if (cmd ==VFIO_IOMMU_GET_FLAGS ) { +u64 flags =VFIO_IOMMU_FLAGS_MAP_ANY ; + + ret =put_user (flags, (u64 __user *)arg);Quoted 8 lines
drivers/vfio/vfio_iommu.c line 493
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ + }else if (cmd ==VFIO_IOMMU_MAP_DMA ) { +struct vfio_dma_map dm; + +if (copy_from_user (&dm, (void __user *)arg,sizeof dm)) +return -EFAULT ;What does the "
_dm" suffix stand for?
Quoted 2 lines
Inherited from Tom, but I figure
_dma_map_dm= action(dma map), object(dm), which is avfio_Dma_Map.
OK. The reason why I asked is that '_dm' does not add anything to 'vfio_dma_map'.
/Chris
Quoted 11 lines · Christian Benvenuti (benve)
Quoted 11 lines
-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Friday, November 11, 2011 10:04 AM
To: Christian Benvenuti (benve)
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Aaron Fabbri (aafabbri); [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [RFC PATCH] vfio: VFIO Driver core framework
Quoted 7 lines
Quoted 5 lines · Christian Benvenuti (benve)
Quoted 1 line
Here are few minor comments on
vfio_iommu.c...Sorry, I've been poking sticks at trying to figure out a clean way to solve the force vfio driver attach problem.
Attach o detach?
Attach. For the case when a new device appears that belongs to a group that already in use. I'll probably add a claim() operation to the vfio_device_ops that tells the driver to grab it. I was hoping for pci this would just add it to the dynamic ids, but that hits device lock problems.
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
drivers/vfio/vfio_iommu.c new file +0
-0<snip>
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
drivers/vfio/vfio_iommu.c
Quoted 2 lines
drivers/vfio/vfio_iommu.c new file +2
-0+ +#include "vfio_private.h" Doesn't the '
dma_' prefix belong to the generic DMA code?Sure, we could these more vfio-centric.
Like
vfio_dma_map_page?
Something like that, though _page doesn't seem appropriate as it tracks a region.
Quoted 54 lines
Quoted 54 lines
Quoted 54 lines
Quoted 42 lines
drivers/vfio/vfio_iommu.c new file +41
-0+struct dma_map_page { +struct list_head list; +dma_addr_t daddr; +unsigned long vaddr; +int npage; +int rdwr; +}; + +/* +* This code handles mapping and unmapping of user data buffers +* into DMA'ble space using the IOMMU +*/ + +#define NPAGE_TO_SIZE (npage) ((size_t)(npage) << PAGE_SHIFT) + +struct vwork { +struct mm_struct *mm; +int npage; +struct work_struct work; +}; + +/* delayed decrement for locked_vm */ +static void vfio_lock_acct_bg (struct work_struct *work) +{ + struct vwork *vwork = container_of(work, struct vwork, work); +struct mm_struct *mm; + + mm = vwork->mm; +down_write (&mm->mmap_sem); + mm->locked_vm += vwork->npage; +up_write (&mm->mmap_sem); +mmput (mm);/* unref mm */ +kfree (vwork); +} + +static void vfio_lock_acct (int npage) +{ +struct vwork *vwork; +struct mm_struct *mm; + +if (!current->mm) {drivers/vfio/vfio_iommu.c line 80
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+/* process exited */ +return ; + } +if (down_write_trylock (¤t->mm->mmap_sem)) { + current->mm->locked_vm += npage; +up_write (¤t->mm->mmap_sem); +return ; + } +/* +* Couldn't get mmap_sem lock, so must setup to decrement ^^^^^^^^^
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Increment?
Yep
Actually, side note, this is increment/decrement depending on the sign of the parameter. So "update" may be more appropriate. I think Tom originally used increment in one place and decrement in another to show it's dual use.
Quoted 19 lines
Quoted 13 lines
<snip>
Quoted 12 lines
drivers/vfio/vfio_iommu.c line 283
Quoted 8 lines
+int vfio_remove_dma_overlap(struct
vfio_iommu*iommu,dma_addr_tstart,drivers/vfio/vfio_iommu.c new file +6
-0+ size_t size, struct dma_map_page *mlp) +{ + struct dma_map_page *split; +int npage_lo, npage_hi; + +/* Existing dma region is completely covered, unmap all */ This works. However, given how
vfio_dma_map_dmimplements the merging logic, I think it is impossible to haveQuoted 5 lines
Quoted 2 lines
(start < mlp->daddr && start + size > mlp->daddr + NPAGE_TO_SIZE(mlp->npage))It's quite possible. This allows userspace to create a sparse mapping, then blow it all away with a single unmap from 0 to ~0.
Quoted 6 lines
I would prefer the user to use exact ranges in the unmap operations because it would make it easier to detect bugs/leaks in the map/unmap logic used by the callers.
My assumptions are that:- the user always keeps track of the mappings
My qemu code plays a little on the loose side here, acting as a passthrough for the internal memory client. But even there, worst case would probably be trying to unmap a non-existent entry, not unmapping a sparse range.
Quoted 9 lines
- the user either unmaps one specific mapping or 'all of them'. The 'all of them' case would also take care of those cases where the user does _not_ keep track of mappings and simply uses the "unmap from 0 to ~0" each time.
Because of this you could still provide an exact map/unmap logic and allow such "unmap from 0 to ~0" by making the latter a special case.
However, if we want to allow any arbitrary/inexact unmap request, then OK.
I can't think of any good reasons we shouldn't be more strict. I think it was primarily just convenient to hit all the corner cases since we merge all the requests together for tracking and need to be able to split them back apart. It does feel a little awkward to have a 0/~0 special case though, but I don't think it's worth adding another ioctl to handle it.
<snip>
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
drivers/vfio/vfio_iommu.c line 493
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+if (cmd ==VFIO_IOMMU_GET_FLAGS ) { +u64 flags =VFIO_IOMMU_FLAGS_MAP_ANY ; + + ret =put_user (flags, (u64 __user *)arg); + + }else if (cmd ==VFIO_IOMMU_MAP_DMA ) { +struct vfio_dma_map dm; + +if (copy_from_user (&dm, (void __user *)arg,sizeof dm)) +return -EFAULT ;What does the "
_dm" suffix stand for?
Quoted 4 lines
Quoted 2 lines
Inherited from Tom, but I figure
_dma_map_dm= action(dma map), object(dm), which is avfio_Dma_Map.OK. The reason why I asked is that '
_dm' does not add anything to 'vfio_dma_map'.
Yep. Thanks,
Alex
Quoted 1 line · Alex Williamson
On Fri, 2011-11-11 at 16:22 -0600, Christian Benvenuti (benve) wrote:
[snip]
Quoted 10 lines
Quoted 9 lines
- the user either unmaps one specific mapping or 'all of them'. The 'all of them' case would also take care of those cases where the user does _not_ keep track of mappings and simply uses the "unmap from 0 to ~0" each time.
Because of this you could still provide an exact map/unmap logic and allow such "unmap from 0 to ~0" by making the latter a special case.
However, if we want to allow any arbitrary/inexact unmap request, then OK.
Quoted 6 lines
I can't think of any good reasons we shouldn't be more strict. I think it was primarily just convenient to hit all the corner cases since we merge all the requests together for tracking and need to be able to split them back apart. It does feel a little awkward to have a 0/~0 special case though, but I don't think it's worth adding another ioctl to handle it.
Being strict, or at least enforcing strictness, requires that the infrastructure track all the maps, so that the unmaps can be matching. This is not a natural thing with the data structures you want for all IOMMUs. For example on POWER, the IOMMU (aka TCE table) is a simple 1-level pagetable. One pointer with a couple of permission bits per IOMMU page. Handling oddly overlapping operations on that data structure is natural, enforcing strict matching of maps and unmaps is not and would require extra information to be stored by vfio. On POWER, the IOMMU operations often *are* a hot path, so manipulating those structures would have a real cost, too.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 10 lines · David Gibson
Being strict, or at least enforcing strictness, requires that the infrastructure track all the maps, so that the unmaps can be matching. This is not a natural thing with the data structures you want for all IOMMUs. For example on POWER, the IOMMU (aka TCE table) is a simple 1-level pagetable. One pointer with a couple of permission bits per IOMMU page. Handling oddly overlapping operations on that data structure is natural, enforcing strict matching of maps and unmaps is not and would require extra information to be stored by vfio. On POWER, the IOMMU operations often *are* a hot path, so manipulating those structures would have a real cost, too.
In fact they are a very hot path even. There's no way we can afford the cost of tracking per page mapping/unmapping (other than bumping the page count on a page that's currently mapped or via some debug-only feature).
Cheers,
Ben.
Here are few minor comments on vfio_iommu.c ...
Quoted 25 lines
drivers/vfio/vfio_iommu.c new file +7
-0@@ -0,0 +1,530 @@ +/* +* VFIO: IOMMU DMA mapping support +* +* Copyright (C) 2011 Red Hat, Inc. All rights reserved. +* Author: Alex Williamson <[email protected]> +* +* This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Derived from original vfio:
+ * Copyright 2010 Cisco Systems, Inc. All rights reserved.
+ * Author: Tom Lyon, [email protected]
+ */drivers/vfio/vfio_iommu.c new file +4
-0+ +#include <linux/compat.h> +#include <linux/device.h> +#include <linux/fs.h>
drivers/vfio/vfio_iommu.c line 28
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+#include <linux/iommu.h> +#include <linux/module.h> +#include <linux/mm.h> +#include <linux/sched.h> +#include <linux/slab.h> +#include <linux/uaccess.h> +#include <linux/vfio.h> +#include <linux/workqueue.h> + +#include "vfio_private.h"
Doesn't the 'dma_' prefix belong to the generic DMA code?
Quoted 41 lines
drivers/vfio/vfio_iommu.c new file +41
-0+struct dma_map_page { +struct list_head list; +dma_addr_t daddr; +unsigned long vaddr; +int npage; +int rdwr; +}; + +/* +* This code handles mapping and unmapping of user data buffers +* into DMA'ble space using the IOMMU +*/ + +#define NPAGE_TO_SIZE (npage) ((size_t)(npage) << PAGE_SHIFT) + +struct vwork { +struct mm_struct *mm; +int npage; +struct work_struct work; +}; + +/* delayed decrement for locked_vm */ +static void vfio_lock_acct_bg (struct work_struct *work) +{ + struct vwork *vwork = container_of(work, struct vwork, work); +struct mm_struct *mm; + + mm = vwork->mm; +down_write (&mm->mmap_sem); + mm->locked_vm += vwork->npage; +up_write (&mm->mmap_sem); +mmput (mm);/* unref mm */ +kfree (vwork); +} + +static void vfio_lock_acct (int npage) +{ +struct vwork *vwork; +struct mm_struct *mm; + +if (!current->mm) {
drivers/vfio/vfio_iommu.c line 80
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+/* process exited */ +return ; + } +if (down_write_trylock (¤t->mm->mmap_sem)) { + current->mm->locked_vm += npage; +up_write (¤t->mm->mmap_sem); +return ; + } +/* +* Couldn't get mmap_sem lock, so must setup to decrement
^^^^^^^^^
Increment?
Quoted 204 lines
drivers/vfio/vfio_iommu.c new file +62
-0+* mm->locked_vm later. If locked_vm were atomic, we wouldn't +* need this silliness +*/ + vwork =kmalloc (sizeof (struct vwork ),GFP_KERNEL ); +if (!vwork) +return ; + mm =get_task_mm (current);/* take ref mm */ +if (!mm) { +kfree (vwork); +return ; + } +INIT_WORK (&vwork->work, vfio_lock_acct_bg); + vwork->mm = mm; + vwork->npage = npage; +schedule_work (&vwork->work); +} + +/* Some mappings aren't backed by a struct page, for example an mmap'd +* MMIO range for our own or another device. These use a different +* pfn conversion and shouldn't be tracked as locked pages. */ +static int is_invalid_reserved_pfn (unsigned long pfn) +{ +if (pfn_valid (pfn)) { +int reserved; +struct page *tail =pfn_to_page (pfn); +struct page *head =compound_trans_head (tail); + reserved =PageReserved (head); +if (head != tail) { +/* "head" is not a dangling pointer +* (compound_trans_head takes care of that) +* but the hugepage may have been split +* from under us (and we may not hold a +* reference count on the head page so it can +* be reused before we run PageReferenced), so +* we've to check PageTail before returning +* what we just read. +*/ +smp_rmb (); +if (PageTail (tail)) +return reserved; + } +return PageReserved (tail); + } + +return true; +} + +static int put_pfn (unsigned long pfn,int rdwr) +{ +if (!is_invalid_reserved_pfn (pfn)) { +struct page *page =pfn_to_page (pfn); +if (rdwr) +SetPageDirty (page); +put_page (page); +return 1 ; + } +return 0 ; +} + +/* Unmap DMA region */ +/* dgate must be held */ +static int __vfio_dma_unmap(struct vfio_iommu *iommu, unsigned longiova,
drivers/vfio/vfio_iommu.c new file +8
-0+ int npage, int rdwr) +{ + int i, unlocked = 0; + +for (i =0 ; i < npage; i++, iova +=PAGE_SIZE ) { +unsigned long pfn; + + pfn = iommu_iova_to_phys(iommu->domain, iova) >>
PAGE_SHIFT;drivers/vfio/vfio_iommu.c new file +9
-0+if (pfn) { +iommu_unmap (iommu->domain, iova,0 ); + unlocked +=put_pfn (pfn, rdwr); + } + } +return unlocked; +} + +static void vfio_dma_unmap(struct vfio_iommu *iommu, unsigned longiova,
drivers/vfio/vfio_iommu.c new file +25
-0+ unsigned long npage, int rdwr) +{ + int unlocked; + + unlocked =__vfio_dma_unmap (iommu, iova, npage, rdwr); +vfio_lock_acct (-unlocked); +} + +/* Unmap ALL DMA regions */ +void vfio_iommu_unmapall (struct vfio_iommu *iommu) +{ +struct list_head *pos, *pos2; +struct dma_map_page *mlp; + +mutex_lock (&iommu->dgate); +list_for_each_safe (pos, pos2, &iommu->dm_list) { + mlp = list_entry(pos, struct dma_map_page, list); +vfio_dma_unmap (iommu, mlp->daddr, mlp->npage, mlp->rdwr); +list_del (&mlp->list); +kfree (mlp); + } +mutex_unlock (&iommu->dgate); +} + +static int vaddr_get_pfn(unsigned long vaddr, int rdwr, unsigned long*pfn)
drivers/vfio/vfio_iommu.c new file +16
-0+{ +struct page *page[1 ]; +struct vm_area_struct *vma; +int ret = -EFAULT ; + +if (get_user_pages_fast (vaddr,1 , rdwr, page) ==1 ) { + *pfn =page_to_pfn (page[0 ]); +return 0 ; + } + +down_read (¤t->mm->mmap_sem); + + vma =find_vma_intersection (current->mm, vaddr, vaddr +1 ); + +if (vma && vma->vm_flags &VM_PFNMAP ) { + *pfn = ((vaddr - vma->vm_start) >> PAGE_SHIFT) + vma-Quoted 1 line
vm_pgoff;drivers/vfio/vfio_iommu.c new file +30
-0+ if (is_invalid_reserved_pfn(*pfn)) + ret = 0; + } + + up_read(¤t->mm->mmap_sem); + + return ret; +} + +/* Map DMA region */ +/* dgate must be held */ +static int vfio_dma_map(struct vfio_iommu *iommu, unsigned long iova, + unsigned long vaddr, int npage, int rdwr) +{ + unsigned long start = iova; + int i, ret, locked = 0, prot = IOMMU_READ; + + /* Verify pages are not already mapped */ + for (i = 0; i < npage; i++, iova += PAGE_SIZE) + if (iommu_iova_to_phys(iommu->domain, iova)) + return -EBUSY; + + iova = start; + + if (rdwr) + prot |= IOMMU_WRITE; + if (iommu->cache) + prot |= IOMMU_CACHE; + + for (i = 0; i < npage; i++, iova += PAGE_SIZE, vaddr +=
PAGE_SIZE) {drivers/vfio/vfio_iommu.c new file +47
-0+unsigned long pfn =0 ; + + ret =vaddr_get_pfn (vaddr, rdwr, &pfn); +if (ret) { +__vfio_dma_unmap (iommu, start, i, rdwr); +return ret; + } + +/* Only add actual locked pages to accounting */ +if (!is_invalid_reserved_pfn (pfn)) + locked++; + + ret =iommu_map (iommu->domain, iova, + (phys_addr_t )pfn <<PAGE_SHIFT ,0 , prot); +if (ret) { +/* Back out mappings on error */ +put_pfn (pfn, rdwr); +__vfio_dma_unmap (iommu, start, i, rdwr); +return ret; + } + } +vfio_lock_acct (locked); +return 0 ; +} + +static inline int ranges_overlap (unsigned long start1,size_t size1, +unsigned long start2,size_t size2) +{ +return !(start1 + size1 <= start2 || start2 + size2 <= start1); +} + +static struct dma_map_page *vfio_find_dma (struct vfio_iommu *iommu, +dma_addr_t start,size_t size) +{ +struct list_head *pos; +struct dma_map_page *mlp; + +list_for_each (pos, &iommu->dm_list) { + mlp = list_entry(pos, struct dma_map_page, list); +if (ranges_overlap (mlp->daddr,NPAGE_TO_SIZE (mlp->npage), + start, size)) +return mlp; + } +return NULL ; +} + +int vfio_remove_dma_overlap(struct vfio_iommu *iommu, dma_addr_tstart,
drivers/vfio/vfio_iommu.c line 283
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ size_t size, struct dma_map_page *mlp) +{ + struct dma_map_page *split; +int npage_lo, npage_hi; + +/* Existing dma region is completely covered, unmap all */
This works. However, given how vfio_dma_map_dm implements the merging logic, I think it is impossible to have
(start < mlp->daddr &&
start + size > mlp->daddr + NPAGE_TO_SIZE(mlp->npage))drivers/vfio/vfio_iommu.c line 293
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+if (start <= mlp->daddr && + start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +vfio_dma_unmap (iommu, mlp->daddr, mlp->npage, mlp->rdwr); +list_del (&mlp->list); + npage_lo = mlp->npage; +kfree (mlp); +return npage_lo; + } + +/* Overlap low address of existing range */
Same as above (ie, '<' is impossible)
drivers/vfio/vfio_iommu.c line 306
Quoted 13 lines
drivers/vfio/vfio_iommu.c new file +13
-0+if (start <= mlp->daddr) { +size_t overlap; + + overlap = start + size - mlp->daddr; + npage_lo = overlap >>PAGE_SHIFT ; + npage_hi = mlp->npage - npage_lo; + +vfio_dma_unmap (iommu, mlp->daddr, npage_lo, mlp->rdwr); + mlp->daddr += overlap; + mlp->vaddr += overlap; + mlp->npage -= npage_lo; +return npage_lo; + }
Same as above (ie, '>' is impossible).
Quoted 101 lines
drivers/vfio/vfio_iommu.c new file +34
-0+/* Overlap high address of existing range */ +if (start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +size_t overlap; + + overlap = mlp->daddr +NPAGE_TO_SIZE (mlp->npage) - start; + npage_hi = overlap >>PAGE_SHIFT ; + npage_lo = mlp->npage - npage_hi; + +vfio_dma_unmap (iommu, start, npage_hi, mlp->rdwr); + mlp->npage -= npage_hi; +return npage_hi; + } + +/* Split existing */ + npage_lo = (start - mlp->daddr) >>PAGE_SHIFT ; + npage_hi = mlp->npage - (size >>PAGE_SHIFT ) - npage_lo; + + split =kzalloc (sizeof *split,GFP_KERNEL ); +if (!split) +return -ENOMEM ; + +vfio_dma_unmap (iommu, start, size >>PAGE_SHIFT , mlp->rdwr); + + mlp->npage = npage_lo; + + split->npage = npage_hi; + split->daddr = start + size; + split->vaddr = mlp->vaddr +NPAGE_TO_SIZE (npage_lo) + size; + split->rdwr = mlp->rdwr; +list_add (&split->list, &iommu->dm_list); +return size >>PAGE_SHIFT ; +} + +int vfio_dma_unmap_dm(struct vfio_iommu *iommu, struct vfio_dma_map*dmp)
drivers/vfio/vfio_iommu.c new file +31
-0+{ +int ret =0 ; +size_t npage = dmp->size >>PAGE_SHIFT ; +struct list_head *pos, *n; + +if (dmp->dmaaddr & ~PAGE_MASK ) +return -EINVAL ; +if (dmp->size & ~PAGE_MASK ) +return -EINVAL ; + +mutex_lock (&iommu->dgate); + +list_for_each_safe (pos, n, &iommu->dm_list) { +struct dma_map_page *mlp; + + mlp = list_entry(pos, struct dma_map_page, list); +if (ranges_overlap (mlp->daddr,NPAGE_TO_SIZE (mlp->npage), + dmp->dmaaddr, dmp->size)) { + ret =vfio_remove_dma_overlap (iommu, dmp->dmaaddr, + dmp->size, mlp); +if (ret >0 ) + npage -=NPAGE_TO_SIZE (ret); +if (ret <0 || npage ==0 ) +break ; + } + } +mutex_unlock (&iommu->dgate); +return ret >0 ?0 : ret; +} + +int vfio_dma_map_dm(struct vfio_iommu *iommu, struct vfio_dma_map*dmp)
drivers/vfio/vfio_iommu.c new file +34
-0+{ +int npage; +struct dma_map_page *mlp, *mmlp =NULL ; +dma_addr_t daddr = dmp->dmaaddr; +unsigned long locked, lock_limit, vaddr = dmp->vaddr; +size_t size = dmp->size; +int ret =0 , rdwr = dmp->flags &VFIO_DMA_MAP_FLAG_WRITE ; + +if (vaddr & (PAGE_SIZE -1 )) +return -EINVAL ; +if (daddr & (PAGE_SIZE -1 )) +return -EINVAL ; +if (size & (PAGE_SIZE -1 )) +return -EINVAL ; + + npage = size >>PAGE_SHIFT ; +if (!npage) +return -EINVAL ; + +if (!iommu) +return -EINVAL ; + +mutex_lock (&iommu->dgate); + +if (vfio_find_dma (iommu, daddr, size)) { + ret = -EBUSY ; + goto out_lock; + } + +/* account for locked pages */ + locked = current->mm->locked_vm + npage; + lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; +if (locked > lock_limit && !capable (CAP_IPC_LOCK )) { + printk(KERN_WARNING "%s: RLIMIT_MEMLOCK (%ld) exceeded\n",
drivers/vfio/vfio_iommu.c line 416
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+ __func__, rlimit(RLIMIT_MEMLOCK)); + ret = -ENOMEM ; + goto out_lock; + } + + ret =vfio_dma_map (iommu, daddr, vaddr, npage, rdwr); +if (ret) + goto out_lock; + +/* Check if we abut a region below */
Is !daddr possible?
Quoted 4 lines
drivers/vfio/vfio_iommu.c new file +4
-0+if (daddr) { + mlp =vfio_find_dma (iommu, daddr -1 ,1 ); +if (mlp && mlp->rdwr == rdwr && + mlp->vaddr +NPAGE_TO_SIZE (mlp->npage) == vaddr) {
drivers/vfio/vfio_iommu.c line 430
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+ + mlp->npage += npage; + daddr = mlp->daddr; + vaddr = mlp->vaddr; + npage = mlp->npage; + size =NPAGE_TO_SIZE (npage); + + mmlp = mlp; + } + }
Is !(daddr + size) possible?
Quoted 42 lines
drivers/vfio/vfio_iommu.c new file +3
-0+if (daddr + size) { + mlp =vfio_find_dma (iommu, daddr + size,1 ); +if (mlp && mlp->rdwr == rdwr && mlp->vaddr == vaddr + size){
drivers/vfio/vfio_iommu.c new file +38
-0+ + mlp->npage += npage; + mlp->daddr = daddr; + mlp->vaddr = vaddr; + +/* If merged above and below, remove previously +* merged entry. New entry covers it. */ +if (mmlp) { +list_del (&mmlp->list); +kfree (mmlp); + } + mmlp = mlp; + } + } + +if (!mmlp) { + mlp =kzalloc (sizeof *mlp,GFP_KERNEL ); +if (!mlp) { + ret = -ENOMEM ; +vfio_dma_unmap (iommu, daddr, npage, rdwr); + goto out_lock; + } + + mlp->npage = npage; + mlp->daddr = daddr; + mlp->vaddr = vaddr; + mlp->rdwr = rdwr; +list_add (&mlp->list, &iommu->dm_list); + } + +out_lock: +mutex_unlock (&iommu->dgate); +return ret; +} + +static int vfio_iommu_release (struct inode *inode,struct file *filep) +{ +struct vfio_iommu *iommu = filep->private_data;
drivers/vfio/vfio_iommu.c line 482
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+ +vfio_release_iommu (iommu); +return 0 ; +} + +static long vfio_iommu_unl_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ +struct vfio_iommu *iommu = filep->private_data; +int ret = -ENOSYS ;
Any reason for not using "switch" ?
drivers/vfio/vfio_iommu.c line 493
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+if (cmd ==VFIO_IOMMU_GET_FLAGS ) { +u64 flags =VFIO_IOMMU_FLAGS_MAP_ANY ; + + ret =put_user (flags, (u64 __user *)arg); + + }else if (cmd ==VFIO_IOMMU_MAP_DMA ) { +struct vfio_dma_map dm; + +if (copy_from_user (&dm, (void __user *)arg,sizeof dm)) +return -EFAULT ;
What does the "_dm" suffix stand for?
Quoted 28 lines
drivers/vfio/vfio_iommu.c new file +3
-0+ ret =vfio_dma_map_dm (iommu, &dm); + + if (!ret && copy_to_user((void __user *)arg, &dm, sizeofdm))
drivers/vfio/vfio_iommu.c new file +11
-0+ ret = -EFAULT ; + + } else if (cmd == VFIO_IOMMU_UNMAP_DMA) { + struct vfio_dma_map dm; + + if (copy_from_user(&dm, (void __user *)arg, sizeof dm)) + return -EFAULT; + + ret = vfio_dma_unmap_dm(iommu, &dm); + + if (!ret && copy_to_user((void __user *)arg, &dm, sizeofdm))
drivers/vfio/vfio_iommu.c new file +12
-0+ ret = -EFAULT ; + } +return ret; +} + +#ifdef CONFIG_COMPAT +static long vfio_iommu_compat_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ + arg = (unsigned long )compat_ptr (arg); +return vfio_iommu_unl_ioctl (filep, cmd, arg); +}
drivers/vfio/vfio_iommu.c line 530
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+#endif /* CONFIG_COMPAT */ + +const struct file_operations vfio_iommu_fops = { + .owner =THIS_MODULE , + .release = vfio_iommu_release, + .unlocked_ioctl = vfio_iommu_unl_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = vfio_iommu_compat_ioctl, +#endif +};
/Chris
Quoted 91 lines · Alex Williamson
VFIO provides a secure, IOMMU based interface for user space drivers, including device assignment to virtual machines. This provides the base management of IOMMU groups, devices, and IOMMU objects. See
Documentation/vfio.txtincluded in this patch for user and kernel API description.Note, this implements the new API discussed at KVM Forum 2011, as represented by the drvier version 0.2. It's hoped that this provides a modular enough interface to support PCI and non-PCI userspace drivers across various architectures and IOMMU implementations.
Signed-off-by Alex Williamson <[email protected]>---
Fingers crossed, this is the last RFC for VFIO, but we need the iommu group support before this can go upstream
(http://lkml.indiana.edu/hypermail/linux/kernel/1110.2/02303.html),
hoping this helps push that along.Since the last posting, this version completely modularizes the device backends and better defines the APIs between the core VFIO code and the device backends. I expect that we might also adopt a modular IOMMU interface as
iommu_opslearns about different types of hardware. Also many, many cleanups. Check the complete git history for details:
git://github.com/awilliam/linux-vfio.gitvfio-ng(matching qemu tree:
git://github.com/awilliam/qemu-vfio.git)This version, along with the supporting VFIO PCI backend can be found here:
git://github.com/awilliam/linux-vfio.gitvfio-next-20111103I've held off on implementing a
kernel->usersignaling mechanism for now since the previous netlink version produced too many gag reflexes. It's easy enough to set a bit in the group flags too indicate such support in the future, so I think we can move ahead without it.Appreciate any feedback or suggestions. Thanks,
Alex
Documentation/ioctl/ioctl-number.txt | 1 Documentation/vfio.txt | 304 +++++++++ MAINTAINERS | 8 drivers/Kconfig | 2 drivers/Makefile | 1 drivers/vfio/Kconfig | 8 drivers/vfio/Makefile | 3 drivers/vfio/vfio_iommu.c | 530 ++++++++++++++++ drivers/vfio/vfio_main.c | 1151 ++++++++++++++++++++++++++++++++++ drivers/vfio/vfio_private.h | 34 + include/linux/vfio.h | 155 +++++ 11 files changed, 2197 insertions(+), 0 deletions(-) create mode 100644 Documentation/vfio.txt create mode 100644 drivers/vfio/Kconfig create mode 100644 drivers/vfio/Makefile create mode 100644 drivers/vfio/vfio_iommu.c create mode 100644 drivers/vfio/vfio_main.c create mode 100644 drivers/vfio/vfio_private.h create mode 100644 include/linux/vfio.hDocumentation/ioctl/ioctl-number.txt +1
-0@@ -88,6 +88,7 @@ Code Seq#(hex) Include File Comments and kernel/power/user.c '8' all SNP8023 advanced NIC card <mailto:[email protected]> +';' 64-76 linux/vfio.h '@' 00-0F linux/radeonfb.h conflict! '@' 00-0F drivers/video/aty/aty128fb.c conflict! 'A' 00-1F linux/apm_bios.h conflict!Documentation/vfio.txt new file +7
-0@@ -0,0 +1,304 @@ +VFIO - "Virtual Function I/O"[1] +------------------------------------------------------------------------------- +Many modern system now provide DMA and interrupt remapping facilities +to help ensure I/O devices behave within the boundaries they've been +allotted. This includes x86 hardware with AMD-Vi and Intel VT-d as +well as POWER systems with Partitionable Endpoints (PEs) and even +embedded powerpc systems (technology name unknown). The VFIO driver
Documentation/vfio.txt line 17
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+is an IOMMU/device agnostic framework for exposing direct device +access to userspace, in a secure, IOMMU protected environment. In +other words, this allows safe, non-privileged, userspace drivers. + +Why do we want that? Virtual machines often make use of direct device +access ("device assignment") when configured for the highest possible +I/O performance. From a device and host perspective, this simply turns +the VM into a userspace driver, with the benefits of significantly +reduced latency, higher bandwidth, and direct use of bare-metal device +drivers[2].
Are there any constraints of running a 32-bit userspace with a 64-bit kernel and with 32-bit user space drivers?
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+ +Some applications, particularly in the high performance computing +field, also benefit from low-overhead, direct device access from +userspace. Examples include network adapters (often non-TCP/IP based) +and compute accelerators. Previous to VFIO, these drivers needed to
Documentation/vfio.txt line 32
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+go through the full development cycle to become proper upstream driver, +be maintained out of tree, or make use of the UIO framework, which +has no notion of IOMMU protection, limited interrupt support, and +requires root privileges to access things like PCI configuration space. + +The VFIO driver framework intends to unify these, replacing both the +KVM PCI specific device assignment currently used as well as provide +a more secure, more featureful userspace driver environment than UIO. + +Groups, Devices, IOMMUs, oh my
<chuckles> oh my, eh?
Quoted 68 lines
Documentation/vfio.txt new file +68
-0+------------------------------------------------------------------------------- + +A fundamental component of VFIO is the notion of IOMMU groups. IOMMUs +can't always distinguish transactions from each individual device in +the system. Sometimes this is because of the IOMMU design, such as with +PEs, other times it's caused by the I/O topology, for instance a +PCIe-to-PCI bridge masking all devices behind it. We call the sets of +devices created by these restictions IOMMU groups (or just "groups" for +this document). + +The IOMMU cannot distiguish transactions between the individual devices +within the group, therefore the group is the basic unit of ownership for +a userspace process. Because of this, groups are also the primary +interface to both devices and IOMMU domains in VFIO. + +The VFIO representation of groups is created as devices are added into +the framework by a VFIO bus driver. The vfio-pci module is an example +of a bus driver. This module registers devices along with a set of bus +specific callbacks with the VFIO core. These callbacks provide the +interfaces later used for device access. As each new group is created, +as determined by iommu_device_group(), VFIO creates a /dev/vfio/$GROUP +character device. + +In addition to the device enumeration and callbacks, the VFIO bus driver +also provides a traditional device driver and is able to bind to devices +on it's bus. When a device is bound to the bus driver it's available to +VFIO. When all the devices within a group are bound to their bus drivers, +the group becomes "viable" and a user with sufficient access to the VFIO +group chardev can obtain exclusive access to the set of group devices. + +As documented in linux/vfio.h, several ioctls are provided on the +group chardev: + +#define VFIO_GROUP_GET_FLAGS _IOR(';', 100, __u64) + #define VFIO_GROUP_FLAGS_VIABLE (1 << 0) + #define VFIO_GROUP_FLAGS_MM_LOCKED (1 << 1) +#define VFIO_GROUP_MERGE _IOW(';', 101, int) +#define VFIO_GROUP_UNMERGE _IOW(';', 102, int) +#define VFIO_GROUP_GET_IOMMU_FD _IO(';', 103) +#define VFIO_GROUP_GET_DEVICE_FD _IOW(';', 104, char *) + +The last two ioctls return new file descriptors for accessing +individual devices within the group and programming the IOMMU. Each of +these new file descriptors provide their own set of file interfaces. +These ioctls will fail if any of the devices within the group are not +bound to their VFIO bus driver. Additionally, when either of these +interfaces are used, the group is then bound to the struct_mm of the +caller. The GET_FLAGS ioctl can be used to view the state of the group. + +When either the GET_IOMMU_FD or GET_DEVICE_FD ioctls are invoked, a +new IOMMU domain is created and all of the devices in the group are +attached to it. This is the only way to ensure full IOMMU isolation +of the group, but potentially wastes resources and cycles if the user +intends to manage multiple groups with the same set of IOMMU mappings. +VFIO therefore provides a group MERGE and UNMERGE interface, which +allows multiple groups to share an IOMMU domain. Not all IOMMUs allow +arbitrary groups to be merged, so the user should assume merging is +opportunistic. A new group, with no open device or IOMMU file +descriptors, can be merged into an existing, in-use, group using the +MERGE ioctl. A merged group can be unmerged using the UNMERGE ioctl +once all of the device file descriptors for the group being merged +"out" are closed. + +When groups are merged, the GET_IOMMU_FD and GET_DEVICE_FD ioctls are +essentially fungible between group file descriptors (ie. if device A +is in group X, and X is merged with Y, a file descriptor for A can be +retrieved using GET_DEVICE_FD on Y. Likewise, GET_IOMMU_FD returns a +file descriptor referencing the same internal IOMMU object from either
Documentation/vfio.txt line 110
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+X or Y). Merged groups can be dissolved either explictly with UNMERGE +or automatically when ALL file descriptors for the merged group are +closed (all IOMMUs, all devices, all groups). + +The IOMMU file descriptor provides this set of ioctls: + +#define VFIO_IOMMU_GET_FLAGS _IOR(';', 105, __u64) + #define VFIO_IOMMU_FLAGS_MAP_ANY (1 << 0) +#define VFIO_IOMMU_MAP_DMA _IOWR(';', 106, struct vfio_dma_map) +#define VFIO_IOMMU_UNMAP_DMA _IOWR(';', 107, struct vfio_dma_map)
Coherency support is not going to be addressed right? What about sync? Say you need to sync CPU to Device address?
Documentation/vfio.txt line 120
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+ +The GET_FLAGS ioctl returns basic information about the IOMMU domain. +We currently only support IOMMU domains that are able to map any +virtual address to any IOVA. This is indicated by the MAP_ANY flag. + +The (UN)MAP_DMA commands make use of struct vfio_dma_map for mapping +and unmapping IOVAs to process virtual addresses: + +struct vfio_dma_map { + __u64 len; /* length of structure */
What is the purpose of the 'len' field? Is it to guard against future version changes?
Documentation/vfio.txt line 129
Quoted 9 lines
Documentation/vfio.txt new file +9
-0+ __u64 vaddr; /* process virtual addr */ + __u64 dmaaddr; /* desired and/or returned dma address */ + __u64 size; /* size in bytes */ + __u64 flags; +#define VFIO_DMA_MAP_FLAG_WRITE (1 << 0) /* req writeable DMA mem */ +}; + +Current users of VFIO use relatively static DMA mappings, not requiring +high frequency turnover. As new users are added, it's expected that the
Is there a limit to how many DMA mappings can be created?
Documentation/vfio.txt line 137
Quoted 8 lines
Documentation/vfio.txt new file +8
-0+IOMMU file descriptor will evolve to support new mapping interfaces, this +will be reflected in the flags and may present new ioctls and file +interfaces. + +The device GET_FLAGS ioctl is intended to return basic device type and +indicate support for optional capabilities. Flags currently include whether +the device is PCI or described by Device Tree, and whether the RESET ioctl +is supported:
And reset in terms of PCIe spec is the FLR?
Documentation/vfio.txt line 144
Quoted 7 lines
Documentation/vfio.txt new file +7
-0+ +#define VFIO_DEVICE_GET_FLAGS _IOR(';', 108, __u64) + #define VFIO_DEVICE_FLAGS_PCI (1 << 0) + #define VFIO_DEVICE_FLAGS_DT (1 << 1) + #define VFIO_DEVICE_FLAGS_RESET (1 << 2) + +The MMIO and IOP resources used by a device are described by regions.
IOP?
Documentation/vfio.txt line 147
Quoted 3 lines
Documentation/vfio.txt new file +3
-0+The GET_NUM_REGIONS ioctl tells us how many regions the device supports: + +#define VFIO_DEVICE_GET_NUM_REGIONS _IOR(';', 109, int)
Don't want __u32?
Documentation/vfio.txt line 151
Quoted 5 lines
Documentation/vfio.txt new file +4
-0+ +Regions are described by a struct vfio_region_info, which is retrieved by +using the GET_REGION_INFO ioctl with vfio_region_info.index field set to +the desired region (0 based index). Note that devices may implement zero
+sized regions (vfio-pci does this to provide a 1:1 BAR to region index
Documentation/vfio.txt line 153
Quoted 1 line
Documentation/vfio.txt new file +1
-0+mapping).
Huh?
Documentation/vfio.txt line 163
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+ +struct vfio_region_info { + __u32 len; /* length of structure */ + __u32 index; /* region number */ + __u64 size; /* size in bytes of region */ + __u64 offset; /* start offset of region */ + __u64 flags; +#define VFIO_REGION_INFO_FLAG_MMAP (1 << 0) +#define VFIO_REGION_INFO_FLAG_RO (1 << 1) +#define VFIO_REGION_INFO_FLAG_PHYS_VALID (1 << 2)
What is FLAG_MMAP? Does it mean: 1) it can be mmaped, or 2) it is mmaped? FLAG_RO is pretty obvious - presumarily this is for firmware regions and such. And PHYS_VALID is if the region is disabled for some reasons? If so would the name FLAG_DISABLED be better?
Quoted 4 lines
Documentation/vfio.txt new file +4
-0+ __u64 phys; /* physical address of region */ +}; + +#define VFIO_DEVICE_GET_REGION_INFO _IOWR(';', 110, struct vfio_region_info)
Documentation/vfio.txt line 177
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+ +The offset indicates the offset into the device file descriptor which +accesses the given range (for read/write/mmap/seek). Flags indicate the +available access types and validity of optional fields. For instance +the phys field may only be valid for certain devices types. + +Interrupts are described using a similar interface. GET_NUM_IRQS +reports the number or IRQ indexes for the device. + +#define VFIO_DEVICE_GET_NUM_IRQS _IOR(';', 111, int)
_u32?
Documentation/vfio.txt line 188
Quoted 11 lines
Documentation/vfio.txt new file +11
-0+ +struct vfio_irq_info { + __u32 len; /* length of structure */ + __u32 index; /* IRQ number */ + __u32 count; /* number of individual IRQs */ + __u64 flags; +#define VFIO_IRQ_INFO_FLAG_LEVEL (1 << 0) +}; + +Again, zero count entries are allowed (vfio-pci uses a static interrupt +type to index mapping).
I am not really sure what that means.
Quoted 4 lines
Documentation/vfio.txt new file +4
-0+ +Information about each index can be retrieved using the GET_IRQ_INFO +ioctl, used much like GET_REGION_INFO. +
Documentation/vfio.txt line 202
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+#define VFIO_DEVICE_GET_IRQ_INFO _IOWR(';', 112, struct vfio_irq_info) + +Individual indexes can describe single or sets of IRQs. This provides the +flexibility to describe PCI INTx, MSI, and MSI-X using a single interface. + +All VFIO interrupts are signaled to userspace via eventfds. Integer arrays, +as shown below, are used to pass the IRQ info index, the number of eventfds, +and each eventfd to be signaled. Using a count of 0 disables the interrupt. + +/* Set IRQ eventfds, arg[0] = index, arg[1] = count, arg[2-n] = eventfds */
Are eventfds u64 or u32?
Why not just define a structure?
struct vfio_irq_eventfds {
__u32 index;
__u32 count;
__u64 eventfds[0]
};How do you get an eventfd to feed in here?
Documentation/vfio.txt line 202
Quoted 1 line
Documentation/vfio.txt new file +1
-0+#define VFIO_DEVICE_SET_IRQ_EVENTFDS _IOW(';', 113, int)
u32?
Documentation/vfio.txt line 209
Quoted 6 lines
Documentation/vfio.txt new file +6
-0+ +When a level triggered interrupt is signaled, the interrupt is masked +on the host. This prevents an unresponsive userspace driver from +continuing to interrupt the host system. After servicing the interrupt, +UNMASK_IRQ is used to allow the interrupt to retrigger. Note that level +triggered interrupts implicitly have a count of 1 per index.
So they are enabled automatically? Meaning you don't even hav to do SET_IRQ_EVENTFDS b/c the count is set to 1?
Documentation/vfio.txt line 212
Quoted 3 lines
Documentation/vfio.txt new file +3
-0+ +/* Unmask IRQ index, arg[0] = index */ +#define VFIO_DEVICE_UNMASK_IRQ _IOW(';', 114, int)
So this is for MSI as well? So if I've an index = 1, with count = 4, and doing unmaks IRQ will chip enable all the MSI event at once?
I guess there is not much point in enabling/disabling selective MSI IRQs..
Documentation/vfio.txt line 213
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+ +Level triggered interrupts can also be unmasked using an irqfd. Use
irqfd or eventfd?
Documentation/vfio.txt line 215
Quoted 1 line
Documentation/vfio.txt new file +1
-0+SET_UNMASK_IRQ_EVENTFD to set the file descriptor for this.
So only level triggered? Hmm, how do I know whether the device is level or edge? Or is that edge (MSI) can also be unmaked using the eventfs
Documentation/vfio.txt line 222
Quoted 7 lines
Documentation/vfio.txt new file +7
-0+ +/* Set unmask eventfd, arg[0] = index, arg[1] = eventfd */ +#define VFIO_DEVICE_SET_UNMASK_IRQ_EVENTFD _IOW(';', 115, int) + +When supported, as indicated by the device flags, reset the device. + +#define VFIO_DEVICE_RESET _IO(';', 116)
Does it disable the 'count'? Err, does it disable the IRQ on the device after this and one should call VFIO_DEVICE_SET_IRQ_EVENTFDS to set new eventfds? Or does it re-use the eventfds and the device is enabled after this?
Documentation/vfio.txt line 223
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+ +Device tree devices also invlude ioctls for further defining the
include
Documentation/vfio.txt line 229
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+device tree properties of the device: + +struct vfio_dtpath { + __u32 len; /* length of structure */ + __u32 index;
0 based I presume?
Documentation/vfio.txt line 230
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+ __u64 flags; +#define VFIO_DTPATH_FLAGS_REGION (1 << 0)
What is region in this context?? Or would this make much more sense if I knew what Device Tree actually is.
Documentation/vfio.txt line 232
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+#define VFIO_DTPATH_FLAGS_IRQ (1 << 1) + char *path;
Ah, now I see why you want 'len' here.. But I am still at loss why you want that with the other structures.
Documentation/vfio.txt line 240
Quoted 7 lines
Documentation/vfio.txt new file +7
-0+}; +#define VFIO_DEVICE_GET_DTPATH _IOWR(';', 117, struct vfio_dtpath) + +struct vfio_dtindex { + __u32 len; /* length of structure */ + __u32 index; + __u32 prop_type;
Is that an enum type? Is this definied somewhere?
Documentation/vfio.txt line 240
Quoted 1 line
Documentation/vfio.txt new file +1
-0+ __u32 prop_index;
What is the purpose of this field?
Quoted 4 lines
Documentation/vfio.txt new file +4
-0+ __u64 flags; +#define VFIO_DTINDEX_FLAGS_REGION (1 << 0) +#define VFIO_DTINDEX_FLAGS_IRQ (1 << 1) +};
Documentation/vfio.txt line 255
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+#define VFIO_DEVICE_GET_DTINDEX _IOWR(';', 118, struct vfio_dtindex) + + +VFIO bus driver API +------------------------------------------------------------------------------- + +Bus drivers, such as PCI, have three jobs: + 1) Add/remove devices from vfio + 2) Provide vfio_device_ops for device access + 3) Device binding and unbinding
suspend/resume?
Documentation/vfio.txt line 264
Quoted 9 lines
Documentation/vfio.txt new file +9
-0+ +When initialized, the bus driver should enumerate the devices on it's +bus and call vfio_group_add_dev() for each device. If the bus supports +hotplug, notifiers should be enabled to track devices being added and +removed. vfio_group_del_dev() removes a previously added device from +vfio. + +Adding a device registers a vfio_device_ops function pointer structure +for the device:
Huh? So this gets created for _every_ 'struct device' that is added the VFIO bus? Is this structure exposed? Or is this an internal one?
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+ +struct vfio_device_ops { + bool (*match)(struct device *, char *); + int (*get)(void *); + void (*put)(void *);
Documentation/vfio.txt line 279
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+ ssize_t (*read)(void *, char __user *, + size_t, loff_t *); + ssize_t (*write)(void *, const char __user *, + size_t, loff_t *); + long (*ioctl)(void *, unsigned int, unsigned long); + int (*mmap)(void *, struct vm_area_struct *); +}; + +When a device is bound to the bus driver, the bus driver indicates this +to vfio using the vfio_bind_dev() interface. The device_data parameter
Might want to paste the function decleration for it.. b/c I am not sure where the 'device_data' parameter is on the argument list.
Documentation/vfio.txt line 282
Quoted 3 lines
Documentation/vfio.txt new file +3
-0+is a pointer to an opaque data structure for use only by the bus driver. +The get, put, read, write, ioctl, and mmap vfio_device_ops all pass +this data structure back to the bus driver. When a device is unbound
Oh, so it is on the 'void *'.
Documentation/vfio.txt line 284
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+from the bus driver, the vfio_unbind_dev() interface signals this to +vfio. This function returns the pointer to the device_data structure
That function
Documentation/vfio.txt line 285
Quoted 1 line
Documentation/vfio.txt new file +1
-0+registered for the device.
I am not really sure what this section purpose is? Could this be part of the header file or the code? It does not look to be part of the ioctl API?
Quoted 24 lines
Documentation/vfio.txt new file +19
-0+ +As noted previously, a group contains one or more devices, so +GROUP_GET_DEVICE_FD needs to identify the specific device being requested. +The vfio_device_ops.match callback is used to allow bus drivers to determine +the match. For drivers like vfio-pci, it's a simple match to dev_name(), +which is unique in the system due to the PCI bus topology, other bus drivers +may need to include parent devices to create a unique match, so this is +left as a bus driver interface. + +------------------------------------------------------------------------------- + +[1] VFIO was originally an acronym for "Virtual Function I/O" in it's +initial implementation by Tom Lyon while as Cisco. We've since outgrown +the acronym, but it's catchy. + +[2] As always there are trade-offs to virtual machine device +assignment that are beyond the scope of VFIO. It's expected that +future IOMMU technologies will reduce some, but maybe not all, of +these trade-offs.MAINTAINERS +0
-0
MAINTAINERS S: Maintained · line 7111
Quoted 6 lines
MAINTAINERS +3
-0@@ -7106,6 +7106,14 @@ S: Maintained F: Documentation/filesystems/vfat.txt F: fs/fat/ +VFIO DRIVER +M: Alex Williamson <[email protected]> +L: [email protected]
No vfio mailing list? Or a vfio-mailing list?
Quoted 87 lines
MAINTAINERS +5
-0+S: Maintained +F: Documentation/vfio.txt +F: drivers/vfio/ +F: include/linux/vfio.h + VIDEOBUF2 FRAMEWORK M: Pawel Osciak <[email protected]> M: Marek Szyprowski <[email protected]>drivers/Kconfig +2
-0@@ -112,6 +112,8 @@ source "drivers/auxdisplay/Kconfig" source "drivers/uio/Kconfig" +source "drivers/vfio/Kconfig" + source "drivers/vlynq/Kconfig" source "drivers/virtio/Kconfig"drivers/Makefile +1
-0@@ -58,6 +58,7 @@ obj-$(CONFIG_ATM) += atm/ obj-$(CONFIG_FUSION) += message/ obj-y += firewire/ obj-$(CONFIG_UIO) += uio/ +obj-$(CONFIG_VFIO) += vfio/ obj-y += cdrom/ obj-y += auxdisplay/ obj-$(CONFIG_PCCARD) += pcmcia/drivers/vfio/Kconfig new file +8
-0@@ -0,0 +1,8 @@ +menuconfig VFIO + tristate "VFIO Non-Privileged userspace driver framework" + depends on IOMMU_API + help + VFIO provides a framework for secure userspace device drivers. + See Documentation/vfio.txt for more details. + + If you don't know what to do here, say N.drivers/vfio/Makefile new file +3
-0@@ -0,0 +1,3 @@ +vfio-y := vfio_main.o vfio_iommu.o + +obj-$(CONFIG_VFIO) := vfio.odrivers/vfio/vfio_iommu.c new file +25
-0@@ -0,0 +1,530 @@ +/* +* VFIO: IOMMU DMA mapping support +* +* Copyright (C) 2011 Red Hat, Inc. All rights reserved. +* Author: Alex Williamson <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* Derived from original vfio: +* Copyright 2010 Cisco Systems, Inc. All rights reserved. +* Author: Tom Lyon, [email protected] +*/ + +#include <linux/compat.h> +#include <linux/device.h> +#include <linux/fs.h> +#include <linux/iommu.h> +#include <linux/module.h> +#include <linux/mm.h> +#include <linux/sched.h> +#include <linux/slab.h> +#include <linux/uaccess.h> +#include <linux/vfio.h>
drivers/vfio/vfio_iommu.c line 35
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+#include <linux/workqueue.h> + +#include "vfio_private.h" + +struct dma_map_page { +struct list_head list; +dma_addr_t daddr; +unsigned long vaddr; +int npage; +int rdwr;
rdwr? Is this a flag thing? Could it be made in an enum?
Quoted 56 lines
drivers/vfio/vfio_iommu.c new file +56
-0+}; + +/* +* This code handles mapping and unmapping of user data buffers +* into DMA'ble space using the IOMMU +*/ + +#define NPAGE_TO_SIZE (npage) ((size_t)(npage) << PAGE_SHIFT) + +struct vwork { +struct mm_struct *mm; +int npage; +struct work_struct work; +}; + +/* delayed decrement for locked_vm */ +static void vfio_lock_acct_bg (struct work_struct *work) +{ + struct vwork *vwork = container_of(work, struct vwork, work); +struct mm_struct *mm; + + mm = vwork->mm; +down_write (&mm->mmap_sem); + mm->locked_vm += vwork->npage; +up_write (&mm->mmap_sem); +mmput (mm);/* unref mm */ +kfree (vwork); +} + +static void vfio_lock_acct (int npage) +{ +struct vwork *vwork; +struct mm_struct *mm; + +if (!current->mm) { +/* process exited */ +return ; + } +if (down_write_trylock (¤t->mm->mmap_sem)) { + current->mm->locked_vm += npage; +up_write (¤t->mm->mmap_sem); +return ; + } +/* +* Couldn't get mmap_sem lock, so must setup to decrement +* mm->locked_vm later. If locked_vm were atomic, we wouldn't +* need this silliness +*/ + vwork =kmalloc (sizeof (struct vwork ),GFP_KERNEL ); +if (!vwork) +return ; + mm =get_task_mm (current);/* take ref mm */ +if (!mm) { +kfree (vwork); +return ; + }
drivers/vfio/vfio_iommu.c line 101
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+INIT_WORK (&vwork->work, vfio_lock_acct_bg); + vwork->mm = mm; + vwork->npage = npage; +schedule_work (&vwork->work); +} + +/* Some mappings aren't backed by a struct page, for example an mmap'd +* MMIO range for our own or another device. These use a different +* pfn conversion and shouldn't be tracked as locked pages. */ +static int is_invalid_reserved_pfn (unsigned long pfn)
static bool
drivers/vfio/vfio_iommu.c line 107
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+{ +if (pfn_valid (pfn)) { +int reserved; +struct page *tail =pfn_to_page (pfn); +struct page *head =compound_trans_head (tail); + reserved =PageReserved (head);
bool reserved = PageReserved(head);
Quoted 24 lines
drivers/vfio/vfio_iommu.c new file +24
-0+if (head != tail) { +/* "head" is not a dangling pointer +* (compound_trans_head takes care of that) +* but the hugepage may have been split +* from under us (and we may not hold a +* reference count on the head page so it can +* be reused before we run PageReferenced), so +* we've to check PageTail before returning +* what we just read. +*/ +smp_rmb (); +if (PageTail (tail)) +return reserved; + } +return PageReserved (tail); + } + +return true; +} + +static int put_pfn (unsigned long pfn,int rdwr) +{ +if (!is_invalid_reserved_pfn (pfn)) { +struct page *page =pfn_to_page (pfn);
drivers/vfio/vfio_iommu.c line 141
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+if (rdwr) +SetPageDirty (page); +put_page (page); +return 1 ; + } +return 0 ; +} + +/* Unmap DMA region */ +/* dgate must be held */
dgate?
drivers/vfio/vfio_iommu.c line 152
Quoted 11 lines
drivers/vfio/vfio_iommu.c new file +11
-0+static int __vfio_dma_unmap (struct vfio_iommu *iommu,unsigned long iova, +int npage,int rdwr) +{ +int i, unlocked =0 ; + +for (i =0 ; i < npage; i++, iova +=PAGE_SIZE ) { +unsigned long pfn; + + pfn =iommu_iova_to_phys (iommu->domain, iova) >>PAGE_SHIFT ; +if (pfn) { +iommu_unmap (iommu->domain, iova,0 );
What is the '0' for? Perhaps a comment: /* We only do zero order */
Quoted 9 lines
drivers/vfio/vfio_iommu.c new file +9
-0+ unlocked +=put_pfn (pfn, rdwr); + } + } +return unlocked; +} + +static void vfio_dma_unmap (struct vfio_iommu *iommu,unsigned long iova, +unsigned long npage,int rdwr) +{
drivers/vfio/vfio_iommu.c line 171
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+int unlocked; + + unlocked =__vfio_dma_unmap (iommu, iova, npage, rdwr); +vfio_lock_acct (-unlocked); +} + +/* Unmap ALL DMA regions */ +void vfio_iommu_unmapall (struct vfio_iommu *iommu) +{ +struct list_head *pos, *pos2;
pos2 should probably be just called 'tmp'
drivers/vfio/vfio_iommu.c line 172
Quoted 1 line
drivers/vfio/vfio_iommu.c new file +1
-0+struct dma_map_page *mlp;
What does 'mlp' stand for?
mlp -> dma_page ?
drivers/vfio/vfio_iommu.c line 177
Quoted 5 lines
drivers/vfio/vfio_iommu.c new file +5
-0+ +mutex_lock (&iommu->dgate); +list_for_each_safe (pos, pos2, &iommu->dm_list) { + mlp = list_entry(pos, struct dma_map_page, list); +vfio_dma_unmap (iommu, mlp->daddr, mlp->npage, mlp->rdwr);
Uh, so if it did not get put_page() we would try to still delete it? Couldn't that lead to corruption as the 'mlp' is returned to the poll?
Ah wait, the put_page is on the DMA page, so it is OK to delete the tracking structure. It will be just a leaked page.
Quoted 15 lines
drivers/vfio/vfio_iommu.c new file +15
-0+list_del (&mlp->list); +kfree (mlp); + } +mutex_unlock (&iommu->dgate); +} + +static int vaddr_get_pfn (unsigned long vaddr,int rdwr,unsigned long *pfn) +{ +struct page *page[1 ]; +struct vm_area_struct *vma; +int ret = -EFAULT ; + +if (get_user_pages_fast (vaddr,1 , rdwr, page) ==1 ) { + *pfn =page_to_pfn (page[0 ]); +return 0 ;
drivers/vfio/vfio_iommu.c line 202
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+ } + +down_read (¤t->mm->mmap_sem); + + vma =find_vma_intersection (current->mm, vaddr, vaddr +1 ); + +if (vma && vma->vm_flags &VM_PFNMAP ) { + *pfn = ((vaddr - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff; +if (is_invalid_reserved_pfn (*pfn)) + ret =0 ;
Did you mean to break here?
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ } + +up_read (¤t->mm->mmap_sem); + +return ret; +}
drivers/vfio/vfio_iommu.c line 218
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+ +/* Map DMA region */ +/* dgate must be held */ +static int vfio_dma_map (struct vfio_iommu *iommu,unsigned long iova, +unsigned long vaddr,int npage,int rdwr) +{ +unsigned long start = iova; +int i, ret, locked =0 , prot =IOMMU_READ ; + +/* Verify pages are not already mapped */
I think a 'that' is missing above.
Quoted 16 lines
drivers/vfio/vfio_iommu.c new file +16
-0+for (i =0 ; i < npage; i++, iova +=PAGE_SIZE ) +if (iommu_iova_to_phys (iommu->domain, iova)) +return -EBUSY ; + + iova = start; + +if (rdwr) + prot |=IOMMU_WRITE ; +if (iommu->cache) + prot |=IOMMU_CACHE ; + +for (i =0 ; i < npage; i++, iova +=PAGE_SIZE , vaddr +=PAGE_SIZE ) { +unsigned long pfn =0 ; + + ret =vaddr_get_pfn (vaddr, rdwr, &pfn); +if (ret) {
drivers/vfio/vfio_iommu.c line 244
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+__vfio_dma_unmap (iommu, start, i, rdwr); +return ret; + } + +/* Only add actual locked pages to accounting */ +if (!is_invalid_reserved_pfn (pfn)) + locked++; + + ret =iommu_map (iommu->domain, iova, + (phys_addr_t )pfn <<PAGE_SHIFT ,0 , prot);
Put a comment by the 0 saying /* order 0 pages only! */
drivers/vfio/vfio_iommu.c line 256
Quoted 12 lines
drivers/vfio/vfio_iommu.c new file +12
-0+if (ret) { +/* Back out mappings on error */ +put_pfn (pfn, rdwr); +__vfio_dma_unmap (iommu, start, i, rdwr); +return ret; + } + } +vfio_lock_acct (locked); +return 0 ; +} + +static inline int ranges_overlap(unsigned long start1, size_t size1,
Perhaps a bool?
Quoted 218 lines
drivers/vfio/vfio_iommu.c new file +218
-0+ unsigned long start2, size_t size2) +{ +return !(start1 + size1 <= start2 || start2 + size2 <= start1); +} + +static struct dma_map_page *vfio_find_dma (struct vfio_iommu *iommu, +dma_addr_t start,size_t size) +{ +struct list_head *pos; +struct dma_map_page *mlp; + +list_for_each (pos, &iommu->dm_list) { + mlp = list_entry(pos, struct dma_map_page, list); +if (ranges_overlap (mlp->daddr,NPAGE_TO_SIZE (mlp->npage), + start, size)) +return mlp; + } +return NULL ; +} + +int vfio_remove_dma_overlap (struct vfio_iommu *iommu,dma_addr_t start, +size_t size,struct dma_map_page *mlp) +{ +struct dma_map_page *split; +int npage_lo, npage_hi; + +/* Existing dma region is completely covered, unmap all */ +if (start <= mlp->daddr && + start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +vfio_dma_unmap (iommu, mlp->daddr, mlp->npage, mlp->rdwr); +list_del (&mlp->list); + npage_lo = mlp->npage; +kfree (mlp); +return npage_lo; + } + +/* Overlap low address of existing range */ +if (start <= mlp->daddr) { +size_t overlap; + + overlap = start + size - mlp->daddr; + npage_lo = overlap >>PAGE_SHIFT ; + npage_hi = mlp->npage - npage_lo; + +vfio_dma_unmap (iommu, mlp->daddr, npage_lo, mlp->rdwr); + mlp->daddr += overlap; + mlp->vaddr += overlap; + mlp->npage -= npage_lo; +return npage_lo; + } + +/* Overlap high address of existing range */ +if (start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +size_t overlap; + + overlap = mlp->daddr +NPAGE_TO_SIZE (mlp->npage) - start; + npage_hi = overlap >>PAGE_SHIFT ; + npage_lo = mlp->npage - npage_hi; + +vfio_dma_unmap (iommu, start, npage_hi, mlp->rdwr); + mlp->npage -= npage_hi; +return npage_hi; + } + +/* Split existing */ + npage_lo = (start - mlp->daddr) >>PAGE_SHIFT ; + npage_hi = mlp->npage - (size >>PAGE_SHIFT ) - npage_lo; + + split =kzalloc (sizeof *split,GFP_KERNEL ); +if (!split) +return -ENOMEM ; + +vfio_dma_unmap (iommu, start, size >>PAGE_SHIFT , mlp->rdwr); + + mlp->npage = npage_lo; + + split->npage = npage_hi; + split->daddr = start + size; + split->vaddr = mlp->vaddr +NPAGE_TO_SIZE (npage_lo) + size; + split->rdwr = mlp->rdwr; +list_add (&split->list, &iommu->dm_list); +return size >>PAGE_SHIFT ; +} + +int vfio_dma_unmap_dm (struct vfio_iommu *iommu,struct vfio_dma_map *dmp) +{ +int ret =0 ; +size_t npage = dmp->size >>PAGE_SHIFT ; +struct list_head *pos, *n; + +if (dmp->dmaaddr & ~PAGE_MASK ) +return -EINVAL ; +if (dmp->size & ~PAGE_MASK ) +return -EINVAL ; + +mutex_lock (&iommu->dgate); + +list_for_each_safe (pos, n, &iommu->dm_list) { +struct dma_map_page *mlp; + + mlp = list_entry(pos, struct dma_map_page, list); +if (ranges_overlap (mlp->daddr,NPAGE_TO_SIZE (mlp->npage), + dmp->dmaaddr, dmp->size)) { + ret =vfio_remove_dma_overlap (iommu, dmp->dmaaddr, + dmp->size, mlp); +if (ret >0 ) + npage -=NPAGE_TO_SIZE (ret); +if (ret <0 || npage ==0 ) +break ; + } + } +mutex_unlock (&iommu->dgate); +return ret >0 ?0 : ret; +} + +int vfio_dma_map_dm (struct vfio_iommu *iommu,struct vfio_dma_map *dmp) +{ +int npage; +struct dma_map_page *mlp, *mmlp =NULL ; +dma_addr_t daddr = dmp->dmaaddr; +unsigned long locked, lock_limit, vaddr = dmp->vaddr; +size_t size = dmp->size; +int ret =0 , rdwr = dmp->flags &VFIO_DMA_MAP_FLAG_WRITE ; + +if (vaddr & (PAGE_SIZE -1 )) +return -EINVAL ; +if (daddr & (PAGE_SIZE -1 )) +return -EINVAL ; +if (size & (PAGE_SIZE -1 )) +return -EINVAL ; + + npage = size >>PAGE_SHIFT ; +if (!npage) +return -EINVAL ; + +if (!iommu) +return -EINVAL ; + +mutex_lock (&iommu->dgate); + +if (vfio_find_dma (iommu, daddr, size)) { + ret = -EBUSY ; + goto out_lock; + } + +/* account for locked pages */ + locked = current->mm->locked_vm + npage; + lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; +if (locked > lock_limit && !capable (CAP_IPC_LOCK )) { +printk (KERN_WARNING "%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , + __func__,rlimit (RLIMIT_MEMLOCK )); + ret = -ENOMEM ; + goto out_lock; + } + + ret =vfio_dma_map (iommu, daddr, vaddr, npage, rdwr); +if (ret) + goto out_lock; + +/* Check if we abut a region below */ +if (daddr) { + mlp =vfio_find_dma (iommu, daddr -1 ,1 ); +if (mlp && mlp->rdwr == rdwr && + mlp->vaddr +NPAGE_TO_SIZE (mlp->npage) == vaddr) { + + mlp->npage += npage; + daddr = mlp->daddr; + vaddr = mlp->vaddr; + npage = mlp->npage; + size =NPAGE_TO_SIZE (npage); + + mmlp = mlp; + } + } + +if (daddr + size) { + mlp =vfio_find_dma (iommu, daddr + size,1 ); +if (mlp && mlp->rdwr == rdwr && mlp->vaddr == vaddr + size) { + + mlp->npage += npage; + mlp->daddr = daddr; + mlp->vaddr = vaddr; + +/* If merged above and below, remove previously +* merged entry. New entry covers it. */ +if (mmlp) { +list_del (&mmlp->list); +kfree (mmlp); + } + mmlp = mlp; + } + } + +if (!mmlp) { + mlp =kzalloc (sizeof *mlp,GFP_KERNEL ); +if (!mlp) { + ret = -ENOMEM ; +vfio_dma_unmap (iommu, daddr, npage, rdwr); + goto out_lock; + } + + mlp->npage = npage; + mlp->daddr = daddr; + mlp->vaddr = vaddr; + mlp->rdwr = rdwr; +list_add (&mlp->list, &iommu->dm_list); + } + +out_lock: +mutex_unlock (&iommu->dgate); +return ret; +} + +static int vfio_iommu_release (struct inode *inode,struct file *filep) +{ +struct vfio_iommu *iommu = filep->private_data; + +vfio_release_iommu (iommu);
drivers/vfio/vfio_iommu.c line 484
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+return 0 ; +} + +static long vfio_iommu_unl_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ +struct vfio_iommu *iommu = filep->private_data; +int ret = -ENOSYS ; + +if (cmd ==VFIO_IOMMU_GET_FLAGS ) {
Something is weird with the tabbing here..
Quoted 80 lines
drivers/vfio/vfio_iommu.c new file +46
-0+u64 flags =VFIO_IOMMU_FLAGS_MAP_ANY ; + + ret =put_user (flags, (u64 __user *)arg); + + }else if (cmd ==VFIO_IOMMU_MAP_DMA ) { +struct vfio_dma_map dm; + +if (copy_from_user (&dm, (void __user *)arg,sizeof dm)) +return -EFAULT ; + + ret =vfio_dma_map_dm (iommu, &dm); + +if (!ret &©_to_user ((void __user *)arg, &dm,sizeof dm)) + ret = -EFAULT ; + + }else if (cmd ==VFIO_IOMMU_UNMAP_DMA ) { +struct vfio_dma_map dm; + +if (copy_from_user (&dm, (void __user *)arg,sizeof dm)) +return -EFAULT ; + + ret =vfio_dma_unmap_dm (iommu, &dm); + +if (!ret &©_to_user ((void __user *)arg, &dm,sizeof dm)) + ret = -EFAULT ; + } +return ret; +} + +#ifdef CONFIG_COMPAT +static long vfio_iommu_compat_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ + arg = (unsigned long )compat_ptr (arg); +return vfio_iommu_unl_ioctl (filep, cmd, arg); +} +#endif /* CONFIG_COMPAT */ + +const struct file_operations vfio_iommu_fops = { + .owner =THIS_MODULE , + .release = vfio_iommu_release, + .unlocked_ioctl = vfio_iommu_unl_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = vfio_iommu_compat_ioctl, +#endif +};drivers/vfio/vfio_main.c new file +28
-0@@ -0,0 +1,1151 @@ +/* +* VFIO framework +* +* Copyright (C) 2011 Red Hat, Inc. All rights reserved. +* Author: Alex Williamson <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* Derived from original vfio: +* Copyright 2010 Cisco Systems, Inc. All rights reserved. +* Author: Tom Lyon, [email protected] +*/ + +#include <linux/cdev.h> +#include <linux/compat.h> +#include <linux/device.h> +#include <linux/file.h> +#include <linux/anon_inodes.h> +#include <linux/fs.h> +#include <linux/idr.h> +#include <linux/iommu.h> +#include <linux/mm.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/uaccess.h>
drivers/vfio/vfio_main.c line 38
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+#include <linux/vfio.h> +#include <linux/wait.h> + +#include "vfio_private.h" + +#define DRIVER_VERSION "0.2" +#define DRIVER_AUTHOR "Alex Williamson <[email protected]>" +#define DRIVER_DESC "VFIO - User Level meta-driver" + +static int allow_unsafe_intrs;
__read_mostly
drivers/vfio/vfio_main.c line 39
Quoted 1 line
drivers/vfio/vfio_main.c new file +1
-0+module_param (allow_unsafe_intrs, int,0 );
S_IRUGO ?
drivers/vfio/vfio_main.c line 52
Quoted 13 lines
drivers/vfio/vfio_main.c new file +13
-0+MODULE_PARM_DESC (allow_unsafe_intrs, +"Allow use of IOMMUs which do not support interrupt remapping" ); + +static struct vfio { +dev_t devt; +struct cdev cdev; +struct list_head group_list; +struct mutex lock; +struct kref kref; +struct class *class; +struct idr idr; +wait_queue_head_t release_q; +} vfio;
You probably want to move this below the 'vfio_group' as vfio contains the vfio_group.
Quoted 15 lines
drivers/vfio/vfio_main.c new file +15
-0+ +static const struct file_operations vfio_group_fops; +extern const struct file_operations vfio_iommu_fops; + +struct vfio_group { +dev_t devt; +unsigned int groupid; +struct bus_type *bus; +struct vfio_iommu *iommu; +struct list_head device_list; +struct list_head iommu_next; +struct list_head group_next; +int refcnt; +}; +
drivers/vfio/vfio_main.c line 77
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+struct vfio_device { +struct device *dev; +const struct vfio_device_ops *ops; +struct vfio_iommu *iommu; +struct vfio_group *group; +struct list_head device_next; +bool attached; +int refcnt; +void *device_data; +};
And perhaps move this above vfio_group. As vfio_group contains a list of these structures?
Quoted 70 lines
drivers/vfio/vfio_main.c new file +70
-0+ +/* +* Helper functions called under vfio.lock +*/ + +/* Return true if any devices within a group are opened */ +static bool __vfio_group_devs_inuse (struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next); +if (device->refcnt) +return true; + } +return false; +} + +/* Return true if any of the groups attached to an iommu are opened. +* We can only tear apart merged groups when nothing is left open. */ +static bool __vfio_iommu_groups_inuse (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group, iommu_next); +if (group->refcnt) +return true; + } +return false; +} + +/* An iommu is "in use" if it has a file descriptor open or if any of +* the groups assigned to the iommu have devices open. */ +static bool __vfio_iommu_inuse (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (iommu->refcnt) +return true; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group, iommu_next); + +if (__vfio_group_devs_inuse (group)) +return true; + } +return false; +} + +static void __vfio_group_set_iommu (struct vfio_group *group, +struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (group->iommu) +list_del (&group->iommu_next); +if (iommu) +list_add (&group->iommu_next, &iommu->group_list); + + group->iommu = iommu; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device;
drivers/vfio/vfio_main.c line 157
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+ + device = list_entry(pos, struct vfio_device, device_next); + device->iommu = iommu; + } +} + +static void __vfio_iommu_detach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +BUG_ON (!iommu->domain && device->attached);
Whoa. Heavy hammer there.
Perhaps WARN_ON as you do check it later on.
Quoted 17 lines
drivers/vfio/vfio_main.c new file +17
-0+ +if (!iommu->domain || !device->attached) +return ; + +iommu_detach_device (iommu->domain, device->dev); + device->attached = false; +} + +static void __vfio_iommu_detach_group (struct vfio_iommu *iommu, +struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next);
drivers/vfio/vfio_main.c line 184
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+__vfio_iommu_detach_dev (iommu, device); + } +} + +static int __vfio_iommu_attach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +int ret; + +BUG_ON (device->attached);
How about:
WARN_ON(device->attached, "The engineer who wrote the user-space device driver is trying to register the device again! Tell him/her to stop please.\n");
Quoted 80 lines
drivers/vfio/vfio_main.c new file +80
-0+ +if (!iommu || !iommu->domain) +return -EINVAL ; + + ret =iommu_attach_device (iommu->domain, device->dev); +if (!ret) + device->attached = true; + +return ret; +} + +static int __vfio_iommu_attach_group (struct vfio_iommu *iommu, +struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; +int ret; + + device = list_entry(pos, struct vfio_device, device_next); + ret =__vfio_iommu_attach_dev (iommu, device); +if (ret) { +__vfio_iommu_detach_group (iommu, group); +return ret; + } + } +return 0 ; +} + +/* The iommu is viable, ie. ready to be configured, when all the devices +* for all the groups attached to the iommu are bound to their vfio device +* drivers (ex. vfio-pci). This sets the device_data private data pointer. */ +static bool __vfio_iommu_viable (struct vfio_iommu *iommu) +{ +struct list_head *gpos, *dpos; + +list_for_each (gpos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(gpos, struct vfio_group, iommu_next); + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + device =list_entry (dpos, + struct vfio_device, device_next); + +if (!device->device_data) +return false; + } + } +return true; +} + +static void __vfio_close_iommu (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (!iommu->domain) +return ; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(pos, struct vfio_group, iommu_next); + +__vfio_iommu_detach_group (iommu, group); + } + +vfio_iommu_unmapall (iommu); + +iommu_domain_free (iommu->domain); + iommu->domain =NULL ; + iommu->mm =NULL ; +} + +/* Open the IOMMU. This gates all access to the iommu or device file +* descriptors and sets current->mm as the exclusive user. */ +static int __vfio_open_iommu (struct vfio_iommu *iommu) +{ +struct list_head *pos; +int ret;
drivers/vfio/vfio_main.c line 274
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+ +if (!__vfio_iommu_viable (iommu)) +return -EBUSY ; + +if (iommu->domain) +return -EINVAL ; + + iommu->domain =iommu_domain_alloc (iommu->bus); +if (!iommu->domain) +return -EFAULT ;
ENOMEM?
Quoted 50 lines
drivers/vfio/vfio_main.c new file +50
-0+ +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(pos, struct vfio_group, iommu_next); + + ret =__vfio_iommu_attach_group (iommu, group); +if (ret) { +__vfio_close_iommu (iommu); +return ret; + } + } + +if (!allow_unsafe_intrs && + !iommu_domain_has_cap (iommu->domain,IOMMU_CAP_INTR_REMAP )) { +__vfio_close_iommu (iommu); +return -EFAULT ; + } + + iommu->cache = (iommu_domain_has_cap (iommu->domain, +IOMMU_CAP_CACHE_COHERENCY ) !=0 ); + iommu->mm = current->mm; + +return 0 ; +} + +/* Actively try to tear down the iommu and merged groups. If there are no +* open iommu or device fds, we close the iommu. If we close the iommu and +* there are also no open group fds, we can futher dissolve the group to +* iommu association and free the iommu data structure. */ +static int __vfio_try_dissolve_iommu (struct vfio_iommu *iommu) +{ + +if (__vfio_iommu_inuse (iommu)) +return -EBUSY ; + +__vfio_close_iommu (iommu); + +if (!__vfio_iommu_groups_inuse (iommu)) { +struct list_head *pos, *ppos; + +list_for_each_safe (pos, ppos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group, iommu_next); +__vfio_group_set_iommu (group,NULL ); + } + + +kfree (iommu); + }
drivers/vfio/vfio_main.c line 334
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+ +return 0 ; +} + +static struct vfio_device *__vfio_lookup_dev (struct device *dev) +{ +struct list_head *gpos; +unsigned int groupid; + +if (iommu_device_group (dev, &groupid))
Hmm, where is this defined? v3.2-rc1 does not seem to have it?
Quoted 527 lines
drivers/vfio/vfio_main.c new file +527
-0+return NULL ; + +list_for_each (gpos, &vfio.group_list) { +struct vfio_group *group; +struct list_head *dpos; + + group = list_entry(gpos, struct vfio_group, group_next); + +if (group->groupid != groupid) +continue ; + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + + device =list_entry (dpos, + struct vfio_device, device_next); + +if (device->dev == dev) +return device; + } + } +return NULL ; +} + +/* All release paths simply decrement the refcnt, attempt to teardown +* the iommu and merged groups, and wakeup anything that might be +* waiting if we successfully dissolve anything. */ +static int vfio_do_release (int *refcnt,struct vfio_iommu *iommu) +{ +bool wake; + +mutex_lock (&vfio.lock); + + (*refcnt)--; + wake = (__vfio_try_dissolve_iommu (iommu) ==0 ); + +mutex_unlock (&vfio.lock); + +if (wake) +wake_up (&vfio.release_q); + +return 0 ; +} + +/* +* Device fops - passthrough to vfio device driver w/ device_data +*/ +static int vfio_device_release (struct inode *inode,struct file *filep) +{ +struct vfio_device *device = filep->private_data; + +vfio_do_release (&device->refcnt, device->iommu); + + device->ops->put (device->device_data); + +return 0 ; +} + +static long vfio_device_unl_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ +struct vfio_device *device = filep->private_data; + +return device->ops->ioctl (device->device_data, cmd, arg); +} + +static ssize_t vfio_device_read (struct file *filep,char __user *buf, +size_t count,loff_t *ppos) +{ +struct vfio_device *device = filep->private_data; + +return device->ops->read (device->device_data, buf, count, ppos); +} + +static ssize_t vfio_device_write (struct file *filep,const char __user *buf, +size_t count,loff_t *ppos) +{ +struct vfio_device *device = filep->private_data; + +return device->ops->write (device->device_data, buf, count, ppos); +} + +static int vfio_device_mmap (struct file *filep,struct vm_area_struct *vma) +{ +struct vfio_device *device = filep->private_data; + +return device->ops->mmap (device->device_data, vma); +} + +#ifdef CONFIG_COMPAT +static long vfio_device_compat_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ + arg = (unsigned long )compat_ptr (arg); +return vfio_device_unl_ioctl (filep, cmd, arg); +} +#endif /* CONFIG_COMPAT */ + +const struct file_operations vfio_device_fops = { + .owner =THIS_MODULE , + .release = vfio_device_release, + .read = vfio_device_read, + .write = vfio_device_write, + .unlocked_ioctl = vfio_device_unl_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = vfio_device_compat_ioctl, +#endif + .mmap = vfio_device_mmap, +}; + +/* +* Group fops +*/ +static int vfio_group_open (struct inode *inode,struct file *filep) +{ +struct vfio_group *group; +int ret =0 ; + +mutex_lock (&vfio.lock); + + group =idr_find (&vfio.idr,iminor (inode)); + +if (!group) { + ret = -ENODEV ; + goto out; + } + + filep->private_data = group; + +if (!group->iommu) { +struct vfio_iommu *iommu; + + iommu =kzalloc (sizeof (*iommu),GFP_KERNEL ); +if (!iommu) { + ret = -ENOMEM ; + goto out; + } +INIT_LIST_HEAD (&iommu->group_list); +INIT_LIST_HEAD (&iommu->dm_list); +mutex_init (&iommu->dgate); + iommu->bus = group->bus; +__vfio_group_set_iommu (group, iommu); + } + group->refcnt++; + +out: +mutex_unlock (&vfio.lock); + +return ret; +} + +static int vfio_group_release (struct inode *inode,struct file *filep) +{ +struct vfio_group *group = filep->private_data; + +return vfio_do_release (&group->refcnt, group->iommu); +} + +/* Attempt to merge the group pointed to by fd into group. The merge-ee +* group must not have an iommu or any devices open because we cannot +* maintain that context across the merge. The merge-er group can be +* in use. */ +static int vfio_group_merge (struct vfio_group *group,int fd) +{ +struct vfio_group *new; +struct vfio_iommu *old_iommu; +struct file *file; +int ret =0 ; +bool opened = false; + +mutex_lock (&vfio.lock); + + file =fget (fd); +if (!file) { + ret = -EBADF ; + goto out_noput; + } + +/* Sanity check, is this really our fd? */ +if (file->f_op != &vfio_group_fops) { + ret = -EINVAL ; + goto out; + } + + new = file->private_data; + +if (!new || new == group || !new->iommu || + new->iommu->domain || new->bus != group->bus) { + ret = -EINVAL ; + goto out; + } + +/* We need to attach all the devices to each domain separately +* in order to validate that the capabilities match for both. */ + ret =__vfio_open_iommu (new->iommu); +if (ret) + goto out; + +if (!group->iommu->domain) { + ret =__vfio_open_iommu (group->iommu); +if (ret) + goto out; + opened = true; + } + +/* If cache coherency doesn't match we'd potentialy need to +* remap existing iommu mappings in the merge-er domain. +* Poor return to bother trying to allow this currently. */ +if (iommu_domain_has_cap (group->iommu->domain, +IOMMU_CAP_CACHE_COHERENCY ) != +iommu_domain_has_cap (new->iommu->domain, +IOMMU_CAP_CACHE_COHERENCY )) { +__vfio_close_iommu (new->iommu); +if (opened) +__vfio_close_iommu (group->iommu); + ret = -EINVAL ; + goto out; + } + +/* Close the iommu for the merge-ee and attach all its devices +* to the merge-er iommu. */ +__vfio_close_iommu (new->iommu); + + ret =__vfio_iommu_attach_group (group->iommu, new); +if (ret) + goto out; + +/* set_iommu unlinks new from the iommu, so save a pointer to it */ + old_iommu = new->iommu; +__vfio_group_set_iommu (new, group->iommu); +kfree (old_iommu); + +out: +fput (file); +out_noput: +mutex_unlock (&vfio.lock); +return ret; +} + +/* Unmerge the group pointed to by fd from group. */ +static int vfio_group_unmerge (struct vfio_group *group,int fd) +{ +struct vfio_group *new; +struct vfio_iommu *new_iommu; +struct file *file; +int ret =0 ; + +/* Since the merge-out group is already opened, it needs to +* have an iommu struct associated with it. */ + new_iommu =kzalloc (sizeof (*new_iommu),GFP_KERNEL ); +if (!new_iommu) +return -ENOMEM ; + +INIT_LIST_HEAD (&new_iommu->group_list); +INIT_LIST_HEAD (&new_iommu->dm_list); +mutex_init (&new_iommu->dgate); + new_iommu->bus = group->bus; + +mutex_lock (&vfio.lock); + + file =fget (fd); +if (!file) { + ret = -EBADF ; + goto out_noput; + } + +/* Sanity check, is this really our fd? */ +if (file->f_op != &vfio_group_fops) { + ret = -EINVAL ; + goto out; + } + + new = file->private_data; +if (!new || new == group || new->iommu != group->iommu) { + ret = -EINVAL ; + goto out; + } + +/* We can't merge-out a group with devices still in use. */ +if (__vfio_group_devs_inuse (new)) { + ret = -EBUSY ; + goto out; + } + +__vfio_iommu_detach_group (group->iommu, new); +__vfio_group_set_iommu (new, new_iommu); + +out: +fput (file); +out_noput: +if (ret) +kfree (new_iommu); +mutex_unlock (&vfio.lock); +return ret; +} + +/* Get a new iommu file descriptor. This will open the iommu, setting +* the current->mm ownership if it's not already set. */ +static int vfio_group_get_iommu_fd (struct vfio_group *group) +{ +int ret =0 ; + +mutex_lock (&vfio.lock); + +if (!group->iommu->domain) { + ret =__vfio_open_iommu (group->iommu); +if (ret) + goto out; + } + + ret =anon_inode_getfd ("[vfio-iommu]" , &vfio_iommu_fops, + group->iommu,O_RDWR ); +if (ret <0 ) + goto out; + + group->iommu->refcnt++; +out: +mutex_unlock (&vfio.lock); +return ret; +} + +/* Get a new device file descriptor. This will open the iommu, setting +* the current->mm ownership if it's not already set. It's difficult to +* specify the requirements for matching a user supplied buffer to a +* device, so we use a vfio driver callback to test for a match. For +* PCI, dev_name(dev) is unique, but other drivers may require including +* a parent device string. */ +static int vfio_group_get_device_fd (struct vfio_group *group,char *buf) +{ +struct vfio_iommu *iommu = group->iommu; +struct list_head *gpos; +int ret = -ENODEV ; + +mutex_lock (&vfio.lock); + +if (!iommu->domain) { + ret =__vfio_open_iommu (iommu); +if (ret) + goto out; + } + +list_for_each (gpos, &iommu->group_list) { +struct list_head *dpos; + + group = list_entry(gpos, struct vfio_group, iommu_next); + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + + device =list_entry (dpos, + struct vfio_device, device_next); + +if (device->ops->match (device->dev, buf)) { +struct file *file; + +if (device->ops->get (device->device_data)) { + ret = -EFAULT ; + goto out; + } + +/* We can't use anon_inode_getfd(), like above +* because we need to modify the f_mode flags +* directly to allow more than just ioctls */ + ret =get_unused_fd (); +if (ret <0 ) { + device->ops->put (device->device_data); + goto out; + } + + file =anon_inode_getfile ("[vfio-device]" , + &vfio_device_fops, + device,O_RDWR ); +if (IS_ERR (file)) { +put_unused_fd (ret); + ret =PTR_ERR (file); + device->ops->put (device->device_data); + goto out; + } + +/* Todo: add an anon_inode interface to do +* this. Appears to be missing by lack of +* need rather than explicitly prevented. +* Now there's need. */ + file->f_mode |= (FMODE_LSEEK | +FMODE_PREAD | +FMODE_PWRITE ); + +fd_install (ret, file); + + device->refcnt++; + goto out; + } + } + } +out: +mutex_unlock (&vfio.lock); +return ret; +} + +static long vfio_group_unl_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ +struct vfio_group *group = filep->private_data; + +if (cmd ==VFIO_GROUP_GET_FLAGS ) { +u64 flags =0 ; + +mutex_lock (&vfio.lock); +if (__vfio_iommu_viable (group->iommu)) + flags |=VFIO_GROUP_FLAGS_VIABLE ; +mutex_unlock (&vfio.lock); + +if (group->iommu->mm) + flags |=VFIO_GROUP_FLAGS_MM_LOCKED ; + +return put_user (flags, (u64 __user *)arg); + } + +/* Below commands are restricted once the mm is set */ +if (group->iommu->mm && group->iommu->mm != current->mm) +return -EPERM ; + +if (cmd ==VFIO_GROUP_MERGE || cmd ==VFIO_GROUP_UNMERGE ) { +int fd; + +if (get_user (fd, (int __user *)arg)) +return -EFAULT ; +if (fd <0 ) +return -EINVAL ; + +if (cmd ==VFIO_GROUP_MERGE ) +return vfio_group_merge (group, fd); +else +return vfio_group_unmerge (group, fd); + }else if (cmd ==VFIO_GROUP_GET_IOMMU_FD ) { +return vfio_group_get_iommu_fd (group); + }else if (cmd ==VFIO_GROUP_GET_DEVICE_FD ) { +char *buf; +int ret; + + buf =strndup_user ((const char __user *)arg,PAGE_SIZE ); +if (IS_ERR (buf)) +return PTR_ERR (buf); + + ret =vfio_group_get_device_fd (group, buf); +kfree (buf); +return ret; + } + +return -ENOSYS ; +} + +#ifdef CONFIG_COMPAT +static long vfio_group_compat_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ + arg = (unsigned long )compat_ptr (arg); +return vfio_group_unl_ioctl (filep, cmd, arg); +} +#endif /* CONFIG_COMPAT */ + +static const struct file_operations vfio_group_fops = { + .owner =THIS_MODULE , + .open = vfio_group_open, + .release = vfio_group_release, + .unlocked_ioctl = vfio_group_unl_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = vfio_group_compat_ioctl, +#endif +}; + +/* iommu fd release hook */ +int vfio_release_iommu (struct vfio_iommu *iommu) +{ +return vfio_do_release (&iommu->refcnt, iommu); +} + +/* +* VFIO driver API +*/ + +/* Add a new device to the vfio framework with associated vfio driver +* callbacks. This is the entry point for vfio drivers to register devices. */ +int vfio_group_add_dev (struct device *dev,const struct vfio_device_ops *ops) +{ +struct list_head *pos; +struct vfio_group *group =NULL ; +struct vfio_device *device =NULL ; +unsigned int groupid; +int ret =0 ; +bool new_group = false; + +if (!ops) +return -EINVAL ; + +if (iommu_device_group (dev, &groupid)) +return -ENODEV ; + +mutex_lock (&vfio.lock); + +list_for_each (pos, &vfio.group_list) { + group = list_entry(pos, struct vfio_group, group_next); +if (group->groupid == groupid) +break ; + group =NULL ; + } + +if (!group) { +int minor; + +if (unlikely (idr_pre_get (&vfio.idr,GFP_KERNEL ) ==0 )) { + ret = -ENOMEM ; + goto out; + } + + group =kzalloc (sizeof (*group),GFP_KERNEL ); +if (!group) { + ret = -ENOMEM ; + goto out; + } + + group->groupid = groupid; +INIT_LIST_HEAD (&group->device_list); + + ret =idr_get_new (&vfio.idr, group, &minor); +if (ret ==0 && minor >MINORMASK ) { +idr_remove (&vfio.idr, minor);
drivers/vfio/vfio_main.c line 871
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+kfree (group); + ret = -ENOSPC ; + goto out; + } + + group->devt =MKDEV (MAJOR (vfio.devt), minor); +device_create (vfio.class,NULL , group->devt, + group,"%u" , groupid); + + group->bus = dev->bus;
Oh, so that is how the IOMMU iommu_ops get copied! You might want to mention that - I was not sure where the 'handoff' is was done to insert a device so that it can do iommu_ops properly.
Ok, so the time when a device is detected whether it can do IOMMU is when we try to open it - as that is when iommu_domain_alloc is called which can return NULL if the iommu_ops is not set.
So what about devices that don't have an iommu_ops? Say they are using SWIOTLB? (like the AMD-Vi sometimes does if the device is not on its list).
Can we use iommu_present?
Quoted 472 lines
drivers/vfio/vfio_main.c new file +280
-0+list_add (&group->group_next, &vfio.group_list); + new_group = true; + }else { +if (group->bus != dev->bus) { +printk (KERN_WARNING +"Error: IOMMU group ID conflict. Group ID %u " +"on both bus %s and %s\n" , groupid, + group->bus->name, dev->bus->name); + ret = -EFAULT ; + goto out; + } + +list_for_each (pos, &group->device_list) { + device =list_entry (pos, + struct vfio_device, device_next); +if (device->dev == dev) +break ; + device =NULL ; + } + } + +if (!device) { +if (__vfio_group_devs_inuse (group) || + (group->iommu && group->iommu->refcnt)) { +printk (KERN_WARNING +"Adding device %s to group %u while group is already in use!!\n" , +dev_name (dev), group->groupid); +/* XXX How to prevent other drivers from claiming? */ + } + + device =kzalloc (sizeof (*device),GFP_KERNEL ); +if (!device) { +/* If we just created this group, tear it down */ +if (new_group) { +list_del (&group->group_next); +device_destroy (vfio.class, group->devt); +idr_remove (&vfio.idr,MINOR (group->devt)); +kfree (group); + } + ret = -ENOMEM ; + goto out; + } + +list_add (&device->device_next, &group->device_list); + device->dev = dev; + device->ops = ops; + device->iommu = group->iommu;/* NULL if new */ +__vfio_iommu_attach_dev (group->iommu, device); + } +out: +mutex_unlock (&vfio.lock); +return ret; +} +EXPORT_SYMBOL_GPL (vfio_group_add_dev); + +/* Remove a device from the vfio framework */ +void vfio_group_del_dev (struct device *dev) +{ +struct list_head *pos; +struct vfio_group *group =NULL ; +struct vfio_device *device =NULL ; +unsigned int groupid; + +if (iommu_device_group (dev, &groupid)) +return ; + +mutex_lock (&vfio.lock); + +list_for_each (pos, &vfio.group_list) { + group = list_entry(pos, struct vfio_group, group_next); +if (group->groupid == groupid) +break ; + group =NULL ; + } + +if (!group) + goto out; + +list_for_each (pos, &group->device_list) { + device = list_entry(pos, struct vfio_device, device_next); +if (device->dev == dev) +break ; + device =NULL ; + } + +if (!device) + goto out; + +BUG_ON (device->refcnt); + +if (device->attached) +__vfio_iommu_detach_dev (group->iommu, device); + +list_del (&device->device_next); +kfree (device); + +/* If this was the only device in the group, remove the group. +* Note that we intentionally unmerge empty groups here if the +* group fd isn't opened. */ +if (list_empty (&group->device_list) && group->refcnt ==0 ) { +struct vfio_iommu *iommu = group->iommu; + +if (iommu) { +__vfio_group_set_iommu (group,NULL ); +__vfio_try_dissolve_iommu (iommu); + } + +device_destroy (vfio.class, group->devt); +idr_remove (&vfio.idr,MINOR (group->devt)); +list_del (&group->group_next); +kfree (group); + } +out: +mutex_unlock (&vfio.lock); +} +EXPORT_SYMBOL_GPL (vfio_group_del_dev); + +/* When a device is bound to a vfio device driver (ex. vfio-pci), this +* entry point is used to mark the device usable (viable). The vfio +* device driver associates a private device_data struct with the device +* here, which will later be return for vfio_device_fops callbacks. */ +int vfio_bind_dev (struct device *dev,void *device_data) +{ +struct vfio_device *device; +int ret = -EINVAL ; + +BUG_ON (!device_data); + +mutex_lock (&vfio.lock); + + device =__vfio_lookup_dev (dev); + +BUG_ON (!device); + + ret =dev_set_drvdata (dev, device); +if (!ret) + device->device_data = device_data; + +mutex_unlock (&vfio.lock); +return ret; +} +EXPORT_SYMBOL_GPL (vfio_bind_dev); + +/* A device is only removeable if the iommu for the group is not in use. */ +static bool vfio_device_removeable (struct vfio_device *device) +{ +bool ret = true; + +mutex_lock (&vfio.lock); + +if (device->iommu &&__vfio_iommu_inuse (device->iommu)) + ret = false; + +mutex_unlock (&vfio.lock); +return ret; +} + +/* Notify vfio that a device is being unbound from the vfio device driver +* and return the device private device_data pointer. If the group is +* in use, we need to block or take other measures to make it safe for +* the device to be removed from the iommu. */ +void *vfio_unbind_dev (struct device *dev) +{ +struct vfio_device *device =dev_get_drvdata (dev); +void *device_data; + +BUG_ON (!device); + +again: +if (!vfio_device_removeable (device)) { +/* XXX signal for all devices in group to be removed or +* resort to killing the process holding the device fds. +* For now just block waiting for releases to wake us. */ +wait_event (vfio.release_q,vfio_device_removeable (device)); + } + +mutex_lock (&vfio.lock); + +/* Need to re-check that the device is still removeable under lock. */ +if (device->iommu &&__vfio_iommu_inuse (device->iommu)) { +mutex_unlock (&vfio.lock); + goto again; + } + + device_data = device->device_data; + + device->device_data =NULL ; +dev_set_drvdata (dev,NULL ); + +mutex_unlock (&vfio.lock); +return device_data; +} +EXPORT_SYMBOL_GPL (vfio_unbind_dev); + +/* +* Module/class support +*/ +static void vfio_class_release (struct kref *kref) +{ +class_destroy (vfio.class); + vfio.class =NULL ; +} + +static char *vfio_devnode (struct device *dev,mode_t *mode) +{ +return kasprintf (GFP_KERNEL ,"vfio/%s" ,dev_name (dev)); +} + +static int __init vfio_init (void ) +{ +int ret; + +idr_init (&vfio.idr); +mutex_init (&vfio.lock); +INIT_LIST_HEAD (&vfio.group_list); +init_waitqueue_head (&vfio.release_q); + +kref_init (&vfio.kref); + vfio.class =class_create (THIS_MODULE ,"vfio" ); +if (IS_ERR (vfio.class)) { + ret =PTR_ERR (vfio.class); + goto err_class; + } + + vfio.class->devnode = vfio_devnode; + +/* FIXME - how many minors to allocate... all of them! */ + ret =alloc_chrdev_region (&vfio.devt,0 ,MINORMASK ,"vfio" ); +if (ret) + goto err_chrdev; + +cdev_init (&vfio.cdev, &vfio_group_fops); + ret =cdev_add (&vfio.cdev, vfio.devt,MINORMASK ); +if (ret) + goto err_cdev; + +pr_info (DRIVER_DESC " version: " DRIVER_VERSION "\n" ); + +return 0 ; + +err_cdev: +unregister_chrdev_region (vfio.devt,MINORMASK ); +err_chrdev: +kref_put (&vfio.kref, vfio_class_release); +err_class: +return ret; +} + +static void __exit vfio_cleanup(void) +{ +struct list_head *gpos, *gppos; + +list_for_each_safe (gpos, gppos, &vfio.group_list) { +struct vfio_group *group; +struct list_head *dpos, *dppos; + + group = list_entry(gpos, struct vfio_group, group_next); + +list_for_each_safe (dpos, dppos, &group->device_list) { +struct vfio_device *device; + + device =list_entry (dpos, + struct vfio_device, device_next); +vfio_group_del_dev (device->dev); + } + } + +idr_destroy (&vfio.idr); +cdev_del (&vfio.cdev); +unregister_chrdev_region (vfio.devt,MINORMASK ); +kref_put (&vfio.kref, vfio_class_release); +} + +module_init (vfio_init); +module_exit (vfio_cleanup); + +MODULE_VERSION (DRIVER_VERSION ); +MODULE_LICENSE ("GPL v2" ); +MODULE_AUTHOR (DRIVER_AUTHOR ); +MODULE_DESCRIPTION (DRIVER_DESC );drivers/vfio/vfio_private.h new file +34
-0@@ -0,0 +1,34 @@ +/* +* Copyright (C) 2011 Red Hat, Inc. All rights reserved. +* Author: Alex Williamson <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* Derived from original vfio: +* Copyright 2010 Cisco Systems, Inc. All rights reserved. +* Author: Tom Lyon, [email protected] +*/ + +#include <linux/list.h> +#include <linux/mutex.h> + +#ifndef VFIO_PRIVATE_H +#define VFIO_PRIVATE_H + +struct vfio_iommu { +struct iommu_domain *domain; +struct bus_type *bus; +struct mutex dgate; +struct list_head dm_list; +struct mm_struct *mm; +struct list_head group_list; +int refcnt; +bool cache; +}; + +extern int vfio_release_iommu (struct vfio_iommu *iommu); +extern void vfio_iommu_unmapall (struct vfio_iommu *iommu); + +#endif /* VFIO_PRIVATE_H */ include/linux/vfio.h new file +145
-0@@ -0,0 +1,155 @@ +/* +* Copyright 2010 Cisco Systems, Inc. All rights reserved. +* Author: Tom Lyon, [email protected] +* +* This program is free software; you may redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; version 2 of the License. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* SOFTWARE. +* +* Portions derived from drivers/uio/uio.c: +* Copyright(C) 2005, Benedikt Spranger <[email protected]> +* Copyright(C) 2005, Thomas Gleixner <[email protected]> +* Copyright(C) 2006, Hans J. Koch <[email protected]> +* Copyright(C) 2006, Greg Kroah-Hartman <[email protected]> +* +* Portions derived from drivers/uio/uio_pci_generic.c: +* Copyright (C) 2009 Red Hat, Inc. +* Author: Michael S. Tsirkin <[email protected]> +*/ +#include <linux/types.h> + +#ifndef VFIO_H +#define VFIO_H + +#ifdef __KERNEL__ + +struct vfio_device_ops { + bool (*match)(struct device *, char *); + int (*get)(void *); + void (*put)(void *); + ssize_t (*read)(void *, char __user *, + size_t, loff_t *); + ssize_t (*write)(void *, const char __user *, + size_t, loff_t *); + long (*ioctl)(void *, unsigned int, unsigned long); + int (*mmap)(void *, struct vm_area_struct *); +}; + +extern int vfio_group_add_dev(struct device *device, + const struct vfio_device_ops *ops); +extern void vfio_group_del_dev(struct device *device); +extern int vfio_bind_dev(struct device *device, void *device_data); +extern void *vfio_unbind_dev(struct device *device); + +#endif /* __KERNEL__ */ + +/* + * VFIO driver - allow mapping and use of certain devices + * in unprivileged user processes. (If IOMMU is present) + * Especially useful for Virtual Function parts of SR-IOV devices + */ + + +/* Kernel & User level defines for ioctls */ + +#define VFIO_GROUP_GET_FLAGS _IOR(';', 100, __u64) + #define VFIO_GROUP_FLAGS_VIABLE (1 << 0) + #define VFIO_GROUP_FLAGS_MM_LOCKED (1 << 1) +#define VFIO_GROUP_MERGE _IOW(';', 101, int) +#define VFIO_GROUP_UNMERGE _IOW(';', 102, int) +#define VFIO_GROUP_GET_IOMMU_FD _IO(';', 103) +#define VFIO_GROUP_GET_DEVICE_FD _IOW(';', 104, char *) + +/* + * Structure for DMA mapping of user buffers + * vaddr, dmaaddr, and size must all be page aligned + */ +struct vfio_dma_map { + __u64 len; /* length of structure */ + __u64 vaddr; /* process virtual addr */ + __u64 dmaaddr; /* desired and/or returned dma address */ + __u64 size; /* size in bytes */ + __u64 flags; +#define VFIO_DMA_MAP_FLAG_WRITE (1 << 0) /* req writeable DMA mem */ +}; + +#define VFIO_IOMMU_GET_FLAGS _IOR(';', 105, __u64) + /* Does the IOMMU support mapping any IOVA to any virtual address? */ + #define VFIO_IOMMU_FLAGS_MAP_ANY (1 << 0) +#define VFIO_IOMMU_MAP_DMA _IOWR(';', 106, struct vfio_dma_map) +#define VFIO_IOMMU_UNMAP_DMA _IOWR(';', 107, struct vfio_dma_map) + +#define VFIO_DEVICE_GET_FLAGS _IOR(';', 108, __u64) + #define VFIO_DEVICE_FLAGS_PCI (1 << 0) + #define VFIO_DEVICE_FLAGS_DT (1 << 1) + #define VFIO_DEVICE_FLAGS_RESET (1 << 2) +#define VFIO_DEVICE_GET_NUM_REGIONS _IOR(';', 109, int) + +struct vfio_region_info { + __u32 len; /* length of structure */ + __u32 index; /* region number */ + __u64 size; /* size in bytes of region */ + __u64 offset; /* start offset of region */ + __u64 flags; +#define VFIO_REGION_INFO_FLAG_MMAP (1 << 0) +#define VFIO_REGION_INFO_FLAG_RO (1 << 1) +#define VFIO_REGION_INFO_FLAG_PHYS_VALID (1 << 2) + __u64 phys; /* physical address of region */ +}; + +#define VFIO_DEVICE_GET_REGION_INFO _IOWR(';', 110, struct vfio_region_info) + +#define VFIO_DEVICE_GET_NUM_IRQS _IOR(';', 111, int) + +struct vfio_irq_info { + __u32 len; /* length of structure */ + __u32 index; /* IRQ number */ + __u32 count; /* number of individual IRQs */ + __u32 flags; +#define VFIO_IRQ_INFO_FLAG_LEVEL (1 << 0) +}; + +#define VFIO_DEVICE_GET_IRQ_INFO _IOWR(';', 112, struct vfio_irq_info) + +/* Set IRQ eventfds, arg[0] = index, arg[1] = count, arg[2-n] = eventfds */ +#define VFIO_DEVICE_SET_IRQ_EVENTFDS _IOW(';', 113, int) + +/* Unmask IRQ index, arg[0] = index */ +#define VFIO_DEVICE_UNMASK_IRQ _IOW(';', 114, int) + +/* Set unmask eventfd, arg[0] = index, arg[1] = eventfd */ +#define VFIO_DEVICE_SET_UNMASK_IRQ_EVENTFD _IOW(';', 115, int) + +#define VFIO_DEVICE_RESET _IO(';', 116) + +struct vfio_dtpath { + __u32 len; /* length of structure */ + __u32 index; + __u64 flags; +#define VFIO_DTPATH_FLAGS_REGION (1 << 0) +#define VFIO_DTPATH_FLAGS_IRQ (1 << 1) + char *path; +}; +#define VFIO_DEVICE_GET_DTPATH _IOWR(';', 117, struct vfio_dtpath) + +struct vfio_dtindex { + __u32 len; /* length of structure */
include/linux/vfio.h line 155
Quoted 10 lines
include/linux/vfio.h new file +10
-0+__u32 index; +__u32 prop_type; +__u32 prop_index; +__u64 flags; +#define VFIO_DTINDEX_FLAGS_REGION (1 << 0) +#define VFIO_DTINDEX_FLAGS_IRQ (1 << 1) +}; +#define VFIO_DEVICE_GET_DTINDEX _IOWR(';', 118, struct vfio_dtindex) + +#endif /* VFIO_H */
So where is the vfio-pci? Is that a seperate posting?
Thanks Konrad! Comments inline.
Quoted 97 lines · Konrad Rzeszutek Wilk
Quoted 96 lines · Alex Williamson
VFIO provides a secure, IOMMU based interface for user space drivers, including device assignment to virtual machines. This provides the base management of IOMMU groups, devices, and IOMMU objects. See
Documentation/vfio.txtincluded in this patch for user and kernel API description.Note, this implements the new API discussed at KVM Forum 2011, as represented by the drvier version 0.2. It's hoped that this provides a modular enough interface to support PCI and non-PCI userspace drivers across various architectures and IOMMU implementations.
Signed-off-by Alex Williamson <[email protected]>---
Fingers crossed, this is the last RFC for VFIO, but we need the iommu group support before this can go upstream
(http://lkml.indiana.edu/hypermail/linux/kernel/1110.2/02303.html),
hoping this helps push that along.Since the last posting, this version completely modularizes the device backends and better defines the APIs between the core VFIO code and the device backends. I expect that we might also adopt a modular IOMMU interface as
iommu_opslearns about different types of hardware. Also many, many cleanups. Check the complete git history for details:
git://github.com/awilliam/linux-vfio.gitvfio-ng(matching qemu tree:
git://github.com/awilliam/qemu-vfio.git)This version, along with the supporting VFIO PCI backend can be found here:
git://github.com/awilliam/linux-vfio.gitvfio-next-20111103I've held off on implementing a
kernel->usersignaling mechanism for now since the previous netlink version produced too many gag reflexes. It's easy enough to set a bit in the group flags too indicate such support in the future, so I think we can move ahead without it.Appreciate any feedback or suggestions. Thanks,
Alex
Documentation/ioctl/ioctl-number.txt | 1 Documentation/vfio.txt | 304 +++++++++ MAINTAINERS | 8 drivers/Kconfig | 2 drivers/Makefile | 1 drivers/vfio/Kconfig | 8 drivers/vfio/Makefile | 3 drivers/vfio/vfio_iommu.c | 530 ++++++++++++++++ drivers/vfio/vfio_main.c | 1151 ++++++++++++++++++++++++++++++++++ drivers/vfio/vfio_private.h | 34 + include/linux/vfio.h | 155 +++++ 11 files changed, 2197 insertions(+), 0 deletions(-) create mode 100644 Documentation/vfio.txt create mode 100644 drivers/vfio/Kconfig create mode 100644 drivers/vfio/Makefile create mode 100644 drivers/vfio/vfio_iommu.c create mode 100644 drivers/vfio/vfio_main.c create mode 100644 drivers/vfio/vfio_private.h create mode 100644 include/linux/vfio.hDocumentation/ioctl/ioctl-number.txt +1
-0@@ -88,6 +88,7 @@ Code Seq#(hex) Include File Comments and kernel/power/user.c '8' all SNP8023 advanced NIC card <mailto:[email protected]> +';' 64-76 linux/vfio.h '@' 00-0F linux/radeonfb.h conflict! '@' 00-0F drivers/video/aty/aty128fb.c conflict! 'A' 00-1F linux/apm_bios.h conflict!Documentation/vfio.txt new file +12
-0@@ -0,0 +1,304 @@ +VFIO - "Virtual Function I/O"[1] +------------------------------------------------------------------------------- +Many modern system now provide DMA and interrupt remapping facilities +to help ensure I/O devices behave within the boundaries they've been +allotted. This includes x86 hardware with AMD-Vi and Intel VT-d as +well as POWER systems with Partitionable Endpoints (PEs) and even +embedded powerpc systems (technology name unknown). The VFIO driver +is an IOMMU/device agnostic framework for exposing direct device +access to userspace, in a secure, IOMMU protected environment. In +other words, this allows safe, non-privileged, userspace drivers. + +Why do we want that? Virtual machines often make use of direct device
Quoted 8 lines
Documentation/vfio.txt line 17
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+access ("device assignment") when configured for the highest possible +I/O performance. From a device and host perspective, this simply turns +the VM into a userspace driver, with the benefits of significantly +reduced latency, higher bandwidth, and direct use of bare-metal device +drivers[2].Are there any constraints of running a 32-bit userspace with a 64-bit kernel and with 32-bit user space drivers?
Shouldn't be. I'll need to do some testing on that, but it was working on the previous generation of vfio.
Quoted 9 lines
Quoted 9 lines
Documentation/vfio.txt new file +9
-0+ +Some applications, particularly in the high performance computing +field, also benefit from low-overhead, direct device access from +userspace. Examples include network adapters (often non-TCP/IP based) +and compute accelerators. Previous to VFIO, these drivers needed to +go through the full development cycle to become proper upstream driver, +be maintained out of tree, or make use of the UIO framework, which +has no notion of IOMMU protection, limited interrupt support, and +requires root privileges to access things like PCI configuration space.
Quoted 8 lines
Documentation/vfio.txt line 32
Quoted 6 lines
Documentation/vfio.txt new file +6
-0+ +The VFIO driver framework intends to unify these, replacing both the +KVM PCI specific device assignment currently used as well as provide +a more secure, more featureful userspace driver environment than UIO. + +Groups, Devices, IOMMUs, oh my<chuckles> oh my, eh?
Anything for a corny chuckle :)
Quoted 73 lines
Quoted 73 lines
Documentation/vfio.txt new file +73
-0+------------------------------------------------------------------------------- + +A fundamental component of VFIO is the notion of IOMMU groups. IOMMUs +can't always distinguish transactions from each individual device in +the system. Sometimes this is because of the IOMMU design, such as with +PEs, other times it's caused by the I/O topology, for instance a +PCIe-to-PCI bridge masking all devices behind it. We call the sets of +devices created by these restictions IOMMU groups (or just "groups" for +this document). + +The IOMMU cannot distiguish transactions between the individual devices +within the group, therefore the group is the basic unit of ownership for +a userspace process. Because of this, groups are also the primary +interface to both devices and IOMMU domains in VFIO. + +The VFIO representation of groups is created as devices are added into +the framework by a VFIO bus driver. The vfio-pci module is an example +of a bus driver. This module registers devices along with a set of bus +specific callbacks with the VFIO core. These callbacks provide the +interfaces later used for device access. As each new group is created, +as determined by iommu_device_group(), VFIO creates a /dev/vfio/$GROUP +character device. + +In addition to the device enumeration and callbacks, the VFIO bus driver +also provides a traditional device driver and is able to bind to devices +on it's bus. When a device is bound to the bus driver it's available to +VFIO. When all the devices within a group are bound to their bus drivers, +the group becomes "viable" and a user with sufficient access to the VFIO +group chardev can obtain exclusive access to the set of group devices. + +As documented in linux/vfio.h, several ioctls are provided on the +group chardev: + +#define VFIO_GROUP_GET_FLAGS _IOR(';', 100, __u64) + #define VFIO_GROUP_FLAGS_VIABLE (1 << 0) + #define VFIO_GROUP_FLAGS_MM_LOCKED (1 << 1) +#define VFIO_GROUP_MERGE _IOW(';', 101, int) +#define VFIO_GROUP_UNMERGE _IOW(';', 102, int) +#define VFIO_GROUP_GET_IOMMU_FD _IO(';', 103) +#define VFIO_GROUP_GET_DEVICE_FD _IOW(';', 104, char *) + +The last two ioctls return new file descriptors for accessing +individual devices within the group and programming the IOMMU. Each of +these new file descriptors provide their own set of file interfaces. +These ioctls will fail if any of the devices within the group are not +bound to their VFIO bus driver. Additionally, when either of these +interfaces are used, the group is then bound to the struct_mm of the +caller. The GET_FLAGS ioctl can be used to view the state of the group. + +When either the GET_IOMMU_FD or GET_DEVICE_FD ioctls are invoked, a +new IOMMU domain is created and all of the devices in the group are +attached to it. This is the only way to ensure full IOMMU isolation +of the group, but potentially wastes resources and cycles if the user +intends to manage multiple groups with the same set of IOMMU mappings. +VFIO therefore provides a group MERGE and UNMERGE interface, which +allows multiple groups to share an IOMMU domain. Not all IOMMUs allow +arbitrary groups to be merged, so the user should assume merging is +opportunistic. A new group, with no open device or IOMMU file +descriptors, can be merged into an existing, in-use, group using the +MERGE ioctl. A merged group can be unmerged using the UNMERGE ioctl +once all of the device file descriptors for the group being merged +"out" are closed. + +When groups are merged, the GET_IOMMU_FD and GET_DEVICE_FD ioctls are +essentially fungible between group file descriptors (ie. if device A +is in group X, and X is merged with Y, a file descriptor for A can be +retrieved using GET_DEVICE_FD on Y. Likewise, GET_IOMMU_FD returns a +file descriptor referencing the same internal IOMMU object from either +X or Y). Merged groups can be dissolved either explictly with UNMERGE +or automatically when ALL file descriptors for the merged group are +closed (all IOMMUs, all devices, all groups). + +The IOMMU file descriptor provides this set of ioctls:
Quoted 8 lines
Documentation/vfio.txt line 110
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+ +#define VFIO_IOMMU_GET_FLAGS _IOR(';', 105, __u64) + #define VFIO_IOMMU_FLAGS_MAP_ANY (1 << 0) +#define VFIO_IOMMU_MAP_DMA _IOWR(';', 106, struct vfio_dma_map) +#define VFIO_IOMMU_UNMAP_DMA _IOWR(';', 107, struct vfio_dma_map)Coherency support is not going to be addressed right? What about sync? Say you need to sync CPU to Device address?
Do we need to expose that to userspace or should the underlying iommu_ops take care of it?
Quoted 5 lines
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+ +The GET_FLAGS ioctl returns basic information about the IOMMU domain. +We currently only support IOMMU domains that are able to map any +virtual address to any IOVA. This is indicated by the MAP_ANY flag. +
Quoted 8 lines
Documentation/vfio.txt line 120
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+The (UN)MAP_DMA commands make use of struct vfio_dma_map for mapping +and unmapping IOVAs to process virtual addresses: + +struct vfio_dma_map { + __u64 len; /* length of structure */What is the purpose of the 'len' field? Is it to guard against future version changes?
Yes, David Gibson suggested we include flags & len for all data structures to help future proof them.
Quoted 11 lines
Documentation/vfio.txt line 129
Quoted 9 lines
Documentation/vfio.txt new file +9
-0+ __u64 vaddr; /* process virtual addr */ + __u64 dmaaddr; /* desired and/or returned dma address */ + __u64 size; /* size in bytes */ + __u64 flags; +#define VFIO_DMA_MAP_FLAG_WRITE (1 << 0) /* req writeable DMA mem */ +}; + +Current users of VFIO use relatively static DMA mappings, not requiring +high frequency turnover. As new users are added, it's expected that theIs there a limit to how many DMA mappings can be created?
Not that I'm aware of for the current AMD-Vi/VT-d implementations. I suppose iommu_ops would return -ENOSPC if it hit a limit. I added the VFIO_IOMMU_FLAGS_MAP_ANY flag above to try to identify that kind of restriction.
Quoted 10 lines
Documentation/vfio.txt line 137
Quoted 8 lines
Documentation/vfio.txt new file +8
-0+IOMMU file descriptor will evolve to support new mapping interfaces, this +will be reflected in the flags and may present new ioctls and file +interfaces. + +The device GET_FLAGS ioctl is intended to return basic device type and +indicate support for optional capabilities. Flags currently include whether +the device is PCI or described by Device Tree, and whether the RESET ioctl +is supported:And reset in terms of PCIe spec is the FLR?
Yes, just a pass through to pci_reset_function() for the pci vfio bus driver.
Quoted 9 lines
Documentation/vfio.txt line 144
Quoted 7 lines
Documentation/vfio.txt new file +7
-0+ +#define VFIO_DEVICE_GET_FLAGS _IOR(';', 108, __u64) + #define VFIO_DEVICE_FLAGS_PCI (1 << 0) + #define VFIO_DEVICE_FLAGS_DT (1 << 1) + #define VFIO_DEVICE_FLAGS_RESET (1 << 2) + +The MMIO and IOP resources used by a device are described by regions.IOP?
I/O port, I'll spell it out.
Quoted 5 lines
Documentation/vfio.txt line 147
Quoted 3 lines
Documentation/vfio.txt new file +3
-0+The GET_NUM_REGIONS ioctl tells us how many regions the device supports: + +#define VFIO_DEVICE_GET_NUM_REGIONS _IOR(';', 109, int)Don't want
__u32?
It could be, not sure if it buys us anything maybe even restricts us. We likely don't need 2^32 regions (famous last words?), so we could later define <0 to something?
Quoted 9 lines
Documentation/vfio.txt line 151
Quoted 5 lines
Documentation/vfio.txt new file +4
-0+ +Regions are described by a struct vfio_region_info, which is retrieved by +using the GET_REGION_INFO ioctl with vfio_region_info.index field set to +the desired region (0 based index). Note that devices may implement zero+sized regions (vfio-pci does this to provide a 1:1 BAR to region index
Documentation/vfio.txt line 153
Quoted 1 line
Documentation/vfio.txt new file +1
-0+mapping).Huh?
PCI has the following static mapping:
enum {
VFIO_PCI_BAR0_REGION_INDEX,
VFIO_PCI_BAR1_REGION_INDEX,
VFIO_PCI_BAR2_REGION_INDEX,
VFIO_PCI_BAR3_REGION_INDEX,
VFIO_PCI_BAR4_REGION_INDEX,
VFIO_PCI_BAR5_REGION_INDEX,
VFIO_PCI_ROM_REGION_INDEX,
VFIO_PCI_CONFIG_REGION_INDEX,
VFIO_PCI_NUM_REGIONS
};
So 8 regions are always reported regardless of whether the device implements all the BARs and the ROM. Then we have a fixed bar:index mapping so we don't have to create a region_info field to describe the bar number for the index.
Quoted 4 lines
Quoted 4 lines
Documentation/vfio.txt new file +4
-0+ +struct vfio_region_info { + __u32 len; /* length of structure */ + __u32 index; /* region number */
Quoted 8 lines
Documentation/vfio.txt line 163
Quoted 6 lines
Documentation/vfio.txt new file +6
-0+ __u64 size; /* size in bytes of region */ + __u64 offset; /* start offset of region */ + __u64 flags; +#define VFIO_REGION_INFO_FLAG_MMAP (1 << 0) +#define VFIO_REGION_INFO_FLAG_RO (1 << 1) +#define VFIO_REGION_INFO_FLAG_PHYS_VALID (1 << 2)What is
FLAG_MMAP? Does it mean: 1) it can be mmaped, or 2) it is mmaped?
Supports mmap
Quoted 3 lines
FLAG_ROis pretty obvious - presumarily this is for firmware regions and such. AndPHYS_VALIDis if the region is disabled for some reasons? If so would the nameFLAG_DISABLEDbe better?
No, POWER guys have some need to report the host physical address of the region, so the flag indicates whether the below field is present and valid. I'll clarify these in the docs.
Quoted 17 lines
Quoted 5 lines
Documentation/vfio.txt new file +4
-0+ __u64 phys; /* physical address of region */ +}; + +#define VFIO_DEVICE_GET_REGION_INFO _IOWR(';', 110, struct vfio_region_info)Documentation/vfio.txt line 177
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+ +The offset indicates the offset into the device file descriptor which +accesses the given range (for read/write/mmap/seek). Flags indicate the +available access types and validity of optional fields. For instance +the phys field may only be valid for certain devices types. + +Interrupts are described using a similar interface. GET_NUM_IRQS +reports the number or IRQ indexes for the device. + +#define VFIO_DEVICE_GET_NUM_IRQS _IOR(';', 111, int)
_u32?
Same as above, but I don't have a strong preference.
Quoted 5 lines
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+ +struct vfio_irq_info { + __u32 len; /* length of structure */ + __u32 index; /* IRQ number */ + __u32 count; /* number of individual IRQs */
Quoted 8 lines
Documentation/vfio.txt line 188
Quoted 6 lines
Documentation/vfio.txt new file +6
-0+ __u64 flags; +#define VFIO_IRQ_INFO_FLAG_LEVEL (1 << 0) +}; + +Again, zero count entries are allowed (vfio-pci uses a static interrupt +type to index mapping).I am not really sure what that means.
This is so PCI can expose:
enum {
VFIO_PCI_INTX_IRQ_INDEX,
VFIO_PCI_MSI_IRQ_INDEX,
VFIO_PCI_MSIX_IRQ_INDEX,
VFIO_PCI_NUM_IRQS
};
So like regions it always exposes 3 IRQ indexes where count=0 if the device doesn't actually support that type of interrupt. I just want to spell out that bus drivers have this kind of flexibility.
Quoted 8 lines
Quoted 8 lines
Documentation/vfio.txt new file +8
-0+ +Information about each index can be retrieved using the GET_IRQ_INFO +ioctl, used much like GET_REGION_INFO. + +#define VFIO_DEVICE_GET_IRQ_INFO _IOWR(';', 112, struct vfio_irq_info) + +Individual indexes can describe single or sets of IRQs. This provides the +flexibility to describe PCI INTx, MSI, and MSI-X using a single interface.
Quoted 8 lines
Documentation/vfio.txt line 202
Quoted 6 lines
Documentation/vfio.txt new file +6
-0+ +All VFIO interrupts are signaled to userspace via eventfds. Integer arrays, +as shown below, are used to pass the IRQ info index, the number of eventfds, +and each eventfd to be signaled. Using a count of 0 disables the interrupt. + +/* Set IRQ eventfds, arg[0] = index, arg[1] = count, arg[2-n] = eventfds */Are eventfds u64 or u32?
int, they're just file descriptors
Quoted 6 lines
Why not just define a structure?
struct vfio_irq_eventfds { __u32 index; __u32 count; __u64 eventfds[0] };
We could do that if preferred. Hmm, are we then going to need size/flags?
Quoted 1 line
How do you get an eventfd to feed in here?
eventfd(2), in qemu event_notifier_init() -> event_notifier_get_fd()
Quoted 3 lines
Documentation/vfio.txt line 202
Quoted 1 line
Documentation/vfio.txt new file +1
-0+#define VFIO_DEVICE_SET_IRQ_EVENTFDS _IOW(';', 113, int)u32?
Not here, it's an fd, so should be an int.
Quoted 9 lines
Documentation/vfio.txt line 209
Quoted 6 lines
Documentation/vfio.txt new file +6
-0+ +When a level triggered interrupt is signaled, the interrupt is masked +on the host. This prevents an unresponsive userspace driver from +continuing to interrupt the host system. After servicing the interrupt, +UNMASK_IRQ is used to allow the interrupt to retrigger. Note that level +triggered interrupts implicitly have a count of 1 per index.So they are enabled automatically? Meaning you don't even hav to do
SET_IRQ_EVENTFDSb/c the count is set to 1?
I suppose that should be "no more than 1 per index" (ie. PCI would report a count of 0 for VFIO_PCI_INTX_IRQ_INDEX if the device doesn't support INTx). I think you might be confusing VFIO_DEVICE_GET_IRQ_INFO which tells how many are available with VFIO_DEVICE_SET_IRQ_EVENTFDS which does the enabling/disabling. All interrupts are disabled by default because userspace needs to give us a way to signal them via eventfds. It will be device dependent whether multiple index can be enabled simultaneously. Hmm, is that another flag on the irq_info struct or do we expect drivers to implicitly have that kind of knowledge?
Quoted 6 lines
Documentation/vfio.txt line 212
Quoted 3 lines
Documentation/vfio.txt new file +3
-0+ +/* Unmask IRQ index, arg[0] = index */ +#define VFIO_DEVICE_UNMASK_IRQ _IOW(';', 114, int)So this is for MSI as well? So if I've an index = 1, with count = 4, and doing unmaks IRQ will chip enable all the MSI event at once?
No, this is only for re-enabling level triggered interrupts as discussed above. Edge triggered interrupts like MSI don't need an unmask... we may want to do something to accelerate the MSI-X table access for masking specific interrupts, but I figured that would need to be PCI aware since those are PCI features, and would therefore be some future extension of the PCI bus driver and exposed via VFIO_DEVICE_GET_FLAGS.
Quoted 2 lines
I guess there is not much point in enabling/disabling selective MSI IRQs..
Some older OSes are said to make extensive use of masking for MSI, so we probably want this at some point. I'm assuming future PCI extension for now.
Quoted 4 lines
Documentation/vfio.txt line 213
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+ +Level triggered interrupts can also be unmasked using an irqfd. Useirqfd or eventfd?
irqfd is an eventfd in reverse. eventfd = kernel signals userspace via an fd, irqfd = userspace signals kernel via an fd.
Quoted 5 lines
Documentation/vfio.txt line 215
Quoted 1 line
Documentation/vfio.txt new file +1
-0+SET_UNMASK_IRQ_EVENTFD to set the file descriptor for this.So only level triggered? Hmm, how do I know whether the device is level or edge? Or is that edge (MSI) can also be unmaked using the eventfs
Yes, only for level. Isn't a device going to know what type of interrupt it uses? MSI masking is PCI specific, not handled by this.
Quoted 4 lines
Quoted 4 lines
Documentation/vfio.txt new file +4
-0+ +/* Set unmask eventfd, arg[0] = index, arg[1] = eventfd */ +#define VFIO_DEVICE_SET_UNMASK_IRQ_EVENTFD _IOW(';', 115, int) +
Quoted 8 lines
Documentation/vfio.txt line 222
Quoted 3 lines
Documentation/vfio.txt new file +3
-0+When supported, as indicated by the device flags, reset the device. + +#define VFIO_DEVICE_RESET _IO(';', 116)Does it disable the 'count'? Err, does it disable the IRQ on the device after this and one should call
VFIO_DEVICE_SET_IRQ_EVENTFDSto set new eventfds? Or does it re-use the eventfds and the device is enabled after this?
It doesn't affect the interrupt programming. Should it?
Quoted 5 lines
Documentation/vfio.txt line 223
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+ +Device tree devices also invlude ioctls for further defining theinclude
Quoted 7 lines
Documentation/vfio.txt line 229
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+device tree properties of the device: + +struct vfio_dtpath { + __u32 len; /* length of structure */ + __u32 index;0 based I presume?
Everything else is, I would assume so/
Quoted 5 lines
Documentation/vfio.txt line 230
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+ __u64 flags; +#define VFIO_DTPATH_FLAGS_REGION (1 << 0)What is region in this context?? Or would this make much more sense if I knew what Device Tree actually is.
Powerpc guys, any comments? This was their suggestion. These are effectively the first device specific extension, available when VFIO_DEVICE_FLAGS_DT is set.
Quoted 5 lines
Documentation/vfio.txt line 232
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+#define VFIO_DTPATH_FLAGS_IRQ (1 << 1) + char *path;Ah, now I see why you want 'len' here.. But I am still at loss why you want that with the other structures.
Attempt to future proof and validate input.
Quoted 9 lines
Documentation/vfio.txt line 240
Quoted 7 lines
Documentation/vfio.txt new file +7
-0+}; +#define VFIO_DEVICE_GET_DTPATH _IOWR(';', 117, struct vfio_dtpath) + +struct vfio_dtindex { + __u32 len; /* length of structure */ + __u32 index; + __u32 prop_type;Is that an enum type? Is this definied somewhere?
Quoted 3 lines
Documentation/vfio.txt line 240
Quoted 1 line
Documentation/vfio.txt new file +1
-0+ __u32 prop_index;What is the purpose of this field?
Need input from powerpc folks here
Quoted 8 lines
Quoted 8 lines
Documentation/vfio.txt new file +8
-0+ __u64 flags; +#define VFIO_DTINDEX_FLAGS_REGION (1 << 0) +#define VFIO_DTINDEX_FLAGS_IRQ (1 << 1) +}; +#define VFIO_DEVICE_GET_DTINDEX _IOWR(';', 118, struct vfio_dtindex) + + +VFIO bus driver API
Quoted 8 lines
Documentation/vfio.txt line 255
Quoted 6 lines
Documentation/vfio.txt new file +6
-0+------------------------------------------------------------------------------- + +Bus drivers, such as PCI, have three jobs: + 1) Add/remove devices from vfio + 2) Provide vfio_device_ops for device access + 3) Device binding and unbindingsuspend/resume?
In the previous version of vfio, the vfio core signaled suspend/resume to userspace via netlink, effectively putting userspace on the pm notifier chain. I was intending to do the same here.
Quoted 4 lines
Quoted 4 lines
Documentation/vfio.txt new file +4
-0+ +When initialized, the bus driver should enumerate the devices on it's +bus and call vfio_group_add_dev() for each device. If the bus supports +hotplug, notifiers should be enabled to track devices being added and
Quoted 8 lines
Documentation/vfio.txt line 264
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+removed. vfio_group_del_dev() removes a previously added device from +vfio. + +Adding a device registers a vfio_device_ops function pointer structure +for the device:Huh? So this gets created for _every_ 'struct device' that is added the VFIO bus? Is this structure exposed? Or is this an internal one?
Every device added creates a struct vfio_device and if necessary a struct vfio_group. These are internal, just for managing groups and devices.
Quoted 10 lines
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+ +struct vfio_device_ops { + bool (*match)(struct device *, char *); + int (*get)(void *); + void (*put)(void *); + ssize_t (*read)(void *, char __user *, + size_t, loff_t *); + ssize_t (*write)(void *, const char __user *, + size_t, loff_t *); + long (*ioctl)(void *, unsigned int, unsigned long);
Quoted 8 lines
Documentation/vfio.txt line 279
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+ int (*mmap)(void *, struct vm_area_struct *); +}; + +When a device is bound to the bus driver, the bus driver indicates this +to vfio using the vfio_bind_dev() interface. The device_data parameterMight want to paste the function decleration for it.. b/c I am not sure where the '
device_data' parameter is on the argument list.
Ok
Quoted 5 lines
Documentation/vfio.txt line 282
Quoted 3 lines
Documentation/vfio.txt new file +3
-0+is a pointer to an opaque data structure for use only by the bus driver. +The get, put, read, write, ioctl, and mmap vfio_device_ops all pass +this data structure back to the bus driver. When a device is unboundOh, so it is on the 'void *'.
Right
Quoted 9 lines
Documentation/vfio.txt line 284
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+from the bus driver, the vfio_unbind_dev() interface signals this to +vfio. This function returns the pointer to the device_data structureThat function
Documentation/vfio.txt line 285
Quoted 1 line
Documentation/vfio.txt new file +1
-0+registered for the device.I am not really sure what this section purpose is? Could this be part of the header file or the code? It does not look to be part of the ioctl API?
We've passed into the "VFIO bus driver API" section of the document, to explain the interaction between vfio-core and vfio bus drivers.
Quoted 27 lines
Quoted 27 lines
Documentation/vfio.txt new file +19
-0+ +As noted previously, a group contains one or more devices, so +GROUP_GET_DEVICE_FD needs to identify the specific device being requested. +The vfio_device_ops.match callback is used to allow bus drivers to determine +the match. For drivers like vfio-pci, it's a simple match to dev_name(), +which is unique in the system due to the PCI bus topology, other bus drivers +may need to include parent devices to create a unique match, so this is +left as a bus driver interface. + +------------------------------------------------------------------------------- + +[1] VFIO was originally an acronym for "Virtual Function I/O" in it's +initial implementation by Tom Lyon while as Cisco. We've since outgrown +the acronym, but it's catchy. + +[2] As always there are trade-offs to virtual machine device +assignment that are beyond the scope of VFIO. It's expected that +future IOMMU technologies will reduce some, but maybe not all, of +these trade-offs.MAINTAINERS +0
-0@@ -7106,6 +7106,14 @@ S: Maintained F: Documentation/filesystems/vfat.txt F: fs/fat/
Quoted 5 lines
MAINTAINERS S: Maintained · line 7111
Quoted 3 lines
MAINTAINERS +3
-0+VFIO DRIVER +M: Alex Williamson <[email protected]> +L: [email protected]No vfio mailing list? Or a vfio-mailing list?
IIRC, Avi had agreed that we could use kvm for now. I don't know that vfio will warrant it's own list. If it picks up, sure, we can move it.
Quoted 91 lines
Quoted 91 lines
MAINTAINERS +5
-0+S: Maintained +F: Documentation/vfio.txt +F: drivers/vfio/ +F: include/linux/vfio.h + VIDEOBUF2 FRAMEWORK M: Pawel Osciak <[email protected]> M: Marek Szyprowski <[email protected]>drivers/Kconfig +2
-0@@ -112,6 +112,8 @@ source "drivers/auxdisplay/Kconfig" source "drivers/uio/Kconfig" +source "drivers/vfio/Kconfig" + source "drivers/vlynq/Kconfig" source "drivers/virtio/Kconfig"drivers/Makefile +1
-0@@ -58,6 +58,7 @@ obj-$(CONFIG_ATM) += atm/ obj-$(CONFIG_FUSION) += message/ obj-y += firewire/ obj-$(CONFIG_UIO) += uio/ +obj-$(CONFIG_VFIO) += vfio/ obj-y += cdrom/ obj-y += auxdisplay/ obj-$(CONFIG_PCCARD) += pcmcia/drivers/vfio/Kconfig new file +8
-0@@ -0,0 +1,8 @@ +menuconfig VFIO + tristate "VFIO Non-Privileged userspace driver framework" + depends on IOMMU_API + help + VFIO provides a framework for secure userspace device drivers. + See Documentation/vfio.txt for more details. + + If you don't know what to do here, say N.drivers/vfio/Makefile new file +3
-0@@ -0,0 +1,3 @@ +vfio-y := vfio_main.o vfio_iommu.o + +obj-$(CONFIG_VFIO) := vfio.odrivers/vfio/vfio_iommu.c new file +29
-0@@ -0,0 +1,530 @@ +/* +* VFIO: IOMMU DMA mapping support +* +* Copyright (C) 2011 Red Hat, Inc. All rights reserved. +* Author: Alex Williamson <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* Derived from original vfio: +* Copyright 2010 Cisco Systems, Inc. All rights reserved. +* Author: Tom Lyon, [email protected] +*/ + +#include <linux/compat.h> +#include <linux/device.h> +#include <linux/fs.h> +#include <linux/iommu.h> +#include <linux/module.h> +#include <linux/mm.h> +#include <linux/sched.h> +#include <linux/slab.h> +#include <linux/uaccess.h> +#include <linux/vfio.h> +#include <linux/workqueue.h> + +#include "vfio_private.h" +
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 35
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+struct dma_map_page { +struct list_head list; +dma_addr_t daddr; +unsigned long vaddr; +int npage; +int rdwr;rdwr? Is this a flag thing? Could it be made in an enum?
Or maybe better would just be a bool.
Quoted 69 lines
Quoted 56 lines
drivers/vfio/vfio_iommu.c new file +56
-0+}; + +/* +* This code handles mapping and unmapping of user data buffers +* into DMA'ble space using the IOMMU +*/ + +#define NPAGE_TO_SIZE (npage) ((size_t)(npage) << PAGE_SHIFT) + +struct vwork { +struct mm_struct *mm; +int npage; +struct work_struct work; +}; + +/* delayed decrement for locked_vm */ +static void vfio_lock_acct_bg (struct work_struct *work) +{ + struct vwork *vwork = container_of(work, struct vwork, work); +struct mm_struct *mm; + + mm = vwork->mm; +down_write (&mm->mmap_sem); + mm->locked_vm += vwork->npage; +up_write (&mm->mmap_sem); +mmput (mm);/* unref mm */ +kfree (vwork); +} + +static void vfio_lock_acct (int npage) +{ +struct vwork *vwork; +struct mm_struct *mm; + +if (!current->mm) { +/* process exited */ +return ; + } +if (down_write_trylock (¤t->mm->mmap_sem)) { + current->mm->locked_vm += npage; +up_write (¤t->mm->mmap_sem); +return ; + } +/* +* Couldn't get mmap_sem lock, so must setup to decrement +* mm->locked_vm later. If locked_vm were atomic, we wouldn't +* need this silliness +*/ + vwork =kmalloc (sizeof (struct vwork ),GFP_KERNEL ); +if (!vwork) +return ; + mm =get_task_mm (current);/* take ref mm */ +if (!mm) { +kfree (vwork); +return ; + }drivers/vfio/vfio_iommu.c line 101
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+INIT_WORK (&vwork->work, vfio_lock_acct_bg); + vwork->mm = mm; + vwork->npage = npage; +schedule_work (&vwork->work); +} + +/* Some mappings aren't backed by a struct page, for example an mmap'd +* MMIO range for our own or another device. These use a different +* pfn conversion and shouldn't be tracked as locked pages. */ +static int is_invalid_reserved_pfn (unsigned long pfn)static bool
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 107
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+{ +if (pfn_valid (pfn)) { +int reserved; +struct page *tail =pfn_to_page (pfn); +struct page *head =compound_trans_head (tail); + reserved =PageReserved (head);bool reserved = PageReserved(head);
Agree on both
Quoted 28 lines
Quoted 28 lines
drivers/vfio/vfio_iommu.c new file +28
-0+if (head != tail) { +/* "head" is not a dangling pointer +* (compound_trans_head takes care of that) +* but the hugepage may have been split +* from under us (and we may not hold a +* reference count on the head page so it can +* be reused before we run PageReferenced), so +* we've to check PageTail before returning +* what we just read. +*/ +smp_rmb (); +if (PageTail (tail)) +return reserved; + } +return PageReserved (tail); + } + +return true; +} + +static int put_pfn (unsigned long pfn,int rdwr) +{ +if (!is_invalid_reserved_pfn (pfn)) { +struct page *page =pfn_to_page (pfn); +if (rdwr) +SetPageDirty (page); +put_page (page); +return 1 ;
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 141
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ } +return 0 ; +} + +/* Unmap DMA region */ +/* dgate must be held */ dgate?
DMA gate, the mutex for iommu operations. This a carry over from old vfio. As there's only one mutex on the struct vfio_iommu, I can just rename that to "lock".
Quoted 5 lines
Quoted 5 lines
drivers/vfio/vfio_iommu.c new file +5
-0+static int __vfio_dma_unmap (struct vfio_iommu *iommu,unsigned long iova, +int npage,int rdwr) +{ +int i, unlocked =0 ; +
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 152
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+for (i =0 ; i < npage; i++, iova +=PAGE_SIZE ) { +unsigned long pfn; + + pfn =iommu_iova_to_phys (iommu->domain, iova) >>PAGE_SHIFT ; +if (pfn) { +iommu_unmap (iommu->domain, iova,0 );What is the '0' for? Perhaps a comment: /* We only do zero order */
yep. We'll need to improve this at some point to take advantage of large iommu pages, but it shouldn't affect the API. I'll add comment.
Quoted 13 lines
Quoted 13 lines
drivers/vfio/vfio_iommu.c new file +13
-0+ unlocked +=put_pfn (pfn, rdwr); + } + } +return unlocked; +} + +static void vfio_dma_unmap (struct vfio_iommu *iommu,unsigned long iova, +unsigned long npage,int rdwr) +{ +int unlocked; + + unlocked =__vfio_dma_unmap (iommu, iova, npage, rdwr); +vfio_lock_acct (-unlocked);
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 171
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+} + +/* Unmap ALL DMA regions */ +void vfio_iommu_unmapall (struct vfio_iommu *iommu) +{ +struct list_head *pos, *pos2;pos2 should probably be just called 'tmp'
ok
Quoted 5 lines
drivers/vfio/vfio_iommu.c line 172
Quoted 1 line
drivers/vfio/vfio_iommu.c new file +1
-0+struct dma_map_page *mlp;What does 'mlp' stand for?
mlp ->
dma_page?
Carry over from original code, I can guess, but not sure what Tom was originally thinking. I think everyone has asked so far, so I'll make a pass at coming up with a names that I can explain.
Quoted 11 lines
drivers/vfio/vfio_iommu.c line 177
Quoted 5 lines
drivers/vfio/vfio_iommu.c new file +5
-0+ +mutex_lock (&iommu->dgate); +list_for_each_safe (pos, pos2, &iommu->dm_list) { + mlp = list_entry(pos, struct dma_map_page, list); +vfio_dma_unmap (iommu, mlp->daddr, mlp->npage, mlp->rdwr);Uh, so if it did not get
put_page()we would try to still delete it? Couldn't that lead to corruption as the 'mlp' is returned to the poll?Ah wait, the
put_pageis on the DMA page, so it is OK to delete the tracking structure. It will be just a leaked page.
Assume you're referencing this chunk:
vfio_dma_unmap
__vfio_dma_unmap
...
pfn = iommu_iova_to_phys(iommu->domain, iova) >> PAGE_SHIFT;
if (pfn) {
iommu_unmap(iommu->domain, iova, 0);
unlocked += put_pfn(pfn, rdwr);
}So we skip things that aren't mapped in the iommu, but anything not mapped should have already been put (failed vfio_dma_map). If it is mapped, we put it if we originally got it via get_user_pages_fast. unlocked would only not get incremented here if it was an mmap'd page (such as the mmap of an mmio space of another vfio device), via the code in vaddr_get_pfn (stolen from KVM).
Quoted 19 lines
Quoted 19 lines
drivers/vfio/vfio_iommu.c new file +19
-0+list_del (&mlp->list); +kfree (mlp); + } +mutex_unlock (&iommu->dgate); +} + +static int vaddr_get_pfn (unsigned long vaddr,int rdwr,unsigned long *pfn) +{ +struct page *page[1 ]; +struct vm_area_struct *vma; +int ret = -EFAULT ; + +if (get_user_pages_fast (vaddr,1 , rdwr, page) ==1 ) { + *pfn =page_to_pfn (page[0 ]); +return 0 ; + } + +down_read (¤t->mm->mmap_sem); +
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 202
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ vma =find_vma_intersection (current->mm, vaddr, vaddr +1 ); + +if (vma && vma->vm_flags &VM_PFNMAP ) { + *pfn = ((vaddr - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff; +if (is_invalid_reserved_pfn (*pfn)) + ret =0 ;Did you mean to break here?
We're in an if block, not a loop.
Quoted 10 lines
Quoted 10 lines
drivers/vfio/vfio_iommu.c new file +10
-0+ } + +up_read (¤t->mm->mmap_sem); + +return ret; +} + +/* Map DMA region */ +/* dgate must be held */ +static int vfio_dma_map(struct vfio_iommu *iommu, unsigned long iova,
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 218
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ unsigned long vaddr, int npage, int rdwr) +{ + unsigned long start = iova; +int i, ret, locked =0 , prot =IOMMU_READ ; + +/* Verify pages are not already mapped */ I think a 'that' is missing above.
Ok.
Quoted 20 lines
Quoted 20 lines
drivers/vfio/vfio_iommu.c new file +20
-0+for (i =0 ; i < npage; i++, iova +=PAGE_SIZE ) +if (iommu_iova_to_phys (iommu->domain, iova)) +return -EBUSY ; + + iova = start; + +if (rdwr) + prot |=IOMMU_WRITE ; +if (iommu->cache) + prot |=IOMMU_CACHE ; + +for (i =0 ; i < npage; i++, iova +=PAGE_SIZE , vaddr +=PAGE_SIZE ) { +unsigned long pfn =0 ; + + ret =vaddr_get_pfn (vaddr, rdwr, &pfn); +if (ret) { +__vfio_dma_unmap (iommu, start, i, rdwr); +return ret; + } +
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 244
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+/* Only add actual locked pages to accounting */ +if (!is_invalid_reserved_pfn (pfn)) + locked++; + + ret =iommu_map (iommu->domain, iova, + (phys_addr_t )pfn <<PAGE_SHIFT ,0 , prot);Put a comment by the 0 saying /* order 0 pages only! */
Yep
Quoted 6 lines
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+if (ret) { +/* Back out mappings on error */ +put_pfn (pfn, rdwr); +__vfio_dma_unmap (iommu, start, i, rdwr); +return ret; + }
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 256
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ } +vfio_lock_acct (locked); +return 0 ; +} + +static inline int ranges_overlap(unsigned long start1, size_t size1,Perhaps a bool?
Sure
Quoted 222 lines
Quoted 222 lines
drivers/vfio/vfio_iommu.c new file +222
-0+ unsigned long start2, size_t size2) +{ +return !(start1 + size1 <= start2 || start2 + size2 <= start1); +} + +static struct dma_map_page *vfio_find_dma (struct vfio_iommu *iommu, +dma_addr_t start,size_t size) +{ +struct list_head *pos; +struct dma_map_page *mlp; + +list_for_each (pos, &iommu->dm_list) { + mlp = list_entry(pos, struct dma_map_page, list); +if (ranges_overlap (mlp->daddr,NPAGE_TO_SIZE (mlp->npage), + start, size)) +return mlp; + } +return NULL ; +} + +int vfio_remove_dma_overlap (struct vfio_iommu *iommu,dma_addr_t start, +size_t size,struct dma_map_page *mlp) +{ +struct dma_map_page *split; +int npage_lo, npage_hi; + +/* Existing dma region is completely covered, unmap all */ +if (start <= mlp->daddr && + start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +vfio_dma_unmap (iommu, mlp->daddr, mlp->npage, mlp->rdwr); +list_del (&mlp->list); + npage_lo = mlp->npage; +kfree (mlp); +return npage_lo; + } + +/* Overlap low address of existing range */ +if (start <= mlp->daddr) { +size_t overlap; + + overlap = start + size - mlp->daddr; + npage_lo = overlap >>PAGE_SHIFT ; + npage_hi = mlp->npage - npage_lo; + +vfio_dma_unmap (iommu, mlp->daddr, npage_lo, mlp->rdwr); + mlp->daddr += overlap; + mlp->vaddr += overlap; + mlp->npage -= npage_lo; +return npage_lo; + } + +/* Overlap high address of existing range */ +if (start + size >= mlp->daddr +NPAGE_TO_SIZE (mlp->npage)) { +size_t overlap; + + overlap = mlp->daddr +NPAGE_TO_SIZE (mlp->npage) - start; + npage_hi = overlap >>PAGE_SHIFT ; + npage_lo = mlp->npage - npage_hi; + +vfio_dma_unmap (iommu, start, npage_hi, mlp->rdwr); + mlp->npage -= npage_hi; +return npage_hi; + } + +/* Split existing */ + npage_lo = (start - mlp->daddr) >>PAGE_SHIFT ; + npage_hi = mlp->npage - (size >>PAGE_SHIFT ) - npage_lo; + + split =kzalloc (sizeof *split,GFP_KERNEL ); +if (!split) +return -ENOMEM ; + +vfio_dma_unmap (iommu, start, size >>PAGE_SHIFT , mlp->rdwr); + + mlp->npage = npage_lo; + + split->npage = npage_hi; + split->daddr = start + size; + split->vaddr = mlp->vaddr +NPAGE_TO_SIZE (npage_lo) + size; + split->rdwr = mlp->rdwr; +list_add (&split->list, &iommu->dm_list); +return size >>PAGE_SHIFT ; +} + +int vfio_dma_unmap_dm (struct vfio_iommu *iommu,struct vfio_dma_map *dmp) +{ +int ret =0 ; +size_t npage = dmp->size >>PAGE_SHIFT ; +struct list_head *pos, *n; + +if (dmp->dmaaddr & ~PAGE_MASK ) +return -EINVAL ; +if (dmp->size & ~PAGE_MASK ) +return -EINVAL ; + +mutex_lock (&iommu->dgate); + +list_for_each_safe (pos, n, &iommu->dm_list) { +struct dma_map_page *mlp; + + mlp = list_entry(pos, struct dma_map_page, list); +if (ranges_overlap (mlp->daddr,NPAGE_TO_SIZE (mlp->npage), + dmp->dmaaddr, dmp->size)) { + ret =vfio_remove_dma_overlap (iommu, dmp->dmaaddr, + dmp->size, mlp); +if (ret >0 ) + npage -=NPAGE_TO_SIZE (ret); +if (ret <0 || npage ==0 ) +break ; + } + } +mutex_unlock (&iommu->dgate); +return ret >0 ?0 : ret; +} + +int vfio_dma_map_dm (struct vfio_iommu *iommu,struct vfio_dma_map *dmp) +{ +int npage; +struct dma_map_page *mlp, *mmlp =NULL ; +dma_addr_t daddr = dmp->dmaaddr; +unsigned long locked, lock_limit, vaddr = dmp->vaddr; +size_t size = dmp->size; +int ret =0 , rdwr = dmp->flags &VFIO_DMA_MAP_FLAG_WRITE ; + +if (vaddr & (PAGE_SIZE -1 )) +return -EINVAL ; +if (daddr & (PAGE_SIZE -1 )) +return -EINVAL ; +if (size & (PAGE_SIZE -1 )) +return -EINVAL ; + + npage = size >>PAGE_SHIFT ; +if (!npage) +return -EINVAL ; + +if (!iommu) +return -EINVAL ; + +mutex_lock (&iommu->dgate); + +if (vfio_find_dma (iommu, daddr, size)) { + ret = -EBUSY ; + goto out_lock; + } + +/* account for locked pages */ + locked = current->mm->locked_vm + npage; + lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; +if (locked > lock_limit && !capable (CAP_IPC_LOCK )) { +printk (KERN_WARNING "%s: RLIMIT_MEMLOCK (%ld) exceeded\n" , + __func__,rlimit (RLIMIT_MEMLOCK )); + ret = -ENOMEM ; + goto out_lock; + } + + ret =vfio_dma_map (iommu, daddr, vaddr, npage, rdwr); +if (ret) + goto out_lock; + +/* Check if we abut a region below */ +if (daddr) { + mlp =vfio_find_dma (iommu, daddr -1 ,1 ); +if (mlp && mlp->rdwr == rdwr && + mlp->vaddr +NPAGE_TO_SIZE (mlp->npage) == vaddr) { + + mlp->npage += npage; + daddr = mlp->daddr; + vaddr = mlp->vaddr; + npage = mlp->npage; + size =NPAGE_TO_SIZE (npage); + + mmlp = mlp; + } + } + +if (daddr + size) { + mlp =vfio_find_dma (iommu, daddr + size,1 ); +if (mlp && mlp->rdwr == rdwr && mlp->vaddr == vaddr + size) { + + mlp->npage += npage; + mlp->daddr = daddr; + mlp->vaddr = vaddr; + +/* If merged above and below, remove previously +* merged entry. New entry covers it. */ +if (mmlp) { +list_del (&mmlp->list); +kfree (mmlp); + } + mmlp = mlp; + } + } + +if (!mmlp) { + mlp =kzalloc (sizeof *mlp,GFP_KERNEL ); +if (!mlp) { + ret = -ENOMEM ; +vfio_dma_unmap (iommu, daddr, npage, rdwr); + goto out_lock; + } + + mlp->npage = npage; + mlp->daddr = daddr; + mlp->vaddr = vaddr; + mlp->rdwr = rdwr; +list_add (&mlp->list, &iommu->dm_list); + } + +out_lock: +mutex_unlock (&iommu->dgate); +return ret; +} + +static int vfio_iommu_release (struct inode *inode,struct file *filep) +{ +struct vfio_iommu *iommu = filep->private_data; + +vfio_release_iommu (iommu); +return 0 ; +} + +static long vfio_iommu_unl_ioctl(struct file *filep,
Quoted 8 lines
drivers/vfio/vfio_iommu.c line 484
Quoted 6 lines
drivers/vfio/vfio_iommu.c new file +6
-0+ unsigned int cmd, unsigned long arg) +{ +struct vfio_iommu *iommu = filep->private_data; +int ret = -ENOSYS ; + +if (cmd ==VFIO_IOMMU_GET_FLAGS ) {Something is weird with the tabbing here..
Indeed, the joys of switching between kernel and qemu ;) fixed
Quoted 84 lines
Quoted 84 lines
drivers/vfio/vfio_iommu.c new file +46
-0+u64 flags =VFIO_IOMMU_FLAGS_MAP_ANY ; + + ret =put_user (flags, (u64 __user *)arg); + + }else if (cmd ==VFIO_IOMMU_MAP_DMA ) { +struct vfio_dma_map dm; + +if (copy_from_user (&dm, (void __user *)arg,sizeof dm)) +return -EFAULT ; + + ret =vfio_dma_map_dm (iommu, &dm); + +if (!ret &©_to_user ((void __user *)arg, &dm,sizeof dm)) + ret = -EFAULT ; + + }else if (cmd ==VFIO_IOMMU_UNMAP_DMA ) { +struct vfio_dma_map dm; + +if (copy_from_user (&dm, (void __user *)arg,sizeof dm)) +return -EFAULT ; + + ret =vfio_dma_unmap_dm (iommu, &dm); + +if (!ret &©_to_user ((void __user *)arg, &dm,sizeof dm)) + ret = -EFAULT ; + } +return ret; +} + +#ifdef CONFIG_COMPAT +static long vfio_iommu_compat_ioctl (struct file *filep, +unsigned int cmd,unsigned long arg) +{ + arg = (unsigned long )compat_ptr (arg); +return vfio_iommu_unl_ioctl (filep, cmd, arg); +} +#endif /* CONFIG_COMPAT */ + +const struct file_operations vfio_iommu_fops = { + .owner =THIS_MODULE , + .release = vfio_iommu_release, + .unlocked_ioctl = vfio_iommu_unl_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = vfio_iommu_compat_ioctl, +#endif +};drivers/vfio/vfio_main.c new file +32
-0@@ -0,0 +1,1151 @@ +/* +* VFIO framework +* +* Copyright (C) 2011 Red Hat, Inc. All rights reserved. +* Author: Alex Williamson <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* Derived from original vfio: +* Copyright 2010 Cisco Systems, Inc. All rights reserved. +* Author: Tom Lyon, [email protected] +*/ + +#include <linux/cdev.h> +#include <linux/compat.h> +#include <linux/device.h> +#include <linux/file.h> +#include <linux/anon_inodes.h> +#include <linux/fs.h> +#include <linux/idr.h> +#include <linux/iommu.h> +#include <linux/mm.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/uaccess.h> +#include <linux/vfio.h> +#include <linux/wait.h> + +#include "vfio_private.h"
Quoted 8 lines
drivers/vfio/vfio_main.c line 38
Quoted 6 lines
drivers/vfio/vfio_main.c new file +6
-0+ +#define DRIVER_VERSION "0.2" +#define DRIVER_AUTHOR "Alex Williamson <[email protected]>" +#define DRIVER_DESC "VFIO - User Level meta-driver" + +static int allow_unsafe_intrs;
__read_mostly
Ok
Quoted 3 lines
drivers/vfio/vfio_main.c line 39
Quoted 1 line
drivers/vfio/vfio_main.c new file +1
-0+module_param (allow_unsafe_intrs, int,0 );
S_IRUGO?
I actually intended that to be S_IRUGO | S_IWUSR just like the kvm parameter so it can be toggled runtime.
Quoted 8 lines
Quoted 8 lines
drivers/vfio/vfio_main.c new file +8
-0+MODULE_PARM_DESC (allow_unsafe_intrs, +"Allow use of IOMMUs which do not support interrupt remapping" ); + +static struct vfio { + dev_t devt; + struct cdev cdev; + struct list_head group_list; + struct mutex lock;
Quoted 8 lines
drivers/vfio/vfio_main.c line 52
Quoted 5 lines
drivers/vfio/vfio_main.c new file +5
-0+struct kref kref; +struct class *class; +struct idr idr; +wait_queue_head_t release_q; +} vfio;You probably want to move this below the '
vfio_group' as vfio contains thevfio_group.
Only via the group_list. Are you suggesting for readability or to avoid forward declarations (which we don't need between these two with current ordering).
Quoted 20 lines
Quoted 20 lines
drivers/vfio/vfio_main.c new file +20
-0+ +static const struct file_operations vfio_group_fops; +extern const struct file_operations vfio_iommu_fops; + +struct vfio_group { +dev_t devt; +unsigned int groupid; +struct bus_type *bus; +struct vfio_iommu *iommu; +struct list_head device_list; +struct list_head iommu_next; +struct list_head group_next; +int refcnt; +}; + +struct vfio_device { +struct device *dev; +const struct vfio_device_ops *ops; +struct vfio_iommu *iommu; +struct vfio_group *group;
Quoted 8 lines
drivers/vfio/vfio_main.c line 77
Quoted 5 lines
drivers/vfio/vfio_main.c new file +5
-0+struct list_head device_next; +bool attached; +int refcnt; +void *device_data; +};And perhaps move this above
vfio_group. Asvfio_groupcontains a list of these structures?
These are inter-linked, so chicken and egg. The current ordering is more function based than definition based. struct vfio is the highest level object, groups are next, iommus and devices are next, but we need to share iommus with the other file, so that lands in the header.
Quoted 83 lines
Quoted 70 lines
drivers/vfio/vfio_main.c new file +70
-0+ +/* +* Helper functions called under vfio.lock +*/ + +/* Return true if any devices within a group are opened */ +static bool __vfio_group_devs_inuse (struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next); +if (device->refcnt) +return true; + } +return false; +} + +/* Return true if any of the groups attached to an iommu are opened. +* We can only tear apart merged groups when nothing is left open. */ +static bool __vfio_iommu_groups_inuse (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group, iommu_next); +if (group->refcnt) +return true; + } +return false; +} + +/* An iommu is "in use" if it has a file descriptor open or if any of +* the groups assigned to the iommu have devices open. */ +static bool __vfio_iommu_inuse (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (iommu->refcnt) +return true; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group, iommu_next); + +if (__vfio_group_devs_inuse (group)) +return true; + } +return false; +} + +static void __vfio_group_set_iommu (struct vfio_group *group, +struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (group->iommu) +list_del (&group->iommu_next); +if (iommu) +list_add (&group->iommu_next, &iommu->group_list); + + group->iommu = iommu; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device;drivers/vfio/vfio_main.c line 157
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+ + device = list_entry(pos, struct vfio_device, device_next); + device->iommu = iommu; + } +} + +static void __vfio_iommu_detach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +BUG_ON (!iommu->domain && device->attached);Whoa. Heavy hammer there.
Quoted 1 line
Perhaps
WARN_ONas you do check it later on.
I think it's warranted, internal consistency is broken if we have a device that thinks it's attached to an iommu domain that doesn't exist. It should, of course, never happen and this isn't a performance path.
Quoted 30 lines
Quoted 17 lines
drivers/vfio/vfio_main.c new file +17
-0+ +if (!iommu->domain || !device->attached) +return ; + +iommu_detach_device (iommu->domain, device->dev); + device->attached = false; +} + +static void __vfio_iommu_detach_group (struct vfio_iommu *iommu, +struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next);drivers/vfio/vfio_main.c line 184
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+__vfio_iommu_detach_dev (iommu, device); + } +} + +static int __vfio_iommu_attach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +int ret; + +BUG_ON (device->attached);How about:
Quoted 2 lines
WARN_ON(device->attached, "The engineer who wrote the user-space device driver is trying to register the device again! Tell him/her to stop please.\n");
I would almost demote this one to a WARN_ON, but userspace isn't in control of attaching and detaching devices from the iommu. That's a side effect of getting the iommu or device file descriptor. So again, this is an internal consistency check and it should never happen, regardless of userspace.
Quoted 84 lines
Quoted 84 lines
drivers/vfio/vfio_main.c new file +84
-0+ +if (!iommu || !iommu->domain) +return -EINVAL ; + + ret =iommu_attach_device (iommu->domain, device->dev); +if (!ret) + device->attached = true; + +return ret; +} + +static int __vfio_iommu_attach_group (struct vfio_iommu *iommu, +struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; +int ret; + + device = list_entry(pos, struct vfio_device, device_next); + ret =__vfio_iommu_attach_dev (iommu, device); +if (ret) { +__vfio_iommu_detach_group (iommu, group); +return ret; + } + } +return 0 ; +} + +/* The iommu is viable, ie. ready to be configured, when all the devices +* for all the groups attached to the iommu are bound to their vfio device +* drivers (ex. vfio-pci). This sets the device_data private data pointer. */ +static bool __vfio_iommu_viable (struct vfio_iommu *iommu) +{ +struct list_head *gpos, *dpos; + +list_for_each (gpos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(gpos, struct vfio_group, iommu_next); + +list_for_each (dpos, &group->device_list) { +struct vfio_device *device; + device =list_entry (dpos, + struct vfio_device, device_next); + +if (!device->device_data) +return false; + } + } +return true; +} + +static void __vfio_close_iommu (struct vfio_iommu *iommu) +{ +struct list_head *pos; + +if (!iommu->domain) +return ; + +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(pos, struct vfio_group, iommu_next); + +__vfio_iommu_detach_group (iommu, group); + } + +vfio_iommu_unmapall (iommu); + +iommu_domain_free (iommu->domain); + iommu->domain =NULL ; + iommu->mm =NULL ; +} + +/* Open the IOMMU. This gates all access to the iommu or device file +* descriptors and sets current->mm as the exclusive user. */ +static int __vfio_open_iommu (struct vfio_iommu *iommu) +{ +struct list_head *pos; +int ret; + +if (!__vfio_iommu_viable (iommu)) +return -EBUSY ; +
Quoted 8 lines
drivers/vfio/vfio_main.c line 274
Quoted 6 lines
drivers/vfio/vfio_main.c new file +6
-0+if (iommu->domain) +return -EINVAL ; + + iommu->domain =iommu_domain_alloc (iommu->bus); +if (!iommu->domain) +return -EFAULT ;ENOMEM?
Yeah, probably more appropriate.
Quoted 63 lines
Quoted 50 lines
drivers/vfio/vfio_main.c new file +50
-0+ +list_for_each (pos, &iommu->group_list) { +struct vfio_group *group; + group = list_entry(pos, struct vfio_group, iommu_next); + + ret =__vfio_iommu_attach_group (iommu, group); +if (ret) { +__vfio_close_iommu (iommu); +return ret; + } + } + +if (!allow_unsafe_intrs && + !iommu_domain_has_cap (iommu->domain,IOMMU_CAP_INTR_REMAP )) { +__vfio_close_iommu (iommu); +return -EFAULT ; + } + + iommu->cache = (iommu_domain_has_cap (iommu->domain, +IOMMU_CAP_CACHE_COHERENCY ) !=0 ); + iommu->mm = current->mm; + +return 0 ; +} + +/* Actively try to tear down the iommu and merged groups. If there are no +* open iommu or device fds, we close the iommu. If we close the iommu and +* there are also no open group fds, we can futher dissolve the group to +* iommu association and free the iommu data structure. */ +static int __vfio_try_dissolve_iommu (struct vfio_iommu *iommu) +{ + +if (__vfio_iommu_inuse (iommu)) +return -EBUSY ; + +__vfio_close_iommu (iommu); + +if (!__vfio_iommu_groups_inuse (iommu)) { +struct list_head *pos, *ppos; + +list_for_each_safe (pos, ppos, &iommu->group_list) { +struct vfio_group *group; + + group = list_entry(pos, struct vfio_group, iommu_next); +__vfio_group_set_iommu (group,NULL ); + } + + +kfree (iommu); + }drivers/vfio/vfio_main.c line 334
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+ +return 0 ; +} + +static struct vfio_device *__vfio_lookup_dev (struct device *dev) +{ +struct list_head *gpos; +unsigned int groupid; + +if (iommu_device_group (dev, &groupid))Hmm, where is this defined? v3.2-rc1 does not seem to have it?
Quoted 1 line
From patch header:
Fingers crossed, this is the last RFC for VFIO, but we need the iommu group support before this can go upstream
(http://lkml.indiana.edu/hypermail/linux/kernel/1110.2/02303.html),
hoping this helps push that along.
1,059 lines not shown · raw message
Part of this message is not shown here. View the raw message.
Quoted 3 lines · Alex Williamson
Thanks Konrad! Comments inline.
Quoted 1 line · Konrad Rzeszutek Wilk
On Thu, Nov 03, 2011 at 02:12:24PM -0600, Alex Williamson wrote:
[snip]
Quoted 9 lines
Quoted 5 lines
Documentation/vfio.txt line 147
Quoted 3 lines
Documentation/vfio.txt new file +3
-0+The GET_NUM_REGIONS ioctl tells us how many regions the device supports: + +#define VFIO_DEVICE_GET_NUM_REGIONS _IOR(';', 109, int)Don't want
__u32?It could be, not sure if it buys us anything maybe even restricts us. We likely don't need 2^32 regions (famous last words?), so we could later define <0 to something?
As a rule, it's best to use explicit fixed width types for all ioctl() arguments, to avoid compat hell for 32-bit userland on 64-bit kernel setups.
[snip]
Quoted 14 lines
Quoted 4 lines
Documentation/vfio.txt line 188
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+Again, zero count entries are allowed (vfio-pci uses a static interrupt +type to index mapping).I am not really sure what that means.
This is so PCI can expose:
enum {
VFIO_PCI_INTX_IRQ_INDEX,
VFIO_PCI_MSI_IRQ_INDEX,
VFIO_PCI_MSIX_IRQ_INDEX,
VFIO_PCI_NUM_IRQS
};
Quoted 3 lines
So like regions it always exposes 3 IRQ indexes where count=0 if the device doesn't actually support that type of interrupt. I just want to spell out that bus drivers have this kind of flexibility.
I knew what you were aiming for, so I could see what you meant here, but I don't think the doco is very clearly expressed at all.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 110 lines · Alex Williamson
Thanks Konrad! Comments inline.
Quoted 97 lines · Konrad Rzeszutek Wilk
Quoted 96 lines · Alex Williamson
VFIO provides a secure, IOMMU based interface for user space drivers, including device assignment to virtual machines. This provides the base management of IOMMU groups, devices, and IOMMU objects. See
Documentation/vfio.txtincluded in this patch for user and kernel API description.Note, this implements the new API discussed at KVM Forum 2011, as represented by the drvier version 0.2. It's hoped that this provides a modular enough interface to support PCI and non-PCI userspace drivers across various architectures and IOMMU implementations.
Signed-off-by Alex Williamson <[email protected]>---
Fingers crossed, this is the last RFC for VFIO, but we need the iommu group support before this can go upstream
(http://lkml.indiana.edu/hypermail/linux/kernel/1110.2/02303.html),
hoping this helps push that along.Since the last posting, this version completely modularizes the device backends and better defines the APIs between the core VFIO code and the device backends. I expect that we might also adopt a modular IOMMU interface as
iommu_opslearns about different types of hardware. Also many, many cleanups. Check the complete git history for details:
git://github.com/awilliam/linux-vfio.gitvfio-ng(matching qemu tree:
git://github.com/awilliam/qemu-vfio.git)This version, along with the supporting VFIO PCI backend can be found here:
git://github.com/awilliam/linux-vfio.gitvfio-next-20111103I've held off on implementing a
kernel->usersignaling mechanism for now since the previous netlink version produced too many gag reflexes. It's easy enough to set a bit in the group flags too indicate such support in the future, so I think we can move ahead without it.Appreciate any feedback or suggestions. Thanks,
Alex
Documentation/ioctl/ioctl-number.txt | 1 Documentation/vfio.txt | 304 +++++++++ MAINTAINERS | 8 drivers/Kconfig | 2 drivers/Makefile | 1 drivers/vfio/Kconfig | 8 drivers/vfio/Makefile | 3 drivers/vfio/vfio_iommu.c | 530 ++++++++++++++++ drivers/vfio/vfio_main.c | 1151 ++++++++++++++++++++++++++++++++++ drivers/vfio/vfio_private.h | 34 + include/linux/vfio.h | 155 +++++ 11 files changed, 2197 insertions(+), 0 deletions(-) create mode 100644 Documentation/vfio.txt create mode 100644 drivers/vfio/Kconfig create mode 100644 drivers/vfio/Makefile create mode 100644 drivers/vfio/vfio_iommu.c create mode 100644 drivers/vfio/vfio_main.c create mode 100644 drivers/vfio/vfio_private.h create mode 100644 include/linux/vfio.hDocumentation/ioctl/ioctl-number.txt +1
-0@@ -88,6 +88,7 @@ Code Seq#(hex) Include File Comments and kernel/power/user.c '8' all SNP8023 advanced NIC card <mailto:[email protected]> +';' 64-76 linux/vfio.h '@' 00-0F linux/radeonfb.h conflict! '@' 00-0F drivers/video/aty/aty128fb.c conflict! 'A' 00-1F linux/apm_bios.h conflict!Documentation/vfio.txt new file +12
-0@@ -0,0 +1,304 @@ +VFIO - "Virtual Function I/O"[1] +------------------------------------------------------------------------------- +Many modern system now provide DMA and interrupt remapping facilities +to help ensure I/O devices behave within the boundaries they've been +allotted. This includes x86 hardware with AMD-Vi and Intel VT-d as +well as POWER systems with Partitionable Endpoints (PEs) and even +embedded powerpc systems (technology name unknown). The VFIO driver +is an IOMMU/device agnostic framework for exposing direct device +access to userspace, in a secure, IOMMU protected environment. In +other words, this allows safe, non-privileged, userspace drivers. + +Why do we want that? Virtual machines often make use of direct deviceQuoted 8 lines
Documentation/vfio.txt line 17
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+access ("device assignment") when configured for the highest possible +I/O performance. From a device and host perspective, this simply turns +the VM into a userspace driver, with the benefits of significantly +reduced latency, higher bandwidth, and direct use of bare-metal device +drivers[2].Are there any constraints of running a 32-bit userspace with a 64-bit kernel and with 32-bit user space drivers?
Quoted 2 lines
Shouldn't be. I'll need to do some testing on that, but it was working on the previous generation of vfio.
<nods> ok
.. snip..
Quoted 10 lines
Quoted 7 lines
Documentation/vfio.txt line 110
Quoted 4 lines
Documentation/vfio.txt new file +4
-0+#define VFIO_IOMMU_GET_FLAGS _IOR(';', 105, __u64) + #define VFIO_IOMMU_FLAGS_MAP_ANY (1 << 0) +#define VFIO_IOMMU_MAP_DMA _IOWR(';', 106, struct vfio_dma_map) +#define VFIO_IOMMU_UNMAP_DMA _IOWR(';', 107, struct vfio_dma_map)Coherency support is not going to be addressed right? What about sync? Say you need to sync CPU to Device address?
Do we need to expose that to userspace or should the underlying
iommu_opstake care of it?
That I am not sure of. I know that the kernel drivers (especially network ones) are riddled with:
pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE); skb_copy_from_linear_data(skb, copy_skb->data, len); pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
But I think that has come from the fact that the devices are 32-bit so they could not do DMA above 4GB. Hence the bounce buffer usage and the proliferation of pci_dma_sync.. calls to copy the contents to a bounce buffer if neccessary.
But IOMMUs seem to deal with devices that can map the full gamma of memory so they are not constrained to that 32-bit or 36-bit, but rather they do the mapping in hardware if neccessary.
So I think I just answered the question - which is: No. .. snip..
Quoted 12 lines
Quoted 11 lines
Documentation/vfio.txt line 129
Quoted 9 lines
Documentation/vfio.txt new file +9
-0+ __u64 vaddr; /* process virtual addr */ + __u64 dmaaddr; /* desired and/or returned dma address */ + __u64 size; /* size in bytes */ + __u64 flags; +#define VFIO_DMA_MAP_FLAG_WRITE (1 << 0) /* req writeable DMA mem */ +}; + +Current users of VFIO use relatively static DMA mappings, not requiring +high frequency turnover. As new users are added, it's expected that theIs there a limit to how many DMA mappings can be created?
Quoted 2 lines
Not that I'm aware of for the current AMD-Vi/VT-d implementations. I suppose
iommu_opswould return-ENOSPCif it hit a limit. I added the
Not -ENOMEM? Either way, might want to mention that in this nice document.
Quoted 2 lines
VFIO_IOMMU_FLAGS_MAP_ANYflag above to try to identify that kind of restriction.
.. snip..
Quoted 9 lines
Quoted 5 lines
Documentation/vfio.txt line 147
Quoted 3 lines
Documentation/vfio.txt new file +3
-0+The GET_NUM_REGIONS ioctl tells us how many regions the device supports: + +#define VFIO_DEVICE_GET_NUM_REGIONS _IOR(';', 109, int)Don't want
__u32?It could be, not sure if it buys us anything maybe even restricts us. We likely don't need 2^32 regions (famous last words?), so we could later define <0 to something?
OK.
Quoted 25 lines
Quoted 10 lines
Documentation/vfio.txt line 151
Quoted 6 lines
Documentation/vfio.txt new file +4
-0+ +Regions are described by a struct vfio_region_info, which is retrieved by +using the GET_REGION_INFO ioctl with vfio_region_info.index field set to +the desired region (0 based index). Note that devices may implement zero+sized regions (vfio-pci does this to provide a 1:1 BAR to region index
Quoted 1 line
+mapping).
Huh?
PCI has the following static mapping:
enum {
VFIO_PCI_BAR0_REGION_INDEX,
VFIO_PCI_BAR1_REGION_INDEX,
VFIO_PCI_BAR2_REGION_INDEX,
VFIO_PCI_BAR3_REGION_INDEX,
VFIO_PCI_BAR4_REGION_INDEX,
VFIO_PCI_BAR5_REGION_INDEX,
VFIO_PCI_ROM_REGION_INDEX,
VFIO_PCI_CONFIG_REGION_INDEX,
VFIO_PCI_NUM_REGIONS
};
Quoted 4 lines
So 8 regions are always reported regardless of whether the device implements all the BARs and the ROM. Then we have a fixed bar:index mapping so we don't have to create a
region_infofield to describe the bar number for the index.
OK. Is that a problem if the real device actually has a zero sized BAR? Or is zero sized BAR in PCI spec equal to "disabled, not in use" ? Just wondering whether (-1ULL) should be used instead? (Which seems the case in QEMU code).
Quoted 17 lines
Quoted 13 lines
Documentation/vfio.txt line 163
Quoted 11 lines
Documentation/vfio.txt new file +10
-0+ +struct vfio_region_info { + __u32 len; /* length of structure */ + __u32 index; /* region number */ + __u64 size; /* size in bytes of region */ + __u64 offset; /* start offset of region */ + __u64 flags; +#define VFIO_REGION_INFO_FLAG_MMAP (1 << 0) +#define VFIO_REGION_INFO_FLAG_RO (1 << 1) +#define VFIO_REGION_INFO_FLAG_PHYS_VALID (1 << 2)What is
FLAG_MMAP? Does it mean: 1) it can be mmaped, or 2) it is mmaped?Supports mmap
Quoted 7 lines
Quoted 3 lines
FLAG_ROis pretty obvious - presumarily this is for firmware regions and such. AndPHYS_VALIDis if the region is disabled for some reasons? If so would the nameFLAG_DISABLEDbe better?No, POWER guys have some need to report the host physical address of the region, so the flag indicates whether the below field is present and valid. I'll clarify these in the docs.
Thanks.
.. snip..
Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
Documentation/vfio.txt new file +4
-0+struct vfio_irq_info { + __u32 len; /* length of structure */ + __u32 index; /* IRQ number */ + __u32 count; /* number of individual IRQs */Quoted 8 lines
Documentation/vfio.txt line 188
Quoted 6 lines
Documentation/vfio.txt new file +6
-0+ __u64 flags; +#define VFIO_IRQ_INFO_FLAG_LEVEL (1 << 0) +}; + +Again, zero count entries are allowed (vfio-pci uses a static interrupt +type to index mapping).I am not really sure what that means.
This is so PCI can expose:
enum {
VFIO_PCI_INTX_IRQ_INDEX,
VFIO_PCI_MSI_IRQ_INDEX,
VFIO_PCI_MSIX_IRQ_INDEX,
VFIO_PCI_NUM_IRQS
};
Quoted 3 lines
So like regions it always exposes 3 IRQ indexes where count=0 if the device doesn't actually support that type of interrupt. I just want to spell out that bus drivers have this kind of flexibility.
I think you should change the comment that says 'IRQ number', as the first thing that comes in my mind is 'GSI' or MSI/MSI-x vector. Perhaps '/* index to be used with return value from GET_NUM_IRQS ioctl. Order of structures can be unsorted. */
Quoted 27 lines
Quoted 17 lines
Quoted 5 lines
Documentation/vfio.txt new file +4
-0+ +Information about each index can be retrieved using the GET_IRQ_INFO +ioctl, used much like GET_REGION_INFO. +Documentation/vfio.txt line 202
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+#define VFIO_DEVICE_GET_IRQ_INFO _IOWR(';', 112, struct vfio_irq_info) + +Individual indexes can describe single or sets of IRQs. This provides the +flexibility to describe PCI INTx, MSI, and MSI-X using a single interface. + +All VFIO interrupts are signaled to userspace via eventfds. Integer arrays, +as shown below, are used to pass the IRQ info index, the number of eventfds, +and each eventfd to be signaled. Using a count of 0 disables the interrupt. + +/* Set IRQ eventfds, arg[0] = index, arg[1] = count, arg[2-n] = eventfds */Are eventfds u64 or u32?
int, they're just file descriptors
Quoted 6 lines
Why not just define a structure?
struct vfio_irq_eventfds { __u32 index; __u32 count; __u64 eventfds[0] };
Quoted 2 lines
We could do that if preferred. Hmm, are we then going to need size/flags?
Sure.
Quoted 21 lines
Quoted 2 lines
How do you get an eventfd to feed in here?
eventfd(2), in qemu
event_notifier_init()->event_notifier_get_fd()Quoted 3 lines
Quoted 1 line
+#define
VFIO_DEVICE_SET_IRQ_EVENTFDS_IOW(';', 113, int)u32?
Not here, it's an fd, so should be an int.
Quoted 9 lines
Documentation/vfio.txt line 209
Quoted 6 lines
Documentation/vfio.txt new file +6
-0+ +When a level triggered interrupt is signaled, the interrupt is masked +on the host. This prevents an unresponsive userspace driver from +continuing to interrupt the host system. After servicing the interrupt, +UNMASK_IRQ is used to allow the interrupt to retrigger. Note that level +triggered interrupts implicitly have a count of 1 per index.So they are enabled automatically? Meaning you don't even hav to do
SET_IRQ_EVENTFDSb/c the count is set to 1?
Quoted 10 lines
I suppose that should be "no more than 1 per index" (ie. PCI would report a count of 0 for
VFIO_PCI_INTX_IRQ_INDEXif the device doesn't support INTx). I think you might be confusingVFIO_DEVICE_GET_IRQ_INFOwhich tells how many are available withVFIO_DEVICE_SET_IRQ_EVENTFDSwhich does the enabling/disabling. All interrupts are disabled by default because userspace needs to give us a way to signal them via eventfds. It will be device dependent whether multiple index can be enabled simultaneously. Hmm, is that another flag on theirq_infostruct or do we expect drivers to implicitly have that kind of knowledge?
Right, that was what I was wondering. Not sure how the PowerPC world works with this.
Quoted 8 lines
Quoted 7 lines
Documentation/vfio.txt line 212
Quoted 4 lines
Documentation/vfio.txt new file +3
-0+ +/* Unmask IRQ index, arg[0] = index */ +#define VFIO_DEVICE_UNMASK_IRQ _IOW(';', 114, int)So this is for MSI as well? So if I've an index = 1, with count = 4, and doing unmaks IRQ will chip enable all the MSI event at once?
Quoted 6 lines
No, this is only for re-enabling level triggered interrupts as discussed above. Edge triggered interrupts like MSI don't need an unmask... we may want to do something to accelerate the MSI-X table access for masking specific interrupts, but I figured that would need to be PCI aware since those are PCI features, and would therefore be some future extension of the PCI bus driver and exposed via
VFIO_DEVICE_GET_FLAGS.
OK.
Quoted 8 lines
Quoted 3 lines
I guess there is not much point in enabling/disabling selective MSI IRQs..
Some older OSes are said to make extensive use of masking for MSI, so we probably want this at some point. I'm assuming future PCI extension for now.
Quoted 7 lines
Quoted 4 lines
Documentation/vfio.txt
Quoted 2 lines
Documentation/vfio.txt new file +2
-0+ +Level triggered interrupts can also be unmasked using an irqfd. Useirqfd or eventfd?
irqfd is an eventfd in reverse. eventfd = kernel signals userspace via an fd, irqfd = userspace signals kernel via an fd.
Ah neat.
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines
+SET_UNMASK_IRQ_EVENTFD to set the file descriptor for this.
So only level triggered? Hmm, how do I know whether the device is level or edge? Or is that edge (MSI) can also be unmaked using the eventfs
Yes, only for level. Isn't a device going to know what type of interrupt it uses? MSI masking is PCI specific, not handled by this.
I certainly hope it knows, but you know buggy drivers do exist.
What would be the return value if somebody tried to unmask an edge one? Should that be documented here? -ENOSPEC?
Quoted 14 lines
Quoted 13 lines
Documentation/vfio.txt line 222
Quoted 8 lines
Documentation/vfio.txt new file +7
-0+ +/* Set unmask eventfd, arg[0] = index, arg[1] = eventfd */ +#define VFIO_DEVICE_SET_UNMASK_IRQ_EVENTFD _IOW(';', 115, int) + +When supported, as indicated by the device flags, reset the device. + +#define VFIO_DEVICE_RESET _IO(';', 116)Does it disable the 'count'? Err, does it disable the IRQ on the device after this and one should call
VFIO_DEVICE_SET_IRQ_EVENTFDSto set new eventfds? Or does it re-use the eventfds and the device is enabled after this?
Quoted 1 line
It doesn't affect the interrupt programming. Should it?
I would hope not, but I am trying to think of ways one could screw this up. Perhaps just saying that - "No need to call VFIO_DEVICE_SET_IRQ_EVENTFDS as the kernel (and the device) will retain the interrupt.".
.. snip..
Quoted 6 lines
Quoted 3 lines
I am not really sure what this section purpose is? Could this be part of the header file or the code? It does not look to be part of the ioctl API?
We've passed into the "VFIO bus driver API" section of the document, to explain the interaction between vfio-core and vfio bus drivers.
Perhaps a different file?
.. large snip ..
Quoted 23 lines
Quoted 11 lines
Documentation/vfio.txt
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+ + mutex_lock(&iommu->dgate); + list_for_each_safe(pos, pos2, &iommu->dm_list) { + mlp = list_entry(pos, struct dma_map_page, list); + vfio_dma_unmap(iommu, mlp->daddr, mlp->npage, mlp->rdwr);Uh, so if it did not get
put_page()we would try to still delete it? Couldn't that lead to corruption as the 'mlp' is returned to the poll?Ah wait, the
put_pageis on the DMA page, so it is OK to delete the tracking structure. It will be just a leaked page.Assume you're referencing this chunk:
vfio_dma_unmap
__vfio_dma_unmap... pfn = iommu_iova_to_phys(iommu->domain, iova) >> PAGE_SHIFT; if (pfn) { iommu_unmap(iommu->domain, iova, 0); unlocked += put_pfn(pfn, rdwr); }
Quoted 6 lines
So we skip things that aren't mapped in the iommu, but anything not mapped should have already been put (failed
vfio_dma_map). If it is mapped, we put it if we originally got it viaget_user_pages_fast. unlocked would only not get incremented here if it was an mmap'd page (such as the mmap of an mmio space of another vfio device), via the code invaddr_get_pfn(stolen from KVM).
Yup. Sounds right.
.. snip..
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
+module_param(allow_unsafe_intrs, int, 0);
S_IRUGO?I actually intended that to be
S_IRUGO|S_IWUSRjust like the kvm parameter so it can be toggled runtime.
OK.
Quoted 18 lines
Quoted 17 lines
Quoted 4 lines
Documentation/vfio.txt new file +3
-0+MODULE_PARM_DESC(allow_unsafe_intrs, + "Allow use of IOMMUs which do not support interrupt remapping"); +Documentation/vfio.txt
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+static struct vfio { + dev_t devt; + struct cdev cdev; + struct list_head group_list; + struct mutex lock; + struct kref kref; + struct class *class; + struct idr idr; + wait_queue_head_t release_q; +} vfio;You probably want to move this below the '
vfio_group' as vfio contains thevfio_group.
Quoted 3 lines
Only via the
group_list. Are you suggesting for readability or to avoid forward declarations (which we don't need between these two with current ordering).
Just for readability.
Quoted 30 lines
Quoted 21 lines
Quoted 21 lines
Documentation/vfio.txt new file +20
-0+ +static const struct file_operations vfio_group_fops; +extern const struct file_operations vfio_iommu_fops; + +struct vfio_group { + dev_t devt; + unsigned int groupid; + struct bus_type *bus; + struct vfio_iommu *iommu; + struct list_head device_list; + struct list_head iommu_next; + struct list_head group_next; + int refcnt; +}; + +struct vfio_device { + struct device *dev; + const struct vfio_device_ops *ops; + struct vfio_iommu *iommu; + struct vfio_group *group;Quoted 8 lines
Documentation/vfio.txt
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+ struct list_head device_next; + bool attached; + int refcnt; + void *device_data; +};And perhaps move this above
vfio_group. Asvfio_groupcontains a list of these structures?
Quoted 4 lines
These are inter-linked, so chicken and egg. The current ordering is more function based than definition based. struct vfio is the highest level object, groups are next, iommus and devices are next, but we need to share iommus with the other file, so that lands in the header.
Ah, OK.
Quoted 86 lines
Quoted 84 lines
Quoted 71 lines
Documentation/vfio.txt new file +70
-0+ +/* + * Helper functions called under vfio.lock + */ + +/* Return true if any devices within a group are opened */ +static bool __vfio_group_devs_inuse(struct vfio_group *group) +{ + struct list_head *pos; + + list_for_each(pos, &group->device_list) { + struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next); + if (device->refcnt) + return true; + } + return false; +} + +/* Return true if any of the groups attached to an iommu are opened. + * We can only tear apart merged groups when nothing is left open. */ +static bool __vfio_iommu_groups_inuse(struct vfio_iommu *iommu) +{ + struct list_head *pos; + + list_for_each(pos, &iommu->group_list) { + struct vfio_group *group; + + group = list_entry(pos, struct vfio_group, iommu_next); + if (group->refcnt) + return true; + } + return false; +} + +/* An iommu is "in use" if it has a file descriptor open or if any of + * the groups assigned to the iommu have devices open. */ +static bool __vfio_iommu_inuse(struct vfio_iommu *iommu) +{ + struct list_head *pos; + + if (iommu->refcnt) + return true; + + list_for_each(pos, &iommu->group_list) { + struct vfio_group *group; + + group = list_entry(pos, struct vfio_group, iommu_next); + + if (__vfio_group_devs_inuse(group)) + return true; + } + return false; +} + +static void __vfio_group_set_iommu(struct vfio_group *group, + struct vfio_iommu *iommu) +{ + struct list_head *pos; + + if (group->iommu) + list_del(&group->iommu_next); + if (iommu) + list_add(&group->iommu_next, &iommu->group_list); + + group->iommu = iommu; + + list_for_each(pos, &group->device_list) { + struct vfio_device *device;Documentation/vfio.txt
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+ + device = list_entry(pos, struct vfio_device, device_next); + device->iommu = iommu; + } +} + +static void __vfio_iommu_detach_dev(struct vfio_iommu *iommu, + struct vfio_device *device) +{ + BUG_ON(!iommu->domain && device->attached);Whoa. Heavy hammer there.
Quoted 1 line
Perhaps
WARN_ONas you do check it later on.
Quoted 7 lines
I think it's warranted, internal consistency is broken if we have a device that thinks it's attached to an iommu domain that doesn't exist. It should, of course, never happen and this isn't a performance path.
Quoted 3 lines
Documentation/vfio.txt
Quoted 3 lines
Documentation/vfio.txt new file +3
-0+ + if (!iommu->domain || !device->attached) + return;
Well, the deal is that you BUG_ON earlier, but you check for it here. But the BUG_ON will stop execution , so the check 'if ..' is actually not needed.
Quoted 27 lines
Quoted 27 lines
Quoted 14 lines
Documentation/vfio.txt new file +14
-0+ + iommu_detach_device(iommu->domain, device->dev); + device->attached = false; +} + +static void __vfio_iommu_detach_group(struct vfio_iommu *iommu, + struct vfio_group *group) +{ + struct list_head *pos; + + list_for_each(pos, &group->device_list) { + struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next);Documentation/vfio.txt
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+ __vfio_iommu_detach_dev(iommu, device); + } +} + +static int __vfio_iommu_attach_dev(struct vfio_iommu *iommu, + struct vfio_device *device) +{ + int ret; + + BUG_ON(device->attached);How about:
Quoted 9 lines
Quoted 2 lines
WARN_ON(device->attached, "The engineer who wrote the user-space device driver is trying to register the device again! Tell him/her to stop please.\n");
I would almost demote this one to a
WARN_ON, but userspace isn't in control of attaching and detaching devices from the iommu. That's a side effect of getting the iommu or device file descriptor. So again, this is an internal consistency check and it should never happen, regardless of userspace.
Ok, then you might want to expand it to
BUG_ON(!device || device->attached);
In case something has gone horribly wrong.
.. snip..
Quoted 15 lines
Quoted 15 lines
Documentation/vfio.txt
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+ group->devt = MKDEV(MAJOR(vfio.devt), minor); + device_create(vfio.class, NULL, group->devt, + group, "%u", groupid); + + group->bus = dev->bus;Oh, so that is how the IOMMU
iommu_opsget copied! You might want to mention that - I was not sure where the 'handoff' is was done to insert a device so that it can doiommu_opsproperly.Ok, so the time when a device is detected whether it can do IOMMU is when we try to open it - as that is when
iommu_domain_allocis called which can return NULL if theiommu_opsis not set.
Quoted 7 lines
Quoted 5 lines
So what about devices that don't have an
iommu_ops? Say they are using SWIOTLB? (like the AMD-Vi sometimes does if the device is not on its list).Can we use
iommu_present?I'm not sure I'm following your revelation ;) Take a look at the
I am trying to figure out who sets the iommu_ops call on the devices.
Quoted 12 lines
pointer to
iommu_device_groupI pasted above, or these:https://github.com/awilliam/linux-vfio/commit/37dd08c90d149caaed7779d4f38850a8f7ed0fa5
https://github.com/awilliam/linux-vfio/commit/63ca8543533d8130db23d7949133e548c3891c97
https://github.com/awilliam/linux-vfio/commit/8d7d70eb8e714fbf8710848a06f8cab0c741631eThat call includes an
iommu_present()check, so if there's no iommu or the iommu can't provide a groupid, the device is skipped over from vfio (can't be used).So the ordering is:
Quoted 7 lines
- bus driver registers device
- if it has an iommu group, add it to the vfio device/group tracking- group gets opened
- user gets iommu or device fd results iniommu_domain_allocDevices without
iommu_opsdon't get to play in the vfio world.
Right, and I think the answer of which devices get iommu_ops is done via bus_set_iommu.
(Thinking in long-term of what would be required to make this work with Xen and it sounds like I will need to implement a Xen IOMMU driver)
.. snip..
Quoted 6 lines
Quoted 2 lines
So where is the vfio-pci? Is that a seperate posting?
You can find it in the tree pointed to in the patch description:
https://github.com/awilliam/linux-vfio/commit/534725d327e2b7791a229ce72d2ae8a62ee0a4e5
Thanks.
Quoted 4 lines
I was hoping to get some consensus around the new core before spending too much time polishing up the bus driver. Thanks for the review, it's very much appreciated!
Sure thing.
Quoted 3 lines
Alex
Quoted 1 line · Konrad Rzeszutek Wilk
On Fri, Nov 11, 2011 at 03:10:56PM -0700, Alex Williamson wrote:
<snip>
Quoted 29 lines
Quoted 24 lines
Quoted 9 lines
Documentation/vfio.txt line 151
Quoted 5 lines
Documentation/vfio.txt new file +4
-0+ +Regions are described by a struct vfio_region_info, which is retrieved by +using the GET_REGION_INFO ioctl with vfio_region_info.index field set to +the desired region (0 based index). Note that devices may implement zero+sized regions (vfio-pci does this to provide a 1:1 BAR to region index
Quoted 1 line
+mapping).
Huh?
PCI has the following static mapping:
enum {
VFIO_PCI_BAR0_REGION_INDEX,
VFIO_PCI_BAR1_REGION_INDEX,
VFIO_PCI_BAR2_REGION_INDEX,
VFIO_PCI_BAR3_REGION_INDEX,
VFIO_PCI_BAR4_REGION_INDEX,
VFIO_PCI_BAR5_REGION_INDEX,
VFIO_PCI_ROM_REGION_INDEX,
VFIO_PCI_CONFIG_REGION_INDEX,
VFIO_PCI_NUM_REGIONS
};Quoted 4 lines
So 8 regions are always reported regardless of whether the device implements all the BARs and the ROM. Then we have a fixed bar:index mapping so we don't have to create a
region_infofield to describe the bar number for the index.
Quoted 4 lines
OK. Is that a problem if the real device actually has a zero sized BAR? Or is zero sized BAR in PCI spec equal to "disabled, not in use" ? Just wondering whether (-1ULL) should be used instead? (Which seems the case in QEMU code).
Yes, PCI spec defines that unimplemented BARs are hardwired to zero, so the sizing operation returns zero for the size.
<snip>
Quoted 22 lines
Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
Documentation/vfio.txt new file +4
-0+struct vfio_irq_info { + __u32 len; /* length of structure */ + __u32 index; /* IRQ number */ + __u32 count; /* number of individual IRQs */Quoted 8 lines
Documentation/vfio.txt line 188
Quoted 6 lines
Documentation/vfio.txt new file +6
-0+ __u64 flags; +#define VFIO_IRQ_INFO_FLAG_LEVEL (1 << 0) +}; + +Again, zero count entries are allowed (vfio-pci uses a static interrupt +type to index mapping).I am not really sure what that means.
This is so PCI can expose:
enum {
VFIO_PCI_INTX_IRQ_INDEX,
VFIO_PCI_MSI_IRQ_INDEX,
VFIO_PCI_MSIX_IRQ_INDEX,
VFIO_PCI_NUM_IRQS
};
Quoted 8 lines
Quoted 3 lines
So like regions it always exposes 3 IRQ indexes where count=0 if the device doesn't actually support that type of interrupt. I just want to spell out that bus drivers have this kind of flexibility.
I think you should change the comment that says 'IRQ number', as the first thing that comes in my mind is 'GSI' or MSI/MSI-x vector. Perhaps '/* index to be used with return value from
GET_NUM_IRQSioctl. Order of structures can be unsorted. */
Ah, yes. I see the confusion. They can't really be unsorted though, the user needs some point of reference. For PCI they will be strictly ordered. For Device Tree, I assume there will be a path referencing the index. I'll update the doc to clarify.
<snip>
Quoted 21 lines
Quoted 10 lines
Quoted 9 lines
Documentation/vfio.txt line 209
Quoted 6 lines
Documentation/vfio.txt new file +6
-0+ +When a level triggered interrupt is signaled, the interrupt is masked +on the host. This prevents an unresponsive userspace driver from +continuing to interrupt the host system. After servicing the interrupt, +UNMASK_IRQ is used to allow the interrupt to retrigger. Note that level +triggered interrupts implicitly have a count of 1 per index.So they are enabled automatically? Meaning you don't even hav to do
SET_IRQ_EVENTFDSb/c the count is set to 1?Quoted 10 lines
I suppose that should be "no more than 1 per index" (ie. PCI would report a count of 0 for
VFIO_PCI_INTX_IRQ_INDEXif the device doesn't support INTx). I think you might be confusingVFIO_DEVICE_GET_IRQ_INFOwhich tells how many are available withVFIO_DEVICE_SET_IRQ_EVENTFDSwhich does the enabling/disabling. All interrupts are disabled by default because userspace needs to give us a way to signal them via eventfds. It will be device dependent whether multiple index can be enabled simultaneously. Hmm, is that another flag on theirq_infostruct or do we expect drivers to implicitly have that kind of knowledge?
Quoted 2 lines
Right, that was what I was wondering. Not sure how the PowerPC world works with this.
On second thought, I think an exclusive flag isn't appropriate. VFIO is not meant to abstract the device to the level that a user could write a generic "vfio driver". The user will always need to understand the type of device, VFIO just provides the conduit to make use of it. There's too much left undefined with a simplistic exclusive flag.
<snip>
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 1 line
+SET_UNMASK_IRQ_EVENTFD to set the file descriptor for this.
So only level triggered? Hmm, how do I know whether the device is level or edge? Or is that edge (MSI) can also be unmaked using the eventfs
Quoted 7 lines
Quoted 2 lines
Yes, only for level. Isn't a device going to know what type of interrupt it uses? MSI masking is PCI specific, not handled by this.
I certainly hope it knows, but you know buggy drivers do exist.
What would be the return value if somebody tried to unmask an edge one? Should that be documented here?
-ENOSPEC?
I would assume EINVAL or EFAULT since the user is providing an invalid argument/bad address.
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Documentation/vfio.txt line 222
Quoted 7 lines
Documentation/vfio.txt new file +7
-0+ +/* Set unmask eventfd, arg[0] = index, arg[1] = eventfd */ +#define VFIO_DEVICE_SET_UNMASK_IRQ_EVENTFD _IOW(';', 115, int) + +When supported, as indicated by the device flags, reset the device. + +#define VFIO_DEVICE_RESET _IO(';', 116)Does it disable the 'count'? Err, does it disable the IRQ on the device after this and one should call
VFIO_DEVICE_SET_IRQ_EVENTFDSto set new eventfds? Or does it re-use the eventfds and the device is enabled after this?
Quoted 5 lines
Quoted 1 line
It doesn't affect the interrupt programming. Should it?
I would hope not, but I am trying to think of ways one could screw this up. Perhaps just saying that - "No need to call
VFIO_DEVICE_SET_IRQ_EVENTFDSas the kernel (and the device) will retain the interrupt.".
Ok, I added some words around this in the doc.
Quoted 9 lines
.. snip..
Quoted 6 lines
Quoted 3 lines
I am not really sure what this section purpose is? Could this be part of the header file or the code? It does not look to be part of the ioctl API?
We've passed into the "VFIO bus driver API" section of the document, to explain the interaction between vfio-core and vfio bus drivers.
Perhaps a different file?
The entire file is ~300 lines. Seems excessive to split.
<snip>
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines
drivers/vfio/vfio_main.c line 157
Quoted 4 lines
drivers/vfio/vfio_main.c new file +4
-0+static void __vfio_iommu_detach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +BUG_ON (!iommu->domain && device->attached);Whoa. Heavy hammer there.
Perhaps
WARN_ONas you do check it later on.I think it's warranted, internal consistency is broken if we have a device that thinks it's attached to an iommu domain that doesn't exist. It should, of course, never happen and this isn't a performance path.
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
drivers/vfio/vfio_main.c line 160
Quoted 3 lines
drivers/vfio/vfio_main.c new file +3
-0+ +if (!iommu->domain || !device->attached) +return ;Well, the deal is that you
BUG_ONearlier, but you check for it here. But theBUG_ONwill stop execution , so the check 'if ..' is actually not needed.
The BUG_ON is a subtly different check:
domain | attached
-------+---------
0 | 0 Nothing to do 0 | 1 <--- BUG_ON, we're broken 1 | 0 Nothing to do 1 | 1 Do stuff
Writing out the truth table, I see now I could just make this:
if (!attached) {return;}
since the BUG_ON takes care of the other case.
The reason for the laziness of allowing this to simply return is that if we hit an error attaching an individual device within a group, we just push the whole group back through __vfio_iommu_detach_group(), so some devices may have never been attached.
Quoted 37 lines
Quoted 27 lines
Quoted 27 lines
Quoted 14 lines
drivers/vfio/vfio_main.c new file +14
-0+ +iommu_detach_device (iommu->domain, device->dev); + device->attached = false; +} + +static void __vfio_iommu_detach_group (struct vfio_iommu *iommu, +struct vfio_group *group) +{ +struct list_head *pos; + +list_for_each (pos, &group->device_list) { +struct vfio_device *device; + + device = list_entry(pos, struct vfio_device, device_next);drivers/vfio/vfio_main.c line 184
Quoted 10 lines
drivers/vfio/vfio_main.c new file +10
-0+__vfio_iommu_detach_dev (iommu, device); + } +} + +static int __vfio_iommu_attach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +int ret; + +BUG_ON (device->attached);How about:
Quoted 9 lines
Quoted 2 lines
WARN_ON(device->attached, "The engineer who wrote the user-space device driver is trying to register the device again! Tell him/her to stop please.\n");
I would almost demote this one to a
WARN_ON, but userspace isn't in control of attaching and detaching devices from the iommu. That's a side effect of getting the iommu or device file descriptor. So again, this is an internal consistency check and it should never happen, regardless of userspace.
Quoted 5 lines
Ok, then you might want to expand it to
BUG_ON(!device || device->attached);In case something has gone horribly wrong.
Impressive, that exceeds even my paranoia ;) For that we would have had to walk the group->device_list and come up with a NULL device pointer. I think we can assume that won't happen. I've also got this though:
if (!iommu || !iommu->domain)
return -EINVAL;Which is effectively just being lazy without a good excuse like above. That could probably be folded into the BUG_ON.
Quoted 21 lines
.. snip..
Quoted 19 lines
Quoted 19 lines
drivers/vfio/vfio_main.c line 871
Quoted 5 lines
drivers/vfio/vfio_main.c new file +5
-0+ group->devt =MKDEV (MAJOR (vfio.devt), minor); +device_create (vfio.class,NULL , group->devt, + group,"%u" , groupid); + + group->bus = dev->bus;Oh, so that is how the IOMMU
iommu_opsget copied! You might want to mention that - I was not sure where the 'handoff' is was done to insert a device so that it can doiommu_opsproperly.Ok, so the time when a device is detected whether it can do IOMMU is when we try to open it - as that is when
iommu_domain_allocis called which can return NULL if theiommu_opsis not set.So what about devices that don't have an
iommu_ops? Say they are using SWIOTLB? (like the AMD-Vi sometimes does if the device is not on its list).
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Can we use
iommu_present?I'm not sure I'm following your revelation ;) Take a look at the
I am trying to figure out who sets the
iommu_opscall on the devices.
The iommu driver registers ops with bus_set_iommu, so then we just need to pass the bus pointer and iommu_ops figures out the rest. If there's no iommu_ops for a device or the iommu_ops doesn't implement the device_group callback, it gets skipped by vfio and therefore won't be usable by this interface.
Quoted 15 lines
Quoted 15 lines
pointer to
iommu_device_groupI pasted above, or these:https://github.com/awilliam/linux-vfio/commit/37dd08c90d149caaed7779d4f38850a8f7ed0fa5
https://github.com/awilliam/linux-vfio/commit/63ca8543533d8130db23d7949133e548c3891c97
https://github.com/awilliam/linux-vfio/commit/8d7d70eb8e714fbf8710848a06f8cab0c741631eThat call includes an
iommu_present()check, so if there's no iommu or the iommu can't provide a groupid, the device is skipped over from vfio (can't be used).So the ordering is:
- bus driver registers device
- if it has an iommu group, add it to the vfio device/group tracking
Quoted 7 lines
Quoted 4 lines
- group gets opened
- user gets iommu or device fd results iniommu_domain_allocDevices without
iommu_opsdon't get to play in the vfio world.Right, and I think the answer of which devices get
iommu_opsis done viabus_set_iommu.
Exactly.
Quoted 2 lines
(Thinking in long-term of what would be required to make this work with Xen and it sounds like I will need to implement a Xen IOMMU driver)
Yeah, that would make sense. Thanks!
Alex
Quoted 7 lines · Alex Williamson
Quoted 3 lines · Konrad Rzeszutek Wilk
On Fri, Nov 11, 2011 at 03:10:56PM -0700, Alex Williamson wrote:
What would be the return value if somebody tried to unmask an edge one? Should that be documented here?-ENOSPEC?I would assume EINVAL or EFAULT since the user is providing an invalid argument/bad address.
EINVAL. EFAULT is normally only used for when the user passes a bad virtual memory address to the kernel. This isn't an address at all, it's an index that points to an object for which this operation does not make sense.
-Scott
Quoted 14 lines · Alex Williamson
Thanks Konrad! Comments inline.
Quoted 9 lines · Konrad Rzeszutek Wilk
Documentation/vfio.txt line 222
Quoted 3 lines · Alex Williamson
Documentation/vfio.txt new file +3
-0+When supported, as indicated by the device flags, reset the device. + +#define VFIO_DEVICE_RESET _IO(';', 116)Does it disable the 'count'? Err, does it disable the IRQ on the device after this and one should call
VFIO_DEVICE_SET_IRQ_EVENTFDSto set new eventfds? Or does it re-use the eventfds and the device is enabled after this?
Quoted 1 line
It doesn't affect the interrupt programming. Should it?
It should probably clear any currently pending interrupts, as if the unmask IOCTL were called.
Quoted 9 lines
Quoted 7 lines
Documentation/vfio.txt line 229
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+device tree properties of the device: + +struct vfio_dtpath { + __u32 len; /* length of structure */ + __u32 index;0 based I presume?
Everything else is, I would assume so/
Yes, it should be zero-based -- this matches how such indices are done in the kernel device tree APIs.
Quoted 9 lines
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ __u64 flags; +#define VFIO_DTPATH_FLAGS_REGION (1 << 0)What is region in this context?? Or would this make much more sense if I knew what Device Tree actually is.
Powerpc guys, any comments? This was their suggestion. These are effectively the first device specific extension, available when
VFIO_DEVICE_FLAGS_DTis set.
An assigned device may consist of an entire subtree of the device tree, and both register banks and interrupts can come from any node in the tree. Region versus IRQ here indicates the context in which to interpret index, in order to retrieve the path of the node that supplied this particular region or IRQ.
Quoted 9 lines
Quoted 9 lines
Documentation/vfio.txt line 240
Quoted 7 lines
Documentation/vfio.txt new file +7
-0+}; +#define VFIO_DEVICE_GET_DTPATH _IOWR(';', 117, struct vfio_dtpath) + +struct vfio_dtindex { + __u32 len; /* length of structure */ + __u32 index; + __u32 prop_type;Is that an enum type? Is this definied somewhere?
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
+
__u32prop_index;What is the purpose of this field?
Need input from powerpc folks here
To identify what this resource (register bank or IRQ) this is, we need both the path to the node and the index into the reg or interrupts property within the node.
We also need to distinguish reg from ranges, and interrupts from interrupt-map. As you suggested elsewhere in the thread, the device tree API should probably be left out for now, and added later along with the device tree "bus" driver.
Quoted 13 lines
Quoted 8 lines
drivers/vfio/vfio_main.c line 157
Quoted 4 lines
drivers/vfio/vfio_main.c new file +4
-0+static void __vfio_iommu_detach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +BUG_ON (!iommu->domain && device->attached);Whoa. Heavy hammer there.
Perhaps
WARN_ONas you do check it later on.I think it's warranted, internal consistency is broken if we have a device that thinks it's attached to an iommu domain that doesn't exist. It should, of course, never happen and this isn't a performance path.
[snip]
Quoted 9 lines
Quoted 9 lines
drivers/vfio/vfio_main.c line 184
Quoted 6 lines
drivers/vfio/vfio_main.c new file +6
-0+static int __vfio_iommu_attach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +int ret; + +BUG_ON (device->attached);How about:
Quoted 8 lines
Quoted 2 lines
WARN_ON(device->attached, "The engineer who wrote the user-space device driver is trying to register the device again! Tell him/her to stop please.\n");
I would almost demote this one to a
WARN_ON, but userspace isn't in control of attaching and detaching devices from the iommu. That's a side effect of getting the iommu or device file descriptor. So again, this is an internal consistency check and it should never happen, regardless of userspace.
The rule isn't to use BUG for internal consistency checks and WARN for stuff userspace can trigger, but rather to use BUG if you cannot reasonably continue, WARN for "significant issues that need prompt attention" that are reasonably recoverable. Most instances of WARN are internal consistency checks.
Quoted 4 lines
From
include/asm-generic/bug.h:
If you're tempted toBUG(), think again: is completely giving up really the *only* solution? There are usually better options, where users don't need to reboot ASAP and can mostly shut down cleanly.
-Scott
Quoted 15 lines · Scott Wood
Quoted 14 lines · Alex Williamson
Thanks Konrad! Comments inline.
Quoted 9 lines · Konrad Rzeszutek Wilk
Documentation/vfio.txt line 222
Quoted 3 lines · Alex Williamson
Documentation/vfio.txt new file +3
-0+When supported, as indicated by the device flags, reset the device. + +#define VFIO_DEVICE_RESET _IO(';', 116)Does it disable the 'count'? Err, does it disable the IRQ on the device after this and one should call
VFIO_DEVICE_SET_IRQ_EVENTFDSto set new eventfds? Or does it re-use the eventfds and the device is enabled after this?
Quoted 4 lines
Quoted 1 line
It doesn't affect the interrupt programming. Should it?
It should probably clear any currently pending interrupts, as if the unmask IOCTL were called.
Sounds reasonable.
Quoted 23 lines
Quoted 9 lines
Quoted 7 lines
Documentation/vfio.txt line 229
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+device tree properties of the device: + +struct vfio_dtpath { + __u32 len; /* length of structure */ + __u32 index;0 based I presume?
Everything else is, I would assume so/
Yes, it should be zero-based -- this matches how such indices are done in the kernel device tree APIs.
Quoted 9 lines
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ __u64 flags; +#define VFIO_DTPATH_FLAGS_REGION (1 << 0)What is region in this context?? Or would this make much more sense if I knew what Device Tree actually is.
Powerpc guys, any comments? This was their suggestion. These are effectively the first device specific extension, available when
VFIO_DEVICE_FLAGS_DTis set.
Quoted 5 lines
An assigned device may consist of an entire subtree of the device tree, and both register banks and interrupts can come from any node in the tree. Region versus IRQ here indicates the context in which to interpret index, in order to retrieve the path of the node that supplied this particular region or IRQ.
Ok. Thanks for the clarification. We'll wait for the vfio-dt bus driver before actually including this.
Quoted 15 lines
Quoted 9 lines
Quoted 9 lines
Documentation/vfio.txt line 240
Quoted 7 lines
Documentation/vfio.txt new file +7
-0+}; +#define VFIO_DEVICE_GET_DTPATH _IOWR(';', 117, struct vfio_dtpath) + +struct vfio_dtindex { + __u32 len; /* length of structure */ + __u32 index; + __u32 prop_type;Is that an enum type? Is this definied somewhere?
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
+
__u32prop_index;What is the purpose of this field?
Need input from powerpc folks here
Quoted 8 lines
To identify what this resource (register bank or IRQ) this is, we need both the path to the node and the index into the reg or interrupts property within the node.
We also need to distinguish reg from ranges, and interrupts from interrupt-map. As you suggested elsewhere in the thread, the device tree API should probably be left out for now, and added later along with the device tree "bus" driver.
Yep, I'll do that.
Quoted 32 lines
Quoted 13 lines
Quoted 8 lines
drivers/vfio/vfio_main.c line 157
Quoted 4 lines
drivers/vfio/vfio_main.c new file +4
-0+static void __vfio_iommu_detach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +BUG_ON (!iommu->domain && device->attached);Whoa. Heavy hammer there.
Perhaps
WARN_ONas you do check it later on.I think it's warranted, internal consistency is broken if we have a device that thinks it's attached to an iommu domain that doesn't exist. It should, of course, never happen and this isn't a performance path.
[snip]
Quoted 9 lines
Quoted 9 lines
drivers/vfio/vfio_main.c line 184
Quoted 6 lines
drivers/vfio/vfio_main.c new file +6
-0+static int __vfio_iommu_attach_dev (struct vfio_iommu *iommu, +struct vfio_device *device) +{ +int ret; + +BUG_ON (device->attached);How about:
Quoted 8 lines
Quoted 2 lines
WARN_ON(device->attached, "The engineer who wrote the user-space device driver is trying to register the device again! Tell him/her to stop please.\n");
I would almost demote this one to a
WARN_ON, but userspace isn't in control of attaching and detaching devices from the iommu. That's a side effect of getting the iommu or device file descriptor. So again, this is an internal consistency check and it should never happen, regardless of userspace.
Quoted 5 lines
The rule isn't to use BUG for internal consistency checks and WARN for stuff userspace can trigger, but rather to use BUG if you cannot reasonably continue, WARN for "significant issues that need prompt attention" that are reasonably recoverable. Most instances of WARN are internal consistency checks.
That makes sense.
Quoted 4 lines
From
include/asm-generic/bug.h:Quoted 3 lines
If you're tempted to
BUG(), think again: is completely giving up really the *only* solution? There are usually better options, where users don't need to reboot ASAP and can mostly shut down cleanly.
Ok, I'll make a cleanup pass of demoting BUG_ONs to WARN_ONs. Thanks,
Alex
Documentation/vfio.txt
Quoted 5 lines · Alex Williamson
Documentation/vfio.txt new file +5
-0+Many modern system now provide DMA and interrupt remapping facilities +to help ensure I/O devices behave within the boundaries they've been +allotted. This includes x86 hardware with AMD-Vi and Intel VT-d as +well as POWER systems with Partitionable Endpoints (PEs) and even +embedded powerpc systems (technology name unknown).
Maybe replace "(technology name unknown)" with "(such as Freescale chips with PAMU)" or similar?
Or just leave out the parenthetical.
Documentation/vfio.txt line 72
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+As documented in linux/vfio.h, several ioctls are provided on the +group chardev: + +#define VFIO_GROUP_GET_FLAGS _IOR(';', 100, __u64) + #define VFIO_GROUP_FLAGS_VIABLE (1 << 0) + #define VFIO_GROUP_FLAGS_MM_LOCKED (1 << 1) +#define VFIO_GROUP_MERGE _IOW(';', 101, int) +#define VFIO_GROUP_UNMERGE _IOW(';', 102, int) +#define VFIO_GROUP_GET_IOMMU_FD _IO(';', 103) +#define VFIO_GROUP_GET_DEVICE_FD _IOW(';', 104, char *)
This suggests the argument to VFIO_GROUP_GET_DEVICE_FD is a pointer to a pointer to char rather than a pointer to an array of char (just as e.g. VFIO_GROUP_MERGE takes a pointer to an int, not just an int).
Documentation/vfio.txt line 110
Quoted 6 lines
Documentation/vfio.txt new file +6
-0+The IOMMU file descriptor provides this set of ioctls: + +#define VFIO_IOMMU_GET_FLAGS _IOR(';', 105, __u64) + #define VFIO_IOMMU_FLAGS_MAP_ANY (1 << 0) +#define VFIO_IOMMU_MAP_DMA _IOWR(';', 106, struct vfio_dma_map) +#define VFIO_IOMMU_UNMAP_DMA _IOWR(';', 107, struct vfio_dma_map)
What is the implication if VFIO_IOMMU_FLAGS_MAP_ANY is clear? Is such an implementation supposed to add a new flag that describes its restrictions?
Can we get a way to turn DMA access off and on, short of unmapping everything, and then mapping it again?
Quoted 5 lines
Documentation/vfio.txt new file +5
-0+The GET_FLAGS ioctl returns basic information about the IOMMU domain. +We currently only support IOMMU domains that are able to map any +virtual address to any IOVA. This is indicated by the MAP_ANY flag. + +The (UN)MAP_DMA commands make use of struct vfio_dma_map for mapping
Documentation/vfio.txt line 126
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+and unmapping IOVAs to process virtual addresses: + +struct vfio_dma_map { + __u64 len; /* length of structure */ + __u64 vaddr; /* process virtual addr */ + __u64 dmaaddr; /* desired and/or returned dma address */ + __u64 size; /* size in bytes */ + __u64 flags; +#define VFIO_DMA_MAP_FLAG_WRITE (1 << 0) /* req writeable DMA mem */ +};
What are the semantics of "desired and/or returned dma address"?
Are we always supposed to provide a desired address, but it may be different on return? Or are there cases where we want to say "give me whatever you want" or "give me this or fail"?
How much of this needs to be filled out for unmap?
Note that the "length of structure" approach means that ioctl numbers will change whenever this grows -- perhaps we should avoid encoding the struct size into these ioctls?
Quoted 20 lines
Documentation/vfio.txt new file +20
-0+struct vfio_region_info { + __u32 len; /* length of structure */ + __u32 index; /* region number */ + __u64 size; /* size in bytes of region */ + __u64 offset; /* start offset of region */ + __u64 flags; +#define VFIO_REGION_INFO_FLAG_MMAP (1 << 0) +#define VFIO_REGION_INFO_FLAG_RO (1 << 1) +#define VFIO_REGION_INFO_FLAG_PHYS_VALID (1 << 2) + __u64 phys; /* physical address of region */ +}; + +#define VFIO_DEVICE_GET_REGION_INFO _IOWR(';', 110, struct vfio_region_info) + +The offset indicates the offset into the device file descriptor which +accesses the given range (for read/write/mmap/seek). Flags indicate the +available access types and validity of optional fields. For instance +the phys field may only be valid for certain devices types. + +Interrupts are described using a similar interface. GET_NUM_IRQS
Documentation/vfio.txt line 184
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+reports the number or IRQ indexes for the device. + +#define VFIO_DEVICE_GET_NUM_IRQS _IOR(';', 111, int) + +struct vfio_irq_info { + __u32 len; /* length of structure */ + __u32 index; /* IRQ number */ + __u32 count; /* number of individual IRQs */ + __u64 flags; +#define VFIO_IRQ_INFO_FLAG_LEVEL (1 << 0)
Make sure flags is 64-bit aligned -- some 32-bit ABIs, such as x86, will not do this, causing problems if the kernel is 64-bit and thus assumes a different layout.
Quoted 8 lines
Documentation/vfio.txt new file +8
-0+Information about each index can be retrieved using the GET_IRQ_INFO +ioctl, used much like GET_REGION_INFO. + +#define VFIO_DEVICE_GET_IRQ_INFO _IOWR(';', 112, struct vfio_irq_info) + +Individual indexes can describe single or sets of IRQs. This provides the +flexibility to describe PCI INTx, MSI, and MSI-X using a single interface. +
Documentation/vfio.txt
Quoted 10 lines
Documentation/vfio.txt new file +10
-0+All VFIO interrupts are signaled to userspace via eventfds. Integer arrays, +as shown below, are used to pass the IRQ info index, the number of eventfds, +and each eventfd to be signaled. Using a count of 0 disables the interrupt. + +/* Set IRQ eventfds, arg[0] = index, arg[1] = count, arg[2-n] = eventfds */ +#define VFIO_DEVICE_SET_IRQ_EVENTFDS _IOW(';', 113, int) + +When a level triggered interrupt is signaled, the interrupt is masked +on the host. This prevents an unresponsive userspace driver from +continuing to interrupt the host system.
It's usually necessary even in the case of responsive userspace, just to get to the point where userspace can execute (ignoring cases where userspace runs on one core while the interrupt storms another).
For edge interrupts, will me mask if an interrupt comes in and the previous interrupt hasn't been read out yet (and then unmask when the last interrupt gets read out), to isolate us from a rapidly firing interrupt source that userspace can't keep up with?
Documentation/vfio.txt line 235
Quoted 12 lines
Documentation/vfio.txt new file +12
-0+Device tree devices also invlude ioctls for further defining the +device tree properties of the device: + +struct vfio_dtpath { + __u32 len; /* length of structure */ + __u32 index; + __u64 flags; +#define VFIO_DTPATH_FLAGS_REGION (1 << 0) +#define VFIO_DTPATH_FLAGS_IRQ (1 << 1) + char *path; +}; +#define VFIO_DEVICE_GET_DTPATH _IOWR(';', 117, struct vfio_dtpath)
Where is length of buffer (and description of associated semantics)?
Review
Quoted 2 lines
(quoted diff) +2
-0+struct vfio_device_ops { + bool (*match)(struct device *, char *);
const char *?
Documentation/vfio.txt line 276
Quoted 9 lines
Documentation/vfio.txt new file +9
-0+ int (*get)(void *); + void (*put)(void *); + ssize_t (*read)(void *, char __user *, + size_t, loff_t *); + ssize_t (*write)(void *, const char __user *, + size_t, loff_t *); + long (*ioctl)(void *, unsigned int, unsigned long); + int (*mmap)(void *, struct vm_area_struct *); +};
When defining an API, please do not omit parameter names.
Should specify what the driver is supposed to do with get/put -- I guess not try to unbind when the count is nonzero? Races could still lead the unbinder to be blocked, but I guess it lets the driver know when it's likely to succeed.
Quoted 6 lines
drivers/vfio/Kconfig new file +0
-0
drivers/vfio/Kconfig line 8
Quoted 8 lines
drivers/vfio/Kconfig new file +8
-0@@ -0,0 +1,8 @@ +menuconfig VFIO + tristate "VFIO Non-Privileged userspace driver framework" + depends on IOMMU_API + help + VFIO provides a framework for secure userspace device drivers. + See Documentation/vfio.txt for more details. + + If you don't know what to do here, say N.
Can we limit the IOMMU_API dependency to the IOMMU parts of VFIO? It would still be useful for devices which don't do DMA, or where we accept the lack of protection/translation (e.g. we have a customer that wants to do KVM device assignment on one of our lower-end chips that lacks an IOMMU).
drivers/vfio/Kconfig
Quoted 7 lines
drivers/vfio/Kconfig new file +7
-0+struct dma_map_page { + struct list_head list; + dma_addr_t daddr; + unsigned long vaddr; + int npage; + int rdwr; +};
npage should be long.
What is "rdwr"? non-zero for write? non-zero for read? :-) is_write would be a better name.
Quoted 12 lines
drivers/vfio/Kconfig new file +12
-0+ for (i = 0; i < npage; i++, iova += PAGE_SIZE, vaddr += PAGE_SIZE) { + unsigned long pfn = 0; + + ret = vaddr_get_pfn(vaddr, rdwr, &pfn); + if (ret) { + __vfio_dma_unmap(iommu, start, i, rdwr); + return ret; + } + + /* Only add actual locked pages to accounting */ + if (!is_invalid_reserved_pfn(pfn)) + locked++;
drivers/vfio/Kconfig
Quoted 10 lines
drivers/vfio/Kconfig new file +10
-0+ + ret = iommu_map(iommu->domain, iova, + (phys_addr_t)pfn << PAGE_SHIFT, 0, prot); + if (ret) { + /* Back out mappings on error */ + put_pfn(pfn, rdwr); + __vfio_dma_unmap(iommu, start, i, rdwr); + return ret; + } + }
There's no way to hand this stuff to the IOMMU driver in chunks larger than a page? That's going to be a problem for our IOMMU, which wants to deal with large windows.
drivers/vfio/Kconfig
Quoted 9 lines
drivers/vfio/Kconfig new file +9
-0+ vfio_lock_acct(locked); + return 0; +} + +static inline int ranges_overlap(unsigned long start1, size_t size1, + unsigned long start2, size_t size2) +{ + return !(start1 + size1 <= start2 || start2 + size2 <= start1); +}
You pass DMA addresses to this, so use dma_addr_t. unsigned long is not always large enough.
What if one of the ranges wraps around (including the legitimate possibility of start + size == 0)?
drivers/vfio/Kconfig
Quoted 5 lines
drivers/vfio/Kconfig new file +5
-0+static long vfio_iommu_unl_ioctl(struct file *filep, + unsigned int cmd, unsigned long arg) +{ + struct vfio_iommu *iommu = filep->private_data; + int ret = -ENOSYS;
-ENOIOCTLCMD or -ENOTTY?
drivers/vfio/Kconfig
Quoted 8 lines
drivers/vfio/Kconfig new file +8
-0+ + if (cmd == VFIO_IOMMU_GET_FLAGS) { + u64 flags = VFIO_IOMMU_FLAGS_MAP_ANY; + + ret = put_user(flags, (u64 __user *)arg); + + } else if (cmd == VFIO_IOMMU_MAP_DMA) { + struct vfio_dma_map dm;
Whitespace.
Any reason not to use a switch?
Quoted 2 lines
drivers/vfio/Kconfig new file +2
-0+/* Return true if any devices within a group are opened */ +static bool __vfio_group_devs_inuse(struct vfio_group *group)
[snip]
Quoted 1 line
+static bool
__vfio_iommu_groups_inuse(struct vfio_iommu *iommu)
[snip]
Quoted 1 line
+static bool
__vfio_iommu_inuse(struct vfio_iommu *iommu)
[snip]
drivers/vfio/Kconfig
Quoted 2 lines
drivers/vfio/Kconfig new file +2
-0+static void __vfio_group_set_iommu(struct vfio_group *group, + struct vfio_iommu *iommu)
...and so on.
Why all the leading underscores? Doesn't look like you're trying to distinguish between this and a more public version with the same name.
Quoted 22 lines
drivers/vfio/Kconfig new file +22
-0+/* Get a new device file descriptor. This will open the iommu, setting + * the current->mm ownership if it's not already set. It's difficult to + * specify the requirements for matching a user supplied buffer to a + * device, so we use a vfio driver callback to test for a match. For + * PCI, dev_name(dev) is unique, but other drivers may require including + * a parent device string. */ +static int vfio_group_get_device_fd(struct vfio_group *group, char *buf) +{ + struct vfio_iommu *iommu = group->iommu; + struct list_head *gpos; + int ret = -ENODEV; + + mutex_lock(&vfio.lock); + + if (!iommu->domain) { + ret = __vfio_open_iommu(iommu); + if (ret) + goto out; + } + + list_for_each(gpos, &iommu->group_list) { + struct list_head *dpos;
drivers/vfio/Kconfig
Quoted 10 lines
drivers/vfio/Kconfig new file +10
-0+ + group = list_entry(gpos, struct vfio_group, iommu_next); + + list_for_each(dpos, &group->device_list) { + struct vfio_device *device; + + device = list_entry(dpos, + struct vfio_device, device_next); + + if (device->ops->match(device->dev, buf)) {
If there's a match, we're done with the loop -- might as well break out now rather than indent everything else.
drivers/vfio/Kconfig
Quoted 6 lines
drivers/vfio/Kconfig new file +6
-0+ struct file *file; + + if (device->ops->get(device->device_data)) { + ret = -EFAULT; + goto out; + }
Why does a failure of get() result in -EFAULT? -EFAULT is for bad user addresses.
Quoted 9 lines
drivers/vfio/Kconfig new file +9
-0+ + /* We can't use anon_inode_getfd(), like above + * because we need to modify the f_mode flags + * directly to allow more than just ioctls */ + ret = get_unused_fd(); + if (ret < 0) { + device->ops->put(device->device_data); + goto out; + }
drivers/vfio/Kconfig
Quoted 10 lines
drivers/vfio/Kconfig new file +10
-0+ + file = anon_inode_getfile("[vfio-device]", + &vfio_device_fops, + device, O_RDWR); + if (IS_ERR(file)) { + put_unused_fd(ret); + ret = PTR_ERR(file); + device->ops->put(device->device_data); + goto out; + }
Maybe cleaner with goto-based error management?
Quoted 15 lines
drivers/vfio/Kconfig new file +15
-0+/* Add a new device to the vfio framework with associated vfio driver + * callbacks. This is the entry point for vfio drivers to register devices. */ +int vfio_group_add_dev(struct device *dev, const struct vfio_device_ops *ops) +{ + struct list_head *pos; + struct vfio_group *group = NULL; + struct vfio_device *device = NULL; + unsigned int groupid; + int ret = 0; + bool new_group = false; + + if (!ops) + return -EINVAL; + + if (iommu_device_group(dev, &groupid))
drivers/vfio/Kconfig
Quoted 10 lines
drivers/vfio/Kconfig new file +10
-0+ return -ENODEV; + + mutex_lock(&vfio.lock); + + list_for_each(pos, &vfio.group_list) { + group = list_entry(pos, struct vfio_group, group_next); + if (group->groupid == groupid) + break; + group = NULL; + }
Factor this into vfio_dev_to_group() (and likewise for other such lookups)?
Quoted 21 lines
drivers/vfio/Kconfig new file +21
-0+ if (!group) { + int minor; + + if (unlikely(idr_pre_get(&vfio.idr, GFP_KERNEL) == 0)) { + ret = -ENOMEM; + goto out; + } + + group = kzalloc(sizeof(*group), GFP_KERNEL); + if (!group) { + ret = -ENOMEM; + goto out; + } + + group->groupid = groupid; + INIT_LIST_HEAD(&group->device_list); + + ret = idr_get_new(&vfio.idr, group, &minor); + if (ret == 0 && minor > MINORMASK) { + idr_remove(&vfio.idr, minor); + kfree(group);
drivers/vfio/Kconfig
Quoted 10 lines
drivers/vfio/Kconfig new file +10
-0+ ret = -ENOSPC; + goto out; + } + + group->devt = MKDEV(MAJOR(vfio.devt), minor); + device_create(vfio.class, NULL, group->devt, + group, "%u", groupid); + + group->bus = dev->bus; + list_add(&group->group_next, &vfio.group_list);
Factor out into vfio_create_group()?
drivers/vfio/Kconfig
Quoted 10 lines
drivers/vfio/Kconfig new file +10
-0+ new_group = true; + } else { + if (group->bus != dev->bus) { + printk(KERN_WARNING + "Error: IOMMU group ID conflict. Group ID %u " + "on both bus %s and %s\n", groupid, + group->bus->name, dev->bus->name); + ret = -EFAULT; + goto out; + }
It took me a little while to figure out that this was comparing bus types, not actual bus instances (which would be an inappropriate restriction). :-P
Still, isn't it what we really care about that it's the same IOMMU domain? Couldn't different bus types share an iommu_ops?
And again, -EFAULT isn't the right error.
-Scott