devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Robin Murphy <robin.murphy@arm.com>
To: mst@redhat.com, jasowang@redhat.com
Cc: mark.rutland@arm.com, devicetree@vger.kernel.org,
	pawel.moll@arm.com, will.deacon@arm.com,
	virtualization@lists.linux-foundation.org, robh+dt@kernel.org,
	linux-arm-kernel@lists.infradead.org
Subject: [PATCH] virtio: Try to untangle DMA coherency
Date: Wed,  1 Feb 2017 12:25:57 +0000	[thread overview]
Message-ID: <8a6494f6409c20b4609cd6bdcdd751f68b5c0564.1485951731.git.robin.murphy@arm.com> (raw)

By forcing on DMA API usage for ARM systems, we have inadvertently
kicked open a hornets' nest in terms of cache-coherency. Namely that
unless the virtio device is explicitly described as capable of coherent
DMA by firmware, the DMA APIs on ARM and other DT-based platforms will
assume it is non-coherent. This turns out to cause a big problem for the
likes of QEMU and kvmtool, which generate virtio-mmio devices in their
guest DTs but neglect to add the often-overlooked "dma-coherent"
property; as a result, we end up with the guest making non-cacheable
accesses to the vring, the host doing so cacheably, both talking past
each other and things going horribly wrong.

To prevent regressing those existing use cases relying on implicit
coherency, but still fixing the original problem of (coherent PCI)
legacy devices behind IOMMUs, take the more conservative approach of
only hitting the DMA API switch for coherent devices, where we can be
sure it is safe, and preserving the old non-DMA behaviour otherwise.
This does not affect devices setting the VIRTIO_F_IOMMU_PLATFORM flag,
which as before are still at the mercy of architecture code correctly
knowing their coherency, so explicitly call this out in the virtio-mmio
DT binding in the hope of heading off any further workarounds for future
firmware mishaps.

Fixes: c7070619f340 ("vring: Force use of DMA API for ARM-based systems with legacy devices")
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
---
 Documentation/devicetree/bindings/virtio/mmio.txt |  3 +++
 drivers/virtio/virtio_ring.c                      | 11 ++++++++---
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/Documentation/devicetree/bindings/virtio/mmio.txt b/Documentation/devicetree/bindings/virtio/mmio.txt
index 5069c1b8e193..8f2a981e1010 100644
--- a/Documentation/devicetree/bindings/virtio/mmio.txt
+++ b/Documentation/devicetree/bindings/virtio/mmio.txt
@@ -7,6 +7,8 @@ Required properties:
 - compatible:	"virtio,mmio" compatibility string
 - reg:		control registers base address and size including configuration space
 - interrupts:	interrupt generated by the device
+- dma-coherent:	required if the device (or host emulation) accesses memory
+		cache-coherently, absent otherwise
 
 Example:
 
@@ -14,4 +16,5 @@ Example:
 		compatible = "virtio,mmio";
 		reg = <0x3000 0x100>;
 		interrupts = <41>;
+		dma-coherent;
 	}
diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 7e38ed79c3fc..961af25b385c 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -20,6 +20,7 @@
 #include <linux/virtio_ring.h>
 #include <linux/virtio_config.h>
 #include <linux/device.h>
+#include <linux/property.h>
 #include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/hrtimer.h>
@@ -160,10 +161,14 @@ static bool vring_use_dma_api(struct virtio_device *vdev)
 		return true;
 
 	/*
-	 * On ARM-based machines, the DMA ops will do the right thing,
-	 * so always use them with legacy devices.
+	 * On ARM-based machines, the coherent DMA ops will do the right
+	 * thing, so always use them with legacy devices. However, using
+	 * non-coherent DMA when the host *is* actually coherent, but has
+	 * forgotten to tell us, is going to break badly; since this situation
+	 * already exists in the wild, maintain the old behaviour there.
 	 */
-	if (IS_ENABLED(CONFIG_ARM) || IS_ENABLED(CONFIG_ARM64))
+	if ((IS_ENABLED(CONFIG_ARM) || IS_ENABLED(CONFIG_ARM64)) &&
+	    device_get_dma_attr(&vdev->dev) == DEV_DMA_COHERENT)
 		return !virtio_has_feature(vdev, VIRTIO_F_VERSION_1);
 
 	return false;
-- 
2.11.0.dirty

             reply	other threads:[~2017-02-01 12:25 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-01 12:25 Robin Murphy [this message]
2017-02-01 14:57 ` [PATCH] virtio: Try to untangle DMA coherency Will Deacon
     [not found]   ` <20170201145711.GC8177-5wv7dgnIgG8@public.gmane.org>
2017-02-01 17:58     ` Rob Herring
     [not found] ` <8a6494f6409c20b4609cd6bdcdd751f68b5c0564.1485951731.git.robin.murphy-5wv7dgnIgG8@public.gmane.org>
2017-02-01 18:09   ` Michael S. Tsirkin
2017-02-01 18:27     ` Will Deacon
     [not found]       ` <20170201182659.GM8177-5wv7dgnIgG8@public.gmane.org>
2017-02-01 19:19         ` Michael S. Tsirkin
     [not found]           ` <20170201210648-mutt-send-email-mst-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2017-02-02 11:26             ` Will Deacon
2017-02-02 13:34               ` Robin Murphy
     [not found]                 ` <bb041fbe-3fde-0dc1-ae28-cca5acfe9dc5-5wv7dgnIgG8@public.gmane.org>
2017-02-02 16:16                   ` Michael S. Tsirkin
     [not found]                     ` <20170202181357-mutt-send-email-mst-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2017-02-02 16:39                       ` Marc Zyngier
     [not found]                         ` <d9732885-7cd2-461c-6812-37581294b47f-5wv7dgnIgG8@public.gmane.org>
2017-02-02 16:44                           ` Michael S. Tsirkin
2017-02-02 16:30                   ` Michael S. Tsirkin
2017-02-02 16:40                     ` Will Deacon
     [not found]                       ` <20170202164049.GI13839-5wv7dgnIgG8@public.gmane.org>
2017-02-02 16:45                         ` Michael S. Tsirkin
2017-02-09 18:17                       ` Michael S. Tsirkin
2017-02-09 18:31                         ` Will Deacon
2017-02-09 18:49                           ` Michael S. Tsirkin
2017-02-09 18:54                             ` Ard Biesheuvel
2017-02-09 18:56                             ` Will Deacon
2017-02-10 17:16                           ` Michael S. Tsirkin
2017-02-13 11:57                             ` Will Deacon
2017-02-08 12:58           ` Alexander Graf
2017-02-09 20:57             ` Michael S. Tsirkin
2017-02-09 22:20               ` Alexander Graf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8a6494f6409c20b4609cd6bdcdd751f68b5c0564.1485951731.git.robin.murphy@arm.com \
    --to=robin.murphy@arm.com \
    --cc=devicetree@vger.kernel.org \
    --cc=jasowang@redhat.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=mark.rutland@arm.com \
    --cc=mst@redhat.com \
    --cc=pawel.moll@arm.com \
    --cc=robh+dt@kernel.org \
    --cc=virtualization@lists.linux-foundation.org \
    --cc=will.deacon@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).