From nobody Thu Oct 31 23:53:24 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+107652+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+107652+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1691571880; cv=none; d=zohomail.com; s=zohoarc; b=AZkAtspmvGK2h3xu6ADL5Tg4EkP8ffLNR7FkIeWp+uJ95B4PNAER03YFlb1v9DMg3sAcgklcK3n9XjRhaSRWvdyCcx4vGj5qbSzZMgAgbZ5g1KX2XpQw9H2ELVgGxflaWRv7BhGw5GK7OPknyw9+Eh5CckAWjFJHtyt5YvvabS8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1691571880; h=Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:Message-ID:Reply-To:Sender:Subject:To; bh=NZdjVigU0MDfz/OaUUbKIk12ZxM0BeflzAVXxQeNngM=; b=BiFvml3sIS5PUeSIYFb/7/w3ALIK4elqJNjaTVeBPi+xFkYyqKzwQAK7wPXFnsHkE9PWbZWUYXRH/JjSyxqQkXakb5+NeCeBVLfnl+Qi180bq3Czi/Rqi7+Uez09zHW4HuQyUjmDJBKLtz/svpC1yAI4Twj+A4olT6KvgJeCa3Y= 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+107652+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 1691571880506401.4574860137649; Wed, 9 Aug 2023 02:04:40 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=9FIGmnmZQ6Ipaf/5bcjWz8nxVg+12HH7Rt/t7HcFn9k=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe; s=20140610; t=1691571880; v=1; b=xRuc+NIbfbL558BDo/OnziZhgGAbd8MOHntU4d3lqp/e5geNyrpKzTtwj2cYopvQpHG7evwB ECMXhdgb4ldbsbpfp0gppLELdhxl82/ztq4ncRmNF4YKVmQOKRugl5HLN18LQwKnzZN9k8XLrn6 aHTqNfWV3lJcenFubSNCVrZg= X-Received: by 127.0.0.2 with SMTP id ZTikYY1788612xHcW2f3W9Lu; Wed, 09 Aug 2023 02:04:40 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.115]) by mx.groups.io with SMTP id smtpd.web10.84108.1691571879028426517 for ; Wed, 09 Aug 2023 02:04:39 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10795"; a="371063074" X-IronPort-AV: E=Sophos;i="6.01,158,1684825200"; d="scan'208";a="371063074" X-Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Aug 2023 02:04:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10795"; a="734922834" X-IronPort-AV: E=Sophos;i="6.01,158,1684825200"; d="scan'208";a="734922834" X-Received: from sh1gapp1009.ccr.corp.intel.com ([10.239.189.219]) by fmsmga007.fm.intel.com with ESMTP; 09 Aug 2023 02:04:13 -0700 From: "Wu, Jiaxin" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Zeng Star , Rahul Kumar , Gerd Hoffmann Subject: [edk2-devel] [PATCH v1] UefiCpuPkg/SmmCpu: Refine semaphore sync between BSP and AP Date: Wed, 9 Aug 2023 17:04:10 +0800 Message-Id: <20230809090410.12428-1-jiaxin.wu@intel.com> Precedence: Bulk 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,jiaxin.wu@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: UVMyjXnPhPZYwj5vNhjezTsqx1787277AA= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1691571881173100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" For SMM CPU semaphore sync, there is no need atomic semaphore operation, just use the flag to indicate it has complete the wait/release. Based on this, this patch is to refine 2 functions (WaitForAllAPs & ReleaseAllAPs) and define 2 new functions (WaitForBsp & ReleaseBsp) used for the semaphore sync between BSP & AP. Sync flow like below: 1. BSP to Release All APs ---> 1. AP to Wait BSP ReleaseAllAPs () WaitForBsp 2. BSP to Wait All APs <--- 2. AP to Release BSP WaitForAllAPs () ReleaseBsp With this change, SMM CPU semaphore sync for SMI exit performance will be significant improved. Cc: Eric Dong Cc: Ray Ni Cc: Zeng Star Cc: Rahul Kumar Cc: Gerd Hoffmann Signed-off-by: Jiaxin Wu --- UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c | 68 ++++++++++++++++++++++++++-----= ---- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c b/UefiCpuPkg/PiSmmCpuDxe= Smm/MpService.c index 25d058c5b9..0bf460e81c 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c @@ -120,11 +120,11 @@ LockdownSemaphore ( =20 return Value; } =20 /** - Wait all APs to performs an atomic compare exchange operation to release= semaphore. + Used for BSP to wait all APs. =20 @param NumberOfAPs AP number =20 **/ VOID @@ -133,18 +133,19 @@ WaitForAllAPs ( ) { UINTN BspIndex; =20 BspIndex =3D mSmmMpSyncData->BspIndex; - while (NumberOfAPs-- > 0) { - WaitForSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run); + while (NumberOfAPs !=3D *mSmmMpSyncData->CpuData[BspIndex].Run) { + CpuPause (); } + + *mSmmMpSyncData->CpuData[BspIndex].Run =3D 0; } =20 /** - Performs an atomic compare exchange operation to release semaphore - for each AP. + Used for BSP to release all APs. =20 **/ VOID ReleaseAllAPs ( VOID @@ -152,15 +153,48 @@ ReleaseAllAPs ( { UINTN Index; =20 for (Index =3D 0; Index < mMaxNumberOfCpus; Index++) { if (IsPresentAp (Index)) { - ReleaseSemaphore (mSmmMpSyncData->CpuData[Index].Run); + ASSERT (*mSmmMpSyncData->CpuData[Index].Run =3D=3D 0); + *mSmmMpSyncData->CpuData[Index].Run =3D 1; } } } =20 +/** + Used for Ap to wait BSP. + + @param ApSem IN: 32-bit unsigned integer + OUT: original integer 0 +**/ +VOID +WaitForBsp ( + IN OUT volatile UINT32 *ApSem + ) +{ + while (*ApSem =3D=3D 0) { + CpuPause (); + } + + *ApSem =3D 0; +} + +/** + Used for Ap to release BSP. + + @param BspSem IN: 32-bit unsigned integer + OUT: original integer + 1 +**/ +VOID +ReleaseBsp ( + IN OUT volatile UINT32 *BspSem + ) +{ + InterlockedIncrement (BspSem); +} + /** Check whether the index of CPU perform the package level register programming during System Management Mode initialization. =20 The index of Processor specified by mPackageFirstThreadIndex[PackageInde= x] @@ -898,50 +932,50 @@ APHandler ( =20 if ((SyncMode =3D=3D SmmCpuSyncModeTradition) || SmmCpuFeaturesNeedConfi= gureMtrrs ()) { // // Notify BSP of arrival at this point // - ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run); + ReleaseBsp (mSmmMpSyncData->CpuData[BspIndex].Run); } =20 if (SmmCpuFeaturesNeedConfigureMtrrs ()) { // // Wait for the signal from BSP to backup MTRRs // - WaitForSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run); + WaitForBsp (mSmmMpSyncData->CpuData[CpuIndex].Run); =20 // // Backup OS MTRRs // MtrrGetAllMtrrs (&Mtrrs); =20 // // Signal BSP the completion of this AP // - ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run); + ReleaseBsp (mSmmMpSyncData->CpuData[BspIndex].Run); =20 // // Wait for BSP's signal to program MTRRs // - WaitForSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run); + WaitForBsp (mSmmMpSyncData->CpuData[CpuIndex].Run); =20 // // Replace OS MTRRs with SMI MTRRs // ReplaceOSMtrrs (CpuIndex); =20 // // Signal BSP the completion of this AP // - ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run); + ReleaseBsp (mSmmMpSyncData->CpuData[BspIndex].Run); } =20 while (TRUE) { // // Wait for something to happen // - WaitForSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run); + WaitForBsp (mSmmMpSyncData->CpuData[CpuIndex].Run); =20 // // Check if BSP wants to exit SMM // if (!(*mSmmMpSyncData->InsideSmm)) { @@ -977,16 +1011,16 @@ APHandler ( =20 if (SmmCpuFeaturesNeedConfigureMtrrs ()) { // // Notify BSP the readiness of this AP to program MTRRs // - ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run); + ReleaseBsp (mSmmMpSyncData->CpuData[BspIndex].Run); =20 // // Wait for the signal from BSP to program MTRRs // - WaitForSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run); + WaitForBsp (mSmmMpSyncData->CpuData[CpuIndex].Run); =20 // // Restore OS MTRRs // SmmCpuFeaturesReenableSmrr (); @@ -994,26 +1028,26 @@ APHandler ( } =20 // // Notify BSP the readiness of this AP to Reset states/semaphore for thi= s processor // - ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run); + ReleaseBsp (mSmmMpSyncData->CpuData[BspIndex].Run); =20 // // Wait for the signal from BSP to Reset states/semaphore for this proce= ssor // - WaitForSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run); + WaitForBsp (mSmmMpSyncData->CpuData[CpuIndex].Run); =20 // // Reset states/semaphore for this processor // *(mSmmMpSyncData->CpuData[CpuIndex].Present) =3D FALSE; =20 // // Notify BSP the readiness of this AP to exit SMM // - ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run); + ReleaseBsp (mSmmMpSyncData->CpuData[BspIndex].Run); } =20 /** Checks whether the input token is the current used token. =20 --=20 2.16.2.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 (#107652): https://edk2.groups.io/g/devel/message/107652 Mute This Topic: https://groups.io/mt/100639360/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-