Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp2581559imi; Mon, 25 Jul 2022 02:33:04 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vJ9Nw/PmmSa6PqVO2Gv6q1Qkwypzof1y269rrswIa5ePrH7AnFUQ4Xk2sMbMX6BeKiOD+a X-Received: by 2002:a17:90b:388f:b0:1f2:c9fb:e58a with SMTP id mu15-20020a17090b388f00b001f2c9fbe58amr1702391pjb.162.1658741584355; Mon, 25 Jul 2022 02:33:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658741584; cv=none; d=google.com; s=arc-20160816; b=EN0SA22F3f1VTTdcjmJRCcc6HryaBKs3eI8gzyxvMo8VN08kcpyf3zd+78Ccm4mQN6 jnVsDqqXjUtfN4/gvxlUyeR2mywL015wYZUMz6uZUkv0kzCEFhBLpBDazkHbeGoqJGeh yr8T2lmTA/Tf8Yns90w2q6lcJ+KEuxA/i8Y6FWqis81Xvi8a8RpWSehsr4JBGIyXABTL jq/31Pg83F6mhaTxSvaI0uiODwnskfOTDcCAZl28E0WopPiUIHD6P1SkXhfU9uh/0guk yLyand5bd+m8uh81BEVPDduHddFU2OGqgGHkMl4g8FxBgy6atG5L9Pd51XsROZqlKAPU g1UA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:from :dkim-signature; bh=JRx05kPh3Ii0mpG7hTQJSpxflPSjOu6HTrFUf3L55sY=; b=VO9yoqAVYkiCWYpTJHLnTF3buCyK3x8I/17xZCqa5EiIKRxAaGdK7hWIU8WU+6Ps3X i1mpoVynaBw7oI15QbM4Pw/M6nstddkC6Qtw5OPZ6Gv9Sox0KcEbDjH43DzC93gl4dJI Tsf7QSmhSmvbzBW95p24NBpx/WEr+wvrO0uvjH0BKDfL2didmcZFr2cC0B7Rca4TfGmX C6bmZyEs5GL4uNMr/ZxahFxLre/eedJk7IEZbjUPXLJgD/COG4pii9mVSQQlTPsXSv8z EKW2mriRqiIIZ8NiiZyFoLLKWCl5caH+C0mkoFie3zIxNwvYI9jzqB7bsCDVfU5bVC+3 hJhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b="b/gJd3iU"; 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=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h8-20020a056a00230800b00528a105625esi15673175pfh.316.2022.07.25.02.32.48; Mon, 25 Jul 2022 02:33:04 -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=fail header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b="b/gJd3iU"; 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=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229863AbiGYIkn (ORCPT + 99 others); Mon, 25 Jul 2022 04:40:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233167AbiGYIkl (ORCPT ); Mon, 25 Jul 2022 04:40:41 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DB5AF16 for ; Mon, 25 Jul 2022 01:40:40 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id gq7so9767577pjb.1 for ; Mon, 25 Jul 2022 01:40:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JRx05kPh3Ii0mpG7hTQJSpxflPSjOu6HTrFUf3L55sY=; b=b/gJd3iUl8OERmp+WyI5dC15jE1/7lh4kN0ML2m8TDjjg5+A09Zs5c/jcux/umjCNB E9b2j3m11+23MfAV2gUuOIwI4Tafw/ldONvsGh4u2J8Vpdcvltmgs6t+k5KXpZzo0VZ9 Y1b6DFFSZs/j7GucnJod6cEQ+zQ5+1cbLlm3Gk94Vu/bqLd4ImYU+t3/WYn7oY+2dhKr B5ZHR1BDmNOKRNaElyjk4HYwq9sdhws0NKdtm4Z0V+ls3QKwWfZFpeMSsVggUD+tsR9v ydOTI29C1Ubsavi5EufYYWVC6mJ2f1KC1HCQCHSHAQ8jJXNmCUzdmst8S5xXiUUZic+Q lmXg== 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=JRx05kPh3Ii0mpG7hTQJSpxflPSjOu6HTrFUf3L55sY=; b=jlJm4TKI0Vt4bVvZTu5uddykoH/tnBJfE1YyWCs7ryRu8o4G1RTZz4FGz7xIXv0SbM RDMWKu3q44IDYWr9fI7JBhQNa0ymuWQRphs5OrsqNUb4XfhAiBChvTDFTbY0XWH7f8tz AO/3d4+/G0c8zzIS53QGiBCKLdtdZPw8qMIJ+BChz6MSDKltEoUg9WDziRWfH3JE4G36 DGElONZ49sFuszDni4kMF/ipIZizMLopAXAtjkfOdbQb+yr8HEoCFG4z1k/IUTsQzvj3 OFIn/l0I83o59oSnUXAgs91uhJoeBp9vu6xaTKDRQmuQZ9f73Chor2hDS03Y0H21iDfE SX8w== X-Gm-Message-State: AJIora8d33KnmPY3WNgE8otwHi4ayQVH+2XL0rBWANNRKlWdQ4x3rn5/ w9LpPtIckCIKHo54T1xFrmVgrQ== X-Received: by 2002:a17:902:c401:b0:16d:8109:4f18 with SMTP id k1-20020a170902c40100b0016d81094f18mr2037084plk.32.1658738439453; Mon, 25 Jul 2022 01:40:39 -0700 (PDT) Received: from C02FG34NMD6R.bytedance.net ([139.177.225.241]) by smtp.gmail.com with ESMTPSA id 128-20020a621886000000b0052abc2438f1sm8893479pfy.55.2022.07.25.01.40.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Jul 2022 01:40:39 -0700 (PDT) From: Albert Huang Cc: "huangjie.albert" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Eric Biederman , Masahiro Yamada , Michal Marek , Nick Desaulniers , "Kirill A. Shutemov" , Kuppuswamy Sathyanarayanan , Michael Roth , Nathan Chancellor , Ard Biesheuvel , Mark Rutland , Sean Christopherson , Peter Zijlstra , Kees Cook , Tony Luck , linux-kernel@vger.kernel.org, kexec@lists.infradead.org, linux-kbuild@vger.kernel.org Subject: [PATCH 4/4] x86: boot: avoid memory copy if kernel is uncompressed Date: Mon, 25 Jul 2022 16:38:56 +0800 Message-Id: <20220725083904.56552-5-huangjie.albert@bytedance.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20220725083904.56552-1-huangjie.albert@bytedance.com> References: <20220725083904.56552-1-huangjie.albert@bytedance.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "huangjie.albert" 1、if kernel is uncompressed. we do not need to relocate kernel image for decompression 2、if kaslr is disabled, we do not need to do a memory copy before prase_elf. Two memory copies can be skipped with this patch. this can save aboat 20ms during booting. Signed-off-by: huangjie.albert --- arch/x86/boot/compressed/head_64.S | 8 ++++++-- arch/x86/boot/compressed/misc.c | 22 +++++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index d33f060900d2..9e7770c7047b 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -398,10 +398,13 @@ SYM_CODE_START(startup_64) 1: /* Target address to relocate to for decompression */ +#ifdef CONFIG_KERNEL_UNCOMPRESSED + movq %rbp, %rbx +#else movl BP_init_size(%rsi), %ebx subl $ rva(_end), %ebx addq %rbp, %rbx - +#endif /* Set up the stack */ leaq rva(boot_stack_end)(%rbx), %rsp @@ -522,6 +525,7 @@ trampoline_return: * Copy the compressed kernel to the end of our buffer * where decompression in place becomes safe. */ +#ifndef CONFIG_KERNEL_UNCOMPRESSED pushq %rsi leaq (_bss-8)(%rip), %rsi leaq rva(_bss-8)(%rbx), %rdi @@ -531,7 +535,7 @@ trampoline_return: rep movsq cld popq %rsi - +#endif /* * The GDT may get overwritten either during the copy we just did or * during extract_kernel below. To avoid any issues, repoint the GDTR diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c index c23c0f525d93..d8445562d4e9 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -290,7 +290,7 @@ static inline void handle_relocations(void *output, unsigned long output_len, { } #endif -static void parse_elf(void *output) +static void parse_elf(void *output, void *input) { #ifdef CONFIG_X86_64 Elf64_Ehdr ehdr; @@ -302,7 +302,7 @@ static void parse_elf(void *output) void *dest; int i; - memcpy(&ehdr, output, sizeof(ehdr)); + memcpy(&ehdr, input, sizeof(ehdr)); if (ehdr.e_ident[EI_MAG0] != ELFMAG0 || ehdr.e_ident[EI_MAG1] != ELFMAG1 || ehdr.e_ident[EI_MAG2] != ELFMAG2 || @@ -317,7 +317,7 @@ static void parse_elf(void *output) if (!phdrs) error("Failed to allocate space for phdrs"); - memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum); + memcpy(phdrs, input + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum); for (i = 0; i < ehdr.e_phnum; i++) { phdr = &phdrs[i]; @@ -334,7 +334,7 @@ static void parse_elf(void *output) #else dest = (void *)(phdr->p_paddr); #endif - memmove(dest, output + phdr->p_offset, phdr->p_filesz); + memmove(dest, input + phdr->p_offset, phdr->p_filesz); break; default: /* Ignore other PT_* */ break; } @@ -467,9 +467,21 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, #endif debug_putstr("\nDecompressing Linux... "); + +#ifdef CONFIG_KERNEL_UNCOMPRESSED + if (cmdline_find_option_bool("nokaslr")) { + parse_elf(output, input_data); + } else { + __decompress(input_data, input_len, NULL, NULL, output, output_len, + NULL, error); + parse_elf(output, output); + } +#else __decompress(input_data, input_len, NULL, NULL, output, output_len, NULL, error); - parse_elf(output); + parse_elf(output, output); +#endif + handle_relocations(output, output_len, virt_addr); debug_putstr("done.\nBooting the kernel.\n"); -- 2.31.1