Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1p8Hiz-0008W2-CD for mharc-grub-devel@gnu.org; Thu, 22 Dec 2022 04:18:49 -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 1p8Hix-0008VX-F5 for grub-devel@gnu.org; Thu, 22 Dec 2022 04:18:47 -0500 Received: from mail-pg1-x530.google.com ([2607:f8b0:4864:20::530]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p8Hiv-0008FW-Se for grub-devel@gnu.org; Thu, 22 Dec 2022 04:18:47 -0500 Received: by mail-pg1-x530.google.com with SMTP id s196so947469pgs.3 for ; Thu, 22 Dec 2022 01:18:45 -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=lz2u1/A3CzgXGrwAFObkWUcb1iJkWrGuMHmV5GNzEHU=; b=oCDj8E5zqywKAbcznkxkZxsbpJehM/fWxI6MFdK6NAaxihyug59LbFd/DXId5mWJBC fCogh16W05X16lLCzTx0qzcHfGYb3jnUwF/YKKr81/oYx8Jrbo8IUKdqkjIMTe2Eohfj 07QtbkqKYFPWlc8tUV2Q+Cz8aDcjitbMbtANTN1d6Q1srrISCTYDoDSc24voHS3NF77O c5/sh0bsLy/TGCaOyncikAzkZ+SLWinPaa5yaHzy1ZXXkBLj7799OyOq22B/DmaawzT0 tJJyMSPOBfqcKcjOrd6FPhSQiOzxec27LT1aU16oY8+DSGozMObY/4D7v9yELdkUbBv+ OwLA== 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=lz2u1/A3CzgXGrwAFObkWUcb1iJkWrGuMHmV5GNzEHU=; b=RoGkAIJaf92+Wh4D39TEWaZzrXrpnbYq6C27nIkL2Jb4uf2IKOI2aGzLz3K25bgA7w yM+JiZUlK6BuGwhO2JI+3d+q3D8tc9IsNvdmiZe2gGmAVPpR+GTyqHfagSoGdwGmPBVF RxH/k2g+9x9UpLb/PlFhjwBw4eIvXhRy/hlrXFB3ahKtaGr8iYqArxsRP45veY64h/Sz 4wwn3O8YkwrnLZSkCQ5r301o9/kpdJYz9oYSJgFns/1ELnhE/exDmwBqjeF70yDxHdSt vMDBjjkb1Zokb2HA3wEL0ihZoXYBg5mY70KhCtejWNxdUPNVgAnvhm3IOrICFgVUroAh rK9A== X-Gm-Message-State: AFqh2kqNQeYgV5VlJSnhyEw0Jct9Ksd4SP6tRKywLdN8q6qPChwiRaN0 hc5MeaTOejXnhI4PE5IiMWJ0lwPnHmxZ0Q== X-Google-Smtp-Source: AMrXdXvnsUTWdreVEzB4wI3X3ulIuzMnQ/62TOuQgcoA+kfbisKa9HRNbSntpnQlxqgEQ7fOygjgqQ== X-Received: by 2002:a62:36c4:0:b0:578:7e9d:97b2 with SMTP id d187-20020a6236c4000000b005787e9d97b2mr6335655pfa.3.1671700724312; Thu, 22 Dec 2022 01:18:44 -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 p2-20020aa79e82000000b0056b8b17f914sm155657pfq.216.2022.12.22.01.18.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Dec 2022 01:18:44 -0800 (PST) From: Zhang Boyang To: grub-devel@gnu.org Cc: dkiper@net-space.pl, Zhang Boyang Subject: [PATCH v3 2/2] mm: Preallocate some space when adding new regions Date: Thu, 22 Dec 2022 17:18:24 +0800 Message-Id: <20221222091824.233227-3-zhangboyang.id@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221222091824.233227-1-zhangboyang.id@gmail.com> References: <20221222091824.233227-1-zhangboyang.id@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::530; envelope-from=zhangboyang.id@gmail.com; helo=mail-pg1-x530.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, 22 Dec 2022 09:18:47 -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, 5 insertions(+), 1 deletion(-) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index 5c0e5bbbf..16c28cab8 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -120,6 +120,9 @@ + sizeof (struct grub_mm_header) \ + (GRUB_MM_ALIGN - 1)) +/* 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; @@ -468,11 +471,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 (size + align, 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) -- 2.30.2