linux-efi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ardb@kernel.org>
To: linux-efi@vger.kernel.org, Ingo Molnar <mingo@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>
Cc: Ard Biesheuvel <ardb@kernel.org>,
	linux-kernel@vger.kernel.org,
	Arvind Sankar <nivedita@alum.mit.edu>,
	Benjamin Thiel <b.thiel@posteo.de>,
	Borislav Petkov <bp@alien8.de>, Dave Young <dyoung@redhat.com>,
	Heinrich Schuchardt <xypron.glpk@gmx.de>,
	Javier Martinez Canillas <javierm@redhat.com>,
	Jerry Snitselaar <jsnitsel@redhat.com>,
	Lenny Szubowicz <lszubowi@redhat.com>,
	linux-acpi@vger.kernel.org, Loic Yhuel <loic.yhuel@gmail.com>,
	Matthew Garrett <mjg59@google.com>,
	Mike Lothian <mike@fireburn.co.uk>,
	Punit Agrawal <punit1.agrawal@toshiba.co.jp>
Subject: [PATCH 3/7] efi/libstub/x86: Avoid EFI map buffer alloc in allocate_e820()
Date: Sun, 17 May 2020 14:57:50 +0200	[thread overview]
Message-ID: <20200517125754.8934-4-ardb@kernel.org> (raw)
In-Reply-To: <20200517125754.8934-1-ardb@kernel.org>

From: Lenny Szubowicz <lszubowi@redhat.com>

In allocate_e820(), call the EFI get_memory_map() service directly
instead of indirectly via efi_get_memory_map(). This avoids allocation
of a buffer and return of the full EFI memory map, which is not needed
here and would otherwise need to be freed.

Routine allocate_e820() only needs to know how many EFI memory
descriptors there are in the map to allocate an adequately sized
e820ext buffer, if it's needed. Note that since efi_get_memory_map()
returns a memory map buffer sized with extra headroom, allocate_e820()
now needs to explicitly factor that into the e820ext size calculation.

Signed-off-by: Lenny Szubowicz <lszubowi@redhat.com>
Suggested-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 drivers/firmware/efi/libstub/efistub.h  | 13 +++++++++++++
 drivers/firmware/efi/libstub/mem.c      |  2 --
 drivers/firmware/efi/libstub/x86-stub.c | 24 +++++++++---------------
 3 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
index 67d26949fd26..62943992f02f 100644
--- a/drivers/firmware/efi/libstub/efistub.h
+++ b/drivers/firmware/efi/libstub/efistub.h
@@ -92,6 +92,19 @@ extern __pure efi_system_table_t  *efi_system_table(void);
 #define EFI_LOCATE_BY_REGISTER_NOTIFY		1
 #define EFI_LOCATE_BY_PROTOCOL			2
 
