Received: by 2002:ab2:6f44:0:b0:1fd:c486:4f03 with SMTP id l4csp225996lqq; Thu, 13 Jun 2024 00:49:34 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXQ6efRRv5WGbdJKu+nNbx5o1f2BfzemtqaPDlI7yMF1YV1puH9Nhf8mZvWKxZh8vRnKoz2e+xdCVZ2hzNQTN29kN+lFy90Lxxwrv3arw== X-Google-Smtp-Source: AGHT+IGVuHPjw5eB2rJC2S4eak3te2S0w9AqMJ8M5ndo4BNnC/3bUMpXyDB6vpYWWA4g+npAzJ1y X-Received: by 2002:a1f:c4c2:0:b0:4ed:5c1:6794 with SMTP id 71dfb90a1353d-4ed07bc2d6cmr3517234e0c.12.1718264973901; Thu, 13 Jun 2024 00:49:33 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718264973; cv=pass; d=google.com; s=arc-20160816; b=A1U6WcK0W90GmTA61/tVGJ/ENFEGgIOukQRtGndZDw928KKmwTMm4rDsR7sgBL0ukE zEqTwjalScvQ1q0LxXWxqyq3lfY2n5IJxVwdIsoeV4zwNT8NTnY9gHsTuEWK0NonxFTY YL7tYMdskYAZ7p59uYs+iV5eIonhLnHWvmg8xakjnCxsORM7QxljPyKrz1FOzbD8OqK9 NnGmMiGG6XFgwkIFMqXGV6oAy2ogZ7oyrIHbJOp5I8Xfp4dMJ1n0kmuB0z3CrpbYs4fr dK4Tff822AS1CNeMbsHP1okBv/bTZeqqYf/WAsdWf9ks5qi7Re+AMLi6wpiqv7SUVuB1 vKEA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=oEhojbsUyqLtmEbGicYHMcsx4Afq5q5pIFYyTgw+GM8=; fh=6mq5rlVRvqKiCPR21cv9J7xuj5lRoCVa8alAmb2zj+w=; b=bvyy4pJctjn7YkkoAQgXg8j9+4HBdJah24BNP7hF7Fg8TvAggzOZCE4N9fZCYEpiRH pUOmBqsfFpBOm0uapcP0N0GWKdP9zPkW6g7b/Rwqf5BqNipf76iZPGDVMLydvNApnhfk 3ozr/8m1m5/+02ZKCXqcJjstMC46jJkGLjeyUM+8V1kNU9eb+QzRbtxEU/R/yv/Z9u14 JNDCEk3h5/DwUBm3On0v4FzhGntZTdNIRbC67zEmwmf+Pidjq30GSSI5kYFVgMQzFXOQ hSPEheYzAuEsW4+1VsngtjEdZMJAxPE5DpnQhuX5rq1S3peQU6FKCMG9Ijmcwk5cPFdj bhHg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=HMvLdNuo; arc=pass (i=1 spf=pass spfdomain=microchip.com dkim=pass dkdomain=microchip.com dmarc=pass fromdomain=microchip.com); spf=pass (google.com: domain of linux-kernel+bounces-212746-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212746-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microchip.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id af79cd13be357-798abe6223csi83660585a.648.2024.06.13.00.49.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 00:49:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-212746-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=HMvLdNuo; arc=pass (i=1 spf=pass spfdomain=microchip.com dkim=pass dkdomain=microchip.com dmarc=pass fromdomain=microchip.com); spf=pass (google.com: domain of linux-kernel+bounces-212746-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212746-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microchip.com 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 99A2D1C21E35 for ; Thu, 13 Jun 2024 07:49:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0D53D13C9D0; Thu, 13 Jun 2024 07:49:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=microchip.com header.i=@microchip.com header.b="HMvLdNuo" Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.153.233]) (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 09E5913C8FF; Thu, 13 Jun 2024 07:49:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=68.232.153.233 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718264958; cv=none; b=Q2AbHutii92cnpcd/NtH/vzdiNmPYzjMq9iyxbTpXV9yUH2VNqbg3DGKCC/FnsvT8oyeNTJUJ82ejRSPakmE7iVXuxCWWXd39P14WVTFdRNPQ2lJhXjGq1lR69Z88jrxZUzJr9uaFJYsFoknTHXtAue0UYzecN9JF2DlcngpqnE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718264958; c=relaxed/simple; bh=tOsnQVOFTDPDAjyXqM6o5BX1/ALySEuh1yBpE5U5t04=; h=Date:From:To:CC:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=POrwzKFW3kXvLAy2vACT5dviI84CkJPtVgCsDAxTPqjL1tTk4LW526zQo9jz9A8B8iMeLYL4QLW4Fdtrz2J44oC1Pc9X/R/LpF0K0nW8R64mVNXGoIJMTOfPnD3RVTLX8XCPSaNGCV+73iUD+trdLjT3W2uOPdN6JEZ83g7V/QM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=microchip.com; spf=pass smtp.mailfrom=microchip.com; dkim=pass (2048-bit key) header.d=microchip.com header.i=@microchip.com header.b=HMvLdNuo; arc=none smtp.client-ip=68.232.153.233 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=microchip.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=microchip.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1718264955; x=1749800955; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=tOsnQVOFTDPDAjyXqM6o5BX1/ALySEuh1yBpE5U5t04=; b=HMvLdNuoCW8QchGY2gQAkxqeLGY+6ijhiwXQECNNzQ9dNSYAPU6ZI+2B tsuWMRPLjwamHRPaWi1C73VEhpuYAJFSdL5PLMk9Uj+Ap+WnmAPLE0C2B CdlC6Xd4IYdnIO7u5v6rgXOnuLgT3hVs6dyPltUYu18kC4YPcocFZfxh2 pe2wm2SRGIi4NXPrQQcBDxwlhO+UmkbrCMLTpSJbo5dimubNJgR6M8d+h k3eCjlvuMmXcoeK3jst2qROa0TjIfRWnLHTmvUWvwylb8EUvLlLhCUmZT RUd+8Hu1/hADsZ8wJdlM4NsfTlWdoM3ICnjVuKqZ20aqtg4SSzn2bqqSa g==; X-CSE-ConnectionGUID: HD7DQktUQset5I4Uvw9H8Q== X-CSE-MsgGUID: EH4vLWffT2uZaRrw4Ct1EA== X-IronPort-AV: E=Sophos;i="6.08,234,1712646000"; d="asc'?scan'208";a="29819693" X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa1.microchip.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 13 Jun 2024 00:49:13 -0700 Received: from chn-vm-ex03.mchp-main.com (10.10.85.151) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Thu, 13 Jun 2024 00:48:53 -0700 Received: from wendy (10.10.85.11) by chn-vm-ex03.mchp-main.com (10.10.85.151) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35 via Frontend Transport; Thu, 13 Jun 2024 00:48:51 -0700 Date: Thu, 13 Jun 2024 08:48:34 +0100 From: Conor Dooley To: Alexandre Ghiti CC: Paul Walmsley , Palmer Dabbelt , Albert Ou , Steven Rostedt , Masami Hiramatsu , Mark Rutland , Andrea Parri , =?iso-8859-1?Q?Bj=F6rn_T=F6pel?= , , , Subject: Re: [PATCH] riscv: Fix early ftrace nop patching Message-ID: <20240613-lubricant-breath-061192a9489a@wendy> References: <20240523115134.70380-1-alexghiti@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="ucH24q+vfrE+sERa" Content-Disposition: inline In-Reply-To: <20240523115134.70380-1-alexghiti@rivosinc.com> --ucH24q+vfrE+sERa Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, May 23, 2024 at 01:51:34PM +0200, Alexandre Ghiti wrote: > 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. >=20 > 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). >=20 > This caused crashes on multiple HW as reported by Conor and Bj=F6rn 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. >=20 > Fixes: c97bf629963e ("riscv: Fix text patching when IPI are used") > Signed-off-by: Alexandre Ghiti > --- > arch/riscv/include/asm/cacheflush.h | 6 ++++++ > arch/riscv/kernel/ftrace.c | 3 +++ > 2 files changed, 9 insertions(+) >=20 > 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"); > } > =20 > +static inline void local_flush_icache_range(unsigned long start, > + unsigned long end) > +{ > + local_flush_icache_all(); > +} > + > #define PG_dcache_clean PG_arch_1 > =20 > 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_ft= race *rec) > out =3D ftrace_make_nop(mod, rec, MCOUNT_ADDR); > mutex_unlock(&text_mutex); So, turns out that this patch is not sufficient. I've seen some more crashes, seemingly due to initialising nops on this mutex_unlock(). Palmer suggested moving the if (!mod) ... into the lock, which fixed the problem for me. > =20 > + if (!mod) > + local_flush_icache_range(rec->ip, rec->ip + MCOUNT_INSN_SIZE); > + > return out; > } > =20 > --=20 > 2.39.2 >=20 --ucH24q+vfrE+sERa Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iHUEABYIAB0WIQRh246EGq/8RLhDjO14tDGHoIJi0gUCZmqkUQAKCRB4tDGHoIJi 0m3VAQDMZOg52eVsvZthKmdZBM/Y3SXPrObGxlcvKyFsEVooWQD8C8VClKYq1GTX I/KHs08ahGuI+30Vfq/MEeFlrbU1AgU= =hljr -----END PGP SIGNATURE----- --ucH24q+vfrE+sERa--