From patchwork Mon Oct 17 14:53:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 3509 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1493497wrs; Mon, 17 Oct 2022 07:56:34 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6sThZlRkkbM6LuxDp+93rxgayU32do1jA1VLVoL8NenyMIrBY6ZztcP+YfBvnpWrya9m1e X-Received: by 2002:a05:6a02:20c:b0:461:74e5:ce9f with SMTP id bh12-20020a056a02020c00b0046174e5ce9fmr11151451pgb.294.1666018594358; Mon, 17 Oct 2022 07:56:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666018594; cv=none; d=google.com; s=arc-20160816; b=pHzG5Ol9T5HVOA2yaf3tQeh+eh6ihO63nTX+MZaIL8zwbc7CKoz60GhAyrw66ZosLg 05BuY7lR1foWpbBTC323DITv7KeNVAuMZfNLkLmVdLArIYAl6QqgpjzA8PT7yxwepai9 izaGgYti4hWRwx+VsqKsp6JVlmzzchHCMbU1oMkqYZ1S1+684zeDeF3WigBm17Lupnia asEjod1M5DsjAc3HSMy5mTu8JWdvg0LMdGInzCsgx6WPrMWlRzQc/5HhlAtHcxWGye5z 7GBxTw1TcWkbhLO6hLZvP7RgIkQKAwWIwmBPbesRAX96bZbKsSkxCkqhCsds6mShqSqt 1IDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=kKeknugwLk+xvR0FEL/ext5YoRe9kcdKsrruIcJuqcM=; b=Dq/k9Vx4Uk6K0roybwU5xifrFDHOpczXywFhWyUCXw5zlYwmsgtAwxgFxPxizc/XAt cYkyD4X83cEauWokEL0KgtoNq/++n803AycIwMiEigzLB1+tvZwzmZIsP13fsnskaQn1 mKEQWwizRH7QvhSsSN4XFKljONtSCg9/mjV/OLPe+gVSOMFR7HWzvMOEJjHVh3+0D0k6 D6niYwE9FKQyi9f+W/NWmNUCxS4+tuKk2NXNxGX7A5m9s5aok/vdgyGz4ZIrb5QBYUWY M+Yr1UVa+kSrXzGg5nie15novlCwJsNXrprnh4LA1Y58kdW+E0YFjdELyahFKbs9aeoL RI9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=aim5g2OX; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h18-20020a056a001a5200b00562aaf29d36si10392846pfv.298.2022.10.17.07.56.19; Mon, 17 Oct 2022 07:56:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=aim5g2OX; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231343AbiJQOze (ORCPT + 99 others); Mon, 17 Oct 2022 10:55:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231147AbiJQOyE (ORCPT ); Mon, 17 Oct 2022 10:54:04 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78D6367CAA; Mon, 17 Oct 2022 07:53:57 -0700 (PDT) Date: Mon, 17 Oct 2022 14:53:48 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666018429; 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=kKeknugwLk+xvR0FEL/ext5YoRe9kcdKsrruIcJuqcM=; b=aim5g2OXS9V44PfIi7lP1jv2gYXtAgZ10YMOgsAcIqTNaYgGrbrzCmbR+xY9ziz8E2ws5y L2XxRd++vAs0LCYzEKbr5ENtgmkBv13jB4/JuJorpeGJzLcIqg9wVBjZwtvJLIY5ek3GVl pgJgf61MhHZh96eH87ayRyLSc2xIraWCYCc7LfjiHAWXAKRNjtFXPX/4ED8969STjzts7s UX9Pwgw0exsXzrjzF9QLnHgar3CsG/AVPn4M6kOOBcXka4Tuvh1ylGJ/HhtcpK3CYMKXjZ gRRP1vOqpDlniBf3giifpixxjqAPi36fID5sHmyaKVV7kf6eaobRR7vzVs7ccQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666018429; 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=kKeknugwLk+xvR0FEL/ext5YoRe9kcdKsrruIcJuqcM=; b=3RD6Gm4wFGOtkOBNbwnXQdVFQoKaoFHZ43+S18ReDyCnz1Q9iRmsKKStyRI60b6sqX+Ypp 26ipxeYp3/nvPpDQ== From: "tip-bot2 for Thomas Gleixner" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/core] x86/alternatives: Provide text_poke_copy_locked() Cc: Thomas Gleixner , "Peter Zijlstra (Intel)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20220915111147.159977224@infradead.org> References: <20220915111147.159977224@infradead.org> MIME-Version: 1.0 Message-ID: <166601842806.401.11281852653540224244.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746947114004935461?= X-GMAIL-MSGID: =?utf-8?q?1746947114004935461?= The following commit has been merged into the x86/core branch of tip: Commit-ID: fe54d0793796ccdb213d8ea7bff0b49903b6afaa Gitweb: https://git.kernel.org/tip/fe54d0793796ccdb213d8ea7bff0b49903b6afaa Author: Thomas Gleixner AuthorDate: Thu, 15 Sep 2022 13:11:20 +02:00 Committer: Peter Zijlstra CommitterDate: Mon, 17 Oct 2022 16:41:11 +02:00 x86/alternatives: Provide text_poke_copy_locked() The upcoming call thunk patching must hold text_mutex and needs access to text_poke_copy(), which takes text_mutex. Provide a _locked postfixed variant to expose the inner workings. Signed-off-by: Thomas Gleixner Signed-off-by: Peter Zijlstra (Intel) Link: https://lore.kernel.org/r/20220915111147.159977224@infradead.org --- arch/x86/include/asm/text-patching.h | 1 +- arch/x86/kernel/alternative.c | 37 ++++++++++++++++----------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/arch/x86/include/asm/text-patching.h b/arch/x86/include/asm/text-patching.h index 1cc1552..f4b87f0 100644 --- a/arch/x86/include/asm/text-patching.h +++ b/arch/x86/include/asm/text-patching.h @@ -45,6 +45,7 @@ extern void *text_poke(void *addr, const void *opcode, size_t len); extern void text_poke_sync(void); extern void *text_poke_kgdb(void *addr, const void *opcode, size_t len); extern void *text_poke_copy(void *addr, const void *opcode, size_t len); +extern void *text_poke_copy_locked(void *addr, const void *opcode, size_t len, bool core_ok); extern void *text_poke_set(void *addr, int c, size_t len); extern int poke_int3_handler(struct pt_regs *regs); extern void text_poke_bp(void *addr, const void *opcode, size_t len, const void *emulate); diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 5cadcea..fad3c0e 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -1236,27 +1236,15 @@ void *text_poke_kgdb(void *addr, const void *opcode, size_t len) return __text_poke(text_poke_memcpy, addr, opcode, len); } -/** - * text_poke_copy - Copy instructions into (an unused part of) RX memory - * @addr: address to modify - * @opcode: source of the copy - * @len: length to copy, could be more than 2x PAGE_SIZE - * - * Not safe against concurrent execution; useful for JITs to dump - * new code blocks into unused regions of RX memory. Can be used in - * conjunction with synchronize_rcu_tasks() to wait for existing - * execution to quiesce after having made sure no existing functions - * pointers are live. - */ -void *text_poke_copy(void *addr, const void *opcode, size_t len) +void *text_poke_copy_locked(void *addr, const void *opcode, size_t len, + bool core_ok) { unsigned long start = (unsigned long)addr; size_t patched = 0; - if (WARN_ON_ONCE(core_kernel_text(start))) + if (WARN_ON_ONCE(!core_ok && core_kernel_text(start))) return NULL; - mutex_lock(&text_mutex); while (patched < len) { unsigned long ptr = start + patched; size_t s; @@ -1266,6 +1254,25 @@ void *text_poke_copy(void *addr, const void *opcode, size_t len) __text_poke(text_poke_memcpy, (void *)ptr, opcode + patched, s); patched += s; } + return addr; +} + +/** + * text_poke_copy - Copy instructions into (an unused part of) RX memory + * @addr: address to modify + * @opcode: source of the copy + * @len: length to copy, could be more than 2x PAGE_SIZE + * + * Not safe against concurrent execution; useful for JITs to dump + * new code blocks into unused regions of RX memory. Can be used in + * conjunction with synchronize_rcu_tasks() to wait for existing + * execution to quiesce after having made sure no existing functions + * pointers are live. + */ +void *text_poke_copy(void *addr, const void *opcode, size_t len) +{ + mutex_lock(&text_mutex); + addr = text_poke_copy_locked(addr, opcode, len, false); mutex_unlock(&text_mutex); return addr; }