From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1maCG4-0007ld-KX for mharc-grub-devel@gnu.org; Tue, 12 Oct 2021 03:31:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42750) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maCG3-0007gf-GY for grub-devel@gnu.org; Tue, 12 Oct 2021 03:31:31 -0400 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]:45779) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1maCG1-0007Yi-Gq for grub-devel@gnu.org; Tue, 12 Oct 2021 03:31:31 -0400 Received: by mail-pf1-x431.google.com with SMTP id i65so13942776pfe.12 for ; Tue, 12 Oct 2021 00:31:29 -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=vNBKWnBZ9+kIKMnBoTPbHq6fZ0fa20W31BYsqw3OfWU=; b=hNjl2UJqdqmh9+M2IS5GbRA3nmYDW1IGv2jpVj9QzJ2pCNFt+l+6OxVLhSPFPC3KS+ 0T/jYOFuPtJoAFoiErjlq+Fx7TmVFa0Mmc15kp+Ma1wi0wTBlvME3Kwke6eqdBrXVp9R BEB/LxXw9jhNhW+4Yyd9f3FLXngRaypwMgxOk= 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=vNBKWnBZ9+kIKMnBoTPbHq6fZ0fa20W31BYsqw3OfWU=; b=lM37MMWVzmR0qlkFnkI/AIXhM2ui891FtHuCjSsmm1qP/zXekgDqUivLQGsXkTGGOa FM4ObICDqYN1BBpExn0iQwy8Xi2L73eK9fvLsUvRlWe4FyPSfuw7cv5xMCzmoHrGQQSo /B1D0+CxLeC7+KH8sFMaTPkn3eT56hfqcZIJv+xcVILvaHBlnNH4NEOTaaZsRzQtkk3l pX+LhImGb0qq2Vo7Y5sWj1m6ehqh7cwA6ToD5RnGHY0ZhhM+YibgHzG88R2R7rMsXyvX AMuAxqiUFN8b+3oAHtrlykgrerhPF5+qPY66BQilmNMknrt2ASROQiwsOFldY5yBma3e K2aQ== X-Gm-Message-State: AOAM531xqWPk/ez6A831t4iFFeZ5kWk8yd580DrHYQNiJwAQmI+VlShh hKXbAXXO7Bmv7IatYfm0PpOObrD/zCrPAw== X-Google-Smtp-Source: ABdhPJwJtwCWXlF6l6/FcCWhlXc+pJnAdAxKIMJ2Hq8qp6hIDsVqGFxaAqZom3n1m2s8YFYkTHnUtA== X-Received: by 2002:a62:60c2:0:b0:446:b494:39cc with SMTP id u185-20020a6260c2000000b00446b49439ccmr30260532pfb.22.1634023887793; Tue, 12 Oct 2021 00:31:27 -0700 (PDT) Received: from localhost ([2001:4479:e300:600:41fe:d15:934f:4029]) by smtp.gmail.com with ESMTPSA id i2sm1655395pjg.48.2021.10.12.00.31.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Oct 2021 00:31:27 -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 19/19] RFC: Ignore REGION_CONSECUTIVE Date: Tue, 12 Oct 2021 18:30:08 +1100 Message-Id: <20211012073008.4059133-20-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::431; envelope-from=dja@axtens.net; helo=mail-pf1-x431.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:31:31 -0000 Looking into the region merging stuff, I started to wonder if we actually want to just try adding as much memory as we can (up to the limit requested), even if we cannot satisfy the full allocation (i.e. we are not CONSECUTIVE). On powerpc-ieee1275 this actually allows me to satisfy bigger allocations: while the FW cannot necessarily satisfy e.g. a 128MB allocation, it can satisfy e.g. a 32MB allocation, and that might merge with a region that's 100MB, and then we can satisfy our 128MB allocation. The way the call out to firmware operates in mm.c is that it preceeds a 'goto again;' - it kicks off a whole new round of attempting to satisfy allocation. mm.c doesn't fill the allocation only from the new memory from firmware. Perhaps an even better approach would be for the mm.c code to try going to firmware twice - once for the precise size of the allocation (rounded up etc) and then if that doesn't succeed go back and ask for 'anything you can give me'? Signed-off-by: Daniel Axtens --- grub-core/kern/ieee1275/init.c | 37 +++++++++------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/grub-core/kern/ieee1275/init.c b/grub-core/kern/ieee1275/init.c index f70cb134178b..87c1ab997138 100644 --- a/grub-core/kern/ieee1275/init.c +++ b/grub-core/kern/ieee1275/init.c @@ -186,7 +186,7 @@ count_free (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, static int regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, - unsigned int flags, void *data) + void *data) { grub_uint32_t total = *(grub_uint32_t *)data; grub_uint32_t linux_rmo_save; @@ -257,13 +257,6 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, grub_printf ("now %llx - %llx (vs %x)\n", addr, addr + len, linux_rmo_save); } - if (flags & GRUB_MM_ADD_REGION_CONSECUTIVE) - { - // only continue if we can satisfy the entire allocation - if (len < total) - return 0; - } - if (len > total) len = total; @@ -287,21 +280,6 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, return 0; } -static int -heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, - void *data) -{ - return regions_claim(addr, len, type, GRUB_MM_ADD_REGION_NONE, data); -} - -static int -region_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, - void *data) -{ - return regions_claim(addr, len, type, GRUB_MM_ADD_REGION_CONSECUTIVE, data); -} - - static grub_err_t grub_ieee1275_mm_add_region (grub_size_t size, unsigned int flags) { grub_uint32_t total = size; @@ -325,7 +303,10 @@ static grub_err_t grub_ieee1275_mm_add_region (grub_size_t size, unsigned int fl grub_printf ("ieee1275: free = 0x%x, allocated = 0x%x\n", free_memory, allocated_memory); if (size > free_memory - RUNTIME_MIN_SPACE) - return GRUB_ERR_OUT_OF_MEMORY; + { + // hope region extension saves us + total = free_memory - RUNTIME_MIN_SPACE; + } else { total = grub_max(ALIGN_UP(size, 1024 * 1024) + 1024 * 1024, 32 * 1024 * 1024); @@ -333,13 +314,15 @@ static grub_err_t grub_ieee1275_mm_add_region (grub_size_t size, unsigned int fl } grub_printf ("ieee1275: looking for %x bytes of memory (%x requested)\n", total, size); - grub_machine_mmap_iterate (region_claim, &total); + grub_machine_mmap_iterate (regions_claim, &total); grub_printf ("ieee1275: get memory from fw %s\n", total == 0 ? "succeeded" : "failed"); } else - grub_machine_mmap_iterate (heap_init, &total); + grub_machine_mmap_iterate (regions_claim, &total); - if (total == 0) + // lie a little: if we allocated _anything_ give it another go + // because region merging might have saved the day + if (total != size) return GRUB_ERR_NONE; else return GRUB_ERR_OUT_OF_MEMORY; -- 2.30.2