Skip to contentSkip to conversation

virtio: Support encrypted memory on powerpc secure guests

PATCH In review Ram Pai 5 participants 24 messages Oct 12, 2019 – Oct 21, 2019

Subsystems (3)
lore
Share & tools
lore

Ram Pai

author
#0 lore raw

cover virtio: Support encrypted memory on powerpc secure guests

**We would like the patches to be merged through the virtio tree. Please review, and ack merging the DMA mapping change through that tree. Thanks!**

The memory of powerpc secure guests can't be accessed by the hypervisor / virtio device except for a few memory regions designated as 'shared'.

At the moment, Linux uses bounce-buffering to communicate with the hypervisor, with a bounce buffer marked as shared. This is how the DMA API is implemented on this platform.

In particular, the most convenient way to use virtio on this platform is by making virtio use the DMA API: in fact, this is exactly what happens if the virtio device exposes the flag VIRTIO_F_ACCESS_PLATFORM. However, bugs in the hypervisor on the powerpc platform do not allow setting this flag, with some hypervisors already in the field that don't set this flag. At the moment they are forced to use emulated devices when guest is in secure mode; virtio is only useful when guest is not secure.

Normally, both device and driver must support VIRTIO_F_ACCESS_PLATFORM:
if one of them doesn't, the other mustn't assume it for communication to work.

However, a guest-side work-around is possible to enable virtio for these hypervisors with guest in secure mode: it so happens that on powerpc secure platform the DMA address is actually a physical address - that of the bounce buffer. For these platforms we can make the virtio driver go through the DMA API even though the device itself ignores the DMA API.

These patches implement this work around for virtio: we detect that
- secure guest mode is enabled - so we know that since we don't share most memory and Hypervisor has not enabled VIRTIO_F_ACCESS_PLATFORM, regular virtio code won't work.
- DMA API is giving us addresses that are actually also physical addresses.
- Hypervisor has not enabled VIRTIO_F_ACCESS_PLATFORM.

and if all conditions are true, we force all data through the bounce buffer.

To put it another way, from hypervisor's point of view DMA API is not required: hypervisor would be happy to get access to all of guest memory. That's why it does not set VIRTIO_F_ACCESS_PLATFORM. However, guest decides that it does not trust the hypervisor and wants to force a bounce buffer for its own reasons.

Thiago Jung Bauermann (2):
dma-mapping: Add dma_addr_is_phys_addr()
virtio_ring: Use DMA API if memory is encrypted

 arch/powerpc/include/asm/dma-mapping.h | 21 +++++++++++++++++++++
 arch/powerpc/platforms/pseries/Kconfig |  1 +
 drivers/virtio/virtio.c                | 18 ++++++++++++++++++
 drivers/virtio/virtio_ring.c           |  8 ++++++++
 include/linux/dma-mapping.h            | 20 ++++++++++++++++++++
 include/linux/virtio_config.h          | 14 ++++++++++++++
 kernel/dma/Kconfig                     |  3 +++
 7 files changed, 85 insertions(+)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 1/2 dma-mapping: Add dma_addr_is_phys_addr()

From: Thiago Jung Bauermann <[email protected]>

In order to safely use the DMA API, virtio needs to know whether DMA addresses are in fact physical addresses and for that purpose, dma_addr_is_phys_addr() is introduced.

cc Benjamin Herrenschmidt <[email protected]>
cc David Gibson <[email protected]>
cc Michael Ellerman <[email protected]>
cc Paul Mackerras <[email protected]>
cc Michael Roth <[email protected]>
cc Alexey Kardashevskiy <[email protected]>
cc Paul Burton <[email protected]>
cc Robin Murphy <[email protected]>
cc Bartlomiej Zolnierkiewicz <[email protected]>
cc Marek Szyprowski <[email protected]>
cc Christoph Hellwig <[email protected]>
---
 arch/powerpc/include/asm/dma-mapping.h | 21 +++++++++++++++++++++
 arch/powerpc/platforms/pseries/Kconfig |  1 +
 include/linux/dma-mapping.h            | 20 ++++++++++++++++++++
 kernel/dma/Kconfig                     |  3 +++
 4 files changed, 45 insertions(+)
arch/powerpc/include/asm/dma-mapping.h +21 -0
index 565d6f7..f92c0a4b 100644
@@ -5,6 +5,8 @@
 #ifndef _ASM_DMA_MAPPING_H
 #define _ASM_DMA_MAPPING_H
 
+#include <asm/svm.h>
+
 static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
 {
 	/* We don't handle the NULL dev case for ISA for now. We could
@@ -15,4 +17,23 @@ static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
 	return NULL;
 }
 
+#ifdef CONFIG_ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+/**
+ * dma_addr_is_phys_addr - check whether a device DMA address is a physical
+ *		address
+ * @dev:	device to check
+ *
+ * Returns %true if any DMA address for this device happens to also be a valid
+ * physical address (not necessarily of the same page).
+ */
+static inline bool dma_addr_is_phys_addr(struct device *dev)
+{
+	/*
+	 * Secure guests always use the SWIOTLB, therefore DMA addresses are
+	 * actually the physical address of the bounce buffer.
+	 */
+	return is_secure_guest();
+}
+#endif
+
 #endif	/* _ASM_DMA_MAPPING_H */
arch/powerpc/platforms/pseries/Kconfig +1 -0
index 9e35cdd..0108150 100644
@@ -152,6 +152,7 @@ config PPC_SVM
 	select SWIOTLB
 	select ARCH_HAS_MEM_ENCRYPT
 	select ARCH_HAS_FORCE_DMA_UNENCRYPTED
+	select ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
 	help
 	 There are certain POWER platforms which support secure guests using
 	 the Protected Execution Facility, with the help of an Ultravisor
include/linux/dma-mapping.h +20 -0
index f7d1eea..6df5664 100644
@@ -693,6 +693,26 @@ static inline bool dma_addressing_limited(struct device *dev)
 			    dma_get_required_mask(dev);
 }
 
+#ifndef CONFIG_ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+/**
+ * dma_addr_is_phys_addr - check whether a device DMA address is a physical
+ *		address
+ * @dev:	device to check
+ *
+ * Returns %true if any DMA address for this device happens to also be a valid
+ * physical address (not necessarily of the same page).
+ */
+static inline bool dma_addr_is_phys_addr(struct device *dev)
+{
+	/*
+	 * Except in very specific setups, DMA addresses exist in a different
+	 * address space from CPU physical addresses and cannot be directly used
+	 * to reference system memory.
+	 */
+	return false;
+}
+#endif
+
 #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS
 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 		const struct iommu_ops *iommu, bool coherent);
kernel/dma/Kconfig +3 -0
index 9decbba..6209b46 100644
@@ -51,6 +51,9 @@ config ARCH_HAS_DMA_MMAP_PGPROT
 config ARCH_HAS_FORCE_DMA_UNENCRYPTED
 	bool
 
+config ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+	bool
+
 config DMA_NONCOHERENT_CACHE_SYNC
 	bool
 
Signature · 1 line
1.8.3.1
author
#2 lore raw

PATCH 1/2 dma-mapping: Add dma_addr_is_phys_addr()

In reply to Ram Pai

From: Thiago Jung Bauermann <[email protected]>

In order to safely use the DMA API, virtio needs to know whether DMA addresses are in fact physical addresses and for that purpose, dma_addr_is_phys_addr() is introduced.

cc Benjamin Herrenschmidt <[email protected]>
cc David Gibson <[email protected]>
cc Michael Ellerman <[email protected]>
cc Paul Mackerras <[email protected]>
cc Michael Roth <[email protected]>
cc Alexey Kardashevskiy <[email protected]>
cc Paul Burton <[email protected]>
cc Robin Murphy <[email protected]>
cc Bartlomiej Zolnierkiewicz <[email protected]>
cc Marek Szyprowski <[email protected]>
cc Christoph Hellwig <[email protected]>
---
 arch/powerpc/include/asm/dma-mapping.h | 21 +++++++++++++++++++++
 arch/powerpc/platforms/pseries/Kconfig |  1 +
 include/linux/dma-mapping.h            | 20 ++++++++++++++++++++
 kernel/dma/Kconfig                     |  3 +++
 4 files changed, 45 insertions(+)
