[PATCH v2 4/5] fuse: don't sleep in ->poll before a sync-init mount

From: Yun Wang

Date: Sun Nov 22 2026 - 10:27:12 EST


From: Yun Wang <arsenova@xxxxxxxxx>

fuse_dev_poll() gets the device with _fuse_get_dev(), which waits for
the mount of a device in sync-init mode. So before the mount, ->poll
sleeps until the mount or a signal, and so do its callers:

- poll(2), even with a zero timeout. An eventfd in the same poll set
can't end the wait, and after a signal poll(2) reports POLLERR.
libfuse's sync-init worker polls the device together with an
eventfd that is meant to stop it, and libfuse had to work around
mounts that hung there, see the Link.
- epoll_ctl(EPOLL_CTL_ADD), while holding ep->mtx, so other
epoll_ctl() calls and an epoll_wait(2) with events to report on the
same epoll instance wait too.
- io_uring_enter(2) for a read or a poll, since io_uring polls the
device before it tries the read or when it arms a poll for it.
That happens with the ring's uring_lock held, so any other thread
that submits to the same ring waits for it in D state meanwhile.

The sleep can't simply be dropped. Before the mount there is no
fiq->waitq to wait on, and poll(2), epoll and io_uring register their
wait queue entries only once, so a poller that registered somewhere
else before the mount would miss the requests that come after it.

Give struct fuse_dev a wait queue for that window. Before the mount,
->poll waits on fud->mount_waitq and reports no events. When a
sync-init device is installed, add an entry to fiq->waitq that passes
every wakeup on to fud->mount_waitq, and wake fud->mount_waitq once,
since the device is writable now. ->poll checks for the channel again
after queueing itself, and the channel is set before that wakeup, so a
poller racing with the mount either gets woken or sees the channel and
then waits on fiq->waitq as well. fuse_dev_release() removes the entry
before it drops the connection reference. Pollers that come after the
mount wait on fiq->waitq as before.

This adds 64 bytes to struct fuse_dev on x86-64 without lock
debugging. The wake_up_sync() of a synchronous request is passed on as
it is. Any other wakeup may come from wake_up_all(), so it wakes every
waiter on fud->mount_waitq, EPOLLEXCLUSIVE ones too; that only affects
pollers that came before the mount.

The entry stays on fiq->waitq while the device is open, so every
wakeup of fiq->waitq calls it. If nobody waits on fud->mount_waitq,
it returns without taking that lock, and a request costs one more
indirect call. That is the case once the pollers from before the
mount are gone, such as libfuse's worker once its poll(2) returns.
The check needs no barrier: every waker of fiq->waitq holds fiq->lock,
and the pollers woken by the mount look at the queue under fiq->lock
again, see the comment in fuse_dev_mount_wake().

Fixes: dfb84c330794 ("fuse: allow synchronous FUSE_INIT")
Link: https://github.com/libfuse/libfuse/pull/1633
Signed-off-by: Yun Wang <arsenova@xxxxxxxxx>
---
fs/fuse/dev.c | 70 +++++++++++++++++++++++++++++++++++++++++---
fs/fuse/fuse_dev_i.h | 9 ++++++
2 files changed, 75 insertions(+), 4 deletions(-)

diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c
index 7cb93885cd0a..187fd13fefe0 100644
--- a/fs/fuse/dev.c
+++ b/fs/fuse/dev.c
@@ -439,6 +439,7 @@ static struct fuse_dev *fuse_dev_alloc_no_pq(void)

refcount_set(&fud->ref, 1);
fuse_pqueue_init(&fud->pq);
+ init_waitqueue_head(&fud->mount_waitq);

return fud;
}
@@ -457,6 +458,35 @@ struct fuse_dev *fuse_dev_alloc(void)
}
EXPORT_SYMBOL_GPL(fuse_dev_alloc);

