1
0
mirror of https://github.com/veracrypt/VeraCrypt.git synced 2025-11-11 02:58:02 -06:00

Windows: correct processor group affinity handling and off-by-one mapping

- Replace dynamic GetProcAddress usage with direct SetThreadGroupAffinity call since we run under Windows 10 minimum
- Compute affinity mask based on actual active processor count
- Fix off-by-one when assigning threads to processor groups (use > instead of >=), preventing premature group advance
- Improves correctness on multi-group (>=64 CPU) systems
This commit is contained in:
Mounir IDRASSI
2025-09-13 23:30:13 +09:00
parent f380dc13d9
commit 41812674bb

View File

@@ -196,13 +196,16 @@ static TC_THREAD_PROC EncryptionThreadProc (void *threadArg)
#ifdef DEVICE_DRIVER
SetThreadCpuGroupAffinity ((USHORT) *(WORD*)(threadArg));
#else
SetThreadGroupAffinityFn SetThreadGroupAffinityPtr = (SetThreadGroupAffinityFn) GetProcAddress (GetModuleHandle (L"kernel32.dll"), "SetThreadGroupAffinity");
if (SetThreadGroupAffinityPtr && threadArg)
if (threadArg)
{
GROUP_AFFINITY oldAffinity;
GROUP_AFFINITY groupAffinity = {0};
groupAffinity.Mask = ~0ULL;
groupAffinity.Group = *(WORD*)(threadArg);
SetThreadGroupAffinityPtr(GetCurrentThread(), &groupAffinity, NULL);
WORD groupIndex = *(WORD*)(threadArg);
DWORD activeProcessorCount = GetActiveProcessorCount(groupIndex);
KAFFINITY mask = (activeProcessorCount >= 64) ? ~0ULL : ((1ULL << activeProcessorCount) - 1);
groupAffinity.Mask = mask;
groupAffinity.Group = groupIndex;
SetThreadGroupAffinity(GetCurrentThread(), &groupAffinity, &oldAffinity);
}
#endif
@@ -464,7 +467,7 @@ BOOL EncryptionThreadPoolStart (size_t encryptionFreeCpuCount)
for (j = 0U; j < groupCount; j++)
{
totalProcessors += (uint32) GetActiveProcessorCountPtr(j);
if (totalProcessors >= ThreadCount)
if (totalProcessors > ThreadCount)
{
ThreadProcessorGroups[ThreadCount] = j;
break;