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 DF325C433EF for ; Tue, 5 Apr 2022 23:50:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FD6E8D0002; Tue, 5 Apr 2022 19:48:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8AB9E8D0001; Tue, 5 Apr 2022 19:48:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 74CD88D0002; Tue, 5 Apr 2022 19:48:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0199.hostedemail.com [216.40.44.199]) by kanga.kvack.org (Postfix) with ESMTP id 654B48D0001 for ; Tue, 5 Apr 2022 19:48:55 -0400 (EDT) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 1DF9C18397F17 for ; Tue, 5 Apr 2022 23:48:45 +0000 (UTC) X-FDA: 79324467810.21.CBF29D1 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by imf08.hostedemail.com (Postfix) with ESMTP id 6A015160027 for ; Tue, 5 Apr 2022 23:48:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1649202524; x=1680738524; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vDtT5eCuwypFPjQBE9Qdn6flkyTeEBWCMmVpOI5Jnq4=; b=lKYngpShbnIwBrVBdw4AmYpMVJmWVFvMD0dadZkPAsCglxE7PNt6ilep bfi5CbofvrD/hA+hhskgbArstAIEBH1+6cdSfP9HkH1ZsEKve2J+rHU8r X/lq9sMt0CKYSi692kOO4fIl0kygwaxEV0VI6Dub9cd1Vqmn57tvvOiwm EVOvy8p6J5V9/R9o7JJkuf3zHVdAaXW56JiKUK3mKZw6Sk15kN5dF8D4T BQvBelSjeINt9RoTbS6ZR28H2XOtxZWgsbS4IhQfSqYIBzwB/EB9qQY/H lr94RPFHZ505gSVS8T/L+GgfeSBBmWeZXSzksXuHKOMbuoD9p59LmtQRH g==; X-IronPort-AV: E=McAfee;i="6200,9189,10308"; a="243035273" X-IronPort-AV: E=Sophos;i="5.90,238,1643702400"; d="scan'208";a="243035273" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Apr 2022 16:48:41 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,238,1643702400"; d="scan'208";a="608658173" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga008.fm.intel.com with ESMTP; 05 Apr 2022 16:48:35 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id B42034B3; Wed, 6 Apr 2022 02:43:47 +0300 (EEST) From: "Kirill A. Shutemov" To: Borislav Petkov , Andy Lutomirski , 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 , Varad Gautam , Dario Faggioli , Dave Hansen , Brijesh Singh , Mike Rapoport , David Hildenbrand , 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: [PATCHv4 4/8] x86/boot/compressed: Handle unaccepted memory Date: Wed, 6 Apr 2022 02:43:39 +0300 Message-Id: <20220405234343.74045-5-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405234343.74045-1-kirill.shutemov@linux.intel.com> References: <20220405234343.74045-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Stat-Signature: hjuzxrmoyu3n7p48agommgnwzbwfp7re X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 6A015160027 Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=lKYngpSh; dmarc=pass (policy=none) header.from=intel.com; spf=none (imf08.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.126) smtp.mailfrom=kirill.shutemov@linux.intel.com X-Rspam-User: X-HE-Tag: 1649202524-120788 Content-Transfer-Encoding: quoted-printable 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: Firmware is responsible for accepting memory where compressed kernel image and initrd land. But kernel has to accept memory for decompression buffer: accept memory just before decompression starts. KASLR is allowed to use unaccepted memory for the output buffer. Signed-off-by: Kirill A. Shutemov --- arch/x86/boot/compressed/bitmap.c | 62 ++++++++++++++++++++ arch/x86/boot/compressed/kaslr.c | 14 ++++- arch/x86/boot/compressed/misc.c | 11 ++++ arch/x86/boot/compressed/unaccepted_memory.c | 14 +++++ arch/x86/include/asm/unaccepted_memory.h | 2 + 5 files changed, 101 insertions(+), 2 deletions(-) diff --git a/arch/x86/boot/compressed/bitmap.c b/arch/x86/boot/compressed= /bitmap.c index bf58b259380a..ba2de61c0823 100644 --- a/arch/x86/boot/compressed/bitmap.c +++ b/arch/x86/boot/compressed/bitmap.c @@ -2,6 +2,48 @@ /* Taken from lib/string.c */ =20 #include +#include +#include + +unsigned long _find_next_bit(const unsigned long *addr1, + const unsigned long *addr2, unsigned long nbits, + unsigned long start, unsigned long invert, unsigned long le) +{ + unsigned long tmp, mask; + + if (unlikely(start >=3D nbits)) + return nbits; + + tmp =3D addr1[start / BITS_PER_LONG]; + if (addr2) + tmp &=3D addr2[start / BITS_PER_LONG]; + tmp ^=3D invert; + + /* Handle 1st word. */ + mask =3D BITMAP_FIRST_WORD_MASK(start); + if (le) + mask =3D swab(mask); + + tmp &=3D mask; + + start =3D round_down(start, BITS_PER_LONG); + + while (!tmp) { + start +=3D BITS_PER_LONG; + if (start >=3D nbits) + return nbits; + + tmp =3D addr1[start / BITS_PER_LONG]; + if (addr2) + tmp &=3D addr2[start / BITS_PER_LONG]; + tmp ^=3D invert; + } + + if (le) + tmp =3D swab(tmp); + + return min(start + __ffs(tmp), nbits); +} =20 void __bitmap_set(unsigned long *map, unsigned int start, int len) { @@ -22,3 +64,23 @@ void __bitmap_set(unsigned long *map, unsigned int sta= rt, int len) *p |=3D mask_to_set; } } + +void __bitmap_clear(unsigned long *map, unsigned int start, int len) +{ + unsigned long *p =3D map + BIT_WORD(start); + const unsigned int size =3D start + len; + int bits_to_clear =3D BITS_PER_LONG - (start % BITS_PER_LONG); + unsigned long mask_to_clear =3D BITMAP_FIRST_WORD_MASK(start); + + while (len - bits_to_clear >=3D 0) { + *p &=3D ~mask_to_clear; + len -=3D bits_to_clear; + bits_to_clear =3D BITS_PER_LONG; + mask_to_clear =3D ~0UL; + p++; + } + if (len) { + mask_to_clear &=3D BITMAP_LAST_WORD_MASK(size); + *p &=3D ~mask_to_clear; + } +} diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/= kaslr.c index 411b268bc0a2..59db90626042 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -725,10 +725,20 @@ process_efi_entries(unsigned long minimum, unsigned= long image_size) * but in practice there's firmware where using that memory leads * to crashes. * - * Only EFI_CONVENTIONAL_MEMORY is guaranteed to be free. + * Only EFI_CONVENTIONAL_MEMORY and EFI_UNACCEPTED_MEMORY (if + * supported) are guaranteed to be free. */ - if (md->type !=3D EFI_CONVENTIONAL_MEMORY) + + switch (md->type) { + case EFI_CONVENTIONAL_MEMORY: + break; + case EFI_UNACCEPTED_MEMORY: + if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY)) + break; continue; + default: + continue; + } =20 if (efi_soft_reserve_enabled() && (md->attribute & EFI_MEMORY_SP)) diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/m= isc.c index fa8969fad011..c1d9d71a6615 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -18,6 +18,7 @@ #include "../string.h" #include "../voffset.h" #include +#include =20 /* * WARNING!! @@ -43,6 +44,9 @@ void *memmove(void *dest, const void *src, size_t n); #endif =20 +#undef __pa +#define __pa(x) ((unsigned long)(x)) + /* * This is set up by the setup-routine at boot-time */ @@ -451,6 +455,13 @@ asmlinkage __visible void *extract_kernel(void *rmod= e, memptr heap, #endif =20 debug_putstr("\nDecompressing Linux... "); + + if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && + 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); parse_elf(output); diff --git a/arch/x86/boot/compressed/unaccepted_memory.c b/arch/x86/boot= /compressed/unaccepted_memory.c index d363acf59c08..3ebab63789bb 100644 --- a/arch/x86/boot/compressed/unaccepted_memory.c +++ b/arch/x86/boot/compressed/unaccepted_memory.c @@ -51,3 +51,17 @@ void mark_unaccepted(struct boot_params *params, u64 s= tart, 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 *unaccepted_memory; + unsigned int rs, re; + + unaccepted_memory =3D (unsigned long *)boot_params->unaccepted_memory; + rs =3D start / PMD_SIZE; + for_each_set_bitrange_from(rs, re, unaccepted_memory, + DIV_ROUND_UP(end, PMD_SIZE)) { + __accept_memory(rs * PMD_SIZE, re * PMD_SIZE); + bitmap_clear(unaccepted_memory, rs, re - rs); + } +} diff --git a/arch/x86/include/asm/unaccepted_memory.h b/arch/x86/include/= asm/unaccepted_memory.h index cbc24040b853..f1f835d3cd78 100644 --- a/arch/x86/include/asm/unaccepted_memory.h +++ b/arch/x86/include/asm/unaccepted_memory.h @@ -9,4 +9,6 @@ struct boot_params; =20 void mark_unaccepted(struct boot_params *params, u64 start, u64 num); =20 +void accept_memory(phys_addr_t start, phys_addr_t end); + #endif --=20 2.35.1