Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pM6C2-0003Up-Au for mharc-grub-devel@gnu.org; Sun, 29 Jan 2023 06:49:54 -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 1pM6C0-0003UT-SE for grub-devel@gnu.org; Sun, 29 Jan 2023 06:49:52 -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 1pM6Bz-0002DS-1D for grub-devel@gnu.org; Sun, 29 Jan 2023 06:49:52 -0500 Received: by mail-pj1-x102f.google.com with SMTP id nn18-20020a17090b38d200b0022bfb584987so8663085pjb.2 for ; Sun, 29 Jan 2023 03:49:49 -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=4wSLmO81MXlP6mBQ6UH4G/uD3d/6Xc+bcpvYieVmuk4=; b=gtHbIBnGUf8neBssyjEFnNTL4+75F2G2mIMQ5RaSIHTHi0qeQnJCf7FgEZkcq6c+Vq HrdxQd7YBBA6VvMrChGuhqF09cLz/P1G5zmot6BbK3gJfiWhUBYrk2/Bnj4r4mkh/RqD Ew6XcT+gdvFxPUsqh5Vdm8vfrfGKbo0G21gNK2ng9ymoN+Kh6gitkbJ46gdhen43A2mr 9KWi9S7jui4On0LND/zJ/V3Y6yitj0TZPkJia4ZaArcC5JuiUyZfdx0qXm3vOe8tkWWs OE//aQRYAzxvBRDEw82RNRMqgIuUIikde/AZc3VKRMXzfRSfATK5E3Q9NZC5QXDo2/JA RJ2A== 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=4wSLmO81MXlP6mBQ6UH4G/uD3d/6Xc+bcpvYieVmuk4=; b=Mx9xTEjYlZfZBC9WQfjIfSdtHUS9f9BWF9yKvXgZZq9h2WdXIusSLEZfko9NkPjO0f uFXnKC38tG6tAsfeg3kSV3Qv8pf9H8IFZOzxL7YaVes80cqXH+zCZofSvptwbUOj+nJZ +0K/axXJhS6fEyykrk17G4TSMMoRH8ulxmwBH46ueTSnxCNUkwu1kc6HlJmI9IbgWc9E 5L1WsXwj/p9jOAHYEwvT9cM+QNskZFt1GJdgXWDRvLp4qrOi4KyyvhBP3jV/HtJh40Jv 3G6vryOfDRVdVE1P3jGi79gi4U4svRYob+oTgSlRbhMpvNm45eaeUzDI4/llCZW5aIXA KbOQ== X-Gm-Message-State: AO0yUKXh3KigNckjryMI7KBZIH+cD91grDyO/oEIjGfEQP6htiasXbFt Syu8XYJCV145fT2x0t2vL1MM27zRJqs= X-Google-Smtp-Source: AK7set/l45IROkFe68SNCiTiRmjzFyNBaOsM5LW8CWfr2uNv2Xk/97GuBQRtgQG31z0QPGwddf3GZQ== X-Received: by 2002:a17:90b:3881:b0:22b:e367:9b2a with SMTP id mu1-20020a17090b388100b0022be3679b2amr5556939pjb.44.1674992988694; Sun, 29 Jan 2023 03:49:48 -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 md24-20020a17090b23d800b0022c1fa729e6sm6019685pjb.7.2023.01.29.03.49.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Jan 2023 03:49:48 -0800 (PST) From: Zhang Boyang To: grub-devel@gnu.org Cc: Zhang Boyang Subject: [PATCH v5 1/3] mm: Adjust new region size to take management overhead into account Date: Sun, 29 Jan 2023 19:49:31 +0800 Message-Id: <20230129114933.192259-2-zhangboyang.id@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230129114933.192259-1-zhangboyang.id@gmail.com> References: <20230129114933.192259-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: Sun, 29 Jan 2023 11:49:53 -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_memalign(align, size) always succeeds after a successful call to grub_mm_init_region(addr, size + align + 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 succeed. 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..6efd59840 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_memalign(align, size) + * always succeeds after a successful call to + * grub_mm_init_region(addr, size + align + GRUB_MM_MGMT_OVERHEAD), + * for any given addr, align and size (assuming no interger overflow). + * + * The worst case which has maximum overhead is shown in the figure below: + * + * +-- addr + * v |<- size + align ->| + * +---------+----------------+----------------+------------------+---------+ + * | padding | grub_mm_region | grub_mm_header | usable bytes | padding | + * +---------+----------------+----------------+------------------+---------+ + * |<- a ->|<- b ->|<- c ->|<- d ->|<- e ->| + * ^ + * b == sizeof (struct grub_mm_region) | / Assuming no other suitable + * c == sizeof (struct grub_mm_header) | | block is available, then: + * d == size + align +-| If align == 0, this will be + * | the pointer returned by next + * Assuming addr % GRUB_MM_ALIGN == 1, then: | grub_memalign(align, size). + * a == GRUB_MM_ALIGN - 1 | If align > 0, this chunk may + * | need to be split to fulfill + * Assuming d % GRUB_MM_ALIGN == 1, then: | alignment requirements, and + * e == GRUB_MM_ALIGN - 1 | the returned pointer may be + * \ inside these usable bytes. + * 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