Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp1909502rdh; Sat, 28 Oct 2023 12:08:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHc+v1GnluD67bPcrLT/bQTpVZu/4M1CmkHqdJLtKmg98cnZ7ETtx8So739D7iwIw1qAxd7 X-Received: by 2002:a17:903:1ca:b0:1cc:3bd3:73d8 with SMTP id e10-20020a17090301ca00b001cc3bd373d8mr1238681plh.59.1698520095977; Sat, 28 Oct 2023 12:08:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698520095; cv=none; d=google.com; s=arc-20160816; b=uuaJxZMmNxbDRcr7HauG7gqlOHZ6rpXHUw3C+ncvEKaXkwimwcznyhJEUPV/hGCGym SBCOQb6DSJ2pnO3TbOORLv5Z5IIpbTXko9VdjbnDD+I3uXsKtvjqFjJYt+FJ0+dUpoRg pjm9jD4urYoJoDB8VJ+Y8ASAqIeMniz3DfASwh42j0i01e+P0yWQQMIEjMs5C8wwlfQ7 Lk98dmFLae/Kesn1KJZgNbc77fonCgsZQP0DVHyp85z8Q70ekmXeSbcf5A2tFve8bfiT njz5HMtwtwbNX2gpSFjHTCV//phn2pk8Cj9my5xFwcHcqqMqV1u55wYEElzIwGw4bxQ7 OCVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=SDans3zKYwJeBvlW9uxRahA6xZVgXjUPNuZ8zu843v8=; fh=LeYqmiSqLmC0Mq4ml11y5+VAZrgiUwc+rL1SyWOdips=; b=fBQxZfd+Enwstq/zza2RO6+UPMb+nhS2DmZjYyyBMjv2lDonXDYU63SVRS5V52StNJ QeKTU5TV/aLHZNoFwOuaGjLoXKetqMgSd6US1Thg13FGiIVAwmo2BZ/iQ6WlpwqAPhVf 18GroyLKypHM22DC+DMbyKHLbpCysrzRSQYBfqJiFXx69x0fCLfgu3qukQsecOJfYLCD +z+poD66SxsAJiVRF0Tt56FWHVzdfevbVRQs3wVDJNSJLjwILlJ0Yu8PYjsT2+VlVNw2 lhTMILsC3zLqmGyxGRkSkwpKZVG/kkq1uvlIZp7CupYTlJLPiOaGVADgsSrUQEIYcNJ0 jQ4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=TEdkMwLw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id p11-20020a170902e74b00b001c6189eaadesi2930927plf.132.2023.10.28.12.08.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 12:08:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=TEdkMwLw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id BD28A8057B3C; Sat, 28 Oct 2023 12:08:12 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229446AbjJ1THt (ORCPT + 99 others); Sat, 28 Oct 2023 15:07:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229449AbjJ1THr (ORCPT ); Sat, 28 Oct 2023 15:07:47 -0400 Received: from mail-oi1-x232.google.com (mail-oi1-x232.google.com [IPv6:2607:f8b0:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D429ED for ; Sat, 28 Oct 2023 12:07:44 -0700 (PDT) Received: by mail-oi1-x232.google.com with SMTP id 5614622812f47-3b2f507c03cso1827918b6e.2 for ; Sat, 28 Oct 2023 12:07:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1698520064; x=1699124864; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=SDans3zKYwJeBvlW9uxRahA6xZVgXjUPNuZ8zu843v8=; b=TEdkMwLwMoBzGSlgpljJT16x+bOALZr9nNpPDuABIflNxDpVbpyhern6lzP8/AYuZB SAqNqdImK0HTZY7u9k0fAdP8bF4/nrGpeHQxurwVcKwWDVB3qQEIKshbRqIJ6/xhnEPL PbI06qCr7bp4rKs80Kj+3lc5uehH3GJ/1dOMT6453z6zaDy4aaZsLsOtrjq0jJVQH6TY 2gHi80oGUOFpY0HJjWfg9UK7dpamJe+6sV986Dm2U4vXrUbryUZgmlVV30qO3rvkReUc ymYQu7oO1tYNme+u6oI/IdxruCO1+SWAFdzgRDt2thtewkbyxxj/9Pi7rS7AEdcHwztU oW/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698520064; x=1699124864; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=SDans3zKYwJeBvlW9uxRahA6xZVgXjUPNuZ8zu843v8=; b=bKNoK48wc0q9+nxM3Bn2S2kHbklOJUwFy3n1XcPigMJwPAtoxBj26ySQetQgFIyikN H4eiyUOwzyXcBgptL5W3XA1JEC8UzveKnpAEiM3EFDkztCeUFh4/wxFoVlg2OitJBajq s14Vh0hLjZjGSsZXhVUtReZeVsCPMYcfnsFEZ+FOfadVHZgOC2yZyFwTa2USFQj/xjdh kRFzPjYzZhdIb8yVQJX/kye7DzCXKuRIKZDofOayUMQdKDMZK4tsR8JNYsIVIkDakn31 uQo4SYecOEKMCZIXgufp/GwCm6fj/NUNWVmOiLrwEyAm/KIOX/CpK/FsyFpzCWMHBGZU P7Dg== X-Gm-Message-State: AOJu0YzPm0CDPbsHpM0YGTlOaFn6apux/t9MJwMMGEPP1CJ6pFCR9Q8V jLmQUpZJ9xcFpwxzZju1EO014w== X-Received: by 2002:aca:1304:0:b0:3a8:5ff0:6b52 with SMTP id e4-20020aca1304000000b003a85ff06b52mr6030152oii.15.1698520063810; Sat, 28 Oct 2023 12:07:43 -0700 (PDT) Received: from ?IPV6:2600:1700:2000:b002:c988:e7b9:36ab:57c6? ([2600:1700:2000:b002:c988:e7b9:36ab:57c6]) by smtp.gmail.com with ESMTPSA id y3-20020a37e303000000b00774830b40d4sm1786043qki.47.2023.10.28.12.07.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 28 Oct 2023 12:07:43 -0700 (PDT) Message-ID: Date: Sat, 28 Oct 2023 14:07:41 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 3/4] riscv: Make __flush_tlb_range() loop over pte instead of flushing the whole tlb Content-Language: en-US To: Alexandre Ghiti Cc: Andrew Jones , Lad Prabhakar , 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 References: <20231019140151.21629-1-alexghiti@rivosinc.com> <20231019140151.21629-4-alexghiti@rivosinc.com> From: Samuel Holland In-Reply-To: <20231019140151.21629-4-alexghiti@rivosinc.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,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 agentk.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 (agentk.vger.email [0.0.0.0]); Sat, 28 Oct 2023 12:08:12 -0700 (PDT) On 2023-10-19 9:01 AM, Alexandre Ghiti wrote: > Currently, when the range to flush covers more than one page (a 4K page or > a hugepage), __flush_tlb_range() flushes the whole tlb. Flushing the whole > tlb comes with a greater cost than flushing a single entry so we should > flush single entries up to a certain threshold so that: > threshold * cost of flushing a single entry < cost of flushing the whole > tlb. > > Co-developed-by: Mayuresh Chitale > Signed-off-by: Mayuresh Chitale > Signed-off-by: Alexandre Ghiti > Reviewed-by: Andrew Jones > Tested-by: Lad Prabhakar # On RZ/Five SMARC > --- > arch/riscv/include/asm/sbi.h | 3 - > arch/riscv/include/asm/tlbflush.h | 3 + > arch/riscv/kernel/sbi.c | 32 +++------ > arch/riscv/mm/tlbflush.c | 115 +++++++++++++++--------------- > 4 files changed, 72 insertions(+), 81 deletions(-) > > diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h > index 12dfda6bb924..0892f4421bc4 100644 > --- a/arch/riscv/include/asm/sbi.h > +++ b/arch/riscv/include/asm/sbi.h > @@ -280,9 +280,6 @@ void sbi_set_timer(uint64_t stime_value); > void sbi_shutdown(void); > void sbi_send_ipi(unsigned int cpu); > int sbi_remote_fence_i(const struct cpumask *cpu_mask); > -int sbi_remote_sfence_vma(const struct cpumask *cpu_mask, > - unsigned long start, > - unsigned long size); > > int sbi_remote_sfence_vma_asid(const struct cpumask *cpu_mask, > unsigned long start, > diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h > index f5c4fb0ae642..170a49c531c6 100644 > --- a/arch/riscv/include/asm/tlbflush.h > +++ b/arch/riscv/include/asm/tlbflush.h > @@ -11,6 +11,9 @@ > #include > #include > > +#define FLUSH_TLB_MAX_SIZE ((unsigned long)-1) > +#define FLUSH_TLB_NO_ASID ((unsigned long)-1) > + > #ifdef CONFIG_MMU > extern unsigned long asid_mask; > > diff --git a/arch/riscv/kernel/sbi.c b/arch/riscv/kernel/sbi.c > index c672c8ba9a2a..5a62ed1da453 100644 > --- a/arch/riscv/kernel/sbi.c > +++ b/arch/riscv/kernel/sbi.c > @@ -11,6 +11,7 @@ > #include > #include > #include > +#include > > /* default SBI version is 0.1 */ > unsigned long sbi_spec_version __ro_after_init = SBI_SPEC_VERSION_DEFAULT; > @@ -376,32 +377,15 @@ int sbi_remote_fence_i(const struct cpumask *cpu_mask) > } > EXPORT_SYMBOL(sbi_remote_fence_i); > > -/** > - * sbi_remote_sfence_vma() - Execute SFENCE.VMA instructions on given remote > - * harts for the specified virtual address range. > - * @cpu_mask: A cpu mask containing all the target harts. > - * @start: Start of the virtual address > - * @size: Total size of the virtual address range. > - * > - * Return: 0 on success, appropriate linux error code otherwise. > - */ > -int sbi_remote_sfence_vma(const struct cpumask *cpu_mask, > - unsigned long start, > - unsigned long size) > -{ > - return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_SFENCE_VMA, > - cpu_mask, start, size, 0, 0); > -} > -EXPORT_SYMBOL(sbi_remote_sfence_vma); > - > /** > * sbi_remote_sfence_vma_asid() - Execute SFENCE.VMA instructions on given > - * remote harts for a virtual address range belonging to a specific ASID. > + * remote harts for a virtual address range belonging to a specific ASID or not. > * > * @cpu_mask: A cpu mask containing all the target harts. > * @start: Start of the virtual address > * @size: Total size of the virtual address range. > - * @asid: The value of address space identifier (ASID). > + * @asid: The value of address space identifier (ASID), or FLUSH_TLB_NO_ASID > + * for flushing all address spaces. > * > * Return: 0 on success, appropriate linux error code otherwise. > */ > @@ -410,8 +394,12 @@ int sbi_remote_sfence_vma_asid(const struct cpumask *cpu_mask, > unsigned long size, > unsigned long asid) > { > - return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID, > - cpu_mask, start, size, asid, 0); > + if (asid == FLUSH_TLB_NO_ASID) > + return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_SFENCE_VMA, > + cpu_mask, start, size, 0, 0); > + else > + return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID, > + cpu_mask, start, size, asid, 0); > } > EXPORT_SYMBOL(sbi_remote_sfence_vma_asid); > > diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c > index 5933744df91a..c27ba720e35f 100644 > --- a/arch/riscv/mm/tlbflush.c > +++ b/arch/riscv/mm/tlbflush.c > @@ -9,28 +9,50 @@ > > static inline void local_flush_tlb_all_asid(unsigned long asid) > { > - __asm__ __volatile__ ("sfence.vma x0, %0" > - : > - : "r" (asid) > - : "memory"); > + if (asid != FLUSH_TLB_NO_ASID) > + __asm__ __volatile__ ("sfence.vma x0, %0" > + : > + : "r" (asid) > + : "memory"); > + else > + local_flush_tlb_all(); > } > > static inline void local_flush_tlb_page_asid(unsigned long addr, > unsigned long asid) > { > - __asm__ __volatile__ ("sfence.vma %0, %1" > - : > - : "r" (addr), "r" (asid) > - : "memory"); > + if (asid != FLUSH_TLB_NO_ASID) > + __asm__ __volatile__ ("sfence.vma %0, %1" > + : > + : "r" (addr), "r" (asid) > + : "memory"); > + else > + local_flush_tlb_page(addr); > } > > -static inline void local_flush_tlb_range(unsigned long start, > - unsigned long size, unsigned long stride) > +/* > + * Flush entire TLB if number of entries to be flushed is greater > + * than the threshold below. > + */ > +static unsigned long tlb_flush_all_threshold __read_mostly = 64; > + > +static void local_flush_tlb_range_threshold_asid(unsigned long start, > + unsigned long size, > + unsigned long stride, > + unsigned long asid) > { > - if (size <= stride) > - local_flush_tlb_page(start); > - else > - local_flush_tlb_all(); > + u16 nr_ptes_in_range = DIV_ROUND_UP(size, stride); The result of this division could easily overflow a u16, which makes this code do the wrong thing if the remainder is small. With this fixed: Reviewed-by: Samuel Holland Tested-by: Samuel Holland > + int i; > + > + if (nr_ptes_in_range > tlb_flush_all_threshold) { > + local_flush_tlb_all_asid(asid); > + return; > + } > + > + for (i = 0; i < nr_ptes_in_range; ++i) { > + local_flush_tlb_page_asid(start, asid); > + start += stride; > + } > } > > static inline void local_flush_tlb_range_asid(unsigned long start, > @@ -38,8 +60,10 @@ static inline void local_flush_tlb_range_asid(unsigned long start, > { > if (size <= stride) > local_flush_tlb_page_asid(start, asid); > - else > + else if (size == FLUSH_TLB_MAX_SIZE) > local_flush_tlb_all_asid(asid); > + else > + local_flush_tlb_range_threshold_asid(start, size, stride, asid); > } > > static void __ipi_flush_tlb_all(void *info) > @@ -52,7 +76,7 @@ void flush_tlb_all(void) > if (riscv_use_ipi_for_rfence()) > on_each_cpu(__ipi_flush_tlb_all, NULL, 1); > else > - sbi_remote_sfence_vma(NULL, 0, -1); > + sbi_remote_sfence_vma_asid(NULL, 0, FLUSH_TLB_MAX_SIZE, FLUSH_TLB_NO_ASID); > } > > struct flush_tlb_range_data { > @@ -69,18 +93,12 @@ static void __ipi_flush_tlb_range_asid(void *info) > local_flush_tlb_range_asid(d->start, d->size, d->stride, d->asid); > } > > -static void __ipi_flush_tlb_range(void *info) > -{ > - struct flush_tlb_range_data *d = info; > - > - local_flush_tlb_range(d->start, d->size, d->stride); > -} > - > 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 long asid = FLUSH_TLB_NO_ASID; > unsigned int cpuid; > bool broadcast; > > @@ -90,39 +108,24 @@ static void __flush_tlb_range(struct mm_struct *mm, unsigned long start, > 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; > - > - if (broadcast) { > - if (riscv_use_ipi_for_rfence()) { > - ftd.asid = asid; > - ftd.start = start; > - ftd.size = size; > - ftd.stride = stride; > - on_each_cpu_mask(cmask, > - __ipi_flush_tlb_range_asid, > - &ftd, 1); > - } else > - sbi_remote_sfence_vma_asid(cmask, > - start, size, asid); > - } else { > - local_flush_tlb_range_asid(start, size, stride, asid); > - } > + > + if (static_branch_unlikely(&use_asid_allocator)) > + asid = atomic_long_read(&mm->context.id) & asid_mask; > + > + if (broadcast) { > + if (riscv_use_ipi_for_rfence()) { > + ftd.asid = asid; > + ftd.start = start; > + ftd.size = size; > + ftd.stride = stride; > + on_each_cpu_mask(cmask, > + __ipi_flush_tlb_range_asid, > + &ftd, 1); > + } else > + sbi_remote_sfence_vma_asid(cmask, > + start, size, asid); > } else { > - if (broadcast) { > - if (riscv_use_ipi_for_rfence()) { > - ftd.asid = 0; > - ftd.start = start; > - ftd.size = size; > - ftd.stride = stride; > - on_each_cpu_mask(cmask, > - __ipi_flush_tlb_range, > - &ftd, 1); > - } else > - sbi_remote_sfence_vma(cmask, start, size); > - } else { > - local_flush_tlb_range(start, size, stride); > - } > + local_flush_tlb_range_asid(start, size, stride, asid); > } > > put_cpu(); > @@ -130,7 +133,7 @@ static void __flush_tlb_range(struct mm_struct *mm, unsigned long start, > > void flush_tlb_mm(struct mm_struct *mm) > { > - __flush_tlb_range(mm, 0, -1, PAGE_SIZE); > + __flush_tlb_range(mm, 0, FLUSH_TLB_MAX_SIZE, PAGE_SIZE); > } > > void flush_tlb_mm_range(struct mm_struct *mm,