From nobody Tue Apr 7 13:28:14 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 53E16ECAAD5 for ; Sat, 27 Aug 2022 02:08:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345304AbiH0CIZ (ORCPT ); Fri, 26 Aug 2022 22:08:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232135AbiH0CIO (ORCPT ); Fri, 26 Aug 2022 22:08:14 -0400 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20925C0B64 for ; Fri, 26 Aug 2022 19:08:13 -0700 (PDT) Received: from localhost.localdomain (unknown [80.240.223.29]) by mail.ispras.ru (Postfix) with ESMTPSA id 6BDAE407625D; Sat, 27 Aug 2022 02:08:11 +0000 (UTC) From: Evgeniy Baskov To: Borislav Petkov Cc: Evgeniy Baskov , Dave Hansen , Ingo Molnar , Thomas Gleixner , linux-kernel@vger.kernel.org, x86@kernel.org, Alexey Khoroshilov Subject: [PATCH v6 1/5] x86/boot: Add strlcat() and strscpy() to compressed kernel Date: Sat, 27 Aug 2022 05:08:06 +0300 Message-Id: <147105a124819e88032387d2979fa57e01b2b7d2.1661565218.git.baskov@ispras.ru> X-Mailer: git-send-email 2.37.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" These functions simplify the code of command line concatenation helper and reduce the probability of mistakes. Use simpler implementation of strscpy than used in it kernel itself to avoid code bloat in compressed kernel. Signed-off-by: Evgeniy Baskov --- arch/x86/boot/compressed/string.c | 50 +++++++++++++++++++++++++++++++ arch/x86/purgatory/purgatory.c | 1 + 2 files changed, 51 insertions(+) diff --git a/arch/x86/boot/compressed/string.c b/arch/x86/boot/compressed/s= tring.c index 81fc1eaa3229..5c193fa0a09b 100644 --- a/arch/x86/boot/compressed/string.c +++ b/arch/x86/boot/compressed/string.c @@ -40,6 +40,56 @@ static void *____memcpy(void *dest, const void *src, siz= e_t n) } #endif =20 +size_t strlcat(char *dest, const char *src, size_t count) +{ + size_t dsize =3D strlen(dest); + size_t len =3D strlen(src); + size_t res =3D dsize + len; + + /* This would be a bug */ + if (dsize >=3D count) + error("strlcat(): destination too big\n"); + + dest +=3D dsize; + count -=3D dsize; + if (len >=3D count) + len =3D count-1; + memcpy(dest, src, len); + dest[len] =3D 0; + return res; +} + +/* Don't include word-at-a-time code path in compressed kernel for simplic= ity */ +size_t strscpy(char *dest, const char *src, size_t count) +{ + long res =3D 0; + + if (count =3D=3D 0) + return -E2BIG; + + if (count > INT_MAX) { + warn("strscpy(): Count is too big"); + return -E2BIG; + } + + while (count) { + char c; + + c =3D src[res]; + dest[res] =3D c; + if (!c) + return res; + res++; + count--; + } + + /* Hit buffer length without finding a NUL; force NUL-termination. */ + if (res) + dest[res-1] =3D '\0'; + + return -E2BIG; +} + void *memset(void *s, int c, size_t n) { int i; diff --git a/arch/x86/purgatory/purgatory.c b/arch/x86/purgatory/purgatory.c index 7558139920f8..65f0cedb65ae 100644 --- a/arch/x86/purgatory/purgatory.c +++ b/arch/x86/purgatory/purgatory.c @@ -57,3 +57,4 @@ void purgatory(void) * arch/x86/boot/compressed/string.c */ void warn(const char *msg) {} +void error(char *m) {} --=20 2.37.2 From nobody Tue Apr 7 13:28:14 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5FC2BECAAD4 for ; Sat, 27 Aug 2022 02:08:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345296AbiH0CIV (ORCPT ); Fri, 26 Aug 2022 22:08:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232013AbiH0CIO (ORCPT ); Fri, 26 Aug 2022 22:08:14 -0400 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45877C0B6C for ; Fri, 26 Aug 2022 19:08:13 -0700 (PDT) Received: from localhost.localdomain (unknown [80.240.223.29]) by mail.ispras.ru (Postfix) with ESMTPSA id 7DFD04076267; Sat, 27 Aug 2022 02:08:11 +0000 (UTC) From: Evgeniy Baskov To: Borislav Petkov Cc: Evgeniy Baskov , Dave Hansen , Ingo Molnar , Thomas Gleixner , linux-kernel@vger.kernel.org, x86@kernel.org, Alexey Khoroshilov Subject: [PATCH v6 2/5] x86: Add cmdline_prepare() helper Date: Sat, 27 Aug 2022 05:08:07 +0300 Message-Id: X-Mailer: git-send-email 2.37.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Command line needs to be combined in both compressed and uncompressed kernel from built-in and boot command line strings, which requires non-trivial logic depending on CONFIG_CMDLINE_BOOL and CONFIG_CMDLINE_OVERRIDE. Add a helper function to avoid code duplication. Signed-off-by: Evgeniy Baskov --- arch/x86/include/asm/shared/cmdline.h | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 arch/x86/include/asm/shared/cmdline.h diff --git a/arch/x86/include/asm/shared/cmdline.h b/arch/x86/include/asm/s= hared/cmdline.h new file mode 100644 index 000000000000..a440d0bd6840 --- /dev/null +++ b/arch/x86/include/asm/shared/cmdline.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _ASM_X86_SETUP_CMDLINE_H +#define _ASM_X86_SETUP_CMDLINE_H + +#define _SETUP +#include /* For COMMAND_LINE_SIZE */ +#undef _SETUP + +#include + +#ifdef CONFIG_CMDLINE_BOOL +#define COMMAND_LINE_INIT CONFIG_CMDLINE +#else +#define COMMAND_LINE_INIT "" +#endif + +/* + * command_line and boot_command_line are expected to be at most + * COMMAND_LINE_SIZE length. command_line needs to be initialized + * with COMMAND_LINE_INIT. + */ +static inline void cmdline_prepare(char *command_line, + const char *boot_command_line) +{ +#ifdef CONFIG_CMDLINE_BOOL + if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) { + /* Append boot loader cmdline to builtin */ + strlcat(command_line, " ", COMMAND_LINE_SIZE); + strlcat(command_line, boot_command_line, COMMAND_LINE_SIZE); + } +#else + strscpy(command_line, boot_command_line, COMMAND_LINE_SIZE); +#endif +} + +#endif /* _ASM_X86_SETUP_CMDLINE_H */ --=20 2.37.2 From nobody Tue Apr 7 13:28:14 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2E873ECAAD5 for ; Sat, 27 Aug 2022 02:08:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345366AbiH0CIf (ORCPT ); Fri, 26 Aug 2022 22:08:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232689AbiH0CIO (ORCPT ); Fri, 26 Aug 2022 22:08:14 -0400 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6832EC0E6D for ; Fri, 26 Aug 2022 19:08:13 -0700 (PDT) Received: from localhost.localdomain (unknown [80.240.223.29]) by mail.ispras.ru (Postfix) with ESMTPSA id 906C2407626A; Sat, 27 Aug 2022 02:08:11 +0000 (UTC) From: Evgeniy Baskov To: Borislav Petkov Cc: Evgeniy Baskov , Dave Hansen , Ingo Molnar , Thomas Gleixner , linux-kernel@vger.kernel.org, x86@kernel.org, Alexey Khoroshilov Subject: [PATCH v6 3/5] x86/setup: Use cmdline_prepare() in setup.c Date: Sat, 27 Aug 2022 05:08:08 +0300 Message-Id: <7063609fc7fe8b0a17a855313ff710fe12894cad.1661565218.git.baskov@ispras.ru> X-Mailer: git-send-email 2.37.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Use a common helper function for command line resolving in arch/x86/kernel/setup.c for code unification. Signed-off-by: Evgeniy Baskov --- arch/x86/kernel/setup.c | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index 216fee7144ee..1acea005cad6 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -49,6 +49,7 @@ #include #include #include +#include #include #include #include @@ -166,10 +167,7 @@ unsigned long saved_video_mode; #define RAMDISK_PROMPT_FLAG 0x8000 #define RAMDISK_LOAD_FLAG 0x4000 =20 -static char __initdata command_line[COMMAND_LINE_SIZE]; -#ifdef CONFIG_CMDLINE_BOOL -static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] =3D CONFIG_CMDLI= NE; -#endif +static char command_line[COMMAND_LINE_SIZE] __initdata =3D COMMAND_LINE_IN= IT; =20 #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE) struct edd edd; @@ -969,20 +967,8 @@ void __init setup_arch(char **cmdline_p) bss_resource.start =3D __pa_symbol(__bss_start); bss_resource.end =3D __pa_symbol(__bss_stop)-1; =20 -#ifdef CONFIG_CMDLINE_BOOL -#ifdef CONFIG_CMDLINE_OVERRIDE - strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE); -#else - if (builtin_cmdline[0]) { - /* append boot loader cmdline to builtin */ - strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE); - strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE); - strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE); - } -#endif -#endif - - strscpy(command_line, boot_command_line, COMMAND_LINE_SIZE); + cmdline_prepare(command_line, boot_command_line); + strscpy(boot_command_line, command_line, COMMAND_LINE_SIZE); *cmdline_p =3D command_line; =20 /* --=20 2.37.2 From nobody Tue Apr 7 13:28:14 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0310FECAAD4 for ; Sat, 27 Aug 2022 02:08:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345383AbiH0CIi (ORCPT ); Fri, 26 Aug 2022 22:08:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233391AbiH0CIO (ORCPT ); Fri, 26 Aug 2022 22:08:14 -0400 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C8DFC0E70 for ; Fri, 26 Aug 2022 19:08:13 -0700 (PDT) Received: from localhost.localdomain (unknown [80.240.223.29]) by mail.ispras.ru (Postfix) with ESMTPSA id A0BD4407626E; Sat, 27 Aug 2022 02:08:11 +0000 (UTC) From: Evgeniy Baskov To: Borislav Petkov Cc: Evgeniy Baskov , Dave Hansen , Ingo Molnar , Thomas Gleixner , linux-kernel@vger.kernel.org, x86@kernel.org, Alexey Khoroshilov Subject: [PATCH v6 4/5] x86/boot: Use cmdline_prapare() in compressed kernel Date: Sat, 27 Aug 2022 05:08:09 +0300 Message-Id: <6d220213ad1a90bf905f2ea324dcc8a3db5707ca.1661565218.git.baskov@ispras.ru> X-Mailer: git-send-email 2.37.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" CONFIG_CMDLINE_BOOL and CONFIG_CMDLINE_OVERRIDE were ignored during options lookup in compressed kernel, including earlyprintk option, so it was impossible to get earlyprintk messages from that stage of boot process via command line provided at compile time. Being able to enable earlyprintk via compile-time option might be desirable for booting on systems with broken UEFI command line arguments via EFISTUB. Use a common helper function to handle CONFIG_CMDLINE_* in compressed kernel. Place command_line_init explicitly in '.data' section since it is used before '.bss' section is zeroed and it is expected to be equal to zero. Signed-off-by: Evgeniy Baskov --- arch/x86/boot/compressed/cmdline.c | 24 ++++++++++++++++++++++-- arch/x86/boot/compressed/misc.h | 1 + 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/arch/x86/boot/compressed/cmdline.c b/arch/x86/boot/compressed/= cmdline.c index f1add5d85da9..717f450662aa 100644 --- a/arch/x86/boot/compressed/cmdline.c +++ b/arch/x86/boot/compressed/cmdline.c @@ -12,6 +12,15 @@ static inline char rdfs8(addr_t addr) return *((char *)(fs + addr)); } #include "../cmdline.c" + +static char command_line[COMMAND_LINE_SIZE] __section(".data") =3D COMMAND= _LINE_INIT; +static bool command_line_init __section(".data"); + +/* + * This always returns runtime command line and does not account for built= -in + * command line, so this should not be used for cmdline parsing. + * Use get_cmdline() instead. + */ unsigned long get_cmd_line_ptr(void) { unsigned long cmd_line_ptr =3D boot_params->hdr.cmd_line_ptr; @@ -20,11 +29,22 @@ unsigned long get_cmd_line_ptr(void) =20 return cmd_line_ptr; } + +static inline unsigned long get_cmdline(void) +{ + if (!command_line_init) { + cmdline_prepare(command_line, (char *)get_cmd_line_ptr()); + command_line_init =3D 1; + } + + return (unsigned long)command_line; +} + int cmdline_find_option(const char *option, char *buffer, int bufsize) { - return __cmdline_find_option(get_cmd_line_ptr(), option, buffer, bufsize); + return __cmdline_find_option(get_cmdline(), option, buffer, bufsize); } int cmdline_find_option_bool(const char *option) { - return __cmdline_find_option_bool(get_cmd_line_ptr(), option); + return __cmdline_find_option_bool(get_cmdline(), option); } diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/mis= c.h index 62208ec04ca4..5bf1357c054c 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -26,6 +26,7 @@ #include #include #include +#include =20 #include "tdx.h" =20 --=20 2.37.2 From nobody Tue Apr 7 13:28:14 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E171ECAAD5 for ; Sat, 27 Aug 2022 02:08:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345346AbiH0CIb (ORCPT ); Fri, 26 Aug 2022 22:08:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345224AbiH0CIP (ORCPT ); Fri, 26 Aug 2022 22:08:15 -0400 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18104C0B5E for ; Fri, 26 Aug 2022 19:08:15 -0700 (PDT) Received: from localhost.localdomain (unknown [80.240.223.29]) by mail.ispras.ru (Postfix) with ESMTPSA id B0F004076272; Sat, 27 Aug 2022 02:08:11 +0000 (UTC) From: Evgeniy Baskov To: Borislav Petkov Cc: Evgeniy Baskov , Dave Hansen , Ingo Molnar , Thomas Gleixner , linux-kernel@vger.kernel.org, x86@kernel.org, Alexey Khoroshilov Subject: [PATCH v6 5/5] x86/boot: Remove no longer needed includes Date: Sat, 27 Aug 2022 05:08:10 +0300 Message-Id: X-Mailer: git-send-email 2.37.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" x86/incldue/asm/shared/cmdline.h header already provides COMMAND_LINE_SIZE definition. Signed-off-by: Evgeniy Baskov --- arch/x86/boot/compressed/ident_map_64.c | 4 ---- arch/x86/boot/compressed/kaslr.c | 4 ---- 2 files changed, 8 deletions(-) diff --git a/arch/x86/boot/compressed/ident_map_64.c b/arch/x86/boot/compre= ssed/ident_map_64.c index d4a314cc50d6..fdfe0e5f5cb0 100644 --- a/arch/x86/boot/compressed/ident_map_64.c +++ b/arch/x86/boot/compressed/ident_map_64.c @@ -33,10 +33,6 @@ #define __PAGE_OFFSET __PAGE_OFFSET_BASE #include "../../mm/ident_map.c" =20 -#define _SETUP -#include /* For COMMAND_LINE_SIZE */ -#undef _SETUP - extern unsigned long get_cmd_line_ptr(void); =20 /* Used by PAGE_KERN* macros: */ diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/ka= slr.c index 4a3f223973f4..39e455c105a7 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -31,10 +31,6 @@ #include #include =20 -#define _SETUP -#include /* For COMMAND_LINE_SIZE */ -#undef _SETUP - extern unsigned long get_cmd_line_ptr(void); =20 /* Simplified build-specific string for starting entropy. */ --=20 2.37.2