[PATCH v2 5/5] selftests/fuse: test a sync-init device before the mount
From: Yun Wang
Date: Sun Nov 22 2026 - 10:27:24 EST
From: Yun Wang <arsenova@xxxxxxxxx>
A server that uses FUSE_DEV_IOC_SYNC_INIT has the device before the
mount does. Test that calls which must not block don't wait for the
mount, and that waiters armed before it see its requests.
The fuse_dev tests never mount. poll(2) with a zero timeout or
together with an eventfd, epoll_ctl(2), read(2) and splice(2) on an
O_NONBLOCK device, FUSE_IO_URING_CMD_REGISTER through io_uring, and the
close of a device opened with O_ASYNC must return at once; where they
wait for the mount, the harness kills them after two seconds. A
blocking read(2) must still wait, until an alarm ends it. Two tests
send a REGISTER or a POLL_ADD from one thread and a NOP to the same
ring from another: the NOP must not have to wait for the mount, as it
does when the first thread sleeps with the ring's uring_lock held.
Each test also runs on a device without sync init, which must keep
failing at once.
The mount tests arm epoll (level triggered, edge triggered and with
EPOLLEXCLUSIVE) or a multishot io_uring poll before the mount, then
mount as root of a new user namespace. The waiter has to report
FUSE_INIT, and then a request queued after the mount, without being
armed again.
io_uring is set up with its system calls, with the 128-byte SQEs that
FUSE_IO_URING_CMD_* needs, so the test doesn't depend on liburing. No
privileges are needed; the tests skip without /dev/fuse,
FUSE_DEV_IOC_SYNC_INIT, io_uring or user namespaces, and the uring_cmd
test also without CONFIG_FUSE_IO_URING.
Signed-off-by: Yun Wang <arsenova@xxxxxxxxx>
---
.../selftests/filesystems/fuse/.gitignore | 1 +
.../selftests/filesystems/fuse/Makefile | 2 +
.../filesystems/fuse/fuse_sync_init_test.c | 532 ++++++++++++++++++
3 files changed, 535 insertions(+)
create mode 100644 tools/testing/selftests/filesystems/fuse/fuse_sync_init_test.c
diff --git a/tools/testing/selftests/filesystems/fuse/.gitignore b/tools/testing/selftests/filesystems/fuse/.gitignore
index 7c787313e69b..9066f735cfe2 100644
--- a/tools/testing/selftests/filesystems/fuse/.gitignore
+++ b/tools/testing/selftests/filesystems/fuse/.gitignore
@@ -6,3 +6,4 @@ write_extend_eof_test
fuse_acl_cache_test
fuse_symlink_cache_test
fuse_readdir_cache_test
+fuse_sync_init_test
diff --git a/tools/testing/selftests/filesystems/fuse/Makefile b/tools/testing/selftests/filesystems/fuse/Makefile
index e3ae3acff113..15630db378ca 100644
--- a/tools/testing/selftests/filesystems/fuse/Makefile
+++ b/tools/testing/selftests/filesystems/fuse/Makefile
@@ -7,11 +7,13 @@ TEST_GEN_PROGS += write_extend_eof_test
TEST_GEN_PROGS += fuse_acl_cache_test
TEST_GEN_PROGS += fuse_symlink_cache_test
TEST_GEN_PROGS += fuse_readdir_cache_test
+TEST_GEN_PROGS += fuse_sync_init_test
TEST_GEN_FILES := fuse_mnt
include ../../lib.mk
$(OUTPUT)/write_extend_eof_test: LDLIBS += -lpthread
+$(OUTPUT)/fuse_sync_init_test: LDLIBS += -lpthread
VAR_CFLAGS := $(shell pkg-config fuse3 --cflags 2>/dev/null)
ifeq ($(VAR_CFLAGS),)
diff --git a/tools/testing/selftests/filesystems/fuse/fuse_sync_init_test.c b/tools/testing/selftests/filesystems/fuse/fuse_sync_init_test.c
new file mode 100644
index 000000000000..98eab737ea8a
--- /dev/null
+++ b/tools/testing/selftests/filesystems/fuse/fuse_sync_init_test.c
@@ -0,0 +1,532 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A /dev/fuse fd in sync-init mode (FUSE_DEV_IOC_SYNC_INIT) is opened before
+ * the mount. Until the mount, calls that must not block have to return at
+ * once, and waiters armed before it have to see the requests that come after
+ * it. A call that sleeps instead makes its test hit the harness timeout.
+ *
+ * The fuse_dev tests never mount. Each runs on a sync-init fd, where
+ * non-blocking calls must not wait (while a blocking read still does), and
+ * on an fd without sync init, which keeps failing at once. The mount tests
+ * mount as root of a new user namespace.
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <pthread.h>
+#include <sched.h>
+#include <signal.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/epoll.h>
+#include <sys/eventfd.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/uio.h>
+#include <sys/xattr.h>
+#include <linux/fuse.h>
+#include <linux/io_uring.h>
+
+#include "kselftest_harness.h"
+
+#define MNT "/tmp/fuse"
+
+/* A minimal io_uring with 128-byte SQEs, which FUSE_IO_URING_CMD_* needs */
+struct ring {
+ int fd;
+ void *sq, *cq;
+ size_t sq_sz, cq_sz, sqes_sz;
+ unsigned int *sq_tail, *sq_mask, *sq_array;
+ unsigned int *cq_head, *cq_tail, *cq_mask;
+ struct io_uring_sqe *sqes;
+ struct io_uring_cqe *cqes;
+};
+
+static void ring_exit(struct ring *r)
+{
+ if (r->sq && r->sq != MAP_FAILED)
+ munmap(r->sq, r->sq_sz);
+ if (r->cq && r->cq != MAP_FAILED)
+ munmap(r->cq, r->cq_sz);
+ if (r->sqes && r->sqes != MAP_FAILED)
+ munmap(r->sqes, r->sqes_sz);
+ if (r->fd >= 0)
+ close(r->fd);
+ memset(r, 0, sizeof(*r));
+ r->fd = -1;
+}
+
+static int ring_init(struct ring *r, unsigned int entries)
+{
+ struct io_uring_params p = { .flags = IORING_SETUP_SQE128 };
+ char *sq, *cq;
+
+ memset(r, 0, sizeof(*r));
+ r->fd = syscall(__NR_io_uring_setup, entries, &p);
+ if (r->fd < 0)
+ return -1;
+ r->sq_sz = p.sq_off.array + p.sq_entries * sizeof(unsigned int);
+ r->cq_sz = p.cq_off.cqes + p.cq_entries * sizeof(*r->cqes);
+ r->sqes_sz = p.sq_entries * 2 * sizeof(*r->sqes);
+ sq = r->sq = mmap(NULL, r->sq_sz, PROT_READ | PROT_WRITE, MAP_SHARED,
+ r->fd, IORING_OFF_SQ_RING);
+ cq = r->cq = mmap(NULL, r->cq_sz, PROT_READ | PROT_WRITE, MAP_SHARED,
+ r->fd, IORING_OFF_CQ_RING);
+ r->sqes = mmap(NULL, r->sqes_sz, PROT_READ | PROT_WRITE, MAP_SHARED,
+ r->fd, IORING_OFF_SQES);
+ if (sq == MAP_FAILED || cq == MAP_FAILED || r->sqes == MAP_FAILED) {
+ int err = errno;
+
+ ring_exit(r);
+ errno = err;
+ return -1;
+ }
+ r->sq_tail = (void *)(sq + p.sq_off.tail);
+ r->sq_mask = (void *)(sq + p.sq_off.ring_mask);
+ r->sq_array = (void *)(sq + p.sq_off.array);
+ r->cq_head = (void *)(cq + p.cq_off.head);
+ r->cq_tail = (void *)(cq + p.cq_off.tail);
+ r->cq_mask = (void *)(cq + p.cq_off.ring_mask);
+ r->cqes = (void *)(cq + p.cq_off.cqes);
+ return 0;
+}
+
+/* Return a zeroed SQE at the tail; publish via ring_commit()/ring_submit(). */
+static struct io_uring_sqe *ring_sqe(struct ring *r)
+{
+ unsigned int idx = *r->sq_tail & *r->sq_mask;
+ struct io_uring_sqe *sqe = &r->sqes[idx * 2];
+
+ memset(sqe, 0, 2 * sizeof(*sqe));
+ return sqe;
+}
+
+static void ring_commit(struct ring *r)
+{
+ unsigned int tail = *r->sq_tail;
+ unsigned int idx = tail & *r->sq_mask;
+
+ r->sq_array[idx] = idx;
+ __atomic_store_n(r->sq_tail, tail + 1, __ATOMIC_RELEASE);
+}
+
+static int ring_enter(struct ring *r)
+{
+ return syscall(__NR_io_uring_enter, r->fd, 1, 0, 0, NULL, 0);
+}
+
+static int ring_submit(struct ring *r)
+{
+ ring_commit(r);
+ return ring_enter(r);
+}
+
+/* Wait up to @ms for a CQE; return 1 and its result in @res, or 0. */
+static int ring_cqe(struct ring *r, int ms, int *res)
+{
+ struct pollfd pfd = { .fd = r->fd, .events = POLLIN };
+ unsigned int head = *r->cq_head;
+
+ if (head == __atomic_load_n(r->cq_tail, __ATOMIC_ACQUIRE)) {
+ int ret;
+
+ do {
+ ret = poll(&pfd, 1, ms);
+ } while (ret < 0 && errno == EINTR);
+ if (ret != 1 ||
+ head == __atomic_load_n(r->cq_tail, __ATOMIC_ACQUIRE))
+ return 0;
+ }
+ *res = r->cqes[head & *r->cq_mask].res;
+ __atomic_store_n(r->cq_head, head + 1, __ATOMIC_RELEASE);
+ return 1;
+}
+
+static int open_dev(struct __test_metadata *const _metadata, bool sync_init)
+{
+ int fd = open("/dev/fuse", O_RDWR | O_NONBLOCK);
+
+ if (fd < 0)
+ SKIP(return -1, "/dev/fuse: %s", strerror(errno));
+ if (sync_init && ioctl(fd, FUSE_DEV_IOC_SYNC_INIT)) {
+ if (errno == ENOTTY) {
+ close(fd);
+ SKIP(return -1, "no FUSE_DEV_IOC_SYNC_INIT (v6.18)");
+ }
+ ASSERT_EQ(0, errno);
+ }
+ return fd;
+}
+
+FIXTURE(fuse_dev) {
+ int fd;
+};
+
+FIXTURE_VARIANT(fuse_dev) {
+ bool sync_init;
+ int err; /* what a non-blocking call gets before the mount */
+};
+
+FIXTURE_VARIANT_ADD(fuse_dev, sync_init) {
+ .sync_init = true,
+ .err = EAGAIN,
+};
+
+FIXTURE_VARIANT_ADD(fuse_dev, plain) {
+ .err = EPERM,
+};
+
+FIXTURE_SETUP(fuse_dev)
+{
+ self->fd = open_dev(_metadata, variant->sync_init);
+}
+
+FIXTURE_TEARDOWN(fuse_dev)
+{
+ if (self->fd >= 0)
+ close(self->fd);
+}
+
+TEST_F_TIMEOUT(fuse_dev, poll, 2)
+{
+ struct pollfd pfd = { .fd = self->fd, .events = POLLIN };
+
+ ASSERT_EQ(!variant->sync_init, poll(&pfd, 1, 0));
+ if (!variant->sync_init)
+ ASSERT_EQ(POLLERR, pfd.revents);
+}
+
+static void *kick(void *efd)
+{
+ usleep(100 * 1000);
+ eventfd_write(*(int *)efd, 1);
+ return NULL;
+}
+
+/* libfuse's sync-init worker polls the fd together with an eventfd. */
+TEST_F_TIMEOUT(fuse_dev, poll_eventfd, 2)
+{
+ struct pollfd pfd[2] = {
+ { .fd = self->fd, .events = POLLIN },
+ { .fd = eventfd(0, 0), .events = POLLIN },
+ };
+ pthread_t thread;
+
+ ASSERT_LE(0, pfd[1].fd);
+ ASSERT_EQ(0, pthread_create(&thread, NULL, kick, &pfd[1].fd));
+ ASSERT_LE(1, poll(pfd, 2, -1));
+ if (variant->sync_init) {
+ ASSERT_EQ(0, pfd[0].revents);
+ ASSERT_EQ(POLLIN, pfd[1].revents);
+ } else {
+ ASSERT_EQ(POLLERR, pfd[0].revents);
+ }
+ ASSERT_EQ(0, pthread_join(thread, NULL));
+ close(pfd[1].fd);
+}
+
+TEST_F_TIMEOUT(fuse_dev, epoll, 2)
+{
+ struct epoll_event ev = { .events = EPOLLIN };
+ int epfd = epoll_create1(0);
+
+ ASSERT_LE(0, epfd);
+ ASSERT_EQ(0, epoll_ctl(epfd, EPOLL_CTL_ADD, self->fd, &ev));
+ ASSERT_EQ(!variant->sync_init, epoll_wait(epfd, &ev, 1, 0));
+ if (!variant->sync_init)
+ ASSERT_EQ(EPOLLERR, ev.events);
+ close(epfd);
+}
+
+TEST_F_TIMEOUT(fuse_dev, read, 2)
+{
+ char buf[FUSE_MIN_READ_BUFFER];
+
+ ASSERT_EQ(-1, read(self->fd, buf, sizeof(buf)));
+ ASSERT_EQ(variant->err, errno);
+}
+
+TEST_F_TIMEOUT(fuse_dev, splice, 2)
+{
+ int p[2];
+
+ ASSERT_EQ(0, pipe(p));
+ ASSERT_EQ(-1, splice(self->fd, NULL, p[1], NULL,
+ FUSE_MIN_READ_BUFFER, 0));
+ ASSERT_EQ(variant->err, errno);
+ close(p[0]);
+ close(p[1]);
+}
+
+/*
+ * open(2) doesn't call ->fasync for O_ASYNC, but the last close turns it
+ * off through ->fasync, and that must not wait for a mount either.
+ */
+TEST_F_TIMEOUT(fuse_dev, fasync_close, 2)
+{
+ int fd = open("/dev/fuse", O_RDWR | O_ASYNC);
+
+ ASSERT_LE(0, fd);
+ if (variant->sync_init)
+ ASSERT_EQ(0, ioctl(fd, FUSE_DEV_IOC_SYNC_INIT));
+ ASSERT_EQ(0, close(fd));
+}
+
+static void nop(int sig)
+{
+}
+
+/* A blocking read still waits for the mount, until a signal ends it. */
+TEST_F_TIMEOUT(fuse_dev, read_blocking, 3)
+{
+ struct sigaction sa = { .sa_handler = nop }; /* no SA_RESTART */
+ char buf[FUSE_MIN_READ_BUFFER];
+
+ ASSERT_EQ(0, sigaction(SIGALRM, &sa, NULL));
+ ASSERT_EQ(0, fcntl(self->fd, F_SETFL, 0));
+ alarm(1);
+ ASSERT_EQ(-1, read(self->fd, buf, sizeof(buf)));
+ alarm(0);
+ ASSERT_EQ(variant->sync_init ? EINTR : EPERM, errno);
+}
+
+static void prep(struct io_uring_sqe *sqe, __u8 opcode, int fd)
+{
+ sqe->opcode = opcode;
+ sqe->fd = fd;
+ if (opcode == IORING_OP_URING_CMD)
+ sqe->cmd_op = FUSE_IO_URING_CMD_REGISTER;
+ else
+ sqe->poll32_events = POLLIN;
+}
+
+TEST_F_TIMEOUT(fuse_dev, uring_cmd, 2)
+{
+ struct ring r;
+ int res;
+
+ if (ring_init(&r, 1))
+ SKIP(return, "io_uring_setup: %s", strerror(errno));
+ prep(ring_sqe(&r), IORING_OP_URING_CMD, self->fd);
+ ASSERT_EQ(1, ring_submit(&r));
+ ASSERT_EQ(1, ring_cqe(&r, 1000, &res));
+ ring_exit(&r);
+ if (res == -EOPNOTSUPP)
+ SKIP(return, "no CONFIG_FUSE_IO_URING");
+ ASSERT_EQ(-variant->err, res);
+}
+
+static void *submit(void *r)
+{
+ ring_enter(r);
+ return NULL;
+}
+
+/*
+ * io_uring issues the command and polls the fd with the ring's uring_lock
+ * held: another thread submitting to the same ring must not have to wait
+ * for the mount.
+ */
+static void ring_lock(struct __test_metadata *const _metadata, int fd,
+ __u8 opcode)
+{
+ struct timespec t0, t1;
+ pthread_t thread;
+ struct ring r;
+
+ if (ring_init(&r, 4))
+ SKIP(return, "io_uring_setup: %s", strerror(errno));
+ prep(ring_sqe(&r), opcode, fd);
+ ring_commit(&r);
+ ASSERT_EQ(0, pthread_create(&thread, NULL, submit, &r));
+ usleep(100 * 1000);
+ ring_sqe(&r)->opcode = IORING_OP_NOP;
+ ring_commit(&r);
+ clock_gettime(CLOCK_MONOTONIC, &t0);
+ ASSERT_EQ(1, ring_enter(&r));
+ clock_gettime(CLOCK_MONOTONIC, &t1);
+ ASSERT_GT(500, (t1.tv_sec - t0.tv_sec) * 1000 +
+ (t1.tv_nsec - t0.tv_nsec) / 1000000);
+ ASSERT_EQ(0, pthread_join(thread, NULL));
+ ring_exit(&r);
+}
+
+TEST_F_TIMEOUT(fuse_dev, ring_lock_cmd, 2)
+{
+ ring_lock(_metadata, self->fd, IORING_OP_URING_CMD);
+}
+
+TEST_F_TIMEOUT(fuse_dev, ring_lock_poll, 2)
+{
+ ring_lock(_metadata, self->fd, IORING_OP_POLL_ADD);
+}
+
+FIXTURE(mount) {
+ int fd;
+ int epfd;
+ struct ring r;
+ int err; /* errno of the thread that mounts or calls getxattr */
+};
+
+FIXTURE_VARIANT(mount) {
+ __u32 events; /* epoll events, or 0 for a multishot io_uring poll */
+};
+
+FIXTURE_VARIANT_ADD(mount, epoll) {
+ .events = EPOLLIN,
+};
+
+/* Edge triggered: only a wakeup reports the request after the mount */
+FIXTURE_VARIANT_ADD(mount, epoll_et) {
+ .events = EPOLLIN | EPOLLET,
+};
+
+/* The forwarded wakeup has to reach an exclusive waiter too */
+FIXTURE_VARIANT_ADD(mount, epoll_excl) {
+ .events = EPOLLIN | EPOLLEXCLUSIVE,
+};
+
+FIXTURE_VARIANT_ADD(mount, io_uring) {
+};
+
+static int write_file(const char *path, const char *val)
+{
+ int fd = open(path, O_WRONLY);
+ int ret = fd < 0 ? -1 : write(fd, val, strlen(val));
+
+ if (fd >= 0)
+ close(fd);
+ return ret == (int)strlen(val) ? 0 : -1;
+}
+
+FIXTURE_SETUP(mount)
+{
+ uid_t uid = getuid();
+ gid_t gid = getgid();
+ char map[32];
+
+ snprintf(map, sizeof(map), "0 %u 1", uid);
+ if (unshare(CLONE_NEWUSER | CLONE_NEWNS) ||
+ write_file("/proc/self/setgroups", "deny") ||
+ write_file("/proc/self/uid_map", map))
+ SKIP(return, "no user namespace: %s", strerror(errno));
+ snprintf(map, sizeof(map), "0 %u 1", gid);
+ ASSERT_EQ(0, write_file("/proc/self/gid_map", map));
+ ASSERT_EQ(0, mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL));
+ ASSERT_EQ(0, mount("tmpfs", "/tmp", "tmpfs", 0, NULL));
+ ASSERT_EQ(0, mkdir(MNT, 0755));
+ self->epfd = -1;
+ self->r.fd = -1;
+ self->fd = open_dev(_metadata, true);
+}
+
+FIXTURE_TEARDOWN(mount)
+{
+ umount2(MNT, MNT_DETACH);
+ if (self->epfd >= 0)
+ close(self->epfd);
+ ring_exit(&self->r);
+ if (self->fd >= 0)
+ close(self->fd);
+}
+
+static void *do_mount(void *arg)
+{
+ FIXTURE_DATA(mount) * self = arg;
+ char opts[64];
+
+ snprintf(opts, sizeof(opts),
+ "fd=%d,rootmode=40000,user_id=0,group_id=0", self->fd);
+ self->err = mount("fuse", MNT, "fuse", 0, opts) ? errno : 0;
+ return NULL;
+}
+
+static void *do_getxattr(void *arg)
+{
+ FIXTURE_DATA(mount) * self = arg;
+ char buf[16];
+
+ self->err = getxattr(MNT, "user.x", buf, sizeof(buf)) < 0 ? errno : 0;
+ return NULL;
+}
+
+/* Answer the queued requests: FUSE_INIT, and -ENODATA for the rest. */
+static void serve(struct __test_metadata *const _metadata, int fd)
+{
+ struct fuse_init_out init = {
+ .major = FUSE_KERNEL_VERSION,
+ .minor = FUSE_KERNEL_MINOR_VERSION,
+ .max_write = 4096,
+ };
+ __u64 buf[FUSE_MIN_READ_BUFFER / sizeof(__u64)];
+ struct fuse_in_header *in = (void *)buf;
+ struct fuse_out_header out;
+ struct iovec iov[2] = { { &out, sizeof(out) }, { &init, sizeof(init) } };
+ int n;
+
+ while (read(fd, buf, sizeof(buf)) >= (ssize_t)sizeof(*in)) {
+ n = in->opcode == FUSE_INIT ? 2 : 1;
+ out.unique = in->unique;
+ out.error = n == 2 ? 0 : -ENODATA;
+ out.len = sizeof(out) + (n == 2 ? sizeof(init) : 0);
+ ASSERT_EQ((ssize_t)out.len, writev(fd, iov, n));
+ }
+}
+
+/* Wait up to @ms for the waiter armed before the mount; return its events. */
+static int wait_event(FIXTURE_DATA(mount) * self, int ms)
+{
+ struct epoll_event ev;
+ int res;
+
+ if (self->epfd >= 0)
+ return epoll_wait(self->epfd, &ev, 1, ms) == 1 ? ev.events : 0;
+ return ring_cqe(&self->r, ms, &res) ? res : 0;
+}
+
+TEST_F_TIMEOUT(mount, wake, 5)
+{
+ struct epoll_event ev = { .events = variant->events };
+ pthread_t thread;
+
+ /* Arm the waiter before the mount */
+ if (variant->events) {
+ self->epfd = epoll_create1(0);
+ ASSERT_LE(0, self->epfd);
+ ASSERT_EQ(0, epoll_ctl(self->epfd, EPOLL_CTL_ADD, self->fd, &ev));
+ } else {
+ struct io_uring_sqe *sqe;
+
+ if (ring_init(&self->r, 4))
+ SKIP(return, "io_uring_setup: %s", strerror(errno));
+ sqe = ring_sqe(&self->r);
+ prep(sqe, IORING_OP_POLL_ADD, self->fd);
+ sqe->len = IORING_POLL_ADD_MULTI;
+ ASSERT_EQ(1, ring_submit(&self->r));
+ }
+
+ /* mount(2) waits for the reply to FUSE_INIT */
+ ASSERT_EQ(0, pthread_create(&thread, NULL, do_mount, self));
+ ASSERT_EQ(POLLIN, wait_event(self, 1000));
+ serve(_metadata, self->fd);
+ ASSERT_EQ(0, pthread_join(thread, NULL));
+ ASSERT_EQ(0, self->err);
+
+ /* Drain any leftover mount events before testing a new request */
+ while (wait_event(self, 100))
+ serve(_metadata, self->fd);
+
+ /* A request after the mount, without arming the waiter again */
+ ASSERT_EQ(0, pthread_create(&thread, NULL, do_getxattr, self));
+ ASSERT_EQ(POLLIN, wait_event(self, 1000));
+ serve(_metadata, self->fd);
+ ASSERT_EQ(0, pthread_join(thread, NULL));
+ ASSERT_EQ(ENODATA, self->err);
+}
+
+TEST_HARNESS_MAIN
--
2.56.1