From nobody Fri Dec 19 14:23:30 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E23062E5B09 for ; Sat, 6 Dec 2025 21:42:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765057328; cv=none; b=SgjHWnA+m1URnEL7Jx7m63D4on5QpwucBFhNCAV21G3DBioK+Fq8qNIWjubb/+kIPAjS9aac/oEJx/pwpz1Qwp7X6m/zUVZAJM+/gFZCah5wJyEyTU5ODXSv9fOK4ZtO1u7qfji8/wOVpdsSyJYQMhYtoP3niRMz12QIU4vgtck= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765057328; c=relaxed/simple; bh=reEyLSsptVB2gduPmHwa/pY5nyaIwkkto5v6ZjzdoAg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cUvnFJXg95zTx+Jd9l2zt4IJofvY48J29v/3h71frDh5Co6SEyIf99XA4Bc6IeQBegJPgmYekk6xwE951BZIwB14votoaHNUfZH1Oh4fIx7dewCIm9hzaei+IQpDHKq1uik7pgEBJoWC4eYvj9fgwL9Cz1LH2/E1BazOV3XmkxQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eJMX7o4b; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="eJMX7o4b" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 80A72C4CEF5; Sat, 6 Dec 2025 21:42:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1765057327; bh=reEyLSsptVB2gduPmHwa/pY5nyaIwkkto5v6ZjzdoAg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eJMX7o4b7fpxuV/LiX3XM1SasykbjwN/+Cg4QIp5lGgpGtoy72tmnNElwnM3gZBRL WEIdPmIdpkZutekMihods6h0tjTwq4ilKjqmmANcSQK3Imocemm/iny+qGBS9oLI/D c/6WCyhBR6ye7twtYTwRwo0KhcBOqNST8Zx/+sPPGbis9ZZJbHR6YvZ/at0o/ItCTT sjth5AJSy9npnVI+9FUwbLFGH7Yq1Lk8GZEk0qV0gVZxwzFDbGH5eeaJ67GqnwWjdk pENcEiANBlAAnDKnK0pArvn1e3KHJw0JtuDXbxqQtXKGENISfwGnXcoPqoPONAP9Qi BQ5sv4UISL2eg== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Linus Torvalds , Ingo Molnar , Peter Zijlstra , Borislav Petkov , Thomas Gleixner Subject: [PATCH 8/8] x86/alternative: Convert alternatives to assembler macros Date: Sat, 6 Dec 2025 13:41:15 -0800 Message-ID: <051df5c21cbf6d9ac396c22b3ab3c51d73c0def7.1765044697.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.52.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Improve code generation readability by converting the alternatives into assembler macros which are created when alternative.h is included. Before: # ./arch/x86/include/asm/smap.h:47: alternative("", "stac", X86_FEATURE_= SMAP); # 47 "./arch/x86/include/asm/smap.h" 1 # ALT: oldinstr 771: 772: # ALT: padding .skip -(((775f-774f)-(772b-771b)) > 0) * ((775f-774f)-(772b-771b)),0x90 773: .pushsection .altinstructions, "aM", @progbits, 14 .long 771b - . .long 774f - . .4byte ( 9*32+20) .byte 773b-771b .byte 775f-774f .popsection .pushsection .altinstr_replacement, "ax" ANNOTATE_DATA_SPECIAL # ALT: replacement 774: stac 775: .popsection After: # ./arch/x86/include/asm/smap.h:47: alternative("", "stac", X86_FEATURE_= SMAP); # 47 "./arch/x86/include/asm/smap.h" 1 ALTERNATIVE "", "stac", "( 9*32+20)" Signed-off-by: Josh Poimboeuf --- arch/x86/include/asm/alternative.h | 196 +++++++++++++++-------------- include/linux/annotate.h | 7 +- 2 files changed, 107 insertions(+), 96 deletions(-) diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alte= rnative.h index 03364510d5fe..89c69fe8f337 100644 --- a/arch/x86/include/asm/alternative.h +++ b/arch/x86/include/asm/alternative.h @@ -8,6 +8,90 @@ #include #include =20 +/* + * CALC_MAX: assembler-compatible max based on the idea from: + * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax + * + * The additional "-" is needed because gas uses a "true" value of -1. + */ +#define CALC_MAX(a, b) (a ^ ((a ^ b) & -(-(a < b)))) + +/* Add padding after the original instructions so the replacements can fit= . */ +#define __ALT_SKIP(orig_len, len1, len2, len3) \ + .skip -((CALC_MAX(CALC_MAX(len1, len2), len3) - orig_len) > 0) *\ + (CALC_MAX(CALC_MAX(len1, len2), len3) - orig_len), 0x90 + +#define ALT_SKIP __ALT_SKIP((772b-771b), (775f-774f), (776f-775f), (777f-7= 76f)) + +#define ALT_REPLACE(newinstr1, newinstr2, newinstr3) \ + .pushsection .altinstr_replacement, "ax"; \ + 774: newinstr1; \ + 775: newinstr2; \ + 776: newinstr3; \ + 777: \ + .popsection + +#define ALT_ENTRY(orig_begin, orig_end, new_begin, new_end, ft_flags) \ + __ANNOTATE_DATA_SPECIAL(new_begin); \ + .pushsection .altinstructions, "aM", @progbits, ALT_INSTR_SIZE; \ + .long orig_begin - .; \ + .long new_begin - .; \ + .4byte ft_flags; \ + .byte orig_end - orig_begin; \ + .byte new_end - new_begin; \ + .popsection + +/* + * These ALTERNATIVE .macros get created when this file gets included: + */ + +#define DEFINE_ALTERNATIVE \ +.macro ALTERNATIVE oldinstr, newinstr, ft_flags; \ + 771: __ASM_C(\oldinstr, \\oldinstr); \ + 772: ALT_SKIP; \ + 773: \ + ALT_REPLACE(__ASM_C(\newinstr, \\newinstr),,); \ + ALT_ENTRY(771b, 773b, 774b, 775b, \ + __ASM_C(\ft_flags, \\ft_flags)); \ +.endm + +#define DEFINE_ALTERNATIVE_2 \ +.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, \ + newinstr2, ft_flags2; \ + 771: __ASM_C(\oldinstr, \\oldinstr); \ + 772: ALT_SKIP; \ + 773: \ + ALT_REPLACE(__ASM_C(\newinstr1, \\newinstr1), \ + __ASM_C(\newinstr2, \\newinstr2),); \ + ALT_ENTRY(771b, 773b, 774b, 775b, \ + __ASM_C(\ft_flags1, \\ft_flags1)); \ + ALT_ENTRY(771b, 773b, 775b, 776b, \ + __ASM_C(\ft_flags2, \\ft_flags2)); \ +.endm + +#define DEFINE_ALTERNATIVE_3 \ +.macro ALTERNATIVE_3 oldinstr, newinstr1, ft_flags1, \ + newinstr2, ft_flags2, \ + newinstr3, ft_flags3; \ + 771: __ASM_C(\oldinstr, \\oldinstr); \ + 772: ALT_SKIP; \ + 773: \ + ALT_REPLACE(__ASM_C(\newinstr1, \\newinstr1), \ + __ASM_C(\newinstr2, \\newinstr2), \ + __ASM_C(\newinstr3, \\newinstr3)); \ + ALT_ENTRY(771b, 773b, 774b, 775b, \ + __ASM_C(\ft_flags1, \\ft_flags1)); \ + ALT_ENTRY(771b, 773b, 775b, 776b, \ + __ASM_C(\ft_flags2, \\ft_flags2)); \ + ALT_ENTRY(771b, 773b, 776b, 777b, \ + __ASM_C(\ft_flags3, \\ft_flags3)); \ +.endm + +DEFINE_MACRO(ALTERNATIVE); +DEFINE_MACRO(ALTERNATIVE_2); +DEFINE_MACRO(ALTERNATIVE_3); + + #define ALT_FLAGS_SHIFT 16 =20 #define ALT_FLAG_NOT (1 << 0) @@ -184,53 +268,30 @@ static inline int alternatives_text_reserved(void *st= art, void *end) =20 #define ALT_CALL_INSTR "call BUG_func" =20 -#define alt_slen "772b-771b" -#define alt_total_slen "773b-771b" -#define alt_rlen "775f-774f" +#define ALTERNATIVE(oldinstr, newinstr, ft_flags) \ + "ALTERNATIVE \"" oldinstr "\", " \ + "\"" newinstr "\", \"" __stringify(ft_flags) "\"\n" =20 -#define OLDINSTR(oldinstr) \ - "# ALT: oldinstr\n" \ - "771:\n\t" oldinstr "\n772:\n" \ - "# ALT: padding\n" \ - ".skip -(((" alt_rlen ")-(" alt_slen ")) > 0) * " \ - "((" alt_rlen ")-(" alt_slen ")),0x90\n" \ - "773:\n" +#define ALTERNATIVE_2(oldinstr, \ + newinstr1, ft_flags1, \ + newinstr2, ft_flags2) \ + "ALTERNATIVE_2 \"" oldinstr "\", " \ + "\"" newinstr1 "\", \"" __stringify(ft_flags1) "\", " \ + "\"" newinstr2 "\", \"" __stringify(ft_flags2) "\"\n" =20 -#define ALTINSTR_ENTRY(ft_flags) \ - ".pushsection .altinstructions, \"aM\", @progbits, " \ - __stringify(ALT_INSTR_SIZE) "\n" \ - " .long 771b - .\n" /* label */ \ - " .long 774f - .\n" /* new instruction */ \ - " .4byte " __stringify(ft_flags) "\n" /* feature + flags */ \ - " .byte " alt_total_slen "\n" /* source len */ \ - " .byte " alt_rlen "\n" /* replacement len */ \ - ".popsection\n" - -#define ALTINSTR_REPLACEMENT(newinstr) /* replacement */ \ - ".pushsection .altinstr_replacement, \"ax\"\n" \ - ANNOTATE_DATA_SPECIAL "\n" \ - "# ALT: replacement\n" \ - "774:\n\t" newinstr "\n775:\n" \ - ".popsection\n" - -/* alternative assembly primitive: */ -#define ALTERNATIVE(oldinstr, newinstr, ft_flags) \ - OLDINSTR(oldinstr) \ - ALTINSTR_ENTRY(ft_flags) \ - ALTINSTR_REPLACEMENT(newinstr) - -#define ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2= ) \ - ALTERNATIVE(ALTERNATIVE(oldinstr, newinstr1, ft_flags1), newinstr2, ft_fl= ags2) +#define ALTERNATIVE_3(oldinstr, \ + newinstr1, ft_flags1, \ + newinstr2, ft_flags2, \ + newinstr3, ft_flags3) \ + "ALTERNATIVE_3 \"" oldinstr "\", " \ + "\"" newinstr1 "\", \"" __stringify(ft_flags1) "\", " \ + "\"" newinstr2 "\", \"" __stringify(ft_flags2) "\", " \ + "\"" newinstr3 "\", \"" __stringify(ft_flags3) "\"\n" =20 /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */ #define ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no)= \ ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS, newinstr_yes, ft= _flags) =20 -#define ALTERNATIVE_3(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2= , \ - newinstr3, ft_flags3) \ - ALTERNATIVE(ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_f= lags2), \ - newinstr3, ft_flags3) - /* * Alternative instructions for different CPU types or capabilities. * @@ -332,65 +393,10 @@ void nop_func(void); .endm #endif =20 -/* - * Issue one struct alt_instr descriptor entry (need to put it into - * the section .altinstructions, see below). This entry contains - * enough information for the alternatives patching code to patch an - * instruction. See apply_alternatives(). - */ -.macro altinstr_entry orig alt ft_flags orig_len alt_len - .long \orig - . - .long \alt - . - .4byte \ft_flags - .byte \orig_len - .byte \alt_len -.endm - .macro ALT_CALL_INSTR call BUG_func .endm =20 -/* - * Define an alternative between two instructions. If @feature is - * present, early code in apply_alternatives() replaces @oldinstr with - * @newinstr. ".skip" directive takes care of proper instruction padding - * in case @newinstr is longer than @oldinstr. - */ -#define __ALTERNATIVE(oldinst, newinst, flag) \ -740: \ - oldinst ; \ -741: \ - .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;\ -742: \ - .pushsection .altinstructions, "aM", @progbits, ALT_INSTR_SIZE ;\ - altinstr_entry 740b,743f,flag,742b-740b,744f-743f ; \ - .popsection ; \ - .pushsection .altinstr_replacement,"ax" ; \ -743: \ - ANNOTATE_DATA_SPECIAL ; \ - newinst ; \ -744: \ - .popsection ; - -.macro ALTERNATIVE oldinstr, newinstr, ft_flags - __ALTERNATIVE(\oldinstr, \newinstr, \ft_flags) -.endm - -/* - * Same as ALTERNATIVE macro above but for two alternatives. If CPU - * has @feature1, it replaces @oldinstr with @newinstr1. If CPU has - * @feature2, it replaces @oldinstr with @feature2. - */ -.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2 - __ALTERNATIVE(__ALTERNATIVE(\oldinstr, \newinstr1, \ft_flags1), - \newinstr2, \ft_flags2) -.endm - -.macro ALTERNATIVE_3 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2,= newinstr3, ft_flags3 - __ALTERNATIVE(ALTERNATIVE_2(\oldinstr, \newinstr1, \ft_flags1, \newinstr2= , \ft_flags2), - \newinstr3, \ft_flags3) -.endm - /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */ #define ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no)= \ ALTERNATIVE_2 oldinstr, newinstr_no, X86_FEATURE_ALWAYS, \ diff --git a/include/linux/annotate.h b/include/linux/annotate.h index 8b28f1a81ec4..a80fef36dc92 100644 --- a/include/linux/annotate.h +++ b/include/linux/annotate.h @@ -100,8 +100,13 @@ DEFINE_MACRO(ANNOTATE_DATA(DATA_SPECIAL)); * to find and extract individual special section entries as needed. */ #define ANNOTATE_DATA_SPECIAL "ANNOTATE_DATA_SPECIAL" +#define __ANNOTATE_DATA_SPECIAL(label) __ANNOTATE .discard.annotate_data, = ANNOTYPE_DATA_SPECIAL, label =20 -#endif /* !__ASSEMBLY__ */ +#else /* __ASSEMBLY__ */ + +#define __ANNOTATE_DATA_SPECIAL(label) ANNOTATE_DATA_SPECIAL loc=3Dlabel + +#endif /* __ASSEMBLY__ */ =20 #else /* !OBJTOOL */ =20 --=20 2.52.0