Skip to content

Commit bd2b617

Browse files
committed
virtio-rng: fix DMA alignment for data buffer
The data buffer in struct virtrng_info is used for DMA_FROM_DEVICE via virtqueue_add_inbuf() and shares cachelines with the adjacent CPU-written fields (data_avail, data_idx). The device writing to the DMA buffer and the CPU writing to adjacent fields could corrupt each other's data on non-cache-coherent platforms. Add __dma_from_device_group_begin()/end() annotations to place these in distinct cache lines. Message-ID: <157a63b6324d1f1307ddd4faa3b62a8b90a79423.1767601130.git.mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent 2678369 commit bd2b617

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

drivers/char/hw_random/virtio-rng.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/spinlock.h>
1212
#include <linux/virtio.h>
1313
#include <linux/virtio_rng.h>
14+
#include <linux/dma-mapping.h>
1415
#include <linux/module.h>
1516
#include <linux/slab.h>
1617

@@ -28,11 +29,13 @@ struct virtrng_info {
2829
unsigned int data_avail;
2930
unsigned int data_idx;
3031
/* minimal size returned by rng_buffer_size() */
32+
__dma_from_device_group_begin();
3133
#if SMP_CACHE_BYTES < 32
3234
u8 data[32];
3335
#else
3436
u8 data[SMP_CACHE_BYTES];
3537
#endif
38+
__dma_from_device_group_end();
3639
};
3740

3841
static void random_recv_done(struct virtqueue *vq)

0 commit comments

Comments
 (0)