From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.groups.io with SMTP id smtpd.web10.35181.1614003560261086584 for ; Mon, 22 Feb 2021 06:19:20 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ps6TE3zN; spf=pass (domain: redhat.com, ip: 216.205.24.124, mailfrom: lersek@redhat.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614003559; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Rt7ay/tsbT8hJpBRlDLiIw7s/hQAtHTNRFRevwOz3bc=; b=Ps6TE3zNqvfxenjJSt3SaPDJcBNp+9kas4VwBRCdR/V2m6RODXwKht9YgNjBzA0uiIGsQj hnX7wDYAciR0O+TdwMkqeeYSHeb+75KHE7+4AJvTo+VeyKSzLf75B88i5Eb2QzT1hGDPbR vp0y+d7cU9nLuLj8rBE+BuRdGHiUTH4= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-247-nm-XHomnPTKkg8smS-3CQg-1; Mon, 22 Feb 2021 09:19:15 -0500 X-MC-Unique: nm-XHomnPTKkg8smS-3CQg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 134C280197D; Mon, 22 Feb 2021 14:19:13 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-113-67.ams2.redhat.com [10.36.113.67]) by smtp.corp.redhat.com (Postfix) with ESMTP id CBD4160C43; Mon, 22 Feb 2021 14:19:08 +0000 (UTC) Subject: Re: [edk2-devel] [PATCH v8 06/10] OvmfPkg/SmmCpuFeaturesLib: init CPU ejection state To: devel@edk2.groups.io, ankur.a.arora@oracle.com Cc: imammedo@redhat.com, boris.ostrovsky@oracle.com, Jordan Justen , Ard Biesheuvel , Aaron Young References: <20210222071928.1401820-1-ankur.a.arora@oracle.com> <20210222071928.1401820-7-ankur.a.arora@oracle.com> From: "Laszlo Ersek" Message-ID: <7fcf7a88-9d43-c546-409f-65d2419df7b3@redhat.com> Date: Mon, 22 Feb 2021 15:19:07 +0100 MIME-Version: 1.0 In-Reply-To: <20210222071928.1401820-7-ankur.a.arora@oracle.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=lersek@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 02/22/21 08:19, Ankur Arora wrote: > Init CPU_HOT_EJECT_DATA, which will be used to share CPU ejection > state between SmmCpuFeaturesLib (via PiSmmCpuDxeSmm) and CpuHotPlugSmm. > > The init happens via SmmCpuFeaturesSmmRelocationComplete(), and so it > will run as part of the PiSmmCpuDxeSmm entry point function, > PiCpuSmmEntry(). Once inited, CPU_HOT_EJECT_DATA is exposed via > PcdCpuHotEjectDataAddress. > > The CPU hot-eject handler (CPU_HOT_EJECT_DATA->Handler) is setup when > there is an ejection request via CpuHotplugSmm. > > Cc: Laszlo Ersek > Cc: Jordan Justen > Cc: Ard Biesheuvel > Cc: Igor Mammedov > Cc: Boris Ostrovsky > Cc: Aaron Young > Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3132 > Signed-off-by: Ankur Arora > --- > > Notes: > Addresses the following review comments: > (1) Detail in commit message about context in which CPU_HOT_EJECT_DATA > is inited. > (2) Add in sorted order MemoryAllocationLib in LibraryClasses > (3) Sort added includes in SmmCpuFeaturesLib.c > (4a-4b) Fixup linkage directives for mCpuHotEjectData. > (5) s/CpuHotEjectData/mCpuHotEjectData/ > (6,10a,10b) Remove dependence on PcdCpuHotPlugSupport > (7) Make the tense structure consistent in block comment for > InitCpuHotEject(). > (8) s/SmmCpuFeaturesSmmInitHotEject/InitCpuHotEject/ > (9) s/mMaxNumberOfCpus/MaxNumberOfCpus/ > (11) Remove a bunch of obvious comments. > (14a,14b,14c) Use SafeUint functions and rework the allocation logic > so we can just use a single allocation. > (12) Remove the AllocatePool() cast. > (13) Use a CpuDeadLoop() in case of failure; albeit via a goto, not > inline. > (15) Initialize the mCpuHotEjectData->QemuSelectorMap locally. > (16) Fix indentation in PcdSet64S. > (17) Change the cast in PcdSet64S() to UINTN. > (18) Use RETURN_STATUS instead of EFI_STATUS. > (19,20) Move the Handler logic in SmmCpuFeaturesRendezvousExit() into > into a separate patch. > > .../SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf | 4 + > .../Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c | 92 ++++++++++++++++++++++ > 2 files changed, 96 insertions(+) Nice, I've only superficial comments: > > diff --git a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf > index 97a10afb6e27..8a426a4c10fb 100644 > --- a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf > +++ b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf > @@ -30,9 +30,13 @@ [LibraryClasses] > BaseMemoryLib > DebugLib > MemEncryptSevLib > + MemoryAllocationLib > PcdLib > + SafeIntLib > SmmServicesTableLib > UefiBootServicesTableLib > > [Pcd] > + gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber > + gUefiOvmfPkgTokenSpaceGuid.PcdCpuHotEjectDataAddress > gUefiOvmfPkgTokenSpaceGuid.PcdQ35SmramAtDefaultSmbase > diff --git a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c > index 7ef7ed98342e..adbfc90ad46e 100644 > --- a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c > +++ b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c > @@ -11,10 +11,13 @@ > #include > #include > #include > +#include > #include > +#include > #include > #include > #include > +#include > #include > #include > #include > @@ -171,6 +174,92 @@ SmmCpuFeaturesHookReturnFromSmm ( > return OriginalInstructionPointer; > } > > +STATIC CPU_HOT_EJECT_DATA *mCpuHotEjectData = NULL; > + > +/** > + Initialize mCpuHotEjectData if PcdCpuMaxLogicalProcessorNumber > 1. > + > + Also setup the corresponding PcdCpuHotEjectDataAddress. > +**/ > +STATIC > +VOID > +InitCpuHotEjectData ( > + VOID > + ) > +{ > + UINTN ArrayLen; > + UINTN BaseLen; > + UINTN TotalLen; > + UINT32 Idx; > + UINT32 MaxNumberOfCpus; > + RETURN_STATUS PcdStatus; > + > + MaxNumberOfCpus = PcdGet32 (PcdCpuMaxLogicalProcessorNumber); > + (1) This doesn't deserve an update in itself, but since I'll ask for some more below, I might as well name it: please remove the empty line here. > + if (MaxNumberOfCpus == 1) { > + return; > + } > + > + // > + // We want the following lay out for CPU_HOT_EJECT_DATA: > + // UINTN alignment: CPU_HOT_EJECT_DATA > + // --- padding if needed --- > + // UINT64 alignment: CPU_HOT_EJECT_DATA->QemuSelectorMap[] > + // > + // Accordingly, we allocate: > + // sizeof(*mCpuHotEjectData) + (MaxNumberOfCpus * > + // sizeof(mCpuHotEjectData->QemuSelectorMap[0])). > + // Add sizeof(UINT64) to use as padding if needed. > + // > + > + if (RETURN_ERROR (SafeUintnMult (sizeof (*mCpuHotEjectData), 1, &BaseLen)) || (2) This "multiplication" is somewhat awkward. First, BaseLen is a UINTN, which always matches the return type of the sizeof operator, so a direct assignment would be OK. But even that would be overkill -- I suggest removing the BaseLen variable, and just using "sizeof (*mCpuHotEjectData)" in its place. > + RETURN_ERROR (SafeUintnMult ( > + sizeof (mCpuHotEjectData->QemuSelectorMap[0]), > + MaxNumberOfCpus, &ArrayLen)) || > + RETURN_ERROR (SafeUintnAdd (BaseLen, ArrayLen, &TotalLen))|| (3) Missing space character before "||" > + RETURN_ERROR (SafeUintnAdd (TotalLen, sizeof (UINT64), &TotalLen))) { (4) So, I find the mixture of sizeof (UINT64) and sizeof (mCpuHotEjectData->QemuSelectorMap[0]) confusing. (And, this remark applies to both the code and the (otherwise very helpful!) comment above it.) We use sizeof (UINT64) *because* that's the type of "mCpuHotEjectData->QemuSelectorMap[0]" -- we want to ensure natural alignment for the atomic accesses performed later. So, given both kinds of sizeof expression stand for the same concept, we should use either "sizeof (UINT64)" exclusively, or "sizeof (mCpuHotEjectData->QemuSelectorMap[0])", exclusively. I would vote "sizeof (UINT64)" here, because that could help us eliminate some of the unpleasant line breaks. (5) Not sure if I should obsess about this, but... an alignment to UINT64 may require up to (sizeof (UINT64) - 1) padding bytes. (sizeof (UINT64)) bytes are never needed, as that would imply the allocation is already correctly aligned -- but then we'd need 0 additional bytes. (6) So how about this formulation instead: if (RETURN_ERROR (SafeUintnMult (MaxNumberOfCpus, sizeof (UINT64), &Size)) || RETURN_ERROR (SafeUintnAdd (Size, sizeof (*mCpuHotEjectData), &Size)) || RETURN_ERROR (SafeUintnAdd (Size, sizeof (UINT64) - 1, &Size)) { ... } The longest line (the first line) is 79 characters wide, and we only use one variable (called "Size"). If you don't like this variant, that's OK; but please at least unify the sizeof expressions (4). > + DEBUG ((DEBUG_ERROR, "%a: invalid CPU_HOT_EJECT_DATA\n", __FUNCTION__)); > + goto Fatal; > + } > + > + mCpuHotEjectData = AllocatePool (TotalLen); > + if (mCpuHotEjectData == NULL) { > + ASSERT (mCpuHotEjectData != NULL); > + goto Fatal; > + } > + > + mCpuHotEjectData->Handler = NULL; > + mCpuHotEjectData->ArrayLength = MaxNumberOfCpus; > + > + mCpuHotEjectData->QemuSelectorMap = (void *)mCpuHotEjectData + > + sizeof (*mCpuHotEjectData); > + mCpuHotEjectData->QemuSelectorMap = > + (void *)ALIGN_VALUE ((UINTN)mCpuHotEjectData->QemuSelectorMap, > + sizeof (UINT64)); (7) More idiomatic formulation, for setting the "QemuSelectorMap" field: mCpuHotEjectData->QemuSelectorMap = ALIGN_POINTER (mCpuHotEjectData + 1, sizeof (UINT64)); > + // > + // We use mCpuHotEjectData->QemuSelectorMap to map > + // ProcessorNum -> QemuSelector. Initialize to invalid values. > + // > + for (Idx = 0; Idx < mCpuHotEjectData->ArrayLength; Idx++) { > + mCpuHotEjectData->QemuSelectorMap[Idx] = CPU_EJECT_QEMU_SELECTOR_INVALID; > + } > + > + // > + // Expose address of CPU Hot eject Data structure > + // > + PcdStatus = PcdSet64S (PcdCpuHotEjectDataAddress, > + (UINTN)(VOID *)mCpuHotEjectData); > + if (RETURN_ERROR (PcdStatus)) { > + ASSERT_EFI_ERROR (PcdStatus); (8) To be fully clean semantically, this should be ASSERT_RETURN_ERROR(). ... Sorry about the bike-shedding; the purpose is two-fold: - the new code should feel idiomatic, - I hope you're going to stick around for further edk2 / OVMF development, and then these style hints shouldn't be useless in the long run. Thanks! Laszlo > + goto Fatal; > + } > + > + return; > + > +Fatal: > + CpuDeadLoop (); > +} > + > /** > Hook point in normal execution mode that allows the one CPU that was elected > as monarch during System Management Mode initialization to perform additional > @@ -188,6 +277,9 @@ SmmCpuFeaturesSmmRelocationComplete ( > UINTN MapPagesBase; > UINTN MapPagesCount; > > + > + InitCpuHotEjectData (); > + > if (!MemEncryptSevIsEnabled ()) { > return; > } >