Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nYimx-00068x-9h for mharc-grub-devel@gnu.org; Mon, 28 Mar 2022 02:23:39 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50340) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nYimj-0005zW-7U for grub-devel@gnu.org; Mon, 28 Mar 2022 02:23:27 -0400 Received: from [2607:f8b0:4864:20::1029] (port=56170 helo=mail-pj1-x1029.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nYimg-0005IF-2H for grub-devel@gnu.org; Mon, 28 Mar 2022 02:23:24 -0400 Received: by mail-pj1-x1029.google.com with SMTP id jx9so13058590pjb.5 for ; Sun, 27 Mar 2022 23:23:21 -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=bQXYDGkj2f045ZnLzO2KR3iROFGQ4McGMOwufpNEbZU=; b=M3D38McXURdveaciMYJPHLCjibQMDkiqDP9/roQ01yNoawjqnPd1ugc2NaiZp1Du/E 6J0Y/8UnZYi0NJqefZAxombFdh011uncUxsQuEhvcKjQJW+2x62U7hOUaVbU5XteQT/I HdYqFDnmqKRVwIR7gmnuloVM9WLTWnw5RnHSs= 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=bQXYDGkj2f045ZnLzO2KR3iROFGQ4McGMOwufpNEbZU=; b=OSLnMAvw8Lvg4UaiLVbUo+L/maZlMut5ZMe+XuwmNzB9BRLFaNmY17Z+rwYLh/g4tJ eqt74BRY7raxxws0eKWY8+yRVwdZw9sKCLyFyDqmr9T9ccH2SPEMbESiVHRbhFp0Qcmk bjqjjOFb9G7WYndK/mxmNvvChnV73L7llJuOkstq8dRVGFDyTLrOSxD8qn4JU5A1vY/D 9R0QM+Qh8fKw7JzMvlPWZaEBYHCfn5P4fbUgbVxFM5cpOG6JIBSbGhm95ncPGzs3yuxu TuFhiwD+wQTYDdlzCDtj8hZ4fYvIuq6hFooJ56DxPTRNEPiNiriOU5/rgdmrYB1Ljgkk csgA== X-Gm-Message-State: AOAM530cplw9NTSDUbn0ZOLFDWsi43lI8xWZI33gP+lxDmz/nOJxMI3M ZyhTwAkCg24R/xmJ/irv32NnCL54jbxWlg== X-Google-Smtp-Source: ABdhPJxrbJ6BNYd7yNDMi2QoHh7gtiothaJu5tc9vr8UZBbc6T6I3Flm0wWj+8XdPgyiCfGZeFeC0Q== X-Received: by 2002:a17:902:d346:b0:156:13b1:3bc3 with SMTP id l6-20020a170902d34600b0015613b13bc3mr3185590plk.25.1648448600419; Sun, 27 Mar 2022 23:23:20 -0700 (PDT) Received: from localhost ([2001:4479:e000:e400:743e:bc3e:ec72:bf20]) by smtp.gmail.com with ESMTPSA id g3-20020a056a001a0300b004fa65cbbf4esm14648143pfv.63.2022.03.27.23.23.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Mar 2022 23:23:20 -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 v2 09/15] efi: mm: Pass up errors from `add_memory_regions ()` Date: Mon, 28 Mar 2022 17:22:34 +1100 Message-Id: <20220328062240.878781-10-dja@axtens.net> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220328062240.878781-1-dja@axtens.net> References: <20220328062240.878781-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::1029 (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::1029; envelope-from=dja@axtens.net; helo=mail-pj1-x1029.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: Mon, 28 Mar 2022 06:23:27 -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 --- 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