From patchwork Thu Jul 27 18:55:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13330597 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A38EAC001DC for ; Thu, 27 Jul 2023 18:57:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=/WCoFgWXt72ObxbHjPZQgxVNvZdFumhn1mJ1adInapM=; b=S6mqiGtcF3FxA4 cvFX+ZCw9jV12ZZBvttqBLESgH2ukxVLWquTMa0dR9imLX3ZDfoT8qlObclHxRgQ6hyeFD0db6dlt 4QvdzPJRhYnl5oiTip7RlKxEue6T6EvSS4fx5AKpb78viakrx+/H83byuN6JtbG7gkCIb1o5P8Bi5 RW4cDQ1eNKOno7D9fjK00b+3IpmZyZyojI1XK18Lbk4mFb6oJ5GEbGm7FFBdl8JWijOB3W5LIHUy2 rs/7EmVv3ANGBMxVifSTySjSMfwFaVZ+dfNzcn8/yxmvOZEeA7XFV5Yo0ktCTzZI3tlo54GRo/VtS Nh+I94X8HiRust6OMRew==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qP6Ak-000EnX-0q; Thu, 27 Jul 2023 18:57:14 +0000 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qP6Ag-000ElQ-2g for linux-riscv@lists.infradead.org; Thu, 27 Jul 2023 18:57:12 +0000 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-3fbfa811667so17472385e9.1 for ; Thu, 27 Jul 2023 11:57:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1690484222; x=1691089022; 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=iNM2lfApDpDHo6wA+erk64IwXrZaYOnl0F5llP9QRbw=; b=rezn/W64XZNrrJy+iA17PP4dY7NEgyDtF1IgTecXk40RddgHRKdjQdVLO3Dm1uiPer A/Ii0X04NrJUwOnHByd4OTxbucNLBTZMeUCmg0Q0dLf4NOeYY4NIOM5/xemrnEQdHqM3 3PO1vT7NS3Pz3oSI0IdeDTeUN+MRrZJlAnsoxNG5N9A0RJP7xFhKTwCS9xEYMFj4sKiK upS7cJ6ZI05fw/KrM4vQe5n5xeNNgCxUW9CbTnUp9OdMXMunKs1O9Hg3Uyk0EhlxrNcE bPoSbeW9PJMxsO5k7dAJnx7DOdPk0t+LTa6vDnZpQDrikaZlST3yBaYyPG73FlWwmYXH 6j4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690484222; x=1691089022; 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=iNM2lfApDpDHo6wA+erk64IwXrZaYOnl0F5llP9QRbw=; b=STRTTz3W6zlME1Jv9bP1PMFHDDeBX7CpB01iEr7AM58DGtcJDtU534sFj933EOrzJS cyBt4LPK1Wj1dF7rnMT3/6F0ygv8nBMa6jpGj2ok6lCSaJ3JgyioV5dv9gvE083Ar0Dn YF44U++sDFAETnUZ8gi7LmWbYEtPvvWCSKAzeeFHOMt9m1jVvwCD7Bwn2C8hmMi5WcHm uGWWN4NwaxPIvBj8Sj7KhYENFrbqyMaJksANXzahDm6efRkA+0QF6MDGEbb7p+lTUzeC ApTurEcOD7RxWpzuDkOUVD0twVOgeiOw1PCqM3JfC/tNRdTdKKT838fQ4G5xBSFoX8Fu lZgA== X-Gm-Message-State: ABy/qLY/HiDaGjdj00mbtfpdAAvJSthLxG/xX+NG+Ntkp3imJ8yXJ2mG AJq2hTl8flsuGptl/6QvOoc7KQ== X-Google-Smtp-Source: APBJJlFWr7cvkbR875xoBGCgLZGqrfaKgfeUiW96reSFz9d+/NwsOjVFOGJrqCp4Ny2X3+qOxY92VA== X-Received: by 2002:adf:e692:0:b0:317:36d8:d6e8 with SMTP id r18-20020adfe692000000b0031736d8d6e8mr2479276wrm.25.1690484222369; Thu, 27 Jul 2023 11:57:02 -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 z15-20020a05600c114f00b003fbb5506e54sm2443379wmz.29.2023.07.27.11.57.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jul 2023 11:57:02 -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 1/4] riscv: Improve flush_tlb() Date: Thu, 27 Jul 2023 20:55:50 +0200 Message-Id: <20230727185553.980262-2-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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230727_115710_869008_B90DC360 X-CRM114-Status: GOOD ( 12.57 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org For now, flush_tlb() simply calls flush_tlb_mm() which results in a flush of the whole TLB. So let's use mmu_gather fields to provide a more fine-grained flush of the TLB. Signed-off-by: Alexandre Ghiti Reviewed-by: Andrew Jones --- arch/riscv/include/asm/tlb.h | 8 +++++++- arch/riscv/include/asm/tlbflush.h | 3 +++ arch/riscv/mm/tlbflush.c | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/tlb.h b/arch/riscv/include/asm/tlb.h index 120bcf2ed8a8..1eb5682b2af6 100644 --- a/arch/riscv/include/asm/tlb.h +++ b/arch/riscv/include/asm/tlb.h @@ -15,7 +15,13 @@ static void tlb_flush(struct mmu_gather *tlb); static inline void tlb_flush(struct mmu_gather *tlb) { - flush_tlb_mm(tlb->mm); +#ifdef CONFIG_MMU + if (tlb->fullmm || tlb->need_flush_all) + flush_tlb_mm(tlb->mm); + else + flush_tlb_mm_range(tlb->mm, tlb->start, tlb->end, + tlb_get_unmap_size(tlb)); +#endif } #endif /* _ASM_RISCV_TLB_H */ diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h index a09196f8de68..f5c4fb0ae642 100644 --- a/arch/riscv/include/asm/tlbflush.h +++ b/arch/riscv/include/asm/tlbflush.h @@ -32,6 +32,8 @@ static inline void local_flush_tlb_page(unsigned long addr) #if defined(CONFIG_SMP) && defined(CONFIG_MMU) void flush_tlb_all(void); void flush_tlb_mm(struct mm_struct *mm); +void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start, + unsigned long end, unsigned int page_size); 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); @@ -52,6 +54,7 @@ static inline void flush_tlb_range(struct vm_area_struct *vma, } #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 */ diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c index 77be59aadc73..fa03289853d8 100644 --- a/arch/riscv/mm/tlbflush.c +++ b/arch/riscv/mm/tlbflush.c @@ -132,6 +132,13 @@ void flush_tlb_mm(struct mm_struct *mm) __flush_tlb_range(mm, 0, -1, PAGE_SIZE); } +void flush_tlb_mm_range(struct mm_struct *mm, + unsigned long start, unsigned long end, + unsigned int page_size) +{ + __flush_tlb_range(mm, start, end - start, page_size); +} + void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr) { __flush_tlb_range(vma->vm_mm, addr, PAGE_SIZE, PAGE_SIZE); From patchwork Thu Jul 27 18:55:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13330598 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8880FC001DC for ; Thu, 27 Jul 2023 18:58:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=46kCecVPzSovFYkLRJWR0z60FRtmU10YFGHj8fBpF2E=; b=pEx1rjCm4c4EZv 7syVMKB08X+cWoN/m2bXALlRINcob92h4k1GwQFbsgZvvCKNi8pAUHB6SF55N248PYaZx1VdXoXGf /yaSH1H+L4MaxSDk3FDULPTNKUENAG05Ayd3hydaNw+kN5chax6vAi+zN46Z6yKjiU7ivPYnNBLpL UgO8LJlWZBGC5/f7+a9cgPG47L3wpKxVkQmeelZWCR2cinRwLbNKmUOvHtYdW1YNEVh5WzxMvSc8L S3GlhovnUbi/ODCsd5OgMUkXsSEW67h0unAiS+HyGddS2LRc68kSNhs+kd4SdyqIFZ+MNgnLbb0uX NkG9uHzO8Bl/BKUbo6Tw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qP6Bb-000EvN-0a; Thu, 27 Jul 2023 18:58:07 +0000 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qP6BY-000Eui-2L for linux-riscv@lists.infradead.org; Thu, 27 Jul 2023 18:58:05 +0000 Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-3fbea14700bso13225065e9.3 for ; Thu, 27 Jul 2023 11:58:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1690484283; x=1691089083; 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=FofimiyN+7gyzGEdPyrBP2skR/4a1H3RZbKcHwnJwCU=; b=KnDd+Vx9BzShpCRzLQ631/OTf/QgBq8wCBP80Yw1l/syTWuVZd1mFQHnOFb5n/3cyE TukhFMSWwWymirfBGHC3p4wuay/d0DWPZyl+78CJlng2+dz494c+Eglij+K8qqKQf4hv 4/E+Yc7aIpJ5620zW+lx0o7XJTJtyN+voY8PiGKvUEvR8YD0spKu/4+n762sANqG9nlA JGGxpE/z8ULSbOmkC4CA3cHyh0dj8cWRd0GL1Mq0lF97VyP+Af3o43rlpnU8SYmk3Yki Pg0JAmuERUhqze5Ri32pwH1Xujl7bJXT0s3JtHSJ6deEL2dq525vEra8wG4vcpq+deNd BTNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690484283; x=1691089083; 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=FofimiyN+7gyzGEdPyrBP2skR/4a1H3RZbKcHwnJwCU=; b=bZnt9NG08ZTsLxvGIc0YlLshpM0Db19Dr6EfDVWfMm6Mdtor+R83i3nTo3fMizbHtL 7xqXzjlqR1ydzA7a9K20BKaHs5W6GdalGoaDvYuUOgWDcZ6341rBXj/PmuT04+ewTIBj L9CjSFc+d8QC1c8ipwxAZRi+TLB8OccQGZ+seiByeis3sO5xekjFD57Bp8BAOKF+vfwF dIdp4BAQO0OkSyXOX+wzkc4JQyl9H2Lpr44xhhEffvbXbuDFFknal2aAXQyw7HucErL8 jC2tmzAykkTA/jUODew2TzEeeqdwbLh4odpEc5n3tFJ67il6xstJooOdnCY0N/cATypC 2gWA== X-Gm-Message-State: ABy/qLZqhBOILqDr9Jgf57zppv/lAMdsfqrrOCqBehKocwLjH4bB0Ox5 XiyOCc0Fxk97JN+0jluHQFgFbg== X-Google-Smtp-Source: APBJJlE5yuP6dOyuWEfbshqESX2eaPb5FXyTM6wUkhll2KdkxuRc6Y2JvvBqcbIkk0EYV+e5ssUWbw== X-Received: by 2002:a05:600c:3652:b0:3fa:934c:8350 with SMTP id y18-20020a05600c365200b003fa934c8350mr2269763wmq.27.1690484283337; Thu, 27 Jul 2023 11:58:03 -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 a1-20020a056000050100b003141f96ed36sm2840938wrf.0.2023.07.27.11.58.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jul 2023 11:58:03 -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 2/4] riscv: Improve flush_tlb_range() for hugetlb pages Date: Thu, 27 Jul 2023 20:55:51 +0200 Message-Id: <20230727185553.980262-3-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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230727_115804_764281_7A3084E0 X-CRM114-Status: GOOD ( 11.71 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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. Note that THPs are directly handled by flush_pmd_tlb_range(). Signed-off-by: Alexandre Ghiti Reviewed-by: Andrew Jones --- arch/riscv/mm/tlbflush.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c index fa03289853d8..3e4acef1f6bc 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,14 @@ 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_shift; + + stride_shift = is_vm_hugetlb_page(vma) ? + huge_page_shift(hstate_vma(vma)) : + PAGE_SHIFT; + + __flush_tlb_range(vma->vm_mm, + start, end - start, 1 << stride_shift); } #ifdef CONFIG_TRANSPARENT_HUGEPAGE void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start, From patchwork Thu Jul 27 18:55:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13330599 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 64EB6C04A94 for ; Thu, 27 Jul 2023 18:59:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Nf80ksYXnvNCBktgK4vWXGukX5x0+wihZBeBIZ9t/S8=; b=XL1odqgXGd9b7Z C494VoflCp69QYCH+UXitlDEHI23j06CZsaZobW80GL8/ZrTyEBl/taogdxCHpFolr2spXvVj9a7+ +vK+nfG0gSef+NWU8qduJzE/Qhzty445Ea3CMNQFPxZrwn1GWDdSz43cia2wKNl7XsNyTBPk8Ehjn wPfdmkEJu56IbG4kOaFiF75y6AmGkg+eFqHtrck2mc/YOVClVB/VbxD1qFodkImL3oanVilnVTFIT p53eKKD9wkdw5W17bs2xiVwFd6INs/Mfo48Rwgm21/iBjmWF4U25mw433qaff/I4Rakp42zD0hqcE Fu0x8wfyzBCS9hkvNbjQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qP6Ce-000F6a-1W; Thu, 27 Jul 2023 18:59:12 +0000 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qP6Cb-000F4l-0C for linux-riscv@lists.infradead.org; Thu, 27 Jul 2023 18:59:10 +0000 Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-3fbc5d5746cso14148005e9.2 for ; Thu, 27 Jul 2023 11:59:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1690484344; x=1691089144; 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=EDamHVk4ujUG+CHbAQWQotzld5TgznJ3XAxjQYSqNSU=; b=IdGa66UtIIFbYYRT34X7nXZCEdzcXTUXP42pHU+fRDakhGzxioo4R0X4pMQHNU3UvT cb6zYYiY1GSCrHL23POq5GqGsmx16xdh+ORbhy0jbLlh3508NDq2BU8MNyVptXqxHU3m z6Huh+dgLHgTmMh6/GFFtZgMKDfLTF/lLUhuu6lFFzZ5FfNz2M+UalL7Nxy2wcTt4Urm vHdUKMcDMqL0Rjs+nJEbZgdzu00ikS2T1OAVYH9IyrUU52fcpQTId9PDN+EBqrinVVD3 YmbFuFktcTPCtmoQ7c19/s3iT4UVf0BCy+pzRa9E9o/mm8ujQqggxhdzJL4AmSxTKYKG +0qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690484344; x=1691089144; 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=EDamHVk4ujUG+CHbAQWQotzld5TgznJ3XAxjQYSqNSU=; b=bW/yjsfPO+WDs6dnJXcR22N29/TnU1eEBHaNA2ejcW/a382pNf3OlpK6zaHlvXtcjM OB6+++AuH8P4h7yvDS3R3Oa2uQacoId1VZ+meDDuBSnziQpvFlBpb5VMs7SsRq3xKc7c T7Kqoh/4McEnBwr48crcvYGTAtvuWYM0uVEffUTnqpZUdpQLXBP2iOCLqZRVVF1alQy/ vLIwtFgo1ZCNYZOR5ZIhRlOYApgHytc+t3ZfWG/Nvbp//aZ08R4RWAxm8XDVzGu/cFC1 s6BOZQEziD44B+wlgvHNKxmSI3fZN1X5u3k13rnpywAiuL01pPXIGiPBxf/vyf4PMpW7 mQVw== X-Gm-Message-State: ABy/qLZhbmK/PsLdKUcsqxlw3yLbPxp6TK2JoIfrIms+oI8MNCXO/CZl Ephqp9vn40pAXkD+QRdxp4ArsA== X-Google-Smtp-Source: APBJJlHhcGHjdWFgZg+N+yKc2Y2lhGlFYBwO6VQdAW4Af4g86WHeGB5c7zLJbHluWyguraWCOhnK6A== X-Received: by 2002:a05:600c:aca:b0:3fb:a937:6024 with SMTP id c10-20020a05600c0aca00b003fba9376024mr2398881wmr.29.1690484344310; Thu, 27 Jul 2023 11:59:04 -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 2-20020a05600c22c200b003fa96620b23sm5376517wmg.12.2023.07.27.11.59.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jul 2023 11:59:04 -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 3/4] riscv: Make __flush_tlb_range() loop over pte instead of flushing the whole tlb Date: Thu, 27 Jul 2023 20:55:52 +0200 Message-Id: <20230727185553.980262-4-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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230727_115909_097156_62C82367 X-CRM114-Status: GOOD ( 15.79 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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. This threshold is microarchitecture dependent and can/should be overwritten by vendors. Co-developed-by: Mayuresh Chitale Signed-off-by: Mayuresh Chitale Signed-off-by: Alexandre Ghiti Reviewed-by: Andrew Jones --- arch/riscv/mm/tlbflush.c | 41 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c index 3e4acef1f6bc..8017d2130e27 100644 --- a/arch/riscv/mm/tlbflush.c +++ b/arch/riscv/mm/tlbflush.c @@ -24,13 +24,48 @@ static inline void local_flush_tlb_page_asid(unsigned long addr, : "memory"); } +/* + * Flush entire TLB if number of entries to be flushed is greater + * than the threshold below. Platforms may override the threshold + * value based on marchid, mvendorid, and mimpid. + */ +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) +{ + u16 nr_ptes_in_range = DIV_ROUND_UP(size, stride); + int i; + + if (nr_ptes_in_range > tlb_flush_all_threshold) { + if (asid != -1) + local_flush_tlb_all_asid(asid); + else + local_flush_tlb_all(); + return; + } + + for (i = 0; i < nr_ptes_in_range; ++i) { + if (asid != -1) + local_flush_tlb_page_asid(start, asid); + else + local_flush_tlb_page(start); + start += stride; + } +} + static inline void local_flush_tlb_range(unsigned long start, unsigned long size, unsigned long stride) { if (size <= stride) local_flush_tlb_page(start); - else + else if (size == (unsigned long)-1) local_flush_tlb_all(); + else + local_flush_tlb_range_threshold_asid(start, size, stride, -1); + } static inline void local_flush_tlb_range_asid(unsigned long start, @@ -38,8 +73,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 == (unsigned long)-1) 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) 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: 13330600 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0CF46C001DC for ; Thu, 27 Jul 2023 19:00:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=aWSu79iIBQkPb8A6nda9tKiJ9PU/kAT1Yo0876etKrQ=; b=op856s+9W0qQ9x tyHZ/il4/6RhzRvLUpt9C+WhQaagJoW1Ukpt5t1OyxxkwBj0IMoiNtmmP1MnpliYoS5tUqm5sd8Qc HZOFHVCzQ+xvkpt/wGaRQef8NnFXM8HtM3rVINLc/AoKZ3H/VklDOfoz97N8oUUzwdOPih3njZG7I dhTl9QwrxBXLbxCq/wxAU/CvK/F98hlBYw0kAloQlH2c1U/i0qGNZQdZ8JH5bUdQN9IxhJsCgWnzN vugjS+PoGrZM4bv82TQcSre2ny3iaTUPt6Wh3DVbzV58AWHEY7Rn5jIX1G/flvoutPf3B8Jcx7T6M FBZBqR7v7Y5IM31QDCig==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qP6Db-000FKh-2K; Thu, 27 Jul 2023 19:00:11 +0000 Received: from mail-lj1-x231.google.com ([2a00:1450:4864:20::231]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qP6DZ-000FHK-0r for linux-riscv@lists.infradead.org; Thu, 27 Jul 2023 19:00:10 +0000 Received: by mail-lj1-x231.google.com with SMTP id 38308e7fff4ca-2b9b9f0387dso20697391fa.0 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=k1tU2ZO5DG7JWOVnULekzTSpQxyxc7unUbuA+cLKacnlSXn2F+XfJ7kyLC+s1EtKGC fisinvG1F7Ojb0iRLQImbQeDJscbDe8q6e+h9g7jPQQam46N2LA6Qgz+L/WtdLNgEPaM 9TzDxZ2T6+q6/3T2OPMXJ09WZsJH0tLqWTm4XBK+SSmkp8Hg/Ih+PFru4jVcywpRWzP+ /kNTUNERJgOnkI/UCYL77mBR5YzLsb1VmhrEf9BrIgP6krADFZHV9j4aHbupkFu8uX3R yiuZsAd1cUticmNg7vuGgGkm3Hkf4iRSIcHcrot9ZPYQFAvtftgg+61HZjKwvFOtDmX8 uuXQ== X-Gm-Message-State: ABy/qLbGQREF8tO0QUKG817VJYuXqXPXxAyQC4OtRLhzUtoBTlpt29DY xiEQyrcszJx0L/AAgx0uBngoJg== 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230727_120009_305072_5D712BB5 X-CRM114-Status: GOOD ( 17.65 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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)