From nobody Wed Dec 17 05:30:38 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 BFCB1263F38 for ; Tue, 8 Apr 2025 08:21:27 +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=1744100487; cv=none; b=hUYDmGXgYpCNRpvPWGLOHsUIj563CA6QnD+KHtjqtrHQU0MghamA5ikkCjIG81PnZkde5VhONsSd434lYNSMBg3a9nQ8nwKpQd8psIz/h68pobj3gz9MSYUDQ2uSFPBTJ6Nb9BOHvjtg+J/XtBmpS43lD3mjQ+x0yBTJ3efDT94= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744100487; c=relaxed/simple; bh=3nrms8bo08+6NLT3VdtL3YMYoYdpkWo2StUc6gHDXkM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=H38iu98mbCGSqvCH/+S2dM3lsSw9YY8oAg101AwPgWwdHECqeROISTAUEGdVmpLlJjcczk+OD4Wn25RdufWbGNq1w3Te+ICk2krNX7zFp52OofrsJpRz66+YUElPNb3+5vXTBvHPhZgV9i+ty1CxM5DnCHf59DeRhn44rU6pdqQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=tCHB6NR4; 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="tCHB6NR4" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0314AC4CEE9; Tue, 8 Apr 2025 08:21:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744100487; bh=3nrms8bo08+6NLT3VdtL3YMYoYdpkWo2StUc6gHDXkM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tCHB6NR45bM0ZKk9lkFfxdfhjHfcHzzJo+mP/2EyiqzNS0QJ8xE8E+q9DZvPkYvP6 6Q7qa5aYhK9xWBJyBheF1YPiemHIgjXPkWhagaCvfyFi4ITAO8TCEZVz7mOrOvy2OC I2kXAT796//CVcqQhG+HNiNOFE6QU4A6LXuyQ1dtNUkeJS18xOCw3LSXWD75FMOqB3 Vs+yORslLOvJMYb8sWGc5zjVATaXrguNDzgFnLZzsOfxbGw5kyGQTvBd11JaQYgPD8 GFRUYWx8Gea4MOEmUPK6GYsfUpBOnzZXNh9+HmPHbb1Qw9/NDy0JkeLU+NYVsdxLLF Zn6Pi819oC/tw== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Linus Torvalds , Ingo Molnar , Peter Zijlstra Subject: [PATCH RFC 1/5] objtool: Remove ANNOTATE_IGNORE_ALTERNATIVE from CLAC/STAC Date: Tue, 8 Apr 2025 01:21:14 -0700 Message-ID: X-Mailer: git-send-email 2.49.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" ANNOTATE_IGNORE_ALTERNATIVE adds additional noise to the code generated by CLAC/STAC alternatives, hurting readability for those whose read uaccess-related code generation on a regular basis. Remove the annotation specifically for the "NOP patched with CLAC/STAC" case in favor of a manual check. Leave the other uses of that annotation in place as they're less common and more difficult to detect. Suggested-by: Linus Torvalds Signed-off-by: Josh Poimboeuf --- arch/x86/include/asm/smap.h | 12 ++++++------ tools/objtool/check.c | 30 +++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/smap.h b/arch/x86/include/asm/smap.h index 55a5e656e4b9..4f84d421d1cf 100644 --- a/arch/x86/include/asm/smap.h +++ b/arch/x86/include/asm/smap.h @@ -16,23 +16,23 @@ #ifdef __ASSEMBLER__ =20 #define ASM_CLAC \ - ALTERNATIVE __stringify(ANNOTATE_IGNORE_ALTERNATIVE), "clac", X86_FEATURE= _SMAP + ALTERNATIVE "", "clac", X86_FEATURE_SMAP =20 #define ASM_STAC \ - ALTERNATIVE __stringify(ANNOTATE_IGNORE_ALTERNATIVE), "stac", X86_FEATURE= _SMAP + ALTERNATIVE "", "stac", X86_FEATURE_SMAP =20 #else /* __ASSEMBLER__ */ =20 static __always_inline void clac(void) { /* Note: a barrier is implicit in alternative() */ - alternative(ANNOTATE_IGNORE_ALTERNATIVE "", "clac", X86_FEATURE_SMAP); + alternative("", "clac", X86_FEATURE_SMAP); } =20 static __always_inline void stac(void) { /* Note: a barrier is implicit in alternative() */ - alternative(ANNOTATE_IGNORE_ALTERNATIVE "", "stac", X86_FEATURE_SMAP); + alternative("", "stac", X86_FEATURE_SMAP); } =20 static __always_inline unsigned long smap_save(void) @@ -59,9 +59,9 @@ static __always_inline void smap_restore(unsigned long fl= ags) =20 /* These macros can be used in asm() statements */ #define ASM_CLAC \ - ALTERNATIVE(ANNOTATE_IGNORE_ALTERNATIVE "", "clac", X86_FEATURE_SMAP) + ALTERNATIVE("", "clac", X86_FEATURE_SMAP) #define ASM_STAC \ - ALTERNATIVE(ANNOTATE_IGNORE_ALTERNATIVE "", "stac", X86_FEATURE_SMAP) + ALTERNATIVE("", "stac", X86_FEATURE_SMAP) =20 #define ASM_CLAC_UNSAFE \ ALTERNATIVE("", ANNOTATE_IGNORE_ALTERNATIVE "clac", X86_FEATURE_SMAP) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 69f94bc47499..b649049b6a11 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -3505,6 +3505,34 @@ static struct instruction *next_insn_to_validate(str= uct objtool_file *file, return next_insn_same_sec(file, alt_group->orig_group->last_insn); } =20 +static bool skip_alt_group(struct instruction *insn) +{ + struct instruction *alt_insn =3D insn->alts ? insn->alts->insn : NULL; + + /* ANNOTATE_IGNORE_ALTERNATIVE */ + if (insn->alt_group && insn->alt_group->ignore) + return true; + + /* + * For NOP patched with CLAC/STAC, only follow the latter to avoid + * impossible code paths combining patched CLAC with unpatched STAC + * or vice versa. + * + * ANNOTATE_IGNORE_ALTERNATIVE could have been used here, but Linus + * requested not to do that to avoid hurting .s file readability + * around CLAC/STAC alternative sites. + */ + + if (!alt_insn) + return false; + + /* Don't override ASM_{CLAC,STAC}_UNSAFE */ + if (alt_insn->alt_group && alt_insn->alt_group->ignore) + return false; + + return alt_insn->type =3D=3D INSN_CLAC || alt_insn->type =3D=3D INSN_STAC; +} + /* * Follow the branch starting at the given instruction, and recursively fo= llow * any other branches (jumps). Meanwhile, track the frame pointer state at @@ -3625,7 +3653,7 @@ static int validate_branch(struct objtool_file *file,= struct symbol *func, } } =20 - if (insn->alt_group && insn->alt_group->ignore) + if (skip_alt_group(insn)) return 0; =20 if (handle_insn_ops(insn, next_insn, &state)) --=20 2.49.0 From nobody Wed Dec 17 05:30:38 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 1D47B263F44 for ; Tue, 8 Apr 2025 08:21:27 +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=1744100488; cv=none; b=PeVfEtNZNYBs1dmgy4wyMwr26vA/Cs2G2M4XmgxUhw+fDHJFoiFkOz4IOsw9ZLHMlvYgT6KQaotDPUauHS+ykmKtIkc5P5f9CL+EgvQTHpWG1X3WBTE0VzoAsx5HdIhix1Cj1C+hKFSeTzf1XV5ly4iKZKim5CPEQK937vUsl/k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744100488; c=relaxed/simple; bh=BFxGxGhS95knSx6gCE3mMHsG8u8tZ7dQkfm8+7hymJI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=N47kxaTltBNowI1F3nrHwub2K6iOWiyCVghE2sfT5HQFnZy1YQ1ZM/6neGyWc7Xb6zV48GdSrrGY8+0i9d2tAtz9X5iwY/Jdx6S2K0Ju6E2nPqQLpitv7X8rGvdXBC1m0QwblUKdwNMLh1qt/AeDeYk9roLqf7v+g1vvwQ035PI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Mb25JYUz; 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="Mb25JYUz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5E738C4CEED; Tue, 8 Apr 2025 08:21:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744100487; bh=BFxGxGhS95knSx6gCE3mMHsG8u8tZ7dQkfm8+7hymJI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Mb25JYUzXC7p940p6v1/j8cT9iPgXtp0r39GL1FJl11uIQivXLKjjV6RmXGHZ2fNi vTXnVD7dF8/2gAXuHvIInETgGFKt+03BFdAC4Z9UVdj+nZD2m+w4nszfEilUVpDL9x d2vl4mR343+c/hR56aqRFBXJzhszcrUFiO/Oec02ZX7Q9V5kMDt5Jfrfz12hQHugjP 24lhL/9sD9q1eNyNFA5HWDshHLu+U87ig4Z2GyXugd786u4ita9/KD/oCHKYSIWA/V vnM4/EaNxzpVmlFPizrqbz/LVum00WCBKq+T0OpNreq9UENMddAqFG96BEp4hQtFYa m4lHMuU34cZ1A== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Linus Torvalds , Ingo Molnar , Peter Zijlstra Subject: [PATCH RFC 2/5] objtool, x86/hweight: Remove ANNOTATE_IGNORE_ALTERNATIVE Date: Tue, 8 Apr 2025 01:21:15 -0700 Message-ID: X-Mailer: git-send-email 2.49.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" Since objtool's inception, frame pointer warnings have been manually silenced for __arch_hweight*() to allow those functions' inline asm to avoid using ASM_CALL_CONSTRAINT. The potentially dubious reasoning for that decision over nine years ago was that since !X86_FEATURE_POPCNT is exceedingly rare, it's not worth hurting the code layout for a function call that will never happen on the vast majority of systems. However, those functions actually started using ASM_CALL_CONSTRAINT with the following commit: 194a613088a8 ("x86/hweight: Use ASM_CALL_CONSTRAINT in inline asm()") And rightfully so, as it makes the code correct. ASM_CALL_CONSTRAINT will soon have no effect for non-FP configs anyway. With ASM_CALL_CONSTRAINT in place, ANNOTATE_IGNORE_ALTERNATIVE no longer has a purpose for the hweight functions. Remove it. Signed-off-by: Josh Poimboeuf --- arch/x86/include/asm/arch_hweight.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/arch_hweight.h b/arch/x86/include/asm/arc= h_hweight.h index cbc6157f0b4b..b5982b94bdba 100644 --- a/arch/x86/include/asm/arch_hweight.h +++ b/arch/x86/include/asm/arch_hweight.h @@ -16,8 +16,7 @@ static __always_inline unsigned int __arch_hweight32(unsi= gned int w) { unsigned int res; =20 - asm_inline (ALTERNATIVE(ANNOTATE_IGNORE_ALTERNATIVE - "call __sw_hweight32", + asm_inline (ALTERNATIVE("call __sw_hweight32", "popcntl %[val], %[cnt]", X86_FEATURE_POPCNT) : [cnt] "=3D" REG_OUT (res), ASM_CALL_CONSTRAINT : [val] REG_IN (w)); @@ -46,8 +45,7 @@ static __always_inline unsigned long __arch_hweight64(__u= 64 w) { unsigned long res; =20 - asm_inline (ALTERNATIVE(ANNOTATE_IGNORE_ALTERNATIVE - "call __sw_hweight64", + asm_inline (ALTERNATIVE("call __sw_hweight64", "popcntq %[val], %[cnt]", X86_FEATURE_POPCNT) : [cnt] "=3D" REG_OUT (res), ASM_CALL_CONSTRAINT : [val] REG_IN (w)); --=20 2.49.0 From nobody Wed Dec 17 05:30:38 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 42726263F49 for ; Tue, 8 Apr 2025 08:21:28 +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=1744100488; cv=none; b=pjA7GBybTwiuW4aphgXZZgfojVpp61R9sLhupQgDPm7SzD/AL9lua3gB7/7xV4UnOZaco9QfF38mG9b7+erfDKV60JyFLTvNdCDoNFYUQ79gXJtO9xcHS6ikEwSP+0wzyA6SnsDPvwxl1IPIBkOK4qwk+EhOcn9n3P9BaRvc3l8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744100488; c=relaxed/simple; bh=k6bnnSwlEHgFA+riTMEYB1mSt8DFIyiepASBiJ7AxcM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YcLBQSakMoc2R/575qq2K1JEu6317gllWlwzS2Tch/s3u1Mbadc0IDkTDiVV0pr6TmJwut/c6dudAvxfdMrY+2c8UiCaTjvdCkIzylLEthTSzxCQcseIaTc3JeilJP51GeNmhQDp8eG/J2hGyU5w56IwjkotvaycPzN8Y6aE8J8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QjILnKe9; 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="QjILnKe9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B801DC4CEEA; Tue, 8 Apr 2025 08:21:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744100488; bh=k6bnnSwlEHgFA+riTMEYB1mSt8DFIyiepASBiJ7AxcM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QjILnKe9EtX4PjZMFZ71m3m72cHJcRAUn8jsGof/0oGnjjEGczY8KcsjulTpNBFNy H4YYdNuYYj35gmgF5ISnUCVSEsJfPUWw7CeYsUWxqZ/HexjNJC517SXh7ip/w0QMBk XMli3oO824wplaKuZne9woPRqQgakDGrerD+dsWyIYuVMa1NqaaYI1NRdhL8DS6YKR RJEThCViB4tfyT73hclew+akX1aJOO6ByGuyLjOVZ83Tp/EtS/+hUeh/afCbvrjWGM mqXwh8AA1LFxzq0EWgS7hW5nE2AsccXYT+qPsXEATFBV9uulr47Bp2Ymo90kdS76dJ ENSJAXEExtr4w== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Linus Torvalds , Ingo Molnar , Peter Zijlstra Subject: [PATCH RFC 3/5] noinstr: Use asm_inline() in instrumentation_{begin,end}() Date: Tue, 8 Apr 2025 01:21:16 -0700 Message-ID: <7cb41fe7e87a003fc925164d5cc18efd8e95fcc0.1744098446.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.49.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" Use asm_inline() to prevent the compiler from making poor inlining decisions based on the length of the objtool annotations. For a defconfig kernel built with GCC 14.2.1, bloat-o-meter reports a 0.18% text size increase: add/remove: 88/433 grow/shrink: 967/487 up/down: 87579/-52630 (34949) Total: Before=3D19448407, After=3D19483356, chg +0.18% Presumably the text growth is due to increased inlining. A net total of 345 functions were removed. Signed-off-by: Josh Poimboeuf --- include/linux/instrumentation.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/linux/instrumentation.h b/include/linux/instrumentatio= n.h index bf675a8aef8a..b1007407d272 100644 --- a/include/linux/instrumentation.h +++ b/include/linux/instrumentation.h @@ -9,7 +9,7 @@ =20 /* Begin/end of an instrumentation safe region */ #define __instrumentation_begin(c) ({ \ - asm volatile(__stringify(c) ": nop\n\t" \ + asm_inline volatile(__stringify(c) ": nop\n\t" \ ANNOTATE_INSTR_BEGIN(__ASM_BREF(c)) \ : : "i" (c)); \ }) @@ -47,7 +47,7 @@ * part of the condition block and does not escape. */ #define __instrumentation_end(c) ({ \ - asm volatile(__stringify(c) ": nop\n\t" \ + asm_inline volatile(__stringify(c) ": nop\n\t" \ ANNOTATE_INSTR_END(__ASM_BREF(c)) \ : : "i" (c)); \ }) --=20 2.49.0 From nobody Wed Dec 17 05:30:38 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 EEDE926463C for ; Tue, 8 Apr 2025 08:21:28 +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=1744100489; cv=none; b=JdSOiXveqEX/or1kfvPeXlAv9PoH1fZL7x9QC+vivlgEBHUd+uFxCHYyklyH6j3HUiHns8igyu34sU7UYRqhIzmCZvjJYcKlU35A7seMvS7GSRpoX5kjK4Elu85eu4EJXbXJF9TITSX/bA3DqpFQ9cbcOnSTQq0YdEjkNeXa70g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744100489; c=relaxed/simple; bh=DNXe2cltquMRMBaAJp3jzbgLWlgty9WIo+JVZVQJGRw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KKC8WFy5JRlljfbITSWjnhYIHLTb4xsH5ZiIkMEcIYjZMCut1jMRUPPBoxyyArezYX92vivyWb6MZou8r/oMsSzorFgb4yHcHq6qaVGhd0poUFPTyTIGuCZtLO4BcuCEWSU0IcwW5yYXz8upal5TOX+kVpA81GbqU1icjxcs+vs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GL8Itij/; 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="GL8Itij/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1F9D8C4CEE9; Tue, 8 Apr 2025 08:21:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744100488; bh=DNXe2cltquMRMBaAJp3jzbgLWlgty9WIo+JVZVQJGRw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GL8Itij/tMPdirFnEzoi2iZQFWDbWE7hFWFboPu5By7aM7rYb6Uj4uhAPJ/F1zW6i f8fBPuNpvaPlg94Yq1516mJMbjcP002LVRU+CPevhx4KVclswWTFMXvLF9NR0KOqFg pYuiiB/A+qIQfRxzgD1kdskgTgHSU4ainhZ4sgtwnOrkERaAu/wjxOTgxcCAHDhVGP aZf8oDNYWDNfpzusHDaIsKUskBGTAw+6dNrsmHYwBc0GGgUPdYoJUjt54F1ruPhKlX BacHRKJgSJZBZmBtVDOM/HHf42ifzvCPaaw4eBPOYMIvGdfwkUY8m1JTpNGKgL7cjk YKaEG94Hwj4FA== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Linus Torvalds , Ingo Molnar , Peter Zijlstra Subject: [PATCH RFC 4/5] x86/alternative: Improve code generation readability Date: Tue, 8 Apr 2025 01:21:17 -0700 Message-ID: <35ae5c17e8cc88506b5ae8f3b06d9491f7ef319c.1744098446.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.49.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 the readability and compactness of alternatives code. --------------------- ALTERNATIVE() before: --------------------- # ALT: oldinstr 771: rep movsb 772: # ALT: padding .skip -(((775f-774f)-(772b-771b)) > 0) * ((775f-774f)-(772b-771b)),0x90 773: .pushsection .altinstructions,"a" .long 771b - . .long 774f - . .4byte (((1 << 0) << 16) | ((18*32+ 4))) .byte 773b-771b .byte 775f-774f .popsection .pushsection .altinstr_replacement, "ax" # ALT: replacement 774: call rep_movs_alternative 775: .popsection -------------------- ALTERNATIVE() after: -------------------- # 771: rep movsb 772: .skip -(((775f-774f)-(772b-771b)) > 0) * ((775f-774f)-(772b-771b)),0x= 90 773: # ALT ENTRY: .pushsection .altinstructions,"a"; .long 771b - .; .long 774f - .; .4by= te (((1 << 0) << 16) | ((18*32+ 4))); .byte 773b-771b; .byte 775f-774f; .= popsection # ALT REPLACEMENT: .pushsection .altinstr_replacement, "ax" 774: call rep_movs_alternative 775: .popsection # ----------------------- ALTERNATIVE_2() before: ----------------------- # ALT: oldinstr 771: # ALT: oldinstr 771: jmp 6f 772: # ALT: padding .skip -(((775f-774f)-(772b-771b)) > 0) * ((775f-774f)-(772b-771b)),0x90 773: .pushsection .altinstructions,"a" .long 771b - . .long 774f - . .4byte ( 3*32+21) .byte 773b-771b .byte 775f-774f .popsection .pushsection .altinstr_replacement, "ax" # ALT: replacement 774: jmp .L4 # 775: .popsection 772: # ALT: padding .skip -(((775f-774f)-(772b-771b)) > 0) * ((775f-774f)-(772b-771b)),0x90 773: .pushsection .altinstructions,"a" .long 771b - . .long 774f - . .4byte 297 # .byte 773b-771b .byte 775f-774f .popsection .pushsection .altinstr_replacement, "ax" # ALT: replacement 774: 775: .popsection ---------------------- ALTERNATIVE_2() after: ---------------------- # 771: 771: jmp 6f 772: .skip -(((775f-774f)-(772b-771b)) > 0) * ((775f-774f)-(772b-771b)),0x= 90 773: # ALT ENTRY: .pushsection .altinstructions,"a"; .long 771b - .; .long 774f - .; .4by= te ( 3*32+21); .byte 773b-771b; .byte 775f-774f; .popsection # ALT REPLACEMENT: .pushsection .altinstr_replacement, "ax" 774: jmp .L4 # 775: .popsection 772: .skip -(((775f-774f)-(772b-771b)) > 0) * ((775f-774f)-(772b-771b)),0x= 90 773: # ALT ENTRY: .pushsection .altinstructions,"a"; .long 771b - .; .long 774f - .; .4by= te 297; .byte 773b-771b; .byte 775f-774f; .popsection # # ALT REPLACEMENT: .pushsection .altinstr_replacement, "ax" 774: 775: .popsection # Signed-off-by: Josh Poimboeuf --- arch/x86/include/asm/alternative.h | 88 +++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 25 deletions(-) diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alte= rnative.h index 4a37a8bd87fd..6472d53625dc 100644 --- a/arch/x86/include/asm/alternative.h +++ b/arch/x86/include/asm/alternative.h @@ -151,46 +151,84 @@ static inline int alternatives_text_reserved(void *st= art, void *end) #define alt_rlen "775f-774f" =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" \ + "771:" oldinstr "\n" \ + "772:\t.skip -(((" alt_rlen ")-(" alt_slen ")) > 0) * " \ + "((" alt_rlen ")-(" alt_slen ")),0x90\n" \ "773:\n" =20 -#define ALTINSTR_ENTRY(ft_flags) \ - ".pushsection .altinstructions,\"a\"\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 */ \ +#define ALTINSTR_ENTRY(ft_flags) \ + "# ALT ENTRY:\n" \ + ".pushsection .altinstructions,\"a\"; " \ + " .long 771b - .; " /* label */ \ + " .long 774f - .; " /* new instruction */ \ + " .4byte " __stringify(ft_flags) "; " /* feature + flags */ \ + " .byte " alt_total_slen "; " /* source len */ \ + " .byte " alt_rlen "; " /* replacement len */ \ ".popsection\n" =20 -#define ALTINSTR_REPLACEMENT(newinstr) /* replacement */ \ +#define ALTINSTR_REPLACEMENT(newinstr) \ + "# ALT REPLACEMENT:\n" \ ".pushsection .altinstr_replacement, \"ax\"\n" \ - "# ALT: replacement\n" \ - "774:\n\t" newinstr "\n775:\n" \ - ".popsection\n" + "774:\t" newinstr "\n" \ + "775:\n" \ + ".popsection" =20 -/* alternative assembly primitive: */ -#define ALTERNATIVE(oldinstr, newinstr, ft_flags) \ + +#define __ALTERNATIVE(oldinstr, newinstr, ft_flags) \ OLDINSTR(oldinstr) \ ALTINSTR_ENTRY(ft_flags) \ ALTINSTR_REPLACEMENT(newinstr) =20 -#define ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2= ) \ - ALTERNATIVE(ALTERNATIVE(oldinstr, newinstr1, ft_flags1), newinstr2, ft_fl= ags2) +#define __ALTERNATIVE_2(oldinstr, \ + newinstr1, ft_flags1, \ + newinstr2, ft_flags2) \ + __ALTERNATIVE("\n" \ + __ALTERNATIVE(oldinstr, \ + newinstr1, ft_flags1), \ + newinstr2, ft_flags2) + +#define __ALTERNATIVE_3(oldinstr, \ + newinstr1, ft_flags1, \ + newinstr2, ft_flags2, \ + newinstr3, ft_flags3) \ + __ALTERNATIVE("\n" \ + __ALTERNATIVE_2(oldinstr, \ + newinstr1, ft_flags1, \ + newinstr2, ft_flags2), \ + newinstr3, ft_flags3) + + +#define ALTERNATIVE(oldinstr, newinstr, ft_flags) \ + "\n# \n" \ + __ALTERNATIVE("\t" oldinstr, newinstr, ft_flags) \ + "\n# \n" + +#define ALTERNATIVE_2(oldinstr, \ + newinstr1, ft_flags1, \ + newinstr2, ft_flags2) \ + "\n# \n" \ + __ALTERNATIVE_2("\t" \ + oldinstr, \ + newinstr1, ft_flags1, \ + newinstr2, ft_flags2) \ + "\n# \n" + +#define ALTERNATIVE_3(oldinstr, \ + newinstr1, ft_flags1, \ + newinstr2, ft_flags2, \ + newinstr3, ft_flags3) \ + "\n# \n" \ + __ALTERNATIVE_3("\t" \ + oldinstr, \ + newinstr1, ft_flags1, \ + newinstr2, ft_flags2, \ + newinstr3, ft_flags3) \ + "\n# \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. * --=20 2.49.0 From nobody Wed Dec 17 05:30:38 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 D1724264633 for ; Tue, 8 Apr 2025 08:21:28 +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=1744100488; cv=none; b=t6Wb6Fw3z0LhLMo0+wc2zt+6s+Jg8oJAaPl1EtSRBVLrkVcGIDc3oiqVW0oTxudbn9MgM7GTVyzIWEf+JxYWX7dVs2q1wcROI09etpLjH4ymulHYsdEp3gVjl3u2K0W7pYN4Ymk4egOgCrl2xiUy7BaKs0p6YztHoEhxkAPqpiE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744100488; c=relaxed/simple; bh=XpWuCEwoPXT8o8UPr6REZoN2em1Xvh02RO7hc3Rm1Xw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZST+JK1cdulUyBUHuOjY6dnwC0+q2eN3pBjH+E4t3aoZ2LXRA49Xfe/m6OEQn2p6eSRZkai5FLM76vVSfrFww5bQOO0v3bbPWiKn7ZA0Rf7FPCQUG+n0/Hqk4mXjhkN3Ri3JicL9rv8Nvhy73T88SqhO9U0GbO2xrHzyXNO/xLg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NSPo58Rc; 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="NSPo58Rc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7C962C4CEE5; Tue, 8 Apr 2025 08:21:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744100488; bh=XpWuCEwoPXT8o8UPr6REZoN2em1Xvh02RO7hc3Rm1Xw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NSPo58RcJQz3zoH2B3KAHc+x/g9CKL9pf12o+mZyVmLgW/Jn7Dr4mgfE6Rlo3mGDO X0k7GgwL18iM+a4dYW7S/WaRBI+8k0mqlo978YP7PCWzWjiIrw+72kYwh+axCeY0Mz ile+Zq9RV4+blooZLQyzrrIxPiuEr8tXP+Xv1FriwA9fLNNFkwEbBgwIrbBLoj4F/T 5FueZTrn9S70Xqes/nxbkjauARhYWIde8yQAalSBs5GxDiNIXyBmOMTz/rlyrSmJP7 kfcTYLu+IaL8MdTRKPsc+JNG1Nez5OR8WzC28YC0VNBTBbH8xfo+dL68UeiV+Livau iAcOM9M6UQJqQ== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Linus Torvalds , Ingo Molnar , Peter Zijlstra Subject: [PATCH RFC 5/5] objtool: Improve code generation readability Date: Tue, 8 Apr 2025 01:21:18 -0700 Message-ID: X-Mailer: git-send-email 2.49.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 the readability and compactness of the objtool annotations. This makes it easier to see them and differentiate from other code. Before: ------- 911: .pushsection .discard.annotate_insn,"M",@progbits,8 .long 911b - . .long 1 .popsection After: ------ # 911: .pushsection .discard.annotate_insn,"M",@progbits,8; .long 911b - .; = .long 1; .popsection # Signed-off-by: Josh Poimboeuf --- include/linux/objtool.h | 97 ++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 50 deletions(-) diff --git a/include/linux/objtool.h b/include/linux/objtool.h index 366ad004d794..66549603147e 100644 --- a/include/linux/objtool.h +++ b/include/linux/objtool.h @@ -8,19 +8,31 @@ =20 #include =20 +#define __UNWIND_HINT(label, type, sp_reg, sp_offset, signal) \ + .pushsection .discard.unwind_hints; \ + /* struct unwind_hint */ \ + .long label - .; \ + .short sp_offset; \ + .byte sp_reg; \ + .byte type; \ + .byte signal; \ + .balign 4; \ + .popsection + +#define __ASM_ANNOTATE(label, type) \ + .pushsection .discard.annotate_insn,"M",@progbits,8; \ + .long label - .; \ + .long type; \ + .popsection + #ifndef __ASSEMBLY__ =20 -#define UNWIND_HINT(type, sp_reg, sp_offset, signal) \ - "987: \n\t" \ - ".pushsection .discard.unwind_hints\n\t" \ - /* struct unwind_hint */ \ - ".long 987b - .\n\t" \ - ".short " __stringify(sp_offset) "\n\t" \ - ".byte " __stringify(sp_reg) "\n\t" \ - ".byte " __stringify(type) "\n\t" \ - ".byte " __stringify(signal) "\n\t" \ - ".balign 4 \n\t" \ - ".popsection\n\t" +#define UNWIND_HINT(type, sp_reg, sp_offset, signal) \ + "\n# \n" \ + "987: " \ + __stringify(__UNWIND_HINT(987b, type, sp_reg, \ + sp_offset, signal)) \ + "\n# \n\t" =20 /* * This macro marks the given function's stack frame as "non-standard", wh= ich @@ -45,23 +57,18 @@ #define STACK_FRAME_NON_STANDARD_FP(func) #endif =20 -#define ASM_REACHABLE \ - "998:\n\t" \ - ".pushsection .discard.reachable\n\t" \ - ".long 998b\n\t" \ - ".popsection\n\t" - #define __ASM_BREF(label) label ## b =20 -#define __ASM_ANNOTATE(label, type) \ - ".pushsection .discard.annotate_insn,\"M\",@progbits,8\n\t" \ - ".long " __stringify(label) " - .\n\t" \ - ".long " __stringify(type) "\n\t" \ - ".popsection\n\t" - #define ASM_ANNOTATE(type) \ - "911:\n\t" \ - __ASM_ANNOTATE(911b, type) + "\n# \n" \ + "911:\t" \ + __stringify(__ASM_ANNOTATE(911b, __PASTE(ANNOTYPE_, type))) \ + "\n# \n\t" + +#define ASM_ANNOTATE_LABEL(label, type) \ + "\n# BEGIN ANNOTATE_" __stringify(type) "\n" \ + __stringify(__ASM_ANNOTATE(label, __PASTE(ANNOTYPE_, type))) \ + "\n#