arch/powerpc/include/asm/dma-mapping.h +21 -0
index 565d6f7..f92c0a4b 100644
@@ -5,6 +5,8 @@
 #ifndef _ASM_DMA_MAPPING_H
 #define _ASM_DMA_MAPPING_H
 
+#include <asm/svm.h>
+
 static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
 {
 	/* We don't handle the NULL dev case for ISA for now. We could
@@ -15,4 +17,23 @@ static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
 	return NULL;
 }
 
+#ifdef CONFIG_ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+/**
+ * dma_addr_is_phys_addr - check whether a device DMA address is a physical
+ *		address
+ * @dev:	device to check
+ *
+ * Returns %true if any DMA address for this device happens to also be a valid
+ * physical address (not necessarily of the same page).
+ */
+static inline bool dma_addr_is_phys_addr(struct device *dev)
+{
+	/*
+	 * Secure guests always use the SWIOTLB, therefore DMA addresses are
+	 * actually the physical address of the bounce buffer.
+	 */
+	return is_secure_guest();
+}
+#endif
+
 #endif	/* _ASM_DMA_MAPPING_H */
arch/powerpc/platforms/pseries/Kconfig +1 -0
index 9e35cdd..0108150 100644
@@ -152,6 +152,7 @@ config PPC_SVM
 	select SWIOTLB
 	select ARCH_HAS_MEM_ENCRYPT
 	select ARCH_HAS_FORCE_DMA_UNENCRYPTED
+	select ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
 	help
 	 There are certain POWER platforms which support secure guests using
 	 the Protected Execution Facility, with the help of an Ultravisor
include/linux/dma-mapping.h +20 -0
index f7d1eea..6df5664 100644
@@ -693,6 +693,26 @@ static inline bool dma_addressing_limited(struct device *dev)
 			    dma_get_required_mask(dev);
 }
 
+#ifndef CONFIG_ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+/**
+ * dma_addr_is_phys_addr - check whether a device DMA address is a physical
+ *		address
+ * @dev:	device to check
+ *
+ * Returns %true if any DMA address for this device happens to also be a valid
+ * physical address (not necessarily of the same page).
+ */
+static inline bool dma_addr_is_phys_addr(struct device *dev)
+{
+	/*
+	 * Except in very specific setups, DMA addresses exist in a different
+	 * address space from CPU physical addresses and cannot be directly used
+	 * to reference system memory.
+	 */
+	return false;
+}
+#endif
+
 #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS
 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 		const struct iommu_ops *iommu, bool coherent);
kernel/dma/Kconfig +3 -0
index 9decbba..6209b46 100644
@@ -51,6 +51,9 @@ config ARCH_HAS_DMA_MMAP_PGPROT
 config ARCH_HAS_FORCE_DMA_UNENCRYPTED
 	bool
 
+config ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+	bool
+
 config DMA_NONCOHERENT_CACHE_SYNC
 	bool
 
Signature · 1 line
1.8.3.1
author
#3 lore raw

PATCH 2/2 virtio_ring: Use DMA API if memory is encrypted

From: Thiago Jung Bauermann <[email protected]>

Normally, virtio enables DMA API with VIRTIO_F_IOMMU_PLATFORM, which must be set by both device and guest driver. However, as a hack, when DMA API returns physical addresses, guest driver can use the DMA API; even though device does not set VIRTIO_F_IOMMU_PLATFORM and just uses physical addresses.

Doing this works-around POWER secure guests for which only the bounce buffer is accessible to the device, but which don't set VIRTIO_F_IOMMU_PLATFORM due to a set of hypervisor and architectural bugs. To guard against platform changes, breaking any of these assumptions down the road, we check at probe time and fail if that's not the case.

cc Benjamin Herrenschmidt <[email protected]>
cc David Gibson <[email protected]>
cc Michael Ellerman <[email protected]>
cc Paul Mackerras <[email protected]>
cc Michael Roth <[email protected]>
cc Alexey Kardashevskiy <[email protected]>
cc Jason Wang <[email protected]>
cc Christoph Hellwig <[email protected]>
---
 drivers/virtio/virtio.c       | 18 ++++++++++++++++++
 drivers/virtio/virtio_ring.c  |  8 ++++++++
 include/linux/virtio_config.h | 14 ++++++++++++++
 3 files changed, 40 insertions(+)
drivers/virtio/virtio.c +18 -0
index a977e32..77a3baf 100644
@@ -4,6 +4,7 @@
 #include <linux/virtio_config.h>
 #include <linux/module.h>
 #include <linux/idr.h>
+#include <linux/dma-mapping.h>
 #include <uapi/linux/virtio_ids.h>
 
 /* Unique numbering for virtio devices. */
@@ -245,6 +246,23 @@ static int virtio_dev_probe(struct device *_d)
 	if (err)
 		goto err;
 
+	/*
+	 * If memory is encrypted, but VIRTIO_F_IOMMU_PLATFORM is not set, then
+	 * the device is broken: DMA API is required for these platforms, but
+	 * the only way using the DMA API is going to work at all is if the
+	 * device is ready for it. So we need a flag on the virtio device,
+	 * exposed by the hypervisor (or hardware for hw virtio devices) that
+	 * says: hey, I'm real, don't take a shortcut.
+	 *
+	 * There's one exception where guest can make things work, and that is
+	 * when DMA API is guaranteed to always return physical addresses.
+	 */
+	if (mem_encrypt_active() && !virtio_can_use_dma_api(dev)) {
+		dev_err(_d, "virtio: device unable to access encrypted memory\n");
+		err = -EINVAL;
+		goto err;
+	}
+
 	err = drv->probe(dev);
 	if (err)
 		goto err;
drivers/virtio/virtio_ring.c +8 -0
index c8be1c4..9c56b61 100644
@@ -255,6 +255,14 @@ static bool vring_use_dma_api(struct virtio_device *vdev)
 	if (xen_domain())
 		return true;
 
+	/*
+	 * Also, if guest memory is encrypted the host can't access it
+	 * directly. We need to either use an IOMMU or do bounce buffering.
+	 * Both are done via the DMA API.
+	 */
+	if (mem_encrypt_active() && virtio_can_use_dma_api(vdev))
+		return true;
+
 	return false;
 }
 
include/linux/virtio_config.h +14 -0
index bb4cc49..57bc25c 100644
@@ -4,6 +4,7 @@
 
 #include <linux/err.h>
 #include <linux/bug.h>
+#include <linux/dma-mapping.h>
 #include <linux/virtio.h>
 #include <linux/virtio_byteorder.h>
 #include <uapi/linux/virtio_config.h>
@@ -174,6 +175,19 @@ static inline bool virtio_has_iommu_quirk(const struct virtio_device *vdev)
 	return !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
 }
 
+/**
+ * virtio_can_use_dma_api - determine whether the DMA API can be used
+ * @vdev: the device
+ *
+ * The DMA API can be used either when the device doesn't have the IOMMU quirk,
+ * or when the DMA API is guaranteed to always return physical addresses.
+ */
+static inline bool virtio_can_use_dma_api(const struct virtio_device *vdev)
+{
+	return !virtio_has_iommu_quirk(vdev) ||
+	       dma_addr_is_phys_addr(vdev->dev.parent);
+}
+
 static inline
 struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
 					vq_callback_t *c, const char *n)
Signature · 1 line
1.8.3.1
author
#4 lore raw

PATCH 2/2 virtio_ring: Use DMA API if memory is encrypted

In reply to Ram Pai

From: Thiago Jung Bauermann <[email protected]>

