Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1p01RY-0007Jp-8y for mharc-grub-devel@gnu.org; Tue, 29 Nov 2022 09:18:40 -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 1p01RW-0007JX-NB for grub-devel@gnu.org; Tue, 29 Nov 2022 09:18:38 -0500 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p01RS-0005RZ-2a for grub-devel@gnu.org; Tue, 29 Nov 2022 09:18:38 -0500 Received: by mail-pj1-x1031.google.com with SMTP id x13-20020a17090a46cd00b00218f611b6e9so13581683pjg.1 for ; Tue, 29 Nov 2022 06:18:33 -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=UHwM0RZjiY5LA/e3k5TGgTH/Re9vcfrLXAG1W0+aA+s=; b=BlPZi92ILeJIl41GEQ7glsARoOAhXxA7MzeUAkG4mnzfYA7DvGkPVC1GfsTg4eYNzV JW8YZtMy92ex0z86BePHeFcXd07shIto5nGGuybamgINyhnter7iqMNRSvWScXs51nhl Gttu64zF9cvmDPHCApZo+N04cAutsTqnNYC5R2Ch1UQp5i6hv2snMq3ihSU5D2WTua79 3c/0FceyzEHr0/3FWhFslB2Hki/ruF9Zny7Z/aJfJ/sSS7eDV4OAzLX8KXydXU0J3QID SmAxwxbyNUdSXqTlB3J9h+tdsGJVo4zJFNj8Zr4B8zL3Tx2NCDt/5nwkkQ0Caul3+MVZ nzzQ== 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=UHwM0RZjiY5LA/e3k5TGgTH/Re9vcfrLXAG1W0+aA+s=; b=Kdh13zeVdaa4Qz7EMy4HvGA/tWnly6f8Qeu5LZ+a700vuYUTgVWR9Sle55CEKVZISS sozz5Vn+DV9YejKcZhMUN4rZTlNPabzTPRe/wgVsKWX5bJk0yARBxI26qd0ysTPxgT4r AVrvMStcUJQWK/8Oym6W7Jt2SNZUqmP+NsL/xqcUqurH3zzWlMv+wA92fKGmhTPWHX8T iDK38UAweGZa3HzPoKDoawxgpN/gY0vlEFN3HURg697NJJoZGyRQ/31Bv98GhG+eXu9C euHb+MVg1120+5WVlYhgCFbgScyRssoQixo9YVN4FAc4vowoemMgqPhltARYJsTK3Wbd FcWQ== X-Gm-Message-State: ANoB5plFF2Ksa9nEjil8zeYmffNdbiqpygmp/wNvR6bRsPmq19hRswuw PVcywm9ePJuQHEgpYt0StG4GSrg6zYc= X-Google-Smtp-Source: AA0mqf5QrFQ5jK+G7eUgTJM6cXUPoaNI0pVa/nJPL8j5M4XrWv4m1FluiPb9ZX9ZlASjaSsRT5hqJA== X-Received: by 2002:a17:90b:2642:b0:219:55d5:f30a with SMTP id pa2-20020a17090b264200b0021955d5f30amr2141388pjb.23.1669731512786; Tue, 29 Nov 2022 06:18:32 -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 x11-20020aa7940b000000b0056da63c8515sm1600239pfo.91.2022.11.29.06.18.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Nov 2022 06:18:32 -0800 (PST) From: Zhang Boyang To: grub-devel@gnu.org Cc: Zhang Boyang Subject: [PATCH 2/2] mm: Preallocate some space when adding new regions Date: Tue, 29 Nov 2022 22:17:34 +0800 Message-Id: <20221129141734.123708-3-zhangboyang.id@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221129141734.123708-1-zhangboyang.id@gmail.com> References: <20221129141734.123708-1-zhangboyang.id@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=zhangboyang.id@gmail.com; helo=mail-pj1-x1031.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: Tue, 29 Nov 2022 14:18:39 -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, which may be too slow if those requests are small (e.g. about 4KB). This patch introduces GRUB_MM_HEAP_GROW, the minimal heap growth granularity. The new region size is now set to the bigger one of original value and GRUB_MM_HEAP_GROW. Thus, it will result in some memory space preallocated if current allocations request is small. The value of GRUB_MM_HEAP_GROW 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, 6 insertions(+) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index 973cb6b15..83c618c5b 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -452,6 +452,9 @@ grub_memalign (grub_size_t align, grub_size_t size) if (grub_add (bound, GRUB_MM_MAX_COST, &grow)) goto fail; + /* Calculate optimal size of heap growth. */ + grow = grub_max (grow, GRUB_MM_HEAP_GROW); + /* Request additional pages, contiguous */ count++; diff --git a/include/grub/mm_private.h b/include/grub/mm_private.h index f212110e4..268fbf094 100644 --- a/include/grub/mm_private.h +++ b/include/grub/mm_private.h @@ -104,6 +104,9 @@ typedef struct grub_mm_region */ #define GRUB_MM_MAX_COST 0x1000 +/* Minimal heap growth granularity when existing heap space is exhausted. */ +#define GRUB_MM_HEAP_GROW 0x100000 + #ifndef GRUB_MACHINE_EMU extern grub_mm_region_t EXPORT_VAR (grub_mm_base); #endif -- 2.30.2