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.web11.19719.1661396118095781814 for ; Wed, 24 Aug 2022 19:55:18 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=bm3f8yP3; spf=pass (domain: intel.com, ip: 134.134.136.31, mailfrom: zhiguang.liu@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661396118; x=1692932118; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=WaN5me2UzzTaJYn/IlCAozedj9nXK2aC8YlVqEyDANg=; b=bm3f8yP3wYFSVJdgT0/HpWqYTyPATegrsntphD0Y0K9qNKY0+aKZL5iK T4pZ1NkJntBM6jLLG8jysZfhq3RPUiM21ZHHEOml8Zzh1A5fAgCcyu/Zq 5Hb/Wla62apAn7+6QWkhikjHr4iLsR04sf8/9UPcMzF2DEthfEg5SFCOM LUOW0LGS6gDN3pfCFhuUs5EF0CuNRlp+Bq/jCGg0yG9RWUXA0FyqkthBD 4WRf4csMgsMeAXvxCc3qbpVnJh/1caUkQ+4W6hHyz3u7ZMJzU5my9tuQJ 11m2SuIxWLt1SnbQ487HiY52RQoUxKsS/82Pmq9IzBrCVGpaP4hLifkkn A==; 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" 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" 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: [PATCH] UefiCpuPkg: Enhance logic in InitializeMpExceptionStackSwitchHandlers Date: Thu, 25 Aug 2022 10:55:03 +0800 Message-Id: <20220825025506.2323-1-zhiguang.liu@intel.com> X-Mailer: git-send-email 2.31.1.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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; /** @@ -620,9 +621,13 @@ InitializeExceptionStackSwitchHandlers ( ) { EXCEPTION_STACK_SWITCH_CONTEXT *SwitchStackData; + UINTN Index; + MpInitLibWhoAmI (&Index); SwitchStackData = (EXCEPTION_STACK_SWITCH_CONTEXT *)Buffer; - InitializeSeparateExceptionStacks (SwitchStackData->Buffer, SwitchStackData->BufferSize); + if ((SwitchStackData[Index].Status == EFI_NOT_STARTED) || (SwitchStackData[Index].Status == EFI_BUFFER_TOO_SMALL)) { + SwitchStackData[Index].Status = InitializeSeparateExceptionStacks (SwitchStackData[Index].Buffer, &SwitchStackData[Index].BufferSize); + } } /** @@ -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; - SwitchStackData.BufferSize = &BufferSize; - MpInitLibWhoAmI (&Bsp); + SwitchStackData = AllocateRuntimeZeroPool (mNumberOfProcessors * sizeof (EXCEPTION_STACK_SWITCH_CONTEXT)); for (Index = 0; Index < mNumberOfProcessors; ++Index) { - SwitchStackData.Buffer = NULL; - BufferSize = 0; + // + // Because the procedure may runs multiple times, use the status EFI_NOT_STARTED + // to indicate the procedure haven't been run yet. + // + SwitchStackData[Index].Status = EFI_NOT_STARTED; + } + + Status = MpInitLibStartupAllCPUs ( + InitializeExceptionStackSwitchHandlers, + 0, + SwitchStackData + ); + ASSERT_EFI_ERROR (Status); - if (Index == Bsp) { - InitializeExceptionStackSwitchHandlers (&SwitchStackData); + BufferSize = 0; + for (Index = 0; Index < mNumberOfProcessors; ++Index) { + if (SwitchStackData[Index].Status == EFI_BUFFER_TOO_SMALL) { + BufferSize += SwitchStackData[Index].BufferSize; } else { - // - // AP might need different buffer size from BSP. - // - MpInitLibStartupThisAP (InitializeExceptionStackSwitchHandlers, Index, NULL, 0, (VOID *)&SwitchStackData, NULL); + ASSERT (SwitchStackData[Index].Status == EFI_SUCCESS); } + } - if (BufferSize == 0) { - continue; + if (BufferSize != 0) { + Buffer = AllocateRuntimeZeroPool (BufferSize); + BufferSize = 0; + for (Index = 0; Index < mNumberOfProcessors; ++Index) { + if (SwitchStackData[Index].Status == EFI_BUFFER_TOO_SMALL) { + SwitchStackData[Index].Buffer = (VOID *)(BufferSize + (UINTN)Buffer); + BufferSize += SwitchStackData[Index].BufferSize; + DEBUG (( + DEBUG_INFO, + "Buffer[cpu%lu] for InitializeExceptionStackSwitchHandlers: 0x%lX with size 0x%lX\n", + (UINT64)(UINTN)Index, + (UINT64)(UINTN)SwitchStackData[Index].Buffer, + (UINT64)(UINTN)SwitchStackData[Index].BufferSize + )); + } } - SwitchStackData.Buffer = AllocateRuntimeZeroPool (BufferSize); - ASSERT (SwitchStackData.Buffer != NULL); - DEBUG (( - DEBUG_INFO, - "Buffer[cpu%lu] for InitializeExceptionStackSwitchHandlers: 0x%lX with size 0x%x\n", - (UINT64)(UINTN)Index, - (UINT64)(UINTN)SwitchStackData.Buffer, - (UINT32)BufferSize - )); - - if (Index == Bsp) { - InitializeExceptionStackSwitchHandlers (&SwitchStackData); - } else { - MpInitLibStartupThisAP ( - InitializeExceptionStackSwitchHandlers, - Index, - NULL, - 0, - (VOID *)&SwitchStackData, - NULL - ); - } + Status = MpInitLibStartupAllCPUs ( + InitializeExceptionStackSwitchHandlers, + 0, + SwitchStackData + ); + ASSERT_EFI_ERROR (Status); } + + FreePool (SwitchStackData); } /** 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; /** @@ -435,9 +436,13 @@ InitializeExceptionStackSwitchHandlers ( ) { EXCEPTION_STACK_SWITCH_CONTEXT *SwitchStackData; + UINTN Index; + MpInitLibWhoAmI (&Index); SwitchStackData = (EXCEPTION_STACK_SWITCH_CONTEXT *)Buffer; - InitializeSeparateExceptionStacks (SwitchStackData->Buffer, SwitchStackData->BufferSize); + if ((SwitchStackData[Index].Status == EFI_NOT_STARTED) || (SwitchStackData[Index].Status == EFI_BUFFER_TOO_SMALL)) { + SwitchStackData[Index].Status = InitializeSeparateExceptionStacks (SwitchStackData[Index].Buffer, &SwitchStackData[Index].BufferSize); + } } /** @@ -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; if (!PcdGetBool (PcdCpuStackGuard)) { return; } - SwitchStackData.BufferSize = &BufferSize; MpInitLibGetNumberOfProcessors (&NumberOfProcessors, NULL); - MpInitLibWhoAmI (&Bsp); + SwitchStackData = AllocateRuntimeZeroPool (NumberOfProcessors * sizeof (EXCEPTION_STACK_SWITCH_CONTEXT)); for (Index = 0; Index < NumberOfProcessors; ++Index) { - SwitchStackData.Buffer = NULL; - BufferSize = 0; + // + // Because the procedure may runs multiple times, use the status EFI_NOT_STARTED + // to indicate the procedure haven't been run yet. + // + SwitchStackData[Index].Status = EFI_NOT_STARTED; + } - if (Index == Bsp) { - InitializeExceptionStackSwitchHandlers (&SwitchStackData); + Status = MpInitLibStartupAllCPUs ( + InitializeExceptionStackSwitchHandlers, + 0, + SwitchStackData + ); + ASSERT_EFI_ERROR (Status); + + BufferSize = 0; + for (Index = 0; Index < NumberOfProcessors; ++Index) { + if (SwitchStackData[Index].Status == EFI_BUFFER_TOO_SMALL) { + BufferSize += SwitchStackData[Index].BufferSize; } else { - // - // AP might need different buffer size from BSP. - // - MpInitLibStartupThisAP (InitializeExceptionStackSwitchHandlers, Index, NULL, 0, (VOID *)&SwitchStackData, NULL); + ASSERT (SwitchStackData[Index].Status == EFI_SUCCESS); } + } - if (BufferSize == 0) { - continue; + if (BufferSize != 0) { + Buffer = AllocateRuntimeZeroPool (BufferSize); + BufferSize = 0; + for (Index = 0; Index < NumberOfProcessors; ++Index) { + if (SwitchStackData[Index].Status == EFI_BUFFER_TOO_SMALL) { + SwitchStackData[Index].Buffer = (VOID *)(BufferSize + (UINTN)Buffer); + BufferSize += SwitchStackData[Index].BufferSize; + DEBUG (( + DEBUG_INFO, + "Buffer[cpu%lu] for InitializeExceptionStackSwitchHandlers: 0x%lX with size 0x%lX\n", + (UINT64)(UINTN)Index, + (UINT64)(UINTN)SwitchStackData[Index].Buffer, + (UINT64)(UINTN)SwitchStackData[Index].BufferSize + )); + } } - SwitchStackData.Buffer = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize)); - ASSERT (SwitchStackData.Buffer != NULL); - ZeroMem (SwitchStackData.Buffer, EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (BufferSize))); - DEBUG (( - DEBUG_INFO, - "Buffer[cpu%lu] for InitializeExceptionStackSwitchHandlers: 0x%lX with size 0x%x\n", - (UINT64)(UINTN)Index, - (UINT64)(UINTN)SwitchStackData.Buffer, - (UINT32)BufferSize - )); - - if (Index == Bsp) { - InitializeExceptionStackSwitchHandlers (&SwitchStackData); - } else { - MpInitLibStartupThisAP ( - InitializeExceptionStackSwitchHandlers, - Index, - NULL, - 0, - (VOID *)&SwitchStackData, - NULL - ); - } + Status = MpInitLibStartupAllCPUs ( + InitializeExceptionStackSwitchHandlers, + 0, + SwitchStackData + ); + ASSERT_EFI_ERROR (Status); } + + FreePool (SwitchStackData); } /** -- 2.31.1.windows.1