Thread View: muc.lists.freebsd.bugs
1 messages
1 total messages
Started by bugzilla-noreply
Fri, 13 Nov 2026 01:59
[Bug 300734] tmpfs: read(2) into a writable MAP_SHARED mapping of the same file page hangs forever
Author: bugzilla-noreply
Date: Fri, 13 Nov 2026 01:59
Date: Fri, 13 Nov 2026 01:59
102 lines
6048 bytes
6048 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=300734
Bug ID: 300734
Summary: tmpfs: read(2) into a writable MAP_SHARED mapping of
the same file page hangs forever
Product: Base System
Version: 15.1-RELEASE
Hardware: amd64
OS: Any
Status: New
Severity: Affects Only Me
Priority: ---
Component: kern
Assignee: bugs@FreeBSD.org
Reporter: grepjava@gmail.com
On a tmpfs file, read(2) or pread(2) whose destination buffer is a writable
MAP_SHARED mapping of the same page of that file never returns. The thread
sleeps waiting for a page it has busied itself. The sleep cannot be
interrupted, so the process cannot be killed, and the machine cannot finish
a shutdown: reboot(8) stops after "All buffers synced", and only a power
cycle recovers it.
The same program on ZFS (and, by the code, UFS) returns normally.
Reproducer (as an ordinary user, on a tmpfs mount with default options):
/* cc -o selfmap selfmap.c && ./selfmap /mnt/tmpfs */
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int
main(int argc, char **argv)
{
char data[8192], path[256];
char *map;
int fd;
snprintf(path, sizeof(path), "%s/selfmap", argv[1]);
fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
memset(data, 'x', sizeof(data));
if (fd < 0 || pwrite(fd, data, sizeof(data), 0) != sizeof(data))
return (2);
map = mmap(NULL, 8192, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED)
return (2);
printf("pread...\n");
fflush(stdout);
printf("pread returned %zd\n", pread(fd, map, 8192, 0));
return (0);
}
On /tmp (ZFS) it prints "pread returned 8192". On tmpfs it prints
"pread..." and hangs. procstat -kk of the hung thread:
mi_switch+0xbc _vm_page_busy_sleep+0xff vm_fault_busy_sleep+0xb8
vm_fault+0xa81 vm_fault_trap+0x4d trap_pfault+0x1fc calltrap+0x8
uiomove_fromphys+0x12e uiomove_object+0x170 VOP_READ_APV+0x1c
vn_read+0x201 vn_io_fault+0x1e9 dofileread+0x80 kern_preadv+0x61
sys_pread+0xf5 amd64_syscall+0x126 fast_syscall_common+0xf8
Cause, as far as I can tell:
tmpfs_read() copies with uiomove_object(), which shared-busies the file's
page (vm_page_grab_valid_unlocked(..., VM_ALLOC_SBUSY)) and then copies to
the user buffer with uiomove_fromphys(). The destination is a mapping of
that same page, not yet faulted in for writing, so the copyout faults, and
the write fault waits for the page to be unbusied, which only this thread
can do.
UFS and ZFS avoid this through vn_io_fault(): they set MNTK_NO_IOPF, so
the user buffer's pages are held before the filesystem busies anything,
and the filesystem copies with vn_io_fault_uiomove()/vn_io_fault_pgmove().
tmpfs doesn't set MNTK_NO_IOPF (tmpfs_mount() sets MNTK_LOOKUP_SHARED,
MNTK_EXTENDED_SHARED, MNTK_NOMSYNC and MNTK_FPLOOKUP), so vn_io_fault()
calls VOP_READ directly with the buffer unheld. main has the same code.
A possible fix is for tmpfs to set MNTK_NO_IOPF and have
uiomove_object_page() copy with vn_io_fault_pgmove() rather than
uiomove_fromphys() when the uio comes through vn_io_fault (TDP_UIOHELD).
The write side (write(2) from such a mapping) may deserve the same look;
tools/test/stress2/misc/tmpfs6.sh exercises that direction.
Seen on FreeBSD 15.1-RELEASE releng/15.1-n283562-96841ea08dcf GENERIC,
amd64 (AMD Ryzen 3 PRO 4350G), tmpfs mounted with default options
("mount -t tmpfs -o size=16m tmpfs /mnt/x"). Found while testing an
out-of-tree module (an io_uring implementation, loaded at the time), but
the reproducer above uses only open(2), pwrite(2), mmap(2) and pread(2),
none of which go through it, and the stack above has none of its
functions.
--
You are receiving this mail because:
You are the assignee for the bug.
--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads