Skip to content

fs/v9fs: Do not release the directory fid twice in unlink. - #22065

Open
casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:fb-v9fs-unlink-fid-up
Open

casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:fb-v9fs-unlink-fid-up

Conversation

@casaroli

Copy link
Copy Markdown
Contributor

Summary

v9fs_client_unlink() clunks the fid of the directory after Tunlinkat, and its callers v9fs_vfs_unlink() and v9fs_vfs_rmdir() then release the same fid again with v9fs_fid_put().

After the clunk the fid number is free, and another thread of the mount can get it at once for a new walk. The second release then finds the fid of that thread: it drops its reference, clunks it and frees it while the thread still uses it. v9fs_client_open() and v9fs_client_create() then write the iounit into the freed memory. That breaks the free list of the kernel heap, and the next malloc() or free() faults at address 0x10018 (the iounit is 0x10000). Later requests of that thread use a fid that the server does not know any more. With only one thread, the second release finds no fid and does nothing, so this happens only when threads use the mount at the same time; on SMP it happens within seconds.

Tunlinkat does not change the directory fid. This PR makes v9fs_client_unlink() only send the request. Its callers release the fid, as they do after the other requests.

Impact

fs/v9fs only, one function. No API, ABI or Kconfig change. The same in flat, protected and kernel builds, on every architecture and transport.

Testing

qemu-armv8a:knsh under HVF (Apple M-series), CONFIG_MM_FILL_ALLOCATIONS=y, the root file system on virtio-9p. A test program starts 3 threads that each open (O_CREAT), write, close and unlink files in one directory of the 9p mount, for 30 s; then the same for 30 s in rounds where a child process does it for up to 100 ms and joins its threads.

Tree Result
master, 4 CPUs (CONFIG_SMP) kernel fault in the first seconds: data abort at address 0x10018 in mm_malloc() from v9fs_vfs_open()
master, 4 CPUs, with checks for the test only the fid that v9fs_client_create() uses is freed during its request, by v9fs_vfs_unlink() of another thread, after v9fs_client_unlink() of that thread had clunked its own fid with the same number
master, 1 CPU no fault (57837 operations, 572 rounds): the second release comes right after the clunk, and another thread runs in between only if a time slice ends there
this PR, 4 CPUs threads: 41342 operations, 0 errors; processes: 569 rounds, 0 errors; no fault
this PR, 1 CPU threads: 55326 operations, 0 errors; processes: 571 rounds, 0 errors

With this PR, mkdir, rm and rmdir on the 9p mount work as before, and the kernel heap does not grow over the runs (a fid that is not released would stay in the heap).

tools/checkpatch.sh -c -u -m -g passes. Documentation: no change.

v9fs_client_unlink() clunked the fid of the directory after Tunlinkat,
and v9fs_vfs_unlink() and v9fs_vfs_rmdir() then released the same fid
again with v9fs_fid_put().  After the clunk the fid number is free, and
another thread can get it for a new walk at once.  The second release
then dropped the reference of that thread: its fid was clunked and freed
while the thread still used it.  v9fs_client_open() and
v9fs_client_create() then wrote the iounit into the freed memory, which
broke the free list of the kernel heap (a fault in mm_malloc() or
mm_free() at address 0x10018), and later requests used a fid that the
server did not know any more.  Three threads that create and remove
files in one directory on 4 CPUs did this within seconds.

Tunlinkat does not change the directory fid.  v9fs_client_unlink() now
only sends the request, and the callers release the fid, as they do
after the other requests.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli
casaroli requested a review from jerpelea as a code owner November 19, 2026 14:25
@github-actions github-actions Bot added Area: File System File System issues Size: S The size of the change in this PR is small labels Nov 19, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: File System File System issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants