From patchwork Thu Aug 31 13:54:51 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 111449 Delivered-To: patch@linaro.org Received: by 10.140.95.112 with SMTP id h103csp140418qge; Thu, 31 Aug 2017 15:12:01 -0700 (PDT) X-Google-Smtp-Source: ADKCNb5iQQSbfek3IQ+En1QecBJw7gmp+opsmaFEC3wYVK9vCwMdUWk+tTm2KZVDsmm2uLr5pRdb X-Received: by 10.55.76.135 with SMTP id z129mr6230577qka.264.1504217521489; Thu, 31 Aug 2017 15:12:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1504217521; cv=none; d=google.com; s=arc-20160816; b=l5meH2CL4pmc9AUvR+v9m9BiRBX7IXM6xptNw00qZ6wT6ifZ3s0AkL/KJFRIhTGy1f p7yEEwPsjJ3Mz4vY1oaNFuteZYirHuP7tp4ottKuca4spRXhZWoMZ9ijRNOnnwwmpOSM 2Is7YgVAWJn5lsMyB+KHtaIE2djehVKeP5UiL9mHfwcmDNj1R2OCwpeinSbvjxbcKjGW IIqcA8TKv4zOHKZ6vKhCkpmsUE9F2vH+StF8PkdoP4GIY3qsQrlW+NXEEnSc08uzxb3z cPBN2EOmbWVzmfxFMC18K6F6xCbjyTVArRW5F+y155C3MTOZW4+RfLUFMhozXCvayupx YW5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:references:in-reply-to:message-id:date:subject :to:from:arc-authentication-results; bh=TR5iSuRoLpbSsfaL3JBwUROLGEaMkcF716ogxwWzWb0=; b=dqUBpkWrW/UAIet7zKJSkyhrgzxVZS+EEkurFZ45a3KRafFEo/tGZTSmFyU6xoXiG2 JTY1i0gLahYXmaKt1h7k+Qo+UmnReCSnnexEwvMeu2yl+RRlTFHRHuzKFPP8jqEVc5ww n699XCcW3hFcFnRdekvtnnumjoWjvlE965zLEIEo/TOHmjwHbxD7aFn0LCBfZqLKNNPU 2pjWID1zYfsyIHGAJ7yi9rjcTEEljSRmt9YCEHzESFyTWybhv2rGQU5cqAnxXMEw/rN6 6xe1qR67xOEUbSSrSXKHSI4bswXtl5laT8dY/mRbe1F/2BGJuyTGBGS0cZ2HPoaPjzUh HLgg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=grub-devel-bounces+patch=linaro.org@gnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id 11si751673qtp.509.2017.08.31.15.12.01 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 31 Aug 2017 15:12:01 -0700 (PDT) Received-SPF: pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; spf=pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=grub-devel-bounces+patch=linaro.org@gnu.org Received: from localhost ([::1]:57794 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dnXgt-0007VX-6w for patch@linaro.org; Thu, 31 Aug 2017 18:11:59 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59950) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dnXgh-0007TL-E0 for grub-devel@gnu.org; Thu, 31 Aug 2017 18:11:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dnXgd-0007LZ-FV for grub-devel@gnu.org; Thu, 31 Aug 2017 18:11:47 -0400 Received: from mx2.suse.de ([195.135.220.15]:51185 helo=mx1.suse.de) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dnXgd-0007Kq-6A for grub-devel@gnu.org; Thu, 31 Aug 2017 18:11:43 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id E25AFAF52; Thu, 31 Aug 2017 22:11:41 +0000 (UTC) From: Alexander Graf To: grub-devel@gnu.org Subject: [PATCH v6 2/2] efi: Free malloc regions on exit Date: Thu, 31 Aug 2017 15:54:51 +0200 Message-Id: <20170831135451.55703-3-agraf@suse.de> X-Mailer: git-send-email 2.12.3 In-Reply-To: <20170831135451.55703-1-agraf@suse.de> References: <20170831135451.55703-1-agraf@suse.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 195.135.220.15 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: The development of GNU GRUB Cc: Leif Lindholm , phcoder@gmail.com, mchang@suse.com, Daniel Kiper , elliott@hpe.com MIME-Version: 1.0 Errors-To: grub-devel-bounces+patch=linaro.org@gnu.org Sender: "Grub-devel" When we exit grub, we don't free all the memory that we allocated earlier for our heap region. This can cause problems with setups where you try to descend the boot order using "exit" entries, such as PXE -> HD boot scenarios. Signed-off-by: Alexander Graf --- v2 -> v3: - add comment explaining the number of regions - move nr of regions into a define - add warning if we exceed the number of freeable regions - reset region counter to 0 on fini v3 -> v4: - use dynamic list instead of static array at runtime - use allocate_pool for list, so we are not bound by heap or random numbers - remember all allocations, not just the heap v4 -> v5: - free dynamic list entries on allocation removal v5 -> v6: - move next ptr to last field - s/start_addr/address/ - s/grub_efi_unremember_pages()/grub_efi_drop_alloc()/ - s/grub_efi_remember_pages()/grub_efi_store_alloc()/ - move grub_efi_drop_alloc after grub_efi_store_alloc - s/grub2/GRUB/ - Make grub_efi_memory_fini comment more verbose - Drop useless efi_allocated_memory = NULL; line --- grub-core/kern/efi/init.c | 1 + grub-core/kern/efi/mm.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++ include/grub/efi/efi.h | 1 + 3 files changed, 92 insertions(+) -- 2.12.3 _______________________________________________ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel diff --git a/grub-core/kern/efi/init.c b/grub-core/kern/efi/init.c index 2c31847bf..3dfdf2d22 100644 --- a/grub-core/kern/efi/init.c +++ b/grub-core/kern/efi/init.c @@ -80,4 +80,5 @@ grub_efi_fini (void) { grub_efidisk_fini (); grub_console_fini (); + grub_efi_memory_fini (); } diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c index ac2a4c556..92579a3b4 100644 --- a/grub-core/kern/efi/mm.c +++ b/grub-core/kern/efi/mm.c @@ -49,6 +49,80 @@ static grub_efi_uintn_t finish_desc_size; static grub_efi_uint32_t finish_desc_version; int grub_efi_is_finished = 0; +/* + * We need to roll back EFI allocations on exit. Remember allocations that + * we'll free on exit. + */ +struct efi_allocation; +struct efi_allocation { + grub_efi_physical_address_t address; + grub_efi_uint64_t pages; + struct efi_allocation *next; +}; +static struct efi_allocation *efi_allocated_memory; + +static void +grub_efi_store_alloc (grub_efi_physical_address_t address, + grub_efi_uintn_t pages) +{ + grub_efi_boot_services_t *b; + struct efi_allocation *alloc; + grub_efi_status_t status; + + b = grub_efi_system_table->boot_services; + status = efi_call_3 (b->allocate_pool, GRUB_EFI_LOADER_DATA, + sizeof(*alloc), (void**)&alloc); + if (status == GRUB_EFI_SUCCESS) + { + alloc->next = efi_allocated_memory; + alloc->address = address; + alloc->pages = pages; + efi_allocated_memory = alloc; + } + else + grub_printf ("Could not malloc memory to remember EFI allocation. " + "Exiting GRUB won't free all memory.\n"); +} + +static void +grub_efi_drop_alloc (grub_efi_physical_address_t address, + grub_efi_uintn_t pages) +{ + struct efi_allocation **allocp; + grub_efi_boot_services_t *b; + + b = grub_efi_system_table->boot_services; + + for (allocp = &efi_allocated_memory; *allocp;) + { + struct efi_allocation *alloc; + struct efi_allocation *next; + + alloc = *allocp; + + if (alloc->address != address || + alloc->pages != pages) + { + /* Move on to the next entry */ + allocp = &alloc->next; + + continue; + } + + /* Remember the next entry */ + next = alloc->next; + + /* Free the current list entry */ + efi_call_1 (b->free_pool, alloc); + + /* Remove from list */ + *allocp = next; + + /* Done */ + break; + } +} + /* Allocate pages. Return the pointer to the first of allocated pages. */ void * grub_efi_allocate_pages_real (grub_efi_physical_address_t address, @@ -79,6 +153,7 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address, return 0; } + grub_efi_store_alloc (address, pages); return (void *) ((grub_addr_t) address); } @@ -108,6 +183,7 @@ grub_efi_free_pages (grub_efi_physical_address_t address, b = grub_efi_system_table->boot_services; efi_call_2 (b->free_pages, address, pages); + grub_efi_drop_alloc (address, pages); } #if defined (__i386__) || defined (__x86_64__) @@ -422,6 +498,20 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map, grub_fatal ("too little memory"); } +void +grub_efi_memory_fini (void) +{ + /* + * Free all stale allocations. grub_efi_free_pages() will remove + * the found entry from the list and it will always find the first + * list entry (efi_allocated_memory is the list start). Hence we + * remove all entries from the list until none is left altogether. + */ + while (efi_allocated_memory) + grub_efi_free_pages (efi_allocated_memory->address, + efi_allocated_memory->pages); +} + #if 0 /* Print the memory map. */ static void diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h index 3fa082816..c996913e5 100644 --- a/include/grub/efi/efi.h +++ b/include/grub/efi/efi.h @@ -55,6 +55,7 @@ EXPORT_FUNC(grub_efi_get_memory_map) (grub_efi_uintn_t *memory_map_size, grub_efi_uintn_t *map_key, grub_efi_uintn_t *descriptor_size, grub_efi_uint32_t *descriptor_version); +void grub_efi_memory_fini (void); grub_efi_loaded_image_t *EXPORT_FUNC(grub_efi_get_loaded_image) (grub_efi_handle_t image_handle); void EXPORT_FUNC(grub_efi_print_device_path) (grub_efi_device_path_t *dp); char *EXPORT_FUNC(grub_efi_get_filename) (grub_efi_device_path_t *dp);