-
Notifications
You must be signed in to change notification settings - Fork 42
Add: memfd-based SO loading for all runtimes #513
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
puddingfjz
wants to merge
7
commits into
hw-native-sys:main
Choose a base branch
from
puddingfjz:memfd-so-loading
base: main
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
7 commits
Select commit
Hold shift + click to select a range
62b2fb4
Add: memfd-based SO loading for all runtimes
puddingfjz a3c6353
Fix: address cpplint issues in memfd_loader.h files
puddingfjz 796866e
Fix: correct copyright header typo and apply clang-format
puddingfjz 6f94963
Fix: guard memfd-related code with __linux__ for macOS compatibility
puddingfjz 3b8cd83
Fix: use constant buffer size instead of sizeof(pointer)
puddingfjz 6d6b317
Fix: move memfd variable inside #if defined(__linux__) block
puddingfjz 0bfa0e6
Apply: clang-format formatting changes
puddingfjz 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
130 changes: 130 additions & 0 deletions
130
src/a2a3/runtime/aicpu_build_graph/aicpu/memfd_loader.h
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 |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /* | ||
| * Copyright (c) PyPTO Contributors. | ||
| * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See LICENSE in the root of the software repository for the full text of the License. | ||
| * ----------------------------------------------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| /** | ||
| * @file memfd_loader.h | ||
| * @brief Memory file descriptor based SO loading for AICPU environment | ||
| */ | ||
|
|
||
| #ifndef SRC_A2A3_RUNTIME_AICPU_BUILD_GRAPH_AICPU_MEMFD_LOADER_H_ | ||
| #define SRC_A2A3_RUNTIME_AICPU_BUILD_GRAPH_AICPU_MEMFD_LOADER_H_ | ||
|
|
||
| // Enable GNU extensions for memfd_create and MFD_CLOEXEC | ||
| #ifndef _GNU_SOURCE | ||
| #define _GNU_SOURCE | ||
| #endif | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| #include <dlfcn.h> | ||
| #include <fcntl.h> | ||
| #include <unistd.h> | ||
| #include <sys/mman.h> | ||
| #include <cstring> | ||
| #include <cstdio> | ||
|
|
||
| #include "aicpu/device_log.h" | ||
|
|
||
| /** | ||
| * Load orchestration SO using memfd | ||
| */ | ||
| static inline int load_orchestration_so_with_memfd( | ||
| const void *so_data, size_t so_size, int orch_thread_num, void **out_handle, char *out_so_path, int *out_memfd | ||
| ) { | ||
| *out_handle = nullptr; | ||
| *out_memfd = -1; | ||
| out_so_path[0] = '\0'; | ||
|
|
||
| if (so_data == nullptr || so_size == 0) { | ||
| return -1; | ||
| } | ||
|
|
||
| // Create memfd | ||
| int fd = memfd_create("libdevice_orch", MFD_CLOEXEC); | ||
|
|
||
| if (fd < 0) { | ||
| DEV_INFO("memfd_create failed: errno=%d", errno); | ||
| return -1; | ||
| } | ||
|
|
||
| // Write SO data to memfd | ||
| ssize_t written = write(fd, so_data, so_size); | ||
|
|
||
| if (written < 0) { | ||
| DEV_INFO("memfd write failed: errno=%d", errno); | ||
| close(fd); | ||
| return -1; | ||
| } | ||
| if (written != static_cast<ssize_t>(so_size)) { | ||
| DEV_INFO("memfd partial write: %zd/%zu", written, so_size); | ||
| close(fd); | ||
| return -1; | ||
| } | ||
|
|
||
| // Reset file position to beginning before dlopen | ||
| lseek(fd, 0, SEEK_SET); | ||
|
|
||
| // Construct /proc/self/fd/N path for symlink target | ||
| char proc_fd_path[256]; | ||
| snprintf(proc_fd_path, sizeof(proc_fd_path), "/proc/self/fd/%d", fd); | ||
|
|
||
| // Create a symlink to /proc/self/fd/N with a "normal" path | ||
| // This bypasses the AICPU dynamic linker's issue with /proc/self/fd/N paths | ||
| char link_path[256]; | ||
| snprintf(link_path, sizeof(link_path), "/tmp/libdevice_orch_%d_%d.so", getpid(), orch_thread_num); | ||
|
|
||
| int symlink_rc = symlink(proc_fd_path, link_path); | ||
| if (symlink_rc != 0) { | ||
| DEV_INFO("symlink failed: errno=%d", errno); | ||
| close(fd); | ||
| return -1; | ||
| } | ||
|
|
||
| snprintf(out_so_path, 256, "%s", link_path); | ||
|
|
||
| // Try dlopen from the symlink | ||
| dlerror(); | ||
| void *handle = dlopen(out_so_path, RTLD_LAZY | RTLD_LOCAL); | ||
|
|
||
| // Clean up symlink immediately after dlopen (dlopen has its own reference) | ||
| unlink(link_path); | ||
|
|
||
| if (handle == nullptr) { | ||
| const char *dl_err = dlerror(); | ||
| DEV_INFO("dlopen from memfd symlink failed: %s", dl_err ? dl_err : "unknown"); | ||
| close(fd); | ||
| return -1; | ||
| } | ||
|
|
||
| *out_handle = handle; | ||
| *out_memfd = fd; | ||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * Cleanup memfd-based SO | ||
| */ | ||
| static inline void cleanup_memfd_so(int memfd, void *handle) { | ||
| if (handle != nullptr) { | ||
| dlclose(handle); | ||
| } | ||
| if (memfd >= 0) { | ||
| close(memfd); | ||
| } | ||
| } | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif // SRC_A2A3_RUNTIME_AICPU_BUILD_GRAPH_AICPU_MEMFD_LOADER_H_ | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To improve debuggability, it would be beneficial to add logging for failure cases within this function. When
memfd_create,write,symlink, ordlopenfail, logging the error (e.g., fromerrnoordlerror()) would provide valuable insight before falling back to the file-based method. This would also make the behavior consistent with the fallback path, which does have logging for its failures.References