Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1maCFL-000722-24 for mharc-grub-devel@gnu.org; Tue, 12 Oct 2021 03:30:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42556) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maCFI-0006vD-5r for grub-devel@gnu.org; Tue, 12 Oct 2021 03:30:44 -0400 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]:45939) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1maCFE-0006Sw-Mk for grub-devel@gnu.org; Tue, 12 Oct 2021 03:30:43 -0400 Received: by mail-pj1-x1034.google.com with SMTP id ls14-20020a17090b350e00b001a00e2251c8so1859807pjb.4 for ; Tue, 12 Oct 2021 00:30:39 -0700 (PDT) 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=OBW6PmaOEk314fIVEvpzeVa0Znh2h9ZrrXlKBR4jk3I=; b=MTvFalqm7BdUlF/VxXZ+tKvuJvM3+wfjFm2ag4hHFPiakxn32jgNGcvfYVqVVtKquf cfnBeKgpoApkRCnPXvoU0cfsArbp7CrB0UMtBPDdQ4A32HcfGgmwZzbaRqE6KsI67JpQ 2jB5u3sSeNWcKkJQJXQMGn832kctGo6/sJIpo= 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=OBW6PmaOEk314fIVEvpzeVa0Znh2h9ZrrXlKBR4jk3I=; b=kD7lNnmkO6XleLc8Qpt7wkViwA03BraVsmPvf0MCaIomXDSJY5h/ZU5LOvEjkkuGRg VHU/F0gfAniFl5ZAxzfsxBWBRJhOipewAaeacufHCZXB8KWmQRsDweUOyzZ2K+uffxG8 NgnWBJkLTQM3Y/91S5CG+m0VajdnQo8SrtS5nE/kkg145ojV5Svhv2lzFRKXcn2ftxiz JhWLZcH/Il2WgaKf/pFLmH6BhN9fyRYb/cPM4HXvqBtDrmR5l8+T+fhnH9VZUrNEgirV QBoRl/yFMxJLYWEowpR9Lmi+JHfFKeP8JBMkVjj6pmLPKo41rinnlLS4O1KCQCGXlBd3 2LGg== X-Gm-Message-State: AOAM532C368vIbu2GPLdwjdR9f+fOPSIgWD2TuPIvWglompPfndFDZM5 tNby4RJWF826dov1oA7nVvjmf64Bpf3Mrw== X-Google-Smtp-Source: ABdhPJxekhtSq607uslBZS2vLCH7bMMX9F9n9dXSQq5fjkLr34g1ea2YZnvW/MwWlBd5yg1Cf3MdOw== X-Received: by 2002:a17:90b:3b44:: with SMTP id ot4mr4171288pjb.73.1634023838454; Tue, 12 Oct 2021 00:30:38 -0700 (PDT) Received: from localhost ([2001:4479:e300:600:41fe:d15:934f:4029]) by smtp.gmail.com with ESMTPSA id j8sm9422482pfe.105.2021.10.12.00.30.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Oct 2021 00:30:38 -0700 (PDT) From: Daniel Axtens To: grub-devel@gnu.org Cc: leif@nuviainc.com, stefanb@linux.ibm.com, ps@pks.im, dkiper@net-space.pl, Daniel Axtens Subject: [PATCH 05/19] mm: when adding a region, merge with region after as well as before Date: Tue, 12 Oct 2021 18:29:54 +1100 Message-Id: <20211012073008.4059133-6-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211012073008.4059133-1-dja@axtens.net> References: <20211012073008.4059133-1-dja@axtens.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::1034; envelope-from=dja@axtens.net; helo=mail-pj1-x1034.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, 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.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Oct 2021 07:30:44 -0000 On x86_64-efi (at least) regions seem to be added from top down. The mm code will merge a new region with an existing region that comes immediately before the new region. This allows larger allocations to be satisfied that would otherwise be the case. On powerpc-ieee1275, however, regions are added from bottom up. So if we add 3x 32MB regions, we can still only satisfy a 32MB allocation, rather than the 96MB allocation we might otherwise be able to satisfy. * Define 'post_size' as being bytes lost to the end of an allocation due to being given weird sizes from firmware that are not multiples of GRUB_MM_ALIGN. * Allow merging of regions immediately _after_ existing regions, not just before. As with the other approach, we create an allocated block to represent the new space and the pass it to grub_free() to get the metadata right. Signed-off-by: Daniel Axtens --- grub-core/kern/mm.c | 55 +++++++++++++++++++++++++-------------- include/grub/mm_private.h | 15 +++++++++++ 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index c070afc621f8..835ed8a8f6f9 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -129,25 +129,41 @@ grub_mm_init_region (void *addr, grub_size_t size) size = ((grub_addr_t) -0x1000) - (grub_addr_t) addr; for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p) - if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q) - { - r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN); - *r = *q; - r->pre_size += size; - - if (r->pre_size >> GRUB_MM_ALIGN_LOG2) - { - h = (grub_mm_header_t) (r + 1); - h->size = (r->pre_size >> GRUB_MM_ALIGN_LOG2); - h->magic = GRUB_MM_ALLOC_MAGIC; - r->size += h->size << GRUB_MM_ALIGN_LOG2; - r->pre_size &= (GRUB_MM_ALIGN - 1); - *p = r; - grub_free (h + 1); - } - *p = r; - return; - } + { + /* Does this region come _before_ an existing region? */ + if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q) + { + r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN); + *r = *q; + r->pre_size += size; + + if (r->pre_size >> GRUB_MM_ALIGN_LOG2) + { + h = (grub_mm_header_t) (r + 1); + h->size = (r->pre_size >> GRUB_MM_ALIGN_LOG2); + h->magic = GRUB_MM_ALLOC_MAGIC; + r->size += h->size << GRUB_MM_ALIGN_LOG2; + r->pre_size &= (GRUB_MM_ALIGN - 1); + *p = r; + grub_free (h + 1); + } + *p = r; + return; + } + + /* Does this region come _after_ an existing region? */ + if ((grub_uint8_t *)q + sizeof(*q) + q->size + q->post_size == + (grub_uint8_t *) addr) + { + h = (grub_mm_header_t) ((grub_uint8_t *)addr - q->post_size); + h->size = (size + q->post_size) >> GRUB_MM_ALIGN_LOG2; + h->magic = GRUB_MM_ALLOC_MAGIC; + q->size += h->size << GRUB_MM_ALIGN_LOG2; + q->post_size = (q->post_size + size) & (GRUB_MM_ALIGN - 1); + grub_free (h + 1); + return; + } + } /* Allocate a region from the head. */ r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN); @@ -166,6 +182,7 @@ grub_mm_init_region (void *addr, grub_size_t size) 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; /* Find where to insert this region. Put a smaller one before bigger ones, to prevent fragmentation. */ diff --git a/include/grub/mm_private.h b/include/grub/mm_private.h index 533b47173e18..0effbc45a668 100644 --- a/include/grub/mm_private.h +++ b/include/grub/mm_private.h @@ -74,8 +74,23 @@ typedef struct grub_mm_region */ grub_size_t pre_size; + /* Likewise, the post-size is the number of bytes we wasted at the end + of the allocation because it wasn't a multiple of GRUB_MM_ALIGN + */ + grub_size_t post_size; + /* How many bytes are in this region? (free and allocated) */ grub_size_t size; + + /* pad to a multiple of cell size */ +#if GRUB_CPU_SIZEOF_VOID_P == 4 + char padding[4+4+4]; +#elif GRUB_CPU_SIZEOF_VOID_P == 8 + char padding[8+8+8]; +#else +# error "unknown word size" +#endif + } *grub_mm_region_t; -- 2.30.2