Normally, virtio enables DMA API with VIRTIO_F_IOMMU_PLATFORM, which must be set by both device and guest driver. However, as a hack, when DMA API returns physical addresses, guest driver can use the DMA API; even though device does not set VIRTIO_F_IOMMU_PLATFORM and just uses physical addresses.

Doing this works-around POWER secure guests for which only the bounce buffer is accessible to the device, but which don't set VIRTIO_F_IOMMU_PLATFORM due to a set of hypervisor and architectural bugs. To guard against platform changes, breaking any of these assumptions down the road, we check at probe time and fail if that's not the case.

cc Benjamin Herrenschmidt <[email protected]>
cc David Gibson <[email protected]>
cc Michael Ellerman <[email protected]>
cc Paul Mackerras <[email protected]>
cc Michael Roth <[email protected]>
cc Alexey Kardashevskiy <[email protected]>
cc Jason Wang <[email protected]>
cc Christoph Hellwig <[email protected]>
---
 drivers/virtio/virtio.c       | 18 ++++++++++++++++++
 drivers/virtio/virtio_ring.c  |  8 ++++++++
 include/linux/virtio_config.h | 14 ++++++++++++++
 3 files changed, 40 insertions(+)
drivers/virtio/virtio.c +18 -0
index a977e32..77a3baf 100644
@@ -4,6 +4,7 @@
 #include <linux/virtio_config.h>
 #include <linux/module.h>
 #include <linux/idr.h>
+#include <linux/dma-mapping.h>
 #include <uapi/linux/virtio_ids.h>
 
 /* Unique numbering for virtio devices. */
@@ -245,6 +246,23 @@ static int virtio_dev_probe(struct device *_d)
 	if (err)
 		goto err;
 
+	/*
+	 * If memory is encrypted, but VIRTIO_F_IOMMU_PLATFORM is not set, then
+	 * the device is broken: DMA API is required for these platforms, but
+	 * the only way using the DMA API is going to work at all is if the
+	 * device is ready for it. So we need a flag on the virtio device,
+	 * exposed by the hypervisor (or hardware for hw virtio devices) that
+	 * says: hey, I'm real, don't take a shortcut.
+	 *
+	 * There's one exception where guest can make things work, and that is
+	 * when DMA API is guaranteed to always return physical addresses.
+	 */
+	if (mem_encrypt_active() && !virtio_can_use_dma_api(dev)) {
+		dev_err(_d, "virtio: device unable to access encrypted memory\n");
+		err = -EINVAL;
+		goto err;
+	}
+
 	err = drv->probe(dev);
 	if (err)
 		goto err;
drivers/virtio/virtio_ring.c +8 -0
index c8be1c4..9c56b61 100644
@@ -255,6 +255,14 @@ static bool vring_use_dma_api(struct virtio_device *vdev)
 	if (xen_domain())
 		return true;
 
+	/*
+	 * Also, if guest memory is encrypted the host can't access it
+	 * directly. We need to either use an IOMMU or do bounce buffering.
+	 * Both are done via the DMA API.
+	 */
+	if (mem_encrypt_active() && virtio_can_use_dma_api(vdev))
+		return true;
+
 	return false;
 }
 
include/linux/virtio_config.h +14 -0
index bb4cc49..57bc25c 100644
@@ -4,6 +4,7 @@
 
 #include <linux/err.h>
 #include <linux/bug.h>
+#include <linux/dma-mapping.h>
 #include <linux/virtio.h>
 #include <linux/virtio_byteorder.h>
 #include <uapi/linux/virtio_config.h>
@@ -174,6 +175,19 @@ static inline bool virtio_has_iommu_quirk(const struct virtio_device *vdev)
 	return !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
 }
 
+/**
+ * virtio_can_use_dma_api - determine whether the DMA API can be used
+ * @vdev: the device
+ *
+ * The DMA API can be used either when the device doesn't have the IOMMU quirk,
+ * or when the DMA API is guaranteed to always return physical addresses.
+ */
+static inline bool virtio_can_use_dma_api(const struct virtio_device *vdev)
+{
+	return !virtio_has_iommu_quirk(vdev) ||
+	       dma_addr_is_phys_addr(vdev->dev.parent);
+}
+
 static inline
 struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
 					vq_callback_t *c, const char *n)
Signature · 1 line
1.8.3.1
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] virtio_ring: Use DMA API if memory is encrypted

Quoted 14 lines · Ram Pai

From: Thiago Jung Bauermann <[email protected]>

Normally, virtio enables DMA API with VIRTIO_F_IOMMU_PLATFORM, which must be set by both device and guest driver. However, as a hack, when DMA API returns physical addresses, guest driver can use the DMA API; even though device does not set VIRTIO_F_IOMMU_PLATFORM and just uses physical addresses.

Doing this works-around POWER secure guests for which only the bounce buffer is accessible to the device, but which don't set VIRTIO_F_IOMMU_PLATFORM due to a set of hypervisor and architectural bugs. To guard against platform changes, breaking any of these assumptions down the road, we check at probe time and fail if that's not the case.

Quoted 11 lines
cc Benjamin Herrenschmidt <[email protected]>
cc David Gibson <[email protected]>
cc Michael Ellerman <[email protected]>
cc Paul Mackerras <[email protected]>
cc Michael Roth <[email protected]>
cc Alexey Kardashevskiy <[email protected]>
cc Jason Wang <[email protected]>
cc Christoph Hellwig <[email protected]>

I don't know that this is the most elegant solution possible. But it's simple, gets the job done and pretty unlikely to cause mysterious breakage down the road.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

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

Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] virtio_ring: Use DMA API if memory is encrypted

In reply to Ram Pai
Quoted 14 lines · Ram Pai

From: Thiago Jung Bauermann <[email protected]>

Normally, virtio enables DMA API with VIRTIO_F_IOMMU_PLATFORM, which must be set by both device and guest driver. However, as a hack, when DMA API returns physical addresses, guest driver can use the DMA API; even though device does not set VIRTIO_F_IOMMU_PLATFORM and just uses physical addresses.

Doing this works-around POWER secure guests for which only the bounce buffer is accessible to the device, but which don't set VIRTIO_F_IOMMU_PLATFORM due to a set of hypervisor and architectural bugs. To guard against platform changes, breaking any of these assumptions down the road, we check at probe time and fail if that's not the case.

Quoted 11 lines
cc Benjamin Herrenschmidt <[email protected]>
cc David Gibson <[email protected]>
cc Michael Ellerman <[email protected]>
cc Paul Mackerras <[email protected]>
cc Michael Roth <[email protected]>
cc Alexey Kardashevskiy <[email protected]>
cc Jason Wang <[email protected]>
cc Christoph Hellwig <[email protected]>

I don't know that this is the most elegant solution possible. But it's simple, gets the job done and pretty unlikely to cause mysterious breakage down the road.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#7 lore raw

Re: [PATCH 2/2] virtio_ring: Use DMA API if memory is encrypted

In reply to Ram Pai
Quoted 7 lines · Ram Pai

From: Thiago Jung Bauermann <[email protected]>

Normally, virtio enables DMA API with VIRTIO_F_IOMMU_PLATFORM, which must be set by both device and guest driver. However, as a hack, when DMA API returns physical addresses, guest driver can use the DMA API; even though device does not set VIRTIO_F_IOMMU_PLATFORM and just uses physical addresses.

Sorry, but this is a complete bullshit hack. Driver must always use the DMA API if they do DMA, and if virtio devices use physical addresses that needs to be returned through the platform firmware interfaces for the dma setup. If you don't do that yet (which based on previous informations you don't), you need to fix it, and we can then quirk old implementations that already are out in the field.

In other words: we finally need to fix that virtio mess and not pile hacks on top of hacks.

#8 lore raw

Re: [PATCH 2/2] virtio_ring: Use DMA API if memory is encrypted

In reply to Ram Pai
Quoted 7 lines · Ram Pai

From: Thiago Jung Bauermann <[email protected]>

Normally, virtio enables DMA API with VIRTIO_F_IOMMU_PLATFORM, which must be set by both device and guest driver. However, as a hack, when DMA API returns physical addresses, guest driver can use the DMA API; even though device does not set VIRTIO_F_IOMMU_PLATFORM and just uses physical addresses.

Sorry, but this is a complete bullshit hack. Driver must always use the DMA API if they do DMA, and if virtio devices use physical addresses that needs to be returned through the platform firmware interfaces for the dma setup. If you don't do that yet (which based on previous informations you don't), you need to fix it, and we can then quirk old implementations that already are out in the field.

In other words: we finally need to fix that virtio mess and not pile hacks on top of hacks.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

author
#9 lore raw

RE: [PATCH 2/2] virtio_ring: Use DMA API if memory is encrypted

Quoted 16 lines · Christoph Hellwig
Quoted 7 lines · Ram Pai

From: Thiago Jung Bauermann <[email protected]>

Normally, virtio enables DMA API with VIRTIO_F_IOMMU_PLATFORM, which must be set by both device and guest driver. However, as a hack, when DMA API returns physical addresses, guest driver can use the DMA API; even though device does not set VIRTIO_F_IOMMU_PLATFORM and just uses physical addresses.

Sorry, but this is a complete bullshit hack. Driver must always use the DMA API if they do DMA, and if virtio devices use physical addresses that needs to be returned through the platform firmware interfaces for the dma setup. If you don't do that yet (which based on previous informations you don't), you need to fix it, and we can then quirk old implementations that already are out in the field.

Quoted 2 lines

In other words: we finally need to fix that virtio mess and not pile hacks on top of hacks.

So force all virtio devices to use DMA API, except when VIRTIO_F_IOMMU_PLATFORM is not enabled?

Any help detailing the idea, will enable us fix this issue once for all.

Will something like below work? It removes the prior hacks, and always uses DMA API; except when VIRTIO_F_IOMMU_PLATFORM is not enabled.

drivers/virtio/virtio_ring.c +3 -15
index c8be1c4..b593d3d 100644
@@ -240,22 +240,10 @@ static inline bool virtqueue_use_indirect(struct virtqueue *_vq,
 
 static bool vring_use_dma_api(struct virtio_device *vdev)
 {
-	if (!virtio_has_iommu_quirk(vdev))
-		return true;
-
-	/* Otherwise, we are left to guess. */
-	/*
-	 * In theory, it's possible to have a buggy QEMU-supposed
-	 * emulated Q35 IOMMU and Xen enabled at the same time.  On
-	 * such a configuration, virtio has never worked and will
-	 * not work without an even larger kludge.  Instead, enable
-	 * the DMA API if we're a Xen guest, which at least allows
-	 * all of the sensible Xen configurations to work correctly.
-	 */
-	if (xen_domain())
-		return true;
+	if (virtio_has_iommu_quirk(vdev))
+		return false;
 
-	return false;
+	return true;
 }
 
 size_t virtio_max_dma_size(struct virtio_device *vdev)
Signature · 1 line
Ram Pai
author
#10 lore raw

RE: [PATCH 2/2] virtio_ring: Use DMA API if memory is encrypted

In reply to Christoph Hellwig
Quoted 16 lines · Christoph Hellwig
Quoted 7 lines · Ram Pai

From: Thiago Jung Bauermann <[email protected]>

Normally, virtio enables DMA API with VIRTIO_F_IOMMU_PLATFORM, which must be set by both device and guest driver. However, as a hack, when DMA API returns physical addresses, guest driver can use the DMA API; even though device does not set VIRTIO_F_IOMMU_PLATFORM and just uses physical addresses.

Sorry, but this is a complete bullshit hack. Driver must always use the DMA API if they do DMA, and if virtio devices use physical addresses that needs to be returned through the platform firmware interfaces for the dma setup. If you don't do that yet (which based on previous informations you don't), you need to fix it, and we can then quirk old implementations that already are out in the field.

Quoted 2 lines

In other words: we finally need to fix that virtio mess and not pile hacks on top of hacks.

So force all virtio devices to use DMA API, except when VIRTIO_F_IOMMU_PLATFORM is not enabled?

Any help detailing the idea, will enable us fix this issue once for all.

Will something like below work? It removes the prior hacks, and always uses DMA API; except when VIRTIO_F_IOMMU_PLATFORM is not enabled.

drivers/virtio/virtio_ring.c +3 -15
index c8be1c4..b593d3d 100644
@@ -240,22 +240,10 @@ static inline bool virtqueue_use_indirect(struct virtqueue *_vq,
 
 static bool vring_use_dma_api(struct virtio_device *vdev)
 {
-	if (!virtio_has_iommu_quirk(vdev))
-		return true;
-
-	/* Otherwise, we are left to guess. */
-	/*
-	 * In theory, it's possible to have a buggy QEMU-supposed
-	 * emulated Q35 IOMMU and Xen enabled at the same time.  On
-	 * such a configuration, virtio has never worked and will
-	 * not work without an even larger kludge.  Instead, enable
-	 * the DMA API if we're a Xen guest, which at least allows
-	 * all of the sensible Xen configurations to work correctly.
-	 */
-	if (xen_domain())
-		return true;
+	if (virtio_has_iommu_quirk(vdev))
+		return false;
 
-	return false;
+	return true;
 }
 
 size_t virtio_max_dma_size(struct virtio_device *vdev)
Signature · 1 line
Ram Pai
#11 lore raw

Re: [PATCH 2/2] virtio_ring: Use DMA API if memory is encrypted

In reply to Christoph Hellwig
Quoted 15 lines · Christoph Hellwig
Quoted 7 lines · Ram Pai

From: Thiago Jung Bauermann <[email protected]>

Normally, virtio enables DMA API with VIRTIO_F_IOMMU_PLATFORM, which must be set by both device and guest driver. However, as a hack, when DMA API returns physical addresses, guest driver can use the DMA API; even though device does not set VIRTIO_F_IOMMU_PLATFORM and just uses physical addresses.

Sorry, but this is a complete bullshit hack. Driver must always use the DMA API if they do DMA, and if virtio devices use physical addresses that needs to be returned through the platform firmware interfaces for the dma setup. If you don't do that yet (which based on previous informations you don't), you need to fix it, and we can then quirk old implementations that already are out in the field.

Quoted 2 lines

In other words: we finally need to fix that virtio mess and not pile hacks on top of hacks.

I agree, the only reason for IOMMU_PLATFORM is to make sure guest works for some old and buggy qemu when vIOMMU is enabled which seems useless (note we don't even support vIOMMU migration in that case).

Thanks

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

#12 lore raw

Re: [PATCH 2/2] virtio_ring: Use DMA API if memory is encrypted

In reply to Christoph Hellwig
Quoted 15 lines · Christoph Hellwig
Quoted 7 lines · Ram Pai

From: Thiago Jung Bauermann <[email protected]>

Normally, virtio enables DMA API with VIRTIO_F_IOMMU_PLATFORM, which must be set by both device and guest driver. However, as a hack, when DMA API returns physical addresses, guest driver can use the DMA API; even though device does not set VIRTIO_F_IOMMU_PLATFORM and just uses physical addresses.

Sorry, but this is a complete bullshit hack. Driver must always use the DMA API if they do DMA, and if virtio devices use physical addresses that needs to be returned through the platform firmware interfaces for the dma setup. If you don't do that yet (which based on previous informations you don't), you need to fix it, and we can then quirk old implementations that already are out in the field.

Quoted 2 lines

In other words: we finally need to fix that virtio mess and not pile hacks on top of hacks.

