From: Juergen Gross <jgross@suse.com>
To: linux-kernel@vger.kernel.org, x86@kernel.org
Cc: Juergen Gross <jgross@suse.com>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
"H. Peter Anvin" <hpa@zytor.com>
Subject: [PATCH 2/3] x86/alternative: Refactor apply_alternatives()
Date: Mon, 29 Sep 2025 13:29:46 +0200 [thread overview]
Message-ID: <20250929112947.27267-3-jgross@suse.com> (raw)
In-Reply-To: <20250929112947.27267-1-jgross@suse.com>
Refactor apply_alternatives() by splitting out patching of a single
alt_instr instance into a sub-function.
Keep the final text_poke_early() call in apply_alternatives() in
order to prepare merging multiple alternative patching instances of
the same location.
Signed-off-by: Juergen Gross <jgross@suse.com>
---
V2:
- new patch
---
arch/x86/kernel/alternative.c | 60 +++++++++++++++++++----------------
1 file changed, 32 insertions(+), 28 deletions(-)
diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index 05d3dbe7104e..735cc017f2d3 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -604,6 +604,34 @@ static inline u8 * instr_va(struct alt_instr *i)
return (u8 *)&i->instr_offset + i->instr_offset;
}
+static void __init_or_module apply_one_alternative(u8 *instr, u8 *insn_buff,
+ struct alt_instr *a)
+{
+ u8 *replacement = (u8 *)&a->repl_offset + a->repl_offset;
+ unsigned int insn_buff_sz;
+
+ DPRINTK(ALT, "feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x",
+ a->cpuid >> 5,
+ a->cpuid & 0x1f,
+ instr, instr, a->instrlen,
+ replacement, a->replacementlen, a->flags);
+
+ memcpy(insn_buff, replacement, a->replacementlen);
+ insn_buff_sz = a->replacementlen;
+
+ if (a->flags & ALT_FLAG_DIRECT_CALL)
+ insn_buff_sz = alt_replace_call(instr, insn_buff, a);
+
+ for (; insn_buff_sz < a->instrlen; insn_buff_sz++)
+ insn_buff[insn_buff_sz] = 0x90;
+
+ text_poke_apply_relocation(insn_buff, instr, a->instrlen, replacement, a->replacementlen);
+
+ DUMP_BYTES(ALT, instr, a->instrlen, "%px: old_insn: ", instr);
+ DUMP_BYTES(ALT, replacement, a->replacementlen, "%px: rpl_insn: ", replacement);
+ DUMP_BYTES(ALT, insn_buff, insn_buff_sz, "%px: final_insn: ", instr);
+}
+
/*
* Replace instructions with better alternatives for this CPU type. This runs
* before SMP is initialized to avoid SMP problems with self modifying code.
@@ -618,7 +646,7 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
struct alt_instr *end)
{
u8 insn_buff[MAX_PATCH_LEN];
- u8 *instr, *replacement;
+ u8 *instr;
struct alt_instr *a, *b;
DPRINTK(ALT, "alt table %px, -> %px", start, end);
@@ -643,8 +671,6 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
* order.
*/
for (a = start; a < end; a++) {
- unsigned int insn_buff_sz = 0;
-
/*
* In case of nested ALTERNATIVE()s the outer alternative might
* add more padding. To ensure consistent patching find the max
@@ -657,7 +683,6 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
}
instr = instr_va(a);
- replacement = (u8 *)&a->repl_offset + a->repl_offset;
BUG_ON(a->instrlen > sizeof(insn_buff));
BUG_ON(a->cpuid >= (NCAPINTS + NBUGINTS) * 32);
@@ -670,32 +695,11 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) {
memcpy(insn_buff, instr, a->instrlen);
optimize_nops(instr, insn_buff, a->instrlen);
- text_poke_early(instr, insn_buff, a->instrlen);
- continue;
+ } else {
+ apply_one_alternative(instr, insn_buff, a);
}
- DPRINTK(ALT, "feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x",
- a->cpuid >> 5,
- a->cpuid & 0x1f,
- instr, instr, a->instrlen,
- replacement, a->replacementlen, a->flags);
-
- memcpy(insn_buff, replacement, a->replacementlen);
- insn_buff_sz = a->replacementlen;
-
- if (a->flags & ALT_FLAG_DIRECT_CALL)
- insn_buff_sz = alt_replace_call(instr, insn_buff, a);
-
- for (; insn_buff_sz < a->instrlen; insn_buff_sz++)
- insn_buff[insn_buff_sz] = 0x90;
-
- text_poke_apply_relocation(insn_buff, instr, a->instrlen, replacement, a->replacementlen);
-
- DUMP_BYTES(ALT, instr, a->instrlen, "%px: old_insn: ", instr);
- DUMP_BYTES(ALT, replacement, a->replacementlen, "%px: rpl_insn: ", replacement);
- DUMP_BYTES(ALT, insn_buff, insn_buff_sz, "%px: final_insn: ", instr);
-
- text_poke_early(instr, insn_buff, insn_buff_sz);
+ text_poke_early(instr, insn_buff, a->instrlen);
}
kasan_enable_current();
--
2.51.0
next prev parent reply other threads:[~2025-09-29 11:30 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-29 11:29 [PATCH 0/3] x86/alternative: Make alternative patching more robust Juergen Gross
2025-09-29 11:29 ` [PATCH 1/3] x86/alternative: Drop not needed test after call of alt_replace_call() Juergen Gross
2025-10-14 8:42 ` [tip: x86/core] " tip-bot2 for Juergen Gross
2025-09-29 11:29 ` Juergen Gross [this message]
2025-10-14 8:42 ` [tip: x86/core] x86/alternative: Refactor apply_alternatives() tip-bot2 for Juergen Gross
2025-09-29 11:29 ` [PATCH 3/3] x86/alternative: Patch a single alternative location only once Juergen Gross
2025-09-30 7:26 ` Peter Zijlstra
2025-09-30 7:33 ` Jürgen Groß
2025-09-30 7:39 ` Peter Zijlstra
2025-09-30 8:04 ` Jürgen Groß
2025-10-14 8:42 ` [tip: x86/core] " tip-bot2 for Juergen Gross
2025-10-14 12:59 ` Borislav Petkov
2025-10-14 13:25 ` Borislav Petkov
2025-10-14 14:13 ` Jürgen Groß
2025-10-14 14:17 ` Borislav Petkov
2025-10-14 14:18 ` Peter Zijlstra
2025-10-14 14:26 ` Juergen Gross
2025-10-14 14:29 ` Borislav Petkov
2025-10-14 14:08 ` Jürgen Groß
2025-10-14 14:12 ` Borislav Petkov
2025-10-14 14:22 ` Jürgen Groß
2025-10-14 14:26 ` Borislav Petkov
2025-10-14 14:31 ` Jürgen Groß
2025-10-14 14:39 ` Borislav Petkov
2025-10-14 14:46 ` Jürgen Groß
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250929112947.27267-3-jgross@suse.com \
--to=jgross@suse.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®