+/*
+ * Pollers of a sync-init device that came before the mount are queued on
+ * fud->mount_waitq, and poll(2), epoll and io_uring don't register again
+ * later. Pass every wakeup of fiq->waitq on to them: a sync one as it is,
+ * any other to all of them, since it may come from wake_up_all().
+ */
+static int fuse_dev_mount_wake(struct wait_queue_entry *wait, unsigned int mode,
+ int wake_flags, void *key)
+{
+ struct fuse_dev *fud = container_of(wait, struct fuse_dev, mount_fwd);
+
+ /*
+ * Nobody to pass the wakeup on to. Every waker of fiq->waitq holds
+ * fiq->lock, and the pollers on mount_waitq were woken by the mount
+ * and look at the queue under fiq->lock again. If that look came
+ * first, this waker sees them here; if it came later, it sees the
+ * new request.
+ */
+ if (!waitqueue_active(&fud->mount_waitq))
+ return 0;
+
+ /* fiq->waitq is woken only by wake_up{,_all,_sync}(): 0 or WF_SYNC */
+ if (wake_flags)
+ __wake_up_sync_key(&fud->mount_waitq, mode, key);
+ else
+ __wake_up(&fud->mount_waitq, mode, 0, key);
+ return 0;
+}
+
/*
* Installs @fch into @fud, return true on success. "Consumes" @pq in either case.
*/
@@ -490,6 +520,19 @@ static bool fuse_dev_install_with_pq(struct fuse_dev *fud, struct fuse_chan *fch
}
list_add_tail(&fud->entry, &fch->devices);
fuse_conn_get(fch->conn);
+
+ if (fud->sync_init) {
+ /* Removed in fuse_dev_release(), before the conn ref is put */
+ init_waitqueue_func_entry(&fud->mount_fwd, fuse_dev_mount_wake);
+ add_wait_queue(&fch->iq.waitq, &fud->mount_fwd);
+ fud->mount_fwd_queued = true;
+ /*
+ * The device is writable now. Wake without a key: a poller
+ * that waits only for EPOLLIN has to look again as well, see
+ * fuse_dev_mount_wake().
+ */
+ wake_up_all(&fud->mount_waitq);
+ }
wake_up_all(&fuse_dev_waitq);
return true;
}
@@ -2102,16 +2145,33 @@ static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
return ret;
}

+/*
+ * Before the mount, a sync-init device has no fiq->waitq to wait on. Its
+ * pollers wait on fud->mount_waitq instead, and the mount passes the
+ * wakeups of fiq->waitq on to it, see fuse_dev_mount_wake().
+ */
static __poll_t fuse_dev_poll(struct file *file, poll_table *wait)
{
__poll_t mask = EPOLLOUT | EPOLLWRNORM;
struct fuse_iqueue *fiq;
- struct fuse_dev *fud = _fuse_get_dev(file);
+ struct fuse_dev *fud = fuse_file_to_fud(file);
+ struct fuse_chan *fch = fuse_dev_chan_get(fud);

- if (IS_ERR(fud))
- return EPOLLERR;
+ if (unlikely(!fch)) {
+ if (!fud->sync_init)
+ return EPOLLERR;
+
+ poll_wait(file, &fud->mount_waitq, wait);
+ /*
+ * Check again after queueing: fuse_dev_install_with_pq() sets
+ * the channel before it wakes fud->mount_waitq.
+ */
+ fch = fuse_dev_chan_get(fud);
+ if (!fch)
+ return 0;
+ }

- fiq = &fud->chan->iq;
+ fiq = &fch->iq;
poll_wait(file, &fiq->waitq, wait);

spin_lock(&fiq->lock);
@@ -2264,6 +2324,8 @@ int fuse_dev_release(struct inode *inode, struct file *file)
/* Are we the last open device? */
last = list_empty(&fch->devices);
spin_unlock(&fch->lock);
+ if (fud->mount_fwd_queued)
+ remove_wait_queue(&fch->iq.waitq, &fud->mount_fwd);

fuse_dev_end_requests(&to_end);

diff --git a/fs/fuse/fuse_dev_i.h b/fs/fuse/fuse_dev_i.h
index 808152b75792..40caa4d820a1 100644
--- a/fs/fuse/fuse_dev_i.h
+++ b/fs/fuse/fuse_dev_i.h
@@ -305,6 +305,9 @@ struct fuse_dev {
/** @sync_init: Issue FUSE_INIT synchronously */
bool sync_init;

+ /** @mount_fwd_queued: @mount_fwd is on chan->iq.waitq */
+ bool mount_fwd_queued;
+
/** @chan: Fuse channel for this device */
struct fuse_chan *chan;

@@ -313,6 +316,12 @@ struct fuse_dev {

/** @entry: list entry on fch->devices */
struct list_head entry;
+
+ /** @mount_waitq: pollers that came before the mount wait here */
+ wait_queue_head_t mount_waitq;
+
+ /** @mount_fwd: passes wakeups of chan->iq.waitq on to @mount_waitq */
+ wait_queue_entry_t mount_fwd;
};

struct fuse_copy_state {
--
2.56.1