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 1/2] 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"; } From d1b30db332a1937b71d6748ad65a155b541269f7 Mon Sep 17 00:00:00 2001 From: pipeob0 <36382709+pipeob0@users.noreply.github.com> Date: Mon, 28 Sep 2026 09:51:47 +0200 Subject: [PATCH 2/2] pinned: say how many bytes were refused, and stop blaming the privilege The refusal note blamed SeLockMemoryPrivilege whatever VirtualAlloc answered, and did not say what was asked for: 1450 (ERROR_NO_SYSTEM_RESOURCES, the large-page pool has that many 2 MB pages free right now), 87 (a size that is not a multiple of the minimum) and 1314 (the privilege is genuinely off) all read as the same one line. Observed for real on a 5700X3D box: a run minutes after an engine was killed printed 1450 while the next one succeeded - the killed process had not released its pinned large pages yet, which is only visible if the byte count and the error are both in the log. --- src/core/pinned.cu | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/core/pinned.cu b/src/core/pinned.cu index 841ca233..90c22d17 100644 --- a/src/core/pinned.cu +++ b/src/core/pinned.cu @@ -62,9 +62,11 @@ void* reserve(uint64_t bytes, PageBacking& got, std::string& note) { note = "large pages (" + std::to_string((unsigned long long) large) + " B)"; return p; } - 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"; + // 1450 (ERROR_NO_SYSTEM_RESOURCES) is the large-page pool saying no, 87 is a size that is not a + // multiple of the minimum, 1314 is the privilege: without the byte count the three read as one bug. + note = "large pages refused for " + std::to_string((unsigned long long) lbytes) + " B (GetLargePageMinimum=" + + std::to_string((unsigned long long) large) + ", VirtualAlloc error " + + std::to_string((unsigned long long) GetLastError()) + "); using 4 KB pages"; } else if (std::getenv("STRATA_NO_LARGEPAGES") != nullptr) { note = "large pages skipped (STRATA_NO_LARGEPAGES); using 4 KB pages"; } else {