From nobody Tue Dec 2 02:30:42 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 18B002D59F7 for ; Wed, 19 Nov 2025 16:04:42 +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=1763568284; cv=none; b=VbueOSqVsN3ulUH+IkHcQi/stMvl6PQn77HevPwY21tR/88qYyfKZ/nNU+d/4Cnbo4su6r61BDwOuhmAjI3IDYHFO0Psc+icqiW2P5HubTWcijh4EtJjv+iyyfq1m7HfzEnbozImyaQTLh7JvGvIcDI+i6yFYt64xKwsHWGuD1U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763568284; c=relaxed/simple; bh=VJGybevZvon4o/gmT8Q572FufRSXvmgEF4BEtrRdz18=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Wa/ZIUETurf1NZ1PloyMPUYULLt1Hv6FwQwYMhX9qMkE0mQSzESTVYoigipggKW6DZDV1LCQG5Td/uYayZji9TEVR0umqyi6nLRW8CwhfxFcyAcPlIWaSfTQ6hpZyoMis+eRoQedotwnaXsPq4TybnVq78CnCzrYeL62ZQJ/GYE= 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; 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 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 7F57520575; Wed, 19 Nov 2025 16:04:39 +0000 (UTC) Authentication-Results: smtp-out2.suse.de; none 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 3D83E3EA61; Wed, 19 Nov 2025 16:04:39 +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 Q2idDZfqHWmTUgAAD6G6ig (envelope-from ); Wed, 19 Nov 2025 16:04:39 +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 v4 3/3] x86/alternative: Patch a single alternative location only once Date: Wed, 19 Nov 2025 17:04:20 +0100 Message-ID: <20251119160420.22160-4-jgross@suse.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251119160420.22160-1-jgross@suse.com> References: <20251119160420.22160-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-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Queue-Id: 7F57520575 X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Action: no action X-Spam-Flag: NO X-Spam-Score: -4.00 X-Spam-Level: X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spamd-Result: default: False [-4.00 / 50.00]; REPLY(-4.00)[] 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). This issue has been hit when trying to do paravirt patching similar to the following: ALTERNATIVE_2(PARAVIRT_CALL, // indirect call instr, feature, // native instruction ALT_CALL_INSTR, X86_FEATURE_XENPV) // Xen function In case "feature" was true, "instr" replaced the indirect call. Under Xen PV the patching to have a direct call failed, as the original indirect call was no longer there to find the call target. - 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: - complete rework (Boris Petkov) V3: - rebase to added patch 2 --- arch/x86/kernel/alternative.c | 42 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index afcc681ff3bd..c1032840df4e 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -611,37 +611,36 @@ struct patch_site { u8 len; }; =20 -static void __init_or_module analyze_patch_site(struct patch_site *ps, - struct alt_instr *p, struct alt_instr *end) +static struct alt_instr * __init_or_module analyze_patch_site( + struct patch_site *ps, struct alt_instr *p, struct alt_instr *end) { - struct alt_instr *r; - /* * 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). + * Find the last alt_instr eligible for patching at the site. */ ps->instr =3D instr_va(p); - ps->len =3D p->instrlen; - for (r =3D p+1; r < end && instr_va(r) =3D=3D ps->instr; r++) { - ps->len =3D max(ps->len, r->instrlen); - p->instrlen =3D r->instrlen =3D ps->len; + ps->alt =3D NULL; + ps->len =3D 0; + for (; p < end && instr_va(p) =3D=3D ps->instr; p++) { + ps->len =3D max(ps->len, p->instrlen); + + BUG_ON(p->cpuid >=3D (NCAPINTS + NBUGINTS) * 32); + /* + * Patch if either: + * - feature is present + * - feature not present but ALT_FLAG_NOT is set to mean, + * patch if feature is *NOT* present. + */ + if (!boot_cpu_has(p->cpuid) !=3D !(p->flags & ALT_FLAG_NOT)) + ps->alt =3D p; } =20 BUG_ON(ps->len > sizeof(ps->buff)); - BUG_ON(p->cpuid >=3D (NCAPINTS + NBUGINTS) * 32); =20 - /* - * Patch if either: - * - feature is present - * - feature not present but ALT_FLAG_NOT is set to mean, - * patch if feature is *NOT* present. - */ - if (!boot_cpu_has(p->cpuid) =3D=3D !(p->flags & ALT_FLAG_NOT)) - ps->alt =3D NULL; - else - ps->alt =3D p; + return p; } =20 static void __init_or_module prep_patch_site(struct patch_site *ps) @@ -720,10 +719,11 @@ void __init_or_module noinline apply_alternatives(str= uct alt_instr *start, * So be careful if you want to change the scan order to any other * order. */ - for (a =3D start; a < end; a++) { + a =3D start; + while (a < end) { struct patch_site ps; =20 - analyze_patch_site(&ps, a, end); + a =3D analyze_patch_site(&ps, a, end); prep_patch_site(&ps); patch_site(&ps); } --=20 2.51.0