[PATCH v2 43/62] x86/alternative: Define ELF section entry size for alternatives

Josh Poimboeuf posted 62 patches 7 months, 1 week ago
There is a newer version of this series
[PATCH v2 43/62] x86/alternative: Define ELF section entry size for alternatives
Posted by Josh Poimboeuf 7 months, 1 week ago
In preparation for the objtool klp diff subcommand, define the entry
size for the .altinstructions section in its ELF header.  This will
allow tooling to extract individual entries.

Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
 arch/x86/include/asm/alternative.h | 7 +++++--
 arch/x86/kernel/alternative.c      | 2 ++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alternative.h
index e18cdaa1573c..212761eec886 100644
--- a/arch/x86/include/asm/alternative.h
+++ b/arch/x86/include/asm/alternative.h
@@ -15,6 +15,8 @@
 #define ALT_DIRECT_CALL(feature) ((ALT_FLAG_DIRECT_CALL << ALT_FLAGS_SHIFT) | (feature))
 #define ALT_CALL_ALWAYS		ALT_DIRECT_CALL(X86_FEATURE_ALWAYS)
 
+#define ALTINSTR_SIZE		14
+
 #ifndef __ASSEMBLER__
 
 #include <linux/stddef.h>
@@ -165,7 +167,8 @@ static inline int alternatives_text_reserved(void *start, void *end)
 	"773:\n"
 
 #define ALTINSTR_ENTRY(ft_flags)					      \
-	".pushsection .altinstructions,\"a\"\n"				      \
+	".pushsection .altinstructions, \"aM\", @progbits, "		      \
+		      __stringify(ALTINSTR_SIZE) "\n"			      \
 	" .long 771b - .\n"				/* label           */ \
 	" .long 774f - .\n"				/* new instruction */ \
 	" .4byte " __stringify(ft_flags) "\n"		/* feature + flags */ \
@@ -328,7 +331,7 @@ void nop_func(void);
 741:									\
 	.skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90	;\
 742:									\
-	.pushsection .altinstructions,"a" ;				\
+	.pushsection .altinstructions, "aM", @progbits, ALTINSTR_SIZE ;	\
 	altinstr_entry 740b,743f,flag,742b-740b,744f-743f ;		\
 	.popsection ;							\
 	.pushsection .altinstr_replacement,"ax"	;			\
diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index ec220e53cb52..d6064dd87dde 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -425,6 +425,8 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
 	u8 *instr, *replacement;
 	struct alt_instr *a, *b;
 
+	BUILD_BUG_ON(ALTINSTR_SIZE != sizeof(struct alt_instr));
+
 	DPRINTK(ALT, "alt table %px, -> %px", start, end);
 
 	/*
-- 
2.49.0
Re: [PATCH v2 43/62] x86/alternative: Define ELF section entry size for alternatives
Posted by Borislav Petkov 7 months, 1 week ago
On Fri, May 09, 2025 at 01:17:07PM -0700, Josh Poimboeuf wrote:
> +#define ALTINSTR_SIZE		14

We have sizeof(struct alt_instr) to offer...

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Re: [PATCH v2 43/62] x86/alternative: Define ELF section entry size for alternatives
Posted by Josh Poimboeuf 7 months, 1 week ago
On Fri, May 09, 2025 at 11:36:35PM +0200, Borislav Petkov wrote:
> On Fri, May 09, 2025 at 01:17:07PM -0700, Josh Poimboeuf wrote:
> > +#define ALTINSTR_SIZE		14
> 
> We have sizeof(struct alt_instr) to offer...

Right, but IIRC, sizeof(struct alt_instr) isn't available at macro
expansion time so it would have to be provided as an input constraint.

That doesn't really work for the ALTERNATIVE macro, where the asm
constraints are out of our control because they're set by the caller.

-- 
Josh
Re: [PATCH v2 43/62] x86/alternative: Define ELF section entry size for alternatives
Posted by Borislav Petkov 7 months, 1 week ago
On Fri, May 09, 2025 at 02:54:00PM -0700, Josh Poimboeuf wrote:
> On Fri, May 09, 2025 at 11:36:35PM +0200, Borislav Petkov wrote:
> > On Fri, May 09, 2025 at 01:17:07PM -0700, Josh Poimboeuf wrote:
> > > +#define ALTINSTR_SIZE		14
> > 
> > We have sizeof(struct alt_instr) to offer...
> 
> Right, but IIRC, sizeof(struct alt_instr) isn't available at macro
> expansion time so it would have to be provided as an input constraint.
> 
> That doesn't really work for the ALTERNATIVE macro, where the asm
> constraints are out of our control because they're set by the caller.

Bah, that doesn't work. And you're enforcing it with BUILD_BUG_ON(). Oh well,
ignore the noise.

:-)

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette