Skip to contentSkip to conversation

iommu/amd: Don't put completion-wait semaphore on stack

PATCH Superseded Joerg Roedel 3 participants 7 messages Sep 14, 2016 – Sep 15, 2016 6 messages not in the archive IOMMU

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/amd: Don't put completion-wait semaphore on stack

From: Joerg Roedel <[email protected]>

The semaphore used by the AMD IOMMU to signal command completion lived on the stack until now, which was safe as the driver busy-waited on the semaphore with IRQs disabled, so the stack can't go away under the driver.

But the recently introduced vmap-based stacks break this as the physical address of the semaphore can't be determinded easily anymore. The driver used the __pa() macro, but that only works in the direct-mapping. The result were Completion-Wait timeout errors seen by the IOMMU driver, breaking system boot.

Since putting the semaphore on the stack is bad design anyway, move the semaphore into 'struct amd_iommu'. It is protected by the per-iommu lock and now in the direct mapping again. This fixes the Completion-Wait timeout errors and makes AMD IOMMU systems boot again with vmap-based stacks enabled.

---
 drivers/iommu/amd_iommu.c       | 14 ++++++++------
 drivers/iommu/amd_iommu_types.h |  2 ++
 2 files changed, 10 insertions(+), 6 deletions(-)
drivers/iommu/amd_iommu.c +8 -6
index 96de97a..1307e73 100644
@@ -957,15 +957,16 @@ again:
 
 	if (left <= 2) {
 		struct iommu_cmd sync_cmd;
-		volatile u64 sem = 0;
 		int ret;
 
-		build_completion_wait(&sync_cmd, (u64)&sem);
+		iommu->cmd_sem = 0;
+
+		build_completion_wait(&sync_cmd, (u64)&iommu->cmd_sem);
 		copy_cmd_to_buffer(iommu, &sync_cmd, tail);
 
 		spin_unlock_irqrestore(&iommu->lock, flags);
 
-		if ((ret = wait_on_sem(&sem)) != 0)
+		if ((ret = wait_on_sem(&iommu->cmd_sem)) != 0)
 			return ret;
 
 		goto again;
@@ -993,19 +994,20 @@ static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
 static int iommu_completion_wait(struct amd_iommu *iommu)
 {
 	struct iommu_cmd cmd;
-	volatile u64 sem = 0;
 	int ret;
 
 	if (!iommu->need_sync)
 		return 0;
 
-	build_completion_wait(&cmd, (u64)&sem);
+	iommu->cmd_sem = 0;
+
+	build_completion_wait(&cmd, (u64)&iommu->cmd_sem);
 
 	ret = iommu_queue_command_sync(iommu, &cmd, false);
 	if (ret)
 		return ret;
 
-	return wait_on_sem(&sem);
+	return wait_on_sem(&iommu->cmd_sem);
 }
 
 static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
drivers/iommu/amd_iommu_types.h +2 -0
index caf5e38..9652848 100644
@@ -524,6 +524,8 @@ struct amd_iommu {
 	struct irq_domain *ir_domain;
 	struct irq_domain *msi_domain;
 #endif
+
+	volatile u64 __aligned(8) cmd_sem;
 };
 
 #define ACPIHID_UID_LEN 256
Signature · 1 line
2.6.2

A message that is not in the archive (sent off-list or not yet received): 1473846119-9527-1-git-send-email-joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org

#2 lore raw
Quoted 21 lines · Joerg Roedel

From: Joerg Roedel <[email protected]>

The semaphore used by the AMD IOMMU to signal command completion lived on the stack until now, which was safe as the driver busy-waited on the semaphore with IRQs disabled, so the stack can't go away under the driver.

But the recently introduced vmap-based stacks break this as the physical address of the semaphore can't be determinded easily anymore. The driver used the __pa() macro, but that only works in the direct-mapping. The result were Completion-Wait timeout errors seen by the IOMMU driver, breaking system boot.

Since putting the semaphore on the stack is bad design anyway, move the semaphore into 'struct amd_iommu'. It is protected by the per-iommu lock and now in the direct mapping again. This fixes the Completion-Wait timeout errors and makes AMD IOMMU systems boot again with vmap-based stacks enabled.

Quoted 6 lines
---
 drivers/iommu/amd_iommu.c       | 14 ++++++++------
 drivers/iommu/amd_iommu_types.h |  2 ++
 2 files changed, 10 insertions(+), 6 deletions(-)

Cool, thanks! I'll put this into tip:x86/asm which has the virtually mapped stack patches - ok?

Thanks,

Ingo

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Hi Ingo,

Quoted 3 lines · Ingo Molnar

Cool, thanks! I'll put this into tip:x86/asm which has the virtually mapped stack patches - ok?

Yeah, sure, that is the best thing to do. Just wait for the v2 I'll sending tomorrow. I just realised that the locking is not correct in one of the cases with this patch and I'd like to fix that first.

Thanks,

Joerg

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw
Quoted 8 lines · Joerg Roedel
Quoted 3 lines · Ingo Molnar

Cool, thanks! I'll put this into tip:x86/asm which has the virtually mapped stack patches - ok?

Yeah, sure, that is the best thing to do. Just wait for the v2 I'll sending tomorrow. I just realised that the locking is not correct in one of the cases with this patch and I'd like to fix that first.

Oh sorry, just saw the tip-bot mail. Let me know whether you can still remove it and just take v2 or if you want a follow-on patch.

Thanks,

Joerg

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw
Quoted 6 lines · Joerg Roedel
Quoted 5 lines · Joerg Roedel
Quoted 3 lines · Ingo Molnar

Cool, thanks! I'll put this into tip:x86/asm which has the virtually mapped stack patches - ok?

Quoted 6 lines
Quoted 3 lines

Yeah, sure, that is the best thing to do. Just wait for the v2 I'll sending tomorrow. I just realised that the locking is not correct in one of the cases with this patch and I'd like to fix that first.

Oh sorry, just saw the tip-bot mail. Let me know whether you can still remove it and just take v2 or if you want a follow-on patch.

Yeah, I can still remove it - just zapped it in fact.

Thanks,

Ingo

A message that is not in the archive (sent off-list or not yet received): [email protected]

#10 lore raw

PATCH v2 1/1 iommu/amd: Don't put completion-wait semaphore on stack

Hi Ingo,

Quoted 1 line · Ingo Molnar

Yeah, I can still remove it - just zapped it in fact.

Thanks, and sorry for the hassle. Here is the v2 patch that has the correct locking. I tested it with and without lockdep enabled and also under some load. Looks all fine.

So here is the patch:

Quoted 1 line

From 9becedb853c682c3fc3dd5574b933df216d91acd Mon Sep 17 00:00:00 2001

From: Joerg Roedel <[email protected]>
Date: Wed, 14 Sep 2016 11:41:59 +0200
Subject: [PATCH] iommu/amd: Don't put completion-wait semaphore on stack

The semaphore used by the AMD IOMMU to signal command completion lived on the stack until now, which was safe as the driver busy-waited on the semaphore with IRQs disabled, so the stack can't go away under the driver.

But the recently introduced vmap-based stacks break this as the physical address of the semaphore can't be determinded easily anymore. The driver used the __pa() macro, but that only works in the direct-mapping. The result were Completion-Wait timeout errors seen by the IOMMU driver, breaking system boot.

Since putting the semaphore on the stack is bad design anyway, move the semaphore into 'struct amd_iommu'. It is protected by the per-iommu lock and now in the direct mapping again. This fixes the Completion-Wait timeout errors and makes AMD IOMMU systems boot again with vmap-based stacks enabled.

---
 drivers/iommu/amd_iommu.c       | 51 ++++++++++++++++++++++++++++-------------
 drivers/iommu/amd_iommu_types.h |  2 ++
 2 files changed, 37 insertions(+), 16 deletions(-)
drivers/iommu/amd_iommu.c +35 -16
index 96de97a..4025291 100644
@@ -940,15 +940,13 @@ static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)
  * Writes the command to the IOMMUs command buffer and informs the
  * hardware about the new command.
  */
