All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: The development of GNU GRUB <grub-devel@gnu.org>
Cc: Zhang Boyang <zhangboyang.id@gmail.com>
Subject: Re: [PATCH v4 2/2] mm: Preallocate some space when adding new regions
Date: Wed, 18 Jan 2023 08:14:13 +0100	[thread overview]
Message-ID: <Y8ecRd+71UpX+DgX@ncase> (raw)
In-Reply-To: <20230114132323.132210-3-zhangboyang.id@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2176 bytes --]

On Sat, Jan 14, 2023 at 09:23:23PM +0800, Zhang Boyang wrote:
> When grub_memalign() encounters out-of-memory, it will try
> grub_mm_add_region_fn() to request more memory from system firmware.
> However, it doesn't preallocate memory space for future allocation
> requests. In extreme cases, it requires one call to
> grub_mm_add_region_fn() for each memory allocation request. This can be
> very slow.
> 
> This patch introduces GRUB_MM_HEAP_GROW_EXTRA, the minimal heap growth
> granularity. The new region size is now set to the bigger one of its
> original value and GRUB_MM_HEAP_GROW_EXTRA. Thus, it will result in some
> memory space preallocated if current allocations request is small.
> 
> The value of GRUB_MM_HEAP_GROW_EXTRA is set to 1MB. If this value is
> smaller, the cost of small memory allocations will be higher. If this
> value is larger, more memory will be wasted and it might cause
> out-of-memory on machines with small amount of RAM.
> 
> Signed-off-by: Zhang Boyang <zhangboyang.id@gmail.com>
> ---
>  grub-core/kern/mm.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c
> index 278756dea..4e220bbff 100644
> --- a/grub-core/kern/mm.c
> +++ b/grub-core/kern/mm.c
> @@ -123,6 +123,9 @@
>  /* The size passed to grub_mm_add_region_fn() is aligned up by this value. */
>  #define GRUB_MM_HEAP_GROW_ALIGN	4096
>  
> +/* Minimal heap growth granularity when existing heap space is exhausted. */
> +#define GRUB_MM_HEAP_GROW_EXTRA	0x100000
> +
>  grub_mm_region_t grub_mm_base;
>  grub_mm_add_region_func_t grub_mm_add_region_fn;
>  
> @@ -471,6 +474,9 @@ grub_memalign (grub_size_t align, grub_size_t size)
>    if (grub_add (size + align, GRUB_MM_MGMT_OVERHEAD, &grow))
>      goto fail;
>  
> +  /* Preallocate some extra space if heap growth is small. */
> +  grow = grub_max (grow, GRUB_MM_HEAP_GROW_EXTRA);
> +
>    /* Align up heap growth to make it friendly to CPU/MMU. */
>    if (grow > ~(grub_size_t) (GRUB_MM_HEAP_GROW_ALIGN - 1))
>      goto fail;

    Reviewed-by: Patrick Steinhardt <ps@pks.im>

Thanks for both patches!

Patrick

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2023-01-18  7:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-14 13:23 [PATCH v4 0/2] mm: Better handling of adding new regions Zhang Boyang
2023-01-14 13:23 ` [PATCH v4 1/2] mm: Adjust new region size to take management overhead into account Zhang Boyang
2023-01-18  7:11   ` Patrick Steinhardt
2023-01-19 15:36   ` Daniel Kiper
2023-01-29 11:44     ` Zhang Boyang
2023-01-14 13:23 ` [PATCH v4 2/2] mm: Preallocate some space when adding new regions Zhang Boyang
2023-01-18  7:14   ` Patrick Steinhardt [this message]
2023-01-17 17:10 ` [PATCH v4 0/2] mm: Better handling of " 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=Y8ecRd+71UpX+DgX@ncase \
    --to=ps@pks.im \
    --cc=grub-devel@gnu.org \
    --cc=zhangboyang.id@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.