Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pGgVg-00050p-Oj for mharc-grub-devel@gnu.org; Sat, 14 Jan 2023 08:23:48 -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 1pGgVb-00050N-R1 for grub-devel@gnu.org; Sat, 14 Jan 2023 08:23:43 -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 1pGgVZ-0003D7-Nw for grub-devel@gnu.org; Sat, 14 Jan 2023 08:23:43 -0500 Received: by mail-pj1-x1035.google.com with SMTP id bj3so21803918pjb.0 for ; Sat, 14 Jan 2023 05:23:41 -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=NUjqVObLnDIiwVQH9qk2171KT4NM3+X10SMGiY+XeAg=; b=T+kKp6lVWghnfRuFADT4g0HesRXg7IGinNP3kRZUk/Ui1jp3Zor63x2vqb48kkdxw7 oCkwZWJKzQe6Jfw4kkL8YokQ+HkSHn9jWWkEDaEKKObWusmuZCjBuznSvOhaI+zfhDWs AFVh86fu/RNdUPeaxbgso5YC9lrkUjZ1hxTbQVRUiQVc70mCrfXo5AU3UYJA+xikCScU Tc4rVgSRmxKYCzcG2wvoPo7n+uMpKV5gTy3UTIm1HvuNx1fYmy90LtrFDteFxBS/RXRG He0cTU+XGTMm47KY2BneLq4svnGQK902Y7dyEHCPNKVrR5coCkHw+U9spGk8uaqD9OfA fupg== 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=NUjqVObLnDIiwVQH9qk2171KT4NM3+X10SMGiY+XeAg=; b=rrDOKHRyTef+ZrH01kRVvBgC5AEuWf+goL4D1K7tZvGEjm4ON6F39GiPgGF/Yn2gXW YC59UTGOffhIbroYIBZHa71XdtH7zjp4x7H2Q2XlB+GyRcY3+nWE/7yH7j2KFCvT0rFP 295++ZhrTJhpLowp3jO0hdoyzZbFufOjVNMbjm37J6GqL+xsLhr64wGjBkUO4eX44CMH I6PKB6Z1za0O9rUBaqwr+aar/RTgEXiN4K7p061zlMgUFVYEewgzcLQXrB1Y2WiCqiTk rCPeGmZ0YQdrqr73YYtYw+Mi7htIbD5eJ0rVRG2SOldYgtkw33jV2R5w2TiBii+0ljNx rXCw== X-Gm-Message-State: AFqh2konBxs/mkNlcX9a16pfzIXjlVvsLs+ED1hyQZzqPOk1F8sSG4xv 8HzUfM4nB/D0knsXHs2U41WfePnxNfI= X-Google-Smtp-Source: AMrXdXsoat0sLUEbPmciAc/VKLkwYCpdpGGtk0smhhbRgVCkx4hEe64re4QEvmqZNbWZ14FFBaABoA== X-Received: by 2002:a17:902:d349:b0:194:75cf:71d7 with SMTP id l9-20020a170902d34900b0019475cf71d7mr4173481plk.0.1673702620458; Sat, 14 Jan 2023 05:23:40 -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.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Jan 2023 05:23:39 -0800 (PST) From: Zhang Boyang To: grub-devel@gnu.org Cc: Zhang Boyang Subject: [PATCH v4 1/2] mm: Adjust new region size to take management overhead into account Date: Sat, 14 Jan 2023 21:23:22 +0800 Message-Id: <20230114132323.132210-2-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:44 -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 overhead into account. Adding a memory area of "size" bytes may result in a heap region of less than "size" bytes truely avaliable. Thus, the new region may not be adequate for current allocation request, confusing out-of-memory handling code. This patch introduces GRUB_MM_MGMT_OVERHEAD to address the region management overhead (e.g. metadata, padding). The value of this new constant must be large enough to make sure grub_malloc(size) always success after a successful call to grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD), for any given addr and size (assuming no interger overflow). The size passed to grub_mm_add_region_fn() is now correctly adjusted, thus if grub_mm_add_region_fn() succeeded, current allocation request can always success. Signed-off-by: Zhang Boyang --- grub-core/kern/mm.c | 64 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index ae2279133..278756dea 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -83,6 +83,46 @@ +/* + * GRUB_MM_MGMT_OVERHEAD is an upper bound of management overhead of + * each region, with any possible padding taken into account. + * + * The value must be large enough to make sure grub_malloc(size) always + * success after a successful call to + * grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD), for any given + * addr and size (assuming no interger overflow). + * + * The worst case which has maximum overhead is shown in the figure below: + * + * +-- addr + * v |<- size ->| + * +---------+----------------+----------------+--------------+---------+ + * | padding | grub_mm_region | grub_mm_header | usable bytes | padding | + * +---------+----------------+----------------+--------------+---------+ + * |<- a ->|<- b ->|<- c ->|<- d ->|<- e ->| + * ^ + * b == sizeof (struct grub_mm_region) +--/ This will be the pointer + * c == sizeof (struct grub_mm_header) | returned by next + * d == size | grub_malloc(size), + * | if no other suitable free + * Assuming addr % GRUB_MM_ALIGN == 1, then \ block is available. + * a == GRUB_MM_ALIGN - 1 + * + * Assuming size % GRUB_MM_ALIGN == 1, then + * e == GRUB_MM_ALIGN - 1 + * + * Therefore, the maximum overhead is: + * a + b + c + e == (GRUB_MM_ALIGN - 1) + sizeof (struct grub_mm_region) + * + sizeof (struct grub_mm_header) + (GRUB_MM_ALIGN - 1) + */ +#define GRUB_MM_MGMT_OVERHEAD ((GRUB_MM_ALIGN - 1) \ + + sizeof (struct grub_mm_region) \ + + sizeof (struct grub_mm_header) \ + + (GRUB_MM_ALIGN - 1)) + +/* The size passed to grub_mm_add_region_fn() is aligned up by this value. */ +#define GRUB_MM_HEAP_GROW_ALIGN 4096 + grub_mm_region_t grub_mm_base; grub_mm_add_region_func_t grub_mm_add_region_fn; @@ -230,6 +270,11 @@ grub_mm_init_region (void *addr, grub_size_t size) grub_dprintf ("regions", "No: considering a new region at %p of size %" PRIxGRUB_SIZE "\n", addr, size); + /* + * If you want to modify the code below, please also take a look at + * GRUB_MM_MGMT_OVERHEAD and make sure it is synchronized with the code. + */ + /* Allocate a region from the head. */ r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN); @@ -410,6 +455,7 @@ 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 grow; int count = 0; if (!grub_mm_base) @@ -418,10 +464,22 @@ grub_memalign (grub_size_t align, grub_size_t size) if (size > ~(grub_size_t) align) goto fail; + /* + * Pre-calculate the necessary 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; + + /* Align up heap growth to make it friendly to CPU/MMU. */ + if (grow > ~(grub_size_t) (GRUB_MM_HEAP_GROW_ALIGN - 1)) + goto fail; + grow = ALIGN_UP (grow, GRUB_MM_HEAP_GROW_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 (grow > ~(grub_size_t) 0x100000) goto fail; align = (align >> GRUB_MM_ALIGN_LOG2); @@ -447,7 +505,7 @@ grub_memalign (grub_size_t align, grub_size_t size) 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 +520,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; } -- 2.30.2