From nobody Sun May 19 16:27:51 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+92850+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+92850+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1661497504; cv=none; d=zohomail.com; s=zohoarc; b=WXY2FMqjj1dPZ2yai9sza70jAgFuzHLpVPtg5TMo2Mb9mNNbpfRZ+61DgdPDAGMI3qqjpwSFw66u9tkym58tIcTFYi0D+oc6E1pRWZ+eSFutA02ef6yMCAtKS1hJRXzU5NNt8r/WKBUEkwCp5qulQYMi4wsSSAZzv30uGGTteH0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661497504; 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=F/Nw1pV5Ifucf3oyuZlCtyG8ZqoqWdgx8xL4CqBEEz4=; b=Bph8uodfSB34Yp5mbUahvtDTiLYuE1TwSB7RnYq3RZQYwBIrIdVax4Ee6bZv6zvA9osJ9FOr+QzI8pjNrzKBCKJtLHpQrzbvVBRCW2mfxIPkOW7lPcWY5z0B787BWgZZiBgycVPdlygU9gWYSTdweQHCVpXVUkKFyEgUt67veCo= 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+92850+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 16614975049919.739176421037314; Fri, 26 Aug 2022 00:05:04 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id BTZSYY1788612xTehozdwYnn; Fri, 26 Aug 2022 00:05:04 -0700 X-Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by mx.groups.io with SMTP id smtpd.web08.34472.1661497504014522082 for ; Fri, 26 Aug 2022 00:05:04 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10450"; a="358408837" X-IronPort-AV: E=Sophos;i="5.93,264,1654585200"; d="scan'208";a="358408837" X-Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2022 00:04:58 -0700 X-IronPort-AV: E=Sophos;i="5.93,264,1654585200"; d="scan'208";a="938651851" X-Received: from shwdesfp01.ccr.corp.intel.com ([10.239.158.151]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2022 00:04:56 -0700 From: "Zhiguang Liu" To: devel@edk2.groups.io Cc: Zhiguang Liu , Eric Dong , Ray Ni , Rahul Kumar Subject: [edk2-devel] [PATCH v2] UefiCpuPkg: Enhance logic in InitializeMpExceptionStackSwitchHandlers Date: Fri, 26 Aug 2022 15:04:45 +0800 Message-Id: <20220826070447.2508-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: JbFfCCSU2WeVeWCp9kWzeuaMx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1661497504; bh=CVP1qWAmwx7d9zRUAqu0sSGrtaPVu2rfncTRDW8bpPU=; h=Cc:Date:From:Reply-To:Subject:To; b=KmuZEqr1LMse2CHGBomrvedvZufa0JMIy53OLghXH+VB8CDxFOGkPj14utErRPIym+P wtqhGZPrh5kQQNw1aM040VRAjI8rEI77sxgBrmwXyppcxiIxTUobV/nsD1u//0+ICKVWk Ssks7w9gfrL2RTLDJIFkitC6qt9f5Lyri5Y= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1661497505212100008 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 | 104 ++++++++++++++++++++------------ UefiCpuPkg/CpuMpPei/CpuMpPei.c | 107 ++++++++++++++++++++------------- 2 files changed, 131 insertions(+), 80 deletions(-) diff --git a/UefiCpuPkg/CpuDxe/CpuMp.c b/UefiCpuPkg/CpuDxe/CpuMp.c index f3ca813d2a..e7575d9b80 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,18 @@ InitializeExceptionStackSwitchHandlers ( ) { EXCEPTION_STACK_SWITCH_CONTEXT *SwitchStackData; + UINTN Index; =20 + MpInitLibWhoAmI (&Index); SwitchStackData =3D (EXCEPTION_STACK_SWITCH_CONTEXT *)Buffer; - InitializeSeparateExceptionStacks (SwitchStackData->Buffer, SwitchStackD= ata->BufferSize); + + // + // This may be called twice for each Cpu. Only run InitializeSeparateExc= eptionStacks + // if this is the first call or the first call failed because of size to= o small. + // + 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 +648,69 @@ InitializeMpExceptionStackSwitchHandlers ( ) { UINTN Index; - UINTN Bsp; - EXCEPTION_STACK_SWITCH_CONTEXT SwitchStackData; + EXCEPTION_STACK_SWITCH_CONTEXT *SwitchStackData; UINTN BufferSize; + EFI_STATUS Status; + UINT8 *Buffer; =20 - SwitchStackData.BufferSize =3D &BufferSize; - MpInitLibWhoAmI (&Bsp); - + SwitchStackData =3D AllocateZeroPool (mNumberOfProcessors * sizeof (EXCE= PTION_STACK_SWITCH_CONTEXT)); + ASSERT (SwitchStackData !=3D NULL); 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; + } =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 < mNumberOfProcessors; ++Index) { + if (SwitchStackData[Index].Status =3D=3D EFI_BUFFER_TOO_SMALL) { + ASSERT (SwitchStackData[Index].BufferSize !=3D 0); + 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); + ASSERT (SwitchStackData[Index].BufferSize =3D=3D 0); } + } =20 - if (BufferSize =3D=3D 0) { - continue; + if (BufferSize !=3D 0) { + Buffer =3D AllocateRuntimeZeroPool (BufferSize); + ASSERT (Buffer !=3D NULL); + 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 *)(&Buffer[BufferSize]); + 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); + for (Index =3D 0; Index < mNumberOfProcessors; ++Index) { + ASSERT (SwitchStackData[Index].Status =3D=3D EFI_SUCCESS); } } + + FreePool (SwitchStackData); } =20 /** diff --git a/UefiCpuPkg/CpuMpPei/CpuMpPei.c b/UefiCpuPkg/CpuMpPei/CpuMpPei.c index c0be11d3ad..f2eb582e9b 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,18 @@ InitializeExceptionStackSwitchHandlers ( ) { EXCEPTION_STACK_SWITCH_CONTEXT *SwitchStackData; + UINTN Index; =20 + MpInitLibWhoAmI (&Index); SwitchStackData =3D (EXCEPTION_STACK_SWITCH_CONTEXT *)Buffer; - InitializeSeparateExceptionStacks (SwitchStackData->Buffer, SwitchStackD= ata->BufferSize); + + // + // This function may be called twice for each Cpu. Only run InitializeSe= parateExceptionStacks + // if this is the first call or the first call failed because of size to= o small. + // + 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 +463,75 @@ InitializeMpExceptionStackSwitchHandlers ( ) { UINTN Index; - UINTN Bsp; - EXCEPTION_STACK_SWITCH_CONTEXT SwitchStackData; - UINTN BufferSize; UINTN NumberOfProcessors; + EXCEPTION_STACK_SWITCH_CONTEXT *SwitchStackData; + UINTN BufferSize; + EFI_STATUS Status; + UINT8 *Buffer; =20 if (!PcdGetBool (PcdCpuStackGuard)) { return; } =20 - SwitchStackData.BufferSize =3D &BufferSize; MpInitLibGetNumberOfProcessors (&NumberOfProcessors, NULL); - MpInitLibWhoAmI (&Bsp); - + SwitchStackData =3D AllocateZeroPool (NumberOfProcessors * sizeof (EXCEP= TION_STACK_SWITCH_CONTEXT)); + ASSERT (SwitchStackData !=3D NULL); 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; + } + + 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 < NumberOfProcessors; ++Index) { + if (SwitchStackData[Index].Status =3D=3D EFI_BUFFER_TOO_SMALL) { + ASSERT (SwitchStackData[Index].BufferSize !=3D 0); + 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); + ASSERT (SwitchStackData[Index].BufferSize =3D=3D 0); } + } =20 - if (BufferSize =3D=3D 0) { - continue; + if (BufferSize !=3D 0) { + Buffer =3D AllocatePages (EFI_SIZE_TO_PAGES (BufferSize)); + ASSERT (Buffer !=3D NULL); + 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 *)(&Buffer[BufferSize]); + 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); + for (Index =3D 0; Index < NumberOfProcessors; ++Index) { + ASSERT (SwitchStackData[Index].Status =3D=3D EFI_SUCCESS); } } + + 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 (#92850): https://edk2.groups.io/g/devel/message/92850 Mute This Topic: https://groups.io/mt/93265210/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-