Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp1903678rdh; Sat, 28 Oct 2023 11:53:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHzgX/Rc44mTFjUv4YG13oYLcAfQMRLo27AlRKDgJPVAHhP9MbCNo+g5o+7HzSI4z/VfJM7 X-Received: by 2002:a05:6a00:99e:b0:68a:6018:a66f with SMTP id u30-20020a056a00099e00b0068a6018a66fmr6967403pfg.2.1698519198133; Sat, 28 Oct 2023 11:53:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698519198; cv=none; d=google.com; s=arc-20160816; b=xSg1RREVtc6VvyiDbqzA/qnPJ001v5POFhj4D/988SyXY0I/DxgoQoTIA2JGqnOnQ5 xHy5+gew0rDTImRlyAO98HPl9xSuVxo6InbvMlyog6M2VpiHlGXYxlVnc3mpmRwVMS4Y TWadD70gsnoFRGU7wWyjUr0doS0EzpjFsSukTcI6dk2avafjZBL06SkuziyLA2Rp4/GR 1hSSuMY6jD3eJluExw/Noz9VxbpIbaHcK4A+91/YJzdFw97BpNwC9ph8O2HWS1zQPkFx b8P095imjUFDUfDtXId7c8SgI4XQDrwLk3CI4gZY+pxS0fqJH1UMsKB/SztJT4kBAJ7i kEUg== 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:cc :references:to:content-language:subject:user-agent:mime-version:date :message-id:dkim-signature; bh=rTzbMaF9e6LFHLkvVnlr1iqkQocBR5uDxVZIr/Uj4WE=; fh=72YprTlTyqIDbcDndmj4MIrrG66eQbcf+ajLXvJGMfA=; b=A6QZd8305OopeaZYMxwal59G/fRY/0WH4HX+W5mB6J3V+t5Ejk6CnYWNkXFojDIwEC 3aOloPXvNJu/+Jnoeuf5RgP6e0xQofgsti7X9rzDarCjNksSlzVfRZiYh5Arlut7DIfs +T0pt1lfUVr8kjjVPxYIVMRbQG7GiILMINF0eFVdGJlw31GW7FDVMAXTlADPD5/UooYZ J21+a95ZYsCXaNa+QrvpYIOUTiP5lrFFMFqxjXfuBZAwZJdW73+k7FpuXvcMDCmoSFGB 6NLJEpXQ4afoqKMscuSTO0Q4G6vstwWpdMmhAU+HeZX6ejvXTuCGPLVXNZW+lBjZUVZ1 GPLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=bKiCN+Xi; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id m19-20020a056a00081300b006be2f4171b6si2738334pfk.151.2023.10.28.11.53.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 11:53:18 -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=@sifive.com header.s=google header.b=bKiCN+Xi; 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; 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 fry.vger.email (Postfix) with ESMTP id 40CC3809AFEE; Sat, 28 Oct 2023 11:53:15 -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 S229448AbjJ1Swr (ORCPT + 99 others); Sat, 28 Oct 2023 14:52:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229570AbjJ1Swq (ORCPT ); Sat, 28 Oct 2023 14:52:46 -0400 Received: from mail-qv1-xf36.google.com (mail-qv1-xf36.google.com [IPv6:2607:f8b0:4864:20::f36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB9D0F3 for ; Sat, 28 Oct 2023 11:52:43 -0700 (PDT) Received: by mail-qv1-xf36.google.com with SMTP id 6a1803df08f44-66d0f945893so25934426d6.1 for ; Sat, 28 Oct 2023 11:52:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1698519163; x=1699123963; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:cc:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=rTzbMaF9e6LFHLkvVnlr1iqkQocBR5uDxVZIr/Uj4WE=; b=bKiCN+XifgLo/kCsXVZ5lyU1MMBSR5PZtUh2oeq3zr3v1A31KOPcJjRPnNr1RFnWya UBCYDpidnkE4lxC4pEAi+LfJoldNgHoDda6NrGGRw9NLWXvQ2vu5n0lo7fzbPgeOfWAP JS7epE/YfcPehp1w8QFwp3bobJG0mqIt1XJGDkSndFNSqApFAdP0SkaAmXQO3GV5wNoU Wqu8ESb06tZ26R0lGjsup22v3l4BE++bPpq+EmqM9ul5ZSvtxtPnDRz3YsujL4VPbvPs oQ4wGRaKBZ827tdr9smCebiTxQu4lUv2F3zUPEADtMwji/lCH57iMNm8wEKgrtY7KUZY ClmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698519163; x=1699123963; h=content-transfer-encoding:in-reply-to:from:cc:references: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=rTzbMaF9e6LFHLkvVnlr1iqkQocBR5uDxVZIr/Uj4WE=; b=S39JQRbGoSBIY+brkc3hpPk3BA4qaA0nDuly1+JN8A1PhLQKjQQjkFMCcEu3GbWIVG CO9O2zPMaRSTXatpnYuCgm+nhQA00Cpn7GBq5atvAenCQyFdHv9boWNhOrmX9KI71vDk dklWaKV51Ute0hbf0xwBTSrCvR7myBOiCIure5SM5BVFKJOT+1fxGThShdzi18HtRYEX OwCm6bXoctOo9iGh2PdrrA4d7PeswSYJMdhnhTYImzg81sQTbgxPgGiOp8qHdOCMS84X Z/p3igkc3/8HmtPOwcH0m3M39jfSvMT6RijcydlAgBQybb+9fDaVFsvKMf0DZm9xiU7F Bxew== X-Gm-Message-State: AOJu0Yz8Mb5MEsfYljAiCuG3Gw3zBP1V8JGioFJXQJGr+grGCtFJHRXT 047l5qD33MCkQERcBE9feVwoPQ== X-Received: by 2002:ad4:5aaf:0:b0:66d:8752:b6cf with SMTP id u15-20020ad45aaf000000b0066d8752b6cfmr5006791qvg.26.1698519163004; Sat, 28 Oct 2023 11:52: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 mg22-20020a056214561600b0065b0d9b4ee7sm1820353qvb.20.2023.10.28.11.52.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 28 Oct 2023 11:52:42 -0700 (PDT) Message-ID: Date: Sat, 28 Oct 2023 13:52:41 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 2/4] riscv: Improve flush_tlb_range() for hugetlb pages Content-Language: en-US To: Alexandre Ghiti References: <20231019140151.21629-1-alexghiti@rivosinc.com> <20231019140151.21629-3-alexghiti@rivosinc.com> Cc: 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 From: Samuel Holland In-Reply-To: <20231019140151.21629-3-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 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]); Sat, 28 Oct 2023 11:53:15 -0700 (PDT) On 2023-10-19 9:01 AM, Alexandre Ghiti wrote: > flush_tlb_range() uses a fixed stride of PAGE_SIZE and in its current form, > when a hugetlb mapping needs to be flushed, flush_tlb_range() flushes the > whole tlb: so set a stride of the size of the hugetlb mapping in order to > only flush the hugetlb mapping. However, if the hugepage is a NAPOT region, > all PTEs that constitute this mapping must be invalidated, so the stride > size must actually be the size of the PTE. > > Note that THPs are directly handled by flush_pmd_tlb_range(). > > Signed-off-by: Alexandre Ghiti > --- > arch/riscv/mm/tlbflush.c | 31 ++++++++++++++++++++++++++++++- > 1 file changed, 30 insertions(+), 1 deletion(-) > > diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c > index fa03289853d8..5933744df91a 100644 > --- a/arch/riscv/mm/tlbflush.c > +++ b/arch/riscv/mm/tlbflush.c > @@ -3,6 +3,7 @@ > #include > #include > #include > +#include > #include > #include > > @@ -147,7 +148,35 @@ 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) > { > - __flush_tlb_range(vma->vm_mm, start, end - start, PAGE_SIZE); > + unsigned long stride_size; > + > + if (!is_vm_hugetlb_page(vma)) { > + stride_size = PAGE_SIZE; > + } else { > + stride_size = huge_page_size(hstate_vma(vma)); > + > +#ifdef CONFIG_RISCV_ISA_SVNAPOT There is a fallback implementation of has_svnapot(), so you do not need this preprocessor check. With that removed: Reviewed-by: Samuel Holland > + /* > + * As stated in the privileged specification, every PTE in a > + * NAPOT region must be invalidated, so reset the stride in that > + * case. > + */ > + if (has_svnapot()) { > + if (stride_size >= PGDIR_SIZE) > + stride_size = PGDIR_SIZE; > + else if (stride_size >= P4D_SIZE) > + stride_size = P4D_SIZE; As a side note, and this is probably premature optimization... PGDIR_SIZE and P4D_SIZE check pgtable_l{4,5}_enabled. That's not really necessary here, since we are just trying to round down, and there won't be any higher-order hugepages if those paging levels are disabled. > + else if (stride_size >= PUD_SIZE) > + stride_size = PUD_SIZE; > + else if (stride_size >= PMD_SIZE) > + stride_size = PMD_SIZE; > + else > + stride_size = PAGE_SIZE; > + } > +#endif > + } > + > + __flush_tlb_range(vma->vm_mm, start, end - start, stride_size); > } > #ifdef CONFIG_TRANSPARENT_HUGEPAGE > void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start,