From nobody Sun May 19 12:45:57 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+92780+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+92780+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1661396119; cv=none; d=zohomail.com; s=zohoarc; b=KNBplONyc/ITr5rHCKG62kgmkpO2u4I4ox0ebFveZFvgIittiKMUkm7sD3vcU65X5Oo+4u51TRmHYmht+o5qoe865WgLJnNRCobB50S43nG/zLXcHx5oEDoFAG3vyZ+1Mss1Jg5Ck4chqEa9pGt7k+BvPRIXtTu+DaX7DnNjhnw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661396119; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=6BlDunKHyHI/Vl3hch6mfyYJbh2OIXBE51pkgqC84O8=; b=JS5RArznEMnYewaEgkCzdFEllI4NpsVaeAoDjS1sDnju6rQjS3HJs694eOnh/Wr77HP7QIt5qMhqKsWO3jZEdeXhGha5bvnfaXJtqsFD4q0LNQXgsRr1kmoYm/sf8fmn/XAzA0G2wQ1tzKmoFHUsqaec155bSwx3bXI0xKRKo8M= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+92780+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1661396119377777.4343855135949; Wed, 24 Aug 2022 19:55:19 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id fgqIYY1788612xym7VBmiiKq; Wed, 24 Aug 2022 19:55:19 -0700 X-Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web11.19719.1661396118095781814 for ; Wed, 24 Aug 2022 19:55:18 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10449"; a="355855700" X-IronPort-AV: E=Sophos;i="5.93,262,1654585200"; d="scan'208";a="355855700" X-Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Aug 2022 19:55:17 -0700 X-IronPort-AV: E=Sophos;i="5.93,262,1654585200"; d="scan'208";a="670782618" X-Received: from shwdesfp01.ccr.corp.intel.com ([10.239.158.151]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Aug 2022 19:55:15 -0700 From: "Zhiguang Liu" To: devel@edk2.groups.io Cc: Zhiguang Liu , Eric Dong , Ray Ni , Rahul Kumar Subject: [edk2-devel] [PATCH] UefiCpuPkg: Enhance logic in InitializeMpExceptionStackSwitchHandlers Date: Thu, 25 Aug 2022 10:55:03 +0800 Message-Id: <20220825025506.2323-1-zhiguang.liu@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: 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,zhiguang.liu@intel.com X-Gm-Message-State: qrpAOOVkBl01ZDVwXFhGbmLJx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1661396119; bh=jlKAPuk1nvtvfjVMdyj5wrnFAWAQW64B0ZkOpCWmyN8=; h=Cc:Date:From:Reply-To:Subject:To; b=gcXIFC47EKO5UanCtsKOUouTuhMnpaWyz25dzK0W62MwMvgmDtdeP5Tt5sOmKOyKiOU 5nPBrbC4f3Po3JfH3/5n8Pyscl0dG9whqMeGI540DDrvo1/V81Of1NIQKyGvttokWd2/a kZOCjv1sQ9aYG9xFGNvHZKkcHD4p+KXzloM= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1661396120106100001 Content-Type: text/plain; charset="utf-8" Parallelly run the function to SeparateExceptionStacks for all CPUs and allocate buffers together for better performance. Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Signed-off-by: Zhiguang Liu --- UefiCpuPkg/CpuDxe/CpuMp.c | 93 ++++++++++++++++++-------------- UefiCpuPkg/CpuMpPei/CpuMpPei.c | 96 +++++++++++++++++++--------------- 2 files changed, 109 insertions(+), 80 deletions(-) diff --git a/UefiCpuPkg/CpuDxe/CpuMp.c b/UefiCpuPkg/CpuDxe/CpuMp.c index f3ca813d2a..6362e03f1b 100644 --- a/UefiCpuPkg/CpuDxe/CpuMp.c +++ b/UefiCpuPkg/CpuDxe/CpuMp.c @@ -600,8 +600,9 @@ CollectBistDataFromHob ( // Structure for InitializeSeparateExceptionStacks // typedef struct { - VOID *Buffer; - UINTN *BufferSize; + VOID *Buffer; + UINTN BufferSize; + EFI_STATUS Status; } EXCEPTION_STACK_SWITCH_CONTEXT; =20 /** @@ -620,9 +621,13 @@ InitializeExceptionStackSwitchHandlers ( ) { EXCEPTION_STACK_SWITCH_CONTEXT *SwitchStackData; + UINTN Index; =20 + MpInitLibWhoAmI (&Index); SwitchStackData =3D (EXCEPTION_STACK_SWITCH_CONTEXT *)Buffer; - InitializeSeparateExceptionStacks (SwitchStackData->Buffer, SwitchStackD= ata->BufferSize); + if ((SwitchStackData[Index].Status =3D=3D EFI_NOT_STARTED) || (SwitchSta= ckData[Index].Status =3D=3D EFI_BUFFER_TOO_SMALL)) { + SwitchStackData[Index].Status =3D InitializeSeparateExceptionStacks (S= witchStackData[Index].Buffer, &SwitchStackData[Index].BufferSize); + } } =20 /** @@ -638,53 +643,63 @@ InitializeMpExceptionStackSwitchHandlers ( ) { UINTN Index; - UINTN Bsp; - EXCEPTION_STACK_SWITCH_CONTEXT SwitchStackData; + EXCEPTION_STACK_SWITCH_CONTEXT *SwitchStackData; UINTN BufferSize; + EFI_STATUS Status; + VOID *Buffer; =20 - SwitchStackData.BufferSize =3D &BufferSize; - MpInitLibWhoAmI (&Bsp); + SwitchStackData =3D AllocateRuntimeZeroPool (mNumberOfProcessors * sizeo= f (EXCEPTION_STACK_SWITCH_CONTEXT)); =20 for (Index =3D 0; Index < mNumberOfProcessors; ++Index) { - SwitchStackData.Buffer =3D NULL; - BufferSize =3D 0; + // + // Because the procedure may runs multiple times, use the status EFI_N= OT_STARTED + // to indicate the procedure haven't been run yet. + // + SwitchStackData[Index].Status =3D EFI_NOT_STARTED; + } + + Status =3D MpInitLibStartupAllCPUs ( + InitializeExceptionStackSwitchHandlers, + 0, + SwitchStackData + ); + ASSERT_EFI_ERROR (Status); =20 - if (Index =3D=3D Bsp) { - InitializeExceptionStackSwitchHandlers (&SwitchStackData); + BufferSize =3D 0; + for (Index =3D 0; Index < mNumberOfProcessors; ++Index) { + if (SwitchStackData[Index].Status =3D=3D EFI_BUFFER_TOO_SMALL) { + BufferSize +=3D SwitchStackData[Index].BufferSize; } else { - // - // AP might need different buffer size from BSP. - // - MpInitLibStartupThisAP (InitializeExceptionStackSwitchHandlers, Inde= x, NULL, 0, (VOID *)&SwitchStackData, NULL); + ASSERT (SwitchStackData[Index].Status =3D=3D EFI_SUCCESS); } + } =20 - if (BufferSize =3D=3D 0) { - continue; + if (BufferSize !=3D 0) { + Buffer =3D AllocateRuntimeZeroPool (BufferSize); + BufferSize =3D 0; + for (Index =3D 0; Index < mNumberOfProcessors; ++Index) { + if (SwitchStackData[Index].Status =3D=3D EFI_BUFFER_TOO_SMALL) { + SwitchStackData[Index].Buffer =3D (VOID *)(BufferSize + (UINTN)Buf= fer); + BufferSize +=3D SwitchStackData[Index].BufferSiz= e; + DEBUG (( + DEBUG_INFO, + "Buffer[cpu%lu] for InitializeExceptionStackSwitchHandlers: 0x%l= X with size 0x%lX\n", + (UINT64)(UINTN)Index, + (UINT64)(UINTN)SwitchStackData[Index].Buffer, + (UINT64)(UINTN)SwitchStackData[Index].BufferSize + )); + } } =20 - SwitchStackData.Buffer =3D AllocateRuntimeZeroPool (BufferSize); - ASSERT (SwitchStackData.Buffer !=3D NULL); - DEBUG (( - DEBUG_INFO, - "Buffer[cpu%lu] for InitializeExceptionStackSwitchHandlers: 0x%lX wi= th size 0x%x\n", - (UINT64)(UINTN)Index, - (UINT64)(UINTN)SwitchStackData.Buffer, - (UINT32)BufferSize - )); - - if (Index =3D=3D Bsp) { - InitializeExceptionStackSwitchHandlers (&SwitchStackData); - } else { - MpInitLibStartupThisAP ( - InitializeExceptionStackSwitchHandlers, - Index, - NULL, - 0, - (VOID *)&SwitchStackData, - NULL - ); - } + Status =3D MpInitLibStartupAllCPUs ( + InitializeExceptionStackSwitchHandlers, + 0, + SwitchStackData + ); + ASSERT_EFI_ERROR (Status); } + + FreePool (SwitchStackData); } =20 /** diff --git a/UefiCpuPkg/CpuMpPei/CpuMpPei.c b/UefiCpuPkg/CpuMpPei/CpuMpPei.c index c0be11d3ad..5e4a7a3dcf 100644 --- a/UefiCpuPkg/CpuMpPei/CpuMpPei.c +++ b/UefiCpuPkg/CpuMpPei/CpuMpPei.c @@ -415,8 +415,9 @@ PeiWhoAmI ( // Structure for InitializeSeparateExceptionStacks // typedef struct { - VOID *Buffer; - UINTN *BufferSize; + VOID *Buffer; + UINTN BufferSize; + EFI_STATUS Status; } EXCEPTION_STACK_SWITCH_CONTEXT; =20 /** @@ -435,9 +436,13 @@ InitializeExceptionStackSwitchHandlers ( ) { EXCEPTION_STACK_SWITCH_CONTEXT *SwitchStackData; + UINTN Index; =20 + MpInitLibWhoAmI (&Index); SwitchStackData =3D (EXCEPTION_STACK_SWITCH_CONTEXT *)Buffer; - InitializeSeparateExceptionStacks (SwitchStackData->Buffer, SwitchStackD= ata->BufferSize); + if ((SwitchStackData[Index].Status =3D=3D EFI_NOT_STARTED) || (SwitchSta= ckData[Index].Status =3D=3D EFI_BUFFER_TOO_SMALL)) { + SwitchStackData[Index].Status =3D InitializeSeparateExceptionStacks (S= witchStackData[Index].Buffer, &SwitchStackData[Index].BufferSize); + } } =20 /** @@ -453,60 +458,69 @@ InitializeMpExceptionStackSwitchHandlers ( ) { UINTN Index; - UINTN Bsp; - EXCEPTION_STACK_SWITCH_CONTEXT SwitchStackData; - UINTN BufferSize; UINTN NumberOfProcessors; + EXCEPTION_STACK_SWITCH_CONTEXT *SwitchStackData; + UINTN BufferSize; + EFI_STATUS Status; + VOID *Buffer; =20 if (!PcdGetBool (PcdCpuStackGuard)) { return; } =20 - SwitchStackData.BufferSize =3D &BufferSize; MpInitLibGetNumberOfProcessors (&NumberOfProcessors, NULL); - MpInitLibWhoAmI (&Bsp); + SwitchStackData =3D AllocateRuntimeZeroPool (NumberOfProcessors * sizeof= (EXCEPTION_STACK_SWITCH_CONTEXT)); =20 for (Index =3D 0; Index < NumberOfProcessors; ++Index) { - SwitchStackData.Buffer =3D NULL; - BufferSize =3D 0; + // + // Because the procedure may runs multiple times, use the status EFI_N= OT_STARTED + // to indicate the procedure haven't been run yet. + // + SwitchStackData[Index].Status =3D EFI_NOT_STARTED; + } =20 - if (Index =3D=3D Bsp) { - InitializeExceptionStackSwitchHandlers (&SwitchStackData); + Status =3D MpInitLibStartupAllCPUs ( + InitializeExceptionStackSwitchHandlers, + 0, + SwitchStackData + ); + ASSERT_EFI_ERROR (Status); + + BufferSize =3D 0; + for (Index =3D 0; Index < NumberOfProcessors; ++Index) { + if (SwitchStackData[Index].Status =3D=3D EFI_BUFFER_TOO_SMALL) { + BufferSize +=3D SwitchStackData[Index].BufferSize; } else { - // - // AP might need different buffer size from BSP. - // - MpInitLibStartupThisAP (InitializeExceptionStackSwitchHandlers, Inde= x, NULL, 0, (VOID *)&SwitchStackData, NULL); + ASSERT (SwitchStackData[Index].Status =3D=3D EFI_SUCCESS); } + } =20 - if (BufferSize =3D=3D 0) { - continue; + if (BufferSize !=3D 0) { + Buffer =3D AllocateRuntimeZeroPool (BufferSize); + BufferSize =3D 0; + for (Index =3D 0; Index < NumberOfProcessors; ++Index) { + if (SwitchStackData[Index].Status =3D=3D EFI_BUFFER_TOO_SMALL) { + SwitchStackData[Index].Buffer =3D (VOID *)(BufferSize + (UINTN)Buf= fer); + BufferSize +=3D SwitchStackData[Index].BufferSiz= e; + DEBUG (( + DEBUG_INFO, + "Buffer[cpu%lu] for InitializeExceptionStackSwitchHandlers: 0x%l= X with size 0x%lX\n", + (UINT64)(UINTN)Index, + (UINT64)(UINTN)SwitchStackData[Index].Buffer, + (UINT64)(UINTN)SwitchStackData[Index].BufferSize + )); + } } =20 - SwitchStackData.Buffer =3D AllocatePages (EFI_SIZE_TO_PAGES (BufferSiz= e)); - ASSERT (SwitchStackData.Buffer !=3D NULL); - ZeroMem (SwitchStackData.Buffer, EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES = (BufferSize))); - DEBUG (( - DEBUG_INFO, - "Buffer[cpu%lu] for InitializeExceptionStackSwitchHandlers: 0x%lX wi= th size 0x%x\n", - (UINT64)(UINTN)Index, - (UINT64)(UINTN)SwitchStackData.Buffer, - (UINT32)BufferSize - )); - - if (Index =3D=3D Bsp) { - InitializeExceptionStackSwitchHandlers (&SwitchStackData); - } else { - MpInitLibStartupThisAP ( - InitializeExceptionStackSwitchHandlers, - Index, - NULL, - 0, - (VOID *)&SwitchStackData, - NULL - ); - } + Status =3D MpInitLibStartupAllCPUs ( + InitializeExceptionStackSwitchHandlers, + 0, + SwitchStackData + ); + ASSERT_EFI_ERROR (Status); } + + FreePool (SwitchStackData); } =20 /** --=20 2.31.1.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 (#92780): https://edk2.groups.io/g/devel/message/92780 Mute This Topic: https://groups.io/mt/93241590/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-