Merge PR #42 (pipeob0): MEM_LARGE_PAGES on Windows - enable the privilege, round the size
This commit is contained in:
commit
5c52c0fd00
1 file changed
+31
-5
+31
-5
@@ -29,20 +29,46 @@ 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;
|
||||
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 {
|
||||
note = "this system has no large-page minimum; using 4 KB pages";
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user