-
Notifications
You must be signed in to change notification settings - Fork 1.8k
WSL2: ship kernel headers and perf in the kernel artifacts VHD #41267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ben Hillis (benhillis)
wants to merge
12
commits into
master
Choose a base branch
from
user/benhill/module_vhd_artifacts
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e8124eb
WSL2: ship kernel headers and perf in the modules artifacts VHD
af71d78
Use unified kernel artifacts VHD
962befc
Validate bind mount file sources
71f1a73
Harden kernel artifact exposure
762eae0
Mount WSLC modules from artifacts layout
e512aba
Use dedicated WSLC modules mount message
a90bfd8
Simplify kernel artifact mount handling
8d9fcde
Expose kernel perf without modifying the distro file system
1153cbc
Address code review feedback for kernel perf exposure
854b1ca
Address review feedback on kernel artifact mounting
b04d3bb
Remove redundant comment in UtilMountFile
7d9b8f3
Improve kernel artifact diagnostics and test cleanup
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,8 @@ Module Name: | |
| #define LOCALE_FILE_PATH ETC_DEFAULT_FOLDER "locale" | ||
| #define LOCALE_CONF_FILE_PATH ETC_FOLDER "locale.conf" | ||
| #define PATH_ENV "PATH" | ||
| #define PERF_BINARY_PATH "/usr/bin/perf" | ||
| #define PERF_EXEC_PATH_ENV "PERF_EXEC_PATH" | ||
| #define RESOLV_CONF_DIRECTORY_MODE 0755 | ||
| #define RESOLV_CONF_FILE_MODE 0644 | ||
| #define RESOLV_CONF_FILE_NAME "resolv.conf" | ||
|
|
@@ -170,6 +172,32 @@ class RemoveMountAndEnvironmentOnScopeExit | |
| const char* m_mountPath = nullptr; | ||
| }; | ||
|
|
||
| // | ||
| // Moves a temporary mount created by mini_init into the distro namespace. The temporary mount point is | ||
| // passed via MountEnvironmentName and its final target via PathEnvironmentName. The callback is invoked | ||
| // with the target path once the mount has been moved. | ||
| // | ||
| template <typename TCallback> | ||
| static void MoveTemporaryMount(const char* MountEnvironmentName, const char* PathEnvironmentName, const TCallback& Callback) | ||
| try | ||
| { | ||
| auto tempMount = RemoveMountAndEnvironmentOnScopeExit(MountEnvironmentName); | ||
| const char* target = tempMount ? getenv(PathEnvironmentName) : nullptr; | ||
| if (target == nullptr) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| const std::string targetPath{target}; | ||
| unsetenv(PathEnvironmentName); | ||
|
|
||
| if (tempMount.MoveMount(targetPath.c_str())) | ||
| { | ||
| Callback(targetPath); | ||
| } | ||
| } | ||
| CATCH_LOG() | ||
|
|
||
| constexpr auto HostsFileFormatString = LX_INIT_AUTO_GENERATED_FILE_HEADER | ||
| "# [network]\n" | ||
| "# generateHosts = false\n" | ||
|
|
@@ -1110,20 +1138,73 @@ Return Value: | |
| } | ||
| CATCH_LOG() | ||
|
|
||
| try | ||
| { | ||
| auto tempMount = RemoveMountAndEnvironmentOnScopeExit(LX_WSL2_KERNEL_MODULES_MOUNT_ENV); | ||
| if (tempMount) | ||
| std::string kernelModulesPath; | ||
| MoveTemporaryMount(LX_WSL2_KERNEL_MODULES_MOUNT_ENV, LX_WSL2_KERNEL_MODULES_PATH_ENV, [&](const std::string& target) { | ||
| kernelModulesPath = target; | ||
| }); | ||
|
|
||
| MoveTemporaryMount(LX_WSL2_KERNEL_HEADERS_MOUNT_ENV, LX_WSL2_KERNEL_HEADERS_PATH_ENV, [&](const std::string& target) { | ||
| if (kernelModulesPath.empty()) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // | ||
| // Point /lib/modules/<release>/build at the kernel headers, replacing any entry that the | ||
| // distro may have created so that it can't shadow the headers matching the running kernel. | ||
| // | ||
| // N.B. A directory can't be replaced with a symlink (and removing it would mean a recursive | ||
| // delete), so bind mount the headers over it instead. | ||
| // | ||
|
|
||
| const std::string linkPath = kernelModulesPath + "/build"; | ||
| struct stat existing{}; | ||
| if ((lstat(linkPath.c_str(), &existing) == 0) && S_ISDIR(existing.st_mode)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need this check ? |
||
| { | ||
| auto target = getenv(LX_WSL2_KERNEL_MODULES_PATH_ENV); | ||
| if (target) | ||
| if (UtilMount(target.c_str(), linkPath.c_str(), nullptr, (MS_BIND | MS_REC), nullptr) < 0) | ||
| { | ||
| unsetenv(LX_WSL2_KERNEL_MODULES_PATH_ENV); | ||
| tempMount.MoveMount(target); | ||
| LOG_ERROR("UtilMount({}, {}) failed {}", target, linkPath, errno); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
| } | ||
| CATCH_LOG() | ||
|
|
||
| if ((unlink(linkPath.c_str()) < 0) && (errno != ENOENT)) | ||
|
benhillis marked this conversation as resolved.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this |
||
| { | ||
| LOG_ERROR("unlink({}) failed {}", linkPath, errno); | ||
| } | ||
|
|
||
| if (symlink(target.c_str(), linkPath.c_str()) < 0) | ||
| { | ||
| LOG_ERROR("symlink({}, {}) failed {}", target, linkPath, errno); | ||
| } | ||
| }); | ||
|
|
||
| MoveTemporaryMount(LX_WSL2_KERNEL_PERF_MOUNT_ENV, LX_WSL2_KERNEL_PERF_PATH_ENV, [&](const std::string& target) { | ||
| // | ||
| // Expose the kernel-matched perf via the environment block (see ConfigCreateEnvironmentBlock). | ||
| // | ||
|
|
||
| Config.KernelPerfPath = target; | ||
|
|
||
| // | ||
| // If the distro ships its own perf, shadow it with a bind mount so that the binary matching the | ||
| // running kernel is used. | ||
| // | ||
| // N.B. The distro's file system is only modified if perf is already present as a regular file. | ||
| // Distros without perf pick it up via $PATH instead. | ||
| // | ||
|
|
||
| struct stat existing{}; | ||
| if ((lstat(PERF_BINARY_PATH, &existing) == 0) && S_ISREG(existing.st_mode)) | ||
| { | ||
| const std::string perfBinary = target + "/bin/perf"; | ||
| if (UtilMountFile(perfBinary.c_str(), PERF_BINARY_PATH) < 0) | ||
| { | ||
| LOG_ERROR("UtilMountFile({}, {}) failed {}", perfBinary, PERF_BINARY_PATH, errno); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| // | ||
| // Change the permission of some devtmpfs devices to be more permissive. | ||
|
|
@@ -1664,6 +1745,25 @@ Return Value: | |
| { | ||
| ConfigAppendToPath(Environment, LXSS_LIB_PATH); | ||
| } | ||
|
|
||
| // | ||
| // Add the kernel-matched perf tools to the $PATH variable and point perf at its helper | ||
| // scripts since it is built with a prefix that does not match where it is mounted. | ||
| // | ||
|
|
||
| if (Config.KernelPerfPath.has_value()) | ||
|
benhillis marked this conversation as resolved.
|
||
| { | ||
| ConfigAppendToPath(Environment, std::format("{}/bin", *Config.KernelPerfPath)); | ||
|
|
||
| // | ||
| // N.B. This is only set if the user has not provided a value via WSLENV. | ||
| // | ||
|
|
||
| if (Environment.GetVariable(PERF_EXEC_PATH_ENV).empty()) | ||
| { | ||
| Environment.AddVariable(PERF_EXEC_PATH_ENV, std::format("{}/libexec/perf-core", *Config.KernelPerfPath)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.