From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pI2eO-0006m3-VA for mharc-grub-devel@gnu.org; Wed, 18 Jan 2023 02:14:25 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pI2eK-0006bl-5V for grub-devel@gnu.org; Wed, 18 Jan 2023 02:14:23 -0500 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pI2eI-0004a9-EU for grub-devel@gnu.org; Wed, 18 Jan 2023 02:14:19 -0500 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 2090732001C6; Wed, 18 Jan 2023 02:14:17 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Wed, 18 Jan 2023 02:14:17 -0500 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=1674026056; x=1674112456; bh=yMj7DiermA WYIhodBACctrFht+3F5+3cUAbBVpKfK+M=; b=uo/CGge8ep3KtuvDjanmqvLei6 WjyyM2ppIOciH1+dNGfGAP3wF6Jb6SmSHrqhlCoJA6IrT1NDvBr/oA/pRfLE/KRq CXkMkEA8C4ANdX5Z+3RpUSafXgq6lHCZj2j9tedUf362b0BVoMckqVzyZgWQ/IiF Jr2vZN7uYIsSIJ9fT+M3kBFgHKaAoN0weB9hB3OTOH4zQ3K2HXuDO1efwdTI3zIW dsqzW1YMQMZDpIuaRQtoYTSRWtUUTUYyt33G5101da2lvvrVdImOgPVnXCjINetc hgYIIdYt9UhWIXBKtgOTYJne6ybX/4ziiNiRAFO94KuExirDpSIHGYZ22fBA== 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=1674026056; x=1674112456; bh=yMj7DiermAWYIhodBACctrFht+3F 5+3cUAbBVpKfK+M=; b=G6meWQXKChxDCnkRXKw0g+d7UVU1AnWz3eP1CbrdS8oF 97X4FEBXQflAFdnncIOIbkWwIMsTJg8FgxynFXoiW2dmL95vhw0gtX5/OD/Tug02 Gh0+nKgpMva5dU3AZv4+0FJf2NFZyayFO0eZgLbkV1sGnmQ5lbVstGN2fnRYT0Bz Q5wffRWxv+7vELUJB9X0e1gyfRVIRwR4zfkUFkSsjtlgzqxYiWVFnbdP19+l1CCs HSPaMsmhe2alYX4h8C2aKtN5k0cTpOEXb3MtKEW6Fbp+U3ftMHbHRt2QevAITKia LaFopRSMp4Rky4+vG3p6/yye/nnHibGPJQFjydp2yw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedruddtjedguddtkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefrrght rhhitghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqeenucggtffrrghtth gvrhhnpeeukedtvedtffevleejtefgheehieegkeeluddvfeefgeehgfeltddtheejleff teenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehpsh esphhkshdrihhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 18 Jan 2023 02:14:15 -0500 (EST) Received: by pks.im (OpenSMTPD) with ESMTPSA id e8c71d9e (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 18 Jan 2023 07:13:54 +0000 (UTC) Date: Wed, 18 Jan 2023 08:14:13 +0100 From: Patrick Steinhardt To: The development of GNU GRUB Cc: Zhang Boyang Subject: Re: [PATCH v4 2/2] mm: Preallocate some space when adding new regions Message-ID: References: <20230114132323.132210-1-zhangboyang.id@gmail.com> <20230114132323.132210-3-zhangboyang.id@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="vynybeH/jrwHYAHb" Content-Disposition: inline In-Reply-To: <20230114132323.132210-3-zhangboyang.id@gmail.com> Received-SPF: pass client-ip=64.147.123.25; envelope-from=ps@pks.im; helo=wout2-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_H2=-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: Wed, 18 Jan 2023 07:14:23 -0000 --vynybeH/jrwHYAHb Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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. >=20 > 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. >=20 > 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. >=20 > Signed-off-by: Zhang Boyang > --- > grub-core/kern/mm.c | 6 ++++++ > 1 file changed, 6 insertions(+) >=20 > 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 valu= e. */ > #define GRUB_MM_HEAP_GROW_ALIGN 4096 > =20 > +/* Minimal heap growth granularity when existing heap space is exhausted= =2E */ > +#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; > =20 > @@ -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; > =20 > + /* Preallocate some extra space if heap growth is small. */ > + grow =3D 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 Thanks for both patches! Patrick --vynybeH/jrwHYAHb Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEF9hrgiFbCdvenl/rVbJhu7ckPpQFAmPHnEQACgkQVbJhu7ck PpSDdg//eL7ul5czfaeBo+vmjYKGn1XBIhvnaZG17vXJohiUD/by16NZZ7LOcCOl gzKXJMeZdQu9/JvTspPXyaDIilp4jOlm95hdsEvyESPRpBW/H7LYD7uCOSVyylUm lF3jIhzL2wmWftADCXl83ZC/8Z/bsgq+E9kEESsYZIByH8HOEFhKrl+SQaOHulKm wVXqFoezep8byyTAzQvufEO5L1vid9ym3WrzLsHQSHnjxACH+REHWr0CZc5l1CJ5 ADZlfbTVx77/DiCEZeEokhKl74tXxpcxkVyyYPPHR7WgniXt7zuA/iqqFeEL7BlJ x8DVGxig/dgX07lvgMuyV73HzouZYguBYMdV6usBFpN1YTyrGzdNUROy6uZrxF0K PIiBPAYevJIWa+B8CmoMBpYQ1DHhS0ipyDvJWygDfCbgkWUy53qZK02cIla7oXvx 4z0P6vFpYrjtJG3MJiFsmFG6ocigDSIsQ9+3nTTf7I4QZ4o/QnliR+nSYk4Ch2B8 b9G0ti8x3jF1R1VY5Kjk75iQXuTm7Oejq//B3SVhAvXmSEagUhc1yeRhIhlRfyeo fwx8dxh4JQcsz9kQ3HPB33hVEB50XpvuKxv24tCCeEcpBBoC29hQeA69TxksyyiR PAXvWws+zdBsFYuVbw2ZsL5JLHAGvwI23ODZ82s22gRl6UiGrzE= =R/eT -----END PGP SIGNATURE----- --vynybeH/jrwHYAHb--