Skip to content

Commit cdb8a3c

Browse files
committed
iommu/arm-smmu-v3: Avoid locking on invalidation path when not using ATS
When ATS is not in use, we can avoid taking the 'devices_lock' for the domain on the invalidation path by simply caching the number of ATS masters currently attached. The fiddly part is handling a concurrent ->attach() of an ATS-enabled master to a domain that is being invalidated, but we can handle this using an 'smp_mb()' to ensure that our check of the count is ordered after completion of our prior TLB invalidation. This also makes our ->attach() and ->detach() flows symmetric wrt ATS interactions. Acked-by: Robin Murphy <[email protected]> Signed-off-by: Will Deacon <[email protected]>
1 parent 353e3cf commit cdb8a3c

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

drivers/iommu/arm-smmu-v3.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ struct arm_smmu_domain {
654654

655655
struct io_pgtable_ops *pgtbl_ops;
656656
bool non_strict;
657+
atomic_t nr_ats_masters;
657658

658659
enum arm_smmu_domain_stage stage;
659660
union {
@@ -1926,6 +1927,23 @@ static int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain,
19261927
if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_ATS))
19271928
return 0;
19281929

1930+
/*
1931+
* Ensure that we've completed prior invalidation of the main TLBs
1932+
* before we read 'nr_ats_masters' in case of a concurrent call to
1933+
* arm_smmu_enable_ats():
1934+
*
1935+
* // unmap() // arm_smmu_enable_ats()
1936+
* TLBI+SYNC atomic_inc(&nr_ats_masters);
1937+
* smp_mb(); [...]
1938+
* atomic_read(&nr_ats_masters); pci_enable_ats() // writel()
1939+
*
1940+
* Ensures that we always see the incremented 'nr_ats_masters' count if
1941+
* ATS was enabled at the PCI device before completion of the TLBI.
1942+
*/
1943+
smp_mb();
1944+
if (!atomic_read(&smmu_domain->nr_ats_masters))
1945+
return 0;
1946+
19291947
arm_smmu_atc_inv_to_cmd(ssid, iova, size, &cmd);
19301948

19311949
spin_lock_irqsave(&smmu_domain->devices_lock, flags);
@@ -2312,6 +2330,7 @@ static void arm_smmu_enable_ats(struct arm_smmu_master *master)
23122330
size_t stu;
23132331
struct pci_dev *pdev;
23142332
struct arm_smmu_device *smmu = master->smmu;
2333+
struct arm_smmu_domain *smmu_domain = master->domain;
23152334

23162335
/* Don't enable ATS at the endpoint if it's not enabled in the STE */
23172336
if (!master->ats_enabled)
@@ -2320,13 +2339,17 @@ static void arm_smmu_enable_ats(struct arm_smmu_master *master)
23202339
/* Smallest Translation Unit: log2 of the smallest supported granule */
23212340
stu = __ffs(smmu->pgsize_bitmap);
23222341
pdev = to_pci_dev(master->dev);
2342+
2343+
atomic_inc(&smmu_domain->nr_ats_masters);
2344+
arm_smmu_atc_inv_domain(smmu_domain, 0, 0, 0);
23232345
if (pci_enable_ats(pdev, stu))
23242346
dev_err(master->dev, "Failed to enable ATS (STU %zu)\n", stu);
23252347
}
23262348

23272349
static void arm_smmu_disable_ats(struct arm_smmu_master *master)
23282350
{
23292351
struct arm_smmu_cmdq_ent cmd;
2352+
struct arm_smmu_domain *smmu_domain = master->domain;
23302353

23312354
if (!master->ats_enabled)
23322355
return;
@@ -2339,6 +2362,7 @@ static void arm_smmu_disable_ats(struct arm_smmu_master *master)
23392362
wmb();
23402363
arm_smmu_atc_inv_to_cmd(0, 0, 0, &cmd);
23412364
arm_smmu_atc_inv_master(master, &cmd);
2365+
atomic_dec(&smmu_domain->nr_ats_masters);
23422366
}
23432367

23442368
static void arm_smmu_detach_dev(struct arm_smmu_master *master)
@@ -2349,11 +2373,12 @@ static void arm_smmu_detach_dev(struct arm_smmu_master *master)
23492373
if (!smmu_domain)
23502374
return;
23512375

2376+
arm_smmu_disable_ats(master);
2377+
23522378
spin_lock_irqsave(&smmu_domain->devices_lock, flags);
23532379
list_del(&master->domain_head);
23542380
spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
23552381

2356-
arm_smmu_disable_ats(master);
23572382
master->domain = NULL;
23582383
master->ats_enabled = false;
23592384
arm_smmu_install_ste_for_dev(master);
@@ -2396,18 +2421,20 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
23962421

23972422
master->domain = smmu_domain;
23982423

2399-
spin_lock_irqsave(&smmu_domain->devices_lock, flags);
2400-
list_add(&master->domain_head, &smmu_domain->devices);
2401-
spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
2402-
24032424
if (smmu_domain->stage != ARM_SMMU_DOMAIN_BYPASS)
24042425
master->ats_enabled = arm_smmu_ats_supported(master);
24052426

24062427
if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1)
24072428
arm_smmu_write_ctx_desc(smmu, &smmu_domain->s1_cfg);
24082429

24092430
arm_smmu_install_ste_for_dev(master);
2431+
2432+
spin_lock_irqsave(&smmu_domain->devices_lock, flags);
2433+
list_add(&master->domain_head, &smmu_domain->devices);
2434+
spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
2435+
24102436
arm_smmu_enable_ats(master);
2437+
24112438
out_unlock:
24122439
mutex_unlock(&smmu_domain->init_mutex);
24132440
return ret;

0 commit comments

Comments
 (0)