Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pGgVj-00051C-UL for mharc-grub-devel@gnu.org; Sat, 14 Jan 2023 08:23:52 -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 1pGgVc-00050b-R0 for grub-devel@gnu.org; Sat, 14 Jan 2023 08:23:45 -0500 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pGgVb-0003DN-7S for grub-devel@gnu.org; Sat, 14 Jan 2023 08:23:44 -0500 Received: by mail-pj1-x1035.google.com with SMTP id s13-20020a17090a6e4d00b0022900843652so8067949pjm.1 for ; Sat, 14 Jan 2023 05:23:42 -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=P8opzILpB36EGPQie+0dtPpx3pxqT+YLlzn7KVzbwBM=; b=b1UNe3B5ZoU5yzgmOQOi+tSfPEwI5Xz49HXz6XQ06uXaqFa0+aKREKpSovVs9cwRb+ MRoz+Ihe6kECT8Jlm7Dv2KdCiOZvo2r/4C8aTgppUjR1vq7BqXL9o8WYCTvPHYleSxvJ DjqXZjzoEnNFkplY7l6nur0RQruwiProcIl6foiDYLAqoiPsrQAoUCD0hF9q8idLB+i8 gM6qzWlkpmrSM/eqs7EGTnXuuqKGrlfsL7VE/rdPBrER6uGOa1Y6KZESoQftD5rhQsGU 62RRds944iSYGYjaFl/J6oXT1GuZ0uvJAhSGGJ6Xf89NAWBfXvJ2LD8Gezz+XBoexAPB oRUw== 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=P8opzILpB36EGPQie+0dtPpx3pxqT+YLlzn7KVzbwBM=; b=GTZUWg77xTXjRpzLE2fzvxNyaJuRhNJFkvkNMyhYXyzPJ9WklzOWxTXunU6pD4cpd5 xUw1nhs8PlZDSgO5Tq9/ViPT3yBSo08EvMzrgJabZWWeFIR3WBaZ9brJLZhkhBofo01Q L+Ox9TLF6AavXMncm4LbUw8mb4wq99rXU4pYpVOyNlxJltENVzyxnlwJFHDCE6DNml4H 5NW/va3vqphPeADp+Maa7QgMWQ7iLHb1M2tcKcae/2PHF6o4FsErQk26fMpCYLF/RhKy 6vvMyeCllM75B8K5JAprT0H4Jm09blQ5jNdqaKq3KTZ3W4M6gPQ88PhOO6gZsWuJDYml Se8g== X-Gm-Message-State: AFqh2kr97HoYAg8hw0hbNE3epmgCTQhTv8FlE7sYdp0lGWw4X63LYskY q7C2ogaPsxIIEJcQarzC77zD1ycPbpo= X-Google-Smtp-Source: AMrXdXsN+Cj6JhTgBZcs8G65OebCI1CODNUfAvZZfGvxhdOj7kJPCVGQuNzLyw6+dw+/mP2bFBnO1w== X-Received: by 2002:a17:903:26c9:b0:192:5282:6833 with SMTP id jg9-20020a17090326c900b0019252826833mr14283445plb.29.1673702622018; Sat, 14 Jan 2023 05:23:42 -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 u10-20020a170902e80a00b001947982eb8fsm1737022plg.60.2023.01.14.05.23.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Jan 2023 05:23:41 -0800 (PST) From: Zhang Boyang To: grub-devel@gnu.org Cc: Zhang Boyang Subject: [PATCH v4 2/2] mm: Preallocate some space when adding new regions Date: Sat, 14 Jan 2023 21:23:23 +0800 Message-Id: <20230114132323.132210-3-zhangboyang.id@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230114132323.132210-1-zhangboyang.id@gmail.com> References: <20230114132323.132210-1-zhangboyang.id@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::1035; envelope-from=zhangboyang.id@gmail.com; helo=mail-pj1-x1035.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: Sat, 14 Jan 2023 13:23:45 -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 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 --- 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; -- 2.30.2