Skip to content

Commit 754265b

Browse files
committed
iommu/amd: Fix race in increase_address_space()
After the conversion to lock-less dma-api call the increase_address_space() function can be called without any locking. Multiple CPUs could potentially race for increasing the address space, leading to invalid domain->mode settings and invalid page-tables. This has been happening in the wild under high IO load and memory pressure. Fix the race by locking this operation. The function is called infrequently so that this does not introduce a performance regression in the dma-api path again. Reported-by: Qian Cai <[email protected]> Fixes: 256e462 ('iommu/amd: Make use of the generic IOVA allocator') Signed-off-by: Joerg Roedel <[email protected]>
1 parent 36b7200 commit 754265b

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

drivers/iommu/amd_iommu.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,26 +1435,32 @@ static void free_pagetable(struct protection_domain *domain)
14351435
* another level increases the size of the address space by 9 bits to a size up
14361436
* to 64 bits.
14371437
*/
1438-
static bool increase_address_space(struct protection_domain *domain,
1438+
static void increase_address_space(struct protection_domain *domain,
14391439
gfp_t gfp)
14401440
{
1441+
unsigned long flags;
14411442
u64 *pte;
14421443

1443-
if (domain->mode == PAGE_MODE_6_LEVEL)
1444+
spin_lock_irqsave(&domain->lock, flags);
1445+
1446+
if (WARN_ON_ONCE(domain->mode == PAGE_MODE_6_LEVEL))
14441447
/* address space already 64 bit large */
1445-
return false;
1448+
goto out;
14461449

14471450
pte = (void *)get_zeroed_page(gfp);
14481451
if (!pte)
1449-
return false;
1452+
goto out;
14501453

14511454
*pte = PM_LEVEL_PDE(domain->mode,
14521455
iommu_virt_to_phys(domain->pt_root));
14531456
domain->pt_root = pte;
14541457
domain->mode += 1;
14551458
domain->updated = true;
14561459

1457-
return true;
1460+
out:
1461+
spin_unlock_irqrestore(&domain->lock, flags);
1462+
1463+
return;
14581464
}
14591465

14601466
static u64 *alloc_pte(struct protection_domain *domain,

0 commit comments

Comments
 (0)