Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp2701640imi; Mon, 25 Jul 2022 06:10:38 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v78L457eELstYHQmiXyEpWVXy0vCsIPsXzmejKwiwYoilCvfJO6gyCEaEWl2y8rQRGNH44 X-Received: by 2002:a63:e457:0:b0:419:a843:5b46 with SMTP id i23-20020a63e457000000b00419a8435b46mr10777962pgk.287.1658754638087; Mon, 25 Jul 2022 06:10:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658754638; cv=none; d=google.com; s=arc-20160816; b=u9/fN3DyB6De4dDHmidi5JYVhcSvbE4Q/jDyeAVjNLBQhxo7pUnor2t3aonHNPGCkG dIshhkPYwGtQm1QFicYrkC5FTmaBSdJsM2L6GJ5kA+B2KRCxoGEJsDsKqFYmBWfXpAIc EGMdn8EoTaFDTq8RUj1DgX/jDyK2+yLgX2lg9ZcqgsaOqbZTN1REUqXLGXsw7aKwrYtZ pqRMVk69hOcyLKpIhmYx4MrWYl7IPPvq6pOi7UOouqHn70IwQ4TTq26NGoxTF1kpzEGU pOeDKWg1wa1z5t83eWGk8Q2hEkH+vw3BDqo7m25YaJYcU+t4KhV71/wI/afpIGQQRnxd xXdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:to:subject:message-id :date:from:in-reply-to:references:mime-version:dkim-signature; bh=H9ottcv2Ksz0B+4pJCK3xpnZJpAvCKLgMsQNt3fRODU=; b=GcQ7/sCiSKdz90ufn7Qggu/cmIcDy0OSXeHyG1oA3WGxyuvHQQst9s27DlCBr57n7b n+rpVUAV7XrRL8HzmAoAF4rK+C2WV9R2t928RWn4F0MtM4tYK+j6oAS3lQMnODMdOTQk SRUeHDVGzmaPQDAbklBiGYE4QTl8YG1wkD6M8ZntJv4YIdDk9d09yI/g3FvyN+8HhB1S OqnlSoPUc8vKwIrSHtrnu3xPwRT3rLOpLn2JXt1u1BEo9rqyYLgXPXMiUMlGGy+2VR8J 8vnegn3Ab+e8HFp0Rj0/cv38trPb5QZ9ZAMgmZJi5SaGlWWfd1phw5xVg8AxU7aTPeR8 gM6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=irqzMeXy; 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 c7-20020a170902b68700b0016c432fc9bcsi12318522pls.292.2022.07.25.06.10.23; Mon, 25 Jul 2022 06:10:38 -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=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=irqzMeXy; 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 S235293AbiGYMzz (ORCPT + 99 others); Mon, 25 Jul 2022 08:55:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235076AbiGYMzx (ORCPT ); Mon, 25 Jul 2022 08:55:53 -0400 Received: from mail-io1-xd34.google.com (mail-io1-xd34.google.com [IPv6:2607:f8b0:4864:20::d34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D713DB1FA for ; Mon, 25 Jul 2022 05:55:51 -0700 (PDT) Received: by mail-io1-xd34.google.com with SMTP id e69so8687297iof.5 for ; Mon, 25 Jul 2022 05:55:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-transfer-encoding; bh=H9ottcv2Ksz0B+4pJCK3xpnZJpAvCKLgMsQNt3fRODU=; b=irqzMeXyJAkiNze/TqgZt5WRBDgkHI24zJTp2KdxtJbkBIql0GIz6hlkJbIE/NFIXL QLxBu7UGVKxTCdvJS2SzpaRAwEM5fMwRIki2unw+CoaYltOL3gQmtEGRXmJIUeFKFKET ZLdkq2d9JX1TfED5OzBAfRRdcS8DPZzsnGFd420qxd4XLGq0itPlmXY3aRePxCLEBqzq eGnXCeT38lk06CWOHH6GjJCUv7CUl1stbmbrC2GxAAcTCRlOgw/DzqImyvJ3teBlIc9C si1xxu8X3JDop5H6xVt7580mx7ig0hB4MVDAO/UACy9Ei7AXJo645DIAfKDGo8BaGJT/ ZY1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-transfer-encoding; bh=H9ottcv2Ksz0B+4pJCK3xpnZJpAvCKLgMsQNt3fRODU=; b=i75OkanpflCrFvAhXFJYhhjw+JK5v5xsNNzvgYjOZIrnZ+vyqCPt+F5D/Bk6v7vrW0 0kBWFs1jNOpTa/uw9jiVT/o4VYXbVgt7l1jLqNUCDhKHyjssCfucpwPjMBLPOa+951QI 5UtI9qYLbj6qMuoxmKJs1RMHFrJjoYd5d5BzkAFHAXrGsdXT0TgykmBA58jB8r+tBBHQ svWcBa9v+qg5QgI3UKZLxds3Ce2RCqcM1TDZHc8+xWiQmINTdd0fCoth4mve3Prjduzb d9GVDdW5hW700XponPavKMRvKP5pln1mcLxtZu+JJbwSen1im3C08eVM0Y7u4XePexKG TVTg== X-Gm-Message-State: AJIora+PnifslIuhDnGF3Txiwq2nOa7/Do6qisZbLDZe3bN1NeyJWEa0 sXO3buvc7BGApSRbjYj6+6mKddfUv+wWExppRgfFD28nEqE= X-Received: by 2002:a02:c722:0:b0:33f:393d:d198 with SMTP id h2-20020a02c722000000b0033f393dd198mr4948737jao.22.1658753751071; Mon, 25 Jul 2022 05:55:51 -0700 (PDT) MIME-Version: 1.0 References: <20220725083904.56552-1-huangjie.albert@bytedance.com> <20220725083904.56552-5-huangjie.albert@bytedance.com> In-Reply-To: <20220725083904.56552-5-huangjie.albert@bytedance.com> From: =?UTF-8?B?6buE5p2w?= Date: Mon, 25 Jul 2022 20:55:39 +0800 Message-ID: Subject: Fwd: [PATCH 4/4] x86: boot: avoid memory copy if kernel is uncompressed To: linux-kernel Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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=ham 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 ---------- Forwarded message --------- =E5=8F=91=E4=BB=B6=E4=BA=BA=EF=BC=9A Albert Huang Date: 2022=E5=B9=B47=E6=9C=8825=E6=97=A5=E5=91=A8=E4=B8=80 16:40 Subject: [PATCH 4/4] x86: boot: avoid memory copy if kernel is uncompressed To: Cc: huangjie.albert , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , , 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 , , , From: "huangjie.albert" 1=E3=80=81if kernel is uncompressed. we do not need to relocate kernel image for decompression 2=E3=80=81if 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 o= r * during extract_kernel below. To avoid any issues, repoint the GD= TR diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/mis= c.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] !=3D ELFMAG0 || ehdr.e_ident[EI_MAG1] !=3D ELFMAG1 || ehdr.e_ident[EI_MAG2] !=3D 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 =3D 0; i < ehdr.e_phnum; i++) { phdr =3D &phdrs[i]; @@ -334,7 +334,7 @@ static void parse_elf(void *output) #else dest =3D (void *)(phdr->p_paddr); #endif - memmove(dest, output + phdr->p_offset, phdr->p_file= sz); + memmove(dest, input + phdr->p_offset, phdr->p_files= z); 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