From nobody Sun May 19 18:09:55 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+92856+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+92856+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1661499556; cv=none; d=zohomail.com; s=zohoarc; b=N6AAHCiaEbsvN/fU2GAX2i5poZHHOnguL86QLvQoMOMzdu3f1rREun4hCyLazb2q8ffXm+d8uY1O6S2w4v3wtFFtJGUCZFEmLr3jH+JMtKHuxcAIoQskuJt+eWMx8IUq1s5tR6F/sovEehV6TMdymAUL+QgzjRtaOlpsj2dLhUs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661499556; 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=D0xkM1gA9Je3hAcrRCtJrv2t2/bc6dIsHnMP02iDy0M=; b=fxkTfS5APTRBcL3WRgLtHCsAcxdQgwjrals+toq0KbScLRGG79fkB32MfSEnhIIcCTF/3Mf62A36cuv3ndAEBht3d5avtyes5hhy7b9R6uab6KAQ5ZIfemB8FwKqwWyB+sjeJt66vsh9sOaz5uNjuvaTqqbj/JGr/cvvkSa3f84= 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+92856+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 1661499556390324.5721610058523; Fri, 26 Aug 2022 00:39:16 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id vKxhYY1788612xbXBBnuxgho; Fri, 26 Aug 2022 00:39:16 -0700 X-Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by mx.groups.io with SMTP id smtpd.web12.34229.1661499555356761933 for ; Fri, 26 Aug 2022 00:39:15 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10450"; a="281418036" X-IronPort-AV: E=Sophos;i="5.93,264,1654585200"; d="scan'208";a="281418036" X-Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2022 00:39:14 -0700 X-IronPort-AV: E=Sophos;i="5.93,264,1654585200"; d="scan'208";a="671361284" 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; 26 Aug 2022 00:39:08 -0700 From: "Zhiguang Liu" To: devel@edk2.groups.io Cc: Zhiguang Liu , Eric Dong , Ray Ni , Rahul Kumar Subject: [edk2-devel] [PATCH v4] UefiCpuPkg: Enhance logic in InitializeMpExceptionStackSwitchHandlers Date: Fri, 26 Aug 2022 15:39:01 +0800 Message-Id: <20220826073901.2634-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: uEYguof50IQrrqbmR5DCAlSkx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1661499556; bh=6sRKJ5zUY1ru4vww7cEcljPHYAPt0TEginNk6ZfTXgI=; h=Cc:Date:From:Reply-To:Subject:To; b=oUxumNS9fvoVx56IUVThWrGW8hcNMl1JQOnafE7mNkdcZf2Xfn+o3M8wo+UZ5wYiuz3 b8BRNqHXWrg8QorUx2ZsryT0ZC70ZZ3CONEr2TFfXbkg/tph24UnyOd2N++CEyQfJOsvO xx25e9W4P1W9jlHregz7X1QjMGeunUviXjI= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1661499557799100003 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 Reviewed-by: Ray Ni --- UefiCpuPkg/CpuDxe/CpuMp.c | 104 +++++++++++++++++++------------ UefiCpuPkg/CpuMpPei/CpuMpPei.c | 108 ++++++++++++++++++++------------- 2 files changed, 132 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..e7f1fe9f42 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,76 @@ 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 AllocatePages (EFI_SIZE_TO_PAGES (NumberOfProcessors= * sizeof (EXCEPTION_STACK_SWITCH_CONTEXT))); + ASSERT (SwitchStackData !=3D NULL); + ZeroMem (SwitchStackData, NumberOfProcessors * sizeof (EXCEPTION_STACK_S= WITCH_CONTEXT)); 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); } } + + FreePages (SwitchStackData, EFI_SIZE_TO_PAGES (NumberOfProcessors * size= of (EXCEPTION_STACK_SWITCH_CONTEXT))); } =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 (#92856): https://edk2.groups.io/g/devel/message/92856 Mute This Topic: https://groups.io/mt/93265684/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-