Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1p5mJb-0008OP-74 for mharc-grub-devel@gnu.org; Thu, 15 Dec 2022 06:22:15 -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 1p5mJZ-0008O7-C0 for grub-devel@gnu.org; Thu, 15 Dec 2022 06:22:13 -0500 Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p5mJX-0002ow-Lx for grub-devel@gnu.org; Thu, 15 Dec 2022 06:22:13 -0500 Received: by mail-pj1-x1033.google.com with SMTP id z8-20020a17090abd8800b00219ed30ce47so2393024pjr.3 for ; Thu, 15 Dec 2022 03:22:11 -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=O9jC1mfa7eB3SNBnBXZRaQjO3yGS5mglE7K7Sw+w6k4=; b=FTPchomlajl8Vt8wqcdKku2ChkvI3ty/DZWYfgW7u8X2bklmFPFpUJnCU05SRNLssO V7+AA61+iFKBjDi6SQwKeA/f7b3Qvqq2ARN2QNSd15Qb2rtx4V7x/Ho1WSezQWr0X7Ug isqOuN0wk6BDxWsnf+zE0OPuXOnp0UmOyfChNoEbBwjqknzg0/rDmm0B2iUfouyq58IO fUEEigndbnJo6exSXtDIoNDuA4UVwrS7aqfSLJcY10rWv3JpriP/GlYatayGkNRiAb0T 8LIS7CQ57TFpRrUes24hatVqsLcI8mz5Cq6RkUi2H42IRlkiPA2iP1zFvCEPIkGKjUDg 6P2w== 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=O9jC1mfa7eB3SNBnBXZRaQjO3yGS5mglE7K7Sw+w6k4=; b=m6H7PJT+R/eWNjXqUWHYMTWQkAKojH4yJVw/9SRwWOhcYIAxKdxd0vQ+fGeJ64PtG3 ipfptuA0RlU/tvfR8NGZdPKCP2phYhQbr3vbzL5IoNh75bn3hvmHDUKisP9T1+99kXM7 f5s9kcem+eKjpFb+7RVK3KKpddFfbs43UHdpCEZvyWY61AMMenHmNlKOp4iKxDF27P0/ x6AtDcadQqW9e8nctLRGh1DYkg0JeN+Vv+OJpgCnrzBq2imXsp91YddAed1SLORa+0RF LrNVi1ZxKNXBWOUugJLZLPynRyUVMeedCLCmbhIG5qua9X3tLf3AfaeX4gAU53Fzn/nr 3TpA== X-Gm-Message-State: ANoB5pl1FunesO93HC1AmoNs8guovJT4hBnZ5zdGTMUdEFH5HLm3Bb/f ZkqtqwNCcA7CT+aOr/8ov4LIO5q2KyY= X-Google-Smtp-Source: AA0mqf6YCD49VF5V155l9p5SoAtrN45nLJPU0pu4hFfZg0HDCO9w9STVNFNyTAF3d1zqDIU9kuW8BA== X-Received: by 2002:a17:902:ce10:b0:189:e577:c842 with SMTP id k16-20020a170902ce1000b00189e577c842mr29879156plg.45.1671103330291; Thu, 15 Dec 2022 03:22:10 -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 z5-20020a1709027e8500b00176a2d23d1asm3589386pla.56.2022.12.15.03.22.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Dec 2022 03:22:09 -0800 (PST) From: Zhang Boyang To: grub-devel@gnu.org Cc: Zhang Boyang , dkiper@net-space.pl Subject: [PATCH v2 1/2] mm: Adjust new region size to take management overhead into account Date: Thu, 15 Dec 2022 19:19:23 +0800 Message-Id: <20221215111921.132967-2-zhangboyang.id@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <78b71224-5956-e806-2638-4f66dca99d08@gmail.com> References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::1033; envelope-from=zhangboyang.id@gmail.com; helo=mail-pj1-x1033.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, 15 Dec 2022 11:22:13 -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 will 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 should make grub_malloc(size) always success after a successful call to grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD). The new region size is now set to "size + GRUB_MM_MGMT_OVERHEAD", thus if grub_mm_add_region_fn() succeeded, current allocation request can always success. Signed-off-by: Zhang Boyang --- grub-core/kern/mm.c | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index ae2279133..20bb54dde 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -232,6 +232,7 @@ grub_mm_init_region (void *addr, grub_size_t size) addr, size); /* Allocate a region from the head. */ r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN); +#define GRUB_MM_MGMT_OVERHEAD_HEAD_PADDING (GRUB_MM_ALIGN - 1) /* If this region is too small, ignore it. */ if (size < GRUB_MM_ALIGN + (char *) r - (char *) addr + sizeof (*r)) @@ -239,15 +240,18 @@ grub_mm_init_region (void *addr, grub_size_t size) size -= (char *) r - (char *) addr + sizeof (*r); +#define GRUB_MM_MGMT_OVERHEAD_MM_HEADER sizeof (struct grub_mm_header) h = (grub_mm_header_t) (r + 1); h->next = h; h->magic = GRUB_MM_FREE_MAGIC; h->size = (size >> GRUB_MM_ALIGN_LOG2); +#define GRUB_MM_MGMT_OVERHEAD_REGION_HEADER sizeof (struct grub_mm_region) r->first = h; r->pre_size = (grub_addr_t) r - (grub_addr_t) addr; r->size = (h->size << GRUB_MM_ALIGN_LOG2); r->post_size = size - r->size; +#define GRUB_MM_MGMT_OVERHEAD_TAIL_PADDING (GRUB_MM_ALIGN - 1) /* Find where to insert this region. Put a smaller one before bigger ones, to prevent fragmentation. */ @@ -259,6 +263,17 @@ grub_mm_init_region (void *addr, grub_size_t size) r->next = q; } +/* + * GRUB_MM_MGMT_OVERHEAD is an upper bound of management overhead of + * each region, with any possible 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_MGMT_OVERHEAD). + */ +#define GRUB_MM_MGMT_OVERHEAD (GRUB_MM_MGMT_OVERHEAD_HEAD_PADDING + \ + GRUB_MM_MGMT_OVERHEAD_REGION_HEADER + \ + GRUB_MM_MGMT_OVERHEAD_MM_HEADER + \ + GRUB_MM_MGMT_OVERHEAD_TAIL_PADDING) + /* Allocate the number of units N with the alignment ALIGN from the ring * buffer given in *FIRST. ALIGN must be a power of two. Both N and * ALIGN are in units of GRUB_MM_ALIGN. Return a non-NULL if successful, @@ -410,6 +425,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 bound, grow; int count = 0; if (!grub_mm_base) @@ -418,10 +434,25 @@ grub_memalign (grub_size_t align, grub_size_t size) if (size > ~(grub_size_t) align) goto fail; + /* + * If we have a free chunk whose real size is bigger than or equal to + * "size + align", the alignment requirement can always be satisfied. + * However, this is an overestimate. The allocation may still success even if + * all free chunks are smaller than "size + align". + */ + 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; + + /* + * Pre-calculate the size of heap growth (if applicable), + * with region management overhead taken into account. + */ + if (grub_add (bound, GRUB_MM_MGMT_OVERHEAD, &grow)) goto fail; align = (align >> GRUB_MM_ALIGN_LOG2); @@ -447,7 +478,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 +493,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