Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nhPJZ-0005jW-IB for mharc-grub-devel@gnu.org; Thu, 21 Apr 2022 01:25:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42798) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nhPJX-0005dJ-Fw for grub-devel@gnu.org; Thu, 21 Apr 2022 01:25:11 -0400 Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]:44002) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nhPJU-0005gH-JG for grub-devel@gnu.org; Thu, 21 Apr 2022 01:25:11 -0400 Received: by mail-pj1-x1033.google.com with SMTP id j8-20020a17090a060800b001cd4fb60dccso4132837pjj.2 for ; Wed, 20 Apr 2022 22:25:08 -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=4HCXLIvAWk8k/XCsLkTdIX0FKDIqd2EYzsVP1Z1mO88=; b=kSQ2bKyr+QtoCrXUDyQkZxm80V+7mp6I/xyt003J84VeSKBmG0HX2Ncow5cdhxKu2E DEC79kvkPMju12ZRvU+PfyTIrU+cf7Pcm1Xz5Mfm4EiODcnVrRXpspKzQg6nbrI3bKtM OZ6OTiON1Z5jCV6XP1t84vwMzHhIdSymuwrxc= 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=4HCXLIvAWk8k/XCsLkTdIX0FKDIqd2EYzsVP1Z1mO88=; b=X2uj6z4vk0oWeAXNk4N15VSd9137MpASGCabaBXQj0qjrZ3OUk/7YENzG/cjY4O1JQ yqUl+vbObM6z8+Zgga3ofbeVHbZ1vwReuOBPUl9qsKs24A856FH2E9Ro17L6OQXrI9a0 qeb4VSfKa+NsYor8VEoApDRm2/WizUCrZfKFODSEyGeNmFjSjoKbjXpEPWNGkJlx2gRT w6bmw8XBvx6y24aIoCLBo2DwNZ+1IOfvJVeggOHlk3F6t5hw0yDplBjmU2qozD01CHly E3hsW5XVg8X29pwccgWZeRczpcDJ24Pmg32Qo6S9Vo9uybjR2ScLQXSAU9ZpUaLBV2/p dd+w== X-Gm-Message-State: AOAM530ppoGIal0PmRgAnrTqplXlwEzN41y0ITFEfSYRB50pMp/yMc9G HSQ3ctEDHY4ZWGQhoiGa2Z5vLBN7SNVsiw== X-Google-Smtp-Source: ABdhPJxcBTLVdNiSR7YVXtenPGdF07UI+ftWsg19ir/zU7Y3S/LCuMO01sMyZqJ5jNSiCHWqUo2hfw== X-Received: by 2002:a17:903:18f:b0:15a:d3e:1e47 with SMTP id z15-20020a170903018f00b0015a0d3e1e47mr11881822plg.54.1650518707083; Wed, 20 Apr 2022 22:25:07 -0700 (PDT) Received: from localhost ([2001:4479:e000:e400:4260:be14:52a8:8c94]) by smtp.gmail.com with ESMTPSA id b13-20020a056a00114d00b004c122b90703sm22686471pfm.27.2022.04.20.22.25.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 22:25:06 -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 , Daniel Kiper Subject: [PATCH v3 09/15] efi: mm: Pass up errors from `add_memory_regions ()` Date: Thu, 21 Apr 2022 15:24:21 +1000 Message-Id: <20220421052427.1389987-10-dja@axtens.net> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220421052427.1389987-1-dja@axtens.net> References: <20220421052427.1389987-1-dja@axtens.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: -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, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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: Thu, 21 Apr 2022 05:25:11 -0000 From: Patrick Steinhardt The function `add_memory_regions ()` is currently only called on system initialization to allocate a fixed amount of pages. As such, it didn't need to return any errors: in case it failed, we cannot proceed anyway. This will change with the upcoming support for requesting more memory from the firmware at runtime, where it doesn't make sense anymore to fail hard. Refactor the function to return an error to prepare for this. Note that this does not change the behaviour when initializing the memory system because `grub_efi_mm_init ()` knows to call `grub_fatal ()` in case `grub_efi_mm_add_regions ()` returns an error. Signed-off-by: Patrick Steinhardt [dja: clarify error messages] Signed-off-by: Daniel Axtens Reviewed-by: Daniel Kiper --- v2: Thank you Glenn and Daniel K for the feedback. This and all EFI changes were not tested on real hardware. --- grub-core/kern/efi/mm.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c index c8d3968f7f68..4e594659a1ff 100644 --- a/grub-core/kern/efi/mm.c +++ b/grub-core/kern/efi/mm.c @@ -478,7 +478,7 @@ filter_memory_map (grub_efi_memory_descriptor_t *memory_map, } /* Add memory regions. */ -static void +static grub_err_t add_memory_regions (grub_efi_memory_descriptor_t *memory_map, grub_efi_uintn_t desc_size, grub_efi_memory_descriptor_t *memory_map_end, @@ -506,9 +506,9 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map, GRUB_EFI_ALLOCATE_ADDRESS, GRUB_EFI_LOADER_CODE); if (! addr) - grub_fatal ("cannot allocate conventional memory %p with %u pages", - (void *) ((grub_addr_t) start), - (unsigned) pages); + return grub_error (GRUB_ERR_OUT_OF_MEMORY, + "Memory starting at %p (%u pages) marked as free, but EFI would not allocate", + (void *) ((grub_addr_t) start), (unsigned) pages); grub_mm_init_region (addr, PAGES_TO_BYTES (pages)); @@ -518,7 +518,11 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map, } if (required_pages > 0) - grub_fatal ("too little memory"); + return grub_error (GRUB_ERR_OUT_OF_MEMORY, + "could not allocate all requested memory: %" PRIuGRUB_UINT64_T " pages still required after iterating EFI memory map", + required_pages); + + return GRUB_ERR_NONE; } void @@ -565,6 +569,7 @@ grub_efi_mm_add_regions (grub_efi_uint64_t required_bytes) grub_efi_memory_descriptor_t *filtered_memory_map_end; grub_efi_uintn_t map_size; grub_efi_uintn_t desc_size; + grub_err_t err; int mm_status; /* Prepare a memory region to store two memory maps. */ @@ -609,8 +614,11 @@ grub_efi_mm_add_regions (grub_efi_uint64_t required_bytes) sort_memory_map (filtered_memory_map, desc_size, filtered_memory_map_end); /* Allocate memory regions for GRUB's memory management. */ - add_memory_regions (filtered_memory_map, desc_size, - filtered_memory_map_end, BYTES_TO_PAGES (required_bytes)); + err = add_memory_regions (filtered_memory_map, desc_size, + filtered_memory_map_end, + BYTES_TO_PAGES (required_bytes)); + if (err != GRUB_ERR_NONE) + return err; #if 0 /* For debug. */ -- 2.32.0