All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5 0/2] efi: Free memory on exit
@ 2017-08-29 19:26 Alexander Graf
  2017-08-29 19:26 ` [PATCH v5 1/2] efi: Move grub_reboot() into kernel Alexander Graf
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Alexander Graf @ 2017-08-29 19:26 UTC (permalink / raw)
  To: grub-devel; +Cc: elliott, mchang, Daniel Kiper, phcoder, Leif Lindholm

When exiting grub, we currently leave the heap allocated. This means that if we
exit and rerun grub, after a few iterations we can run into memory shortage.

This patch set frees the heap on exit. That way we can rerun grub many times
without much problem.

v3 -> v4:

  - Move grub_reboot to kern/efi/efi.c
  - 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

Alexander Graf (2):
  efi: Move grub_reboot() into kernel
  efi: Free malloc regions on exit

 grub-core/Makefile.core.def |  4 ---
 grub-core/kern/efi/efi.c    |  9 +++++
 grub-core/kern/efi/init.c   |  1 +
 grub-core/kern/efi/mm.c     | 88 +++++++++++++++++++++++++++++++++++++++++++++
 grub-core/lib/efi/reboot.c  | 33 -----------------
 include/grub/efi/efi.h      |  1 +
 include/grub/misc.h         |  3 +-
 7 files changed, 101 insertions(+), 38 deletions(-)
 delete mode 100644 grub-core/lib/efi/reboot.c

-- 
2.12.3



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v5 1/2] efi: Move grub_reboot() into kernel
  2017-08-29 19:26 [PATCH v5 0/2] efi: Free memory on exit Alexander Graf
@ 2017-08-29 19:26 ` Alexander Graf
  2017-08-30 11:14   ` Daniel Kiper
  2017-08-29 19:26 ` [PATCH v5 2/2] efi: Free malloc regions on exit Alexander Graf
  2017-08-29 22:05 ` [PATCH v5 0/2] efi: Free memory " Daniel Kiper
  2 siblings, 1 reply; 11+ messages in thread
From: Alexander Graf @ 2017-08-29 19:26 UTC (permalink / raw)
  To: grub-devel; +Cc: elliott, mchang, Daniel Kiper, phcoder, Leif Lindholm

The reboot function calls machine_fini() and then reboots the system.
Currently it lives in lib/ which means it gets compiled into the
reboot module which lives on the heap.

In a following patch, I want to free the heap on machine_fini()
though, so we would free the memory that the code is running in. That
obviously breaks with smarter UEFI implementations.

So this patch moves it into the core. That way we ensure that all
code running after machine_fini() in the UEFI case is running from
memory that got allocated (and gets deallocated) by the UEFI core.

Signed-off-by: Alexander Graf <agraf@suse.de>

---

v3 -> v4:

  - Move grub_reboot to kern/efi/efi.c
---
 grub-core/Makefile.core.def |  4 ----
 grub-core/kern/efi/efi.c    |  9 +++++++++
 grub-core/lib/efi/reboot.c  | 33 ---------------------------------
 include/grub/misc.h         |  3 ++-
 4 files changed, 11 insertions(+), 38 deletions(-)
 delete mode 100644 grub-core/lib/efi/reboot.c

diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index a65c27f7f..1cc4ce374 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -872,10 +872,6 @@ module = {
   name = reboot;
   i386 = lib/i386/reboot.c;
   i386 = lib/i386/reboot_trampoline.S;
-  ia64_efi = lib/efi/reboot.c;
-  x86_64_efi = lib/efi/reboot.c;
-  arm_efi = lib/efi/reboot.c;
-  arm64_efi = lib/efi/reboot.c;
   powerpc_ieee1275 = lib/ieee1275/reboot.c;
   sparc64_ieee1275 = lib/ieee1275/reboot.c;
   mips_arc = lib/mips/arc/reboot.c;
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
index d467785fc..708581fcb 100644
--- a/grub-core/kern/efi/efi.c
+++ b/grub-core/kern/efi/efi.c
@@ -155,6 +155,15 @@ grub_efi_get_loaded_image (grub_efi_handle_t image_handle)
 }
 
 void
+grub_reboot (void)
+{
+  grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
+  efi_call_4 (grub_efi_system_table->runtime_services->reset_system,
+              GRUB_EFI_RESET_COLD, GRUB_EFI_SUCCESS, 0, NULL);
+  for (;;) ;
+}
+
+void
 grub_exit (void)
 {
   grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
diff --git a/grub-core/lib/efi/reboot.c b/grub-core/lib/efi/reboot.c
deleted file mode 100644
index 7de8bcb5d..000000000
--- a/grub-core/lib/efi/reboot.c
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- *  GRUB  --  GRand Unified Bootloader
- *  Copyright (C) 2011  Free Software Foundation, Inc.
- *
- *  GRUB is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  GRUB is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <grub/efi/api.h>
-#include <grub/efi/efi.h>
-#include <grub/mm.h>
-#include <grub/misc.h>
-#include <grub/kernel.h>
-#include <grub/loader.h>
-
-void
-grub_reboot (void)
-{
-  grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
-  efi_call_4 (grub_efi_system_table->runtime_services->reset_system,
-              GRUB_EFI_RESET_COLD, GRUB_EFI_SUCCESS, 0, NULL);
-  for (;;) ;
-}
diff --git a/include/grub/misc.h b/include/grub/misc.h
index 2a9f87cc2..372f009e8 100644
--- a/include/grub/misc.h
+++ b/include/grub/misc.h
@@ -396,7 +396,8 @@ grub_abs (int x)
 }
 
 /* Reboot the machine.  */
-#if defined (GRUB_MACHINE_EMU) || defined (GRUB_MACHINE_QEMU_MIPS)
+#if defined (GRUB_MACHINE_EMU) || defined (GRUB_MACHINE_QEMU_MIPS) || \
+    defined (GRUB_MACHINE_EFI)
 void EXPORT_FUNC(grub_reboot) (void) __attribute__ ((noreturn));
 #else
 void grub_reboot (void) __attribute__ ((noreturn));
-- 
2.12.3



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v5 2/2] efi: Free malloc regions on exit
  2017-08-29 19:26 [PATCH v5 0/2] efi: Free memory on exit Alexander Graf
  2017-08-29 19:26 ` [PATCH v5 1/2] efi: Move grub_reboot() into kernel Alexander Graf
