public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Ankur Arora" <ankur.a.arora@oracle.com>
To: devel@edk2.groups.io
Cc: imammedo@redhat.com, lersek@redhat.com,
	Ankur Arora <ankur.a.arora@oracle.com>,
	Jordan Justen <jordan.l.justen@intel.com>,
	Ard Biesheuvel <ard.biesheuvel@arm.com>,
	Boris Ostrovsky <boris.ostrovsky@oracle.com>,
	Aaron Young <aaron.young@oracle.com>
Subject: [PATCH v3 08/10] OvmfPkg/CpuHotplugSmm: add worker to do CPU ejection
Date: Thu, 14 Jan 2021 23:45:31 -0800	[thread overview]
Message-ID: <20210115074533.277448-9-ankur.a.arora@oracle.com> (raw)
In-Reply-To: <20210115074533.277448-1-ankur.a.arora@oracle.com>

Designate a worker CPU (we use the one executing the root MMI handler),
which will do the final CPU ejection. This happens via CpuEject().

On the worker CPU, CpuEject() calls QEMU to do the ejection for each CPU
that is unplugged. QEMU handles this by signalling the remote VCPU thread
which forces the SMI AP to context switch out of the SMI, ending with its
QEMU state destroyed.

On the AP, CpuEject() spins around in its holding area until the
context-switch happens. Given that the context switch would end up with
the AP state being cleaned up, this means that the AP will never
return to finish the SMI handling, and thus would not restore some
of the CPU state that it ordinarily would (in SmiRendezvous() and in
SmiEntry.nasm::CommonHandler).

This unrestored state includes FPU state, CET enable, stuffing of
RSB and the final RSM. Given that the CPU state is destroyed by
QEMU on unplug, this should be okay.

Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
Cc: Igor Mammedov <imammedo@redhat.com>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: Aaron Young <aaron.young@oracle.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3132
Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com>
---
 OvmfPkg/CpuHotplugSmm/CpuHotplug.c | 61 ++++++++++++++++++++++++++++++++++----
 1 file changed, 56 insertions(+), 5 deletions(-)

diff --git a/OvmfPkg/CpuHotplugSmm/CpuHotplug.c b/OvmfPkg/CpuHotplugSmm/CpuHotplug.c
index 4048490783e4..8aa52ebe5dd1 100644
--- a/OvmfPkg/CpuHotplugSmm/CpuHotplug.c
+++ b/OvmfPkg/CpuHotplugSmm/CpuHotplug.c
@@ -266,6 +266,20 @@ UnplugCpus(
     ToUnplugIdx++;
   }
 
+  if (EjectCount) {
+    UINTN  Worker;
+    Status = mMmCpuService->WhoAmI(mMmCpuService, &Worker);
+    ASSERT_EFI_ERROR(Status);
+    //
+    // UnplugCpus() is called via the root MMI handler and thus we are in the
+    // BSP context. Accordingly, mark ourselves as the ejecting CPU.
+    // Note that, the QEMU eject protocol does not specify that only the BSP
+    // can do the ejection, so this should be safe on any CPU (that is not itself
+    // being unplugged.)
+    //
+    mCpuHotEjectData->ApicIdMap[Worker] = CPU_EJECT_WORKER;
+  }
+
   //
   // We've handled this unplug.
   //
