Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1p823o-0003h1-7c for mharc-grub-devel@gnu.org; Wed, 21 Dec 2022 11:35:17 -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 1p823n-0003fe-1X for grub-devel@gnu.org; Wed, 21 Dec 2022 11:35:15 -0500 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p823l-0003z8-0n for grub-devel@gnu.org; Wed, 21 Dec 2022 11:35:14 -0500 Received: by mail-pj1-x102d.google.com with SMTP id v23so11026615pju.3 for ; Wed, 21 Dec 2022 08:35:12 -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=16bc1WkmedKDh5/L2yVURK97iUfxZ8P6pMBVnV8hAss=; b=YcLPlJ578tKSfUIVv6XaBhGfJ2XRGivbmHWYIDHZFotIt+JQuNCFLjxqImBWem9YoW zP3dWiFJK0XRuHxBpr/T5zvIiT3JmBD44cnFdQx42f9EDoAQDd+NGivQGuVRGj8B1g9X sNiTGBWqsjIbFLwlzccjIl1x8FdlkJmBGPkr11Mrf+hX/tMKhC1CzuK66358tI98N6ir FcY8NpOu7JNGvRTLYMn+lHjAp3wLBBdpzN4hH+O/TTYPxGYcoykrXdbV6zIkxxHfefIy SIdiZDt8TkWZrpzP210BqMYeMkWYFgouFrXBrvIg85CezI8lY6iH5QrByG0al2T43b3G 6P5w== 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=16bc1WkmedKDh5/L2yVURK97iUfxZ8P6pMBVnV8hAss=; b=qJwt+PtF1Z5oB3EYoJyFiubb2S5xkeSL6wpXozlM4kjmvNwibjO2zC0kpQ1rwH9CAN ePv9Cx7au9HfQ7pfEj+Qc6o9SG53L9K0rhtUC627ix5f/SEAJ431sdhCD/lvmTRiEhSz ogO80J/Sc/IPYkWg55+LdrZXYLrozQdsVMN11Vgvw7rrG2+HIPaMM+sRzVZCuAPT8+5A nxnGBicPnkeCYZEO5UnFtD0MSWFzycqKke4GUid/ZuC0qPRhBLDGHgEtF62nzfOlJOAR LHwEj8ccA56lIPg8u6OhUz+1HWpKeTCMwr3SfE0EItk/+OojrUeSnjC/2BJ8szi1uKHd +alw== X-Gm-Message-State: AFqh2kqqMjuvCYZJSi0CmYwo59mtRgbTrvM4VuwrS2vQX1c5FdmZoSPw p5ePVW8QXPj4tApcphZOx09z3bvmT+fvNQ== X-Google-Smtp-Source: AMrXdXuO0+A1EBkFM/AkJT05PVc/GKvvMPNCze8R2TCWNU3tktjQe3+HZl+jc8OpSim3yYppErSAmg== X-Received: by 2002:a05:6a20:65a9:b0:a5:df86:f0e1 with SMTP id p41-20020a056a2065a900b000a5df86f0e1mr19939570pzh.16.1671640510945; Wed, 21 Dec 2022 08:35:10 -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 j5-20020a170903024500b001869f2120a4sm11690101plh.94.2022.12.21.08.35.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 21 Dec 2022 08:35:10 -0800 (PST) Message-ID: <45f9f9e4-52a8-bea6-f461-e7f0147daae6@gmail.com> Date: Thu, 22 Dec 2022 00:35:06 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 Subject: Re: [PATCH v2 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: <78b71224-5956-e806-2638-4f66dca99d08@gmail.com> <20221215111921.132967-2-zhangboyang.id@gmail.com> <20221220171618.soqyybc66vpjbekg@tomti.i.net-space.pl> From: Zhang Boyang In-Reply-To: <20221220171618.soqyybc66vpjbekg@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::102d; envelope-from=zhangboyang.id@gmail.com; helo=mail-pj1-x102d.google.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 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=-1.148, 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: Wed, 21 Dec 2022 16:35:15 -0000 Hi, On 2022/12/21 01:16, Daniel Kiper wrote: > Please do not send your new sets of patches as a reply to previous > discussions. If you do that then it is more difficult to find them > in threaded views. > OK. I will not do that in future. > On Thu, Dec 15, 2022 at 07:19: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 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) > > Please do not sprinkle constants definitions over the code. This does > not help in reading. Additionally, I think these extra constants > make code less readable. > My motivation is to force who want to modify this code notice there is a constant tightly connected to region layout. So he will not forget update the constant if he changes region layout. I agree this will reduce readability. I will replace this with a comment in next version. >> /* 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) > > I do not think this math is correct. Please use mine from previous > reply. When it is wrong please say where exactly it is wrong. OK, I can > agree using "sizeof (struct grub_mm_region)" is more readable than > "sizeof (*grub_mm_region_t)". Anything else? > I have to use "sizeof (struct grub_mm_region)", because "sizeof (*grub_mm_region_t)" won't compile. After rethinking, I think the formula need to be refactored. The purpose of this constant, same as what's its comment says, is to make grub_malloc(size) always success after a successful call to grub_mm_init_region(addr, size + GRUB_MM_MGMT_OVERHEAD). If new region can be merged into an existing region, all of its area can be used for forming a free block (grub_mm_header and user usable bytes). This is the most simple case. Even in this case, GRUB_MM_MGMT_OVERHEAD should be "sizeof(struct grub_mm_header) + (GRUB_MM_ALIGN-1)". This is because "size" may not be integral multiple of GRUB_MM_ALIGN. As GRUB_MM_MGMT_OVERHEAD is a constant designed to be added to "size", we can't use macros like ALIGN_UP. Therefore, we must make GRUB_MM_MGMT_OVERHEAD sufficient large, so "x + GRUB_MM_MGMT_OVERHEAD >= sizeof(struct grub_mm_header) + ALIGN_UP(x, GRUB_MM_ALIGN)" is always true for any "x". The smallest GRUB_MM_MGMT_OVERHEAD would be "sizeof(struct grub_mm_header) + (GRUB_MM_ALIGN-1)". The complex case is, new region is independent and can't be merged into other regions. In this case, a grub_mm_header struct must be formed along with a free block. It looks like: +---------+--------------------+--------------------+------------------+ | padding | grub_mm_region | grub_mm_header | usable bytes | +---------+--------------------+--------------------+------------------+ If we ignore alignment/padding, using the method above, GRUB_MM_MGMT_OVERHEAD would be "sizeof (struct grub_mm_region) + sizeof(struct grub_mm_header) + (GRUB_MM_ALIGN-1)". Unfortunately, the grub_mm_region must be aligned, and the algorithm used by grub_mm_init_region(some_addr) is new_addr=ALIGN_UP(some_addr,GRUB_MM_ALIGN). So we must make GRUB_MM_MGMT_OVERHEAD sufficient large, so "x + GRUB_MM_MGMT_OVERHEAD >= (ALIGN_UP(y,GRUB_MM_ALIGN)-y) + sizeof (struct grub_mm_region) + sizeof(struct grub_mm_header) + (GRUB_MM_ALIGN-1)" is always true for any "x" and any "y". Thus, GRUB_MM_MGMT_OVERHEAD should be "(GRUB_MM_ALIGN-1) + sizeof (struct grub_mm_region) + sizeof(struct grub_mm_header) + (GRUB_MM_ALIGN-1)" (by the way, I miscalculated this component in V2, GRUB_MM_MGMT_OVERHEAD_TAIL_PADDING is not necessary) Finally, we should take max of two GRUB_MM_MGMT_OVERHEAD from 2 cases. So the final value of GRUB_MM_MGMT_OVERHEAD shoule be: (GRUB_MM_ALIGN-1) + sizeof (struct grub_mm_region) + sizeof(struct grub_mm_header) + (GRUB_MM_ALIGN-1) On 2022/12/13 21:48, Daniel Kiper wrote: > > I would define this variable in the following way even if the result > is the same: > > #define GRUB_MM_MGMT_OVERHEAD ALIGN_UP (ALIGN_UP (sizeof (*grub_mm_region_t), GRUB_MM_ALIGN) + > 10 * ALIGN_UP (sizeof (*c), GRUB_MM_ALIGN), 0x1000) > > This way it will be more readable what is happening here, i.e. size of > header of memory region plus size of 10, this is quite arbitrary figure, I didn't understand this formula and it is probably not what GRUB_MM_MGMT_OVERHEAD designed to be. By the way, grub_mm_header and grub_mm_region already aligned to GRUB_MM_ALIGN, so there is no need to ALIGN_UP them. > memory block headers aligned to page size (it would be nice to have > a constant here but it would require more work to make it happen for > all archs; so, let's use a number for this for time being). It would > be nice to have relevant comment before GRUB_MM_MGMT_OVERHEAD > definition too. I don't think it is necessary to align some value to page size. If I understand correctly, grub_mm_init_region() make no assumptions on alignment of its arguments. > And I would define GRUB_MM_MGMT_OVERHEAD constant at the beginning of > the grub-core/kern/mm.c file before grub_mm_base definition. > >> /* 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; > > I think you do not need bound at all. Just use grow... > OK, will fix in next version. Best Regards, Zhang Boyang >> 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; >> } > > Daniel