@ 2017-08-29 19:26 ` Alexander Graf
  2017-08-30 12:11   ` Daniel Kiper
  2017-08-29 22:05 ` [PATCH v5 0/2] efi: Free memory " Daniel Kiper
  2 siblings, 1 reply; 11+ messages in thread
From: Alexander Graf @ 2017-08-29 19:26 UTC (permalink / raw)
  To: grub-devel; +Cc: elliott, mchang, Daniel Kiper, phcoder, Leif Lindholm

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>

---

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
---
 grub-core/kern/efi/init.c |  1 +
 grub-core/kern/efi/mm.c   | 88 +++++++++++++++++++++++++++++++++++++++++++++++
 include/grub/efi/efi.h    |  1 +
 3 files changed, 90 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..c9bd3568d 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 {
+	struct efi_allocation *next;
+	grub_efi_physical_address_t start_addr;
+	grub_efi_uint64_t pages;
+};
+static struct efi_allocation *efi_allocated_memory;
+
+static void
+grub_efi_unremember_pages (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->start_addr != 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;
+    }
+}
+
+static void
+grub_efi_remember_pages (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->start_addr = address;
+      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");
+}
+
 /* 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_remember_pages (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_unremember_pages (address, pages);
 }
 
 #if defined (__i386__) || defined (__x86_64__)
@@ -422,6 +498,18 @@ 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 */
+
+  while (efi_allocated_memory)
+      grub_efi_free_pages (efi_allocated_memory->start_addr,
+                           efi_allocated_memory->pages);
+
+  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



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v5 0/2] efi: Free memory on exit
  2017-08-29 19:26 [PATCH v5 0/2] efi: Free memory on exit Alexander Graf
  2017-08-29 19:26 ` [PATCH v5 1/2] efi: Move grub_reboot() into kernel Alexander Graf
  2017-08-29 19:26 ` [PATCH v5 2/2] efi: Free malloc regions on exit Alexander Graf
@ 2017-08-29 22:05 ` Daniel Kiper
  2017-08-30  6:33   ` Alexander Graf
  2 siblings, 1 reply; 11+ messages in thread
From: Daniel Kiper @ 2017-08-29 22:05 UTC (permalink / raw)
  To: Alexander Graf
  Cc: grub-devel, elliott, mchang, Daniel Kiper, phcoder, Leif Lindholm

On Tue, Aug 29, 2017 at 09:26:46PM +0200, Alexander Graf wrote:
> When exiting grub, we currently leave the heap allocated. This means that if we
> exit and rerun grub, after a few iterations we can run into memory shortage.

Hmmm... Which one should I review? v3, v4, v5 or maybe I should wait for v6?

Daniel


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v5 0/2] efi: Free memory on exit
  2017-08-29 22:05 ` [PATCH v5 0/2] efi: Free memory " Daniel Kiper
