From patchwork Thu May 23 11:51:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13671667 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 F2B60C25B79 for ; Thu, 23 May 2024 11:51:48 +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:Message-Id:Date:Subject:To :From:Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=M6McBCff15cyu3zbkWOygrVUzRVo0pZUh0QdXH6qfB4=; b=JUJLyM3mS1oZvt LErt/vLCxO/K7zOWa2fk3M9628OdDZoqUSCwCusCL6mKzfiir3bp8d9FBy33D8Tx9eUy6hPefDWvN 2S73N/g2ZGG87iP5uUBTcWATau7UyTY3Jd+BdDFUGNcQ/5kyPz7aBpVE1yj62wP7fNQtKxB7DR2vo ZfR+CwxRV8RfXzM6EN/1OgqDhkuTFFu4iUrpDM7XuoTGGfdO3Riy2UywxcjcrghpNasExi1W+Cshs E4hTYSAfvVVfbmD0fwm/yyAAiacdYiL0pTcdfZHHqD0JLqmMImUXfQLiS45shd6Ral3yyBgaz/Jex THmnOqfUBKgQfa3H6McQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sA6z0-0000000668I-3D44; Thu, 23 May 2024 11:51:42 +0000 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sA6yx-0000000667U-3jFp for linux-riscv@lists.infradead.org; Thu, 23 May 2024 11:51:41 +0000 Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-354f6ab168aso443330f8f.1 for ; Thu, 23 May 2024 04:51:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1716465096; x=1717069896; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=tomuose927MUcTT3sGm0eYQ4ZHc9XHuBgI6ykSWqNcI=; b=HYYrEt/uEsGfng7PYFNm8HF7ckuJkio4wNxdp5DKWlSwX05lvaIOCp+xvZd3dqvXWl fbqu7l/DeLxOY5slHs/g7cSrm4MFsbIRm5MQRjMLq4YEx01eRfaEo8lp09y6lLBaSp9E 7F3q6UG3fU2a5XB+haydYh9if+PHYmFivA/iNqE7OQu+y3xC3aiJYt7LkR0aefWSyOy9 2dQOeu6NjNdQLSETu6hoFyy1ky+52CzgIctuNllD/uuvLcWXhuKmjAK/7JTOa8K/xJlO O50CYhhJsT6AYFBBy4U3K+vyTqbUtJE7dki/r37lBxqD5Pdfi5+nv7qwnZlbO+tV8L2B rGNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716465096; x=1717069896; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tomuose927MUcTT3sGm0eYQ4ZHc9XHuBgI6ykSWqNcI=; b=AG7Sa0gHvK4rF0i1+cnXF7nJw75WiaeXBnxh9t1y2j7yNvAtD0wsA2LKf1CZev4G5f 4ml5vd0YYQhu2DI/XPhItWabDnQnxRC2sIR+uoOwCFFy+jSHkhvw5Ids/O2h64DC4i8p rvK00P0vp6vKIOp4ndxNpwhmvb9+fJ696IZ3mOyFFTIgH94M/6vH4XLgWck8ELkltGCw qLTdOkiQ9ORoBb1p+gC1/MCJUf3+XzoNKZpywf8D9cArLkSbwXBakYEWM8BLHUDRdDtV N3r8wmSHQqEsNTrjBPyoJDJmSHQKOqtPTFMIcfL3REQr8YUuAy+mkE+V49Ytiprw0GtF Popg== X-Forwarded-Encrypted: i=1; AJvYcCWo12UYpbkoo+e8hBbsE5bW8vmL/HB9Pmo/zumZREb9DvpRMuchxQXJeqfih6nG7TFws9fWIhfsUwrIEduv7x5G1yJlXkd8KMYza1K44Z1w X-Gm-Message-State: AOJu0YzJTd+IAuj3Shqj2luf3rbiy2IabFz4YtA+6j2qNgJuLVM51SL1 EE7bVQj3gwpFora0AFEQq8EH8ky4BSWjz4TDHxMjGHok14C+FYlHzYAx8TsUrH7jaPZspu23XXW R X-Google-Smtp-Source: AGHT+IFe90bcCCk3DsNh1uSEukvSAXaPs6XHlXPOkfTOcX5gFcR6LlB//XRU3ng08XrqXVcAz3+1YQ== X-Received: by 2002:adf:fd04:0:b0:34d:ce05:29ef with SMTP id ffacd0b85a97d-354f75216eamr2192712f8f.17.1716465096610; Thu, 23 May 2024 04:51:36 -0700 (PDT) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-354e03420b4sm3414511f8f.24.2024.05.23.04.51.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 04:51:36 -0700 (PDT) From: Alexandre Ghiti To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Steven Rostedt , Masami Hiramatsu , Mark Rutland , Alexandre Ghiti , Andrea Parri , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Conor Dooley , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH] riscv: Fix early ftrace nop patching Date: Thu, 23 May 2024 13:51:34 +0200 Message-Id: <20240523115134.70380-1-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240523_045140_069751_CD7E23F6 X-CRM114-Status: GOOD ( 11.00 ) 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 Commit c97bf629963e ("riscv: Fix text patching when IPI are used") converted ftrace_make_nop() to use patch_insn_write() which does not emit any icache flush relying entirely on __ftrace_modify_code() to do that. But we missed that ftrace_make_nop() was called very early directly when converting mcount calls into nops (actually on riscv it converts 2B nops emitted by the compiler into 4B nops). This caused crashes on multiple HW as reported by Conor and Björn since the booting core could have half-patched instructions in its icache which would trigger an illegal instruction trap: fix this by emitting a local flush icache when early patching nops. Fixes: c97bf629963e ("riscv: Fix text patching when IPI are used") Signed-off-by: Alexandre Ghiti Reviewed-by: Björn Töpel Tested-by: Björn Töpel Reported-by: Conor Dooley Tested-by: Conor Dooley --- arch/riscv/include/asm/cacheflush.h | 6 ++++++ arch/riscv/kernel/ftrace.c | 3 +++ 2 files changed, 9 insertions(+) diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h index dd8d07146116..ce79c558a4c8 100644 --- a/arch/riscv/include/asm/cacheflush.h +++ b/arch/riscv/include/asm/cacheflush.h @@ -13,6 +13,12 @@ static inline void local_flush_icache_all(void) asm volatile ("fence.i" ::: "memory"); } +static inline void local_flush_icache_range(unsigned long start, + unsigned long end) +{ + local_flush_icache_all(); +} + #define PG_dcache_clean PG_arch_1 static inline void flush_dcache_folio(struct folio *folio) diff --git a/arch/riscv/kernel/ftrace.c b/arch/riscv/kernel/ftrace.c index 4f4987a6d83d..32e7c401dfb4 100644 --- a/arch/riscv/kernel/ftrace.c +++ b/arch/riscv/kernel/ftrace.c @@ -120,6 +120,9 @@ int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec) out = ftrace_make_nop(mod, rec, MCOUNT_ADDR); mutex_unlock(&text_mutex); + if (!mod) + local_flush_icache_range(rec->ip, rec->ip + MCOUNT_INSN_SIZE); + return out; }