Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2345772rdb; Mon, 5 Feb 2024 03:57:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IFPhootCLy7U+oZnQhMdO9APMib2bpDY4cVZXAD3eT+HRkBpimx5W5iQZpZQ5cO6rWvnUd1 X-Received: by 2002:a05:6e02:1307:b0:363:b676:359d with SMTP id g7-20020a056e02130700b00363b676359dmr7844713ilr.18.1707134238620; Mon, 05 Feb 2024 03:57:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707134238; cv=pass; d=google.com; s=arc-20160816; b=JvKuoqbpH6EjCrSaZqu2pPXSSyHrTs0pGxtS8XtRNRarBtVOe3d0Ac64SuIhBj1OZb Vvo66mFMeKfZ2T4Gau4RzcqQjXh5591awR50E+/EoalRzskvPd2R0Rn9ETDdA13ezVSB GVmjY1i3jXQeDR5hjtPL14tRN1iOiuAELORYa0c8WG1S2FNDE4/pXSEP7l6KREUWHsr9 NtS1LtB5NwcpA+fBguBJybAWuYZ2AeQU0EVEWG6LlmK/9AT04tUZZBeTOhDpvrffHHkR WT3j2+O8NqMm4lv6Pn4CcchAlT31fKOa6Ht8WNUglWlO7lFAzyLNBFc292hpqhF5I6O7 RYWA== 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=66aXltmZt08Hf9bEnsTNakwJrPHRha4Y3MZJesRliD4=; fh=b8/mYeP1/lUSS/X4ftI4pjpzojJOPZdt2VrwdIZdNK4=; b=Ue2ngAfQG2DpYfXMy/i6IRF+nZhE3WH6vWx0+qobdEbfb2Y8HMCtKtFrfOmb2GgIok 5TIIA2kYYso5O9K1kiFlB2NIa9sB/TBM9bqZlnh7aBeAIK2GUA7guFGsxntfxSnbOLFA ZE5jiPFJ4Cf9R29qu4qCY/pKJaP9UrkPyFAwHf8cvSMbHXxY9/lxZ3udWB89asZ4aCpU 8wluzgRnDCr6Z6c2ueqyJh0ORxyFJQtRBZE/ibwmQBALQSy47ut4/i54h3A2nCEJM+md KWB6Oj+X9qfVS+i+JjZS3c88YuduMPnPRCqLFOFYDzkFQzgpGDpxQuDO7VP9BoZQWxyN vxoA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=oYSqRifo; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-52498-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52498-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; AJvYcCWpIAxKBYKDhY5Vz1yh0aJL+YKR2uQiM3becFxXoNuUoG7bZABZPMlFMOrx6vIp/uljnvRKAy3q4NSf2f7oWtZWDDN6wvsN01oafJCXhw== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id 21-20020a630015000000b005d9252c9cf5si5990218pga.62.2024.02.05.03.57.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 03:57:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-52498-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=oYSqRifo; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-52498-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52498-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 7483EB27796 for ; Mon, 5 Feb 2024 11:41:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8017618EAB; Mon, 5 Feb 2024 11:41:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="oYSqRifo" 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 7AD1718E1E for ; Mon, 5 Feb 2024 11:41:12 +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=1707133272; cv=none; b=UxsHUaysrQCB6aRgVUbXiFKJDO6uQ5DggUSjJ9iDQAGhygw3+jQ2xt4CQj+iziLXAvUECYu6zX0ls7idbUQ8vIutBRdAFH/lb90zWrDmyrW/en4Y4CKGisghSzE3dE1RaeZ1XqD7yQCrznnqDLXW/H0Xu90pp0f+7DpzEh0mTAQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707133272; c=relaxed/simple; bh=VoW3whsdIu6vZsz8npvZyTC66/rJHpfHtp4NTFQ9DEY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=lStiUhLir2VkIIEAyAxgH4kzkWJzmOYRq5nxLokkCh4A1fCMW0EVYoAB/a7AMT3+Fwq2kuYO2gKKmKRApo8bX2BRWeq1n/Z5DslIlVGZ0UzsSTjsmOD22D4VGrqG6Ql1L2E3D/SAXZNxHU79UiSVveXLx7JV3s5Y9sVhrTFN83w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oYSqRifo; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 982C7C43390; Mon, 5 Feb 2024 11:41:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707133272; bh=VoW3whsdIu6vZsz8npvZyTC66/rJHpfHtp4NTFQ9DEY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=oYSqRifo11ky9u/KqgYB3jd809jSdYf8kepFH+twUrN8V+elsmOdNat2isPHwKU+F fHtk7FWA82SEJ/Vs8LHSJ3CXt0Rdyizc8W3eFEVSWgN18Sv+2/VE+PIqL0cobGkfE2 Bc7lIKSq6u7BDhb4fjyNdvRuSrLJwoBUPXYGUKinAsP7+QbdoBBv5rwZHET5Y5WXgg vr199tagrDnWGWJDk8WSjPdqoN77n7goUFXS1dJud5AxaRmKwXknMs/VXBgm3jdnk2 Axp6nADXphLhMJUZ8S/x+rZ31pJuylpe17hrPyaMTumVakWc29WdbwTW09hWLVHSSO 8B+6mn++4WugQ== From: =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= To: Anup Patel Cc: Alexandre Ghiti , 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> <87y1bzch0u.fsf@all.your.base.are.belong.to.us> Date: Mon, 05 Feb 2024 12:41:08 +0100 Message-ID: <87wmrj9m0b.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 4:30=E2=80=AFPM Bj=C3=B6rn T=C3=B6pel wrote: >> >> Anup Patel writes: >> >> > On Mon, Feb 5, 2024 at 11:52=E2=80=AFAM Alexandre Ghiti 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 >> >> > Closes: https://gist.github.com/bjoto/04a580568378f3b5483af07cd9d22= 501 >> >> > 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/inclu= de/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 instr= uction 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(fals= e) >> >> > #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 pag= e *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/hibe= rnate.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, siz= e_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 *i= nsns, size_t len) >> >> > ret =3D patch_insn_write(tp, insns, len); >> >> > >> >> > if (!ret) >> >> > - flush_icache_range((uintptr_t) tp, (uintptr_t) tp + l= en); >> >> > + 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 direc= tly >> > 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=C3=B6rn