iommu/vt-d: Some Intel IOMMU cleanups
Subsystems (2)
Lu Baolu
maintainerv2 cover iommu/vt-d: Some Intel IOMMU cleanups
Hi folks,
After a long time of evolution, the drivers/iommu/intel/iommu.c becomes a bit messy. This series tries to cleanup and refactor the driver to make it more concise.
It includes,
- Remove some unnecessary code, includes and prototypes;
- Move creating per-device driver data from domain_attach to device_probe callback.
- Use an array for global per-device driver data so that they could be searched repidly.
- Use the rculist for per-domain device driver data and remove the existing spinlock.
- Code style fix.
Your comments are very appreciated.
Best regards,
baolu
Change log:
v1: initial post
- https://lore.kernel.org/linux-iommu/[email protected]/
v2:
- Avoid migration of large amounts of code.
- Split the cleanup patches into separated ones for convenient code review.
- This version of series is available on github:
https://github.com/LuBaolu/intel-iommu/commits/iommu-vtd-cleanup-v2
Lu Baolu (11):
iommu/vt-d: Remove intel_iommu::domains
iommu/vt-d: Remove finding domain in dmar_insert_one_dev_info() iommu/vt-d: Remove iova_cache_get/put()
iommu/vt-d: Remove domain and devinfo mempool iommu/vt-d: Remove DEFER_DEVICE_DOMAIN_INFO iommu/vt-d: Remove unnecessary includes
iommu/vt-d: Remove unnecessary prototypes
iommu/vt-d: Fix indentation of goto labels iommu/vt-d: Remove commented code
iommu/vt-d: Use xarray for global device_domain_info iommu/vt-d: Use rculist for per-domain device list
include/linux/intel-iommu.h | 6 +- drivers/iommu/intel/debugfs.c | 14 +- drivers/iommu/intel/iommu.c | 597 +++++++++------------------------- drivers/iommu/intel/pasid.c | 12 +- drivers/iommu/intel/svm.c | 6 +- 5 files changed, 165 insertions(+), 470 deletions(-)
Signature · 1 line
2.25.1
Lu Baolu
maintainerPATCH v2 1/11 iommu/vt-d: Remove intel_iommu::domains
The "domains" field of the intel_iommu structure keeps the mapping of domain_id to dmar_domain. This information is not used anywhere. Remove and cleanup it to avoid unnecessary memory consumption.
--- include/linux/intel-iommu.h | 1 - drivers/iommu/intel/iommu.c | 68 ++----------------------------------- 2 files changed, 3 insertions(+), 66 deletions(-)
include/linux/intel-iommu.h +0 -1
@@ -578,7 +578,6 @@ struct intel_iommu {#ifdef CONFIG_INTEL_IOMMU unsigned long *domain_ids;/* bitmap of domains */ -struct dmar_domain ***domains;/* ptr to domains */ spinlock_t lock;/* protect context, domain ids */ struct root_entry *root_entry;/* virtual address */
drivers/iommu/intel/iommu.c +3 -65
119 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
Re: [PATCH v2 01/11] iommu/vt-d: Remove intel_iommu::domains
Quoted 10 lines · Lu Baolu
The "domains" field of the
intel_iommustructure keeps the mapping ofdomain_idtodmar_domain. This information is not used anywhere. Remove and cleanup it to avoid unnecessary memory consumption.Signed-off-by Lu Baolu <[email protected]>Reviewed-by Christoph Hellwig <[email protected]>--- include/linux/intel-iommu.h | 1 - drivers/iommu/intel/iommu.c | 68 ++----------------------------------- 2 files changed, 3 insertions(+), 66 deletions(-)
Jason
Lu Baolu
maintainerPATCH v2 2/11 iommu/vt-d: Remove finding domain in dmar_insert_one_dev_info()
In reply to Lu BaoluThe Intel IOMMU driver has already converted to use default domain framework in iommu core. There's no need to find a domain for the device in the domain attaching path. Cleanup that code.
--- drivers/iommu/intel/iommu.c | 21 --------------------- 1 file changed, 21 deletions(-)
drivers/iommu/intel/iommu.c +0 -21
@@ -2554,7 +2554,6 @@ static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,struct device *dev, struct dmar_domain *domain) { - struct dmar_domain *found = NULL; struct device_domain_info *info;unsigned long flags;int ret;
@@ -2605,26 +2604,6 @@ static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu, }spin_lock_irqsave (&device_domain_lock, flags); -if (dev) - found =find_domain (dev); - -if (!found) { -struct device_domain_info *info2; - info2 =dmar_search_domain_by_dev_info (info->segment, info->bus, - info->devfn); -if (info2) { - found = info2->domain; - info2->dev = dev; - } - } - -if (found) { -spin_unlock_irqrestore (&device_domain_lock, flags); -free_devinfo_mem (info); -/* Caller must free the original domain */ -return found; - } -spin_lock (&iommu->lock); ret =domain_attach_iommu (domain, iommu);spin_unlock (&iommu->lock);
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 02/11] iommu/vt-d: Remove finding domain in dmar_insert_one_dev_info()
Quoted 9 lines · Lu Baolu
The Intel IOMMU driver has already converted to use default domain framework in iommu core. There's no need to find a domain for the device in the domain attaching path. Cleanup that code.
Signed-off-by Lu Baolu <[email protected]>Reviewed-by Christoph Hellwig <[email protected]>--- drivers/iommu/intel/iommu.c | 21 --------------------- 1 file changed, 21 deletions(-)
Jason
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Lu Baolu
maintainerPATCH v2 3/11 iommu/vt-d: Remove iova_cache_get/put()
In reply to Lu BaoluThese have been done in drivers/iommu/dma-iommu.c. Remove this duplicate code.
--- drivers/iommu/intel/iommu.c | 5 ----- 1 file changed, 5 deletions(-)
drivers/iommu/intel/iommu.c +0 -5
@@ -3381,9 +3381,6 @@ static inline int iommu_devinfo_cache_init(void)static int __init iommu_init_mempool (void ) {int ret; - ret =iova_cache_get (); -if (ret) -return ret; ret =iommu_domain_cache_init ();if (ret)
@@ -3395,7 +3392,6 @@ static int __init iommu_init_mempool(void)kmem_cache_destroy (iommu_domain_cache); domain_error: -iova_cache_put ();return -ENOMEM ; }
@@ -3404,7 +3400,6 @@ static void __init iommu_exit_mempool(void) {kmem_cache_destroy (iommu_devinfo_cache);kmem_cache_destroy (iommu_domain_cache); -iova_cache_put (); }static void __init init_no_remapping_devices (void )
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 03/11] iommu/vt-d: Remove iova_cache_get/put()
Quoted 8 lines · Lu Baolu
These have been done in
drivers/iommu/dma-iommu.c. Remove this duplicate code.Signed-off-by Lu Baolu <[email protected]>Reviewed-by Christoph Hellwig <[email protected]>--- drivers/iommu/intel/iommu.c | 5 ----- 1 file changed, 5 deletions(-)
Jasno
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Lu Baolu
maintainerPATCH v2 4/11 iommu/vt-d: Remove domain and devinfo mempool
In reply to Lu BaoluThe domain and devinfo memory blocks are only allocated during device probe and released during remove. There's no hot-path context, hence no need for memory pools.
--- drivers/iommu/intel/iommu.c | 104 ++---------------------------------- 1 file changed, 5 insertions(+), 99 deletions(-)
drivers/iommu/intel/iommu.c +5 -99
@@ -452,9 +452,6 @@ static int __init intel_iommu_setup(char *str) }__setup ("intel_iommu=" , intel_iommu_setup); -static struct kmem_cache *iommu_domain_cache; -static struct kmem_cache *iommu_devinfo_cache; -void *alloc_pgtable_page (int node) {struct page *page;
@@ -471,26 +468,6 @@ void free_pgtable_page(void *vaddr)free_page ((unsigned long )vaddr); } -static inline void *alloc_domain_mem (void ) -{ -return kmem_cache_alloc (iommu_domain_cache,GFP_ATOMIC ); -} - -static void free_domain_mem (void *vaddr) -{ -kmem_cache_free (iommu_domain_cache, vaddr); -} - -static inline void *alloc_devinfo_mem (void ) -{ -return kmem_cache_alloc (iommu_devinfo_cache,GFP_ATOMIC ); -} - -static inline void free_devinfo_mem (void *vaddr) -{ -kmem_cache_free (iommu_devinfo_cache, vaddr); -} -static inline int domain_type_is_si (struct dmar_domain *domain) {return domain->domain.type ==IOMMU_DOMAIN_IDENTITY ;
@@ -1885,11 +1862,10 @@ static struct dmar_domain *alloc_domain(unsigned int type) {struct dmar_domain *domain; - domain =alloc_domain_mem (); + domain =kzalloc (sizeof (*domain),GFP_KERNEL );if (!domain)return NULL ; -memset (domain,0 ,sizeof (*domain)); domain->nid =NUMA_NO_NODE ;if (first_level_by_default (type)) domain->flags |=DOMAIN_FLAG_USE_FIRST_LEVEL ;
@@ -1973,7 +1949,7 @@ static void domain_exit(struct dmar_domain *domain)put_pages_list (&freelist); } -free_domain_mem (domain); +kfree (domain); }/*
@@ -2558,7 +2534,7 @@ static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,unsigned long flags;int ret; - info =alloc_devinfo_mem (); + info =kzalloc (sizeof (*info),GFP_KERNEL );if (!info)return NULL ;
@@ -2574,13 +2550,9 @@ static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu, info->segment =pci_domain_nr (pdev->bus); } - info->ats_supported = info->pasid_supported = info->pri_supported =0 ; - info->ats_enabled = info->pasid_enabled = info->pri_enabled =0 ; - info->ats_qdep =0 ; info->dev = dev; info->domain = domain; info->iommu = iommu; - info->pasid_table =NULL ;if (dev &&dev_is_pci (dev)) {struct pci_dev *pdev =to_pci_dev (info->dev);
@@ -2610,7 +2582,7 @@ static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,if (ret) {spin_unlock_irqrestore (&device_domain_lock, flags); -free_devinfo_mem (info); +kfree (info);return NULL ; }
@@ -3343,65 +3315,6 @@ static int __init init_dmars(void)return ret; } -static inline int iommu_domain_cache_init (void ) -{ -int ret =0 ; - - iommu_domain_cache =kmem_cache_create ("iommu_domain" , -sizeof (struct dmar_domain ), -0 , -SLAB_HWCACHE_ALIGN , - -NULL ); -if (!iommu_domain_cache) { -pr_err ("Couldn't create iommu_domain cache\n" ); - ret = -ENOMEM ; - } - -return ret; -} - -static inline int iommu_devinfo_cache_init (void ) -{ -int ret =0 ; - - iommu_devinfo_cache =kmem_cache_create ("iommu_devinfo" , -sizeof (struct device_domain_info ), -0 , -SLAB_HWCACHE_ALIGN , -NULL ); -if (!iommu_devinfo_cache) { -pr_err ("Couldn't create devinfo cache\n" ); - ret = -ENOMEM ; - } - -return ret; -} - -static int __init iommu_init_mempool (void ) -{ -int ret; - - ret =iommu_domain_cache_init (); -if (ret) - goto domain_error; - - ret =iommu_devinfo_cache_init (); -if (!ret) -return ret; - -kmem_cache_destroy (iommu_domain_cache); -domain_error: - -return -ENOMEM ; -} - -static void __init iommu_exit_mempool (void ) -{ -kmem_cache_destroy (iommu_devinfo_cache); -kmem_cache_destroy (iommu_domain_cache); -} -static void __init init_no_remapping_devices (void ) {struct dmar_drhd_unit *drhd;
@@ -4253,12 +4166,6 @@ int __init intel_iommu_init(void) force_on = (!intel_iommu_tboot_noforce &&tboot_force_iommu ()) ||platform_optin_force_iommu (); -if (iommu_init_mempool ()) { -if (force_on) -panic ("tboot: Failed to initialize iommu memory\n" ); -return -ENOMEM ; - } -down_write (&dmar_global_lock);if (dmar_table_init ()) {if (force_on)
@@ -4379,7 +4286,6 @@ int __init intel_iommu_init(void) out_free_dmar:intel_iommu_free_dmars ();up_write (&dmar_global_lock); -iommu_exit_mempool ();return ret; }
@@ -4436,7 +4342,7 @@ static void __dmar_remove_one_dev_info(struct device_domain_info *info)domain_detach_iommu (domain, iommu);spin_unlock_irqrestore (&iommu->lock, flags); -free_devinfo_mem (info); +kfree (info); }static void dmar_remove_one_dev_info (struct device *dev)
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 04/11] iommu/vt-d: Remove domain and devinfo mempool
Quoted 9 lines · Lu Baolu
The domain and devinfo memory blocks are only allocated during device probe and released during remove. There's no hot-path context, hence no need for memory pools.
Signed-off-by Lu Baolu <[email protected]>Reviewed-by Christoph Hellwig <[email protected]>--- drivers/iommu/intel/iommu.c | 104 ++---------------------------------- 1 file changed, 5 insertions(+), 99 deletions(-)
Jason
Lu Baolu
maintainerPATCH v2 5/11 iommu/vt-d: Remove DEFER_DEVICE_DOMAIN_INFO
In reply to Lu BaoluAllocate and set the per-device iommu private data during iommu device probe. This makes the per-device iommu private data always available during iommu_probe_device() and iommu_release_device(). With this changed, the dummy DEFER_DEVICE_DOMAIN_INFO pointer could be removed. The wrappers for getting the private data and domain are also cleaned.
--- include/linux/intel-iommu.h | 2 - drivers/iommu/intel/debugfs.c | 3 +- drivers/iommu/intel/iommu.c | 186 +++++++++++++--------------------- drivers/iommu/intel/pasid.c | 12 +-- drivers/iommu/intel/svm.c | 6 +- 5 files changed, 79 insertions(+), 130 deletions(-)
include/linux/intel-iommu.h +0 -2
@@ -733,8 +733,6 @@ int for_each_device_domain(int (*fn)(struct device_domain_info *info, void *data), void *data);void iommu_flush_write_buffer (struct intel_iommu *iommu);int intel_iommu_enable_pasid (struct intel_iommu *iommu,struct device *dev); -struct dmar_domain *find_domain (struct device *dev); -struct device_domain_info *get_domain_info (struct device *dev);struct intel_iommu *device_to_iommu (struct device *dev,u8 *bus,u8 *devfn);#ifdef CONFIG_INTEL_IOMMU_SVM
drivers/iommu/intel/debugfs.c +2 -1
@@ -344,7 +344,8 @@ static void pgtable_walk_level(struct seq_file *m, struct dma_pte *pde,static int show_device_domain_translation (struct device *dev,void *data) { -struct dmar_domain *domain =find_domain (dev); +struct device_domain_info *info =dev_iommu_priv_get (dev); +struct dmar_domain *domain = info->domain;struct seq_file *m = data;u64 path[6 ] = {0 };
drivers/iommu/intel/iommu.c +68 -118
313 lines not on this page · raw message
drivers/iommu/intel/pasid.c +6 -6
@@ -150,7 +150,7 @@ int intel_pasid_alloc_table(struct device *dev)int size;might_sleep (); - info =get_domain_info (dev); + info =dev_iommu_priv_get (dev);if (WARN_ON (!info || !dev_is_pci (dev) || info->pasid_table))return -EINVAL ;
@@ -197,7 +197,7 @@ void intel_pasid_free_table(struct device *dev)struct pasid_entry *table;int i, max_pde; - info =get_domain_info (dev); + info =dev_iommu_priv_get (dev);if (!info || !dev_is_pci (dev) || !info->pasid_table)return ;
@@ -223,7 +223,7 @@ struct pasid_table *intel_pasid_get_table(struct device *dev) {struct device_domain_info *info; - info =get_domain_info (dev); + info =dev_iommu_priv_get (dev);if (!info)return NULL ;
@@ -234,7 +234,7 @@ static int intel_pasid_get_dev_max_id(struct device *dev) {struct device_domain_info *info; - info =get_domain_info (dev); + info =dev_iommu_priv_get (dev);if (!info || !info->pasid_table)return 0 ;
@@ -254,7 +254,7 @@ static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid)return NULL ; dir = pasid_table->table; - info =get_domain_info (dev); + info =dev_iommu_priv_get (dev); dir_index = pasid >>PASID_PDE_SHIFT ; index = pasid &PASID_PTE_MASK ;
@@ -487,7 +487,7 @@ devtlb_invalidation_with_pasid(struct intel_iommu *iommu,struct device_domain_info *info;u16 sid, qdep, pfsid; - info =get_domain_info (dev); + info =dev_iommu_priv_get (dev);if (!info || !info->ats_enabled)return ;
drivers/iommu/intel/svm.c +3 -3
@@ -200,7 +200,7 @@ static void __flush_svm_range_dev(struct intel_svm *svm,unsigned long address, unsigned long pages, int ih) { - struct device_domain_info *info = get_domain_info(sdev->dev); + struct device_domain_info *info = dev_iommu_priv_get(sdev->dev);if (WARN_ON (!pages))return ;
@@ -337,7 +337,7 @@ static struct iommu_sva *intel_svm_bind_mm(struct intel_iommu *iommu, struct mm_struct *mm, unsigned int flags) { -struct device_domain_info *info =get_domain_info (dev); +struct device_domain_info *info =dev_iommu_priv_get (dev);unsigned long iflags, sflags;struct intel_svm_dev *sdev;struct intel_svm *svm;
@@ -545,7 +545,7 @@ static void intel_svm_drain_prq(struct device *dev, u32 pasid)u16 sid, did;int qdep; - info =get_domain_info (dev); + info =dev_iommu_priv_get (dev);if (WARN_ON (!info || !dev_is_pci (dev)))return ;
Signature · 1 line
2.25.1
Re: [PATCH v2 05/11] iommu/vt-d: Remove DEFER_DEVICE_DOMAIN_INFO
Looks good,
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 05/11] iommu/vt-d: Remove DEFER_DEVICE_DOMAIN_INFO
In reply to Lu Baoludrivers/iommu/intel/debugfs.c static void pgtable_walk_level(struct seq_file *m, struct dma_pte *pde, · line 348
Quoted 11 lines · Lu Baolu
drivers/iommu/intel/debugfs.c +2
-1@@ -344,7 +344,8 @@ static void pgtable_walk_level(struct seq_file *m, struct dma_pte *pde,static int show_device_domain_translation (struct device *dev,void *data) { -struct dmar_domain *domain =find_domain (dev); +struct device_domain_info *info =dev_iommu_priv_get (dev); +struct dmar_domain *domain = info->domain;
It might be nice to someday call 'struct device_domain_info' something clearer since it is now the dev_iommu_priv..
Jason
Lu Baolu
maintainerRe: [PATCH v2 05/11] iommu/vt-d: Remove DEFER_DEVICE_DOMAIN_INFO
Quoted 8 lines · Jason Gunthorpe
Quoted 6 lines · Lu Baolu
drivers/iommu/intel/debugfs.c +0
-0@@ -344,7 +344,8 @@ static void pgtable_walk_level(struct seq_file *m, struct dma_pte *pde,
Quoted 8 lines
drivers/iommu/intel/debugfs.c static void pgtable_walk_level(struct seq_file *m, struct dma_pte *pde, · line 348
Quoted 5 lines
drivers/iommu/intel/debugfs.c +2
-1static int show_device_domain_translation (struct device *dev,void *data) { -struct dmar_domain *domain =find_domain (dev); +struct device_domain_info *info =dev_iommu_priv_get (dev); +struct dmar_domain *domain = info->domain;It might be nice to someday call 'struct
device_domain_info' something clearer since it is now thedev_iommu_priv..
I have the same idea. :-) I will rename it later.
Quoted 4 lines
Reviewed-by Jason Gunthorpe <[email protected]>Jason
Best regards,
baolu
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Lu Baolu
maintainerPATCH v2 6/11 iommu/vt-d: Remove unnecessary includes
In reply to Lu BaoluRemove unnecessary include files and sort the remaining alphabetically.
--- drivers/iommu/intel/iommu.c | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-)
drivers/iommu/intel/iommu.c +7 -27
@@ -13,38 +13,18 @@#define pr_fmt (fmt) "DMAR: " fmt#define dev_fmt (fmt) pr_fmt(fmt) -#include <linux/init.h> -#include <linux/bitmap.h> -#include <linux/debugfs.h> -#include <linux/export.h> -#include <linux/slab.h> -#include <linux/irq.h> -#include <linux/interrupt.h> -#include <linux/spinlock.h> -#include <linux/pci.h> -#include <linux/dmar.h> -#include <linux/dma-map-ops.h> -#include <linux/mempool.h> -#include <linux/memory.h> -#include <linux/cpu.h> -#include <linux/timer.h> -#include <linux/io.h> -#include <linux/iova.h> -#include <linux/iommu.h> +#include <linux/crash_dump.h> +#include <linux/dma-direct.h> #include <linux/dma-iommu.h> +#include <linux/dmi.h> #include <linux/intel-iommu.h> #include <linux/intel-svm.h> +#include <linux/memory.h> +#include <linux/pci.h> +#include <linux/pci-ats.h> +#include <linux/spinlock.h> #include <linux/syscore_ops.h> #include <linux/tboot.h> -#include <linux/dmi.h> -#include <linux/pci-ats.h> -#include <linux/memblock.h> -#include <linux/dma-direct.h> -#include <linux/crash_dump.h> -#include <linux/numa.h> -#include <asm/irq_remapping.h> -#include <asm/cacheflush.h> -#include <asm/iommu.h> #include "../irq_remapping.h" #include "../iommu-sva-lib.h"
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 06/11] iommu/vt-d: Remove unnecessary includes
Quoted 1 line · Lu Baolu
Remove unnecessary include files and sort the remaining alphabetically.
I can't see I like any of the sorting schemes very much, but dropping the unused includes is always good, so:
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 06/11] iommu/vt-d: Remove unnecessary includes
In reply to Lu BaoluQuoted 6 lines · Lu Baolu
Remove unnecessary include files and sort the remaining alphabetically.
Signed-off-by Lu Baolu <[email protected]>--- drivers/iommu/intel/iommu.c | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-)
Are we sorting includes alphabetically now?
Jason
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Lu Baolu
maintainerRe: [PATCH v2 06/11] iommu/vt-d: Remove unnecessary includes
Quoted 9 lines · Jason Gunthorpe
Quoted 6 lines · Lu Baolu
Remove unnecessary include files and sort the remaining alphabetically.
Signed-off-by Lu Baolu <[email protected]>--- drivers/iommu/intel/iommu.c | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-)Are we sorting includes alphabetically now?
I sorted them in this patch.
Quoted 4 lines
Reviewed-by Jason Gunthorpe <[email protected]>Jason
Best regards,
baolu
Lu Baolu
maintainerPATCH v2 7/11 iommu/vt-d: Remove unnecessary prototypes
In reply to Lu BaoluSome prototypes in iommu.c are unnecessary. Delete them.
--- drivers/iommu/intel/iommu.c | 5 ----- 1 file changed, 5 deletions(-)
drivers/iommu/intel/iommu.c +0 -5
@@ -296,14 +296,9 @@ static LIST_HEAD(dmar_satc_units);/* bitmap for indexing intel_iommus */ static int g_num_of_iommus; -static void domain_exit (struct dmar_domain *domain);static void domain_remove_dev_info (struct dmar_domain *domain);static void dmar_remove_one_dev_info (struct device *dev);static void __dmar_remove_one_dev_info (struct device_domain_info *info); -static int intel_iommu_attach_device (struct iommu_domain *domain, -struct device *dev); -static phys_addr_t intel_iommu_iova_to_phys (struct iommu_domain *domain, -dma_addr_t iova);int dmar_disabled = !IS_ENABLED (CONFIG_INTEL_IOMMU_DEFAULT_ON );int intel_iommu_sm =IS_ENABLED (CONFIG_INTEL_IOMMU_SCALABLE_MODE_DEFAULT_ON );
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 07/11] iommu/vt-d: Remove unnecessary prototypes
Quoted 3 lines · Lu Baolu
Some prototypes in
iommu.care unnecessary. Delete them.Signed-off-by Lu Baolu <[email protected]>
Looks good:
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 07/11] iommu/vt-d: Remove unnecessary prototypes
In reply to Lu BaoluQuoted 6 lines · Lu Baolu
Some prototypes in
iommu.care unnecessary. Delete them.Signed-off-by Lu Baolu <[email protected]>--- drivers/iommu/intel/iommu.c | 5 ----- 1 file changed, 5 deletions(-)
Jason
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Lu Baolu
maintainerPATCH v2 8/11 iommu/vt-d: Fix indentation of goto labels
In reply to Lu BaoluRemove blanks before goto labels.
--- drivers/iommu/intel/iommu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/intel/iommu.c +2 -2
@@ -827,7 +827,7 @@ struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn) }if (pdev && drhd->include_all) { - got_pdev: +got_pdev:if (bus && devfn) { *bus = pdev->bus->number; *devfn = pdev->devfn;
@@ -836,7 +836,7 @@ struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn) } } iommu =NULL ; - out: +out:if (iommu_is_dummy (iommu, dev)) iommu =NULL ;
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 08/11] iommu/vt-d: Fix indentation of goto labels
Quoted 1 line · Lu Baolu
Remove blanks before goto labels.
The blanks are still pretty common in parts of the kernel, buf if you want them gone that sounds fine:
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 08/11] iommu/vt-d: Fix indentation of goto labels
In reply to Lu BaoluQuoted 5 lines · Lu Baolu
Remove blanks before goto labels.
Signed-off-by Lu Baolu <[email protected]>drivers/iommu/intel/iommu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
It would be better to rework this function to not have the goto spaghetti in the first place.. Looks like putting the 'for_each_active_dev_scope' into another function would do the trick.
Anyhow, this is more consistent with kernel style:
Jason
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Lu Baolu
maintainerRe: [PATCH v2 08/11] iommu/vt-d: Fix indentation of goto labels
Quoted 10 lines · Jason Gunthorpe
Quoted 5 lines · Lu Baolu
Remove blanks before goto labels.
Signed-off-by Lu Baolu <[email protected]>drivers/iommu/intel/iommu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)It would be better to rework this function to not have the goto spaghetti in the first place.. Looks like putting the '
for_each_active_dev_scope' into another function would do the trick.
Good idea. Let me revisit this later.
Quoted 6 lines
Anyhow, this is more consistent with kernel style:
Reviewed-by Jason Gunthorpe <[email protected]>Jason
Best regards,
baolu
Lu Baolu
maintainerPATCH v2 9/11 iommu/vt-d: Remove commented code
In reply to Lu BaoluThis removes unnecessary commented code.
--- drivers/iommu/intel/iommu.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)
drivers/iommu/intel/iommu.c +1 -9
@@ -1407,15 +1407,7 @@ static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,default :BUG (); } -/* Note: set drain read/write */ -#if 0 -/* -* This is probably to be super secure.. Looks like we can -* ignore it without any impact. -*/ -if (cap_read_drain (iommu->cap)) - val |=DMA_TLB_READ_DRAIN ; -#endif +if (cap_write_drain (iommu->cap)) val |=DMA_TLB_WRITE_DRAIN ;
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 09/11] iommu/vt-d: Remove commented code
Quoted 1 line · Lu Baolu
This removes unnecessary commented code.
Removing dead code is always good:
But someone might really want to take a look if draining makes sense here or not.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
RE: [PATCH v2 09/11] iommu/vt-d: Remove commented code
Quoted 4 lines
From: Christoph Hellwig <[email protected]>
Sent: Monday, February 14, 2022 3:34 PMOn Mon, Feb 14, 2022 at 10:57:02AM +0800, Lu Baolu wrote:
Quoted 8 lines
Quoted 1 line
This removes unnecessary commented code.
Removing dead code is always good:
Reviewed-by Christoph Hellwig <[email protected]>But someone might really want to take a look if draining makes sense here or not.
Looks that dead code has been there since intel-iommu driver was firstly introduced in 2007. I don't know whether we can dig out the reason (Baolu, can you have a check?) why it's only special cased for read draining but not write draining. I cannot find any such recommendation from VT-d spec.
Looking at VT-d spec it stated that since VT-d major version 2 drain is conducted automatically by hardware and above flags are essentially ignored.
Given that possibly a safer option is to always set read/write draining flags before version 2 and skip it after.
Thanks
Kevin
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Lu Baolu
maintainerRe: [PATCH v2 09/11] iommu/vt-d: Remove commented code
Quoted 9 lines · Kevin
Quoted 9 lines
From: Christoph Hellwig<[email protected]>
Sent: Monday, February 14, 2022 3:34 PMQuoted 1 line · Lu Baolu
This removes unnecessary commented code.
Removing dead code is always good:
Reviewed-by Christoph Hellwig<[email protected]>
Quoted 7 lines
Quoted 2 lines
But someone might really want to take a look if draining makes sense here or not.
Looks that dead code has been there since intel-iommu driver was firstly introduced in 2007. I don't know whether we can dig out the reason (Baolu, can you have a check?) why it's only special cased for read draining but not write draining. I cannot find any such recommendation from VT-d spec.
Emm, I have no idea either. Let me check it with the architecture experts.
Quoted 7 lines
Looking at VT-d spec it stated that since VT-d major version 2 drain is conducted automatically by hardware and above flags are essentially ignored.
Given that possibly a safer option is to always set read/write draining flags before version 2 and skip it after.
Best regards,
baolu
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 09/11] iommu/vt-d: Remove commented code
In reply to Lu BaoluQuoted 6 lines · Lu Baolu
This removes unnecessary commented code.
Signed-off-by Lu Baolu <[email protected]>--- drivers/iommu/intel/iommu.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)
Assuming you don't want to uncomment it
Jason
Lu Baolu
maintainerRe: [PATCH v2 09/11] iommu/vt-d: Remove commented code
Quoted 9 lines · Jason Gunthorpe
Quoted 6 lines · Lu Baolu
This removes unnecessary commented code.
Signed-off-by Lu Baolu <[email protected]>--- drivers/iommu/intel/iommu.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)Assuming you don't want to uncomment it
I planned to postpone the decision as I need time to check the details in the VT-d spec and talk to the architecture experts if needed.
Quoted 4 lines
Reviewed-by Jason Gunthorpe <[email protected]>Jason
Best regards,
baolu
Lu Baolu
maintainerPATCH v2 10/11 iommu/vt-d: Use xarray for global device_domain_info
In reply to Lu BaoluReplace the existing global device_domain_list with an array so that it could be rapidly searched. The index of the array is composed by the PCI segment, bus and devfn. Use RCU for lock protection.
--- include/linux/intel-iommu.h | 1 - drivers/iommu/intel/iommu.c | 72 ++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 39 deletions(-)
include/linux/intel-iommu.h +0 -1
@@ -610,7 +610,6 @@ struct intel_iommu {/* PCI domain-device relationship */ struct device_domain_info {struct list_head link;/* link to domain siblings */ -struct list_head global;/* link to global list */ struct list_head table;/* link to pasid table */ u32 segment;/* PCI segment number */ u8 bus;/* PCI bus number */
drivers/iommu/intel/iommu.c +34 -38
144 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
Re: [PATCH v2 10/11] iommu/vt-d: Use xarray for global device_domain_info
drivers/iommu/intel/iommu.c static int iommu_init_domains(struct intel_iommu *iommu)
Quoted 13 lines
const struct
iommu_opsintel_iommu_ops;drivers/iommu/intel/iommu.c +2
-1@@ -903,7 +905,8 @@ static void pgtable_walk(struct intel_iommu *iommu, unsigned long pfn, u8 bus, ustruct dmar_domain *domain;int offset, level; - info =dmar_search_domain_by_dev_info (iommu->segment, bus, devfn); + info =xa_load (&device_domain_array, +devi_idx (iommu->segment, bus, devfn));if (!info || !info->domain) {pr_info ("device [%02x:%02x.%d] not probed\n" , bus,PCI_SLOT (devfn),PCI_FUNC (devfn));@@ -1742,14 +1745,14 @@ static int iommu_init_domains(struct intel_iommu *iommu)
Don't we need a rcu critical section here?
drivers/iommu/intel/iommu.c static void disable_dmar_iommu(struct intel_iommu *iommu) · line 1764
Quoted 12 lines
drivers/iommu/intel/iommu.c +3
-2-list_for_each_entry_safe (info, tmp, &device_domain_list, global) { +rcu_read_lock (); +xa_for_each (&device_domain_array, index, info) {if (info->iommu != iommu)continue ;@@ -1758,7 +1761,7 @@ static void disable_dmar_iommu(struct intel_iommu *iommu)__dmar_remove_one_dev_info (info); } -spin_unlock_irqrestore (&device_domain_lock, flags); +rcu_read_unlock ();
__dmar_remove_one_dev_info asserts that device_domain_lock is held, which these two hunks remove.
drivers/iommu/intel/iommu.c static void intel_iommu_release_device(struct device *dev) · line 4578
Quoted 8 lines
drivers/iommu/intel/iommu.c +2
-1spin_lock_irqsave (&device_domain_lock, flags);dev_iommu_priv_set (dev,NULL ); -list_del (&info->global); +xa_erase (&device_domain_array, +devi_idx (info->segment, info->bus, info->devfn));spin_unlock_irqrestore (&device_domain_lock, flags);kfree (info);
Do we need a kfree_rcu here to allow rcu based access?
_______________________________________________
iommu mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Lu Baolu
maintainerRe: [PATCH v2 10/11] iommu/vt-d: Use xarray for global device_domain_info
Hi Christoph,
Quoted 39 lines · Christoph Hellwig
drivers/iommu/intel/iommu.c static int iommu_init_domains(struct intel_iommu *iommu)
Quoted 13 lines
const struct
iommu_opsintel_iommu_ops;drivers/iommu/intel/iommu.c +2
-1@@ -903,7 +905,8 @@ static void pgtable_walk(struct intel_iommu *iommu, unsigned long pfn, u8 bus, ustruct dmar_domain *domain;int offset, level; - info =dmar_search_domain_by_dev_info (iommu->segment, bus, devfn); + info =xa_load (&device_domain_array, +devi_idx (iommu->segment, bus, devfn));if (!info || !info->domain) {pr_info ("device [%02x:%02x.%d] not probed\n" , bus,PCI_SLOT (devfn),PCI_FUNC (devfn));@@ -1742,14 +1745,14 @@ static int iommu_init_domains(struct intel_iommu *iommu)Don't we need a rcu critical section here?
drivers/iommu/intel/iommu.c static void disable_dmar_iommu(struct intel_iommu *iommu) · line 1764
Quoted 12 lines
drivers/iommu/intel/iommu.c +3
-2-list_for_each_entry_safe (info, tmp, &device_domain_list, global) { +rcu_read_lock (); +xa_for_each (&device_domain_array, index, info) {if (info->iommu != iommu)continue ;@@ -1758,7 +1761,7 @@ static void disable_dmar_iommu(struct intel_iommu *iommu)__dmar_remove_one_dev_info (info); } -spin_unlock_irqrestore (&device_domain_lock, flags); +rcu_read_unlock ();
__dmar_remove_one_dev_infoasserts thatdevice_domain_lockis held, which these two hunks remove.Quoted 7 lines
drivers/iommu/intel/iommu.c +2
-1spin_lock_irqsave (&device_domain_lock, flags);dev_iommu_priv_set (dev,NULL ); -list_del (&info->global); +xa_erase (&device_domain_array, +devi_idx (info->segment, info->bus, info->devfn));spin_unlock_irqrestore (&device_domain_lock, flags);
Quoted 3 lines
Quoted 1 line
kfree(info);Do we need a
kfree_rcuhere to allow rcu based access?
Thanks for your time and very appreciated for your comments. As replied to Jason, I will stop 10/11 and 11/11 for now. From the current usage scenario, the value of such refactoring is limited.
Best regards,
baolu
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v2 10/11] iommu/vt-d: Use xarray for global device_domain_info
In reply to Lu BaoluQuoted 33 lines · Lu Baolu
Replace the existing global
device_domain_listwith an array so that it could be rapidly searched. The index of the array is composed by the PCI segment, bus and devfn. Use RCU for lock protection.Signed-off-by Lu Baolu <[email protected]>include/linux/intel-iommu.h | 1 - drivers/iommu/intel/iommu.c | 72 ++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 39 deletions(-)include/linux/intel-iommu.h +1
-1+++ b/include/linux/intel-iommu.h@@ -610,7 +610,6 @@ struct intel_iommu {/* PCI domain-device relationship */ struct device_domain_info {struct list_head link;/* link to domain siblings */ -struct list_head global;/* link to global list */ struct list_head table;/* link to pasid table */ u32 segment;/* PCI segment number */ u8 bus;/* PCI bus number */ drivers/iommu/intel/iommu.c +1
-2+++ b/drivers/iommu/intel/iommu.c@@ -131,8 +131,6 @@ static struct intel_iommu **g_iommus;static void __init check_tylersburg_isoch (void );static int rwbf_quirk; -static inline struct device_domain_info * -dmar_search_domain_by_dev_info (int segment,int bus,int devfn);/* * set to 1 to panic kernel if can't successfully enable VT-d
drivers/iommu/intel/iommu.c int intel_iommu_gfx_mapped; · line 319
Quoted 5 lines
drivers/iommu/intel/iommu.c +1
-1@@ -318,30 +316,34 @@ int intel_iommu_gfx_mapped;EXPORT_SYMBOL_GPL (intel_iommu_gfx_mapped);DEFINE_SPINLOCK (device_domain_lock); -static LIST_HEAD (device_domain_list); +static DEFINE_XARRAY_ALLOC (device_domain_array);
The 'device_domain_lock' should be replaced by the internal xarray spinlock, no reason to have two locks.
Quoted 22 lines
drivers/iommu/intel/iommu.c +9
-4+ +/* Convert device source ID into the index of device_domain_array. */ +static inline unsigned long devi_idx (unsigned long seg,u8 bus,u8 devfn) +{ +return (seg <<16 ) |PCI_DEVID (bus, devfn); +}/* -* Iterate over elements in device_domain_list and call the specified +* Iterate over elements in device_domain_array and call the specified * callback @fn against each element. */ int for_each_device_domain (int (*fn)(struct device_domain_info *info,void *data),void *data) { -int ret =0 ; -unsigned long flags;struct device_domain_info *info; +unsigned long index; +int ret =0 ; -spin_lock_irqsave (&device_domain_lock, flags);
drivers/iommu/intel/iommu.c int intel_iommu_gfx_mapped; · line 342
Quoted 10 lines
drivers/iommu/intel/iommu.c +4
-5-list_for_each_entry (info, &device_domain_list, global) { +rcu_read_lock (); +xa_for_each (&device_domain_array, index, info) { ret =fn (info, data); -if (ret) { -spin_unlock_irqrestore (&device_domain_lock, flags); -return ret; - } +if (ret) +break ;
And you probably shouldn't try to use RCU. It is really unclear how this function can be useful while racing against intel_iommu_release_device(), eg today the only user of this function does:
static int search_pasid_table(struct device_domain_info *info, void *opaque)
{
struct pasid_table_opaque *data = opaque;
if (info->iommu->segment == data->segment &&
info->bus == data->bus &&
info->devfn == data->devfn &&And even if you kfree_rcu(info) then 'info->iommu->' is still racy unlocked.
RCU is complicated to use, it is not just a drop in replacement for a spinlock.
Jason
Lu Baolu
maintainerRe: [PATCH v2 10/11] iommu/vt-d: Use xarray for global device_domain_info
Hi Jason,
Quoted 45 lines · Jason Gunthorpe
Quoted 22 lines
drivers/iommu/intel/iommu.c +9
-4+ +/* Convert device source ID into the index of device_domain_array. */ +static inline unsigned long devi_idx (unsigned long seg,u8 bus,u8 devfn) +{ +return (seg <<16 ) |PCI_DEVID (bus, devfn); +}/* -* Iterate over elements in device_domain_list and call the specified +* Iterate over elements in device_domain_array and call the specified * callback @fn against each element. */ int for_each_device_domain (int (*fn)(struct device_domain_info *info,void *data),void *data) { -int ret =0 ; -unsigned long flags;struct device_domain_info *info; +unsigned long index; +int ret =0 ; -spin_lock_irqsave (&device_domain_lock, flags);drivers/iommu/intel/iommu.c int intel_iommu_gfx_mapped; · line 342
Quoted 10 lines
drivers/iommu/intel/iommu.c +4
-5-list_for_each_entry (info, &device_domain_list, global) { +rcu_read_lock (); +xa_for_each (&device_domain_array, index, info) { ret =fn (info, data); -if (ret) { -spin_unlock_irqrestore (&device_domain_lock, flags); -return ret; - } +if (ret) +break ;And you probably shouldn't try to use RCU. It is really unclear how this function can be useful while racing against
intel_iommu_release_device(), eg today the only user of this function does:static int
search_pasid_table(struct device_domain_info *info, void *opaque){ struct pasid_table_opaque *data = opaque; if (info->iommu->segment == data->segment && info->bus == data->bus && info->devfn == data->devfn &&
Quoted 5 lines
And even if you
kfree_rcu(info)then 'info->iommu->' is still racy unlocked.RCU is complicated to use, it is not just a drop in replacement for a spinlock.
Thanks for your comments. I am going to stop this patch (and the next 11/11) and spend more time figuring them out.
Best regards,
baolu
Lu Baolu
maintainerPATCH v2 11/11 iommu/vt-d: Use rculist for per-domain device list
In reply to Lu BaoluUse rculist for list of devices attached by a domain. And use the RCU lock for read/write protection. As both the global device_domain array and per-domain device_domain list are protected by RCU lock now, there is no need to use the spinlock anymore. Cleanup device_domain_lock.
--- include/linux/intel-iommu.h | 2 +- drivers/iommu/intel/debugfs.c | 11 +-- drivers/iommu/intel/iommu.c | 126 ++++++++++++++-------------------- 3 files changed, 53 insertions(+), 86 deletions(-)
include/linux/intel-iommu.h +1 -1
@@ -480,7 +480,6 @@ enum {#define VTD_FLAG_SVM_CAPABLE (1 << 2)extern int intel_iommu_sm; -extern spinlock_t device_domain_lock;#define sm_supported (iommu) (intel_iommu_sm && ecap_smts((iommu)->ecap))#define pasid_supported (iommu) (sm_supported(iommu) && \
@@ -609,6 +608,7 @@ struct intel_iommu {/* PCI domain-device relationship */ struct device_domain_info { +struct rcu_head rcu;struct list_head link;/* link to domain siblings */ struct list_head table;/* link to pasid table */ u32 segment;/* PCI segment number */
drivers/iommu/intel/debugfs.c +2 -9
@@ -364,15 +364,8 @@ static int show_device_domain_translation(struct device *dev, void *data)static int domain_translation_struct_show (struct seq_file *m,void *unused) { -unsigned long flags; -int ret; - -spin_lock_irqsave (&device_domain_lock, flags); - ret =bus_for_each_dev (&pci_bus_type,NULL , m, - show_device_domain_translation); -spin_unlock_irqrestore (&device_domain_lock, flags); - -return ret; +return bus_for_each_dev (&pci_bus_type,NULL , m, + show_device_domain_translation); }DEFINE_SHOW_ATTRIBUTE (domain_translation_struct);
drivers/iommu/intel/iommu.c +50 -76
317 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