From nobody Wed Oct 1 22:34:49 2025 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB0772E8DF0 for ; Mon, 29 Sep 2025 11:29:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759145401; cv=none; b=GQQD51qS/giwWZpcFu04+O9j80t9YDmRd8vyNgF7GIKBv+Ak0RbZR0jclhS2gT37mmCiOBE6l+QQGaDTOyVehtQP6j+uAW7JPsMzqYRevKMoF69XW7A981J72d/VTk8fbAmgxJ1Lpm3YsSOuOwPKAUk4iBIdurJ6hFfVjfraBfM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759145401; c=relaxed/simple; bh=Q0RMUIfZ1lLbI+KfFiVh8RSH+tZlHw8ZsERa9sIO9Gg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=X8rfCEQXjmp0uxclbvYL7LLpwq7JNEL4b35L4KNA7fOs8RVy8xny0RKoXwHEatkyTKnHFgVF9JiXHmf64iNUioujhVTdCQLFamA5INQTGtYCk2cDWfDRzE01rThVwxPpwQsZoRcxL1oOYKFq0PZfSyBE1yQC557oramk9JNYHmM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=KXFFZXtr; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=KXFFZXtr; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="KXFFZXtr"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="KXFFZXtr" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 262383183F; Mon, 29 Sep 2025 11:29:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1759145396; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=E3FNZgeYfwwNy9v4xrRjT/JVmGNGOcSlyg7nziKLf/8=; b=KXFFZXtrlMUh/Lac3n8bMcX2tG2VT7BmCNCIzAKcvP215xcFu7whX/FHHbIf0cB0xmrCcF ibujnyqktUX0ReNfEpuyOhyo77e7SSaFw97tWFsQ7lj+3MNl5+g0ZC6czX8L//J0+tiL2e 3yZyPJNirL6K5VQ4EejN1ucdv1coVjA= Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1759145396; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=E3FNZgeYfwwNy9v4xrRjT/JVmGNGOcSlyg7nziKLf/8=; b=KXFFZXtrlMUh/Lac3n8bMcX2tG2VT7BmCNCIzAKcvP215xcFu7whX/FHHbIf0cB0xmrCcF ibujnyqktUX0ReNfEpuyOhyo77e7SSaFw97tWFsQ7lj+3MNl5+g0ZC6czX8L//J0+tiL2e 3yZyPJNirL6K5VQ4EejN1ucdv1coVjA= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id DA9CE13782; Mon, 29 Sep 2025 11:29:55 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id 5jrxM7Nt2mhFcAAAD6G6ig (envelope-from ); Mon, 29 Sep 2025 11:29:55 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Juergen Gross , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH 1/3] x86/alternative: Drop not needed test after call of alt_replace_call() Date: Mon, 29 Sep 2025 13:29:45 +0200 Message-ID: <20250929112947.27267-2-jgross@suse.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250929112947.27267-1-jgross@suse.com> References: <20250929112947.27267-1-jgross@suse.com> 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 X-Spam-Level: X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; TO_MATCH_ENVRCPT_ALL(0.00)[]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,suse.com:email,suse.com:mid]; FUZZY_RATELIMITED(0.00)[rspamd.com]; RCPT_COUNT_SEVEN(0.00)[8]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; TO_DN_SOME(0.00)[]; RCVD_TLS_ALL(0.00)[] X-Spam-Flag: NO X-Spam-Score: -2.80 Content-Type: text/plain; charset="utf-8" alt_replace_call() will never return a negative value, so testing the return value to be less than zero can be dropped. This makes it possible to switch the return type of alt_replace_call() and the type of insn_buff_sz to unsigned int. Signed-off-by: Juergen Gross --- V2: - new patch --- arch/x86/kernel/alternative.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 7bde68247b5f..05d3dbe7104e 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -559,7 +559,7 @@ EXPORT_SYMBOL(BUG_func); * Rewrite the "call BUG_func" replacement to point to the target of the * indirect pv_ops call "call *disp(%ip)". */ -static int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_instr *a) +static unsigned int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_= instr *a) { void *target, *bug =3D &BUG_func; s32 disp; @@ -643,7 +643,7 @@ void __init_or_module noinline apply_alternatives(struc= t alt_instr *start, * order. */ for (a =3D start; a < end; a++) { - int insn_buff_sz =3D 0; + unsigned int insn_buff_sz =3D 0; =20 /* * In case of nested ALTERNATIVE()s the outer alternative might @@ -683,11 +683,8 @@ void __init_or_module noinline apply_alternatives(stru= ct alt_instr *start, memcpy(insn_buff, replacement, a->replacementlen); insn_buff_sz =3D a->replacementlen; =20 - if (a->flags & ALT_FLAG_DIRECT_CALL) { + if (a->flags & ALT_FLAG_DIRECT_CALL) insn_buff_sz =3D alt_replace_call(instr, insn_buff, a); - if (insn_buff_sz < 0) - continue; - } =20 for (; insn_buff_sz < a->instrlen; insn_buff_sz++) insn_buff[insn_buff_sz] =3D 0x90; --=20 2.51.0 From nobody Wed Oct 1 22:34:49 2025 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B5163002A8 for ; Mon, 29 Sep 2025 11:30:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759145409; cv=none; b=kzgeHBu+N09pkB1vka0DkzJ8ufn38c64TH8cKa1QnPLiqnoLrU+Ur42IEz2ZBXJSR4P0ZRJfApigTV5yy12aSuaSkT0jG5EqkzBBwKiwpZVFDzlA7sSIq7eyF4hqnPfZ5HEvS5VxxmwEyeR6N0QpoXTkSczJM9RFlwz4uFtxoQs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759145409; c=relaxed/simple; bh=996yjX/e8fwqrXO79tWt4OSnuI0Dfs/ap51rrZd0Lmc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=o3gy6DTkka973T5or4Ar2gztvcMEwtObLXz2JpfVbcyjLzhyBjKP7cRpEtGWonIWvqI46/gSduWqgpbUDeJBmd2EumdtkWA/zJQcjGP2YWOUOPsO8iTMZBknY2rlCAZnYtxLGBBwuVawaH/zQrixykDamIWLsq6NZO9fEUvAtkI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=OaTkYi1M; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=OaTkYi1M; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="OaTkYi1M"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="OaTkYi1M" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id F410F3184A; Mon, 29 Sep 2025 11:30:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1759145402; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CV8kD4z8bPQT5dyMv9OmeX+3SSTb+6IeBhpyFSMHzsQ=; b=OaTkYi1M0rdgT8sxJ7ZmAdHsCIl0RuvNW8optTdKZ0/2AsKYu9r/+O1XeXmSqDCImgLZa7 iPWdRvG6TuDCVug27pyDzGBVmC0PI3Wq59Q5XQHYctsU1T71RBgKRgfX9f7JQe99kQvDKA Hg6fvicBpM678mCsgCWCLiXPNIRMEQI= Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=OaTkYi1M DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1759145402; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CV8kD4z8bPQT5dyMv9OmeX+3SSTb+6IeBhpyFSMHzsQ=; b=OaTkYi1M0rdgT8sxJ7ZmAdHsCIl0RuvNW8optTdKZ0/2AsKYu9r/+O1XeXmSqDCImgLZa7 iPWdRvG6TuDCVug27pyDzGBVmC0PI3Wq59Q5XQHYctsU1T71RBgKRgfX9f7JQe99kQvDKA Hg6fvicBpM678mCsgCWCLiXPNIRMEQI= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id A099C13782; Mon, 29 Sep 2025 11:30:01 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id AfaTJblt2mhxcAAAD6G6ig (envelope-from ); Mon, 29 Sep 2025 11:30:01 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Juergen Gross , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH 2/3] x86/alternative: Refactor apply_alternatives() Date: Mon, 29 Sep 2025 13:29:46 +0200 Message-ID: <20250929112947.27267-3-jgross@suse.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250929112947.27267-1-jgross@suse.com> References: <20250929112947.27267-1-jgross@suse.com> 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 X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; MIME_TRACE(0.00)[0:+]; FUZZY_RATELIMITED(0.00)[rspamd.com]; TO_DN_SOME(0.00)[]; FROM_HAS_DN(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; RCPT_COUNT_SEVEN(0.00)[8]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns,suse.com:mid,suse.com:dkim,suse.com:email]; RCVD_TLS_ALL(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; DKIM_TRACE(0.00)[suse.com:+] X-Spam-Flag: NO X-Spam-Level: X-Rspamd-Queue-Id: F410F3184A X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Action: no action X-Spam-Score: -3.01 Content-Type: text/plain; charset="utf-8" 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 --- 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; } =20 +static void __init_or_module apply_one_alternative(u8 *instr, u8 *insn_buf= f, + struct alt_instr *a) +{ + u8 *replacement =3D (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 =3D a->replacementlen; + + if (a->flags & ALT_FLAG_DIRECT_CALL) + insn_buff_sz =3D alt_replace_call(instr, insn_buff, a); + + for (; insn_buff_sz < a->instrlen; insn_buff_sz++) + insn_buff[insn_buff_sz] =3D 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: ", repl= acement); + DUMP_BYTES(ALT, insn_buff, insn_buff_sz, "%px: final_insn: ", instr); +} + /* * Replace instructions with better alternatives for this CPU type. This r= uns * before SMP is initialized to avoid SMP problems with self modifying cod= e. @@ -618,7 +646,7 @@ void __init_or_module noinline apply_alternatives(struc= t alt_instr *start, struct alt_instr *end) { u8 insn_buff[MAX_PATCH_LEN]; - u8 *instr, *replacement; + u8 *instr; struct alt_instr *a, *b; =20 DPRINTK(ALT, "alt table %px, -> %px", start, end); @@ -643,8 +671,6 @@ void __init_or_module noinline apply_alternatives(struc= t alt_instr *start, * order. */ for (a =3D start; a < end; a++) { - unsigned int insn_buff_sz =3D 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(struc= t alt_instr *start, } =20 instr =3D instr_va(a); - replacement =3D (u8 *)&a->repl_offset + a->repl_offset; BUG_ON(a->instrlen > sizeof(insn_buff)); BUG_ON(a->cpuid >=3D (NCAPINTS + NBUGINTS) * 32); =20 @@ -670,32 +695,11 @@ void __init_or_module noinline apply_alternatives(str= uct alt_instr *start, if (!boot_cpu_has(a->cpuid) =3D=3D !(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); } =20 - 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 =3D a->replacementlen; - - if (a->flags & ALT_FLAG_DIRECT_CALL) - insn_buff_sz =3D alt_replace_call(instr, insn_buff, a); - - for (; insn_buff_sz < a->instrlen; insn_buff_sz++) - insn_buff[insn_buff_sz] =3D 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: ", rep= lacement); - 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); } =20 kasan_enable_current(); --=20 2.51.0 From nobody Wed Oct 1 22:34:49 2025 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B07C43002A1 for ; Mon, 29 Sep 2025 11:30:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759145415; cv=none; b=b6zcqRKh7fFG1RHmg9CX5cFwSRsufH0yE0HOJoQ1/AQS6HJsivAK4ROijgGbFUYkaqMKPNxWAm00E2hWbqxqHCvYcvYRReY9KOyxF75vVhG91sIbFdB6Ihgj/WAE+syOYSsD7xhSsrHMbrH8L4ZqG0e20KVc5b6anYnVyJ5VWgU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759145415; c=relaxed/simple; bh=Ert/PyLXTU+1PrSLcE29tHWI73rVMIEE04cTleIo+as=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oz/KXsqThaiD/ejDD+jKWcAYcfmzn4fGXB7TH/cnzCPtn+6zAnVPrf8hnY/5mM7pWI4+zUtgHW/HAppQdPz43gK90iDwNZ1Hd2gdfu9yJfw8px/rM7Q6XAHtsHHtnZM4Ldr88jKDa0axEIaMh/uO3VCEbVXYtoPm5adj6GgZDlU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=ck1404zp; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=ck1404zp; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="ck1404zp"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="ck1404zp" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id AAF9A31863; Mon, 29 Sep 2025 11:30:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1759145407; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=H5pJdfVVyxk9Zhe1m3GkIXtRWmbwvjHnx8uLJu12sw8=; b=ck1404zpFdrHhJsfrUk+B1jM7VaEHOrw5FfgRh4yS+MruaiTgiXuY6tWqqfpE69/9E4scd aVMMyrFRfRw0hd8Eiy9tMPyEcwsc2UVBgIreH2LRu0Z4NMtZwQYEtD4xMarBAjbkKQhsnb tmVEPN4vGIgk+qiUjEl/NkNxZTF4Wok= Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1759145407; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=H5pJdfVVyxk9Zhe1m3GkIXtRWmbwvjHnx8uLJu12sw8=; b=ck1404zpFdrHhJsfrUk+B1jM7VaEHOrw5FfgRh4yS+MruaiTgiXuY6tWqqfpE69/9E4scd aVMMyrFRfRw0hd8Eiy9tMPyEcwsc2UVBgIreH2LRu0Z4NMtZwQYEtD4xMarBAjbkKQhsnb tmVEPN4vGIgk+qiUjEl/NkNxZTF4Wok= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 6928913782; Mon, 29 Sep 2025 11:30:07 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id HQg/GL9t2miDcAAAD6G6ig (envelope-from ); Mon, 29 Sep 2025 11:30:07 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Juergen Gross , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH 3/3] x86/alternative: Patch a single alternative location only once Date: Mon, 29 Sep 2025 13:29:47 +0200 Message-ID: <20250929112947.27267-4-jgross@suse.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250929112947.27267-1-jgross@suse.com> References: <20250929112947.27267-1-jgross@suse.com> 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 X-Spam-Level: X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; TO_MATCH_ENVRCPT_ALL(0.00)[]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:email,suse.com:mid,imap1.dmz-prg2.suse.org:helo]; FUZZY_RATELIMITED(0.00)[rspamd.com]; RCPT_COUNT_SEVEN(0.00)[8]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; TO_DN_SOME(0.00)[]; RCVD_TLS_ALL(0.00)[] X-Spam-Flag: NO X-Spam-Score: -2.80 Content-Type: text/plain; charset="utf-8" Instead of patching a single location potentially multiple times in case of nested ALTERNATIVE()s, do the patching only after having evaluated all alt_instr instances for that location. This has multiple advantages: - In case of replacing an indirect with a direct call using the ALT_FLAG_DIRECT_CALL flag, there is no longer the need to have that instance before any other instances at the same location (the original instruction is needed for finding the target of the direct call). - In case of nested ALTERNATIVE()s there is no intermediate replacement visible. This avoids any problems in case e.g. an interrupt is happening between the single instances and the patched location is used during handling the interrupt. Signed-off-by: Juergen Gross --- V2: - new patch --- arch/x86/kernel/alternative.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 735cc017f2d3..ccf07131cd47 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -648,6 +648,8 @@ void __init_or_module noinline apply_alternatives(struc= t alt_instr *start, u8 insn_buff[MAX_PATCH_LEN]; u8 *instr; struct alt_instr *a, *b; + unsigned int instances =3D 0; + bool patched =3D false; =20 DPRINTK(ALT, "alt table %px, -> %px", start, end); =20 @@ -677,9 +679,13 @@ void __init_or_module noinline apply_alternatives(stru= ct alt_instr *start, * padding for all alt_instr entries for this site (nested * alternatives result in consecutive entries). */ - for (b =3D a+1; b < end && instr_va(b) =3D=3D instr_va(a); b++) { - u8 len =3D max(a->instrlen, b->instrlen); - a->instrlen =3D b->instrlen =3D len; + if (!instances) { + for (b =3D a+1; b < end && instr_va(b) =3D=3D instr_va(a); b++) { + u8 len =3D max(a->instrlen, b->instrlen); + a->instrlen =3D b->instrlen =3D len; + } + instances =3D b - a; + patched =3D false; } =20 instr =3D instr_va(a); @@ -692,14 +698,19 @@ void __init_or_module noinline apply_alternatives(str= uct alt_instr *start, * - feature not present but ALT_FLAG_NOT is set to mean, * patch if feature is *NOT* present. */ - if (!boot_cpu_has(a->cpuid) =3D=3D !(a->flags & ALT_FLAG_NOT)) { - memcpy(insn_buff, instr, a->instrlen); - optimize_nops(instr, insn_buff, a->instrlen); - } else { + if (!boot_cpu_has(a->cpuid) !=3D !(a->flags & ALT_FLAG_NOT)) { apply_one_alternative(instr, insn_buff, a); + patched =3D true; } =20 - text_poke_early(instr, insn_buff, a->instrlen); + instances--; + if (!instances) { + if (!patched) { + memcpy(insn_buff, instr, a->instrlen); + optimize_nops(instr, insn_buff, a->instrlen); + } + text_poke_early(instr, insn_buff, a->instrlen); + } } =20 kasan_enable_current(); --=20 2.51.0