diff options
| author | Logan <Logan@cattn.dev> | 2026-11-23 12:01:48 -0500 |
|---|---|---|
| committer | Logan <Logan@cattn.dev> | 2026-11-23 12:01:49 -0500 |
| commit | a07c8c0e895dd8c2755171bdda9b78ef75b65a38 (patch) | |
| tree | accb540ad28d89f086f733c3a2115bc86c81d75a /runtime/test/xmem_test.cpp | |
| parent | 37398f98d91ec6992d5621e9f4fdef64d59dc09b (diff) | |
xmem: commit physical pages on first touch, not when they are allocated
XMemAllocatePhysicalPages backed each call with one SEC_COMMIT section, so
the title's ~6.1 GiB page-pool request after boot stop 17 was charged in
full at once and fails with ERROR_COMMITMENT_LIMIT on a 16 GB machine.
Mapping the pages right away (as the title does) would charge most of it
within seconds too, so the commit now follows first access.
The section is SEC_RESERVE. A vectored handler commits the faulting 64 KiB
page through a reserve-only view of the whole section when the address
lies in a live mapping, so every view of that page sees it. Faults on
committed pages whose protection forbids the access, and faults outside
the mapped pages, go on to the next handler, and a failed commit is not
swallowed. rt::xmem::commit_physical_range commits a range up front for
shims that hand guest memory to the kernel.
Reported by controldebils and rafaelpssz in #7333.
Diffstat (limited to 'runtime/test/xmem_test.cpp')
| -rw-r--r-- | runtime/test/xmem_test.cpp | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index eed030c12..bd24de018 100644 --- a/runtime/test/xmem_test.cpp +++ b/runtime/test/xmem_test.cpp | |||
| @@ -18,9 +18,11 @@ | |||
| 18 | #include <limits> | 18 | #include <limits> |
| 19 | #include <sstream> | 19 | #include <sstream> |
| 20 | #include <string> | 20 | #include <string> |
| 21 | #include <vector> | ||
| 21 | 22 | ||
| 22 | #include "shims.h" | 23 | #include "shims.h" |
| 23 | #include "startup_trace.h" | 24 | #include "startup_trace.h" |
| 25 | #include "xmem.h" | ||
| 24 | 26 | ||
| 25 | namespace { | 27 | namespace { |
| 26 | 28 | ||
| @@ -440,6 +442,129 @@ void test_physical_pages(const PhysicalApi& api) { | |||
| 440 | // The windows were split into one placeholder per page; the process exit releases them. | 442 | // The windows were split into one placeholder per page; the process exit releases them. |
| 441 | } | 443 | } |
| 442 | 444 | ||
| 445 | bool access_faults(volatile uint32_t* address, bool write) { | ||
| 446 | __try { | ||
| 447 | if (write) { | ||
| 448 | *address = 1; | ||
| 449 | } else { | ||
| 450 | const uint32_t value = *address; | ||
| 451 | (void)value; | ||
| 452 | } | ||
| 453 | } __except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ? EXCEPTION_EXECUTE_HANDLER | ||
| 454 | : EXCEPTION_CONTINUE_SEARCH) { | ||
| 455 | return true; | ||
| 456 | } | ||
| 457 | return false; | ||
| 458 | } | ||
| 459 | |||
| 460 | int64_t commit_growth(const PERFORMANCE_INFORMATION& from, const PERFORMANCE_INFORMATION& to) { | ||
| 461 | return (static_cast<int64_t>(to.CommitTotal) - static_cast<int64_t>(from.CommitTotal)) * | ||
| 462 | static_cast<int64_t>(to.PageSize); | ||
| 463 | } | ||
| 464 | |||
| 465 | void test_physical_pages_commit_on_touch(const PhysicalApi& api) { | ||
| 466 | PERFORMANCE_INFORMATION before{}; | ||
| 467 | if (!GetPerformanceInfo(&before, sizeof(before))) { | ||
| 468 | check(false, "GetPerformanceInfo reads the system commit charge"); | ||
| 469 | return; | ||
| 470 | } | ||
| 471 | const uint64_t free_commit = static_cast<uint64_t>(before.CommitLimit - before.CommitTotal) * before.PageSize; | ||
| 472 | const uint64_t count = (free_commit + (uint64_t{1} << 30)) / kPage64k; | ||
| 473 | if (count >= (uint64_t{1} << 20)) { | ||
| 474 | std::printf("SKIP physical pages past the free commit: %llu MiB of commit is free\n", | ||
| 475 | static_cast<unsigned long long>(free_commit >> 20)); | ||
| 476 | return; | ||
| 477 | } | ||
| 478 | std::vector<uint64_t> pages(static_cast<size_t>(count)); | ||
| 479 | uint64_t got = count; | ||
| 480 | check(api.allocate(0x380, &got, pages.data()) == TRUE && got == count, | ||
| 481 | "XMemAllocatePhysicalPages gives 1 GiB more pages than the system has commit free"); | ||
| 482 | if (got != count) return; | ||
| 483 | PERFORMANCE_INFORMATION allocated{}; | ||
| 484 | GetPerformanceInfo(&allocated, sizeof(allocated)); | ||
| 485 | check(commit_growth(before, allocated) < static_cast<int64_t>(count * kPage64k / 8), | ||
| 486 | "allocating those pages does not charge them"); | ||
| 487 | |||
| 488 | constexpr uint64_t kMapped = 1024; | ||
| 489 | constexpr uint64_t kTouched = 512; | ||
| 490 | auto* const window = static_cast<uint8_t*>( | ||
| 491 | api.virtual_alloc(nullptr, kMapped * kPage64k, MEM_RESERVE | kXmem64kPages, 0x21, PAGE_READWRITE)); | ||
| 492 | auto* const second = static_cast<uint8_t*>( | ||
| 493 | api.virtual_alloc(nullptr, 2 * kPage64k, MEM_RESERVE | kXmem64kPages, 0x21, PAGE_READWRITE)); | ||
| 494 | check(window != nullptr && second != nullptr, "XMemVirtualAlloc reserves windows for them"); | ||
| 495 | if (!window || !second) { | ||
| 496 | api.free_pages(count, pages.data()); | ||
| 497 | return; | ||
| 498 | } | ||
| 499 | check(api.map(window, kMapped, pages.data()) == window, "1,024 of them map into one window"); | ||
| 500 | PERFORMANCE_INFORMATION mapped{}; | ||
| 501 | GetPerformanceInfo(&mapped, sizeof(mapped)); | ||
| 502 | check(commit_growth(allocated, mapped) < static_cast<int64_t>(kMapped * kPage64k / 2), | ||
| 503 | "mapping them does not charge them"); | ||
| 504 | |||
| 505 | for (uint64_t i = 0; i < kTouched; ++i) { | ||
| 506 | static_cast<volatile uint32_t*>(static_cast<void*>(window + i * kPage64k))[0] = | ||
| 507 | 0xc0de0000u | static_cast<uint32_t>(i); | ||
| 508 | } | ||
| 509 | PERFORMANCE_INFORMATION touched{}; | ||
| 510 | GetPerformanceInfo(&touched, sizeof(touched)); | ||
| 511 | const int64_t touched_growth = commit_growth(mapped, touched); | ||
| 512 | check(touched_growth >= static_cast<int64_t>(kTouched * kPage64k / 2) && | ||
| 513 | touched_growth < static_cast<int64_t>(kMapped * kPage64k), | ||
| 514 | "writing to 512 of them charges about those 512 pages"); | ||
| 515 | bool written = true; | ||
| 516 | for (uint64_t i = 0; i < kTouched; ++i) written &= read_u32(window + i * kPage64k, 0) == (0xc0de0000u | i); | ||
| 517 | check(written, "each touched page keeps what was written to it"); | ||
| 518 | check(read_u32(window + kTouched * kPage64k, 0) == 0, "an untouched page reads as zero"); | ||
| 519 | |||
| 520 | check(api.map(second, 2, pages.data()) == second, "two of the pages map into a second window"); | ||
| 521 | check(read_u32(second, 0) == 0xc0de0000u && read_u32(second + kPage64k, 0) == 0xc0de0001u, | ||
| 522 | "the second window sees what was written through the first"); | ||
| 523 | static_cast<volatile uint32_t*>(static_cast<void*>(second))[1] = 0x5ca1ab1e; | ||
| 524 | check(read_u32(window, 4) == 0x5ca1ab1e, "and the first window sees what is written through the second"); | ||
| 525 | |||
| 526 | DWORD old = 0; | ||
| 527 | check(VirtualProtect(window, kPage64k, PAGE_READONLY, &old) != FALSE && | ||
| 528 | access_faults(static_cast<volatile uint32_t*>(static_cast<void*>(window)), true), | ||
| 529 | "a write to a committed read-only page still faults to the next handler"); | ||
| 530 | VirtualProtect(window, kPage64k, PAGE_READWRITE, &old); | ||
| 531 | void* const elsewhere = VirtualAlloc(nullptr, kPage64k, MEM_RESERVE, PAGE_READWRITE); | ||
| 532 | check(elsewhere != nullptr && access_faults(static_cast<volatile uint32_t*>(elsewhere), false), | ||
| 533 | "a fault outside the mapped pages still reaches the next handler"); | ||
| 534 | if (elsewhere) VirtualFree(elsewhere, 0, MEM_RELEASE); | ||
| 535 | |||
| 536 | uint8_t* const target = window + (kTouched + 1) * kPage64k; | ||
| 537 | check(rt::xmem::commit_physical_range(target, kPage64k), "commit_physical_range commits an untouched page"); | ||
| 538 | uint32_t local = 0; | ||
| 539 | check(rt::xmem::commit_physical_range(&local, sizeof(local)), "commit_physical_range skips memory it does not own"); | ||
| 540 | wchar_t directory[MAX_PATH]{}; | ||
| 541 | wchar_t path[MAX_PATH]{}; | ||
| 542 | bool read_ok = false; | ||
| 543 | if (GetTempPathW(MAX_PATH, directory) && GetTempFileNameW(directory, L"xpg", 0, path)) { | ||
| 544 | std::vector<uint32_t> pattern(kPage64k / sizeof(uint32_t)); | ||
| 545 | for (size_t i = 0; i < pattern.size(); ++i) pattern[i] = 0x7e570000u + static_cast<uint32_t>(i); | ||
| 546 | const HANDLE out = | ||
| 547 | CreateFileW(path, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, nullptr); | ||
| 548 | DWORD bytes = 0; | ||
| 549 | const bool wrote = out != INVALID_HANDLE_VALUE && | ||
| 550 | WriteFile(out, pattern.data(), static_cast<DWORD>(kPage64k), &bytes, nullptr) && | ||
| 551 | bytes == kPage64k; | ||
| 552 | if (out != INVALID_HANDLE_VALUE) CloseHandle(out); | ||
| 553 | const HANDLE in = wrote ? CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr) | ||
| 554 | : INVALID_HANDLE_VALUE; | ||
| 555 | if (in != INVALID_HANDLE_VALUE) { | ||
| 556 | read_ok = ReadFile(in, target, static_cast<DWORD>(kPage64k), &bytes, nullptr) && bytes == kPage64k && | ||
| 557 | std::memcmp(target, pattern.data(), kPage64k) == 0; | ||
| 558 | CloseHandle(in); | ||
| 559 | } | ||
| 560 | DeleteFileW(path); | ||
| 561 | } | ||
| 562 | check(read_ok, "ReadFile fills a page that commit_physical_range committed"); | ||
| 563 | |||
| 564 | check(api.map(second, 2, nullptr) == second && api.map(window, kMapped, nullptr) == window, "they unmap"); | ||
| 565 | check(api.free_pages(count, pages.data()) == TRUE, "XMemFreePhysicalPages frees them"); | ||
| 566 | } | ||
| 567 | |||
| 443 | // The placeholder flags pass through: reserve a placeholder, carve windows and committed memory out of it. | 568 | // The placeholder flags pass through: reserve a placeholder, carve windows and committed memory out of it. |
| 444 | void test_placeholders(const PhysicalApi& api) { | 569 | void test_placeholders(const PhysicalApi& api) { |
| 445 | auto* const base = static_cast<uint8_t*>( | 570 | auto* const base = static_cast<uint8_t*>( |
| @@ -718,6 +843,7 @@ int main() { | |||
| 718 | if (tools_mem) check(tools_mem() == FALSE, "XMemIsToolsMemAvailable returns FALSE (no devkit tools memory)"); | 843 | if (tools_mem) check(tools_mem() == FALSE, "XMemIsToolsMemAvailable returns FALSE (no devkit tools memory)"); |
| 719 | if (virtual_alloc && physical.allocate && physical.free_pages && physical.map && physical.scatter) { | 844 | if (virtual_alloc && physical.allocate && physical.free_pages && physical.map && physical.scatter) { |
| 720 | test_physical_pages(physical); | 845 | test_physical_pages(physical); |
| 846 | test_physical_pages_commit_on_touch(physical); | ||
| 721 | test_placeholders(physical); | 847 | test_placeholders(physical); |
| 722 | } | 848 | } |
| 723 | 849 | ||