I agree, the only reason for IOMMU_PLATFORM is to make sure guest works for some old and buggy qemu when vIOMMU is enabled which seems useless (note we don't even support vIOMMU migration in that case).

Thanks

#13 lore raw

Re: [PATCH 2/2] virtio_ring: Use DMA API if memory is encrypted

In reply to Christoph Hellwig
Quoted 16 lines · Christoph Hellwig
Quoted 7 lines · Ram Pai

From: Thiago Jung Bauermann <[email protected]>

Normally, virtio enables DMA API with VIRTIO_F_IOMMU_PLATFORM, which must be set by both device and guest driver. However, as a hack, when DMA API returns physical addresses, guest driver can use the DMA API; even though device does not set VIRTIO_F_IOMMU_PLATFORM and just uses physical addresses.

Sorry, but this is a complete bullshit hack. Driver must always use the DMA API if they do DMA, and if virtio devices use physical addresses that needs to be returned through the platform firmware interfaces for the dma setup. If you don't do that yet (which based on previous informations you don't), you need to fix it, and we can then quirk old implementations that already are out in the field.

Quoted 2 lines

In other words: we finally need to fix that virtio mess and not pile hacks on top of hacks.

Christoph, if I understand correctly, your objection isn't so much to the proposed change here of itself, except insofar as it entrenches virtio's existing code allowing it to either use the DMA api or bypass it and use physical addresses directly. Is that right, or have I missed something?

Where do you envisage the decision to bypass the IOMMU being made? The virtio spec more or less states that virtio devices use hypervisor magic to access physical addresses directly, rather than using normal DMA channels. The F_IOMMU_PLATFORM flag then overrides that, since it obviously won't work for hardware devices.

The platform code isn't really in a position to know that virtio devices are (usually) magic. So were you envisaging the virtio driver explicitly telling the platform to use bypassing DMA operations?

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

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

#14 lore raw

Re: [PATCH 2/2] virtio_ring: Use DMA API if memory is encrypted

In reply to Christoph Hellwig
Quoted 16 lines · Christoph Hellwig
Quoted 7 lines · Ram Pai

From: Thiago Jung Bauermann <[email protected]>

Normally, virtio enables DMA API with VIRTIO_F_IOMMU_PLATFORM, which must be set by both device and guest driver. However, as a hack, when DMA API returns physical addresses, guest driver can use the DMA API; even though device does not set VIRTIO_F_IOMMU_PLATFORM and just uses physical addresses.

Sorry, but this is a complete bullshit hack. Driver must always use the DMA API if they do DMA, and if virtio devices use physical addresses that needs to be returned through the platform firmware interfaces for the dma setup. If you don't do that yet (which based on previous informations you don't), you need to fix it, and we can then quirk old implementations that already are out in the field.

Quoted 2 lines

In other words: we finally need to fix that virtio mess and not pile hacks on top of hacks.

Christoph, if I understand correctly, your objection isn't so much to the proposed change here of itself, except insofar as it entrenches virtio's existing code allowing it to either use the DMA api or bypass it and use physical addresses directly. Is that right, or have I missed something?

Where do you envisage the decision to bypass the IOMMU being made? The virtio spec more or less states that virtio devices use hypervisor magic to access physical addresses directly, rather than using normal DMA channels. The F_IOMMU_PLATFORM flag then overrides that, since it obviously won't work for hardware devices.

The platform code isn't really in a position to know that virtio devices are (usually) magic. So were you envisaging the virtio driver explicitly telling the platform to use bypassing DMA operations?

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

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

Reviewed-by on v1 1/2
#15 lore raw

Re: [PATCH 1/2] dma-mapping: Add dma_addr_is_phys_addr()

In reply to Ram Pai
Quoted 6 lines · Ram Pai

From: Thiago Jung Bauermann <[email protected]>

In order to safely use the DMA API, virtio needs to know whether DMA addresses are in fact physical addresses and for that purpose, dma_addr_is_phys_addr() is introduced.

Quoted 14 lines
cc Benjamin Herrenschmidt <[email protected]>
cc David Gibson <[email protected]>
cc Michael Ellerman <[email protected]>
cc Paul Mackerras <[email protected]>
cc Michael Roth <[email protected]>
cc Alexey Kardashevskiy <[email protected]>
cc Paul Burton <[email protected]>
cc Robin Murphy <[email protected]>
cc Bartlomiej Zolnierkiewicz <[email protected]>
cc Marek Szyprowski <[email protected]>
cc Christoph Hellwig <[email protected]>

The change itself looks ok, so

However, I would like to see the commit message (and maybe the inline comments) expanded a bit on what the distinction here is about. Some of the text from the next patch would be suitable, about DMA addresses usually being in a different address space but not in the case of bounce buffering.

Quoted 101 lines
---
 arch/powerpc/include/asm/dma-mapping.h | 21 +++++++++++++++++++++
 arch/powerpc/platforms/pseries/Kconfig |  1 +
 include/linux/dma-mapping.h            | 20 ++++++++++++++++++++
 kernel/dma/Kconfig                     |  3 +++
 4 files changed, 45 insertions(+)
arch/powerpc/include/asm/dma-mapping.h +21 -0
index 565d6f7..f92c0a4b 100644
@@ -5,6 +5,8 @@
 #ifndef _ASM_DMA_MAPPING_H
 #define _ASM_DMA_MAPPING_H
 
+#include <asm/svm.h>
+
 static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
 {
 	/* We don't handle the NULL dev case for ISA for now. We could
@@ -15,4 +17,23 @@ static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
 	return NULL;
 }
 
+#ifdef CONFIG_ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+/**
+ * dma_addr_is_phys_addr - check whether a device DMA address is a physical
+ *		address
+ * @dev:	device to check
+ *
+ * Returns %true if any DMA address for this device happens to also be a valid
+ * physical address (not necessarily of the same page).
+ */
+static inline bool dma_addr_is_phys_addr(struct device *dev)
+{
+	/*
+	 * Secure guests always use the SWIOTLB, therefore DMA addresses are
+	 * actually the physical address of the bounce buffer.
+	 */
+	return is_secure_guest();
+}
+#endif
+
 #endif	/* _ASM_DMA_MAPPING_H */
arch/powerpc/platforms/pseries/Kconfig +1 -0
index 9e35cdd..0108150 100644
@@ -152,6 +152,7 @@ config PPC_SVM
 	select SWIOTLB
 	select ARCH_HAS_MEM_ENCRYPT
 	select ARCH_HAS_FORCE_DMA_UNENCRYPTED
+	select ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
 	help
 	 There are certain POWER platforms which support secure guests using
 	 the Protected Execution Facility, with the help of an Ultravisor
include/linux/dma-mapping.h +20 -0
index f7d1eea..6df5664 100644
@@ -693,6 +693,26 @@ static inline bool dma_addressing_limited(struct device *dev)
 			    dma_get_required_mask(dev);
 }
 
+#ifndef CONFIG_ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+/**
+ * dma_addr_is_phys_addr - check whether a device DMA address is a physical
+ *		address
+ * @dev:	device to check
+ *
+ * Returns %true if any DMA address for this device happens to also be a valid
+ * physical address (not necessarily of the same page).
+ */
+static inline bool dma_addr_is_phys_addr(struct device *dev)
+{
+	/*
+	 * Except in very specific setups, DMA addresses exist in a different
+	 * address space from CPU physical addresses and cannot be directly used
+	 * to reference system memory.
+	 */
+	return false;
+}
+#endif
+
 #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS
 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 		const struct iommu_ops *iommu, bool coherent);
kernel/dma/Kconfig +3 -0
index 9decbba..6209b46 100644
@@ -51,6 +51,9 @@ config ARCH_HAS_DMA_MMAP_PGPROT
 config ARCH_HAS_FORCE_DMA_UNENCRYPTED
 	bool
 
+config ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+	bool
+
 config DMA_NONCOHERENT_CACHE_SYNC
 	bool
 
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

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

Reviewed-by on v1 1/2
#16 lore raw

Re: [PATCH 1/2] dma-mapping: Add dma_addr_is_phys_addr()

