Sparse VHDX leaks host space: vhdmp drops file-level TRIM when a request crosses an unmapped block

Aperta
#42,179 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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

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

file system hypervisor-platform wsl2
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.

  1. 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)
    
  2. 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
    
  3. Delete it, then measure on the host again:

    rm -f /root/trimtest.bin; sync; sync; sleep 6; sync
    
  4. Repeat steps 2 and 3, but before deleting, limit each UNMAP to one VHDX block:

    echo 1048576 > /sys/block/<root device>/queue/discard_max_bytes
    

    This value cannot be written back: the kernel rejects both the original 4294966784 and 4 GiB. wsl --shutdown restores 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:

  1. The guest TRIM reaches past the freed extent. ext4 extends it to the end of the free run, for online discard and fstrim alike. For a 40 MiB extent at 9752 MiB, block_rq_issue shows 19972096 + 84416 sectors, which is [9752, 9793.2) MiB. The blocks after the extent are usually already unmapped.

  2. Vhd2iContinueFileTrim forgets the pending run at a non-present block. Present blocks (state 6 or 7) are accumulated by Vhd2iExtendRange into a file-contiguous run. Other blocks take this path, which moves the start of the pending run without calling Vhd2iDispatchRange:

    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
    
  3. Queued runs can be lost too. VhdmpiVhd2HandleFileTrimSubIo holds runs until it has 32 or receives the final dispatch. If the request ends on a non-present block, VhdmpiVhd2ProcessSingleSrbRange has nothing pending, so it makes no final dispatch and the queued runs are not sent.

  4. The loss is permanent and silent. The file trim still completes successfully, so VhdmpiVhd2SrbRangeComplete goes on to the block trim and the BAT entries become UNMAPPED. 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_TRIM sent from user mode to these stale ranges released their clusters immediately, so NTFS can free them. Its conditions in NtfsFileLevelTrim (sparse attribute, alignment, length) also do not depend on position.
  • Serial fstrim and online discard lose the same blocks, and running fstrim again 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

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di microsoft/WSL

Tutte le issue di microsoft/WSL

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.