From 41812674bbed6b6c781ece0d8c2818f4e53b5d54 Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Sat, 13 Sep 2025 23:30:13 +0900 Subject: [PATCH] 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 --- src/Common/EncryptionThreadPool.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Common/EncryptionThreadPool.c b/src/Common/EncryptionThreadPool.c index e09cce30..7b605eaf 100644 --- a/src/Common/EncryptionThreadPool.c +++ b/src/Common/EncryptionThreadPool.c @@ -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;