Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp5434351rwb; Tue, 1 Aug 2023 02:27:08 -0700 (PDT) X-Google-Smtp-Source: APBJJlFcnaMwFUalaz3Pxr88WGgxMxERYmbHV4wfU5NwZz8yh5oa4eHquDAx0fgRvs4xb+Es48wf X-Received: by 2002:a17:906:7696:b0:994:47a5:a377 with SMTP id o22-20020a170906769600b0099447a5a377mr1855736ejm.24.1690882028380; Tue, 01 Aug 2023 02:27:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690882028; cv=none; d=google.com; s=arc-20160816; b=mboPzIw+/u8qwL1hxayJiBKoGFeFmKHmorCquT/NYoWYTS6sZhrfuU/J5S/Vg/0vcI i3S+4efqgqna0oNHWNiRWQFywfYtHKX5NTLIQ3EDH2O7s06w/QI89oFUBTjJ/OC2UTkc A7Zsf5LXON2Aq7FJXgU2DWAFaLqYQAQuYFlivtNpQNcfiL4VjJkz5BZPBywQPYKBNpx9 Ny/+Gd1jVFaGd9MfDtdXflfh9Epo0oBLpG3PX2k1vvfWS9Mu3KYlZkV55kJ4GN3jFiNK 9pFaw3ghZ4YtPXnlwYNQhD+CPvd8nets8XRh8MFFU5rh5rWumDfVO0l1Aw3pGIHowtt7 66iQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OMcN1+GqTG/8Na8yFouBO/VVBZef6ZJ3qQN1SPpRTO4=; fh=FUpKYc+jGKezHChimvGHso2Y2jew/UgGSnq0l63BhsQ=; b=AIUmHymZFWpv57fpBxI/dIcTcsPi3OUHFza2+sjUO9XU/o4s/MKC8+q5HAVtMbFb9f I8lWniohzYVLBwb1XYKjmFHuQqXU6PI+RLxDbjz7KuuxyN49yuY8qcogr79jdQZMGzKg WvWPKWPUbwSDp9aq3AAwpZ8kGtINczeLFGP3ARZGRVXF9diNTEdrKxROkkIcPr/703LA 1GpLtxnC9YgkDGYPHsokOhd3hkP3VNrQjqUokLtCZ8mRt+xAJzw1MbjZ3sJNVmhUBioE kWKKL5EZzJAYPX2gm6NktKj9TD6BXoFBf2otHeGhCjkoakXsc0lVk6GcHG0QgTB3xdzM 03Rw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b="L6MD/Ipk"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o5-20020a17090608c500b0098e24139529si8201549eje.733.2023.08.01.02.26.44; Tue, 01 Aug 2023 02:27:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b="L6MD/Ipk"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231932AbjHAI6T (ORCPT + 99 others); Tue, 1 Aug 2023 04:58:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229823AbjHAI6O (ORCPT ); Tue, 1 Aug 2023 04:58:14 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6214B19A8 for ; Tue, 1 Aug 2023 01:58:12 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-3fbd33a57b6so59883495e9.2 for ; Tue, 01 Aug 2023 01:58:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1690880291; x=1691485091; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OMcN1+GqTG/8Na8yFouBO/VVBZef6ZJ3qQN1SPpRTO4=; b=L6MD/IpkJ8ijD8AQNDxVBvTARXP1lNuZC1nsssVE1h3LiyUW7vd/sEKLX+4vvj3bC3 GAbuOy4LDXQWnyB80R1g/cm5UVJhCHHyfh/rUGwrJGyuUad+i3gBhRlknE+R4jdD/+UG gJbmECUiJGhTG0NacduUsWtak+tjx/jnkdY9FXJnlH4HxB82s4ORT/QIdYV9z/xkF4/K faM3GF13ygpkXHTDnFrYgoD4HcG2BZgiqUceZvmSv87fyDS6JjKD1dRjiDQCznsNu2lQ vwHMBsfaVvBqKLKQp+D/dRJqukzaNyIX1DRhPPpk8LawOxtRUH/BOPeGyUTtn+4Myb5q oRog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690880291; x=1691485091; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OMcN1+GqTG/8Na8yFouBO/VVBZef6ZJ3qQN1SPpRTO4=; b=EFVAu01Zg66FD07fRer8txqSOGEJy4maf2A0IqZXt1zbxQV4m6hZ7+ESdQ/mt5IoGd kaSYZREQQlwOHM4pIaiuBzKwjC0RqsXRXjjHEiHgJsD7WOjqLDfKcYfs4O1HC5iGzQDJ vwIBJmPIutwGOpR6fts8XfRn+pTG54KEV8V1JJKrpSUa/eJ3EoSU++dKieBMw5dXz6v9 88R1hdYQ/XwdLboFc78TCFBpxAgfbmAfkRG2vlDDj+9uTZAdSJ1GvhsZdHPEMCgfwcCz uNeNcNrhlgNCJNgXKHKnCo1DAjC+88j3t49eA8wOySBuggwS00oC5oqKTtMo67yKfSHJ 0Zqg== X-Gm-Message-State: ABy/qLYq7LxQK5tk8qhmvNJH/eGP1Z+e5BBjI4t1WdzzMv1aUeYmGSEd dmq4DZignY4ujSVRlItE5wobKg== X-Received: by 2002:a05:600c:285:b0:3fe:1d13:4664 with SMTP id 5-20020a05600c028500b003fe1d134664mr1898359wmk.6.1690880290827; Tue, 01 Aug 2023 01:58:10 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id c6-20020a05600c0ac600b003fe20db88e2sm5255370wmr.18.2023.08.01.01.58.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 01:58:10 -0700 (PDT) From: Alexandre Ghiti To: Will Deacon , "Aneesh Kumar K . V" , Andrew Morton , Nick Piggin , Peter Zijlstra , Mayuresh Chitale , Vincent Chen , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Alexandre Ghiti , Andrew Jones Subject: [PATCH v3 4/4] riscv: Improve flush_tlb_kernel_range() Date: Tue, 1 Aug 2023 10:54:02 +0200 Message-Id: <20230801085402.1168351-5-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230801085402.1168351-1-alexghiti@rivosinc.com> References: <20230801085402.1168351-1-alexghiti@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This function used to simply flush the whole tlb of all harts, be more subtile and try to only flush the range. The problem is that we can only use PAGE_SIZE as stride since we don't know the size of the underlying mapping and then this function will be improved only if the size of the region to flush is < threshold * PAGE_SIZE. Signed-off-by: Alexandre Ghiti Reviewed-by: Andrew Jones --- arch/riscv/include/asm/tlbflush.h | 11 +++++----- arch/riscv/mm/tlbflush.c | 34 +++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h index f5c4fb0ae642..7426fdcd8ec5 100644 --- a/arch/riscv/include/asm/tlbflush.h +++ b/arch/riscv/include/asm/tlbflush.h @@ -37,6 +37,7 @@ void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start, void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr); void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end); +void flush_tlb_kernel_range(unsigned long start, unsigned long end); #ifdef CONFIG_TRANSPARENT_HUGEPAGE #define __HAVE_ARCH_FLUSH_PMD_TLB_RANGE void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start, @@ -53,15 +54,15 @@ static inline void flush_tlb_range(struct vm_area_struct *vma, local_flush_tlb_all(); } -#define flush_tlb_mm(mm) flush_tlb_all() -#define flush_tlb_mm_range(mm, start, end, page_size) flush_tlb_all() -#endif /* !CONFIG_SMP || !CONFIG_MMU */ - /* Flush a range of kernel pages */ static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end) { - flush_tlb_all(); + local_flush_tlb_all(); } +#define flush_tlb_mm(mm) flush_tlb_all() +#define flush_tlb_mm_range(mm, start, end, page_size) flush_tlb_all() +#endif /* !CONFIG_SMP || !CONFIG_MMU */ + #endif /* _ASM_RISCV_TLBFLUSH_H */ diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c index 0c955c474f3a..687808013758 100644 --- a/arch/riscv/mm/tlbflush.c +++ b/arch/riscv/mm/tlbflush.c @@ -120,18 +120,27 @@ static void __flush_tlb_range(struct mm_struct *mm, unsigned long start, unsigned long size, unsigned long stride) { struct flush_tlb_range_data ftd; - struct cpumask *cmask = mm_cpumask(mm); - unsigned int cpuid; + struct cpumask *cmask, full_cmask; bool broadcast; - if (cpumask_empty(cmask)) - return; + if (mm) { + unsigned int cpuid; + + cmask = mm_cpumask(mm); + if (cpumask_empty(cmask)) + return; + + cpuid = get_cpu(); + /* check if the tlbflush needs to be sent to other CPUs */ + broadcast = cpumask_any_but(cmask, cpuid) < nr_cpu_ids; + } else { + cpumask_setall(&full_cmask); + cmask = &full_cmask; + broadcast = true; + } - cpuid = get_cpu(); - /* check if the tlbflush needs to be sent to other CPUs */ - broadcast = cpumask_any_but(cmask, cpuid) < nr_cpu_ids; if (static_branch_unlikely(&use_asid_allocator)) { - unsigned long asid = atomic_long_read(&mm->context.id) & asid_mask; + unsigned long asid = mm ? atomic_long_read(&mm->context.id) & asid_mask : 0; if (broadcast) { if (riscv_use_ipi_for_rfence()) { @@ -165,7 +174,8 @@ static void __flush_tlb_range(struct mm_struct *mm, unsigned long start, } } - put_cpu(); + if (mm) + put_cpu(); } void flush_tlb_mm(struct mm_struct *mm) @@ -196,6 +206,12 @@ void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, __flush_tlb_range(vma->vm_mm, start, end - start, stride_size); } + +void flush_tlb_kernel_range(unsigned long start, unsigned long end) +{ + __flush_tlb_range(NULL, start, end, PAGE_SIZE); +} + #ifdef CONFIG_TRANSPARENT_HUGEPAGE void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end) -- 2.39.2