iommu/vt-d: Use ida for domain ID management
Subsystems (2)
Lu Baolu
maintainerv2 cover iommu/vt-d: Use ida for domain ID management
This converts the Intel iommu driver's domain ID management from a fixed-size bitmap to the dynamic ida allocator. This improves memory efficiency by only allocating resources for the domain IDs actually in use, rather than the maximum possible number.
The also includes necessary cleanups after the ida conversion, including locking adjustment for the ida.
---
Change log:
v2:
- Drop the last patch which simplified the code with __free(). There needs a helper like xa_store_or_{reset,kfree}(). Thus I plan to put it a separated series with broader reviewers.
v1: https://lore.kernel.org/linux-iommu/[email protected]/
Lu Baolu (2):
iommu/vt-d: Use ida to manage domain id
iommu/vt-d: Replace spin_lock with mutex to protect domain ida
drivers/iommu/intel/dmar.c | 4 ++ drivers/iommu/intel/iommu.c | 90 ++++++++----------------------------- drivers/iommu/intel/iommu.h | 21 +++++++-- 3 files changed, 40 insertions(+), 75 deletions(-)
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH v2 1/2 iommu/vt-d: Use ida to manage domain id
Switch the intel iommu driver to use the ida mechanism for managing domain IDs, replacing the previous fixed-size bitmap.
The previous approach allocated a bitmap large enough to cover the maximum number of domain IDs supported by the hardware, regardless of the actual number of domains in use. This led to unnecessary memory consumption, especially on systems supporting a large number of iommu units but only utilizing a small number of domain IDs.
The ida allocator dynamically manages the allocation and freeing of integer IDs, only consuming memory for the IDs that are currently in use. This significantly optimizes memory usage compared to the fixed-size bitmap.
--- drivers/iommu/intel/dmar.c | 3 ++ drivers/iommu/intel/iommu.c | 80 ++++++++----------------------------- drivers/iommu/intel/iommu.h | 19 +++++++-- 3 files changed, 34 insertions(+), 68 deletions(-)
drivers/iommu/intel/dmar.c +3 -0
@@ -1099,6 +1099,8 @@ static int alloc_iommu(struct dmar_drhd_unit *drhd)spin_lock_init (&iommu->device_rbtree_lock);mutex_init (&iommu->iopf_lock); iommu->node =NUMA_NO_NODE ; +spin_lock_init (&iommu->lock); +ida_init (&iommu->domain_ida); ver =readl (iommu->reg +DMAR_VER_REG ); pr_info("%s: reg_base_addr %llx ver %d:%d cap %llx ecap %llx\n",
@@ -1195,6 +1197,7 @@ static void free_iommu(struct intel_iommu *iommu)if (iommu->reg)unmap_iommu (iommu); +ida_destroy (&iommu->domain_ida);ida_free (&dmar_seq_ids, iommu->seq_id);kfree (iommu); }
drivers/iommu/intel/iommu.c +16 -64
150 lines not on this page · raw message
drivers/iommu/intel/iommu.h +15 -4
@@ -722,7 +722,7 @@ struct intel_iommu {unsigned char name[16 ];/* Device Name */ #ifdef CONFIG_INTEL_IOMMU -unsigned long *domain_ids;/* bitmap of domains */ +struct ida domain_ida;/* domain id allocator */ unsigned long *copied_tables;/* bitmap of copied tables */ spinlock_t lock;/* protect context, domain ids */ struct root_entry *root_entry;/* virtual address */
@@ -809,11 +809,22 @@ static inline struct dmar_domain *to_dmar_domain(struct iommu_domain *dom) }/* -* Domain ID reserved for pasid entries programmed for first-level -* only and pass-through transfer modes. +* Domain ID 0 and 1 are reserved: +* +* If Caching mode is set, then invalid translations are tagged +* with domain-id 0, hence we need to pre-allocate it. We also +* use domain-id 0 as a marker for non-allocated domain-id, so +* make sure it is not used for a real domain. +* +* Vt-d spec rev3.0 (section 6.2.3.1) requires that each pasid +* entry for first-level or pass-through translation modes should +* be programmed with a domain id different from those used for +* second-level or nested translation. We reserve a domain id for +* this purpose. This domain id is also used for identity domain +* in legacy mode. */ #define FLPT_DEFAULT_DID 1 -#define NUM_RESERVED_DID 2 +#define IDA_START_DID 2/* Retrieve the domain ID which has allocated to the domain */ static inline u16
Signature · 1 line
2.43.0
Re: [PATCH v2 1/2] iommu/vt-d: Use ida to manage domain id
Quoted 13 lines · Lu Baolu
Switch the intel iommu driver to use the ida mechanism for managing domain IDs, replacing the previous fixed-size bitmap.
The previous approach allocated a bitmap large enough to cover the maximum number of domain IDs supported by the hardware, regardless of the actual number of domains in use. This led to unnecessary memory consumption, especially on systems supporting a large number of iommu units but only utilizing a small number of domain IDs.
The ida allocator dynamically manages the allocation and freeing of integer IDs, only consuming memory for the IDs that are currently in use. This significantly optimizes memory usage compared to the fixed-size bitmap.
Quoted 7 lines
Signed-off-by Lu Baolu <[email protected]>Reviewed-by Kevin Tian <[email protected]>--- drivers/iommu/intel/dmar.c | 3 ++ drivers/iommu/intel/iommu.c | 80 ++++++++----------------------------- drivers/iommu/intel/iommu.h | 19 +++++++-- 3 files changed, 34 insertions(+), 68 deletions(-)
Jason
Lu Baolu
maintainerPATCH v2 2/2 iommu/vt-d: Replace spin_lock with mutex to protect domain ida
In reply to Lu BaoluThe domain ID allocator is currently protected by a spin_lock. However, ida_alloc_range can potentially block if it needs to allocate memory to grow its internal structures.
Replace the spin_lock with a mutex which allows sleep on block. Thus, the memory allocation flags can be updated from GFP_ATOMIC to GFP_KERNEL to allow blocking memory allocations if necessary.
Introduce a new mutex, did_lock, specifically for protecting the domain ida. The existing spinlock will remain for protecting other intel_iommu fields.
--- drivers/iommu/intel/dmar.c | 1 + drivers/iommu/intel/iommu.c | 12 ++++-------- drivers/iommu/intel/iommu.h | 2 ++ 3 files changed, 7 insertions(+), 8 deletions(-)
drivers/iommu/intel/dmar.c +1 -0
@@ -1101,6 +1101,7 @@ static int alloc_iommu(struct dmar_drhd_unit *drhd) iommu->node =NUMA_NO_NODE ;spin_lock_init (&iommu->lock);ida_init (&iommu->domain_ida); +mutex_init (&iommu->did_lock); ver =readl (iommu->reg +DMAR_VER_REG ); pr_info("%s: reg_base_addr %llx ver %d:%d cap %llx ecap %llx\n",
drivers/iommu/intel/iommu.c +4 -8
@@ -1345,17 +1345,16 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)if (!info)return -ENOMEM ; -spin_lock (&iommu->lock); +guard (mutex)(&iommu->did_lock); curr =xa_load (&domain->iommu_array, iommu->seq_id);if (curr) { curr->refcnt++; -spin_unlock (&iommu->lock);kfree (info);return 0 ; } num =ida_alloc_range (&iommu->domain_ida,IDA_START_DID , -cap_ndoms (iommu->cap) -1 ,GFP_ATOMIC ); +cap_ndoms (iommu->cap) -1 ,GFP_KERNEL );if (num <0 ) {pr_err ("%s: No free domain ids\n" , iommu->name); goto err_unlock;
@@ -1365,19 +1364,17 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) info->did = num; info->iommu = iommu; curr =xa_cmpxchg (&domain->iommu_array, iommu->seq_id, -NULL , info,GFP_ATOMIC ); +NULL , info,GFP_KERNEL );if (curr) { ret =xa_err (curr) ? : -EBUSY ; goto err_clear; } -spin_unlock (&iommu->lock);return 0 ; err_clear:ida_free (&iommu->domain_ida, info->did); err_unlock: -spin_unlock (&iommu->lock);kfree (info);return ret; }
@@ -1389,7 +1386,7 @@ void domain_detach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)if (domain->domain.type ==IOMMU_DOMAIN_SVA )return ; -spin_lock (&iommu->lock); +guard (mutex)(&iommu->did_lock); info =xa_load (&domain->iommu_array, iommu->seq_id);if (--info->refcnt ==0 ) {ida_free (&iommu->domain_ida, info->did);
@@ -1397,7 +1394,6 @@ void domain_detach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) domain->nid =NUMA_NO_NODE ;kfree (info); } -spin_unlock (&iommu->lock); }static void domain_exit (struct dmar_domain *domain)
drivers/iommu/intel/iommu.h +2 -0
@@ -722,6 +722,8 @@ struct intel_iommu {unsigned char name[16 ];/* Device Name */ #ifdef CONFIG_INTEL_IOMMU +/* mutex to protect domain_ida */ +struct mutex did_lock;struct ida domain_ida;/* domain id allocator */ unsigned long *copied_tables;/* bitmap of copied tables */ spinlock_t lock;/* protect context, domain ids */
Signature · 1 line
2.43.0
Re: [PATCH v2 2/2] iommu/vt-d: Replace spin_lock with mutex to protect domain ida
Quoted 12 lines · Lu Baolu
The domain ID allocator is currently protected by a
spin_lock. However,ida_alloc_rangecan potentially block if it needs to allocate memory to grow its internal structures.Replace the
spin_lockwith a mutex which allows sleep on block. Thus, the memory allocation flags can be updated fromGFP_ATOMICtoGFP_KERNELto allow blocking memory allocations if necessary.Introduce a new mutex,
did_lock, specifically for protecting the domain ida. The existing spinlock will remain for protecting otherintel_iommufields.
Quoted 7 lines
Signed-off-by Lu Baolu <[email protected]>Reviewed-by Kevin Tian <[email protected]>--- drivers/iommu/intel/dmar.c | 1 + drivers/iommu/intel/iommu.c | 12 ++++-------- drivers/iommu/intel/iommu.h | 2 ++ 3 files changed, 7 insertions(+), 8 deletions(-)
Jason
Baolu Lu
maintainerQuoted 15 lines · Lu Baolu
This converts the Intel iommu driver's domain ID management from a fixed-size bitmap to the dynamic ida allocator. This improves memory efficiency by only allocating resources for the domain IDs actually in use, rather than the maximum possible number.
The also includes necessary cleanups after the ida conversion, including locking adjustment for the ida.
---
Change log:
v2:
- Drop the last patch which simplified the code with__free(). There needs a helper like xa_store_or_{reset,kfree}(). Thus I plan to put it a separated series with broader reviewers.
Quoted 5 lines
v1:https://lore.kernel.org/linux-iommu/20250423031020.2189546-1- [email protected]/
Lu Baolu (2):
iommu/vt-d: Use ida to manage domain id
iommu/vt-d: Replacespin_lockwith mutex to protect domain ida
Queued for v6.16-rc1.