Skip to contentSkip to conversation

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

PATCH Superseded Will Deacon 4 participants 5 messages Sep 12, 2016 – Sep 13, 2016

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 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]>
---
 drivers/virtio/virtio_pci_legacy.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/virtio/virtio_pci_legacy.c +2 -2
index 8c4e61783441..f4852febd40c 100644
@@ -212,12 +212,12 @@ 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));
+	rc = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(44));
 	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 44-bit or 32-bit DMA.  Trying to continue, but this might not work.\n");
 
 	rc = pci_request_region(pci_dev, 0, "virtio-pci-legacy");
 	if (rc)
Signature · 1 line
2.1.4
#1 lore raw
Quoted 8 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.

Quoted 6 lines

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]>

Hmm - IIUC it's actually only the case for the virtio rings themselves. The buffer addresses put in the rings are full 64 bit ones.

It so happens that virtio doesn't use coherent allocs except for the rings.
So I'm inclined to say the coherent mask should be set to 44, with a comment explaning that this is for the rings.

In case we start using coherent allocations in virtio, it might be cleaner to relax the mask after allocating the rings, but I'm not sure that's allowed by the DMA API. thoughts?

Quoted 9 lines
---
 drivers/virtio/virtio_pci_legacy.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/virtio/virtio_pci_legacy.c +0 -0
index 8c4e61783441..f4852febd40c 100644

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

Quoted 5 lines
drivers/virtio/virtio_pci_legacy.c +1 -1
@@ -212,12 +212,12 @@ 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));
+	rc = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(44));

44 would be cleaner as 32 + VIRTIO_PCI_QUEUE_ADDR_SHIFT.

Quoted 11 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 44-bit or 32-bit DMA.  Trying to continue, but this might not work.\n");
 
 	rc = pci_request_region(pci_dev, 0, "virtio-pci-legacy");
 	if (rc)
Signature · 1 line
2.1.4
#2 lore raw
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 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.

Quoted 6 lines
Quoted 3 lines
Cc Andy Lutomirski <[email protected]>
Cc Michael S. Tsirkin <[email protected]>

Hmm - IIUC it's actually only the case for the virtio rings themselves. The buffer addresses put in the rings are full 64 bit ones.

I think that's right, yes.

Quoted 4 lines

It so happens that virtio doesn't use coherent allocs except for the rings.
So I'm inclined to say the coherent mask should be set to 44, with a comment explaning that this is for the rings.

I can certainly add that in v2, along with your suggestion to use 32 + VIRTIO_PCI_QUEUE_ADDR_SHIFT instead of the 44.

Quoted 4 lines

In case we start using coherent allocations in virtio, it might be cleaner to relax the mask after allocating the rings, but I'm not sure that's allowed by the DMA API. thoughts?

Hmm, that *might* work, but I could certainly imagine some DMA implementations going wrong if they assume the mask is fixed.

Will

#3 lore raw
Quoted 14 lines · Will Deacon
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 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.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Cc Andy Lutomirski <[email protected]>
Cc Michael S. Tsirkin <[email protected]>

Hmm - IIUC it's actually only the case for the virtio rings themselves. The buffer addresses put in the rings are full 64 bit ones.

I think that's right, yes.

Yes.

Quoted 10 lines
Quoted 6 lines

It so happens that virtio doesn't use coherent allocs except for the rings.
So I'm inclined to say the coherent mask should be set to 44, with a comment explaning that this is for the rings.

I can certainly add that in v2, along with your suggestion to use 32 + VIRTIO_PCI_QUEUE_ADDR_SHIFT instead of the 44.

Quoted 7 lines
Quoted 4 lines

In case we start using coherent allocations in virtio, it might be cleaner to relax the mask after allocating the rings, but I'm not sure that's allowed by the DMA API. thoughts?

Hmm, that *might* work, but I could certainly imagine some DMA implementations going wrong if they assume the mask is fixed.

There's probably a hack to make the buffer addresses be 64-bit capable while letting the queue addresses be 44-bit, but is it worth the trouble? Maybe assume that guests with more than 16 TB of RAM are rare today, and catch the Virtio 1.x spec to fix the 32-bit PFN limit?

Alternately, you could keep the DMA mask at 64 bits but try to allocate the rings early, and abort if they can't get a < 16 TB address. I think I prefer the 44 bit mask approach, though.

Quoted 7 lines

Will
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#4 lore raw
In reply to Will Deacon
Quoted 14 lines · Will Deacon
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 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.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Cc Andy Lutomirski <[email protected]>
Cc Michael S. Tsirkin <[email protected]>

Hmm - IIUC it's actually only the case for the virtio rings themselves. The buffer addresses put in the rings are full 64 bit ones.

I think that's right, yes.

Yes.

Quoted 10 lines
Quoted 6 lines

It so happens that virtio doesn't use coherent allocs except for the rings.
So I'm inclined to say the coherent mask should be set to 44, with a comment explaning that this is for the rings.

I can certainly add that in v2, along with your suggestion to use 32 + VIRTIO_PCI_QUEUE_ADDR_SHIFT instead of the 44.

Quoted 7 lines
Quoted 4 lines

In case we start using coherent allocations in virtio, it might be cleaner to relax the mask after allocating the rings, but I'm not sure that's allowed by the DMA API. thoughts?

Hmm, that *might* work, but I could certainly imagine some DMA implementations going wrong if they assume the mask is fixed.

There's probably a hack to make the buffer addresses be 64-bit capable while letting the queue addresses be 44-bit, but is it worth the trouble? Maybe assume that guests with more than 16 TB of RAM are rare today, and catch the Virtio 1.x spec to fix the 32-bit PFN limit?

Alternately, you could keep the DMA mask at 64 bits but try to allocate the rings early, and abort if they can't get a < 16 TB address. I think I prefer the 44 bit mask approach, though.

Quoted 7 lines

Will
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization