From patchwork Thu Jul 27 18:55:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13330589 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03B01C001DC for ; Thu, 27 Jul 2023 19:00:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 94DAB6B0072; Thu, 27 Jul 2023 15:00:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FDC36B0074; Thu, 27 Jul 2023 15:00:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C6286B0075; Thu, 27 Jul 2023 15:00:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6CC7A6B0072 for ; Thu, 27 Jul 2023 15:00:09 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 38BAC1A1149 for ; Thu, 27 Jul 2023 19:00:09 +0000 (UTC) X-FDA: 81058306938.24.A330207 Received: from mail-lj1-f171.google.com (mail-lj1-f171.google.com [209.85.208.171]) by imf09.hostedemail.com (Postfix) with ESMTP id 41E3A14002F for ; Thu, 27 Jul 2023 19:00:06 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=GSLEhl1R; dmarc=none; spf=pass (imf09.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.208.171 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690484407; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=RNz5ULfyNJeCjPsvwCv5L4clxVFquhVEWOvSxelKP+w=; b=221SQS/j+YAPQPU5Ol9pfLtgC/4n0A1gZTJywq3eKtH/o/UsI/SQgEapWBnew5PkMWkzAb t8wpH2nf6tL3xgqFy+ZdosIGkqrwHXPOT4qxXuQT/0iZa4A4iyhF70nal7Y318hFYqvSFV H0eDi8O0fn1an9XDi9Hlcw+fdpue7OA= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=GSLEhl1R; dmarc=none; spf=pass (imf09.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.208.171 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690484407; a=rsa-sha256; cv=none; b=saV+i+oaycLw8m1wAVPGjdA9VJN2gyztRMLk+BFoc7x0sb5bxUe6Ol0SLW4TKAWVgBJ7pi o/xEyotg0c212+G3EJOGISe8LEkurIXTCUmRbrk29a3kcPFCQw4thUP9NCPbvZE4OFl1n+ 9I4KiGluHKcMOtLw4Sp8S9QUElcL1cQ= Received: by mail-lj1-f171.google.com with SMTP id 38308e7fff4ca-2b9bf52cd08so20334971fa.2 for ; Thu, 27 Jul 2023 12:00:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1690484405; x=1691089205; 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=RNz5ULfyNJeCjPsvwCv5L4clxVFquhVEWOvSxelKP+w=; b=GSLEhl1R8oaK89GQOF+Pv24vQFwey5dkoAOjZEenIXfi01TU4C1AFY7vwTgG+IbUlF Bt6mSf5r0S5qnDSbj7fbdtGv7jJ9iOIgyrK6WLr9GeK+jc2ceGyLFhK5wvI+lwtOGyns czustvBmWlA/JC7ECm2Z0wVylVcw7vbMO0XeSmF1L7ausPlrs8/XAS6Xs65/CWeQ8Wfp tsRKYYdO/ryRUCT9y2Jg2iATmCj4AqgpipC57hi+IHiQ5cyV+qpL+Zjknvlp0yeTBYnd SQsKP9CGJ03Unrmv/tUdQbc4zK6ruo/AZl67eW8Md9grR2PmdteBl/N9wtoGp55xr42V jXYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690484405; x=1691089205; 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=RNz5ULfyNJeCjPsvwCv5L4clxVFquhVEWOvSxelKP+w=; b=b7AmrGBejHC07PSvSy6nZgXuZSivV73t3Ju8g8XTCV3Oqq7NgfHTrq65/Z6qD3bqR1 3a/Di1PZBQ5zv4cNbHxBlHUhtytp/i83mY0YuZwiX3WT1ZBkXgf2pxBxFnfAffU7yUkG L4DYOCPaICwmSw/Mjf8T/IeGT33UvCLY8r5IA+vughVfZpKDjNLbk9z69SdcnV8wmTk5 ajUwItMWAycGzxAcBJ1PR2XEpcjs8hamydkasAG3+/ccHkS7Lmdlk4K/kzolEMRXZ0u9 mQsMVTlkdovusFOTsg54BkdxudKQQ4zC9Si65pPe1qADugMqAjsM2+3QX7MAXiN5dpFe Y7MA== X-Gm-Message-State: ABy/qLZaopyYswb4jBH59J7d+brJQ9sxv0Cej/uaGEMR/ATVP7zEs95N ki15dXvqv+AS70InX2JQU80KKA== X-Google-Smtp-Source: APBJJlHJDmgCrOPWkDvISdqVBAhll7aGnu9u1uL1uj24Zj+LruMascb6qDrvCi38iKxywHDYgHx7iw== X-Received: by 2002:a2e:9444:0:b0:2b6:da64:321 with SMTP id o4-20020a2e9444000000b002b6da640321mr2299474ljh.45.1690484405350; Thu, 27 Jul 2023 12:00:05 -0700 (PDT) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id p1-20020a7bcc81000000b003fa96fe2bebsm2470792wma.41.2023.07.27.12.00.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jul 2023 12:00:05 -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 Subject: [PATCH v2 4/4] riscv: Improve flush_tlb_kernel_range() Date: Thu, 27 Jul 2023 20:55:53 +0200 Message-Id: <20230727185553.980262-5-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230727185553.980262-1-alexghiti@rivosinc.com> References: <20230727185553.980262-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 41E3A14002F X-Stat-Signature: formk93dmfjie867xem3fga4pigonk1b X-HE-Tag: 1690484406-927216 X-HE-Meta: U2FsdGVkX19ML8UclaId2vxTRBeyYc/F61dcAMj+EXH8UKPlL7fGa5cCfWngKicHIN9fErI2YLzULkegjqrRmYwaFs/XdK5U1wGOinCeXJLB9JFvJJSi39MsidyZ0BN2WgrNu9rAYH6Cg6zoBMMHoPIg3dsWJOTQhWDzIWJGBjiiACLoSW/jrMsWjamnGUPB5WgemKSvz/j1i9XYmawJ4llYGiec5SD9rlVROX9xfJ3ALgarurHBmM1rWAaPupmoiDxF2Dty7HkqqrNladSabxpb8MjUuf744mnyYW5HSJunRyATCUfEC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ZKdjg19D5In0YKz6PTARCFGig9XcSYajGq19dLwY5ZeAFwp6NjWQz+QiyiOAe3qKAhgFA75w31Ac6kLTGLSPVxH667bJ2A/2x8puRb6J2Ta4Yg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 | 35 +++++++++++++++++++++++-------- 2 files changed, 32 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 8017d2130e27..96aeacb269d5 100644 --- a/arch/riscv/mm/tlbflush.c +++ b/arch/riscv/mm/tlbflush.c @@ -117,18 +117,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()) { @@ -162,7 +171,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) @@ -194,6 +204,13 @@ void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, __flush_tlb_range(vma->vm_mm, start, end - start, 1 << stride_shift); } + +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)