@ 2017-08-30  6:33   ` Alexander Graf
  0 siblings, 0 replies; 11+ messages in thread
From: Alexander Graf @ 2017-08-30  6:33 UTC (permalink / raw)
  To: Daniel Kiper; +Cc: grub-devel, elliott, mchang, phcoder, Leif Lindholm



> Am 30.08.2017 um 00:05 schrieb Daniel Kiper <dkiper@net-space.pl>:
> 
>> On Tue, Aug 29, 2017 at 09:26:46PM +0200, Alexander Graf wrote:
>> When exiting grub, we currently leave the heap allocated. This means that if we
>> exit and rerun grub, after a few iterations we can run into memory shortage.
> 
> Hmmm... Which one should I review? v3, v4, v5 or maybe I should wait for v6?

v5 is good, please review that one.

Alex

> 
> Daniel



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v5 1/2] efi: Move grub_reboot() into kernel
  2017-08-29 19:26 ` [PATCH v5 1/2] efi: Move grub_reboot() into kernel Alexander Graf
@ 2017-08-30 11:14   ` Daniel Kiper
  0 siblings, 0 replies; 11+ messages in thread
From: Daniel Kiper @ 2017-08-30 11:14 UTC (permalink / raw)
  To: Alexander Graf
  Cc: grub-devel, elliott, mchang, Daniel Kiper, phcoder, Leif Lindholm

On Tue, Aug 29, 2017 at 09:26:47PM +0200, Alexander Graf wrote:
> The reboot function calls machine_fini() and then reboots the system.
> Currently it lives in lib/ which means it gets compiled into the
> reboot module which lives on the heap.
>
> In a following patch, I want to free the heap on machine_fini()
> though, so we would free the memory that the code is running in. That
> obviously breaks with smarter UEFI implementations.
>
> So this patch moves it into the core. That way we ensure that all
> code running after machine_fini() in the UEFI case is running from
> memory that got allocated (and gets deallocated) by the UEFI core.
>
> Signed-off-by: Alexander Graf <agraf@suse.de>

Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>

Daniel


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v5 2/2] efi: Free malloc regions on exit
  2017-08-29 19:26 ` [PATCH v5 2/2] efi: Free malloc regions on exit Alexander Graf
