iommu/vt-d: Reset DMAR_UNITS_SUPPORTED
Subsystems (2)
Lu Baolu
maintainerv2 cover iommu/vt-d: Reset DMAR_UNITS_SUPPORTED
Hi folks,
This is a follow-up series of changes proposed by this patch:
https://lore.kernel.org/linux-iommu/[email protected]/
It removes several static arrays of size DMAR_UNITS_SUPPORTED and sets the DMAR_UNITS_SUPPORTED to 1024.
This series is also available on github:
https://github.com/LuBaolu/intel-iommu/commits/reset-DMAR_UNITS_SUPPORTED-v2
Please help review and suggest.
Best regards,
baolu
Change log:
v2:
- Set the right @max for ida_alloc_range() and return the right error when ida_alloc_range() returns failure.
- Replace xa_store() with xa_cmpxchg().
- Set domain->nid to NUMA_NO_NODE when domain is detached from an iommu.
- Avoid adding a new VTD_FLAG_IOMMU_PROBED flag. Remove the duplicate check directly.
v1:
- https://lore.kernel.org/lkml/[email protected]/
- Initial post.
Lu Baolu (6):
iommu/vt-d: Remove unused domain_get_iommu() iommu/vt-d: Use IDA interface to manage iommu sequence id iommu/vt-d: Refactor iommu information of each domain iommu/vt-d: Remove unnecessary check in intel_iommu_add() iommu/vt-d: Remove global g_iommus array iommu/vt-d: Make DMAR_UNITS_SUPPORTED default 1024
include/linux/dmar.h | 6 +- drivers/iommu/intel/iommu.h | 30 ++++-- drivers/iommu/intel/dmar.c | 35 ++----- drivers/iommu/intel/iommu.c | 189 ++++++++++++++---------------------- drivers/iommu/intel/pasid.c | 2 +- drivers/iommu/intel/svm.c | 2 +- 6 files changed, 103 insertions(+), 161 deletions(-)
Signature · 1 line
2.25.1
Lu Baolu
maintainerPATCH v2 1/6 iommu/vt-d: Remove unused domain_get_iommu()
It is not used anywhere. Remove it to avoid dead code.
--- drivers/iommu/intel/iommu.h | 1 - drivers/iommu/intel/iommu.c | 18 ------------------ 2 files changed, 19 deletions(-)
drivers/iommu/intel/iommu.h +0 -1
@@ -725,7 +725,6 @@ extern int dmar_ir_support(void);void *alloc_pgtable_page (int node);void free_pgtable_page (void *vaddr); -struct intel_iommu *domain_get_iommu (struct dmar_domain *domain);void iommu_flush_write_buffer (struct intel_iommu *iommu);int intel_iommu_enable_pasid (struct intel_iommu *iommu,struct device *dev);struct intel_iommu *device_to_iommu (struct device *dev,u8 *bus,u8 *devfn);
drivers/iommu/intel/iommu.c +0 -18
@@ -445,24 +445,6 @@ int iommu_calculate_agaw(struct intel_iommu *iommu)return __iommu_calculate_agaw (iommu,DEFAULT_DOMAIN_ADDRESS_WIDTH ); } -/* This functionin only returns single iommu in a domain */ -struct intel_iommu *domain_get_iommu (struct dmar_domain *domain) -{ -int iommu_id; - -/* si_domain and vm domain should not get here. */ -if (WARN_ON (!iommu_is_dma_domain (&domain->domain))) -return NULL ; - -for_each_domain_iommu (iommu_id, domain) -break ; - -if (iommu_id <0 || iommu_id >= g_num_of_iommus) -return NULL ; - -return g_iommus[iommu_id]; -} -static inline bool iommu_paging_structure_coherency (struct intel_iommu *iommu) { return sm_supported(iommu) ?
Signature · 1 line
2.25.1
Lu Baolu
maintainerPATCH v2 2/6 iommu/vt-d: Use IDA interface to manage iommu sequence id
In reply to Lu BaoluSwitch dmar unit sequence id allocation and release from bitmap to IDA interface.
--- drivers/iommu/intel/dmar.c | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-)
drivers/iommu/intel/dmar.c +8 -27
@@ -60,7 +60,7 @@ LIST_HEAD(dmar_drhd_units);struct acpi_table_header *__initdata dmar_tbl;static int dmar_dev_scope_status =1 ; -static unsigned long dmar_seq_ids[BITS_TO_LONGS (DMAR_UNITS_SUPPORTED )]; +static DEFINE_IDA (dmar_seq_ids);static int alloc_iommu (struct dmar_drhd_unit *drhd);static void free_iommu (struct intel_iommu *iommu);
@@ -1023,28 +1023,6 @@ static int map_iommu(struct intel_iommu *iommu, u64 phys_addr)return err; } -static int dmar_alloc_seq_id (struct intel_iommu *iommu) -{ - iommu->seq_id =find_first_zero_bit (dmar_seq_ids, -DMAR_UNITS_SUPPORTED ); -if (iommu->seq_id >=DMAR_UNITS_SUPPORTED ) { - iommu->seq_id =-1 ; - }else { -set_bit (iommu->seq_id, dmar_seq_ids); -sprintf (iommu->name,"dmar%d" , iommu->seq_id); - } - -return iommu->seq_id; -} - -static void dmar_free_seq_id (struct intel_iommu *iommu) -{ -if (iommu->seq_id >=0 ) { -clear_bit (iommu->seq_id, dmar_seq_ids); - iommu->seq_id =-1 ; - } -} -static int alloc_iommu (struct dmar_drhd_unit *drhd) {struct intel_iommu *iommu;
@@ -1062,11 +1040,14 @@ static int alloc_iommu(struct dmar_drhd_unit *drhd)if (!iommu)return -ENOMEM ; -if (dmar_alloc_seq_id (iommu) <0 ) { + iommu->seq_id =ida_alloc_range (&dmar_seq_ids,0 , +DMAR_UNITS_SUPPORTED -1 ,GFP_KERNEL ); +if (iommu->seq_id <0 ) {pr_err ("Failed to allocate seq_id\n" ); - err = -ENOSPC ; + err = iommu->seq_id; goto error; } +sprintf (iommu->name,"dmar%d" , iommu->seq_id); err =map_iommu (iommu, drhd->reg_base_addr);if (err) {
@@ -1150,7 +1131,7 @@ static int alloc_iommu(struct dmar_drhd_unit *drhd) err_unmap:unmap_iommu (iommu); error_free_seq_id: -dmar_free_seq_id (iommu); +ida_free (&dmar_seq_ids, iommu->seq_id); error:kfree (iommu);return err;
@@ -1183,7 +1164,7 @@ static void free_iommu(struct intel_iommu *iommu)if (iommu->reg)unmap_iommu (iommu); -dmar_free_seq_id (iommu); +ida_free (&dmar_seq_ids, iommu->seq_id);kfree (iommu); }
Signature · 1 line
2.25.1
RE: [PATCH v2 2/6] iommu/vt-d: Use IDA interface to manage iommu sequence id
Quoted 7 lines
From: Lu Baolu <[email protected]>
Sent: Saturday, July 2, 2022 9:56 AMSwitch dmar unit sequence id allocation and release from bitmap to IDA interface.
Signed-off-by Lu Baolu <[email protected]>
Lu Baolu
maintainerPATCH v2 3/6 iommu/vt-d: Refactor iommu information of each domain
In reply to Lu BaoluWhen a DMA domain is attached to a device, it needs to allocate a domain ID from its IOMMU. Currently, the domain ID information is stored in two static arrays embedded in the domain structure. This can lead to memory waste when the driver is running on a small platform.
This optimizes these static arrays by replacing them with an xarray and consuming memory on demand.
--- drivers/iommu/intel/iommu.h | 29 ++++++--- drivers/iommu/intel/iommu.c | 124 +++++++++++++++++++++--------------- drivers/iommu/intel/pasid.c | 2 +- drivers/iommu/intel/svm.c | 2 +- 4 files changed, 94 insertions(+), 63 deletions(-)
drivers/iommu/intel/iommu.h +20 -9
@@ -21,6 +21,7 @@#include <linux/dmar.h> #include <linux/ioasid.h> #include <linux/bitfield.h> +#include <linux/xarray.h> #include <asm/cacheflush.h> #include <asm/iommu.h>
@@ -524,17 +525,17 @@ struct context_entry {*/ #define DOMAIN_FLAG_USE_FIRST_LEVEL BIT(1) -struct dmar_domain { -int nid;/* node id */ - -unsigned int iommu_refcnt[DMAR_UNITS_SUPPORTED ]; -/* Refcount of devices per iommu */ - - -u16 iommu_did[DMAR_UNITS_SUPPORTED ]; -/* Domain ids per IOMMU. Use u16 since +struct iommu_domain_info { +struct intel_iommu *iommu; +unsigned int refcnt;/* Refcount of devices per iommu */ +u16 did;/* Domain ids per IOMMU. Use u16 since * domain ids are 16 bit wide according * to VT-d spec, section 9.3 */ +}; + +struct dmar_domain { +int nid;/* node id */ +struct xarray iommu_array;/* Attached IOMMU array */ u8 has_iotlb_device:1 ;u8 iommu_coherency:1 ;/* indicate coherency of iommu access */
@@ -640,6 +641,16 @@ static inline struct dmar_domain *to_dmar_domain(struct iommu_domain *dom) return container_of(dom, struct dmar_domain, domain); } +/* Retrieve the domain ID which has allocated to the domain */ +static inline u16 +domain_id_iommu (struct dmar_domain *domain,struct intel_iommu *iommu) +{ +struct iommu_domain_info *info = +xa_load (&domain->iommu_array, iommu->seq_id); + +return info->did; +} +/* * 0: readable * 1: writable
drivers/iommu/intel/iommu.c +72 -52
239 lines not on this page · raw message
drivers/iommu/intel/pasid.c +1 -1
@@ -626,7 +626,7 @@ int intel_pasid_setup_second_level(struct intel_iommu *iommu, } pgd_val =virt_to_phys (pgd); - did = domain->iommu_did[iommu->seq_id]; + did =domain_id_iommu (domain, iommu);spin_lock (&iommu->lock); pte =intel_pasid_get_entry (dev, pasid);
drivers/iommu/intel/svm.c +1 -1
@@ -541,7 +541,7 @@ static void intel_svm_drain_prq(struct device *dev, u32 pasid) domain = info->domain; pdev =to_pci_dev (dev); sid =PCI_DEVID (info->bus, info->devfn); - did = domain->iommu_did[iommu->seq_id]; + did =domain_id_iommu (domain, iommu); qdep =pci_ats_queue_depth (pdev);/*
Signature · 1 line
2.25.1
RE: [PATCH v2 3/6] iommu/vt-d: Refactor iommu information of each domain
drivers/iommu/intel/iommu.c static struct dmar_domain *alloc_domain(unsigned int type) · line 1781
Quoted 9 lines
From: Lu Baolu <[email protected]>
Sent: Saturday, July 2, 2022 9:56 AMdrivers/iommu/intel/iommu.c +5
-1-out_unlock: +set_bit (num, iommu->domain_ids); + info->refcnt =1 ; + info->did = num; + info->iommu = iommu; + domain->nid = iommu->node;
One nit. this line should be removed as it's incorrect to blindly update domain->nid and we should just leave to domain_update_iommu_cap() to decide the right node. Otherwise this causes a policy conflict as here it is the last attached device deciding the node which is different from domain_update_iommu_cap() which picks the node of the first attached device.
Otherwise,
Baolu Lu
maintainerRe: [PATCH v2 3/6] iommu/vt-d: Refactor iommu information of each domain
Quoted 16 lines · Kevin
drivers/iommu/intel/iommu.c static struct dmar_domain *alloc_domain(unsigned int type) · line 1781
Quoted 9 lines
From: Lu Baolu <[email protected]>
Sent: Saturday, July 2, 2022 9:56 AMdrivers/iommu/intel/iommu.c +5
-1-out_unlock: +set_bit (num, iommu->domain_ids); + info->refcnt =1 ; + info->did = num; + info->iommu = iommu; + domain->nid = iommu->node;One nit. this line should be removed as it's incorrect to blindly update
domain->nidand we should just leave todomain_update_iommu_cap()to decide the right node. Otherwise this causes a policy conflict as here it is the last attached device deciding the node which is different fromdomain_update_iommu_cap()which picks the node of the first attached device.
Agreed and updated. Thank you!
Quoted 4 lines
Otherwise,
Reviewed-by Kevin Tian <[email protected]>
Best regards,
baolu
Lu Baolu
maintainerPATCH v2 4/6 iommu/vt-d: Remove unnecessary check in intel_iommu_add()
In reply to Lu BaoluThe Intel IOMMU hot-add process starts from dmar_device_hotplug(). It uses the global dmar_global_lock to synchronize all the hot-add and hot-remove paths. In the hot-add path, the new IOMMU data structures are allocated firstly by dmar_parse_one_drhd() and then initialized by dmar_hp_add_drhd(). All the IOMMU units are allocated and initialized in the same synchronized path. There is no case where any IOMMU unit is created and then initialized for multiple times.
This removes the unnecessary check in intel_iommu_add() which is the last reference place of the global IOMMU array.
--- drivers/iommu/intel/iommu.c | 3 --- 1 file changed, 3 deletions(-)
drivers/iommu/intel/iommu.c +0 -3
@@ -3463,9 +3463,6 @@ static int intel_iommu_add(struct dmar_drhd_unit *dmaru)int sp, ret;struct intel_iommu *iommu = dmaru->iommu; -if (g_iommus[iommu->seq_id]) -return 0 ; - ret =intel_cap_audit (CAP_AUDIT_HOTPLUG_DMAR , iommu);if (ret) goto out;
Signature · 1 line
2.25.1
RE: [PATCH v2 4/6] iommu/vt-d: Remove unnecessary check in intel_iommu_add()
Quoted 11 lines
From: Lu Baolu <[email protected]>
Sent: Saturday, July 2, 2022 9:56 AMThe Intel IOMMU hot-add process starts from
dmar_device_hotplug(). It uses the globaldmar_global_lockto synchronize all the hot-add and hot-remove paths. In the hot-add path, the new IOMMU data structures are allocated firstly bydmar_parse_one_drhd()and then initialized bydmar_hp_add_drhd(). All the IOMMU units are allocated and initialized in the same synchronized path. There is no case where any IOMMU unit is created and then initialized for multiple times.
Quoted 4 lines
This removes the unnecessary check in
intel_iommu_add()which is the last reference place of the global IOMMU array.Signed-off-by Lu Baolu <[email protected]>
Lu Baolu
maintainerPATCH v2 5/6 iommu/vt-d: Remove global g_iommus array
In reply to Lu BaoluThe g_iommus and g_num_of_iommus is not used anywhere. Remove them to avoid dead code.
--- drivers/iommu/intel/iommu.c | 44 ------------------------------------- 1 file changed, 44 deletions(-)
drivers/iommu/intel/iommu.c +0 -44
@@ -126,9 +126,6 @@ static inline unsigned long virt_to_dma_pfn(void *p)return page_to_dma_pfn (virt_to_page (p)); } -/* global iommu list, set NULL for ignored DMAR units */ -static struct intel_iommu **g_iommus; -static void __init check_tylersburg_isoch (void );static int rwbf_quirk;
@@ -287,9 +284,6 @@ static LIST_HEAD(dmar_satc_units);#define for_each_rmrr_units (rmrr) \ list_for_each_entry(rmrr, &dmar_rmrr_units, list) -/* bitmap for indexing intel_iommus */ -static int g_num_of_iommus; -static void dmar_remove_one_dev_info (struct device *dev);int dmar_disabled = !IS_ENABLED (CONFIG_INTEL_IOMMU_DEFAULT_ON );
@@ -1694,8 +1688,6 @@ static void free_dmar_iommu(struct intel_iommu *iommu) iommu->domain_ids =NULL ; } - g_iommus[iommu->seq_id] =NULL ; -/* free context mapping */ free_context_table (iommu);
@@ -2901,36 +2893,6 @@ static int __init init_dmars(void)struct intel_iommu *iommu;int ret; -/* -* for each drhd -* allocate root -* initialize and program root entry to not present -* endfor -*/ -for_each_drhd_unit (drhd) { -/* -* lock not needed as this is only incremented in the single -* threaded kernel __init code path all other access are read -* only -*/ -if (g_num_of_iommus <DMAR_UNITS_SUPPORTED ) { - g_num_of_iommus++; -continue ; - } -pr_err_once ("Exceeded %d IOMMUs\n" ,DMAR_UNITS_SUPPORTED ); - } - -/* Preallocate enough resources for IOMMU hot-addition */ -if (g_num_of_iommus <DMAR_UNITS_SUPPORTED ) - g_num_of_iommus =DMAR_UNITS_SUPPORTED ; - - g_iommus =kcalloc (g_num_of_iommus,sizeof (struct intel_iommu *), -GFP_KERNEL ); -if (!g_iommus) { - ret = -ENOMEM ; - goto error; - } - ret =intel_cap_audit (CAP_AUDIT_STATIC_DMAR ,NULL );if (ret) goto free_iommu;
@@ -2953,8 +2915,6 @@ static int __init init_dmars(void) intel_pasid_max_id); } - g_iommus[iommu->seq_id] = iommu; -intel_iommu_init_qi (iommu); ret =iommu_init_domains (iommu);
@@ -3080,9 +3040,6 @@ static int __init init_dmars(void)free_dmar_iommu (iommu); } -kfree (g_iommus); - -error:return ret; }
@@ -3486,7 +3443,6 @@ static int intel_iommu_add(struct dmar_drhd_unit *dmaru)if (iommu->gcmd &DMA_GCMD_TE )iommu_disable_translation (iommu); - g_iommus[iommu->seq_id] = iommu; ret =iommu_init_domains (iommu);if (ret ==0 ) ret =iommu_alloc_root_entry (iommu);
Signature · 1 line
2.25.1
Lu Baolu
maintainerPATCH v2 6/6 iommu/vt-d: Make DMAR_UNITS_SUPPORTED default 1024
In reply to Lu BaoluIf the available hardware exceeds DMAR_UNITS_SUPPORTED (previously set to MAX_IO_APICS, or 128), it causes these messages: "DMAR: Failed to allocate seq_id", "DMAR: Parse DMAR table failure.", and "x2apic: IRQ remapping doesn't support X2APIC mode x2apic disabled"; and the system fails to boot properly.
To support up to 64 sockets with 10 DMAR units each (640), make the value of DMAR_UNITS_SUPPORTED default 1024.
--- include/linux/dmar.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
include/linux/dmar.h +1 -5
@@ -18,11 +18,7 @@struct acpi_dmar_header ; -#ifdef CONFIG_X86 -# define DMAR_UNITS_SUPPORTED MAX_IO_APICS -#else -# define DMAR_UNITS_SUPPORTED 64 -#endif +#define DMAR_UNITS_SUPPORTED 1024/* DMAR Flags */ #define DMAR_INTR_REMAP 0x1
Signature · 1 line
2.25.1