Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1mpu7P-0005tH-QJ for mharc-grub-devel@gnu.org; Wed, 24 Nov 2021 10:23:31 -0500 Received: from eggs.gnu.org ([209.51.188.92]:53596) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mpu7G-0005f8-2w for grub-devel@gnu.org; Wed, 24 Nov 2021 10:23:24 -0500 Received: from [2607:f8b0:4864:20::1031] (port=56085 helo=mail-pj1-x1031.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mpu72-0000cM-4O for grub-devel@gnu.org; Wed, 24 Nov 2021 10:23:21 -0500 Received: by mail-pj1-x1031.google.com with SMTP id v23so2808092pjr.5 for ; Wed, 24 Nov 2021 07:23:02 -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=r1Gkb/zSzQjxUaOMZHHN4OnYSSii95ZVeA1vHUQWHIU=; b=GFtUaWdNZCzNb0inVIFJj/Za/GvXC4MTbZNtwZkwLvQXqjDFYf66riJMHbJ3iHy8Dx g7fe5XiV9pT5pZCFR7gaBBNfF37+xG3CGSKcOD2AJxWQKdY0HjYru1g8WEeuU178oiPw zx9SN09HGSB/wxNvFf0QHfNz93ZJ5E5lOkhBg= 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=r1Gkb/zSzQjxUaOMZHHN4OnYSSii95ZVeA1vHUQWHIU=; b=zz6XEiMAnr82W9QfY8Ybn90OlpPXopbzeA3xbj5O8jygZnJDPvEJsWiWIDYt/EzRGC HNTa0fLiRfnOM5kmMQHahsNQmMKXlNW2P8HV1lVmL3QjIafnrsQX7wwnRxComyygDuPe Co7fwSqBk7m8x6w6Gjsqk6ad/CF9d1BoJsn5+dcotYGKNJm0lrha7MOxAlY+A6vm0L2e rt/T9PtONmJCUn9wJBTmvIr6fWzpE8iV3qNvtRDrk/TOso4cbWDAC5LNAffWM7iKBolY B8Uxfbwft5f0qGv+9AsZDM/GLr/uNqQhD/8SWf8s5nmoglrmoKFTJqrZxIxfTT24t/PV T3mA== X-Gm-Message-State: AOAM531eDeECfE/2Wvu/7FQzBs/EmqQszPvykQG70l3F0Fb+/e5pRa/c OrPKoraAg+46w7kQsnk/XOWbsnftPZ6LkQ== X-Google-Smtp-Source: ABdhPJza9y6APu8wXam1vVmLieVk084lvg8lGjI+gdKHFh3XModiHJwfxZEEVfcurvmp7IWOl0h1Sg== X-Received: by 2002:a17:902:8f93:b0:142:8731:1a5d with SMTP id z19-20020a1709028f9300b0014287311a5dmr19318540plo.60.1637767381140; Wed, 24 Nov 2021 07:23:01 -0800 (PST) Received: from localhost ([2001:4479:e000:e400:9826:f987:bef5:b0c4]) by smtp.gmail.com with ESMTPSA id f5sm101446pju.15.2021.11.24.07.23.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Nov 2021 07:23:00 -0800 (PST) From: Daniel Axtens To: grub-devel@gnu.org Cc: development@efficientek.com, dkiper@net-space.pl, Daniel Axtens Subject: [PATCH 2/6] mm: clarify grub_real_malloc Date: Thu, 25 Nov 2021 02:22:46 +1100 Message-Id: <20211124152250.1040794-3-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::1031 (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=dja@axtens.net; helo=mail-pj1-x1031.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, 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 When iterating through the singly linked list of free blocks, grub_real_malloc uses p and q for the current and previous blocks respectively. This isn't super clear, so swap to using prev and cur. This makes another quirk more obvious. The comment at the top of grub_real_malloc might lead you to believe that the function will allocate from *first if there is space in that block. It actually doesn't do that, and it can't do that with the current data structures. If we used up all of *first, we would need to change the ->next of the previous block to point to *first->next, but we can't do that because it's a singly linked list and we don't have access to *first's previous block. What grub_real_malloc actually does is set *first to the initial previous block, and *first->next is the block we try to allocate from. That allows us to keep all the data structures consistent. Document that. Signed-off-by: Daniel Axtens --- grub-core/kern/mm.c | 76 ++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index c070afc621f8..6efabe92df0e 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -178,13 +178,20 @@ grub_mm_init_region (void *addr, grub_size_t size) } /* Allocate the number of units N with the alignment ALIGN from the ring - buffer starting from *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, - otherwise return NULL. */ + * 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, + * otherwise return NULL. + * + * Note: because in certain circumstances we need to adjust the ->next + * pointer of the previous block, we iterate over the singly linked + * list with the pair (prev, cur). *FIRST is our initial previous, and + * *FIRST->next is our initial current pointer. So we will actually + * allocate from *FIRST->next first and *FIRST itself last. + */ static void * grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align) { - grub_mm_header_t p, q; + grub_mm_header_t cur, prev; /* When everything is allocated side effect is that *first will have alloc magic marked, meaning that there is no room in this region. */ @@ -192,24 +199,24 @@ grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align) return 0; /* Try to search free slot for allocation in this memory region. */ - for (q = *first, p = q->next; ; q = p, p = p->next) + for (prev = *first, cur = prev->next; ; prev = cur, cur = cur->next) { grub_off_t extra; - extra = ((grub_addr_t) (p + 1) >> GRUB_MM_ALIGN_LOG2) & (align - 1); + extra = ((grub_addr_t) (cur + 1) >> GRUB_MM_ALIGN_LOG2) & (align - 1); if (extra) extra = align - extra; - if (! p) + if (! cur) grub_fatal ("null in the ring"); - if (p->magic != GRUB_MM_FREE_MAGIC) - grub_fatal ("free magic is broken at %p: 0x%x", p, p->magic); + if (cur->magic != GRUB_MM_FREE_MAGIC) + grub_fatal ("free magic is broken at %p: 0x%x", cur, cur->magic); - if (p->size >= n + extra) + if (cur->size >= n + extra) { - extra += (p->size - extra - n) & (~(align - 1)); - if (extra == 0 && p->size == n) + extra += (cur->size - extra - n) & (~(align - 1)); + if (extra == 0 && cur->size == n) { /* There is no special alignment requirement and memory block is complete match. @@ -222,9 +229,9 @@ grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align) | alloc, size=n | | +---------------+ v */ - q->next = p->next; + prev->next = cur->next; } - else if (align == 1 || p->size == n + extra) + else if (align == 1 || cur->size == n + extra) { /* There might be alignment requirement, when taking it into account memory block fits in. @@ -241,23 +248,22 @@ grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align) | alloc, size=n | | +---------------+ v */ - - p->size -= n; - p += p->size; + cur->size -= n; + cur += cur->size; } else if (extra == 0) { grub_mm_header_t r; - r = p + extra + n; + r = cur + extra + n; r->magic = GRUB_MM_FREE_MAGIC; - r->size = p->size - extra - n; - r->next = p->next; - q->next = r; + r->size = cur->size - extra - n; + r->next = cur->next; + prev->next = r; - if (q == p) + if (prev == cur) { - q = r; + prev = r; r->next = r; } } @@ -284,32 +290,32 @@ grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align) */ grub_mm_header_t r; - r = p + extra + n; + r = cur + extra + n; r->magic = GRUB_MM_FREE_MAGIC; - r->size = p->size - extra - n; - r->next = p; + r->size = cur->size - extra - n; + r->next = cur; - p->size = extra; - q->next = r; - p += extra; + cur->size = extra; + prev->next = r; + cur += extra; } - p->magic = GRUB_MM_ALLOC_MAGIC; - p->size = n; + cur->magic = GRUB_MM_ALLOC_MAGIC; + cur->size = n; /* Mark find as a start marker for next allocation to fasten it. This will have side effect of fragmenting memory as small pieces before this will be un-used. */ /* So do it only for chunks under 64K. */ if (n < (0x8000 >> GRUB_MM_ALIGN_LOG2) - || *first == p) - *first = q; + || *first == cur) + *first = prev; - return p + 1; + return cur + 1; } /* Search was completed without result. */ - if (p == *first) + if (cur == *first) break; } -- 2.32.0