From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 192.55.52.120, mailfrom: damian.nikodem@intel.com) Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by groups.io with SMTP; Tue, 03 Sep 2019 07:59:44 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 Sep 2019 07:59:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,463,1559545200"; d="scan'208";a="182147280" Received: from dnikode-mobl.ger.corp.intel.com ([10.102.88.77]) by fmsmga008.fm.intel.com with ESMTP; 03 Sep 2019 07:59:42 -0700 From: Damian Nikodem To: devel@edk2.groups.io Cc: Damian Nikodem , Eric Dong , Ray Ni , Benjamin You , Laszlo Ersek , Krzysztof Rusocki Subject: [PATCH] UefiCpuPkg/PiSmmCpuDxeSmm: Race condition between APHandler's release of Busy spinlock and user-triggered SmmStartupThisAP's Date: Tue, 3 Sep 2019 16:57:32 +0200 Message-Id: <20190903145732.18604-1-damian.nikodem@intel.com> X-Mailer: git-send-email 2.17.1.windows.2 Race condition between APHandler's release of Busy spinlock and user-triggered SmmStartupThisAP's acquisition attempt of the Busy spinlock (non-blocking mode). UserProc is the user's procedure to execute on an AP. UserProcCompletion is the user procedure's completion spinlock. All other variables are from EDK2. BSP AP ===================================================================================== APHandler () WaitForSemaphore (Run) << initial state >> AcquireSpinLock (UserProcCompletion) SmmStartupThisAp (Procedure) AcquireSpinLockOrFail (Busy) ReleaseSemaphore (Run) UserProc () DoStuff() DoSomeOtherStuff () AcquireSpinLockOrFail (UserProcCompletion) AcquireSpinLockOrFail (UserProcCompletion) ^^ waiting in a loop for user procedure's completion == these fail ReleaseSpinLock (UserProcCompletion) AcquireSpinLockOrFail (UserProcCompletion) ^^ this succeeds ReleaseSpinLock (UserProcCompletion) << return control to the caller and reenter the flow >>> AcquireSpinLock (UserProcCompletion) SmmStartupThisAp (Procedure) AcquireSpinLockOrFail (Busy) ^^ this wins the race with AP's ReleaseSpinLock and fails; ReleaseSpinLock (Busy) return EFI_INVALID_PARAMETER; To remedy, if AcquireSpinLockOrFail (of the Busy spinlock) fails, perform regular AcquireSpinLock -- this eliminates the race condition. Signed-off-by: Damian Nikodem Cc: Eric Dong Cc: Ray Ni Cc: Benjamin You Cc: Laszlo Ersek Cc: Krzysztof Rusocki diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c b/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c index d8d2b6f444..206e196a76 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c @@ -1239,8 +1239,16 @@ InternalSmmStartupThisAp ( AcquireSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy); } else { if (!AcquireSpinLockOrFail (mSmmMpSyncData->CpuData[CpuIndex].Busy)) { - DEBUG((DEBUG_ERROR, "Can't acquire mSmmMpSyncData->CpuData[%d].Busy\n", CpuIndex)); - return EFI_NOT_READY; + DEBUG ((DEBUG_INFO, "BSP[%d] finds AP[%d] busy at proc 0x%llX (param 0x%llX), ", + mSmmMpSyncData->BspIndex, + CpuIndex, + *mSmmMpSyncData->CpuData[CpuIndex].Procedure, + (VOID*)mSmmMpSyncData->CpuData[CpuIndex].Parameter)); + DEBUG ((DEBUG_INFO, "new proc 0x%llX (param 0x%llX). Waiting for the previous AP procedure to complete...\n", + Procedure, + ProcArguments)); + + AcquireSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy); } *Token = (MM_COMPLETION) CreateToken (); -------------------------------------------------------------------- Intel Technology Poland sp. z o.o. ul. Slowackiego 173 | 80-298 Gdansk | Sad Rejonowy Gdansk Polnoc | VII Wydzial Gospodarczy Krajowego Rejestru Sadowego - KRS 101882 | NIP 957-07-52-316 | Kapital zakladowy 200.000 PLN. Ta wiadomosc wraz z zalacznikami jest przeznaczona dla okreslonego adresata i moze zawierac informacje poufne. W razie przypadkowego otrzymania tej wiadomosci, prosimy o powiadomienie nadawcy oraz trwale jej usuniecie; jakiekolwiek przegladanie lub rozpowszechnianie jest zabronione. This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). If you are not the intended recipient, please contact the sender and delete all copies; any review or distribution by others is strictly prohibited.