-
Notifications
You must be signed in to change notification settings - Fork 741
redpanda: Add support to map code into hugepages #30190
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| // Copyright 2026 Redpanda Data, Inc. | ||
| // | ||
| // Use of this software is governed by the Business Source License | ||
| // included in the file licenses/BSL.md | ||
| // | ||
| // As of the Change Date specified in that file, in accordance with | ||
| // the Business Source License, use of this software will be governed | ||
| // by the Apache License, Version 2.0 | ||
|
|
||
| #include "syschecks/hugepages.h" | ||
|
|
||
| #include "base/vlog.h" | ||
| #include "syschecks/syschecks.h" | ||
|
|
||
| #include <sys/mman.h> | ||
|
|
||
| #include <cstddef> | ||
| #include <link.h> | ||
|
|
||
| // MADV_COLLAPSE was added in Linux 6.1. Define it for older headers. | ||
| #ifndef MADV_COLLAPSE | ||
| #define MADV_COLLAPSE 25 | ||
| #endif | ||
|
StephanDollberg marked this conversation as resolved.
|
||
|
|
||
| namespace syschecks { | ||
|
|
||
| namespace { | ||
|
|
||
| /// Invoke fn(addr, len) for each non-writable PT_LOAD segment across | ||
| /// all loaded ELF objects (main binary + shared libraries). This covers | ||
| /// .text (PF_R|PF_X) and .rodata (PF_R) segments. | ||
| template<typename Fn> | ||
| void for_each_ro_segment(Fn fn) { | ||
| dl_iterate_phdr( | ||
| [](struct dl_phdr_info* info, size_t /*size*/, void* data) -> int { | ||
| auto& callback = *static_cast<Fn*>(data); | ||
| for (int i = 0; i < info->dlpi_phnum; ++i) { | ||
| const auto& phdr = info->dlpi_phdr[i]; | ||
| if (phdr.p_type != PT_LOAD) { | ||
| continue; | ||
| } | ||
| // Skip writable segments (.data, .bss). | ||
| if (phdr.p_flags & PF_W) { | ||
| continue; | ||
| } | ||
| auto addr = info->dlpi_addr + phdr.p_vaddr; | ||
| auto len = phdr.p_memsz; | ||
| if (len == 0) { | ||
| continue; | ||
| } | ||
| callback(reinterpret_cast<void*>(addr), static_cast<size_t>(len)); | ||
| } | ||
|
StephanDollberg marked this conversation as resolved.
|
||
| return 0; // continue iteration | ||
| }, | ||
| &fn); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| void promote_code_to_hugepages() { | ||
| vlog(checklog.info, "Starting hugepage promotioin of code segments"); | ||
|
|
||
| size_t total_bytes = 0; | ||
| size_t marked_bytes = 0; | ||
| size_t collapsed_bytes = 0; | ||
|
|
||
| for_each_ro_segment([&](void* addr, size_t len) { | ||
| total_bytes += len; | ||
|
|
||
| // Mark the VMA for huge pages. In "madvise" THP mode (the common | ||
| // default), khugepaged only scans VMAs with VM_HUGEPAGE set, so this | ||
| // is required for ongoing huge page maintenance — not just a hint. | ||
| if (::madvise(addr, len, MADV_HUGEPAGE) == 0) { | ||
| marked_bytes += len; | ||
| } | ||
|
|
||
| // Fault in all pages so MADV_COLLAPSE has something to work with. | ||
|
travisdowns marked this conversation as resolved.
|
||
| // At startup most pages are still demand-paged. | ||
| // In theory this is not needed with MADV_COLLAPSE but the docs leave a | ||
| // cop out so we are just explicit in any case. | ||
| // Incompatible with ASAN, disable if on | ||
| #if !__has_feature(address_sanitizer) | ||
| auto* base = static_cast<volatile const char*>(addr); | ||
| for (size_t off = 0; off < len; off += 4096) { | ||
| [[maybe_unused]] char c = base[off]; | ||
|
travisdowns marked this conversation as resolved.
|
||
| } | ||
| #endif | ||
|
|
||
| // Synchronously collapse 4 KB pages into 2 MB huge pages | ||
| // (Linux 6.1+). Without this, khugepaged promotes pages in the | ||
| // background over the next few seconds; MADV_COLLAPSE makes it | ||
| // immediate (best effort). | ||
| if (::madvise(addr, len, MADV_COLLAPSE) == 0) { | ||
| collapsed_bytes += len; | ||
| } | ||
|
StephanDollberg marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| if (total_bytes > 0) { | ||
| vlog( | ||
| checklog.info, | ||
| "hugepages: {}/{} MiB marked, {}/{} MiB collapsed", | ||
| marked_bytes / (1024 * 1024), | ||
| total_bytes / (1024 * 1024), | ||
| collapsed_bytes / (1024 * 1024), | ||
| total_bytes / (1024 * 1024)); | ||
|
StephanDollberg marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| void demote_code_from_hugepages() { | ||
| size_t total_bytes = 0; | ||
| size_t demoted_bytes = 0; | ||
|
|
||
| for_each_ro_segment([&](void* addr, size_t len) { | ||
| total_bytes += len; | ||
|
|
||
| // Prevent khugepaged from re-promoting these pages. | ||
| if (::madvise(addr, len, MADV_NOHUGEPAGE) != 0) { | ||
| return; | ||
| } | ||
|
|
||
| // MADV_NOHUGEPAGE only prevents future promotions — existing PMD | ||
| // entries for file-backed pages are not split. MADV_DONTNEED drops | ||
| // the page table entries. They will be re-faulted at 4 KB granularity | ||
| // (since MADV_NOHUGEPAGE is set). | ||
| if (::madvise(addr, len, MADV_DONTNEED) == 0) { | ||
| demoted_bytes += len; | ||
| } | ||
| }); | ||
|
|
||
| if (total_bytes > 0) { | ||
| vlog( | ||
| checklog.info, | ||
| "hugepages: demoted {}/{} MiB from huge pages", | ||
| demoted_bytes / (1024 * 1024), | ||
| total_bytes / (1024 * 1024)); | ||
| } | ||
| } | ||
|
|
||
| } // namespace syschecks | ||
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,24 @@ | ||
| /* | ||
| * Copyright 2026 Redpanda Data, Inc. | ||
| * | ||
| * Use of this software is governed by the Business Source License | ||
| * included in the file licenses/BSL.md | ||
| * | ||
| * As of the Change Date specified in that file, in accordance with | ||
| * the Business Source License, use of this software will be governed | ||
| * by the Apache License, Version 2.0 | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| namespace syschecks { | ||
|
|
||
| /// Promote file-backed executable mappings (code segments) to transparent huge | ||
| /// pages. | ||
| void promote_code_to_hugepages(); | ||
|
|
||
| /// Undo the effect of promote_code_to_hugepages(). Marks executable VMAs with | ||
| /// MADV_NOHUGEPAGE | ||
|
StephanDollberg marked this conversation as resolved.
|
||
| void demote_code_from_hugepages(); | ||
|
|
||
| } // namespace syschecks | ||
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.
how many PT_LOAD segments are there? i.e., are we wasting a lot of "space" if we don't fill them out to the next 2MB boundary
Uh oh!
There was an error while loading. Please reload this page.
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.
There are 4 in the redpanda binary. We adding ~2MB (just conicidental similar to the 2MiB alignment) of total padding. This is about a 2% binary size increase.
From when I looked into this earlier.
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.
Thanks, that sounds fine.