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 39330941D73 for ; Wed, 31 Jan 2024 19:29:43 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=yIrHKuno/bRrcAgeSCErifBDLWYxmvLTjaDiWJ5FLgI=; 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=1706729382; v=1; b=Qq3ErOzdUTsMQU0UJR57sJ1L2WXRJQrWszLObSK/w6uiVjZp9+zd/LAzVGJMUbdMq97csQhc I7nESmGw2qEJA3lzgl2tawg3+5zlkfOG6QV766tz6GIr9Fgvj5qV1noGD0jSaaf6o+JjJ7U+ybS g7Jws32Hm+emQgNQEgQV8PkA= X-Received: by 127.0.0.2 with SMTP id l7ehYY7687511xuqQfY83MwK; Wed, 31 Jan 2024 11:29:42 -0800 X-Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.groups.io with SMTP id smtpd.web10.4248.1706729381980000324 for ; Wed, 31 Jan 2024 11:29:42 -0800 X-Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-100-V4_5qUrLMfyJPfYLyayIUg-1; Wed, 31 Jan 2024 14:29:37 -0500 X-MC-Unique: V4_5qUrLMfyJPfYLyayIUg-1 X-Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 0EE69185A783; Wed, 31 Jan 2024 19:29:37 +0000 (UTC) X-Received: from [10.39.192.20] (unknown [10.39.192.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D8D102026D66; Wed, 31 Jan 2024 19:29:35 +0000 (UTC) Message-ID: <54eae7c0-0bfd-f464-1190-1d89d634884a@redhat.com> Date: Wed, 31 Jan 2024 20:29:34 +0100 MIME-Version: 1.0 Subject: Re: [edk2-devel] [PATCH 1/3] OvmfPkg/PlatformPei: consider AP stacks for pei memory cap To: Gerd Hoffmann Cc: devel@edk2.groups.io, Oliver Steffen , Jiewen Yao , Ard Biesheuvel References: <20240131120000.358090-1-kraxel@redhat.com> <20240131120000.358090-2-kraxel@redhat.com> <65a072d8-9f2c-3487-dc26-9a72e9eccc3d@redhat.com> From: "Laszlo Ersek" In-Reply-To: X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 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: ZrqFIjkCvb2QqI5hV3vFvRLxx7686176AA= 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=Qq3ErOzd; 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 15:55, Gerd Hoffmann wrote: > Hi, > >>> + 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 >> intentional that the code be dead on IA32, then that should be >> documented.) > > IA32 has an early exit in this function. > > Looking again I see this applies to 32-bit DXE only, so with mixed > 32-bit PEI / 64-bit DXE we can still land here. Hmm, so yes, you > have a point here. > > In practice we don't come even close to MAX_UINT32. With 4096 vcpus > the amount of memory needed for the AP stacks sums up to 128 MB. The > page table memory wouldn't grow that big too because OVMF will use at > most 1 TB of address space if gigabyte pages are not available (to > limit page table memory and boot time needed to set them all up). > >> Second, while returning MAX_UINT32 from this function is safe, it's >> also obscure. After the call site, we've got >> >> MemoryBase =3D PlatformInfoHob->S3Supported && PlatformInfoHob->SmmS= mramRequire ? >> PcdGet32 (PcdOvmfDecompressionScratchEnd) : >> PcdGet32 (PcdOvmfDxeMemFvBase) + PcdGet32 (PcdOvmfDxeMe= mFvSize); >> MemorySize =3D LowerMemorySize - MemoryBase; >> if (MemorySize > PeiMemoryCap) { >> MemoryBase =3D LowerMemorySize - PeiMemoryCap; >> MemorySize =3D PeiMemoryCap; >> } > > MemorySize is UINT64. So I guess the easiest would be to just switch > MemoryCap variable and GetPeiMemoryCap() return value to UINT64 too. > Avoids all the thinking and arguing whenever UINT32 is safe or not. I consider that a cop-out; I'd want to see evidence (or deduce the evidence myself) that UINT64 was safe, too. UINT64 is not "unlimited" either. In general, I don't want to do integer arithmetic without knowing the potential value ranges at all times. (1) It's great that we're discussing this, BTW -- an integer overflow is already possible (at least in theory) after the second patch in the series. Before the series, TotalPages is at most 0x40201, so EFI_PAGES_TO_SIZE(TotalPages) is at most ~1GB. We add SIZE_64MB to it; that still fits in the UINT32 return value. After the first patch in the series, a new increment is ApStacks; you mention it never exceeds 128 MB in practice. So that's safe (in practice). However, after the second patch in the series, that no longer suffices (in theory). As I calculated under patch#2, TotalPages can grow up to and including 0x8040201, which means EFI_PAGES_TO_SIZE(TotalPages) is ~513 GB. Meaning that, (1.1) on IA32X64, the following MemoryCap =3D EFI_PAGES_TO_SIZE (TotalPages) + ApStacks + SIZE_64MB; already truncates (because EFI_PAGES_TO_SIZE takes a UINTN and produces a UINTN, but UINTN is just UINT32); and that (1.2) on X64, while "MemoryCap" itself is fine, the final return (UINT32)(MemoryCap); will truncate. (2) Now, if we consider the 1TB address space limitation from PlatformAddressWidthFromCpuid() [OvmfPkg/Library/PlatformInitLib/MemDetect.c] that you highlight: if (!Page1GSupport && (PhysBits > 40)) { DEBUG ((DEBUG_INFO, "%a: limit PhysBits to 40 (no 1G pages available)= \n", __func__)); PhysBits =3D 40; } then we get (from the exact calculation): Level2Pages =3D BIT10; Level3Pages =3D BIT1; Level4Pages =3D 1; Level5Pages =3D 1; TotalPages =3D 1024 + 2 + 1 + 1; and then EFI_PAGES_TO_SIZE(1028) is just ~ 4 MB. So the UINT32 retval is safe, with the 1TB address space limitation in place -- but then we should ASSERT the predicate here that PlatformAddressWidthFromCpuid() ensures: ((PhysBits <=3D 40) || Page1GSupport)! (3) Finally, if Page1GSupport is TRUE, and PhysMemAddressWidth is 57, we get: Level2Pages =3D 0; Level3Pages =3D BIT18; Level4Pages =3D BIT9; Level5Pages =3D BIT0; which produces the pre-series value 0x40201 for TotalPages -- so that again will be safe for the UINT32 retval (the memory allocated for page tables will be again ~1GB). OK, summary: - before calculating "End" and "Level2Pages" in patch#2, we should ASSERT ((PhysBits <=3D 40) || Page1GSupport). - I guess it is fine to assume that (PcdCpuMaxLogicalProcessorNumber*PcdCpuApStackSize) will never exceed a few hundred MB. Maybe add a comment? - I suggest keeping the retval UINT32; there is no reason for widening it to UINT64 (thanks to the 1TB address space limitation, without 1G pages!) - The pre-series assertion that (TotalPages <=3D 0x40201) remains correct, after all, using the exact calculation (again due to the 1TB address space limitation, without 1G pages). > > And maybe add an else branch to log a warning in case > MemorySize < PeiMemoryCap. That is a good idea even with the unchanged UINT32 retval type! 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 (#114917): https://edk2.groups.io/g/devel/message/114917 Mute This Topic: https://groups.io/mt/104073297/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-