From nobody Tue May 21 05:56:44 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+57141+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+57141+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1586461638; cv=none; d=zohomail.com; s=zohoarc; b=c+L8HAiZhUMIGW8Q+o/HQL7stiAS35wVjdM7SxbJeE5P2qfBboaohctO92ztiExJEoMdJZMr3qyO1/Ph3neA3QaxojllFzTSOgcJu7D4NQ6WCC3VzeLU2iRjG0r/ktsr1SxLSD8mkOigEFJIzQlhAQq9zj3FmPrmBW0gNgBO7VE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1586461638; h=Content-Transfer-Encoding:Cc:Date:From:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=z0DtFbAxCccZCIqYls23ZTig9ofMkxftlByCYMInwXs=; b=R55DdD0jNs/y1XdO/9GJBjyDEjDE5o3VHB66DJQNroZve6r1NcQIvK/EQjECxx5rE5p1uSOPBVi652NNFLqlP3IKnGtLRaPhoo6zeSMnXI7v8F21Et4KTCrQtOzVDj/gzHXZCbZk3soxdznko9LqMblPx9pdRnowyreWfvIzV8I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+57141+1787277+3901457@groups.io Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 158646163798319.969278797233414; Thu, 9 Apr 2020 12:47:17 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id lWxFYY1788612xask5uhlqxt; Thu, 09 Apr 2020 12:47:17 -0700 X-Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by mx.groups.io with SMTP id smtpd.web10.17578.1586461267611129673 for ; Thu, 09 Apr 2020 12:41:07 -0700 IronPort-SDR: lK0SD1lmWTirp1gWVfh/A0+jPwsg57oajUnhKlLt7H+bGhxzzvgqnczPI3fFHoskc8VQMlZ/h3 ppfTabYpIDbw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Apr 2020 12:41:07 -0700 IronPort-SDR: KHeVGLzqmJYL5NTg+vRdc+kAFoWdT0NjEH50d1x63yLo+V2+L9Pfr0kanrzLDw54zhsdNmwhXu Vsa5hKOhZq8g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,363,1580803200"; d="scan'208";a="275949539" X-Received: from ray-dev.ccr.corp.intel.com ([10.239.158.43]) by fmsmga004.fm.intel.com with ESMTP; 09 Apr 2020 12:41:05 -0700 From: Ray Ni To: devel@edk2.groups.io Cc: Ray Ni , Eric Dong , Star Zeng Subject: [edk2-devel] [PATCH] UefiCpuPkg/PiSmmCpuDxeSmm: Improve the performance of GetFreeToken() Date: Fri, 10 Apr 2020 03:40:33 +0800 Message-Id: <20200409194033.82256-1-niruiyu@users.noreply.github.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,niruiyu@users.noreply.github.com X-Gm-Message-State: yFODUty4tl77HjnzoBjVo0cix1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1586461637; bh=RkHtI5C1NzXFtAm+2Zbk7ymOrHL2/XsWTc3N5WMRBEw=; h=Cc:Date:From:Reply-To:Subject:To; b=Tee2f9ZTl8XDoQ0VCanCNEK76zxN+xIZTAyA39FL/Pp3aHrAkP5YAKlj6t8tsMIt3Hq See/0H57e8f2vVLxIHOJVygdxwSGyQ8Q5I/53npxjxMKQxwwQGKNsxLsJ393bxJQL9OyO 50EhAQ1iiKt3mBkWymjFA9Tpe8MJrXdB4zI= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" Today's GetFreeToken() runs at the algorithm complexity of O(n) where n is the size of the token list. The change introduces a new global variable FirstFreeToken and it always points to the first free token. So the algorithm complexity of GetFreeToken() decreases from O(n) to O(1). The improvement matters when some SMI code uses StartupThisAP() service for each of the AP such that the algorithm complexity becomes O(n) * O(m) where m is the AP count. As next steps, 1. PROCEDURE_TOKEN.Used field can be optimized out because all tokens before FirstFreeToken should have "Used" set while all after FirstFreeToken should have "Used" cleared. 2. ResetTokens() can be optimized to only reset tokens before FirstFreeToken. Signed-off-by: Ray Ni Cc: Eric Dong Cc: Star Zeng --- UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c | 71 ++++++++-------------- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 3 +- 2 files changed, 27 insertions(+), 47 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c b/UefiCpuPkg/PiSmmCpuDxe= Smm/MpService.c index c285a70ebb..7a223410d7 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c @@ -1,7 +1,7 @@ /** @file SMM MP service implementation =20 -Copyright (c) 2009 - 2020, Intel Corporation. All rights reserved.
+Copyright (c) 2009 - 2021, Intel Corporation. All rights reserved.
Copyright (c) 2017, AMD Incorporated. All rights reserved.
=20 SPDX-License-Identifier: BSD-2-Clause-Patent @@ -453,6 +453,11 @@ ResetTokens ( =20 Link =3D GetNextNode (&gSmmCpuPrivate->TokenList, Link); } + + // + // Reset the FirstFreeToken to the beginning of token list upon exiting = SMI. + // + gSmmCpuPrivate->FirstFreeToken =3D GetFirstNode (&gSmmCpuPrivate->TokenL= ist); } =20 /** @@ -1060,23 +1065,21 @@ IsTokenInUse ( /** Allocate buffer for the SPIN_LOCK and PROCEDURE_TOKEN. =20 + @return First token of the token buffer. **/ -VOID +LIST_ENTRY * AllocateTokenBuffer ( VOID ) { UINTN SpinLockSize; UINT32 TokenCountPerChunk; - UINTN ProcTokenSize; UINTN Index; - PROCEDURE_TOKEN *ProcToken; SPIN_LOCK *SpinLock; UINT8 *SpinLockBuffer; - UINT8 *ProcTokenBuffer; + PROCEDURE_TOKEN *ProcTokens; =20 SpinLockSize =3D GetSpinLockProperties (); - ProcTokenSize =3D sizeof (PROCEDURE_TOKEN); =20 TokenCountPerChunk =3D FixedPcdGet32 (PcdCpuSmmMpTokenCountPerChunk); ASSERT (TokenCountPerChunk !=3D 0); @@ -1092,49 +1095,22 @@ AllocateTokenBuffer ( SpinLockBuffer =3D AllocatePool (SpinLockSize * TokenCountPerChunk); ASSERT (SpinLockBuffer !=3D NULL); =20 - ProcTokenBuffer =3D AllocatePool (ProcTokenSize * TokenCountPerChunk); - ASSERT (ProcTokenBuffer !=3D NULL); + ProcTokens =3D AllocatePool (sizeof (PROCEDURE_TOKEN) * TokenCountPerChu= nk); + ASSERT (ProcTokens !=3D NULL); =20 for (Index =3D 0; Index < TokenCountPerChunk; Index++) { SpinLock =3D (SPIN_LOCK *)(SpinLockBuffer + SpinLockSize * Index); InitializeSpinLock (SpinLock); =20 - ProcToken =3D (PROCEDURE_TOKEN *)(ProcTokenBuffer + ProcTokenSize * In= dex); - ProcToken->Signature =3D PROCEDURE_TOKEN_SIGNATURE; - ProcToken->SpinLock =3D SpinLock; - ProcToken->Used =3D FALSE; - ProcToken->RunningApCount =3D 0; + ProcTokens[Index].Signature =3D PROCEDURE_TOKEN_SIGNATURE; + ProcTokens[Index].SpinLock =3D SpinLock; + ProcTokens[Index].Used =3D FALSE; + ProcTokens[Index].RunningApCount =3D 0; =20 - InsertTailList (&gSmmCpuPrivate->TokenList, &ProcToken->Link); + InsertTailList (&gSmmCpuPrivate->TokenList, &ProcTokens[Index].Link); } -} =20 -/** - Find first free token in the allocated token list. - - @retval return the first free PROCEDURE_TOKEN. - -**/ -PROCEDURE_TOKEN * -FindFirstFreeToken ( - VOID - ) -{ - LIST_ENTRY *Link; - PROCEDURE_TOKEN *ProcToken; - - Link =3D GetFirstNode (&gSmmCpuPrivate->TokenList); - while (!IsNull (&gSmmCpuPrivate->TokenList, Link)) { - ProcToken =3D PROCEDURE_TOKEN_FROM_LINK (Link); - - if (!ProcToken->Used) { - return ProcToken; - } - - Link =3D GetNextNode (&gSmmCpuPrivate->TokenList, Link); - } - - return NULL; + return &ProcTokens[0].Link; } =20 /** @@ -1154,12 +1130,15 @@ GetFreeToken ( { PROCEDURE_TOKEN *NewToken; =20 - NewToken =3D FindFirstFreeToken (); - if (NewToken =3D=3D NULL) { - AllocateTokenBuffer (); - NewToken =3D FindFirstFreeToken (); + // + // If FirstFreeToken meets the end of token list, enlarge the token list. + // Set FirstFreeToken to the first free token. + // + if (gSmmCpuPrivate->FirstFreeToken =3D=3D &gSmmCpuPrivate->TokenList) { + gSmmCpuPrivate->FirstFreeToken =3D AllocateTokenBuffer (); } - ASSERT (NewToken !=3D NULL); + NewToken =3D PROCEDURE_TOKEN_FROM_LINK (gSmmCpuPrivate->FirstFreeToken); + gSmmCpuPrivate->FirstFreeToken =3D GetNextNode (&gSmmCpuPrivate->TokenLi= st, gSmmCpuPrivate->FirstFreeToken); =20 NewToken->Used =3D TRUE; NewToken->RunningApCount =3D RunningApsCount; diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h b/UefiCpuPkg/PiSmmC= puDxeSmm/PiSmmCpuDxeSmm.h index fe7e8b0323..17f4bd34fa 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h @@ -1,7 +1,7 @@ /** @file Agent Module to load other modules to deploy SMM Entry Vector for X86 CPU. =20 -Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.
+Copyright (c) 2009 - 2021, Intel Corporation. All rights reserved.
Copyright (c) 2017, AMD Incorporated. All rights reserved.
=20 SPDX-License-Identifier: BSD-2-Clause-Patent @@ -255,6 +255,7 @@ typedef struct { =20 PROCEDURE_WRAPPER *ApWrapperFunc; LIST_ENTRY TokenList; + LIST_ENTRY *FirstFreeToken; } SMM_CPU_PRIVATE_DATA; =20 extern SMM_CPU_PRIVATE_DATA *gSmmCpuPrivate; --=20 2.21.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#57141): https://edk2.groups.io/g/devel/message/57141 Mute This Topic: https://groups.io/mt/72905543/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-