Skip to content

virtio: improve virtiofs and VirtioProxy performance with per-device SWIOTLB pool#40654

Open
benhillis wants to merge 1 commit into
masterfrom
user/benhill/swiotlb-squashed
Open

virtio: improve virtiofs and VirtioProxy performance with per-device SWIOTLB pool#40654
benhillis wants to merge 1 commit into
masterfrom
user/benhill/swiotlb-squashed

Conversation

@benhillis
Copy link
Copy Markdown
Member

@benhillis benhillis commented May 27, 2026

Improves virtiofs and VirtioProxy performance by giving each virtio device its own SWIOTLB aperture instead of sharing a single global pool. The guest kernel reserves a contiguous physical range at boot, publishes the (base, size), and the host programs a matching per-device aperture in wsldevicehost.

Handshake:

  1. Guest kernel allocates a contiguous range (alloc_contig_pages, __GFP_DMA32 | __GFP_ZERO) and exposes it under /sys/bus/vmbus/drivers/hv_pci/swiotlb_{base,size}.
  2. mini_init (WSL2) and the WSLC init handler read the sysfs files and return the values in LX_INIT_GUEST_CAPABILITIES / WSLC_GET_GUEST_CAPABILITIES_RESULT.
  3. WslCoreVm::ReadGuestCapabilities and WSLCVirtualMachine::ReadGuestCapabilities capture them. WSLC forwards them via the new HcsVirtualMachine::ApplyGuestCapabilities IDL method (taking a WSLCGuestCapabilities struct).
  4. Both VM owners format swiotlb=0x{base:x},{size} once into m_swiotlbOption and pass it to AddGuestDevice / AddSharePath for every virtiofs share and virtio-net adapter (VirtioProxy networking).

Bumps Microsoft.WSL.Kernel to 6.18.26.3-1 (first official kernel with the patch) and Microsoft.WSL.DeviceHost to 1.2.29-0 (consumes the swiotlb device-options token).

Copilot AI review requested due to automatic review settings May 27, 2026 17:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces an end-to-end “guest-published SWIOTLB pool” handshake: the Linux guest (WSL kernel) publishes an hv_pci SWIOTLB pool (base/size), the Windows side reads and forwards it, and device creation paths pass a swiotlb=... token to wsldevicehost so it can program a matching per-device aperture (virtio-fs and virtio-net).

Changes:

  • Add guest→host capability plumbing for hv_pci SWIOTLB pool (new init/WSLC messages, service/session forwarding, and telemetry logging).
  • Thread a cached swiotlb=0x{base:x},{size} token into virtio device creation (VirtioFS shares and VirtioProxy/virtio-net).
  • Add .wslconfig parsing/validation and user-facing localization for SWIOTLB/virtio-related warnings, plus unit test coverage.

Reviewed changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
test/windows/UnitTests.cpp Adds unit tests validating .wslconfig experimental.swiotlb parsing and rejection of malformed values.
src/windows/wslcsession/WSLCVirtualMachine.h Adds per-VM guest capability fields and a ReadGuestCapabilities() step.
src/windows/wslcsession/WSLCVirtualMachine.cpp Implements guest capability query over the init channel and forwards to the service via COM.
src/windows/service/inc/wslc.idl Adds WSLCGuestCapabilities struct and a new ApplyGuestCapabilities method on IWSLCVirtualMachine.
src/windows/service/exe/WslCoreVm.h Stores hv_pci SWIOTLB base/size and a cached SWIOTLB device-options token.
src/windows/service/exe/WslCoreVm.cpp Reads hv_pci SWIOTLB info, caches formatted token, passes it to VirtioNetworking, and appends it to VirtioFS share options.
src/windows/service/exe/HcsVirtualMachine.h Stores a cached SWIOTLB token for WSLC virtio device creation paths.
src/windows/service/exe/HcsVirtualMachine.cpp Computes default SWIOTLB request size, passes it to kernel cmdline helper, threads token into VirtioNetworking/AddShare, and implements ApplyGuestCapabilities.
src/windows/common/WslCoreConfig.h Adds experimental.swiotlb key constant and config field SwiotlbSizeBytes.
src/windows/common/WslCoreConfig.cpp Parses experimental.swiotlb, validates virtio dependencies, and computes default SWIOTLB sizing only when relevant virtio paths are enabled.
src/windows/common/VirtioNetworking.h Extends ctor to accept SWIOTLB token to pass into virtio-net device creation.
src/windows/common/VirtioNetworking.cpp Passes SWIOTLB token into virtio-net/loopback device creation calls.
src/windows/common/helpers.hpp Extends AppendCommonKernelCommandLine to accept SWIOTLB size and adds ComputeDefaultSwiotlbConfig.
src/windows/common/helpers.cpp Appends swiotlb=force hv_pci_swiotlb=<bytes> when requested; implements default sizing helper.
src/windows/common/GuestDeviceManager.h Adds m_vmIdOption member for device-host ownership tagging.
src/windows/common/GuestDeviceManager.cpp Appends a vm_id= option to device share names and avoids adding an empty options delimiter.
src/shared/inc/lxinitshared.h Adds WSLC message types for guest capability query and adds SWIOTLB fields to LX_INIT_GUEST_CAPABILITIES.
src/linux/init/WSLCInit.cpp Handles WSLC guest capability query by reading hv_pci SWIOTLB sysfs and returning base/size.
src/linux/init/util.h Introduces HvPciSwiotlbPool and declaration for reading the sysfs pool info.
src/linux/init/util.cpp Implements sysfs reads for swiotlb_base/swiotlb_size with a safe fallback to zeros.
src/linux/init/main.cpp Includes SWIOTLB base/size in LX_INIT_GUEST_CAPABILITIES sent to the service.
packages.config Bumps Microsoft.WSL.DeviceHost and Microsoft.WSL.Kernel packages for SWIOTLB aperture/handshake support.
localization/strings/en-US/Resources.resw Adds new localized messages for VirtioProxy fallback and missing-kernel-patch SWIOTLB warning.

Comment thread src/windows/service/inc/wslc.idl
Comment thread src/windows/service/exe/HcsVirtualMachine.cpp
@benhillis benhillis force-pushed the user/benhill/swiotlb-squashed branch from a8be2fb to 5041235 Compare May 27, 2026 23:15
@benhillis benhillis marked this pull request as ready for review May 27, 2026 23:15
@benhillis benhillis requested a review from a team as a code owner May 27, 2026 23:15
Copilot AI review requested due to automatic review settings May 27, 2026 23:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 3 comments.

Comment thread src/windows/service/inc/wslc.idl
Comment on lines +431 to +435
if (m_hvPciSwiotlbBase != 0 && m_hvPciSwiotlbSize != 0)
{
m_swiotlbOption = std::format(L"swiotlb=0x{:x},{}", m_hvPciSwiotlbBase, m_hvPciSwiotlbSize);
}
else if (m_vmConfig.SwiotlbSizeBytes != 0)
Comment thread src/windows/service/exe/HcsVirtualMachine.cpp
@benhillis benhillis force-pushed the user/benhill/swiotlb-squashed branch from 5041235 to 2620337 Compare May 27, 2026 23:23
asherkariv
asherkariv previously approved these changes May 27, 2026
Copilot AI review requested due to automatic review settings May 27, 2026 23:30
@benhillis benhillis force-pushed the user/benhill/swiotlb-squashed branch from 2620337 to 82a476c Compare May 27, 2026 23:30
@benhillis benhillis force-pushed the user/benhill/swiotlb-squashed branch from 82a476c to fe4406e Compare May 27, 2026 23:38
@benhillis benhillis changed the title swiotlb: forward guest-published hv_pci pool to the device host virtio: improve virtiofs and VirtioProxy performance with per-device SWIOTLB pool May 27, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 22 out of 22 changed files in this pull request and generated 1 comment.

Comment on lines +422 to +423
// swiotlb device-options token. Passing zero for both means the guest kernel does not
// support hv_pci swiotlb; the service then omits the token and emits a user warning.
…SWIOTLB pool

Improves virtiofs and VirtioProxy performance by giving each virtio
device its own SWIOTLB aperture instead of sharing a single global
pool. The guest kernel reserves a contiguous physical range at boot,
publishes the (base, size), and the host programs a matching
per-device aperture in wsldevicehost.

  1. The WSL kernel allocates a contiguous range at boot
     (alloc_contig_pages with __GFP_DMA32 | __GFP_ZERO) and exposes
     the chosen physical (base, size) under
       /sys/bus/vmbus/drivers/hv_pci/swiotlb_{base,size}

  2. mini_init (WSL2) and the WSLC init handler read those sysfs files
     and return the values in LX_INIT_GUEST_CAPABILITIES and
     WSLC_GET_GUEST_CAPABILITIES_RESULT respectively.

  3. WslCoreVm::ReadGuestCapabilities and
     WSLCVirtualMachine::ReadGuestCapabilities capture the values.
     WSLC forwards them to wslservice via the new
     HcsVirtualMachine::ApplyGuestCapabilities IDL method (with a
     WSLCGuestCapabilities struct so future kernel-published values
     can be added without bumping the interface IID).

  4. Both VM owners format "swiotlb=0x{base:x},{size}" once into
     m_swiotlbOption and pass it verbatim to AddGuestDevice /
     AddSharePath for every virtiofs share and virtio-net adapter
     (VirtioProxy networking). wsldevicehost consumes the token and
     creates the per-device SWIOTLB aperture.

If the kernel does not publish the sysfs files (older kernel) both
values come back as zero, the host omits the device-options token,
and the WSL2 path emits a one-time user warning via
MessageSwiotlbKernelUnsupported so users understand why performance
is degraded. (The WSLC path always uses the bundled kernel, so the
warning does not apply there.)

Other changes:

  * Bump Microsoft.WSL.Kernel to 6.18.26.3-1, which is the first
    official kernel that publishes the hv_pci swiotlb_{base,size}
    sysfs files this PR consumes.

  * Bump Microsoft.WSL.DeviceHost to 1.2.29-0 for the device-side
    SWIOTLB aperture support.

  * Default pool sizing moves to helpers::ComputeDefaultSwiotlbConfig
    and is only requested on the kernel command line when a virtio
    device that needs bounce buffers (VirtioFs / Virtio9p /
    VirtioProxy) is in use.

  * Telemetry: emit GuestKernelInfo / WSLCReadGuestCapabilities /
    WSLCApplyGuestCapabilities events with the kernel-chosen base
    and size so we can validate the handshake in CI.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@benhillis benhillis force-pushed the user/benhill/swiotlb-squashed branch from fe4406e to 1ee87b2 Compare May 27, 2026 23:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants