All of lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Kiper <dkiper@net-space.pl>
To: Patrick Steinhardt <ps@pks.im>
Cc: grub-devel@gnu.org, Leif Lindholm <leif@nuviainc.com>,
	agraf@csgraf.de, pjones@redhat.com, mjg59@google.com,
	phcoder@gmail.com, Milan Broz <gmazyland@gmail.com>
Subject: Re: [PATCH v3 1/5] efi: Always try to allocate heap size of 1.6GB
Date: Fri, 13 Mar 2020 13:42:17 +0100	[thread overview]
Message-ID: <20200313124217.wzmyolxu7az2xtbu@tomti.i.net-space.pl> (raw)
In-Reply-To: <c783f34d70e8ac236ed8b29d3a31967c64b79a0e.1583866610.git.ps@pks.im>

On Tue, Mar 10, 2020 at 07:58:28PM +0100, Patrick Steinhardt wrote:
> By default, GRUB will allocate a quarter of the pages it got available
> in the EFI subsystem. On many current systems, this will amount to
> roughly 800MB of RAM assuming an address space of 32 bits. This is
> plenty for most use cases, but it doesn't suffice when using full disk
> encryption with a key derival function based on Argon2.
>
> Besides the usual iteration count known from PBKDF2, Argon2 introduces
> two additional parameters "memory" and "parallelism". While the latter
> doesn't really matter to us, the memory parameter is quite interesting.
> If encrypting a partition with LUKS2 using Argon2 as KDF, then
> cryptsetup will default to a memory parameter of 1GB. Meaning we need to
> allocate a buffer of 1GB in size in order to be able to derive the key,
> which definitely won't squeeze into the limit of 800MB.
>
> To prepare for Argon2, this commit reworks the memory allocation
> algorithm for EFI platforms. Instead of trying to allocate a quarter of
> memory available, let's instead introduce a constant target amount of
> bytes that we try to allocate. The target is set to the previous value
> of MAX_HEAP_SIZE, which amounts to 1.6GB and thus sufficiently high to
> accomodate for both Argon2 as well as other functionality. The value is
> then clamped to at most half of available memory but at least 100MB.

It seems to me that 0x100000 (MIN_HEAP_PAGES) equals to 1 MiB...

> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  grub-core/kern/efi/mm.c | 21 +++++++++++----------
>  1 file changed, 11 insertions(+), 10 deletions(-)
>
> diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
> index b02fab1b1..367a726c6 100644
> --- a/grub-core/kern/efi/mm.c
> +++ b/grub-core/kern/efi/mm.c
> @@ -39,8 +39,8 @@
>  #define MEMORY_MAP_SIZE	0x3000
>
>  /* The minimum and maximum heap size for GRUB itself.  */
> -#define MIN_HEAP_SIZE	0x100000
> -#define MAX_HEAP_SIZE	(1600 * 0x100000)
> +#define MIN_HEAP_PAGES		BYTES_TO_PAGES(       0x100000)
> +#define TARGET_HEAP_PAGES	BYTES_TO_PAGES(1600 * 0x100000)
>
>  static void *finish_mmap_buf = 0;
>  static grub_efi_uintn_t finish_mmap_size = 0;
> @@ -559,7 +559,7 @@ grub_efi_mm_init (void)
>    grub_efi_uintn_t map_size;
>    grub_efi_uintn_t desc_size;
>    grub_efi_uint64_t total_pages;
> -  grub_efi_uint64_t required_pages;
> +  grub_efi_uint64_t target_heap_pages;
>    int mm_status;
>
>    /* Prepare a memory region to store two memory maps.  */
> @@ -599,14 +599,15 @@ grub_efi_mm_init (void)
>    filtered_memory_map_end = filter_memory_map (memory_map, filtered_memory_map,
>  					       desc_size, memory_map_end);
>
> -  /* By default, request a quarter of the available memory.  */
> +  /* By default, request TARGET_HEAP_PAGES pages. If that exceeds half of meory
> +   * available, clamp it, but request at least MIN_HEAP_PAGES. */
>    total_pages = get_total_pages (filtered_memory_map, desc_size,
>  				 filtered_memory_map_end);
> -  required_pages = (total_pages >> 2);
> -  if (required_pages < BYTES_TO_PAGES (MIN_HEAP_SIZE))
> -    required_pages = BYTES_TO_PAGES (MIN_HEAP_SIZE);
> -  else if (required_pages > BYTES_TO_PAGES (MAX_HEAP_SIZE))
> -    required_pages = BYTES_TO_PAGES (MAX_HEAP_SIZE);
> +  target_heap_pages = TARGET_HEAP_PAGES;
> +  if (target_heap_pages > (total_pages / 2))
> +    target_heap_pages = total_pages / 2;
> +  if (target_heap_pages < MIN_HEAP_PAGES)
> +    target_heap_pages = MIN_HEAP_PAGES;

target_heap_pages = grub_min (total_pages / 2, TARGET_HEAP_PAGES);
target_heap_pages = grub_max (target_heap_pages, MIN_HEAP_PAGES);

Except that algorithm LGTM. Leif?

Daniel


  reply	other threads:[~2020-03-13 12:42 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-10 18:58 [PATCH v3 0/5] Support Argon2 KDF in LUKS2 Patrick Steinhardt
2020-03-10 18:58 ` [PATCH v3 1/5] efi: Always try to allocate heap size of 1.6GB Patrick Steinhardt
2020-03-13 12:42   ` Daniel Kiper [this message]
2020-03-13 12:55   ` Leif Lindholm
2020-03-13 13:59     ` Daniel Kiper
2020-03-15 14:01       ` Patrick Steinhardt
2020-03-15 10:14     ` Patrick Steinhardt
2020-03-15 10:41     ` Patrick Steinhardt
2020-03-10 18:58 ` [PATCH v3 2/5] types.h: add UINT-related macros needed for Argon2 Patrick Steinhardt
2020-03-13 12:49   ` Daniel Kiper
2020-03-10 18:58 ` [PATCH v3 3/5] argon2: Import Argon2 from cryptsetup Patrick Steinhardt
2020-03-10 20:44   ` Eli Schwartz
2020-03-10 21:42     ` Patrick Steinhardt
2020-03-13 13:13   ` Daniel Kiper
2020-03-16 17:21     ` Daniel Kiper
2020-03-16 17:52       ` Patrick Steinhardt
2020-03-16 20:03         ` Daniel Kiper
2020-03-17  5:51           ` Patrick Steinhardt
2020-03-17 10:45             ` Leif Lindholm
2020-03-16 19:57       ` Konrad Rzeszutek Wilk
2020-03-18 11:52         ` Patrick Steinhardt
2021-01-19  0:07   ` Petr Vorel
2021-01-19 11:10     ` Dmitry
2021-01-19 13:06       ` Petr Vorel
2021-01-19 15:42         ` Matt Turner
2021-01-19 19:03         ` Patrick Steinhardt
2021-01-19 19:31           ` Petr Vorel
2021-01-21 14:49             ` IS: GRUB release cycle: WAS: " Daniel Kiper
2021-01-21 21:30               ` Petr Vorel
2021-01-26 13:00                 ` Daniel Kiper
2021-01-26 19:57                   ` Petr Vorel
2021-08-08 13:58   ` [PATCH v4 0/5] Support Argon2 KDF in LUKS2 Patrick Steinhardt
2021-08-08 13:58     ` [PATCH v4 1/5] kern: dl: Allow modules under CC0 license Patrick Steinhardt
2021-08-08 15:03       ` Petr Vorel
2021-08-08 13:58     ` [PATCH v4 2/5] types.h: Add UINT-related macros needed for Argon2 Patrick Steinhardt
2021-08-08 13:58     ` [PATCH v4 3/5] argon2: Import reference implementation of Argon2 Patrick Steinhardt
2021-08-08 13:58     ` [PATCH v4 4/5] luks2: Discern Argon2i and Argon2id Patrick Steinhardt
2021-08-08 13:59     ` [PATCH v4 5/5] luks2: Support key derival via Argon2 Patrick Steinhardt
2020-03-10 18:58 ` [PATCH v3 4/5] luks2: Discern Argon2i and Argon2id Patrick Steinhardt
2020-03-10 18:58 ` [PATCH v3 5/5] luks2: Support key derival via Argon2 Patrick Steinhardt
2020-03-25 17:12 ` [PATCH v3 0/5] Support Argon2 KDF in LUKS2 Daniel Kiper

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=20200313124217.wzmyolxu7az2xtbu@tomti.i.net-space.pl \
    --to=dkiper@net-space.pl \
    --cc=agraf@csgraf.de \
    --cc=gmazyland@gmail.com \
    --cc=grub-devel@gnu.org \
    --cc=leif@nuviainc.com \
    --cc=mjg59@google.com \
    --cc=phcoder@gmail.com \
    --cc=pjones@redhat.com \
    --cc=ps@pks.im \
    /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.