Skip to content

Commit 29615fe

Browse files
committed
gpio: virtio: fix DMA alignment
The res and ires buffers in struct virtio_gpio_line and struct vgpio_irq_line respectively are used for DMA_FROM_DEVICE via virtqueue_add_sgs(). However, within these structs, even though these elements are tagged as ____cacheline_aligned, adjacent struct elements can share DMA cachelines on platforms where ARCH_DMA_MINALIGN > L1_CACHE_BYTES (e.g., arm64 with 128-byte DMA alignment but 64-byte cache lines). The existing ____cacheline_aligned annotation aligns to L1_CACHE_BYTES which is not always sufficient for DMA alignment. For example, with L1_CACHE_BYTES = 32 and ARCH_DMA_MINALIGN = 128 - irq_lines[0].ires at offset 128 - irq_lines[1].type at offset 192 both in same 128-byte DMA cacheline [128-256) When the device writes to irq_lines[0].ires and the CPU concurrently modifies one of irq_lines[1].type/disabled/masked/queued flags, corruption can occur on non-cache-coherent platforms. Fix by using __dma_from_device_group_begin()/end() annotations on the DMA buffers. Drop ____cacheline_aligned - it's not required to isolate request and response, and keeping them would increase the memory cost. Acked-by: Viresh Kumar <viresh.kumar@linaro.org> Message-ID: <ba7e025a6c84aed012421468d83639e5dae982b0.1767601130.git.mst@redhat.com> Acked-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent f9108de commit 29615fe

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

drivers/gpio/gpio-virtio.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
#include <linux/completion.h>
13+
#include <linux/dma-mapping.h>
1314
#include <linux/err.h>
1415
#include <linux/gpio/driver.h>
1516
#include <linux/io.h>
@@ -24,8 +25,11 @@
2425
struct virtio_gpio_line {
2526
struct mutex lock; /* Protects line operation */
2627
struct completion completion;
27-
struct virtio_gpio_request req ____cacheline_aligned;
28-
struct virtio_gpio_response res ____cacheline_aligned;
28+
29+
__dma_from_device_group_begin();
30+
struct virtio_gpio_request req;
31+
struct virtio_gpio_response res;
32+
__dma_from_device_group_end();
2933
unsigned int rxlen;
3034
};
3135

@@ -37,8 +41,10 @@ struct vgpio_irq_line {
3741
bool update_pending;
3842
bool queue_pending;
3943

40-
struct virtio_gpio_irq_request ireq ____cacheline_aligned;
41-
struct virtio_gpio_irq_response ires ____cacheline_aligned;
44+
__dma_from_device_group_begin();
45+
struct virtio_gpio_irq_request ireq;
46+
struct virtio_gpio_irq_response ires;
47+
__dma_from_device_group_end();
4248
};
4349

4450
struct virtio_gpio {

0 commit comments

Comments
 (0)