kernel: limit FUSE readdir replies to one page with a flat vmalloc (#127)

This commit is contained in:
minchenlee
2026-12-02 19:58:35 +08:00
committed by GitHub
parent 303320394b
commit bba44ebc06
4 changed files with 253 additions and 2 deletions

View File

@@ -29,7 +29,8 @@ patches (the arch panic hook, modpost on Wasm) are his as they were, and
the arch patch, which keeps about a quarter of its lines from his port,
names him in a `Co-developed-by` trailer (keep it when rewording that
patch). The virtio-gpu damage-clip backports keep their upstream author,
the BPF arena patch is Greg Bell's (from unLAB), and every other patch
the BPF arena patch is Greg Bell's (from unLAB), the FUSE readdir patch
is minchenlee's, and every other patch
is Pooya Parsa's (set `user.name` and `user.email` in the kernel tree,
so new commits are too).
@@ -43,7 +44,8 @@ kernel/build.sh patches # kernel/tools/export-patches.sh: the patches and serie
```
- One patch per piece, in this order: generic changes (Joel's two, stock
LLVM, the mm changes (`VMALLOC_FLAT`, percpu-km with it, BPF arena
LLVM, the mm changes (`VMALLOC_FLAT`, FUSE readdir replies of one page
with it, percpu-km with it, BPF arena
maps left out with it, the `folio_ref_add()` skip, `drop_caches()`
for callers in the kernel), `BPF_CGDEV_ONLY` (a bpf(2) for cgroup
device filters only), 9p `metatimeout`, the kthread park

View File

@@ -136,9 +136,11 @@ static int failures;
} while (0)
#ifdef __aarch64__
#include <dirent.h>
#include <setjmp.h>
#include <sys/auxv.h>
#include <linux/fb.h>
#include <linux/fuse.h>
#include <linux/futex.h>
#include <poll.h>
#include <sys/epoll.h>
@@ -2985,6 +2987,208 @@ static void ninep_checks(void)
CHECK(strstr(buf, "\t9p\n") != NULL, "9p: file system type %s", strstr(buf, "\t9p\n") ? "registered" : "MISSING from /proc/filesystems");
}
/* More than a page of records, so the guest must continue at the right offset. */
#define FUSE_TEST_ENTRIES 256
/*
* The largest READDIR(PLUS) size the kernel asked for, set by the daemon in
* shared memory. VMALLOC_FLAT can map only one page for sure, so a larger
* request means the kernel could fail with ENOMEM, even when the pages it
* got this time happened to be contiguous.
*/
static uint32_t *fuse_test_max_size;
static void fuse_test_serve(int fd, int plus)
{
union {
struct fuse_in_header hdr;
char bytes[FUSE_MIN_READ_BUFFER];
} req;
struct {
struct fuse_out_header hdr;
union {
struct fuse_init_out init;
struct fuse_attr_out attr;
struct fuse_open_out open;
struct fuse_direntplus align;
char bytes[32768];
} data;
} reply;
while (read(fd, req.bytes, sizeof(req.bytes)) >= (ssize_t)sizeof(req.hdr)) {
size_t size = 0;
int error = 0;
switch (req.hdr.opcode) {
case FUSE_INIT:
reply.data.init = (struct fuse_init_out){
.major = FUSE_KERNEL_VERSION, .minor = 31,
.flags = FUSE_MAX_PAGES | (plus ? FUSE_DO_READDIRPLUS : 0),
.max_pages = 32, .max_write = 4096,
};
size = sizeof(reply.data.init);
break;
case FUSE_GETATTR:
reply.data.attr = (struct fuse_attr_out){
.attr = { .ino = FUSE_ROOT_ID, .mode = S_IFDIR | 0755, .nlink = 2 },
};
size = sizeof(reply.data.attr);
break;
case FUSE_OPENDIR:
reply.data.open = (struct fuse_open_out){};
size = sizeof(reply.data.open);
break;
case FUSE_READDIR:
case FUSE_READDIRPLUS: {
struct fuse_read_in in;
size_t start = plus ? FUSE_NAME_OFFSET_DIRENTPLUS : FUSE_NAME_OFFSET;
if (req.hdr.opcode != (plus ? FUSE_READDIRPLUS : FUSE_READDIR)) {
error = -EIO;
break;
}
memcpy(&in, req.bytes + sizeof(req.hdr), sizeof(in));
if (in.size > *fuse_test_max_size)
*fuse_test_max_size = in.size;
for (uint64_t i = in.offset; i < FUSE_TEST_ENTRIES; i++) {
char name[16];
size_t len = snprintf(name, sizeof(name), "entry-%03u", (unsigned int)i);
size_t reclen = FUSE_DIRENT_ALIGN(start + len);
char *record = reply.data.bytes + size;
struct fuse_dirent *entry = (struct fuse_dirent *)(record + start - FUSE_NAME_OFFSET);
if (size + reclen > in.size || size + reclen > sizeof(reply.data.bytes))
break;
memset(record, 0, reclen);
entry->ino = i + 2;
entry->off = i + 1;
entry->type = DT_REG;
entry->namelen = len;
memcpy(entry->name, name, len);
size += reclen;
}
break;
}
case FUSE_FORGET:
case FUSE_BATCH_FORGET:
continue;
case FUSE_RELEASEDIR:
case FUSE_DESTROY:
break;
default:
error = -ENOSYS;
break;
}
reply.hdr = (struct fuse_out_header){
.len = sizeof(reply.hdr) + size, .error = error, .unique = req.hdr.unique,
};
if (write(fd, &reply, reply.hdr.len) != (ssize_t)reply.hdr.len)
break;
}
}
static void fuse_test_list(const char *path, size_t count, int plus)
{
union {
struct linux_dirent64 {
uint64_t ino;
int64_t off;
unsigned short reclen;
unsigned char type;
char name[];
} align;
char bytes[32768];
} buf;
unsigned int seen = 0;
int fd = open(path, O_RDONLY | O_DIRECTORY);
int err = fd < 0 ? errno : 0;
while (!err) {
ssize_t n = syscall(SYS_getdents64, fd, buf.bytes, count);
if (n <= 0) {
err = n < 0 ? errno : 0;
break;
}
for (size_t pos = 0; pos < (size_t)n;) {
struct linux_dirent64 *entry = (struct linux_dirent64 *)(buf.bytes + pos);
char expected[16];
snprintf(expected, sizeof(expected), "entry-%03u", seen);
if (strcmp(entry->name, expected) || entry->off != seen + 1) {
err = EIO;
break;
}
seen++;
pos += entry->reclen;
}
}
if (fd >= 0)
close(fd);
CHECK(!err && seen == FUSE_TEST_ENTRIES, "fuse: %s with a %zu-byte buffer: %u entries, %s",
plus ? "readdirplus" : "readdir", count, seen, strerror(err));
}
static void fuse_checks(void)
{
char path[] = "/tmp/kwfuse-XXXXXX";
char node[sizeof(path) + sizeof("/dev")], opts[96];
fuse_test_max_size = mmap(NULL, sizeof(*fuse_test_max_size), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (fuse_test_max_size == MAP_FAILED) {
CHECK(0, "fuse: mmap: %s", strerror(errno));
return;
}
if (!mkdtemp(path)) {
CHECK(0, "fuse: mkdtemp: %s", strerror(errno));
munmap(fuse_test_max_size, sizeof(*fuse_test_max_size));
return;
}
snprintf(node, sizeof(node), "%s/dev", path);
for (int plus = 0; plus <= 1; plus++) {
int fd = open("/dev/fuse", O_RDWR);
pid_t daemon;
if (fd < 0 && errno == ENOENT && !mknod(node, S_IFCHR | 0600, makedev(10, 229))) {
fd = open(node, O_RDWR);
unlink(node);
}
if (fd < 0) {
CHECK(0, "fuse: open device: %s", strerror(errno));
break;
}
snprintf(opts, sizeof(opts), "fd=%d,rootmode=40000,user_id=0,group_id=0", fd);
if (mount("kwtest", path, "fuse", 0, opts)) {
CHECK(0, "fuse: mount: %s", strerror(errno));
close(fd);
break;
}
*fuse_test_max_size = 0;
daemon = fork();
if (!daemon) {
fuse_test_serve(fd, plus);
_exit(0);
}
close(fd);
if (daemon > 0) {
fuse_test_list(path, 4096, plus);
fuse_test_list(path, 32768, plus);
CHECK(*fuse_test_max_size == (uint32_t)getpagesize(), "fuse: %s request size %u (expect one page, %d)",
plus ? "readdirplus" : "readdir", *fuse_test_max_size, getpagesize());
} else {
CHECK(0, "fuse: fork: %s", strerror(errno));
}
if (umount2(path, MNT_DETACH))
CHECK(0, "fuse: unmount: %s", strerror(errno));
if (daemon > 0) {
kill(daemon, SIGKILL);
waitpid(daemon, NULL, 0);
}
}
rmdir(path);
munmap(fuse_test_max_size, sizeof(*fuse_test_max_size));
}
/*
* kwcam (VIDEO_KWCAM in kw_defconfig, which selects VIDEO_DEV): the V4L2
* core registers its character major whether or not the host has a camera
@@ -4837,6 +5041,10 @@ int main(int argc, char **argv)
*/
if (getpid() == 1)
mkdir("/tmp", 01777);
if (argc > 1 && !strcmp(argv[1], "fuse")) {
fuse_checks();
goto done;
}
/* Only the SMP checks (`rdinit=/usr/bin/kwtest -- smp` repeats them faster). */
if (argc > 1 && !strcmp(argv[1], "smp")) {
mkdir("/proc", 0555);
@@ -4967,6 +5175,7 @@ int main(int argc, char **argv)
input_handler_checks();
vsock_checks();
ninep_checks();
fuse_checks();
camera_checks();
shm_checks();
fb_checks();

View File

@@ -0,0 +1,39 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: minchenlee <110967458+minchenlee@users.noreply.github.com>
Date: Tue, 1 Dec 2026 13:18:16 +0800
Subject: [PATCH] fuse: limit readdir replies to one page with flat vmalloc
FUSE maps READDIR and READDIRPLUS replies with vm_map_ram(). The bulk
page allocator may return scattered pages, which VMALLOC_FLAT cannot
map. The directory read then fails with ENOMEM despite free memory.
Use one page for the reply with VMALLOC_FLAT. Subsequent getdents calls
continue from the last accepted directory offset. A server that allows
names longer than about 4000 bytes (name_max above a page) cannot fit
such an entry in one page; that is left for a contiguous allocation.
---
fs/fuse/readdir.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/fs/fuse/readdir.c b/fs/fuse/readdir.c
--- a/fs/fuse/readdir.c
+++ b/fs/fuse/readdir.c
@@ -380,11 +380,16 @@ static int fuse_readdir_uncached(struct file *file, struct dir_context *ctx)
struct fuse_io_args ia = {};
struct fuse_args_pages *ap = &ia.ap;
void *buf;
- size_t bufsize = clamp((unsigned int) ctx->count, PAGE_SIZE, fc->max_pages << PAGE_SHIFT);
+ size_t bufsize = PAGE_SIZE;
u64 attr_version = 0, evict_ctr = 0;
bool locked;
- struct page **pages = fuse_readdir_alloc_buf(ap, &bufsize);
+ struct page **pages;
+ /* Flat vmalloc can only map physically contiguous pages. */
+ if (!IS_ENABLED(CONFIG_VMALLOC_FLAT))
+ bufsize = clamp((unsigned int) ctx->count, PAGE_SIZE,
+ fc->max_pages << PAGE_SHIFT);
+ pages = fuse_readdir_alloc_buf(ap, &bufsize);
if (!pages)
return -ENOMEM;

View File

@@ -2,6 +2,7 @@ kernel-Allow-arch-specific-panic-handling.patch
NOMERGE-kbuild-Provide-Wasm-support-in-mk_elfconfig.patch
wasm-allow-building-with-stock-LLVM-no-WasmAsmParser.patch
mm-add-VMALLOC_FLAT-a-vmalloc-that-returns-direct-ma.patch
fuse-limit-readdir-replies-to-one-page-with-flat-vma.patch
mm-allow-percpu-km-on-SMP-with-a-flat-vmalloc.patch
bpf-leave-arena-maps-out-with-a-flat-vmalloc.patch
mm-skip-folio_ref_add-of-zero-when-mapping-an-order-.patch