Skip to content

Commit 868720f

Browse files
LuBaolujoergroedel
authored andcommitted
iommu/vt-d: Replace spin_lock with mutex to protect domain ida
The 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. Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Link: https://lore.kernel.org/r/20250430021135.2370244-3-baolu.lu@linux.intel.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent f93b4ac commit 868720f

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

drivers/iommu/intel/dmar.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ static int alloc_iommu(struct dmar_drhd_unit *drhd)
11011101
iommu->node = NUMA_NO_NODE;
11021102
spin_lock_init(&iommu->lock);
11031103
ida_init(&iommu->domain_ida);
1104+
mutex_init(&iommu->did_lock);
11041105

11051106
ver = readl(iommu->reg + DMAR_VER_REG);
11061107
pr_info("%s: reg_base_addr %llx ver %d:%d cap %llx ecap %llx\n",

drivers/iommu/intel/iommu.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,17 +1345,16 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)
13451345
if (!info)
13461346
return -ENOMEM;
13471347

1348-
spin_lock(&iommu->lock);
1348+
guard(mutex)(&iommu->did_lock);
13491349
curr = xa_load(&domain->iommu_array, iommu->seq_id);
13501350
if (curr) {
13511351
curr->refcnt++;
1352-
spin_unlock(&iommu->lock);
13531352
kfree(info);
13541353
return 0;
13551354
}
13561355

13571356
num = ida_alloc_range(&iommu->domain_ida, IDA_START_DID,
1358-
cap_ndoms(iommu->cap) - 1, GFP_ATOMIC);
1357+
cap_ndoms(iommu->cap) - 1, GFP_KERNEL);
13591358
if (num < 0) {
13601359
pr_err("%s: No free domain ids\n", iommu->name);
13611360
goto err_unlock;
@@ -1365,19 +1364,17 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)
13651364
info->did = num;
13661365
info->iommu = iommu;
13671366
curr = xa_cmpxchg(&domain->iommu_array, iommu->seq_id,
1368-
NULL, info, GFP_ATOMIC);
1367+
NULL, info, GFP_KERNEL);
13691368
if (curr) {
13701369
ret = xa_err(curr) ? : -EBUSY;
13711370
goto err_clear;
13721371
}
13731372

1374-
spin_unlock(&iommu->lock);
13751373
return 0;
13761374

13771375
err_clear:
13781376
ida_free(&iommu->domain_ida, info->did);
13791377
err_unlock:
1380-
spin_unlock(&iommu->lock);
13811378
kfree(info);
13821379
return ret;
13831380
}
@@ -1389,15 +1386,14 @@ void domain_detach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)
13891386
if (domain->domain.type == IOMMU_DOMAIN_SVA)
13901387
return;
13911388

1392-
spin_lock(&iommu->lock);
1389+
guard(mutex)(&iommu->did_lock);
13931390
info = xa_load(&domain->iommu_array, iommu->seq_id);
13941391
if (--info->refcnt == 0) {
13951392
ida_free(&iommu->domain_ida, info->did);
13961393
xa_erase(&domain->iommu_array, iommu->seq_id);
13971394
domain->nid = NUMA_NO_NODE;
13981395
kfree(info);
13991396
}
1400-
spin_unlock(&iommu->lock);
14011397
}
14021398

14031399
static void domain_exit(struct dmar_domain *domain)

drivers/iommu/intel/iommu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,8 @@ struct intel_iommu {
722722
unsigned char name[16]; /* Device Name */
723723

724724
#ifdef CONFIG_INTEL_IOMMU
725+
/* mutex to protect domain_ida */
726+
struct mutex did_lock;
725727
struct ida domain_ida; /* domain id allocator */
726728
unsigned long *copied_tables; /* bitmap of copied tables */
727729
spinlock_t lock; /* protect context, domain ids */

0 commit comments

Comments
 (0)