From patchwork Sun Feb 8 16:51:13 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 44504 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-we0-f197.google.com (mail-we0-f197.google.com [74.125.82.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id DAA512153F for ; Sun, 8 Feb 2015 16:52:14 +0000 (UTC) Received: by mail-we0-f197.google.com with SMTP id l61sf15900143wev.0 for ; Sun, 08 Feb 2015 08:52:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:subject:precedence:reply-to:list-id:list-unsubscribe :list-archive:list-post:list-help:list-subscribe:mime-version :content-type:content-transfer-encoding:errors-to:x-original-sender :x-original-authentication-results:mailing-list; bh=nlIK4IotBSF6nAACl5Qhq9eIh8Hcym1l8vIYnA3Lu4U=; b=kvdkcCCZRpdsp52wSk83yea+DFXX37eXL6Hxfhe9Lgj7nr/mK9dNKIi57pjx9Aq43T bFfsb6RLB5f6MvTMo9OjIu/5488GLkeJZc6+wVNFycT7xRM9jZO2W5tFjVMeQG13vsv6 dz6X/C9AlJ0lKpXnEvCwjKZ1/ei2oPzAkHR6jcZkcDfyAA2cckcYZ18qgFjcs8EXYWmV 3Di9O6Xjj5l3JKwFWa+UR7IBozWeswi/eD4aCT0TZX2AiFl2vSlxWKAvcbgc6EfPn46k I8gK3HN03lS3oiBdcfx4r6tN7tTS6y3ayriZNtDpmTBumPmPUhfYRbQM9HAJyLy8V57n klZw== X-Gm-Message-State: ALoCoQkAtf08cgKxLubFGSE0fE5l46eiu9cm62uk4tSbig/ieXvS/9+6JAL0yfYyAO9tVzCbgwwi X-Received: by 10.194.142.174 with SMTP id rx14mr1454179wjb.4.1423414334200; Sun, 08 Feb 2015 08:52:14 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.43.113 with SMTP id v17ls481898lal.3.gmail; Sun, 08 Feb 2015 08:52:13 -0800 (PST) X-Received: by 10.152.9.2 with SMTP id v2mr12374990laa.67.1423414333964; Sun, 08 Feb 2015 08:52:13 -0800 (PST) Received: from mail-lb0-f172.google.com (mail-lb0-f172.google.com. [209.85.217.172]) by mx.google.com with ESMTPS id o1si6888867lal.131.2015.02.08.08.52.13 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 08 Feb 2015 08:52:13 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.172 as permitted sender) client-ip=209.85.217.172; Received: by mail-lb0-f172.google.com with SMTP id l4so25829682lbv.3 for ; Sun, 08 Feb 2015 08:52:13 -0800 (PST) X-Received: by 10.112.67.41 with SMTP id k9mr12857936lbt.35.1423414333786; Sun, 08 Feb 2015 08:52:13 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.35.133 with SMTP id h5csp2925299lbj; Sun, 8 Feb 2015 08:52:12 -0800 (PST) X-Received: by 10.107.30.69 with SMTP id e66mr21619457ioe.77.1423414331726; Sun, 08 Feb 2015 08:52:11 -0800 (PST) Received: from lists.sourceforge.net (lists.sourceforge.net. [216.34.181.88]) by mx.google.com with ESMTPS id il5si4827194igb.61.2015.02.08.08.52.11 (version=TLSv1 cipher=RC4-SHA bits=128/128); Sun, 08 Feb 2015 08:52:11 -0800 (PST) Received-SPF: pass (google.com: domain of edk2-devel-bounces@lists.sourceforge.net designates 216.34.181.88 as permitted sender) client-ip=216.34.181.88; Received: from localhost ([127.0.0.1] helo=sfs-ml-1.v29.ch3.sourceforge.com) by sfs-ml-1.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1YKV5N-0002tf-EO; Sun, 08 Feb 2015 16:51:53 +0000 Received: from sog-mx-2.v43.ch3.sourceforge.com ([172.29.43.192] helo=mx.sourceforge.net) by sfs-ml-1.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1YKV5L-0002tW-5K for edk2-devel@lists.sourceforge.net; Sun, 08 Feb 2015 16:51:51 +0000 Received-SPF: pass (sog-mx-2.v43.ch3.sourceforge.com: domain of linaro.org designates 209.85.220.44 as permitted sender) client-ip=209.85.220.44; envelope-from=ard.biesheuvel@linaro.org; helo=mail-pa0-f44.google.com; Received: from mail-pa0-f44.google.com ([209.85.220.44]) by sog-mx-2.v43.ch3.sourceforge.com with esmtps (TLSv1:RC4-SHA:128) (Exim 4.76) id 1YKV5J-0006rp-WC for edk2-devel@lists.sourceforge.net; Sun, 08 Feb 2015 16:51:51 +0000 Received: by mail-pa0-f44.google.com with SMTP id rd3so28436433pab.3 for ; Sun, 08 Feb 2015 08:51:44 -0800 (PST) X-Received: by 10.66.66.106 with SMTP id e10mr21410663pat.147.1423414304270; Sun, 08 Feb 2015 08:51:44 -0800 (PST) Received: from ards-macbook-pro.local ([113.28.134.59]) by mx.google.com with ESMTPSA id di5sm759152pbc.36.2015.02.08.08.51.41 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 08 Feb 2015 08:51:43 -0800 (PST) From: Ard Biesheuvel To: edk2-devel@lists.sourceforge.net, lersek@redhat.com, olivier.martin@arm.com, roy.franz@linaro.org, leif.lindholm@linaro.org, jordan.l.justen@intel.com, feng.tian@intel.com Date: Mon, 9 Feb 2015 00:51:13 +0800 Message-Id: <1423414278-8455-2-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1423414278-8455-1-git-send-email-ard.biesheuvel@linaro.org> References: <1423414278-8455-1-git-send-email-ard.biesheuvel@linaro.org> X-Spam-Score: -1.5 (-) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -1.5 SPF_CHECK_PASS SPF reports sender host as permitted sender for sender-domain -0.0 SPF_PASS SPF: sender matches SPF record X-Headers-End: 1YKV5J-0006rp-WC Subject: [edk2] [PATCH 1/6] MdeModulePkg: use correct granularity when allocating pool pages X-BeenThere: edk2-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list Reply-To: edk2-devel@lists.sourceforge.net List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.sourceforge.net X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ard.biesheuvel@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.172 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 After fixing the sanity check on the alignment of the runtime regions in SVN revision #16630 ("MdeModulePkg/DxeMain: Fix wrong sanity check in CoreTerminateMemoryMap()"), it is no longer possible to define a runtime allocation alignment that is different from the boot time allocation alignment. For instance, #defining the following in MdeModulePkg/Core/Dxe/Mem/Imem.h will hit the ASSERT () in MdeModulePkg/Core/Dxe/Mem/Page.c:1798 #define EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT (SIZE_64KB) #define DEFAULT_PAGE_ALLOCATION (EFI_PAGE_SIZE) (which is needed for 64-bit ARM to adhere to the Server Base Boot Requirements [SBBR], which stipulates that all runtime memory regions should be naturally aligned multiples of 64 KB) This patch fixes this use case by ensuring that the backing for the memory pools is allocated in appropriate chunks for the memory type. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ard Biesheuvel --- MdeModulePkg/Core/Dxe/Mem/Pool.c | 52 ++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Mem/Pool.c b/MdeModulePkg/Core/Dxe/Mem/Pool.c index a40ddd51bd13..f99293da5c51 100644 --- a/MdeModulePkg/Core/Dxe/Mem/Pool.c +++ b/MdeModulePkg/Core/Dxe/Mem/Pool.c @@ -274,9 +274,20 @@ CoreAllocatePoolI ( UINTN FSize; UINTN Offset; UINTN NoPages; + UINTN Granularity; ASSERT_LOCKED (&gMemoryLock); + if (PoolType == EfiACPIReclaimMemory || + PoolType == EfiACPIMemoryNVS || + PoolType == EfiRuntimeServicesCode || + PoolType == EfiRuntimeServicesData) { + + Granularity = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT; + } else { + Granularity = DEFAULT_PAGE_ALLOCATION; + } + // // Adjust the size by the pool header & tail overhead // @@ -301,9 +312,9 @@ CoreAllocatePoolI ( // (slow) // if (Index >= MAX_POOL_LIST) { - NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION) - 1; - NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION) - 1); - Head = CoreAllocatePoolPages (PoolType, NoPages, DEFAULT_PAGE_ALLOCATION); + NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (Granularity) - 1; + NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (Granularity) - 1); + Head = CoreAllocatePoolPages (PoolType, NoPages, Granularity); goto Done; } @@ -315,7 +326,7 @@ CoreAllocatePoolI ( // // Get another page // - NewPage = CoreAllocatePoolPages(PoolType, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION), DEFAULT_PAGE_ALLOCATION); + NewPage = CoreAllocatePoolPages(PoolType, EFI_SIZE_TO_PAGES (Granularity), Granularity); if (NewPage == NULL) { goto Done; } @@ -324,11 +335,11 @@ CoreAllocatePoolI ( // Carve up new page into free pool blocks // Offset = 0; - while (Offset < DEFAULT_PAGE_ALLOCATION) { + while (Offset < Granularity) { ASSERT (Index < MAX_POOL_LIST); FSize = LIST_TO_SIZE(Index); - while (Offset + FSize <= DEFAULT_PAGE_ALLOCATION) { + while (Offset + FSize <= Granularity) { Free = (POOL_FREE *) &NewPage[Offset]; Free->Signature = POOL_FREE_SIGNATURE; Free->Index = (UINT32)Index; @@ -339,7 +350,7 @@ CoreAllocatePoolI ( Index -= 1; } - ASSERT (Offset == DEFAULT_PAGE_ALLOCATION); + ASSERT (Offset == Granularity); Index = SIZE_TO_LIST(Size); } @@ -467,6 +478,7 @@ CoreFreePoolI ( UINTN FSize; UINTN Offset; BOOLEAN AllFree; + UINTN Granularity; ASSERT(Buffer != NULL); // @@ -508,6 +520,16 @@ CoreFreePoolI ( Pool->Used -= Size; DEBUG ((DEBUG_POOL, "FreePool: %p (len %lx) %,ld\n", Head->Data, (UINT64)(Head->Size - POOL_OVERHEAD), (UINT64) Pool->Used)); + if (Head->Type == EfiACPIReclaimMemory || + Head->Type == EfiACPIMemoryNVS || + Head->Type == EfiRuntimeServicesCode || + Head->Type == EfiRuntimeServicesData) { + + Granularity = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT; + } else { + Granularity = DEFAULT_PAGE_ALLOCATION; + } + // // Determine the pool list // @@ -522,8 +544,8 @@ CoreFreePoolI ( // // Return the memory pages back to free memory // - NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION) - 1; - NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION) - 1); + NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (Granularity) - 1; + NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (Granularity) - 1); CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN) Head, NoPages); } else { @@ -541,7 +563,7 @@ CoreFreePoolI ( // See if all the pool entries in the same page as Free are freed pool // entries // - NewPage = (CHAR8 *)((UINTN)Free & ~((DEFAULT_PAGE_ALLOCATION) -1)); + NewPage = (CHAR8 *)((UINTN)Free & ~(Granularity - 1)); Free = (POOL_FREE *) &NewPage[0]; ASSERT(Free != NULL); @@ -552,9 +574,9 @@ CoreFreePoolI ( AllFree = TRUE; Offset = 0; - while ((Offset < DEFAULT_PAGE_ALLOCATION) && (AllFree)) { + while ((Offset < Granularity) && (AllFree)) { FSize = LIST_TO_SIZE(Index); - while (Offset + FSize <= DEFAULT_PAGE_ALLOCATION) { + while (Offset + FSize <= Granularity) { Free = (POOL_FREE *) &NewPage[Offset]; ASSERT(Free != NULL); if (Free->Signature != POOL_FREE_SIGNATURE) { @@ -577,9 +599,9 @@ CoreFreePoolI ( Index = Free->Index; Offset = 0; - while (Offset < DEFAULT_PAGE_ALLOCATION) { + while (Offset < Granularity) { FSize = LIST_TO_SIZE(Index); - while (Offset + FSize <= DEFAULT_PAGE_ALLOCATION) { + while (Offset + FSize <= Granularity) { Free = (POOL_FREE *) &NewPage[Offset]; ASSERT(Free != NULL); RemoveEntryList (&Free->Link); @@ -591,7 +613,7 @@ CoreFreePoolI ( // // Free the page // - CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN)NewPage, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION)); + CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN)NewPage, EFI_SIZE_TO_PAGES (Granularity)); } } }