Remove bus dependency for iommu_domain_alloc.
Subsystems (10)
RFC PATCH 1/1 Remove bus dependency for iommu_domain_alloc.
This patch attempts to remove iommu_domain_alloc function's dependency on the bus type. This dependency is quiet restrictive in case of vfio, where it's possible to bind multiple iommu groups (from different bus types) to the same iommu domain.
This patch is based on the assumption, that there is a single iommu for all bus types on the system.
We maintain a list of bus types (for which iommu ops are registered). In the iommu_domain_alloc function we ensure that all bus types correspond to the same set of iommu operations.
--- arch/arm/mm/dma-mapping.c | 2 +- drivers/gpu/drm/msm/msm_gpu.c | 2 +- drivers/iommu/amd_iommu_v2.c | 2 +- drivers/iommu/iommu.c | 32 +++++++++++++++++++++++++++++--- drivers/media/platform/omap3isp/isp.c | 2 +- drivers/remoteproc/remoteproc_core.c | 2 +- drivers/vfio/vfio_iommu_type1.c | 2 +- include/linux/device.h | 2 ++ include/linux/iommu.h | 4 ++-- virt/kvm/iommu.c | 2 +- 10 files changed, 40 insertions(+), 12 deletions(-)
arch/arm/mm/dma-mapping.c +1 -1
@@ -1910,7 +1910,7 @@ arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size, mapping->order = order;spin_lock_init (&mapping->lock); - mapping->domain =iommu_domain_alloc (bus); + mapping->domain =iommu_domain_alloc ();if (!mapping->domain) goto err3;
drivers/gpu/drm/msm/msm_gpu.c +1 -1
@@ -428,7 +428,7 @@ int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,* and have separate page tables per context. For now, to keep things * simple and to get something working, just use a single address space: */ - gpu->iommu =iommu_domain_alloc (&platform_bus_type); + gpu->iommu =iommu_domain_alloc ();if (!gpu->iommu) {dev_err (drm->dev,"failed to allocate IOMMU\n" ); ret = -ENOMEM ;
drivers/iommu/amd_iommu_v2.c +1 -1
@@ -785,7 +785,7 @@ int amd_iommu_init_device(struct pci_dev *pdev, int pasids)if (dev_state->states ==NULL ) goto out_free_dev_state; - dev_state->domain =iommu_domain_alloc (&pci_bus_type); + dev_state->domain =iommu_domain_alloc ();if (dev_state->domain ==NULL ) goto out_free_states;
drivers/iommu/iommu.c +29 -3
@@ -35,6 +35,9 @@ static struct kset *iommu_group_kset;static struct ida iommu_group_ida;static struct mutex iommu_group_mutex; +static struct list_head bus_list; +static struct mutex bus_list_mutex; +struct iommu_group {struct kobject kobj;struct kobject *devices_kobj;
@@ -611,6 +614,10 @@ int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops) bus->iommu_ops = ops; +mutex_lock (&bus_list_mutex); +list_add (&bus->bus_list, &bus_list); +mutex_unlock (&bus_list_mutex); +/* Do IOMMU specific setup for this bus-type */ iommu_bus_init (bus, ops);
@@ -647,19 +654,36 @@ void iommu_set_fault_handler(struct iommu_domain *domain, }EXPORT_SYMBOL_GPL (iommu_set_fault_handler); -struct iommu_domain *iommu_domain_alloc (struct bus_type *bus) +struct iommu_domain *iommu_domain_alloc (void ) {struct iommu_domain *domain; +struct iommu_ops *ops =NULL ; +struct bus_type *bus;int ret; -if (bus ==NULL || bus->iommu_ops ==NULL ) +/* +* Traverse the bus list and verify that same iommu_ops +* are registered for all the bus types. +*/ +mutex_lock (&bus_list_mutex); +list_for_each_entry (bus, &bus_list, bus_list) { +if (!bus->iommu_ops || (ops && (bus->iommu_ops != ops))) { +mutex_unlock (&bus_list_mutex); +return NULL ; + }else if (!ops) { + ops = bus->iommu_ops; + } + } +mutex_unlock (&bus_list_mutex); + +if (!ops)return NULL ; domain =kzalloc (sizeof (*domain),GFP_KERNEL );if (!domain)return NULL ; - domain->ops = bus->iommu_ops; + domain->ops = ops; ret = domain->ops->domain_init (domain);if (ret)
@@ -924,6 +948,8 @@ static int __init iommu_init(void) NULL, kernel_kobj);ida_init (&iommu_group_ida);mutex_init (&iommu_group_mutex); +mutex_init (&bus_list_mutex); +INIT_LIST_HEAD (&bus_list);BUG_ON (!iommu_group_kset);
drivers/media/platform/omap3isp/isp.c +1 -1
@@ -2248,7 +2248,7 @@ static int isp_probe(struct platform_device *pdev) } } - isp->domain =iommu_domain_alloc (pdev->dev.bus); + isp->domain =iommu_domain_alloc ();if (!isp->domain) {dev_err (isp->dev,"can't alloc iommu domain\n" ); ret = -ENOMEM ;
drivers/remoteproc/remoteproc_core.c +1 -1
@@ -110,7 +110,7 @@ static int rproc_enable_iommu(struct rproc *rproc)return 0 ; } - domain =iommu_domain_alloc (dev->bus); + domain =iommu_domain_alloc ();if (!domain) {dev_err (dev,"can't alloc iommu domain\n" );return -ENOMEM ;
drivers/vfio/vfio_iommu_type1.c +1 -1
@@ -783,7 +783,7 @@ static void *vfio_iommu_type1_open(unsigned long arg)/* * Wish we didn't have to know about bus_type here. */ - iommu->domain =iommu_domain_alloc (&pci_bus_type); + iommu->domain =iommu_domain_alloc ();if (!iommu->domain) {kfree (iommu);return ERR_PTR (-EIO );
include/linux/device.h +2 -0
@@ -88,6 +88,7 @@ extern void bus_remove_file(struct bus_type *, struct bus_attribute *);* @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU * driver implementations to a bus and allow the driver to do * bus-specific setup +* @bus_list: List of bus types for which iommu operations have been setup * @p: The private data of the driver core, only the driver core can * touch this. * @lock_key: Lock class key for use by the lock validator
@@ -125,6 +126,7 @@ struct bus_type {const struct dev_pm_ops *pm;struct iommu_ops *iommu_ops; +struct list_head bus_list;struct subsys_private *p;struct lock_class_key lock_key;
include/linux/iommu.h +2 -2
@@ -141,7 +141,7 @@ struct iommu_ops {extern int bus_set_iommu (struct bus_type *bus,struct iommu_ops *ops);extern bool iommu_present (struct bus_type *bus); -extern struct iommu_domain *iommu_domain_alloc (struct bus_type *bus); +extern struct iommu_domain *iommu_domain_alloc (void );extern struct iommu_group *iommu_group_get_by_id (int id);extern void iommu_domain_free (struct iommu_domain *domain); extern int iommu_attach_device(struct iommu_domain *domain,
@@ -242,7 +242,7 @@ static inline bool iommu_present(struct bus_type *bus)return false; } -static inline struct iommu_domain *iommu_domain_alloc (struct bus_type *bus) +static inline struct iommu_domain *iommu_domain_alloc (void ) {return NULL ; }
virt/kvm/iommu.c +1 -1
@@ -238,7 +238,7 @@ int kvm_iommu_map_guest(struct kvm *kvm)mutex_lock (&kvm->slots_lock); - kvm->arch.iommu_domain =iommu_domain_alloc (&pci_bus_type); + kvm->arch.iommu_domain =iommu_domain_alloc ();if (!kvm->arch.iommu_domain) { r = -ENOMEM ; goto out_unlock;
Signature · 1 line
1.7.9.5
Quoted 9 lines · Varun Sethi
This patch attempts to remove
iommu_domain_allocfunction's dependency on the bus type. This dependency is quiet restrictive in case of vfio, where it's possible to bind multiple iommu groups (from different bus types) to the same iommu domain.This patch is based on the assumption, that there is a single iommu for all bus types on the system.
We maintain a list of bus types (for which iommu ops are registered). In the
iommu_domain_allocfunction we ensure that all bus types correspond to the same set of iommu operations.
Seems like this just kicks the problem down the road a little ways as I expect the assumption isn't going to last long. I think there's another way to do this and we can do it entirely from within vfio_iommu_type1. We have a problem on x86 that the IOMMU driver can be backed by multiple IOMMU hardware devices. These separate devices are architecturally allowed to have different properties. The property causing us trouble is cache coherency. Some hardware devices allow us to use IOMMU_CACHE as a mapping attribute, others do not. Therefore we cannot use a single IOMMU domain to optimally handle all devices in a heterogeneous environment.
I think the solution to this is to have vfio_iommu_type1 transparently support multiple IOMMU domains. In the implementation of that, it seems to make sense to move the iommu_domain_alloc() to the point where we attach a group to the domain. That means we can scan the devices in the domain to determine the bus. I suppose there is still an assumption that all the devices in a group are on the same bus, but since the group is determined by the IOMMU and we already assume only a single IOMMU per bus, I think we're ok. I spent some time working on a patch to do this, but it isn't quite finished. I'll try to bandage the rough edges and send it out as an RFC so you can see what I'm talking about. Thanks,
Alex
Quoted 221 lines
Signed-off-by Varun Sethi <[email protected]>--- arch/arm/mm/dma-mapping.c | 2 +- drivers/gpu/drm/msm/msm_gpu.c | 2 +- drivers/iommu/amd_iommu_v2.c | 2 +- drivers/iommu/iommu.c | 32 +++++++++++++++++++++++++++++--- drivers/media/platform/omap3isp/isp.c | 2 +- drivers/remoteproc/remoteproc_core.c | 2 +- drivers/vfio/vfio_iommu_type1.c | 2 +- include/linux/device.h | 2 ++ include/linux/iommu.h | 4 ++-- virt/kvm/iommu.c | 2 +- 10 files changed, 40 insertions(+), 12 deletions(-)arch/arm/mm/dma-mapping.c +1
-1@@ -1910,7 +1910,7 @@ arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size, mapping->order = order;spin_lock_init (&mapping->lock); - mapping->domain =iommu_domain_alloc (bus); + mapping->domain =iommu_domain_alloc ();if (!mapping->domain) goto err3;drivers/gpu/drm/msm/msm_gpu.c +1
-1@@ -428,7 +428,7 @@ int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,* and have separate page tables per context. For now, to keep things * simple and to get something working, just use a single address space: */ - gpu->iommu =iommu_domain_alloc (&platform_bus_type); + gpu->iommu =iommu_domain_alloc ();if (!gpu->iommu) {dev_err (drm->dev,"failed to allocate IOMMU\n" ); ret = -ENOMEM ;drivers/iommu/amd_iommu_v2.c +1
-1@@ -785,7 +785,7 @@ int amd_iommu_init_device(struct pci_dev *pdev, int pasids)if (dev_state->states ==NULL ) goto out_free_dev_state; - dev_state->domain =iommu_domain_alloc (&pci_bus_type); + dev_state->domain =iommu_domain_alloc ();if (dev_state->domain ==NULL ) goto out_free_states;drivers/iommu/iommu.c +29
-3@@ -35,6 +35,9 @@ static struct kset *iommu_group_kset;static struct ida iommu_group_ida;static struct mutex iommu_group_mutex; +static struct list_head bus_list; +static struct mutex bus_list_mutex; +struct iommu_group {struct kobject kobj;struct kobject *devices_kobj;@@ -611,6 +614,10 @@ int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops) bus->iommu_ops = ops; +mutex_lock (&bus_list_mutex); +list_add (&bus->bus_list, &bus_list); +mutex_unlock (&bus_list_mutex); +/* Do IOMMU specific setup for this bus-type */ iommu_bus_init (bus, ops);@@ -647,19 +654,36 @@ void iommu_set_fault_handler(struct iommu_domain *domain, }EXPORT_SYMBOL_GPL (iommu_set_fault_handler); -struct iommu_domain *iommu_domain_alloc (struct bus_type *bus) +struct iommu_domain *iommu_domain_alloc (void ) {struct iommu_domain *domain; +struct iommu_ops *ops =NULL ; +struct bus_type *bus;int ret; -if (bus ==NULL || bus->iommu_ops ==NULL ) +/* +* Traverse the bus list and verify that same iommu_ops +* are registered for all the bus types. +*/ +mutex_lock (&bus_list_mutex); +list_for_each_entry (bus, &bus_list, bus_list) { +if (!bus->iommu_ops || (ops && (bus->iommu_ops != ops))) { +mutex_unlock (&bus_list_mutex); +return NULL ; + }else if (!ops) { + ops = bus->iommu_ops; + } + } +mutex_unlock (&bus_list_mutex); + +if (!ops)return NULL ; domain =kzalloc (sizeof (*domain),GFP_KERNEL );if (!domain)return NULL ; - domain->ops = bus->iommu_ops; + domain->ops = ops; ret = domain->ops->domain_init (domain);if (ret)@@ -924,6 +948,8 @@ static int __init iommu_init(void) NULL, kernel_kobj);ida_init (&iommu_group_ida);mutex_init (&iommu_group_mutex); +mutex_init (&bus_list_mutex); +INIT_LIST_HEAD (&bus_list);BUG_ON (!iommu_group_kset);drivers/media/platform/omap3isp/isp.c +1
-1@@ -2248,7 +2248,7 @@ static int isp_probe(struct platform_device *pdev) } } - isp->domain =iommu_domain_alloc (pdev->dev.bus); + isp->domain =iommu_domain_alloc ();if (!isp->domain) {dev_err (isp->dev,"can't alloc iommu domain\n" ); ret = -ENOMEM ;drivers/remoteproc/remoteproc_core.c +1
-1@@ -110,7 +110,7 @@ static int rproc_enable_iommu(struct rproc *rproc)return 0 ; } - domain =iommu_domain_alloc (dev->bus); + domain =iommu_domain_alloc ();if (!domain) {dev_err (dev,"can't alloc iommu domain\n" );return -ENOMEM ;drivers/vfio/vfio_iommu_type1.c +1
-1@@ -783,7 +783,7 @@ static void *vfio_iommu_type1_open(unsigned long arg)/* * Wish we didn't have to know about bus_type here. */ - iommu->domain =iommu_domain_alloc (&pci_bus_type); + iommu->domain =iommu_domain_alloc ();if (!iommu->domain) {kfree (iommu);return ERR_PTR (-EIO );include/linux/device.h +2
-0@@ -88,6 +88,7 @@ extern void bus_remove_file(struct bus_type *, struct bus_attribute *);* @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU * driver implementations to a bus and allow the driver to do * bus-specific setup +* @bus_list: List of bus types for which iommu operations have been setup * @p: The private data of the driver core, only the driver core can * touch this. * @lock_key: Lock class key for use by the lock validator @@ -125,6 +126,7 @@ struct bus_type {const struct dev_pm_ops *pm;struct iommu_ops *iommu_ops; +struct list_head bus_list;struct subsys_private *p;struct lock_class_key lock_key;include/linux/iommu.h +2
-2@@ -141,7 +141,7 @@ struct iommu_ops {extern int bus_set_iommu (struct bus_type *bus,struct iommu_ops *ops);extern bool iommu_present (struct bus_type *bus); -extern struct iommu_domain *iommu_domain_alloc (struct bus_type *bus); +extern struct iommu_domain *iommu_domain_alloc (void );extern struct iommu_group *iommu_group_get_by_id (int id);extern void iommu_domain_free (struct iommu_domain *domain); extern int iommu_attach_device(struct iommu_domain *domain,@@ -242,7 +242,7 @@ static inline bool iommu_present(struct bus_type *bus)return false; } -static inline struct iommu_domain *iommu_domain_alloc (struct bus_type *bus) +static inline struct iommu_domain *iommu_domain_alloc (void ) {return NULL ; }virt/kvm/iommu.c +1
-1@@ -238,7 +238,7 @@ int kvm_iommu_map_guest(struct kvm *kvm)mutex_lock (&kvm->slots_lock); - kvm->arch.iommu_domain =iommu_domain_alloc (&pci_bus_type); + kvm->arch.iommu_domain =iommu_domain_alloc ();if (!kvm->arch.iommu_domain) { r = -ENOMEM ; goto out_unlock;
Quoted 34 lines
-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Saturday, January 18, 2014 1:39 AM
To: Sethi Varun-B16395
Cc: [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; linux- [email protected]
Subject: Re: [RFC][PATCH] Remove bus dependency foriommu_domain_alloc.Quoted 1 line · Varun Sethi
This patch attempts to remove
iommu_domain_allocfunction's dependencyon the bus type.
Quoted 2 lines
This dependency is quiet restrictive in case of vfio, where it's possible to bind multiple iommu groups (from different bus types) to
the same iommu domain.
Quoted 6 lines
This patch is based on the assumption, that there is a single iommu for all bus types on the system.
We maintain a list of bus types (for which iommu ops are registered). In the
iommu_domain_allocfunction we ensure that all bus typescorrespond to the same set of iommu operations.
Seems like this just kicks the problem down the road a little ways as I expect the assumption isn't going to last long. I think there's another way to do this and we can do it entirely from within
vfio_iommu_type1. We have a problem on x86 that the IOMMU driver can be backed by multiple IOMMU hardware devices. These separate devices are architecturally allowed to have different properties. The property causing us trouble is cache coherency. Some hardware devices allow us to useIOMMU_CACHEas a mapping attribute, others do not. Therefore we cannot use a single IOMMU domain to optimally handle all devices in a heterogeneous environment.
Quoted 4 lines
I think the solution to this is to have
vfio_iommu_type1transparently support multiple IOMMU domains. In the implementation of that, it seems to make sense to move theiommu_domain_alloc()to the point where we attach a group to the domain. That means we can scan the devices in the
[Sethi Varun-B16395] Multiple iommu groups can also share the same domain (as a part Of the same VFIO container). I am not sure how can we handle the case of iommu groups from Different bus types in vfio.
-Varun
Quoted 23 lines
domain to determine the bus. I suppose there is still an assumption that all the devices in a group are on the same bus, but since the group is determined by the IOMMU and we already assume only a single IOMMU per bus, I think we're ok. I spent some time working on a patch to do this, but it isn't quite finished. I'll try to bandage the rough edges and send it out as an RFC so you can see what I'm talking about. Thanks,
Alex
Quoted 6 lines
Signed-off-by Varun Sethi <[email protected]>--- arch/arm/mm/dma-mapping.c | 2 +- drivers/gpu/drm/msm/msm_gpu.c | 2 +- drivers/iommu/amd_iommu_v2.c | 2 +- drivers/iommu/iommu.c | 32+++++++++++++++++++++++++++++---
Quoted 7 lines
drivers/media/platform/omap3isp/isp.c | 2 +- drivers/remoteproc/remoteproc_core.c | 2 +- drivers/vfio/vfio_iommu_type1.c | 2 +- include/linux/device.h | 2 ++ include/linux/iommu.h | 4 ++-- virt/kvm/iommu.c | 2 +- 10 files changed, 40 insertions(+), 12 deletions(-)
Quoted 33 lines · Varun Sethi
Quoted 33 lines
-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Saturday, January 18, 2014 1:39 AM
To: Sethi Varun-B16395
Cc: [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; linux- [email protected]
Subject: Re: [RFC][PATCH] Remove bus dependency foriommu_domain_alloc.Quoted 1 line · Varun Sethi
This patch attempts to remove
iommu_domain_allocfunction's dependencyon the bus type.
Quoted 2 lines
This dependency is quiet restrictive in case of vfio, where it's possible to bind multiple iommu groups (from different bus types) to
the same iommu domain.
Quoted 6 lines
This patch is based on the assumption, that there is a single iommu for all bus types on the system.
We maintain a list of bus types (for which iommu ops are registered). In the
iommu_domain_allocfunction we ensure that all bus typescorrespond to the same set of iommu operations.
Seems like this just kicks the problem down the road a little ways as I expect the assumption isn't going to last long. I think there's another way to do this and we can do it entirely from within
vfio_iommu_type1. We have a problem on x86 that the IOMMU driver can be backed by multiple IOMMU hardware devices. These separate devices are architecturally allowed to have different properties. The property causing us trouble is cache coherency. Some hardware devices allow us to useIOMMU_CACHEas a mapping attribute, others do not. Therefore we cannot use a single IOMMU domain to optimally handle all devices in a heterogeneous environment.
Quoted 7 lines
Quoted 4 lines
I think the solution to this is to have
vfio_iommu_type1transparently support multiple IOMMU domains. In the implementation of that, it seems to make sense to move theiommu_domain_alloc()to the point where we attach a group to the domain. That means we can scan the devices in the[Sethi Varun-B16395] Multiple iommu groups can also share the same domain (as a part Of the same VFIO container). I am not sure how can we handle the case of iommu groups from Different bus types in vfio.
Correct and believe I handle this. The difference is that rather than attaching a new group to and old domain and hoping for the best, we now allocate a domain for each group, attach the group to the new domain, then compare the capabilities of the new domain to the old domain. If we determine they are compatible, we throw away the new domain and use the old one. If they are not compatible, for instance if they are for different bus_types or if the cache coherence support is different, they remain separate and we duplicate mappings to both domains. Hopefully this is more clear in the code I just sent. Thanks,
Alex
Quoted 24 lines
Quoted 23 lines
domain to determine the bus. I suppose there is still an assumption that all the devices in a group are on the same bus, but since the group is determined by the IOMMU and we already assume only a single IOMMU per bus, I think we're ok. I spent some time working on a patch to do this, but it isn't quite finished. I'll try to bandage the rough edges and send it out as an RFC so you can see what I'm talking about. Thanks,
Alex
Quoted 6 lines
Signed-off-by Varun Sethi <[email protected]>--- arch/arm/mm/dma-mapping.c | 2 +- drivers/gpu/drm/msm/msm_gpu.c | 2 +- drivers/iommu/amd_iommu_v2.c | 2 +- drivers/iommu/iommu.c | 32+++++++++++++++++++++++++++++---
Quoted 7 lines
drivers/media/platform/omap3isp/isp.c | 2 +- drivers/remoteproc/remoteproc_core.c | 2 +- drivers/vfio/vfio_iommu_type1.c | 2 +- include/linux/device.h | 2 ++ include/linux/iommu.h | 4 ++-- virt/kvm/iommu.c | 2 +- 10 files changed, 40 insertions(+), 12 deletions(-)