Fix BUG_ON in vfio_iommu_group_notifier()
Subsystems (10)
v8 cover Fix BUG_ON in vfio_iommu_group_notifier()
Hi folks,
The iommu group is the minimal isolation boundary for DMA. Devices in a group can access each other's MMIO registers via peer to peer DMA and also need share the same I/O address space.
Once the I/O address space is assigned to user control it is no longer available to the dma_map* API, which effectively makes the DMA API non-working.
Second, userspace can use DMA initiated by a device that it controls to access the MMIO spaces of other devices in the group. This allows userspace to indirectly attack any kernel owned device and it's driver.
Therefore groups must either be entirely under kernel control or userspace control, never a mixture. Unfortunately some systems have problems with the granularity of groups and there are a couple of important exceptions:
- pci_stub allows the admin to block driver binding on a device and make it permanently shared with userspace. Since PCI stub does not do DMA it is safe, however the admin must understand that using pci_stub allows userspace to attack whatever device it was bound it.
- PCI bridges are sometimes included in groups. Typically PCI bridges do not use DMA, and generally do not have MMIO regions.
Generally any device that does not have any MMIO registers is a possible candidate for an exception.
Currently vfio adopts a workaround to detect violations of the above restrictions by monitoring the driver core BOUND event, and hardwiring the above exceptions. Since there is no way for vfio to reject driver binding at this point, BUG_ON() is triggered if a violation is captured (kernel driver BOUND event on a group which already has some devices assigned to userspace). Aside from the bad user experience this opens a way for root userspace to crash the kernel, even in high integrity configurations, by manipulating the module binding and triggering the BUG_ON.
This series solves this problem by making the user/kernel ownership a core concept at the IOMMU layer. The driver core enforces kernel ownership while drivers are bound and violations now result in a error codes during probe, not BUG_ON failures.
Patch partitions:
[PATCH 1-4]: Detect DMA ownership conflicts during driver binding; [PATCH 5-7]: Add security context management for assigned devices; [PATCH 8-11]: Various cleanups.
This is also part one of three initial series for IOMMUFD:
* Move IOMMU Group security into the iommu layer
- Generic IOMMUFD implementation
- VFIO ability to consume IOMMUFD
Change log:
v1: initial post
- https://lore.kernel.org/linux-iommu/[email protected]/
v2:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Move kernel dma ownership auto-claiming from driver core to bus
callback. [Greg/Christoph/Robin/Jason]
https://lore.kernel.org/linux-iommu/[email protected]/T/#m153706912b770682cb12e3c28f57e171aa1f9d0c- Code and interface refactoring for iommu_set/release_dma_owner()
interfaces. [Jason]
https://lore.kernel.org/linux-iommu/[email protected]/T/#mea70ed8e4e3665aedf32a5a0a7db095bf680325e - [NEW]Add new iommu_attach/detach_device_shared() interfaces for multiple devices group. [Robin/Jason]
https://lore.kernel.org/linux-iommu/[email protected]/T/#mea70ed8e4e3665aedf32a5a0a7db095bf680325e
- [NEW]Use iommu_attach/detach_device_shared() in drm/tegra drivers.
- Refactoring and description refinement.
v3:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Rename bus_type::dma_unconfigure to bus_type::dma_cleanup. [Greg]
https://lore.kernel.org/linux-iommu/[email protected]/T/#m6711e041e47cb0cbe3964fad0a3466f5ae4b3b9b
- Avoid _platform_dma_configure for platform_bus_type::dma_configure.
[Greg]
https://lore.kernel.org/linux-iommu/[email protected]/T/#m43fc46286611aa56a5c0eeaad99d539e5519f3f6- Patch "0012-iommu-Add-iommu_at-de-tach_device_shared-for-mult.patch" and "0018-drm-tegra-Use-the-iommu-dma_owner-mechanism.patch" have been tested by Dmitry Osipenko <[email protected]>.
v4:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Remove unnecessary tegra->domain chech in the tegra patch. (Jason)
- Remove DMA_OWNER_NONE. (Joerg)
- Change refcount to unsigned int. (Christoph)
- Move mutex lock into group set_dma_owner functions. (Christoph)
- Add kernel doc for iommu_attach/detach_domain_shared(). (Christoph)
- Move dma auto-claim into driver core. (Jason/Christoph)
v5:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Move kernel dma ownership auto-claiming from driver core to bus callback. (Greg)
- Refactor the iommu interfaces to make them more specific.
(Jason/Robin)
- Simplify the dma ownership implementation by removing the owner
type. (Jason)
- Commit message refactoring for PCI drivers. (Bjorn)
- Move iommu_attach/detach_device() improvement patches into another series as there are a lot of code refactoring and cleanup staffs
in various device drivers.
v6:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Refine comments and commit mesages.
- Rename iommu_group_set_dma_owner() to iommu_group_claim_dma_owner().
- Rename iommu_device_use/unuse_kernel_dma() to iommu_device_use/unuse_default_domain().
- Remove unnecessary EXPORT_SYMBOL_GPL.
- Change flag name from no_kernel_api_dma to driver_managed_dma.
- Merge 4 "Add driver dma ownership management" patches into single
one.
v7:
- We discussed about adding some fields in driver structure and intercepting it in the bus notifier for driver unbinding. We agreed that the driver structure should not be used out of the driver core.
- As iommu_group_claim/release_dma_owner() are only used by the VFIO, there're no use cases for multiple calls for a single group.
- Add some commit messages in "vfio: Set DMA ownership for VFIO" to describe the intentional enhancement of unsafe bridge drivers.
- Comments refinement.
v8:
- Move iommu_use_default_domain() to the end of .dma_configure callback to avoid firmware-data-ordering thing.
- Add Acked-by from PCI and VFIO maintainers.
This is based on next branch of linux-iommu tree:
https://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu.git
and also available on github:
https://github.com/LuBaolu/intel-iommu/commits/iommu-dma-ownership-v8
Best regards,
baolu
Jason Gunthorpe (1):
vfio: Delete the unbound_list
Lu Baolu (10):
iommu: Add DMA ownership management interfaces
driver core: Add dma_cleanup callback in bus_type
amba: Stop sharing platform_dma_configure()
bus: platform,amba,fsl-mc,PCI: Add device DMA ownership management
PCI: pci_stub: Set driver_managed_dma
PCI: portdrv: Set driver_managed_dma
vfio: Set DMA ownership for VFIO devices
vfio: Remove use of vfio_group_viable()
vfio: Remove iommu group notifier
iommu: Remove iommu group changes notifier
include/linux/amba/bus.h | 8 + include/linux/device/bus.h | 3 + include/linux/fsl/mc.h | 8 + include/linux/iommu.h | 54 +++--- include/linux/pci.h | 8 + include/linux/platform_device.h | 10 +- drivers/amba/bus.c | 37 +++- drivers/base/dd.c | 5 + drivers/base/platform.c | 21 ++- drivers/bus/fsl-mc/fsl-mc-bus.c | 24 ++- drivers/iommu/iommu.c | 228 ++++++++++++++++-------- drivers/pci/pci-driver.c | 18 ++ drivers/pci/pci-stub.c | 1 + drivers/pci/pcie/portdrv_pci.c | 2 + drivers/vfio/fsl-mc/vfio_fsl_mc.c | 1 + drivers/vfio/pci/vfio_pci.c | 1 + drivers/vfio/platform/vfio_amba.c | 1 + drivers/vfio/platform/vfio_platform.c | 1 + drivers/vfio/vfio.c | 245 ++------------------------ 19 files changed, 338 insertions(+), 338 deletions(-)
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v8 1/11 iommu: Add DMA ownership management interfaces
Multiple devices may be placed in the same IOMMU group because they cannot be isolated from each other. These devices must either be entirely under kernel control or userspace control, never a mixture.
This adds dma ownership management in iommu core and exposes several interfaces for the device drivers and the device userspace assignment framework (i.e. VFIO), so that any conflict between user and kernel controlled dma could be detected at the beginning.
The device driver oriented interfaces are,
int iommu_device_use_default_domain(struct device *dev); void iommu_device_unuse_default_domain(struct device *dev);
By calling iommu_device_use_default_domain(), the device driver tells the iommu layer that the device dma is handled through the kernel DMA APIs. The iommu layer will manage the IOVA and use the default domain for DMA address translation.
The device user-space assignment framework oriented interfaces are,
int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner); void iommu_group_release_dma_owner(struct iommu_group *group); bool iommu_group_dma_owner_claimed(struct iommu_group *group);
The device userspace assignment must be disallowed if the DMA owner claiming interface returns failure.
--- include/linux/iommu.h | 31 +++++++++ drivers/iommu/iommu.c | 153 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 181 insertions(+), 3 deletions(-)
include/linux/iommu.h +31 -0
@@ -675,6 +675,13 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev,void iommu_sva_unbind_device (struct iommu_sva *handle);u32 iommu_sva_get_pasid (struct iommu_sva *handle); +int iommu_device_use_default_domain (struct device *dev); +void iommu_device_unuse_default_domain (struct device *dev); + +int iommu_group_claim_dma_owner (struct iommu_group *group,void *owner); +void iommu_group_release_dma_owner (struct iommu_group *group); +bool iommu_group_dma_owner_claimed (struct iommu_group *group); +#else /* CONFIG_IOMMU_API */ struct iommu_ops {};
@@ -1031,6 +1038,30 @@ static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev) {return NULL ; } + +static inline int iommu_device_use_default_domain (struct device *dev) +{ +return 0 ; +} + +static inline void iommu_device_unuse_default_domain (struct device *dev) +{ +} + +static inline int +iommu_group_claim_dma_owner (struct iommu_group *group,void *owner) +{ +return -ENODEV ; +} + +static inline void iommu_group_release_dma_owner (struct iommu_group *group) +{ +} + +static inline bool iommu_group_dma_owner_claimed (struct iommu_group *group) +{ +return false; +}#endif /* CONFIG_IOMMU_API */ /**
drivers/iommu/iommu.c +150 -3
180 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v8 01/11] iommu: Add DMA ownership management interfaces
Quoted 21 lines · Lu Baolu
Multiple devices may be placed in the same IOMMU group because they cannot be isolated from each other. These devices must either be entirely under kernel control or userspace control, never a mixture.
This adds dma ownership management in iommu core and exposes several interfaces for the device drivers and the device userspace assignment framework (i.e. VFIO), so that any conflict between user and kernel controlled dma could be detected at the beginning.
The device driver oriented interfaces are,
int iommu_device_use_default_domain(struct device *dev); void iommu_device_unuse_default_domain(struct device *dev);By calling
iommu_device_use_default_domain(), the device driver tells the iommu layer that the device dma is handled through the kernel DMA APIs. The iommu layer will manage the IOVA and use the default domain for DMA address translation.The device user-space assignment framework oriented interfaces are,
Quoted 7 lines
int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner); void iommu_group_release_dma_owner(struct iommu_group *group); bool iommu_group_dma_owner_claimed(struct iommu_group *group);The device userspace assignment must be disallowed if the DMA owner claiming interface returns failure.
Quoted 3 lines
Signed-off-by Jason Gunthorpe <[email protected]>Signed-off-by Kevin Tian <[email protected]>Signed-off-by Lu Baolu <[email protected]>
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v8 2/11 driver core: Add dma_cleanup callback in bus_type
In reply to Lu BaoluThe bus_type structure defines dma_configure() callback for bus drivers to configure DMA on the devices. This adds the paired dma_cleanup() callback and calls it during driver unbinding so that bus drivers can do some cleanup work.
One use case for this paired DMA callbacks is for the bus driver to check for DMA ownership conflicts during driver binding, where multiple devices belonging to a same IOMMU group (the minimum granularity of isolation and protection) may be assigned to kernel drivers or user space respectively.
Without this change, for example, the vfio driver has to listen to a bus BOUND_DRIVER event and then BUG_ON() in case of dma ownership conflict. This leads to bad user experience since careless driver binding operation may crash the system if the admin overlooks the group restriction. Aside from bad design, this leads to a security problem as a root user, even with lockdown=integrity, can force the kernel to BUG.
With this change, the bus driver could check and set the DMA ownership in driver binding process and fail on ownership conflicts. The DMA ownership should be released during driver unbinding.
--- include/linux/device/bus.h | 3 +++ drivers/base/dd.c | 5 +++++ 2 files changed, 8 insertions(+)
include/linux/device/bus.h +3 -0
@@ -59,6 +59,8 @@ struct fwnode_handle;* bus supports. * @dma_configure: Called to setup DMA configuration on a device on * this bus. +* @dma_cleanup: Called to cleanup DMA configuration on a device on +* this bus. * @pm: Power management operations of this bus, callback the specific * device driver's pm-ops. * @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU
@@ -103,6 +105,7 @@ struct bus_type {int (*num_vf)(struct device *dev);int (*dma_configure)(struct device *dev); +void (*dma_cleanup)(struct device *dev);const struct dev_pm_ops *pm;
drivers/base/dd.c +5 -0
@@ -665,6 +665,8 @@ static int really_probe(struct device *dev, struct device_driver *drv)if (dev->bus)blocking_notifier_call_chain (&dev->bus->p->bus_notifier,BUS_NOTIFY_DRIVER_NOT_BOUND , dev); +if (dev->bus && dev->bus->dma_cleanup) + dev->bus->dma_cleanup (dev); pinctrl_bind_failed:device_links_no_driver (dev);devres_release_all (dev);
@@ -1208,6 +1210,9 @@ static void __device_release_driver(struct device *dev, struct device *parent)else if (drv->remove) drv->remove (dev); +if (dev->bus && dev->bus->dma_cleanup) + dev->bus->dma_cleanup (dev); +device_links_driver_cleanup (dev);devres_release_all (dev);
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v8 3/11 amba: Stop sharing platform_dma_configure()
In reply to Lu BaoluStop sharing platform_dma_configure() helper as they are about to have their own bus dma_configure callbacks.
--- include/linux/platform_device.h | 2 -- drivers/amba/bus.c | 19 ++++++++++++++++++- drivers/base/platform.c | 3 +-- 3 files changed, 19 insertions(+), 5 deletions(-)
include/linux/platform_device.h +0 -2
@@ -328,8 +328,6 @@ extern int platform_pm_restore(struct device *dev);#define platform_pm_restore NULL#endif -extern int platform_dma_configure (struct device *dev); -#ifdef CONFIG_PM_SLEEP #define USE_PLATFORM_PM_SLEEP_OPS \ .suspend = platform_pm_suspend, \
drivers/amba/bus.c +18 -1
@@ -20,6 +20,8 @@#include <linux/platform_device.h> #include <linux/reset.h> #include <linux/of_irq.h> +#include <linux/of_device.h> +#include <linux/acpi.h> #define to_amba_driver (d) container_of(d, struct amba_driver, drv)
@@ -273,6 +275,21 @@ static void amba_shutdown(struct device *dev) drv->shutdown (to_amba_device (dev)); } +static int amba_dma_configure (struct device *dev) +{ +enum dev_dma_attr attr; +int ret =0 ; + +if (dev->of_node) { + ret =of_dma_configure (dev, dev->of_node, true); + }else if (has_acpi_companion (dev)) { + attr =acpi_get_dma_attr (to_acpi_device_node (dev->fwnode)); + ret =acpi_dma_configure (dev, attr); + } + +return ret; +} +#ifdef CONFIG_PM /* * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
@@ -341,7 +358,7 @@ struct bus_type amba_bustype = { .probe = amba_probe, .remove = amba_remove, .shutdown = amba_shutdown, - .dma_configure = platform_dma_configure, + .dma_configure = amba_dma_configure, .pm = &amba_pm, };EXPORT_SYMBOL_GPL (amba_bustype);
drivers/base/platform.c +1 -2
@@ -1454,8 +1454,7 @@ static void platform_shutdown(struct device *_dev) drv->shutdown (dev); } - -int platform_dma_configure (struct device *dev) +static int platform_dma_configure (struct device *dev) {enum dev_dma_attr attr;int ret =0 ;
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v8 4/11 bus: platform,amba,fsl-mc,PCI: Add device DMA ownership management
In reply to Lu BaoluThe devices on platform/amba/fsl-mc/PCI buses could be bound to drivers with the device DMA managed by kernel drivers or user-space applications. Unfortunately, multiple devices may be placed in the same IOMMU group because they cannot be isolated from each other. The DMA on these devices must either be entirely under kernel control or userspace control, never a mixture. Otherwise the driver integrity is not guaranteed because they could access each other through the peer-to-peer accesses which by-pass the IOMMU protection.
This checks and sets the default DMA mode during driver binding, and cleanups during driver unbinding. In the default mode, the device DMA is managed by the device driver which handles DMA operations through the kernel DMA APIs (see Documentation/core-api/dma-api.rst).
For cases where the devices are assigned for userspace control through the userspace driver framework(i.e. VFIO), the drivers(for example, vfio_pci/ vfio_platfrom etc.) may set a new flag (driver_managed_dma) to skip this default setting in the assumption that the drivers know what they are doing with the device DMA.
Calling iommu_device_use_default_domain() before {of,acpi}_dma_configure is currently a problem. As things stand, the IOMMU driver ignored the initial iommu_probe_device() call when the device was added, since at that point it had no fwspec yet. In this situation, {of,acpi}_iommu_configure() are retriggering iommu_probe_device() after the IOMMU driver has seen the firmware data via .of_xlate to learn that it actually responsible for the given device. As the result, before that gets fixed, iommu_use_default_domain() goes at the end, and calls arch_teardown_dma_ops() if it fails.
--- include/linux/amba/bus.h | 8 ++++++++ include/linux/fsl/mc.h | 8 ++++++++ include/linux/pci.h | 8 ++++++++ include/linux/platform_device.h | 8 ++++++++ drivers/amba/bus.c | 18 ++++++++++++++++++ drivers/base/platform.c | 18 ++++++++++++++++++ drivers/bus/fsl-mc/fsl-mc-bus.c | 24 ++++++++++++++++++++++-- drivers/pci/pci-driver.c | 18 ++++++++++++++++++ 8 files changed, 108 insertions(+), 2 deletions(-)
include/linux/amba/bus.h +8 -0
@@ -79,6 +79,14 @@ struct amba_driver {void (*remove)(struct amba_device *);void (*shutdown)(struct amba_device *);const struct amba_id *id_table; +/* +* For most device drivers, no need to care about this flag as long as +* all DMAs are handled through the kernel DMA API. For some special +* ones, for example VFIO drivers, they know how to manage the DMA +* themselves and set this flag so that the IOMMU layer will allow them +* to setup and manage their own I/O address space. +*/ +bool driver_managed_dma; };/*
include/linux/fsl/mc.h +8 -0
@@ -32,6 +32,13 @@ struct fsl_mc_io;* @shutdown: Function called at shutdown time to quiesce the device * @suspend: Function called when a device is stopped * @resume: Function called when a device is resumed +* @driver_managed_dma: Device driver doesn't use kernel DMA API for DMA. +* For most device drivers, no need to care about this flag +* as long as all DMAs are handled through the kernel DMA API. +* For some special ones, for example VFIO drivers, they know +* how to manage the DMA themselves and set this flag so that +* the IOMMU layer will allow them to setup and manage their +* own I/O address space. * * Generic DPAA device driver object for device drivers that are registered * with a DPRC bus. This structure is to be embedded in each device-specific
@@ -45,6 +52,7 @@ struct fsl_mc_driver {void (*shutdown)(struct fsl_mc_device *dev);int (*suspend)(struct fsl_mc_device *dev,pm_message_t state);int (*resume)(struct fsl_mc_device *dev); +bool driver_managed_dma; };#define to_fsl_mc_driver (_drv) \
include/linux/pci.h +8 -0
@@ -894,6 +894,13 @@ struct module;* created once it is bound to the driver. * @driver: Driver model structure. * @dynids: List of dynamically added device IDs. +* @driver_managed_dma: Device driver doesn't use kernel DMA API for DMA. +* For most device drivers, no need to care about this flag +* as long as all DMAs are handled through the kernel DMA API. +* For some special ones, for example VFIO drivers, they know +* how to manage the DMA themselves and set this flag so that +* the IOMMU layer will allow them to setup and manage their +* own I/O address space. */ struct pci_driver {struct list_head node;
@@ -912,6 +919,7 @@ struct pci_driver {const struct attribute_group **dev_groups;struct device_driver driver;struct pci_dynids dynids; +bool driver_managed_dma; };static inline struct pci_driver *to_pci_driver (struct device_driver *drv)
include/linux/platform_device.h +8 -0
@@ -210,6 +210,14 @@ struct platform_driver {struct device_driver driver;const struct platform_device_id *id_table;bool prevent_deferred_probe; +/* +* For most device drivers, no need to care about this flag as long as +* all DMAs are handled through the kernel DMA API. For some special +* ones, for example VFIO drivers, they know how to manage the DMA +* themselves and set this flag so that the IOMMU layer will allow them +* to setup and manage their own I/O address space. +*/ +bool driver_managed_dma; };#define to_platform_driver (drv) (container_of((drv), struct platform_driver, \
drivers/amba/bus.c +18 -0
@@ -22,6 +22,8 @@#include <linux/of_irq.h> #include <linux/of_device.h> #include <linux/acpi.h> +#include <linux/iommu.h> +#include <linux/dma-map-ops.h> #define to_amba_driver (d) container_of(d, struct amba_driver, drv)
@@ -277,6 +279,7 @@ static void amba_shutdown(struct device *dev)static int amba_dma_configure (struct device *dev) { +struct amba_driver *drv =to_amba_driver (dev->driver);enum dev_dma_attr attr;int ret =0 ;
@@ -287,9 +290,23 @@ static int amba_dma_configure(struct device *dev) ret =acpi_dma_configure (dev, attr); } +if (!ret && !drv->driver_managed_dma) { + ret =iommu_device_use_default_domain (dev); +if (ret) +arch_teardown_dma_ops (dev); + } +return ret; } +static void amba_dma_cleanup (struct device *dev) +{ +struct amba_driver *drv =to_amba_driver (dev->driver); + +if (!drv->driver_managed_dma) +iommu_device_unuse_default_domain (dev); +} +#ifdef CONFIG_PM /* * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
@@ -359,6 +376,7 @@ struct bus_type amba_bustype = { .remove = amba_remove, .shutdown = amba_shutdown, .dma_configure = amba_dma_configure, + .dma_cleanup = amba_dma_cleanup, .pm = &amba_pm, };EXPORT_SYMBOL_GPL (amba_bustype);
drivers/base/platform.c +18 -0
@@ -30,6 +30,8 @@#include <linux/property.h> #include <linux/kmemleak.h> #include <linux/types.h> +#include <linux/iommu.h> +#include <linux/dma-map-ops.h> #include "base.h" #include "power/power.h"
@@ -1456,6 +1458,7 @@ static void platform_shutdown(struct device *_dev)static int platform_dma_configure (struct device *dev) { +struct platform_driver *drv =to_platform_driver (dev->driver);enum dev_dma_attr attr;int ret =0 ;
@@ -1466,9 +1469,23 @@ static int platform_dma_configure(struct device *dev) ret =acpi_dma_configure (dev, attr); } +if (!ret && !drv->driver_managed_dma) { + ret =iommu_device_use_default_domain (dev); +if (ret) +arch_teardown_dma_ops (dev); + } +return ret; } +static void platform_dma_cleanup (struct device *dev) +{ +struct platform_driver *drv =to_platform_driver (dev->driver); + +if (!drv->driver_managed_dma) +iommu_device_unuse_default_domain (dev); +} +static const struct dev_pm_ops platform_dev_pm_ops = {SET_RUNTIME_PM_OPS (pm_generic_runtime_suspend, pm_generic_runtime_resume,NULL ) USE_PLATFORM_PM_SLEEP_OPS
@@ -1483,6 +1500,7 @@ struct bus_type platform_bus_type = { .remove = platform_remove, .shutdown = platform_shutdown, .dma_configure = platform_dma_configure, + .dma_cleanup = platform_dma_cleanup, .pm = &platform_dev_pm_ops, };EXPORT_SYMBOL_GPL (platform_bus_type);
drivers/bus/fsl-mc/fsl-mc-bus.c +22 -2
@@ -21,6 +21,7 @@#include <linux/dma-mapping.h> #include <linux/acpi.h> #include <linux/iommu.h> +#include <linux/dma-map-ops.h> #include "fsl-mc-private.h"
@@ -140,15 +141,33 @@ static int fsl_mc_dma_configure(struct device *dev) {struct device *dma_dev = dev;struct fsl_mc_device *mc_dev =to_fsl_mc_device (dev); +struct fsl_mc_driver *mc_drv =to_fsl_mc_driver (dev->driver);u32 input_id = mc_dev->icid; +int ret;while (dev_is_fsl_mc (dma_dev)) dma_dev = dma_dev->parent;if (dev_of_node (dma_dev)) -return of_dma_configure_id (dev, dma_dev->of_node,0 , &input_id); + ret =of_dma_configure_id (dev, dma_dev->of_node,0 , &input_id); +else + ret =acpi_dma_configure_id (dev,DEV_DMA_COHERENT , &input_id); + +if (!ret && !mc_drv->driver_managed_dma) { + ret =iommu_device_use_default_domain (dev); +if (ret) +arch_teardown_dma_ops (dev); + } + +return ret; +} + +static void fsl_mc_dma_cleanup (struct device *dev) +{ +struct fsl_mc_driver *mc_drv =to_fsl_mc_driver (dev->driver); -return acpi_dma_configure_id (dev,DEV_DMA_COHERENT , &input_id); +if (!mc_drv->driver_managed_dma) +iommu_device_unuse_default_domain (dev); } static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
@@ -312,6 +331,7 @@ struct bus_type fsl_mc_bus_type = { .match = fsl_mc_bus_match, .uevent = fsl_mc_bus_uevent, .dma_configure = fsl_mc_dma_configure, + .dma_cleanup = fsl_mc_dma_cleanup, .dev_groups = fsl_mc_dev_groups, .bus_groups = fsl_mc_bus_groups, };
drivers/pci/pci-driver.c +18 -0
@@ -20,6 +20,7 @@#include <linux/of_device.h> #include <linux/acpi.h> #include <linux/dma-map-ops.h> +#include <linux/iommu.h> #include "pci.h" #include "pcie/portdrv.h"
@@ -1590,6 +1591,7 @@ static int pci_bus_num_vf(struct device *dev)*/ static int pci_dma_configure (struct device *dev) { +struct pci_driver *driver =to_pci_driver (dev->driver);struct device *bridge;int ret =0 ;
@@ -1605,9 +1607,24 @@ static int pci_dma_configure(struct device *dev) }pci_put_host_bridge_device (bridge); + +if (!ret && !driver->driver_managed_dma) { + ret =iommu_device_use_default_domain (dev); +if (ret) +arch_teardown_dma_ops (dev); + } +return ret; } +static void pci_dma_cleanup (struct device *dev) +{ +struct pci_driver *driver =to_pci_driver (dev->driver); + +if (!driver->driver_managed_dma) +iommu_device_unuse_default_domain (dev); +} +struct bus_type pci_bus_type = { .name ="pci" , .match = pci_bus_match,
@@ -1621,6 +1638,7 @@ struct bus_type pci_bus_type = { .pm = PCI_PM_OPS_PTR, .num_vf = pci_bus_num_vf, .dma_configure = pci_dma_configure, + .dma_cleanup = pci_dma_cleanup, };EXPORT_SYMBOL (pci_bus_type);
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v8 04/11] bus: platform,amba,fsl-mc,PCI: Add device DMA ownership management
Quoted 30 lines · Lu Baolu
The devices on platform/amba/fsl-mc/PCI buses could be bound to drivers with the device DMA managed by kernel drivers or user-space applications. Unfortunately, multiple devices may be placed in the same IOMMU group because they cannot be isolated from each other. The DMA on these devices must either be entirely under kernel control or userspace control, never a mixture. Otherwise the driver integrity is not guaranteed because they could access each other through the peer-to-peer accesses which by-pass the IOMMU protection.
This checks and sets the default DMA mode during driver binding, and cleanups during driver unbinding. In the default mode, the device DMA is managed by the device driver which handles DMA operations through the kernel DMA APIs (see
Documentation/core-api/dma-api.rst).For cases where the devices are assigned for userspace control through the userspace driver framework(i.e. VFIO), the
drivers(for example, vfio_pci/ vfio_platfrom etc.)may set a new flag (driver_managed_dma) to skip this default setting in the assumption that the drivers know what they are doing with the device DMA.Calling
iommu_device_use_default_domain()before {of,acpi}_dma_configure is currently a problem. As things stand, the IOMMU driver ignored the initialiommu_probe_device()call when the device was added, since at that point it had no fwspec yet. In this situation, {of,acpi}_iommu_configure() are retriggeringiommu_probe_device()after the IOMMU driver has seen the firmware data via .of_xlate to learn that it actually responsible for the given device. As the result, before that gets fixed,iommu_use_default_domain()goes at the end, and callsarch_teardown_dma_ops()if it fails.
Quoted 7 lines
Cc Greg Kroah-Hartman <[email protected]>Cc Bjorn Helgaas <[email protected]>Cc Stuart Yoder <[email protected]>Cc Laurentiu Tudor <[email protected]>Signed-off-by Lu Baolu <[email protected]>Reviewed-by Greg Kroah-Hartman <[email protected]>Reviewed-by Jason Gunthorpe <[email protected]>
PATCH v8 5/11 PCI: pci_stub: Set driver_managed_dma
In reply to Lu BaoluThe current VFIO implementation allows pci-stub driver to be bound to a PCI device with other devices in the same IOMMU group being assigned to userspace. The pci-stub driver has no dependencies on DMA or the IOVA mapping of the device, but it does prevent the user from having direct access to the device, which is useful in some circumstances.
The pci_dma_configure() marks the iommu_group as containing only devices with kernel drivers that manage DMA. For compatibility with the VFIO usage, avoid this default behavior for the pci_stub. This allows the pci_stub still able to be used by the admin to block driver binding after applying the DMA ownership to VFIO.
--- drivers/pci/pci-stub.c | 1 + 1 file changed, 1 insertion(+)
drivers/pci/pci-stub.c +1 -0
@@ -36,6 +36,7 @@ static struct pci_driver stub_driver = { .name = "pci-stub", .id_table = NULL, /* only dynamic id's */ .probe = pci_stub_probe, + .driver_managed_dma = true, };static int __init pci_stub_init (void )
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v8 6/11 PCI: portdrv: Set driver_managed_dma
In reply to Lu BaoluIf a switch lacks ACS P2P Request Redirect, a device below the switch can bypass the IOMMU and DMA directly to other devices below the switch, so all the downstream devices must be in the same IOMMU group as the switch itself.
The existing VFIO framework allows the portdrv driver to be bound to the bridge while its downstream devices are assigned to user space. The pci_dma_configure() marks the IOMMU group as containing only devices with kernel drivers that manage DMA. Avoid this default behavior for the portdrv driver in order for compatibility with the current VFIO usage.
We achieve this by setting ".driver_managed_dma = true" in pci_driver structure. It is safe because the portdrv driver meets below criteria:
- This driver doesn't use DMA, as you can't find any related calls like pci_set_master() or any kernel DMA API (dma_map_*() and etc.).
- It doesn't use MMIO as you can't find ioremap() or similar calls. It's tolerant to userspace possibly also touching the same MMIO registers via P2P DMA access.
--- drivers/pci/pcie/portdrv_pci.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/pci/pcie/portdrv_pci.c +2 -0
@@ -202,6 +202,8 @@ static struct pci_driver pcie_portdriver = { .err_handler = &pcie_portdrv_err_handler, + .driver_managed_dma = true, + .driver.pm = PCIE_PORTDRV_PM_OPS, };
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v8 7/11 vfio: Set DMA ownership for VFIO devices
In reply to Lu BaoluClaim group dma ownership when an IOMMU group is set to a container, and release the dma ownership once the iommu group is unset from the container.
This change disallows some unsafe bridge drivers to bind to non-ACS bridges while devices under them are assigned to user space. This is an intentional enhancement and possibly breaks some existing configurations. The recommendation to such an affected user would be that the previously allowed host bridge driver was unsafe for this use case and to continue to enable assignment of devices within that group, the driver should be unbound from the bridge device or replaced with the pci-stub driver.
For any bridge driver, we consider it unsafe if it satisfies any of the following conditions:
1) The bridge driver uses DMA. Calling pci_set_master() or calling any kernel DMA API (dma_map_*() and etc.) is an indicate that the driver is doing DMA.
2) If the bridge driver uses MMIO, it should be tolerant to hostile userspace also touching the same MMIO registers via P2P DMA attacks.
If the bridge driver turns out to be a safe one, it could be used as before by setting the driver's .driver_managed_dma field, just like what we have done in the pcieport driver.
--- drivers/vfio/fsl-mc/vfio_fsl_mc.c | 1 + drivers/vfio/pci/vfio_pci.c | 1 + drivers/vfio/platform/vfio_amba.c | 1 + drivers/vfio/platform/vfio_platform.c | 1 + drivers/vfio/vfio.c | 10 +++++++++- 5 files changed, 13 insertions(+), 1 deletion(-)
drivers/vfio/fsl-mc/vfio_fsl_mc.c +1 -0
@@ -588,6 +588,7 @@ static struct fsl_mc_driver vfio_fsl_mc_driver = { .name = "vfio-fsl-mc", .owner = THIS_MODULE, }, + .driver_managed_dma = true, };static int __init vfio_fsl_mc_driver_init (void )
drivers/vfio/pci/vfio_pci.c +1 -0
@@ -193,6 +193,7 @@ static struct pci_driver vfio_pci_driver = { .remove = vfio_pci_remove, .sriov_configure = vfio_pci_sriov_configure, .err_handler = &vfio_pci_core_err_handlers, + .driver_managed_dma = true, };static void __init vfio_pci_fill_ids (void )
drivers/vfio/platform/vfio_amba.c +1 -0
@@ -95,6 +95,7 @@ static struct amba_driver vfio_amba_driver = { .name = "vfio-amba", .owner = THIS_MODULE, }, + .driver_managed_dma = true, };module_amba_driver (vfio_amba_driver);
drivers/vfio/platform/vfio_platform.c +1 -0
@@ -76,6 +76,7 @@ static struct platform_driver vfio_platform_driver = { .driver = { .name = "vfio-platform", }, + .driver_managed_dma = true, };module_platform_driver (vfio_platform_driver);
drivers/vfio/vfio.c +9 -1
@@ -1198,6 +1198,8 @@ static void __vfio_group_unset_container(struct vfio_group *group) driver->ops->detach_group (container->iommu_data, group->iommu_group); +iommu_group_release_dma_owner (group->iommu_group); + group->container =NULL ;wake_up (&group->container_q);list_del (&group->container_next);
@@ -1282,13 +1284,19 @@ static int vfio_group_set_container(struct vfio_group *group, int container_fd) goto unlock_out; } + ret =iommu_group_claim_dma_owner (group->iommu_group, f.file); +if (ret) + goto unlock_out; + driver = container->iommu_driver;if (driver) { ret = driver->ops->attach_group (container->iommu_data, group->iommu_group, group->type); -if (ret) +if (ret) { +iommu_group_release_dma_owner (group->iommu_group); goto unlock_out; + } } group->container = container;
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v8 8/11 vfio: Remove use of vfio_group_viable()
In reply to Lu BaoluAs DMA ownership is claimed for the iommu group when a VFIO group is added to a VFIO container, the VFIO group viability is guaranteed as long as group->container_users > 0. Remove those unnecessary group viability checks which are only hit when group->container_users is not zero.
The only remaining reference is in GROUP_GET_STATUS, which could be called at any time when group fd is valid. Here we just replace the vfio_group_viable() by directly calling IOMMU core to get viability status.
--- drivers/vfio/vfio.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-)
drivers/vfio/vfio.c +6 -12
@@ -1313,12 +1313,6 @@ static int vfio_group_set_container(struct vfio_group *group, int container_fd)return ret; } -static bool vfio_group_viable (struct vfio_group *group) -{ -return (iommu_group_for_each_dev (group->iommu_group, - group, vfio_dev_viable) ==0 ); -} -static int vfio_group_add_container_user (struct vfio_group *group) {if (!atomic_inc_not_zero (&group->container_users))
@@ -1328,7 +1322,7 @@ static int vfio_group_add_container_user(struct vfio_group *group)atomic_dec (&group->container_users);return -EPERM ; } -if (!group->container->iommu_driver || !vfio_group_viable (group)) { +if (!group->container->iommu_driver) {atomic_dec (&group->container_users);return -EINVAL ; }
@@ -1346,7 +1340,7 @@ static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)int ret =0 ;if (0 ==atomic_read (&group->container_users) || - !group->container->iommu_driver || !vfio_group_viable (group)) + !group->container->iommu_driver)return -EINVAL ;if (group->type ==VFIO_NO_IOMMU && !capable (CAP_SYS_RAWIO ))
@@ -1438,11 +1432,11 @@ static long vfio_group_fops_unl_ioctl(struct file *filep, status.flags =0 ; -if (vfio_group_viable (group)) - status.flags |=VFIO_GROUP_FLAGS_VIABLE ; -if (group->container) - status.flags |=VFIO_GROUP_FLAGS_CONTAINER_SET ; + status.flags |=VFIO_GROUP_FLAGS_CONTAINER_SET | +VFIO_GROUP_FLAGS_VIABLE ; +else if (!iommu_group_dma_owner_claimed (group->iommu_group)) + status.flags |=VFIO_GROUP_FLAGS_VIABLE ;if (copy_to_user ((void __user *)arg, &status, minsz))return -EFAULT ;
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v8 9/11 vfio: Delete the unbound_list
In reply to Lu BaoluFrom: Jason Gunthorpe <[email protected]>
commit 60720a0fc646 ("vfio: Add device tracking during unbind") added the unbound list to plug a problem with KVM where KVM_DEV_VFIO_GROUP_DEL relied on vfio_group_get_external_user() succeeding to return the vfio_group from a group file descriptor. The unbound list allowed vfio_group_get_external_user() to continue to succeed in edge cases.
However commit 5d6dee80a1e9 ("vfio: New external user group/file match") deleted the call to vfio_group_get_external_user() during KVM_DEV_VFIO_GROUP_DEL. Instead vfio_external_group_match_file() is used to directly match the file descriptor to the group pointer.
This in turn avoids the call down to vfio_dev_viable() during KVM_DEV_VFIO_GROUP_DEL and also avoids the trouble the first commit was trying to fix.
There are no other users of vfio_dev_viable() that care about the time after vfio_unregister_group_dev() returns, so simply delete the unbound_list entirely.
--- drivers/vfio/vfio.c | 74 ++------------------------------------------- 1 file changed, 2 insertions(+), 72 deletions(-)
drivers/vfio/vfio.c +2 -72
@@ -62,11 +62,6 @@ struct vfio_container {bool noiommu; }; -struct vfio_unbound_dev { -struct device *dev; -struct list_head unbound_next; -}; -struct vfio_group {struct device dev;struct cdev cdev;
@@ -79,8 +74,6 @@ struct vfio_group {struct notifier_block nb;struct list_head vfio_next;struct list_head container_next; -struct list_head unbound_list; -struct mutex unbound_lock;atomic_t opened;wait_queue_head_t container_q;enum vfio_group_type type;
@@ -340,16 +333,8 @@ vfio_group_get_from_iommu(struct iommu_group *iommu_group)static void vfio_group_release (struct device *dev) { struct vfio_group *group = container_of(dev, struct vfio_group, dev); -struct vfio_unbound_dev *unbound, *tmp; - -list_for_each_entry_safe (unbound, tmp, - &group->unbound_list, unbound_next) { -list_del (&unbound->unbound_next); -kfree (unbound); - }mutex_destroy (&group->device_lock); -mutex_destroy (&group->unbound_lock);iommu_group_put (group->iommu_group);ida_free (&vfio.group_ida,MINOR (group->dev.devt));kfree (group);
@@ -381,8 +366,6 @@ static struct vfio_group *vfio_group_alloc(struct iommu_group *iommu_group,refcount_set (&group->users,1 );INIT_LIST_HEAD (&group->device_list);mutex_init (&group->device_lock); -INIT_LIST_HEAD (&group->unbound_list); -mutex_init (&group->unbound_lock);init_waitqueue_head (&group->container_q); group->iommu_group = iommu_group;/* put in vfio_group_release() */
@@ -571,19 +554,8 @@ static int vfio_dev_viable(struct device *dev, void *data)struct vfio_group *group = data;struct vfio_device *device;struct device_driver *drv =READ_ONCE (dev->driver); -struct vfio_unbound_dev *unbound; -int ret = -EINVAL ; -mutex_lock (&group->unbound_lock); -list_for_each_entry (unbound, &group->unbound_list, unbound_next) { -if (dev == unbound->dev) { - ret =0 ; -break ; - } - } -mutex_unlock (&group->unbound_lock); - -if (!ret || !drv ||vfio_dev_driver_allowed (dev, drv)) +if (!drv ||vfio_dev_driver_allowed (dev, drv))return 0 ; device =vfio_group_get_device (group, dev);
@@ -592,7 +564,7 @@ static int vfio_dev_viable(struct device *dev, void *data)return 0 ; } -return ret; +return -EINVAL ; }/*
@@ -634,7 +606,6 @@ static int vfio_iommu_group_notifier(struct notifier_block *nb, { struct vfio_group *group = container_of(nb, struct vfio_group, nb);struct device *dev = data; -struct vfio_unbound_dev *unbound;switch (action) {case IOMMU_GROUP_NOTIFY_ADD_DEVICE :
@@ -663,28 +634,6 @@ static int vfio_iommu_group_notifier(struct notifier_block *nb, __func__,iommu_group_id (group->iommu_group), dev->driver->name);break ; -case IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER : -dev_dbg (dev,"%s: group %d unbound from driver\n" , __func__, -iommu_group_id (group->iommu_group)); -/* -* XXX An unbound device in a live group is ok, but we'd -* really like to avoid the above BUG_ON by preventing other -* drivers from binding to it. Once that occurs, we have to -* stop the system to maintain isolation. At a minimum, we'd -* want a toggle to disable driver auto probe for this device. -*/ - -mutex_lock (&group->unbound_lock); -list_for_each_entry (unbound, - &group->unbound_list, unbound_next) { -if (dev == unbound->dev) { -list_del (&unbound->unbound_next); -kfree (unbound); -break ; - } - } -mutex_unlock (&group->unbound_lock); -break ; }return NOTIFY_OK ; }
@@ -889,29 +838,10 @@ static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group,void vfio_unregister_group_dev (struct vfio_device *device) {struct vfio_group *group = device->group; -struct vfio_unbound_dev *unbound;unsigned int i =0 ;bool interrupted = false;long rc; -/* -* When the device is removed from the group, the group suddenly -* becomes non-viable; the device has a driver (until the unbind -* completes), but it's not present in the group. This is bad news -* for any external users that need to re-acquire a group reference -* in order to match and release their existing reference. To -* solve this, we track such devices on the unbound_list to bridge -* the gap until they're fully unbound. -*/ - unbound =kzalloc (sizeof (*unbound),GFP_KERNEL ); -if (unbound) { - unbound->dev = device->dev; -mutex_lock (&group->unbound_lock); -list_add (&unbound->unbound_next, &group->unbound_list); -mutex_unlock (&group->unbound_lock); - } -WARN_ON (!unbound); -vfio_device_put (device); rc =try_wait_for_completion (&device->comp);while (rc <=0 ) {
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v8 10/11 vfio: Remove iommu group notifier
In reply to Lu BaoluThe iommu core and driver core have been enhanced to avoid unsafe driver binding to a live group after iommu_group_set_dma_owner(PRIVATE_USER) has been called. There's no need to register iommu group notifier. This removes the iommu group notifer which contains BUG_ON() and WARN().
--- drivers/vfio/vfio.c | 147 -------------------------------------------- 1 file changed, 147 deletions(-)
drivers/vfio/vfio.c +0 -147
@@ -71,7 +71,6 @@ struct vfio_group {struct vfio_container *container;struct list_head device_list;struct mutex device_lock; -struct notifier_block nb;struct list_head vfio_next;struct list_head container_next;atomic_t opened;
@@ -274,8 +273,6 @@ void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops) }EXPORT_SYMBOL_GPL (vfio_unregister_iommu_driver); -static int vfio_iommu_group_notifier (struct notifier_block *nb, -unsigned long action,void *data);static void vfio_group_get (struct vfio_group *group);/*
@@ -395,13 +392,6 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group, goto err_put; } - group->nb.notifier_call = vfio_iommu_group_notifier; - err =iommu_group_register_notifier (iommu_group, &group->nb); -if (err) { - ret =ERR_PTR (err); - goto err_put; - } -mutex_lock (&vfio.group_lock);/* Did we race creating this group? */
@@ -422,7 +412,6 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group, err_unlock:mutex_unlock (&vfio.group_lock); -iommu_group_unregister_notifier (group->iommu_group, &group->nb); err_put:put_device (&group->dev);return ret;
@@ -447,7 +436,6 @@ static void vfio_group_put(struct vfio_group *group)cdev_device_del (&group->cdev, &group->dev);mutex_unlock (&vfio.group_lock); -iommu_group_unregister_notifier (group->iommu_group, &group->nb);put_device (&group->dev); }
@@ -503,141 +491,6 @@ static struct vfio_device *vfio_group_get_device(struct vfio_group *group,return NULL ; } -/* -* Some drivers, like pci-stub, are only used to prevent other drivers from -* claiming a device and are therefore perfectly legitimate for a user owned -* group. The pci-stub driver has no dependencies on DMA or the IOVA mapping -* of the device, but it does prevent the user from having direct access to -* the device, which is useful in some circumstances. -* -* We also assume that we can include PCI interconnect devices, ie. bridges. -* IOMMU grouping on PCI necessitates that if we lack isolation on a bridge -* then all of the downstream devices will be part of the same IOMMU group as -* the bridge. Thus, if placing the bridge into the user owned IOVA space -* breaks anything, it only does so for user owned devices downstream. Note -* that error notification via MSI can be affected for platforms that handle -* MSI within the same IOVA space as DMA. -*/ -static const char *const vfio_driver_allowed[] = {"pci-stub" }; - -static bool vfio_dev_driver_allowed (struct device *dev, -struct device_driver *drv) -{ -if (dev_is_pci (dev)) { -struct pci_dev *pdev =to_pci_dev (dev); - -if (pdev->hdr_type !=PCI_HEADER_TYPE_NORMAL ) -return true; - } - -return match_string (vfio_driver_allowed, -ARRAY_SIZE (vfio_driver_allowed), - drv->name) >=0 ; -} - -/* -* A vfio group is viable for use by userspace if all devices are in -* one of the following states: -* - driver-less -* - bound to a vfio driver -* - bound to an otherwise allowed driver -* - a PCI interconnect device -* -* We use two methods to determine whether a device is bound to a vfio -* driver. The first is to test whether the device exists in the vfio -* group. The second is to test if the device exists on the group -* unbound_list, indicating it's in the middle of transitioning from -* a vfio driver to driver-less. -*/ -static int vfio_dev_viable (struct device *dev,void *data) -{ -struct vfio_group *group = data; -struct vfio_device *device; -struct device_driver *drv =READ_ONCE (dev->driver); - -if (!drv ||vfio_dev_driver_allowed (dev, drv)) -return 0 ; - - device =vfio_group_get_device (group, dev); -if (device) { -vfio_device_put (device); -return 0 ; - } - -return -EINVAL ; -} - -/* -* Async device support -*/ -static int vfio_group_nb_add_dev (struct vfio_group *group,struct device *dev) -{ -struct vfio_device *device; - -/* Do we already know about it? We shouldn't */ - device =vfio_group_get_device (group, dev); -if (WARN_ON_ONCE (device)) { -vfio_device_put (device); -return 0 ; - } - -/* Nothing to do for idle groups */ -if (!atomic_read (&group->container_users)) -return 0 ; - -/* TODO Prevent device auto probing */ -dev_WARN (dev,"Device added to live group %d!\n" , -iommu_group_id (group->iommu_group)); - -return 0 ; -} - -static int vfio_group_nb_verify (struct vfio_group *group,struct device *dev) -{ -/* We don't care what happens when the group isn't in use */ -if (!atomic_read (&group->container_users)) -return 0 ; - -return vfio_dev_viable (dev, group); -} - -static int vfio_iommu_group_notifier (struct notifier_block *nb, -unsigned long action,void *data) -{ - struct vfio_group *group = container_of(nb, struct vfio_group, nb); -struct device *dev = data; - -switch (action) { -case IOMMU_GROUP_NOTIFY_ADD_DEVICE : -vfio_group_nb_add_dev (group, dev); -break ; -case IOMMU_GROUP_NOTIFY_DEL_DEVICE : -/* -* Nothing to do here. If the device is in use, then the -* vfio sub-driver should block the remove callback until -* it is unused. If the device is unused or attached to a -* stub driver, then it should be released and we don't -* care that it will be going away. -*/ -break ; -case IOMMU_GROUP_NOTIFY_BIND_DRIVER : -dev_dbg (dev,"%s: group %d binding to driver\n" , __func__, -iommu_group_id (group->iommu_group)); -break ; -case IOMMU_GROUP_NOTIFY_BOUND_DRIVER : -dev_dbg (dev,"%s: group %d bound to driver %s\n" , __func__, -iommu_group_id (group->iommu_group), dev->driver->name); -BUG_ON (vfio_group_nb_verify (group, dev)); -break ; -case IOMMU_GROUP_NOTIFY_UNBIND_DRIVER : -dev_dbg (dev,"%s: group %d unbinding from driver %s\n" , - __func__,iommu_group_id (group->iommu_group), - dev->driver->name); -break ; - } -return NOTIFY_OK ; -} -/* * VFIO driver API */
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v8 11/11 iommu: Remove iommu group changes notifier
In reply to Lu BaoluThe iommu group changes notifer is not referenced in the tree. Remove it to avoid dead code.
--- include/linux/iommu.h | 23 ------------- drivers/iommu/iommu.c | 75 ------------------------------------------- 2 files changed, 98 deletions(-)
include/linux/iommu.h +0 -23
@@ -407,13 +407,6 @@ static inline const struct iommu_ops *dev_iommu_ops(struct device *dev)return dev->iommu->iommu_dev->ops; } -#define IOMMU_GROUP_NOTIFY_ADD_DEVICE 1/* Device added */ -#define IOMMU_GROUP_NOTIFY_DEL_DEVICE 2/* Pre Device removed */ -#define IOMMU_GROUP_NOTIFY_BIND_DRIVER 3/* Pre Driver bind */ -#define IOMMU_GROUP_NOTIFY_BOUND_DRIVER 4/* Post Driver bind */ -#define IOMMU_GROUP_NOTIFY_UNBIND_DRIVER 5/* Pre Driver unbind */ -#define IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER 6/* Post Driver unbind */ -extern int bus_set_iommu (struct bus_type *bus,const struct iommu_ops *ops);extern int bus_iommu_probe (struct bus_type *bus);extern bool iommu_present (struct bus_type *bus);
@@ -478,10 +471,6 @@ extern int iommu_group_for_each_dev(struct iommu_group *group, void *data,extern struct iommu_group *iommu_group_get (struct device *dev);extern struct iommu_group *iommu_group_ref_get (struct iommu_group *group);extern void iommu_group_put (struct iommu_group *group); -extern int iommu_group_register_notifier (struct iommu_group *group, -struct notifier_block *nb); -extern int iommu_group_unregister_notifier (struct iommu_group *group, -struct notifier_block *nb);extern int iommu_register_device_fault_handler (struct device *dev,iommu_dev_fault_handler_t handler,void *data);
@@ -878,18 +867,6 @@ static inline void iommu_group_put(struct iommu_group *group) { } -static inline int iommu_group_register_notifier (struct iommu_group *group, -struct notifier_block *nb) -{ -return -ENODEV ; -} - -static inline int iommu_group_unregister_notifier (struct iommu_group *group, -struct notifier_block *nb) -{ -return 0 ; -} - static inline int iommu_register_device_fault_handler(struct device *dev, iommu_dev_fault_handler_t handler,
drivers/iommu/iommu.c +0 -75
125 lines not on this page · raw message
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Hi Lu,
Quoted 137 lines · Lu Baolu
Hi folks,
The iommu group is the minimal isolation boundary for DMA. Devices in a group can access each other's MMIO registers via peer to peer DMA and also need share the same I/O address space.
Once the I/O address space is assigned to user control it is no longer available to the dma_map* API, which effectively makes the DMA API non-working.
Second, userspace can use DMA initiated by a device that it controls to access the MMIO spaces of other devices in the group. This allows userspace to indirectly attack any kernel owned device and it's driver.
Therefore groups must either be entirely under kernel control or userspace control, never a mixture. Unfortunately some systems have problems with the granularity of groups and there are a couple of important exceptions:
-
pci_stuballows the admin to block driver binding on a device and make it permanently shared with userspace. Since PCI stub does not do DMA it is safe, however the admin must understand that usingpci_stuballows userspace to attack whatever device it was bound it.- PCI bridges are sometimes included in groups. Typically PCI bridges do not use DMA, and generally do not have MMIO regions.
Generally any device that does not have any MMIO registers is a possible candidate for an exception.
Currently vfio adopts a workaround to detect violations of the above restrictions by monitoring the driver core BOUND event, and hardwiring the above exceptions. Since there is no way for vfio to reject driver binding at this point,
BUG_ON()is triggered if a violation is captured (kernel driver BOUND event on a group which already has some devices assigned to userspace). Aside from the bad user experience this opens a way for root userspace to crash the kernel, even in high integrity configurations, by manipulating the module binding and triggering theBUG_ON.This series solves this problem by making the user/kernel ownership a core concept at the IOMMU layer. The driver core enforces kernel ownership while drivers are bound and violations now result in a error codes during probe, not
BUG_ONfailures.Patch partitions:
[PATCH 1-4]: Detect DMA ownership conflicts during driver binding; [PATCH 5-7]: Add security context management for assigned devices; [PATCH 8-11]: Various cleanups.This is also part one of three initial series for IOMMUFD:
* Move IOMMU Group security into the iommu layer
- Generic IOMMUFD implementation
- VFIO ability to consume IOMMUFDChange log:
v1: initial post
- https://lore.kernel.org/linux-iommu/[email protected]/v2:
- https://lore.kernel.org/linux-iommu/[email protected]/- Move kernel dma ownership auto-claiming from driver core to bus
callback. [Greg/Christoph/Robin/Jason] https://lore.kernel.org/linux-iommu/[email protected]/T/#m153706912b770682cb12e3c28f57e171aa1f9d0c- Code and interface refactoring for iommu_set/release_dma_owner()
interfaces. [Jason] https://lore.kernel.org/linux-iommu/[email protected]/T/#mea70ed8e4e3665aedf32a5a0a7db095bf680325e- [NEW]Add new iommu_attach/detach_device_shared() interfaces for multiple devices group. [Robin/Jason]
https://lore.kernel.org/linux-iommu/[email protected]/T/#mea70ed8e4e3665aedf32a5a0a7db095bf680325e- [NEW]Use iommu_attach/detach_device_shared() in drm/tegra drivers.
- Refactoring and description refinement.
v3:
- https://lore.kernel.org/linux-iommu/[email protected]/- Rename
bus_type::dma_unconfiguretobus_type::dma_cleanup. [Greg]https://lore.kernel.org/linux-iommu/[email protected]/T/#m6711e041e47cb0cbe3964fad0a3466f5ae4b3b9b- Avoid
_platform_dma_configureforplatform_bus_type::dma_configure.[Greg] https://lore.kernel.org/linux-iommu/[email protected]/T/#m43fc46286611aa56a5c0eeaad99d539e5519f3f6- Patch "0012-iommu-Add-iommu_at-de-tach_device_shared-for-mult.patch" and "0018-drm-tegra-Use-the-iommu-dma_owner-mechanism.patch" have been tested by Dmitry Osipenko <[email protected]>.
v4:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Remove unnecessarytegra->domainchech in the tegra patch. (Jason)
- RemoveDMA_OWNER_NONE. (Joerg)
- Change refcount to unsigned int. (Christoph)
- Move mutex lock into groupset_dma_ownerfunctions. (Christoph)
- Add kernel doc for iommu_attach/detach_domain_shared(). (Christoph)
- Move dma auto-claim into driver core. (Jason/Christoph)v5:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Move kernel dma ownership auto-claiming from driver core to bus callback. (Greg)
- Refactor the iommu interfaces to make them more specific.
(Jason/Robin)
- Simplify the dma ownership implementation by removing the owner
type. (Jason)
- Commit message refactoring for PCI drivers. (Bjorn)
- Move iommu_attach/detach_device() improvement patches into another series as there are a lot of code refactoring and cleanup staffs
in various device drivers.v6:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Refine comments and commit mesages.
- Renameiommu_group_set_dma_owner()toiommu_group_claim_dma_owner().
- Rename iommu_device_use/unuse_kernel_dma() to iommu_device_use/unuse_default_domain().
- Remove unnecessaryEXPORT_SYMBOL_GPL.
- Change flag name fromno_kernel_api_dmatodriver_managed_dma.
- Merge 4 "Add driver dma ownership management" patches into single
one.v7:
- We discussed about adding some fields in driver structure and intercepting it in the bus notifier for driver unbinding. We agreed that the driver structure should not be used out of the driver core.
- As iommu_group_claim/release_dma_owner() are only used by the VFIO, there're no use cases for multiple calls for a single group.
- Add some commit messages in "vfio: Set DMA ownership for VFIO" to describe the intentional enhancement of unsafe bridge drivers.
- Comments refinement.
Quoted 4 lines
v8:
- Moveiommu_use_default_domain()to the end of .dma_configure callback to avoid firmware-data-ordering thing.
Feel free to add my T-b
Thanks
Eric
Quoted 46 lines
- Add Acked-by from PCI and VFIO maintainers.
This is based on next branch of linux-iommu tree:
https://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu.git
and also available on github:
https://github.com/LuBaolu/intel-iommu/commits/iommu-dma-ownership-v8Best regards,
baoluJason Gunthorpe (1):
vfio: Delete theunbound_listLu Baolu (10):
iommu: Add DMA ownership management interfaces
driver core: Adddma_cleanupcallback inbus_type
amba: Stop sharingplatform_dma_configure()
bus: platform,amba,fsl-mc,PCI: Add device DMA ownership management
PCI:pci_stub: Setdriver_managed_dma
PCI: portdrv: Setdriver_managed_dma
vfio: Set DMA ownership for VFIO devices
vfio: Remove use ofvfio_group_viable()
vfio: Remove iommu group notifier
iommu: Remove iommu group changes notifierinclude/linux/amba/bus.h | 8 + include/linux/device/bus.h | 3 + include/linux/fsl/mc.h | 8 + include/linux/iommu.h | 54 +++--- include/linux/pci.h | 8 + include/linux/platform_device.h | 10 +- drivers/amba/bus.c | 37 +++- drivers/base/dd.c | 5 + drivers/base/platform.c | 21 ++- drivers/bus/fsl-mc/fsl-mc-bus.c | 24 ++- drivers/iommu/iommu.c | 228 ++++++++++++++++-------- drivers/pci/pci-driver.c | 18 ++ drivers/pci/pci-stub.c | 1 + drivers/pci/pcie/portdrv_pci.c | 2 + drivers/vfio/fsl-mc/vfio_fsl_mc.c | 1 + drivers/vfio/pci/vfio_pci.c | 1 + drivers/vfio/platform/vfio_amba.c | 1 + drivers/vfio/platform/vfio_platform.c | 1 + drivers/vfio/vfio.c | 245 ++------------------------ 19 files changed, 338 insertions(+), 338 deletions(-)
Quoted 5 lines · Lu Baolu
Hi folks,
The iommu group is the minimal isolation boundary for DMA. Devices in a group can access each other's MMIO registers via peer to peer DMA and also need share the same I/O address space.
Joerg, are we good for the coming v5.18 merge window now? There are several things backed up behind this series.
Thanks,
Jason
Joerg Roedel
maintainerQuoted 2 lines · Jason Gunthorpe
Joerg, are we good for the coming v5.18 merge window now? There are several things backed up behind this series.
I usually don't apply bigger changes like this after -rc7, so it didn't make it. Please re-send after -rc3 is out and I will consider it.
Thanks,
Joerg
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Hi Joerg,
Quoted 6 lines · Joerg Roedel
Quoted 2 lines · Jason Gunthorpe
Joerg, are we good for the coming v5.18 merge window now? There are several things backed up behind this series.
I usually don't apply bigger changes like this after -rc7, so it didn't make it. Please re-send after -rc3 is out and I will consider it.
Sure. I will do.
Best regards,
baolu
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Quoted 11 lines · Lu Baolu
Hi Joerg,
Quoted 6 lines · Joerg Roedel
Quoted 2 lines · Jason Gunthorpe
Joerg, are we good for the coming v5.18 merge window now? There are several things backed up behind this series.
I usually don't apply bigger changes like this after -rc7, so it didn't make it. Please re-send after -rc3 is out and I will consider it.
Sure. I will do.
Why rc3? It has been 4 weeks now with no futher comments.
Jason
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Joerg Roedel
maintainerQuoted 1 line · Jason Gunthorpe
Why rc3? It has been 4 weeks now with no futher comments.
Because I start applying new code to branches based on -rc3. In the past I used different -rc's for the topic branches (usually the latest -rc available when I started applying to that branch), but that caused silly merge conflicts from time to time. So I am now basing every topic branch on the same -rc, which is usually -rc3. Rationale is that by -rc3 time the kernel should have reasonably stabilized after the merge window.
Regards,
Joerg
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Quoted 9 lines · Joerg Roedel
Quoted 1 line · Jason Gunthorpe
Why rc3? It has been 4 weeks now with no futher comments.
Because I start applying new code to branches based on -rc3. In the past I used different -rc's for the topic branches (usually the latest -rc available when I started applying to that branch), but that caused silly merge conflicts from time to time. So I am now basing every topic branch on the same -rc, which is usually -rc3. Rationale is that by -rc3 time the kernel should have reasonably stabilized after the merge window.
You might consider using a linear tree instead of the topic branches, topics are tricky and I'm not sure it helps a small subsystem so much. Conflicts between topics are a PITA for everyone, and it makes handling conflicts with rc much harder than it needs to be.
At least I haven't felt a need for topics while running larger trees, and would find it stressful to try and squeeze the entire patch flow into only 3 weeks out of the 7 week cycle.
In any event, I'd like this on a branch so Alex can pull it too, I guess it means Alex has to merge rc3 to VFIO as well?
Thanks for explaining
Jason
Joerg Roedel
maintainerQuoted 4 lines · Jason Gunthorpe
You might consider using a linear tree instead of the topic branches, topics are tricky and I'm not sure it helps a small subsystem so much. Conflicts between topics are a PITA for everyone, and it makes handling conflicts with rc much harder than it needs to be.
I like the concept of a branch per driver, because with that I can just exclude that branch from my next-merge when there are issues with it. Conflicts between branches happen too, but they are quite manageable when the branches have the same base.
Overall I am thinking of reorganizing the IOMMU tree, but it will likely not end up to be a single-branch tree, although the number of patches per cycle _could_ just be carried in a single branch.
Quoted 3 lines
At least I haven't felt a need for topics while running larger trees, and would find it stressful to try and squeeze the entire patch flow into only 3 weeks out of the 7 week cycle.
Yeah, so it is 4 weeks in an 9 weeks cycle :) The merge window is 2 weeks and not a lot happens. The 2 weeks after are for stabilization and I usually only pick up fixes. Then come the 4 weeks were new code gets into the tree. In the last week everything gets testing in linux-next to be ready for the merge window. I will pickup fixes in that week, of course.
Quoted 2 lines
In any event, I'd like this on a branch so Alex can pull it too, I guess it means Alex has to merge rc3 to VFIO as well?
Sure, I can put these patches in a separate branch for Alex to pull into the VFIO tree.
Regards,
Joerg
Quoted 10 lines · Joerg Roedel
Quoted 4 lines · Jason Gunthorpe
You might consider using a linear tree instead of the topic branches, topics are tricky and I'm not sure it helps a small subsystem so much. Conflicts between topics are a PITA for everyone, and it makes handling conflicts with rc much harder than it needs to be.
I like the concept of a branch per driver, because with that I can just exclude that branch from my next-merge when there are issues with it. Conflicts between branches happen too, but they are quite manageable when the branches have the same base.
FWIW, I use the same topic branch approach for PCI. I like the ability to squash in fixes or drop things without having to clutter the history with trivial commits and reverts. I haven't found conflicts to be a problem.
Bjorn
Quoted 12 lines · On Fri, 8 Apr 2022 10:59:22 -0500 Bjorn Helgaas <[email protected]> wrote:
Quoted 10 lines · Joerg Roedel
Quoted 4 lines · Jason Gunthorpe
You might consider using a linear tree instead of the topic branches, topics are tricky and I'm not sure it helps a small subsystem so much. Conflicts between topics are a PITA for everyone, and it makes handling conflicts with rc much harder than it needs to be.
I like the concept of a branch per driver, because with that I can just exclude that branch from my next-merge when there are issues with it. Conflicts between branches happen too, but they are quite manageable when the branches have the same base.
Quoted 4 lines
FWIW, I use the same topic branch approach for PCI. I like the ability to squash in fixes or drop things without having to clutter the history with trivial commits and reverts. I haven't found conflicts to be a problem.
Same. I think I've generally modeled my branch handling after Bjorn and Joerg, I don't always use topic branches, but will for larger contributions and I don't generally find conflicts to be a problem. I'm always open to adopting best practices though. Thanks,
Alex
Quoted 20 lines · Alex Williamson
Quoted 12 lines · On Fri, 8 Apr 2022 10:59:22 -0500 Bjorn Helgaas <[email protected]> wrote:
Quoted 10 lines · Joerg Roedel
Quoted 4 lines · Jason Gunthorpe
You might consider using a linear tree instead of the topic branches, topics are tricky and I'm not sure it helps a small subsystem so much. Conflicts between topics are a PITA for everyone, and it makes handling conflicts with rc much harder than it needs to be.
I like the concept of a branch per driver, because with that I can just exclude that branch from my next-merge when there are issues with it. Conflicts between branches happen too, but they are quite manageable when the branches have the same base.
Quoted 4 lines
FWIW, I use the same topic branch approach for PCI. I like the ability to squash in fixes or drop things without having to clutter the history with trivial commits and reverts. I haven't found conflicts to be a problem.
Quoted 4 lines
Same. I think I've generally modeled my branch handling after Bjorn and Joerg, I don't always use topic branches, but will for larger contributions and I don't generally find conflicts to be a problem. I'm always open to adopting best practices though. Thanks,
I don't know about best practices, but I see most maintainers fall somewhere on a continuum between how Andrew Morton works and how David Miller/Linus work.
Andrew's model is to try and send patches that are perfect and he manipulates his queue continually. It is never quite clear what will get merged until Linus actually merges it.
The David/Linus model is that git is immutable and they only move forward. Never rebase, never manipulate an applied patch.
Andrew has significantly reigned in how much he manipulates his queue
- mostly due to pressure from Linus. Some of the remarks on this topic over the last year are pretty informative. So I would say changing patches once applied, or any rebasing, is now being seen as not best practice. (Indeed if Linus catches it and a mistake was made you are likely to get a sharp email)
Why I made the note, is that at least in my flow, I would not trade two weeks of accepting patches for topics. I'll probably have 20-30 patches merged already before rc3 comes out. I never have problems merging rc's because when a rc conflicts with the next I have only one branch and can just merge the rc and resolve the conflict, or merge the rc before applying a patch that would create a conflict in the first place. Linus has given some guidance on when/how he prefers to see those merges done.
Though I tend to advocate for a philosophy more like DaveM that the maintainer role is to hurry up and accept good patches - to emphasize flow as a way to build involvement and community.
That is not necessarily an entirely appropriate approach in some of the more critical subsystems like mm/pci - if they are broken in a way that impacts a large number of people at rc1 then it can cause a lot of impact. For instance our QA team is always paniced if rc1 doesn't work on our test environments.
Cheers,
Jason