Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pGg0W-00047v-NT for mharc-grub-devel@gnu.org; Sat, 14 Jan 2023 07:51:38 -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 1pGg0M-000466-Gt for grub-devel@gnu.org; Sat, 14 Jan 2023 07:51:27 -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 1pGg0K-00059b-C9 for grub-devel@gnu.org; Sat, 14 Jan 2023 07:51:26 -0500 Received: by mail-pj1-x1033.google.com with SMTP id v13-20020a17090a6b0d00b00219c3be9830so26928819pjj.4 for ; Sat, 14 Jan 2023 04:51:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=X3VFnzoLUl4hKoKplR+vFu339mf+gZxaahUnYSeKhQg=; b=KCtE8Xt/mLVX31Y44xhiv9Qyh7m0rBxl0EJ7H6Pl8qu+ghkTUugQzhqpFp0n2huz6c 9KD3Qeq/CgVq7NuyIQN+j5QmM/n2yCAV4Bp+1KUsqYP8sMhEOxa/0xnnP1QRJYwp96Rc 6XSkcrddAs+XIM7WpbsvBo2Z2NrtacAI1BW2sn/h52FR3ePuEinACiztfr8J2k72kmly XQ6kV4oGjgIK10OzlgCMoIEvoiwmPEyZjqfmRXfqcBy17Q/BMC4fHJ/UWk1/zXQgZesq nbDtUJuZxsZhADI/dveXvaEzB6Z7lYZk33MfCoODa6hJZLNUwkxBETs6mUPV0xANhP/v JVMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=X3VFnzoLUl4hKoKplR+vFu339mf+gZxaahUnYSeKhQg=; b=DVQf8genJFIKJz62e+1+6QHkg8N5MIkCftjfNUnCuiJ4d8w80a8ls+eC3gXnoH6mYB OFXlvaulrLPC8spSxueMnb+/GNlpre+kxmBJbwxT+fReBiaVZmBw9TK5CrnOv1zRMmcn HQ+Nt7nnm8W8NGtBucG+XujTDlIC0mbir+9xGUd2+BNpeT0E32zgTtEubyMBzGIWF484 YSM0DK3zCAmK63LcR6u+o7bcorNnunDwod02N4GEnxe9q9SWwg++S7NdB6othCXJdox/ wtRSa4udj8SyEleCHHvZz71uIzLfl43oG57mPlMWdQFi+AcpFzW3QvVcpQs5RCbfhEdi UnzQ== X-Gm-Message-State: AFqh2kqKkWaRk1xldTtarxNEDgsVGYcDD8xJ3dvCyAzX9raDdZB8IIn1 mONb2dZ2RJ6hCWSHS8sv08XmaZ7ntGA= X-Google-Smtp-Source: AMrXdXuFrYxLAhB1LApHWgjVXPFbULKHgi3j6UKXQffh0IgdRjCBpEBIWXt2rRK3v9vam+3OoCOAbg== X-Received: by 2002:a17:902:868c:b0:194:7ad2:caa6 with SMTP id g12-20020a170902868c00b001947ad2caa6mr2788297plo.23.1673700679523; Sat, 14 Jan 2023 04:51:19 -0800 (PST) Received: from [0.0.0.0] (ec2-13-113-80-70.ap-northeast-1.compute.amazonaws.com. [13.113.80.70]) by smtp.gmail.com with ESMTPSA id p5-20020a170902e74500b001945339354asm6215697plf.197.2023.01.14.04.51.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 14 Jan 2023 04:51:19 -0800 (PST) Message-ID: <1e66e979-1ea3-d12b-0399-b7c866e49ded@gmail.com> Date: Sat, 14 Jan 2023 20:51:16 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [PATCH v3 1/2] mm: Adjust new region size to take management overhead into account Content-Language: en-US To: Daniel Kiper Cc: grub-devel@gnu.org References: <20221222091824.233227-1-zhangboyang.id@gmail.com> <20221222091824.233227-2-zhangboyang.id@gmail.com> <20230112135220.qj33qbqbkudnbwj4@tomti.i.net-space.pl> From: Zhang Boyang In-Reply-To: <20230112135220.qj33qbqbkudnbwj4@tomti.i.net-space.pl> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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, NICE_REPLY_A=-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 12:51:29 -0000 Hi, On 2023/1/12 21:52, Daniel Kiper wrote: > On Thu, Dec 22, 2022 at 05:18:23PM +0800, Zhang Boyang wrote: >> 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, > > This comment is not in line with the code... > Fixed in v4. >> 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 > > s/addr % GRUB_MM_ALIGN == 1/assuming addr % GRUB_MM_ALIGN == 1/ > Fixed in v4. > It took me some time to understand what do you mean by that. > >> + * 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 > > Ditto... > >> + * 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) >> + */ > > Thank you for this comment! It clarifies a lot... > >> +#define GRUB_MM_MGMT_OVERHEAD ((GRUB_MM_ALIGN - 1) \ >> + + sizeof (struct grub_mm_region) \ >> + + sizeof (struct grub_mm_header) \ >> + + (GRUB_MM_ALIGN - 1)) > > It is correct from math POV but the value may not be very friendly to > the CPU/MMU. Please look below for more details... > >> + >> 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; > > This should go before "if ((size + align) > ~(grub_size_t) 0x100000)" > and then you should replace "size" with "grow". Additionally, as I said > above due to GRUB_MM_MGMT_OVERHEAD value final "grow" value may not be > very CPU/MMU friendly. So, I suggest to do ALIGN_UP(grow, 4096) after > grub_add() and before "if ((grow + align) > ...". > Fixed in v4. Please check if my changes matches your intention. >> + >> 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; >> } > > Daniel On 2023/1/12 21:59, Daniel Kiper wrote: > On Thu, Dec 22, 2022 at 05:18:24PM +0800, Zhang Boyang wrote: >> /* >> - * Pre-calculate the size of heap growth (if applicable), >> + * Pre-calculate the optimal 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; >> + grow = grub_max (grow, GRUB_MM_HEAP_GROW_EXTRA); > > If you update the #1 patch as I suggested then this should be > > grow = grub_max (size, GRUB_MM_HEAP_GROW_EXTRA); > I think this shouldn't be changed. If "size" is used here, then "grow" will be irrelevant to GRUB_MM_MGMT_OVERHEAD. > Daniel Best Regards, Zhang Boyang