]> git.proxmox.com Git - mirror_edk2.git/commitdiff
UefiCpuPkg/RegisterCpuFeaturesLib: Separate semaphore container.
authorEric Dong <eric.dong@intel.com>
Thu, 8 Nov 2018 01:06:33 +0000 (09:06 +0800)
committerEric Dong <eric.dong@intel.com>
Sun, 11 Nov 2018 02:02:45 +0000 (10:02 +0800)
In current implementation, core and package level sync uses same semaphores.
Sharing the semaphore may cause wrong execution order.
For example:
1. Feature A has CPU_FEATURE_CORE_BEFORE dependency with Feature B.
2. Feature C has CPU_FEATURE_PACKAGE_AFTER dependency with Feature B.
The expected feature initialization order is A B C:
A ---- (Core Depends) ----> B ---- (Package Depends) ----> C

For a CPU has 1 package, 2 cores and 4 threads. The feature initialization
order may like below:

   Thread#1             Thread#2       Thread#3         Thread#4
   [A.Init]             [A.Init]                        [A.Init]
Release(S1, S2)        Release(S1, S2)                Release(S3, S4)
Wait(S1) * 2           Wait(S2) * 2  <------------------------------- Core sync

   [B.Init]             [B.Init]
Release (S1,S2,S3,S4)
Wait (S1) * 4  <----------------------------------------------------- Package sync
                                                      Wait(S4 * 2) <- Core sync
                                                        [B.Init]

In above case, for thread#4, when it syncs in core level, Wait(S4) * 2 isn't
blocked and [B.Init] runs. But [A.Init] hasn't run in thread#3. It's wrong!
Thread#4 should execute [B.Init] after thread#3 executes [A.Init] because B
core level depends on A.

The reason of the wrong execution order is that S4 is released in thread#1
by calling Release (S1, S2, S3, S4) and in thread #4 by calling
Release (S3, S4).

To fix this issue, core level sync and package level sync should use separate
semaphores.

In above example, the S4 released in Release (S1, S2, S3, S4) should not be the
same semaphore as that in Release (S3, S4).

Related BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=1311

Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Eric Dong <eric.dong@intel.com>
Reviewed-by: Ruiyu Ni <ruiyu.ni@intel.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
UefiCpuPkg/Library/RegisterCpuFeaturesLib/CpuFeaturesInitialize.c
UefiCpuPkg/Library/RegisterCpuFeaturesLib/RegisterCpuFeatures.h

index e61ace9bade84a954f6475b7dd6040e907b4f2f4..0a74d448c88bd3c8dc00f785aba649af7cc43ab9 100644 (file)
@@ -269,8 +269,10 @@ CpuInitDataInitialize (
     DEBUG ((DEBUG_INFO, "Package: %d, Valid Core : %d\n", Index, ValidCoreCountPerPackage[Index]));\r
   }\r
 \r
-  CpuFeaturesData->CpuFlags.SemaphoreCount = AllocateZeroPool (sizeof (UINT32) * CpuStatus->PackageCount * CpuStatus->MaxCoreCount * CpuStatus->MaxThreadCount);\r
-  ASSERT (CpuFeaturesData->CpuFlags.SemaphoreCount != NULL);\r
+  CpuFeaturesData->CpuFlags.CoreSemaphoreCount = AllocateZeroPool (sizeof (UINT32) * CpuStatus->PackageCount * CpuStatus->MaxCoreCount * CpuStatus->MaxThreadCount);\r
+  ASSERT (CpuFeaturesData->CpuFlags.CoreSemaphoreCount != NULL);\r
+  CpuFeaturesData->CpuFlags.PackageSemaphoreCount = AllocateZeroPool (sizeof (UINT32) * CpuStatus->PackageCount * CpuStatus->MaxCoreCount * CpuStatus->MaxThreadCount);\r
+  ASSERT (CpuFeaturesData->CpuFlags.PackageSemaphoreCount != NULL);\r
 \r
   //\r
   // Get support and configuration PCDs\r
@@ -963,9 +965,9 @@ ProgramProcessorRegister (
       //  V(0...n)       V(0...n)      ...           V(0...n)\r
       //  n * P(0)       n * P(1)      ...           n * P(n)\r
       //\r
-      SemaphorePtr = CpuFlags->SemaphoreCount;\r
       switch (RegisterTableEntry->Value) {\r
       case CoreDepType:\r
+        SemaphorePtr = CpuFlags->CoreSemaphoreCount;\r
         //\r
         // Get Offset info for the first thread in the core which current thread belongs to.\r
         //\r
@@ -986,6 +988,7 @@ ProgramProcessorRegister (
         break;\r
 \r
       case PackageDepType:\r
+        SemaphorePtr = CpuFlags->PackageSemaphoreCount;\r
         ValidCoreCountPerPackage = (UINT32 *)(UINTN)CpuStatus->ValidCoreCountPerPackage;\r
         //\r
         // Get Offset info for the first thread in the package which current thread belongs to.\r
index 0a67a0581ad31fce6a078f17c0d761d0550897c6..cf3da8483732a66d011ad1fe694f123cc8319f61 100644 (file)
@@ -60,9 +60,10 @@ typedef struct {
 // Flags used when program the register.\r
 //\r
 typedef struct {\r
-  volatile UINTN           ConsoleLogLock;       // Spinlock used to control console.\r
-  volatile UINTN           MemoryMappedLock;     // Spinlock used to program mmio\r
-  volatile UINT32          *SemaphoreCount;      // Semaphore used to program semaphore.\r
+  volatile UINTN           ConsoleLogLock;          // Spinlock used to control console.\r
+  volatile UINTN           MemoryMappedLock;        // Spinlock used to program mmio\r
+  volatile UINT32          *CoreSemaphoreCount;     // Semaphore containers used to program Core semaphore.\r
+  volatile UINT32          *PackageSemaphoreCount;  // Semaphore containers used to program Package semaphore.\r
 } PROGRAM_CPU_REGISTER_FLAGS;\r
 \r
 typedef struct {\r