From nobody Thu Sep 11 21:09:01 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BEBF8C001DB for ; Tue, 8 Aug 2023 16:46:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231222AbjHHQqo (ORCPT ); Tue, 8 Aug 2023 12:46:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231224AbjHHQqA (ORCPT ); Tue, 8 Aug 2023 12:46:00 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5AA8E44475; Tue, 8 Aug 2023 08:56:29 -0700 (PDT) Date: Tue, 08 Aug 2023 08:43:58 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1691484239; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CCsPqlpWJZk2Y6BdMqZeDawq4IwargCWojdv25BU75E=; b=txQcgdi7KG/kLXNoNuqfB20nXY+ch6PSGQ4xpLFT8zr2NPl1eDpHjFlKIE6xQpP1q+6xTf eG0qOpXPLW+8Tl4/2oPP9yI4vs5vsvrFuc6AxpOHkMs0GWDOrGIT7NZYcqvbed5ZWN1f+z WsVMMTMRCWYvgQuBwqmX+KHaJrKsRrMEe17dcDqGCBZnIvjwXUmH/WiuBDRnBLmIPaA9/Y 32w4hSTtGhwFXaBXWbymA6BA0rKl7uewyd7SJIpUEO+ngV2P5qczh9/UpNplsE+CTo8Tke vecyFnAL3Ak42Guhnr7Nx5hEj5/5QG+lm8LcD+D5hNH/AQzrd+aag3BS1Sy+sg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1691484239; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CCsPqlpWJZk2Y6BdMqZeDawq4IwargCWojdv25BU75E=; b=Omu1CWTba3ACvQeNjuQxTISfnTyoSQF2DwEtA3vFlz8Zh2glGuDyGBvzNnjf0Ve1qaBoi3 1nQ2wP2Am2SMRBCg== From: "tip-bot2 for Ard Biesheuvel" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/boot] efi/libstub: Add limit argument to efi_random_alloc() Cc: Ard Biesheuvel , "Borislav Petkov (AMD)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230807162720.545787-22-ardb@kernel.org> References: <20230807162720.545787-22-ardb@kernel.org> MIME-Version: 1.0 Message-ID: <169148423849.27769.4118568530248068899.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/boot branch of tip: Commit-ID: bc5ddceff4c14494d83449ad45c985e6cd353fce Gitweb: https://git.kernel.org/tip/bc5ddceff4c14494d83449ad45c985e6c= d353fce Author: Ard Biesheuvel AuthorDate: Mon, 07 Aug 2023 18:27:18 +02:00 Committer: Borislav Petkov (AMD) CommitterDate: Mon, 07 Aug 2023 21:01:46 +02:00 efi/libstub: Add limit argument to efi_random_alloc() x86 will need to limit the kernel memory allocation to the lowest 512 MiB of memory, to match the behavior of the existing bare metal KASLR physical randomization logic. So in preparation for that, add a limit parameter to efi_random_alloc() and wire it up. Signed-off-by: Ard Biesheuvel Signed-off-by: Borislav Petkov (AMD) Link: https://lore.kernel.org/r/20230807162720.545787-22-ardb@kernel.org --- drivers/firmware/efi/libstub/arm64-stub.c | 2 +- drivers/firmware/efi/libstub/efistub.h | 2 +- drivers/firmware/efi/libstub/randomalloc.c | 10 ++++++---- drivers/firmware/efi/libstub/zboot.c | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/e= fi/libstub/arm64-stub.c index 770b8ec..8c40fc8 100644 --- a/drivers/firmware/efi/libstub/arm64-stub.c +++ b/drivers/firmware/efi/libstub/arm64-stub.c @@ -106,7 +106,7 @@ efi_status_t handle_kernel_image(unsigned long *image_a= ddr, */ status =3D efi_random_alloc(*reserve_size, min_kimg_align, reserve_addr, phys_seed, - EFI_LOADER_CODE); + EFI_LOADER_CODE, EFI_ALLOC_LIMIT); if (status !=3D EFI_SUCCESS) efi_warn("efi_random_alloc() failed: 0x%lx\n", status); } else { diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/= libstub/efistub.h index 06b7abc..9823f6f 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -956,7 +956,7 @@ efi_status_t efi_get_random_bytes(unsigned long size, u= 8 *out); =20 efi_status_t efi_random_alloc(unsigned long size, unsigned long align, unsigned long *addr, unsigned long random_seed, - int memory_type); + int memory_type, unsigned long alloc_limit); =20 efi_status_t efi_random_get_seed(void); =20 diff --git a/drivers/firmware/efi/libstub/randomalloc.c b/drivers/firmware/= efi/libstub/randomalloc.c index 32c7a54..674a064 100644 --- a/drivers/firmware/efi/libstub/randomalloc.c +++ b/drivers/firmware/efi/libstub/randomalloc.c @@ -16,7 +16,8 @@ */ static unsigned long get_entry_num_slots(efi_memory_desc_t *md, unsigned long size, - unsigned long align_shift) + unsigned long align_shift, + u64 alloc_limit) { unsigned long align =3D 1UL << align_shift; u64 first_slot, last_slot, region_end; @@ -29,7 +30,7 @@ static unsigned long get_entry_num_slots(efi_memory_desc_= t *md, return 0; =20 region_end =3D min(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1, - (u64)EFI_ALLOC_LIMIT); + alloc_limit); if (region_end < size) return 0; =20 @@ -54,7 +55,8 @@ efi_status_t efi_random_alloc(unsigned long size, unsigned long align, unsigned long *addr, unsigned long random_seed, - int memory_type) + int memory_type, + unsigned long alloc_limit) { unsigned long total_slots =3D 0, target_slot; unsigned long total_mirrored_slots =3D 0; @@ -76,7 +78,7 @@ efi_status_t efi_random_alloc(unsigned long size, efi_memory_desc_t *md =3D (void *)map->map + map_offset; unsigned long slots; =20 - slots =3D get_entry_num_slots(md, size, ilog2(align)); + slots =3D get_entry_num_slots(md, size, ilog2(align), alloc_limit); MD_NUM_SLOTS(md) =3D slots; total_slots +=3D slots; if (md->attribute & EFI_MEMORY_MORE_RELIABLE) diff --git a/drivers/firmware/efi/libstub/zboot.c b/drivers/firmware/efi/li= bstub/zboot.c index e5d7fa1..bdb17ea 100644 --- a/drivers/firmware/efi/libstub/zboot.c +++ b/drivers/firmware/efi/libstub/zboot.c @@ -119,7 +119,7 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t= *systab) } =20 status =3D efi_random_alloc(alloc_size, min_kimg_align, &image_base, - seed, EFI_LOADER_CODE); + seed, EFI_LOADER_CODE, EFI_ALLOC_LIMIT); if (status !=3D EFI_SUCCESS) { efi_err("Failed to allocate memory\n"); goto free_cmdline;