From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by mx.groups.io with SMTP id smtpd.web10.5889.1575529688459926727 for ; Wed, 04 Dec 2019 23:08:08 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 192.55.52.151, mailfrom: eric.dong@intel.com) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Dec 2019 23:08:08 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,280,1571727600"; d="scan'208";a="243143247" Received: from ydong10-desktop.ccr.corp.intel.com ([10.239.158.133]) by fmsmga002.fm.intel.com with ESMTP; 04 Dec 2019 23:08:07 -0800 From: "Dong, Eric" To: devel@edk2.groups.io Cc: Ray Ni , Laszlo Ersek Subject: [PATCH v5] UefiCpuPkg/PiSmmCpuDxeSmm: Avoid allocate Token every time Date: Thu, 5 Dec 2019 15:08:05 +0800 Message-Id: <20191205070805.1951-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. Signed-off-by: Eric Dong Cc: Ray Ni Cc: Laszlo Ersek ---=0D V5 changes:=0D Refine PCD names and some code comments.=0D V4 changes:=0D Specify PCD type to FixedPcd in code. =0D V3 changes:=0D Introduce PCD to control the pre allocated toke buffer size.=0D v2 changes:=0D Minor update based on comments.=0D UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c | 67 ++++++++++++++++++-- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 15 +++++ UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf | 3 + UefiCpuPkg/UefiCpuPkg.dec | 4 ++ UefiCpuPkg/UefiCpuPkg.uni | 3 + 5 files changed, 88 insertions(+), 4 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c b/UefiCpuPkg/PiSmmCpuDxe= Smm/MpService.c index 0685637c2b..757f1056f7 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c @@ -492,6 +492,24 @@ FreeTokens ( {=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 @@ -499,7 +517,6 @@ FreeTokens ( =0D RemoveEntryList (&ProcToken->Link);=0D =0D - FreePool ((VOID *)ProcToken->ProcedureToken);=0D FreePool (ProcToken);=0D }=0D }=0D @@ -1115,13 +1132,37 @@ CreateToken ( VOID=0D )=0D {=0D - PROCEDURE_TOKEN *ProcToken;=0D + PROCEDURE_TOKEN *ProcToken;=0D SPIN_LOCK *CpuToken;=0D UINTN SpinLockSize;=0D + TOKEN_BUFFER *TokenBuf;=0D + UINT32 TokenCountPerChunk;=0D =0D SpinLockSize =3D GetSpinLockProperties ();=0D - CpuToken =3D AllocatePool (SpinLockSize);=0D - ASSERT (CpuToken !=3D NULL);=0D + TokenCountPerChunk =3D FixedPcdGet32 (PcdCpuSmmMpTokenCountPerChunk);=0D +=0D + if (gSmmCpuPrivate->UsedTokenNum =3D=3D TokenCountPerChunk) {=0D + DEBUG ((DEBUG_VERBOSE, "CpuSmm: No free token buffer, allocate new buf= fer!\n"));=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 +=0D + InsertTailList (&gSmmCpuPrivate->OldTokenBufList, &TokenBuf->Link);=0D +=0D + gSmmCpuPrivate->CurrentTokenBuf =3D AllocatePool (SpinLockSize * Token= CountPerChunk);=0D + ASSERT (gSmmCpuPrivate->CurrentTokenBuf !=3D NULL);=0D + gSmmCpuPrivate->UsedTokenNum =3D 0;=0D + }=0D +=0D + CpuToken =3D (SPIN_LOCK *)(gSmmCpuPrivate->CurrentTokenBuf + SpinLockSiz= e * gSmmCpuPrivate->UsedTokenNum);=0D + gSmmCpuPrivate->UsedTokenNum++;=0D +=0D InitializeSpinLock (CpuToken);=0D AcquireSpinLock (CpuToken);=0D =0D @@ -1732,10 +1773,28 @@ 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 =0D /**=0D diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h b/UefiCpuPkg/PiSmmC= puDxeSmm/PiSmmCpuDxeSmm.h index 7e7c73f27f..5c1a01e42b 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h @@ -217,6 +217,17 @@ typedef struct { =0D #define PROCEDURE_TOKEN_FROM_LINK(a) CR (a, PROCEDURE_TOKEN, Link, PROCED= URE_TOKEN_SIGNATURE)=0D =0D +#define TOKEN_BUFFER_SIGNATURE SIGNATURE_32 ('T', 'K', 'B', 'S')=0D +=0D +typedef struct {=0D + UINTN Signature;=0D + LIST_ENTRY Link;=0D +=0D + UINT8 *Buffer;=0D +} TOKEN_BUFFER;=0D +=0D +#define TOKEN_BUFFER_FROM_LINK(a) CR (a, TOKEN_BUFFER, Link, TOKEN_BUFFER= _SIGNATURE)=0D +=0D //=0D // Private structure for the SMM CPU module that is stored in DXE Runtime = memory=0D // Contains the SMM Configuration Protocols that is produced.=0D @@ -243,6 +254,10 @@ typedef struct { 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 diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf b/UefiCpuPkg/PiSm= mCpuDxeSmm/PiSmmCpuDxeSmm.inf index b12b2691f8..76b1462996 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf @@ -140,6 +140,9 @@ gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPropertyMask ##= CONSUMES=0D gEfiMdePkgTokenSpaceGuid.PcdControlFlowEnforcementPropertyMask ##= CONSUMES=0D =0D +[FixedPcd]=0D + gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmMpTokenCountPerChunk ##= CONSUMES=0D +=0D [Pcd.X64]=0D gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmRestrictedMemoryAccess ## CONS= UMES=0D =0D diff --git a/UefiCpuPkg/UefiCpuPkg.dec b/UefiCpuPkg/UefiCpuPkg.dec index 12f4413ea5..797f948631 100644 --- a/UefiCpuPkg/UefiCpuPkg.dec +++ b/UefiCpuPkg/UefiCpuPkg.dec @@ -148,6 +148,10 @@ # @Prompt Specify size of good stack of exception which need switching s= tack.=0D gUefiCpuPkgTokenSpaceGuid.PcdCpuKnownGoodStackSize|2048|UINT32|0x3000200= 1=0D =0D + ## Count of pre allocated SMM MP tokens per chunk.=0D + # @Prompt Specify the count of pre allocated SMM MP tokens per chunk.=0D + gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmMpTokenCountPerChunk|64|UINT32|0x3000= 2002=0D +=0D [PcdsFixedAtBuild, PcdsPatchableInModule]=0D ## This value is the CPU Local APIC base address, which aligns the addre= ss on a 4-KByte boundary.=0D # @Prompt Configure base address of CPU Local APIC=0D diff --git a/UefiCpuPkg/UefiCpuPkg.uni b/UefiCpuPkg/UefiCpuPkg.uni index bfd696f48c..442ce5cb85 100644 --- a/UefiCpuPkg/UefiCpuPkg.uni +++ b/UefiCpuPkg/UefiCpuPkg.uni @@ -272,3 +272,6 @@ = "24000000 - 6th and 7th generation Intel Core processor= s and Intel Xeon W Processor Family(24MHz).
\n"=0D = "19200000 - Intel Atom processors based on Goldmont Mic= roarchitecture with CPUID signature 06_5CH(19.2MHz).
\n"=0D =0D +#string STR_gUefiCpuPkgTokenSpaceGuid_PcdCpuSmmMpTokenCountPerChunk_PROMPT= #language en-US "Specify the count of pre allocated SMM MP tokens per chu= nk.\n"=0D +=0D +#string STR_gUefiCpuPkgTokenSpaceGuid_PcdCpuSmmMpTokenCountPerChunk_HELP = #language en-US "This value used to specify the count of pre allocated SM= M MP tokens per chunk.\n" \ No newline at end of file --=20 2.23.0.windows.1