Skip to contentSkip to conversation

virtio_pci: Limit DMA mask to 44 bits for legacy virtio devices

PATCH Superseded Will Deacon 2 participants 3 messages Sep 14, 2016

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 virtio_pci: Limit DMA mask to 44 bits for legacy virtio devices

Legacy virtio defines the virtqueue base using a 32-bit PFN field, with a read-only register indicating a fixed page size of 4k.

This can cause problems for DMA allocators that allocate top down from the DMA mask, which is set to 64 bits. In this case, the addresses are silently truncated to 44-bit, leading to IOMMU faults, failure to read from the queue or data corruption.

This patch restricts the DMA mask for legacy PCI virtio devices to 44 bits, which matches the specification.

Cc Andy Lutomirski <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
Cc Benjamin Serebrin <[email protected]>
---
 drivers/virtio/virtio_pci_legacy.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/virtio/virtio_pci_legacy.c +7 -2
index 8c4e61783441..efb3f5dff4b7 100644
@@ -212,12 +212,17 @@ int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev)
 		return -ENODEV;
 	}
 
-	rc = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
+	/*
+	 * The virtio ring base address is expressed as a 32-bit PFN, with a
+	 * page size of 1 << VIRTIO_PCI_QUEUE_ADDR_SHIFT.
+	 */
+	rc = dma_set_mask_and_coherent(&pci_dev->dev,
+				DMA_BIT_MASK(32 + VIRTIO_PCI_QUEUE_ADDR_SHIFT));
 	if (rc)
 		rc = dma_set_mask_and_coherent(&pci_dev->dev,
 						DMA_BIT_MASK(32));
 	if (rc)
-		dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA.  Trying to continue, but this might not work.\n");
+		dev_warn(&pci_dev->dev, "Failed to enable %d-bit or 32-bit DMA.  Trying to continue, but this might not work.\n", 32 + VIRTIO_PCI_QUEUE_ADDR_SHIFT);
 
 	rc = pci_request_region(pci_dev, 0, "virtio-pci-legacy");
 	if (rc)
Signature · 1 line
2.1.4
#1 lore raw
Quoted 24 lines · Will Deacon

Legacy virtio defines the virtqueue base using a 32-bit PFN field, with a read-only register indicating a fixed page size of 4k.

This can cause problems for DMA allocators that allocate top down from the DMA mask, which is set to 64 bits. In this case, the addresses are silently truncated to 44-bit, leading to IOMMU faults, failure to read from the queue or data corruption.

This patch restricts the DMA mask for legacy PCI virtio devices to 44 bits, which matches the specification.

Cc Andy Lutomirski <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
Cc Benjamin Serebrin <[email protected]>
---
 drivers/virtio/virtio_pci_legacy.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/virtio/virtio_pci_legacy.c +0 -0
index 8c4e61783441..efb3f5dff4b7 100644

drivers/virtio/virtio_pci_legacy.c int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev) · line 220

Quoted 10 lines
drivers/virtio/virtio_pci_legacy.c +6 -1
@@ -212,12 +212,17 @@ int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev)
 		return -ENODEV;
 	}
 
-	rc = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
+	/*
+	 * The virtio ring base address is expressed as a 32-bit PFN, with a
+	 * page size of 1 << VIRTIO_PCI_QUEUE_ADDR_SHIFT.
+	 */
+	rc = dma_set_mask_and_coherent(&pci_dev->dev,
+				DMA_BIT_MASK(32 + VIRTIO_PCI_QUEUE_ADDR_SHIFT));

So why not limit just the coherent mask, as I suggested in a comment on v1?

drivers/virtio/virtio_pci_legacy.c int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev) · line 225

Quoted 6 lines
drivers/virtio/virtio_pci_legacy.c +1 -1
 	if (rc)
 		rc = dma_set_mask_and_coherent(&pci_dev->dev,
 						DMA_BIT_MASK(32));
 	if (rc)
-		dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA.  Trying to continue, but this might not work.\n");
+		dev_warn(&pci_dev->dev, "Failed to enable %d-bit or 32-bit DMA.  Trying to continue, but this might not work.\n", 32 + VIRTIO_PCI_QUEUE_ADDR_SHIFT);

I'd rather we split this.

Quoted 5 lines
drivers/virtio/virtio_pci_legacy.c +0 -0
 	rc = pci_request_region(pci_dev, 0, "virtio-pci-legacy");
 	if (rc)
Signature · 1 line
2.1.4
#2 lore raw

Hi Michael,

Quoted 28 lines · Michael S. Tsirkin
Quoted 27 lines · Will Deacon

Legacy virtio defines the virtqueue base using a 32-bit PFN field, with a read-only register indicating a fixed page size of 4k.

This can cause problems for DMA allocators that allocate top down from the DMA mask, which is set to 64 bits. In this case, the addresses are silently truncated to 44-bit, leading to IOMMU faults, failure to read from the queue or data corruption.

This patch restricts the DMA mask for legacy PCI virtio devices to 44 bits, which matches the specification.

Cc Andy Lutomirski <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
Cc Benjamin Serebrin <[email protected]>
---
 drivers/virtio/virtio_pci_legacy.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/virtio/virtio_pci_legacy.c +0 -0
index 8c4e61783441..efb3f5dff4b7 100644
@@ -212,12 +212,17 @@ int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev)
 		return -ENODEV;
 	}
 
Quoted 9 lines

drivers/virtio/virtio_pci_legacy.c int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev) · line 220

Quoted 7 lines
drivers/virtio/virtio_pci_legacy.c +6 -1
-	rc = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
+	/*
+	 * The virtio ring base address is expressed as a 32-bit PFN, with a
+	 * page size of 1 << VIRTIO_PCI_QUEUE_ADDR_SHIFT.
+	 */
+	rc = dma_set_mask_and_coherent(&pci_dev->dev,
+				DMA_BIT_MASK(32 + VIRTIO_PCI_QUEUE_ADDR_SHIFT));

So why not limit just the coherent mask, as I suggested in a comment on v1?

Sorry, I completely missed that suggestion. I'll roll a v3 with that included, although I'll rejig things a bit to make the error handling a bit more bearable.

Quoted 9 lines

drivers/virtio/virtio_pci_legacy.c int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev) · line 225

Quoted 6 lines
drivers/virtio/virtio_pci_legacy.c +1 -1
 	if (rc)
 		rc = dma_set_mask_and_coherent(&pci_dev->dev,
 						DMA_BIT_MASK(32));
 	if (rc)
-		dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA.  Trying to continue, but this might not work.\n");
+		dev_warn(&pci_dev->dev, "Failed to enable %d-bit or 32-bit DMA.  Trying to continue, but this might not work.\n", 32 + VIRTIO_PCI_QUEUE_ADDR_SHIFT);

I'd rather we split this.

I'll actually just leave like it was in the first place if we're trying a 64-bit streaming mask.

Stay tuned,

Will