Received: from list by lists.gnu.org with archive (Exim 4.71) id 1b6Ibu-0000ER-RM for mharc-grub-devel@gnu.org; Fri, 27 May 2016 10:19:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36300) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b6Ibf-0008UX-Fb for grub-devel@gnu.org; Fri, 27 May 2016 10:19:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b6IbT-0001fV-5b for grub-devel@gnu.org; Fri, 27 May 2016 10:19:19 -0400 Received: from mx2.suse.de ([195.135.220.15]:40478) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b6IbS-0001eY-Qv for grub-devel@gnu.org; Fri, 27 May 2016 10:19:07 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 4DB4DAC77; Fri, 27 May 2016 14:19:06 +0000 (UTC) From: Alexander Graf To: grub-devel@gnu.org Cc: elliott@hpe.com, mchang@suse.com, leif.lindholm@linaro.org Subject: [PATCH v2] efi: Free malloc regions on exit Date: Fri, 27 May 2016 16:19:07 +0200 Message-Id: <1464358747-231035-1-git-send-email-agraf@suse.de> X-Mailer: git-send-email 1.8.5.6 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] 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: , X-List-Received-Date: Fri, 27 May 2016 14:19:33 -0000 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 --- v1 -> v2: - 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 --- grub-core/kern/efi/init.c | 1 + grub-core/kern/efi/mm.c | 36 ++++++++++++++++++++++++++++++++++++ include/grub/efi/efi.h | 1 + 3 files changed, 38 insertions(+) diff --git a/grub-core/kern/efi/init.c b/grub-core/kern/efi/init.c index e9c85de..b848014 100644 --- a/grub-core/kern/efi/init.c +++ b/grub-core/kern/efi/init.c @@ -77,4 +77,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 20a47aa..6f1cb1e 100644 --- a/grub-core/kern/efi/mm.c +++ b/grub-core/kern/efi/mm.c @@ -49,6 +49,18 @@ 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 up to a randomly + * chosen 16 allocations that we'll free on exit. In most cases only one + * will be used. + */ +#define MAX_REVERSIBLE_EFI_ALLOCATIONS 16 +static struct efi_allocation { + grub_efi_physical_address_t start_addr; + grub_efi_uint64_t pages; +} efi_allocated_memory[MAX_REVERSIBLE_EFI_ALLOCATIONS]; +unsigned int efi_allocated_memory_idx = 0; + /* Allocate pages. Return the pointer to the first of allocated pages. */ void * grub_efi_allocate_pages (grub_efi_physical_address_t address, @@ -408,6 +420,17 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map, (void *) ((grub_addr_t) start), (unsigned) pages); + if (efi_allocated_memory_idx < ARRAY_SIZE(efi_allocated_memory)) { + efi_allocated_memory[efi_allocated_memory_idx].start_addr = start; + efi_allocated_memory[efi_allocated_memory_idx].pages = pages; + efi_allocated_memory_idx++; + + if (efi_allocated_memory_idx == ARRAY_SIZE(efi_allocated_memory)) { + grub_printf ("EFI memory allocations exceeded limit. Exiting grub2 " + "won't free all memory.\n"); + } + } + grub_mm_init_region (addr, PAGES_TO_BYTES (pages)); required_pages -= pages; @@ -419,6 +442,19 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map, grub_fatal ("too little memory"); } +void +grub_efi_memory_fini (void) +{ + unsigned int i; + + for (i = 0; i < efi_allocated_memory_idx; i++) { + grub_efi_free_pages (efi_allocated_memory[i].start_addr, + efi_allocated_memory[i].pages); + } + + efi_allocated_memory_idx = 0; +} + #if 0 /* Print the memory map. */ static void diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h index 0e6fd86..545e7ce 100644 --- a/include/grub/efi/efi.h +++ b/include/grub/efi/efi.h @@ -48,6 +48,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); -- 1.8.5.6