From patchwork Tue Aug 8 08:43:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 132826 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2368126vqr; Tue, 8 Aug 2023 13:09:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGQFEPei4zr+PqCVAvs85QWabxTyFyKsWKz6azHPxLfn+lkIjXTDLAdanz2C2Y9r49gNzgy X-Received: by 2002:a17:906:328f:b0:99b:f3d1:7718 with SMTP id 15-20020a170906328f00b0099bf3d17718mr533470ejw.28.1691525363208; Tue, 08 Aug 2023 13:09:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691525363; cv=none; d=google.com; s=arc-20160816; b=aMsKcsEBCUvspjHLdzRWPfcY+K7F5EE9SQ4yEuOXxsOdTTEw1x4VhCDuLFVZNgT5G8 oRJL3lrDKZuIuQtnnfuegBncji9CXV+ESAfdxfKwGWpQWfKbVRUC5NFTpwHfUNSFbCAt G3YrJBCYM44Swffh4B1yH65zE/Hd2veDCPSPNTdvatSPhSQs+vvxVvWGE8Eu3cn1zGZS 2aP+PVRvBaOzosDPSiOky7dUXyBZuZl32h4lJbMC79BhL4qvvZZBvh4rG2/P0EHGaPnF bcw8Rhv1IxhNiaprQmeSy8W/lWKTXDuMFbFgOvyB788DgWfSaJi5NkMpmb/CppGrc/al VogQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=CCsPqlpWJZk2Y6BdMqZeDawq4IwargCWojdv25BU75E=; fh=upm4t26Ojn/J6N+Q9k8Qqzx52VymNDAlYlARzLeNh5s=; b=Z4rwL6qWpqfICGKboMObJ/2AIjk8egVBrK891zo842d6N62JzFmAapiwOEkKGRaMTt ATqCAt5c86AqU3iw95oUgYaQTdT4DUVEHxTCxa/XzZVJC7TeqQA/Y/bd+yPEcqg9k+GN 2ksZvAxrdTJlmwYTIVQt+egXJ703HfrVNJf1OdFwVGh0O7xjgS817rAz3+ixcr0Wze7M S4h9B5iNxuNCjxUjBJLHxKoGwBdSXL90zw33y8rni5JRzvPURhcG2AWCF2x2FCXhn6K8 UOW27bOcLVUpN+e70/3jxfYbsppHp4TDne7AEcIWzdSH6gpjUQMrtcR6lzUpHiD5ul9F ZLlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=txQcgdi7; dkim=neutral (no key) header.i=@linutronix.de header.b=Omu1CWTb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v8-20020a170906180800b009928b4e3b9esi8280576eje.313.2023.08.08.13.08.59; Tue, 08 Aug 2023 13:09:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=txQcgdi7; dkim=neutral (no key) header.i=@linutronix.de header.b=Omu1CWTb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233482AbjHHQqo (ORCPT + 99 others); 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 X-Spam-Status: No, score=-0.6 required=5.0 tests=BAYES_00,DATE_IN_PAST_06_12, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773590135776284941 X-GMAIL-MSGID: 1773692899030664207 The following commit has been merged into the x86/boot branch of tip: Commit-ID: bc5ddceff4c14494d83449ad45c985e6cd353fce Gitweb: https://git.kernel.org/tip/bc5ddceff4c14494d83449ad45c985e6cd353fce 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/efi/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_addr, */ status = efi_random_alloc(*reserve_size, min_kimg_align, reserve_addr, phys_seed, - EFI_LOADER_CODE); + EFI_LOADER_CODE, EFI_ALLOC_LIMIT); if (status != 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, u8 *out); 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); efi_status_t efi_random_get_seed(void); 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 = 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; region_end = min(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1, - (u64)EFI_ALLOC_LIMIT); + alloc_limit); if (region_end < size) return 0; @@ -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 = 0, target_slot; unsigned long total_mirrored_slots = 0; @@ -76,7 +78,7 @@ efi_status_t efi_random_alloc(unsigned long size, efi_memory_desc_t *md = (void *)map->map + map_offset; unsigned long slots; - slots = get_entry_num_slots(md, size, ilog2(align)); + slots = get_entry_num_slots(md, size, ilog2(align), alloc_limit); MD_NUM_SLOTS(md) = slots; total_slots += slots; if (md->attribute & EFI_MEMORY_MORE_RELIABLE) diff --git a/drivers/firmware/efi/libstub/zboot.c b/drivers/firmware/efi/libstub/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) } status = efi_random_alloc(alloc_size, min_kimg_align, &image_base, - seed, EFI_LOADER_CODE); + seed, EFI_LOADER_CODE, EFI_ALLOC_LIMIT); if (status != EFI_SUCCESS) { efi_err("Failed to allocate memory\n"); goto free_cmdline;