Repository navigation
init: size hvsocket listener buffers so host-initiated connections avoid order-7 ring allocations - #41690
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
A moderate concern remains about unmeasured throughput regression on bulk process I/O.
Review effort: Lite
Findings: None
What changed in this PR
This PR reduces HvSocket listener buffers to avoid order-7 ring allocation failures under guest memory fragmentation.
Changes:
- Adds a shared listener buffer-size constant.
- Configures send/receive buffers before listening.
- Applies sizing to control, stdio, interop, and WSLC listeners.
- Throughput impact for bulk process I/O remains unresolved.
| File | Summary |
|---|---|
src/shared/inc/lxinitshared.h |
Defines the listener buffer size. |
src/linux/init/WSLCInit.cpp |
Sizes WSLC accept listeners. |
src/linux/init/util.h |
Updates the listener API. |
src/linux/init/util.cpp |
Applies socket buffer options. |
src/linux/init/init.cpp |
Sizes session and stdio listeners. |
src/linux/init/config.cpp |
Sizes the interop listener. |
src/linux/init/binfmt.cpp |
Sizes Windows interop listeners. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
@microsoft-github-policy-service agree |
…oid order-7 ring allocations
hv_sock sizes the VMBus rings of a connection the host opens into the
guest from the listening socket's SO_SNDBUF/SO_RCVBUF. Since kernel 5.5
(c0cfa2d8a788, "vsock: add multi-transports support") a listener keeps
net.core.wmem_default/rmem_default (212992 by default), so every accepted
connection needs one physically contiguous order-7 block (52 + 52 pages).
When guest memory is fragmented that allocation fails, the offer is
dropped, and new sessions and interop hang ("UtilAcceptVsock ...
accept4 failed 110").
Add an optional SocketBuffer to UtilListenVsockAnyPort, like
UtilConnectVsock, and pass LX_INIT_HVSOCKET_LISTEN_BUFFER_SIZE (14336,
which the kernel doubles to 28672) at the listeners that accept control
and stdio connections. Their rings become 7 + 7 pages (8 + 8 on 6.18),
an order-4 allocation. Listeners that move bulk data (NAT localhost
relay, import/export, WSLC port relay, WSLC_FORK) and the Plan 9 server
are unchanged.
Related: microsoft#40795
53e76c4 to
f970ae6
Compare
|
This is closely related to the order-7 VMBus ring allocation failure, and I am working on a complementary kernel-side upstream series. The series changes VMBus ring allocation to fall back through smaller page orders and hardens buffer ownership/cleanup when GPADL operations have uncertain outcomes. It is maintained separately from this WSL userspace change in the kernel fork. The latest hosted run 36148296003 passed all six patch stages on x86_64 and arm64, the separate WSL backport build, and x86_64 KUnit 14/14. The The ordinary x86_64 Hyper-V runtime test covered normal GPADL create/teardown and UIO/ring mmap on an earlier series snapshot. The hosted injected tests do not prove live allocator fragmentation or host-response/rescind interleaving. SEV-SNP, TDX, and Arm CCA memory-transition tests also remain open. The kernel work may complement the listener-buffer reduction here; it does not replace the userspace change. I have not measured its effect on the throughput tradeoff discussed in this PR, and I am not claiming full CoCo qualification or universal support. |
|
Hi, this is affecting my day-to-day WSL use too. On WSL 3.0.3 with kernel 6.18.54.1, intermittent I have captured I also tried a temporary 48-hour change to Could a maintainer trigger the pending CI build and advise what further validation would be most useful? I would appreciate getting this tested properly, as the intermittent failures are a recurring hindrance. Thanks for working on it. |
|
Field report in favour of this per-listener approach over the global sysctl workaround from #40795. Environment: WSL 3.0.3.0, kernel 6.18.54.1-microsoft-standard-WSL2, Windows 11 26200.9711, Ubuntu 24.04, Still reproduces on 3.0.3. The relay leak from #41242 is gone (0 orphaned with 5.7 GB The global sysctl workaround breaks Docker Desktop. and after a restart Docker Desktop sits on "waiting for the Docker API: context deadline exceeded" until both values are set back to 212992. Sizing only WSL's own listeners, as this PR does, avoids that, which is why I would like to see it merged. Happy to test a build on this machine if that helps. |
Resolve the conflict in UtilListenVsockAnyPort with the EINTR retry on listen(): the listener's send and receive buffers are set first, then listen() runs with the retry.
There was a problem hiding this comment.
🔵 Needs a closer look
Reported 18–24% stdio throughput losses warrant maintainer review of the reliability/performance tradeoff, with full-build validation and representative benchmarks for current 64 KiB relays and WSLC still pending.
No open issues 🎉
Review effort: Balanced
|
Ben Hillis (@benhillis) Feng Wang (@chemwolf6922), could you please start the pipelines ( The hang this PR mitigates still affects 3.0.3, the current stable release:
The global Thank you. |
|
Another field report in favour of this per-listener approach, from a machine already running the global sysctl workaround. Environment: WSL 3.0.1.0, kernel 6.18.40.1-microsoft-standard-WSL2, Ubuntu with systemd, Docker Engine inside WSL, uptime 8 days, many parallel terminal sessions. Workaround in place: What still happened: with order 6 and order 7 both empty in the Normal zone an interop call (
So the global 65536 narrows the window but does not close it, and it cannot go lower without breaking Docker. Shrinking only init's listeners, as this PR does, is the fix we need. Would be great to see this reviewed. |
|
same here on a second machine, also running the global 65536 workaround (rmem_default/wmem_default via kernelCommandLine in .wslconfig). WSL 2.7.8.0, kernel 6.18.33.1, docker engine 29.9.2 inside wsl, 16 GB ram. about 6h uptime. no and one hv_sock ring already failed at order 6, around 4h20m in: i think that's the plan 9 ring, since the other listeners only get 64 KB rings with this sysctl. so the global setting helps for now, but not much room left after a few hours. would be nice to get this per listener fix merged. |
|
I would also benefit from the per-listener approach here. I run long-lived development agents in Ubuntu 24.04 on WSL2, with Windows interop and Docker Desktop. My setup has previously hit new-session/interop hangs with the On 15 November, my running kernel is 6.18.33.2-microsoft-standard-WSL2, and the read-only checks show: Those are mitigations, not a validation of this PR. My interop check currently succeeds; I have not built or tested this patch and am not claiming a new failure under today's settings. I also reproduced a contributing Docker behaviour independently on 14 November: after booting without Docker, compaction was 20 and watermark boost 15000. Starting Docker Desktop 4.99 changed both to 0, and a targeted kernel trace captured The listener-specific change is valuable because it avoids relying on smaller kernel-wide socket defaults shared with Docker, and I understand it does not fix every VMBus ring allocation. Is an official candidate build available, or can the required CI/maintainer validation be identified so a build can be offered for controlled testing? I would need a maintenance window before testing a replacement WSL build; I do not want to interrupt the active workloads to apply an unvalidated patch. |
📌 Still reproduces on WSL 3.0.5.0 / kernel 6.18.58.1, and freeing memory did not helpTwo points the earlier field reports (all ≤ 3.0.3) do not cover:
🔁 What happensflowchart TD
A["Host opens connection<br/>(terminal tab, interop)"] --> C{"512 KiB contiguous<br/>(order-7) free?"}
C -- yes --> G["✅ session opens"]
C -- no --> D["order:7 alloc failure<br/>offer dropped"]
D --> F["accept4 failed 110<br/>⏱️ 10–30 s hang"]
🖥️ Environment
📊 Observations (VM up since 11-11 17:36, ~8 days)
🧹 Freeing memory did not help
After the cleanup, 👍 +1 for running the pipelines and getting this reviewed. Drafted with Claude Code from local logs on the affected machine. |
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
This PR is a solid userspace mitigation for existing kernels (bringing host-opened listener rings down from order-7 to order-4). As noted in the description and comments above, high-order contiguous allocations (including order-4 listeners and order-6 Plan 9 rings) can still fail once buddy fragmentation sets in. As a complementary kernel-side fix, I just sent v3 of the upstream VMBus series to
What changed between v2 and v3 (addressing maintainer feedback)Wei Liu and Michael Kelley rightly pushed back on v2: it had ballooned to 14 patches (+4,515 / -427 lines) mixing VMBus ring allocation, unrelated For v3, I stripped the series down and reworked it around their feedback:
Quick WSL2 6.18.64.1 backport noteI also backported this onto
Across ~106–121 VMBus channels ( Having PR #41690 in WSL init still helps users on unpatched kernels right away, while the VMBus order-0 ring change removes the contiguous-order ceiling on the kernel side. |

Summary of the Pull Request
Under guest memory fragmentation, new WSL sessions and Windows interop hang:
page allocation failure: order:7invmbus_alloc_ring, thenUtilAcceptVsock ... accept4 failed 110(#40795). For a connection the host opens, hv_sock sizes both VMBus rings from the listening socket's buffers. Init's listeners, other than the Plan 9 server, set none, so they inheritnet.core.wmem_default/rmem_default(212992), and every accepted connection needs one physically contiguous order-7 block. This PR setsSO_SNDBUF/SO_RCVBUFto 14336 (doubled by the kernel to 28672) on the listeners that accept control and stdio connections, except WSLC's thread forks (see Not changed). Each of those connections then needs an order-4 block, the same as a connection the guest opens.PR Checklist
doc/do not describe hvsocket buffer or ring sizes.Detailed Description of the Pull Request / Additional comments
Root cause (kernel
linux-msft-wsl-6.6.114.1; WSL line numbers are master8a9533901ae5before this change)hvs_open_connection()sizes both rings of a host-initiated connection from the listener:max(sk_sndbuf, 6 pages), capped at 64 pages (hyperv_transport.c#L372-L381).hvs_sock_init()(#L453-L454). Since c0cfa2d8a788 ("vsock: add multi-transports support", v5.5) it runs only atconnect()and for the accepted child, never on a listener. A listener keeps whatsock_init_data()copied fromwmem_default/rmem_default(sock.c#L3444-L3445).UtilListenVsockAnyPort(util.cpp:1596) sets no buffer size.vmbus_alloc_ring()allocates both rings as one physically contiguous block with no smaller fallback (channel.c#L167-L192): 104 pages, order 7. If the allocation fails, the offer is dropped and the connection never reachesaccept(), so a timedUtilAcceptVsocksetsETIMEDOUT(util.cpp:268) and logsaccept4 failed 110(:281).VMBUS_RING_SIZE()adds a 4 KB header page only to rings under 32 KB (hyperv.h#L183-L189), so 212992 is order 7 there too.Change
UtilListenVsockAnyPorttakes an optionalSocketBuffer, likeUtilConnectVsock, and setsSO_SNDBUF/SO_RCVBUFbeforelisten().LX_INIT_HVSOCKET_LISTEN_BUFFER_SIZE(14336) inlxinitshared.h, passed at:init.cpp:1232)wsl.exeprocess stdio and control (init.cpp:1421)config.cpp:906)binfmt.cpp:153)CreateLinuxProcess(ProcessCreateProcessMessage,util.cpp:3456)WSLC_ACCEPT, the stdio of processes WSLC starts in the VM's root namespace (WSLCInit.cpp:272)WSLC_FORKprocess and pty forks (WSLCInit.cpp:569), which carry only control messages: every root-namespace process launch (WSLCVirtualMachine.cpp:836, pty:883), the port-relay setup (:1054) and the 9P mount setup (:1245)Not changed
main.cpp:1139), import/export (main.cpp:2529) and the WSLC port relay (WSLCInit.cpp:890), because they carry bulk data.WSLC_FORKthread forks (WSLCInit.cpp:569). They include the relay to/var/run/docker.sock(DockerHTTPClient::ConnectSocket), which carries bulk Docker traffic such asimages/loadand container stdio (attach,exec), and the guest cannot tell that relay from the other thread forks by fork type, so all thread forks keep the default. Docker API calls, including containerattachandexec, can therefore still need an order-7 ring.plan9.cpp:318-320: 65536, doubled to 131072, order 6).Why 14336: it is the value tested below. Any
SocketBufferup to 14336, or exactly 16384, is order 4 on both kernels. Values in between are order 5 on 6.18, which still adds the header page below 32 KB. Order 5 and above is also the range free page reporting works on: WSL boots the kernel withpage_reporting.page_reporting_order(5 on the machine tested), and the reporting worker takes up to 32 not-yet-reported free blocks of that order or above off the free lists while it reports them to the host (page_reporting.c#L279, #L201). Order-4 blocks are never taken. I have not measured how much this matters.Why in init: it works on the 6.6 and 6.18 kernels already shipped, and it leaves the bulk listeners alone. A kernel change would complement it: use the listener's buffers only when they were set explicitly, as before 5.5, or allocate a non-contiguous ring as proposed in #41634.
Related: #40519 (protects WSL's own processes under memory pressure) and #41252 (times out the create-process accepts, so leaked relays exit; #41242) are both merged and address the same hang. Neither changes the ring size.
Risks
wsl.exe, interop or WSLC root-namespace process stdio. Measured (see Validation): on 2.7.3, which relays stdio in 4 KiB chunks, bulk data from Windows into Linux was about 18-24% slower with 28 KB rings, and Linux to Windows was within about 4% at the median. On 3.0.1, which has Change default relay buffer size to 64KiB #41603's 64 KiB relays, the loss is larger: with this PR's 32 KB rings, bulk stdio ran at 0.31-0.37 of the 208 KB speed from Windows to Linux and 0.57-0.59 from Linux to Windows (about 520-600 MiB/s, against 940-1,680 MiB/s). The fallback is 32768 (a 64 KB ring, order 5), which measured about 2,000 and 1,070 MB/s in the same commit, and on 3.0.1 0.54-0.58 of the 208 KB speed from Windows to Linux and 0.97-1.05 from Linux to Windows. From Linux to Windows, every size from 64 KB up ran at about the same speed (median 860-990 MiB/s), so something other than the ring, probably the Windows reader, limits that direction from 64 KB up and could hide a smaller ring effect there. I have field data for order-4 rings only (below), not for order 5.setsockoptfails, the listener is not created (so the session, interop call,CreateLinuxProcesscall, or WSLC process launch, port relay or 9P mount fails), the same as inUtilConnectVsock. For these options, only an LSM or a cgroup setsockopt BPF program can make it fail. I can change this to log and continue if you prefer.accept4 failed 110after its 10-second accept timeout. No size the listener can request avoids the out-of-memory case, since order 4 is the smallest. This makes the failure much rarer, not impossible.Validation Steps Performed
clang-format19.1.7 with the repo's.clang-formatmakes no changes to the seven files.Compiled the patched
UtilListenVsockAnyPorton its own (g++ with stub macros, not the repo build) and ran it on 6.6.114.1.SO_SNDBUF/SO_RCVBUFread back doubled afterlisten(). The Plan 9 server's listener sets 65536, and every traced 9P ring is 131072, so the listener's setting does size these rings.Same ring size without this build:
net.core.wmem_default/rmem_default= 28672 on WSL 2.7.3.0 / kernel 6.6.114.1, with everyvmbus_alloc_ring()traced by a kprobe.cmd.exe /c verpassed 294/294; for 84 of those runs, no free block of order 7 or larger existed.binfmt.cpp:153. It did not exerciseconfig.cpp:906,ProcessCreateProcessMessageor WSLC, which is not in 2.7.3.Full build and CI: Azure Pipelines build 394557 of
c1a655f61merged into master35052e9c1succeeded: formatting and localization checks, x64 and arm64 builds, packaging, and the wsl1, wsl2 and wslc test sessions on rs_prerelease.A kprobe trace with this build at default sysctls: not run yet.
Throughput on WSL 2.7.3.0 / kernel 6.6.114.1, emulated through
net.core.wmem_default/rmem_default(the same listenersk_sndbuf/sk_rcvbufthis PR sets), with every run's ring sizes confirmed by thevmbus_alloc_ring()kprobe: 2 GiB per transfer, five runs of five rounds, sizes interleaved, each compared with the 208 KB run in the same round (median [range], n rounds):SocketBuffer32768)wsl.exestdinwsl.exestdout2.7.3 relays stdio in 4 KiB chunks, so these ratios understate the effect on a release with Change default relay buffer size to 64KiB #41603. The Linux-side writer and reader used on these paths, connected by a plain Linux pipe, ran at 776-1,054 MiB/s, against at most 549 MiB/s on any path. None of the 3,062 traced ring allocations failed. The 28 KB arm was in three of the five runs, and 31 of the 100 planned 208 KB runs were skipped because free order-7 blocks were scarce, so n varies.
Throughput on a release with Change default relay buffer size to 64KiB #41603 (first in 2.9.13 pre-release and 3.0.1 stable): WSL 3.0.1.0 / kernel 6.18.40.1, the same emulation and kprobe, 2 GiB per transfer, two runs of five rounds, sizes interleaved, each compared with the 208 KB run in the same round (median [range], n = 10 for every cell):
SocketBuffer32768)wsl.exestdinwsl.exestdoutThe 208 KB medians were 937-1,678 MiB/s and the 32 KB medians 516-603 MiB/s. The interop paths have no Linux pipe. The first run had load 3-7 on 12 CPUs, the second 0.2-2.3. In the second run's two rounds below load 0.6, the 32 KB ratios were 0.33-0.36 from Windows to Linux and 0.53-0.66 from Linux to Windows, so load does not explain the loss. The high ends of the
wsl.exestdin ranges come from one 208 KB run slowed by load (813 MiB/s) in the first run. All 168 runs' ring sizes were confirmed, none of the ring allocations failed, and no 208 KB run was skipped. WSLC root-namespace process stdio is not measured.18 days of daily use with the same emulation (
net.core.wmem_default/rmem_default= 28672 from 2026-10-13, WSL 3.0.1.0, seven boots, kernel log of every boot). The kernel printed 157 page allocation failures and suppressed 3,695 more (warn_allocratelimit), so the counts below are lower bounds. The setting also shrank the listeners this PR leaves alone, and rings for connections the guest opens are order 4 as well, so these rings are not all from the listeners this PR changes. 25 printed failures were order-4vmbus_alloc_ringrings (hvs_probe): 21 in the two periods when the guest ran out of memory and swap (OOM kills), and 4 with 2.3-4.0 GiB of swap free. There were 16accept4 failed 110timeouts: 15 in the two out-of-memory periods, and 1, from an interop call bywsl-pro-service, ten seconds after one of the 4 other failures (interop processes log only to their own stderr, so this count is also a lower bound). Order-6 hv_sock rings, the size the Plan 9 server sets, failed 58 times on 14 of the 18 days, none with swap full and 3 at or below the low watermark, so fragmentation alone fails order-6 rings here. The other 74 were 9P client allocations (67, order 4) and other callers (7).6.18, the same emulation on WSL 3.0.1.0 / kernel 6.18.40.1: a vsock listener reads
SocketBuffer14336 back as 28672, and with listener buffers of 28672 every traced ring was 32768 bytes (8 + 8 pages with the header page, order 4). Listener buffers of 32770, 65536 and 212992 traced as rings of 36864, 65536 and 212992, matching the source. None of the 672 traced ring allocations failed. The ring sizing inhvs_open_connection(),VMBUS_RING_SIZE()andvmbus_alloc_ring()is unchanged in 6.18.54.1 (WSL 3.0.3).