All of lore.kernel.org
 help / color / mirror / Atom feed
From: Borislav Petkov <bp@alien8.de>
To: Juergen Gross <jgross@suse.com>
Cc: linux-kernel@vger.kernel.org, x86@kernel.org,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	"H. Peter Anvin" <hpa@zytor.com>
Subject: Re: [PATCH v4 2/3] x86/alternative: Use helper functions for patching alternatives
Date: Fri, 2 Jan 2026 14:00:24 +0100	[thread overview]
Message-ID: <20260102130024.GDaVfBaPAnQZ9PdohC@fat_crate.local> (raw)
In-Reply-To: <20251119160420.22160-3-jgross@suse.com>

On Wed, Nov 19, 2025 at 05:04:19PM +0100, Juergen Gross wrote:
> Tidy up apply_alternatives() by moving the main patching action of a
> single alternative instance into 3 helper functions:
> 
> - analyze_patch_site() for selection whether patching should occur or
>   not and to handle nested alternatives.
> 
> - prep_patch_site() for applying any needed relocations and issuing
>   debug prints for the site.
> 
> - patch_site() doing the real patching action, including optimization
>   of any padding NOPs.
> 
> In prep_patch_site() use __apply_relocation() instead of
> text_poke_apply_relocation(), as the NOP optimization is now done
> in patch_site() for all cases.
> 
> Suggested-by: Borislav Petkov <bp@alien8.de>
> Signed-off-by: Juergen Gross <jgross@suse.com>
> ---
> V3:
> - new patch
> V4:
> - further split coding in more helpers (Borislav Petkov)
> ---
>  arch/x86/kernel/alternative.c | 140 +++++++++++++++++++++-------------
>  1 file changed, 85 insertions(+), 55 deletions(-)

Better...

Some additional changes ontop:

- put the comment over the loop where it belongs

- name variables into something more descriptive than just a single letter.

With that I think it is starting to look better/more readable.

diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index 49a9eed24fdd..66868ecdebd2 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -594,25 +594,27 @@ struct patch_site {
 };
 
 static void __init_or_module analyze_patch_site(struct patch_site *ps,
-						struct alt_instr *p, struct alt_instr *end)
+						struct alt_instr *start,
+						struct alt_instr *end)
 {
 	struct alt_instr *r;
 
+	ps->instr = instr_va(start);
+	ps->len   = start->instrlen;
+
 	/*
 	 * In case of nested ALTERNATIVE()s the outer alternative might add
 	 * more padding. To ensure consistent patching find the max padding for
 	 * all alt_instr entries for this site (nested alternatives result in
 	 * consecutive entries).
 	 */
-	ps->instr = instr_va(p);
-	ps->len = p->instrlen;
-	for (r = p+1; r < end && instr_va(r) == ps->instr; r++) {
+	for (r = start+1; r < end && instr_va(r) == ps->instr; r++) {
 		ps->len = max(ps->len, r->instrlen);
-		p->instrlen = r->instrlen = ps->len;
+		start->instrlen = r->instrlen = ps->len;
 	}
 
 	BUG_ON(ps->len > sizeof(ps->buff));
-	BUG_ON(p->cpuid >= (NCAPINTS + NBUGINTS) * 32);
+	BUG_ON(start->cpuid >= (NCAPINTS + NBUGINTS) * 32);
 
 	/*
 	 * Patch if either:
@@ -620,43 +622,43 @@ static void __init_or_module analyze_patch_site(struct patch_site *ps,
 	 * - feature not present but ALT_FLAG_NOT is set to mean,
 	 *   patch if feature is *NOT* present.
 	 */
-	if (!boot_cpu_has(p->cpuid) == !(p->flags & ALT_FLAG_NOT))
+	if (!boot_cpu_has(start->cpuid) == !(start->flags & ALT_FLAG_NOT))
 		ps->alt = NULL;
 	else
-		ps->alt = p;
+		ps->alt = start;
 }
 
 static void __init_or_module prep_patch_site(struct patch_site *ps)
 {
-	struct alt_instr *p = ps->alt;
+	struct alt_instr *alt = ps->alt;
 	u8 buff_sz;
 	u8 *repl;
 
-	if (!p) {
+	if (!alt) {
 		/* Nothing to patch, use original instruction. */
 		memcpy(ps->buff, ps->instr, ps->len);
 		return;
 	}
 
-	repl = (u8 *)&p->repl_offset + p->repl_offset;
+	repl = (u8 *)&alt->repl_offset + alt->repl_offset;
 	DPRINTK(ALT, "feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x",
-		p->cpuid >> 5, p->cpuid & 0x1f,
+		alt->cpuid >> 5, alt->cpuid & 0x1f,
 		ps->instr, ps->instr, ps->len,
-		repl, p->replacementlen, p->flags);
+		repl, alt->replacementlen, alt->flags);
 
-	memcpy(ps->buff, repl, p->replacementlen);
-	buff_sz = p->replacementlen;
+	memcpy(ps->buff, repl, alt->replacementlen);
+	buff_sz = alt->replacementlen;
 
-	if (p->flags & ALT_FLAG_DIRECT_CALL)
-		buff_sz = alt_replace_call(ps->instr, ps->buff, p);
+	if (alt->flags & ALT_FLAG_DIRECT_CALL)
+		buff_sz = alt_replace_call(ps->instr, ps->buff, alt);
 
 	for (; buff_sz < ps->len; buff_sz++)
 		ps->buff[buff_sz] = 0x90;
 
-	__apply_relocation(ps->buff, ps->instr, ps->len, repl, p->replacementlen);
+	__apply_relocation(ps->buff, ps->instr, ps->len, repl, alt->replacementlen);
 
 	DUMP_BYTES(ALT, ps->instr, ps->len, "%px:   old_insn: ", ps->instr);
-	DUMP_BYTES(ALT, repl, p->replacementlen, "%px:   rpl_insn: ", repl);
+	DUMP_BYTES(ALT, repl, alt->replacementlen, "%px:   rpl_insn: ", repl);
 	DUMP_BYTES(ALT, ps->buff, ps->len, "%px: final_insn: ", ps->instr);
 }
 

-- 
Regards/Gruss,
    Boris.

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

  reply	other threads:[~2026-01-02 13:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-19 16:04 [PATCH v4 0/3] x86/alternative: Patch a single alternative location only once Juergen Gross
2025-11-19 16:04 ` [PATCH v4 1/3] x86/alternative: Drop not needed test after call of alt_replace_call() Juergen Gross
2025-11-19 16:04 ` [PATCH v4 2/3] x86/alternative: Use helper functions for patching alternatives Juergen Gross
2026-01-02 13:00   ` Borislav Petkov [this message]
2025-11-19 16:04 ` [PATCH v4 3/3] x86/alternative: Patch a single alternative location only once Juergen Gross
2026-01-02 16:05   ` Borislav Petkov
2025-12-15  8:25 ` [PATCH v4 0/3] " Juergen Gross
2025-12-15 10:25   ` Borislav Petkov

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=20260102130024.GDaVfBaPAnQZ9PdohC@fat_crate.local \
    --to=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=jgross@suse.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.