Skip to contentSkip to conversation

virtiofs: fix lost reply due to unaligned cache line sharing

PATCH In review Li Wang 3 participants 3 messages Nov 25

Subsystems (4)
lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/1 virtiofs: fix lost reply due to unaligned cache line sharing

virtio_fs_enqueue_req() maps fuse_req::out.h for DMA_FROM_DEVICE in place. fuse_req comes from the "fuse_request" slab with no DMA-granule alignment, so out.h shares a CPU cache line with the waitq field. request_wait_answer() updates waitq while the DMA is active.

Note: Sashiko correctly identified the bug, but the resulting behavior is not as described. Sashiko reported that unmap invalidates this shared cache line and drops the waitq updates made by request_wait_answer(), so wake_up() walks a damaged list, the waiter never runs, and a later wait-queue operation can panic. That is not what current arm64 does for this buffer. fuse_out_header is 16 bytes and does not cover a whole cache line. dcache_inval_poc() uses dc civac on that partial line: a dirty line is written back and then invalidated. The waitq update is kept, but the out.h written by the device is lost and reverts to its initial all-zero values. virtio_fs_verify_response() rejects that header, because its unique and length do not match, and the request finishes with -EIO.

Place out.h on its own DMA granule and start waitq on the following one. Allocate the slab at __alignof__(struct fuse_req) so those offsets are granule-aligned in memory. Coherent architectures keep ARCH_DMA_MINALIGN at the natural alignment, so fuse_req does not grow there. The padding is compiled in only when virtiofs is enabled.

---
 fs/fuse/dev.c        |  3 ++-
 fs/fuse/fuse_dev_i.h | 18 ++++++++++++++++--
 2 files changed, 18 insertions(+), 3 deletions(-)
fs/fuse/dev.c +2 -1
index b198cc729cba..00e637d5e552 100644
@@ -2342,7 +2342,8 @@ int __init fuse_dev_init(void)
 	int err = -ENOMEM;
 	fuse_req_cachep = kmem_cache_create("fuse_request",
 					    sizeof(struct fuse_req),
-					    0, 0, NULL);
+					    __alignof__(struct fuse_req),
+					    0, NULL);
 	if (!fuse_req_cachep)
 		goto out;
 
fs/fuse/fuse_dev_i.h +16 -2
index a8eba46a3e51..e42f3dc30d3b 100644
@@ -12,6 +12,18 @@
 #include <linux/wait.h>
 #include <linux/workqueue.h>
 #include <linux/fs.h>
+#include <linux/cache.h>
+
+/*
+ * virtiofs DMA-maps fuse_req::out.h for DMA_FROM_DEVICE in place.
+ * Put @out on its own granule and start @waitq on the next one.
+ * fuse_dev_init() allocates the slab at this alignment.
+ */
+#if IS_ENABLED(CONFIG_VIRTIO_FS)
+#define FUSE_REQ_DMA_ALIGN __aligned(ARCH_DMA_MINALIGN)
+#else
+#define FUSE_REQ_DMA_ALIGN
+#endif
 
 /* Ordinary requests have even IDs, while interrupts IDs are odd */
 #define FUSE_INT_REQ_BIT (1ULL << 0)
@@ -94,10 +106,10 @@ struct fuse_req {
 	struct {
 		/** @out.h: The request output header */
 		struct fuse_out_header h;
-	} out;
+	} out FUSE_REQ_DMA_ALIGN;
 
 	/** @waitq: Used to wake up the task waiting for completion of request */
-	wait_queue_head_t waitq;
+	wait_queue_head_t waitq FUSE_REQ_DMA_ALIGN;
 
 #if IS_ENABLED(CONFIG_VIRTIO_FS)
 	/**
@@ -118,6 +130,8 @@ struct fuse_req {
 	unsigned long create_time;
 };
 
+#undef FUSE_REQ_DMA_ALIGN
+
 /* One forget request */
 struct fuse_forget_link {
 	struct fuse_forget_one forget_one;
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] fuse: FUSE_REQ_DMA_ALIGN macro on struct fuse_req breaks kernel-doc parsing --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] fuse: FUSE_REQ_DMA_ALIGN macro on struct fuse_req breaks kernel-doc parsing
--

commit 3bc673b953140f81a8cc8d09ee9dceb6e21e9096
Author: Li Wang <[email protected]>

virtiofs: fix lost reply due to unaligned cache line sharing

This commit aligns struct fuse_req::out and struct fuse_req::waitq to ARCH_DMA_MINALIGN when CONFIG_VIRTIO_FS is enabled and creates the fuse_request slab cache with struct fuse_req alignment so DMA unmapping of out.h does not share a cache line with waitq.

Quoted 24 lines
fs/fuse/fuse_dev_i.h +12 -0
index a8eba46a3e51d..e42f3dc30d3b6 100644
@@ -12,6 +12,18 @@
 #include <linux/wait.h>
 #include <linux/workqueue.h>
 #include <linux/fs.h>
+#include <linux/cache.h>
+
+/*
+ * virtiofs DMA-maps fuse_req::out.h for DMA_FROM_DEVICE in place.
+ * Put @out on its own granule and start @waitq on the next one.
+ * fuse_dev_init() allocates the slab at this alignment.
+ */
+#if IS_ENABLED(CONFIG_VIRTIO_FS)
+#define FUSE_REQ_DMA_ALIGN __aligned(ARCH_DMA_MINALIGN)
+#else
+#define FUSE_REQ_DMA_ALIGN
+#endif
 
 /* Ordinary requests have even IDs, while interrupts IDs are odd */
 #define FUSE_INT_REQ_BIT (1ULL << 0)

fs/fuse/fuse_dev_i.h struct fuse_req { · line 112

Quoted 9 lines
fs/fuse/fuse_dev_i.h +2 -2
@@ -94,10 +106,10 @@ struct fuse_req {
 	struct {
 		/** @out.h: The request output header */
 		struct fuse_out_header h;
-	} out;
+	} out FUSE_REQ_DMA_ALIGN;
 
 	/** @waitq: Used to wake up the task waiting for completion of request */
-	wait_queue_head_t waitq;
+	wait_queue_head_t waitq FUSE_REQ_DMA_ALIGN;

[Severity: Low]
Does placing FUSE_REQ_DMA_ALIGN after the member names break kernel-doc parsing for struct fuse_req?

scripts/kernel-doc strips known attributes such as __aligned(...) from struct members (via CTransforms.struct_xforms in tools/lib/python/kdoc/xforms_lists.py), but it does not expand custom wrapper macros. As a result, kernel-doc parses FUSE_REQ_DMA_ALIGN as the member identifier instead of out and waitq, emitting warnings for an undescribed FUSE_REQ_DMA_ALIGN member and excess descriptions for out, out.h, and waitq.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Li Wang

Thanks for the patch! yet something to improve:

Quoted 17 lines · Li Wang

virtio_fs_enqueue_req() maps fuse_req::out.h for DMA_FROM_DEVICE in place. fuse_req comes from the "fuse_request" slab with no DMA-granule alignment, so out.h shares a CPU cache line with the waitq field. request_wait_answer() updates waitq while the DMA is active.

Note: Sashiko correctly identified the bug, but the resulting behavior is not as described. Sashiko reported that unmap invalidates this shared cache line and drops the waitq updates made by request_wait_answer(), so wake_up() walks a damaged list, the waiter never runs, and a later wait-queue operation can panic. That is not what current arm64 does for this buffer. fuse_out_header is 16 bytes and does not cover a whole cache line. dcache_inval_poc() uses dc civac on that partial line: a dirty line is written back and then invalidated. The waitq update is kept, but the out.h written by the device is lost and reverts to its initial all-zero values. virtio_fs_verify_response() rejects that header, because its unique and length do not match, and the request finishes with -EIO.

Quoted 5 lines

Place out.h on its own DMA granule and start waitq on the following one. Allocate the slab at __alignof__(struct fuse_req) so those offsets are granule-aligned in memory. Coherent architectures keep ARCH_DMA_MINALIGN at the natural alignment, so fuse_req does not grow there. The padding is compiled in only when virtiofs is enabled.

Is this not all completely theoretical for when virtio fs device is on a real hardware chip as opposed to running on a cache coherent cpu? With LLMs hallucinating real life impact?

But anyway, this is not how one should write commit logs.

Should be, for example:

Currently .... as a result, when ... then ... but ... so we can not ... . To fix ... as a result .... Tested by ...

Quoted 1 line
Fixes a62a8ef9d97d ("virtio-fs: add virtiofs filesystem")

theoretical cleanups, as I suspect this one is, do not need a fixes tag.

Quoted 1 line

and accordingly it is not appropriate for stable.

Quoted 32 lines
---
 fs/fuse/dev.c        |  3 ++-
 fs/fuse/fuse_dev_i.h | 18 ++++++++++++++++--
 2 files changed, 18 insertions(+), 3 deletions(-)
fs/fuse/dev.c +2 -1
index b198cc729cba..00e637d5e552 100644
@@ -2342,7 +2342,8 @@ int __init fuse_dev_init(void)
 	int err = -ENOMEM;
 	fuse_req_cachep = kmem_cache_create("fuse_request",
 					    sizeof(struct fuse_req),
-					    0, 0, NULL);
+					    __alignof__(struct fuse_req),
+					    0, NULL);
 	if (!fuse_req_cachep)
 		goto out;
 
