From: "Michael S. Tsirkin" <mst@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: "Cong Wang" <xiyou.wangcong@gmail.com>,
"Jonathan Corbet" <corbet@lwn.net>,
"Olivia Mackall" <olivia@selenic.com>,
"Herbert Xu" <herbert@gondor.apana.org.au>,
"Jason Wang" <jasowang@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Stefan Hajnoczi" <stefanha@redhat.com>,
"Eugenio Pérez" <eperezma@redhat.com>,
"James E.J. Bottomley" <James.Bottomley@hansenpartnership.com>,
"Martin K. Petersen" <martin.petersen@oracle.com>,
"Gerd Hoffmann" <kraxel@redhat.com>,
"Xuan Zhuo" <xuanzhuo@linux.alibaba.com>,
"Marek Szyprowski" <m.szyprowski@samsung.com>,
"Robin Murphy" <robin.murphy@arm.com>,
"Stefano Garzarella" <sgarzare@redhat.com>,
"David S. Miller" <davem@davemloft.net>,
"Eric Dumazet" <edumazet@google.com>,
"Jakub Kicinski" <kuba@kernel.org>,
"Paolo Abeni" <pabeni@redhat.com>,
"Simon Horman" <horms@kernel.org>,
"Petr Tesarik" <ptesarik@suse.com>,
"Leon Romanovsky" <leon@kernel.org>,
"Jason Gunthorpe" <jgg@ziepe.ca>,
linux-doc@vger.kernel.org, linux-crypto@vger.kernel.org,
virtualization@lists.linux.dev, linux-scsi@vger.kernel.org,
iommu@lists.linux.dev, kvm@vger.kernel.org,
netdev@vger.kernel.org
Subject: [PATCH RFC 10/13] virtio_scsi: fix DMA cacheline issues for events
Date: Tue, 30 Dec 2025 05:16:18 -0500 [thread overview]
Message-ID: <c238ea28521dc0433350f848361b46e7d451b96c.1767089672.git.mst@redhat.com> (raw)
In-Reply-To: <cover.1767089672.git.mst@redhat.com>
Current struct virtio_scsi_event_node layout has two problems:
The event (DMA_FROM_DEVICE) and work (CPU-written via
INIT_WORK/queue_work) fields share a cacheline.
On non-cache-coherent platforms, CPU writes to work can
corrupt device-written event data.
If DMA_MIN_ALIGN is large enough, the 8 events in event_list share
cachelines, triggering CONFIG_DMA_API_DEBUG warnings.
Fix the corruption by moving event buffers to a separate array and
aligning using __dma_from_device_aligned_begin/end.
Suppress the (now spurious) DMA debug warnings using
virtqueue_add_inbuf_cache_clean().
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
drivers/scsi/virtio_scsi.c | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)
diff --git a/drivers/scsi/virtio_scsi.c b/drivers/scsi/virtio_scsi.c
index 96a69edddbe5..b0ce3884e22a 100644
--- a/drivers/scsi/virtio_scsi.c
+++ b/drivers/scsi/virtio_scsi.c
@@ -29,6 +29,7 @@
#include <scsi/scsi_tcq.h>
#include <scsi/scsi_devinfo.h>
#include <linux/seqlock.h>
+#include <linux/dma-mapping.h>
#include "sd.h"
@@ -61,7 +62,7 @@ struct virtio_scsi_cmd {
struct virtio_scsi_event_node {
struct virtio_scsi *vscsi;
- struct virtio_scsi_event event;
+ struct virtio_scsi_event *event;
struct work_struct work;
};
@@ -89,6 +90,12 @@ struct virtio_scsi {
struct virtio_scsi_vq ctrl_vq;
struct virtio_scsi_vq event_vq;
+
+ /* DMA buffers for events - aligned, kept separate from CPU-written fields */
+ __dma_from_device_aligned_begin
+ struct virtio_scsi_event events[VIRTIO_SCSI_EVENT_LEN];
+ __dma_from_device_aligned_end
+
struct virtio_scsi_vq req_vqs[];
};
@@ -237,12 +244,12 @@ static int virtscsi_kick_event(struct virtio_scsi *vscsi,
unsigned long flags;
INIT_WORK(&event_node->work, virtscsi_handle_event);
- sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
+ sg_init_one(&sg, event_node->event, sizeof(struct virtio_scsi_event));
spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
- err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node,
- GFP_ATOMIC);
+ err = virtqueue_add_inbuf_cache_clean(vscsi->event_vq.vq, &sg, 1, event_node,
+ GFP_ATOMIC);
if (!err)
virtqueue_kick(vscsi->event_vq.vq);
@@ -257,6 +264,7 @@ static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) {
vscsi->event_list[i].vscsi = vscsi;
+ vscsi->event_list[i].event = &vscsi->events[i];
virtscsi_kick_event(vscsi, &vscsi->event_list[i]);
}
@@ -380,7 +388,7 @@ static void virtscsi_handle_event(struct work_struct *work)
struct virtio_scsi_event_node *event_node =
container_of(work, struct virtio_scsi_event_node, work);
struct virtio_scsi *vscsi = event_node->vscsi;
- struct virtio_scsi_event *event = &event_node->event;
+ struct virtio_scsi_event *event = event_node->event;
if (event->event &
cpu_to_virtio32(vscsi->vdev, VIRTIO_SCSI_T_EVENTS_MISSED)) {
--
MST
next prev parent reply other threads:[~2025-12-30 10:16 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-30 10:15 [PATCH RFC 00/13] fix DMA aligment issues around virtio Michael S. Tsirkin
2025-12-30 10:15 ` [PATCH RFC 01/13] dma-mapping: add __dma_from_device_align_begin/end Michael S. Tsirkin
2025-12-31 14:01 ` Petr Tesarik
2025-12-31 14:40 ` Michael S. Tsirkin
2025-12-31 20:48 ` Michael S. Tsirkin
2026-01-02 8:14 ` Petr Tesarik
2025-12-30 10:15 ` [PATCH RFC 02/13] docs: dma-api: document __dma_align_begin/end Michael S. Tsirkin
2025-12-30 10:15 ` [PATCH RFC 03/13] dma-mapping: add DMA_ATTR_CPU_CACHE_CLEAN Michael S. Tsirkin
2025-12-30 10:15 ` [PATCH RFC 04/13] docs: dma-api: document DMA_ATTR_CPU_CACHE_CLEAN Michael S. Tsirkin
2025-12-30 10:16 ` [PATCH RFC 05/13] dma-debug: track cache clean flag in entries Michael S. Tsirkin
2026-01-02 7:59 ` Petr Tesarik
2026-01-02 11:17 ` Michael S. Tsirkin
2025-12-30 10:16 ` [PATCH RFC 06/13] virtio: add virtqueue_add_inbuf_cache_clean API Michael S. Tsirkin
2025-12-30 10:16 ` [PATCH RFC 07/13] vsock/virtio: fix DMA alignment for event_list Michael S. Tsirkin
2025-12-30 10:16 ` [PATCH RFC 08/13] vsock/virtio: use virtqueue_add_inbuf_cache_clean for events Michael S. Tsirkin
2025-12-30 10:16 ` [PATCH RFC 09/13] virtio_input: fix DMA alignment for evts Michael S. Tsirkin
2025-12-30 10:16 ` Michael S. Tsirkin [this message]
2025-12-30 10:16 ` [PATCH RFC 11/13] virtio-rng: fix DMA alignment for data buffer Michael S. Tsirkin
2025-12-30 10:16 ` [PATCH RFC 12/13] virtio_input: use virtqueue_add_inbuf_cache_clean for events Michael S. Tsirkin
2025-12-30 10:16 ` [PATCH RFC 13/13] vsock/virtio: reorder fields to reduce struct padding Michael S. Tsirkin
2025-12-30 16:40 ` [PATCH RFC 14/13] gpio: virtio: fix DMA alignment Michael S. Tsirkin
2026-01-02 12:46 ` Bartosz Golaszewski
2026-01-05 4:56 ` Viresh Kumar
2025-12-30 16:40 ` [PATCH RFC 15/13] gpio: virtio: reorder fields to reduce struct padding Michael S. Tsirkin
2026-01-02 12:47 ` Bartosz Golaszewski
2026-01-02 13:02 ` Michael S. Tsirkin
2026-01-02 13:27 ` Bartosz Golaszewski
2026-01-05 4:57 ` Viresh Kumar
2025-12-31 13:12 ` [PATCH RFC 00/13] fix DMA aligment issues around virtio Petr Tesarik
2025-12-31 14:42 ` Michael S. Tsirkin
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=c238ea28521dc0433350f848361b46e7d451b96c.1767089672.git.mst@redhat.com \
--to=mst@redhat.com \
--cc=James.Bottomley@hansenpartnership.com \
--cc=corbet@lwn.net \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=eperezma@redhat.com \
--cc=herbert@gondor.apana.org.au \
--cc=horms@kernel.org \
--cc=iommu@lists.linux.dev \
--cc=jasowang@redhat.com \
--cc=jgg@ziepe.ca \
--cc=kraxel@redhat.com \
--cc=kuba@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=leon@kernel.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=m.szyprowski@samsung.com \
--cc=martin.petersen@oracle.com \
--cc=netdev@vger.kernel.org \
--cc=olivia@selenic.com \
--cc=pabeni@redhat.com \
--cc=pbonzini@redhat.com \
--cc=ptesarik@suse.com \
--cc=robin.murphy@arm.com \
--cc=sgarzare@redhat.com \
--cc=stefanha@redhat.com \
--cc=virtualization@lists.linux.dev \
--cc=xiyou.wangcong@gmail.com \
--cc=xuanzhuo@linux.alibaba.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
all inboxes | Powered by JetHome®