From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by mx.groups.io with SMTP id smtpd.web09.5751.1576820090871902675 for ; Thu, 19 Dec 2019 21:34:51 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 134.134.136.20, mailfrom: eric.dong@intel.com) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Dec 2019 21:34:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,334,1571727600"; d="scan'208";a="213474489" Received: from ydong10-desktop.ccr.corp.intel.com ([10.239.158.133]) by fmsmga008.fm.intel.com with ESMTP; 19 Dec 2019 21:34:49 -0800 From: "Dong, Eric" To: devel@edk2.groups.io Cc: Ray Ni , Laszlo Ersek Subject: [PATCH] UefiCpuPkg/PiSmmCpuDxeSmm: Remove dependence between APs. Date: Fri, 20 Dec 2019 13:34:46 +0800 Message-Id: <20191220053446.1532-1-eric.dong@intel.com> X-Mailer: git-send-email 2.23.0.windows.1 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 | 107 +++++++-------------- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 4 +- 2 files changed, 36 insertions(+), 75 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c b/UefiCpuPkg/PiSmmCpuDxe= Smm/MpService.c index 757f1056f7..bd5fdfd728 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,20 @@ 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 + Token =3D mSmmMpSyncData->CpuData[CpuIndex].Token;=0D + mSmmMpSyncData->CpuData[CpuIndex].Token =3D NULL;=0D +=0D + if (!Token->SingleAp) {=0D + ReleaseSemaphore (&Token->FinishedApCount);=0D +=0D + if (Token->FinishedApCount < (UINT32)mMaxNumberOfCpus) {=0D + return;=0D }=0D }=0D +=0D + ReleaseSpinLock (Token->ProcedureToken);=0D }=0D =0D /**=0D @@ -912,12 +860,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 @@ -1127,7 +1077,7 @@ IsTokenInUse ( @retval return the spin lock used as token.=0D =0D **/=0D -SPIN_LOCK *=0D +PROCEDURE_TOKEN *=0D CreateToken (=0D VOID=0D )=0D @@ -1170,10 +1120,12 @@ CreateToken ( ASSERT (ProcToken !=3D NULL);=0D ProcToken->Signature =3D PROCEDURE_TOKEN_SIGNATURE;=0D ProcToken->ProcedureToken =3D CpuToken;=0D + ProcToken->FinishedApCount =3D 0;=0D + ProcToken->SingleAp =3D TRUE;=0D =0D InsertTailList (&gSmmCpuPrivate->TokenList, &ProcToken->Link);=0D =0D - return CpuToken;=0D + return ProcToken;=0D }=0D =0D /**=0D @@ -1278,14 +1230,11 @@ 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 + mSmmMpSyncData->CpuData[CpuIndex].Token =3D CreateToken ();=0D + *Token =3D (MM_COMPLETION) mSmmMpSyncData->CpuData[CpuIndex].Token->Pr= ocedureToken;=0D }=0D mSmmMpSyncData->CpuData[CpuIndex].Status =3D CpuStatus;=0D if (mSmmMpSyncData->CpuData[CpuIndex].Status !=3D NULL) {=0D @@ -1343,6 +1292,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 +1321,11 @@ InternalSmmStartupAllAPs ( }=0D =0D if (Token !=3D NULL) {=0D - *Token =3D (MM_COMPLETION) CreateToken ();=0D + ProcToken =3D CreateToken ();=0D + *Token =3D (MM_COMPLETION)ProcToken->ProcedureToken;=0D + ProcToken->SingleAp =3D FALSE;=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,11 @@ InternalSmmStartupAllAPs ( if (CPUStatus !=3D NULL) {=0D CPUStatus[Index] =3D EFI_NOT_STARTED;=0D }=0D +=0D + //=0D + // Increate the count to mark this AP as finished.=0D + //=0D + ReleaseSemaphore (&ProcToken->FinishedApCount);=0D }=0D }=0D =0D diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h b/UefiCpuPkg/PiSmmC= puDxeSmm/PiSmmCpuDxeSmm.h index 5c1a01e42b..0551539aee 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h @@ -213,6 +213,8 @@ typedef struct { LIST_ENTRY Link;=0D =0D SPIN_LOCK *ProcedureToken;=0D + BOOLEAN SingleAp;=0D + volatile UINT32 FinishedApCount;=0D } PROCEDURE_TOKEN;=0D =0D #define PROCEDURE_TOKEN_FROM_LINK(a) CR (a, PROCEDURE_TOKEN, Link, PROCED= URE_TOKEN_SIGNATURE)=0D @@ -407,7 +409,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