From patchwork Tue Aug 1 08:54:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13335926 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 1C0E4C001DF for ; Tue, 1 Aug 2023 08:58:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 900CB8E000D; Tue, 1 Aug 2023 04:58:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 861B88E0002; Tue, 1 Aug 2023 04:58:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 702108E000D; Tue, 1 Aug 2023 04:58:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 5A39F8E0002 for ; Tue, 1 Aug 2023 04:58:15 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 2B02B40508 for ; Tue, 1 Aug 2023 08:58:15 +0000 (UTC) X-FDA: 81074934150.29.B055A45 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) by imf24.hostedemail.com (Postfix) with ESMTP id 35C4318001E for ; Tue, 1 Aug 2023 08:58:11 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b="L6MD/Ipk"; dmarc=none; spf=pass (imf24.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.128.45 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690880292; a=rsa-sha256; cv=none; b=AQJguwV6B9/EREdQIGrvRX0FCIhURp1TaMzzNd/oCFOR+j8NPi0ALyS7hptEugDSjCzb51 vSnwodP4yA8I83gORQZ7aKpYC6rL9x5ublUMFQJN0sB3Qcu/5C7qvXCinTt6IlfW+38qHO 7lO4sPUzP8ZVfqTkWnRfPky4DvdntGk= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b="L6MD/Ipk"; dmarc=none; spf=pass (imf24.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.128.45 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=1690880292; 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=OMcN1+GqTG/8Na8yFouBO/VVBZef6ZJ3qQN1SPpRTO4=; b=EvXTF8WRcwyTcdThxQjeloJ/fw0ut9OCSwluvMfxFYd2rHQUqWBpHC/q6xRr2t92I+xEq4 tcdSw1cJLB76Wqu/D+7fQSocptVFecHcNImHt3kQMl/rvpwJMwMZEPHrGL02EF7UtI+W3s 0OKdmMKBHkYgnapNQ2U1ZLSSIH8iA60= Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-3fe2bc27029so1143455e9.3 for ; Tue, 01 Aug 2023 01:58:11 -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=MHxAd7up7l+U3npr4oyYIvF0kcjiq+Juv+0GW7ChJsQPK7riElt1vnIdTlZH8QCTRe vrOVaFDM/Y3EQGxcf1qTvkfxtLrL0qOOfDT9Xc+VHDb7+bMRfKLDC3HwUdAVWHtwqcku DB6ZqZGDglpYs52XDU3Ws7jiknP7OFZW0Nri265wdMJwgLjVYnDoCLymt9F2IShEG6mA vD1ofneVD4Nmt8VhQVyWVwMmmIaaF+J+lRACft1u5WJgpqrpFW4mh1iaXkSDU9wZeAeh Xn8vbQLzSrJm1JO6nNKHsOu2ymIkNHYzfNiYRoc6WP0R2G2SHRfx//DOvWYdJJSRTVCO tNng== X-Gm-Message-State: ABy/qLYmrQVGtCaX5H+hBPbROEAoKoMjUoCtk2D/C7QYFjhp7vXai9/T FSQpG/54gALjejVIlDy586t54A== X-Google-Smtp-Source: APBJJlEF2Zvw+ZslWuFsVPPOKTzS3UA31UlJYffINUkSj0yPwhIPAXHFC54ECs88TtNi5FfcGYso9w== 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 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 35C4318001E X-Stat-Signature: 6iahcpbmegdyn8rg8hizy4ui9dxyfhys X-HE-Tag: 1690880291-481436 X-HE-Meta: U2FsdGVkX1+ZMs9KFuPGaGrGtJ8aVK/LwrClulh+cvR5YA+ynHr1ckh6GOKktz62wDl70ORdrgzgeAVv3SiBpmI5zDC6N1FVvjNLPw8LYQumb1pSxexSI+8HlGeH9tCj9AYzejt7vTu/bVNVfxmauzD3+B3jklh2lqYW21XShrypisuoExfnZFI1VKG+9ge4UZAlZfEEYlMcR9kIy/fPRW6n2TKxlGvezPcbJcrTznJCk8UZ+OIkgse2hxK47L6YhUWqgfycqmto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dIyz0eLb0hq6Wh7tAxS2s82xMcWdeSEKIKrbbFzSnaglXPLFvkfui4szzy+sLImmKO/NY25FAXy3zQ/6O3XcqJX8K+0unYJLeAOP6NO8eFXJlACYgQpAeF/qxPmoHNIx4ebphBmFVpBGOEDJGyMtEk0uv64tfzCiCFvw7FoX4HRIULsHW1UeQAOOQFsvtWCECeY1QEMkklmiYs= 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 | 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)