Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2223309rdb; Sun, 4 Feb 2024 22:23:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IGk7Jfllkbevdc1MhBid6VB1rFR4QfxzMOgbqliwrdBN+SFNYaqMHwVkF2iTGxieoY+eGxG X-Received: by 2002:a17:90b:e8f:b0:296:3daf:8245 with SMTP id fv15-20020a17090b0e8f00b002963daf8245mr8367057pjb.19.1707114235296; Sun, 04 Feb 2024 22:23:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707114235; cv=pass; d=google.com; s=arc-20160816; b=0ch0qVSLR615bNLb31Bry64bOP/aIqtxcL1u+QHpbg7Fyey09r9qMVhbrM86GM9xDU oOrDQPPVVOnoLyk+ITBtF5EryCJNM7q0zBmPwFBVQY7ROgx/h3xHwB3mcxDLzHtM6EHG G6MY3UjlIpw8cB/EAA84JNXjQUdL4nv8bHnUB21CtSIh7qruGRrEh0LLUr//1DVLoSUs hddexoO+CL7L7kUTYyX+WPSb1V+2mMmjKNBAP4iTzSo4QTKAJyvxvV5bZMtE/pQdlrsc HRZAz9uPjMmv1ipqs3Q/XqyA7Y6otS7tRjvhik4q88AQCyJTIrPThzTQSrXqzR22ztmI N8dg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=wrtdWWX4XED+bEaclCplZyidewTIV7Y0ufTYa74U0Bw=; fh=rQ++QmWhC8TIXDgX33ebA1S/Olovyly8zc++hhegw/M=; b=Tolu4wMTiUe7/M5Mw7GIx5fhkLA9/ppaaUz0GbAx/2RCZaVUHN4tarhv+8S16da2vP wsSeQGhp6ds37aL06Dkna9WAb25NYbA3axIHz+OYtx+Wt8imkffv696aj0wY1gEvu2RR qLVyexBU4rGxyuoP05IQTms0Em3a3YnY5byGGUmehgHR2yqjGOxHrS6EFFigBdiS9wrN AKY6waT0y01b7SEl0n4b8JSLGLgB/pjxZQ0LL1oSkBIBGjP6gNd0UacIax+Hl6AiOnkw hwAdT0sb+XWGd5FWLuMXJNlw+tVW3OVVUF4if7DQILuLjWC+kkADZHTz0yJg+H+xzE/r qDkg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=ghiti.fr); spf=pass (google.com: domain of linux-kernel+bounces-52074-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52074-linux.lists.archive=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCXahf2r+zE9lYxCZ2G6+B0A+Jh5rTtGmxkENDOBxhf8UJ/YgBNzoGk3j9HV2Cp5Jv7rrAxK5nMvcfupgH0Tjib9XyaC+3LUZV4rOgDDJg== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id kb8-20020a17090ae7c800b002960c2b0da9si4096401pjb.0.2024.02.04.22.23.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Feb 2024 22:23:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-52074-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=ghiti.fr); spf=pass (google.com: domain of linux-kernel+bounces-52074-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52074-linux.lists.archive=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 89974B22F01 for ; Mon, 5 Feb 2024 06:22:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A5FC2C121; Mon, 5 Feb 2024 06:22:11 +0000 (UTC) Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) (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 28C49BA45 for ; Mon, 5 Feb 2024 06:22:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.197 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707114131; cv=none; b=q9WOCbT288NbdI7Xx19oYj0EBqKVTQE7O+s7CaIBiBxn+ykhrq+mMd54Ql0u9s5rD/itRLn9p7w6FwcmHhxd69sYh9lOyjrQWFtELgm5CZhmH3bcjVVe6HSKWa5AyKD7GDmSgLtwNLbZopqko7SufAPxsoKjh9Ph2DgXpxeMaoE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707114131; c=relaxed/simple; bh=n1E9SAggeIq8He3CCq0IU2a7Ti6Dtrns+nYyMulBAeo=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=c2PPwbkjIQlqOo1a1WJbvLsKdvwatE8i3xQXH2kqZYhIN9iZn9REnkaEut8DkNN6am/Rn1n++nrh5+4mVh9rDK0bVW183gcJfE9RLcVzuWbjFtROLnf9CtHrbTyRshwt4i/Z0NW/yXv9zMYkPp97LkbzeVlILTiDHRWFBjD9Y8I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr; spf=pass smtp.mailfrom=ghiti.fr; arc=none smtp.client-ip=217.70.183.197 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ghiti.fr Received: by mail.gandi.net (Postfix) with ESMTPSA id E0F701C0004; Mon, 5 Feb 2024 06:22:04 +0000 (UTC) Message-ID: Date: Mon, 5 Feb 2024 07:22:04 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] RISC-V: Don't use IPIs in flush_icache_all() when patching text Content-Language: en-US To: Anup Patel , Palmer Dabbelt , Paul Walmsley Cc: Anup Patel , linux-kernel@vger.kernel.org, =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , Atish Patra , linux-riscv@lists.infradead.org, Andrew Jones References: <20240205042955.833752-1-apatel@ventanamicro.com> From: Alexandre Ghiti In-Reply-To: <20240205042955.833752-1-apatel@ventanamicro.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-GND-Sasl: alex@ghiti.fr 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/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 *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);