From nobody Wed Apr 24 13:45:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+105012+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+105012+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1684395092; cv=none; d=zohomail.com; s=zohoarc; b=fsL2UaNEZtnnib4keYLhafFQRwmiqFCcUAb6r4CFQ9Bzs/PStWeQ9t3PUBompuh7G4XW/dkIV4qw4PLuFHlH1SC/vUkeo6nx93/ZThk7k7jnqTr8ZcCQcJ80ub7Hecrea4Z4l4elH/291b2vZoY1hMnyKcVP79yHl/8S8+b5pa4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1684395092; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=qliN6lkPaTvfF7imwblor8CHJHnZaYNA4spEncOdhDg=; b=FEkmwd++GDo+IlooL3JoW6Uvv2W9trxKauANbsicNUv7BBAt5fITkjna/NRcjniz1L+HMNLOk1lQE52EcWB5EkKeF7RjZUDWV8/6cvF9jL7g9Pr6FVhyB2aBXHLNyQ0HTT+Y6iSbGyaxJpqARpvmxnnzrsfruPKZo3cjxakgKOk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+105012+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1684395092590580.8440801791386; Thu, 18 May 2023 00:31:32 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id VLL0YY1788612xqVLvfQcdDT; Thu, 18 May 2023 00:31:32 -0700 X-Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by mx.groups.io with SMTP id smtpd.web11.11657.1684395091896332465 for ; Thu, 18 May 2023 00:31:31 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10713"; a="438337203" X-IronPort-AV: E=Sophos;i="5.99,284,1677571200"; d="scan'208";a="438337203" X-Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2023 00:31:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10713"; a="948590538" X-IronPort-AV: E=Sophos;i="5.99,284,1677571200"; d="scan'208";a="948590538" X-Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.158]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2023 00:31:10 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Ray Ni , Michael D Kinney , Liming Gao , Zhiguang Liu Subject: [edk2-devel] [Patch V2] MdePkg: Code optimization to SMM InternalAllocateAlignedPages Date: Thu, 18 May 2023 15:31:01 +0800 Message-Id: <20230518073101.1838-1-dun.tan@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: 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,dun.tan@intel.com X-Gm-Message-State: b5I9GySxIWmxebBEWDOlPqsMx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1684395092; bh=wPdErq3pOdBUxVdjrRZHRMdtqGQOLLbnaBmUr542uN4=; h=Cc:Date:From:Reply-To:Subject:To; b=L+0uJ3IzcQzC7I0iNOW0FEmCVfJZtQF4JUR535/ELIJEue5LE7lhpEdRxtxtBXJPOA5 qjN5wnwb3FRzLZoAqcgz+tyjM2iVJY1iBgPmgh80sLx/MfAvWh157LVHwpErwKDH1GOqX uPcW/0XPji8D9/b5YiX6zYADC/qTUIOUDMA= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1684395092799100001 Content-Type: text/plain; charset="utf-8" This commit is code optimization to InternalAllocateAlignedPages of SmmMemoryAllocationLib which can reduce free memory fragments. Also it can reduce one pre-allocation page. Let's take a simple example: The expected pages size is 8KB, Alignment value is 8KB. In original InternalAllocateAlignedPages(), the first step is to allocate 4 pages and then find the first 8KB-aligned address in allocated 4 pages. If the upper limit address of allocated 4 pages is already 8KB aligned, then the allocated 4 pages contains two 8KB-aligned 8KB ranges. The lower 2 pages will be selected and removed from free pages. Then the higher 2 pages will be free. Since the whole memory allocation is from high address to low address, then the higher 2 pages cann't be merged with other free pages, causing the free memory fragments. However, when only allocate 3(2+2-1) pages, we can avoid the free memory fragments in specific case. Also 3 pages must contain a 8KB-aligned 8KB range, which meets the requirement. If the upper limit address of allocated 3 pages is 8KB-aligned, then the higher 2 pages range of allocated 3 pages is 8KB-aligned and will be selected and removed from free pages. The remaining lower one page of allocated 3 pages will be free and merged with left lower free memory. This can reduce free memory fragments in smm. Signed-off-by: Dun Tan Cc: Ray Ni Cc: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu Reviewed-by: Ray Ni --- MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c b/= MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c index 3ab2c1ebfd..99a8259325 100644 --- a/MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c @@ -322,7 +322,7 @@ InternalAllocateAlignedPages ( // Calculate the total number of pages since alignment is larger than = page size. // AlignmentMask =3D Alignment - 1; - RealPages =3D Pages + EFI_SIZE_TO_PAGES (Alignment); + RealPages =3D Pages + EFI_SIZE_TO_PAGES (Alignment) - 1; // // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not ov= erflow. // --=20 2.31.1.windows.1 -=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 (#105012): https://edk2.groups.io/g/devel/message/105012 Mute This Topic: https://groups.io/mt/98986911/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-