Sparse VHDX leaks host space: vhdmp drops file-level TRIM when a request crosses an unmapped block
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 25/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- linux
- Ambito
- operating-systems
Direzione di ricerca
The report implicates Windows vhdmp.sys paths Vhd2iContinueFileTrim, VhdmpiVhd2HandleFileTrimSubIo, and VhdmpiVhd2ProcessSingleSrbRange; it does not identify WSL source files or tests for these components. Start by checking whether the suspected trim handling is in code maintained by this repository and reproduce with the listed sparse VHDX steps. Done means the full deleted extent is released to the host for both default and one-block discard limits.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Windows Version
Microsoft Windows [Version 10.0.20348.5761] (Windows Server 2022, vhdmp.sys 10.0.20348.5756)
WSL Version
3.0.3.0
Are you using WSL 1, WSL 2, or WSLC?
- WSL 2
- WSL 1
- WSLC
Kernel Version
6.18.54.1-microsoft-standard-WSL2
Distro Version
Ubuntu 24.04.5 LTS
Other Software
None relevant.
Repro Steps
Summary: on a sparse VHDX, part of every guest TRIM is never passed to NTFS. The blocks become UNMAPPED in the BAT but keep their host clusters, and later TRIMs do not recover them. This looks like a bug in how vhdmp.sys handles a TRIM that spans both present and already-unmapped blocks, and it may explain reports such as #12103.
Setup: wsl --manage <distro> --set-sparse true --allow-unsafe. The root filesystem is ext4 mounted with discard. The VHDX block size and the guest discard_granularity are both 1 MiB.
-
On the host, measure the size on disk (the file's reported size does not change):
Add-Type -Namespace W -Name K -MemberDefinition '[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] public static extern uint GetCompressedFileSizeW(string n, out uint hi);' $hi = 0; $lo = [W.K]::GetCompressedFileSizeW('<path>\ext4.vhdx', [ref]$hi) '{0:N1} MiB on disk' -f ((([uint64]$hi -shl 32) + $lo) / 1MB) -
In the distro, as root, write a file, then measure on the host:
dd if=/dev/urandom of=/root/trimtest.bin bs=1M count=256 conv=fsync status=none; sync -
Delete it, then measure on the host again:
rm -f /root/trimtest.bin; sync; sync; sleep 6; sync -
Repeat steps 2 and 3, but before deleting, limit each UNMAP to one VHDX block:
echo 1048576 > /sys/block/<root device>/queue/discard_max_bytesThis value cannot be written back: the kernel rejects both the original
4294966784and 4 GiB.wsl --shutdownrestores it.
Expected Behavior
Deleting the file returns about 256 MiB to the host, whatever the discard_max_bytes value.
Actual Behavior
| Round | discard_max_bytes |
Size on disk released by the delete |
|---|---|---|
| A | default (4294966784) |
187.9 MiB of 256 MiB |
| B | 1048576 |
256.0 MiB |
| C | default, after wsl --shutdown |
187.9 MiB of 256 MiB |
The amount lost depends on the free-space layout. Over a longer period, this disk accumulated 137.7 GiB of UNMAPPED blocks that still held clusters, in a file using 528.3 GiB on disk while the guest used 356.7 GiB. Some of that may predate the current sparse setup (see Limits below).
Suspected cause
From traces of the guest requests and static analysis of vhdmp.sys with public symbols:
-
The guest TRIM reaches past the freed extent. ext4 extends it to the end of the free run, for online
discardandfstrimalike. For a 40 MiB extent at 9752 MiB,block_rq_issueshows19972096 + 84416sectors, which is[9752, 9793.2)MiB. The blocks after the extent are usually already unmapped. -
Vhd2iContinueFileTrimforgets the pending run at a non-present block. Present blocks (state 6 or 7) are accumulated byVhd2iExtendRangeinto a file-contiguous run. Other blocks take this path, which moves the start of the pending run without callingVhd2iDispatchRange:vhdmp!Vhd2iContinueFileTrim+0xa1b4: add dword ptr [rbx+1C8h],edi ; advance the position in the request mov edx,dword ptr [rbx+1C8h] mov dword ptr [rbx+1FCh],edx ; start of pending run = current position -
Queued runs can be lost too.
VhdmpiVhd2HandleFileTrimSubIoholds runs until it has 32 or receives the final dispatch. If the request ends on a non-present block,VhdmpiVhd2ProcessSingleSrbRangehas nothing pending, so it makes no final dispatch and the queued runs are not sent. -
The loss is permanent and silent. The file trim still completes successfully, so
VhdmpiVhd2SrbRangeCompletegoes on to the block trim and the BAT entries becomeUNMAPPED. Later TRIMs skip those blocks because they are no longer present.
Evidence
- The on/off comparison above is repeatable and reversible.
- Applying rules 2 and 3 to traced requests predicted the outcome of 1021 of 1023 blocks across four file layouts. The two mismatches are unexplained.
- With WSL stopped,
FSCTL_FILE_LEVEL_TRIMsent from user mode to these stale ranges released their clusters immediately, so NTFS can free them. Its conditions inNtfsFileLevelTrim(sparse attribute, alignment, length) also do not depend on position. - Serial
fstrimand onlinediscardlose the same blocks, and runningfstrimagain over them recovers nothing.
Limits of this analysis
- I did not trace the host-side FSCTLs, so steps 2 to 4 are inferred from the disassembly and the prediction above. The field meanings are my interpretation.
- I tested one Windows build. I did not check other builds or a freshly created distro.
- This VHDX is not pristine. In October 2026 it failed to attach, had 27 inconsistent BAT entries repaired by hand, and was rewritten and made sparse again. Stale ranges were later released by hand with
FSCTL_SET_ZERO_DATA. The test blocks above are freshly written each round, so this should not affect the repro, but it may account for part of the 137.7 GiB.
Suggested fix (untested)
In the skip path of Vhd2iContinueFileTrim, dispatch the pending run before moving its start, and make sure queued runs are flushed when a request ends on a non-present block.
Workaround
Set discard_max_bytes to 1048576 in the guest, so each UNMAP covers at most one block. A 268 MiB TRIM took about 174 ms instead of about 23 ms. The setting does not persist across a VM restart, and I have not measured its effect on foreground I/O.
Related: #12103, #13241.
Diagnostic Logs
No log archive attached. Microsoft-Windows-VHDMP-Operational and Microsoft-Windows-Ntfs/Operational recorded no warnings or errors on the day of these tests.
- Lingua principale
- C++
- Stelle
- 34.4k
- Fork
- 1.9k
- Merge medio
- 1g 23h
- PR unite (30g)
- 166
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di microsoft/WSL
-
feature wsl2
Difficoltà 2/5 1-3 ore Idoneità per principianti 80/100
microsoft/WSL#42306 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
microsoft/WSL#42114 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
feature kernel wsl2
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
microsoft/WSL#41107 · 3 commenti · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
-
feature kernel
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
microsoft/WSL#41054 · 9 commenti · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
-
feature GPU
Difficoltà 1/5 1-3 ore Idoneità per principianti 70/100
microsoft/WSL#13592 · 4 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di microsoft/WSL
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
HarperFast/rocksdb-js#1131 ·
I maintainer di solito rispondono entro 1 giorno
-
area/daemon bug good first issue platform/linux priority/p2
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
ToshiNamura/Deimos#5 ·
-
needs-format
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
Niko1221/Strata#6251 · 1 commento ·
I maintainer di solito rispondono entro 5 giorni
-
[Bug]: NP 管理器文件编辑器里删除键无效Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
metasequoiaime/msime#11666 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
patrick-hudson/put.gd-desktop#1679 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno