From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1mFE14-0007gM-E6 for mharc-grub-devel@gnu.org; Sun, 15 Aug 2021 07:09:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39088) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mFE12-0007bm-Rh for grub-devel@gnu.org; Sun, 15 Aug 2021 07:09:20 -0400 Received: from out2-smtp.messagingengine.com ([66.111.4.26]:50955) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mFE11-0005IX-2Y for grub-devel@gnu.org; Sun, 15 Aug 2021 07:09:20 -0400 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 94D505C00E4; Sun, 15 Aug 2021 07:09:18 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Sun, 15 Aug 2021 07:09:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm1; bh=bhDwoyLUMdD0b6GfvHxNrhBSOkW /1+CTiWit3I+QZSQ=; b=khxjO+pPkcXEOnI9eXzTsD3uSaJyu+mCUZyIZLXs61m CvplaX4hgtfEMEPObmAKNIbV/rKnrEtUpIKo2oSA1jsagkErv3jRK/fNxvoUl46u onVhuLcB1tPoylbNe+ZlK1yf4veObfFvLNVsjs/rZf3NHLLwtiVZUngLbfn6oNHJ TEY1qH2Th7swFoIMWMwDMNPhYn/83BYjMpmokqbYoEli/+xLVhSSdhENv5rRVwLu ZR6o8ZbA0ljCpNLoo37L1q1dusrD/OYtDHKlMeV+X+1FxxUsp68gE65uUvq7y+q+ XFrG+FoCeJ+/TBJfF599j1+S3bTOsbJeaKqZTWxeBjA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=bhDwoy LUMdD0b6GfvHxNrhBSOkW/1+CTiWit3I+QZSQ=; b=rhBrH88nCrm5mnzKUr8h2k 2qCp7LwODohNQq8jHH65/DLh8GVwW8G3iCV/S7pQJoyP3SFavz+ZCtRogD9IpKp8 LzneVZpME3v/MugnjsrITsCQtCozZGMjForjJ2tzbRWn6H5OgOYoORWT5A2UsMHV JCxUowdcHj04r5NpsQcTbcLLiZw6HaJ4+DoEBBioJNOe2jJKcP1kDUlZTbxWVJRq qvXDKUVEOzo4iYYw/KMRE9dYCIWyof9cwAryDgGN1JbkrAV9iNE6+8uf51mMgQKm 5Ai8J448p/iMeOfXo9FM7gePonTcRi6KUD4+RPeuSsTLJ0aq5k1/ysoh5Uif0CcA == X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrkeelgdefjecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhrihgt khcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvghrnh epheeghfdtfeeuffehkefgffduleffjedthfdvjeektdfhhedvlefgtefgvdettdfhnecu vehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhssehpkh hsrdhimh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 15 Aug 2021 07:09:16 -0400 (EDT) Received: from localhost (ncase [10.192.0.11]) by vm-mail.pks.im (OpenSMTPD) with ESMTPSA id 78931790 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sun, 15 Aug 2021 11:09:15 +0000 (UTC) Date: Sun, 15 Aug 2021 13:09:14 +0200 From: Patrick Steinhardt To: grub-devel@gnu.org Cc: Leif Lindholm , Daniel Kiper , Stefan Berger Subject: [PATCH v3 2/6] mm: Allow dynamically requesting additional memory regions Message-ID: <3f0ec2a76c1b3aa722efdd540b4d3ecce1789750.1629025332.git.ps@pks.im> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="DsCcm0u8ZPVrymEX" Content-Disposition: inline In-Reply-To: Received-SPF: pass client-ip=66.111.4.26; envelope-from=ps@pks.im; helo=out2-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.01, RCVD_IN_MSPIKE_WL=-0.01, 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.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 15 Aug 2021 11:09:21 -0000 --DsCcm0u8ZPVrymEX Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Currently, all platforms will set up their heap on initialization of the platform code. While this works mostly fine, it poses some limitations on memory management on us. Most notably, allocating big chunks of memory in the gigabyte range would require us to pre-request this many bytes from the firmware and add it to the heap from the beginning on some platforms like EFI. As this isn't needed for most configurations, it is inefficient and may even negatively impact some usecases when, e.g., chainloading. Nonetheless, allocating big chunks of memory is required sometimes, where one example is the upcoming support for the Argon2 key derival function in LUKS2. In order to avoid pre-allocating big chunks of memory, this commit implements a runtime mechanism to add more pages to the system. When a given allocation cannot be currently satisfied, we'll call a given callback set up by the platform's own memory management subsystem, asking it to add a memory area with at least `n` bytes. If this succeeds, we retry searching for a valid memory region, which should now succeed. Signed-off-by: Patrick Steinhardt --- grub-core/kern/mm.c | 10 ++++++++++ include/grub/mm.h | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index e0e580270..2df835392 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -81,6 +81,7 @@ =0C =20 grub_mm_region_t grub_mm_base; +grub_mm_add_region_func_t grub_mm_add_region_fn; =20 /* Get a header from the pointer PTR, and set *P and *R to a pointer to the header and a pointer to its region, respectively. PTR must @@ -360,6 +361,15 @@ grub_memalign (grub_size_t align, grub_size_t size) count++; goto again; =20 + case 1: + /* Request additional pages. */ + count++; + + if (grub_mm_add_region_fn && grub_mm_add_region_fn (size, GRUB_MM_AD= D_REGION_CONSECUTIVE) =3D=3D GRUB_ERR_NONE) + goto again; + + /* fallthrough */ + default: break; } diff --git a/include/grub/mm.h b/include/grub/mm.h index 9c38dd3ca..afde57d2e 100644 --- a/include/grub/mm.h +++ b/include/grub/mm.h @@ -20,6 +20,7 @@ #ifndef GRUB_MM_H #define GRUB_MM_H 1 =20 +#include #include #include #include @@ -28,6 +29,21 @@ # define NULL ((void *) 0) #endif =20 +#define GRUB_MM_ADD_REGION_NONE 0 +#define GRUB_MM_ADD_REGION_CONSECUTIVE (1 << 0) + +/* + * Function used to request memory regions of `grub_size_t` bytes. The sec= ond + * parameter is a bitfield of `GRUB_MM_ADD_REGION` flags. + */ +typedef grub_err_t (*grub_mm_add_region_func_t) (grub_size_t, unsigned int= ); + +/* + * Set this function pointer to enable adding memory-regions at runtime in= case + * a memory allocation cannot be satisfied with existing regions. + */ +extern grub_mm_add_region_func_t EXPORT_VAR(grub_mm_add_region_fn); + void grub_mm_init_region (void *addr, grub_size_t size); void *EXPORT_FUNC(grub_calloc) (grub_size_t nmemb, grub_size_t size); void *EXPORT_FUNC(grub_malloc) (grub_size_t size); --=20 2.32.0 --DsCcm0u8ZPVrymEX Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEF9hrgiFbCdvenl/rVbJhu7ckPpQFAmEY9dkACgkQVbJhu7ck PpScOQ//UbROjKLOAviqmbQoJLJHxwcOKGzpSWDmHI4mjXxc+wafFL6rPOa75//8 R5QoYbEN1uCSkrK4Mgjn4aqwCHp9PyL+thJXuryzHNVtyPrz96aUi4kwXWfsX6Jw O6a1JpqTJieJyRcTWG91i6k8SMdtPuNXAt/tsllbRy6jp/fYDz4inJHgtG6p//WI 4ORbW4OcFO7n3FCFa1l4EYqsaWJDSkAuKLk5QUKgpAKn3YEc1uL32TgFt77jIjSi 4RIVZT4kNv6aVhCfwLQQW8g1BZkTYO5ehzDXB6SnI+4CjxeGwVMgeZFF6NbU68LX B4ZBO+SfTpwMoexHykM3VtVEzAQfmY8idIeS0m0ptC5j8rlz/oxI3TrqHkoTxgSU ZhyrqNOdMOnYx2jLqnh79qmizEebai1nPA/bl2Jke0lQvWSORH9h/HpfN3vCFTW3 Vw8nfZS7MgbU0hk/14rFd/1TWV9m+aEgUITYxL1cnOI70V/Ne+HO41O+NJh6fT14 5yZScrJ3FD8BkvJi/IW4PBZ2Iq8n7u9/UIvhKiNisQuHr13jgaGF2NYm6RYYZANg 1qz7wkf3ora3nN74fu26ohWejlC9hcNGYlIjBI0iRF5E6IJ/mT4jq8PVJ0DJnrUc 8AeLPgnW6BiPgTUevUOODo5jGZQ3oJ6zFumZNLl28wN0Lr9Mqnw= =+47H -----END PGP SIGNATURE----- --DsCcm0u8ZPVrymEX--