From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web12.1306.1575449716321379221 for ; Wed, 04 Dec 2019 00:55:16 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 134.134.136.31, 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 orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Dec 2019 00:55:15 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,276,1571727600"; d="scan'208";a="208734245" Received: from ydong10-desktop.ccr.corp.intel.com ([10.239.158.133]) by fmsmga008.fm.intel.com with ESMTP; 04 Dec 2019 00:55:12 -0800 From: "Dong, Eric" To: devel@edk2.groups.io Cc: Ray Ni , Laszlo Ersek Subject: [PATCH v3] UefiCpuPkg/PiSmmCpuDxeSmm: Avoid allocate Token every time Date: Wed, 4 Dec 2019 16:55:12 +0800 Message-Id: <20191204085512.1512-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 V3 changes:=0D Introduce PCD to control the pre allocated toke buffer size.=0D =0D v2 changes:=0D Minor update based on comments.=0D UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c | 67 ++++++++++++++++++-- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 15 +++++ UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf | 1 + UefiCpuPkg/UefiCpuPkg.dec | 4 ++ UefiCpuPkg/UefiCpuPkg.uni | 1 + 5 files changed, 84 insertions(+), 4 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c b/UefiCpuPkg/PiSmmCpuDxe= Smm/MpService.c index 0685637c2b..b0caa4f3db 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 + // Not free the buffer, just clear the UsedTokenNum. In order to=0D + // avoid triggering allocate action when we need to use the token,=0D + // do not free the buffer.=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 PcdGet32 (PcdTokenCountPerChunk);=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 PcdGet32 (PcdTokenCountPerChunk);=0D + ASSERT_EFI_ERROR (TokenCountPerChunk !=3D 0);=0D + if (TokenCountPerChunk =3D=3D 0) {=0D + DEBUG ((EFI_D_ERROR, "PcdTokenCountPerChunk should not be Zero!\n"));= =0D + CpuDeadLoop ();=0D + }=0D + DEBUG ((DEBUG_INFO, "CpuSmm: SpinLock Size =3D 0x%x, PreAllocateTokenNum= =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..a5607590ce 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 + UINTN 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..82d8c25db1 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf @@ -139,6 +139,7 @@ gEfiMdeModulePkgTokenSpaceGuid.PcdNullPointerDetectionPropertyMask ##= CONSUMES=0D gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPropertyMask ##= CONSUMES=0D gEfiMdePkgTokenSpaceGuid.PcdControlFlowEnforcementPropertyMask ##= CONSUMES=0D + gUefiCpuPkgTokenSpaceGuid.PcdTokenCountPerChunk ##= CONSUMES=0D =0D [Pcd.X64]=0D gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmRestrictedMemoryAccess ## CONS= UMES=0D diff --git a/UefiCpuPkg/UefiCpuPkg.dec b/UefiCpuPkg/UefiCpuPkg.dec index 12f4413ea5..349100371e 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 + ## Size of pre allocated token count per chunk.=0D + # @Prompt Specify the size of pre allocated token count per chunk.=0D + gUefiCpuPkgTokenSpaceGuid.PcdTokenCountPerChunk|64|UINT32|0x30002002=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..fb0491350a 100644 --- a/UefiCpuPkg/UefiCpuPkg.uni +++ b/UefiCpuPkg/UefiCpuPkg.uni @@ -272,3 +272,4 @@ = "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_PcdTokenCountPerChunk_PROMPT #langu= age en-US "Specify the size of pre allocated token count per chunk.\n" \ No newline at end of file --=20 2.23.0.windows.1