In reply to Ram Pai
Quoted 6 lines · Ram Pai

From: Thiago Jung Bauermann <[email protected]>

In order to safely use the DMA API, virtio needs to know whether DMA addresses are in fact physical addresses and for that purpose, dma_addr_is_phys_addr() is introduced.

Quoted 14 lines
cc Benjamin Herrenschmidt <[email protected]>
cc David Gibson <[email protected]>
cc Michael Ellerman <[email protected]>
cc Paul Mackerras <[email protected]>
cc Michael Roth <[email protected]>
cc Alexey Kardashevskiy <[email protected]>
cc Paul Burton <[email protected]>
cc Robin Murphy <[email protected]>
cc Bartlomiej Zolnierkiewicz <[email protected]>
cc Marek Szyprowski <[email protected]>
cc Christoph Hellwig <[email protected]>

The change itself looks ok, so

However, I would like to see the commit message (and maybe the inline comments) expanded a bit on what the distinction here is about. Some of the text from the next patch would be suitable, about DMA addresses usually being in a different address space but not in the case of bounce buffering.

Quoted 101 lines
---
 arch/powerpc/include/asm/dma-mapping.h | 21 +++++++++++++++++++++
 arch/powerpc/platforms/pseries/Kconfig |  1 +
 include/linux/dma-mapping.h            | 20 ++++++++++++++++++++
 kernel/dma/Kconfig                     |  3 +++
 4 files changed, 45 insertions(+)
arch/powerpc/include/asm/dma-mapping.h +21 -0
index 565d6f7..f92c0a4b 100644
@@ -5,6 +5,8 @@
 #ifndef _ASM_DMA_MAPPING_H
 #define _ASM_DMA_MAPPING_H
 
+#include <asm/svm.h>
+
 static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
 {
 	/* We don't handle the NULL dev case for ISA for now. We could
@@ -15,4 +17,23 @@ static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
 	return NULL;
 }
 
+#ifdef CONFIG_ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+/**
+ * dma_addr_is_phys_addr - check whether a device DMA address is a physical
+ *		address
+ * @dev:	device to check
+ *
+ * Returns %true if any DMA address for this device happens to also be a valid
+ * physical address (not necessarily of the same page).
+ */
+static inline bool dma_addr_is_phys_addr(struct device *dev)
+{
+	/*
+	 * Secure guests always use the SWIOTLB, therefore DMA addresses are
+	 * actually the physical address of the bounce buffer.
+	 */
+	return is_secure_guest();
+}
+#endif
+
 #endif	/* _ASM_DMA_MAPPING_H */
arch/powerpc/platforms/pseries/Kconfig +1 -0
index 9e35cdd..0108150 100644
@@ -152,6 +152,7 @@ config PPC_SVM
 	select SWIOTLB
 	select ARCH_HAS_MEM_ENCRYPT
 	select ARCH_HAS_FORCE_DMA_UNENCRYPTED
+	select ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
 	help
 	 There are certain POWER platforms which support secure guests using
 	 the Protected Execution Facility, with the help of an Ultravisor
include/linux/dma-mapping.h +20 -0
index f7d1eea..6df5664 100644
@@ -693,6 +693,26 @@ static inline bool dma_addressing_limited(struct device *dev)
 			    dma_get_required_mask(dev);
 }
 
+#ifndef CONFIG_ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+/**
+ * dma_addr_is_phys_addr - check whether a device DMA address is a physical
+ *		address
+ * @dev:	device to check
+ *
+ * Returns %true if any DMA address for this device happens to also be a valid
+ * physical address (not necessarily of the same page).
+ */
+static inline bool dma_addr_is_phys_addr(struct device *dev)
+{
+	/*
+	 * Except in very specific setups, DMA addresses exist in a different
+	 * address space from CPU physical addresses and cannot be directly used
+	 * to reference system memory.
+	 */
+	return false;
+}
+#endif
+
 #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS
 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 		const struct iommu_ops *iommu, bool coherent);
kernel/dma/Kconfig +3 -0
index 9decbba..6209b46 100644
@@ -51,6 +51,9 @@ config ARCH_HAS_DMA_MMAP_PGPROT
 config ARCH_HAS_FORCE_DMA_UNENCRYPTED
 	bool
 
+config ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+	bool
+
 config DMA_NONCOHERENT_CACHE_SYNC
 	bool
 
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
reviewer
#17 lore raw

Re: [PATCH 1/2] dma-mapping: Add dma_addr_is_phys_addr()

Quoted 24 lines · David Gibson
Quoted 6 lines · Ram Pai

From: Thiago Jung Bauermann <[email protected]>

In order to safely use the DMA API, virtio needs to know whether DMA addresses are in fact physical addresses and for that purpose, dma_addr_is_phys_addr() is introduced.

Quoted 14 lines
cc Benjamin Herrenschmidt <[email protected]>
cc David Gibson <[email protected]>
cc Michael Ellerman <[email protected]>
cc Paul Mackerras <[email protected]>
cc Michael Roth <[email protected]>
cc Alexey Kardashevskiy <[email protected]>
cc Paul Burton <[email protected]>
cc Robin Murphy <[email protected]>
cc Bartlomiej Zolnierkiewicz <[email protected]>
cc Marek Szyprowski <[email protected]>
cc Christoph Hellwig <[email protected]>

The change itself looks ok, so

Quoted 7 lines

However, I would like to see the commit message (and maybe the inline comments) expanded a bit on what the distinction here is about. Some of the text from the next patch would be suitable, about DMA addresses usually being in a different address space but not in the case of bounce buffering.

Right, this needs a much tighter definition. "DMA address happens to be a valid physical address" is true of various IOMMU setups too, but I can't believe it's meaningful in such cases.

If what you actually want is "DMA is direct or SWIOTLB" - i.e. "DMA address is physical address of DMA data (not necessarily the original buffer)" - wouldn't dma_is_direct() suffice?

Robin.

Quoted 102 lines
Quoted 101 lines
---
  arch/powerpc/include/asm/dma-mapping.h | 21 +++++++++++++++++++++
  arch/powerpc/platforms/pseries/Kconfig |  1 +
  include/linux/dma-mapping.h            | 20 ++++++++++++++++++++
  kernel/dma/Kconfig                     |  3 +++
  4 files changed, 45 insertions(+)
arch/powerpc/include/asm/dma-mapping.h +21 -0
index 565d6f7..f92c0a4b 100644
@@ -5,6 +5,8 @@
  #ifndef _ASM_DMA_MAPPING_H
  #define _ASM_DMA_MAPPING_H
 
+#include <asm/svm.h>
+
  static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
  {
  	/* We don't handle the NULL dev case for ISA for now. We could
@@ -15,4 +17,23 @@ static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
  	return NULL;
  }
 
+#ifdef CONFIG_ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+/**
+ * dma_addr_is_phys_addr - check whether a device DMA address is a physical
+ *		address
+ * @dev:	device to check
+ *
+ * Returns %true if any DMA address for this device happens to also be a valid
+ * physical address (not necessarily of the same page).
+ */
+static inline bool dma_addr_is_phys_addr(struct device *dev)
+{
+	/*
+	 * Secure guests always use the SWIOTLB, therefore DMA addresses are
+	 * actually the physical address of the bounce buffer.
+	 */
+	return is_secure_guest();
+}
+#endif
+
  #endif	/* _ASM_DMA_MAPPING_H */
arch/powerpc/platforms/pseries/Kconfig +1 -0
index 9e35cdd..0108150 100644
@@ -152,6 +152,7 @@ config PPC_SVM
  	select SWIOTLB
  	select ARCH_HAS_MEM_ENCRYPT
  	select ARCH_HAS_FORCE_DMA_UNENCRYPTED
+	select ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
  	help
  	 There are certain POWER platforms which support secure guests using
  	 the Protected Execution Facility, with the help of an Ultravisor
include/linux/dma-mapping.h +20 -0
index f7d1eea..6df5664 100644
@@ -693,6 +693,26 @@ static inline bool dma_addressing_limited(struct device *dev)
  			    dma_get_required_mask(dev);
  }
 