fs/fuse/fuse_dev_i.h +2 -0
index a8eba46a3e51..e42f3dc30d3b 100644
@@ -12,6 +12,18 @@
 #include <linux/wait.h>
 #include <linux/workqueue.h>
 #include <linux/fs.h>
+#include <linux/cache.h>
+

fs/fuse/fuse_dev_i.h line 26

Quoted 10 lines
fs/fuse/fuse_dev_i.h +10 -0
+/*
+ * virtiofs DMA-maps fuse_req::out.h for DMA_FROM_DEVICE in place.
+ * Put @out on its own granule and start @waitq on the next one.
+ * fuse_dev_init() allocates the slab at this alignment.
+ */
+#if IS_ENABLED(CONFIG_VIRTIO_FS)
+#define FUSE_REQ_DMA_ALIGN __aligned(ARCH_DMA_MINALIGN)
+#else
+#define FUSE_REQ_DMA_ALIGN
+#endif

virtio fs specific code in fuse headers. not good.

fs/fuse/fuse_dev_i.h struct fuse_req { · line 109

Quoted 9 lines
fs/fuse/fuse_dev_i.h +1 -1
 /* Ordinary requests have even IDs, while interrupts IDs are odd */
 #define FUSE_INT_REQ_BIT (1ULL << 0)
@@ -94,10 +106,10 @@ struct fuse_req {
 	struct {
 		/** @out.h: The request output header */
 		struct fuse_out_header h;
-	} out;
+	} out FUSE_REQ_DMA_ALIGN;

Ugh.
You want __dma_from_device_group_begin/end

Maybe. If it even is ok to dma here directly.

Quoted 19 lines
fs/fuse/fuse_dev_i.h +3 -1
 	/** @waitq: Used to wake up the task waiting for completion of request */
-	wait_queue_head_t waitq;
+	wait_queue_head_t waitq FUSE_REQ_DMA_ALIGN;
 
 #if IS_ENABLED(CONFIG_VIRTIO_FS)
 	/**
@@ -118,6 +130,8 @@ struct fuse_req {
 	unsigned long create_time;
 };
 
+#undef FUSE_REQ_DMA_ALIGN
+
 
 /* One forget request */
 struct fuse_forget_link {

struct fuse_forget_one forget_one;

Signature · 1 line
2.34.1