@ 2017-08-30 12:11   ` Daniel Kiper
  2017-08-30 13:03     ` Leif Lindholm
  2017-08-31 12:10     ` Alexander Graf
  0 siblings, 2 replies; 11+ messages in thread
From: Daniel Kiper @ 2017-08-30 12:11 UTC (permalink / raw)
  To: Alexander Graf
  Cc: grub-devel, elliott, mchang, Daniel Kiper, phcoder, Leif Lindholm

On Tue, Aug 29, 2017 at 09:26:48PM +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>
>
> ---
>
> 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
> ---
>  grub-core/kern/efi/init.c |  1 +
>  grub-core/kern/efi/mm.c   | 88 +++++++++++++++++++++++++++++++++++++++++++++++
>  include/grub/efi/efi.h    |  1 +
>  3 files changed, 90 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..c9bd3568d 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 {
> +	struct efi_allocation *next;

Please put this member as the last one.

> +	grub_efi_physical_address_t start_addr;

s/start_addr/address/

> +	grub_efi_uint64_t pages;
> +};
> +static struct efi_allocation *efi_allocated_memory;
> +
> +static void
> +grub_efi_unremember_pages (grub_efi_physical_address_t address,
> +                           grub_efi_uintn_t pages)

This function should be after grub_efi_remember_pages().

And maybe s/grub_efi_unremember_pages()/grub_efi_drop_alloc()/...

> +{
> +  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->start_addr != 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;
> +    }

Hmmm... This looks a bit complicated. Could you try this:

struct efi_allocation *ea, *eap;

for (eap = NULL, ea = efi_allocated_memory; ea; eap = ea, ea = ea->next)
  {
    if (ea->start_addr != address || ea->pages != pages)
       continue;

    if (eap)
      efi_allocated_memory = ea->next;
    else
      eap->next = ea->next;

    efi_call_1 (b->free_pool, ea);

    return;
  }

> +}
> +
> +static void
> +grub_efi_remember_pages (grub_efi_physical_address_t address,
> +                         grub_efi_uintn_t pages)

This function should be before grub_efi_unremember_pages().

And maybe s/grub_efi_remember_pages()/grub_efi_store_alloc()/...

> +{
> +  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->start_addr = address;
> +      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");

s/grub2/GRUB2/

> +}
> +
>  /* 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_remember_pages (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_unremember_pages (address, pages);
>  }
>
>  #if defined (__i386__) || defined (__x86_64__)
> @@ -422,6 +498,18 @@ 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 */
> +

Drop this empty line. And please improve the comment here. It took me
some time to understand why just "while (efi_allocated_memory)" works
here. grub_efi_free_pages() calls grub_efi_unremember_pages()
which advances the pointer.

> +  while (efi_allocated_memory)
> +      grub_efi_free_pages (efi_allocated_memory->start_addr,
> +                           efi_allocated_memory->pages);
> +
> +  efi_allocated_memory = NULL;

I have a feeling that you do not need this here.

Daniel


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v5 2/2] efi: Free malloc regions on exit
  2017-08-30 12:11   ` Daniel Kiper
