Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1p8Hix-0008VH-5X for mharc-grub-devel@gnu.org; Thu, 22 Dec 2022 04:18:47 -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 1p8Hiv-0008Uk-40 for grub-devel@gnu.org; Thu, 22 Dec 2022 04:18:45 -0500 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p8Hit-0008FH-C8 for grub-devel@gnu.org; Thu, 22 Dec 2022 04:18:44 -0500 Received: by mail-pf1-x430.google.com with SMTP id c13so807016pfp.5 for ; Thu, 22 Dec 2022 01:18:42 -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=zn2+BgEg8/9cWvti/zu+hWHJx/mC4m4wElCjuaBQlz0=; b=cy8N7xSaJf/3aDVyMEFMx/d5s3ZH79ZXpdD2tr4L/ffvMXg5VOhCWbRNdldYZOXz0J 5kAD/+vheGqgIq0In7LZmhdFn1OAdwHC9zIYWYI8/E2w3dVRbGQXTs8pHIiCc1Xdv1Ws Fk8OsbF2BTivYTBzJMBrU2d0UbCm/T1W1MxbgU2WJDT9iZzF/tXysnoMI8p1rTsKGNbE uzdEuhaJjDjLZQ5cMqmIrQLW54Rvv2grk+T+bf8Ni0YSmLoVXB3YzQdEKkfMBhvfqeO6 BnWXvBDxniOO5mwXe6dtVCiqminq22HVjlo4ITed5n/NTm2mDb39TCwcfJS/uDfIPchq 8gxQ== 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=zn2+BgEg8/9cWvti/zu+hWHJx/mC4m4wElCjuaBQlz0=; b=Lfn7Hvyg5GE5lYlaGfnY01qKEzaSxOgQjqawi54pLsUK4pxg43Kda1Hluu8FremOqr DqJuB6RpptC7MQfNBiqpAj6PY3DtNFQtB9Fj77IEEPYnPbpjTFU7lncsIB4IacbiBcvL k9Tt4Do7uNva3uDDJKYwIqd6FhMoiqpXXkh7HDxy2CnYTdoHyInsSkVeJjKvmjJtGoCb jYwIeCsHhUMbuSvRDLoKj3x5SJNkoqjYbwqj58cP8/2PuVp1UKCY0HueF/y2QZRlMN8+ 1TjY3ft1ZU4DBJuriUrb2/2JnpkjBkm6rSmothH8tYWkNEGDkTTsCDYxMSWj3FhZOfxi PgnQ== X-Gm-Message-State: AFqh2kq+dYep8I57fKwz8vkwt3l2Rv+JuNGRcuUAerZ3lLA6fsXaMkyP AOPpRwasBemU8W6PJ1yrkgr8qfs3zAumEw== X-Google-Smtp-Source: AMrXdXsHDRAlfJXObEo+XC0TnwHJKvqtaeaJ3CKrDGg7/KkpotxPfu7rGBLHU5ZIRt93g+kmZz356A== X-Received: by 2002:a62:1d56:0:b0:56d:dd2a:c906 with SMTP id d83-20020a621d56000000b0056ddd2ac906mr6125982pfd.5.1671700721994; Thu, 22 Dec 2022 01:18:41 -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.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Dec 2022 01:18:41 -0800 (PST) From: Zhang Boyang To: grub-devel@gnu.org Cc: dkiper@net-space.pl, Zhang Boyang Subject: [PATCH v3 1/2] mm: Adjust new region size to take management overhead into account Date: Thu, 22 Dec 2022 17:18:23 +0800 Message-Id: <20221222091824.233227-2-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::430; envelope-from=zhangboyang.id@gmail.com; helo=mail-pf1-x430.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:45 -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 set to "size + align + 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 | 54 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index ae2279133..5c0e5bbbf 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -83,6 +83,43 @@ +/* + * 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 ->| + * ^ + * addr % GRUB_MM_ALIGN == 1 +--/ This will be the pointer + * a == GRUB_MM_ALIGN - 1 | returned by next + * | grub_malloc(size), + * b == sizeof (struct grub_mm_region) | if no other suitable free + * c == sizeof (struct grub_mm_header) \ block is available. + * + * size % GRUB_MM_ALIGN == 1 + * d == size + * 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)) + grub_mm_region_t grub_mm_base; grub_mm_add_region_func_t grub_mm_add_region_fn; @@ -230,6 +267,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 +452,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) @@ -424,6 +467,13 @@ grub_memalign (grub_size_t align, grub_size_t size) if ((size + align) > ~(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 (size + align, GRUB_MM_MGMT_OVERHEAD, &grow)) + goto fail; + align = (align >> GRUB_MM_ALIGN_LOG2); if (align == 0) align = 1; @@ -447,7 +497,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 +512,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