From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.alien8.de (mail.alien8.de [65.109.113.108]) (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 73A472D9481 for ; Fri, 2 Jan 2026 13:00:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=65.109.113.108 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767358850; cv=none; b=p9UcIY5c34or0CcpbPFfjUxXwqTghCy5UV0Fc7YvaWC4+CAzZHgZWhOmiN+7T3J2LhpOMtn9ohR+AN2JLa2blJF+i35mbY+KyMyQPu+nJDU46FnldJ+bAu1J0IOSJWqS+YSW8nGSgYurpNOLdYdO2laCu7hLBTW0NSG53VbQIuY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767358850; c=relaxed/simple; bh=u5SgI7sBlELLLnKQYEHWX+9BacWhzCt4RqLrkWVBCsU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ntIB9oD1Gj3qYCAZEQuKxcZbj+51KHJWfBDHEs4Ekcqqht06E+PluaFm3IH65pw3iK3wM/ceLtlmKAUxXKe6WjnqsCXpOKYNtou9o6NUaiJi3bqWDGYP6UOc7qRqHDx8LCO0pyW96WnhEyHdGX71UAe4NFHU6afKNUc+Jzb9nTI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=alien8.de; spf=pass smtp.mailfrom=alien8.de; dkim=pass (4096-bit key) header.d=alien8.de header.i=@alien8.de header.b=HWwiLBtN; arc=none smtp.client-ip=65.109.113.108 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=alien8.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=alien8.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (4096-bit key) header.d=alien8.de header.i=@alien8.de header.b="HWwiLBtN" Received: from localhost (localhost.localdomain [127.0.0.1]) by mail.alien8.de (SuperMail on ZX Spectrum 128k) with ESMTP id A616A40E00DA; Fri, 2 Jan 2026 13:00:42 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at mail.alien8.de Authentication-Results: mail.alien8.de (amavisd-new); dkim=pass (4096-bit key) header.d=alien8.de Received: from mail.alien8.de ([127.0.0.1]) by localhost (mail.alien8.de [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 6JAONScJFN87; Fri, 2 Jan 2026 13:00:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=alien8; t=1767358838; bh=49cFaT7F+1HEdGhZaAX9Ff8aMnZHbrUaZb90RHSGZLU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=HWwiLBtNlP4pqP3B8BTZZxwg7Of/4XW4ngya2CkLfXYbR0VAyDluWNAfrVaV3CA5i nOn6xAYSvr3YAd6GgijNoji7VIVeuU1Exu3IrsB+TXRwfZQgw1Z2hujD71dZcb1DZJ 8fh2aOHX6oOtgL4i7IJFAvb5ZMgKD8RWxQ5WHOR5mzxopbOFiKn61U2f0jsPNfnzbn /zoscFF6VZuPAaMBgo1ssYn1ktlGggy0fOG3qAeOv235wBDei8Zheh8+37Xl0ULAYn xe46noqrtI4g5sxDIp9CB7EjjkvfBXW2EfX7hBJLO/Lx5SzEvW08OG9yRNaDuDizmb 8O9Rut5OhKlvPHZ55H3r2HgKUbisN2K5jL0nDbJSr13oGpamvqJvhX2UeyHkM1wInO M2+U6B6+EdS/jNzVkRJwa4OQp2Banc1mzkmnbHsBPEn0SThOeNSt5EzY5aMnDXf5vI hCbSI4hDqjRmfm4lltf7UQ8e5dgM9x/TkVU8qvRtxtwP/+UCHXGuByJPazls25Fn80 I/BumVuV+dBfpC4OFzvWErs3OAPHvNbLKlz5ytRGI0K+a0oFETqemm50rg743yIvTp qzYxS+hl6AtTpMNeKzqzRiQcV34tEzXmvbaqDA9lPC9FgtnI6vXLXZegA75mwVJH0z vytr3+ePkXXMqwTa3Nl9TyhU= Received: from zn.tnic (pd953023b.dip0.t-ipconnect.de [217.83.2.59]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-256) server-digest SHA256) (No client certificate requested) by mail.alien8.de (SuperMail on ZX Spectrum 128k) with UTF8SMTPSA id D9B9740E01AD; Fri, 2 Jan 2026 13:00:30 +0000 (UTC) Date: Fri, 2 Jan 2026 14:00:24 +0100 From: Borislav Petkov To: Juergen Gross Cc: linux-kernel@vger.kernel.org, x86@kernel.org, Thomas Gleixner , Ingo Molnar , Dave Hansen , "H. Peter Anvin" Subject: Re: [PATCH v4 2/3] x86/alternative: Use helper functions for patching alternatives Message-ID: <20260102130024.GDaVfBaPAnQZ9PdohC@fat_crate.local> References: <20251119160420.22160-1-jgross@suse.com> <20251119160420.22160-3-jgross@suse.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline 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 > Signed-off-by: Juergen Gross > --- > 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