All of lore.kernel.org
 help / color / mirror / Atom feed
From: Leif Lindholm <leif.lindholm@linaro.org>
To: The development of GNU GRUB <grub-devel@gnu.org>
Cc: phcoder@gmail.com, mchang@suse.com,
	Daniel Kiper <dkiper@net-space.pl>,
	elliott@hpe.com, agraf@suse.de
Subject: Re: [PATCH v3 2/2] efi: Free malloc regions on exit
Date: Tue, 29 Aug 2017 19:25:49 +0100	[thread overview]
Message-ID: <20170829182549.yc4wpdvbuvqzoayl@bivouac.eciton.net> (raw)
In-Reply-To: <20170829160035.11987-3-agraf@suse.de>

On Tue, Aug 29, 2017 at 06:00:35PM +0200, Alexander Graf wrote:
> 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 <agraf@suse.de>

Really happy to see this, but repeating a comment here from irc:
This deals with the biggest problem with returning to UEFI - GRUB
allocates 25% of available RAM, which before this patch we don't
release. However, various operations may allocate EFI memory directly,
and those allocations are not currently being tracked.

If further allocations could be tracked from
grub_efi_allocate_pages_real, these could be unwound in
_memory_fini(). Those can safely be allocated from the heap.

/
    Leif

> ---
> 
> 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
> 
> v2 -> v3:
> 
>   - use dynamic list instead of static array at runtime
>   - use allocate_pool for list, so we are not bound by heap or random numbers
> ---
>  grub-core/kern/efi/init.c |  1 +
>  grub-core/kern/efi/mm.c   | 59 +++++++++++++++++++++++++++++++++++++++++++++++
>  include/grub/efi/efi.h    |  1 +
>  3 files changed, 61 insertions(+)
> 
> 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..3455076e3 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 allocations that
> + * we'll free on exit. In most cases only one will be used.
> + */
> +struct efi_allocation;
> +struct efi_allocation {
> +	struct efi_allocation *next;
> +	grub_efi_physical_address_t start_addr;
> +	grub_efi_uint64_t pages;
> +};
> +static struct efi_allocation *efi_allocated_memory;
> +
>  /* Allocate pages. Return the pointer to the first of allocated pages.  */
>  void *
>  grub_efi_allocate_pages_real (grub_efi_physical_address_t address,
> @@ -386,14 +398,19 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
>  		    grub_efi_uint64_t required_pages)
>  {
>    grub_efi_memory_descriptor_t *desc;
> +  grub_efi_boot_services_t *b;
> +
> +  b = grub_efi_system_table->boot_services;
>  
>    for (desc = memory_map;
>         desc < memory_map_end;
>         desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
>      {
> +      struct efi_allocation *alloc;
>        grub_efi_uint64_t pages;
>        grub_efi_physical_address_t start;
>        void *addr;
> +      grub_efi_status_t status;
>  
>        start = desc->physical_start;
>        pages = desc->num_pages;
> @@ -411,6 +428,19 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
>  		    (void *) ((grub_addr_t) start),
>  		    (unsigned) pages);
>  
> +      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->start_addr = start;
> +          alloc->pages = pages;
> +          efi_allocated_memory = alloc;
> +        }
> +      else
> +          grub_printf ("Could not malloc memory to remember EFI allocation. "
> +                       "Exiting grub2 won't free all memory.\n");
> +
>        grub_mm_init_region (addr, PAGES_TO_BYTES (pages));
>  
>        required_pages -= pages;
> @@ -422,6 +452,35 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
>      grub_fatal ("too little memory");
>  }
>  
> +void
> +grub_efi_memory_fini (void)
> +{
> +  struct efi_allocation *alloc;
> +  grub_efi_boot_services_t *b;
> +
> +  b = grub_efi_system_table->boot_services;
> +
> +  for (alloc = efi_allocated_memory; alloc;)
> +    {
> +      struct efi_allocation *next;
> +
> +      /* Free the allocation */
> +      grub_efi_free_pages (alloc->start_addr,
> +                           alloc->pages);
> +
> +      /* Remember the next entry */
> +      next = alloc->next;
> +
> +      /* Free the current list entry */
> +      efi_call_1 (b->free_pool, alloc);
> +
> +      /* And move on to the next */
> +      alloc = next;
> +    }
> +
> +  efi_allocated_memory = NULL;
> +}
> +
>  #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);
> -- 
> 2.12.3
> 
> 
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel


  reply	other threads:[~2017-08-29 18:25 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-29 16:00 [PATCH v3 0/2] efi: Free memory on exit Alexander Graf
2017-08-29 16:00 ` [PATCH v3 1/2] efi: Move grub_reboot() into kernel Alexander Graf
2017-08-29 16:14   ` Leif Lindholm
2017-08-29 16:00 ` [PATCH v3 2/2] efi: Free malloc regions on exit Alexander Graf
2017-08-29 18:25   ` Leif Lindholm [this message]
2017-08-29 18:45     ` Alexander Graf
2017-08-29 19:05       ` Leif Lindholm

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170829182549.yc4wpdvbuvqzoayl@bivouac.eciton.net \
    --to=leif.lindholm@linaro.org \
    --cc=agraf@suse.de \
    --cc=dkiper@net-space.pl \
    --cc=elliott@hpe.com \
    --cc=grub-devel@gnu.org \
    --cc=mchang@suse.com \
    --cc=phcoder@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.