From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id B5D1A74003A for ; Wed, 31 Jan 2024 14:09:01 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=rVriKEveclqeWeTyMU5A/90AL27keWGAQbSpu2Bf8rQ=; c=relaxed/simple; d=groups.io; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From:In-Reply-To:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Language:Content-Type:Content-Transfer-Encoding; s=20140610; t=1706710140; v=1; b=uZI8kkjKUJ7e9+O57C9Bp8euTcHLzBurDXN4cwTE3iVRglq5cSSvnAfDlIvuQ9bUbB+ddNlI cB5pR+CMGc7Xbw88unxvF79GJH7J1EhggsRrRd2Rin+SdJW1h5k89V2C/LcxNv+r1FOO/+ht7Y2 EZY+gB8tBjFS18UxZ4Jp219g= X-Received: by 127.0.0.2 with SMTP id kSCpYY7687511xf36WO5LVgu; Wed, 31 Jan 2024 06:09:00 -0800 X-Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.groups.io with SMTP id smtpd.web11.14678.1706710139707922457 for ; Wed, 31 Jan 2024 06:08:59 -0800 X-Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-93-RrinXgP8PaWO9dGtsQBlwA-1; Wed, 31 Jan 2024 09:08:57 -0500 X-MC-Unique: RrinXgP8PaWO9dGtsQBlwA-1 X-Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E45AB1C07F23; Wed, 31 Jan 2024 14:08:56 +0000 (UTC) X-Received: from [10.39.192.35] (unknown [10.39.192.35]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 052C93C2E; Wed, 31 Jan 2024 14:08:55 +0000 (UTC) Message-ID: <65a072d8-9f2c-3487-dc26-9a72e9eccc3d@redhat.com> Date: Wed, 31 Jan 2024 15:08:54 +0100 MIME-Version: 1.0 Subject: Re: [edk2-devel] [PATCH 1/3] OvmfPkg/PlatformPei: consider AP stacks for pei memory cap To: devel@edk2.groups.io, kraxel@redhat.com Cc: Oliver Steffen , Jiewen Yao , Ard Biesheuvel References: <20240131120000.358090-1-kraxel@redhat.com> <20240131120000.358090-2-kraxel@redhat.com> From: "Laszlo Ersek" In-Reply-To: <20240131120000.358090-2-kraxel@redhat.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,lersek@redhat.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: wu2OgRNDQrxf7NJG2r5eBDqSx7686176AA= Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=uZI8kkjK; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=redhat.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io On 1/31/24 12:59, Gerd Hoffmann wrote: > Needed to avoid running out of memory when booting > with a large (~2048) number of vcpus. >=20 > Signed-off-by: Gerd Hoffmann > --- > OvmfPkg/PlatformPei/MemDetect.c | 10 +++++++++- > 1 file changed, 9 insertions(+), 1 deletion(-) This idea seems justified; it resembles commit 45d870815156 ("OvmfPkg/Platf= ormPei: rebase and resize the permanent PEI memory for S3", 2016-07-15), wh= ich also operates with the (PcdCpuMaxLogicalProcessorNumber * PcdCpuApStack= Size) product. OK. >=20 > diff --git a/OvmfPkg/PlatformPei/MemDetect.c b/OvmfPkg/PlatformPei/MemDet= ect.c > index 493cb1fbeb01..338798b54171 100644 > --- a/OvmfPkg/PlatformPei/MemDetect.c > +++ b/OvmfPkg/PlatformPei/MemDetect.c > @@ -187,6 +187,8 @@ GetPeiMemoryCap ( > UINT32 Pml4Entries; > UINT32 PdpEntries; > UINTN TotalPages; > + UINTN ApStacks; > + UINTN MemoryCap; > =20 > // > // If DXE is 32-bit, then just return the traditional 64 MB cap. > @@ -234,12 +236,18 @@ GetPeiMemoryCap ( > (PdpEntries + 1) * Pml4Entries + 1; > ASSERT (TotalPages <=3D 0x40201); > =20 > + ApStacks =3D PlatformInfoHob->PcdCpuMaxLogicalProcessorNumber * PcdGe= t32 (PcdCpuApStackSize); Doing this here is safe; we have the following call tree: InitializePlatform() [OvmfPkg/PlatformPei/Platform.c] MaxCpuCountInitialization() [OvmfPkg/PlatformPei/Platform.c] PlatformMaxCpuCountInitialization() [OvmfPkg/Library/PlatformInitLib/= Platform.c] PublishPeiMemory() [OvmfPkg/PlatformPei/MemDetect.c] GetPeiMemoryCap() [OvmfPkg/PlatformPei/MemDetect.c] Meaning that "PlatformInfoHob->PcdCpuMaxLogicalProcessorNumber" will be set= here. OK. > + MemoryCap =3D EFI_PAGES_TO_SIZE (TotalPages) + ApStacks + SIZE_64MB; (1) The comment at the bottom is now both misplaced, and out of date. Firs= t, we perform the addition here, not down there. Second, we now have an ext= ra addend, which may even dominate the cap. > + if (MemoryCap > MAX_UINT32) { > + MemoryCap =3D MAX_UINT32; > + } > + (2) This doesn't look good, for two reasons. First, if UINTN is UINT32, then we're too late to check. (If it's intention= al that the code be dead on IA32, then that should be documented.) Second, while returning MAX_UINT32 from this function is safe, it's also ob= scure. After the call site, we've got MemoryBase =3D PlatformInfoHob->S3Supported && PlatformInfoHob->SmmSmra= mRequire ? PcdGet32 (PcdOvmfDecompressionScratchEnd) : PcdGet32 (PcdOvmfDxeMemFvBase) + PcdGet32 (PcdOvmfDxeMemFv= Size); MemorySize =3D LowerMemorySize - MemoryBase; if (MemorySize > PeiMemoryCap) { MemoryBase =3D LowerMemorySize - PeiMemoryCap; MemorySize =3D PeiMemoryCap; } If PeiMemoryCap is huge (e.g., it approximates 4GB), then (MemorySize > Pei= MemoryCap) will evaluate to FALSE, and we'll just give most of the low RAM = to PEI. This effectively means that permanent PEI RAM is "uncapped" (while = remaining 32-bit only, of course). Whether that will cause a boot failure l= ater on, or not, remains to be seen, but as far as this code is affected, i= t's not a problem if GetPeiMemoryCap() returns MAX_UINT32. *However*, this = argument should be captured in a comment. > // > // Add 64 MB for miscellaneous allocations. Note that for > // PhysMemAddressWidth values close to 36, the cap will actually be > // dominated by this increment. > // > - return (UINT32)(EFI_PAGES_TO_SIZE (TotalPages) + SIZE_64MB); > + return (UINT32)(MemoryCap); > } > =20 > /** Thanks Laszlo -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#114902): https://edk2.groups.io/g/devel/message/114902 Mute This Topic: https://groups.io/mt/104073297/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/leave/12367111/7686176/19134562= 12/xyzzy [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-