Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1p5mJq-0000B5-C1 for mharc-grub-devel@gnu.org; Thu, 15 Dec 2022 06:22:30 -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 1p5mJo-000078-Pr for grub-devel@gnu.org; Thu, 15 Dec 2022 06:22:28 -0500 Received: from mail-pj1-x102c.google.com ([2607:f8b0:4864:20::102c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p5mJn-0002sZ-AF for grub-devel@gnu.org; Thu, 15 Dec 2022 06:22:28 -0500 Received: by mail-pj1-x102c.google.com with SMTP id t17so10125668pjo.3 for ; Thu, 15 Dec 2022 03:22:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5BxTlYqDlSgSFX+pn1Q+Z0Bygdw+f2ZUNyxkJufgAEQ=; b=AYPNvSeXwbk7yNlr2rAx4tSe+odWuCNVzjUYAm3/K6zlQAESYo7+cEy5/5FBjhDNIF uAg0ccxF09wEMVCIUnis0r3KZSrx7xWBrnPjuNt4YZ2yccK/XsM7of/9PafNc6ySwj3d zfS+YoJXEygqMsW5cvoEABMgfdK0kqZhGblV2RwVY4UpcR8qJ6/rBg5KZ099oVrgcBpA N7k/43fhBTbvEHh7jU/FMG7cKvvqZ9bhmdy4aZKNhSPLRcTLzJCSm6ANHkRKBR57/hTG jxhalkunkdPAWitiTtZdD760O0PfzpP9/135NXy5c5wwsDhRUBmlOl5PUkA/e645QYku sW4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5BxTlYqDlSgSFX+pn1Q+Z0Bygdw+f2ZUNyxkJufgAEQ=; b=VNG7zPhsK6HkijVDZOYiBno3J21M7jwbopsly2YhFWSDYHXFcm3tnatcH2NzFo5+CF oU9qg0ieo5MZFJaixUunPlifZOjoGLfdgs9Jn0MslQLXUqQ36AKXF+bgEUnUgiJYKisK OnDLWKBCeTIw8sZBjhxYh+hk9wU+ht0W8WCrHd/gTbXPt8VcOpKbBR62oTggNuSqyexD WZ5YXWfijPY4qGbsBtxbwxvAnf7e3sTV3vLJce9rygu6k0A2gpWC0zAmPBqhfrgjpLG8 SNbFGf/8A8AIaXADuvMFkbxYVH6bN3/lZa6HLbUGpX41jRRzufJXynh73F0EeTYFv1AV wmKA== X-Gm-Message-State: ANoB5pktFkE1+zLCIAxWBQrDu0rjT93jpUcpAdDuM9gYBBdpzlZR6vD9 sjEPaw69FYPAULkwy0camCksKwEX0Wo= X-Google-Smtp-Source: AA0mqf7PpjFeJ8m3suajKc/9do+hbuSaxnxvSzTcHRiYZmas65jSeuA2pJok/xOxPTv8qyncmO/+WQ== X-Received: by 2002:a17:902:dac3:b0:189:62f7:e5fc with SMTP id q3-20020a170902dac300b0018962f7e5fcmr37168910plx.14.1671103344699; Thu, 15 Dec 2022 03:22:24 -0800 (PST) Received: from localhost.localdomain (ec2-13-113-80-70.ap-northeast-1.compute.amazonaws.com. [13.113.80.70]) by smtp.gmail.com with ESMTPSA id z5-20020a1709027e8500b00176a2d23d1asm3589386pla.56.2022.12.15.03.22.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Dec 2022 03:22:24 -0800 (PST) From: Zhang Boyang To: grub-devel@gnu.org Cc: Zhang Boyang , dkiper@net-space.pl Subject: [PATCH v2 2/2] mm: Preallocate some space when adding new regions Date: Thu, 15 Dec 2022 19:19:25 +0800 Message-Id: <20221215111921.132967-3-zhangboyang.id@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <78b71224-5956-e806-2638-4f66dca99d08@gmail.com> References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::102c; envelope-from=zhangboyang.id@gmail.com; helo=mail-pj1-x102c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=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: Thu, 15 Dec 2022 11:22:29 -0000 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 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 --- grub-core/kern/mm.c | 3 ++- include/grub/mm_private.h | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index 20bb54dde..f023247bb 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -449,11 +449,12 @@ grub_memalign (grub_size_t align, grub_size_t size) goto fail; /* - * Pre-calculate the size of heap growth (if applicable), + * Pre-calculate the optimal size of heap growth (if applicable), * with region management overhead taken into account. */ if (grub_add (bound, GRUB_MM_MGMT_OVERHEAD, &grow)) goto fail; + grow = grub_max (grow, GRUB_MM_HEAP_GROW_EXTRA); align = (align >> GRUB_MM_ALIGN_LOG2); if (align == 0) diff --git a/include/grub/mm_private.h b/include/grub/mm_private.h index 96c2d816b..6e4256555 100644 --- a/include/grub/mm_private.h +++ b/include/grub/mm_private.h @@ -95,6 +95,9 @@ typedef struct grub_mm_region } *grub_mm_region_t; +/* Minimal heap growth granularity when existing heap space is exhausted. */ +#define GRUB_MM_HEAP_GROW_EXTRA 0x100000 + #ifndef GRUB_MACHINE_EMU extern grub_mm_region_t EXPORT_VAR (grub_mm_base); #endif -- 2.30.2