Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp375319rdb; Thu, 19 Oct 2023 07:06:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHJxWZflTjTPjKZjf02sP62QT5G8/QMVV4HsyNxH7ptkvt1xKE8QDo7wVVnwrniTN9+UZwR X-Received: by 2002:a17:90a:c90c:b0:27d:9f6:47a3 with SMTP id v12-20020a17090ac90c00b0027d09f647a3mr2100103pjt.31.1697724407637; Thu, 19 Oct 2023 07:06:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697724407; cv=none; d=google.com; s=arc-20160816; b=1EKOe6ElU+HOiyOgPklwimiuA1DxLtg0BfKqWltrXScY95hcwXrDvZ5+YAzXlWDfEE 4qbDC48KbEun/l4e80KWIemN7O3f51OAc0FXhfTCMQC7khjzaqEgsc+dJponu3VX843i 9wRymmkcquGvgT9ZNZnjlYzDUzWI6ctn80ogWUMnmuUMSjm07Pb6fAcWdE5K/mhZJphU baaNHAXaHJWxVqawWPxrsidcK5AeYwrndbyS13Za3O++PGH/Kk9PplHEuUgK9tiK6Zyy YEnLBHcix0QEdXuvG3DjXjaTDTq9GRLzLIhEpxkEkz48VJoqpCiT1fCXmwRyjLvqeCJs 0aOw== 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=kwR/Ke20NAcUS81IhTmctZxyEuc+6gPtGwlWuG8Kglw=; fh=HFZAU7RUgUmy4jfChlNw5O41uiJclfFakTpQ7fjwr98=; b=pzO01dR8xEk+29ar0dl2778pqI9tkraA/2a4kubHml7jR6zeB5XxHlAn6AkIvc/MBg cAOXvKS5dLwTFSwKZUKbaZaxHCbFVdQWJXhz/3WVRzYZ+5eS3emwhqwHZTnjxpyPSeLk GC6dk8B8CSx+hogRp0HAqaDQFiV9NN8tmeoExuR1nCwGaVcmzWKNx2vZyEu/Hu8Ypafd kSY0o3yPUJXqq/WNYnODptC19UjWpSRpnYQ8SHamZAH/yVjKhsgGaHsw7CW0tvl0IDj5 fuTtxAWXRzevF1YcxrzIZcX3UZ6Q5c9rV/DCxecPsYTZKjXute3fOlmnshiG9fhEyIev sC0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=cC1UDbLH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id m3-20020a17090b068300b00268278c51e3si2058317pjz.49.2023.10.19.07.06.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 07:06:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=cC1UDbLH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 5868583762E9; Thu, 19 Oct 2023 07:06:36 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345531AbjJSOGU (ORCPT + 99 others); Thu, 19 Oct 2023 10:06:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346015AbjJSOGS (ORCPT ); Thu, 19 Oct 2023 10:06:18 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 55B06136 for ; Thu, 19 Oct 2023 07:06:16 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-317c3ac7339so6811062f8f.0 for ; Thu, 19 Oct 2023 07:06:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1697724375; x=1698329175; darn=vger.kernel.org; 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=kwR/Ke20NAcUS81IhTmctZxyEuc+6gPtGwlWuG8Kglw=; b=cC1UDbLH7ixnvMUdBQ7wIbDKBxbypXS4GBX6klSsRfuokhgCNeU7qiwDNVzqCDjC99 jt+A0QkyWWErARQdkwGxOAdCPR5sIjpIppUiRggDJgqRGZV1VDTYfpQ0RNVoZhtd4n4n PEmJkvmwWxppkbCLKFAXNhXN8AuXMn39KkVPvt6kfE4fFvMJRePv10rnvDP5C3iaXSMq sjj65LYF1AMEEHjLTCEtpTgyx3u16lV0PD1KIpUXPOJWOul7treYl3/SJLuRbwahnO1Q vGVbnYgxnqpk15spdroR3MBxp1MFe7jMugLdxFXoVCdVpEchdgFS1KHjo1hlzHsYMgWI PtLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697724375; x=1698329175; 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=kwR/Ke20NAcUS81IhTmctZxyEuc+6gPtGwlWuG8Kglw=; b=exrIdLVoHjD939xbKvfVw1VP5U6BNxDV4IIrUG+caGXAPTQuIlPjAHxbEK0UJEVSoF //vCeF0FO3vDXlZWKVnYeOZgKJKJWb4Lfm+89GXKNyuXM+rMVK1vi2mTlztKp2cLVLhl 9Zb3nhlDNng0kOtBjvXHB24PgKVnoAW2qT9weq/fNZZVJdLynlZXBLhblLoWeILX4zFt 3Qa7+2FU0WgR4BwuqFuBkOy0J7y7hUN0ejtvKMEQn6/agO6SkqWe7RGILQxVZcnVssNJ HFV0reni7M0GajB0Av99VCWkMSwFahLt8OkVB+gfyCT7TPGXA8QtMZ2USBG6nJ3Ff3TV oYPA== X-Gm-Message-State: AOJu0YwN4m9/HjcNsFXjMeGYP7PelDDTK51H4xpXuBuOhz0VNFSNY/Gz Jo5R2kU3Y85aRKFD/qt15fLP7w== X-Received: by 2002:a5d:6e0b:0:b0:32d:8505:b9d7 with SMTP id h11-20020a5d6e0b000000b0032d8505b9d7mr1610093wrz.43.1697724374647; Thu, 19 Oct 2023 07:06:14 -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 u11-20020a5d514b000000b0032db4825495sm4572197wrt.22.2023.10.19.07.06.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 07:06:14 -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, Samuel Holland , Lad Prabhakar Cc: Alexandre Ghiti , Andrew Jones , Lad Prabhakar Subject: [PATCH v5 4/4] riscv: Improve flush_tlb_kernel_range() Date: Thu, 19 Oct 2023 16:01:51 +0200 Message-Id: <20231019140151.21629-5-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231019140151.21629-1-alexghiti@rivosinc.com> References: <20231019140151.21629-1-alexghiti@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Thu, 19 Oct 2023 07:06:36 -0700 (PDT) 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 Tested-by: Lad Prabhakar # On RZ/Five SMARC --- arch/riscv/include/asm/tlbflush.h | 11 ++++++----- arch/riscv/mm/tlbflush.c | 33 ++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h index 170a49c531c6..8f3418c5f172 100644 --- a/arch/riscv/include/asm/tlbflush.h +++ b/arch/riscv/include/asm/tlbflush.h @@ -40,6 +40,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, @@ -56,15 +57,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 c27ba720e35f..7e182f2bc0ab 100644 --- a/arch/riscv/mm/tlbflush.c +++ b/arch/riscv/mm/tlbflush.c @@ -97,19 +97,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); + struct cpumask *cmask, full_cmask; unsigned long asid = FLUSH_TLB_NO_ASID; - unsigned int cpuid; 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; + 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; + } - if (static_branch_unlikely(&use_asid_allocator)) + if (static_branch_unlikely(&use_asid_allocator) && mm) asid = atomic_long_read(&mm->context.id) & asid_mask; if (broadcast) { @@ -128,7 +136,8 @@ static void __flush_tlb_range(struct mm_struct *mm, unsigned long start, local_flush_tlb_range_asid(start, size, stride, asid); } - put_cpu(); + if (mm) + put_cpu(); } void flush_tlb_mm(struct mm_struct *mm) @@ -181,6 +190,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 - start, 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