From 97cf327c1e720732a3a19eb941d3c7658d2d5cd0 Mon Sep 17 00:00:00 2001 From: pipeob0 <36382709+pipeob0@users.noreply.github.com> Date: Mon, 28 Sep 2026 09:51:46 +0200 Subject: [PATCH] pinned: enable SeLockMemoryPrivilege and round the arena to GetLargePageMinimum reserve() asked for MEM_LARGE_PAGES without ever enabling SeLockMemoryPrivilege in the process token, and passed the arena size unrounded. VirtualAlloc requires an exact multiple of GetLargePageMinimum, so the call failed with 87 (ERROR_INVALID_PARAMETER) and the log blamed the privilege. Both fixed; STRATA_NO_LARGEPAGES skips the attempt and the note now says what actually happened. --- src/core/pinned.cu | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/core/pinned.cu b/src/core/pinned.cu index 56573ce3..841ca233 100644 --- a/src/core/pinned.cu +++ b/src/core/pinned.cu @@ -29,11 +29,33 @@ namespace { // A 2 MB-aligned reservation. Large pages first, then the largest alignment the OS will give us for free. void* reserve(uint64_t bytes, PageBacking& got, std::string& note) { #ifdef _WIN32 + // MEM_LARGE_PAGES needs SeLockMemoryPrivilege. Having it assigned to the account is not enough: the + // PROCESS must enable it in its own token (AdjustTokenPrivileges) before VirtualAlloc, or the call fails. + // An account without the assignment, or a failure to enable, leaves the process as it was: VirtualAlloc + // then refuses and the 4 KB fallback below runs - that is the EXPECTED outcome on a desktop. + { + HANDLE tok = nullptr; + if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tok)) { + TOKEN_PRIVILEGES tp{}; + tp.PrivilegeCount = 1; + if (LookupPrivilegeValueW(nullptr, L"SeLockMemoryPrivilege", &tp.Privileges[0].Luid)) { + tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; + if (!AdjustTokenPrivileges(tok, FALSE, &tp, 0, nullptr, nullptr) && GetLastError() != ERROR_NOT_ALL_ASSIGNED) + (void) 0; // nothing actionable: the large-page attempt below reports the outcome + } + CloseHandle(tok); + } + } // MEM_LARGE_PAGES needs SeLockMemoryPrivilege; a normal account does not have it and VirtualAlloc then // fails with ERROR_PRIVILEGE_NOT_HELD. That is the EXPECTED outcome on a desktop, not an error. SIZE_T large = GetLargePageMinimum(); - if (large > 0) { - void* p = VirtualAlloc(nullptr, (SIZE_T) bytes, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES, + // A/B switch: STRATA_NO_LARGEPAGES=1 skips the large-page attempt, same run, same boot. + if (large > 0 && std::getenv("STRATA_NO_LARGEPAGES") == nullptr) { + // MEM_LARGE_PAGES requires the allocation size to be an exact multiple of the large page size - + // anything else is ERROR_INVALID_PARAMETER (87), which reads like a privilege problem but is not. + // Round up: the slack is under 2 MB and the tail stays unused. + const SIZE_T lbytes = (SIZE_T) (((SIZE_T) bytes + large - 1) / large * large); + void* p = VirtualAlloc(nullptr, lbytes, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE); if (p) { got = PageBacking::LargePages; @@ -43,6 +65,8 @@ void* reserve(uint64_t bytes, PageBacking& got, std::string& note) { note = "large pages refused (GetLargePageMinimum=" + std::to_string((unsigned long long) large) + ", VirtualAlloc error " + std::to_string((unsigned long long) GetLastError()) + " - needs SeLockMemoryPrivilege); using 4 KB pages"; + } else if (std::getenv("STRATA_NO_LARGEPAGES") != nullptr) { + note = "large pages skipped (STRATA_NO_LARGEPAGES); using 4 KB pages"; } else { note = "this system has no large-page minimum; using 4 KB pages"; }