[edk2] [PATCH] EmulatorPkg/MpService: Read/Write processor State should hold mutex lock

Subject: [edk2] [PATCH] EmulatorPkg/MpService: Read/Write processor State should hold mutex lock

From: Chen Fan <chen.fan.fnst@cn.fujitsu.com>

To: <edk2-devel@lists.sourceforge.net>

Date: 2014-07-23 21:59:57

Add mutex protect when manipulate processor State.
and fix some wrong Lock's usage in where using the addressof StateLock's address.
gThread->MutexLock (&gMPSystem.ProcessorData[NextNumber].StateLock);
=>
gThread->MutexLock (gMPSystem.ProcessorData[NextNumber].StateLock);

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Chen Fan 
---
 EmulatorPkg/CpuRuntimeDxe/MpService.c | 37 ++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 14 deletions(-)

diff --git a/EmulatorPkg/CpuRuntimeDxe/MpService.c b/EmulatorPkg/CpuRuntimeDxe/MpService.c
index 9d67b86..0b5fcbd 100644
--- a/EmulatorPkg/CpuRuntimeDxe/MpService.c
+++ b/EmulatorPkg/CpuRuntimeDxe/MpService.c
@@ -437,17 +437,17 @@ CpuMpServicesStartupAllAps (
     // state 1 by 1, until the previous 1 finished its task
     // if not "SingleThread", all APs are put to ready state from the beginning
     //
+    gThread->MutexLock(ProcessorData->StateLock);
     if (ProcessorData->State == CPU_STATE_IDLE) {
-      gMPSystem.StartCount++;
-
-      gThread->MutexLock (&ProcessorData->StateLock);
       ProcessorData->State = APInitialState;
-      gThread->MutexUnlock (&ProcessorData->StateLock);
+      gThread->MutexUnlock (ProcessorData->StateLock);
 
+      gMPSystem.StartCount++;
       if (SingleThread) {
         APInitialState = CPU_STATE_BLOCKED;
       }
     } else {
+      gThread->MutexUnlock (ProcessorData->StateLock);
       return EFI_NOT_READY;
     }
   }
@@ -512,9 +512,9 @@ CpuMpServicesStartupAllAps (
         if (SingleThread) {
           Status = GetNextBlockedNumber (&NextNumber);
           if (!EFI_ERROR (Status)) {
-            gThread->MutexLock (&gMPSystem.ProcessorData[NextNumber].StateLock);
+            gThread->MutexLock (gMPSystem.ProcessorData[NextNumber].StateLock);
             gMPSystem.ProcessorData[NextNumber].State = CPU_STATE_READY;
-            gThread->MutexUnlock (&gMPSystem.ProcessorData[NextNumber].StateLock);
+            gThread->MutexUnlock (gMPSystem.ProcessorData[NextNumber].StateLock);
           }
         }
 
@@ -671,9 +671,12 @@ CpuMpServicesStartupThisAP (
     return EFI_INVALID_PARAMETER;
   }
 
+  gThread->MutexLock(gMPSystem.ProcessorData[ProcessorNumber].StateLock);
   if (gMPSystem.ProcessorData[ProcessorNumber].State != CPU_STATE_IDLE) {
+    gThread->MutexUnlock(gMPSystem.ProcessorData[ProcessorNumber].StateLock);
     return EFI_NOT_READY;
   }
+  gThread->MutexUnlock(gMPSystem.ProcessorData[ProcessorNumber].StateLock);
 
   if ((WaitEvent != NULL)  && gReadToBoot) {
     return EFI_UNSUPPORTED;
@@ -699,14 +702,14 @@ CpuMpServicesStartupThisAP (
 
   // Blocking
   while (TRUE) {
-    gThread->MutexLock (&gMPSystem.ProcessorData[ProcessorNumber].StateLock);
+    gThread->MutexLock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
     if (gMPSystem.ProcessorData[ProcessorNumber].State == CPU_STATE_FINISHED) {
       gMPSystem.ProcessorData[ProcessorNumber].State = CPU_STATE_IDLE;
-      gThread->MutexUnlock (&gMPSystem.ProcessorData[ProcessorNumber].StateLock);
+      gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
       break;
     }
 
-    gThread->MutexUnlock (&gMPSystem.ProcessorData[ProcessorNumber].StateLock);
+    gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
 
     if ((TimeoutInMicroseconds != 0) && (Timeout < 0)) {
       return EFI_TIMEOUT;
@@ -789,10 +792,14 @@ CpuMpServicesSwitchBSP (
   }
   ASSERT (Index != gMPSystem.NumberOfProcessors);
 
+  gThread->MutexLock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
   if (gMPSystem.ProcessorData[ProcessorNumber].State != CPU_STATE_IDLE) {
+    gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
     return EFI_NOT_READY;
   }
 
+  gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
+
   // Skip for now as we need switch a bunch of stack stuff around and it's complex
   // May not be worth it?
   return EFI_NOT_READY;
@@ -861,11 +868,13 @@ CpuMpServicesEnableDisableAP (
     return EFI_INVALID_PARAMETER;
   }
 
+  gThread->MutexLock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
   if (gMPSystem.ProcessorData[ProcessorNumber].State != CPU_STATE_IDLE) {
+    gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
     return EFI_UNSUPPORTED;
   }
 
-  gThread->MutexLock (&gMPSystem.ProcessorData[ProcessorNumber].StateLock);
+  gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
 
   if (EnableAP) {
     if ((gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag & PROCESSOR_ENABLED_BIT) == 0 ) {
@@ -884,8 +893,6 @@ CpuMpServicesEnableDisableAP (
     gMPSystem.ProcessorData[ProcessorNumber].Info.StatusFlag |= (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT);
   }
 
-  gThread->MutexUnlock (&gMPSystem.ProcessorData[ProcessorNumber].StateLock);
-
   return EFI_SUCCESS;
 }
 
@@ -1015,15 +1022,17 @@ CpuCheckAllAPsStatus (
         if (!EFI_ERROR (Status)) {
           NextData = &gMPSystem.ProcessorData[NextNumber];
 
-          gThread->MutexLock (&NextData->ProcedureLock);
+          gThread->MutexLock (NextData->StateLock);
           NextData->State = CPU_STATE_READY;
-          gThread->MutexUnlock (&NextData->ProcedureLock);
+          gThread->MutexUnlock (NextData->StateLock);
 
           SetApProcedure (NextData, gMPSystem.Procedure, gMPSystem.ProcedureArgument);
         }
       }
 
+      gThread->MutexLock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
       gMPSystem.ProcessorData[ProcessorNumber].State = CPU_STATE_IDLE;
+      gThread->MutexUnlock (gMPSystem.ProcessorData[ProcessorNumber].StateLock);
       gMPSystem.FinishCount++;
       break;
 
-- 
1.9.3


------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/edk2-devel