VFIO support for platform devices
Subsystems (5)
v5 cover VFIO support for platform devices
This patch series aims to implement VFIO support for platform devices that reside behind an IOMMU. Examples of such devices are devices behind an ARM SMMU, or behind a Samsung Exynos System MMU.
This v5 of the VFIO_PLATFORN patch series includes a number of fixes and improvements over the features already implemented in the series. The next version v6 of the VFIO_PLATFORM patch series is planned to introduce more complete functionality with more comprehensive eventfd support.
The API used is based on the existing VFIO API that is also used with PCI devices. Only devices that include a basic set of IRQs and memory regions are targeted; devices with complex relationships with other devices on a device tree are not taken into account at this stage.
A copy with all the dependencies applied can be cloned from branch
vfio-platform-v5 at git-9UaJU3cA/F/[email protected]:virtualopensystems/linux-kvm-arm.git
For those who want to apply manually, these patches are based on Alex Williamson's 'next' branch available from https://github.com/awilliam/linux-vfio
which includes the multi domain support patches for VFIO_IOMMU_TYPE1. Kim Phillip's driver_override patch for platform bus devices is also included here for convenience.
The following IOCTLs have been found to be working on FastModels with an ARM SMMU (MMU400). Testing was based on the ARM PL330 DMA Controller featured on those models.
- VFIO_GET_API_VERSION
- VFIO_CHECK_EXTENSION
- VFIO_GROUP_GET_STATUS
- VFIO_GROUP_SET_CONTAINER
- VFIO_SET_IOMMU
- VFIO_IOMMU_GET_INFO
- VFIO_IOMMU_MAP_DMA
For this ioctl specifically, a new flag has been added:
VFIO_DMA_MAP_FLAG_EXEC. This flag is taken into account on systems with an ARM SMMU. The availability of this flag is exposed via the capability VFIO_IOMMU_PROT_EXEC.
The VFIO platform driver proposed here implements the following:
- VFIO_GROUP_GET_DEVICE_FD
- VFIO_DEVICE_GET_INFO
- VFIO_DEVICE_GET_REGION_INFO
- VFIO_DEVICE_GET_IRQ_INFO
- VFIO_DEVICE_SET_IRQS
IRQs are implemented partially using this ioctl. Handling incoming interrupts with an eventfd is supported, as is masking and unmasking. Level sensitive interrupts are automasked. What is not implemented is masking/unmasking via eventfd. More comprehensive support for eventfd functionality is planned for v6.
In addition, the VFIO platform driver implements the following through the VFIO device file descriptor:
- MMAPing memory regions to the virtual address space of the VFIO user.
- Read / write of memory regions directly through the file descriptor.
What still needs to be done, includes:
- Eventfd for masking/unmasking (IRQFD and IOEVENTFD from the KVM side)
- Extend the driver and API for device tree metadata
- Support ARM AMBA devices natively
- Device specific functionality (e.g. VFIO_DEVICE_RESET)
- IOMMUs with nested page tables (Stage 1 & 2 translation on ARM SMMUs)
Changes since v4:
- Use static offsets for each region in the VFIO device fd
- Include patch in the series for the ARM SMMU to expose IOMMU_EXEC availability via IOMMU_CAP_DMA_EXEC
- Rebased on VFIO multi domain support:
- IOMMU_EXEC is now available if at least one IOMMU in the container supports it
- Expose IOMMU_EXEC if available via the capability VFIO_IOMMU_PROT_EXEC
- Some bug fixes
Changes since v3:
- Use Kim Phillips' driver_probe_device()
Changes since v2:
- Fixed Read/Write and MMAP on device regions
- Removed dependency on Device Tree
- Interrupts support
- Interrupt masking/unmasking
- Automask level sensitive interrupts
- Introduced VFIO_DMA_MAP_FLAG_EXEC
- Code clean ups
Antonios Motakis (10):
ARM SMMU: Add capability IOMMU_CAP_DMA_EXEC VFIO_IOMMU_TYPE1 for platform bus devices on ARM
VFIO_IOMMU_TYPE1: Introduce the VFIO_DMA_MAP_FLAG_EXEC flag
VFIO_PLATFORM: Initial skeleton of VFIO support for platform devices
VFIO_PLATFORM: Return info for device and its memory mapped IO regions
VFIO_PLATFORM: Read and write support for the device fd
VFIO_PLATFORM: Support MMAP of MMIO regions
VFIO_PLATFORM: Return IRQ info
VFIO_PLATFORM: Initial interrupts support
VFIO_PLATFORM: Support for maskable and automasked interrupts
Kim Phillips (1):
driver core: platform: add device binding path 'driver_override'
Documentation/ABI/testing/sysfs-bus-platform | 20 ++ drivers/base/platform.c | 46 +++ drivers/iommu/arm-smmu.c | 2 + drivers/vfio/Kconfig | 3 +- drivers/vfio/Makefile | 1 + drivers/vfio/platform/Kconfig | 9 + drivers/vfio/platform/Makefile | 4 + drivers/vfio/platform/vfio_platform.c | 436 ++++++++++++++++++++++++++ drivers/vfio/platform/vfio_platform_irq.c | 289 +++++++++++++++++ drivers/vfio/platform/vfio_platform_private.h | 59 ++++ drivers/vfio/vfio_iommu_type1.c | 39 ++- include/linux/iommu.h | 5 +- include/linux/platform_device.h | 1 + include/uapi/linux/vfio.h | 3 + 14 files changed, 910 insertions(+), 7 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-platform create mode 100644 drivers/vfio/platform/Kconfig create mode 100644 drivers/vfio/platform/Makefile create mode 100644 drivers/vfio/platform/vfio_platform.c create mode 100644 drivers/vfio/platform/vfio_platform_irq.c create mode 100644 drivers/vfio/platform/vfio_platform_private.h
Signature · 1 line
1.8.3.2
RFC PATCH v5 1/11 driver core: platform: add device binding path 'driver_override'
From: Kim Phillips <[email protected]>
Needed by platform device drivers, such as the vfio-platform driver [1], in order to bypass the existing OF, ACPI, id_table and name string matches, and successfully be able to be bound to any device, like so:
echo vfio-platform > /sys/bus/platform/devices/fff51000.ethernet/driver_override echo fff51000.ethernet > /sys/bus/platform/devices/fff51000.ethernet/driver/unbind echo fff51000.ethernet > /sys/bus/platform/drivers_probe
This mimics "PCI: Introduce new device binding path using pci_dev.driver_override" [2], which is an interface enhancement for more deterministic PCI device binding, e.g., when in the presence of hotplug.
[1] http://lkml.iu.edu/hypermail/linux/kernel/1402.1/00177.html
[2] http://lists-archives.com/linux-kernel/28030441-pci-introduce-new-device-binding-path-using-pci_dev-driver_override.html
--- Documentation/ABI/testing/sysfs-bus-platform | 20 ++++++++++++ drivers/base/platform.c | 46 ++++++++++++++++++++++++++++ include/linux/platform_device.h | 1 + 3 files changed, 67 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-platform
Documentation/ABI/testing/sysfs-bus-platform new file +20 -0
@@ -0,0 +1,20 @@ +What: /sys/bus/platform/devices/.../driver_override +Date: April 2014 +Contact: Kim Phillips <[email protected]> +Description: + This file allows the driver for a device to be specified which + will override standard OF, ACPI, ID table, and name matching. + When specified, only a driver with a name matching the value + written to driver_override will have an opportunity to bind + to the device. The override is specified by writing a string + to the driver_override file (echo vfio-platform > \ + driver_override) and may be cleared with an empty string + (echo > driver_override). This returns the device to standard + matching rules binding. Writing to driver_override does not + automatically unbind the device from its current driver or make + any attempt to automatically load the specified driver. If no + driver with a matching name is currently loaded in the kernel, + the device will not bind to any driver. This also allows + devices to opt-out of driver binding using a driver_override + name such as "none". Only a single driver may be specified in + the override, there is no support for parsing delimiters.
drivers/base/platform.c +46 -0
66 lines not on this page · raw message
include/linux/platform_device.h +1 -0
@@ -28,6 +28,7 @@ struct platform_device {struct resource *resource;const struct platform_device_id *id_entry; +char *driver_override;/* Driver name to force a match */ /* MFD cell pointer */ struct mfd_cell *mfd_cell;
Signature · 1 line
1.8.3.2
RFC PATCH 1/11 driver core: platform: add device binding path 'driver_override'
From: Kim Phillips <[email protected]>
Needed by platform device drivers, such as the vfio-platform driver later in series, in order to bypass the existing OF, ACPI, id_table and name string matches, and successfully be able to be bound to any device, like so:
echo vfio-platform > /sys/bus/platform/devices/fff51000.ethernet/driver_override echo fff51000.ethernet > /sys/bus/platform/devices/fff51000.ethernet/driver/unbind echo fff51000.ethernet > /sys/bus/platform/drivers_probe
This mimics "PCI: Introduce new device binding path using pci_dev.driver_override" [1], which is an interface enhancement for more deterministic PCI device binding, e.g., when in the presence of hotplug.
[1] https://lists.cs.columbia.edu/pipermail/kvmarm/2014-May/009527.html
---
changes in v2 patch of v5 of this patchseries:
- rebased onto today's Linus' ToT
- added kfree to match PCI counterpart fix, as Alex Williamson just posted a v3 of the patch (thanks Christoffer for the notification)
- in the commit text, replaced vfio platform driver reference with 'later in series', and updated the PCI version mailing list reference to the v3 version.
Is it safe to assume this patch will continue to as part of the VFIO platform driver patchseries, and be submitted by Antonis? If so, can
we start collecting some {Reviewed,Acked}-bys? Thanks, Kim.
Documentation/ABI/testing/sysfs-bus-platform | 20 ++++++++++++ drivers/base/platform.c | 47 ++++++++++++++++++++++++++++ include/linux/platform_device.h | 1 + 3 files changed, 68 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-platform
Documentation/ABI/testing/sysfs-bus-platform new file +20 -0
@@ -0,0 +1,20 @@ +What: /sys/bus/platform/devices/.../driver_override +Date: April 2014 +Contact: Kim Phillips <[email protected]> +Description: + This file allows the driver for a device to be specified which + will override standard OF, ACPI, ID table, and name matching. + When specified, only a driver with a name matching the value + written to driver_override will have an opportunity to bind + to the device. The override is specified by writing a string + to the driver_override file (echo vfio-platform > \ + driver_override) and may be cleared with an empty string + (echo > driver_override). This returns the device to standard + matching rules binding. Writing to driver_override does not + automatically unbind the device from its current driver or make + any attempt to automatically load the specified driver. If no + driver with a matching name is currently loaded in the kernel, + the device will not bind to any driver. This also allows + devices to opt-out of driver binding using a driver_override + name such as "none". Only a single driver may be specified in + the override, there is no support for parsing delimiters.
drivers/base/platform.c +47 -0
73 lines not on this page · raw message
include/linux/platform_device.h +1 -0
@@ -28,6 +28,7 @@ struct platform_device {struct resource *resource;const struct platform_device_id *id_entry; +char *driver_override;/* Driver name to force a match */ /* MFD cell pointer */ struct mfd_cell *mfd_cell;
Signature · 1 line
1.9.3
Re: [RFC PATCH v5_v2 01/11] driver core: platform: add device binding path 'driver_override'
Quoted 16 lines · Kim Phillips
From: Kim Phillips <[email protected]>
Needed by platform device drivers, such as the vfio-platform driver later in series, in order to bypass the existing OF, ACPI,
id_tableand name string matches, and successfully be able to be bound to any device, like so:echo vfio-platform >
/sys/bus/platform/devices/fff51000.ethernet/driver_overrideecho fff51000.ethernet >/sys/bus/platform/devices/fff51000.ethernet/driver/unbindecho fff51000.ethernet >/sys/bus/platform/drivers_probeThis mimics "PCI: Introduce new device binding path using pci_dev.driver_override" [1], which is an interface enhancement for more deterministic PCI device binding, e.g., when in the presence of hotplug.
Quoted 4 lines
[1] https://lists.cs.columbia.edu/pipermail/kvmarm/2014-May/009527.html
Suggested-by Alex Williamson <[email protected]>Signed-off-by Kim Phillips <[email protected]>
Looks largely identical to the PCI version of the same that has been accepted for v3.16 and ack'd by GregKH.
Quoted 139 lines
---
changes in v2 patch of v5 of this patchseries:
- rebased onto today's Linus' ToT
- added kfree to match PCI counterpart fix, as Alex Williamson just posted a v3 of the patch (thanks Christoffer for the notification)
- in the commit text, replaced vfio platform driver reference with 'later in series', and updated the PCI version mailing list reference to the v3 version.Is it safe to assume this patch will continue to as part of the VFIO platform driver patchseries, and be submitted by Antonis? If so, can
we start collecting some {Reviewed,Acked}-bys? Thanks, Kim.Documentation/ABI/testing/sysfs-bus-platform | 20 ++++++++++++ drivers/base/platform.c | 47 ++++++++++++++++++++++++++++ include/linux/platform_device.h | 1 + 3 files changed, 68 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-platformDocumentation/ABI/testing/sysfs-bus-platform new file +20
-0@@ -0,0 +1,20 @@ +What: /sys/bus/platform/devices/.../driver_override +Date: April 2014 +Contact: Kim Phillips <[email protected]> +Description: + This file allows the driver for a device to be specified which + will override standard OF, ACPI, ID table, and name matching. + When specified, only a driver with a name matching the value + written to driver_override will have an opportunity to bind + to the device. The override is specified by writing a string + to the driver_override file (echo vfio-platform > \ + driver_override) and may be cleared with an empty string + (echo > driver_override). This returns the device to standard + matching rules binding. Writing to driver_override does not + automatically unbind the device from its current driver or make + any attempt to automatically load the specified driver. If no + driver with a matching name is currently loaded in the kernel, + the device will not bind to any driver. This also allows + devices to opt-out of driver binding using a driver_override + name such as "none". Only a single driver may be specified in + the override, there is no support for parsing delimiters.drivers/base/platform.c +47
-0@@ -23,6 +23,7 @@#include <linux/pm_runtime.h> #include <linux/idr.h> #include <linux/acpi.h> +#include <linux/limits.h> #include "base.h" #include "power/power.h" @@ -188,6 +189,7 @@ static void platform_device_release(struct device *dev)kfree (pa->pdev.dev.platform_data);kfree (pa->pdev.mfd_cell);kfree (pa->pdev.resource); +kfree (pa->pdev.driver_override);kfree (pa); }@@ -695,8 +697,49 @@ static ssize_t modalias_show(struct device *dev, struct device_attribute *a, }static DEVICE_ATTR_RO (modalias); +static ssize_t driver_override_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct platform_device *pdev =to_platform_device (dev); +char *driver_override, *old = pdev->driver_override, *cp; + +if (count >PATH_MAX ) +return -EINVAL ; + + driver_override =kstrndup (buf, count,GFP_KERNEL ); +if (!driver_override) +return -ENOMEM ; + + cp =strchr (driver_override,'\n' ); +if (cp) + *cp ='\0' ; + +if (strlen (driver_override)) { + pdev->driver_override = driver_override; + }else { +kfree (driver_override); + pdev->driver_override =NULL ; + } + +kfree (old); + +return count; +} + +static ssize_t driver_override_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct platform_device *pdev =to_platform_device (dev); + +return sprintf (buf,"%s\n" , pdev->driver_override); +} +static DEVICE_ATTR_RW (driver_override); + +static struct attribute *platform_dev_attrs[] = { &dev_attr_modalias.attr, + &dev_attr_driver_override.attr,NULL , };ATTRIBUTE_GROUPS (platform_dev);@@ -752,6 +795,10 @@ static int platform_match(struct device *dev, struct device_driver *drv)struct platform_device *pdev =to_platform_device (dev);struct platform_driver *pdrv =to_platform_driver (drv); +/* When driver_override is set, only bind to the matching driver */ +if (pdev->driver_override) +return !strcmp (pdev->driver_override, drv->name); +/* Attempt an OF style match first */ if (of_driver_match_device (dev, drv))return 1 ;include/linux/platform_device.h +1
-0@@ -28,6 +28,7 @@ struct platform_device {struct resource *resource;const struct platform_device_id *id_entry; +char *driver_override;/* Driver name to force a match */ /* MFD cell pointer */ struct mfd_cell *mfd_cell;
Alexander Graf
Reviewed-byRe: [RFC PATCH v5_v2 01/11] driver core: platform: add device binding path 'driver_override'
Quoted 17 lines · Alex Williamson
Quoted 16 lines · Kim Phillips
From: Kim Phillips <[email protected]>
Needed by platform device drivers, such as the vfio-platform driver later in series, in order to bypass the existing OF, ACPI,
id_tableand name string matches, and successfully be able to be bound to any device, like so:echo vfio-platform >
/sys/bus/platform/devices/fff51000.ethernet/driver_overrideecho fff51000.ethernet >/sys/bus/platform/devices/fff51000.ethernet/driver/unbindecho fff51000.ethernet >/sys/bus/platform/drivers_probeThis mimics "PCI: Introduce new device binding path using pci_dev.driver_override" [1], which is an interface enhancement for more deterministic PCI device binding, e.g., when in the presence of hotplug.
Quoted 8 lines
Quoted 4 lines
[1] https://lists.cs.columbia.edu/pipermail/kvmarm/2014-May/009527.html
Suggested-by Alex Williamson <[email protected]>Signed-off-by Kim Phillips <[email protected]>Looks largely identical to the PCI version of the same that has been accepted for v3.16 and ack'd by GregKH.
Reviewed-by Alex Williamson <[email protected]>
Yup, would be great to have feature parity for device binding on platform and PCI.
Alex
A message that is not in the archive (sent off-list or not yet received): 20140520192537.bab9fa2088c1cd5da3f92639-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org
Stuart Yoder
Reviewed-byRE: [RFC PATCH v5_v2 01/11] driver core: platform: add device binding path 'driver_override'
Quoted 39 lines
-----Original Message-----
From: Kim Phillips [mailto:[email protected]]
Sent: Tuesday, May 20, 2014 7:26 PM
To: a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]
Cc: [email protected]; [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; a.rigo-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]; rob.herring-bsGFqQB8/[email protected]; Yoder Stuart- B08248; rob-VoJi6FS/[email protected]; [email protected]; tech-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]; [email protected]; [email protected]; [email protected]; Julien Grall; Ian Campbell; Stefano Stabellini; Alexander Graf
Subject: [RFC PATCHv5_v201/11] driver core: platform: add device
binding path 'driver_override'From: Kim Phillips <[email protected]>
Needed by platform device drivers, such as the vfio-platform driver later in series, in order to bypass the existing OF, ACPI,
id_tableand name string matches, and successfully be able to be bound to any device, like so:echo vfio-platform >
/sys/bus/platform/devices/fff51000.ethernet/driver_overrideecho fff51000.ethernet >/sys/bus/platform/devices/fff51000.ethernet/driver/unbindecho fff51000.ethernet >/sys/bus/platform/drivers_probeThis mimics "PCI: Introduce new device binding path using pci_dev.driver_override" [1], which is an interface enhancement for more deterministic PCI device binding, e.g., when in the presence of hotplug.
[1] https://lists.cs.columbia.edu/pipermail/kvmarm/2014-May/009527.html
Suggested-by Alex Williamson <[email protected]>Signed-off-by Kim Phillips <[email protected]>
Quoted 9 lines
---
changes in v2 patch of v5 of this patchseries:
- rebased onto today's Linus' ToT
- added kfree to match PCI counterpart fix, as Alex Williamson just posted a v3 of the patch (thanks Christoffer for the notification)
- in the commit text, replaced vfio platform driver reference with 'later in series', and updated the PCI version mailing list reference to the v3 version.
Stuart
PATCH 1/1 driver core: platform: add device binding path 'driver_override'
In reply to Antonios MotakisNeeded by platform device drivers, such as the upcoming vfio-platform driver, in order to bypass the existing OF, ACPI, id_table and name string matches, and successfully be able to be bound to any device, like so:
echo vfio-platform > /sys/bus/platform/devices/fff51000.ethernet/driver_override echo fff51000.ethernet > /sys/bus/platform/devices/fff51000.ethernet/driver/unbind echo fff51000.ethernet > /sys/bus/platform/drivers_probe
This mimics "PCI: Introduce new device binding path using pci_dev.driver_override", which is an interface enhancement for more deterministic PCI device binding, e.g., when in the presence of hotplug.
---
Greg,
This is largely identical to the PCI version of the same that has been accepted for v3.16 and ack'd by you:
https://lists.cs.columbia.edu/pipermail/kvmarm/2014-May/009674.html
and applied to Bjorn Helgaas' PCI tree:
You are the platform driver core maintainer: can you apply this to your driver-core tree now?
Thanks,
Kim
changes since previous version (v2 of the v5 vfio-platform driver series):
- commit text: replaced vfio platform driver reference with 'upcoming', and removed the PCI version mailing list reference since it has now been accepted.
- added Alex W., Alex G., and Stuart's Reviewed-by's.
changes in v2 patch of v5 of this patchseries:
- rebased onto today's Linus' ToT
- added kfree to match PCI counterpart fix, as Alex Williamson just posted a v3 of the patch (thanks Christoffer for the notification)
- in the commit text, replaced vfio platform driver reference with 'later in series', and updated the PCI version mailing list reference to the v3 version.
Documentation/ABI/testing/sysfs-bus-platform | 20 ++++++++++++ drivers/base/platform.c | 47 ++++++++++++++++++++++++++++ include/linux/platform_device.h | 1 + 3 files changed, 68 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-platform
Documentation/ABI/testing/sysfs-bus-platform new file +20 -0
@@ -0,0 +1,20 @@ +What: /sys/bus/platform/devices/.../driver_override +Date: April 2014 +Contact: Kim Phillips <[email protected]> +Description: + This file allows the driver for a device to be specified which + will override standard OF, ACPI, ID table, and name matching. + When specified, only a driver with a name matching the value + written to driver_override will have an opportunity to bind + to the device. The override is specified by writing a string + to the driver_override file (echo vfio-platform > \ + driver_override) and may be cleared with an empty string + (echo > driver_override). This returns the device to standard + matching rules binding. Writing to driver_override does not + automatically unbind the device from its current driver or make + any attempt to automatically load the specified driver. If no + driver with a matching name is currently loaded in the kernel, + the device will not bind to any driver. This also allows + devices to opt-out of driver binding using a driver_override + name such as "none". Only a single driver may be specified in + the override, there is no support for parsing delimiters.
drivers/base/platform.c +47 -0
73 lines not on this page · raw message
include/linux/platform_device.h +1 -0
@@ -28,6 +28,7 @@ struct platform_device {struct resource *resource;const struct platform_device_id *id_entry; +char *driver_override;/* Driver name to force a match */ /* MFD cell pointer */ struct mfd_cell *mfd_cell;
Signature · 1 line
1.9.3
Greg KH
maintainerRe: [PATCH] driver core: platform: add device binding path 'driver_override'
Quoted 24 lines · Kim Phillips
Needed by platform device drivers, such as the upcoming vfio-platform driver, in order to bypass the existing OF, ACPI,
id_tableand name string matches, and successfully be able to be bound to any device, like so:echo vfio-platform >
/sys/bus/platform/devices/fff51000.ethernet/driver_overrideecho fff51000.ethernet >/sys/bus/platform/devices/fff51000.ethernet/driver/unbindecho fff51000.ethernet >/sys/bus/platform/drivers_probeThis mimics "PCI: Introduce new device binding path using pci_dev.driver_override", which is an interface enhancement for more deterministic PCI device binding, e.g., when in the presence of hotplug.
Reviewed-by Alex Williamson <[email protected]>Reviewed-by Alexander Graf <[email protected]>Reviewed-by Stuart Yoder <[email protected]>Signed-off-by Kim Phillips <[email protected]>---
Greg,This is largely identical to the PCI version of the same that has been accepted for v3.16 and ack'd by you:
Quoted 8 lines
https://lists.cs.columbia.edu/pipermail/kvmarm/2014-May/009674.html
and applied to Bjorn Helgaas' PCI tree:
You are the platform driver core maintainer: can you apply this to your driver-core tree now?
Yes, I will after this merge window ends, it's too late for 3.16-rc1 with the window opening up a week early, sorry.
greg k-h
Re: [PATCH] driver core: platform: add device binding path 'driver_override'
Quoted 6 lines · On Mon, 2 Jun 2014 21:28:42 -0700 Greg KH…
Quoted 2 lines · Kim Phillips
You are the platform driver core maintainer: can you apply this to your driver-core tree now?
Yes, I will after this merge window ends, it's too late for 3.16-rc1 with the window opening up a week early, sorry.
How about now? fwiw, I just checked: it still applies cleanly.
Thanks,
Kim
Re: [PATCH] driver core: platform: add device binding path 'driver_override'
In reply to Greg KHQuoted 6 lines · On Mon, 2 Jun 2014 21:28:42 -0700 Greg KH…
Quoted 2 lines · Kim Phillips
You are the platform driver core maintainer: can you apply this to your driver-core tree now?
Yes, I will after this merge window ends, it's too late for 3.16-rc1 with the window opening up a week early, sorry.
Hi Greg,
The merge window has ended, and I see commit activity on your driver-core tree's driver-core-next branch, but this patch is still
missing: Do you think you can get around to applying it before the next merge window?
Thanks,
Kim
Greg KH
maintainerRe: [PATCH] driver core: platform: add device binding path 'driver_override'
In reply to Kim PhillipsQuoted 24 lines · Kim Phillips
Needed by platform device drivers, such as the upcoming vfio-platform driver, in order to bypass the existing OF, ACPI,
id_tableand name string matches, and successfully be able to be bound to any device, like so:echo vfio-platform >
/sys/bus/platform/devices/fff51000.ethernet/driver_overrideecho fff51000.ethernet >/sys/bus/platform/devices/fff51000.ethernet/driver/unbindecho fff51000.ethernet >/sys/bus/platform/drivers_probeThis mimics "PCI: Introduce new device binding path using pci_dev.driver_override", which is an interface enhancement for more deterministic PCI device binding, e.g., when in the presence of hotplug.
Reviewed-by Alex Williamson <[email protected]>Reviewed-by Alexander Graf <[email protected]>Reviewed-by Stuart Yoder <[email protected]>Signed-off-by Kim Phillips <[email protected]>---
Greg,This is largely identical to the PCI version of the same that has been accepted for v3.16 and ack'd by you:
Quoted 8 lines
https://lists.cs.columbia.edu/pipermail/kvmarm/2014-May/009674.html
and applied to Bjorn Helgaas' PCI tree:
You are the platform driver core maintainer: can you apply this to your driver-core tree now?
Sorry for the very long delay, it's now merged in my tree.
Thanks for being persistant.
greg k-h
RFC PATCH v5 2/11 ARM SMMU: Add capability IOMMU_CAP_DMA_EXEC
In reply to Antonios MotakisThe ARM SMMU can take an IOMMU_EXEC protection flag in addition to IOMMU_READ and IOMMU_WRITE. Expose this as an IOMMU capability.
--- drivers/iommu/arm-smmu.c | 2 ++ include/linux/iommu.h | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-)
drivers/iommu/arm-smmu.c +2 -0
@@ -1509,6 +1509,8 @@ static int arm_smmu_domain_has_cap(struct iommu_domain *domain,if (smmu_domain->root_cfg.smmu->features &ARM_SMMU_FEAT_COHERENT_WALK ) caps |=IOMMU_CAP_CACHE_COHERENCY ; + caps |=IOMMU_CAP_DMA_EXEC ; +return !!(cap & caps); }
include/linux/iommu.h +3 -2
@@ -57,8 +57,9 @@ struct iommu_domain {struct iommu_domain_geometry geometry; }; -#define IOMMU_CAP_CACHE_COHERENCY 0x1 -#define IOMMU_CAP_INTR_REMAP 0x2/* isolates device intrs */ +#define IOMMU_CAP_CACHE_COHERENCY (1 << 0) +#define IOMMU_CAP_INTR_REMAP (1 << 1)/* isolates device intrs */ +#define IOMMU_CAP_DMA_EXEC (1 << 2)/* EXEC protection flag */ /* * Following constraints are specifc to FSL_PAMUV1:
Signature · 1 line
1.8.3.2
Re: [RFC PATCH v5 02/11] ARM SMMU: Add capability IOMMU_CAP_DMA_EXEC
Quoted 2 lines · Antonios Motakis
The ARM SMMU can take an
IOMMU_EXECprotection flag in addition toIOMMU_READandIOMMU_WRITE. Expose this as an IOMMU capability.
The other way of handling this would be to negate the capability and advertise a NOEXEC cap instead. That would need the IOMMU_EXEC flag to become IOMMU_NOEXEC and the ARM SMMU driver updating accordingly, but it might make more sense if people don't object to mixing positive and negative logic in the IOMMU_* flags.
Any thoughts?
Will
Re: [RFC PATCH v5 02/11] ARM SMMU: Add capability IOMMU_CAP_DMA_EXEC
Quoted 11 lines · Will Deacon
Quoted 2 lines · Antonios Motakis
The ARM SMMU can take an
IOMMU_EXECprotection flag in addition toIOMMU_READandIOMMU_WRITE. Expose this as an IOMMU capability.The other way of handling this would be to negate the capability and advertise a NOEXEC cap instead. That would need the
IOMMU_EXECflag to becomeIOMMU_NOEXECand the ARM SMMU driver updating accordingly, but it might make more sense if people don't object to mixing positive and negative logic in the IOMMU_* flags.Any thoughts?
A benefit of doing that would be that the flag becomes enforceable. As written in this draft, if a user does not specify EXEC, the mapping may or may not be executable, depending on the IOMMU capability (assuming that if EXEC is not supported that it follows READ). If the flag changes to NOEXEC, then all the domains in the container should support it or else the mapping should fail. We could also avoid the test in vfio code when doing a mapping and just let the IOMMU driver fail the map if NOEXEC is unsupported. Thanks,
Alex
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [RFC PATCH v5 02/11] ARM SMMU: Add capability IOMMU_CAP_DMA_EXEC
I agree, that would be simpler for the user. As it is now we have to worry about whether the EXEC flac is available just to use the PL330, with NOEXEC the device would always be usable by default.
Quoted 15 lines · Will Deacon
Quoted 2 lines · Antonios Motakis
The ARM SMMU can take an
IOMMU_EXECprotection flag in addition toIOMMU_READandIOMMU_WRITE. Expose this as an IOMMU capability.The other way of handling this would be to negate the capability and advertise a NOEXEC cap instead. That would need the
IOMMU_EXECflag to becomeIOMMU_NOEXECand the ARM SMMU driver updating accordingly, but it might make more sense if people don't object to mixing positive and negative
logic in the IOMMU_* flags.Any thoughts?
Will
Signature · 2 lines
Antonios Motakis Virtual Open Systems
Re: [RFC PATCH v5 02/11] ARM SMMU: Add capability IOMMU_CAP_DMA_EXEC
In reply to Will DeaconQuoted 4 lines · Will Deacon
Quoted 2 lines · Antonios Motakis
The ARM SMMU can take an
IOMMU_EXECprotection flag in addition toIOMMU_READandIOMMU_WRITE. Expose this as an IOMMU capability.
Quoted 8 lines
The other way of handling this would be to negate the capability and advertise a NOEXEC cap instead. That would need the
IOMMU_EXECflag to becomeIOMMU_NOEXECand the ARM SMMU driver updating accordingly, but it might make more sense if people don't object to mixing positive and negative logic in the IOMMU_* flags.Any thoughts?
I agree, that would be simpler for the user. As it is now we have to worry about whether the EXEC flac is available just to use the PL330, with NOEXEC the device would always be usable by default.
Quoted 1 line
Will
Signature · 2 lines
Antonios Motakis Virtual Open Systems
RFC PATCH v5 3/11 VFIO_IOMMU_TYPE1 for platform bus devices on ARM
In reply to Antonios MotakisThis allows to make use of the VFIO_IOMMU_TYPE1 driver with platform devices on ARM in addition to PCI. This is required in order to use the Exynos SMMU, or ARM SMMU driver with VFIO_IOMMU_TYPE1.
--- drivers/vfio/Kconfig | 2 +- drivers/vfio/vfio_iommu_type1.c | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-)
drivers/vfio/Kconfig +1 -1
@@ -11,7 +11,7 @@ config VFIO_IOMMU_SPAPR_TCE menuconfig VFIO tristate "VFIO Non-Privileged userspace driver framework" depends on IOMMU_API - select VFIO_IOMMU_TYPE1 if X86 + select VFIO_IOMMU_TYPE1 if X86 || ARM select VFIO_IOMMU_SPAPR_TCE if (PPC_POWERNV || PPC_PSERIES) select ANON_INODES help
drivers/vfio/vfio_iommu_type1.c +4 -1
@@ -30,6 +30,7 @@#include <linux/iommu.h> #include <linux/module.h> #include <linux/mm.h> +#include <linux/pci.h> /* pci_bus_type */ #include <linux/rbtree.h> #include <linux/sched.h> #include <linux/slab.h>
@@ -721,13 +722,15 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,INIT_LIST_HEAD (&domain->group_list);list_add (&group->next, &domain->group_list); -if (!allow_unsafe_interrupts && +#ifdef CONFIG_PCI +if (bus == &pci_bus_type && !allow_unsafe_interrupts && !iommu_domain_has_cap (domain->domain,IOMMU_CAP_INTR_REMAP )) {pr_warn ("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n" , __func__); ret = -EPERM ; goto out_detach; } +#endif if (iommu_domain_has_cap (domain->domain,IOMMU_CAP_CACHE_COHERENCY )) domain->prot |=IOMMU_CACHE ;
Signature · 1 line
1.8.3.2
Re: [RFC PATCH v5 03/11] VFIO_IOMMU_TYPE1 for platform bus devices on ARM
Quoted 42 lines · Antonios Motakis
This allows to make use of the
VFIO_IOMMU_TYPE1driver with platform devices on ARM in addition to PCI. This is required in order to use the Exynos SMMU, or ARM SMMU driver withVFIO_IOMMU_TYPE1.--- drivers/vfio/Kconfig | 2 +- drivers/vfio/vfio_iommu_type1.c | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-)drivers/vfio/Kconfig +1
-1@@ -11,7 +11,7 @@ config VFIO_IOMMU_SPAPR_TCE menuconfig VFIO tristate "VFIO Non-Privileged userspace driver framework" depends on IOMMU_API - select VFIO_IOMMU_TYPE1 if X86 + select VFIO_IOMMU_TYPE1 if X86 || ARM select VFIO_IOMMU_SPAPR_TCE if (PPC_POWERNV || PPC_PSERIES) select ANON_INODES helpdrivers/vfio/vfio_iommu_type1.c +3
-1@@ -30,6 +30,7 @@#include <linux/iommu.h> #include <linux/module.h> #include <linux/mm.h> +#include <linux/pci.h> /* pci_bus_type */ #include <linux/rbtree.h> #include <linux/sched.h> #include <linux/slab.h> @@ -721,13 +722,15 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,INIT_LIST_HEAD (&domain->group_list);list_add (&group->next, &domain->group_list); - if (!allow_unsafe_interrupts && +#ifdef CONFIG_PCI + if (bus == &pci_bus_type && !allow_unsafe_interrupts &&
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 736
Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +1
-0!iommu_domain_has_cap(domain->domain, IOMMU_CAP_INTR_REMAP)) {pr_warn ("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n" , __func__); ret = -EPERM ; goto out_detach; } +#endif if (iommu_domain_has_cap (domain->domain,IOMMU_CAP_CACHE_COHERENCY )) domain->prot |=IOMMU_CACHE ;
This is not a PCI specific requirement. Anything that can support MSI needs an IOMMU that can provide isolation for both DMA and interrupts. I think the IOMMU should still be telling us that it has this feature. Thanks,
Alex
Re: [RFC PATCH v5 03/11] VFIO_IOMMU_TYPE1 for platform bus devices on ARM
Hi Alex,
Quoted 4 lines · Alex Williamson
Quoted 3 lines · Antonios Motakis
This allows to make use of the
VFIO_IOMMU_TYPE1driver with platform devices on ARM in addition to PCI. This is required in order to use the Exynos SMMU, or ARM SMMU driver withVFIO_IOMMU_TYPE1.
[...]
Quoted 15 lines
Quoted 15 lines
drivers/vfio/vfio_iommu_type1.c +3
-1@@ -721,13 +722,15 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,INIT_LIST_HEAD (&domain->group_list);list_add (&group->next, &domain->group_list); -if (!allow_unsafe_interrupts && +#ifdef CONFIG_PCI +if (bus == &pci_bus_type && !allow_unsafe_interrupts && !iommu_domain_has_cap (domain->domain,IOMMU_CAP_INTR_REMAP )) {pr_warn ("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n" , __func__); ret = -EPERM ; goto out_detach; } +#endif
Quoted 6 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 736
Quoted 2 lines
drivers/vfio/vfio_iommu_type1.c +0
-0if (iommu_domain_has_cap (domain->domain,IOMMU_CAP_CACHE_COHERENCY )) domain->prot |=IOMMU_CACHE ;This is not a PCI specific requirement. Anything that can support MSI needs an IOMMU that can provide isolation for both DMA and interrupts. I think the IOMMU should still be telling us that it has this feature.
Please excuse any ignorance on part here (I'm not at all familiar with the Intel IOMMU), but shouldn't this really be a property of the interrupt controller itself? On ARM with GICv3, there is a separate block called the ITS (interrupt translation service) which is part of the interrupt controller. The ITS provides a doorbell page which the SMMU can map into a guest operating system to provide MSI for passthrough devices, but this isn't something the SMMU is aware of -- it will just see the iommu_map request for a non-cacheable mapping.
Will
Re: [RFC PATCH v5 03/11] VFIO_IOMMU_TYPE1 for platform bus devices on ARM
Quoted 32 lines · Will Deacon
Hi Alex,
Quoted 4 lines · Alex Williamson
Quoted 3 lines · Antonios Motakis
This allows to make use of the
VFIO_IOMMU_TYPE1driver with platform devices on ARM in addition to PCI. This is required in order to use the Exynos SMMU, or ARM SMMU driver withVFIO_IOMMU_TYPE1.[...]
Quoted 15 lines
Quoted 15 lines
drivers/vfio/vfio_iommu_type1.c +3
-1@@ -721,13 +722,15 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,INIT_LIST_HEAD (&domain->group_list);list_add (&group->next, &domain->group_list); -if (!allow_unsafe_interrupts && +#ifdef CONFIG_PCI +if (bus == &pci_bus_type && !allow_unsafe_interrupts && !iommu_domain_has_cap (domain->domain,IOMMU_CAP_INTR_REMAP )) {pr_warn ("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n" , __func__); ret = -EPERM ; goto out_detach; } +#endif Quoted 6 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 736
Quoted 2 lines
drivers/vfio/vfio_iommu_type1.c +0
-0if (iommu_domain_has_cap (domain->domain,IOMMU_CAP_CACHE_COHERENCY )) domain->prot |=IOMMU_CACHE ;This is not a PCI specific requirement. Anything that can support MSI needs an IOMMU that can provide isolation for both DMA and interrupts. I think the IOMMU should still be telling us that it has this feature.
Quoted 8 lines
Please excuse any ignorance on part here (I'm not at all familiar with the Intel IOMMU), but shouldn't this really be a property of the interrupt controller itself? On ARM with GICv3, there is a separate block called the ITS (interrupt translation service) which is part of the interrupt controller. The ITS provides a doorbell page which the SMMU can map into a guest operating system to provide MSI for passthrough devices, but this isn't something the SMMU is aware of -- it will just see the
iommu_maprequest for a non-cacheable mapping.
Hi Will,
I don't know the history of why this is an IOMMU domain capability on x86, it's sort of a paradox. An MSI from a device is conceptually just a DMA write and is therefore logically co-located in the IOMMU hardware, but x86 doesn't allow it to be mapped via the IOMMU API interfaces. For compatibility, interrupt remapping support is buried deep in the request_irq interface and effectively invisible other than having this path to query it. Therefore this flag is effectively just saying "MSI isolation support is present and enabled". IOW, the host is protected from interrupt injection attacks from malicious devices. If there is some property of your platform that makes this always the case, then the IOMMU driver can always export this capability as true.
With PCI, MSI is configured via spec defined configuration space registers, so we emulate these registers and prevent user access to them so that we don't need to allow the user a way to setup an interrupt remapping entry. It's done for them via request_irq.
IIRC, the Freescale devices have a limited number of MSI pages and can therefore create some instances with isolation while others may require sharing. In that case I would expect this flag to indicate whether the domain has an exclusive or shared page.
In any case, I suspect keying on the bus_type here is not the correct way to go. Thanks,
Alex
Re: [RFC PATCH v5 03/11] VFIO_IOMMU_TYPE1 for platform bus devices on ARM
Quoted 10 lines · Alex Williamson
Quoted 8 lines · Will Deacon
Please excuse any ignorance on part here (I'm not at all familiar with the Intel IOMMU), but shouldn't this really be a property of the interrupt controller itself? On ARM with GICv3, there is a separate block called the ITS (interrupt translation service) which is part of the interrupt controller. The ITS provides a doorbell page which the SMMU can map into a guest operating system to provide MSI for passthrough devices, but this isn't something the SMMU is aware of -- it will just see the
iommu_maprequest for a non-cacheable mapping.
Quoted 11 lines
I don't know the history of why this is an IOMMU domain capability on x86, it's sort of a paradox. An MSI from a device is conceptually just a DMA write and is therefore logically co-located in the IOMMU hardware, but x86 doesn't allow it to be mapped via the IOMMU API interfaces. For compatibility, interrupt remapping support is buried deep in the
request_irqinterface and effectively invisible other than having this path to query it. Therefore this flag is effectively just saying "MSI isolation support is present and enabled". IOW, the host is protected from interrupt injection attacks from malicious devices. If there is some property of your platform that makes this always the case, then the IOMMU driver can always export this capability as true.
Thanks for the explanation. On ARM, the SMMU does indeed see the MSI write just like a normal write, so it can be mapped via iommu_map() to point at the interrupt controller doorbell page. I guess that means we can enable this capability for all MSI-capable devices upstream of the SMMU, providing that the IRQ controller doesn't have any horrible quirks.
Quoted 5 lines
With PCI, MSI is configured via spec defined configuration space registers, so we emulate these registers and prevent user access to them so that we don't need to allow the user a way to setup an interrupt remapping entry. It's done for them via
request_irq.
Quoted 7 lines
IIRC, the Freescale devices have a limited number of MSI pages and can therefore create some instances with isolation while others may require sharing. In that case I would expect this flag to indicate whether the domain has an exclusive or shared page.
In any case, I suspect keying on the
bus_typehere is not the correct way to go. Thanks,
Agreed, I was more intrigued by the meaning of the flag.
Thanks,
Will
RFC PATCH v5 4/11 VFIO_IOMMU_TYPE1: Introduce the VFIO_DMA_MAP_FLAG_EXEC flag
In reply to Antonios MotakisThe ARM SMMU driver expects the IOMMU_EXEC flag, otherwise it will set the page tables for a device as XN (execute never). This affects devices such as the ARM PL330 DMA Controller, which fails to operate if the XN flag is set on the memory it tries to fetch its instructions from.
We introduce the VFIO_DMA_MAP_FLAG_EXEC to VFIO, and use it in VFIO_IOMMU_TYPE1 to set the IOMMU_EXEC flag. This way the user can control whether the XN flag will be set on the requested mappings. If the IOMMU_EXEC flag is available for at least one IOMMU of a container, the new capability VFIO_IOMMU_PROT_EXEC will be exposed.
--- drivers/vfio/vfio_iommu_type1.c | 34 +++++++++++++++++++++++++++++++--- include/uapi/linux/vfio.h | 2 ++ 2 files changed, 33 insertions(+), 3 deletions(-)
drivers/vfio/vfio_iommu_type1.c +31 -3
@@ -498,12 +498,15 @@ static int map_try_harder(struct vfio_domain *domain, dma_addr_t iova, }static int vfio_iommu_map (struct vfio_iommu *iommu,dma_addr_t iova, -unsigned long pfn,long npage,int prot) +unsigned long pfn,long npage,int prot,bool exec) {struct vfio_domain *d;int ret;list_for_each_entry (d, &iommu->domain_list, next) { +if (exec &&iommu_domain_has_cap (d->domain,IOMMU_CAP_DMA_EXEC )) + prot |=IOMMU_EXEC ; + ret =iommu_map (d->domain, iova, (phys_addr_t )pfn <<PAGE_SHIFT , npage <<PAGE_SHIFT , prot | d->prot);if (ret) {
@@ -530,6 +533,7 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,size_t size = map->size;long npage;int ret =0 , prot =0 ; +bool prot_exec = false;uint64_t mask;struct vfio_dma *dma;unsigned long pfn;
@@ -543,6 +547,8 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, prot |=IOMMU_WRITE ;if (map->flags &VFIO_DMA_MAP_FLAG_READ ) prot |=IOMMU_READ ; +if (map->flags &VFIO_DMA_MAP_FLAG_EXEC ) + prot_exec = true;if (!prot)return -EINVAL ;/* No READ/WRITE? */
@@ -595,7 +601,7 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, }/* Map it! */ - ret =vfio_iommu_map (iommu, iova, pfn, npage, prot); + ret =vfio_iommu_map (iommu, iova, pfn, npage, prot, prot_exec);if (ret) {vfio_unpin_pages (pfn, npage, prot, true);break ;
@@ -887,6 +893,23 @@ static int vfio_domains_have_iommu_cache(struct vfio_iommu *iommu)return ret; } +static int vfio_domains_have_iommu_exec (struct vfio_iommu *iommu) +{ +struct vfio_domain *d; +int ret =0 ; + +mutex_lock (&iommu->lock); +list_for_each_entry (d, &iommu->domain_list, next) { +if (iommu_domain_has_cap (d->domain,IOMMU_CAP_DMA_EXEC )) { + ret =1 ; +break ; + } + } +mutex_unlock (&iommu->lock); + +return ret; +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg) {
@@ -902,6 +925,10 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,if (!iommu)return 0 ;return vfio_domains_have_iommu_cache (iommu); +case VFIO_IOMMU_PROT_EXEC : +if (!iommu) +return 0 ; +return vfio_domains_have_iommu_exec (iommu);default :return 0 ; }
@@ -925,7 +952,8 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, }else if (cmd ==VFIO_IOMMU_MAP_DMA ) {struct vfio_iommu_type1_dma_map map;uint32_t mask =VFIO_DMA_MAP_FLAG_READ | -VFIO_DMA_MAP_FLAG_WRITE ; +VFIO_DMA_MAP_FLAG_WRITE | +VFIO_DMA_MAP_FLAG_EXEC ; minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
include/uapi/linux/vfio.h +2 -0
@@ -29,6 +29,7 @@* capability is subject to change as groups are added or removed. */ #define VFIO_DMA_CC_IOMMU 4 +#define VFIO_IOMMU_PROT_EXEC 5/* * The IOCTL interface is designed for extensibility by embedding the
@@ -398,6 +399,7 @@ struct vfio_iommu_type1_dma_map {__u32 flags;#define VFIO_DMA_MAP_FLAG_READ (1 << 0)/* readable from device */ #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1)/* writable from device */ +#define VFIO_DMA_MAP_FLAG_EXEC (1 << 2)/* executable from device */ __u64 vaddr;/* Process virtual address */ __u64 iova;/* IO virtual address */ __u64 size;/* Size of mapping (bytes) */
Signature · 1 line
1.8.3.2
A message that is not in the archive (sent off-list or not yet received): 1398700371-20096-5-git-send-email-a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]
Re: [RFC PATCH v5 04/11] VFIO_IOMMU_TYPE1: Introduce the VFIO_DMA_MAP_FLAG_EXEC flag
Quoted 48 lines · Antonios Motakis
The ARM SMMU driver expects the
IOMMU_EXECflag, otherwise it will set the page tables for a device as XN (execute never). This affects devices such as the ARM PL330 DMA Controller, which fails to operate if the XN flag is set on the memory it tries to fetch its instructions from.We introduce the
VFIO_DMA_MAP_FLAG_EXECto VFIO, and use it inVFIO_IOMMU_TYPE1to set theIOMMU_EXECflag. This way the user can control whether the XN flag will be set on the requested mappings. If theIOMMU_EXECflag is available for at least one IOMMU of a container, the new capabilityVFIO_IOMMU_PROT_EXECwill be exposed.--- drivers/vfio/vfio_iommu_type1.c | 34 +++++++++++++++++++++++++++++++--- include/uapi/linux/vfio.h | 2 ++ 2 files changed, 33 insertions(+), 3 deletions(-)drivers/vfio/vfio_iommu_type1.c +5
-1@@ -498,12 +498,15 @@ static int map_try_harder(struct vfio_domain *domain, dma_addr_t iova, }static int vfio_iommu_map (struct vfio_iommu *iommu,dma_addr_t iova, -unsigned long pfn,long npage,int prot) +unsigned long pfn,long npage,int prot,bool exec) {struct vfio_domain *d;int ret;list_for_each_entry (d, &iommu->domain_list, next) { +if (exec &&iommu_domain_has_cap (d->domain,IOMMU_CAP_DMA_EXEC )) + prot |=IOMMU_EXEC ; + ret =iommu_map (d->domain, iova, (phys_addr_t )pfn <<PAGE_SHIFT , npage <<PAGE_SHIFT , prot | d->prot);if (ret) {@@ -530,6 +533,7 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,size_t size = map->size;long npage;int ret =0 , prot =0 ; +bool prot_exec = false;uint64_t mask;struct vfio_dma *dma;unsigned long pfn;
drivers/vfio/vfio_iommu_type1.c static int vfio_dma_do_map(struct vfio_iommu *iommu, · line 551
Quoted 5 lines
drivers/vfio/vfio_iommu_type1.c +2
-0@@ -543,6 +547,8 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, prot |=IOMMU_WRITE ;if (map->flags &VFIO_DMA_MAP_FLAG_READ ) prot |=IOMMU_READ ; +if (map->flags &VFIO_DMA_MAP_FLAG_EXEC ) + prot_exec = true;
Is it really necessary for this to be a new parameter rather than prot | = IOMMU_EXEC here? It could simply be masked out in vfio_iommu_map() when not supported. When IOMMU_CAP_DMA_EXEC is not supported, does IOMMU_EXEC effectively follow IOMMU_READ? If so, does that imply we need to error on a request for (IOMMU_WRITE | IOMMU_EXEC)? Thanks,
Alex
Quoted 77 lines
drivers/vfio/vfio_iommu_type1.c +24
-2if (!prot)return -EINVAL ;/* No READ/WRITE? */ @@ -595,7 +601,7 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, }/* Map it! */ - ret =vfio_iommu_map (iommu, iova, pfn, npage, prot); + ret =vfio_iommu_map (iommu, iova, pfn, npage, prot, prot_exec);if (ret) {vfio_unpin_pages (pfn, npage, prot, true);break ;@@ -887,6 +893,23 @@ static int vfio_domains_have_iommu_cache(struct vfio_iommu *iommu)return ret; } +static int vfio_domains_have_iommu_exec (struct vfio_iommu *iommu) +{ +struct vfio_domain *d; +int ret =0 ; + +mutex_lock (&iommu->lock); +list_for_each_entry (d, &iommu->domain_list, next) { +if (iommu_domain_has_cap (d->domain,IOMMU_CAP_DMA_EXEC )) { + ret =1 ; +break ; + } + } +mutex_unlock (&iommu->lock); + +return ret; +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg) {@@ -902,6 +925,10 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,if (!iommu)return 0 ;return vfio_domains_have_iommu_cache (iommu); +case VFIO_IOMMU_PROT_EXEC : +if (!iommu) +return 0 ; +return vfio_domains_have_iommu_exec (iommu);default :return 0 ; }@@ -925,7 +952,8 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, }else if (cmd ==VFIO_IOMMU_MAP_DMA ) {struct vfio_iommu_type1_dma_map map;uint32_t mask =VFIO_DMA_MAP_FLAG_READ | -VFIO_DMA_MAP_FLAG_WRITE ; +VFIO_DMA_MAP_FLAG_WRITE | +VFIO_DMA_MAP_FLAG_EXEC ; minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);include/uapi/linux/vfio.h +2
-0@@ -29,6 +29,7 @@* capability is subject to change as groups are added or removed. */ #define VFIO_DMA_CC_IOMMU 4 +#define VFIO_IOMMU_PROT_EXEC 5/* * The IOCTL interface is designed for extensibility by embedding the @@ -398,6 +399,7 @@ struct vfio_iommu_type1_dma_map {__u32 flags;#define VFIO_DMA_MAP_FLAG_READ (1 << 0)/* readable from device */ #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1)/* writable from device */ +#define VFIO_DMA_MAP_FLAG_EXEC (1 << 2)/* executable from device */ __u64 vaddr;/* Process virtual address */ __u64 iova;/* IO virtual address */ __u64 size;/* Size of mapping (bytes) */
Re: [RFC PATCH v5 04/11] VFIO_IOMMU_TYPE1: Introduce the VFIO_DMA_MAP_FLAG_EXEC flag
Quoted 50 lines · Alex Williamson
Quoted 49 lines · Antonios Motakis
The ARM SMMU driver expects the
IOMMU_EXECflag, otherwise it will set the page tables for a device as XN (execute never). This affects devices such as the ARM PL330 DMA Controller, which fails to operate if the XN flag is set on the memory it tries to fetch its instructions from.We introduce the
VFIO_DMA_MAP_FLAG_EXECto VFIO, and use it inVFIO_IOMMU_TYPE1to set theIOMMU_EXECflag. This way the user can control whether the XN flag will be set on the requested mappings. If theIOMMU_EXECflag is available for at least one IOMMU of a container, the new capabilityVFIO_IOMMU_PROT_EXECwill be exposed.--- drivers/vfio/vfio_iommu_type1.c | 34 +++++++++++++++++++++++++++++++--- include/uapi/linux/vfio.h | 2 ++ 2 files changed, 33 insertions(+), 3 deletions(-)drivers/vfio/vfio_iommu_type1.c +5
-1@@ -498,12 +498,15 @@ static int map_try_harder(struct vfio_domain *domain, dma_addr_t iova, }static int vfio_iommu_map (struct vfio_iommu *iommu,dma_addr_t iova, -unsigned long pfn,long npage,int prot) +unsigned long pfn,long npage,int prot,bool exec) {struct vfio_domain *d;int ret;list_for_each_entry (d, &iommu->domain_list, next) { +if (exec &&iommu_domain_has_cap (d->domain,IOMMU_CAP_DMA_EXEC )) + prot |=IOMMU_EXEC ; + ret =iommu_map (d->domain, iova, (phys_addr_t )pfn <<PAGE_SHIFT , npage <<PAGE_SHIFT , prot | d->prot);if (ret) {@@ -530,6 +533,7 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,size_t size = map->size;long npage;int ret =0 , prot =0 ; +bool prot_exec = false;uint64_t mask;struct vfio_dma *dma;unsigned long pfn;@@ -543,6 +547,8 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, prot |=IOMMU_WRITE ;
Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c static int vfio_dma_do_map(struct vfio_iommu *iommu, · line 551
Quoted 4 lines
drivers/vfio/vfio_iommu_type1.c +2
-0if (map->flags &VFIO_DMA_MAP_FLAG_READ ) prot |=IOMMU_READ ; +if (map->flags &VFIO_DMA_MAP_FLAG_EXEC ) + prot_exec = true;Is it really necessary for this to be a new parameter rather than prot | =
IOMMU_EXEChere? It could simply be masked out invfio_iommu_map()when not supported.
It seemed more readable to me at that point to mask it in, but if you prefer we can mask it out instead.
If we replace EXEC with NOEXEC as suggested in the other thread, then we might just check if all domains in the container support the flag and avoid the trouble altogether.
Quoted 4 lines
When
IOMMU_CAP_DMA_EXECis not supported, doesIOMMU_EXECeffectively followIOMMU_READ? If so, does that imply we need to error on a request for (IOMMU_WRITE|IOMMU_EXEC)? Thanks,
This is probably a good idea, I imagine there won't ever be a platform where EXEC without READ makes sense.
Quoted 82 lines
Alex
Quoted 77 lines
drivers/vfio/vfio_iommu_type1.c +24
-2if (!prot)return -EINVAL ;/* No READ/WRITE? */ @@ -595,7 +601,7 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, }/* Map it! */ - ret =vfio_iommu_map (iommu, iova, pfn, npage, prot); + ret =vfio_iommu_map (iommu, iova, pfn, npage, prot, prot_exec);if (ret) {vfio_unpin_pages (pfn, npage, prot, true);break ;@@ -887,6 +893,23 @@ static int vfio_domains_have_iommu_cache(struct vfio_iommu *iommu)return ret; } +static int vfio_domains_have_iommu_exec (struct vfio_iommu *iommu) +{ +struct vfio_domain *d; +int ret =0 ; + +mutex_lock (&iommu->lock); +list_for_each_entry (d, &iommu->domain_list, next) { +if (iommu_domain_has_cap (d->domain,IOMMU_CAP_DMA_EXEC )) { + ret =1 ; +break ; + } + } +mutex_unlock (&iommu->lock); + +return ret; +} +static long vfio_iommu_type1_ioctl (void *iommu_data,unsigned int cmd,unsigned long arg) {@@ -902,6 +925,10 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,if (!iommu)return 0 ;return vfio_domains_have_iommu_cache (iommu); +case VFIO_IOMMU_PROT_EXEC : +if (!iommu) +return 0 ; +return vfio_domains_have_iommu_exec (iommu);default :return 0 ; }@@ -925,7 +952,8 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, }else if (cmd ==VFIO_IOMMU_MAP_DMA ) {struct vfio_iommu_type1_dma_map map;uint32_t mask =VFIO_DMA_MAP_FLAG_READ | -VFIO_DMA_MAP_FLAG_WRITE ; +VFIO_DMA_MAP_FLAG_WRITE | +VFIO_DMA_MAP_FLAG_EXEC ; minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);include/uapi/linux/vfio.h +2
-0@@ -29,6 +29,7 @@* capability is subject to change as groups are added or removed. */ #define VFIO_DMA_CC_IOMMU 4 +#define VFIO_IOMMU_PROT_EXEC 5/* * The IOCTL interface is designed for extensibility by embedding the @@ -398,6 +399,7 @@ struct vfio_iommu_type1_dma_map {__u32 flags;#define VFIO_DMA_MAP_FLAG_READ (1 << 0)/* readable from device */ #define VFIO_DMA_MAP_FLAG_WRITE (1 << 1)/* writable from device */ +#define VFIO_DMA_MAP_FLAG_EXEC (1 << 2)/* executable from device */ __u64 vaddr;/* Process virtual address */ __u64 iova;/* IO virtual address */ __u64 size;/* Size of mapping (bytes) */
Signature · 2 lines
Antonios Motakis Virtual Open Systems
RFC PATCH v5 5/11 VFIO_PLATFORM: Initial skeleton of VFIO support for platform devices
In reply to Antonios MotakisThis patch forms the skeleton for platform devices support with VFIO.
--- drivers/vfio/Kconfig | 1 + drivers/vfio/Makefile | 1 + drivers/vfio/platform/Kconfig | 9 ++ drivers/vfio/platform/Makefile | 4 + drivers/vfio/platform/vfio_platform.c | 172 ++++++++++++++++++++++++++ drivers/vfio/platform/vfio_platform_private.h | 22 ++++ include/uapi/linux/vfio.h | 1 + 7 files changed, 210 insertions(+) create mode 100644 drivers/vfio/platform/Kconfig create mode 100644 drivers/vfio/platform/Makefile create mode 100644 drivers/vfio/platform/vfio_platform.c create mode 100644 drivers/vfio/platform/vfio_platform_private.h
drivers/vfio/Kconfig +1 -0
@@ -21,3 +21,4 @@ menuconfig VFIO If you don't know what to do here, say N. source "drivers/vfio/pci/Kconfig" +source "drivers/vfio/platform/Kconfig"
drivers/vfio/Makefile +1 -0
@@ -2,3 +2,4 @@ obj-$(CONFIG_VFIO) += vfio.o obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o obj-$(CONFIG_VFIO_PCI) += pci/ +obj-$(CONFIG_VFIO_PLATFORM) += platform/
drivers/vfio/platform/Kconfig new file +9 -0
@@ -0,0 +1,9 @@ +config VFIO_PLATFORM + tristate "VFIO support for platform devices" + depends on VFIO && EVENTFD + help + Support for platform devices with VFIO. This is required to make + use of platform devices present on the system using the VFIO + framework. + + If you don't know what to do here, say N.
drivers/vfio/platform/Makefile new file +4 -0
@@ -0,0 +1,4 @@ + +vfio-platform-y := vfio_platform.o + +obj-$(CONFIG_VFIO_PLATFORM) += vfio-platform.o
drivers/vfio/platform/vfio_platform.c new file +172 -0
172 lines not on this page · raw message
drivers/vfio/platform/vfio_platform_private.h new file +22 -0
@@ -0,0 +1,22 @@ +/* +* Copyright (C) 2013 - Virtual Open Systems +* Author: Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[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. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#ifndef VFIO_PLATFORM_PRIVATE_H +#define VFIO_PLATFORM_PRIVATE_H + +struct vfio_platform_device { +struct platform_device *pdev; +}; + +#endif /* VFIO_PLATFORM_PRIVATE_H */
include/uapi/linux/vfio.h +1 -0
@@ -155,6 +155,7 @@ struct vfio_device_info {__u32 flags;#define VFIO_DEVICE_FLAGS_RESET (1 << 0)/* Device supports reset */ #define VFIO_DEVICE_FLAGS_PCI (1 << 1)/* vfio-pci device */ +#define VFIO_DEVICE_FLAGS_PLATFORM (1 << 2)/* vfio-platform device */ __u32 num_regions;/* Max region index + 1 */ __u32 num_irqs;/* Max IRQ index + 1 */ };
Signature · 1 line
1.8.3.2
A message that is not in the archive (sent off-list or not yet received): 1398700371-20096-6-git-send-email-a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]
Re: [RFC PATCH v5 05/11] VFIO_PLATFORM: Initial skeleton of VFIO support for platform devices
Quoted 41 lines · Antonios Motakis
This patch forms the skeleton for platform devices support with VFIO.
--- drivers/vfio/Kconfig | 1 + drivers/vfio/Makefile | 1 + drivers/vfio/platform/Kconfig | 9 ++ drivers/vfio/platform/Makefile | 4 + drivers/vfio/platform/vfio_platform.c | 172 ++++++++++++++++++++++++++ drivers/vfio/platform/vfio_platform_private.h | 22 ++++ include/uapi/linux/vfio.h | 1 + 7 files changed, 210 insertions(+) create mode 100644 drivers/vfio/platform/Kconfig create mode 100644 drivers/vfio/platform/Makefile create mode 100644 drivers/vfio/platform/vfio_platform.c create mode 100644 drivers/vfio/platform/vfio_platform_private.hdrivers/vfio/Kconfig +1
-0@@ -21,3 +21,4 @@ menuconfig VFIO If you don't know what to do here, say N. source "drivers/vfio/pci/Kconfig" +source "drivers/vfio/platform/Kconfig"drivers/vfio/Makefile +1
-0@@ -2,3 +2,4 @@ obj-$(CONFIG_VFIO) += vfio.o obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o obj-$(CONFIG_VFIO_PCI) += pci/ +obj-$(CONFIG_VFIO_PLATFORM) += platform/drivers/vfio/platform/Kconfig new file +0
-0
drivers/vfio/platform/Kconfig line 3
Quoted 3 lines
drivers/vfio/platform/Kconfig new file +3
-0@@ -0,0 +1,9 @@ +config VFIO_PLATFORM + tristate "VFIO support for platform devices" + depends on VFIO && EVENTFD
VFIO_PCI also depends on PCI, is there no CONFIG_PLATFORM? What benefit would I get from this if I enable it on my x86 box?
Quoted 110 lines
drivers/vfio/platform/Kconfig new file +6
-0+ help + Support for platform devices with VFIO. This is required to make + use of platform devices present on the system using the VFIO + framework. + + If you don't know what to do here, say N.drivers/vfio/platform/Makefile new file +4
-0@@ -0,0 +1,4 @@ + +vfio-platform-y := vfio_platform.o + +obj-$(CONFIG_VFIO_PLATFORM) += vfio-platform.odrivers/vfio/platform/vfio_platform.c new file +88
-0@@ -0,0 +1,172 @@ +/* +* Copyright (C) 2013 - Virtual Open Systems +* Author: Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[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. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/device.h> +#include <linux/eventfd.h> +#include <linux/interrupt.h> +#include <linux/iommu.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/notifier.h> +#include <linux/pm_runtime.h> +#include <linux/slab.h> +#include <linux/types.h> +#include <linux/uaccess.h> +#include <linux/vfio.h> +#include <linux/io.h> +#include <linux/platform_device.h> +#include <linux/irq.h> + +#include "vfio_platform_private.h" + +#define DRIVER_VERSION "0.5" +#define DRIVER_AUTHOR "Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]>" +#define DRIVER_DESC "VFIO for platform devices - User Level meta-driver" + +static void vfio_platform_release (void *device_data) +{ +module_put (THIS_MODULE ); +} + +static int vfio_platform_open (void *device_data) +{ +if (!try_module_get (THIS_MODULE )) +return -ENODEV ; + +return 0 ; +} + +static long vfio_platform_ioctl (void *device_data, +unsigned int cmd,unsigned long arg) +{ +struct vfio_platform_device *vdev = device_data; +unsigned long minsz; + +if (cmd ==VFIO_DEVICE_GET_INFO ) { +struct vfio_device_info info; + + minsz = offsetofend(struct vfio_device_info, num_irqs); + +if (copy_from_user (&info, (void __user *)arg, minsz)) +return -EFAULT ; + +if (info.argsz < minsz) +return -EINVAL ; + + info.flags =VFIO_DEVICE_FLAGS_PLATFORM ; + info.num_regions =0 ; + info.num_irqs =0 ; + +return copy_to_user ((void __user *)arg, &info, minsz); + + }else if (cmd ==VFIO_DEVICE_GET_REGION_INFO ) +return -EINVAL ; + +else if (cmd ==VFIO_DEVICE_GET_IRQ_INFO ) +return -EINVAL ; + +else if (cmd ==VFIO_DEVICE_SET_IRQS ) +return -EINVAL ; + +else if (cmd ==VFIO_DEVICE_RESET ) +return -EINVAL ; + +return -ENOTTY ; +} + +static ssize_t vfio_platform_read(void *device_data, char __user *buf,
drivers/vfio/platform/vfio_platform.c line 98
Quoted 10 lines
drivers/vfio/platform/vfio_platform.c new file +10
-0+ size_t count, loff_t *ppos) +{ +return 0 ; +} + +static ssize_t vfio_platform_write (void *device_data,const char __user *buf, +size_t count,loff_t *ppos) +{ +return 0 ; +}
I know these are just stubs, but I suspect they should probably return error. Thanks,
Alex
Quoted 114 lines
drivers/vfio/platform/vfio_platform.c new file +74
-0+ +static int vfio_platform_mmap (void *device_data,struct vm_area_struct *vma) +{ +return -EINVAL ; +} + +static const struct vfio_device_ops vfio_platform_ops = { + .name ="vfio-platform" , + .open = vfio_platform_open, + .release = vfio_platform_release, + .ioctl = vfio_platform_ioctl, + .read = vfio_platform_read, + .write = vfio_platform_write, + .mmap = vfio_platform_mmap, +}; + +static int vfio_platform_probe (struct platform_device *pdev) +{ +struct vfio_platform_device *vdev; +struct iommu_group *group; +int ret; + + group =iommu_group_get (&pdev->dev); +if (!group) { +pr_err ("VFIO: No IOMMU group for device %s\n" , pdev->name); +return -EINVAL ; + } + + vdev =kzalloc (sizeof (*vdev),GFP_KERNEL ); +if (!vdev) { +iommu_group_put (group); +return -ENOMEM ; + } + + vdev->pdev = pdev; + + ret =vfio_add_group_dev (&pdev->dev, &vfio_platform_ops, vdev); +if (ret) { +iommu_group_put (group); +kfree (vdev); + } + +return ret; +} + +static int vfio_platform_remove (struct platform_device *pdev) +{ +struct vfio_platform_device *vdev; + + vdev =vfio_del_group_dev (&pdev->dev); +if (!vdev) +return -EINVAL ; + +iommu_group_put (pdev->dev.iommu_group); +kfree (vdev); + +return 0 ; +} + +static struct platform_driver vfio_platform_driver = { + .probe = vfio_platform_probe, + .remove = vfio_platform_remove, + .driver = { + .name ="vfio-platform" , + .owner =THIS_MODULE , + }, +}; + +module_platform_driver (vfio_platform_driver); + +MODULE_VERSION (DRIVER_VERSION ); +MODULE_LICENSE ("GPL v2" ); +MODULE_AUTHOR (DRIVER_AUTHOR ); +MODULE_DESCRIPTION (DRIVER_DESC );drivers/vfio/platform/vfio_platform_private.h new file +22
-0@@ -0,0 +1,22 @@ +/* +* Copyright (C) 2013 - Virtual Open Systems +* Author: Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[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. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#ifndef VFIO_PLATFORM_PRIVATE_H +#define VFIO_PLATFORM_PRIVATE_H + +struct vfio_platform_device { +struct platform_device *pdev; +}; + +#endif /* VFIO_PLATFORM_PRIVATE_H */ include/uapi/linux/vfio.h +1
-0@@ -155,6 +155,7 @@ struct vfio_device_info {__u32 flags;#define VFIO_DEVICE_FLAGS_RESET (1 << 0)/* Device supports reset */ #define VFIO_DEVICE_FLAGS_PCI (1 << 1)/* vfio-pci device */ +#define VFIO_DEVICE_FLAGS_PLATFORM (1 << 2)/* vfio-platform device */ __u32 num_regions;/* Max region index + 1 */ __u32 num_irqs;/* Max IRQ index + 1 */ };
Re: [RFC PATCH v5 05/11] VFIO_PLATFORM: Initial skeleton of VFIO support for platform devices
Quoted 40 lines · Alex Williamson
Quoted 39 lines · Antonios Motakis
This patch forms the skeleton for platform devices support with VFIO.
--- drivers/vfio/Kconfig | 1 + drivers/vfio/Makefile | 1 + drivers/vfio/platform/Kconfig | 9 ++ drivers/vfio/platform/Makefile | 4 + drivers/vfio/platform/vfio_platform.c | 172 ++++++++++++++++++++++++++ drivers/vfio/platform/vfio_platform_private.h | 22 ++++ include/uapi/linux/vfio.h | 1 + 7 files changed, 210 insertions(+) create mode 100644 drivers/vfio/platform/Kconfig create mode 100644 drivers/vfio/platform/Makefile create mode 100644 drivers/vfio/platform/vfio_platform.c create mode 100644 drivers/vfio/platform/vfio_platform_private.hdrivers/vfio/Kconfig +1
-0@@ -21,3 +21,4 @@ menuconfig VFIO If you don't know what to do here, say N. source "drivers/vfio/pci/Kconfig" +source "drivers/vfio/platform/Kconfig"drivers/vfio/Makefile +1
-0@@ -2,3 +2,4 @@ obj-$(CONFIG_VFIO) += vfio.o obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o obj-$(CONFIG_VFIO_PCI) += pci/ +obj-$(CONFIG_VFIO_PLATFORM) += platform/drivers/vfio/platform/Kconfig new file +0
-1--- /dev/null
Quoted 8 lines
drivers/vfio/platform/Kconfig line 3
Quoted 5 lines
+++ b/drivers/vfio/platform/Kconfig
drivers/vfio/platform/Kconfig new file +3
-0@@ -0,0 +1,9 @@ +config VFIO_PLATFORM + tristate "VFIO support for platform devices" + depends on VFIO && EVENTFD
VFIO_PCIalso depends on PCI, is there noCONFIG_PLATFORM? What benefit would I get from this if I enable it on my x86 box?
That's a good point. Unfortunately I don't know of an obvious config we can depend on, other than known target platforms (i.e. ARM).
Useless on an x86 PC, but maybe someone has a weird x86 based non-PC based architecture somewhere :)
Quoted 116 lines
Quoted 116 lines
drivers/vfio/platform/Kconfig new file +6
-0+ help + Support for platform devices with VFIO. This is required to make + use of platform devices present on the system using the VFIO + framework. + + If you don't know what to do here, say N.drivers/vfio/platform/Makefile new file +4
-0@@ -0,0 +1,4 @@ + +vfio-platform-y := vfio_platform.o + +obj-$(CONFIG_VFIO_PLATFORM) += vfio-platform.odrivers/vfio/platform/vfio_platform.c new file +93
-0@@ -0,0 +1,172 @@ +/* +* Copyright (C) 2013 - Virtual Open Systems +* Author: Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[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. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/device.h> +#include <linux/eventfd.h> +#include <linux/interrupt.h> +#include <linux/iommu.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/notifier.h> +#include <linux/pm_runtime.h> +#include <linux/slab.h> +#include <linux/types.h> +#include <linux/uaccess.h> +#include <linux/vfio.h> +#include <linux/io.h> +#include <linux/platform_device.h> +#include <linux/irq.h> + +#include "vfio_platform_private.h" + +#define DRIVER_VERSION "0.5" +#define DRIVER_AUTHOR "Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]>" +#define DRIVER_DESC "VFIO for platform devices - User Level meta-driver" + +static void vfio_platform_release (void *device_data) +{ +module_put (THIS_MODULE ); +} + +static int vfio_platform_open (void *device_data) +{ +if (!try_module_get (THIS_MODULE )) +return -ENODEV ; + +return 0 ; +} + +static long vfio_platform_ioctl (void *device_data, +unsigned int cmd,unsigned long arg) +{ +struct vfio_platform_device *vdev = device_data; +unsigned long minsz; + +if (cmd ==VFIO_DEVICE_GET_INFO ) { +struct vfio_device_info info; + + minsz = offsetofend(struct vfio_device_info, num_irqs); + +if (copy_from_user (&info, (void __user *)arg, minsz)) +return -EFAULT ; + +if (info.argsz < minsz) +return -EINVAL ; + + info.flags =VFIO_DEVICE_FLAGS_PLATFORM ; + info.num_regions =0 ; + info.num_irqs =0 ; + +return copy_to_user ((void __user *)arg, &info, minsz); + + }else if (cmd ==VFIO_DEVICE_GET_REGION_INFO ) +return -EINVAL ; + +else if (cmd ==VFIO_DEVICE_GET_IRQ_INFO ) +return -EINVAL ; + +else if (cmd ==VFIO_DEVICE_SET_IRQS ) +return -EINVAL ; + +else if (cmd ==VFIO_DEVICE_RESET ) +return -EINVAL ; + +return -ENOTTY ; +} + +static ssize_t vfio_platform_read (void *device_data,char __user *buf, +size_t count,loff_t *ppos) +{ +return 0 ; +} +
Quoted 9 lines
drivers/vfio/platform/vfio_platform.c line 98
Quoted 5 lines
drivers/vfio/platform/vfio_platform.c new file +5
-0+static ssize_t vfio_platform_write (void *device_data,const char __user *buf, +size_t count,loff_t *ppos) +{ +return 0 ; +}I know these are just stubs, but I suspect they should probably return error. Thanks,
Ok!
Quoted 119 lines
Alex
Quoted 114 lines
drivers/vfio/platform/vfio_platform.c new file +74
-0+ +static int vfio_platform_mmap (void *device_data,struct vm_area_struct *vma) +{ +return -EINVAL ; +} + +static const struct vfio_device_ops vfio_platform_ops = { + .name ="vfio-platform" , + .open = vfio_platform_open, + .release = vfio_platform_release, + .ioctl = vfio_platform_ioctl, + .read = vfio_platform_read, + .write = vfio_platform_write, + .mmap = vfio_platform_mmap, +}; + +static int vfio_platform_probe (struct platform_device *pdev) +{ +struct vfio_platform_device *vdev; +struct iommu_group *group; +int ret; + + group =iommu_group_get (&pdev->dev); +if (!group) { +pr_err ("VFIO: No IOMMU group for device %s\n" , pdev->name); +return -EINVAL ; + } + + vdev =kzalloc (sizeof (*vdev),GFP_KERNEL ); +if (!vdev) { +iommu_group_put (group); +return -ENOMEM ; + } + + vdev->pdev = pdev; + + ret =vfio_add_group_dev (&pdev->dev, &vfio_platform_ops, vdev); +if (ret) { +iommu_group_put (group); +kfree (vdev); + } + +return ret; +} + +static int vfio_platform_remove (struct platform_device *pdev) +{ +struct vfio_platform_device *vdev; + + vdev =vfio_del_group_dev (&pdev->dev); +if (!vdev) +return -EINVAL ; + +iommu_group_put (pdev->dev.iommu_group); +kfree (vdev); + +return 0 ; +} + +static struct platform_driver vfio_platform_driver = { + .probe = vfio_platform_probe, + .remove = vfio_platform_remove, + .driver = { + .name ="vfio-platform" , + .owner =THIS_MODULE , + }, +}; + +module_platform_driver (vfio_platform_driver); + +MODULE_VERSION (DRIVER_VERSION ); +MODULE_LICENSE ("GPL v2" ); +MODULE_AUTHOR (DRIVER_AUTHOR ); +MODULE_DESCRIPTION (DRIVER_DESC );drivers/vfio/platform/vfio_platform_private.h new file +22
-0@@ -0,0 +1,22 @@ +/* +* Copyright (C) 2013 - Virtual Open Systems +* Author: Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[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. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#ifndef VFIO_PLATFORM_PRIVATE_H +#define VFIO_PLATFORM_PRIVATE_H + +struct vfio_platform_device { +struct platform_device *pdev; +}; + +#endif /* VFIO_PLATFORM_PRIVATE_H */ include/uapi/linux/vfio.h +1
-0@@ -155,6 +155,7 @@ struct vfio_device_info {__u32 flags;#define VFIO_DEVICE_FLAGS_RESET (1 << 0)/* Device supports reset */ #define VFIO_DEVICE_FLAGS_PCI (1 << 1)/* vfio-pci device */ +#define VFIO_DEVICE_FLAGS_PLATFORM (1 << 2)/* vfio-platform device */ __u32 num_regions;/* Max region index + 1 */ __u32 num_irqs;/* Max IRQ index + 1 */ };
Signature · 2 lines
Antonios Motakis Virtual Open Systems
RFC PATCH v5 6/11 VFIO_PLATFORM: Return info for device and its memory mapped IO regions
In reply to Antonios MotakisA VFIO userspace driver will start by opening the VFIO device that corresponds to an IOMMU group, and will use the ioctl interface to get the basic device info, such as number of memory regions and interrupts, and their properties.
This patch enables the IOCTLs:
- VFIO_DEVICE_GET_INFO
- VFIO_DEVICE_GET_REGION_INFO
IRQ info is provided by one of the latter patches.
--- drivers/vfio/platform/vfio_platform.c | 77 ++++++++++++++++++++++++--- drivers/vfio/platform/vfio_platform_private.h | 17 ++++++ 2 files changed, 88 insertions(+), 6 deletions(-)
drivers/vfio/platform/vfio_platform.c +71 -6
@@ -34,15 +34,62 @@#define DRIVER_AUTHOR "Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]>"#define DRIVER_DESC "VFIO for platform devices - User Level meta-driver" +static int vfio_platform_regions_init (struct vfio_platform_device *vdev) +{ +int cnt =0 , i; + +while (platform_get_resource (vdev->pdev,IORESOURCE_MEM , cnt)) + cnt++; + + vdev->num_regions = cnt; + + vdev->region =kzalloc (sizeof (struct vfio_platform_region ) * cnt, +GFP_KERNEL ); +if (!vdev->region) +return -ENOMEM ; + +for (i =0 ; i < cnt; i++) { +struct vfio_platform_region region; +struct resource *res = +platform_get_resource (vdev->pdev,IORESOURCE_MEM , i); + + region.addr = res->start; + region.size =resource_size (res); + region.flags =0 ; + + vdev->region[i] = region; + } + +return 0 ; +} + +static void vfio_platform_regions_cleanup (struct vfio_platform_device *vdev) +{ +kfree (vdev->region); +} +static void vfio_platform_release (void *device_data) { +struct vfio_platform_device *vdev = device_data; + +vfio_platform_regions_cleanup (vdev); +module_put (THIS_MODULE ); }static int vfio_platform_open (void *device_data) { -if (!try_module_get (THIS_MODULE )) +struct vfio_platform_device *vdev = device_data; +int ret; + + ret =vfio_platform_regions_init (vdev); +if (ret) +return ret; + +if (!try_module_get (THIS_MODULE )) { +vfio_platform_regions_cleanup (vdev);return -ENODEV ; + }return 0 ; }
@@ -65,18 +112,36 @@ static long vfio_platform_ioctl(void *device_data,return -EINVAL ; info.flags =VFIO_DEVICE_FLAGS_PLATFORM ; - info.num_regions =0 ; + info.num_regions = vdev->num_regions; info.num_irqs =0 ;return copy_to_user ((void __user *)arg, &info, minsz); - }else if (cmd ==VFIO_DEVICE_GET_REGION_INFO ) -return -EINVAL ; + }else if (cmd ==VFIO_DEVICE_GET_REGION_INFO ) { +struct vfio_region_info info; + + minsz = offsetofend(struct vfio_region_info, offset); + +if (copy_from_user (&info, (void __user *)arg, minsz)) +return -EFAULT ; + +if (info.argsz < minsz) +return -EINVAL ; + +if (info.index >= vdev->num_regions) +return -EINVAL ; + +/* map offset to the physical address */ + info.offset =VFIO_PLATFORM_INDEX_TO_OFFSET (info.index); + info.size = vdev->region[info.index].size; + info.flags = vdev->region[info.index].flags; + +return copy_to_user ((void __user *)arg, &info, minsz); -else if (cmd ==VFIO_DEVICE_GET_IRQ_INFO ) + }else if (cmd ==VFIO_DEVICE_GET_IRQ_INFO ) {return -EINVAL ; -else if (cmd ==VFIO_DEVICE_SET_IRQS ) + }else if (cmd ==VFIO_DEVICE_SET_IRQS )return -EINVAL ;else if (cmd ==VFIO_DEVICE_RESET )
drivers/vfio/platform/vfio_platform_private.h +17 -0
@@ -15,8 +15,25 @@#ifndef VFIO_PLATFORM_PRIVATE_H #define VFIO_PLATFORM_PRIVATE_H +#define VFIO_PLATFORM_OFFSET_SHIFT 40 +#define VFIO_PLATFORM_OFFSET_MASK (((u64)(1) << VFIO_PLATFORM_OFFSET_SHIFT) - 1) + +#define VFIO_PLATFORM_OFFSET_TO_INDEX (off) \ + (off >> VFIO_PLATFORM_OFFSET_SHIFT) + +#define VFIO_PLATFORM_INDEX_TO_OFFSET (index) \ + ((u64)(index) << VFIO_PLATFORM_OFFSET_SHIFT) + +struct vfio_platform_region { +u64 addr; +resource_size_t size; +u32 flags; +}; +struct vfio_platform_device {struct platform_device *pdev; +struct vfio_platform_region *region; +u32 num_regions; };#endif /* VFIO_PLATFORM_PRIVATE_H */
Signature · 1 line
1.8.3.2
Re: [RFC PATCH v5 06/11] VFIO_PLATFORM: Return info for device and its memory mapped IO regions
Quoted 27 lines · Antonios Motakis
A VFIO userspace driver will start by opening the VFIO device that corresponds to an IOMMU group, and will use the ioctl interface to get the basic device info, such as number of memory regions and interrupts, and their properties.
This patch enables the IOCTLs:
-VFIO_DEVICE_GET_INFO
-VFIO_DEVICE_GET_REGION_INFOIRQ info is provided by one of the latter patches.
--- drivers/vfio/platform/vfio_platform.c | 77 ++++++++++++++++++++++++--- drivers/vfio/platform/vfio_platform_private.h | 17 ++++++ 2 files changed, 88 insertions(+), 6 deletions(-)drivers/vfio/platform/vfio_platform.c +2
-0@@ -34,15 +34,62 @@#define DRIVER_AUTHOR "Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]>"#define DRIVER_DESC "VFIO for platform devices - User Level meta-driver" +static int vfio_platform_regions_init (struct vfio_platform_device *vdev) +{
drivers/vfio/platform/vfio_platform.c line 48
Quoted 10 lines
drivers/vfio/platform/vfio_platform.c +10
-0+int cnt =0 , i; + +while (platform_get_resource (vdev->pdev,IORESOURCE_MEM , cnt)) + cnt++; + + vdev->num_regions = cnt; + + vdev->region =kzalloc (sizeof (struct vfio_platform_region ) * cnt, +GFP_KERNEL ); +if (!vdev->region)
Should vdev->num_regions be cleared here or set at the end to avoid possibly walking a null pointer later?
drivers/vfio/platform/vfio_platform.c line 60
Quoted 12 lines
drivers/vfio/platform/vfio_platform.c +12
-0+return -ENOMEM ; + +for (i =0 ; i < cnt; i++) { +struct vfio_platform_region region; +struct resource *res = +platform_get_resource (vdev->pdev,IORESOURCE_MEM , i); + + region.addr = res->start; + region.size =resource_size (res); + region.flags =0 ; + + vdev->region[i] = region;
nit, the local variable with copy at the end seems rather unnecessary here.
drivers/vfio/platform/vfio_platform.c line 68
Quoted 8 lines
drivers/vfio/platform/vfio_platform.c +8
-0+ } + +return 0 ; +} + +static void vfio_platform_regions_cleanup (struct vfio_platform_device *vdev) +{ +kfree (vdev->region);
Makes me nervous again that we have vdev->num_regions still set to a value. Maybe just paranoia.
Quoted 15 lines
drivers/vfio/platform/vfio_platform.c +7
-1+} +static void vfio_platform_release (void *device_data) { +struct vfio_platform_device *vdev = device_data; + +vfio_platform_regions_cleanup (vdev); +module_put (THIS_MODULE ); }static int vfio_platform_open (void *device_data) { -if (!try_module_get (THIS_MODULE )) +struct vfio_platform_device *vdev = device_data;
drivers/vfio/platform/vfio_platform.c line 92
Quoted 10 lines
drivers/vfio/platform/vfio_platform.c +9
-0+int ret; + + ret =vfio_platform_regions_init (vdev); +if (ret) +return ret; + +if (!try_module_get (THIS_MODULE )) { +vfio_platform_regions_cleanup (vdev);return -ENODEV ; + }
Getting a reference to the module seems like it should be step 1 here. Thanks,
Alex
Quoted 75 lines
drivers/vfio/platform/vfio_platform.c +23
-5return 0 ; }@@ -65,18 +112,36 @@ static long vfio_platform_ioctl(void *device_data,return -EINVAL ; info.flags =VFIO_DEVICE_FLAGS_PLATFORM ; - info.num_regions =0 ; + info.num_regions = vdev->num_regions; info.num_irqs =0 ;return copy_to_user ((void __user *)arg, &info, minsz); - }else if (cmd ==VFIO_DEVICE_GET_REGION_INFO ) -return -EINVAL ; + }else if (cmd ==VFIO_DEVICE_GET_REGION_INFO ) { +struct vfio_region_info info; + + minsz = offsetofend(struct vfio_region_info, offset); + +if (copy_from_user (&info, (void __user *)arg, minsz)) +return -EFAULT ; + +if (info.argsz < minsz) +return -EINVAL ; + +if (info.index >= vdev->num_regions) +return -EINVAL ; + +/* map offset to the physical address */ + info.offset =VFIO_PLATFORM_INDEX_TO_OFFSET (info.index); + info.size = vdev->region[info.index].size; + info.flags = vdev->region[info.index].flags; + +return copy_to_user ((void __user *)arg, &info, minsz); -else if (cmd ==VFIO_DEVICE_GET_IRQ_INFO ) + }else if (cmd ==VFIO_DEVICE_GET_IRQ_INFO ) {return -EINVAL ; -else if (cmd ==VFIO_DEVICE_SET_IRQS ) + }else if (cmd ==VFIO_DEVICE_SET_IRQS )return -EINVAL ;else if (cmd ==VFIO_DEVICE_RESET )drivers/vfio/platform/vfio_platform_private.h +17
-0@@ -15,8 +15,25 @@#ifndef VFIO_PLATFORM_PRIVATE_H #define VFIO_PLATFORM_PRIVATE_H +#define VFIO_PLATFORM_OFFSET_SHIFT 40 +#define VFIO_PLATFORM_OFFSET_MASK (((u64)(1) << VFIO_PLATFORM_OFFSET_SHIFT) - 1) + +#define VFIO_PLATFORM_OFFSET_TO_INDEX (off) \ + (off >> VFIO_PLATFORM_OFFSET_SHIFT) + +#define VFIO_PLATFORM_INDEX_TO_OFFSET (index) \ + ((u64)(index) << VFIO_PLATFORM_OFFSET_SHIFT) + +struct vfio_platform_region { +u64 addr; +resource_size_t size; +u32 flags; +}; +struct vfio_platform_device {struct platform_device *pdev; +struct vfio_platform_region *region; +u32 num_regions; };#endif /* VFIO_PLATFORM_PRIVATE_H */
A message that is not in the archive (sent off-list or not yet received): 1398705389.24318.279.camel-85EaTFmN5p//[email protected]
Re: [RFC PATCH v5 06/11] VFIO_PLATFORM: Return info for device and its memory mapped IO regions
On Mon, Apr 28, 2014 at 7:16 PM, Alex Williamson <[email protected]
Quoted 32 lines
wrote:
Quoted 9 lines · Antonios Motakis
A VFIO userspace driver will start by opening the VFIO device that corresponds to an IOMMU group, and will use the ioctl interface to get the basic device info, such as number of memory regions and interrupts, and their properties.
This patch enables the IOCTLs:
-VFIO_DEVICE_GET_INFO
-VFIO_DEVICE_GET_REGION_INFOQuoted 5 lines
IRQ info is provided by one of the latter patches.
--- drivers/vfio/platform/vfio_platform.c | 77++++++++++++++++++++++++---
Review
Quoted 4 lines
drivers/vfio/platform/vfio_platform_private.h | 17 ++++++ 2 files changed, 88 insertions(+), 6 deletions(-)(quoted diff) +0
-0
b/drivers/vfio/platform/vfio_platform.cdrivers/vfio/platform/vfio_platform.c line 34
Quoted 5 lines
index 1661746..5430cbe 100644
drivers/vfio/platform/vfio_platform.c +0
-0@@ -34,15 +34,62 @@#define DRIVER_AUTHOR "Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]>"
drivers/vfio/platform/vfio_platform.c line 35
Quoted 1 line
drivers/vfio/platform/vfio_platform.c +0
-0#define DRIVER_DESC "VFIO for platform devices - User Levelmeta-driver"
Quoted 1 line
Quoted 17 lines
drivers/vfio/platform/vfio_platform.c line 48
Quoted 12 lines
drivers/vfio/platform/vfio_platform.c +12
-0+static int vfio_platform_regions_init (struct vfio_platform_device *vdev) +{ +int cnt =0 , i; + +while (platform_get_resource (vdev->pdev,IORESOURCE_MEM , cnt)) + cnt++; + + vdev->num_regions = cnt; + + vdev->region =kzalloc (sizeof (struct vfio_platform_region ) * cnt, +GFP_KERNEL ); +if (!vdev->region)Should
vdev->num_regionsbe cleared here or set at the end to avoid possibly walking a null pointer later?
Ack.
Quoted 7 lines
drivers/vfio/platform/vfio_platform.c
Quoted 6 lines
drivers/vfio/platform/vfio_platform.c +6
-0+return -ENOMEM ; + +for (i =0 ; i < cnt; i++) { +struct vfio_platform_region region; +struct resource *res = + platform_get_resource(vdev->pdev, IORESOURCE_MEM,i);
Quoted 11 lines
drivers/vfio/platform/vfio_platform.c line 60
Quoted 6 lines
drivers/vfio/platform/vfio_platform.c +6
-0+ + region.addr = res->start; + region.size =resource_size (res); + region.flags =0 ; + + vdev->region[i] = region;nit, the local variable with copy at the end seems rather unnecessary here.
I'm doing it purely for readability reasons, the compiler should be able to do the right thing anyway. But I am not married to the idea of course.
Quoted 7 lines
drivers/vfio/platform/vfio_platform.c
Quoted 6 lines
drivers/vfio/platform/vfio_platform.c +6
-0+ } + +return 0 ; +} + +static void vfio_platform_regions_cleanup(struct vfio_platform_device*vdev)
Quoted 6 lines
drivers/vfio/platform/vfio_platform.c
Quoted 2 lines
drivers/vfio/platform/vfio_platform.c +2
-0+{ +kfree (vdev->region);Makes me nervous again that we have
vdev->num_regionsstill set to a value. Maybe just paranoia.
Definitely not paranoia, I will reset the num_regions here.
Quoted 21 lines
Quoted 21 lines
drivers/vfio/platform/vfio_platform.c +12
-1+} +static void vfio_platform_release (void *device_data) { +struct vfio_platform_device *vdev = device_data; + +vfio_platform_regions_cleanup (vdev); +module_put (THIS_MODULE ); }static int vfio_platform_open (void *device_data) { -if (!try_module_get (THIS_MODULE )) +struct vfio_platform_device *vdev = device_data; +int ret; + + ret =vfio_platform_regions_init (vdev); +if (ret) +return ret;
Quoted 10 lines
drivers/vfio/platform/vfio_platform.c line 92
Quoted 5 lines
drivers/vfio/platform/vfio_platform.c +4
-0+ +if (!try_module_get (THIS_MODULE )) { +vfio_platform_regions_cleanup (vdev);return -ENODEV ; + }Getting a reference to the module seems like it should be step 1 here. Thanks,
Ack.
Quoted 83 lines
Alex
Review
Quoted 46 lines
return 0;}
drivers/vfio/platform/vfio_platform.c +23
-5@@ -65,18 +112,36 @@ static long vfio_platform_ioctl(void *device_data,return -EINVAL ; info.flags =VFIO_DEVICE_FLAGS_PLATFORM ; - info.num_regions =0 ; + info.num_regions = vdev->num_regions; info.num_irqs =0 ;return copy_to_user ((void __user *)arg, &info, minsz); - }else if (cmd ==VFIO_DEVICE_GET_REGION_INFO ) -return -EINVAL ; + }else if (cmd ==VFIO_DEVICE_GET_REGION_INFO ) { +struct vfio_region_info info; + + minsz = offsetofend(struct vfio_region_info, offset); + +if (copy_from_user (&info, (void __user *)arg, minsz)) +return -EFAULT ; + +if (info.argsz < minsz) +return -EINVAL ; + +if (info.index >= vdev->num_regions) +return -EINVAL ; + +/* map offset to the physical address */ + info.offset =VFIO_PLATFORM_INDEX_TO_OFFSET (info.index); + info.size = vdev->region[info.index].size; + info.flags = vdev->region[info.index].flags; + +return copy_to_user ((void __user *)arg, &info, minsz); -else if (cmd ==VFIO_DEVICE_GET_IRQ_INFO ) + }else if (cmd ==VFIO_DEVICE_GET_IRQ_INFO ) {return -EINVAL ; -else if (cmd ==VFIO_DEVICE_SET_IRQS ) + }else if (cmd ==VFIO_DEVICE_SET_IRQS )return -EINVAL ;else if (cmd ==VFIO_DEVICE_RESET )(quoted diff) +0
-0
b/drivers/vfio/platform/vfio_platform_private.hdrivers/vfio/platform/vfio_platform_private.h line 19
Quoted 9 lines
index 4ae88f8..3448f918 100644
drivers/vfio/platform/vfio_platform_private.h +2
-0@@ -15,8 +15,25 @@#ifndef VFIO_PLATFORM_PRIVATE_H #define VFIO_PLATFORM_PRIVATE_H +#define VFIO_PLATFORM_OFFSET_SHIFT 40 +#define VFIO_PLATFORM_OFFSET_MASK (((u64)(1) <<
VFIO_PLATFORM_OFFSET_SHIFT) - 1)Quoted 20 lines
drivers/vfio/platform/vfio_platform_private.h +15
-0+ +#define VFIO_PLATFORM_OFFSET_TO_INDEX (off) \ + (off >> VFIO_PLATFORM_OFFSET_SHIFT) + +#define VFIO_PLATFORM_INDEX_TO_OFFSET (index) \ + ((u64)(index) << VFIO_PLATFORM_OFFSET_SHIFT) + +struct vfio_platform_region { +u64 addr; +resource_size_t size; +u32 flags; +}; +struct vfio_platform_device {struct platform_device *pdev; +struct vfio_platform_region *region; +u32 num_regions; };#endif /* VFIO_PLATFORM_PRIVATE_H */
Signature · 2 lines
Antonios Motakis Virtual Open Systems
RFC PATCH v5 7/11 VFIO_PLATFORM: Read and write support for the device fd
In reply to Antonios MotakisVFIO returns a file descriptor which we can use to manipulate the memory regions of the device. Since some memory regions we cannot mmap due to security concerns, we also allow to read and write to this file descriptor directly.
--- drivers/vfio/platform/vfio_platform.c | 110 +++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 3 deletions(-)
drivers/vfio/platform/vfio_platform.c +107 -3
@@ -55,7 +55,8 @@ static int vfio_platform_regions_init(struct vfio_platform_device *vdev) region.addr = res->start; region.size =resource_size (res); - region.flags =0 ; + region.flags =VFIO_REGION_INFO_FLAG_READ + |VFIO_REGION_INFO_FLAG_WRITE ; vdev->region[i] = region; }
@@ -153,13 +154,116 @@ static long vfio_platform_ioctl(void *device_data,static ssize_t vfio_platform_read (void *device_data,char __user *buf,size_t count,loff_t *ppos) { -return 0 ; +struct vfio_platform_device *vdev = device_data; +unsigned int index =VFIO_PLATFORM_OFFSET_TO_INDEX (*ppos); +loff_t off = *ppos &VFIO_PLATFORM_OFFSET_MASK ; +unsigned int done =0 ; +void __iomem *io; + +if (index >= vdev->num_regions) +return -EINVAL ; + + io =ioremap_nocache (vdev->region[index].addr, + vdev->region[index].size); + +while (count) { +size_t filled; + +if (count >=4 && !(off %4 )) { +u32 val; + + val =ioread32 (io + off); +if (copy_to_user (buf, &val,4 )) + goto err; + + filled =4 ; + }else if (count >=2 && !(off %2 )) { +u16 val; + + val =ioread16 (io + off); +if (copy_to_user (buf, &val,2 )) + goto err; + + filled =2 ; + }else { +u8 val; + + val =ioread8 (io + off); +if (copy_to_user (buf, &val,1 )) + goto err; + + filled =1 ; + } + + + count -= filled; + done += filled; + off += filled; + buf += filled; + } + +iounmap (io); +return done; +err: +iounmap (io); +return -EFAULT ; }static ssize_t vfio_platform_write (void *device_data,const char __user *buf,size_t count,loff_t *ppos) { -return 0 ; +struct vfio_platform_device *vdev = device_data; +unsigned int index =VFIO_PLATFORM_OFFSET_TO_INDEX (*ppos); +loff_t off = *ppos &VFIO_PLATFORM_OFFSET_MASK ; +unsigned int done =0 ; +void __iomem *io; + +if (index >= vdev->num_regions) +return -EINVAL ; + + io =ioremap_nocache (vdev->region[index].addr, + vdev->region[index].size); + +while (count) { +size_t filled; + +if (count >=4 && !(off %4 )) { +u32 val; + +if (copy_from_user (&val, buf,4 )) + goto err; +iowrite32 (val, io + off); + + filled =4 ; + }else if (count >=2 && !(off %2 )) { +u16 val; + +if (copy_from_user (&val, buf,2 )) + goto err; +iowrite16 (val, io + off); + + filled =2 ; + }else { +u8 val; + +if (copy_from_user (&val, buf,1 )) + goto err; +iowrite8 (val, io + off); + + filled =1 ; + } + + count -= filled; + done += filled; + off += filled; + buf += filled; + } + +iounmap (io); +return done; +err: +iounmap (io); +return -EFAULT ; }static int vfio_platform_mmap (void *device_data,struct vm_area_struct *vma)
Signature · 1 line
1.8.3.2
Re: [RFC PATCH v5 07/11] VFIO_PLATFORM: Read and write support for the device fd
Quoted 4 lines · Antonios Motakis
VFIO returns a file descriptor which we can use to manipulate the memory regions of the device. Since some memory regions we cannot mmap due to security concerns, we also allow to read and write to this file descriptor directly.
If there are regions we cannot mmap for security, why do we provide full read/write access to them?
Quoted 25 lines
--- drivers/vfio/platform/vfio_platform.c | 110 +++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 3 deletions(-)drivers/vfio/platform/vfio_platform.c +3
-2@@ -55,7 +55,8 @@ static int vfio_platform_regions_init(struct vfio_platform_device *vdev) region.addr = res->start; region.size =resource_size (res); - region.flags =0 ; + region.flags =VFIO_REGION_INFO_FLAG_READ + |VFIO_REGION_INFO_FLAG_WRITE ; vdev->region[i] = region; }@@ -153,13 +154,116 @@ static long vfio_platform_ioctl(void *device_data,static ssize_t vfio_platform_read (void *device_data,char __user *buf,size_t count,loff_t *ppos) { -return 0 ; +struct vfio_platform_device *vdev = device_data;
drivers/vfio/platform/vfio_platform.c static long vfio_platform_ioctl(void *device_data, · line 167
Quoted 10 lines
drivers/vfio/platform/vfio_platform.c +10
-0+unsigned int index =VFIO_PLATFORM_OFFSET_TO_INDEX (*ppos); +loff_t off = *ppos &VFIO_PLATFORM_OFFSET_MASK ; +unsigned int done =0 ; +void __iomem *io; + +if (index >= vdev->num_regions) +return -EINVAL ; + + io =ioremap_nocache (vdev->region[index].addr, + vdev->region[index].size);
I haven't looked at ioremap on arm, but if it's remotely non-trivially, it's probably a good idea to do this only on first access, cache it, and unmap it on region cleanup. Thanks,
Alex
Quoted 103 lines
drivers/vfio/platform/vfio_platform.c +94
-1+ +while (count) { +size_t filled; + +if (count >=4 && !(off %4 )) { +u32 val; + + val =ioread32 (io + off); +if (copy_to_user (buf, &val,4 )) + goto err; + + filled =4 ; + }else if (count >=2 && !(off %2 )) { +u16 val; + + val =ioread16 (io + off); +if (copy_to_user (buf, &val,2 )) + goto err; + + filled =2 ; + }else { +u8 val; + + val =ioread8 (io + off); +if (copy_to_user (buf, &val,1 )) + goto err; + + filled =1 ; + } + + + count -= filled; + done += filled; + off += filled; + buf += filled; + } + +iounmap (io); +return done; +err: +iounmap (io); +return -EFAULT ; }static ssize_t vfio_platform_write (void *device_data,const char __user *buf,size_t count,loff_t *ppos) { -return 0 ; +struct vfio_platform_device *vdev = device_data; +unsigned int index =VFIO_PLATFORM_OFFSET_TO_INDEX (*ppos); +loff_t off = *ppos &VFIO_PLATFORM_OFFSET_MASK ; +unsigned int done =0 ; +void __iomem *io; + +if (index >= vdev->num_regions) +return -EINVAL ; + + io =ioremap_nocache (vdev->region[index].addr, + vdev->region[index].size); + +while (count) { +size_t filled; + +if (count >=4 && !(off %4 )) { +u32 val; + +if (copy_from_user (&val, buf,4 )) + goto err; +iowrite32 (val, io + off); + + filled =4 ; + }else if (count >=2 && !(off %2 )) { +u16 val; + +if (copy_from_user (&val, buf,2 )) + goto err; +iowrite16 (val, io + off); + + filled =2 ; + }else { +u8 val; + +if (copy_from_user (&val, buf,1 )) + goto err; +iowrite8 (val, io + off); + + filled =1 ; + } + + count -= filled; + done += filled; + off += filled; + buf += filled; + } + +iounmap (io); +return done; +err: +iounmap (io); +return -EFAULT ; }static int vfio_platform_mmap (void *device_data,struct vm_area_struct *vma)
Re: [RFC PATCH v5 07/11] VFIO_PLATFORM: Read and write support for the device fd
Quoted 8 lines · Alex Williamson
Quoted 4 lines · Antonios Motakis
VFIO returns a file descriptor which we can use to manipulate the memory regions of the device. Since some memory regions we cannot mmap due to security concerns, we also allow to read and write to this file descriptor directly.
If there are regions we cannot mmap for security, why do we provide full read/write access to them?
What I refer to here is to the problem of mmaping an MMIO region that is not page-sized. For example if it is just a small number of registers which are not aligned on a page boundary, mmaping them would still make the whole page accessible to the guest, with unpredictable outcomes. But in these cases read/write is still OK.
Quoted 32 lines
Quoted 32 lines
--- drivers/vfio/platform/vfio_platform.c | 110 +++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 3 deletions(-)drivers/vfio/platform/vfio_platform.c +9
-2@@ -55,7 +55,8 @@ static int vfio_platform_regions_init(struct vfio_platform_device *vdev) region.addr = res->start; region.size =resource_size (res); - region.flags =0 ; + region.flags =VFIO_REGION_INFO_FLAG_READ + |VFIO_REGION_INFO_FLAG_WRITE ; vdev->region[i] = region; }@@ -153,13 +154,116 @@ static long vfio_platform_ioctl(void *device_data,static ssize_t vfio_platform_read (void *device_data,char __user *buf,size_t count,loff_t *ppos) { -return 0 ; +struct vfio_platform_device *vdev = device_data; +unsigned int index =VFIO_PLATFORM_OFFSET_TO_INDEX (*ppos); +loff_t off = *ppos &VFIO_PLATFORM_OFFSET_MASK ; +unsigned int done =0 ; +void __iomem *io; + +if (index >= vdev->num_regions)
Quoted 8 lines
drivers/vfio/platform/vfio_platform.c static long vfio_platform_ioctl(void *device_data, · line 167
Quoted 4 lines
drivers/vfio/platform/vfio_platform.c +4
-0+return -EINVAL ; + + io =ioremap_nocache (vdev->region[index].addr, + vdev->region[index].size);I haven't looked at ioremap on arm, but if it's remotely non-trivially, it's probably a good idea to do this only on first access, cache it, and unmap it on region cleanup. Thanks,
Ack.
Quoted 109 lines
Alex
Quoted 103 lines
drivers/vfio/platform/vfio_platform.c +94
-1+ +while (count) { +size_t filled; + +if (count >=4 && !(off %4 )) { +u32 val; + + val =ioread32 (io + off); +if (copy_to_user (buf, &val,4 )) + goto err; + + filled =4 ; + }else if (count >=2 && !(off %2 )) { +u16 val; + + val =ioread16 (io + off); +if (copy_to_user (buf, &val,2 )) + goto err; + + filled =2 ; + }else { +u8 val; + + val =ioread8 (io + off); +if (copy_to_user (buf, &val,1 )) + goto err; + + filled =1 ; + } + + + count -= filled; + done += filled; + off += filled; + buf += filled; + } + +iounmap (io); +return done; +err: +iounmap (io); +return -EFAULT ; }static ssize_t vfio_platform_write (void *device_data,const char __user *buf,size_t count,loff_t *ppos) { -return 0 ; +struct vfio_platform_device *vdev = device_data; +unsigned int index =VFIO_PLATFORM_OFFSET_TO_INDEX (*ppos); +loff_t off = *ppos &VFIO_PLATFORM_OFFSET_MASK ; +unsigned int done =0 ; +void __iomem *io; + +if (index >= vdev->num_regions) +return -EINVAL ; + + io =ioremap_nocache (vdev->region[index].addr, + vdev->region[index].size); + +while (count) { +size_t filled; + +if (count >=4 && !(off %4 )) { +u32 val; + +if (copy_from_user (&val, buf,4 )) + goto err; +iowrite32 (val, io + off); + + filled =4 ; + }else if (count >=2 && !(off %2 )) { +u16 val; + +if (copy_from_user (&val, buf,2 )) + goto err; +iowrite16 (val, io + off); + + filled =2 ; + }else { +u8 val; + +if (copy_from_user (&val, buf,1 )) + goto err; +iowrite8 (val, io + off); + + filled =1 ; + } + + count -= filled; + done += filled; + off += filled; + buf += filled; + } + +iounmap (io); +return done; +err: +iounmap (io); +return -EFAULT ; }static int vfio_platform_mmap (void *device_data,struct vm_area_struct *vma)
Signature · 2 lines
Antonios Motakis Virtual Open Systems
RFC PATCH v5 8/11 VFIO_PLATFORM: Support MMAP of MMIO regions
In reply to Antonios MotakisAllow to memory map the MMIO regions of the device so userspace can directly access them.
--- drivers/vfio/platform/vfio_platform.c | 40 ++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-)
drivers/vfio/platform/vfio_platform.c +39 -1
@@ -58,6 +58,11 @@ static int vfio_platform_regions_init(struct vfio_platform_device *vdev) region.flags =VFIO_REGION_INFO_FLAG_READ |VFIO_REGION_INFO_FLAG_WRITE ; +/* Only regions addressed with PAGE granularity can be MMAPed +* securely. */ +if (!(region.addr & ~PAGE_MASK ) && !(region.size & ~PAGE_MASK )) + region.flags |=VFIO_REGION_INFO_FLAG_MMAP ; + vdev->region[i] = region; }
@@ -268,7 +273,40 @@ err:static int vfio_platform_mmap (void *device_data,struct vm_area_struct *vma) { -return -EINVAL ; +struct vfio_platform_device *vdev = device_data; +unsigned int index; +u64 req_len, pgoff, req_start; +struct vfio_platform_region region; + + index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT -PAGE_SHIFT ); + +if (vma->vm_end < vma->vm_start) +return -EINVAL ; +if ((vma->vm_flags &VM_SHARED ) ==0 ) +return -EINVAL ; +if (index >= vdev->num_regions) +return -EINVAL ; +if (vma->vm_start & ~PAGE_MASK ) +return -EINVAL ; +if (vma->vm_end & ~PAGE_MASK ) +return -EINVAL ; + + region = vdev->region[index]; + + req_len = vma->vm_end - vma->vm_start; + pgoff = vma->vm_pgoff & + ((1U << (VFIO_PLATFORM_OFFSET_SHIFT -PAGE_SHIFT )) -1 ); + req_start = pgoff <<PAGE_SHIFT ; + +if (region.size <PAGE_SIZE || req_start + req_len > region.size) +return -EINVAL ; + + vma->vm_private_data = vdev; + vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); + vma->vm_pgoff = (region.addr >>PAGE_SHIFT ) + pgoff; + +return remap_pfn_range (vma, vma->vm_start, vma->vm_pgoff, + req_len, vma->vm_page_prot); }static const struct vfio_device_ops vfio_platform_ops = {
Signature · 1 line
1.8.3.2
Re: [RFC PATCH v5 08/11] VFIO_PLATFORM: Support MMAP of MMIO regions
Quoted 13 lines · Antonios Motakis
Allow to memory map the MMIO regions of the device so userspace can directly access them.
--- drivers/vfio/platform/vfio_platform.c | 40 ++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-)drivers/vfio/platform/vfio_platform.c +0
-0
drivers/vfio/platform/vfio_platform.c static int vfio_platform_regions_init(struct vfio_platform_device *vdev) · line 65
Quoted 8 lines
drivers/vfio/platform/vfio_platform.c +5
-0@@ -58,6 +58,11 @@ static int vfio_platform_regions_init(struct vfio_platform_device *vdev) region.flags =VFIO_REGION_INFO_FLAG_READ |VFIO_REGION_INFO_FLAG_WRITE ; +/* Only regions addressed with PAGE granularity can be MMAPed +* securely. */ +if (!(region.addr & ~PAGE_MASK ) && !(region.size & ~PAGE_MASK )) + region.flags |=VFIO_REGION_INFO_FLAG_MMAP ; +
Ok, so the security is only that we need page alignment. Maybe that could be made more clear in the previous patch. PCI has regions like the MSI-X table that we need to protect from userspace access beyond page alignment requirements. Thanks,
Alex
Quoted 45 lines
drivers/vfio/platform/vfio_platform.c +34
-1vdev->region[i] = region; }@@ -268,7 +273,40 @@ err:static int vfio_platform_mmap (void *device_data,struct vm_area_struct *vma) { -return -EINVAL ; +struct vfio_platform_device *vdev = device_data; +unsigned int index; +u64 req_len, pgoff, req_start; +struct vfio_platform_region region; + + index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT -PAGE_SHIFT ); + +if (vma->vm_end < vma->vm_start) +return -EINVAL ; +if ((vma->vm_flags &VM_SHARED ) ==0 ) +return -EINVAL ; +if (index >= vdev->num_regions) +return -EINVAL ; +if (vma->vm_start & ~PAGE_MASK ) +return -EINVAL ; +if (vma->vm_end & ~PAGE_MASK ) +return -EINVAL ; + + region = vdev->region[index]; + + req_len = vma->vm_end - vma->vm_start; + pgoff = vma->vm_pgoff & + ((1U << (VFIO_PLATFORM_OFFSET_SHIFT -PAGE_SHIFT )) -1 ); + req_start = pgoff <<PAGE_SHIFT ; + +if (region.size <PAGE_SIZE || req_start + req_len > region.size) +return -EINVAL ; + + vma->vm_private_data = vdev; + vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); + vma->vm_pgoff = (region.addr >>PAGE_SHIFT ) + pgoff; + +return remap_pfn_range (vma, vma->vm_start, vma->vm_pgoff, + req_len, vma->vm_page_prot); }static const struct vfio_device_ops vfio_platform_ops = {
Re: [RFC PATCH v5 08/11] VFIO_PLATFORM: Support MMAP of MMIO regions
Quoted 17 lines · Alex Williamson
Quoted 16 lines · Antonios Motakis
Allow to memory map the MMIO regions of the device so userspace can directly access them.
--- drivers/vfio/platform/vfio_platform.c | 40 ++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-)drivers/vfio/platform/vfio_platform.c +0
-0@@ -58,6 +58,11 @@ static int vfio_platform_regions_init(struct vfio_platform_device *vdev) region.flags =VFIO_REGION_INFO_FLAG_READ |VFIO_REGION_INFO_FLAG_WRITE ;
Quoted 10 lines
drivers/vfio/platform/vfio_platform.c static int vfio_platform_regions_init(struct vfio_platform_device *vdev) · line 65
Quoted 5 lines
drivers/vfio/platform/vfio_platform.c +5
-0+/* Only regions addressed with PAGE granularity can be MMAPed +* securely. */ +if (!(region.addr & ~PAGE_MASK ) && !(region.size & ~PAGE_MASK )) + region.flags |=VFIO_REGION_INFO_FLAG_MMAP ; +Ok, so the security is only that we need page alignment. Maybe that could be made more clear in the previous patch. PCI has regions like the MSI-X table that we need to protect from userspace access beyond page alignment requirements. Thanks,
Ack, I will make the previous description more clear.
Quoted 51 lines
Alex
Quoted 45 lines
drivers/vfio/platform/vfio_platform.c +34
-1vdev->region[i] = region; }@@ -268,7 +273,40 @@ err:static int vfio_platform_mmap (void *device_data,struct vm_area_struct *vma) { -return -EINVAL ; +struct vfio_platform_device *vdev = device_data; +unsigned int index; +u64 req_len, pgoff, req_start; +struct vfio_platform_region region; + + index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT -PAGE_SHIFT ); + +if (vma->vm_end < vma->vm_start) +return -EINVAL ; +if ((vma->vm_flags &VM_SHARED ) ==0 ) +return -EINVAL ; +if (index >= vdev->num_regions) +return -EINVAL ; +if (vma->vm_start & ~PAGE_MASK ) +return -EINVAL ; +if (vma->vm_end & ~PAGE_MASK ) +return -EINVAL ; + + region = vdev->region[index]; + + req_len = vma->vm_end - vma->vm_start; + pgoff = vma->vm_pgoff & + ((1U << (VFIO_PLATFORM_OFFSET_SHIFT -PAGE_SHIFT )) -1 ); + req_start = pgoff <<PAGE_SHIFT ; + +if (region.size <PAGE_SIZE || req_start + req_len > region.size) +return -EINVAL ; + + vma->vm_private_data = vdev; + vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot); + vma->vm_pgoff = (region.addr >>PAGE_SHIFT ) + pgoff; + +return remap_pfn_range (vma, vma->vm_start, vma->vm_pgoff, + req_len, vma->vm_page_prot); }static const struct vfio_device_ops vfio_platform_ops = {
Signature · 2 lines
Antonios Motakis Virtual Open Systems
RFC PATCH v5 9/11 VFIO_PLATFORM: Return IRQ info
In reply to Antonios MotakisReturn information for the interrupts exposed by the device. This patch extends VFIO_DEVICE_GET_INFO with the number of IRQs and enables VFIO_DEVICE_GET_IRQ_INFO
--- drivers/vfio/platform/Makefile | 2 +- drivers/vfio/platform/vfio_platform.c | 35 +++++++++++++-- drivers/vfio/platform/vfio_platform_irq.c | 63 +++++++++++++++++++++++++++ drivers/vfio/platform/vfio_platform_private.h | 11 +++++ 4 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 drivers/vfio/platform/vfio_platform_irq.c
drivers/vfio/platform/Makefile +1 -1
@@ -1,4 +1,4 @@ -vfio-platform-y := vfio_platform.o +vfio-platform-y := vfio_platform.o vfio_platform_irq.o obj-$(CONFIG_VFIO_PLATFORM) += vfio-platform.o
drivers/vfio/platform/vfio_platform.c +31 -4
@@ -79,6 +79,7 @@ static void vfio_platform_release(void *device_data)struct vfio_platform_device *vdev = device_data;vfio_platform_regions_cleanup (vdev); +vfio_platform_irq_cleanup (vdev);module_put (THIS_MODULE ); }
@@ -92,12 +93,22 @@ static int vfio_platform_open(void *device_data)if (ret)return ret; + ret =vfio_platform_irq_init (vdev); +if (ret) + goto err_irq; +if (!try_module_get (THIS_MODULE )) { -vfio_platform_regions_cleanup (vdev); -return -ENODEV ; + ret = -ENODEV ; + goto err_mod; }return 0 ; + +err_mod: +vfio_platform_irq_cleanup (vdev); +err_irq: +vfio_platform_regions_cleanup (vdev); +return ret; } static long vfio_platform_ioctl(void *device_data,
@@ -119,7 +130,7 @@ static long vfio_platform_ioctl(void *device_data, info.flags =VFIO_DEVICE_FLAGS_PLATFORM ; info.num_regions = vdev->num_regions; - info.num_irqs =0 ; + info.num_irqs = vdev->num_irqs;return copy_to_user ((void __user *)arg, &info, minsz);
@@ -145,7 +156,23 @@ static long vfio_platform_ioctl(void *device_data,return copy_to_user ((void __user *)arg, &info, minsz); }else if (cmd ==VFIO_DEVICE_GET_IRQ_INFO ) { -return -EINVAL ; +struct vfio_irq_info info; + + minsz = offsetofend(struct vfio_irq_info, count); + +if (copy_from_user (&info, (void __user *)arg, minsz)) +return -EFAULT ; + +if (info.argsz < minsz) +return -EINVAL ; + +if (info.index >= vdev->num_irqs) +return -EINVAL ; + + info.flags = vdev->irq[info.index].flags; + info.count = vdev->irq[info.index].count; + +return copy_to_user ((void __user *)arg, &info, minsz); }else if (cmd ==VFIO_DEVICE_SET_IRQS )return -EINVAL ;
drivers/vfio/platform/vfio_platform_irq.c new file +63 -0
63 lines not on this page · raw message
drivers/vfio/platform/vfio_platform_private.h +11 -0
@@ -24,6 +24,11 @@#define VFIO_PLATFORM_INDEX_TO_OFFSET (index) \ ((u64)(index) << VFIO_PLATFORM_OFFSET_SHIFT) +struct vfio_platform_irq { +u32 flags; +u32 count; +}; +struct vfio_platform_region {u64 addr;resource_size_t size;
@@ -34,6 +39,12 @@ struct vfio_platform_device {struct platform_device *pdev;struct vfio_platform_region *region;u32 num_regions; +struct vfio_platform_irq *irq; + u32 num_irqs; }; +extern int vfio_platform_irq_init (struct vfio_platform_device *vdev); + +extern void vfio_platform_irq_cleanup (struct vfio_platform_device *vdev); +#endif /* VFIO_PLATFORM_PRIVATE_H */
Signature · 1 line
1.8.3.2
Re: [RFC PATCH v5 09/11] VFIO_PLATFORM: Return IRQ info
Quoted 39 lines · Antonios Motakis
Return information for the interrupts exposed by the device. This patch extends
VFIO_DEVICE_GET_INFOwith the number of IRQs and enablesVFIO_DEVICE_GET_IRQ_INFO--- drivers/vfio/platform/Makefile | 2 +- drivers/vfio/platform/vfio_platform.c | 35 +++++++++++++-- drivers/vfio/platform/vfio_platform_irq.c | 63 +++++++++++++++++++++++++++ drivers/vfio/platform/vfio_platform_private.h | 11 +++++ 4 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 drivers/vfio/platform/vfio_platform_irq.cdrivers/vfio/platform/Makefile +1
-1@@ -1,4 +1,4 @@ -vfio-platform-y := vfio_platform.o +vfio-platform-y := vfio_platform.o vfio_platform_irq.o obj-$(CONFIG_VFIO_PLATFORM) += vfio-platform.odrivers/vfio/platform/vfio_platform.c +1
-0@@ -79,6 +79,7 @@ static void vfio_platform_release(void *device_data)struct vfio_platform_device *vdev = device_data;vfio_platform_regions_cleanup (vdev); +vfio_platform_irq_cleanup (vdev);module_put (THIS_MODULE ); }@@ -92,12 +93,22 @@ static int vfio_platform_open(void *device_data)if (ret)return ret;
drivers/vfio/platform/vfio_platform.c static int vfio_platform_open(void *device_data) · line 103
Quoted 10 lines
drivers/vfio/platform/vfio_platform.c +6
-2+ ret =vfio_platform_irq_init (vdev); +if (ret) + goto err_irq; +if (!try_module_get (THIS_MODULE )) { -vfio_platform_regions_cleanup (vdev); -return -ENODEV ; + ret = -ENODEV ; + goto err_mod; }
Same comments from REGION_INFO patch apply here. Ordering above, paranoia about keeping num_irqs consistent with allocation, nit about local variable use in initialization loop. Thanks,
Alex
Quoted 143 lines
drivers/vfio/platform/vfio_platform.c +24
-2return 0 ; + +err_mod: +vfio_platform_irq_cleanup (vdev); +err_irq: +vfio_platform_regions_cleanup (vdev); +return ret; } static long vfio_platform_ioctl(void *device_data,@@ -119,7 +130,7 @@ static long vfio_platform_ioctl(void *device_data, info.flags =VFIO_DEVICE_FLAGS_PLATFORM ; info.num_regions = vdev->num_regions; - info.num_irqs =0 ; + info.num_irqs = vdev->num_irqs;return copy_to_user ((void __user *)arg, &info, minsz);@@ -145,7 +156,23 @@ static long vfio_platform_ioctl(void *device_data,return copy_to_user ((void __user *)arg, &info, minsz); }else if (cmd ==VFIO_DEVICE_GET_IRQ_INFO ) { -return -EINVAL ; +struct vfio_irq_info info; + + minsz = offsetofend(struct vfio_irq_info, count); + +if (copy_from_user (&info, (void __user *)arg, minsz)) +return -EFAULT ; + +if (info.argsz < minsz) +return -EINVAL ; + +if (info.index >= vdev->num_irqs) +return -EINVAL ; + + info.flags = vdev->irq[info.index].flags; + info.count = vdev->irq[info.index].count; + +return copy_to_user ((void __user *)arg, &info, minsz); }else if (cmd ==VFIO_DEVICE_SET_IRQS )return -EINVAL ;drivers/vfio/platform/vfio_platform_irq.c new file +63
-0@@ -0,0 +1,63 @@ +/* +* VFIO platform devices interrupt handling +* +* Copyright (C) 2013 - Virtual Open Systems +* Author: Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[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. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <linux/device.h> +#include <linux/eventfd.h> +#include <linux/interrupt.h> +#include <linux/iommu.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/notifier.h> +#include <linux/pm_runtime.h> +#include <linux/slab.h> +#include <linux/types.h> +#include <linux/uaccess.h> +#include <linux/vfio.h> +#include <linux/platform_device.h> +#include <linux/irq.h> + +#include "vfio_platform_private.h" + +int vfio_platform_irq_init (struct vfio_platform_device *vdev) +{ +int cnt =0 , i; + +while (platform_get_irq (vdev->pdev, cnt) >0 ) + cnt++; + + vdev->num_irqs = cnt; + + vdev->irq =kzalloc (sizeof (struct vfio_platform_irq ) * vdev->num_irqs, +GFP_KERNEL ); +if (!vdev->irq) +return -ENOMEM ; + +for (i =0 ; i < cnt; i++) { +struct vfio_platform_irq irq; + + irq.flags =0 ; + irq.count =1 ; + + vdev->irq[i] = irq; + } + +return 0 ; +} + +void vfio_platform_irq_cleanup (struct vfio_platform_device *vdev) +{ +kfree (vdev->irq); +}drivers/vfio/platform/vfio_platform_private.h +11
-0@@ -24,6 +24,11 @@#define VFIO_PLATFORM_INDEX_TO_OFFSET (index) \ ((u64)(index) << VFIO_PLATFORM_OFFSET_SHIFT) +struct vfio_platform_irq { +u32 flags; +u32 count; +}; +struct vfio_platform_region {u64 addr;resource_size_t size;@@ -34,6 +39,12 @@ struct vfio_platform_device {struct platform_device *pdev;struct vfio_platform_region *region;u32 num_regions; +struct vfio_platform_irq *irq; + u32 num_irqs; }; +extern int vfio_platform_irq_init (struct vfio_platform_device *vdev); + +extern void vfio_platform_irq_cleanup (struct vfio_platform_device *vdev); +#endif /* VFIO_PLATFORM_PRIVATE_H */
RFC PATCH v5 10/11 VFIO_PLATFORM: Initial interrupts support
In reply to Antonios MotakisThis patch allows to set an eventfd for a patform device's interrupt, and also to trigger the interrupt eventfd from userspace for testing.
--- drivers/vfio/platform/vfio_platform.c | 36 +++++++- drivers/vfio/platform/vfio_platform_irq.c | 123 +++++++++++++++++++++++++- drivers/vfio/platform/vfio_platform_private.h | 7 ++ 3 files changed, 162 insertions(+), 4 deletions(-)
drivers/vfio/platform/vfio_platform.c +33 -3
@@ -174,10 +174,40 @@ static long vfio_platform_ioctl(void *device_data,return copy_to_user ((void __user *)arg, &info, minsz); - }else if (cmd ==VFIO_DEVICE_SET_IRQS ) -return -EINVAL ; + }else if (cmd ==VFIO_DEVICE_SET_IRQS ) { +struct vfio_irq_set hdr; +int ret =0 ; + + minsz = offsetofend(struct vfio_irq_set, count); + +if (copy_from_user (&hdr, (void __user *)arg, minsz)) +return -EFAULT ; + +if (hdr.argsz < minsz) +return -EINVAL ; + +if (hdr.index >= vdev->num_irqs) +return -EINVAL ; + +if (hdr.start !=0 || hdr.count >1 ) +return -EINVAL ; + +if (hdr.count ==0 && + (!(hdr.flags &VFIO_IRQ_SET_DATA_NONE ) || + !(hdr.flags &VFIO_IRQ_SET_ACTION_TRIGGER ))) +return -EINVAL ; + +if (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK | +VFIO_IRQ_SET_ACTION_TYPE_MASK )) +return -EINVAL ; + + ret =vfio_platform_set_irqs_ioctl (vdev, hdr.flags, hdr.index, + hdr.start, hdr.count, + (void *)arg+minsz); + +return ret; -else if (cmd ==VFIO_DEVICE_RESET ) + }else if (cmd ==VFIO_DEVICE_RESET )return -EINVAL ;return -ENOTTY ;
drivers/vfio/platform/vfio_platform_irq.c +122 -1
142 lines not on this page · raw message
drivers/vfio/platform/vfio_platform_private.h +7 -0
@@ -25,8 +25,11 @@ ((u64)(index) << VFIO_PLATFORM_OFFSET_SHIFT)struct vfio_platform_irq { +struct eventfd_ctx *trigger;u32 flags;u32 count; +int hwirq; +char *name; };struct vfio_platform_region {
@@ -47,4 +50,8 @@ extern int vfio_platform_irq_init(struct vfio_platform_device *vdev);extern void vfio_platform_irq_cleanup (struct vfio_platform_device *vdev); +extern int vfio_platform_set_irqs_ioctl (struct vfio_platform_device *vdev, +uint32_t flags,unsigned index,unsigned start, +unsigned count,void *data); +#endif /* VFIO_PLATFORM_PRIVATE_H */
Signature · 1 line
1.8.3.2
RFC PATCH v5 11/11 VFIO_PLATFORM: Support for maskable and automasked interrupts
In reply to Antonios MotakisAdds support to mask interrupts, and also for automasked interrupts. Level sensitive interrupts are exposed as automasked interrupts and are masked and disabled automatically when they fire.
--- drivers/vfio/platform/vfio_platform_irq.c | 117 ++++++++++++++++++++++++-- drivers/vfio/platform/vfio_platform_private.h | 2 + 2 files changed, 113 insertions(+), 6 deletions(-)
drivers/vfio/platform/vfio_platform_irq.c +111 -6
@@ -52,9 +52,16 @@ int vfio_platform_irq_init(struct vfio_platform_device *vdev)struct vfio_platform_irq irq;int hwirq =platform_get_irq (vdev->pdev, i); - irq.flags =VFIO_IRQ_INFO_EVENTFD ; +spin_lock_init (&irq.lock); + + irq.flags =VFIO_IRQ_INFO_EVENTFD |VFIO_IRQ_INFO_MASKABLE ; + +if (irq_get_trigger_type (hwirq) &IRQ_TYPE_LEVEL_MASK ) + irq.flags |=VFIO_IRQ_INFO_AUTOMASKED ; + irq.count =1 ; irq.hwirq = hwirq; + irq.masked = false; vdev->irq[i] = irq; }
@@ -66,19 +73,39 @@ void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev) {int i; -for (i =0 ; i < vdev->num_irqs; i++) +for (i =0 ; i < vdev->num_irqs; i++) {vfio_set_trigger (vdev, i,-1 ); +if (vdev->irq[i].masked) +enable_irq (vdev->irq[i].hwirq); + } +kfree (vdev->irq); }static irqreturn_t vfio_irq_handler (int irq,void *dev_id) { -struct eventfd_ctx *trigger = dev_id; +struct vfio_platform_irq *irq_ctx = dev_id; +unsigned long flags; +int ret =IRQ_NONE ; + +spin_lock_irqsave (&irq_ctx->lock, flags); + +if (!irq_ctx->masked) { + ret =IRQ_HANDLED ; + +if (irq_ctx->flags &VFIO_IRQ_INFO_AUTOMASKED ) { +disable_irq_nosync (irq_ctx->hwirq); + irq_ctx->masked = true; + } + } -eventfd_signal (trigger,1 ); +spin_unlock_irqrestore (&irq_ctx->lock, flags); -return IRQ_HANDLED ; +if (ret ==IRQ_HANDLED ) +eventfd_signal (irq_ctx->trigger,1 ); + +return ret; } static int vfio_set_trigger(struct vfio_platform_device *vdev,
@@ -159,6 +186,82 @@ static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev,return -EFAULT ; } +static int vfio_platform_set_irq_unmask (struct vfio_platform_device *vdev, +unsigned index,unsigned start, +unsigned count,uint32_t flags,void *data) +{ +uint8_t arr; + +if (start !=0 || count !=1 ) +return -EINVAL ; + +switch (flags &VFIO_IRQ_SET_DATA_TYPE_MASK ) { +case VFIO_IRQ_SET_DATA_BOOL : +if (copy_from_user (&arr, data,sizeof (uint8_t ))) +return -EFAULT ; + +if (arr !=0x1 ) +return -EINVAL ; + +case VFIO_IRQ_SET_DATA_NONE : + +spin_lock_irq (&vdev->irq[index].lock); + +if (vdev->irq[index].masked) { +enable_irq (vdev->irq[index].hwirq); + vdev->irq[index].masked = false; + } + +spin_unlock_irq (&vdev->irq[index].lock); + +return 0 ; + +case VFIO_IRQ_SET_DATA_EVENTFD :/* XXX not implemented yet */ +default : +return -ENOTTY ; + } + +return 0 ; +} + +static int vfio_platform_set_irq_mask (struct vfio_platform_device *vdev, +unsigned index,unsigned start, +unsigned count,uint32_t flags,void *data) +{ +uint8_t arr; + +if (start !=0 || count !=1 ) +return -EINVAL ; + +switch (flags &VFIO_IRQ_SET_DATA_TYPE_MASK ) { +case VFIO_IRQ_SET_DATA_BOOL : +if (copy_from_user (&arr, data,sizeof (uint8_t ))) +return -EFAULT ; + +if (arr !=0x1 ) +return -EINVAL ; + +case VFIO_IRQ_SET_DATA_NONE : + +spin_lock_irq (&vdev->irq[index].lock); + +if (!vdev->irq[index].masked) { +disable_irq (vdev->irq[index].hwirq); + vdev->irq[index].masked = true; + } + +spin_unlock_irq (&vdev->irq[index].lock); + +return 0 ; + +case VFIO_IRQ_SET_DATA_EVENTFD :/* XXX not implemented yet */ +default : +return -ENOTTY ; + } + +return 0 ; +} +int vfio_platform_set_irqs_ioctl (struct vfio_platform_device *vdev,uint32_t flags,unsigned index,unsigned start,unsigned count,void *data)
@@ -169,8 +272,10 @@ int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev,switch (flags &VFIO_IRQ_SET_ACTION_TYPE_MASK ) {case VFIO_IRQ_SET_ACTION_MASK : + func = vfio_platform_set_irq_mask; +break ;case VFIO_IRQ_SET_ACTION_UNMASK : -/* XXX not implemented */ + func = vfio_platform_set_irq_unmask;break ;case VFIO_IRQ_SET_ACTION_TRIGGER : func = vfio_platform_set_irq_trigger;
drivers/vfio/platform/vfio_platform_private.h +2 -0
@@ -30,6 +30,8 @@ struct vfio_platform_irq {u32 count;int hwirq;char *name; +bool masked; +spinlock_t lock; };struct vfio_platform_region {
Signature · 1 line
1.8.3.2
Re: [RFC PATCH v5 11/11] VFIO_PLATFORM: Support for maskable and automasked interrupts
Quoted 33 lines · Antonios Motakis
Adds support to mask interrupts, and also for automasked interrupts. Level sensitive interrupts are exposed as automasked interrupts and are masked and disabled automatically when they fire.
--- drivers/vfio/platform/vfio_platform_irq.c | 117 ++++++++++++++++++++++++-- drivers/vfio/platform/vfio_platform_private.h | 2 + 2 files changed, 113 insertions(+), 6 deletions(-)drivers/vfio/platform/vfio_platform_irq.c +8
-1@@ -52,9 +52,16 @@ int vfio_platform_irq_init(struct vfio_platform_device *vdev)struct vfio_platform_irq irq;int hwirq =platform_get_irq (vdev->pdev, i); - irq.flags =VFIO_IRQ_INFO_EVENTFD ; +spin_lock_init (&irq.lock); + + irq.flags =VFIO_IRQ_INFO_EVENTFD |VFIO_IRQ_INFO_MASKABLE ; + +if (irq_get_trigger_type (hwirq) &IRQ_TYPE_LEVEL_MASK ) + irq.flags |=VFIO_IRQ_INFO_AUTOMASKED ; + irq.count =1 ; irq.hwirq = hwirq; + irq.masked = false; vdev->irq[i] = irq; }
drivers/vfio/platform/vfio_platform_irq.c void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev) · line 80
Quoted 9 lines
drivers/vfio/platform/vfio_platform_irq.c +3
-1@@ -66,19 +73,39 @@ void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev) {int i; -for (i =0 ; i < vdev->num_irqs; i++) +for (i =0 ; i < vdev->num_irqs; i++) {vfio_set_trigger (vdev, i,-1 ); +if (vdev->irq[i].masked) +enable_irq (vdev->irq[i].hwirq);
This looks suspicious. set_trigger(,, -1) calls free_irq() and here we enable_irq(). Shouldn't the nexe user's call to request_irq() be sufficient to re-enable it? Thanks,
Alex
Quoted 142 lines
drivers/vfio/platform/vfio_platform_irq.c +100
-4+ } +kfree (vdev->irq); }static irqreturn_t vfio_irq_handler (int irq,void *dev_id) { -struct eventfd_ctx *trigger = dev_id; +struct vfio_platform_irq *irq_ctx = dev_id; +unsigned long flags; +int ret =IRQ_NONE ; + +spin_lock_irqsave (&irq_ctx->lock, flags); + +if (!irq_ctx->masked) { + ret =IRQ_HANDLED ; + +if (irq_ctx->flags &VFIO_IRQ_INFO_AUTOMASKED ) { +disable_irq_nosync (irq_ctx->hwirq); + irq_ctx->masked = true; + } + } -eventfd_signal (trigger,1 ); +spin_unlock_irqrestore (&irq_ctx->lock, flags); -return IRQ_HANDLED ; +if (ret ==IRQ_HANDLED ) +eventfd_signal (irq_ctx->trigger,1 ); + +return ret; } static int vfio_set_trigger(struct vfio_platform_device *vdev,@@ -159,6 +186,82 @@ static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev,return -EFAULT ; } +static int vfio_platform_set_irq_unmask (struct vfio_platform_device *vdev, +unsigned index,unsigned start, +unsigned count,uint32_t flags,void *data) +{ +uint8_t arr; + +if (start !=0 || count !=1 ) +return -EINVAL ; + +switch (flags &VFIO_IRQ_SET_DATA_TYPE_MASK ) { +case VFIO_IRQ_SET_DATA_BOOL : +if (copy_from_user (&arr, data,sizeof (uint8_t ))) +return -EFAULT ; + +if (arr !=0x1 ) +return -EINVAL ; + +case VFIO_IRQ_SET_DATA_NONE : + +spin_lock_irq (&vdev->irq[index].lock); + +if (vdev->irq[index].masked) { +enable_irq (vdev->irq[index].hwirq); + vdev->irq[index].masked = false; + } + +spin_unlock_irq (&vdev->irq[index].lock); + +return 0 ; + +case VFIO_IRQ_SET_DATA_EVENTFD :/* XXX not implemented yet */ +default : +return -ENOTTY ; + } + +return 0 ; +} + +static int vfio_platform_set_irq_mask (struct vfio_platform_device *vdev, +unsigned index,unsigned start, +unsigned count,uint32_t flags,void *data) +{ +uint8_t arr; + +if (start !=0 || count !=1 ) +return -EINVAL ; + +switch (flags &VFIO_IRQ_SET_DATA_TYPE_MASK ) { +case VFIO_IRQ_SET_DATA_BOOL : +if (copy_from_user (&arr, data,sizeof (uint8_t ))) +return -EFAULT ; + +if (arr !=0x1 ) +return -EINVAL ; + +case VFIO_IRQ_SET_DATA_NONE : + +spin_lock_irq (&vdev->irq[index].lock); + +if (!vdev->irq[index].masked) { +disable_irq (vdev->irq[index].hwirq); + vdev->irq[index].masked = true; + } + +spin_unlock_irq (&vdev->irq[index].lock); + +return 0 ; + +case VFIO_IRQ_SET_DATA_EVENTFD :/* XXX not implemented yet */ +default : +return -ENOTTY ; + } + +return 0 ; +} +int vfio_platform_set_irqs_ioctl (struct vfio_platform_device *vdev,uint32_t flags,unsigned index,unsigned start,unsigned count,void *data)@@ -169,8 +272,10 @@ int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev,switch (flags &VFIO_IRQ_SET_ACTION_TYPE_MASK ) {case VFIO_IRQ_SET_ACTION_MASK : + func = vfio_platform_set_irq_mask; +break ;case VFIO_IRQ_SET_ACTION_UNMASK : -/* XXX not implemented */ + func = vfio_platform_set_irq_unmask;break ;case VFIO_IRQ_SET_ACTION_TRIGGER : func = vfio_platform_set_irq_trigger;drivers/vfio/platform/vfio_platform_private.h +2
-0@@ -30,6 +30,8 @@ struct vfio_platform_irq {u32 count;int hwirq;char *name; +bool masked; +spinlock_t lock; };struct vfio_platform_region {
Re: [RFC PATCH v5 11/11] VFIO_PLATFORM: Support for maskable and automasked interrupts
Quoted 42 lines · Alex Williamson
Quoted 40 lines · Antonios Motakis
Adds support to mask interrupts, and also for automasked interrupts. Level sensitive interrupts are exposed as automasked interrupts and are masked and disabled automatically when they fire.
--- drivers/vfio/platform/vfio_platform_irq.c | 117 ++++++++++++++++++++++++-- drivers/vfio/platform/vfio_platform_private.h | 2 + 2 files changed, 113 insertions(+), 6 deletions(-)drivers/vfio/platform/vfio_platform_irq.c +9
-2@@ -52,9 +52,16 @@ int vfio_platform_irq_init(struct vfio_platform_device *vdev)struct vfio_platform_irq irq;int hwirq =platform_get_irq (vdev->pdev, i); - irq.flags =VFIO_IRQ_INFO_EVENTFD ; +spin_lock_init (&irq.lock); + + irq.flags =VFIO_IRQ_INFO_EVENTFD |VFIO_IRQ_INFO_MASKABLE ; + +if (irq_get_trigger_type (hwirq) &IRQ_TYPE_LEVEL_MASK ) + irq.flags |=VFIO_IRQ_INFO_AUTOMASKED ; + irq.count =1 ; irq.hwirq = hwirq; + irq.masked = false; vdev->irq[i] = irq; }@@ -66,19 +73,39 @@ void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev) {int i; -for (i =0 ; i < vdev->num_irqs; i++) +for (i =0 ; i < vdev->num_irqs; i++) {vfio_set_trigger (vdev, i,-1 );
Quoted 7 lines
drivers/vfio/platform/vfio_platform_irq.c void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev) · line 80
Quoted 2 lines
drivers/vfio/platform/vfio_platform_irq.c +2
-0+if (vdev->irq[i].masked) +enable_irq (vdev->irq[i].hwirq);This looks suspicious.
set_trigger(,, -1)callsfree_irq()and here weenable_irq(). Shouldn't the nexe user's call torequest_irq()be sufficient to re-enable it? Thanks,
This is actually called when we unbind from the device, so it is indeed not needed.
Quoted 2 lines
Alex