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