@ 2017-08-30 13:03     ` Leif Lindholm
  2017-08-31 14:26       ` Daniel Kiper
  2017-08-31 12:10     ` Alexander Graf
  1 sibling, 1 reply; 11+ messages in thread
From: Leif Lindholm @ 2017-08-30 13:03 UTC (permalink / raw)
  To: Daniel Kiper; +Cc: Alexander Graf, grub-devel, elliott, mchang, phcoder

On Wed, Aug 30, 2017 at 02:11:59PM +0200, Daniel Kiper wrote:
> On Tue, Aug 29, 2017 at 09:26:48PM +0200, Alexander Graf wrote:
> > +{
> > +  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->start_addr = address;
> > +      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");
> 
> s/grub2/GRUB2/

Well, GRUB. Grub2 is a distro-ism.
We shouldn't have to update individual message strings when we bump
revisions.

/
    Leif


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v5 2/2] efi: Free malloc regions on exit
  2017-08-30 12:11   ` Daniel Kiper
  2017-08-30 13:03     ` Leif Lindholm
@ 2017-08-31 12:10     ` Alexander Graf
  2017-08-31 14:25       ` Daniel Kiper
  1 sibling, 1 reply; 11+ messages in thread
From: Alexander Graf @ 2017-08-31 12:10 UTC (permalink / raw)
  To: Daniel Kiper; +Cc: grub-devel, elliott, mchang, phcoder, Leif Lindholm

On 08/30/2017 02:11 PM, Daniel Kiper wrote:
> On Tue, Aug 29, 2017 at 09:26:48PM +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>
>>
>> ---
>>
>> 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
>> ---
>>   grub-core/kern/efi/init.c |  1 +
>>   grub-core/kern/efi/mm.c   | 88 +++++++++++++++++++++++++++++++++++++++++++++++
>>   include/grub/efi/efi.h    |  1 +
>>   3 files changed, 90 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..c9bd3568d 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 {
>> +	struct efi_allocation *next;
> Please put this member as the last one.

Sure, but care to explain why? Having the list chaining at the beginning 
of the struct is a very natural thing to do in pretty much every other 
project I've worked on.

>
>> +	grub_efi_physical_address_t start_addr;
> s/start_addr/address/
>
>> +	grub_efi_uint64_t pages;
>> +};
>> +static struct efi_allocation *efi_allocated_memory;
>> +
>> +static void
>> +grub_efi_unremember_pages (grub_efi_physical_address_t address,
>> +                           grub_efi_uintn_t pages)
> This function should be after grub_efi_remember_pages().
>
> And maybe s/grub_efi_unremember_pages()/grub_efi_drop_alloc()/...
Sure...

>
>> +{
>> +  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->start_addr != 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;
>> +    }
> Hmmm... This looks a bit complicated. Could you try this:
>
> struct efi_allocation *ea, *eap;
>
> for (eap = NULL, ea = efi_allocated_memory; ea; eap = ea, ea = ea->next)
>    {
>      if (ea->start_addr != address || ea->pages != pages)
>         continue;
>
>      if (eap)

I suppose you mean !eap here

>        efi_allocated_memory = ea->next;
>      else
>        eap->next = ea->next;
>
>      efi_call_1 (b->free_pool, ea);
>
>      return;
>    }

I'm not sure the version above is actually more readable. Do you really 
want me to move to that? It basically does the same thing, just with an 
explicit branch in between.

>
>> +}
>> +
>> +static void
>> +grub_efi_remember_pages (grub_efi_physical_address_t address,
>> +                         grub_efi_uintn_t pages)
> This function should be before grub_efi_unremember_pages().
>
> And maybe s/grub_efi_remember_pages()/grub_efi_store_alloc()/...

works for me :)

>
>> +{
>> +  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->start_addr = address;
>> +      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");
> s/grub2/GRUB2/
>
>> +}
>> +
>>   /* 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_remember_pages (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_unremember_pages (address, pages);
>>   }
>>
>>   #if defined (__i386__) || defined (__x86_64__)
>> @@ -422,6 +498,18 @@ 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 */
>> +
> Drop this empty line. And please improve the comment here. It took me
> some time to understand why just "while (efi_allocated_memory)" works
> here. grub_efi_free_pages() calls grub_efi_unremember_pages()
> which advances the pointer.

Does this work for you?

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);
}


>
>> +  while (efi_allocated_memory)
>> +      grub_efi_free_pages (efi_allocated_memory->start_addr,
>> +                           efi_allocated_memory->pages);
>> +
>> +  efi_allocated_memory = NULL;
> I have a feeling that you do not need this here.

You're right. It was only needed for the idx based, old version :)


Alex



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v5 2/2] efi: Free malloc regions on exit
  2017-08-31 12:10     ` Alexander Graf
@ 2017-08-31 14:25       ` Daniel Kiper
  0 siblings, 0 replies; 11+ messages in thread
From: Daniel Kiper @ 2017-08-31 14:25 UTC (permalink / raw)
  To: Alexander Graf
  Cc: Daniel Kiper, grub-devel, elliott, mchang, phcoder, Leif Lindholm

On Thu, Aug 31, 2017 at 02:10:48PM +0200, Alexander Graf wrote:
> On 08/30/2017 02:11 PM, Daniel Kiper wrote:
> >On Tue, Aug 29, 2017 at 09:26:48PM +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>
> >>
> >>---
> >>
> >>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
> >>---
> >>  grub-core/kern/efi/init.c |  1 +
> >>  grub-core/kern/efi/mm.c   | 88
> >>  +++++++++++++++++++++++++++++++++++++++++++++++
> >>  include/grub/efi/efi.h    |  1 +
> >>  3 files changed, 90 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..c9bd3568d 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 {
> >>+	struct efi_allocation *next;
> >Please put this member as the last one.
>
> Sure, but care to explain why? Having the list chaining at the beginning
> of the struct is a very natural thing to do in pretty much every other
> project I've worked on.

OK, let's leave it then.

> >>+	grub_efi_physical_address_t start_addr;
> >s/start_addr/address/
> >
> >>+	grub_efi_uint64_t pages;
> >>+};
> >>+static struct efi_allocation *efi_allocated_memory;
> >>+
> >>+static void
> >>+grub_efi_unremember_pages (grub_efi_physical_address_t address,
> >>+                           grub_efi_uintn_t pages)
> >This function should be after grub_efi_remember_pages().
> >
> >And maybe s/grub_efi_unremember_pages()/grub_efi_drop_alloc()/...
> Sure...
>
> >
> >>+{
> >>+  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->start_addr != 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;
> >>+    }
> >Hmmm... This looks a bit complicated. Could you try this:
> >
> >struct efi_allocation *ea, *eap;
> >
> >for (eap = NULL, ea = efi_allocated_memory; ea; eap = ea, ea = ea->next)
> >   {
> >     if (ea->start_addr != address || ea->pages != pages)
> >        continue;
> >
> >     if (eap)
>
> I suppose you mean !eap here

Yep, or even better:

if (eap)
  eap->next = ea->next;
else
  efi_allocated_memory = ea->next;

> >       efi_allocated_memory = ea->next;
> >     else
> >       eap->next = ea->next;
> >
> >     efi_call_1 (b->free_pool, ea);
> >
> >     return;
> >   }
>
> I'm not sure the version above is actually more readable. Do you really
> want me to move to that? It basically does the same thing, just with an

Yes, please.

> explicit branch in between.

...and lower number of pointers...

> >>+}
> >>+
> >>+static void
> >>+grub_efi_remember_pages (grub_efi_physical_address_t address,
> >>+                         grub_efi_uintn_t pages)
> >This function should be before grub_efi_unremember_pages().
> >
> >And maybe s/grub_efi_remember_pages()/grub_efi_store_alloc()/...
>
> works for me :)
>
> >
> >>+{
> >>+  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->start_addr = address;
> >>+      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");
> >s/grub2/GRUB2/
> >
> >>+}
> >>+
> >>  /* 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_remember_pages (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_unremember_pages (address, pages);
> >>  }
> >>
> >>  #if defined (__i386__) || defined (__x86_64__)
> >>@@ -422,6 +498,18 @@ 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 */
> >>+
> >Drop this empty line. And please improve the comment here. It took me
> >some time to understand why just "while (efi_allocated_memory)" works
> >here. grub_efi_free_pages() calls grub_efi_unremember_pages()
> >which advances the pointer.
>
> Does this work for you?
>
> 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);
> }

I am OK with this.

Daniel


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v5 2/2] efi: Free malloc regions on exit
  2017-08-30 13:03     ` Leif Lindholm
@ 2017-08-31 14:26       ` Daniel Kiper
  0 siblings, 0 replies; 11+ messages in thread
From: Daniel Kiper @ 2017-08-31 14:26 UTC (permalink / raw)
  To: Leif Lindholm
  Cc: Daniel Kiper, Alexander Graf, grub-devel, elliott, mchang, phcoder

On Wed, Aug 30, 2017 at 02:03:52PM +0100, Leif Lindholm wrote:
> On Wed, Aug 30, 2017 at 02:11:59PM +0200, Daniel Kiper wrote:
> > On Tue, Aug 29, 2017 at 09:26:48PM +0200, Alexander Graf wrote:
> > > +{
> > > +  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->start_addr = address;
> > > +      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");
> >
> > s/grub2/GRUB2/
>
> Well, GRUB. Grub2 is a distro-ism.
> We shouldn't have to update individual message strings when we bump
> revisions.

I am OK with GRUB too.

Daniel


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2017-08-31 14:26 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-29 19:26 [PATCH v5 0/2] efi: Free memory on exit Alexander Graf
2017-08-29 19:26 ` [PATCH v5 1/2] efi: Move grub_reboot() into kernel Alexander Graf
2017-08-30 11:14   ` Daniel Kiper
2017-08-29 19:26 ` [PATCH v5 2/2] efi: Free malloc regions on exit Alexander Graf
2017-08-30 12:11   ` Daniel Kiper
2017-08-30 13:03     ` Leif Lindholm
2017-08-31 14:26       ` Daniel Kiper
2017-08-31 12:10     ` Alexander Graf
2017-08-31 14:25       ` Daniel Kiper
2017-08-29 22:05 ` [PATCH v5 0/2] efi: Free memory " Daniel Kiper
2017-08-30  6:33   ` Alexander Graf

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.