+#ifndef CONFIG_ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+/**
+ * dma_addr_is_phys_addr - check whether a device DMA address is a physical
+ *		address
+ * @dev:	device to check
+ *
+ * Returns %true if any DMA address for this device happens to also be a valid
+ * physical address (not necessarily of the same page).
+ */
+static inline bool dma_addr_is_phys_addr(struct device *dev)
+{
+	/*
+	 * Except in very specific setups, DMA addresses exist in a different
+	 * address space from CPU physical addresses and cannot be directly used
+	 * to reference system memory.
+	 */
+	return false;
+}
+#endif
+
  #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS
  void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  		const struct iommu_ops *iommu, bool coherent);
kernel/dma/Kconfig +3 -0
index 9decbba..6209b46 100644
@@ -51,6 +51,9 @@ config ARCH_HAS_DMA_MMAP_PGPROT
  config ARCH_HAS_FORCE_DMA_UNENCRYPTED
  	bool
 
+config ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+	bool
+
  config DMA_NONCOHERENT_CACHE_SYNC
  	bool
 
reviewer
#18 lore raw

Re: [PATCH 1/2] dma-mapping: Add dma_addr_is_phys_addr()

In reply to David Gibson
Quoted 24 lines · David Gibson
Quoted 6 lines · Ram Pai

From: Thiago Jung Bauermann <[email protected]>

In order to safely use the DMA API, virtio needs to know whether DMA addresses are in fact physical addresses and for that purpose, dma_addr_is_phys_addr() is introduced.

Quoted 14 lines
cc Benjamin Herrenschmidt <[email protected]>
cc David Gibson <[email protected]>
cc Michael Ellerman <[email protected]>
cc Paul Mackerras <[email protected]>
cc Michael Roth <[email protected]>
cc Alexey Kardashevskiy <[email protected]>
cc Paul Burton <[email protected]>
cc Robin Murphy <[email protected]>
cc Bartlomiej Zolnierkiewicz <[email protected]>
cc Marek Szyprowski <[email protected]>
cc Christoph Hellwig <[email protected]>

The change itself looks ok, so

Quoted 7 lines

However, I would like to see the commit message (and maybe the inline comments) expanded a bit on what the distinction here is about. Some of the text from the next patch would be suitable, about DMA addresses usually being in a different address space but not in the case of bounce buffering.

Right, this needs a much tighter definition. "DMA address happens to be a valid physical address" is true of various IOMMU setups too, but I can't believe it's meaningful in such cases.

If what you actually want is "DMA is direct or SWIOTLB" - i.e. "DMA address is physical address of DMA data (not necessarily the original buffer)" - wouldn't dma_is_direct() suffice?

Robin.

Quoted 102 lines
Quoted 101 lines
---
  arch/powerpc/include/asm/dma-mapping.h | 21 +++++++++++++++++++++
  arch/powerpc/platforms/pseries/Kconfig |  1 +
  include/linux/dma-mapping.h            | 20 ++++++++++++++++++++
  kernel/dma/Kconfig                     |  3 +++
  4 files changed, 45 insertions(+)
arch/powerpc/include/asm/dma-mapping.h +21 -0
index 565d6f7..f92c0a4b 100644
@@ -5,6 +5,8 @@
  #ifndef _ASM_DMA_MAPPING_H
  #define _ASM_DMA_MAPPING_H
 
+#include <asm/svm.h>
+
  static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
  {
  	/* We don't handle the NULL dev case for ISA for now. We could
@@ -15,4 +17,23 @@ static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
  	return NULL;
  }
 
+#ifdef CONFIG_ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+/**
+ * dma_addr_is_phys_addr - check whether a device DMA address is a physical
+ *		address
+ * @dev:	device to check
+ *
+ * Returns %true if any DMA address for this device happens to also be a valid
+ * physical address (not necessarily of the same page).
+ */
+static inline bool dma_addr_is_phys_addr(struct device *dev)
+{
+	/*
+	 * Secure guests always use the SWIOTLB, therefore DMA addresses are
+	 * actually the physical address of the bounce buffer.
+	 */
+	return is_secure_guest();
+}
+#endif
+
  #endif	/* _ASM_DMA_MAPPING_H */
arch/powerpc/platforms/pseries/Kconfig +1 -0
index 9e35cdd..0108150 100644
@@ -152,6 +152,7 @@ config PPC_SVM
  	select SWIOTLB
  	select ARCH_HAS_MEM_ENCRYPT
  	select ARCH_HAS_FORCE_DMA_UNENCRYPTED
+	select ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
  	help
  	 There are certain POWER platforms which support secure guests using
  	 the Protected Execution Facility, with the help of an Ultravisor
include/linux/dma-mapping.h +20 -0
index f7d1eea..6df5664 100644
@@ -693,6 +693,26 @@ static inline bool dma_addressing_limited(struct device *dev)
  			    dma_get_required_mask(dev);
  }
 
+#ifndef CONFIG_ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+/**
+ * dma_addr_is_phys_addr - check whether a device DMA address is a physical
+ *		address
+ * @dev:	device to check
+ *
+ * Returns %true if any DMA address for this device happens to also be a valid
+ * physical address (not necessarily of the same page).
+ */
+static inline bool dma_addr_is_phys_addr(struct device *dev)
+{
+	/*
+	 * Except in very specific setups, DMA addresses exist in a different
+	 * address space from CPU physical addresses and cannot be directly used
+	 * to reference system memory.
+	 */
+	return false;
+}
+#endif
+
  #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS
  void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  		const struct iommu_ops *iommu, bool coherent);
kernel/dma/Kconfig +3 -0
index 9decbba..6209b46 100644
@@ -51,6 +51,9 @@ config ARCH_HAS_DMA_MMAP_PGPROT
  config ARCH_HAS_FORCE_DMA_UNENCRYPTED
  	bool
 
+config ARCH_HAS_DMA_ADDR_IS_PHYS_ADDR
+	bool
+
  config DMA_NONCOHERENT_CACHE_SYNC
  	bool
 
author
#19 lore raw

RE: [PATCH 1/2] dma-mapping: Add dma_addr_is_phys_addr()

Quoted 33 lines · Robin Murphy
Quoted 24 lines · David Gibson
Quoted 6 lines · Ram Pai

From: Thiago Jung Bauermann <[email protected]>

In order to safely use the DMA API, virtio needs to know whether DMA addresses are in fact physical addresses and for that purpose, dma_addr_is_phys_addr() is introduced.

Quoted 14 lines
cc Benjamin Herrenschmidt <[email protected]>
cc David Gibson <[email protected]>
cc Michael Ellerman <[email protected]>
cc Paul Mackerras <[email protected]>
cc Michael Roth <[email protected]>
cc Alexey Kardashevskiy <[email protected]>
cc Paul Burton <[email protected]>
cc Robin Murphy <[email protected]>
cc Bartlomiej Zolnierkiewicz <[email protected]>
cc Marek Szyprowski <[email protected]>
cc Christoph Hellwig <[email protected]>

The change itself looks ok, so

Quoted 7 lines

However, I would like to see the commit message (and maybe the inline comments) expanded a bit on what the distinction here is about. Some of the text from the next patch would be suitable, about DMA addresses usually being in a different address space but not in the case of bounce buffering.

Quoted 3 lines

Right, this needs a much tighter definition. "DMA address happens to be a valid physical address" is true of various IOMMU setups too, but I can't believe it's meaningful in such cases.

The definition by itself is meaningful AFAICT. At its core its just indicating whether DMA addresses are physical addresses or not.

However its up to the caller to use it meaningfully. For non-virtio caller, dma_addr_is_phys_addr() by itself may or may not be meaningful.

But for a virtio caller, this information when paired with mem_encrypt_active() is meaningful.

