Re: [PATCH] 9p: fix data corruption with writeback caching during concurrent stat
From: Michael Mulqueen
Date: Wed Nov 18 2026 - 07:22:10 EST
Hi,
I've encountered this bug and have spent some time confirming it and
writing a minimal reproducer script for it.
I've bisected it with my reproducer script and found it was introduced
in v6.4, specifically commit d9bc0d11e33b. I wasn't sure about the
etiquette of jumping straight into a discussion and tagging regzbot, so
I haven't, but I'm pretty confident that this is where the regression
was introduced. That is the commit that adds the writeback path -
before it, cache=mmap did not buffer writes through the page cache, so
a stale getattr had no unsent data to zero.
I cannot reproduce it on cache=readahead, only on the writeback path
that cache=mmap uses.
The reproducer writes with O_APPEND in one thread while another thread
stats the file and then reads the server copy back at the end. It
writes for a fixed period of 5 seconds, which is why the number of
records in the output differs, but it reproduces reliably on every run.
It needs diod installed and has to be run as root. Here's the output on
the current v7.3 release:
cache=mmap: appending for 5s while a reader stat()s the file...
-> 689268 NUL bytes in the server's copy, 13897 of 16642 records lost
cache=readahead: appending for 5s while a reader stat()s the file...
-> 0 NUL bytes in the server's copy, 0 of 10179 records lost
I have tried the fixes (Pierre Barre's v1 and v2, 27 December 2025, and
David Howells' sketch, 18 February 2026) discussed in this thread and
they appear to resolve it in the basic case. Pierre's apply cleanly to
v7.3; David's I rebased by hand (its step 5 is already present as
c60ae98c5aa6). My reproducer is too minimal to address all the points
raised in this thread, it only confirms that it's still present in the
latest kernel and that it regressed from v6.3 to v6.4. I'll try to find
some more time in the next couple of weeks to test the patches
comprehensively, but I wanted toshare what I already have in case it is
useful to anyone else looking into this.
--- repro-b.py --------------------------------------------------------
#!/usr/bin/env python3
import os, re, shutil, socket, subprocess, tempfile, threading, time
base = tempfile.mkdtemp(prefix="p9b.")
exp = base + "/export"
mnt = base + "/mnt"
os.mkdir(exp)
os.mkdir(mnt)
port = 5700 + os.getpid() % 200
srv = subprocess.Popen(
["diod", "-f", "-n", "-l", f"127.0.0.1:{port}", "-e", exp],
stderr=subprocess.PIPE)
for _ in range(100):
try:
socket.create_connection(("127.0.0.1", port), 0.2).close()
break
except OSError:
time.sleep(0.05)
else:
srv.terminate()
raise SystemExit("diod failed to start:\n"
+ srv.stderr.read().decode())
print(f"diod up on 127.0.0.1:{port}", flush=True)
def run(mode, seconds=5):
opts = (f"trans=tcp,port={port},version=9p2000.L,cache={mode},"
f"aname={exp},uname=root,access=any,ignoreqv")
subprocess.run(["mount", "-t", "9p", "-o", opts,
"127.0.0.1", mnt], check=True)
log = mnt + "/app.log"
open(log, "w").close()
print(f"cache={mode}: appending for {seconds}s while a reader "
f"stat()s the file...", flush=True)
stop = threading.Event()
def poll():
while not stop.is_set():
try:
os.stat(log)
except OSError:
pass
threading.Thread(target=poll, daemon=True).start()
fd = os.open(log, os.O_WRONLY | os.O_APPEND)
n = 0
end = time.time() + seconds
while time.time() < end:
os.write(fd, b"%08d " % n + b"x" * 90 + b"\n")
n += 1
os.fsync(fd)
os.close(fd)
stop.set()
data = open(exp + "/app.log", "rb").read()
nul = data.count(0)
intact = len(set(re.findall(rb"(\d{8}) x{90}", data)))
subprocess.run(["umount", "-l", mnt], stderr=subprocess.DEVNULL)
print(f" -> {nul} NUL bytes in the server's copy, "
f"{n - intact} of {n} records lost", flush=True)
try:
run("mmap")
run("readahead")
finally:
srv.terminate()
srv.wait()
shutil.rmtree(base, ignore_errors=True)
--- end repro-b.py ----------------------------------------------------
Cheers,
Mike