From nobody Mon Feb 9 01:22:19 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 52D335F845; Tue, 13 Feb 2024 15:36:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707838564; cv=none; b=jAntLlrh4mXK+LF144O6AjgIvN75y4zF5CPlq+CRSibSFlwJkX9qjI4WpBFgJKt6zvZZYLkt8bgODNRRpXmdHvCoWQVlYjXcCCJLHRkx4hG/7Zb+RjHOe8KnPcjnjsSdfN1oKE0qWxvDyWF9p/OAv5BEEENmDHNjhlX/0izYbck= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707838564; c=relaxed/simple; bh=2C083pQq7ejIrNta8rh79xzMjzLHYxYyMm1x6sR7OFM=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=Uv5RzOCw1Xtb4PXSGyggwNrSUKzOr8BWe9DefjBwh2clpdVS+oMtNKuyePpS8O4hzrUwCk7cVeBmDZ+02jJPCpGpPgnvqF3Ke0oK/U1+v7YDNHlQCSt4Pxi/88yV1UVaHSdci7HMBGFn/4mtPt9U3X5f0iUPtUKOcMX/TXFQUek= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=OblL9XPv; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=NhPku5T1; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="OblL9XPv"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="NhPku5T1" Date: Tue, 13 Feb 2024 15:35:59 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1707838560; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qAKmX6rPfaSfxw6d+7kK2rtkhTI+gZJ1FunDM/XzF6k=; b=OblL9XPv4uv5vc7YMGp0fC3axOAL58Utj2Gw99L85USrtYbDmth5eWj+0QStqa5j14YK36 8xg40QIIuwwxG9fkcPePcNgHwSOQawLEIqFh/Irj2qnlzlytuxw9HnpYRLW2IESCFm0hVX OZvktqEg2SdvqcUujVprlvGAhoviaW32ZzlhxwM6FlTWBiXDxXD+hg6LB3YParqzXNTdWV 4mfClZDsa/Raqy2Q1U+yVzstkCmEQkxR1P1uidNXVPmntHyzQONGSFJWDyS3nc5fBYJMVB 5zvbgUk7BkVeKHJYd+g6Ge/LQBiB57ABdyvNFQoEKWE3AyJZQXY7CAd/1QjcnA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1707838560; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qAKmX6rPfaSfxw6d+7kK2rtkhTI+gZJ1FunDM/XzF6k=; b=NhPku5T1HgkM7WBIW7SIMgHNiOpN7R7vpQi87xVQ91lnypVzYG6He9lMl5YzQxftfva77Q kQ4zQsRdCNd2QRDw== From: "tip-bot2 for Borislav Petkov (AMD)" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/alternatives] x86/alternatives: Get rid of __optimize_nops() Cc: "Borislav Petkov (AMD)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20240130105941.19707-3-bp@alien8.de> References: <20240130105941.19707-3-bp@alien8.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <170783855963.398.15553842134757811644.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the x86/alternatives branch of ti= p: Commit-ID: f9265b75648165f61467a7ea3c9bbae33be7ce27 Gitweb: https://git.kernel.org/tip/f9265b75648165f61467a7ea3c9bbae33= be7ce27 Author: Borislav Petkov (AMD) AuthorDate: Tue, 30 Jan 2024 11:59:39 +01:00 Committer: Borislav Petkov (AMD) CommitterDate: Tue, 13 Feb 2024 16:25:46 +01:00 x86/alternatives: Get rid of __optimize_nops() There's no need to carve out bits of the NOP optimization functionality and look at JMP opcodes - simply do one more NOPs optimization pass at the end of patching. A lot simpler code. Signed-off-by: Borislav Petkov (AMD) Link: https://lore.kernel.org/r/20240130105941.19707-3-bp@alien8.de --- arch/x86/kernel/alternative.c | 52 ++++++---------------------------- 1 file changed, 10 insertions(+), 42 deletions(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 835e343..cdbece3 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -217,46 +217,11 @@ static int skip_nops(u8 *buf, int offset, int len) } =20 /* - * Optimize a sequence of NOPs, possibly preceded by an unconditional jump - * to the end of the NOP sequence into a single NOP. - */ -static bool __init_or_module -__optimize_nops(const u8 * const instr, u8 *buf, size_t len, struct insn *= insn, int *next, int *prev, int *target) -{ - int i =3D *next - insn->length; - - switch (insn->opcode.bytes[0]) { - case JMP8_INSN_OPCODE: - case JMP32_INSN_OPCODE: - *prev =3D i; - *target =3D *next + insn->immediate.value; - return false; - } - - if (insn_is_nop(insn)) { - int nop =3D i; - - *next =3D skip_nops(buf, *next, len); - if (*target && *next =3D=3D *target) - nop =3D *prev; - - add_nop(buf + nop, *next - nop); - DUMP_BYTES(ALT, buf, len, "%px: [%d:%d) optimized NOPs: ", instr, nop, *= next); - return true; - } - - *target =3D 0; - return false; -} - -/* * "noinline" to cause control flow change and thus invalidate I$ and * cause refetch after modification. */ static void __init_or_module noinline optimize_nops(const u8 * const instr= , u8 *buf, size_t len) { - int prev, target =3D 0; - for (int next, i =3D 0; i < len; i =3D next) { struct insn insn; =20 @@ -265,7 +230,14 @@ static void __init_or_module noinline optimize_nops(co= nst u8 * const instr, u8 * =20 next =3D i + insn.length; =20 - __optimize_nops(instr, buf, len, &insn, &next, &prev, &target); + if (insn_is_nop(&insn)) { + int nop =3D i; + + next =3D skip_nops(buf, next, len); + + add_nop(buf + nop, next - nop); + DUMP_BYTES(ALT, buf, len, "%px: [%d:%d) optimized NOPs: ", instr, nop, = next); + } } } =20 @@ -342,8 +314,6 @@ bool need_reloc(unsigned long offset, u8 *src, size_t s= rc_len) static void __init_or_module noinline apply_relocation(const u8 * const instr, u8 *buf, size_t len, u8 *src, siz= e_t src_len) { - int prev, target =3D 0; - for (int next, i =3D 0; i < len; i =3D next) { struct insn insn; =20 @@ -352,9 +322,6 @@ apply_relocation(const u8 * const instr, u8 *buf, size_= t len, u8 *src, size_t sr =20 next =3D i + insn.length; =20 - if (__optimize_nops(instr, buf, len, &insn, &next, &prev, &target)) - continue; - switch (insn.opcode.bytes[0]) { case 0x0f: if (insn.opcode.bytes[1] < 0x80 || @@ -533,7 +500,8 @@ void __init_or_module noinline apply_alternatives(struc= t alt_instr *start, for (; insn_buff_sz < a->instrlen; insn_buff_sz++) insn_buff[insn_buff_sz] =3D 0x90; =20 - apply_relocation(instr, insn_buff, a->instrlen, replacement, a->replacem= entlen); + apply_relocation(instr, insn_buff, a->instrlen, replacement, insn_buff_s= z); + optimize_nops(instr, insn_buff, insn_buff_sz); =20 DUMP_BYTES(ALT, instr, a->instrlen, "%px: old_insn: ", instr); DUMP_BYTES(ALT, replacement, a->replacementlen, "%px: rpl_insn: ", rep= lacement);