🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: muc.lists.freebsd.bugs
Article #439

[Bug 300734] tmpfs: read(2) into a writable MAP_SHARED mapping of the same file page hangs forever

#439
From: bugzilla-noreply
Date: Fri, 13 Nov 2026 01:59
102 lines
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
Technical Details
Message-ID: <bug-300734-227@https.bugs.freebsd.org/bugzilla/>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mail
From: bugzilla-noreply@freebsd.org
Newsgroups: muc.lists.freebsd.bugs
Subject: [Bug 300734] tmpfs: read(2) into a writable MAP_SHARED mapping of
 the same file page hangs forever
Date: Fri, 13 Nov 2026 01:59:07 +0000
Organization: Newsgate at muc.de e.V.
Approved: news-admin@muc.de
Distribution: world
Message-ID: <bug-300734-227@https.bugs.freebsd.org/bugzilla/>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Info: news.muc.de; posting-host="news.muc.de:193.149.48.2";
	logging-data="26234"; mail-complaints-to="news-admin@muc.de"
X-Original-To: bugs@mlmmj.nyi.freebsd.org
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim;
	t=1794535147;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:mime-version:mime-version:content-type:content-type:
	 content-transfer-encoding:content-transfer-encoding;
	bh=T4FrHCIhrAg/VnqfO/PmjtLRlRPGJ+AUuVEdbgLRm3U=;
	b=rPYULNVDvo+X+1JlGsLXC23peaHP7sGeUvb410C32d9XXpnjBKfs4vTaQ8/IFduSMaoQF3
	Gf8JIlEaffydHZEEJuAdiK9mTevSS1pgm73NLaXg/MxySqdCtVxg4k1ol3MY8pZRauHhxL
	WcAAmc5k1H4YOlk6DQY5Ej0d7gXq0fOCJzDsu6tmV2SSqbuVAnucbBQWHFjZEQX4tCyWAB
	oGAydbvCAHx1gEmmdzoXvjJLAhMcphxJkksyGg6Bw2RbtW8U7OZ58eLalM7DDk5RR+XPz1
	tuHSA6h2hHuq2D70l2Dkn0dicsBNreA6t9dxBzelQqHbJCSUmi1l3hemMFCfow==
X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f
Auto-Submitted: auto-generated
Precedence: list
X-Newsgate-Id: f02c70b8849f+
X-No-Archive: Yes