+/*
+ * An efi_boot_memmap is used by efi_get_memory_map() to return the
+ * EFI memory map in a dynamically allocated buffer.
+ *
+ * The buffer allocated for the EFI memory map includes extra room for
+ * a minimum of EFI_MMAP_NR_SLACK_SLOTS additional EFI memory descriptors.
+ * This facilitates the reuse of the EFI memory map buffer when a second
+ * call to ExitBootServices() is needed because of intervening changes to
+ * the EFI memory map. Other related structures, e.g. x86 e820ext, need
+ * to factor in this headroom requirement as well.
+ */
+#define EFI_MMAP_NR_SLACK_SLOTS	8
+
 struct efi_boot_memmap {
 	efi_memory_desc_t	**map;
 	unsigned long		*map_size;
diff --git a/drivers/firmware/efi/libstub/mem.c b/drivers/firmware/efi/libstub/mem.c
index 869a79c8946f..09f4fa01914e 100644
--- a/drivers/firmware/efi/libstub/mem.c
+++ b/drivers/firmware/efi/libstub/mem.c
@@ -5,8 +5,6 @@
 
 #include "efistub.h"
 
-#define EFI_MMAP_NR_SLACK_SLOTS	8
-
 static inline bool mmap_has_headroom(unsigned long buff_size,
 				     unsigned long map_size,
 				     unsigned long desc_size)
diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c
index 05ccb229fb45..f0339b5d3658 100644
--- a/drivers/firmware/efi/libstub/x86-stub.c
+++ b/drivers/firmware/efi/libstub/x86-stub.c
@@ -606,24 +606,18 @@ static efi_status_t allocate_e820(struct boot_params *params,
 				  struct setup_data **e820ext,
 				  u32 *e820ext_size)
 {
-	unsigned long map_size, desc_size, buff_size;
-	struct efi_boot_memmap boot_map;
-	efi_memory_desc_t *map;
+	unsigned long map_size, desc_size, map_key;
 	efi_status_t status;
-	__u32 nr_desc;
+	__u32 nr_desc, desc_version;
 
-	boot_map.map		= &map;
-	boot_map.map_size	= &map_size;
-	boot_map.desc_size	= &desc_size;
-	boot_map.desc_ver	= NULL;
-	boot_map.key_ptr	= NULL;
-	boot_map.buff_size	= &buff_size;
+	/* Only need the size of the mem map and size of each mem descriptor */
+	map_size = 0;
+	status = efi_bs_call(get_memory_map, &map_size, NULL, &map_key,
+			     &desc_size, &desc_version);
+	if (status != EFI_BUFFER_TOO_SMALL)
+		return (status != EFI_SUCCESS) ? status : EFI_UNSUPPORTED;
 
-	status = efi_get_memory_map(&boot_map);
-	if (status != EFI_SUCCESS)
-		return status;
-
-	nr_desc = buff_size / desc_size;
+	nr_desc = map_size / desc_size + EFI_MMAP_NR_SLACK_SLOTS;
 
 	if (nr_desc > ARRAY_SIZE(params->e820_table)) {
 		u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
-- 
2.17.1


  parent reply	other threads:[~2020-05-17 12:58 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-17 12:57 [GIT PULL 0/7] EFI fixes for v5.7 Ard Biesheuvel
2020-05-17 12:57 ` [PATCH 1/7] efi/libstub: Avoid returning uninitialized data from setup_graphics() Ard Biesheuvel
2020-05-17 12:57 ` [PATCH 2/7] efi/earlycon: Fix early printk for wider fonts Ard Biesheuvel
2020-05-17 12:57 ` Ard Biesheuvel [this message]
2020-05-17 12:57 ` [PATCH 4/7] efi: cper: Add support for printing Firmware Error Record Reference Ard Biesheuvel
2020-05-17 12:57 ` [PATCH 5/7] x86/boot: Mark global variables as static Ard Biesheuvel
2020-05-17 12:57 ` [PATCH 6/7] efi: Pull up arch-specific prototype efi_systab_show_arch() Ard Biesheuvel
2020-05-17 12:57 ` [PATCH 7/7] tpm: check event log version before reading final events Ard Biesheuvel
2020-05-18  9:07 ` [GIT PULL 0/7] EFI fixes for v5.7 Borislav Petkov
2020-05-18  9:15   ` Ard Biesheuvel
2020-05-22 13:06 ` Ard Biesheuvel
2020-05-22 13:40   ` Borislav Petkov
2020-05-22 13:44     ` Ard Biesheuvel
2020-05-22 14:04       ` Thomas Gleixner
2020-05-22 14:45         ` Ard Biesheuvel

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=20200517125754.8934-4-ardb@kernel.org \
    --to=ardb@kernel.org \
    --cc=b.thiel@posteo.de \
    --cc=bp@alien8.de \
    --cc=dyoung@redhat.com \
    --cc=javierm@redhat.com \
    --cc=jsnitsel@redhat.com \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=loic.yhuel@gmail.com \
    --cc=lszubowi@redhat.com \
    --cc=mike@fireburn.co.uk \
    --cc=mingo@kernel.org \
    --cc=mjg59@google.com \
    --cc=nivedita@alum.mit.edu \
    --cc=punit1.agrawal@toshiba.co.jp \
    --cc=tglx@linutronix.de \
    --cc=xypron.glpk@gmx.de \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).