From: "Laszlo Ersek" <lersek@redhat.com>
To: Ankur Arora <ankur.a.arora@oracle.com>, devel@edk2.groups.io
Cc: imammedo@redhat.com, boris.ostrovsky@oracle.com,
Jordan Justen <jordan.l.justen@intel.com>,
Ard Biesheuvel <ard.biesheuvel@arm.com>,
Aaron Young <aaron.young@oracle.com>
Subject: Re: [edk2-devel] [PATCH v8 09/10] OvmfPkg/CpuHotplugSmm: do actual CPU hot-eject
Date: Thu, 25 Feb 2021 20:22:58 +0100 [thread overview]
Message-ID: <ce1bdb39-992d-7d81-24b8-60f04a0316a2@redhat.com> (raw)
In-Reply-To: <f0334b92-5c35-f4e1-d0b9-9991ca2fcb8d@oracle.com>
On 02/24/21 04:44, Ankur Arora wrote:
> On 2021-02-23 1:39 p.m., Laszlo Ersek wrote:
>> On 02/22/21 08:19, Ankur Arora wrote:
>>> + UINT32 Idx;
>>> +
>>> + for (Idx = 0; Idx < mCpuHotEjectData->ArrayLength; Idx++) {
>>> + UINT64 QemuSelector;
>>> +
>>> + QemuSelector = mCpuHotEjectData->QemuSelectorMap[Idx];
>>> +
>>> + if (QemuSelector != CPU_EJECT_QEMU_SELECTOR_INVALID) {
>>> + //
>>> + // This to-be-ejected-CPU has already received the BSP's SMI
>>> exit
>>> + // signal and, will execute SmmCpuFeaturesRendezvousExit()
>>> + // followed by this callback or is already waiting in the
>>> + // CpuSleep() loop below.
>>> + //
>>> + // Tell QEMU to context-switch it out.
>>> + //
>>> + QemuCpuhpWriteCpuSelector (mMmCpuIo, (UINT32) QemuSelector);
>>> + QemuCpuhpWriteCpuStatus (mMmCpuIo, QEMU_CPUHP_STAT_EJECT);
>>> +
>>> + //
>>> + // We need a compiler barrier here to ensure that the compiler
>>> + // does not reorder the CpuStatus and QemuSelectorMap[Idx]
>>> stores.
>>> + //
>>> + // A store fence is not strictly necessary on x86 which has
>>> + // TSO; however, both of these stores are in different
>>> address spaces
>>> + // so also add a Store Fence here.
>>> + //
>>> + MemoryFence ();
>>
>> (6) I wonder if this compiler barrier + comment block are helpful.
>> Paraphrasing your (ex-)colleague Liran, if MMIO and IO Port accessors
>> didn't contain built-in fences, all hell would break lose. We're using
>> EFI_MM_CPU_IO_PROTOCOL for IO Port accesses. I think we should be safe
>> ordering-wise, even without an explicit compiler barrier here.
>>
>> To me personally, this particular fence only muddies the picture --
>> where we already have an acquire memory fence and a store memory fence
>> to couple with each other.
>>
>> I'd recommend removing this. (If you disagree, I'm willing to listen to
>> arguments, of course!)
>
> You are right that we don't need a memory fence here -- given that there
> is an implicit fence due to the MMIO.
>
> As for the compiler fence, I'm just now re-looking at handlers in
> EFI_MM_CPU_IO_PROTOCOL and they do seem to include a compiler barrier.
>
> So I agree with you that we have all the fences that we need. However,
> I do think it's a good idea to document both of these here.
OK.
>>> diff --git a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c
>>> b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c
>>> index 99988285b6a2..ddfef05ee6cf 100644
>>> --- a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c
>>> +++ b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c
>>> @@ -472,6 +472,37 @@ SmmCpuFeaturesRendezvousExit (
>>> // (PcdCpuMaxLogicalProcessorNumber > 1), and hot-eject is needed
>>> // in this SMI exit (otherwise mCpuHotEjectData->Handler is not
>>> armed.)
>>> //
>>> + // mCpuHotEjectData itself is stable once setup so it can be
>>> + // dereferenced without needing any synchronization,
>>> + // but, mCpuHotEjectData->Handler is updated on the BSP in the
>>> + // ongoing SMI iteration at two places:
>>> + //
>>> + // - UnplugCpus() where the BSP determines if a CPU is under ejection
>>> + // or not. As the comment where mCpuHotEjectData->Handler is set-up
>>> + // describes any such updates are guaranteed to be
>>> ordered-before the
>>> + // dereference below.
>>> + //
>>> + // - EjectCpu() (which is called via the Handler below), on the BSP
>>> + // updates mCpuHotEjectData->Handler once it is done with all
>>> ejections.
>>> + //
>>> + // The CPU under ejection: might be executing anywhere between the
>>> + // "AllCpusInSync" exit loop in SmiRendezvous() to about to
>>> + // dereference the Handler field.
>>> + // Given that the BSP ensures that this store only happens after
>>> all
>>> + // CPUs under ejection have been ejected, this CPU would never see
>>> + // the after value.
>>> + // (Note that any CPU that is already executing the CpuSleep() loop
>>> + // below never raced any updates and always saw the before value.)
>>> + //
>>> + // CPUs not-under ejection: might see either value of the Handler
>>> + // which is fine, because the Handler is a NOP for CPUs not-under
>>> + // ejection.
>>> + //
>>> + // Lastly, note that we are also guaranteed that any dereferencing
>>> + // CPU only sees the before or after value and not an intermediate
>>> + // value. This is because mCpuHotEjectData->Handler is aligned at a
>>> + // natural boundary.
>>> + //
>>> if (mCpuHotEjectData != NULL) {
>>> CPU_HOT_EJECT_HANDLER Handler;
>>>
>>
>> (8) I can't really put my finger on it, I just feel that repeating
>> (open-coding) this wall of text here is not really productive.
>
> Part of the reason I wanted to document this here was to get your
> opinion on it and figure out how much of it is useful and how
> much might be overkill.
>
>>
>> Do you think that, after you add the "acquire memory fence" comment in
>> patch #7, we could avoid most of the text here? I think we should only
>> point out (in patch #7) the "release fence" that the logic here pairs
>> with.> If you really want to present it all from both perspectives, I
>> guess I'm
>> OK with that, but then I believe we should drop the last paragraph at
>> least (see point (4)).
>
> Rereading it after a gap of a few days and given that most of this is
> just a repeat, I'm also tending towards overkill. I think a comment
> talking about acquire/release pairing is useful. Rest of it can probably
> be met with just a pointer towards the comment in EjectCpus(). Does that
> make sense?
Yes, absolutely. Short comment + pointer to the "other half" (which has
the large comment too) seem best.
Thanks
Laszlo
next prev parent reply other threads:[~2021-02-25 19:23 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-22 7:19 [PATCH v8 00/10] support CPU hot-unplug Ankur Arora
2021-02-22 7:19 ` [PATCH v8 01/10] OvmfPkg/CpuHotplugSmm: refactor hotplug logic Ankur Arora
2021-02-22 11:49 ` [edk2-devel] " Laszlo Ersek
2021-02-22 7:19 ` [PATCH v8 02/10] OvmfPkg/CpuHotplugSmm: collect hot-unplug events Ankur Arora
2021-02-22 12:27 ` [edk2-devel] " Laszlo Ersek
2021-02-22 22:03 ` Ankur Arora
2021-02-23 16:44 ` Laszlo Ersek
2021-02-22 7:19 ` [PATCH v8 03/10] OvmfPkg/CpuHotplugSmm: add Qemu Cpu Status helper Ankur Arora
2021-02-22 12:31 ` [edk2-devel] " Laszlo Ersek
2021-02-22 22:22 ` Ankur Arora
2021-02-22 7:19 ` [PATCH v8 04/10] OvmfPkg/CpuHotplugSmm: introduce UnplugCpus() Ankur Arora
2021-02-22 12:39 ` [edk2-devel] " Laszlo Ersek
2021-02-22 22:22 ` Ankur Arora
2021-02-22 7:19 ` [PATCH v8 05/10] OvmfPkg/CpuHotplugSmm: define CPU_HOT_EJECT_DATA Ankur Arora
2021-02-22 13:06 ` [edk2-devel] " Laszlo Ersek
2021-02-22 22:33 ` Ankur Arora
2021-02-22 7:19 ` [PATCH v8 06/10] OvmfPkg/SmmCpuFeaturesLib: init CPU ejection state Ankur Arora
2021-02-22 14:19 ` [edk2-devel] " Laszlo Ersek
2021-02-23 7:37 ` Ankur Arora
2021-02-22 7:19 ` [PATCH v8 07/10] OvmfPkg/SmmCpuFeaturesLib: call CPU hot-eject handler Ankur Arora
2021-02-22 14:53 ` [edk2-devel] " Laszlo Ersek
2021-02-23 7:37 ` Ankur Arora
2021-02-23 16:52 ` Laszlo Ersek
2021-02-23 7:45 ` Paolo Bonzini
2021-02-23 17:06 ` Laszlo Ersek
2021-02-23 17:18 ` Paolo Bonzini
2021-02-23 20:46 ` Ankur Arora
2021-02-22 7:19 ` [PATCH v8 08/10] OvmfPkg/CpuHotplugSmm: add EjectCpu() Ankur Arora
2021-02-23 20:36 ` [edk2-devel] " Laszlo Ersek
2021-02-23 20:51 ` Ankur Arora
2021-02-22 7:19 ` [PATCH v8 09/10] OvmfPkg/CpuHotplugSmm: do actual CPU hot-eject Ankur Arora
2021-02-23 21:39 ` [edk2-devel] " Laszlo Ersek
2021-02-24 3:44 ` Ankur Arora
2021-02-25 19:22 ` Laszlo Ersek [this message]
2021-02-22 7:19 ` [PATCH v8 10/10] OvmfPkg/SmmControl2Dxe: negotiate CPU hot-unplug Ankur Arora
2021-02-23 21:52 ` [edk2-devel] " Laszlo Ersek
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=ce1bdb39-992d-7d81-24b8-60f04a0316a2@redhat.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