fuse: fix and optimize parallel writes on passthrough mounts
Subsystems (3)
cover fuse: fix and optimize parallel writes on passthrough mounts
These two patches fix a bug that causes FOPEN_PARALLEL_DIRECT_WRITES to have no effect on FUSE passthrough mounts, preventing write IOPS from scaling with concurrency.
The bug has two sides that must be fixed together (patch 1/2):
(a) fuse_passthrough_write_iter() calls inode_lock() directly, bypassing the fuse_dio_lock() function that checks FOPEN_PARALLEL_DIRECT_WRITES.
(b) fuse_file_io_open() strips FOPEN_PARALLEL_DIRECT_WRITES from any open that lacks FOPEN_DIRECT_IO, including passthrough opens where the flag is redundant and should not be required.
Either bug alone is sufficient to serialize all writers. Together they ensure the flag can never take effect on a passthrough-backed file.
Patch 2/2 is an independent performance follow-on: once parallel writes are unblocked, the per-inode spinlock (fi->lock) becomes the next measurable cost. This patch converts iocachectr to atomic_t and adds lockless fast paths to fuse_inode_uncached_io_start/end and fuse_write_update_attr.
Tested with fio randwrite 4K direct, numjobs=1/2/4/8, iodepth=64, on a FUSE passthrough mount backed by XFS on a RAM-backed null_blk device (kernel 6.17.13, AMD EPYC 9B45, 16 threads):
numjobs | Before (IOPS) | After patch 1 | After patch 2
--------|---------------|---------------|---------------
1 | ~470K | ~470K | ~470K
2 | ~650K | ~930K | ~930K
4 | ~640K | ~1,530K | ~1,530K
8 | ~635K | ~1,707K | ~1,707KRaw XFS throughput on the same device at numjobs=8: ~1,702K IOPS.
Russ Fellows (2):
fuse: fix FOPEN_PARALLEL_DIRECT_WRITES being ignored for passthrough writes
fuse: reduce fi->lock contention on parallel direct I/O
fs/fuse/file.c | 49 ++++++++++++++++++++++----- fs/fuse/fuse_i.h | 11 +++++- fs/fuse/inode.c | 2 +- fs/fuse/iomode.c | 61 +++++++++++++++++++++++++++------ fs/fuse/passthrough.c | 6 ++-- 5 files changed, 104 insertions(+), 25 deletions(-)
Signature · 1 line
2.51.0
PATCH 1/2 fuse: fix FOPEN_PARALLEL_DIRECT_WRITES being ignored for passthrough writes
FOPEN_PARALLEL_DIRECT_WRITES has no effect on passthrough-backed FUSE files due to two independent bugs that each prevent it from working. Both must be fixed to restore parallel write concurrency.
Bug 1: fuse_passthrough_write_iter() acquires the exclusive inode lock directly:
inode_lock(inode);
ret = backing_file_write_iter(...);
inode_unlock(inode);This serializes all concurrent writers regardless of whether the server set FOPEN_PARALLEL_DIRECT_WRITES. The flag is checked by fuse_dio_wr_exclusive_lock(), called from fuse_dio_lock(), called from fuse_direct_write_iter() -- the non-passthrough O_DIRECT path. fuse_file_write_iter() routes passthrough opens to fuse_passthrough_write_iter() instead, bypassing the flag check entirely.
Bug 2: fuse_file_io_open() in iomode.c strips FOPEN_PARALLEL_DIRECT_WRITES from any open that lacks FOPEN_DIRECT_IO:
if (!(ff->open_flags & FOPEN_DIRECT_IO))
ff->open_flags &= ~FOPEN_PARALLEL_DIRECT_WRITES;This is correct for regular direct-IO opens where FOPEN_DIRECT_IO ensures O_DIRECT is actually in effect. It is wrong for passthrough opens: a passthrough file already bypasses the FUSE page cache by definition, so FOPEN_DIRECT_IO is redundant and should not be required to preserve the parallel-writes flag.
Note: adding FOPEN_DIRECT_IO to the daemon's open flags is not a valid workaround. fuse_file_write_iter() checks FOPEN_DIRECT_IO before FOPEN_PASSTHROUGH, so setting both causes writes to be routed through fuse_direct_write_iter() (requiring a userspace round-trip) instead of fuse_passthrough_write_iter() (zero-copy kernel path).
Combined effect: a daemon that opens with FOPEN_PASSTHROUGH | FOPEN_PARALLEL_DIRECT_WRITES (without FOPEN_DIRECT_IO) has the parallel flag stripped by Bug 2 before Bug 1 is even reached. Both bugs must be fixed together.
Fix Bug 1: make fuse_dio_lock() and fuse_dio_unlock() non-static and call them from fuse_passthrough_write_iter(), replacing the open-coded inode_lock/inode_unlock. This reuses the existing logic that handles FOPEN_PARALLEL_DIRECT_WRITES, append writes, writes past EOF, and page-cache IO mode transitions.
Fix Bug 2: skip the FOPEN_PARALLEL_DIRECT_WRITES strip when FOPEN_PASSTHROUGH is set. The flag remains stripped for non-passthrough opens without FOPEN_DIRECT_IO, preserving existing behaviour.
Safety: backing_file_write_iter() calls into the backing filesystem's write_iter (e.g. xfs_file_write_iter), which acquires the backing inode's own lock independently. The FUSE inode lock and the backing inode lock are entirely separate; using inode_lock_shared on the FUSE inode does not affect the backing filesystem's concurrency control.
--- fs/fuse/file.c | 6 +++--- fs/fuse/fuse_i.h | 2 ++ fs/fuse/iomode.c | 8 ++++++-- fs/fuse/passthrough.c | 6 +++--- 4 files changed, 14 insertions(+), 8 deletions(-)
fs/fuse/file.c +4 -4
@@ -1428,8 +1428,8 @@ static bool fuse_dio_wr_exclusive_lock(struct kiocb *iocb, struct iov_iter *fromreturn false; } -static void fuse_dio_lock (struct kiocb *iocb,struct iov_iter *from, -bool *exclusive) +void fuse_dio_lock (struct kiocb *iocb,struct iov_iter *from, +bool *exclusive) {struct inode *inode =file_inode (iocb->ki_filp);struct fuse_inode *fi =get_fuse_inode (inode);
@@ -1455,7 +1455,7 @@ static void fuse_dio_lock(struct kiocb *iocb, struct iov_iter *from, } } -static void fuse_dio_unlock (struct kiocb *iocb,bool exclusive) +void fuse_dio_unlock (struct kiocb *iocb,bool exclusive) {struct inode *inode =file_inode (iocb->ki_filp);struct fuse_inode *fi =get_fuse_inode (inode);
@@ -1469,7 +1469,7 @@ static void fuse_dio_unlock(struct kiocb *iocb, bool exclusive) } } -static const struct iomap_write_ops fuse_iomap_write_ops = { +static const struct iomap_write_ops fuse_iomap_write_ops = {/* unchanged */ .read_folio_range = fuse_iomap_read_folio_range, };
fs/fuse/fuse_i.h +2 -0
@@ -1541,6 +1541,8 @@ int fuse_file_io_open(struct file *file, struct inode *inode);void fuse_file_io_release (struct fuse_file *ff,struct inode *inode);/* file.c */ +void fuse_dio_lock (struct kiocb *iocb,struct iov_iter *from,bool *exclusive); +void fuse_dio_unlock (struct kiocb *iocb,bool exclusive);struct fuse_file *fuse_file_open (struct fuse_mount *fm,u64 nodeid,unsigned int open_flags,bool isdir); void fuse_file_release(struct inode *inode, struct fuse_file *ff,
fs/fuse/iomode.c +7 -4
@@ -214,10 +214,14 @@ int fuse_file_io_open(struct file *file, struct inode *inode)if (fuse_inode_backing (fi) && !(ff->open_flags &FOPEN_PASSTHROUGH )) goto fail; -/* -* FOPEN_PARALLEL_DIRECT_WRITES requires FOPEN_DIRECT_IO. -*/ -if (!(ff->open_flags &FOPEN_DIRECT_IO )) +/* +* FOPEN_PARALLEL_DIRECT_WRITES requires FOPEN_DIRECT_IO, except for +* passthrough opens which bypass the page cache regardless and do not +* need FOPEN_DIRECT_IO to guarantee direct I/O semantics. +*/ +if (!(ff->open_flags &FOPEN_DIRECT_IO ) && + !(ff->open_flags &FOPEN_PASSTHROUGH )) ff->open_flags &= ~FOPEN_PARALLEL_DIRECT_WRITES ;/*
fs/fuse/passthrough.c +3 -3
@@ -54,11 +54,11 @@ ssize_t fuse_passthrough_write_iter(struct kiocb *iocb, struct iov_iter *iter) {struct file *file = iocb->ki_filp; -struct inode *inode =file_inode (file);struct fuse_file *ff = file->private_data;struct file *backing_file =fuse_file_passthrough (ff);size_t count =iov_iter_count (iter);ssize_t ret; +bool exclusive;struct backing_file_ctx ctx = { .cred = ff->cred, .end_write = fuse_passthrough_end_write,
@@ -70,10 +70,10 @@ ssize_t fuse_passthrough_write_iter(struct kiocb *iocb,if (!count)return 0 ; -inode_lock (inode); +fuse_dio_lock (iocb, iter, &exclusive); ret =backing_file_write_iter (backing_file, iter, iocb, iocb->ki_flags, &ctx); -inode_unlock (inode); +fuse_dio_unlock (iocb, exclusive);return ret; }
Signature · 1 line
2.51.0
PATCH 2/2 fuse: reduce fi->lock contention on parallel direct I/O
In reply to Russ FellowsOn the parallel passthrough write path, fi->lock was acquired three times per I/O under the original code:
1. fuse_inode_uncached_io_start() -- decrement iocachectr
2. fuse_write_update_attr() -- bump attr_version, check i_size
3. fuse_inode_uncached_io_end() -- increment iocachectr, wake waiters
At 1.7M IOPS (numjobs=8, iodepth=64, 4K) this amounts to ~5.1M spinlock acquisitions/second on a single cache line. While the parallel-writes fix (patch 1/2) is the primary bottleneck, this patch eliminates the remaining fi->lock overhead on the hot path.
Convert iocachectr from int to atomic_t and add lockless fast paths:
fuse_inode_uncached_io_start(fb=NULL): use an atomic_try_cmpxchg loop to check-and-decrement without fi->lock. The lock is still taken for the first open (0→-1 transition) and for backing-file manipulation.
fuse_inode_uncached_io_end(): use atomic_inc_return to detect the still-inflight case (counter still negative after increment) without a lock. fi->lock is only taken when the counter reaches zero, to serialize wake_up and backing-file clear with concurrent opens.
fuse_write_update_attr(): skip fi->lock for the common in-EOF case. Use WRITE_ONCE for fi->attr_version (some readers already access it without fi->lock, e.g. inode.c:355 and dir.c:2069). fi->lock is only taken when pos > i_size, with a double-check inside to handle races near EOF. Parallel direct writes are gated on fuse_io_past_eof() returning false upstream, so this slow path is not taken on the hot path.
All existing callsites that access iocachectr under fi->lock are updated to use the atomic API (atomic_read/inc/dec), which are no-ops with the lock held.
--- fs/fuse/file.c | 31 +++++++++++++++++++++++-------- fs/fuse/fuse_i.h | 9 +++++++-- fs/fuse/inode.c | 2 +- fs/fuse/iomode.c | 53 +++++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 71 insertions(+), 24 deletions(-)
fs/fuse/file.c +22 -9
@@ -1115,16 +1115,29 @@ bool fuse_write_update_attr(struct inode *inode, loff_t pos, ssize_t written)struct fuse_inode *fi =get_fuse_inode (inode);bool ret = false; -spin_lock (&fi->lock); - fi->attr_version =atomic64_inc_return (&fc->attr_version); -if (written >0 && pos > inode->i_size) { -i_size_write (inode, pos); - ret = true; - } -spin_unlock (&fi->lock); - +/* +* Bump the global attr version so stale cached attrs are detected. +* WRITE_ONCE is sufficient: some readers don't hold fi->lock, and +* on x86_64 the store is naturally atomic. fi->lock is only needed +* for the i_size extension case below. +*/ +WRITE_ONCE (fi->attr_version,atomic64_inc_return (&fc->attr_version));fuse_invalidate_attr_mask (inode,FUSE_STATX_MODSIZE ); +/* +* Only take fi->lock when the write may extend the file. Parallel +* direct writes are gated on fuse_io_past_eof() returning false, so +* this slow path is not taken on the hot parallel-write path. +*/ +if (written >0 && pos >READ_ONCE (inode->i_size)) { +spin_lock (&fi->lock); +if (pos > inode->i_size) { +i_size_write (inode, pos); + ret = true; + } +spin_unlock (&fi->lock); + } +return ret; }
@@ -3154,7 +3154,7 @@ void fuse_init_file_inode(struct inode *inode, unsigned int flags)INIT_LIST_HEAD (&fi->write_files);INIT_LIST_HEAD (&fi->queued_writes); fi->writectr =0 ; - fi->iocachectr =0 ; +atomic_set (&fi->iocachectr,0 );init_waitqueue_head (&fi->page_waitq);init_waitqueue_head (&fi->direct_io_waitq);
fs/fuse/fuse_i.h +7 -2
@@ -153,8 +153,13 @@ struct fuse_inode {* (FUSE_NOWRITE) means more writes are blocked */ int writectr; -/** Number of files/maps using page cache */ -int iocachectr; +/** +* Refcount for inode I/O mode: > 0 means cached I/O +* users, 0 is idle, < 0 means parallel uncached I/Os +* in flight. Use atomic ops; fi->lock only needed +* for the 0↔±1 boundary transitions. +*/ +atomic_t iocachectr;/* Waitq for writepage completion */ wait_queue_head_t page_waitq;
fs/fuse/inode.c +1 -1
@@ -190,7 +190,7 @@ static void fuse_evict_inode(struct inode *inode)atomic64_inc (&fc->evict_ctr); }if (S_ISREG (inode->i_mode) && !fuse_is_bad (inode)) { -WARN_ON (fi->iocachectr !=0 ); +WARN_ON (atomic_read (&fi->iocachectr) !=0 );WARN_ON (!list_empty (&fi->write_files));WARN_ON (!list_empty (&fi->queued_writes)); }
fs/fuse/iomode.c +38 -15
98 lines not on this page · raw message
Signature · 1 line
2.51.0