Skip to content

Commit 93494bd

Browse files
Guanghui Fenggregkh
authored andcommitted
iommu/amd: Wait for completion instead of returning early in iommu_completion_wait()
[ Upstream commit 1e75a82 ] need_sync is a per-IOMMU flag shared by all domains and devices behind that IOMMU. It is set whenever a command is queued with sync == true and cleared when a completion-wait (CWAIT) command is queued. However, a cleared need_sync only means that a covering CWAIT has been queued, not that all previously queued commands have actually completed in hardware. iommu_completion_wait() read need_sync locklessly and returned early when it was false. This breaks the "block until all previously queued commands have completed" contract in a multi-CPU scenario: CPU2: queue inv-B => need_sync = true CPU1: queue CWAIT(N); need_sync = false; then wait_on_sem(N) CPU2: read need_sync == false => return 0 (no wait!) CPU2 returns without waiting for any sequence number even though its inv-B may not have completed yet (CWAIT(N), queued after inv-B, has not been signaled). CPU2 then proceeds to, for example, free page-table pages while the IOMMU can still walk stale translations, opening a use-after-free window. This is a logical race in the meaning of the flag, not a memory-visibility issue, so barriers alone do not help. Fix it without losing the optimization of avoiding redundant CWAIT commands: take iommu->lock before testing need_sync, and when it is false do not return early but wait for the last allocated sequence number (cmd_sem_val). Since need_sync == false implies no sync command was queued after the last CWAIT, that CWAIT is FIFO-ordered after every not-yet-completed command, so waiting for its sequence number guarantees all prior commands (possibly queued by another CPU) have completed. The common path with pending work is unchanged and no extra hardware command is issued. Signed-off-by: Guanghui Feng <guanghuifeng@linux.alibaba.com> Fixes: 815b33f ("x86/amd-iommu: Cleanup completion-wait handling") Reviewed-by: Vasant Hegde <vasant.hegde@amd.com> Signed-off-by: Will Deacon <will@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent e23e4a3 commit 93494bd

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

drivers/iommu/amd/iommu.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,11 +1424,23 @@ static int iommu_completion_wait(struct amd_iommu *iommu)
14241424
int ret;
14251425
u64 data;
14261426

1427-
if (!iommu->need_sync)
1428-
return 0;
1429-
14301427
raw_spin_lock_irqsave(&iommu->lock, flags);
14311428

1429+
if (!iommu->need_sync) {
1430+
/*
1431+
* No command has been queued since the last completion-wait.
1432+
* A concurrent CPU may have already queued that CWAIT and
1433+
* cleared need_sync; need_sync == false only means a covering
1434+
* CWAIT is queued, not that all prior commands have completed.
1435+
* Wait for the last allocated sequence number so that any
1436+
* command queued before this call (possibly on another CPU)
1437+
* is guaranteed to have completed before returning.
1438+
*/
1439+
data = iommu->cmd_sem_val;
1440+
raw_spin_unlock_irqrestore(&iommu->lock, flags);
1441+
return wait_on_sem(iommu, data);
1442+
}
1443+
14321444
data = get_cmdsem_val(iommu);
14331445
build_completion_wait(&cmd, iommu, data);
14341446

@@ -1438,9 +1450,7 @@ static int iommu_completion_wait(struct amd_iommu *iommu)
14381450
if (ret)
14391451
return ret;
14401452

1441-
ret = wait_on_sem(iommu, data);
1442-
1443-
return ret;
1453+
return wait_on_sem(iommu, data);
14441454
}
14451455

14461456
static void domain_flush_complete(struct protection_domain *domain)

0 commit comments

Comments
 (0)