@@ -383,11 +397,6 @@ CpuHotplugMmi (
   if (EFI_ERROR (Status)) {
     goto Fatal;
   }
-  if (ToUnplugCount > 0) {
-    DEBUG ((DEBUG_ERROR, "%a: hot-unplug is not supported yet\n",
-      __FUNCTION__));
-    goto Fatal;
-  }
 
   if (PluggedCount > 0) {
     Status = PlugCpus(mPluggedApicIds, PluggedCount);
@@ -430,6 +439,48 @@ CpuEject(
     return;
   }
 
+  if (ApicId == CPU_EJECT_WORKER) {
+    UINT32 CpuIndex;
+    for (CpuIndex = 0; CpuIndex < mCpuHotEjectData->ArrayLength; CpuIndex++) {
+      UINT64 RemoveApicId = mCpuHotEjectData->ApicIdMap[CpuIndex];
+
+      if ((RemoveApicId != CPU_EJECT_INVALID && RemoveApicId != CPU_EJECT_WORKER)) {
+
+        //
+        // The CPUs to be unplugged have received the BSP's signal to exit the
+        // SMI and either will execute SmmCpuFeaturesSmiRendezvousExit()
+        // followed by this callback or are already waiting in the CpuDeadLoop()
+        // below.
+        //
+        // Tell QEMU to put them out of their misery.
+        //
+         QemuCpuhpWriteCpuSelector (mMmCpuIo, RemoveApicId);
+         QemuCpuhpWriteCpuStatus (mMmCpuIo, QEMU_CPUHP_STAT_EJECTED);
+
+         //
+         // Compiler memory barrier to ensure the next store isn't reordered
+         //
+         MemoryFence();
+
+        // Clear the unplug status for CpuIndex to ensure that an invalid SMI
+        // later does not end up trying to unplug it or the newly hotplugged
+        // CpuIndex does not go into the dead loop.
+        //
+        mCpuHotEjectData->ApicIdMap[CpuIndex] = CPU_EJECT_INVALID;
+
+        DEBUG ((DEBUG_INFO, "%a: Unplugged CPU " FMT_APIC_ID "\n",
+               __FUNCTION__, RemoveApicId));
+       }
+     }
+
+    //
+    // Clear our own CPU status to ensure that we don't needlessly enter
+    // the this loop on the next SMI.
+    //
+    mCpuHotEjectData->ApicIdMap[ProcessorNum] = CPU_EJECT_INVALID;
+    return;
+   }
+
   //
   // CPU(s) being unplugged get here from SmmCpuFeaturesSmiRendezvousExit()
   // after having been cleared to exit the SMI by the monarch and thus have
-- 
2.9.3


  parent reply	other threads:[~2021-01-15  7:45 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-15  7:45 [PATCH v3 00/10] support CPU hot-unplug Ankur Arora
2021-01-15  7:45 ` [PATCH v3 01/10] OvmfPkg/CpuHotplugSmm: refactor hotplug logic Ankur Arora
2021-01-15  7:45 ` [PATCH v3 02/10] OvmfPkg/CpuHotplugSmm: collect hot-unplug events Ankur Arora
2021-01-15  7:45 ` [PATCH v3 03/10] OvmfPkg/CpuHotplugSmm: add Qemu Cpu Status helper Ankur Arora
2021-01-15  7:45 ` [PATCH v3 04/10] UefiCpuPkg: add CPU ejection support Ankur Arora
2021-01-15  8:17   ` [edk2-devel] " Laszlo Ersek
2021-01-15 18:16     ` Ankur Arora
2021-01-15 18:44       ` Laszlo Ersek
2021-01-15 19:22         ` Ankur Arora
2021-01-15  7:45 ` [PATCH v3 05/10] OvmfPkg/SmmCpuFeaturesLib: init CPU ejection state Ankur Arora
2021-01-15  7:45 ` [PATCH v3 06/10] OvmfPkg/CpuHotplugSmm: support CPU eject Ankur Arora
2021-01-15  7:45 ` [PATCH v3 07/10] OvmfPkg/CpuHotplugSmm: introduce UnplugCpus() Ankur Arora
2021-01-15  7:45 ` Ankur Arora [this message]
2021-01-15  7:45 ` [PATCH v3 09/10] OvmfPkg/SmmControl2Dxe: negotiate hot-unplug Ankur Arora
2021-01-15  7:45 ` [PATCH v3 10/10] MdePkg: use CpuPause() in CpuDeadLoop() Ankur Arora

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210115074533.277448-9-ankur.a.arora@oracle.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox