Message ID | 20240205042955.833752-1-apatel@ventanamicro.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-52023-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp664615dyb; Sun, 4 Feb 2024 20:30:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IEslXMoNtw2Aul7Z66An2313/VtMaDFApBVYz0BpSzazsEkvFl3Ilm7G3JNiHNZwJJlzGIM X-Received: by 2002:a17:90a:b117:b0:296:1ac3:c573 with SMTP id z23-20020a17090ab11700b002961ac3c573mr8946273pjq.15.1707107426040; Sun, 04 Feb 2024 20:30:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707107426; cv=pass; d=google.com; s=arc-20160816; b=sQcukT1qYGozYqo/i6hVwcUWHZpoV5Th23e9A3f/VXHVuD9TPUAfgSjeRNNfVa1hFZ vDeicOH1NkC1KZOpf+E9s1yE6MJ327VF/hAWq48bR6x6a5dk0xJdudxEIYzSz081hOk3 hVe4THkyvWikV6nVIGnwJL56Hif1DdtKekn05g6eO4odcxn56qeNY2hSdnm4boW6k0M6 6jVwBwSZQUTiS1ecyFmTidNLkB8BpdKc4ZLb0H+tAutCIjCCjDv+uMY/XnR+GUOq1lRf bY+sN+NpBePKkL/Q3Ocv0AE+5wLzOLXOTGUgULSv3EHmJmN2iNReplWe+qF+RQXIFPBT M6lw== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=3r2vA5FIOIQLGifM1d7hYof79l8b2L/VuDazcZIHp8o=; fh=Un7PhSIotXlRRR24jsedfsXyvX1uyOpydTp/Azmmkz0=; b=FgmupuwhDAP/6dX5txKeWi4gAT3C34Bj2hJszPOHeELkWn+sUYVt+cjeU0g7wXq5PM LAbh0OV0yJ9pnxvU1ZwiPhnauT+9Qk17sJw8hsX692vgzb8QVLsISF99cgTf2AkO3vJQ yYidc71ehOR5oxv7fz1YWM8am/wF8SKFn2l6Umt5qUWVAtUpnR9izaiZcR842P05XGVG 3cj5rlbxasXA7y1JZLtvahSIGmSd58q86yeQ4mIkXDvnfANXIwcl5LYeZhDzObt8+6uF eFZ/ejG4A8LNw67A180deygBS8oIIEUEjftmRCk71mLiDWSLAnuHbmOYfYkI+OwTxF9q sX9w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=J2EynJHt; arc=pass (i=1 spf=pass spfdomain=ventanamicro.com dkim=pass dkdomain=ventanamicro.com); spf=pass (google.com: domain of linux-kernel+bounces-52023-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52023-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCWTXOvu8MkeY0hqOeey0NEIQDraEHDn74vWR4BU35INuHGbiVgjn8Bl/qXCzeu1+3ToWvuQTMFmMkFekFx+gJ65igPhaQ== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id n15-20020a17090ade8f00b00295f1aea266si3822166pjv.89.2024.02.04.20.30.25 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Feb 2024 20:30:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-52023-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=J2EynJHt; arc=pass (i=1 spf=pass spfdomain=ventanamicro.com dkim=pass dkdomain=ventanamicro.com); spf=pass (google.com: domain of linux-kernel+bounces-52023-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52023-ouuuleilei=gmail.com@vger.kernel.org" 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id C6B79282C0D for <ouuuleilei@gmail.com>; Mon, 5 Feb 2024 04:30:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F0542B660; Mon, 5 Feb 2024 04:30:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b="J2EynJHt" Received: from mail-il1-f180.google.com (mail-il1-f180.google.com [209.85.166.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 003B69463 for <linux-kernel@vger.kernel.org>; Mon, 5 Feb 2024 04:30:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707107409; cv=none; b=UzBH7t/QYOtz9klU5EgBfM18ObX/Wu6uSqdD09wJa0aEa8BZQHYp5ULT3YT4VdjkTLv/VxWxhNV8afxSYYbu3WL2CjFXq23WBl7s7xghEInn5OYAX8htDU6aBe7VbYV2GGn1jWxYcPrFd9ntbcRxsi5pL1pEPi2hJ2I3znDr4l8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707107409; c=relaxed/simple; bh=G0RsFhkK9T958p1QLSDmEXpLOv5/l46Rv0ysQAhZKUo=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=KQ/POSMhjjDoqDLr/2cmGWtCbqZgvUqALemHBtBuwsUhu/4sJF5S3VAekrvXEeMvNHUdtwvT7Y7HMXI0cfqoI0VR9V4c2N3WOZIPwTmxVi+f0pE1PYT/1BNEUdhTcB7b/seR+oXELrXtFX51sHNO+59xOxe7FzxlKlJ5K2c3o2I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com; spf=pass smtp.mailfrom=ventanamicro.com; dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com header.b=J2EynJHt; arc=none smtp.client-ip=209.85.166.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ventanamicro.com Received: by mail-il1-f180.google.com with SMTP id e9e14a558f8ab-363c869d850so1566185ab.0 for <linux-kernel@vger.kernel.org>; Sun, 04 Feb 2024 20:30:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1707107407; x=1707712207; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=3r2vA5FIOIQLGifM1d7hYof79l8b2L/VuDazcZIHp8o=; b=J2EynJHtmc5fBSbax0r50vP0+CZW0sdSUs4zxlTjpnDtMXlQZrevzcvVJ72PSov2T1 +uL/iYlj4w8kXHX1JLpV+PcTv23Ukk8W/SBg0oeiAo3FYeQbG26wNa4Iimhp1mcVu+4S jWkXKA3cAx9aXkJzJfaZire6x4BjvHVwF9LyABo15QwwbuPhYn471wwKiukJMrwSrX5G yyCmt9r7n6q30ss9BrV31lohSpBAZY88PgKIwc2Q+5QHX31NbnTdrro7/I5YUqysGB54 o3HCw8SEyi0eqKeYJq44NGI4aGOkprrDRkYCgj7FsT+ewNJmM0ogaS+IU1PKu2I0AoMU LXGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707107407; x=1707712207; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3r2vA5FIOIQLGifM1d7hYof79l8b2L/VuDazcZIHp8o=; b=NfSQszDSGdbwknRSlEMST7T7hyFZRML5fbAECE5M9MLgU57BLup1i2E1CStcAapQvE mXtme4myrincqZMZBqsYfAm+os9wJUZcHLaI0tsfC/EJ6vyecqzjTOX+6dKLs5Ofb8Kn t1wSe/BfqmHgeVT9tHqvExPTJWJO592WN6OqHeUZmhTKfev6chqDtS6OR/2yJuiq0c3U KzNNdeCGGSgg06E49n59JAh7WwO8rfxtVBagy6jxzlSpHUwA5bNw6FQMlaFHiWNU6gpP EgJlV4xRn4f8aZ+GgHKeYkUFFCMZiDxtxVuNjCieTnFCQAel2ZEgNos9f4i/0M2h7V1W cvcA== X-Gm-Message-State: AOJu0YzkhxEj0Zqk7lIkCZgnX2JxKWSjta5iVAvSiNvPKWoD+pcHaGoQ So63gzzMR5zp2eO/bcqy9vKP43LdU0u0flkqpGfQIZGL3Piqi5TtmgheFionhEo= X-Received: by 2002:a92:c150:0:b0:363:c63f:6dcc with SMTP id b16-20020a92c150000000b00363c63f6dccmr2960923ilh.29.1707107406907; Sun, 04 Feb 2024 20:30:06 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXzNYHT4cS5fhRnf89OrnogwBxuC4KnPYpo14hZA9l6pDsnlJSHRLanviOZDz4C+Agl2JWXISe7b3KfV9HfScCZa5tCnUDTgDNNpb+e2QsYZeUnExe1sG4GNKNkJ6tozMAmBJW/66GJHbHeMJtsycQz6FiRPzkLqsxbha+aEYG3+AvcFnsMtO5xM0OotGhUEKD6PAlAqcFWV7B5Znhwo1srt4gFzaIcQ13EcbB7LsXJ0mSpz7FuAH5xPS/W4pha3lS0oJ+sAAub8isRwya7Da3+vpvnq85/CK+dO99ygVc7ADtkZzdGDUlrfvOcD4+o4/m/t7kRahyyUgFQON5pH5k= Received: from anup-ubuntu-vm.localdomain ([103.97.165.210]) by smtp.gmail.com with ESMTPSA id m20-20020a056638409400b0046e917416c8sm1889131jam.89.2024.02.04.20.30.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Feb 2024 20:30:06 -0800 (PST) From: Anup Patel <apatel@ventanamicro.com> To: Palmer Dabbelt <palmer@dabbelt.com>, Paul Walmsley <paul.walmsley@sifive.com> Cc: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= <bjorn@kernel.org>, Atish Patra <atishp@atishpatra.org>, Andrew Jones <ajones@ventanamicro.com>, Sunil V L <sunilvl@ventanamicro.com>, Anup Patel <anup@brainfault.org>, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Anup Patel <apatel@ventanamicro.com> Subject: [PATCH] RISC-V: Don't use IPIs in flush_icache_all() when patching text Date: Mon, 5 Feb 2024 09:59:55 +0530 Message-Id: <20240205042955.833752-1-apatel@ventanamicro.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790031876561448532 X-GMAIL-MSGID: 1790031876561448532 |
Series |
RISC-V: Don't use IPIs in flush_icache_all() when patching text
|
|
Commit Message
Anup Patel
Feb. 5, 2024, 4:29 a.m. UTC
If some of the HARTs are parked by stop machine then IPI-based
flushing in flush_icache_all() will hang. This hang is observed
when text patching is invoked by various debug and BPF features.
To avoid this hang, we force use of SBI-based icache flushing
when patching text.
Fixes: 627922843235 ("RISC-V: Use IPIs for remote icache flush when possible")
Reported-by: Bjorn Topel <bjorn@kernel.org>
Closes: https://gist.github.com/bjoto/04a580568378f3b5483af07cd9d22501
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
---
arch/riscv/include/asm/cacheflush.h | 7 ++++---
arch/riscv/kernel/hibernate.c | 2 +-
arch/riscv/kernel/patch.c | 4 ++--
arch/riscv/mm/cacheflush.c | 7 ++++---
4 files changed, 11 insertions(+), 9 deletions(-)
Comments
Hi Anup, On 05/02/2024 05:29, Anup Patel wrote: > If some of the HARTs are parked by stop machine then IPI-based > flushing in flush_icache_all() will hang. This hang is observed > when text patching is invoked by various debug and BPF features. > > To avoid this hang, we force use of SBI-based icache flushing > when patching text. > > Fixes: 627922843235 ("RISC-V: Use IPIs for remote icache flush when possible") > Reported-by: Bjorn Topel <bjorn@kernel.org> > Closes: https://gist.github.com/bjoto/04a580568378f3b5483af07cd9d22501 > Signed-off-by: Anup Patel <apatel@ventanamicro.com> > --- > arch/riscv/include/asm/cacheflush.h | 7 ++++--- > arch/riscv/kernel/hibernate.c | 2 +- > arch/riscv/kernel/patch.c | 4 ++-- > arch/riscv/mm/cacheflush.c | 7 ++++--- > 4 files changed, 11 insertions(+), 9 deletions(-) > > diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h > index a129dac4521d..561e079f34af 100644 > --- a/arch/riscv/include/asm/cacheflush.h > +++ b/arch/riscv/include/asm/cacheflush.h > @@ -32,7 +32,8 @@ static inline void flush_dcache_page(struct page *page) > * RISC-V doesn't have an instruction to flush parts of the instruction cache, > * so instead we just flush the whole thing. > */ > -#define flush_icache_range(start, end) flush_icache_all() > +#define flush_icache_range(start, end) flush_icache_all(true) > +#define flush_icache_patch_range(start, end) flush_icache_all(false) > #define flush_icache_user_page(vma, pg, addr, len) \ > flush_icache_mm(vma->vm_mm, 0) > > @@ -43,12 +44,12 @@ static inline void flush_dcache_page(struct page *page) > > #ifndef CONFIG_SMP > > -#define flush_icache_all() local_flush_icache_all() > +#define flush_icache_all(want_ipi) local_flush_icache_all() > #define flush_icache_mm(mm, local) flush_icache_all() > > #else /* CONFIG_SMP */ > > -void flush_icache_all(void); > +void flush_icache_all(bool want_ipi); > void flush_icache_mm(struct mm_struct *mm, bool local); > > #endif /* CONFIG_SMP */ > diff --git a/arch/riscv/kernel/hibernate.c b/arch/riscv/kernel/hibernate.c > index 671b686c0158..388f10e187ba 100644 > --- a/arch/riscv/kernel/hibernate.c > +++ b/arch/riscv/kernel/hibernate.c > @@ -153,7 +153,7 @@ int swsusp_arch_suspend(void) > } else { > suspend_restore_csrs(hibernate_cpu_context); > flush_tlb_all(); > - flush_icache_all(); > + flush_icache_all(true); > > /* > * Tell the hibernation core that we've just restored the memory. > diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c > index 37e87fdcf6a0..721e144a7847 100644 > --- a/arch/riscv/kernel/patch.c > +++ b/arch/riscv/kernel/patch.c > @@ -182,7 +182,7 @@ int patch_text_set_nosync(void *addr, u8 c, size_t len) > ret = patch_insn_set(tp, c, len); > > if (!ret) > - flush_icache_range((uintptr_t)tp, (uintptr_t)tp + len); > + flush_icache_patch_range((uintptr_t)tp, (uintptr_t)tp + len); > > return ret; > } > @@ -217,7 +217,7 @@ int patch_text_nosync(void *addr, const void *insns, size_t len) > ret = patch_insn_write(tp, insns, len); > > if (!ret) > - flush_icache_range((uintptr_t) tp, (uintptr_t) tp + len); > + flush_icache_patch_range((uintptr_t) tp, (uintptr_t) tp + len); > > return ret; > } > diff --git a/arch/riscv/mm/cacheflush.c b/arch/riscv/mm/cacheflush.c > index 55a34f2020a8..03cd3d4831ef 100644 > --- a/arch/riscv/mm/cacheflush.c > +++ b/arch/riscv/mm/cacheflush.c > @@ -17,11 +17,12 @@ static void ipi_remote_fence_i(void *info) > return local_flush_icache_all(); > } > > -void flush_icache_all(void) > +void flush_icache_all(bool want_ipi) > { > local_flush_icache_all(); > > - if (IS_ENABLED(CONFIG_RISCV_SBI) && !riscv_use_ipi_for_rfence()) > + if (IS_ENABLED(CONFIG_RISCV_SBI) && > + (!want_ipi || !riscv_use_ipi_for_rfence())) > sbi_remote_fence_i(NULL); > else > on_each_cpu(ipi_remote_fence_i, NULL, 1); > @@ -87,7 +88,7 @@ void flush_icache_pte(pte_t pte) > struct folio *folio = page_folio(pte_page(pte)); > > if (!test_bit(PG_dcache_clean, &folio->flags)) { > - flush_icache_all(); > + flush_icache_all(true); > set_bit(PG_dcache_clean, &folio->flags); > } > } Since patch_text_cb() is run on all cpus, couldn't we completely avoid any remote icache flush by slightly changing patch_text_cb() instead as follows? diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c index 37e87fdcf6a0..075c376ed528 100644 --- a/arch/riscv/kernel/patch.c +++ b/arch/riscv/kernel/patch.c @@ -232,8 +232,8 @@ 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((u32 *)(patch->addr + i * len), + &patch->insns[i], len); } atomic_inc(&patch->cpu_count); } else { @@ -242,6 +242,8 @@ static int patch_text_cb(void *data) smp_mb(); } + local_flush_icache_all(); + return ret; } NOKPROBE_SYMBOL(patch_text_cb);
On Mon, Feb 5, 2024 at 11:52 AM Alexandre Ghiti <alex@ghiti.fr> wrote: > > Hi Anup, > > On 05/02/2024 05:29, Anup Patel wrote: > > If some of the HARTs are parked by stop machine then IPI-based > > flushing in flush_icache_all() will hang. This hang is observed > > when text patching is invoked by various debug and BPF features. > > > > To avoid this hang, we force use of SBI-based icache flushing > > when patching text. > > > > Fixes: 627922843235 ("RISC-V: Use IPIs for remote icache flush when possible") > > Reported-by: Bjorn Topel <bjorn@kernel.org> > > Closes: https://gist.github.com/bjoto/04a580568378f3b5483af07cd9d22501 > > Signed-off-by: Anup Patel <apatel@ventanamicro.com> > > --- > > arch/riscv/include/asm/cacheflush.h | 7 ++++--- > > arch/riscv/kernel/hibernate.c | 2 +- > > arch/riscv/kernel/patch.c | 4 ++-- > > arch/riscv/mm/cacheflush.c | 7 ++++--- > > 4 files changed, 11 insertions(+), 9 deletions(-) > > > > diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h > > index a129dac4521d..561e079f34af 100644 > > --- a/arch/riscv/include/asm/cacheflush.h > > +++ b/arch/riscv/include/asm/cacheflush.h > > @@ -32,7 +32,8 @@ static inline void flush_dcache_page(struct page *page) > > * RISC-V doesn't have an instruction to flush parts of the instruction cache, > > * so instead we just flush the whole thing. > > */ > > -#define flush_icache_range(start, end) flush_icache_all() > > +#define flush_icache_range(start, end) flush_icache_all(true) > > +#define flush_icache_patch_range(start, end) flush_icache_all(false) > > #define flush_icache_user_page(vma, pg, addr, len) \ > > flush_icache_mm(vma->vm_mm, 0) > > > > @@ -43,12 +44,12 @@ static inline void flush_dcache_page(struct page *page) > > > > #ifndef CONFIG_SMP > > > > -#define flush_icache_all() local_flush_icache_all() > > +#define flush_icache_all(want_ipi) local_flush_icache_all() > > #define flush_icache_mm(mm, local) flush_icache_all() > > > > #else /* CONFIG_SMP */ > > > > -void flush_icache_all(void); > > +void flush_icache_all(bool want_ipi); > > void flush_icache_mm(struct mm_struct *mm, bool local); > > > > #endif /* CONFIG_SMP */ > > diff --git a/arch/riscv/kernel/hibernate.c b/arch/riscv/kernel/hibernate.c > > index 671b686c0158..388f10e187ba 100644 > > --- a/arch/riscv/kernel/hibernate.c > > +++ b/arch/riscv/kernel/hibernate.c > > @@ -153,7 +153,7 @@ int swsusp_arch_suspend(void) > > } else { > > suspend_restore_csrs(hibernate_cpu_context); > > flush_tlb_all(); > > - flush_icache_all(); > > + flush_icache_all(true); > > > > /* > > * Tell the hibernation core that we've just restored the memory. > > diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c > > index 37e87fdcf6a0..721e144a7847 100644 > > --- a/arch/riscv/kernel/patch.c > > +++ b/arch/riscv/kernel/patch.c > > @@ -182,7 +182,7 @@ int patch_text_set_nosync(void *addr, u8 c, size_t len) > > ret = patch_insn_set(tp, c, len); > > > > if (!ret) > > - flush_icache_range((uintptr_t)tp, (uintptr_t)tp + len); > > + flush_icache_patch_range((uintptr_t)tp, (uintptr_t)tp + len); > > > > return ret; > > } > > @@ -217,7 +217,7 @@ int patch_text_nosync(void *addr, const void *insns, size_t len) > > ret = patch_insn_write(tp, insns, len); > > > > if (!ret) > > - flush_icache_range((uintptr_t) tp, (uintptr_t) tp + len); > > + flush_icache_patch_range((uintptr_t) tp, (uintptr_t) tp + len); > > > > return ret; > > } > > diff --git a/arch/riscv/mm/cacheflush.c b/arch/riscv/mm/cacheflush.c > > index 55a34f2020a8..03cd3d4831ef 100644 > > --- a/arch/riscv/mm/cacheflush.c > > +++ b/arch/riscv/mm/cacheflush.c > > @@ -17,11 +17,12 @@ static void ipi_remote_fence_i(void *info) > > return local_flush_icache_all(); > > } > > > > -void flush_icache_all(void) > > +void flush_icache_all(bool want_ipi) > > { > > local_flush_icache_all(); > > > > - if (IS_ENABLED(CONFIG_RISCV_SBI) && !riscv_use_ipi_for_rfence()) > > + if (IS_ENABLED(CONFIG_RISCV_SBI) && > > + (!want_ipi || !riscv_use_ipi_for_rfence())) > > sbi_remote_fence_i(NULL); > > else > > on_each_cpu(ipi_remote_fence_i, NULL, 1); > > @@ -87,7 +88,7 @@ void flush_icache_pte(pte_t pte) > > struct folio *folio = page_folio(pte_page(pte)); > > > > if (!test_bit(PG_dcache_clean, &folio->flags)) { > > - flush_icache_all(); > > + flush_icache_all(true); > > set_bit(PG_dcache_clean, &folio->flags); > > } > > } > > > Since patch_text_cb() is run on all cpus, couldn't we completely avoid > any remote icache flush by slightly changing patch_text_cb() instead as > follows? Unfortunately patch_text_cb() is not the only user of patch_text_nosync since patch_text_nosync() and patch_text_set_nosync() are called directly from other places as well. We have to update all users of patch_text_nosync() and patch_text_set_nosync() to move to local icache flushes which is a much bigger change. Regards, Anup > > diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c > index 37e87fdcf6a0..075c376ed528 100644 > --- a/arch/riscv/kernel/patch.c > +++ b/arch/riscv/kernel/patch.c > @@ -232,8 +232,8 @@ 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((u32 *)(patch->addr + i * > len), > + &patch->insns[i], len); > } > atomic_inc(&patch->cpu_count); > } else { > @@ -242,6 +242,8 @@ static int patch_text_cb(void *data) > smp_mb(); > } > > + local_flush_icache_all(); > + > return ret; > } > NOKPROBE_SYMBOL(patch_text_cb); > > >
Anup Patel <apatel@ventanamicro.com> writes: > On Mon, Feb 5, 2024 at 11:52 AM Alexandre Ghiti <alex@ghiti.fr> wrote: >> >> Hi Anup, >> >> On 05/02/2024 05:29, Anup Patel wrote: >> > If some of the HARTs are parked by stop machine then IPI-based >> > flushing in flush_icache_all() will hang. This hang is observed >> > when text patching is invoked by various debug and BPF features. >> > >> > To avoid this hang, we force use of SBI-based icache flushing >> > when patching text. >> > >> > Fixes: 627922843235 ("RISC-V: Use IPIs for remote icache flush when possible") >> > Reported-by: Bjorn Topel <bjorn@kernel.org> >> > Closes: https://gist.github.com/bjoto/04a580568378f3b5483af07cd9d22501 >> > Signed-off-by: Anup Patel <apatel@ventanamicro.com> >> > --- >> > arch/riscv/include/asm/cacheflush.h | 7 ++++--- >> > arch/riscv/kernel/hibernate.c | 2 +- >> > arch/riscv/kernel/patch.c | 4 ++-- >> > arch/riscv/mm/cacheflush.c | 7 ++++--- >> > 4 files changed, 11 insertions(+), 9 deletions(-) >> > >> > diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h >> > index a129dac4521d..561e079f34af 100644 >> > --- a/arch/riscv/include/asm/cacheflush.h >> > +++ b/arch/riscv/include/asm/cacheflush.h >> > @@ -32,7 +32,8 @@ static inline void flush_dcache_page(struct page *page) >> > * RISC-V doesn't have an instruction to flush parts of the instruction cache, >> > * so instead we just flush the whole thing. >> > */ >> > -#define flush_icache_range(start, end) flush_icache_all() >> > +#define flush_icache_range(start, end) flush_icache_all(true) >> > +#define flush_icache_patch_range(start, end) flush_icache_all(false) >> > #define flush_icache_user_page(vma, pg, addr, len) \ >> > flush_icache_mm(vma->vm_mm, 0) >> > >> > @@ -43,12 +44,12 @@ static inline void flush_dcache_page(struct page *page) >> > >> > #ifndef CONFIG_SMP >> > >> > -#define flush_icache_all() local_flush_icache_all() >> > +#define flush_icache_all(want_ipi) local_flush_icache_all() >> > #define flush_icache_mm(mm, local) flush_icache_all() >> > >> > #else /* CONFIG_SMP */ >> > >> > -void flush_icache_all(void); >> > +void flush_icache_all(bool want_ipi); >> > void flush_icache_mm(struct mm_struct *mm, bool local); >> > >> > #endif /* CONFIG_SMP */ >> > diff --git a/arch/riscv/kernel/hibernate.c b/arch/riscv/kernel/hibernate.c >> > index 671b686c0158..388f10e187ba 100644 >> > --- a/arch/riscv/kernel/hibernate.c >> > +++ b/arch/riscv/kernel/hibernate.c >> > @@ -153,7 +153,7 @@ int swsusp_arch_suspend(void) >> > } else { >> > suspend_restore_csrs(hibernate_cpu_context); >> > flush_tlb_all(); >> > - flush_icache_all(); >> > + flush_icache_all(true); >> > >> > /* >> > * Tell the hibernation core that we've just restored the memory. >> > diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c >> > index 37e87fdcf6a0..721e144a7847 100644 >> > --- a/arch/riscv/kernel/patch.c >> > +++ b/arch/riscv/kernel/patch.c >> > @@ -182,7 +182,7 @@ int patch_text_set_nosync(void *addr, u8 c, size_t len) >> > ret = patch_insn_set(tp, c, len); >> > >> > if (!ret) >> > - flush_icache_range((uintptr_t)tp, (uintptr_t)tp + len); >> > + flush_icache_patch_range((uintptr_t)tp, (uintptr_t)tp + len); >> > >> > return ret; >> > } >> > @@ -217,7 +217,7 @@ int patch_text_nosync(void *addr, const void *insns, size_t len) >> > ret = patch_insn_write(tp, insns, len); >> > >> > if (!ret) >> > - flush_icache_range((uintptr_t) tp, (uintptr_t) tp + len); >> > + flush_icache_patch_range((uintptr_t) tp, (uintptr_t) tp + len); >> > >> > return ret; >> > } >> > diff --git a/arch/riscv/mm/cacheflush.c b/arch/riscv/mm/cacheflush.c >> > index 55a34f2020a8..03cd3d4831ef 100644 >> > --- a/arch/riscv/mm/cacheflush.c >> > +++ b/arch/riscv/mm/cacheflush.c >> > @@ -17,11 +17,12 @@ static void ipi_remote_fence_i(void *info) >> > return local_flush_icache_all(); >> > } >> > >> > -void flush_icache_all(void) >> > +void flush_icache_all(bool want_ipi) >> > { >> > local_flush_icache_all(); >> > >> > - if (IS_ENABLED(CONFIG_RISCV_SBI) && !riscv_use_ipi_for_rfence()) >> > + if (IS_ENABLED(CONFIG_RISCV_SBI) && >> > + (!want_ipi || !riscv_use_ipi_for_rfence())) >> > sbi_remote_fence_i(NULL); >> > else >> > on_each_cpu(ipi_remote_fence_i, NULL, 1); >> > @@ -87,7 +88,7 @@ void flush_icache_pte(pte_t pte) >> > struct folio *folio = page_folio(pte_page(pte)); >> > >> > if (!test_bit(PG_dcache_clean, &folio->flags)) { >> > - flush_icache_all(); >> > + flush_icache_all(true); >> > set_bit(PG_dcache_clean, &folio->flags); >> > } >> > } >> >> >> Since patch_text_cb() is run on all cpus, couldn't we completely avoid >> any remote icache flush by slightly changing patch_text_cb() instead as >> follows? > > Unfortunately patch_text_cb() is not the only user of patch_text_nosync > since patch_text_nosync() and patch_text_set_nosync() are called directly > from other places as well. Yeah. There is one more stop_machine() text patching user, and that's ftrace. ftrace is using stop_machine() with the last argument set to NULL, so only patching on *any* hart. Continuing on Alex' idea would be to place an IPI flush in ftrace_arch_code_modify_post_process(), unfortately that's too late since we're already moved on from stop_machine(). > We have to update all users of patch_text_nosync() and > patch_text_set_nosync() to move to local icache flushes which > is a much bigger change. Only the ftrace stop_machine() user, right? Alex solution is sufficient for patch_text(). I'm not a super fan of conditionally calling into SBI and passing around boolean context flags as a workaround... :-( Any other alternatives? The obvious fixing text patching not to be completly useless on RISC-V, but that's an even bigger patch... Björn
On Mon, Feb 5, 2024 at 4:30 PM Björn Töpel <bjorn@kernel.org> wrote: > > Anup Patel <apatel@ventanamicro.com> writes: > > > On Mon, Feb 5, 2024 at 11:52 AM Alexandre Ghiti <alex@ghiti.fr> wrote: > >> > >> Hi Anup, > >> > >> On 05/02/2024 05:29, Anup Patel wrote: > >> > If some of the HARTs are parked by stop machine then IPI-based > >> > flushing in flush_icache_all() will hang. This hang is observed > >> > when text patching is invoked by various debug and BPF features. > >> > > >> > To avoid this hang, we force use of SBI-based icache flushing > >> > when patching text. > >> > > >> > Fixes: 627922843235 ("RISC-V: Use IPIs for remote icache flush when possible") > >> > Reported-by: Bjorn Topel <bjorn@kernel.org> > >> > Closes: https://gist.github.com/bjoto/04a580568378f3b5483af07cd9d22501 > >> > Signed-off-by: Anup Patel <apatel@ventanamicro.com> > >> > --- > >> > arch/riscv/include/asm/cacheflush.h | 7 ++++--- > >> > arch/riscv/kernel/hibernate.c | 2 +- > >> > arch/riscv/kernel/patch.c | 4 ++-- > >> > arch/riscv/mm/cacheflush.c | 7 ++++--- > >> > 4 files changed, 11 insertions(+), 9 deletions(-) > >> > > >> > diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h > >> > index a129dac4521d..561e079f34af 100644 > >> > --- a/arch/riscv/include/asm/cacheflush.h > >> > +++ b/arch/riscv/include/asm/cacheflush.h > >> > @@ -32,7 +32,8 @@ static inline void flush_dcache_page(struct page *page) > >> > * RISC-V doesn't have an instruction to flush parts of the instruction cache, > >> > * so instead we just flush the whole thing. > >> > */ > >> > -#define flush_icache_range(start, end) flush_icache_all() > >> > +#define flush_icache_range(start, end) flush_icache_all(true) > >> > +#define flush_icache_patch_range(start, end) flush_icache_all(false) > >> > #define flush_icache_user_page(vma, pg, addr, len) \ > >> > flush_icache_mm(vma->vm_mm, 0) > >> > > >> > @@ -43,12 +44,12 @@ static inline void flush_dcache_page(struct page *page) > >> > > >> > #ifndef CONFIG_SMP > >> > > >> > -#define flush_icache_all() local_flush_icache_all() > >> > +#define flush_icache_all(want_ipi) local_flush_icache_all() > >> > #define flush_icache_mm(mm, local) flush_icache_all() > >> > > >> > #else /* CONFIG_SMP */ > >> > > >> > -void flush_icache_all(void); > >> > +void flush_icache_all(bool want_ipi); > >> > void flush_icache_mm(struct mm_struct *mm, bool local); > >> > > >> > #endif /* CONFIG_SMP */ > >> > diff --git a/arch/riscv/kernel/hibernate.c b/arch/riscv/kernel/hibernate.c > >> > index 671b686c0158..388f10e187ba 100644 > >> > --- a/arch/riscv/kernel/hibernate.c > >> > +++ b/arch/riscv/kernel/hibernate.c > >> > @@ -153,7 +153,7 @@ int swsusp_arch_suspend(void) > >> > } else { > >> > suspend_restore_csrs(hibernate_cpu_context); > >> > flush_tlb_all(); > >> > - flush_icache_all(); > >> > + flush_icache_all(true); > >> > > >> > /* > >> > * Tell the hibernation core that we've just restored the memory. > >> > diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c > >> > index 37e87fdcf6a0..721e144a7847 100644 > >> > --- a/arch/riscv/kernel/patch.c > >> > +++ b/arch/riscv/kernel/patch.c > >> > @@ -182,7 +182,7 @@ int patch_text_set_nosync(void *addr, u8 c, size_t len) > >> > ret = patch_insn_set(tp, c, len); > >> > > >> > if (!ret) > >> > - flush_icache_range((uintptr_t)tp, (uintptr_t)tp + len); > >> > + flush_icache_patch_range((uintptr_t)tp, (uintptr_t)tp + len); > >> > > >> > return ret; > >> > } > >> > @@ -217,7 +217,7 @@ int patch_text_nosync(void *addr, const void *insns, size_t len) > >> > ret = patch_insn_write(tp, insns, len); > >> > > >> > if (!ret) > >> > - flush_icache_range((uintptr_t) tp, (uintptr_t) tp + len); > >> > + flush_icache_patch_range((uintptr_t) tp, (uintptr_t) tp + len); > >> > > >> > return ret; > >> > } > >> > diff --git a/arch/riscv/mm/cacheflush.c b/arch/riscv/mm/cacheflush.c > >> > index 55a34f2020a8..03cd3d4831ef 100644 > >> > --- a/arch/riscv/mm/cacheflush.c > >> > +++ b/arch/riscv/mm/cacheflush.c > >> > @@ -17,11 +17,12 @@ static void ipi_remote_fence_i(void *info) > >> > return local_flush_icache_all(); > >> > } > >> > > >> > -void flush_icache_all(void) > >> > +void flush_icache_all(bool want_ipi) > >> > { > >> > local_flush_icache_all(); > >> > > >> > - if (IS_ENABLED(CONFIG_RISCV_SBI) && !riscv_use_ipi_for_rfence()) > >> > + if (IS_ENABLED(CONFIG_RISCV_SBI) && > >> > + (!want_ipi || !riscv_use_ipi_for_rfence())) > >> > sbi_remote_fence_i(NULL); > >> > else > >> > on_each_cpu(ipi_remote_fence_i, NULL, 1); > >> > @@ -87,7 +88,7 @@ void flush_icache_pte(pte_t pte) > >> > struct folio *folio = page_folio(pte_page(pte)); > >> > > >> > if (!test_bit(PG_dcache_clean, &folio->flags)) { > >> > - flush_icache_all(); > >> > + flush_icache_all(true); > >> > set_bit(PG_dcache_clean, &folio->flags); > >> > } > >> > } > >> > >> > >> Since patch_text_cb() is run on all cpus, couldn't we completely avoid > >> any remote icache flush by slightly changing patch_text_cb() instead as > >> follows? > > > > Unfortunately patch_text_cb() is not the only user of patch_text_nosync > > since patch_text_nosync() and patch_text_set_nosync() are called directly > > from other places as well. > > Yeah. There is one more stop_machine() text patching user, and that's > ftrace. ftrace is using stop_machine() with the last argument set to > NULL, so only patching on *any* hart. Continuing on Alex' idea would be > to place an IPI flush in ftrace_arch_code_modify_post_process(), > unfortately that's too late since we're already moved on from > stop_machine(). > > > We have to update all users of patch_text_nosync() and > > patch_text_set_nosync() to move to local icache flushes which > > is a much bigger change. > > Only the ftrace stop_machine() user, right? Alex solution is sufficient > for patch_text(). I'm not a super fan of conditionally calling into SBI > and passing around boolean context flags as a workaround... :-( Any > other alternatives? I was seeing hang because of patch_text_nosync() getting called from the BPF path in my debug sessions. I am certainly not a fan of the approach taken by this patch but this is the smallest amount of change I could come-up as FIXUP. We should certainly have a separate patch to do this in a proper way. > > The obvious fixing text patching not to be completly useless on RISC-V, > but that's an even bigger patch... > I agree. Regards, Anup
Anup Patel <apatel@ventanamicro.com> writes: > On Mon, Feb 5, 2024 at 4:30 PM Björn Töpel <bjorn@kernel.org> wrote: >> >> Anup Patel <apatel@ventanamicro.com> writes: >> >> > On Mon, Feb 5, 2024 at 11:52 AM Alexandre Ghiti <alex@ghiti.fr> wrote: >> >> >> >> Hi Anup, >> >> >> >> On 05/02/2024 05:29, Anup Patel wrote: >> >> > If some of the HARTs are parked by stop machine then IPI-based >> >> > flushing in flush_icache_all() will hang. This hang is observed >> >> > when text patching is invoked by various debug and BPF features. >> >> > >> >> > To avoid this hang, we force use of SBI-based icache flushing >> >> > when patching text. >> >> > >> >> > Fixes: 627922843235 ("RISC-V: Use IPIs for remote icache flush when possible") >> >> > Reported-by: Bjorn Topel <bjorn@kernel.org> >> >> > Closes: https://gist.github.com/bjoto/04a580568378f3b5483af07cd9d22501 >> >> > Signed-off-by: Anup Patel <apatel@ventanamicro.com> >> >> > --- >> >> > arch/riscv/include/asm/cacheflush.h | 7 ++++--- >> >> > arch/riscv/kernel/hibernate.c | 2 +- >> >> > arch/riscv/kernel/patch.c | 4 ++-- >> >> > arch/riscv/mm/cacheflush.c | 7 ++++--- >> >> > 4 files changed, 11 insertions(+), 9 deletions(-) >> >> > >> >> > diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h >> >> > index a129dac4521d..561e079f34af 100644 >> >> > --- a/arch/riscv/include/asm/cacheflush.h >> >> > +++ b/arch/riscv/include/asm/cacheflush.h >> >> > @@ -32,7 +32,8 @@ static inline void flush_dcache_page(struct page *page) >> >> > * RISC-V doesn't have an instruction to flush parts of the instruction cache, >> >> > * so instead we just flush the whole thing. >> >> > */ >> >> > -#define flush_icache_range(start, end) flush_icache_all() >> >> > +#define flush_icache_range(start, end) flush_icache_all(true) >> >> > +#define flush_icache_patch_range(start, end) flush_icache_all(false) >> >> > #define flush_icache_user_page(vma, pg, addr, len) \ >> >> > flush_icache_mm(vma->vm_mm, 0) >> >> > >> >> > @@ -43,12 +44,12 @@ static inline void flush_dcache_page(struct page *page) >> >> > >> >> > #ifndef CONFIG_SMP >> >> > >> >> > -#define flush_icache_all() local_flush_icache_all() >> >> > +#define flush_icache_all(want_ipi) local_flush_icache_all() >> >> > #define flush_icache_mm(mm, local) flush_icache_all() >> >> > >> >> > #else /* CONFIG_SMP */ >> >> > >> >> > -void flush_icache_all(void); >> >> > +void flush_icache_all(bool want_ipi); >> >> > void flush_icache_mm(struct mm_struct *mm, bool local); >> >> > >> >> > #endif /* CONFIG_SMP */ >> >> > diff --git a/arch/riscv/kernel/hibernate.c b/arch/riscv/kernel/hibernate.c >> >> > index 671b686c0158..388f10e187ba 100644 >> >> > --- a/arch/riscv/kernel/hibernate.c >> >> > +++ b/arch/riscv/kernel/hibernate.c >> >> > @@ -153,7 +153,7 @@ int swsusp_arch_suspend(void) >> >> > } else { >> >> > suspend_restore_csrs(hibernate_cpu_context); >> >> > flush_tlb_all(); >> >> > - flush_icache_all(); >> >> > + flush_icache_all(true); >> >> > >> >> > /* >> >> > * Tell the hibernation core that we've just restored the memory. >> >> > diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c >> >> > index 37e87fdcf6a0..721e144a7847 100644 >> >> > --- a/arch/riscv/kernel/patch.c >> >> > +++ b/arch/riscv/kernel/patch.c >> >> > @@ -182,7 +182,7 @@ int patch_text_set_nosync(void *addr, u8 c, size_t len) >> >> > ret = patch_insn_set(tp, c, len); >> >> > >> >> > if (!ret) >> >> > - flush_icache_range((uintptr_t)tp, (uintptr_t)tp + len); >> >> > + flush_icache_patch_range((uintptr_t)tp, (uintptr_t)tp + len); >> >> > >> >> > return ret; >> >> > } >> >> > @@ -217,7 +217,7 @@ int patch_text_nosync(void *addr, const void *insns, size_t len) >> >> > ret = patch_insn_write(tp, insns, len); >> >> > >> >> > if (!ret) >> >> > - flush_icache_range((uintptr_t) tp, (uintptr_t) tp + len); >> >> > + flush_icache_patch_range((uintptr_t) tp, (uintptr_t) tp + len); >> >> > >> >> > return ret; >> >> > } >> >> > diff --git a/arch/riscv/mm/cacheflush.c b/arch/riscv/mm/cacheflush.c >> >> > index 55a34f2020a8..03cd3d4831ef 100644 >> >> > --- a/arch/riscv/mm/cacheflush.c >> >> > +++ b/arch/riscv/mm/cacheflush.c >> >> > @@ -17,11 +17,12 @@ static void ipi_remote_fence_i(void *info) >> >> > return local_flush_icache_all(); >> >> > } >> >> > >> >> > -void flush_icache_all(void) >> >> > +void flush_icache_all(bool want_ipi) >> >> > { >> >> > local_flush_icache_all(); >> >> > >> >> > - if (IS_ENABLED(CONFIG_RISCV_SBI) && !riscv_use_ipi_for_rfence()) >> >> > + if (IS_ENABLED(CONFIG_RISCV_SBI) && >> >> > + (!want_ipi || !riscv_use_ipi_for_rfence())) >> >> > sbi_remote_fence_i(NULL); >> >> > else >> >> > on_each_cpu(ipi_remote_fence_i, NULL, 1); >> >> > @@ -87,7 +88,7 @@ void flush_icache_pte(pte_t pte) >> >> > struct folio *folio = page_folio(pte_page(pte)); >> >> > >> >> > if (!test_bit(PG_dcache_clean, &folio->flags)) { >> >> > - flush_icache_all(); >> >> > + flush_icache_all(true); >> >> > set_bit(PG_dcache_clean, &folio->flags); >> >> > } >> >> > } >> >> >> >> >> >> Since patch_text_cb() is run on all cpus, couldn't we completely avoid >> >> any remote icache flush by slightly changing patch_text_cb() instead as >> >> follows? >> > >> > Unfortunately patch_text_cb() is not the only user of patch_text_nosync >> > since patch_text_nosync() and patch_text_set_nosync() are called directly >> > from other places as well. >> >> Yeah. There is one more stop_machine() text patching user, and that's >> ftrace. ftrace is using stop_machine() with the last argument set to >> NULL, so only patching on *any* hart. Continuing on Alex' idea would be >> to place an IPI flush in ftrace_arch_code_modify_post_process(), >> unfortately that's too late since we're already moved on from >> stop_machine(). >> >> > We have to update all users of patch_text_nosync() and >> > patch_text_set_nosync() to move to local icache flushes which >> > is a much bigger change. >> >> Only the ftrace stop_machine() user, right? Alex solution is sufficient >> for patch_text(). I'm not a super fan of conditionally calling into SBI >> and passing around boolean context flags as a workaround... :-( Any >> other alternatives? > > I was seeing hang because of patch_text_nosync() getting called from > the BPF path in my debug sessions. Yeah, and this is ftrace's stop_machine() path. All ftrace() paths, and all kprobe paths (patch_text) will have this hang with the IMSIC series unless the fixup is done. :-( > I am certainly not a fan of the approach taken by this patch but this is > the smallest amount of change I could come-up as FIXUP. We should > certainly have a separate patch to do this in a proper way. Yup (and thanks for working on it BTW!). Hmm, making it possible for Charlie's work [1] to attach to cpu_stopper? But maybe that's more work... Unless anyone can comeup with a cleaner (non-SBI), working solution, I'd say go for this one... Björn
Björn Töpel <bjorn@kernel.org> writes: > Yup (and thanks for working on it BTW!). Hmm, making it possible for > Charlie's work [1] to attach to cpu_stopper? But maybe that's more > work... Trigger finger... [1] https://lore.kernel.org/linux-riscv/20240124-fencei-v10-1-a25971f4301d@rivosinc.com/
On 05/02/2024 12:00, Björn Töpel wrote: > Anup Patel <apatel@ventanamicro.com> writes: > >> On Mon, Feb 5, 2024 at 11:52 AM Alexandre Ghiti <alex@ghiti.fr> wrote: >>> Hi Anup, >>> >>> On 05/02/2024 05:29, Anup Patel wrote: >>>> If some of the HARTs are parked by stop machine then IPI-based >>>> flushing in flush_icache_all() will hang. This hang is observed >>>> when text patching is invoked by various debug and BPF features. >>>> >>>> To avoid this hang, we force use of SBI-based icache flushing >>>> when patching text. >>>> >>>> Fixes: 627922843235 ("RISC-V: Use IPIs for remote icache flush when possible") >>>> Reported-by: Bjorn Topel <bjorn@kernel.org> >>>> Closes: https://gist.github.com/bjoto/04a580568378f3b5483af07cd9d22501 >>>> Signed-off-by: Anup Patel <apatel@ventanamicro.com> >>>> --- >>>> arch/riscv/include/asm/cacheflush.h | 7 ++++--- >>>> arch/riscv/kernel/hibernate.c | 2 +- >>>> arch/riscv/kernel/patch.c | 4 ++-- >>>> arch/riscv/mm/cacheflush.c | 7 ++++--- >>>> 4 files changed, 11 insertions(+), 9 deletions(-) >>>> >>>> diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h >>>> index a129dac4521d..561e079f34af 100644 >>>> --- a/arch/riscv/include/asm/cacheflush.h >>>> +++ b/arch/riscv/include/asm/cacheflush.h >>>> @@ -32,7 +32,8 @@ static inline void flush_dcache_page(struct page *page) >>>> * RISC-V doesn't have an instruction to flush parts of the instruction cache, >>>> * so instead we just flush the whole thing. >>>> */ >>>> -#define flush_icache_range(start, end) flush_icache_all() >>>> +#define flush_icache_range(start, end) flush_icache_all(true) >>>> +#define flush_icache_patch_range(start, end) flush_icache_all(false) >>>> #define flush_icache_user_page(vma, pg, addr, len) \ >>>> flush_icache_mm(vma->vm_mm, 0) >>>> >>>> @@ -43,12 +44,12 @@ static inline void flush_dcache_page(struct page *page) >>>> >>>> #ifndef CONFIG_SMP >>>> >>>> -#define flush_icache_all() local_flush_icache_all() >>>> +#define flush_icache_all(want_ipi) local_flush_icache_all() >>>> #define flush_icache_mm(mm, local) flush_icache_all() >>>> >>>> #else /* CONFIG_SMP */ >>>> >>>> -void flush_icache_all(void); >>>> +void flush_icache_all(bool want_ipi); >>>> void flush_icache_mm(struct mm_struct *mm, bool local); >>>> >>>> #endif /* CONFIG_SMP */ >>>> diff --git a/arch/riscv/kernel/hibernate.c b/arch/riscv/kernel/hibernate.c >>>> index 671b686c0158..388f10e187ba 100644 >>>> --- a/arch/riscv/kernel/hibernate.c >>>> +++ b/arch/riscv/kernel/hibernate.c >>>> @@ -153,7 +153,7 @@ int swsusp_arch_suspend(void) >>>> } else { >>>> suspend_restore_csrs(hibernate_cpu_context); >>>> flush_tlb_all(); >>>> - flush_icache_all(); >>>> + flush_icache_all(true); >>>> >>>> /* >>>> * Tell the hibernation core that we've just restored the memory. >>>> diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c >>>> index 37e87fdcf6a0..721e144a7847 100644 >>>> --- a/arch/riscv/kernel/patch.c >>>> +++ b/arch/riscv/kernel/patch.c >>>> @@ -182,7 +182,7 @@ int patch_text_set_nosync(void *addr, u8 c, size_t len) >>>> ret = patch_insn_set(tp, c, len); >>>> >>>> if (!ret) >>>> - flush_icache_range((uintptr_t)tp, (uintptr_t)tp + len); >>>> + flush_icache_patch_range((uintptr_t)tp, (uintptr_t)tp + len); >>>> >>>> return ret; >>>> } >>>> @@ -217,7 +217,7 @@ int patch_text_nosync(void *addr, const void *insns, size_t len) >>>> ret = patch_insn_write(tp, insns, len); >>>> >>>> if (!ret) >>>> - flush_icache_range((uintptr_t) tp, (uintptr_t) tp + len); >>>> + flush_icache_patch_range((uintptr_t) tp, (uintptr_t) tp + len); >>>> >>>> return ret; >>>> } >>>> diff --git a/arch/riscv/mm/cacheflush.c b/arch/riscv/mm/cacheflush.c >>>> index 55a34f2020a8..03cd3d4831ef 100644 >>>> --- a/arch/riscv/mm/cacheflush.c >>>> +++ b/arch/riscv/mm/cacheflush.c >>>> @@ -17,11 +17,12 @@ static void ipi_remote_fence_i(void *info) >>>> return local_flush_icache_all(); >>>> } >>>> >>>> -void flush_icache_all(void) >>>> +void flush_icache_all(bool want_ipi) >>>> { >>>> local_flush_icache_all(); >>>> >>>> - if (IS_ENABLED(CONFIG_RISCV_SBI) && !riscv_use_ipi_for_rfence()) >>>> + if (IS_ENABLED(CONFIG_RISCV_SBI) && >>>> + (!want_ipi || !riscv_use_ipi_for_rfence())) >>>> sbi_remote_fence_i(NULL); >>>> else >>>> on_each_cpu(ipi_remote_fence_i, NULL, 1); >>>> @@ -87,7 +88,7 @@ void flush_icache_pte(pte_t pte) >>>> struct folio *folio = page_folio(pte_page(pte)); >>>> >>>> if (!test_bit(PG_dcache_clean, &folio->flags)) { >>>> - flush_icache_all(); >>>> + flush_icache_all(true); >>>> set_bit(PG_dcache_clean, &folio->flags); >>>> } >>>> } >>> >>> Since patch_text_cb() is run on all cpus, couldn't we completely avoid >>> any remote icache flush by slightly changing patch_text_cb() instead as >>> follows? >> Unfortunately patch_text_cb() is not the only user of patch_text_nosync >> since patch_text_nosync() and patch_text_set_nosync() are called directly >> from other places as well. > Yeah. There is one more stop_machine() text patching user, and that's > ftrace. ftrace is using stop_machine() with the last argument set to > NULL, so only patching on *any* hart. Continuing on Alex' idea would be > to place an IPI flush in ftrace_arch_code_modify_post_process(), > unfortately that's too late since we're already moved on from > stop_machine(). After discussion with Bjorn, we think the solution would be to reimplement arch_ftrace_update_code() with stop_machine(..., cpu_online_mask) and use the same barrier as the one in patch_text_cb() (csky does just that https://elixir.bootlin.com/linux/latest/source/arch/csky/kernel/ftrace.c#L224). And then we can apply the same solution as I first proposed: no more remote icache flushes, only local ones. What do you think Anup? I can come up with this patch if you want. > >> We have to update all users of patch_text_nosync() and >> patch_text_set_nosync() to move to local icache flushes which >> is a much bigger change. > Only the ftrace stop_machine() user, right? Alex solution is sufficient > for patch_text(). I'm not a super fan of conditionally calling into SBI > and passing around boolean context flags as a workaround... :-( Any > other alternatives? > > The obvious fixing text patching not to be completly useless on RISC-V, > but that's an even bigger patch... > > > Björn > > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv
On Mon, Feb 5, 2024 at 7:38 PM Alexandre Ghiti <alex@ghiti.fr> wrote: > > > On 05/02/2024 12:00, Björn Töpel wrote: > > Anup Patel <apatel@ventanamicro.com> writes: > > > >> On Mon, Feb 5, 2024 at 11:52 AM Alexandre Ghiti <alex@ghiti.fr> wrote: > >>> Hi Anup, > >>> > >>> On 05/02/2024 05:29, Anup Patel wrote: > >>>> If some of the HARTs are parked by stop machine then IPI-based > >>>> flushing in flush_icache_all() will hang. This hang is observed > >>>> when text patching is invoked by various debug and BPF features. > >>>> > >>>> To avoid this hang, we force use of SBI-based icache flushing > >>>> when patching text. > >>>> > >>>> Fixes: 627922843235 ("RISC-V: Use IPIs for remote icache flush when possible") > >>>> Reported-by: Bjorn Topel <bjorn@kernel.org> > >>>> Closes: https://gist.github.com/bjoto/04a580568378f3b5483af07cd9d22501 > >>>> Signed-off-by: Anup Patel <apatel@ventanamicro.com> > >>>> --- > >>>> arch/riscv/include/asm/cacheflush.h | 7 ++++--- > >>>> arch/riscv/kernel/hibernate.c | 2 +- > >>>> arch/riscv/kernel/patch.c | 4 ++-- > >>>> arch/riscv/mm/cacheflush.c | 7 ++++--- > >>>> 4 files changed, 11 insertions(+), 9 deletions(-) > >>>> > >>>> diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h > >>>> index a129dac4521d..561e079f34af 100644 > >>>> --- a/arch/riscv/include/asm/cacheflush.h > >>>> +++ b/arch/riscv/include/asm/cacheflush.h > >>>> @@ -32,7 +32,8 @@ static inline void flush_dcache_page(struct page *page) > >>>> * RISC-V doesn't have an instruction to flush parts of the instruction cache, > >>>> * so instead we just flush the whole thing. > >>>> */ > >>>> -#define flush_icache_range(start, end) flush_icache_all() > >>>> +#define flush_icache_range(start, end) flush_icache_all(true) > >>>> +#define flush_icache_patch_range(start, end) flush_icache_all(false) > >>>> #define flush_icache_user_page(vma, pg, addr, len) \ > >>>> flush_icache_mm(vma->vm_mm, 0) > >>>> > >>>> @@ -43,12 +44,12 @@ static inline void flush_dcache_page(struct page *page) > >>>> > >>>> #ifndef CONFIG_SMP > >>>> > >>>> -#define flush_icache_all() local_flush_icache_all() > >>>> +#define flush_icache_all(want_ipi) local_flush_icache_all() > >>>> #define flush_icache_mm(mm, local) flush_icache_all() > >>>> > >>>> #else /* CONFIG_SMP */ > >>>> > >>>> -void flush_icache_all(void); > >>>> +void flush_icache_all(bool want_ipi); > >>>> void flush_icache_mm(struct mm_struct *mm, bool local); > >>>> > >>>> #endif /* CONFIG_SMP */ > >>>> diff --git a/arch/riscv/kernel/hibernate.c b/arch/riscv/kernel/hibernate.c > >>>> index 671b686c0158..388f10e187ba 100644 > >>>> --- a/arch/riscv/kernel/hibernate.c > >>>> +++ b/arch/riscv/kernel/hibernate.c > >>>> @@ -153,7 +153,7 @@ int swsusp_arch_suspend(void) > >>>> } else { > >>>> suspend_restore_csrs(hibernate_cpu_context); > >>>> flush_tlb_all(); > >>>> - flush_icache_all(); > >>>> + flush_icache_all(true); > >>>> > >>>> /* > >>>> * Tell the hibernation core that we've just restored the memory. > >>>> diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c > >>>> index 37e87fdcf6a0..721e144a7847 100644 > >>>> --- a/arch/riscv/kernel/patch.c > >>>> +++ b/arch/riscv/kernel/patch.c > >>>> @@ -182,7 +182,7 @@ int patch_text_set_nosync(void *addr, u8 c, size_t len) > >>>> ret = patch_insn_set(tp, c, len); > >>>> > >>>> if (!ret) > >>>> - flush_icache_range((uintptr_t)tp, (uintptr_t)tp + len); > >>>> + flush_icache_patch_range((uintptr_t)tp, (uintptr_t)tp + len); > >>>> > >>>> return ret; > >>>> } > >>>> @@ -217,7 +217,7 @@ int patch_text_nosync(void *addr, const void *insns, size_t len) > >>>> ret = patch_insn_write(tp, insns, len); > >>>> > >>>> if (!ret) > >>>> - flush_icache_range((uintptr_t) tp, (uintptr_t) tp + len); > >>>> + flush_icache_patch_range((uintptr_t) tp, (uintptr_t) tp + len); > >>>> > >>>> return ret; > >>>> } > >>>> diff --git a/arch/riscv/mm/cacheflush.c b/arch/riscv/mm/cacheflush.c > >>>> index 55a34f2020a8..03cd3d4831ef 100644 > >>>> --- a/arch/riscv/mm/cacheflush.c > >>>> +++ b/arch/riscv/mm/cacheflush.c > >>>> @@ -17,11 +17,12 @@ static void ipi_remote_fence_i(void *info) > >>>> return local_flush_icache_all(); > >>>> } > >>>> > >>>> -void flush_icache_all(void) > >>>> +void flush_icache_all(bool want_ipi) > >>>> { > >>>> local_flush_icache_all(); > >>>> > >>>> - if (IS_ENABLED(CONFIG_RISCV_SBI) && !riscv_use_ipi_for_rfence()) > >>>> + if (IS_ENABLED(CONFIG_RISCV_SBI) && > >>>> + (!want_ipi || !riscv_use_ipi_for_rfence())) > >>>> sbi_remote_fence_i(NULL); > >>>> else > >>>> on_each_cpu(ipi_remote_fence_i, NULL, 1); > >>>> @@ -87,7 +88,7 @@ void flush_icache_pte(pte_t pte) > >>>> struct folio *folio = page_folio(pte_page(pte)); > >>>> > >>>> if (!test_bit(PG_dcache_clean, &folio->flags)) { > >>>> - flush_icache_all(); > >>>> + flush_icache_all(true); > >>>> set_bit(PG_dcache_clean, &folio->flags); > >>>> } > >>>> } > >>> > >>> Since patch_text_cb() is run on all cpus, couldn't we completely avoid > >>> any remote icache flush by slightly changing patch_text_cb() instead as > >>> follows? > >> Unfortunately patch_text_cb() is not the only user of patch_text_nosync > >> since patch_text_nosync() and patch_text_set_nosync() are called directly > >> from other places as well. > > Yeah. There is one more stop_machine() text patching user, and that's > > ftrace. ftrace is using stop_machine() with the last argument set to > > NULL, so only patching on *any* hart. Continuing on Alex' idea would be > > to place an IPI flush in ftrace_arch_code_modify_post_process(), > > unfortately that's too late since we're already moved on from > > stop_machine(). > > > After discussion with Bjorn, we think the solution would be to > reimplement arch_ftrace_update_code() with stop_machine(..., > cpu_online_mask) and use the same barrier as the one in patch_text_cb() > (csky does just that > https://elixir.bootlin.com/linux/latest/source/arch/csky/kernel/ftrace.c#L224). > And then we can apply the same solution as I first proposed: no more > remote icache flushes, only local ones. > > What do you think Anup? I can come up with this patch if you want. Sounds good to me. You will need a special .config from Bjorn to test your patch. > > > > > >> We have to update all users of patch_text_nosync() and > >> patch_text_set_nosync() to move to local icache flushes which > >> is a much bigger change. > > Only the ftrace stop_machine() user, right? Alex solution is sufficient > > for patch_text(). I'm not a super fan of conditionally calling into SBI > > and passing around boolean context flags as a workaround... :-( Any > > other alternatives? > > > > The obvious fixing text patching not to be completly useless on RISC-V, > > but that's an even bigger patch... > > > > > > Björn > > > > _______________________________________________ > > linux-riscv mailing list > > linux-riscv@lists.infradead.org > > http://lists.infradead.org/mailman/listinfo/linux-riscv Regards, Anup
Anup Patel <anup@brainfault.org> writes: > On Mon, Feb 5, 2024 at 7:38 PM Alexandre Ghiti <alex@ghiti.fr> wrote: >> >> >> On 05/02/2024 12:00, Björn Töpel wrote: >> > Anup Patel <apatel@ventanamicro.com> writes: >> > >> >> On Mon, Feb 5, 2024 at 11:52 AM Alexandre Ghiti <alex@ghiti.fr> wrote: >> >>> Hi Anup, >> >>> >> >>> On 05/02/2024 05:29, Anup Patel wrote: >> >>>> If some of the HARTs are parked by stop machine then IPI-based >> >>>> flushing in flush_icache_all() will hang. This hang is observed >> >>>> when text patching is invoked by various debug and BPF features. >> >>>> >> >>>> To avoid this hang, we force use of SBI-based icache flushing >> >>>> when patching text. >> >>>> >> >>>> Fixes: 627922843235 ("RISC-V: Use IPIs for remote icache flush when possible") >> >>>> Reported-by: Bjorn Topel <bjorn@kernel.org> >> >>>> Closes: https://gist.github.com/bjoto/04a580568378f3b5483af07cd9d22501 >> >>>> Signed-off-by: Anup Patel <apatel@ventanamicro.com> >> >>>> --- >> >>>> arch/riscv/include/asm/cacheflush.h | 7 ++++--- >> >>>> arch/riscv/kernel/hibernate.c | 2 +- >> >>>> arch/riscv/kernel/patch.c | 4 ++-- >> >>>> arch/riscv/mm/cacheflush.c | 7 ++++--- >> >>>> 4 files changed, 11 insertions(+), 9 deletions(-) >> >>>> >> >>>> diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h >> >>>> index a129dac4521d..561e079f34af 100644 >> >>>> --- a/arch/riscv/include/asm/cacheflush.h >> >>>> +++ b/arch/riscv/include/asm/cacheflush.h >> >>>> @@ -32,7 +32,8 @@ static inline void flush_dcache_page(struct page *page) >> >>>> * RISC-V doesn't have an instruction to flush parts of the instruction cache, >> >>>> * so instead we just flush the whole thing. >> >>>> */ >> >>>> -#define flush_icache_range(start, end) flush_icache_all() >> >>>> +#define flush_icache_range(start, end) flush_icache_all(true) >> >>>> +#define flush_icache_patch_range(start, end) flush_icache_all(false) >> >>>> #define flush_icache_user_page(vma, pg, addr, len) \ >> >>>> flush_icache_mm(vma->vm_mm, 0) >> >>>> >> >>>> @@ -43,12 +44,12 @@ static inline void flush_dcache_page(struct page *page) >> >>>> >> >>>> #ifndef CONFIG_SMP >> >>>> >> >>>> -#define flush_icache_all() local_flush_icache_all() >> >>>> +#define flush_icache_all(want_ipi) local_flush_icache_all() >> >>>> #define flush_icache_mm(mm, local) flush_icache_all() >> >>>> >> >>>> #else /* CONFIG_SMP */ >> >>>> >> >>>> -void flush_icache_all(void); >> >>>> +void flush_icache_all(bool want_ipi); >> >>>> void flush_icache_mm(struct mm_struct *mm, bool local); >> >>>> >> >>>> #endif /* CONFIG_SMP */ >> >>>> diff --git a/arch/riscv/kernel/hibernate.c b/arch/riscv/kernel/hibernate.c >> >>>> index 671b686c0158..388f10e187ba 100644 >> >>>> --- a/arch/riscv/kernel/hibernate.c >> >>>> +++ b/arch/riscv/kernel/hibernate.c >> >>>> @@ -153,7 +153,7 @@ int swsusp_arch_suspend(void) >> >>>> } else { >> >>>> suspend_restore_csrs(hibernate_cpu_context); >> >>>> flush_tlb_all(); >> >>>> - flush_icache_all(); >> >>>> + flush_icache_all(true); >> >>>> >> >>>> /* >> >>>> * Tell the hibernation core that we've just restored the memory. >> >>>> diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c >> >>>> index 37e87fdcf6a0..721e144a7847 100644 >> >>>> --- a/arch/riscv/kernel/patch.c >> >>>> +++ b/arch/riscv/kernel/patch.c >> >>>> @@ -182,7 +182,7 @@ int patch_text_set_nosync(void *addr, u8 c, size_t len) >> >>>> ret = patch_insn_set(tp, c, len); >> >>>> >> >>>> if (!ret) >> >>>> - flush_icache_range((uintptr_t)tp, (uintptr_t)tp + len); >> >>>> + flush_icache_patch_range((uintptr_t)tp, (uintptr_t)tp + len); >> >>>> >> >>>> return ret; >> >>>> } >> >>>> @@ -217,7 +217,7 @@ int patch_text_nosync(void *addr, const void *insns, size_t len) >> >>>> ret = patch_insn_write(tp, insns, len); >> >>>> >> >>>> if (!ret) >> >>>> - flush_icache_range((uintptr_t) tp, (uintptr_t) tp + len); >> >>>> + flush_icache_patch_range((uintptr_t) tp, (uintptr_t) tp + len); >> >>>> >> >>>> return ret; >> >>>> } >> >>>> diff --git a/arch/riscv/mm/cacheflush.c b/arch/riscv/mm/cacheflush.c >> >>>> index 55a34f2020a8..03cd3d4831ef 100644 >> >>>> --- a/arch/riscv/mm/cacheflush.c >> >>>> +++ b/arch/riscv/mm/cacheflush.c >> >>>> @@ -17,11 +17,12 @@ static void ipi_remote_fence_i(void *info) >> >>>> return local_flush_icache_all(); >> >>>> } >> >>>> >> >>>> -void flush_icache_all(void) >> >>>> +void flush_icache_all(bool want_ipi) >> >>>> { >> >>>> local_flush_icache_all(); >> >>>> >> >>>> - if (IS_ENABLED(CONFIG_RISCV_SBI) && !riscv_use_ipi_for_rfence()) >> >>>> + if (IS_ENABLED(CONFIG_RISCV_SBI) && >> >>>> + (!want_ipi || !riscv_use_ipi_for_rfence())) >> >>>> sbi_remote_fence_i(NULL); >> >>>> else >> >>>> on_each_cpu(ipi_remote_fence_i, NULL, 1); >> >>>> @@ -87,7 +88,7 @@ void flush_icache_pte(pte_t pte) >> >>>> struct folio *folio = page_folio(pte_page(pte)); >> >>>> >> >>>> if (!test_bit(PG_dcache_clean, &folio->flags)) { >> >>>> - flush_icache_all(); >> >>>> + flush_icache_all(true); >> >>>> set_bit(PG_dcache_clean, &folio->flags); >> >>>> } >> >>>> } >> >>> >> >>> Since patch_text_cb() is run on all cpus, couldn't we completely avoid >> >>> any remote icache flush by slightly changing patch_text_cb() instead as >> >>> follows? >> >> Unfortunately patch_text_cb() is not the only user of patch_text_nosync >> >> since patch_text_nosync() and patch_text_set_nosync() are called directly >> >> from other places as well. >> > Yeah. There is one more stop_machine() text patching user, and that's >> > ftrace. ftrace is using stop_machine() with the last argument set to >> > NULL, so only patching on *any* hart. Continuing on Alex' idea would be >> > to place an IPI flush in ftrace_arch_code_modify_post_process(), >> > unfortately that's too late since we're already moved on from >> > stop_machine(). >> >> >> After discussion with Bjorn, we think the solution would be to >> reimplement arch_ftrace_update_code() with stop_machine(..., >> cpu_online_mask) and use the same barrier as the one in patch_text_cb() >> (csky does just that >> https://elixir.bootlin.com/linux/latest/source/arch/csky/kernel/ftrace.c#L224). >> And then we can apply the same solution as I first proposed: no more >> remote icache flushes, only local ones. >> >> What do you think Anup? I can come up with this patch if you want. > > Sounds good to me. You will need a special .config from Bjorn to test > your patch. I also (apparently ;-)) like this solution! To clarify; "The special config" does ftrace patching in initcall phase, but you can reproduce by exercising kprobe/ftrace with the IMSIC IPI patches. Cheers, Björn
diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h index a129dac4521d..561e079f34af 100644 --- a/arch/riscv/include/asm/cacheflush.h +++ b/arch/riscv/include/asm/cacheflush.h @@ -32,7 +32,8 @@ static inline void flush_dcache_page(struct page *page) * RISC-V doesn't have an instruction to flush parts of the instruction cache, * so instead we just flush the whole thing. */ -#define flush_icache_range(start, end) flush_icache_all() +#define flush_icache_range(start, end) flush_icache_all(true) +#define flush_icache_patch_range(start, end) flush_icache_all(false) #define flush_icache_user_page(vma, pg, addr, len) \ flush_icache_mm(vma->vm_mm, 0) @@ -43,12 +44,12 @@ static inline void flush_dcache_page(struct page *page) #ifndef CONFIG_SMP -#define flush_icache_all() local_flush_icache_all() +#define flush_icache_all(want_ipi) local_flush_icache_all() #define flush_icache_mm(mm, local) flush_icache_all() #else /* CONFIG_SMP */ -void flush_icache_all(void); +void flush_icache_all(bool want_ipi); void flush_icache_mm(struct mm_struct *mm, bool local); #endif /* CONFIG_SMP */ diff --git a/arch/riscv/kernel/hibernate.c b/arch/riscv/kernel/hibernate.c index 671b686c0158..388f10e187ba 100644 --- a/arch/riscv/kernel/hibernate.c +++ b/arch/riscv/kernel/hibernate.c @@ -153,7 +153,7 @@ int swsusp_arch_suspend(void) } else { suspend_restore_csrs(hibernate_cpu_context); flush_tlb_all(); - flush_icache_all(); + flush_icache_all(true); /* * Tell the hibernation core that we've just restored the memory. diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c index 37e87fdcf6a0..721e144a7847 100644 --- a/arch/riscv/kernel/patch.c +++ b/arch/riscv/kernel/patch.c @@ -182,7 +182,7 @@ int patch_text_set_nosync(void *addr, u8 c, size_t len) ret = patch_insn_set(tp, c, len); if (!ret) - flush_icache_range((uintptr_t)tp, (uintptr_t)tp + len); + flush_icache_patch_range((uintptr_t)tp, (uintptr_t)tp + len); return ret; } @@ -217,7 +217,7 @@ int patch_text_nosync(void *addr, const void *insns, size_t len) ret = patch_insn_write(tp, insns, len); if (!ret) - flush_icache_range((uintptr_t) tp, (uintptr_t) tp + len); + flush_icache_patch_range((uintptr_t) tp, (uintptr_t) tp + len); return ret; } diff --git a/arch/riscv/mm/cacheflush.c b/arch/riscv/mm/cacheflush.c index 55a34f2020a8..03cd3d4831ef 100644 --- a/arch/riscv/mm/cacheflush.c +++ b/arch/riscv/mm/cacheflush.c @@ -17,11 +17,12 @@ static void ipi_remote_fence_i(void *info) return local_flush_icache_all(); } -void flush_icache_all(void) +void flush_icache_all(bool want_ipi) { local_flush_icache_all(); - if (IS_ENABLED(CONFIG_RISCV_SBI) && !riscv_use_ipi_for_rfence()) + if (IS_ENABLED(CONFIG_RISCV_SBI) && + (!want_ipi || !riscv_use_ipi_for_rfence())) sbi_remote_fence_i(NULL); else on_each_cpu(ipi_remote_fence_i, NULL, 1); @@ -87,7 +88,7 @@ void flush_icache_pte(pte_t pte) struct folio *folio = page_folio(pte_page(pte)); if (!test_bit(PG_dcache_clean, &folio->flags)) { - flush_icache_all(); + flush_icache_all(true); set_bit(PG_dcache_clean, &folio->flags); } }