Move printk.devkmsg and page_reporting_order to kernel command line (#40380)

Move printk.devkmsg=on from post-boot runtime writes in init to the kernel command line, ensuring it is applied at boot for both WSL2 and WSLC VMs. Also adds AppendCommonKernelCommandLine() helper in helpers.hpp/helpers.cpp to share common kernel parameters (hv_utils.timesync_implicit=1, printk.devkmsg=on, and page_reporting.page_reporting_order) between WslCoreVm and HcsVirtualMachine. N.B. kernel.dmesg_restrict, kernel.print-fatal-signals, and fs.inotify.max_user_watches are sysctl-only settings that cannot be set via kernel command line, so they remain as runtime writes in init. Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Ben Hillis committed May 1, 2026 at 14:32 UTC ce674a6d58fce89cf2d6b927ae22a8a5439cd48e
6 files changed +18 -28
src/linux/init/WSLCInit.cpp
-9
@@ -947,15 +947,6 @@ int WSLCEntryPoint(int Argc, char* Argv[])
947 return -1;
948 }
949
950 - //
951 - // Disable rate limiting of user writes to dmesg.
952 - //
953 -
954 - if (WriteToFile("/proc/sys/kernel/printk_devkmsg", "on\n") < 0)
955 - {
956 - return -1;
957 - }
958 -
950 //
951 // Set the ephemeral port range
952 //
src/linux/init/main.cpp
-9
@@ -1480,15 +1480,6 @@ Return Value:
1480 return -1;
1481 }
1482
1483 - //
1484 - // Disable rate limiting of user writes to dmesg.
1485 - //
1486 -
1487 - if (WriteToFile("/proc/sys/kernel/printk_devkmsg", "on\n") < 0)
1488 - {
1489 - return -1;
1490 - }
1491 -
1483 //
1484 // Set the hostname.
1485 //
src/windows/common/helpers.cpp
+12
@@ -730,3 +730,15 @@ try
730 wsl::windows::common::registry::WriteString(dcatKey.get(), nullptr, L"Version", registeredVersion.c_str());
731 }
732 CATCH_LOG()
733 +
734 +void wsl::windows::common::helpers::AppendCommonKernelCommandLine(_Inout_ std::wstring& kernelCmdLine, _In_ int pageReportingOrder)
735 +{
736 + // Enable timesync workaround to sync on resume from sleep in modern standby.
737 + kernelCmdLine += L" hv_utils.timesync_implicit=1";
738 +
739 + // Disable rate limiting of user writes to dmesg.
740 + kernelCmdLine += L" printk.devkmsg=on";
741 +
742 + // Configure page reporting order - minimum order of pages reported as free to the hypervisor.
743 + kernelCmdLine += std::format(L" page_reporting.page_reporting_order={}", pageReportingOrder);
744 +}
src/windows/common/helpers.hpp
+2
@@ -201,4 +201,6 @@ bool TryAttachConsole();
201
202 void RegisterWithDcat(_In_ bool IncludeVersionNumber = true);
203
204 +void AppendCommonKernelCommandLine(_Inout_ std::wstring& kernelCmdLine, _In_ int pageReportingOrder);
205 +
206 } // namespace wsl::windows::common::helpers
src/windows/service/exe/HcsVirtualMachine.cpp
+2 -5
@@ -111,11 +111,8 @@ HcsVirtualMachine::HcsVirtualMachine(_In_ const WSLCSessionSettings* Settings)
111 std::wstring kernelCmdLine = L"initrd=\\" LXSS_VM_MODE_INITRD_NAME L" " TEXT(WSLC_ROOT_INIT_ENV) L"=1 panic=-1";
112 kernelCmdLine += std::format(L" nr_cpus={}", Settings->CpuCount);
113
114 - // Enable timesync workaround to sync on resume from sleep in modern standby.
115 - kernelCmdLine += L" hv_utils.timesync_implicit=1";
116 -
117 - // Configure page reporting order - minimum order of pages reported as free to the hypervisor.
118 - kernelCmdLine += std::format(L" page_reporting.page_reporting_order={}", pageReportingOrder);
114 + // Append common kernel parameters shared between WSL2 and WSLC.
115 + helpers::AppendCommonKernelCommandLine(kernelCmdLine, pageReportingOrder);
116
117 // Setup dmesg collector with optional DmesgOutput handle.
118 // TODO: move dmesg collector to user session process.
src/windows/service/exe/WslCoreVm.cpp
+2 -5
@@ -1523,11 +1523,8 @@ std::wstring WslCoreVm::GenerateConfigJson()
1523 // Set number of processors.
1524 kernelCmdLine += std::format(L" nr_cpus={}", m_vmConfig.ProcessorCount);
1525
1526 - // Enable timesync workaround to sync on resume from sleep in modern standby.
1527 - kernelCmdLine += L" hv_utils.timesync_implicit=1";
1528 -
1529 - // Configure page reporting order - minimum order of pages reported as free to the hypervisor.
1530 - kernelCmdLine += std::format(L" page_reporting.page_reporting_order={}", m_pageReportingOrder);
1526 + // Append common kernel parameters shared between WSL2 and WSLC.
1527 + helpers::AppendCommonKernelCommandLine(kernelCmdLine, m_pageReportingOrder);
1528
1529 // If using virtio features, enable SWIOTLB as a perf optimization (will cause VM to consume 64MB more memory).
1530 if (m_vmConfig.EnableVirtio9p || m_vmConfig.EnableVirtioFs || m_vmConfig.NetworkingMode == NetworkingMode::VirtioProxy)