From patchwork Tue Jan 30 10:59:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 194023 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1149902dyb; Tue, 30 Jan 2024 03:32:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IElj4/6CSsS+W/WzTRcIObPJ0kCSMK2wcHbnEK0XBFqMgvcaquqZArm1veEYlEvpKtxciYZ X-Received: by 2002:a05:6a00:3d15:b0:6da:fc2b:5a88 with SMTP id lo21-20020a056a003d1500b006dafc2b5a88mr8027016pfb.15.1706614352197; Tue, 30 Jan 2024 03:32:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706614352; cv=pass; d=google.com; s=arc-20160816; b=cC7d3pWLYFtxWM76qh1dj2KEMV+MkAGDEoKgI+sqpP3sGLAxhlKNzsPLCdRzQoJiYl vxkbjAGiFfN0okELWp1Klv6m8cpID67gnGrrZmjNgnbQ+rV61kAmcdTPP4r8Aw8HW/P3 c1HN9CdqsQOJCrvOtfxQPjYNGcfVbmfu1Ggqn43DlFNJ7LLyOud3vIqW/GIacigYy+6q Hnl6/QXzwaiW3JeYpgZ+SNUT2vZ3d4lsCJxnMLOL/Bszoe1evll+/Nq/C46Tn4H7e1c6 iD+FJJa0n5mQaFb6q8SnE118zOs9UBP4+sXm+1UBbSFHRFLMJioW7VeSrx6XQjeT9ea7 QYGQ== 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=Ra2HZFoTz/ENYQfYWITIYHvzF2eheKw8udoQHz0AJv4=; fh=zSaXbEwN8+H5Eur5FdIJJ2+AwJW9h+H8hQ6VtLQvXVE=; b=NEn/F8akrwTB4UtEIKLNgF64WGU14aU7HxRKCTWAAXVOSGDUcAWZS2fWVY55LGpDwk PYelUcEQEMvPyY2KQr4NV598Pk8VSL1IWVVH6TdZyElV7nv/7YJJs7dez0DAYrnnd7Md ckfLjOwQd3fGDeKJQvRybG842P/9KE7iT0juAXIZmn+z53YSOf3NLnC3KPbWDJj7DJzQ OlkO8rPD9a8WEd53cYx+vUdbaNvDXf+s0eBsw3kMJc45uJ/QQ9oItxnOFF0HnYMnQpS5 jtgQD1zalBpitDH2eWHzuylfvJAWAF5QB1VEQMbESNvKl3Su3rHimzctPn2MxkF7QAaT QNxA== ARC-Authentication-Results: i=2; mx.google.com; dkim=neutral (body hash did not verify) header.i=@alien8.de header.s=alien8 header.b=NtM6XUdy; arc=pass (i=1 spf=pass spfdomain=alien8.de dmarc=pass fromdomain=alien8.de); spf=pass (google.com: domain of linux-kernel+bounces-44443-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44443-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 l19-20020a656813000000b005d8c55c9ca1si3886230pgt.358.2024.01.30.03.32.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 03:32:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-44443-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=NtM6XUdy; arc=pass (i=1 spf=pass spfdomain=alien8.de dmarc=pass fromdomain=alien8.de); spf=pass (google.com: domain of linux-kernel+bounces-44443-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44443-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 E6BA0B2E505 for ; Tue, 30 Jan 2024 11:00:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5C0BA67757; Tue, 30 Jan 2024 11:00:08 +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="NtM6XUdy" 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 DDD2766B5E for ; Tue, 30 Jan 2024 11:00:01 +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=1706612404; cv=none; b=eRSyzhNK0SRUqeGCjJK2P/gK0Mo7ZiZ2KztTKAtcGMNcRr/1MvUdX2ZMLSXxG5hMfmOV+FsegtZ4qeZaEUrY3z7C4QHQZpHKDxJKeXHgx/evO+1gCg8bfnkbEOhp+/Z3/Uqp6NycS0qclZHbC0zaYBtAFSarU8lIir3fh+Evulg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706612404; c=relaxed/simple; bh=S6v5NG3zkWzybZS+L6ZCuQTTnUlx2jbdtvaXdKu2GAw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UNxEYpxy0RTX+RMshQSbTazZIw9Hyx7OF0l4dUbOLp3OjTckIDjGg3u+J2baanKQQTtzvG1Q/rYc1JyDyPYSzHu2vcnJYC84fDCyvVN5WxlVDXTqiOQS4A6O05Xw3GdUs6qobuEGPw7J6PKhKcvQ74OWDIsS4rIAntb+lXaM0tA= 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=NtM6XUdy 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 E168B40E01BB; Tue, 30 Jan 2024 10:59:59 +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 Zr7zz19iKXhN; Tue, 30 Jan 2024 10:59:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=alien8; t=1706612397; bh=Nzb7MQEnsKeU91bcnGcqQoyDmQJKUpafnfMpQ9uplZ8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NtM6XUdyOGaMTSux6J2dRXVlZM6So+Ty9wR4Cxs3uMIsJ41WIrgJ6mXk05DiWCOIy sU9uu7SjzzkQjQJBj9Vh2jod9hREa+F5/oeShrHg3Hf/0E8ijkx1i1Ywuhrx7hW8Bg FwqGzixLJ5lDtcyA1UaZOMM6chzyCuahkBa+ASOgTZhSL82W5IWGH/FJ68YteybMo+ I2TBm2KnRp7yAzuPQGo2lRg11jc9NgawaeoLe+ogd+BqVlxEHU439Pqdx/dEl7lIwi YSarGScGDRAgnDX7kYZtHkHLL5Qmh/8GFESOfyHN4TbpCPJ4c6atvbh7vC9JMf+Wnr alwGLPA2cHXtJqLxzHC8t83hkfuEp01IWY8V3Sf+iYOI84nN0OmCVSnxru5g1MmH5d s4OGbs1dfyU2oTrqBcEpQFPi8ikviXEbb2xEMiLxl6iDlVElGgPqF+zZEXCRp7flvV if4hkmHeO9KQHd+j1MZZLSg9tvfcQBIbTkYQWaKyedPCq/3rp9Lx6aCIKuHQSe3LT/ vFkUQ7yu0RPwVS3gPyr5UlTIGdkjPXwX5OtFWAndSJ5xZ0YDf1LCERff0rb/RAuuJO ZDL6iHvI1pF+h7mPgRiM1UCRr3lspm6CwDXlrK5sqTYAVdZ2RVKo/zq4gs1FHA1SLu LP/fvJzkcfQKv6YRjo8lCy0Y= 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 331CC40E0177; Tue, 30 Jan 2024 10:59:53 +0000 (UTC) From: Borislav Petkov To: X86 ML Cc: Paul Gortmaker , LKML , Thomas Gleixner Subject: [PATCH 1/4] x86/alternatives: Use a temporary buffer when optimizing NOPs Date: Tue, 30 Jan 2024 11:59:38 +0100 Message-ID: <20240130105941.19707-2-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: 1789514850922199150 X-GMAIL-MSGID: 1789514850922199150 From: "Borislav Petkov (AMD)" Instead of optimizing NOPs inplace, use a temporary buffer like the usual alternatives patching flow does. This obviates the need to grab locks when patching, see 6778977590da ("x86/alternatives: Disable interrupts and sync when optimizing NOPs in place") While at it, add nomenclature definitions clarifying and simplifying the naming of function-local variables in the alternatives code. Signed-off-by: Borislav Petkov (AMD) Acked-by: Thomas Gleixner --- arch/x86/kernel/alternative.c | 78 +++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index cc130b57542a..d633eb59f2b6 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -124,6 +124,20 @@ const unsigned char * const x86_nops[ASM_NOP_MAX+1] = #endif }; +/* + * Nomenclature for variable names to simplify and clarify this code and ease + * any potential staring at it: + * + * @instr: source address of the original instructions in the kernel text as + * generated by the compiler. + * + * @buf: temporary buffer on which the patching operates. This buffer is + * eventually text-poked into the kernel image. + * + * @replacement: pointer to the opcodes which are replacing @instr, located in + * the .altinstr_replacement section. + */ + /* * Fill the buffer with a single effective instruction of size @len. * @@ -133,28 +147,28 @@ const unsigned char * const x86_nops[ASM_NOP_MAX+1] = * each single-byte NOPs). If @len to fill out is > ASM_NOP_MAX, pad with INT3 and * *jump* over instead of executing long and daft NOPs. */ -static void __init_or_module add_nop(u8 *instr, unsigned int len) +static void __init_or_module add_nop(u8 *buf, unsigned int len) { - u8 *target = instr + len; + u8 *target = buf + len; if (!len) return; if (len <= ASM_NOP_MAX) { - memcpy(instr, x86_nops[len], len); + memcpy(buf, x86_nops[len], len); return; } if (len < 128) { - __text_gen_insn(instr, JMP8_INSN_OPCODE, instr, target, JMP8_INSN_SIZE); - instr += JMP8_INSN_SIZE; + __text_gen_insn(buf, JMP8_INSN_OPCODE, buf, target, JMP8_INSN_SIZE); + buf += JMP8_INSN_SIZE; } else { - __text_gen_insn(instr, JMP32_INSN_OPCODE, instr, target, JMP32_INSN_SIZE); - instr += JMP32_INSN_SIZE; + __text_gen_insn(buf, JMP32_INSN_OPCODE, buf, target, JMP32_INSN_SIZE); + buf += JMP32_INSN_SIZE; } - for (;instr < target; instr++) - *instr = INT3_INSN_OPCODE; + for (;buf < target; buf++) + *buf = INT3_INSN_OPCODE; } extern s32 __retpoline_sites[], __retpoline_sites_end[]; @@ -187,12 +201,12 @@ static bool insn_is_nop(struct insn *insn) * Find the offset of the first non-NOP instruction starting at @offset * but no further than @len. */ -static int skip_nops(u8 *instr, int offset, int len) +static int skip_nops(u8 *buf, int offset, int len) { struct insn insn; for (; offset < len; offset += insn.length) { - if (insn_decode_kernel(&insn, &instr[offset])) + if (insn_decode_kernel(&insn, &buf[offset])) break; if (!insn_is_nop(&insn)) @@ -207,7 +221,7 @@ static int skip_nops(u8 *instr, int offset, int len) * to the end of the NOP sequence into a single NOP. */ static bool __init_or_module -__optimize_nops(u8 *instr, size_t len, struct insn *insn, int *next, int *prev, int *target) +__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; @@ -222,12 +236,12 @@ __optimize_nops(u8 *instr, size_t len, struct insn *insn, int *next, int *prev, if (insn_is_nop(insn)) { int nop = i; - *next = skip_nops(instr, *next, len); + *next = skip_nops(buf, *next, len); if (*target && *next == *target) nop = *prev; - add_nop(instr + nop, *next - nop); - DUMP_BYTES(ALT, instr, len, "%px: [%d:%d) optimized NOPs: ", instr, nop, *next); + add_nop(buf + nop, *next - nop); + DUMP_BYTES(ALT, buf, len, "%px: [%d:%d) optimized NOPs: ", instr, nop, *next); return true; } @@ -239,32 +253,22 @@ __optimize_nops(u8 *instr, size_t len, struct insn *insn, int *next, int *prev, * "noinline" to cause control flow change and thus invalidate I$ and * cause refetch after modification. */ -static void __init_or_module noinline optimize_nops(u8 *instr, size_t len) +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; - if (insn_decode_kernel(&insn, &instr[i])) + if (insn_decode_kernel(&insn, &buf[i])) return; next = i + insn.length; - __optimize_nops(instr, len, &insn, &next, &prev, &target); + __optimize_nops(instr, buf, len, &insn, &next, &prev, &target); } } -static void __init_or_module noinline optimize_nops_inplace(u8 *instr, size_t len) -{ - unsigned long flags; - - local_irq_save(flags); - optimize_nops(instr, len); - sync_core(); - local_irq_restore(flags); -} - /* * In this context, "source" is where the instructions are placed in the * section .altinstr_replacement, for example during kernel build by the @@ -336,7 +340,7 @@ bool need_reloc(unsigned long offset, u8 *src, size_t src_len) } static void __init_or_module noinline -apply_relocation(u8 *buf, size_t len, u8 *dest, u8 *src, size_t src_len) +apply_relocation(const u8 * const instr, u8 *buf, size_t len, u8 *src, size_t src_len) { int prev, target = 0; @@ -348,7 +352,7 @@ apply_relocation(u8 *buf, size_t len, u8 *dest, u8 *src, size_t src_len) next = i + insn.length; - if (__optimize_nops(buf, len, &insn, &next, &prev, &target)) + if (__optimize_nops(instr, buf, len, &insn, &next, &prev, &target)) continue; switch (insn.opcode.bytes[0]) { @@ -365,7 +369,7 @@ apply_relocation(u8 *buf, size_t len, u8 *dest, u8 *src, size_t src_len) if (need_reloc(next + insn.immediate.value, src, src_len)) { apply_reloc(insn.immediate.nbytes, buf + i + insn_offset_immediate(&insn), - src - dest); + src - instr); } /* @@ -373,7 +377,7 @@ apply_relocation(u8 *buf, size_t len, u8 *dest, u8 *src, size_t src_len) */ if (insn.opcode.bytes[0] == JMP32_INSN_OPCODE) { s32 imm = insn.immediate.value; - imm += src - dest; + imm += src - instr; imm += JMP32_INSN_SIZE - JMP8_INSN_SIZE; if ((imm >> 31) == (imm >> 7)) { buf[i+0] = JMP8_INSN_OPCODE; @@ -389,7 +393,7 @@ apply_relocation(u8 *buf, size_t len, u8 *dest, u8 *src, size_t src_len) if (need_reloc(next + insn.displacement.value, src, src_len)) { apply_reloc(insn.displacement.nbytes, buf + i + insn_offset_displacement(&insn), - src - dest); + src - instr); } } } @@ -505,7 +509,9 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, * patch if feature is *NOT* present. */ if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) { - optimize_nops_inplace(instr, a->instrlen); + memcpy(insn_buff, instr, a->instrlen); + optimize_nops(instr, insn_buff, a->instrlen); + text_poke_early(instr, insn_buff, a->instrlen); continue; } @@ -527,7 +533,7 @@ 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(insn_buff, a->instrlen, instr, replacement, a->replacementlen); + apply_relocation(instr, insn_buff, a->instrlen, replacement, a->replacementlen); DUMP_BYTES(ALT, instr, a->instrlen, "%px: old_insn: ", instr); DUMP_BYTES(ALT, replacement, a->replacementlen, "%px: rpl_insn: ", replacement); @@ -762,7 +768,7 @@ void __init_or_module noinline apply_retpolines(s32 *start, s32 *end) len = patch_retpoline(addr, &insn, bytes); if (len == insn.length) { - optimize_nops(bytes, len); + optimize_nops(addr, bytes, len); DUMP_BYTES(RETPOLINE, ((u8*)addr), len, "%px: orig: ", addr); DUMP_BYTES(RETPOLINE, ((u8*)bytes), len, "%px: repl: ", addr); text_poke_early(addr, bytes, len); 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); From patchwork Tue Jan 30 10:59:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 193998 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1139876dyb; Tue, 30 Jan 2024 03:11:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IGLnxYVd7hZHrOXCJ+PNPJ1Bxj5ZDwUtGEjYKLrkmiqFME93u1Hd/JWgA3KeeUW4ZYs6put X-Received: by 2002:a17:906:ae54:b0:a30:b47:b626 with SMTP id lf20-20020a170906ae5400b00a300b47b626mr7191628ejb.35.1706613076577; Tue, 30 Jan 2024 03:11:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706613076; cv=pass; d=google.com; s=arc-20160816; b=x0GOv6omFU1tivZc7EyUHcqZGbXTB9btjIHweomiraHhIHZNdctnIudZ8EzrlO328S xbiNyeGE+kfw8yIy2TYzP4XxdSPfyODRFxWKlEeT9ReiVVBvNLQvY+X0uZtPwmm0DJGP nZCuLS+Y5XPqLJDPMR8/iJ3yw6dTokDIWQwa6g8VNycTCHeUQQDO6D0f4ZwmUChE2ls8 zIBB6gvUZJSStMknNe/dGLLLUFazmEM80QcX7t0t+toVqTDdjdevee5obke5bma38263 KM7D/JOgjXR6Rn0JAldiaQL5lyKTc7wpYDAsqfRJBWxZ9sEag+qkwZKN3gOmmYIzIo88 MvMA== 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=1kMLFDn6HFypUek5nuT7XCSQHyO5gXydItUseUqPOVM=; fh=gL8iKWYz+5Gj006D9LUvWYS+TWLN0CkoSVsKmJTYNn0=; b=TCg+IV+IwT1lYC+1SOjB0VjcntPtthLsezv3Jig+LPUYB0EAbKcFEaB42I7aibhYXX m4to+bGTHrVyxT+qCMhkm+7Ozm13aJ6i1v3kW09GHF5a2FskayLsIpO8/kHLk02YtDPD rCrsfgAZQtKONADkiLZbZHyOxVzI6L6DhjSyKLPxmbnohwd5NDfM/DOaVK8a9Bd3y4g5 ZDA6AK5aJaoMGTJVFNlMcocBdHsn6uT3b4AXwPTj6LxiQkyK8PJFj/qn/7LTctyf8N+7 zyfM7FZl8+8AOfguHyRR5jkWQ2Q4jmq3BIorFNy6qSyFGt7azS7mfHPP0fmToGOuEaPM W/CA== ARC-Authentication-Results: i=2; mx.google.com; dkim=neutral (body hash did not verify) header.i=@alien8.de header.s=alien8 header.b=OTjnlGn6; arc=pass (i=1 spf=pass spfdomain=alien8.de dmarc=pass fromdomain=alien8.de); spf=pass (google.com: domain of linux-kernel+bounces-44445-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44445-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alien8.de Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id i18-20020a170906251200b00a349639fa32si4198913ejb.781.2024.01.30.03.11.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 03:11:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-44445-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@alien8.de header.s=alien8 header.b=OTjnlGn6; arc=pass (i=1 spf=pass spfdomain=alien8.de dmarc=pass fromdomain=alien8.de); spf=pass (google.com: domain of linux-kernel+bounces-44445-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44445-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 am.mirrors.kernel.org (Postfix) with ESMTPS id DE0F91F2DE5D for ; Tue, 30 Jan 2024 11:01:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6210867E64; Tue, 30 Jan 2024 11:00:15 +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="OTjnlGn6" 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 CDB59679FD for ; Tue, 30 Jan 2024 11:00:10 +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=1706612412; cv=none; b=HRHc1AOuXSDS66+Z4peOyu0SPsHRTQ8GXowLeD/5e/n2HNzIOcCWqNJTELF4gR1K3O95sRVocc53rxMNGpIwZGjoVCkFJZB3awF5isGz4UPyCd+wVOMtNAgu6kG3GXTwpksRObmV+MbyMGYAlDzwfmxe2eZYPraIFt1AnoWNZzo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706612412; c=relaxed/simple; bh=jgeafpJI+MYDNRoPvXNTdDfiHQEnLFcoETk5RR++mSA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cD58jd7RfoI5IKPLJo7avPkl9jF11CHcaugnrZ6LNLpj54vxmrZukwy7vTS7NPX4L9d9WLjjZGO9eIJ4XViQRmkWevL0kXjUzteGVo6ExepDrgMJJrZdCSOTITGDKWt2ZkZof2gwYzS+JnkmxT/qpB8oXHhwoD5/8jNo3azgyNA= 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=OTjnlGn6 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 DE99840E01FE; Tue, 30 Jan 2024 11:00:08 +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 km_ryPGWyPCV; Tue, 30 Jan 2024 11:00:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=alien8; t=1706612406; bh=njqwLfrv56leHkX+AonJAZ0qg8TBI+C0ajpxz+Cn0Oo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OTjnlGn6LgS9QuS49oeJ51uVOHhFXrZOzFkMxaJc5FTZQUM24RJpzByIImRZrdi0x Kp2+1i8t7mAruSVfSzPb690CUJlhdcs0PytNvnek5izutBVARqUTr9MFO1DY9t9RGx +ZZwfdjnAMnzDCcioulTTZlnYSJ/coCMeLjFOTjtxNVnZ0iRCfTsZMSIt35DMeYmm9 fXQE/HHPm1irE+ccxrGeKAZMo1O4Yr65tfwXWjPq6geTE4HTI3vr6Ma7zoZNmIsoqT xLCFrDXpy5SuZlNKNgBVk8Yn16ccb42OeYZsss/J4TEdDvqGR5ue7oICVvUTvFiFbF /JHxZaEqNYU39PUHzb7mLaaOoONCHxYXYv5ijYtlyYzsQAp6Srwbu080o4VwBR30Sh 4Hys7u3OV/Ua4IILMgTSSSFcA+u92rKeCTNGHJ01C/QfR5IIJjK6r5ps4uGVQDZGUC 1KCg4RoWOpkiqMafjgFIHWqmEL1NIRqrCBG/SOO1W8+EUTVtgdCi/eg6p+Rx3x8M8I OMINe6Uev0RagiBQ4WGdhhptBaNEv80IeOucy/5psVL05+ysZD/4RORE5wQ8cookqb 8TZIkv69YgilpOIzZ0YeT3Q662yAeneeLhnWqIRfIs3ALBgHVyl/zFjFgf6ih84Lik CVZJWoo4SLSZNQyLEN/Rq1/A= 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 867C840E0177; Tue, 30 Jan 2024 11:00:03 +0000 (UTC) From: Borislav Petkov To: X86 ML Cc: Paul Gortmaker , LKML Subject: [PATCH 3/4] x86/alternatives: Optimize optimize_nops() Date: Tue, 30 Jan 2024 11:59:40 +0100 Message-ID: <20240130105941.19707-4-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: 1789513513785030395 X-GMAIL-MSGID: 1789513513785030395 From: "Borislav Petkov (AMD)" Return early if NOPs have already been optimized. Signed-off-by: Borislav Petkov (AMD) --- arch/x86/kernel/alternative.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 2dd1c7fe0949..68ee46c379c1 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -233,6 +233,10 @@ static void __init_or_module noinline optimize_nops(const u8 * const instr, u8 * if (insn_is_nop(&insn)) { int nop = i; + /* Has the NOP already been optimized? */ + if (i + insn.length == len) + return; + next = skip_nops(buf, next, len); add_nop(buf + nop, next - nop); From patchwork Tue Jan 30 10:59:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Borislav Petkov X-Patchwork-Id: 193999 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1139991dyb; Tue, 30 Jan 2024 03:11:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IFE3XOJR2lFjcmjrqj1mcx2fAVodxjGNmXOGiAtS/Lu4GjCW2Q203xicZf5qxNw45L9GDCo X-Received: by 2002:a17:906:d286:b0:a36:3b65:64b with SMTP id ay6-20020a170906d28600b00a363b65064bmr418328ejb.15.1706613089648; Tue, 30 Jan 2024 03:11:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706613089; cv=pass; d=google.com; s=arc-20160816; b=ya/D3tugGMNU4qTE4w7zzp/TLzUFsNBANX7kyXV1lZlqa2y0XQubA730Jy19HFt+5C BkI55XL8OwGh4v5L0cMEXlCPrcnN19FrYswFzlAssqq9EUppmor9z//F/klHFXhX3OO3 gzdw2rTgZy7WhCFrNRkHFRIXZdfurTsyFK/XXqKKWd1HlhgQKlbqySISKzIDUb+xldoG 3OEBme8Xka2JSdH7aBeNoT16LcPsoeV900K0tnizy/k43LlewNMuLFvGoCvzJKc0HYeB eyHuRfuss6lvNSkZoykRlkVicpRVtW3eceptplXsk1ozAtVwsaS+jr7HSsDfYIvExZkG OFYg== 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=Nk//SKYULprYZKz6Aag3i4AER9JmEF0nplw8l6pga+E=; fh=gL8iKWYz+5Gj006D9LUvWYS+TWLN0CkoSVsKmJTYNn0=; b=WQf0yF1E/K6gCm++XV8SAUP3CIMqdLbGW1/+rYK81i9keuEncRt9IhsQo05YVS67Uz cVGxqOBj/p1lszU1t9yFnhQX9O4oKnAGkMouOSrlnnFkhom2nkl9daaHWhxpSuRFo0D+ wM6SHJcpH7tOLI0LPYKV7+eAfRwDC32vvxGEKGWYs+bDOLiOcFU+g8zjTfSNDqK43C6p V8kT/aWXYStd2NZRqi7dmDa81Y9wIcnliB3AlL3XJg6F2YOZK98pOBUVFRl+/6nvlW0X IiCxSmJZ7A1sUgHDvD7tDin3B33pkQL0ollI4twp2KNxsvo4nOuu0Na4at4GsNmL+9vh nwmw== ARC-Authentication-Results: i=2; mx.google.com; dkim=neutral (body hash did not verify) header.i=@alien8.de header.s=alien8 header.b=ksB+8sP2; arc=pass (i=1 spf=pass spfdomain=alien8.de dmarc=pass fromdomain=alien8.de); spf=pass (google.com: domain of linux-kernel+bounces-44446-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44446-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alien8.de Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id h10-20020a170906718a00b00a35df307711si1405137ejk.286.2024.01.30.03.11.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 03:11:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-44446-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@alien8.de header.s=alien8 header.b=ksB+8sP2; arc=pass (i=1 spf=pass spfdomain=alien8.de dmarc=pass fromdomain=alien8.de); spf=pass (google.com: domain of linux-kernel+bounces-44446-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44446-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 9AB0C1F2E0AB for ; Tue, 30 Jan 2024 11:01:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A8AE267E8F; Tue, 30 Jan 2024 11:00:19 +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="ksB+8sP2" 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 4B64D67E63 for ; Tue, 30 Jan 2024 11:00:15 +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=1706612416; cv=none; b=UkU104PO5+lk8vwWHTVkL7D3knYd45WZNdH6Gv4V4iXBKrOy3trJ+WA/HvSA/VEPoqqEGwxNP1cGeMMacunh3gJgljvtPmPqmIQgjmPDab4+9EZtbgzlTOlGTU32IGub+Fai0VDQNxfTG4YrCd/K1B/UsS5x+9dP/vBQ1s39zLw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706612416; c=relaxed/simple; bh=iZE2cJ+UUdAouN6yh05MZbqDS0n/dNAjBnlGvlNnGZ4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nBDfJIdWj8JHDP4uYRfqM83Ry+XB3Wegd2bfvxjT117/wiTTWjyLUcNoGDTeGVqFxn5AWykhuOHsarTtu13KVBE5WbkNj9E+CuRW82k0VNh9n1xUd9LPVjKJVIhCUGw1AI1GHsjusiGZLnr4+39OJSxa9sDXa2e2SveKNLQYYx0= 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=ksB+8sP2 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 6302440E0205; Tue, 30 Jan 2024 11:00:13 +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 ca8CsOTBOhjH; Tue, 30 Jan 2024 11:00:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=alien8; t=1706612411; bh=RoNfl/PZJS4s6CCR7YHy3tG03HlOLbZeqL1F+quJhz0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ksB+8sP2xf8kWs2Ebc5SwExLY6g1pvpCRQl4iNU+U30tU19CH7boN+YZpymBmIbp6 BNw5OhJTQo/urOeoNcwv6Wa8zHBGi2CcP3kA4Idh7ZELmtOK1tsmAABiVz2nQAuR88 lKiGYtpF5FBNtqSpkYJMyks/vAmHJKsvAxYuRg3MLda88z70bz1nRsaVgDsvDoJjmi WGTjigW+GhhvUnUBjt0UgvR+o5qP6Ck49Z+7tvm5tVAV2nO1fQ4B1PcKE59hi2P5Ae 3RTjdOrlpZIAXtZ4YWbZJ/t0hBLBFKVgiCTuZRxungRMA6DGYgeU4p+Oy1uDb6zKhp r3QE69jRr3e+Jjcq/aBHpGew1CcHvnEZO4Tw0WC4x0KmOrTu09avJxmiTLz+YPkNIt eb7MdS6aNM/IObRGFxBWmGPOJe9mN4fmT/EGUqHoPByxU6fLyTjGD1FL8qzx+sokjq 9AI/2RV7bD76I6HaDlpmN2z5NBwYT2MziRl4TNGPThLGcWKXwLsf4cGgQlS7v+lOoA TdsniwDQjhZVlUT15ggyxUpY2LnYjDn1XF18UsfB6TKa/o3iWpqnZkCpmPOucpV+3p ylzP6F8O4vFoUqyw8LEKSfJ11QroiyeDKZOJ5dg+HfiULQ9r9GGTK6nRSDhCTWGPIr OSIv0t0HmvMC8N3yBGXvES+s= 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 25BDE40E01BB; Tue, 30 Jan 2024 11:00:08 +0000 (UTC) From: Borislav Petkov To: X86 ML Cc: Paul Gortmaker , LKML Subject: [PATCH 4/4] x86/alternatives: Sort local vars in apply_alternatives() Date: Tue, 30 Jan 2024 11:59:41 +0100 Message-ID: <20240130105941.19707-5-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: 1789513527020092608 X-GMAIL-MSGID: 1789513527020092608 From: "Borislav Petkov (AMD)" In a reverse x-mas tree. No functional changes. Signed-off-by: Borislav Petkov (AMD) --- arch/x86/kernel/alternative.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 68ee46c379c1..ee0f681ae107 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -440,9 +440,9 @@ static int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_instr *a) void __init_or_module noinline apply_alternatives(struct alt_instr *start, struct alt_instr *end) { - struct alt_instr *a; - u8 *instr, *replacement; u8 insn_buff[MAX_PATCH_LEN]; + u8 *instr, *replacement; + struct alt_instr *a; DPRINTK(ALT, "alt table %px, -> %px", start, end);