-static int iommu_queue_command_sync(struct amd_iommu *iommu,
-				    struct iommu_cmd *cmd,
-				    bool sync)
+static int __iommu_queue_command_sync(struct amd_iommu *iommu,
+				      struct iommu_cmd *cmd,
+				      bool sync)
 {
 	u32 left, tail, head, next_tail;
-	unsigned long flags;
 
 again:
-	spin_lock_irqsave(&iommu->lock, flags);
 
 	head      = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
 	tail      = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
@@ -957,15 +955,14 @@ again:
 
 	if (left <= 2) {
 		struct iommu_cmd sync_cmd;
-		volatile u64 sem = 0;
 		int ret;
 
-		build_completion_wait(&sync_cmd, (u64)&sem);
-		copy_cmd_to_buffer(iommu, &sync_cmd, tail);
+		iommu->cmd_sem = 0;
 
-		spin_unlock_irqrestore(&iommu->lock, flags);
+		build_completion_wait(&sync_cmd, (u64)&iommu->cmd_sem);
+		copy_cmd_to_buffer(iommu, &sync_cmd, tail);
 
-		if ((ret = wait_on_sem(&sem)) != 0)
+		if ((ret = wait_on_sem(&iommu->cmd_sem)) != 0)
 			return ret;
 
 		goto again;
@@ -976,9 +973,21 @@ again:
 	/* We need to sync now to make sure all commands are processed */
 	iommu->need_sync = sync;
 
+	return 0;
+}
+
+static int iommu_queue_command_sync(struct amd_iommu *iommu,
+				    struct iommu_cmd *cmd,
+				    bool sync)
+{
+	unsigned long flags;
+	int ret;
+
+	spin_lock_irqsave(&iommu->lock, flags);
+	ret = __iommu_queue_command_sync(iommu, cmd, sync);
 	spin_unlock_irqrestore(&iommu->lock, flags);
 
-	return 0;
+	return ret;
 }
 
 static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
@@ -993,19 +1002,29 @@ static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)
 static int iommu_completion_wait(struct amd_iommu *iommu)
 {
 	struct iommu_cmd cmd;
-	volatile u64 sem = 0;
+	unsigned long flags;
 	int ret;
 
 	if (!iommu->need_sync)
 		return 0;
 
-	build_completion_wait(&cmd, (u64)&sem);
 
-	ret = iommu_queue_command_sync(iommu, &cmd, false);
+	build_completion_wait(&cmd, (u64)&iommu->cmd_sem);
+
+	spin_lock_irqsave(&iommu->lock, flags);
+
+	iommu->cmd_sem = 0;
+
+	ret = __iommu_queue_command_sync(iommu, &cmd, false);
 	if (ret)
-		return ret;
+		goto out_unlock;
+
+	ret = wait_on_sem(&iommu->cmd_sem);
 
-	return wait_on_sem(&sem);
+out_unlock:
+	spin_unlock_irqrestore(&iommu->lock, flags);
+
+	return ret;
 }
 
 static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
drivers/iommu/amd_iommu_types.h +2 -0
index caf5e38..9652848 100644
@@ -524,6 +524,8 @@ struct amd_iommu {
 	struct irq_domain *ir_domain;
 	struct irq_domain *msi_domain;
 #endif
+
+	volatile u64 __aligned(8) cmd_sem;
 };
 
 #define ACPIHID_UID_LEN 256
Signature · 1 line
2.6.2

A message that is not in the archive (sent off-list or not yet received): [email protected]

#12 lore raw
Quoted 8 lines · Joerg Roedel

Hi Ingo,

Quoted 1 line · Ingo Molnar

Yeah, I can still remove it - just zapped it in fact.

Thanks, and sorry for the hassle. Here is the v2 patch that has the correct locking. I tested it with and without lockdep enabled and also under some load. Looks all fine.

Thanks a lot for the quick response!

Ingo