From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1888FC7EE2C for ; Sun, 7 May 2023 23:46:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1625E6B0087; Sun, 7 May 2023 19:46:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0EB3F6B0088; Sun, 7 May 2023 19:46:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E7FDC6B0089; Sun, 7 May 2023 19:46:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id CF90D6B0087 for ; Sun, 7 May 2023 19:46:47 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A9BBC1401DB for ; Sun, 7 May 2023 23:46:47 +0000 (UTC) X-FDA: 80765096454.06.11E26DF Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by imf29.hostedemail.com (Postfix) with ESMTP id 80F71120002 for ; Sun, 7 May 2023 23:46:45 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="FLflC/PR"; spf=none (imf29.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 192.55.52.115) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1683503205; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=s2l9L+PPZsIlZ0I0XFYzxl9BcDX1nwLLyMqKbsnyUDA=; b=CCPon1Sabv8ajeE7A6UoMbN2gJByLItGcenM6h7h4mE1c4lwwS98ZvLeTACkj0WfewoSdA 8FzEg/6CWVfskUQa1vyPLxqdxzKvtizrFct3C6k72U0+7RejQY70t9B9lmj/xSNZjvpWf+ Hg1rkbCT5rxDoGxXAo1idPulchoEXCg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1683503205; a=rsa-sha256; cv=none; b=gRLEmGxaeuE37dkwcvOYpDiM9Dh0GY4gvY9upd0dRtnYtO8Ga2KJK6U5/83XwboO8iwXgU eYOAeL6d5IA3es7TGELYCPtDKkvvYsjp1euBqJounUYN/y6vjkv/0PCcETjuGORPYqY/Qj FNA2R7brnPjQotzbeQkJ6xnlEHnVoZ8= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="FLflC/PR"; spf=none (imf29.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 192.55.52.115) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683503205; x=1715039205; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Y619tJKuPjwZ/y2uw6gP6ILX0dPH5S7/IjFdg41hRrw=; b=FLflC/PRTE6kxHP+Ng6fnPFNFJya0Zf1sZYFjUoV7UYvp8MmMxlPlN0M k1xchpA3IypdQu83B4hzwNJL0IA6XWjdnFv0zo/JuuXj8QBR90N0x5/r4 9buXRh9tit7cdw8YfrOt9elqQUbDUmfrRUAFETdN8L/dr3Qd882LJkK5A UI19PzXYvWzc5lcRDKvAQWoKiSDb2tXDSlHiok2n8kTSv4iYp6vNoCYD8 Y8DLemfFrmXs9PF7vmOJeMfAoM8HZaaoxZLA86RuMlufojW2dCOXczys/ r2/8oDSmjRldpiqWkLPwkcvZxany5tBcLa4LQpm/O4soaxaAQWyjO1uGW g==; X-IronPort-AV: E=McAfee;i="6600,9927,10703"; a="349550274" X-IronPort-AV: E=Sophos;i="5.99,258,1677571200"; d="scan'208";a="349550274" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2023 16:46:39 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10703"; a="701222808" X-IronPort-AV: E=Sophos;i="5.99,258,1677571200"; d="scan'208";a="701222808" Received: from dancaspi-mobl1.ger.corp.intel.com (HELO box.shutemov.name) ([10.252.61.73]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 May 2023 16:46:31 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 3C28910D1EB; Mon, 8 May 2023 02:46:20 +0300 (+03) From: "Kirill A. Shutemov" To: Borislav Petkov , Andy Lutomirski , Dave Hansen , Sean Christopherson , Andrew Morton , Joerg Roedel , Ard Biesheuvel Cc: Andi Kleen , Kuppuswamy Sathyanarayanan , David Rientjes , Vlastimil Babka , Tom Lendacky , Thomas Gleixner , Peter Zijlstra , Paolo Bonzini , Ingo Molnar , Dario Faggioli , Mike Rapoport , David Hildenbrand , Mel Gorman , marcelo.cerri@canonical.com, tim.gardner@canonical.com, khalid.elmously@canonical.com, philip.cox@canonical.com, aarcange@redhat.com, peterx@redhat.com, x86@kernel.org, linux-mm@kvack.org, linux-coco@lists.linux.dev, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCHv10 05/11] x86/boot/compressed: Handle unaccepted memory Date: Mon, 8 May 2023 02:46:12 +0300 Message-Id: <20230507234618.18067-6-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230507234618.18067-1-kirill.shutemov@linux.intel.com> References: <20230507234618.18067-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 80F71120002 X-Rspam-User: X-Rspamd-Server: rspam06 X-Stat-Signature: qcw138r58m49i6fj9bafaczchc15w7iu X-HE-Tag: 1683503205-374371 X-HE-Meta: U2FsdGVkX199Kpu5hoSpB5c6frLAAgXSLlyHlo67zUeHpwQ3DNK/rVcowWqYwhTjBhibe58019HNNNJyxPp+k9KiGbTaUbLUUkUVg5wC2NrFZSAPUC6h5nYcZC9RI4JZX0ImLXFUFUj8YAZTUmuLm8XoVbDMvsDwFYDYd9caeNloCJWdIh3QeMp8sZ+JI8VMUiiAvmo1zwgZTKBlX/6QiY5v2o5WKa2F2QXChxLgcr66xf2v7mMDP3J1LZ3Qdoil3Isl7uuP1lILNCZmFV6D9YwAB+cm0PXEeQHvVBJPEXB75kN+YOtP8D05TlEtA9s/5FNDgr9lhd0m09evQmoA1bXSlR/j/9fl4vIcB5MOPLEJlSmpiTfClnBKbQs9OYlg8+aVz1sbJdU3/Sg4Y1XVsCRSl8luQyRNjapmjss/qbAmfq1EttJFfI7viCq6FMjjrOwdM2nCGUL1oCekMnNWTef41jS6zgymwH/G7kBBIRT/KxBeu6SyAkg9sncUR3QHjiqX+Qy01dMwZWPkk4nU7kFPk5IgtF0dxtu4ngUImMO6eTrcaVL9+ppyHDbOI7+TOOLwMh81r+UGvOrBmrCWTCJmbLa9Poblxd0HmXEbU5oPwuzNwTiQY1YUlP4cKQYQn+KqCdad+WjL3j79dLJy5TewbSjFAP7tnQqpGAsLDijyOsiSETnM4CLDKQ2+iAYwgp8qonQySvhSZyyczQL/STzBEaN1bfc4qY0jPCd7ArLE6G/o9s+X+WW2CcouyD09PstEUWVdKk0iYkSOfQIpAyrMzKeIt5iikIDlmNR19Rbyws69W9Lx9uVtprmCEAIMS1RgpvhsT79OU22Vf1/jGI8hjTtWTk6cl5+EWBG+BSjL38cLqwTtqnjtVI0gnYXUu0Jf/nqhASqCMywWXQjWElA3JLjVfDmfAqk4HH3d/ZDLDaYGUHpbBUfCI1DWvAyp5JCZfJU0+9d8RGX8HCQ NzuWKeLm sbrUIdMUym8Pvl9Vruh6loWmPt+efjXhlGHqanh/mQb6BHf8lEcUftktoKbYk8RuvmuT9UBoN1BKzfBiFVDZSWauP1qSmyv0khVafxFn2AIQ4jGQsCaGNbcn9j4oCbGcIfVpjK4Rax/DRMFI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The firmware will pre-accept the memory used to run the stub. But, the stub is responsible for accepting the memory into which it decompresses the main kernel. Accept memory just before decompression starts. The stub is also responsible for choosing a physical address in which to place the decompressed kernel image. The KASLR mechanism will randomize this physical address. Since the unaccepted memory region is relatively small, KASLR would be quite ineffective if it only used the pre-accepted area (EFI_CONVENTIONAL_MEMORY). Ensure that KASLR randomizes among the entire physical address space by also including EFI_UNACCEPTED_MEMORY. Signed-off-by: Kirill A. Shutemov --- arch/x86/boot/compressed/Makefile | 2 +- arch/x86/boot/compressed/efi.h | 1 + arch/x86/boot/compressed/kaslr.c | 35 ++++++++++++++++-------- arch/x86/boot/compressed/mem.c | 18 ++++++++++++ arch/x86/boot/compressed/misc.c | 6 ++++ arch/x86/boot/compressed/misc.h | 6 ++++ arch/x86/include/asm/unaccepted_memory.h | 2 ++ 7 files changed, 57 insertions(+), 13 deletions(-) diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile index f62c02348f9a..74f7adee46ad 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -107,7 +107,7 @@ endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o $(obj)/tdcall.o -vmlinux-objs-$(CONFIG_UNACCEPTED_MEMORY) += $(obj)/bitmap.o $(obj)/mem.o +vmlinux-objs-$(CONFIG_UNACCEPTED_MEMORY) += $(obj)/bitmap.o $(obj)/find.o $(obj)/mem.o vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o diff --git a/arch/x86/boot/compressed/efi.h b/arch/x86/boot/compressed/efi.h index 7db2f41b54cd..cf475243b6d5 100644 --- a/arch/x86/boot/compressed/efi.h +++ b/arch/x86/boot/compressed/efi.h @@ -32,6 +32,7 @@ typedef struct { } efi_table_hdr_t; #define EFI_CONVENTIONAL_MEMORY 7 +#define EFI_UNACCEPTED_MEMORY 15 #define EFI_MEMORY_MORE_RELIABLE \ ((u64)0x0000000000010000ULL) /* higher reliability */ diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index 454757fbdfe5..749f0fe7e446 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -672,6 +672,28 @@ static bool process_mem_region(struct mem_vector *region, } #ifdef CONFIG_EFI + +/* + * Only EFI_CONVENTIONAL_MEMORY and EFI_UNACCEPTED_MEMORY (if supported) are + * guaranteed to be free. + * + * It is more conservative in picking free memory than the EFI spec allows: + * + * According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also free memory + * and thus available to place the kernel image into, but in practice there's + * firmware where using that memory leads to crashes. + */ +static inline bool memory_type_is_free(efi_memory_desc_t *md) +{ + if (md->type == EFI_CONVENTIONAL_MEMORY) + return true; + + if (md->type == EFI_UNACCEPTED_MEMORY) + return IS_ENABLED(CONFIG_UNACCEPTED_MEMORY); + + return false; +} + /* * Returns true if we processed the EFI memmap, which we prefer over the E820 * table if it is available. @@ -716,18 +738,7 @@ process_efi_entries(unsigned long minimum, unsigned long image_size) for (i = 0; i < nr_desc; i++) { md = efi_early_memdesc_ptr(pmap, e->efi_memdesc_size, i); - /* - * Here we are more conservative in picking free memory than - * the EFI spec allows: - * - * According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also - * free memory and thus available to place the kernel image into, - * but in practice there's firmware where using that memory leads - * to crashes. - * - * Only EFI_CONVENTIONAL_MEMORY is guaranteed to be free. - */ - if (md->type != EFI_CONVENTIONAL_MEMORY) + if (!memory_type_is_free(md)) continue; if (efi_soft_reserve_enabled() && diff --git a/arch/x86/boot/compressed/mem.c b/arch/x86/boot/compressed/mem.c index 6b15a0ed8b54..de858a5180b6 100644 --- a/arch/x86/boot/compressed/mem.c +++ b/arch/x86/boot/compressed/mem.c @@ -3,12 +3,15 @@ #include "../cpuflags.h" #include "bitmap.h" #include "error.h" +#include "find.h" #include "math.h" #define PMD_SHIFT 21 #define PMD_SIZE (_AC(1, UL) << PMD_SHIFT) #define PMD_MASK (~(PMD_SIZE - 1)) +extern struct boot_params *boot_params; + static inline void __accept_memory(phys_addr_t start, phys_addr_t end) { /* Platform-specific memory-acceptance call goes here */ @@ -71,3 +74,18 @@ void process_unaccepted_memory(struct boot_params *params, u64 start, u64 end) bitmap_set((unsigned long *)params->unaccepted_memory, start / PMD_SIZE, (end - start) / PMD_SIZE); } + +void accept_memory(phys_addr_t start, phys_addr_t end) +{ + unsigned long range_start, range_end; + unsigned long *bitmap, bitmap_size; + + bitmap = (unsigned long *)boot_params->unaccepted_memory; + range_start = start / PMD_SIZE; + bitmap_size = DIV_ROUND_UP(end, PMD_SIZE); + + for_each_set_bitrange_from(range_start, range_end, bitmap, bitmap_size) { + __accept_memory(range_start * PMD_SIZE, range_end * PMD_SIZE); + bitmap_clear(bitmap, range_start, range_end - range_start); + } +} diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c index 014ff222bf4b..186bfd53e042 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -455,6 +455,12 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, #endif debug_putstr("\nDecompressing Linux... "); + + if (boot_params->unaccepted_memory) { + debug_putstr("Accepting memory... "); + accept_memory(__pa(output), __pa(output) + needed_size); + } + __decompress(input_data, input_len, NULL, NULL, output, output_len, NULL, error); entry_offset = parse_elf(output); diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h index 2f155a0e3041..9663d1839f54 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -247,4 +247,10 @@ static inline unsigned long efi_find_vendor_table(struct boot_params *bp, } #endif /* CONFIG_EFI */ +#ifdef CONFIG_UNACCEPTED_MEMORY +void accept_memory(phys_addr_t start, phys_addr_t end); +#else +static inline void accept_memory(phys_addr_t start, phys_addr_t end) {} +#endif + #endif /* BOOT_COMPRESSED_MISC_H */ diff --git a/arch/x86/include/asm/unaccepted_memory.h b/arch/x86/include/asm/unaccepted_memory.h index df0736d32858..41fbfc798100 100644 --- a/arch/x86/include/asm/unaccepted_memory.h +++ b/arch/x86/include/asm/unaccepted_memory.h @@ -7,4 +7,6 @@ struct boot_params; void process_unaccepted_memory(struct boot_params *params, u64 start, u64 num); +void accept_memory(phys_addr_t start, phys_addr_t end); + #endif -- 2.39.3