From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (NAM12-DM6-obe.outbound.protection.outlook.com [40.107.243.83]) by mx.groups.io with SMTP id smtpd.web12.272.1662565565676368669 for ; Wed, 07 Sep 2022 08:46:06 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@nvidia.com header.s=selector2 header.b=gfqRRhDb; spf=permerror, err=parse error for token &{10 18 %{i}._ip.%{h}._ehlo.%{d}._spf.vali.email}: invalid domain name (domain: nvidia.com, ip: 40.107.243.83, mailfrom: jbrasen@nvidia.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KnL9ac1NmVMxqQuaAEKb5Ik9UkhWlX6aF175zRnEDBaq8r7v5BxSrH+pMl9gGszN9vGqER5pmSNPCjm1pRarmbqcAVPWQIZRDZpspTD3Oxkmqed1sMmMsGEsrH0EChoNzio8JUpz7L2au0lRSln3hPo92dbd75gaEAt2UCEg0ZTIsfdug/iVAA03gzf1Pb5Z5qPC1gmfDYeNQLWtXgjnRAMxtNm4zffuFrXmhlyLcDzxcoiXZuWdVzprCUrF4o0FUOt8vJ0UE3pbZ4QVKmBAWGPVkZ5zKbXkYw1mFCNCgcTC41QpmvT71vjG+d79zepcQVMXCk0nPKtcSD48RNq3MA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=JLUZ7+DxXBXsaf7SutNl8n7LYhwpgFcuuRz/ikIFJw4=; b=NYR9qYshG47cQXc/ErvcNs3aEi9V0/ld3C8LH70cQg/9bz1YlvW6fOI/9Z7EtLGhAh4Gv/Xe7GNa86IedvC4EcZsbQLQuKFqMkvRPXWJEne+JNU2/2SwQIE4gDMOWa3546Q+w9xxnN6aNlpP3Uz+cRALC2xd2OWNyD2Z1n6MaqZ3JtZ+X+qNvjyaNKeZftDvE2Sv8SRp4V3JtlyjCV0fbSzmNn835OEG7rzAx7f8f7b23KVUotn5Bro5+zIFa/4tfVOi78RvjpwyIz4jZd5kLDZB2RTAAk4RQgXyiFZDYHAZUYjO98F02EjntJDJYLkBYKefsa3eWzMK+egOnpoxqA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.234) smtp.rcpttodomain=danielschaefer.me smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=JLUZ7+DxXBXsaf7SutNl8n7LYhwpgFcuuRz/ikIFJw4=; b=gfqRRhDb13NSNE4yCjf0iuds1qKuVWKFzEuO2FHIBJy+NA7fIYbv/J9bX7NOdE8U3TdykJWlm4f0c5LCPxD2D7vY1CB4VjZIGO8H46bq3VFwUgQGc8eYnj+dmj/lGEzSj8ZzCtQt46l6H8zdXSLqDTTEdJd+g/vCB7ctujoCu8uAREEumVkNzcAwtFx627hd4oUkuSu7Z4tThEE6bqJQ+71CYFHDSCE0jtPxuInEuFRpIkcmXQjA9fICT52lpE458tdX/l3tg6ekDrbro0knoDWVCorb47myPJEIEdvkQFmUG7XkCaQBix5qjCkMkx+/k1uEmaiXcBe8enajA9YKAA== Received: from DM5PR07CA0114.namprd07.prod.outlook.com (2603:10b6:4:ae::43) by PH8PR12MB7375.namprd12.prod.outlook.com (2603:10b6:510:215::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5588.15; Wed, 7 Sep 2022 15:46:03 +0000 Received: from DM6NAM11FT058.eop-nam11.prod.protection.outlook.com (2603:10b6:4:ae:cafe::c) by DM5PR07CA0114.outlook.office365.com (2603:10b6:4:ae::43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5612.14 via Frontend Transport; Wed, 7 Sep 2022 15:46:03 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.234) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.234 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.234; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.234) by DM6NAM11FT058.mail.protection.outlook.com (10.13.172.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5612.13 via Frontend Transport; Wed, 7 Sep 2022 15:46:02 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by DRHQMAIL101.nvidia.com (10.27.9.10) with Microsoft SMTP Server (TLS) id 15.0.1497.38; Wed, 7 Sep 2022 15:46:02 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.29; Wed, 7 Sep 2022 08:46:01 -0700 Received: from localhost.localdomain (10.127.8.10) by mail.nvidia.com (10.129.68.7) with Microsoft SMTP Server id 15.2.986.29 via Frontend Transport; Wed, 7 Sep 2022 08:46:01 -0700 From: "Jeff Brasen" To: CC: , , , , Jeff Brasen Subject: [PATCH v2] EmbeddedPkg/PrePiMemoryAllocationLib: Check for space on offset allocation Date: Wed, 7 Sep 2022 09:45:58 -0600 Message-ID: <3bbe3600b3c7012614a09aedb3543821b33f28c0.1662565486.git.jbrasen@nvidia.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-NVConfidentiality: public Return-Path: jbrasen@nvidia.com X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT058:EE_|PH8PR12MB7375:EE_ X-MS-Office365-Filtering-Correlation-Id: 42f30828-9b96-4cb4-0da9-08da90e811c1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Jd+R6x9jMY+sLUh6fDpOcNFgdbuA8HruipYojhjSfRGxiuGKzObiGg70KIIiTbt36qXYc/xkviSMgzGqVzDo8OFziwDMms0vNADWK2HO2B8o9iMJnVO664IHmx8qWH9wb8+uPfP5gBS+k9ETY5oucE/0FzYdsc0DUxfiwGV5+R9gOkdbwnPmHGApM0la0URfMR3Th0TWcHzESPNav/lNpyyM/D+wgBLETnBuyMvlnK5OZUOfNs33Px/7pg+akO1Sir918p5DdkzmqCg6M0yuMFZlIE5S7JFYvh/SF1wMudxQfO9BWqBfZn7Sl0VBkTCYAig7HDZVJJRixDH0E7RonOZCTIc8V9oa7Ee1ngjCMw2BHYorHYQ7jmj76ywhd1m0C7S1e86mCCbmANex37eQReb8hMINySz+/g8+SC+xxmhFNSSyzjGPnJcIGzJRVL/yebqzfaenAiPRiJLSbV5n7+NUPe12NxO6sIKRCk6O+qFtE1414tx5XB+0WWMfqpaaSOASCYiUa9PWeak9axhyEnOmTwAUIRBvR1zALOkf4bHupqzLkmdW0K9uH/4nXwnYHgKgIh2aSmaDFfzhyolVAoQpGv9YTdFBRvkMNwUIKpd3u9ia4OPA1andpiYMLhrp2G27PHPalTldPZcoYDa98NYKh1UOBtqzryQUNR4ysh8n0ch5Asd+orAXHwSO+JNjBQ/PXW+f4c7tey4PoJZh2EkQ9Q1v2m6XtwVfbGwHXucP8so944QYgpJnt+Odrnr4+ZGXqXNujQlCNUkpr6rh+f+N6MGZbYutAkBou1QKmpdglif12IJzCOVc2EeGUulC X-Forefront-Antispam-Report: CIP:12.22.5.234;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(6029001)(4636009)(346002)(396003)(376002)(39860400002)(136003)(46966006)(40470700004)(36840700001)(107886003)(41300700001)(186003)(6666004)(82740400003)(81166007)(478600001)(83380400001)(26005)(40460700003)(426003)(336012)(2616005)(356005)(47076005)(36860700001)(8676002)(70586007)(70206006)(4326008)(5660300002)(36756003)(40480700001)(2906002)(8936002)(82310400005)(316002)(86362001)(54906003)(6916009)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Sep 2022 15:46:02.9773 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 42f30828-9b96-4cb4-0da9-08da90e811c1 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.234];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT058.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB7375 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain Update check for enough space to occur prior to alignment offset=0D modification. This prevents a case where EfiFreeMemoryTop could be=0D less than EfiFreeMemoryBottom=0D =0D Signed-off-by: Jeff Brasen =0D ---=0D .../MemoryAllocationLib.c | 53 +++++++++++--------=0D 1 file changed, 30 insertions(+), 23 deletions(-)=0D =0D diff --git a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationL= ib.c b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c=0D index 2cc2a71121..9208826565 100644=0D --- a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c=0D +++ b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c=0D @@ -27,37 +27,44 @@ InternalAllocatePages (=0D =0D Hob.Raw =3D GetHobList ();=0D =0D - // Check to see if on 4k boundary=0D Offset =3D Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;=0D + //=0D + // Verify that there is sufficient memory to satisfy the allocation and = padding prior to updating anything=0D + //=0D + if ((Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_= SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) - Offset) < Hob.HandoffInformat= ionTable->EfiFreeMemoryBottom) {=0D + if (Offset !=3D 0) {=0D + DEBUG ((DEBUG_ERROR, "Offset applied without enough space\r\n"));=0D + } else {=0D + DEBUG ((DEBUG_ERROR, "Out of memory\r\n"));=0D + }=0D +=0D + ASSERT (FALSE);=0D + return 0;=0D + }=0D +=0D + // Check to see if on 4k boundary=0D if (Offset !=3D 0) {=0D // If not aligned, make the allocation aligned.=0D Hob.HandoffInformationTable->EfiFreeMemoryTop -=3D Offset;=0D }=0D =0D //=0D - // Verify that there is sufficient memory to satisfy the allocation=0D + // Update the PHIT to reflect the memory usage=0D //=0D - if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_S= IZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->E= fiFreeMemoryBottom) {=0D - return 0;=0D - } else {=0D - //=0D - // Update the PHIT to reflect the memory usage=0D - //=0D - Hob.HandoffInformationTable->EfiFreeMemoryTop -=3D Pages * EFI_PAGE_SI= ZE;=0D -=0D - // This routine used to create a memory allocation HOB a la PEI, but t= hat's not=0D - // necessary for us.=0D -=0D - //=0D - // Create a memory allocation HOB.=0D - //=0D - BuildMemoryAllocationHob (=0D - Hob.HandoffInformationTable->EfiFreeMemoryTop,=0D - Pages * EFI_PAGE_SIZE,=0D - MemoryType=0D - );=0D - return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;=0D - }=0D + Hob.HandoffInformationTable->EfiFreeMemoryTop -=3D Pages * EFI_PAGE_SIZE= ;=0D +=0D + // This routine used to create a memory allocation HOB a la PEI, but tha= t's not=0D + // necessary for us.=0D +=0D + //=0D + // Create a memory allocation HOB.=0D + //=0D + BuildMemoryAllocationHob (=0D + Hob.HandoffInformationTable->EfiFreeMemoryTop,=0D + Pages * EFI_PAGE_SIZE,=0D + MemoryType=0D + );=0D + return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;=0D }=0D =0D /**=0D -- =0D 2.25.1=0D =0D