iommu/vt-d: Use ida for domain ID management
Subsystems (3)
Lu Baolu
maintainercover 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 and code simplification.
Please review and comment.
Lu Baolu (3):
iommu/vt-d: Use ida to manage domain id
iommu/vt-d: Replace spin_lock with mutex to protect domain ida iommu/vt-d: Simplify domain_attach_iommu()
drivers/iommu/intel/dmar.c | 3 + drivers/iommu/intel/iommu.c | 113 ++++++++---------------------------- drivers/iommu/intel/iommu.h | 20 +++++-- 3 files changed, 44 insertions(+), 92 deletions(-)
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH 1/3 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 | 2 + drivers/iommu/intel/iommu.c | 80 ++++++++----------------------------- drivers/iommu/intel/iommu.h | 18 +++++++-- 3 files changed, 33 insertions(+), 67 deletions(-)
drivers/iommu/intel/dmar.c +2 -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",
drivers/iommu/intel/iommu.c +17 -63
150 lines not on this page · raw message
drivers/iommu/intel/iommu.h +14 -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,21 @@ 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/* Retrieve the domain ID which has allocated to the domain */ static inline u16
Signature · 1 line
2.43.0
RE: [PATCH 1/3] iommu/vt-d: Use ida to manage domain id
drivers/iommu/intel/iommu.c static void disable_dmar_iommu(struct intel_iommu *iommu) · line 1307
Quoted 10 lines
From: Lu Baolu <[email protected]>
Sent: Wednesday, April 23, 2025 11:10 AMstatic void
free_dmar_iommu(struct intel_iommu *iommu){drivers/iommu/intel/iommu.c +1
-4-if (iommu->domain_ids) { -bitmap_free (iommu->domain_ids); - iommu->domain_ids =NULL ; - } +ida_destroy (&iommu->domain_ida);
since ida_init() is in alloc_iommu() now, the destroy can be moved to free_iommu().
drivers/iommu/intel/iommu.c int domain_attach_iommu(struct dmar_domain · line 1362
Quoted 11 lines
drivers/iommu/intel/iommu.c +2
-3@@ -1399,14 +1356,13 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)return 0 ; } - ndomains =cap_ndoms (iommu->cap); - num =find_first_zero_bit (iommu->domain_ids, ndomains); -if (num >= ndomains) { + num =ida_alloc_range (&iommu->domain_ida,FLPT_DEFAULT_DID +1 , +cap_ndoms (iommu->cap) -1 ,GFP_ATOMIC );
let's define a macro e.g. FIRST_DID for min.
Baolu Lu
maintainerRe: [PATCH 1/3] iommu/vt-d: Use ida to manage domain id
Quoted 13 lines · Kevin
Review
Quoted 10 lines
From: Lu Baolu <[email protected]>
Sent: Wednesday, April 23, 2025 11:10 AMstatic void free_dmar_iommu(struct intel_iommu *iommu) {(quoted diff) +1
-4- if (iommu->domain_ids) { - bitmap_free(iommu->domain_ids); - iommu->domain_ids = NULL; - } + ida_destroy(&iommu->domain_ida);since
ida_init()is inalloc_iommu()now, the destroy can be moved tofree_iommu().
Sure.
drivers/iommu/intel/dmar.c +1 -0
@@ -1198,6 +1198,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 +0 -2
@@ -1304,8 +1304,6 @@ static void disable_dmar_iommu(struct intel_iommu *iommu)static void free_dmar_iommu (struct intel_iommu *iommu) { -ida_destroy (&iommu->domain_ida); -if (iommu->copied_tables) {bitmap_free (iommu->copied_tables); iommu->copied_tables =NULL ;
Quoted 6 lines
Quoted 6 lines
(quoted diff) +0
-0@@ -1399,14 +1356,13 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) return 0; }
Quoted 8 lines
Review int domain_attach_iommu(struct dmar_domain · line 1362
Quoted 6 lines
(quoted diff) +2
-3- ndomains = cap_ndoms(iommu->cap); - num = find_first_zero_bit(iommu->domain_ids, ndomains); - if (num >= ndomains) { + num = ida_alloc_range(&iommu->domain_ida, FLPT_DEFAULT_DID + 1, + cap_ndoms(iommu->cap) - 1, GFP_ATOMIC);let's define a macro e.g.
FIRST_DIDfor min.
Okay, how about IDA_START_DID?
Thanks,
baolu
Quoted 2 lines
Reviewed-by Kevin Tian <[email protected]>
Baolu Lu
maintainerRe: [PATCH 1/3] iommu/vt-d: Use ida to manage domain id
In reply to Lu Baoludrivers/iommu/intel/iommu.c static int copy_context_table(struct intel_iommu *iommu, · line 2001
Quoted 6 lines · Lu Baolu
drivers/iommu/intel/iommu.c +1
-1@@ -2042,7 +1998,7 @@ static int copy_context_table(struct intel_iommu *iommu, did =context_domain_id (&ce);if (did >=0 && did <cap_ndoms (iommu->cap)) -set_bit (did, iommu->domain_ids); +ida_alloc_range (&iommu->domain_ida, did, did +1 ,GFP_KERNEL );
ida allocation range is inclusive, thus here it should be,
ida_alloc_range(&iommu->domain_ida, did, did, GFP_KERNEL);
drivers/iommu/intel/iommu.c static int copy_context_table(struct intel_iommu *iommu, · line 2004
Quoted 3 lines
drivers/iommu/intel/iommu.c +0
-0set_context_copied (iommu, bus, devfn); new_ce[idx] = ce;
Thanks,
baolu
Lu Baolu
maintainerPATCH 2/3 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
@@ -1347,17 +1347,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,FLPT_DEFAULT_DID +1 , -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;
@@ -1367,19 +1366,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; }
@@ -1391,7 +1388,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);
@@ -1399,7 +1396,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 2/3] iommu/vt-d: Replace spin_lock with mutex to protect domain ida
Quoted 12 lines
From: Lu Baolu <[email protected]>
Sent: Wednesday, April 23, 2025 11:10 AMThe 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_KERNEL
to allow blocking memory allocations if necessary.
Quoted 5 lines
Introduce a new mutex,
did_lock, specifically for protecting the domain ida. The existing spinlock will remain for protecting otherintel_iommufields.Signed-off-by Lu Baolu <[email protected]>
Lu Baolu
maintainerPATCH 3/3 iommu/vt-d: Simplify domain_attach_iommu()
In reply to Lu BaoluUse the __free(kfree) attribute with kzalloc() to automatically handle the freeing of the allocated struct iommu_domain_info on error or early exit paths, eliminating the need for explicit kfree() calls in error handling branches.
--- drivers/iommu/intel/iommu.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-)
drivers/iommu/intel/iommu.c +8 -21
@@ -1337,13 +1337,14 @@ static bool first_level_by_default(struct intel_iommu *iommu)int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu) { -struct iommu_domain_info *info, *curr; -int num, ret = -ENOSPC ; +struct iommu_domain_info *curr; +int num;if (domain->domain.type ==IOMMU_DOMAIN_SVA )return 0 ; - info =kzalloc (sizeof (*info),GFP_KERNEL ); + struct iommu_domain_info *info __free(kfree) = +kzalloc (sizeof (*info),GFP_KERNEL );if (!info)return -ENOMEM ;
@@ -1351,34 +1352,20 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) curr =xa_load (&domain->iommu_array, iommu->seq_id);if (curr) { curr->refcnt++; -kfree (info);return 0 ; } num =ida_alloc_range (&iommu->domain_ida,FLPT_DEFAULT_DID +1 ,cap_ndoms (iommu->cap) -1 ,GFP_KERNEL ); -if (num <0 ) { -pr_err ("%s: No free domain ids\n" , iommu->name); - goto err_unlock; - } +if (num <0 ) +return num; info->refcnt =1 ; info->did = num; info->iommu = iommu; - curr =xa_cmpxchg (&domain->iommu_array, iommu->seq_id, -NULL , info,GFP_KERNEL ); -if (curr) { - ret =xa_err (curr) ? : -EBUSY ; - goto err_clear; - } -return 0 ; - -err_clear: -ida_free (&iommu->domain_ida, info->did); -err_unlock: -kfree (info); -return ret; +return xa_err (xa_store (&domain->iommu_array, iommu->seq_id, +no_free_ptr (info),GFP_KERNEL )); }void domain_detach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu)
Signature · 1 line
2.43.0
RE: [PATCH 3/3] iommu/vt-d: Simplify domain_attach_iommu()
drivers/iommu/intel/iommu.c int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) · line 1361
Quoted 8 lines
From: Lu Baolu <[email protected]>
Sent: Wednesday, April 23, 2025 11:10 AMnum = ida_alloc_range(&iommu->domain_ida,
FLPT_DEFAULT_DID+
1,drivers/iommu/intel/iommu.c +0
-2cap_ndoms(iommu->cap) - 1, GFP_KERNEL); -if (num <0 ) { -pr_err ("%s: No free domain ids\n" , iommu->name);
this error message could be kept.
Quoted 15 lines
drivers/iommu/intel/iommu.c +2
-8- goto err_unlock; - } +if (num <0 ) +return num; info->refcnt =1 ; info->did = num; info->iommu = iommu; - curr =xa_cmpxchg (&domain->iommu_array, iommu->seq_id, -NULL , info,GFP_KERNEL ); -if (curr) { - ret =xa_err (curr) ? : -EBUSY ; - goto err_clear; - }
drivers/iommu/intel/iommu.c int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) · line 1369
Quoted 10 lines
drivers/iommu/intel/iommu.c +2
-7-return 0 ; - -err_clear: -ida_free (&iommu->domain_ida, info->did); -err_unlock: -kfree (info); -return ret; +return xa_err (xa_store (&domain->iommu_array, iommu->seq_id, +no_free_ptr (info),GFP_KERNEL )); }
no_free_ptr() should be used before successful return. Here xa_store() could return error but at that point no auto free as no_free_ptr() already changes 'info' to NULL. then memory leak.
Baolu Lu
maintainerRe: [PATCH 3/3] iommu/vt-d: Simplify domain_attach_iommu()
Quoted 10 lines · Kevin
drivers/iommu/intel/iommu.c int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) · line 1361
Quoted 8 lines
From: Lu Baolu <[email protected]>
Sent: Wednesday, April 23, 2025 11:10 AMnum = ida_alloc_range(&iommu->domain_ida,
FLPT_DEFAULT_DID+
1,
cap_ndoms(iommu->cap)- 1,GFP_KERNEL);drivers/iommu/intel/iommu.c +0
-2-if (num <0 ) { -pr_err ("%s: No free domain ids\n" , iommu->name);this error message could be kept.
Okay.
Quoted 16 lines
Quoted 16 lines
drivers/iommu/intel/iommu.c +2
-8- goto err_unlock; - } +if (num <0 ) +return num; info->refcnt =1 ; info->did = num; info->iommu = iommu; - curr =xa_cmpxchg (&domain->iommu_array, iommu->seq_id, -NULL , info,GFP_KERNEL ); -if (curr) { - ret =xa_err (curr) ? : -EBUSY ; - goto err_clear; - }
Quoted 14 lines
drivers/iommu/intel/iommu.c int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) · line 1369
Quoted 10 lines
drivers/iommu/intel/iommu.c +2
-7-return 0 ; - -err_clear: -ida_free (&iommu->domain_ida, info->did); -err_unlock: -kfree (info); -return ret; +return xa_err (xa_store (&domain->iommu_array, iommu->seq_id, +no_free_ptr (info),GFP_KERNEL )); }
no_free_ptr()should be used before successful return. Herexa_store()could return error but at that point no auto free asno_free_ptr()already changes 'info' to NULL. then memory leak.
Hmm, I've considered this. My thought was that xa_store() failure only occurs due to the system running out of memory, and the Linux kernel can't recover from it. In that case, the system is already broken; hence, handling the failure case here doesn't make things better.
Thanks,
baolu
Re: [PATCH 3/3] iommu/vt-d: Simplify domain_attach_iommu()
Quoted 13 lines · Baolu Lu
Quoted 4 lines
Quoted 4 lines
drivers/iommu/intel/iommu.c +0
-4-err_clear: -ida_free (&iommu->domain_ida, info->did); -err_unlock: -kfree (info);Quoted 8 lines
drivers/iommu/intel/iommu.c int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) · line 1369
Quoted 4 lines
drivers/iommu/intel/iommu.c +2
-1-return ret; +return xa_err (xa_store (&domain->iommu_array, iommu->seq_id, +no_free_ptr (info),GFP_KERNEL )); }
no_free_ptr()should be used before successful return. Herexa_store()could return error but at that point no auto free asno_free_ptr()already changes 'info' to NULL. then memory leak.
Quoted 4 lines
Hmm, I've considered this. My thought was that
xa_store()failure only occurs due to the system running out of memory, and the Linux kernel can't recover from it. In that case, the system is already broken; hence, handling the failure case here doesn't make things better.
That's not the kernel pattern, you are supposed to unwind correctly in those failures
I think you should not use cleanup.h for something complicated like this..
Jason
Baolu Lu
maintainerRe: [PATCH 3/3] iommu/vt-d: Simplify domain_attach_iommu()
Quoted 17 lines · Jason Gunthorpe
Quoted 11 lines · Baolu Lu
Quoted 11 lines
drivers/iommu/intel/iommu.c int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) · line 1369
Quoted 8 lines
drivers/iommu/intel/iommu.c +2
-5-err_clear: -ida_free (&iommu->domain_ida, info->did); -err_unlock: -kfree (info); -return ret; +return xa_err (xa_store (&domain->iommu_array, iommu->seq_id, +no_free_ptr (info),GFP_KERNEL )); }
no_free_ptr()should be used before successful return. Herexa_store()could return error but at that point no auto free asno_free_ptr()already changes 'info' to NULL. then memory leak.Quoted 4 lines
Hmm, I've considered this. My thought was that
xa_store()failure only occurs due to the system running out of memory, and the Linux kernel can't recover from it. In that case, the system is already broken; hence, handling the failure case here doesn't make things better.
Quoted 5 lines
That's not the kernel pattern, you are supposed to unwind correctly in those failures
I think you should not use
cleanup.hfor something complicated like this..
Okay, so let me drop this patch.
Thanks,
baolu
Re: [PATCH 3/3] iommu/vt-d: Simplify domain_attach_iommu()
In reply to Lu BaoluQuoted 33 lines · Lu Baolu
Use the
__free(kfree)attribute withkzalloc()to automatically handle the freeing of the allocated structiommu_domain_infoon error or early exit paths, eliminating the need for explicitkfree()calls in error handling branches.Signed-off-by Lu Baolu <[email protected]>--- drivers/iommu/intel/iommu.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-)drivers/iommu/intel/iommu.c +4
-3@@ -1337,13 +1337,14 @@ static bool first_level_by_default(struct intel_iommu *iommu)int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu) { -struct iommu_domain_info *info, *curr; -int num, ret = -ENOSPC ; +struct iommu_domain_info *curr; +int num;if (domain->domain.type ==IOMMU_DOMAIN_SVA )return 0 ; - info =kzalloc (sizeof (*info),GFP_KERNEL ); + struct iommu_domain_info *info __free(kfree) = +kzalloc (sizeof (*info),GFP_KERNEL );if (!info)return -ENOMEM ;@@ -1351,34 +1352,20 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)
[..]
drivers/iommu/intel/iommu.c int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) · line 1369
Quoted 8 lines
drivers/iommu/intel/iommu.c +2
-5-err_clear: -ida_free (&iommu->domain_ida, info->did); -err_unlock: -kfree (info); -return ret; +return xa_err (xa_store (&domain->iommu_array, iommu->seq_id, +no_free_ptr (info),GFP_KERNEL )); }
This pattern looks like it wants a "xa_store_or_{reset,kfree}()" helper that handles both canceling a scope based cleanup and taking responsibility for error-exit-freeing @info in one statement.
I.e. this is similar to a:
"return devm_add_action_or_reset(..., no_free_ptr(obj), ...)"
...pattern.
Baolu Lu
maintainerRe: [PATCH 3/3] iommu/vt-d: Simplify domain_attach_iommu()
Quoted 48 lines · Dan Williams
Quoted 33 lines · Lu Baolu
Use the
__free(kfree)attribute withkzalloc()to automatically handle the freeing of the allocated structiommu_domain_infoon error or early exit paths, eliminating the need for explicitkfree()calls in error handling branches.Signed-off-by Lu Baolu <[email protected]>--- drivers/iommu/intel/iommu.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-)drivers/iommu/intel/iommu.c +4
-3@@ -1337,13 +1337,14 @@ static bool first_level_by_default(struct intel_iommu *iommu)int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu) { -struct iommu_domain_info *info, *curr; -int num, ret = -ENOSPC ; +struct iommu_domain_info *curr; +int num;if (domain->domain.type ==IOMMU_DOMAIN_SVA )return 0 ; - info =kzalloc (sizeof (*info),GFP_KERNEL ); + struct iommu_domain_info *info __free(kfree) = +kzalloc (sizeof (*info),GFP_KERNEL );if (!info)return -ENOMEM ;@@ -1351,34 +1352,20 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)[..]
drivers/iommu/intel/iommu.c
Quoted 8 lines
drivers/iommu/intel/iommu.c +2
-5-err_clear: -ida_free (&iommu->domain_ida, info->did); -err_unlock: -kfree (info); -return ret; +return xa_err (xa_store (&domain->iommu_array, iommu->seq_id, +no_free_ptr (info),GFP_KERNEL )); }This pattern looks like it wants a "xa_store_or_{reset,kfree}()" helper that handles both canceling a scope based cleanup and taking responsibility for error-exit-freeing @info in one statement.
Quoted 6 lines
I.e. this is similar to a:
"return devm_add_action_or_reset(..., no_free_ptr(obj), ...)"...pattern.
Yes. Perhaps adding a xa_store variant would be beneficial in all places that require this pattern.
Something like this?
include/linux/xarray.h +29 -0
@@ -626,6 +626,35 @@ static inline void *xa_store_irq(struct xarray *xa, unsigned long index,return curr; } +/** +* xa_store_or_kfree() - Store this entry in the XArray. +* @xa: XArray. +* @index: Index into array. +* @entry: New entry. +* @gfp: Memory allocation flags. +* +* This function is like calling xa_store() except it kfrees the new +* entry if an error happened. +* +* Context: Process context. Any context. Takes and releases the xa_lock. +* May sleep if the @gfp flags permit. +* Return: The old entry at this index or xa_err() if an error happened. +*/ +static inline void *xa_store_or_kfree (struct xarray *xa,unsigned long index, +void *entry,gfp_t gfp) +{ +void *curr; + +xa_lock (xa); + curr =__xa_store (xa, index, entry, gfp); +xa_unlock (xa); + +if (xa_err (curr)) +kfree (entry); + +return curr; +} +/** * xa_erase_bh() - Erase this entry from the XArray. * @xa: XArray.
Thanks,
baolu