Skip to contentSkip to conversation

iommu/vt-d: Avoid write-tearing on PTE clear

PATCH Superseded Nadav Amit 3 participants 3 messages May 21, 2016 – Jun 15, 2016 1 message not in the archive IOMMU

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Nadav Amit

author
#0 lore raw

PATCH 1/1 iommu/vt-d: Avoid write-tearing on PTE clear

When a PTE is cleared, the write may be teared or perform by multiple writes. In addition, in 32-bit kernel, writes are currently performed using a single 64-bit write, which does not guarantee order.

The byte-code right now does not seem to cause a problem, but it may still occur in theory.

Avoid this scenario by using WRITE_ONCE, and order the writes on 32-bit kernels.

---
 drivers/iommu/intel-iommu.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/iommu/intel-iommu.c +18 -1
index e1852e8..4f488a5 100644
@@ -326,9 +326,26 @@ struct dma_pte {
 	u64 val;
 };
 
+#ifndef CONFIG_64BIT
+union split_dma_pte {
+	struct {
+		u32 val_low;
+		u32 val_high;
+	};
+	u64 val;
+};
+#endif
+
 static inline void dma_clear_pte(struct dma_pte *pte)
 {
-	pte->val = 0;
+#ifdef CONFIG_64BIT
+	WRITE_ONCE(pte->val, 0);
+#else
+	union split_dma_pte *sdma_pte = (union split_dma_pte *)pte;
+
+	WRITE_ONCE(sdma_pte->val_low, 0);
+	sdma_pte->val_high = 0;
+#endif
 }
 
 static inline u64 dma_pte_addr(struct dma_pte *pte)
Signature · 1 line
2.7.4

A message that is not in the archive (sent off-list or not yet received): 1463824283-1683-1-git-send-email-namit-pghWNbHTmq7QT0dZR+AlfA@public.gmane.org

#2 lore raw

Ping?

Quoted 49 lines · Nadav Amit

When a PTE is cleared, the write may be teared or perform by multiple writes. In addition, in 32-bit kernel, writes are currently performed using a single 64-bit write, which does not guarantee order.

The byte-code right now does not seem to cause a problem, but it may still occur in theory.

Avoid this scenario by using WRITE_ONCE, and order the writes on 32-bit kernels.

---
drivers/iommu/intel-iommu.c | 19 ++++++++++++++++++-

1 file changed, 18 insertions(+), 1 deletion(-)
drivers/iommu/intel-iommu.c +18 -1
index e1852e8..4f488a5 100644
@@ -326,9 +326,26 @@ struct dma_pte {
 	u64 val;
 };
 
+#ifndef CONFIG_64BIT
+union split_dma_pte {
+	struct {
+		u32 val_low;
+		u32 val_high;
+	};
+	u64 val;
+};
+#endif
+
 static inline void dma_clear_pte(struct dma_pte *pte)
 {
-	pte->val = 0;
+#ifdef CONFIG_64BIT
+	WRITE_ONCE(pte->val, 0);
+#else
+	union split_dma_pte *sdma_pte = (union split_dma_pte *)pte;
+
+	WRITE_ONCE(sdma_pte->val_low, 0);
+	sdma_pte->val_high = 0;
+#endif
 }
 
 static inline u64 dma_pte_addr(struct dma_pte *pte)
Signature · 1 line
2.7.4
#3 lore raw
In reply to a message not in the archive
Quoted 20 lines · Nadav Amit

When a PTE is cleared, the write may be teared or perform by multiple writes. In addition, in 32-bit kernel, writes are currently performed using a single 64-bit write, which does not guarantee order.

The byte-code right now does not seem to cause a problem, but it may still occur in theory.

Avoid this scenario by using WRITE_ONCE, and order the writes on 32-bit kernels.

---
 drivers/iommu/intel-iommu.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/iommu/intel-iommu.c +0 -0
index e1852e8..4f488a5 100644

drivers/iommu/intel-iommu.c struct dma_pte { · line 334

Quoted 9 lines
drivers/iommu/intel-iommu.c +6 -0
@@ -326,9 +326,26 @@ struct dma_pte {
 	u64 val;
 };
 
+#ifndef CONFIG_64BIT
+union split_dma_pte {
+	struct {
+		u32 val_low;
+		u32 val_high;
+	};

Please move this struct definition to dma_clear_pte().

Quoted 5 lines
drivers/iommu/intel-iommu.c +4 -0
+	u64 val;
+};
+#endif
+
 static inline void dma_clear_pte(struct dma_pte *pte)

drivers/iommu/intel-iommu.c struct dma_pte { · line 348

Quoted 10 lines
drivers/iommu/intel-iommu.c +8 -1
 {
-	pte->val = 0;
+#ifdef CONFIG_64BIT
+	WRITE_ONCE(pte->val, 0);
+#else
+	union split_dma_pte *sdma_pte = (union split_dma_pte *)pte;
+
+	WRITE_ONCE(sdma_pte->val_low, 0);
+	sdma_pte->val_high = 0;
+#endif

And this needs a comment explaining what it going on and why it is necessary.

Thanks,

Joerg