From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga03.intel.com (mga03.intel.com []) by mx.groups.io with SMTP id smtpd.web09.387.1577083689070836499 for ; Sun, 22 Dec 2019 22:48:09 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=fail (domain: intel.com, ip: , mailfrom: eric.dong@intel.com) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 22 Dec 2019 22:48:09 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,346,1571727600"; d="scan'208";a="417163285" Received: from ydong10-desktop.ccr.corp.intel.com ([10.239.158.133]) by fmsmga005.fm.intel.com with ESMTP; 22 Dec 2019 22:48:08 -0800 From: "Dong, Eric" To: devel@edk2.groups.io Cc: Ray Ni , Laszlo Ersek Subject: [PATCH v2 1/2] UefiCpuPkg/PiSmmCpuDxeSmm: Remove dependence between APs Date: Mon, 23 Dec 2019 14:48:05 +0800 Message-Id: <20191223064806.682-2-eric.dong@intel.com> X-Mailer: git-send-email 2.23.0.windows.1 In-Reply-To: <20191223064806.682-1-eric.dong@intel.com> References: <20191223064806.682-1-eric.dong@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2268 In current implementation, when check whether APs called by StartUpAllAPs or StartUpThisAp, it checks the Tokens value used by other APs. Also the AP will update the Token value for itself if its task finished. In this case, the potential race condition issues happens for the tokens. Because of this, system may trig ASSERT during cycling test. This change enhance the code logic, add new attributes for the token to remove the reference for the tokens belongs to other APs. Cc: Ray Ni Cc: Laszlo Ersek Signed-off-by: Eric Dong --- UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c | 125 +++++++-------------- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 5 +- 2 files changed, 46 insertions(+), 84 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c b/UefiCpuPkg/PiSmmCpuDxe= Smm/MpService.c index 757f1056f7..5ad12db980 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c @@ -402,38 +402,6 @@ IsPresentAp ( *(mSmmMpSyncData->CpuData[CpuIndex].Present));=0D }=0D =0D -/**=0D - Check whether execute in single AP or all APs.=0D -=0D - Compare two Tokens used by different APs to know whether in StartAllAps = call.=0D -=0D - Whether is an valid AP base on AP's Present flag.=0D -=0D - @retval TRUE IN StartAllAps call.=0D - @retval FALSE Not in StartAllAps call.=0D -=0D -**/=0D -BOOLEAN=0D -InStartAllApsCall (=0D - VOID=0D - )=0D -{=0D - UINTN ApIndex;=0D - UINTN ApIndex2;=0D -=0D - for (ApIndex =3D mMaxNumberOfCpus; ApIndex-- > 0;) {=0D - if (IsPresentAp (ApIndex) && (mSmmMpSyncData->CpuData[ApIndex].Token != =3D NULL)) {=0D - for (ApIndex2 =3D ApIndex; ApIndex2-- > 0;) {=0D - if (IsPresentAp (ApIndex2) && (mSmmMpSyncData->CpuData[ApIndex2].T= oken !=3D NULL)) {=0D - return mSmmMpSyncData->CpuData[ApIndex2].Token =3D=3D mSmmMpSync= Data->CpuData[ApIndex].Token;=0D - }=0D - }=0D - }=0D - }=0D -=0D - return FALSE;=0D -}=0D -=0D /**=0D Clean up the status flags used during executing the procedure.=0D =0D @@ -445,40 +413,17 @@ ReleaseToken ( IN UINTN CpuIndex=0D )=0D {=0D - UINTN Index;=0D - BOOLEAN Released;=0D + PROCEDURE_TOKEN *Token;=0D =0D - if (InStartAllApsCall ()) {=0D - //=0D - // In Start All APs mode, make sure all APs have finished task.=0D - //=0D - if (WaitForAllAPsNotBusy (FALSE)) {=0D - //=0D - // Clean the flags update in the function call.=0D - //=0D - Released =3D FALSE;=0D - for (Index =3D mMaxNumberOfCpus; Index-- > 0;) {=0D - //=0D - // Only In SMM APs need to be clean up.=0D - //=0D - if (mSmmMpSyncData->CpuData[Index].Present && mSmmMpSyncData->CpuD= ata[Index].Token !=3D NULL) {=0D - if (!Released) {=0D - ReleaseSpinLock (mSmmMpSyncData->CpuData[Index].Token);=0D - Released =3D TRUE;=0D - }=0D - mSmmMpSyncData->CpuData[Index].Token =3D NULL;=0D - }=0D - }=0D - }=0D - } else {=0D - //=0D - // In single AP mode.=0D - //=0D - if (mSmmMpSyncData->CpuData[CpuIndex].Token !=3D NULL) {=0D - ReleaseSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Token);=0D - mSmmMpSyncData->CpuData[CpuIndex].Token =3D NULL;=0D - }=0D + Token =3D mSmmMpSyncData->CpuData[CpuIndex].Token;=0D +=0D + WaitForSemaphore (&Token->RunningApCount);=0D +=0D + if (Token->RunningApCount =3D=3D 0) {=0D + ReleaseSpinLock (Token->SpinLock);=0D }=0D +=0D + mSmmMpSyncData->CpuData[CpuIndex].Token =3D NULL;=0D }=0D =0D /**=0D @@ -912,12 +857,14 @@ APHandler ( *mSmmMpSyncData->CpuData[CpuIndex].Status =3D ProcedureStatus;=0D }=0D =0D + if (mSmmMpSyncData->CpuData[CpuIndex].Token !=3D NULL) {=0D + ReleaseToken (CpuIndex);=0D + }=0D +=0D //=0D // Release BUSY=0D //=0D ReleaseSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy);=0D -=0D - ReleaseToken (CpuIndex);=0D }=0D =0D if (SmmCpuFeaturesNeedConfigureMtrrs()) {=0D @@ -1111,7 +1058,7 @@ IsTokenInUse ( while (!IsNull (&gSmmCpuPrivate->TokenList, Link)) {=0D ProcToken =3D PROCEDURE_TOKEN_FROM_LINK (Link);=0D =0D - if (ProcToken->ProcedureToken =3D=3D Token) {=0D + if (ProcToken->SpinLock =3D=3D Token) {=0D return TRUE;=0D }=0D =0D @@ -1124,16 +1071,18 @@ IsTokenInUse ( /**=0D create token and save it to the maintain list.=0D =0D + @param RunningApCount Input running AP count.=0D +=0D @retval return the spin lock used as token.=0D =0D **/=0D -SPIN_LOCK *=0D +PROCEDURE_TOKEN *=0D CreateToken (=0D - VOID=0D + IN UINT32 RunningApCount=0D )=0D {=0D PROCEDURE_TOKEN *ProcToken;=0D - SPIN_LOCK *CpuToken;=0D + SPIN_LOCK *SpinLock;=0D UINTN SpinLockSize;=0D TOKEN_BUFFER *TokenBuf;=0D UINT32 TokenCountPerChunk;=0D @@ -1160,20 +1109,21 @@ CreateToken ( gSmmCpuPrivate->UsedTokenNum =3D 0;=0D }=0D =0D - CpuToken =3D (SPIN_LOCK *)(gSmmCpuPrivate->CurrentTokenBuf + SpinLockSiz= e * gSmmCpuPrivate->UsedTokenNum);=0D + SpinLock =3D (SPIN_LOCK *)(gSmmCpuPrivate->CurrentTokenBuf + SpinLockSiz= e * gSmmCpuPrivate->UsedTokenNum);=0D gSmmCpuPrivate->UsedTokenNum++;=0D =0D - InitializeSpinLock (CpuToken);=0D - AcquireSpinLock (CpuToken);=0D + InitializeSpinLock (SpinLock);=0D + AcquireSpinLock (SpinLock);=0D =0D ProcToken =3D AllocatePool (sizeof (PROCEDURE_TOKEN));=0D ASSERT (ProcToken !=3D NULL);=0D ProcToken->Signature =3D PROCEDURE_TOKEN_SIGNATURE;=0D - ProcToken->ProcedureToken =3D CpuToken;=0D + ProcToken->SpinLock =3D SpinLock;=0D + ProcToken->RunningApCount =3D RunningApCount;=0D =0D InsertTailList (&gSmmCpuPrivate->TokenList, &ProcToken->Link);=0D =0D - return CpuToken;=0D + return ProcToken;=0D }=0D =0D /**=0D @@ -1246,6 +1196,8 @@ InternalSmmStartupThisAp ( IN OUT EFI_STATUS *CpuStatus=0D )=0D {=0D + PROCEDURE_TOKEN *ProcToken;=0D +=0D if (CpuIndex >=3D gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus) {=0D DEBUG((DEBUG_ERROR, "CpuIndex(%d) >=3D gSmmCpuPrivate->SmmCoreEntryCon= text.NumberOfCpus(%d)\n", CpuIndex, gSmmCpuPrivate->SmmCoreEntryContext.Num= berOfCpus));=0D return EFI_INVALID_PARAMETER;=0D @@ -1278,14 +1230,12 @@ InternalSmmStartupThisAp ( =0D AcquireSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy);=0D =0D - if (Token !=3D NULL) {=0D - *Token =3D (MM_COMPLETION) CreateToken ();=0D - }=0D -=0D mSmmMpSyncData->CpuData[CpuIndex].Procedure =3D Procedure;=0D mSmmMpSyncData->CpuData[CpuIndex].Parameter =3D ProcArguments;=0D if (Token !=3D NULL) {=0D - mSmmMpSyncData->CpuData[CpuIndex].Token =3D (SPIN_LOCK *)(*Token);=0D + ProcToken=3D CreateToken (1);=0D + mSmmMpSyncData->CpuData[CpuIndex].Token =3D ProcToken;=0D + *Token =3D (MM_COMPLETION)ProcToken->SpinLock;=0D }=0D mSmmMpSyncData->CpuData[CpuIndex].Status =3D CpuStatus;=0D if (mSmmMpSyncData->CpuData[CpuIndex].Status !=3D NULL) {=0D @@ -1343,6 +1293,7 @@ InternalSmmStartupAllAPs ( {=0D UINTN Index;=0D UINTN CpuCount;=0D + PROCEDURE_TOKEN *ProcToken;=0D =0D if ((TimeoutInMicroseconds !=3D 0) && ((mSmmMp.Attributes & EFI_MM_MP_TI= MEOUT_SUPPORTED) =3D=3D 0)) {=0D return EFI_INVALID_PARAMETER;=0D @@ -1371,7 +1322,10 @@ InternalSmmStartupAllAPs ( }=0D =0D if (Token !=3D NULL) {=0D - *Token =3D (MM_COMPLETION) CreateToken ();=0D + ProcToken =3D CreateToken ((UINT32)mMaxNumberOfCpus);=0D + *Token =3D (MM_COMPLETION)ProcToken->SpinLock;=0D + } else {=0D + ProcToken =3D NULL;=0D }=0D =0D //=0D @@ -1392,7 +1346,7 @@ InternalSmmStartupAllAPs ( mSmmMpSyncData->CpuData[Index].Procedure =3D (EFI_AP_PROCEDURE2) Pro= cedure;=0D mSmmMpSyncData->CpuData[Index].Parameter =3D ProcedureArguments;=0D if (Token !=3D NULL) {=0D - mSmmMpSyncData->CpuData[Index].Token =3D (SPIN_LOCK *)(*Token);= =0D + mSmmMpSyncData->CpuData[Index].Token =3D ProcToken;=0D }=0D if (CPUStatus !=3D NULL) {=0D mSmmMpSyncData->CpuData[Index].Status =3D &CPUStatus[Index];=0D @@ -1408,6 +1362,13 @@ InternalSmmStartupAllAPs ( if (CPUStatus !=3D NULL) {=0D CPUStatus[Index] =3D EFI_NOT_STARTED;=0D }=0D +=0D + //=0D + // Decrease the count to mark this AP as finished.=0D + //=0D + if (Token !=3D NULL) {=0D + WaitForSemaphore (&ProcToken->RunningApCount);=0D + }=0D }=0D }=0D =0D diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h b/UefiCpuPkg/PiSmmC= puDxeSmm/PiSmmCpuDxeSmm.h index 5c1a01e42b..5c98494e2c 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h @@ -212,7 +212,8 @@ typedef struct { UINTN Signature;=0D LIST_ENTRY Link;=0D =0D - SPIN_LOCK *ProcedureToken;=0D + SPIN_LOCK *SpinLock;=0D + volatile UINT32 RunningApCount;=0D } PROCEDURE_TOKEN;=0D =0D #define PROCEDURE_TOKEN_FROM_LINK(a) CR (a, PROCEDURE_TOKEN, Link, PROCED= URE_TOKEN_SIGNATURE)=0D @@ -407,7 +408,7 @@ typedef struct { volatile VOID *Parameter;=0D volatile UINT32 *Run;=0D volatile BOOLEAN *Present;=0D - SPIN_LOCK *Token;=0D + PROCEDURE_TOKEN *Token;=0D EFI_STATUS *Status;=0D } SMM_CPU_DATA_BLOCK;=0D =0D --=20 2.23.0.windows.1