* [PATCH 6.12.y] fuse: fix fuse_set_nowrite() race with parallel direct writes
@ 2026-11-23 14:12 Junyoung Jang
2026-11-24 0:11 ` Sasha Levin
0 siblings, 1 reply; 3+ messages in thread
From: Junyoung Jang @ 2026-11-23 14:12 UTC (permalink / raw)
To: Miklos Szeredi
Cc: Junyoung Jang, Gwanhyun Lee, Joanne Koong, Bernd Schubert,
Dharmendra Singh, Amir Goldstein, fuse-devel, linux-fsdevel,
linux-kernel, stable
In 6.12.y, queuing a FUSE_WRITE_CACHE request clears folio writeback
immediately while keeping the request in fi->writepages until the
daemon replies. Cached writepage requests can outlive the cached file
handle in two ways: if write_inode_now() fails in fuse_flush(), it
returns early before calling fuse_sync_writes(); and if a file is
mapped with mmap() and closed while clean, final unmapping drops the
last file reference via fuse_release() without calling fuse_flush().
Once the cached file is released, FOPEN_PARALLEL_DIRECT_WRITES allows
concurrent direct writes under inode_lock_shared(). If a request is
still pending in fi->writepages, two direct writers can both enter
fuse_set_nowrite(). The first sets FUSE_NOWRITE (making fi->writectr
negative) and waits on fi->page_waitq; the second sees fi->writectr < 0
and hits BUG_ON(fi->writectr < 0).
Fix this by waiting on fi->page_waitq in fuse_set_nowrite() while
fi->writectr < 0 before setting FUSE_NOWRITE, and waking waiters in
fuse_release_nowrite(). Also still run fuse_sync_writes() in
fuse_flush() when write_inode_now() returns an error so close() drains
in-flight writes before returning the error.
Mainline is unaffected since commit 0c58a97f919c ("fuse: remove tmp
folio for writebacks and internal rb tree") removed fi->writepages and
keeps folio writeback active until the daemon replies. Fix the ordering
directly in 6.12.y rather than backporting that larger rework.
This bug was found with an in-house LLM-based tool, and the root cause,
reproducers, and fix were manually verified by the authors.
Fixes: 153524053bbb ("fuse: allow non-extending parallel direct writes on the same file")
Cc: stable@vger.kernel.org
Co-developed-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Junyoung Jang <graypanda.inzag@gmail.com>
---
Tested on v6.12.116 with KASAN in a 2-vCPU QEMU guest using an
unprivileged FUSE daemon (UID 1000 via fusermount3). Verified against
both C reproducers (FUSE_SETATTR -EIO on close, and dirty mmap
followed by munmap after clean close), which previously triggered
BUG_ON(fi->writectr < 0) and now complete cleanly without BUG or KASAN
reports. Standalone reproducers are available on request.
The patch also applies cleanly to linux-6.6.y (5139929c698c), though
6.6.y was not runtime-tested.
fs/fuse/dir.c | 10 ++++++++--
fs/fuse/file.c | 5 +++--
2 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c
index 45cbe0cb2..54b9138fa 100644
--- a/fs/fuse/dir.c
+++ b/fs/fuse/dir.c
@@ -1821,8 +1821,13 @@ void fuse_set_nowrite(struct inode *inode)
BUG_ON(!inode_is_locked(inode));
- spin_lock(&fi->lock);
- BUG_ON(fi->writectr < 0);
+ for (;;) {
+ spin_lock(&fi->lock);
+ if (fi->writectr >= 0)
+ break;
+ spin_unlock(&fi->lock);
+ wait_event(fi->page_waitq, READ_ONCE(fi->writectr) >= 0);
+ }
fi->writectr += FUSE_NOWRITE;
spin_unlock(&fi->lock);
wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
@@ -1850,6 +1855,7 @@ void fuse_release_nowrite(struct inode *inode)
spin_lock(&fi->lock);
__fuse_release_nowrite(inode);
spin_unlock(&fi->lock);
+ wake_up(&fi->page_waitq);
}
static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
diff --git a/fs/fuse/file.c b/fs/fuse/file.c
index 33ba8ae3b..5e01dd2b7 100644
--- a/fs/fuse/file.c
+++ b/fs/fuse/file.c
@@ -538,13 +538,14 @@ static int fuse_flush(struct file *file, fl_owner_t id)
return 0;
err = write_inode_now(inode, 1);
- if (err)
- return err;
inode_lock(inode);
fuse_sync_writes(inode);
inode_unlock(inode);
+ if (err)
+ return err;
+
err = filemap_check_errors(file->f_mapping);
if (err)
return err;
base-commit: 5c40a1750f44dd6c7292d8c8d0b4ffff1734576e
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH 6.12.y] fuse: fix fuse_set_nowrite() race with parallel direct writes
2026-11-23 14:12 [PATCH 6.12.y] fuse: fix fuse_set_nowrite() race with parallel direct writes Junyoung Jang
@ 2026-11-24 0:11 ` Sasha Levin
2026-11-25 22:34 ` [PATCH 6.12.y 6.6.y 6.1.y v2] " Junyoung Jang
0 siblings, 1 reply; 3+ messages in thread
From: Sasha Levin @ 2026-11-24 0:11 UTC (permalink / raw)
To: Miklos Szeredi
Cc: Sasha Levin, Junyoung Jang, Gwanhyun Lee, Joanne Koong,
Bernd Schubert, Dharmendra Singh, Amir Goldstein, fuse-devel,
linux-fsdevel, linux-kernel, stable
> @@ -1850,6 +1855,7 @@ void fuse_release_nowrite(struct inode *inode)
> spin_lock(&fi->lock);
> __fuse_release_nowrite(inode);
> spin_unlock(&fi->lock);
> + wake_up(&fi->page_waitq);
> }
fuse_do_setattr() drops NOWRITE with __fuse_release_nowrite() directly, so
it never gets this wake_up(). That path can run with the inode lock held
only shared: fuse_direct_IO() -> fuse_do_truncate() when i_size shrank after
fuse_dio_lock() (no writeback_cache). A parallel direct writer sleeping in
the new wait in fuse_set_nowrite() then misses its wakeup and hangs holding
i_rwsem shared. Could you do the wake_up() in __fuse_release_nowrite() (or
on that path as well)?
Since this is a stable-only fix (mainline only fixed it through
0c58a97f919c ("fuse: remove tmp folio for writebacks and internal rb
tree")), it also needs an ack from a FUSE maintainer.
> The patch also applies cleanly to linux-6.6.y (5139929c698c), though
> 6.6.y was not runtime-tested.
6.6.y and 6.1.y have the same bug from 153524053bbb ("fuse: allow
non-extending parallel direct writes on the same file"), so please send
v2 for 6.12.y, 6.6.y and 6.1.y together.
--
Thanks,
Sasha
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 6.12.y 6.6.y 6.1.y v2] fuse: fix fuse_set_nowrite() race with parallel direct writes
2026-11-24 0:11 ` Sasha Levin
@ 2026-11-25 22:34 ` Junyoung Jang
0 siblings, 0 replies; 3+ messages in thread
From: Junyoung Jang @ 2026-11-25 22:34 UTC (permalink / raw)
To: Miklos Szeredi
Cc: Junyoung Jang, Sasha Levin, Gwanhyun Lee, Joanne Koong,
Bernd Schubert, Amir Goldstein, fuse-devel, linux-fsdevel,
linux-kernel, stable
In 6.12.y, 6.6.y, and 6.1.y, queuing a FUSE_WRITE_CACHE request clears
folio writeback immediately while keeping the request in fi->writepages
until the daemon replies. Pending requests can outlive a cached file
handle if write_inode_now() fails in fuse_flush() and returns before
fuse_sync_writes(), or if a cached mmap drops its last file reference
via munmap() without calling fuse_flush().
Once the cached file is released, FOPEN_PARALLEL_DIRECT_WRITES allows
concurrent direct writes under inode_lock_shared(). While one writer
holds FUSE_NOWRITE and waits for the pending request, another can enter
fuse_set_nowrite() and hit BUG_ON(fi->writectr < 0).
Fix this by waiting on fi->page_waitq in fuse_set_nowrite() while
fi->writectr < 0 before setting FUSE_NOWRITE, and still running
fuse_sync_writes() in fuse_flush() when write_inode_now() fails. Wake
waiters in __fuse_release_nowrite() rather than fuse_release_nowrite()
because fuse_do_setattr() calls __fuse_release_nowrite() directly on
success, including under a shared inode lock via fuse_direct_IO() ->
fuse_do_truncate().
Mainline is unaffected since commit 0c58a97f919c ("fuse: remove tmp
folio for writebacks and internal rb tree") removed fi->writepages and
keeps folio writeback active until the daemon replies. Fix the ordering
directly in the stable trees rather than backporting that larger rework.
This bug was found with an in-house LLM-based tool, and the root cause,
reproducers, and fix were manually verified by the authors.
Fixes: 153524053bbb ("fuse: allow non-extending parallel direct writes on the same file")
Cc: stable@vger.kernel.org
Co-developed-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Junyoung Jang <graypanda.inzag@gmail.com>
---
Changes in v2:
- Move wake_up(&fi->page_waitq) into __fuse_release_nowrite() to cover
the shared-lock fuse_do_setattr() path pointed out by Sasha Levin.
- Verify and target 6.12.y, 6.6.y, and 6.1.y together.
- v1: https://lore.kernel.org/r/20261123141024.2476753-1-graypanda.inzag@gmail.com
This single patch applies cleanly to linux-6.12.y, linux-6.6.y, and
linux-6.1.y. Tested with KASAN and lockdep in QEMU on 6.12.119,
6.6.163, and 6.1.193 using unprivileged FUSE daemons (UID 1000 via
fusermount3) against the close -EIO and dirty mmap/munmap reproducers.
Also reproduced the v1 missed-wakeup hang on the shared-lock
fuse_do_truncate() -> fuse_do_setattr() path using an artificial delay
and confirmed v2 completes cleanly across 6.12.y, 6.6.y, and 6.1.y.
Standalone C reproducers are available on request.
FUSE maintainers: could you please consider an Acked-by for this
stable-only fix?
fs/fuse/dir.c | 10 ++++++++--
fs/fuse/file.c | 5 +++--
2 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c
index 54fcf91272a0..aaf364fe0e7b 100644
--- a/fs/fuse/dir.c
+++ b/fs/fuse/dir.c
@@ -1821,8 +1821,13 @@ void fuse_set_nowrite(struct inode *inode)
BUG_ON(!inode_is_locked(inode));
- spin_lock(&fi->lock);
- BUG_ON(fi->writectr < 0);
+ for (;;) {
+ spin_lock(&fi->lock);
+ if (fi->writectr >= 0)
+ break;
+ spin_unlock(&fi->lock);
+ wait_event(fi->page_waitq, READ_ONCE(fi->writectr) >= 0);
+ }
fi->writectr += FUSE_NOWRITE;
spin_unlock(&fi->lock);
wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
@@ -1841,6 +1846,7 @@ static void __fuse_release_nowrite(struct inode *inode)
BUG_ON(fi->writectr != FUSE_NOWRITE);
fi->writectr = 0;
fuse_flush_writepages(inode);
+ wake_up(&fi->page_waitq);
}
void fuse_release_nowrite(struct inode *inode)
diff --git a/fs/fuse/file.c b/fs/fuse/file.c
index f733a0f78114..b1646a452cc4 100644
--- a/fs/fuse/file.c
+++ b/fs/fuse/file.c
@@ -538,13 +538,14 @@ static int fuse_flush(struct file *file, fl_owner_t id)
return 0;
err = write_inode_now(inode, 1);
- if (err)
- return err;
inode_lock(inode);
fuse_sync_writes(inode);
inode_unlock(inode);
+ if (err)
+ return err;
+
err = filemap_check_errors(file->f_mapping);
if (err)
return err;
base-commit: e2e4b92c2b2a3ae78423cb343951cb5d2241b405
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-25 22:34 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 14:12 [PATCH 6.12.y] fuse: fix fuse_set_nowrite() race with parallel direct writes Junyoung Jang
2026-11-24 0:11 ` Sasha Levin
2026-11-25 22:34 ` [PATCH 6.12.y 6.6.y 6.1.y v2] " Junyoung Jang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox