History

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.

Joerg Roedel committed 7 years ago

Commit 754265b1 parent 36b72001 file · +11 −5Browse files

Unified · Split

drivers/iommu/amd_iommu.c+11−5
@@ -1435,18 +1435,21 @@ static void free_pagetable(struct protection_domain *domain) · 1434 unmodified lines
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));
@@ -1454,7 +1457,10 @@ static bool increase_address_space(struct protection_domain *domain, · 1 unmodified line
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,