Skip to content

Commit d51bf43

Browse files
ankitvvsonigregkh
authored andcommitted
iommu/amd: serialize sequence allocation under concurrent TLB invalidations
commit 9e249c4 upstream. With concurrent TLB invalidations, completion wait randomly gets timed out because cmd_sem_val was incremented outside the IOMMU spinlock, allowing CMD_COMPL_WAIT commands to be queued out of sequence and breaking the ordering assumption in wait_on_sem(). Move the cmd_sem_val increment under iommu->lock so completion sequence allocation is serialized with command queuing. And remove the unnecessary return. Fixes: d2a0cac ("iommu/amd: move wait_on_sem() out of spinlock") Tested-by: Srikanth Aithal <sraithal@amd.com> Reported-by: Srikanth Aithal <sraithal@amd.com> Signed-off-by: Ankit Soni <Ankit.Soni@amd.com> Reviewed-by: Vasant Hegde <vasant.hegde@amd.com> Signed-off-by: Joerg Roedel <joerg.roedel@amd.com> [Salvatore Bonaccorso: Backport to v6.12.y where f32fe7c ("iommu/amd: Add support to remap/unmap IOMMU buffers for kdump") is not present] Signed-off-by: Salvatore Bonaccorso <carnil@debian.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent c28c87d commit d51bf43

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

drivers/iommu/amd/amd_iommu_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ struct amd_iommu {
765765

766766
u32 flags;
767767
volatile u64 *cmd_sem;
768-
atomic64_t cmd_sem_val;
768+
u64 cmd_sem_val;
769769

770770
#ifdef CONFIG_AMD_IOMMU_DEBUGFS
771771
/* DebugFS Info */

drivers/iommu/amd/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h,
18051805
iommu->pci_seg = pci_seg;
18061806

18071807
raw_spin_lock_init(&iommu->lock);
1808-
atomic64_set(&iommu->cmd_sem_val, 0);
1808+
iommu->cmd_sem_val = 0;
18091809

18101810
/* Add IOMMU to internal data structures */
18111811
list_add_tail(&iommu->list, &amd_iommu_list);

drivers/iommu/amd/iommu.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,12 @@ static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
11951195
return iommu_queue_command_sync(iommu, cmd, true);
11961196
}
11971197

1198+
static u64 get_cmdsem_val(struct amd_iommu *iommu)
1199+
{
1200+
lockdep_assert_held(&iommu->lock);
1201+
return ++iommu->cmd_sem_val;
1202+
}
1203+
11981204
/*
11991205
* This function queues a completion wait command into the command
12001206
* buffer of an IOMMU
@@ -1209,11 +1215,11 @@ static int iommu_completion_wait(struct amd_iommu *iommu)
12091215
if (!iommu->need_sync)
12101216
return 0;
12111217

1212-
data = atomic64_inc_return(&iommu->cmd_sem_val);
1213-
build_completion_wait(&cmd, iommu, data);
1214-
12151218
raw_spin_lock_irqsave(&iommu->lock, flags);
12161219

1220+
data = get_cmdsem_val(iommu);
1221+
build_completion_wait(&cmd, iommu, data);
1222+
12171223
ret = __iommu_queue_command_sync(iommu, &cmd, false);
12181224
raw_spin_unlock_irqrestore(&iommu->lock, flags);
12191225

@@ -2877,10 +2883,11 @@ static void iommu_flush_irt_and_complete(struct amd_iommu *iommu, u16 devid)
28772883
return;
28782884

28792885
build_inv_irt(&cmd, devid);
2880-
data = atomic64_inc_return(&iommu->cmd_sem_val);
2881-
build_completion_wait(&cmd2, iommu, data);
28822886

28832887
raw_spin_lock_irqsave(&iommu->lock, flags);
2888+
data = get_cmdsem_val(iommu);
2889+
build_completion_wait(&cmd2, iommu, data);
2890+
28842891
ret = __iommu_queue_command_sync(iommu, &cmd, true);
28852892
if (ret)
28862893
goto out_err;
@@ -2894,7 +2901,6 @@ static void iommu_flush_irt_and_complete(struct amd_iommu *iommu, u16 devid)
28942901

28952902
out_err:
28962903
raw_spin_unlock_irqrestore(&iommu->lock, flags);
2897-
return;
28982904
}
28992905

29002906
static void set_dte_irq_entry(struct amd_iommu *iommu, u16 devid,

0 commit comments

Comments
 (0)