Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1mwkJn-0007Sd-9E for mharc-grub-devel@gnu.org; Mon, 13 Dec 2021 07:20:36 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60368) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mwkJi-0007Nr-Sy for grub-devel@gnu.org; Mon, 13 Dec 2021 07:20:31 -0500 Received: from [2607:f8b0:4864:20::1032] (port=54042 helo=mail-pj1-x1032.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mwkJh-0001He-4V for grub-devel@gnu.org; Mon, 13 Dec 2021 07:20:30 -0500 Received: by mail-pj1-x1032.google.com with SMTP id iq11so11742283pjb.3 for ; Mon, 13 Dec 2021 04:20:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:in-reply-to:references:date:message-id :mime-version; bh=MVQsVi5DB2F7dtoAgNexnrxa+aSkmEB1yHgU6z2yLro=; b=DJBsbKOk5T/cAmQCWQ4Pv6NbTkf+1RCq65MMe5iMiM7gI8KmONgI/n0El6xkdZXWfV ZU2DQBOtBaH7nqOj7pt0BdjyGzOqmX/b3WRvHce44UDQSC7HklXTak80Ten18IfRZUJb xnG6TYLPFj6Xd0jXME6vBzkUaoIdqLgPGGeE8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=MVQsVi5DB2F7dtoAgNexnrxa+aSkmEB1yHgU6z2yLro=; b=wuM/wBhnSh19Yzddh6UoctixTQldF2a+ye3vI2H2f3LDfNOml+O0iXjxYG81sS3ROw Bb/Pz1KGZv6c2sKzW+b2XQ0ql4WlPOn9UZFO3HdkLGteWeJWl/amhgOSL+ARdpqCdyw+ uitkXQ6JmIwkogdjG8NxOo+M3wAwG3XFoVRnto5kZeJ60HyAajGsxPUSv/Li4HldmmW4 EasT1icv9iarl5vcb3UPrBX5l6NcxIVK9iGQTOW2Y1O8zOOay1VtXAKnS/+xnXkxFFyW YNfxqfdT1NIFzyJsQY+PJop1Br55SsxFrF40ttR+Y2zhiIFkLgTrGR4BU4zMpl7bl5Pi Ux9A== X-Gm-Message-State: AOAM5324eJY1/WesqhTQTd6Ol+scSwspHkZXa9jPX+RRqUB3Lc1j3dEa 6pL4GqKvxT0VWew962AbIARWeQ== X-Google-Smtp-Source: ABdhPJx0rgaVQkSynW2hOaw+VQKYk00NiDd5fPjti7Lyzg6RIrB8+xViNW0azn3/Hjt46voqE/rBxg== X-Received: by 2002:a17:902:8346:b0:142:9e66:2f54 with SMTP id z6-20020a170902834600b001429e662f54mr94756574pln.27.1639398026361; Mon, 13 Dec 2021 04:20:26 -0800 (PST) Received: from localhost ([2001:4479:e000:e400:a43d:31cb:d6e8:7ee7]) by smtp.gmail.com with ESMTPSA id l13sm13255279pfu.149.2021.12.13.04.20.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Dec 2021 04:20:25 -0800 (PST) From: Daniel Axtens To: Daniel Kiper Cc: grub-devel@gnu.org, development@efficientek.com Subject: Re: [PATCH 5/6] mm: document grub_mm_init_region In-Reply-To: <20211208160107.wfzgnb7xfmgsns4o@tomti.i.net-space.pl> References: <20211124152250.1040794-1-dja@axtens.net> <20211124152250.1040794-6-dja@axtens.net> <20211208160107.wfzgnb7xfmgsns4o@tomti.i.net-space.pl> Date: Mon, 13 Dec 2021 23:20:22 +1100 Message-ID: <87o85kd7cp.fsf@dja-thinkpad.axtens.net> MIME-Version: 1.0 Content-Type: text/plain X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::1032 (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::1032; envelope-from=dja@axtens.net; helo=mail-pj1-x1032.google.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 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, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: Mon, 13 Dec 2021 12:20:32 -0000 Daniel Kiper writes: > On Thu, Nov 25, 2021 at 02:22:49AM +1100, Daniel Axtens wrote: >> grub_mm_init_region does some things that seem magical, especially >> around region merging. Make it a bit clearer. >> >> Signed-off-by: Daniel Axtens >> --- >> grub-core/kern/mm.c | 30 +++++++++++++++++++++++++++++- >> 1 file changed, 29 insertions(+), 1 deletion(-) >> >> diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c >> index bec960c18e2b..ef2025f929de 100644 >> --- a/grub-core/kern/mm.c >> +++ b/grub-core/kern/mm.c >> @@ -128,23 +128,51 @@ grub_mm_init_region (void *addr, grub_size_t size) >> if (((grub_addr_t) addr + 0x1000) > ~(grub_addr_t) size) >> size = ((grub_addr_t) -0x1000) - (grub_addr_t) addr; >> >> + /* Attempt to merge this region with every existing region */ >> for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p) >> + /* >> + * Is the new region immediately below an existing region? That >> + * is, is the address of the memory we're adding now (addr) + size >> + * of the memory we're adding (size) + the bytes we couldn't use >> + * at the start of the region we're considering (q->pre_size) >> + * equal to the address of q? In other words, does the memory >> + * looks like this? >> + * >> + * addr q >> + * |----size-----|-q->pre_size-|| >> + */ >> if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q) >> { >> + /* >> + * Yes, we can merge. r is our new region, it's address is the >> + * first GRUB_MM_ALIGNed address above addr. > > I think s/above/below/... > >> + */ >> r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN); We do r = ALIGN_UP(addr, GRUB_MM_ALIGN), so 'above' is actually correct. But that probably indicates that I'm not explaining it well. How does this sound: /* * Yes, we can merge the memory starting at addr into the * existing region from below. Align up addr to GRUB_MM_ALIGN * so that our new region has proper alignment. */ If you want you can change the comment and apply it, otherwise I will submit a v2. Kind regards, Daniel >> + /* Copy the region data across */ >> *r = *q; >> + /* Consider all the new size as pre-size */ >> r->pre_size += size; >> - >> + >> + /* >> + * If we have enough pre-size to create a block, create a >> + * block with it. Mark it as allocated and pass it to >> + * grub_free (), which will sort out getting it into the free >> + * list. >> + */ >> if (r->pre_size >> GRUB_MM_ALIGN_LOG2) >> { >> h = (grub_mm_header_t) (r + 1); >> + /* block size is pre-size converted to cells */ >> h->size = (r->pre_size >> GRUB_MM_ALIGN_LOG2); >> h->magic = GRUB_MM_ALLOC_MAGIC; >> + /* region size grows by block size converted back to bytes */ >> r->size += h->size << GRUB_MM_ALIGN_LOG2; >> + /* adjust pre_size to be accurate */ >> r->pre_size &= (GRUB_MM_ALIGN - 1); >> *p = r; >> grub_free (h + 1); >> } >> + /* Replace the old region with the new region */ >> *p = r; >> return; >> } > > Otherwise LGTM... > > If you are OK with the change above then I can make it for you before > applying the patch. > > Daniel