From patchwork Tue Jan 30 10:59:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 194028 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1150825dyb; Tue, 30 Jan 2024 03:34:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IHdSGK4dN9lB/48iItEhSncEJhuX+9P/aZ+lE1eyGSPzsvYvLSB7fb360Kd+KEll+r3HH0b X-Received: by 2002:a05:6a00:1d11:b0:6df:8001:501b with SMTP id a17-20020a056a001d1100b006df8001501bmr592153pfx.9.1706614462558; Tue, 30 Jan 2024 03:34:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706614462; cv=pass; d=google.com; s=arc-20160816; b=fZr/3ZLZHsQSHKJZPi3aP7z8pKXpSNubkg7aGqcmX4WtBefdlDwMQcvBhfy5grSzoP ik1ECCCwsFRoNmnnmEdKDg5risouy0R22nWGppIXUim0MfUy/nAmqFSncXdUwSnTN3Cm 6BJBwThNpwrRxm0mUWxb0OjssgsotU5FMzj/t+vPP+hZPkRXjGHeDvlmx0R9jWRov7f8 1LYG3jkQPIG1sCYRfuC5PlPyLNkyxbx9OcT4Klu+mopiln+dE7TNuOAdgKuf6QkQIAh7 vuJK21VNTE9vVSdoAtD3zTBlsYiTQiZ74SlIv8N/svvkQ8/oZvwzjZObY6PGBy/g6P1q QmPg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=BPX4JmmO3y9RwuO8QemagwxsLvxsxSv1Savb0IHZ3b8=; fh=gL8iKWYz+5Gj006D9LUvWYS+TWLN0CkoSVsKmJTYNn0=; b=SL8cFsLXG9orBPB6FvgYQk95gvJi6QyT77J3nwxN4Wu3oiui+UC0ScS2I8H78OcYyB mSp7UiieRUChoo3WJ7F5Wqc4+mSPR/3F6FJvfy1PUeY2WKh7Qm34ct0y1glWXVYa7LaI JSxiJ83Xc+yBuAsDS+ETNjTH3LawsLL9JiDCiIzzeuaxbqjsa1gZoinV5T84eIWyJXTw rco3BuRRxe7YFztOfDhlkriyvYgUzVvcAqbt2cFBtD+t35uffaYv50pclSbOcfktPYvq 5YDGTNq+1iCvXcAxwrZ2T7XL/TdwSXf+LwLQ6qKdT/rL53mcNBHMX42hWAX3bCPleasE q2lg== ARC-Authentication-Results: i=2; mx.google.com; dkim=neutral (body hash did not verify) header.i=@alien8.de header.s=alien8 header.b=ksOA9VxR; arc=pass (i=1 spf=pass spfdomain=alien8.de dmarc=pass fromdomain=alien8.de); spf=pass (google.com: domain of linux-kernel+bounces-44444-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44444-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alien8.de Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id f13-20020a056a001acd00b006ddc21cb473si7388277pfv.379.2024.01.30.03.34.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 03:34:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-44444-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@alien8.de header.s=alien8 header.b=ksOA9VxR; arc=pass (i=1 spf=pass spfdomain=alien8.de dmarc=pass fromdomain=alien8.de); spf=pass (google.com: domain of linux-kernel+bounces-44444-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44444-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alien8.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 7A4D5B24B73 for ; Tue, 30 Jan 2024 11:01:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F0032679FE; Tue, 30 Jan 2024 11:00:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (4096-bit key) header.d=alien8.de header.i=@alien8.de header.b="ksOA9VxR" 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 CAC4A6773D for ; Tue, 30 Jan 2024 11:00:05 +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=1706612407; cv=none; b=MQX9ha0fQpfqvpCwaNnCUEHGTVkbsGv6RaG00aE3bRZRyapEa12PQ1PYF9QtJ3IJTqSC06FdtpsyQvz548SKZRJGH80jHFwXa+74yAhQoFAobrZ3MSQSp91yMWytcwLrxEipEx1JrW5vAoieMwdGeqWJ0DVP85l9WhpkScSxoZk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706612407; c=relaxed/simple; bh=VyFkxB0j+nOgvuEaixpEautusuHm3UDgi6it7/uVzp0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BUbk67UdOpkpZvBT8J8tnjBmYVTM358fglgnnLZ+j2gsVYInmullfxHz95PhlIncIrhyRe+8hLtRkAYh400WwNbT2k59f3IjUSlKUiCttrMBMuwSKF4CNbhRLW+LXYn5jue3xUe5oOszuJg856yZMygGF5Ze88cFQBioAgyIDe0= 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=fail (4096-bit key) header.d=alien8.de header.i=@alien8.de header.b=ksOA9VxR reason="signature verification failed"; 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 Received: from localhost (localhost.localdomain [127.0.0.1]) by mail.alien8.de (SuperMail on ZX Spectrum 128k) with ESMTP id 27EA540E0205; Tue, 30 Jan 2024 11:00:04 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at mail.alien8.de Authentication-Results: mail.alien8.de (amavisd-new); dkim=fail (4096-bit key) reason="fail (body has been altered)" 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 VsO5Brlea4Hx; Tue, 30 Jan 2024 11:00:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=alien8; t=1706612401; bh=TdE1TyLPhJyinJAuaEh9u5E7I9lI8ReROFpNtz5Ue0o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ksOA9VxRXoKxCcXqpVFvEHWsqjugfsl2cCY3Yp2zuFJdL7MvL8RKYmnNKwj525F6d en2M0W7UMbYBfJuTLQQjL9k1G05EVNPa9nfGgMdgE5tZf4+aakZ4E65fJFLWd4bH9b IcBiWgGJs+6cFjvm+5HX3ccbBfTXwar2HiB4WGLIhRqA547I9gxsfH9+n5ol3M4l0d NMLjzY7UvPwdlg4qpNDERdgAowtmDcbOzkLwlu3TQl7xTxvqwBdth0i6M7jMTfbw0K GUBBk/F0yk+a1uL4XiKN2QGwkHiPeeu9VjZ5MXD7wCKwsp4S8LwKAXaRkVl0Gx0Zvx 1wZzI3osDmr1aiIjag4ANDfIzoF8/9ccHYCe3oyd2ZAYnrh1zSSbqQkC5IsI0B7ykd t/ft3zDkGBAQ0SYEhbctQiP8GKemKBweltiLvd7PW3ewKbBpEmc995dgZasCE0eNTL 7hWlsHkHlXbn7UAnncetj2nft/Df+CkIEZU83fdJU7KbtUAWYuUDI5JukOnBZUxEbQ yF+sRwwNtX1OBxnU3CrN/XisSh39KsBUqVz7mRkovuN64Ge0Dzvur0K56sUE47Gn4a EB+CDQ7wEASszHfNDvpMNTvvtBAj5Py1G8IpzGYZNkgCzGmgWmH1q2BjVDu2/FVkfh /JmZT1onOxRj9LqQs/TYCICI= Received: from zn.tnic (pd953033e.dip0.t-ipconnect.de [217.83.3.62]) (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 ESMTPSA id C786040E00C5; Tue, 30 Jan 2024 10:59:58 +0000 (UTC) From: Borislav Petkov To: X86 ML Cc: Paul Gortmaker , LKML Subject: [PATCH 2/4] x86/alternatives: Get rid of __optimize_nops() Date: Tue, 30 Jan 2024 11:59:39 +0100 Message-ID: <20240130105941.19707-3-bp@alien8.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240130105941.19707-1-bp@alien8.de> References: <20240130105941.19707-1-bp@alien8.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789514966856091714 X-GMAIL-MSGID: 1789514966856091714 From: "Borislav Petkov (AMD)" 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 the patching. A lot simpler code. Signed-off-by: Borislav Petkov (AMD) --- 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 d633eb59f2b6..2dd1c7fe0949 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -216,47 +216,12 @@ static int skip_nops(u8 *buf, int offset, int len) return offset; } -/* - * 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 = *next - insn->length; - - switch (insn->opcode.bytes[0]) { - case JMP8_INSN_OPCODE: - case JMP32_INSN_OPCODE: - *prev = i; - *target = *next + insn->immediate.value; - return false; - } - - if (insn_is_nop(insn)) { - int nop = i; - - *next = skip_nops(buf, *next, len); - if (*target && *next == *target) - nop = *prev; - - add_nop(buf + nop, *next - nop); - DUMP_BYTES(ALT, buf, len, "%px: [%d:%d) optimized NOPs: ", instr, nop, *next); - return true; - } - - *target = 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 = 0; - for (int next, i = 0; i < len; i = next) { struct insn insn; @@ -265,7 +230,14 @@ static void __init_or_module noinline optimize_nops(const u8 * const instr, u8 * next = i + insn.length; - __optimize_nops(instr, buf, len, &insn, &next, &prev, &target); + if (insn_is_nop(&insn)) { + int nop = i; + + next = skip_nops(buf, next, len); + + add_nop(buf + nop, next - nop); + DUMP_BYTES(ALT, buf, len, "%px: [%d:%d) optimized NOPs: ", instr, nop, next); + } } } @@ -342,8 +314,6 @@ bool need_reloc(unsigned long offset, u8 *src, size_t src_len) static void __init_or_module noinline apply_relocation(const u8 * const instr, u8 *buf, size_t len, u8 *src, size_t src_len) { - int prev, target = 0; - for (int next, i = 0; i < len; i = next) { struct insn insn; @@ -352,9 +322,6 @@ apply_relocation(const u8 * const instr, u8 *buf, size_t len, u8 *src, size_t sr next = i + insn.length; - 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(struct alt_instr *start, for (; insn_buff_sz < a->instrlen; insn_buff_sz++) insn_buff[insn_buff_sz] = 0x90; - apply_relocation(instr, insn_buff, a->instrlen, replacement, a->replacementlen); + apply_relocation(instr, insn_buff, a->instrlen, replacement, insn_buff_sz); + optimize_nops(instr, insn_buff, insn_buff_sz); DUMP_BYTES(ALT, instr, a->instrlen, "%px: old_insn: ", instr); DUMP_BYTES(ALT, replacement, a->replacementlen, "%px: rpl_insn: ", replacement);