Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1ojd1u-0002zR-AO for mharc-grub-devel@gnu.org; Sat, 15 Oct 2022 05:00:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39060) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ojd1s-0002vz-Uw for grub-devel@gnu.org; Sat, 15 Oct 2022 05:00:24 -0400 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:58195) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ojd1n-0004JJ-6E for grub-devel@gnu.org; Sat, 15 Oct 2022 05:00:23 -0400 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 25B3D5C014E; Sat, 15 Oct 2022 05:00:18 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Sat, 15 Oct 2022 05:00:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to; s=fm3; t=1665824418; x=1665910818; bh=FzDnmkYJDo POqiAkITv49TOVT3vo5ENpONceJrV857U=; b=L+vHaKR7gj1H+5H6/i8df6++Gd 8EyYJIyeBRZU/AbPa22sBKP9i95H2uODuTrO41iV/wXtBGEQkA/4+OZ9NBKf2A+Y yT4Qz5rGVwt++PeFAI3hsMbUQEMqQEnw6rY4G1cmReafqIPh1wUOnVfKGv1tT/kk IavxYG7OwA3yGJqFzgSDlNKR2LhFarh7kpa95VzICuY1rN6Ci+PMNc6AanteAJG0 m3zazRJs7mZBOjiFXtgyiy0V2avC//BZalX6Qkr2DTopTIDASRCPshouo4JeZ3TU ldFkT/ppyTREhbAeEGkl7dSN7jjjOXYz4HUCHBsO4aB/czbvYT/W1EHQnxgg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:date:date:feedback-id :feedback-id:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:sender:subject:subject:to:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1665824418; x=1665910818; bh=FzDnmkYJDoPOqiAkITv49TOVT3vo 5ENpONceJrV857U=; b=Rhj5rRyQK14Pm8J206YHr8M8r4m0JJwMvExZPs8t6Q3G 2rTnE4EuZyx/aSJduMfTmV1zb4Hi+beSl/UuWQMcdo+ghWvYvMvQkB1uBsF19Tvf 3DvsrRXYC3/oAJ79Focb5Bx2wTQTemRSPksOMIFXR9rhPHEFQiQm3RQeSJmD9cHd ril+XgTBr1D0exRqoTRDagU/rzvPQLOTkE8eDFukuDnAqzB+cdkpwBVyAob4ACXi yCpsOPXlvFXn877ujLpIhqpZanLWwYYRRVKz1d38ZTgDPQMkLA6HsZVmGIPtysjv vA9QnIPeEa+XHM/2qGNvTWPgpULEUwtAOMb3YT9k+g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrfeekgedgudduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvvefukfhfgggtuggjsehgtd erredttddvnecuhfhrohhmpefrrghtrhhitghkucfuthgvihhnhhgrrhguthcuoehpshes phhkshdrihhmqeenucggtffrrghtthgvrhhnpeeukedtvedtffevleejtefgheehieegke eluddvfeefgeehgfeltddtheejleffteenucevlhhushhtvghrufhiiigvpedtnecurfgr rhgrmhepmhgrihhlfhhrohhmpehpshesphhkshdrihhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 15 Oct 2022 05:00:16 -0400 (EDT) Received: by pks.im (OpenSMTPD) with ESMTPSA id 641243f0 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sat, 15 Oct 2022 09:00:13 +0000 (UTC) Date: Sat, 15 Oct 2022 11:00:35 +0200 From: Patrick Steinhardt To: Zhang Boyang Cc: grub-devel@gnu.org, dkiper@net-space.pl, jim945@mail.ru, glin@suse.com, dja@axtens.net, droidbittin@gmail.com, heinrich.schuchardt@canonical.com, langner.marcel@myiq.de, marcan@marcan.st Subject: Re: [RFC PATCH v3 2/2] mm: Separate different types of allocations into different regions Message-ID: References: <20221013012919.173082-1-zhangboyang.id@gmail.com> <20221013012919.173082-3-zhangboyang.id@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="6JtSKpa3jUcbXIq5" Content-Disposition: inline In-Reply-To: <20221013012919.173082-3-zhangboyang.id@gmail.com> Received-SPF: pass client-ip=66.111.4.25; envelope-from=ps@pks.im; helo=out1-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 15 Oct 2022 09:00:25 -0000 --6JtSKpa3jUcbXIq5 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Oct 13, 2022 at 09:29:19AM +0800, Zhang Boyang wrote: > This patch add type infomation to heap regions. Currently there are four > types: GRUB_MM_SIZE_SMALL, GRUB_MM_SIZE_LARGE, GRUB_MM_CLASS_DATA, > GRUB_MM_CLASS_MODULE. Each heap region can have its own mask of > acceptable memory allocation types. >=20 > The GRUB_MM_SIZE_* types denotes the relative size of an allocation. > Allocations lesser than GRUB_MM_GROW_SMALL belong to GRUB_MM_SIZE_SMALL, > otherwise they belong to GRUB_MM_SIZE_LARGE. Thus small and large chunks > can be separated into different heaps. This might help reduce heap > fragmentation. >=20 > The GRUB_MM_CLASS_* types denotes whether an allocation is a module > segment. Module segments are allocated with GRUB_MM_CLASS_MODULE flag > set. Plain data chunks (and module metadata) are allocated with > GRUB_MM_CLASS_DATA flag set. Thus module segments and plain data chunks > can be separated into different heaps. >=20 > On platforms with dynamic heap growth support, when heap space is > exhausted, a new region will be allocated from firmware. The new region > has same type flags as the allocation request itself. The size of new > region will be (size + align + GRUB_MM_MAX_COST) for GRUB_MM_SIZE_LARGE > requests, and (GRUB_MM_GROW_SMALL + GRUB_MM_MAX_COST) for > GRUB_MM_SIZE_SMALL requests, with GRUB_MM_MAX_COST denotes the extra > region management cost. >=20 > On platforms without dynamic heap growth support, default heap will be > created with GRUB_MM_TYPE_ALL, so any type of allocation can be > fulfilled by default heap. I wonder whether we can again split this patch up into at least two parts: the first one that starts to round memory allocations, and then the second one to introduce memory types. The former would likely not be all that controversial. So by splitting this patch up we can already land the two patches to reorder disk cache invalidation and round requests, both of which should provide immediate benefit to users. The introduction of memory request types can then be discussed separately. Patrick > Signed-off-by: Zhang Boyang > --- > docs/grub-dev.texi | 3 +- > grub-core/kern/arm/coreboot/init.c | 4 +-- > grub-core/kern/dl.c | 2 +- > grub-core/kern/efi/mm.c | 12 ++++--- > grub-core/kern/i386/coreboot/init.c | 2 +- > grub-core/kern/i386/pc/init.c | 2 +- > grub-core/kern/i386/qemu/init.c | 2 +- > grub-core/kern/i386/xen/pvh.c | 2 +- > grub-core/kern/ieee1275/init.c | 4 +-- > grub-core/kern/main.c | 2 +- > grub-core/kern/mips/arc/init.c | 2 +- > grub-core/kern/mips/loongson/init.c | 2 +- > grub-core/kern/mips/qemu_mips/init.c | 2 +- > grub-core/kern/mm.c | 52 ++++++++++++++++++++++++---- > grub-core/kern/uboot/hw.c | 2 +- > grub-core/kern/xen/init.c | 2 +- > include/grub/mm.h | 23 ++++++++++-- > include/grub/mm_private.h | 5 ++- > util/misc.c | 3 +- > 19 files changed, 95 insertions(+), 33 deletions(-) >=20 > diff --git a/docs/grub-dev.texi b/docs/grub-dev.texi > index f76fc658b..2c27fad98 100644 > --- a/docs/grub-dev.texi > +++ b/docs/grub-dev.texi > @@ -818,7 +818,8 @@ If it works next stage is to have heap, console and t= imer. > =20 > To have the heap working you need to determine which regions are suitabl= e for > heap usage, allocate them from firmware and map (if applicable). Then ca= ll > -grub_mm_init_region (void *start, grub_size_t s) for every of this regio= n. > +grub_mm_init_region (void *addr, grub_size_t size, unsigned int types) f= or > +every of this region. > As a shortcut for early port you can allocate right after _end or have > a big static array for heap. If you do you'll probably need to come back= to > this later. As for output console you should distinguish between an arra= y of > diff --git a/grub-core/kern/arm/coreboot/init.c b/grub-core/kern/arm/core= boot/init.c > index 8d8c5b829..95bd45afd 100644 > --- a/grub-core/kern/arm/coreboot/init.c > +++ b/grub-core/kern/arm/coreboot/init.c > @@ -77,7 +77,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t size, grub= _memory_type_t type, > { > if (begin < (grub_addr_t)_start) > { > - grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) ((gr= ub_addr_t)_start - begin)); > + grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) ((gr= ub_addr_t)_start - begin), GRUB_MM_TYPE_ALL); > have_memory =3D 1; > } > begin =3D modend; > @@ -90,7 +90,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t size, grub= _memory_type_t type, > if (end <=3D begin) > return 0; > =20 > - grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) (end = - begin)); > + grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) (end = - begin), GRUB_MM_TYPE_ALL); > =20 > have_memory =3D 1; > =20 > diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c > index e447fd0fa..f11a23cc6 100644 > --- a/grub-core/kern/dl.c > +++ b/grub-core/kern/dl.c > @@ -256,7 +256,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr = *e) > #ifdef GRUB_MACHINE_EMU > mod->base =3D grub_osdep_dl_memalign (talign, tsize); > #else > - mod->base =3D grub_memalign (talign, tsize); > + mod->base =3D grub_module_memalign (talign, tsize); > #endif > if (!mod->base) > return grub_errno; > diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c > index 3705b8b1b..8218d721a 100644 > --- a/grub-core/kern/efi/mm.c > +++ b/grub-core/kern/efi/mm.c > @@ -483,7 +483,7 @@ add_memory_regions (grub_efi_memory_descriptor_t *mem= ory_map, > grub_efi_uintn_t desc_size, > grub_efi_memory_descriptor_t *memory_map_end, > grub_efi_uint64_t required_pages, > - unsigned int flags) > + unsigned int flags, unsigned int types) > { > grub_efi_memory_descriptor_t *desc; > =20 > @@ -515,7 +515,7 @@ add_memory_regions (grub_efi_memory_descriptor_t *mem= ory_map, > "Memory starting at %p (%u pages) marked as free, but EFI would n= ot allocate", > (void *) ((grub_addr_t) start), (unsigned) pages); > =20 > - grub_mm_init_region (addr, PAGES_TO_BYTES (pages)); > + grub_mm_init_region (addr, PAGES_TO_BYTES (pages), types); > =20 > required_pages -=3D pages; > if (required_pages =3D=3D 0) > @@ -566,7 +566,7 @@ print_memory_map (grub_efi_memory_descriptor_t *memor= y_map, > #endif > =20 > static grub_err_t > -grub_efi_mm_add_regions (grub_size_t required_bytes, unsigned int flags) > +grub_efi_mm_add_regions (grub_size_t required_bytes, unsigned int flags,= unsigned int types) > { > grub_efi_memory_descriptor_t *memory_map; > grub_efi_memory_descriptor_t *memory_map_end; > @@ -622,7 +622,7 @@ grub_efi_mm_add_regions (grub_size_t required_bytes, = unsigned int flags) > err =3D add_memory_regions (filtered_memory_map, desc_size, > filtered_memory_map_end, > BYTES_TO_PAGES (required_bytes), > - flags); > + flags, types); > if (err !=3D GRUB_ERR_NONE) > return err; > =20 > @@ -649,7 +649,9 @@ grub_efi_mm_add_regions (grub_size_t required_bytes, = unsigned int flags) > void > grub_efi_mm_init (void) > { > - if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE, GRUB_MM_ADD_REGION_NON= E) !=3D GRUB_ERR_NONE) > + if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE, > + GRUB_MM_ADD_REGION_NONE, > + GRUB_MM_SIZE_ALL | GRUB_MM_CLASS_DATA) !=3D GRUB_ERR_NONE) > grub_fatal ("%s", grub_errmsg); > grub_mm_add_region_fn =3D grub_efi_mm_add_regions; > } > diff --git a/grub-core/kern/i386/coreboot/init.c b/grub-core/kern/i386/co= reboot/init.c > index 4fae8b571..90b688304 100644 > --- a/grub-core/kern/i386/coreboot/init.c > +++ b/grub-core/kern/i386/coreboot/init.c > @@ -81,7 +81,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t size, grub= _memory_type_t type, > if (end <=3D begin) > return 0; > =20 > - grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) (end = - begin)); > + grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) (end = - begin), GRUB_MM_TYPE_ALL); > =20 > have_memory =3D 1; > =20 > diff --git a/grub-core/kern/i386/pc/init.c b/grub-core/kern/i386/pc/init.c > index 27bc68b8a..4ec26dcf1 100644 > --- a/grub-core/kern/i386/pc/init.c > +++ b/grub-core/kern/i386/pc/init.c > @@ -256,7 +256,7 @@ grub_machine_init (void) > beg =3D modend; > if (beg >=3D fin) > continue; > - grub_mm_init_region ((void *) beg, fin - beg); > + grub_mm_init_region ((void *) beg, fin - beg, GRUB_MM_TYPE_ALL); > } > =20 > grub_tsc_init (); > diff --git a/grub-core/kern/i386/qemu/init.c b/grub-core/kern/i386/qemu/i= nit.c > index 4e84d3ba1..c1d0f6aa3 100644 > --- a/grub-core/kern/i386/qemu/init.c > +++ b/grub-core/kern/i386/qemu/init.c > @@ -77,7 +77,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t size, grub= _memory_type_t type, > if (end <=3D begin) > return 0; > =20 > - grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) (end = - begin)); > + grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) (end = - begin), GRUB_MM_TYPE_ALL); > =20 > return 0; > } > diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c > index 91fbca859..01f11200e 100644 > --- a/grub-core/kern/i386/xen/pvh.c > +++ b/grub-core/kern/i386/xen/pvh.c > @@ -261,7 +261,7 @@ grub_xen_mm_init_regions (void) > continue; > if (to > (1ULL << 32)) > to =3D 1ULL << 32; > - grub_mm_init_region ((void *) (grub_addr_t) from, to - from); > + grub_mm_init_region ((void *) (grub_addr_t) from, to - from, GRUB_= MM_TYPE_ALL); > } > } > =20 > diff --git a/grub-core/kern/ieee1275/init.c b/grub-core/kern/ieee1275/ini= t.c > index 2adf4fdfc..cd8377f7f 100644 > --- a/grub-core/kern/ieee1275/init.c > +++ b/grub-core/kern/ieee1275/init.c > @@ -142,7 +142,7 @@ static void > grub_claim_heap (void) > { > grub_mm_init_region ((void *) (grub_modules_get_end () > - + GRUB_KERNEL_MACHINE_STACK_SIZE), 0x200000); > + + GRUB_KERNEL_MACHINE_STACK_SIZE), 0x200000, GRUB_MM_TYPE_ALL); > } > #else > /* Helper for grub_claim_heap. */ > @@ -190,7 +190,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t len, gru= b_memory_type_t type, > err =3D grub_claimmap (addr, len); > if (err) > return err; > - grub_mm_init_region ((void *) (grub_addr_t) addr, len); > + grub_mm_init_region ((void *) (grub_addr_t) addr, len, GRUB_MM_TYP= E_ALL); > } > =20 > *total +=3D len; > diff --git a/grub-core/kern/main.c b/grub-core/kern/main.c > index 731c07c29..068e48571 100644 > --- a/grub-core/kern/main.c > +++ b/grub-core/kern/main.c > @@ -254,7 +254,7 @@ reclaim_module_space (void) > grub_modbase =3D 0; > =20 > #if GRUB_KERNEL_PRELOAD_SPACE_REUSABLE > - grub_mm_init_region ((void *) modstart, modend - modstart); > + grub_mm_init_region ((void *) modstart, modend - modstart, GRUB_MM_TYP= E_ALL); > #else > (void) modstart; > (void) modend; > diff --git a/grub-core/kern/mips/arc/init.c b/grub-core/kern/mips/arc/ini= t.c > index 2ed3ff319..82b1012ce 100644 > --- a/grub-core/kern/mips/arc/init.c > +++ b/grub-core/kern/mips/arc/init.c > @@ -250,7 +250,7 @@ grub_machine_init (void) > end =3D 0x20000000; > if (end > start) > grub_mm_init_region ((void *) (grub_addr_t) (start | 0x80000000), > - end - start); > + end - start, GRUB_MM_TYPE_ALL); > } > =20 > grub_console_init_lately (); > diff --git a/grub-core/kern/mips/loongson/init.c b/grub-core/kern/mips/lo= ongson/init.c > index 5bd721260..269fa30f1 100644 > --- a/grub-core/kern/mips/loongson/init.c > +++ b/grub-core/kern/mips/loongson/init.c > @@ -202,7 +202,7 @@ grub_machine_init (void) > =20 > modend =3D grub_modules_get_end (); > grub_mm_init_region ((void *) modend, (grub_arch_memsize << 20) > - - (modend - GRUB_ARCH_LOWMEMVSTART)); > + - (modend - GRUB_ARCH_LOWMEMVSTART), GRUB_MM_TYPE_ALL); > /* FIXME: use upper memory as well. */ > =20 > /* Initialize output terminal (can't be done earlier, as gfxterm > diff --git a/grub-core/kern/mips/qemu_mips/init.c b/grub-core/kern/mips/q= emu_mips/init.c > index b5477b87f..5ed51f87e 100644 > --- a/grub-core/kern/mips/qemu_mips/init.c > +++ b/grub-core/kern/mips/qemu_mips/init.c > @@ -53,7 +53,7 @@ grub_machine_init (void) > =20 > modend =3D grub_modules_get_end (); > grub_mm_init_region ((void *) modend, grub_arch_memsize > - - (modend - GRUB_ARCH_LOWMEMVSTART)); > + - (modend - GRUB_ARCH_LOWMEMVSTART), GRUB_MM_TYPE_ALL); > =20 > grub_install_get_time_ms (grub_rtc_get_time_ms); > =20 > diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c > index ae2279133..602838ae1 100644 > --- a/grub-core/kern/mm.c > +++ b/grub-core/kern/mm.c > @@ -114,13 +114,13 @@ get_header_from_pointer (void *ptr, grub_mm_header_= t *p, grub_mm_region_t *r) > /* Initialize a region starting from ADDR and whose size is SIZE, > to use it as free space. */ > void > -grub_mm_init_region (void *addr, grub_size_t size) > +grub_mm_init_region (void *addr, grub_size_t size, unsigned int types) > { > grub_mm_header_t h; > grub_mm_region_t r, *p, q; > =20 > - grub_dprintf ("regions", "Using memory for heap: start=3D%p, end=3D%p\= n", > - addr, (char *) addr + (unsigned int) size); > + grub_dprintf ("regions", "Using memory for heap: start=3D%p, end=3D%p,= types=3D0x%x\n", > + addr, (char *) addr + (unsigned int) size, types); > =20 > /* Exclude last 4K to avoid overflows. */ > /* If addr + 0x1000 overflows then whole region is in excluded zone. = */ > @@ -134,6 +134,10 @@ grub_mm_init_region (void *addr, grub_size_t size) > /* Attempt to merge this region with every existing region */ > for (p =3D &grub_mm_base, q =3D *p; q; p =3D &(q->next), q =3D *p) > { > + /* Don't merge regions with different allocation types */ > + if (q->types !=3D types) > + continue; > + > /* > * Is the new region immediately below an existing region? That > * is, is the address of the memory we're adding now (addr) + size > @@ -248,6 +252,7 @@ grub_mm_init_region (void *addr, grub_size_t size) > r->pre_size =3D (grub_addr_t) r - (grub_addr_t) addr; > r->size =3D (h->size << GRUB_MM_ALIGN_LOG2); > r->post_size =3D size - r->size; > + r->types =3D types; > =20 > /* Find where to insert this region. Put a smaller one before bigger o= nes, > to prevent fragmentation. */ > @@ -405,12 +410,14 @@ grub_real_malloc (grub_mm_header_t *first, grub_siz= e_t n, grub_size_t align) > } > =20 > /* Allocate SIZE bytes with the alignment ALIGN and return the pointer. = */ > -void * > -grub_memalign (grub_size_t align, grub_size_t size) > +static void * > +grub_real_memalign (grub_size_t align, grub_size_t size, unsigned int ty= pes) > { > grub_mm_region_t r; > grub_size_t n =3D ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) += 1; > + grub_size_t guarantee; > int count =3D 0; > + grub_size_t grow; > =20 > if (!grub_mm_base) > goto fail; > @@ -424,6 +431,15 @@ grub_memalign (grub_size_t align, grub_size_t size) > if ((size + align) > ~(grub_size_t) 0x100000) > goto fail; > =20 > + /* Allocation should always success if region free bytes >=3D guarante= e. */ > + guarantee =3D size + align; > + > + /* Set size flag heuristically. */ > + if (guarantee <=3D GRUB_MM_GROW_SMALL) > + types |=3D GRUB_MM_SIZE_SMALL; > + else > + types |=3D GRUB_MM_SIZE_LARGE; > + > align =3D (align >> GRUB_MM_ALIGN_LOG2); > if (align =3D=3D 0) > align =3D 1; > @@ -432,6 +448,9 @@ grub_memalign (grub_size_t align, grub_size_t size) > =20 > for (r =3D grub_mm_base; r; r =3D r->next) > { > + if ((types & ~r->types) !=3D 0) > + continue; > + > void *p; > =20 > p =3D grub_real_malloc (&(r->first), n, align); > @@ -446,8 +465,13 @@ grub_memalign (grub_size_t align, grub_size_t size) > /* Request additional pages, contiguous */ > count++; > =20 > + if ((types & GRUB_MM_SIZE_SMALL)) > + grow =3D GRUB_MM_GROW_SMALL + GRUB_MM_MAX_COST; > + else > + grow =3D guarantee + GRUB_MM_MAX_COST; > + > if (grub_mm_add_region_fn !=3D NULL && > - grub_mm_add_region_fn (size, GRUB_MM_ADD_REGION_CONSECUTIVE) = =3D=3D GRUB_ERR_NONE) > + grub_mm_add_region_fn (grow, GRUB_MM_ADD_REGION_CONSECUTIVE, t= ypes) =3D=3D GRUB_ERR_NONE) > goto again; > =20 > /* fallthrough */ > @@ -462,7 +486,7 @@ grub_memalign (grub_size_t align, grub_size_t size) > * Try again even if this fails, in case it was able to partia= lly > * satisfy the request > */ > - grub_mm_add_region_fn (size, GRUB_MM_ADD_REGION_NONE); > + grub_mm_add_region_fn (grow, GRUB_MM_ADD_REGION_NONE, types); > goto again; > } > =20 > @@ -483,6 +507,20 @@ grub_memalign (grub_size_t align, grub_size_t size) > return 0; > } > =20 > +/* Allocate memory for plain data. */ > +void * > +grub_memalign (grub_size_t align, grub_size_t size) > +{ > + return grub_real_memalign (align, size, GRUB_MM_CLASS_DATA); > +} > + > +/* Allocate memory for modules. */ > +void * > +grub_module_memalign (grub_size_t align, grub_size_t size) > +{ > + return grub_real_memalign (align, size, GRUB_MM_CLASS_MODULE); > +} > + > /* > * Allocate NMEMB instances of SIZE bytes and return the pointer, or err= or on > * integer overflow. > diff --git a/grub-core/kern/uboot/hw.c b/grub-core/kern/uboot/hw.c > index 272b83bd7..e76302ba9 100644 > --- a/grub-core/kern/uboot/hw.c > +++ b/grub-core/kern/uboot/hw.c > @@ -41,7 +41,7 @@ grub_uboot_mm_init (void) > struct sys_info *si =3D grub_uboot_get_sys_info (); > =20 > grub_mm_init_region ((void *) grub_modules_get_end (), > - GRUB_KERNEL_MACHINE_HEAP_SIZE); > + GRUB_KERNEL_MACHINE_HEAP_SIZE, GRUB_MM_TYPE_ALL); > =20 > if (si && (si->mr_no !=3D 0)) > { > diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c > index 782ca7295..038a31a9f 100644 > --- a/grub-core/kern/xen/init.c > +++ b/grub-core/kern/xen/init.c > @@ -531,7 +531,7 @@ map_all_pages (void) > grub_addr_t heap_start =3D grub_modules_get_end (); > grub_addr_t heap_end =3D (grub_addr_t) new_mfn_list; > =20 > - grub_mm_init_region ((void *) heap_start, heap_end - heap_start); > + grub_mm_init_region ((void *) heap_start, heap_end - heap_start, GRUB_= MM_TYPE_ALL); > } > =20 > grub_err_t > diff --git a/include/grub/mm.h b/include/grub/mm.h > index f3bf87fa0..2a7e572a5 100644 > --- a/include/grub/mm.h > +++ b/include/grub/mm.h > @@ -29,14 +29,30 @@ > # define NULL ((void *) 0) > #endif > =20 > +/* An upper bound of management cost of each region, 4KB should be enoug= h. */ > +#define GRUB_MM_MAX_COST 0x1000 > + > +/* Region type flags. */ > +#define GRUB_MM_SIZE_SMALL (1 << 0) > +#define GRUB_MM_SIZE_LARGE (1 << 1) > +#define GRUB_MM_SIZE_ALL (GRUB_MM_SIZE_SMALL | GRUB_MM_SIZE_LARGE) > +#define GRUB_MM_CLASS_DATA (1 << 30) > +#define GRUB_MM_CLASS_MODULE (1 << 31) > +#define GRUB_MM_CLASS_ALL (GRUB_MM_CLASS_DATA | GRUB_MM_CLASS_MODULE) > +#define GRUB_MM_TYPE_ALL (GRUB_MM_SIZE_ALL | GRUB_MM_CLASS_ALL) > + > +/* Heap grow granularity for region of small type. */ > +#define GRUB_MM_GROW_SMALL 0x100000 > + > #define GRUB_MM_ADD_REGION_NONE 0 > #define GRUB_MM_ADD_REGION_CONSECUTIVE (1 << 0) > =20 > /* > * Function used to request memory regions of `grub_size_t` bytes. The s= econd > - * parameter is a bitfield of `GRUB_MM_ADD_REGION` flags. > + * parameter is a bitfield of `GRUB_MM_ADD_REGION` flags. The third para= meter > + * is a bitfield of `GRUB_MM_TYPE` flags. > */ > -typedef grub_err_t (*grub_mm_add_region_func_t) (grub_size_t, unsigned i= nt); > +typedef grub_err_t (*grub_mm_add_region_func_t) (grub_size_t, unsigned i= nt, unsigned int); > =20 > /* > * Set this function pointer to enable adding memory-regions at runtime = in case > @@ -46,7 +62,7 @@ typedef grub_err_t (*grub_mm_add_region_func_t) (grub_s= ize_t, unsigned int); > extern grub_mm_add_region_func_t EXPORT_VAR(grub_mm_add_region_fn); > #endif > =20 > -void grub_mm_init_region (void *addr, grub_size_t size); > +void grub_mm_init_region (void *addr, grub_size_t size, unsigned int typ= es); > void *EXPORT_FUNC(grub_calloc) (grub_size_t nmemb, grub_size_t size); > void *EXPORT_FUNC(grub_malloc) (grub_size_t size); > void *EXPORT_FUNC(grub_zalloc) (grub_size_t size); > @@ -54,6 +70,7 @@ void EXPORT_FUNC(grub_free) (void *ptr); > void *EXPORT_FUNC(grub_realloc) (void *ptr, grub_size_t size); > #ifndef GRUB_MACHINE_EMU > void *EXPORT_FUNC(grub_memalign) (grub_size_t align, grub_size_t size); > +void *EXPORT_FUNC(grub_module_memalign) (grub_size_t align, grub_size_t = size); > #endif > =20 > void grub_mm_check_real (const char *file, int line); > diff --git a/include/grub/mm_private.h b/include/grub/mm_private.h > index 96c2d816b..575c041d6 100644 > --- a/include/grub/mm_private.h > +++ b/include/grub/mm_private.h > @@ -90,8 +90,11 @@ typedef struct grub_mm_region > /* How many bytes are in this region? (free and allocated) */ > grub_size_t size; > =20 > + /* Acceptable memory allocation types for this region. */ > + unsigned int types; > + > /* pad to a multiple of cell size */ > - char padding[3 * GRUB_CPU_SIZEOF_VOID_P]; > + void *padding[2]; > } > *grub_mm_region_t; > =20 > diff --git a/util/misc.c b/util/misc.c > index d545212d9..5315dcc35 100644 > --- a/util/misc.c > +++ b/util/misc.c > @@ -207,7 +207,8 @@ grub_dl_unref (grub_dl_t mod) > /* Some functions that we don't use. */ > void > grub_mm_init_region (void *addr __attribute__ ((unused)), > - grub_size_t size __attribute__ ((unused))) > + grub_size_t size __attribute__ ((unused)), > + unsigned int types __attribute__ ((unused))) > { > } > =20 > --=20 > 2.30.2 >=20 --6JtSKpa3jUcbXIq5 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEF9hrgiFbCdvenl/rVbJhu7ckPpQFAmNKdrIACgkQVbJhu7ck PpSogw/+My0esZNfL+KMquTcDFiykplakUTYKrx9t5gFXhRZDplx2CXnTpxmi+70 83Jyr/WieDJpvvFUIPjYGQjh5EfPXJkg0HokzL7fVl5O0khevz914Up7YP8oJEvn 6oIIe2n+SVupjggEnkk5u295pP6K53lI6GwoOfbUzgXmim6VI3L1F5FdxPih4P0x TnXEQoLwL5kQf3omH4tnaidY+hoK3VfbRRj1rSqQmblfqVPuvgxmqtmiY6/paiqH zNn1wFBLmL9zLvRBqT+yF5486h37i5EhLOAnyW68zjBDqz5MS1oLd1BW6pnwmmsZ OK6LdIABT+NUUqwoK9VfO9rwxKdErLfaIbq6T+ZOXb4lrNaLCWswRnY8qyUOEqKU 0cGFH1xL/FhFGywZ/hIZW8Gw0yrezbAlJl+hJQffiX2obzbceTzYqEx8mO9vWwjx x3RNip83tLE9KeV+wIQmN61ZWt7yB7dxRR1BGkho1lHWyKBL0MKozHk/qkA5pN2G nGoEm/Dz2lHpmDjvNFcuj1Omt3Ja+AI+A+qsHt8Ygcx97W5TNbvXl7jVVcpnLfCo S/0LFq9zbEEgFA0YXZfhcSvcBfSEc7gNTp7Y0CZD2ZVMAJmwRELhT9o0hXqrqbSK DUQYPeTvpG00kVGQONxZRDg2v9Xv7/ABemmGvhjsnK0DYITCsIs= =iDSw -----END PGP SIGNATURE----- --6JtSKpa3jUcbXIq5--