]> git.proxmox.com Git - mirror_edk2.git/commitdiff
UefiCpuPkg/PiSmmCpuDxeSmm: Avoid allocate Token every time
authorEric Dong <eric.dong@intel.com>
Fri, 6 Dec 2019 03:36:35 +0000 (11:36 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Fri, 6 Dec 2019 06:41:16 +0000 (06:41 +0000)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2388

Token is new introduced by MM MP Protocol. Current logic allocate Token
every time when need to use it. The logic caused SMI latency raised to
very high. Update logic to allocate Token buffer at driver's entry point.
Later use the token from the allocated token buffer. Only when all the
buffer have been used, then need to allocate new buffer.

Reviewed-by: Ray Ni <ray.ni@intel.com>
Signed-off-by: Eric Dong <eric.dong@intel.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c
UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h
UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf
UefiCpuPkg/UefiCpuPkg.dec
UefiCpuPkg/UefiCpuPkg.uni

index 0685637c2b51bf3e883e6adfa9aa6291058bd374..757f1056f78dd62fa3fb3b904ed7f212171f58a1 100644 (file)
@@ -492,6 +492,24 @@ FreeTokens (
 {\r
   LIST_ENTRY            *Link;\r
   PROCEDURE_TOKEN       *ProcToken;\r
+  TOKEN_BUFFER          *TokenBuf;\r
+\r
+  //\r
+  // Only free the token buffer recorded in the OldTOkenBufList\r
+  // upon exiting SMI. Current token buffer stays allocated so\r
+  // next SMI doesn't need to re-allocate.\r
+  //\r
+  gSmmCpuPrivate->UsedTokenNum = 0;\r
+\r
+  Link = GetFirstNode (&gSmmCpuPrivate->OldTokenBufList);\r
+  while (!IsNull (&gSmmCpuPrivate->OldTokenBufList, Link)) {\r
+    TokenBuf = TOKEN_BUFFER_FROM_LINK (Link);\r
+\r
+    Link = RemoveEntryList (&TokenBuf->Link);\r
+\r
+    FreePool (TokenBuf->Buffer);\r
+    FreePool (TokenBuf);\r
+  }\r
 \r
   while (!IsListEmpty (&gSmmCpuPrivate->TokenList)) {\r
     Link = GetFirstNode (&gSmmCpuPrivate->TokenList);\r
@@ -499,7 +517,6 @@ FreeTokens (
 \r
     RemoveEntryList (&ProcToken->Link);\r
 \r
-    FreePool ((VOID *)ProcToken->ProcedureToken);\r
     FreePool (ProcToken);\r
   }\r
 }\r
@@ -1115,13 +1132,37 @@ CreateToken (
   VOID\r
   )\r
 {\r
-  PROCEDURE_TOKEN    *ProcToken;\r
+  PROCEDURE_TOKEN     *ProcToken;\r
   SPIN_LOCK           *CpuToken;\r
   UINTN               SpinLockSize;\r
+  TOKEN_BUFFER        *TokenBuf;\r
+  UINT32              TokenCountPerChunk;\r
 \r
   SpinLockSize = GetSpinLockProperties ();\r
-  CpuToken = AllocatePool (SpinLockSize);\r
-  ASSERT (CpuToken != NULL);\r
+  TokenCountPerChunk = FixedPcdGet32 (PcdCpuSmmMpTokenCountPerChunk);\r
+\r
+  if (gSmmCpuPrivate->UsedTokenNum == TokenCountPerChunk) {\r
+    DEBUG ((DEBUG_VERBOSE, "CpuSmm: No free token buffer, allocate new buffer!\n"));\r
+\r
+    //\r
+    // Record current token buffer for later free action usage.\r
+    // Current used token buffer not in this list.\r
+    //\r
+    TokenBuf = AllocatePool (sizeof (TOKEN_BUFFER));\r
+    ASSERT (TokenBuf != NULL);\r
+    TokenBuf->Signature = TOKEN_BUFFER_SIGNATURE;\r
+    TokenBuf->Buffer  = gSmmCpuPrivate->CurrentTokenBuf;\r
+\r
+    InsertTailList (&gSmmCpuPrivate->OldTokenBufList, &TokenBuf->Link);\r
+\r
+    gSmmCpuPrivate->CurrentTokenBuf = AllocatePool (SpinLockSize * TokenCountPerChunk);\r
+    ASSERT (gSmmCpuPrivate->CurrentTokenBuf != NULL);\r
+    gSmmCpuPrivate->UsedTokenNum = 0;\r
+  }\r
+\r
+  CpuToken = (SPIN_LOCK *)(gSmmCpuPrivate->CurrentTokenBuf + SpinLockSize * gSmmCpuPrivate->UsedTokenNum);\r
+  gSmmCpuPrivate->UsedTokenNum++;\r
+\r
   InitializeSpinLock (CpuToken);\r
   AcquireSpinLock (CpuToken);\r
 \r
@@ -1732,10 +1773,28 @@ InitializeDataForMmMp (
   VOID\r
   )\r
 {\r
+  UINTN              SpinLockSize;\r
+  UINT32             TokenCountPerChunk;\r
+\r
+  SpinLockSize = GetSpinLockProperties ();\r
+  TokenCountPerChunk = FixedPcdGet32 (PcdCpuSmmMpTokenCountPerChunk);\r
+  ASSERT (TokenCountPerChunk != 0);\r
+  if (TokenCountPerChunk == 0) {\r
+    DEBUG ((DEBUG_ERROR, "PcdCpuSmmMpTokenCountPerChunk should not be Zero!\n"));\r
+    CpuDeadLoop ();\r
+  }\r
+  DEBUG ((DEBUG_INFO, "CpuSmm: SpinLock Size = 0x%x, PcdCpuSmmMpTokenCountPerChunk = 0x%x\n", SpinLockSize, TokenCountPerChunk));\r
+\r
+  gSmmCpuPrivate->CurrentTokenBuf = AllocatePool (SpinLockSize * TokenCountPerChunk);\r
+  ASSERT (gSmmCpuPrivate->CurrentTokenBuf != NULL);\r
+\r
+  gSmmCpuPrivate->UsedTokenNum = 0;\r
+\r
   gSmmCpuPrivate->ApWrapperFunc = AllocatePool (sizeof (PROCEDURE_WRAPPER) * gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus);\r
   ASSERT (gSmmCpuPrivate->ApWrapperFunc != NULL);\r
 \r
   InitializeListHead (&gSmmCpuPrivate->TokenList);\r
+  InitializeListHead (&gSmmCpuPrivate->OldTokenBufList);\r
 }\r
 \r
 /**\r
index 7e7c73f27f763d7ac8dcab4e59aef03887f4e862..5c1a01e42bf33ebd4eb2e04640bfb583563d07f7 100644 (file)
@@ -217,6 +217,17 @@ typedef struct {
 \r
 #define PROCEDURE_TOKEN_FROM_LINK(a)  CR (a, PROCEDURE_TOKEN, Link, PROCEDURE_TOKEN_SIGNATURE)\r
 \r
+#define TOKEN_BUFFER_SIGNATURE  SIGNATURE_32 ('T', 'K', 'B', 'S')\r
+\r
+typedef struct {\r
+  UINTN                   Signature;\r
+  LIST_ENTRY              Link;\r
+\r
+  UINT8                   *Buffer;\r
+} TOKEN_BUFFER;\r
+\r
+#define TOKEN_BUFFER_FROM_LINK(a)  CR (a, TOKEN_BUFFER, Link, TOKEN_BUFFER_SIGNATURE)\r
+\r
 //\r
 // Private structure for the SMM CPU module that is stored in DXE Runtime memory\r
 // Contains the SMM Configuration Protocols that is produced.\r
@@ -243,6 +254,10 @@ typedef struct {
   PROCEDURE_WRAPPER               *ApWrapperFunc;\r
   LIST_ENTRY                      TokenList;\r
 \r
+  LIST_ENTRY                      OldTokenBufList;\r
+\r
+  UINT8                           *CurrentTokenBuf;\r
+  UINT32                          UsedTokenNum;     // Only record tokens used in CurrentTokenBuf.\r
 } SMM_CPU_PRIVATE_DATA;\r
 \r
 extern SMM_CPU_PRIVATE_DATA  *gSmmCpuPrivate;\r
index b12b2691f8c17f68cb2eb0b989e91625a13c40f5..76b14629967972683142bf4a466cb8db8c780724 100644 (file)
   gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPropertyMask               ## CONSUMES\r
   gEfiMdePkgTokenSpaceGuid.PcdControlFlowEnforcementPropertyMask        ## CONSUMES\r
 \r
+[FixedPcd]\r
+  gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmMpTokenCountPerChunk               ## CONSUMES\r
+\r
 [Pcd.X64]\r
   gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmRestrictedMemoryAccess        ## CONSUMES\r
 \r
index 12f4413ea5b01ec4441f4c8fa893ec2fa58b1ab0..797f94863157ecfd214f0212d3a0c6a13b47d9bc 100644 (file)
   # @Prompt Specify size of good stack of exception which need switching stack.\r
   gUefiCpuPkgTokenSpaceGuid.PcdCpuKnownGoodStackSize|2048|UINT32|0x30002001\r
 \r
+  ## Count of pre allocated SMM MP tokens per chunk.\r
+  # @Prompt Specify the count of pre allocated SMM MP tokens per chunk.\r
+  gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmMpTokenCountPerChunk|64|UINT32|0x30002002\r
+\r
 [PcdsFixedAtBuild, PcdsPatchableInModule]\r
   ## This value is the CPU Local APIC base address, which aligns the address on a 4-KByte boundary.\r
   # @Prompt Configure base address of CPU Local APIC\r
index bfd696f48c35599fc6db18fdb43888f729c83a0c..c0d6ed513678a4ce5ea40a1fdfea6a5241dbcb68 100644 (file)
                                                                                             "24000000  -  6th and 7th generation Intel Core processors and Intel Xeon W Processor Family(24MHz).<BR>\n"\r
                                                                                             "19200000  -  Intel Atom processors based on Goldmont Microarchitecture with CPUID signature 06_5CH(19.2MHz).<BR>\n"\r
 \r
+#string STR_gUefiCpuPkgTokenSpaceGuid_PcdCpuSmmMpTokenCountPerChunk_PROMPT  #language en-US "Specify the count of pre allocated SMM MP tokens per chunk.\n"\r
+\r
+#string STR_gUefiCpuPkgTokenSpaceGuid_PcdCpuSmmMpTokenCountPerChunk_HELP    #language en-US "This value used to specify the count of pre allocated SMM MP tokens per chunk.\n"\r