For IOMMU setups DMA API will get used regardless of "DMA address happens to be a valid physical address"

Quoted 4 lines

If what you actually want is "DMA is direct or SWIOTLB" - i.e. "DMA address is physical address of DMA data (not necessarily the original buffer)" - wouldn't dma_is_direct() suffice?

This may also work, I think. MST: thoughts?

RP

#20 lore raw

Re: [PATCH 1/2] dma-mapping: Add dma_addr_is_phys_addr()

In reply to Robin Murphy
Quoted 6 lines · Robin Murphy
Quoted 5 lines

However, I would like to see the commit message (and maybe the inline comments) expanded a bit on what the distinction here is about. Some of the text from the next patch would be suitable, about DMA addresses usually being in a different address space but not in the case of bounce buffering.

Quoted 7 lines

Right, this needs a much tighter definition. "DMA address happens to be a valid physical address" is true of various IOMMU setups too, but I can't believe it's meaningful in such cases.

If what you actually want is "DMA is direct or SWIOTLB" - i.e. "DMA address is physical address of DMA data (not necessarily the original buffer)" - wouldn't dma_is_direct() suffice?

It would. But drivers have absolutely no business knowing any of this.

#21 lore raw

Re: [PATCH 1/2] dma-mapping: Add dma_addr_is_phys_addr()

In reply to Robin Murphy
Quoted 6 lines · Robin Murphy
Quoted 5 lines

However, I would like to see the commit message (and maybe the inline comments) expanded a bit on what the distinction here is about. Some of the text from the next patch would be suitable, about DMA addresses usually being in a different address space but not in the case of bounce buffering.

Quoted 7 lines

Right, this needs a much tighter definition. "DMA address happens to be a valid physical address" is true of various IOMMU setups too, but I can't believe it's meaningful in such cases.

If what you actually want is "DMA is direct or SWIOTLB" - i.e. "DMA address is physical address of DMA data (not necessarily the original buffer)" - wouldn't dma_is_direct() suffice?

It would. But drivers have absolutely no business knowing any of this.

author
#22 lore raw
In reply to Ram Pai

Hmm.. git-send-email forgot to CC Michael Tsirkin, and Thiago; the original author, who is on vacation.

Adding them now.
RP

Quoted 62 lines · Ram Pai

**We would like the patches to be merged through the virtio tree. Please review, and ack merging the DMA mapping change through that tree. Thanks!**

The memory of powerpc secure guests can't be accessed by the hypervisor / virtio device except for a few memory regions designated as 'shared'.

At the moment, Linux uses bounce-buffering to communicate with the hypervisor, with a bounce buffer marked as shared. This is how the DMA API is implemented on this platform.

In particular, the most convenient way to use virtio on this platform is by making virtio use the DMA API: in fact, this is exactly what happens if the virtio device exposes the flag VIRTIO_F_ACCESS_PLATFORM. However, bugs in the hypervisor on the powerpc platform do not allow setting this flag, with some hypervisors already in the field that don't set this flag. At the moment they are forced to use emulated devices when guest is in secure mode; virtio is only useful when guest is not secure.

Normally, both device and driver must support VIRTIO_F_ACCESS_PLATFORM:
if one of them doesn't, the other mustn't assume it for communication to work.

However, a guest-side work-around is possible to enable virtio for these hypervisors with guest in secure mode: it so happens that on powerpc secure platform the DMA address is actually a physical address - that of the bounce buffer. For these platforms we can make the virtio driver go through the DMA API even though the device itself ignores the DMA API.

These patches implement this work around for virtio: we detect that
- secure guest mode is enabled - so we know that since we don't share most memory and Hypervisor has not enabled VIRTIO_F_ACCESS_PLATFORM, regular virtio code won't work.
- DMA API is giving us addresses that are actually also physical addresses.
- Hypervisor has not enabled VIRTIO_F_ACCESS_PLATFORM.

and if all conditions are true, we force all data through the bounce buffer.

To put it another way, from hypervisor's point of view DMA API is not required: hypervisor would be happy to get access to all of guest memory. That's why it does not set VIRTIO_F_ACCESS_PLATFORM. However, guest decides that it does not trust the hypervisor and wants to force a bounce buffer for its own reasons.

Thiago Jung Bauermann (2):
dma-mapping: Add dma_addr_is_phys_addr()
virtio_ring: Use DMA API if memory is encrypted

 arch/powerpc/include/asm/dma-mapping.h | 21 +++++++++++++++++++++
 arch/powerpc/platforms/pseries/Kconfig |  1 +
 drivers/virtio/virtio.c                | 18 ++++++++++++++++++
 drivers/virtio/virtio_ring.c           |  8 ++++++++
 include/linux/dma-mapping.h            | 20 ++++++++++++++++++++
 include/linux/virtio_config.h          | 14 ++++++++++++++
 kernel/dma/Kconfig                     |  3 +++
 7 files changed, 85 insertions(+)
Signature · 1 line
1.8.3.1
Signature · 1 line
Ram Pai
author
#23 lore raw
In reply to Ram Pai

Hmm.. git-send-email forgot to CC Michael Tsirkin, and Thiago; the original author, who is on vacation.

Adding them now.
RP

Quoted 62 lines · Ram Pai

**We would like the patches to be merged through the virtio tree. Please review, and ack merging the DMA mapping change through that tree. Thanks!**

The memory of powerpc secure guests can't be accessed by the hypervisor / virtio device except for a few memory regions designated as 'shared'.

At the moment, Linux uses bounce-buffering to communicate with the hypervisor, with a bounce buffer marked as shared. This is how the DMA API is implemented on this platform.

In particular, the most convenient way to use virtio on this platform is by making virtio use the DMA API: in fact, this is exactly what happens if the virtio device exposes the flag VIRTIO_F_ACCESS_PLATFORM. However, bugs in the hypervisor on the powerpc platform do not allow setting this flag, with some hypervisors already in the field that don't set this flag. At the moment they are forced to use emulated devices when guest is in secure mode; virtio is only useful when guest is not secure.

Normally, both device and driver must support VIRTIO_F_ACCESS_PLATFORM:
if one of them doesn't, the other mustn't assume it for communication to work.

However, a guest-side work-around is possible to enable virtio for these hypervisors with guest in secure mode: it so happens that on powerpc secure platform the DMA address is actually a physical address - that of the bounce buffer. For these platforms we can make the virtio driver go through the DMA API even though the device itself ignores the DMA API.

These patches implement this work around for virtio: we detect that
- secure guest mode is enabled - so we know that since we don't share most memory and Hypervisor has not enabled VIRTIO_F_ACCESS_PLATFORM, regular virtio code won't work.
- DMA API is giving us addresses that are actually also physical addresses.
- Hypervisor has not enabled VIRTIO_F_ACCESS_PLATFORM.

and if all conditions are true, we force all data through the bounce buffer.

To put it another way, from hypervisor's point of view DMA API is not required: hypervisor would be happy to get access to all of guest memory. That's why it does not set VIRTIO_F_ACCESS_PLATFORM. However, guest decides that it does not trust the hypervisor and wants to force a bounce buffer for its own reasons.

Thiago Jung Bauermann (2):
dma-mapping: Add dma_addr_is_phys_addr()
virtio_ring: Use DMA API if memory is encrypted

 arch/powerpc/include/asm/dma-mapping.h | 21 +++++++++++++++++++++
 arch/powerpc/platforms/pseries/Kconfig |  1 +
 drivers/virtio/virtio.c                | 18 ++++++++++++++++++
 drivers/virtio/virtio_ring.c           |  8 ++++++++
 include/linux/dma-mapping.h            | 20 ++++++++++++++++++++
 include/linux/virtio_config.h          | 14 ++++++++++++++
 kernel/dma/Kconfig                     |  3 +++
 7 files changed, 85 insertions(+)
Signature · 1 line
1.8.3.1
Signature · 1 line
Ram Pai