Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1p01RY-0007Jj-4f 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 1p01RU-0007JE-Kr for grub-devel@gnu.org; Tue, 29 Nov 2022 09:18:38 -0500 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p01RQ-0005RK-64 for grub-devel@gnu.org; Tue, 29 Nov 2022 09:18:33 -0500 Received: by mail-pj1-x102f.google.com with SMTP id x13-20020a17090a46cd00b00218f611b6e9so13581561pjg.1 for ; Tue, 29 Nov 2022 06:18:31 -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=ncp+QpIbYGV+/wrTU9wRSU2gXcxuaESXSZEx40I3eT0=; b=VEDdZAZJpWm0gP5+l4qOP98cqBP+ZK1TejXVzs3p8mXpl0c8OlFiB95V9CgJUIRzfz 3jvXPo3+T5VS174aozPxwCMNoBZRkxeQqCQEQgFiBr5qbm4VVU/bhhH0/oUe34A5CVUn 7nDvSSWJbQKUQ7UzCQYwcwH6d48sxiKTpoyEjUJh7JXE6yuwfudlN7TBns0jbQYqqmBr ld1FbdYTH9X71B3Zug9yRIsyBXAGrQutuktd7O4b2Vbvoz1z0zWZtim81l9/GQvwjii4 vsu5mZHMLvmUyaNdWaqn+ZErw69z8bY6FQaCG36iEZr8Q52xGm4fxTcokO4g31/68+DX gDkQ== 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=ncp+QpIbYGV+/wrTU9wRSU2gXcxuaESXSZEx40I3eT0=; b=xsMjS3JNzku7vTwJN/ZH9PXwQrnv4oXdEhEMKOkURKGYRovGrZ6pZKrKHx4KKua8Wv Ex+2Xl4zn5r/A9cZMaK3UNnoCP9Ed3TwrRC+6isIHEatoDDXSkcfhJ3Kct+SJcFWOrOw jc3DAk6K1xpaLLUs8Pt8Poiho7xD7r/4ux4rFaI0mwvCL4TXFQ8JFq1GFenMa4B5fqm1 Rwiq1CgLTVtIYkupVvOouKTBfetw2BL0Uv9q1JSE4R0DNkVQWesPj26mKoy+Zg4zOh/F 2wxbJYeZ7tG+h57d8B1T3tizr3PuP31VIhd1X3ldZZH9P+LCgCvdPO4R03hQAOmh3YPe n4EQ== X-Gm-Message-State: ANoB5plqqwcbfRLVK6XH+nAYekNguuIWUN7SPLBq60AjHbg0zi4fLawf gdEcluHMsfXkfBcfC1qY1JvlXi67N70= X-Google-Smtp-Source: AA0mqf4xGhmtrINQIHKNaYXAmsD1XJdwC/A0vL61zWwTbjNKIN845GFBOIFAZ01acN/mNYM9SpXY/g== X-Received: by 2002:a17:90a:307:b0:213:ff80:b37f with SMTP id 7-20020a17090a030700b00213ff80b37fmr66513891pje.118.1669731510608; Tue, 29 Nov 2022 06:18:30 -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.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Nov 2022 06:18:30 -0800 (PST) From: Zhang Boyang To: grub-devel@gnu.org Cc: Zhang Boyang Subject: [PATCH 1/2] mm: Adjust new region size to take management cost into account Date: Tue, 29 Nov 2022 22:17:33 +0800 Message-Id: <20221129141734.123708-2-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::102f; envelope-from=zhangboyang.id@gmail.com; helo=mail-pj1-x102f.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:38 -0000 When grub_memalign() encounters out-of-memory, it will try grub_mm_add_region_fn() to request more memory from system firmware. However, the size passed to it doesn't take region management cost into account. Adding a memory area of `size` bytes will result in a heap region of less than `size` bytes truely avaliable. Thus, the new region might not adequate for current allocation request, confusing out-of-memory handling code. This patch introduces GRUB_MM_MAX_COST to address the region management cost (e.g. metadata, alignment). The value of this new constant should make `grub_malloc (size)` always success after a successful call to `grub_mm_init_region (addr, size + GRUB_MM_MAX_COST)`. The new region size is now set to `size + GRUB_MM_MAX_COST`, thus if grub_mm_add_region_fn() succeeded, current allocation request can always success. Signed-off-by: Zhang Boyang --- grub-core/kern/mm.c | 15 ++++++++++++--- include/grub/mm_private.h | 9 +++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index ae2279133..973cb6b15 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -410,7 +410,9 @@ grub_memalign (grub_size_t align, grub_size_t size) { grub_mm_region_t r; grub_size_t n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1; + grub_size_t bound; int count = 0; + grub_size_t grow; if (!grub_mm_base) goto fail; @@ -418,10 +420,13 @@ grub_memalign (grub_size_t align, grub_size_t size) if (size > ~(grub_size_t) align) goto fail; + /* If largest free chunk in region >= bound, allocation can always success */ + bound = size + align; + /* We currently assume at least a 32-bit grub_size_t, so limiting allocations to - 1MiB in name of sanity is beneficial. */ - if ((size + align) > ~(grub_size_t) 0x100000) + if (bound > ~(grub_size_t) 0x100000) goto fail; align = (align >> GRUB_MM_ALIGN_LOG2); @@ -443,11 +448,15 @@ grub_memalign (grub_size_t align, grub_size_t size) switch (count) { case 0: + /* Adjust heap growth to address the region management cost. */ + if (grub_add (bound, GRUB_MM_MAX_COST, &grow)) + goto fail; + /* Request additional pages, contiguous */ count++; if (grub_mm_add_region_fn != NULL && - grub_mm_add_region_fn (size, GRUB_MM_ADD_REGION_CONSECUTIVE) == GRUB_ERR_NONE) + grub_mm_add_region_fn (grow, GRUB_MM_ADD_REGION_CONSECUTIVE) == GRUB_ERR_NONE) goto again; /* fallthrough */ @@ -462,7 +471,7 @@ grub_memalign (grub_size_t align, grub_size_t size) * Try again even if this fails, in case it was able to partially * satisfy the request */ - grub_mm_add_region_fn (size, GRUB_MM_ADD_REGION_NONE); + grub_mm_add_region_fn (grow, GRUB_MM_ADD_REGION_NONE); goto again; } diff --git a/include/grub/mm_private.h b/include/grub/mm_private.h index 96c2d816b..f212110e4 100644 --- a/include/grub/mm_private.h +++ b/include/grub/mm_private.h @@ -95,6 +95,15 @@ typedef struct grub_mm_region } *grub_mm_region_t; +/* + * Set an upper bound of management cost of each region, + * with sizeof(struct grub_mm_region), sizeof(struct grub_mm_header) and + * any possible alignment or padding taken into account. + * The value should make `grub_malloc (size)` always success after a successful + * call to `grub_mm_init_region (addr, size + GRUB_MM_MAX_COST)`. + */ +#define GRUB_MM_MAX_COST 0x1000 + #ifndef GRUB_MACHINE_EMU extern grub_mm_region_t EXPORT_VAR (grub_mm_base); #endif -- 2.30.2