Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2321124rdb; Mon, 5 Feb 2024 03:00:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IHHJTXgbY0dQfeh+TZfB1kJ2B5x2DOFadmPEOz/yiG1kiYTr+okgVc/vuIeBhIQgzts6SOe X-Received: by 2002:a17:902:e74e:b0:1d9:8cb4:5231 with SMTP id p14-20020a170902e74e00b001d98cb45231mr8732153plf.9.1707130843941; Mon, 05 Feb 2024 03:00:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707130843; cv=pass; d=google.com; s=arc-20160816; b=OL/gEa72xE11QO2JLv/35aPOYohj7lrj7qSM0oaHDBulan8izX92JAqW+nu9eTdSes S+7HIKZSbcejD76fOzuuowwB0ArQy7J4Zh89jfXbP5zFtbZg6GWgNkjmunIwk5v7s9Q4 hqlZ1Hiy101I6lP65jQZSeTZIMV96//KbVU92ctQbzh5/z7bz9BQGuSYldRc+gG3eJ5a 003XMRczS417USFHQyPBShVRBWHt16hO1Rer9ViIZP5UL+t/b0xSzTcfJQOt0WJBkV0E TbWvBv+v92dXXNIgnm2UFF7sXcuTLXy/s5YHiTc3lZgLXmp2wu6vS2+RhQI3KKMtXDK/ JH9g== 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:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=A2RWc1K6I3ebUolVcMP4825Kl6NgwZBnAP753CUj7lk=; fh=tbq+nJoRlYO97QEOY+YMItZJqTn46KtXiPJDXIZvTE8=; b=QavkGcbm0/8ybrjkxXO/5ZkLoz1kkGWaOta6oDgJUmt5dzlgIrMfsj77vS5z4+pWcO 3d7K/Q21GtX0cR73A1VyYQOhDpowgwj6kl/31324EfU6se3EvPwfVX3EpXh08TManLRN 9zJzy9nn1IT1QsWfIrd7gcKxINVBkgk1XY2vzIs9x8bHtPTD7uwOqTHcsoX2WfBZ2JYO mrJEIQTEvP4KkEjzGkAoAmS637/typOsmgYCUhgnpAecPf86VerGMKBV9ImwWhjMs6Sv rvlhbOIAk7drl5zp0WrPxkT819c0KQ2sdgefSLnPi56yumHXqmff7FcR7k3bIudnBUvx XpDw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CxHEHoPS; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-52444-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52444-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCV2aU4OzexNbP4HSvJ7Sa+i3klZACKV86AHQbQ1fV+L0FGTDpLDSK/Dywta/F6fAJeu+xzoq6u1q0u1UF5gk+vJhVyTeIWRdIJD+HB0Yw== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id f11-20020a170903104b00b001d5f009098bsi5994427plc.408.2024.02.05.03.00.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 03:00:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-52444-linux.lists.archive=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=@kernel.org header.s=k20201202 header.b=CxHEHoPS; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-52444-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52444-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 906C0288510 for ; Mon, 5 Feb 2024 11:00:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4444F18027; Mon, 5 Feb 2024 11:00:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CxHEHoPS" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 4EB1D17C9E for ; Mon, 5 Feb 2024 11:00:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707130837; cv=none; b=C7EJJKYDCwi+k1IQYwGaD95vKGwiLPu2Lgsc1yUvIpzxMv1Mx0+lHOseiKjUlYStM6HTJMXZ3f5Amo5GpDlU7s7axV5bYUgaoZO1IvfDgtZ/aNSG/vXezb7DD1pcLhdpzYSqURwKiwwAyUnedNBrfdktm8FIh0yoeMwdoXgJxgM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707130837; c=relaxed/simple; bh=ByVFT90O5UMJW4Jjg2Ye3AcSUePaLfH+R123qMH9hC8=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=K3yQyGMj1B1tGPqMoupB8ORp+vf6G3UNCaSwjyaH+9p6rD2cdSKnLeXYdFh0eWlTeU+fiP4+bUTN6yooj6mQ4YEiDv2xBtE0Fbq0DkbLesIbuFAfdtgM9jSnTQDX2b/ewRAxCez3oVAWlDMCJeNqUGHp3D2yqAnn0mudC3HFlR8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CxHEHoPS; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D802C433C7; Mon, 5 Feb 2024 11:00:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707130836; bh=ByVFT90O5UMJW4Jjg2Ye3AcSUePaLfH+R123qMH9hC8=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=CxHEHoPSkRrVVhA+Y5X5sjwUVrR1UfZnDqq2pkHx19cCKr7h7QbTBw3mSt0xl/2dc pRwOfI5eZZKjMVjR+2JT4tVFMAJcDeTKgXdmWOZjpmciy74rijpoJ22q6yDz/2xwSW sKHMxEE5mZBrPYbr4w/BhtQd633c27o/DEcHr6JvYI/aOBBd/dq2DkqwaoKOFc1Yv2 wAUh/nKlE473vOqqXwcSUZUes3n1Be6QppaXsSh5yoxtj78NUT5mJH2VdNa4G3u8l+ Mj09kWdma+ULl8vskaDQufFf3iLr3pg5fcyn/0MHDyBChtBh8QK/zXztjaNPRwj/i/ cqKtDV2zyhzmA== From: =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= To: Anup Patel , Alexandre Ghiti Cc: Palmer Dabbelt , Paul Walmsley , Anup Patel , linux-kernel@vger.kernel.org, Atish Patra , linux-riscv@lists.infradead.org, Andrew Jones Subject: Re: [PATCH] RISC-V: Don't use IPIs in flush_icache_all() when patching text In-Reply-To: References: <20240205042955.833752-1-apatel@ventanamicro.com> Date: Mon, 05 Feb 2024 12:00:33 +0100 Message-ID: <87y1bzch0u.fsf@all.your.base.are.belong.to.us> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Anup Patel writes: > On Mon, Feb 5, 2024 at 11:52=E2=80=AFAM Alexandre Ghiti w= rote: >> >> 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 po= ssible") >> > Reported-by: Bjorn Topel >> > Closes: https://gist.github.com/bjoto/04a580568378f3b5483af07cd9d22501 >> > Signed-off-by: Anup Patel >> > --- >> > 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 *pa= ge) >> > * RISC-V doesn't have an instruction to flush parts of the instruct= ion 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/hiberna= te.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 th= e 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 =3D 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 *insn= s, size_t len) >> > ret =3D 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 =3D 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=C3=B6rn