mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH RFC 09/13] virtio_input: fix DMA cacheline alignment for evts
       [not found] <cover.1767089257.git.mst@redhat.com>
@ 2025-12-30 10:16 ` Michael S. Tsirkin
  2025-12-30 10:16 ` [PATCH RFC 11/13] virtio-rng: fix DMA cacheline alignment for data buffer Michael S. Tsirkin
  1 sibling, 0 replies; 2+ messages in thread
From: Michael S. Tsirkin @ 2025-12-30 10:16 UTC (permalink / raw)
  To: linux-kernel
  Cc: Cong Wang, Jonathan Corbet, Olivia Mackall, Herbert Xu,
	Jason Wang, Paolo Bonzini, Stefan Hajnoczi, Eugenio Pérez,
	James E.J. Bottomley, Martin K. Petersen, Gerd Hoffmann,
	Xuan Zhuo, Marek Szyprowski, Robin Murphy, Stefano Garzarella,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Petr Tesarik, Leon Romanovsky, Jason Gunthorpe,
	linux-doc, linux-crypto, virtualization, linux-scsi, iommu, kvm,
	netdev

On non-cache-coherent platforms, when a structure contains a buffer
used for DMA alongside fields that the CPU writes to, cacheline sharing
can cause data corruption.

The evts array is used for DMA_FROM_DEVICE operations via
virtqueue_add_inbuf(). The adjacent lock and ready fields are written
by the CPU during normal operation. If these share cachelines with evts,
CPU writes can corrupt DMA data.

Add __dma_from_device_aligned_begin/end annotations to ensure evts is
isolated in its own cachelines.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 drivers/virtio/virtio_input.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/virtio/virtio_input.c b/drivers/virtio/virtio_input.c
index d0728285b6ce..774494754a99 100644
--- a/drivers/virtio/virtio_input.c
+++ b/drivers/virtio/virtio_input.c
@@ -4,6 +4,7 @@
 #include <linux/virtio_config.h>
 #include <linux/input.h>
 #include <linux/slab.h>
+#include <linux/dma-mapping.h>
 
 #include <uapi/linux/virtio_ids.h>
 #include <uapi/linux/virtio_input.h>
@@ -16,7 +17,9 @@ struct virtio_input {
 	char                       serial[64];
 	char                       phys[64];
 	struct virtqueue           *evt, *sts;
+	__dma_from_device_aligned_begin
 	struct virtio_input_event  evts[64];
+	__dma_from_device_aligned_end
 	spinlock_t                 lock;
 	bool                       ready;
 };
-- 
MST


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH RFC 11/13] virtio-rng: fix DMA cacheline alignment for data buffer
       [not found] <cover.1767089257.git.mst@redhat.com>
  2025-12-30 10:16 ` [PATCH RFC 09/13] virtio_input: fix DMA cacheline alignment for evts Michael S. Tsirkin
@ 2025-12-30 10:16 ` Michael S. Tsirkin
  1 sibling, 0 replies; 2+ messages in thread
From: Michael S. Tsirkin @ 2025-12-30 10:16 UTC (permalink / raw)
  To: linux-kernel
  Cc: Cong Wang, Jonathan Corbet, Olivia Mackall, Herbert Xu,
	Jason Wang, Paolo Bonzini, Stefan Hajnoczi, Eugenio Pérez,
	James E.J. Bottomley, Martin K. Petersen, Gerd Hoffmann,
	Xuan Zhuo, Marek Szyprowski, Robin Murphy, Stefano Garzarella,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Petr Tesarik, Leon Romanovsky, Jason Gunthorpe,
	linux-doc, linux-crypto, virtualization, linux-scsi, iommu, kvm,
	netdev

Add __dma_from_device_aligned_begin annotation before the data buffer
in struct virtrng_info to ensure proper cacheline alignment on
non-cache-coherent platforms.

The data buffer is used for DMA_FROM_DEVICE via virtqueue_add_inbuf()
and is adjacent to CPU-written fields (data_avail, data_idx). Without
proper alignment, the device writing to the DMA buffer and the CPU
writing to adjacent fields could corrupt each other's data on
platforms where DMA cache maintenance is at cacheline granularity.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 drivers/char/hw_random/virtio-rng.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c
index dd998f4fe4f2..fb3c57bee3b1 100644
--- a/drivers/char/hw_random/virtio-rng.c
+++ b/drivers/char/hw_random/virtio-rng.c
@@ -11,6 +11,7 @@
 #include <linux/spinlock.h>
 #include <linux/virtio.h>
 #include <linux/virtio_rng.h>
+#include <linux/dma-mapping.h>
 #include <linux/module.h>
 #include <linux/slab.h>
 
@@ -28,6 +29,7 @@ struct virtrng_info {
 	unsigned int data_avail;
 	unsigned int data_idx;
 	/* minimal size returned by rng_buffer_size() */
+	__dma_from_device_aligned_begin
 #if SMP_CACHE_BYTES < 32
 	u8 data[32];
 #else
-- 
MST


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-12-30 10:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <cover.1767089257.git.mst@redhat.com>
2025-12-30 10:16 ` [PATCH RFC 09/13] virtio_input: fix DMA cacheline alignment for evts Michael S. Tsirkin
2025-12-30 10:16 ` [PATCH RFC 11/13] virtio-rng: fix DMA cacheline alignment for data buffer Michael S. Tsirkin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®