From patchwork Thu Feb 29 12:10:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13577050 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3AD9FC54798 for ; Thu, 29 Feb 2024 12:13:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=yh0DnDO7F5e+vfJXdizu29KtPGXr9gTcbvD5U/p4Byk=; b=GNgW2Fi6bgXPAv 6Nfa3FBWdXgp75hX0tMEwYO1Be1uFBv+PfZI3uDD9I1jmDAvxJGIYH6PKoZch1Nynt4/nKfXIhiKc 970+rEFwYlmStBkZ6fMXXmmaWF+gGb9YQrg4V7qNEGP96Fdqpu4w3U1BfOlS5hhAYjOKl2AMyG+Kh 4ZGaUj4Np/t09jrVeov9JOlnNH9S+YIuODzg36/fwD8RH4pRclk3XnjTchAtkKIGwoPqskzZ9xats yxxmK2XkILsaQR1t2ImvfHimlo0F2V1spKnxFzxc7GlmT4tGsxkDrKGD7TSYg+LTqmRscZHR9PFAM MPvD2IzwwXVHITDr1oVA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rffHd-0000000DT3R-1aY8; Thu, 29 Feb 2024 12:13:05 +0000 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rffHa-0000000DT1g-3dNz for linux-riscv@lists.infradead.org; Thu, 29 Feb 2024 12:13:04 +0000 Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-33d0a7f2424so559739f8f.0 for ; Thu, 29 Feb 2024 04:13:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1709208781; x=1709813581; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pXXIgfsi20sfR9R1Bl8KEYowaEbmYUeJCudNzsi7ozU=; b=SUq016wdRhO40H4TPkAxxaplDgxLu5NLzuJi2eqiELlDuEIcly5uuw94p2kFMS1dmJ ID2xuEYH0qVp1icKqchg+naeA2M8B1AQPJca5BaPRkLl9eMzhmruTdJgmDL8w5AVAty3 4SWgpEicR9lC6R+3IyiebjCnSD3QaSGI/t7en309jL3+MWP2CJ4zeD2M/VKSYevQs19T bVnMXoNE+Jvw0+uISY/l496gxqqgu5uFTkF/E9Cu1Cy8F4howEXImxos2N219TwKo/H2 1WQJG5wHQymg8er2OQZpiu+8D41WSCVNnnS5Zla4RMm5dXqWp2gz1Bs9KsUSeFhASHgd 9nxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709208781; x=1709813581; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pXXIgfsi20sfR9R1Bl8KEYowaEbmYUeJCudNzsi7ozU=; b=Xf7Edazjqn25f8wr8YRIIZlwJS+t4DL/y/56zr37OggPwiIluAnpG/piLDhIvpNRUf nmxbS6MP/XBJAvbkjYp3MC2Q6KgE+UveMAhGERq7kewofgtOxFo4xSe9SHlGcjOi2+re hh+1cUBgPxft4UiaO0OZw2fLTLh/KI1bwKv4iHS7b7TfcMyHo1kAnl8ocdI5lmqxoeDL kdy6KCHN0MP36jGtMCJ7YFY95McYUIstZoWJGY3M9+NG41IHROTSvK7XyRWeH7gAEJ7L sal874Me/LUhtVuDxfkofmmHqIEqmE/YC6MUtPa1Bu3kPPQhXIZqKbvlQK9/CDgctPTL mLYg== X-Forwarded-Encrypted: i=1; AJvYcCV0n+sktBfSCJ627np4cz7StkXrUrOmQdg9te8QAS96pDFZbCpN/obl/5z7pYcXJ/c2uNYOLNwlVbzR14XAYrun57u5H7ROxvXrj8JVBrZm X-Gm-Message-State: AOJu0YwcD/H67DrWPHZDICOSvOmul1Pv17PXn9HnqgIIyj9r7vP7IR4R YGaIHJ5ddJLsVKtLEQjFvM3NOpkrgk66iIpli4G1s819nRjFwguj7BJLyBJdSRQ= X-Google-Smtp-Source: AGHT+IFT0YxcYDi/LR7DsMQ2nyJ8fcVSxOS6c8osyWaid2ac8gnJzVJrwYje6jjFdkyZbXRi/WxigQ== X-Received: by 2002:adf:f3c6:0:b0:33b:62c0:6181 with SMTP id g6-20020adff3c6000000b0033b62c06181mr1262715wrp.42.1709208780842; Thu, 29 Feb 2024 04:13:00 -0800 (PST) Received: from alex-rivos.ba.rivosinc.com (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id r3-20020adfca83000000b0033d2ae84fafsm1605970wrh.52.2024.02.29.04.13.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 04:13:00 -0800 (PST) From: Alexandre Ghiti To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Andrea Parri , Samuel Holland , Anup Patel , Steven Rostedt , Masami Hiramatsu , Mark Rutland , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Alexandre Ghiti , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Andrea Parri Subject: [PATCH v3 2/2] riscv: Fix text patching when IPI are used Date: Thu, 29 Feb 2024 13:10:56 +0100 Message-Id: <20240229121056.203419-3-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240229121056.203419-1-alexghiti@rivosinc.com> References: <20240229121056.203419-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240229_041302_939114_C24320CE X-CRM114-Status: GOOD ( 18.17 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org For now, we use stop_machine() to patch the text and when we use IPIs for remote icache flushes (which is emitted in patch_text_nosync()), the system hangs. So instead, make sure every CPU executes the stop_machine() patching function and emit a local icache flush there. Co-developed-by: Björn Töpel Signed-off-by: Björn Töpel Signed-off-by: Alexandre Ghiti Reviewed-by: Andrea Parri --- arch/riscv/include/asm/patch.h | 1 + arch/riscv/kernel/ftrace.c | 44 ++++++++++++++++++++++++++++++---- arch/riscv/kernel/patch.c | 16 +++++++++---- 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/arch/riscv/include/asm/patch.h b/arch/riscv/include/asm/patch.h index e88b52d39eac..9f5d6e14c405 100644 --- a/arch/riscv/include/asm/patch.h +++ b/arch/riscv/include/asm/patch.h @@ -6,6 +6,7 @@ #ifndef _ASM_RISCV_PATCH_H #define _ASM_RISCV_PATCH_H +int patch_insn_write(void *addr, const void *insn, size_t len); int patch_text_nosync(void *addr, const void *insns, size_t len); int patch_text_set_nosync(void *addr, u8 c, size_t len); int patch_text(void *addr, u32 *insns, int ninsns); diff --git a/arch/riscv/kernel/ftrace.c b/arch/riscv/kernel/ftrace.c index f5aa24d9e1c1..4f4987a6d83d 100644 --- a/arch/riscv/kernel/ftrace.c +++ b/arch/riscv/kernel/ftrace.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -75,8 +76,7 @@ static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target, make_call_t0(hook_pos, target, call); /* Replace the auipc-jalr pair at once. Return -EPERM on write error. */ - if (patch_text_nosync - ((void *)hook_pos, enable ? call : nops, MCOUNT_INSN_SIZE)) + if (patch_insn_write((void *)hook_pos, enable ? call : nops, MCOUNT_INSN_SIZE)) return -EPERM; return 0; @@ -88,7 +88,7 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) make_call_t0(rec->ip, addr, call); - if (patch_text_nosync((void *)rec->ip, call, MCOUNT_INSN_SIZE)) + if (patch_insn_write((void *)rec->ip, call, MCOUNT_INSN_SIZE)) return -EPERM; return 0; @@ -99,7 +99,7 @@ int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, { unsigned int nops[2] = {NOP4, NOP4}; - if (patch_text_nosync((void *)rec->ip, nops, MCOUNT_INSN_SIZE)) + if (patch_insn_write((void *)rec->ip, nops, MCOUNT_INSN_SIZE)) return -EPERM; return 0; @@ -134,6 +134,42 @@ int ftrace_update_ftrace_func(ftrace_func_t func) return ret; } + +struct ftrace_modify_param { + int command; + atomic_t cpu_count; +}; + +static int __ftrace_modify_code(void *data) +{ + struct ftrace_modify_param *param = data; + + if (atomic_inc_return(¶m->cpu_count) == num_online_cpus()) { + ftrace_modify_all_code(param->command); + /* + * Make sure the patching store is effective *before* we + * increment the counter which releases all waiting CPUs + * by using the release variant of atomic increment. The + * release pairs with the call to local_flush_icache_all() + * on the waiting CPU. + */ + atomic_inc_return_release(¶m->cpu_count); + } else { + while (atomic_read(¶m->cpu_count) <= num_online_cpus()) + cpu_relax(); + } + + local_flush_icache_all(); + + return 0; +} + +void arch_ftrace_update_code(int command) +{ + struct ftrace_modify_param param = { command, ATOMIC_INIT(0) }; + + stop_machine(__ftrace_modify_code, ¶m, cpu_online_mask); +} #endif #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c index 0b5c16dfe3f4..9a1bce1adf5a 100644 --- a/arch/riscv/kernel/patch.c +++ b/arch/riscv/kernel/patch.c @@ -188,7 +188,7 @@ int patch_text_set_nosync(void *addr, u8 c, size_t len) } NOKPROBE_SYMBOL(patch_text_set_nosync); -static int patch_insn_write(void *addr, const void *insn, size_t len) +int patch_insn_write(void *addr, const void *insn, size_t len) { size_t patched = 0; size_t size; @@ -232,15 +232,23 @@ static int patch_text_cb(void *data) if (atomic_inc_return(&patch->cpu_count) == num_online_cpus()) { for (i = 0; ret == 0 && i < patch->ninsns; i++) { len = GET_INSN_LENGTH(patch->insns[i]); - ret = patch_text_nosync(patch->addr + i * len, - &patch->insns[i], len); + ret = patch_insn_write(patch->addr + i * len, &patch->insns[i], len); } - atomic_inc(&patch->cpu_count); + /* + * Make sure the patching store is effective *before* we + * increment the counter which releases all waiting CPUs + * by using the release variant of atomic increment. The + * release pairs with the call to local_flush_icache_all() + * on the waiting CPU. + */ + atomic_inc_return_release(&patch->cpu_count); } else { while (atomic_read(&patch->cpu_count) <= num_online_cpus()) cpu_relax(); } + local_flush_icache_all(); + return ret; } NOKPROBE_SYMBOL(patch_text_cb);