Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1mpu7N-0005m2-Cz for mharc-grub-devel@gnu.org; Wed, 24 Nov 2021 10:23:29 -0500 Received: from eggs.gnu.org ([209.51.188.92]:53586) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mpu7E-0005f4-U4 for grub-devel@gnu.org; Wed, 24 Nov 2021 10:23:24 -0500 Received: from [2607:f8b0:4864:20::1033] (port=44651 helo=mail-pj1-x1033.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mpu76-0000d3-BI for grub-devel@gnu.org; Wed, 24 Nov 2021 10:23:20 -0500 Received: by mail-pj1-x1033.google.com with SMTP id o6-20020a17090a0a0600b001a64b9a11aeso3015994pjo.3 for ; Wed, 24 Nov 2021 07:23:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=eJVEzbVFguVHH0omemDryadyR/JNe8qS2+a0g6VsPgM=; b=Tm/OSxB4qy4jOz4ppigBtzQ063BK4lCE8HFHISiaPvk8XAWhJAisLE/FRZEIoOaS93 qpNT/6i0HfTW6LHRZ8c5yexN8eN7NWbAYxss+vQ4ZZxO3IQFg2ESKNn7OnrLIq57+cmt e1ukn2nv7bnnwnTFUC4rZ6tzPlHwbwlhaNskk= 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:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=eJVEzbVFguVHH0omemDryadyR/JNe8qS2+a0g6VsPgM=; b=xfprTaE/DAwRSccCv2uDwok2WO8P2cqTeEA5/U63rXLSJwRVMS/fa4jQBfgtA1fari VNQ/Ya0VAAhRf0gyIoieOuGLGMBo4xew3EYDPl+qqG3gZrfdo/bxpIJyARTtEK+GjyEj fT5MfRzsyFpJRnZOs8VEpZjKf7gsEO5wSsfO7OEavpZrLKDMPIs0vF3QF32oqGtkMkzV abQYxQ5JE3naH3Ujwxo6Ppz1xFbFfiqIgsNSMwiSeMnCWSz/eD3YsuiQBMGx8jv9vSJk lBmuWoMC/yb17fctNPyTzb4npeEqrKQSzIwaTrl8sOMtrwjueu621NxcBFVKDTmiBvxv 6zJg== X-Gm-Message-State: AOAM531Zpp6c0AXp0prRViF/LfvQT395AMqUqdJAyQN3/yKThVZtTUQP nn8zRDYaHvK5DPSQN6n+RrWd6LrcXX52DQ== X-Google-Smtp-Source: ABdhPJyndrfGqb7WJfTtBJSP8gXEUivQAbee5Kz6Nl6klRXfxpeMYCWCdkqHRTQFfeV/XbLOuIs2IA== X-Received: by 2002:a17:902:f68e:b0:142:c60:475 with SMTP id l14-20020a170902f68e00b001420c600475mr19911245plg.8.1637767390865; Wed, 24 Nov 2021 07:23:10 -0800 (PST) Received: from localhost ([2001:4479:e000:e400:9826:f987:bef5:b0c4]) by smtp.gmail.com with ESMTPSA id q18sm75763pfn.83.2021.11.24.07.23.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 07:23:10 -0800 (PST) From: Daniel Axtens To: grub-devel@gnu.org Cc: development@efficientek.com, dkiper@net-space.pl, Daniel Axtens Subject: [PATCH 5/6] mm: document grub_mm_init_region Date: Thu, 25 Nov 2021 02:22:49 +1100 Message-Id: <20211124152250.1040794-6-dja@axtens.net> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211124152250.1040794-1-dja@axtens.net> References: <20211124152250.1040794-1-dja@axtens.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::1033 (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::1033; envelope-from=dja@axtens.net; helo=mail-pj1-x1033.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, PDS_HP_HELO_NORDNS=0.001, 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: Wed, 24 Nov 2021 15:23:24 -0000 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. + */ r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN); + /* 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; } -- 2.32.0