From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mx.groups.io with SMTP id smtpd.web12.29207.1577432935317389761 for ; Thu, 26 Dec 2019 23:48:55 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 192.55.52.136, mailfrom: eric.dong@intel.com) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 26 Dec 2019 23:48:54 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,362,1571727600"; d="scan'208";a="215134842" Received: from ydong10-desktop.ccr.corp.intel.com ([10.239.158.133]) by fmsmga008.fm.intel.com with ESMTP; 26 Dec 2019 23:48:53 -0800 From: "Dong, Eric" To: devel@edk2.groups.io Cc: Ray Ni , Laszlo Ersek Subject: [PATCH v2] UefiCpuPkg/PiSmmCpuDxeSmm: Pre-allocate PROCEDURE_TOKEN buffer Date: Fri, 27 Dec 2019 15:48:52 +0800 Message-Id: <20191227074852.1332-1-eric.dong@intel.com> X-Mailer: git-send-email 2.23.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2388 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. Former change (9caaa79dd7e078ebb4012dde3b3d3a5d451df609) missed PROCEDURE_TOKEN part, this change covers it. Cc: Ray Ni Cc: Laszlo Ersek Signed-off-by: Eric Dong ---=0D v2 changes:=0D Remove the not used variable.=0D UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c | 190 ++++++++++++--------- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 6 +- 2 files changed, 109 insertions(+), 87 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c b/UefiCpuPkg/PiSmmCpuDxe= Smm/MpService.c index 4808045f71..870250b0c5 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c @@ -429,38 +429,29 @@ ReleaseToken ( =0D **/=0D VOID=0D -FreeTokens (=0D +ResetTokens (=0D VOID=0D )=0D {=0D LIST_ENTRY *Link;=0D PROCEDURE_TOKEN *ProcToken;=0D - TOKEN_BUFFER *TokenBuf;=0D =0D - //=0D - // Only free the token buffer recorded in the OldTOkenBufList=0D - // upon exiting SMI. Current token buffer stays allocated so=0D - // next SMI doesn't need to re-allocate.=0D - //=0D - gSmmCpuPrivate->UsedTokenNum =3D 0;=0D -=0D - Link =3D GetFirstNode (&gSmmCpuPrivate->OldTokenBufList);=0D - while (!IsNull (&gSmmCpuPrivate->OldTokenBufList, Link)) {=0D - TokenBuf =3D TOKEN_BUFFER_FROM_LINK (Link);=0D -=0D - Link =3D RemoveEntryList (&TokenBuf->Link);=0D -=0D - FreePool (TokenBuf->Buffer);=0D - FreePool (TokenBuf);=0D - }=0D -=0D - while (!IsListEmpty (&gSmmCpuPrivate->TokenList)) {=0D - Link =3D GetFirstNode (&gSmmCpuPrivate->TokenList);=0D + Link =3D GetFirstNode (&gSmmCpuPrivate->TokenList);=0D + while (!IsNull (&gSmmCpuPrivate->TokenList, Link)) {=0D ProcToken =3D PROCEDURE_TOKEN_FROM_LINK (Link);=0D =0D - RemoveEntryList (&ProcToken->Link);=0D + ProcToken->RunningApCount =3D 0;=0D + ProcToken->Used =3D FALSE;=0D +=0D + //=0D + // Check the spinlock status and release it if not released yet.=0D + //=0D + if (!AcquireSpinLockOrFail(ProcToken->SpinLock)) {=0D + DEBUG((DEBUG_ERROR, "Risk::SpinLock still not released!"));=0D + }=0D + ReleaseSpinLock (ProcToken->SpinLock);=0D =0D - FreePool (ProcToken);=0D + Link =3D GetNextNode (&gSmmCpuPrivate->TokenList, Link);=0D }=0D }=0D =0D @@ -685,9 +676,9 @@ BSPHandler ( WaitForAllAPs (ApCount);=0D =0D //=0D - // Clean the tokens buffer.=0D + // Reset the tokens buffer.=0D //=0D - FreeTokens ();=0D + ResetTokens ();=0D =0D //=0D // Reset BspIndex to -1, meaning BSP has not been elected.=0D @@ -1056,7 +1047,7 @@ IsTokenInUse ( while (!IsNull (&gSmmCpuPrivate->TokenList, Link)) {=0D ProcToken =3D PROCEDURE_TOKEN_FROM_LINK (Link);=0D =0D - if (ProcToken->SpinLock =3D=3D Token) {=0D + if (ProcToken->Used && ProcToken->SpinLock =3D=3D Token) {=0D return TRUE;=0D }=0D =0D @@ -1067,61 +1058,112 @@ IsTokenInUse ( }=0D =0D /**=0D - create token and save it to the maintain list.=0D -=0D - @param RunningApCount Input running AP count.=0D -=0D - @retval return the spin lock used as token.=0D + Allocate buffer for the SPIN_LOCK and PROCEDURE_TOKEN.=0D =0D **/=0D -PROCEDURE_TOKEN *=0D -CreateToken (=0D - IN UINT32 RunningApCount=0D +VOID=0D +AllocateTokenBuffer (=0D + VOID=0D )=0D {=0D - PROCEDURE_TOKEN *ProcToken;=0D - SPIN_LOCK *SpinLock;=0D UINTN SpinLockSize;=0D - TOKEN_BUFFER *TokenBuf;=0D UINT32 TokenCountPerChunk;=0D + UINTN ProcTokenSize;=0D + UINTN Index;=0D + PROCEDURE_TOKEN *ProcToken;=0D + SPIN_LOCK *SpinLock;=0D + UINT8 *SpinLockBuffer;=0D + UINT8 *ProcTokenBuffer;=0D =0D SpinLockSize =3D GetSpinLockProperties ();=0D + ProcTokenSize =3D sizeof (PROCEDURE_TOKEN);=0D +=0D TokenCountPerChunk =3D FixedPcdGet32 (PcdCpuSmmMpTokenCountPerChunk);=0D + ASSERT (TokenCountPerChunk !=3D 0);=0D + if (TokenCountPerChunk =3D=3D 0) {=0D + DEBUG ((DEBUG_ERROR, "PcdCpuSmmMpTokenCountPerChunk should not be Zero= !\n"));=0D + CpuDeadLoop ();=0D + }=0D + DEBUG ((DEBUG_INFO, "CpuSmm: SpinLock Size =3D 0x%x, PcdCpuSmmMpTokenCou= ntPerChunk =3D 0x%x\n", SpinLockSize, TokenCountPerChunk));=0D =0D - if (gSmmCpuPrivate->UsedTokenNum =3D=3D TokenCountPerChunk) {=0D - DEBUG ((DEBUG_VERBOSE, "CpuSmm: No free token buffer, allocate new buf= fer!\n"));=0D + //=0D + // Separate the Spin_lock and Proc_token because the alignment requires = by Spin_Lock.=0D + //=0D + SpinLockBuffer =3D AllocatePool (SpinLockSize * TokenCountPerChunk);=0D + ASSERT (SpinLockBuffer !=3D NULL);=0D =0D - //=0D - // Record current token buffer for later free action usage.=0D - // Current used token buffer not in this list.=0D - //=0D - TokenBuf =3D AllocatePool (sizeof (TOKEN_BUFFER));=0D - ASSERT (TokenBuf !=3D NULL);=0D - TokenBuf->Signature =3D TOKEN_BUFFER_SIGNATURE;=0D - TokenBuf->Buffer =3D gSmmCpuPrivate->CurrentTokenBuf;=0D + ProcTokenBuffer =3D AllocatePool (ProcTokenSize * TokenCountPerChunk);=0D + ASSERT (ProcTokenBuffer !=3D NULL);=0D +=0D + for (Index =3D 0; Index < TokenCountPerChunk; Index++) {=0D + SpinLock =3D (SPIN_LOCK *)(SpinLockBuffer + SpinLockSize * Index);=0D + InitializeSpinLock (SpinLock);=0D +=0D + ProcToken =3D (PROCEDURE_TOKEN *)(ProcTokenBuffer + ProcTokenSize * In= dex);=0D + ProcToken->Signature =3D PROCEDURE_TOKEN_SIGNATURE;=0D + ProcToken->SpinLock =3D SpinLock;=0D + ProcToken->Used =3D FALSE;=0D + ProcToken->RunningApCount =3D 0;=0D +=0D + InsertTailList (&gSmmCpuPrivate->TokenList, &ProcToken->Link);=0D + }=0D +}=0D =0D - InsertTailList (&gSmmCpuPrivate->OldTokenBufList, &TokenBuf->Link);=0D +/**=0D + Find first free token in the allocated token list.=0D +=0D + @retval return the first free PROCEDURE_TOKEN.=0D +=0D +**/=0D +PROCEDURE_TOKEN *=0D +FindFirstFreeToken (=0D + VOID=0D + )=0D +{=0D + LIST_ENTRY *Link;=0D + PROCEDURE_TOKEN *ProcToken;=0D =0D - gSmmCpuPrivate->CurrentTokenBuf =3D AllocatePool (SpinLockSize * Token= CountPerChunk);=0D - ASSERT (gSmmCpuPrivate->CurrentTokenBuf !=3D NULL);=0D - gSmmCpuPrivate->UsedTokenNum =3D 0;=0D + Link =3D GetFirstNode (&gSmmCpuPrivate->TokenList);=0D + while (!IsNull (&gSmmCpuPrivate->TokenList, Link)) {=0D + ProcToken =3D PROCEDURE_TOKEN_FROM_LINK (Link);=0D +=0D + if (!ProcToken->Used) {=0D + return ProcToken;=0D + }=0D +=0D + Link =3D GetNextNode (&gSmmCpuPrivate->TokenList, Link);=0D }=0D =0D - SpinLock =3D (SPIN_LOCK *)(gSmmCpuPrivate->CurrentTokenBuf + SpinLockSiz= e * gSmmCpuPrivate->UsedTokenNum);=0D - gSmmCpuPrivate->UsedTokenNum++;=0D + return NULL;=0D +}=0D +=0D +/**=0D + Get the free token.=0D +=0D + If no free token, allocate new tokens then return the free one.=0D +=0D + @retval return the first free PROCEDURE_TOKEN.=0D =0D - InitializeSpinLock (SpinLock);=0D - AcquireSpinLock (SpinLock);=0D +**/=0D +PROCEDURE_TOKEN *=0D +GetFreeToken (=0D + IN UINT32 RunningApsCount=0D + )=0D +{=0D + PROCEDURE_TOKEN *NewToken;=0D =0D - ProcToken =3D AllocatePool (sizeof (PROCEDURE_TOKEN));=0D - ASSERT (ProcToken !=3D NULL);=0D - ProcToken->Signature =3D PROCEDURE_TOKEN_SIGNATURE;=0D - ProcToken->SpinLock =3D SpinLock;=0D - ProcToken->RunningApCount =3D RunningApCount;=0D + NewToken =3D FindFirstFreeToken ();=0D + if (NewToken =3D=3D NULL) {=0D + AllocateTokenBuffer ();=0D + NewToken =3D FindFirstFreeToken ();=0D + }=0D + ASSERT (NewToken !=3D NULL);=0D =0D - InsertTailList (&gSmmCpuPrivate->TokenList, &ProcToken->Link);=0D + NewToken->Used =3D TRUE;=0D + NewToken->RunningApCount =3D RunningApsCount;=0D + AcquireSpinLock (NewToken->SpinLock);=0D =0D - return ProcToken;=0D + return NewToken;=0D }=0D =0D /**=0D @@ -1231,7 +1273,7 @@ InternalSmmStartupThisAp ( mSmmMpSyncData->CpuData[CpuIndex].Procedure =3D Procedure;=0D mSmmMpSyncData->CpuData[CpuIndex].Parameter =3D ProcArguments;=0D if (Token !=3D NULL) {=0D - ProcToken=3D CreateToken (1);=0D + ProcToken=3D GetFreeToken (1);=0D mSmmMpSyncData->CpuData[CpuIndex].Token =3D ProcToken;=0D *Token =3D (MM_COMPLETION)ProcToken->SpinLock;=0D }=0D @@ -1320,7 +1362,7 @@ InternalSmmStartupAllAPs ( }=0D =0D if (Token !=3D NULL) {=0D - ProcToken =3D CreateToken ((UINT32)mMaxNumberOfCpus);=0D + ProcToken =3D GetFreeToken ((UINT32)mMaxNumberOfCpus);=0D *Token =3D (MM_COMPLETION)ProcToken->SpinLock;=0D } else {=0D ProcToken =3D NULL;=0D @@ -1732,28 +1774,12 @@ InitializeDataForMmMp ( VOID=0D )=0D {=0D - UINTN SpinLockSize;=0D - UINT32 TokenCountPerChunk;=0D -=0D - SpinLockSize =3D GetSpinLockProperties ();=0D - TokenCountPerChunk =3D FixedPcdGet32 (PcdCpuSmmMpTokenCountPerChunk);=0D - ASSERT (TokenCountPerChunk !=3D 0);=0D - if (TokenCountPerChunk =3D=3D 0) {=0D - DEBUG ((DEBUG_ERROR, "PcdCpuSmmMpTokenCountPerChunk should not be Zero= !\n"));=0D - CpuDeadLoop ();=0D - }=0D - DEBUG ((DEBUG_INFO, "CpuSmm: SpinLock Size =3D 0x%x, PcdCpuSmmMpTokenCou= ntPerChunk =3D 0x%x\n", SpinLockSize, TokenCountPerChunk));=0D -=0D - gSmmCpuPrivate->CurrentTokenBuf =3D AllocatePool (SpinLockSize * TokenCo= untPerChunk);=0D - ASSERT (gSmmCpuPrivate->CurrentTokenBuf !=3D NULL);=0D -=0D - gSmmCpuPrivate->UsedTokenNum =3D 0;=0D -=0D gSmmCpuPrivate->ApWrapperFunc =3D AllocatePool (sizeof (PROCEDURE_WRAPPE= R) * gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus);=0D ASSERT (gSmmCpuPrivate->ApWrapperFunc !=3D NULL);=0D =0D InitializeListHead (&gSmmCpuPrivate->TokenList);=0D - InitializeListHead (&gSmmCpuPrivate->OldTokenBufList);=0D +=0D + AllocateTokenBuffer ();=0D }=0D =0D /**=0D diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h b/UefiCpuPkg/PiSmmC= puDxeSmm/PiSmmCpuDxeSmm.h index 5c98494e2c..33b3dd140e 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h @@ -214,6 +214,7 @@ typedef struct { =0D SPIN_LOCK *SpinLock;=0D volatile UINT32 RunningApCount;=0D + BOOLEAN Used;=0D } PROCEDURE_TOKEN;=0D =0D #define PROCEDURE_TOKEN_FROM_LINK(a) CR (a, PROCEDURE_TOKEN, Link, PROCED= URE_TOKEN_SIGNATURE)=0D @@ -254,11 +255,6 @@ typedef struct { =0D PROCEDURE_WRAPPER *ApWrapperFunc;=0D LIST_ENTRY TokenList;=0D -=0D - LIST_ENTRY OldTokenBufList;=0D -=0D - UINT8 *CurrentTokenBuf;=0D - UINT32 UsedTokenNum; // Only record tokens = used in CurrentTokenBuf.=0D } SMM_CPU_PRIVATE_DATA;=0D =0D extern SMM_CPU_PRIVATE_DATA *gSmmCpuPrivate;=0D --=20 2.23.0.windows.1