From patchwork Tue Aug 1 08:53:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13335923 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 454C8EB64DD for ; Tue, 1 Aug 2023 08:55:21 +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=NEqc7/+Z6O7av2s2Vk7ZyhtpHl3v6Pzqdyi59A60RYk=; b=unW3dw0fjkFh99 KZ0qwVXTLfsNvNrVFY+U5lnZaZ/Pwd1Rc7IRq4EMgGrE0m9QfD3xqZXjkla+w2SMoQf5mmsEuW/Rh 205Bxbn/k5GIMOIR7Kd6WmL3H9oRlivAfH4IRbMyfWE8SjZals3XBMYdbK/9fEneCdoEtyA8xSRyF tHEUiw8EV06c0cVDZikFtvX83UrgKAOq8bVeGD5FeRYRq+iyYFPn4XHFNhrzxI1hm9LNyZi/2wZNA WQPgGUA0NidUn2Yr4xNOoIzZr/hG1WXgrHpvrde6gZ8jVbCgOmaYgy6nVMeFzWmWqvXHG+CBEAdqe yQtm2S8eW1x4906Q5Oow==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qQl9x-000hEj-2O; Tue, 01 Aug 2023 08:55:17 +0000 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qQl9u-000h9H-0T for linux-riscv@lists.infradead.org; Tue, 01 Aug 2023 08:55:15 +0000 Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-314172bac25so4781850f8f.3 for ; Tue, 01 Aug 2023 01:55:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1690880107; x=1691484907; 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=VJvQhuwass6T7QyQKJFHTMKHPeCPiB8wQ+ZaCt6WMq4=; b=Bpl0+hszwA8UQR0niUcF7oQehlmgEjs5cHo4HxCIX40QXmvTR8LBEK7tQ+cI0LB3ay rxJOT3sKokH5JRnOmREiUvCfxX57njbV54QplHg6i6M4zYzPTGgb5q/+1VpctU4MpRch 2LyVMmWtMmogjAz6kCV0lm+c04Jpvg/Gg9NtpVwOclOafX1Kzkd5tX9nP0yk93e9KsXw ZvlG5WTrZQrtnkYLM60wCSK51DxKXb8RPjLidqJQRRr7d92QPdT10NppoNWWqJeyOPUC Py/mV7zxhLphurocEHr98As//5wdmGubwQw9Noeeewke4b5x/ZM2cUOii4fja8jMV1qz OdPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690880107; x=1691484907; 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=VJvQhuwass6T7QyQKJFHTMKHPeCPiB8wQ+ZaCt6WMq4=; b=CwpYMyCORw6I/DNwyjVdcvQc899dpLAm0Yu9CWyArJKxECzk9WZG1EXT3Sqvbwplhs JsDoaKcrpf0LahTeCWcbfywfe66ExhDMbZc5/DNQVh/xKjol0NTpX5blx6rPrBZTdMRv Vwpsz0N8cMZE469FXm0hm5vputYRTTGWWYmbqFq9d8iQV+5iXxb9tkzmB5lWIwf6efcH joeWAKMxqcTftRwUafoTAZtiGU6V7WfV+UDE0TGU5ULfT0r0RstOk+S7e4DnVcRn6PtW OPn8g99VWiG/UGklGEKzk5KHmUojhgPuULs9JKwCcRStDkGSCZbzC9FIw+JvSfETsoEZ 4j1Q== X-Gm-Message-State: ABy/qLZTcm89o9beSHdUwuBvmGNuQFtzeScQnZ16cdUuZk85oLh4lnFx Il3KIP3p1Y5styCPeVNd0uvrDA== X-Google-Smtp-Source: APBJJlHOJJYLrg+ra70JvNHoDNhW65vTt6zyHcHStm5ugNLuPe77B6588sTpV0o89m8oxwcr4IjNuw== X-Received: by 2002:a5d:4909:0:b0:311:1aee:4e1d with SMTP id x9-20020a5d4909000000b003111aee4e1dmr1817877wrq.33.1690880107294; Tue, 01 Aug 2023 01:55:07 -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 h18-20020adff192000000b003113ed02080sm15296992wro.95.2023.08.01.01.55.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 01:55:06 -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 1/4] riscv: Improve flush_tlb() Date: Tue, 1 Aug 2023 10:53:59 +0200 Message-Id: <20230801085402.1168351-2-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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230801_015514_192569_1BC43ACE X-CRM114-Status: GOOD ( 13.28 ) 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 Tue Aug 1 08:54:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13335924 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 028F0EB64DD for ; Tue, 1 Aug 2023 08:56:20 +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=tJHKTS7N85ZUf9kXUWn7nhYK4zwgNMPr66+s6fmkC4g=; b=PIvQnyu7Dq5eLq d23JN7v4dVyJOSre/9QslrJEcvntTt/oy3xfovhoXYY2URZCGtdmVOSYoLwZ0c/CjRUrIor1jAIq7 Bz7W5AtOCIrVsfuHXXCJ+hG+vkaNfsHMUNcQFkrampav6GBEKxTWXuKW8bEO8eoWPwPk3Uw28nq9c r11pr9yC6RG82pPz2DHmZMoOHc8OYVh3UoSQrVk6RbEzdj6TcAbHwqPGt+iDscjy7x66aU6JI6R8v hlyjTN6BYpGFQgHB7dFY5oizpl54lag6w/1uTk2z+P4CR1LdQEYk4meW17ZUIILwfzOItR02jZYVH CnuM2L6FUsMzKm7gKcUA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qQlAq-000hRK-2O; Tue, 01 Aug 2023 08:56:12 +0000 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qQlAo-000hQH-24 for linux-riscv@lists.infradead.org; Tue, 01 Aug 2023 08:56:11 +0000 Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-3fbd33a57b6so59866575e9.2 for ; Tue, 01 Aug 2023 01:56:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1690880168; x=1691484968; 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=YdYxZtsdohXkJiEqvE1Q59ExwQkgmM2r74atDceIuW4=; b=P1QobXoccsE3+9czY0EpajvoT8IDASIi5MHnVOCPkE2PMZjb4NqJkp3pvl6huP8YRq inBiQsncpHkOkganYv8SrZAOd3CtSTRw0CEuM5zl62vwXj74EYJEKz1rWmjPNAGUwXcZ 0T//g3rQxb50UzHNokVzEi/3Xp6BVR9XQED7mluE4W1zuOqwh31OFZ6aHgZa9eXfLMxk Ybdk6zmtonm5+VeE6xBp3pLoAkOzJzfiBcOkz9WrXEMNt9py7w/QriiUEhWdVim5+GYl oNYLzRRYgjWZYYev+98Hpz+c7VOxxXWdwErfKirkGLaiOfoY7c7Toqiek1j1aM9FuYjR xmww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690880168; x=1691484968; 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=YdYxZtsdohXkJiEqvE1Q59ExwQkgmM2r74atDceIuW4=; b=RGdsCkUBxUsUsCQcJTN87CEQms6SE+sPQ1G2Q/mahrRbnGPakvoKReADGSGt715PJ/ qCO5e6tx7hqt3cvQv3dOaxHwyA5ydojIiIGXVh88ThdWiMn/6bK0xT8QIR5QBATbdYXr hVu8iVWWScYFHi7gDVWDO7navksuhaVuJbrauZvsxcrbjdWbeWJhUqZf7m3eqIe7/oQa ABZlg9N5Sa4lFdV4RL8nUbO+b0J8z7kARpKVQiVlHi+TSm06hpovmws72HyAoiBEhxmZ PLTod/8n0PNPOBA/MpTd0qOzxHvaxOGnNpkm9T4/SVIoRL7KpBi55Hl4yKnZ0S4Mtl9i PYdA== X-Gm-Message-State: ABy/qLauhHu1Af1vkjEHCcrkIYo5ZziUNjcWC5fdw/JtejyQnEryW7MX G/SsU0v/bJEtmp88p+4qF9bvrA== X-Google-Smtp-Source: APBJJlG/YdSyO9mZdobMpVoP2KrhKuvsBSBkyDxEF7tvWdeyE2ucJPc2rpUQPp8XKp43TrymC0aSMA== X-Received: by 2002:a7b:c4c5:0:b0:3fe:f99:1ba with SMTP id g5-20020a7bc4c5000000b003fe0f9901bamr1969783wmk.2.1690880168420; Tue, 01 Aug 2023 01:56:08 -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 l12-20020a7bc44c000000b003fe215e4492sm4763826wmi.4.2023.08.01.01.56.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 01:56:08 -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 2/4] riscv: Improve flush_tlb_range() for hugetlb pages Date: Tue, 1 Aug 2023 10:54:00 +0200 Message-Id: <20230801085402.1168351-3-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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230801_015610_682672_3518CED9 X-CRM114-Status: GOOD ( 12.37 ) 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 | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c index fa03289853d8..d883df0dee4a 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,13 @@ 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; + + stride_size = is_vm_hugetlb_page(vma) ? + huge_page_size(hstate_vma(vma)) : + PAGE_SIZE; + + __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, From patchwork Tue Aug 1 08:54:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13335929 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 7272EC001DF for ; Tue, 1 Aug 2023 08:57:21 +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=uMMiHsKx1FR6m2z/q26uGiksckSBEEUqsgU9VC6SrHo=; b=XZHuxQNcNjveyk sB42gB2uBfpUBXyDyEKb7V3rwjCiAdAqoUgYsadorExFyC1oN3Mclw/uMpD0mBk5MpzFQiZQQt7ON 8xUzUlLOK1m+B5S3SMaywYjN3bw+Q0wy8lfTzQqDZ9mZFVMG1Y9lUwHQO0OSsFETHMoCENxGoxk1p VK3MTV1VswBXG968MIjkXXKZU8pSbPQ/DdjOt3QInMVeAZ2BaHPgLM33Lv+SDsbndN1O8AVjEvibz sIGcf6+afU2Jv66Td/ELvLd5yU8/+YyJRI+wUG0aQ16HXkEfgq1SYktuNRuUoX3FYET0cRNJ3A/cm +zLMaDdsgRqonCzY/65A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qQlBq-000hbB-37; Tue, 01 Aug 2023 08:57:14 +0000 Received: from mail-lj1-x229.google.com ([2a00:1450:4864:20::229]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qQlBn-000hZe-2c for linux-riscv@lists.infradead.org; Tue, 01 Aug 2023 08:57:13 +0000 Received: by mail-lj1-x229.google.com with SMTP id 38308e7fff4ca-2b9f0b7af65so22830461fa.1 for ; Tue, 01 Aug 2023 01:57:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1690880229; x=1691485029; 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=C8YprRlaP4W+8yj964kuFlSBdOjyOpODn6Kzt6jcAnQ=; b=qdlxfR2ULJ112i9MarbF8BcXMSwKtF7ipH3Z9ZNh4A1UrVCNFEubO08E+XypP1km40 BIOw6IYnj6AuKJ5Ew5K2k8wdyrUOkb71v8AegiQ+nuqs2nVtNOMNaQypq6vvnPowPpkZ 7Qg3miGQ6+x4e6LXFDodFyE5gWLzjaQHx8E1Z1Yv5J1iMlnH4lG2/0XWFrSIHswKYzyt efFuQOiJLROe9KB2N3hD8VR/lvvtDaF5plKwpbZAgbSmjQSTOUgsKYuP07QH5gjYtlT7 vTiVHiET4IzE53WDZJ0ZMMONmiUAraIM9BVFchpD8Pilr9UwNzxd6uikBobHNhH/wO8t h/dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690880229; x=1691485029; 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=C8YprRlaP4W+8yj964kuFlSBdOjyOpODn6Kzt6jcAnQ=; b=agmxI2aK5gDVbrmx4S1U40wfAgwwv3nBap+KGaHSqtGsD/zyqJdGQqQE0axek67zP/ SeePCNSl35dvFuVAFWkfzxTUMtaGrOCZ6i152KVoCV7sK8ndfOjfi27QyFQagq2fsYD2 ppRTpxJh9rNsb5JzhHUA5Dc9jWi4kOce8mDj9SbSHUb8mJaozggrB8N3HiQ4ixhsrC2U XNzUwOX1ZEfML0WjKBLtX+YuCpvehom0FEot/aCWUXcZbsc29nkBeXjsUwKc1ifc3VDS LBCxyfv8TzMHvBWfCC2GY+kY0eOwfOviXXwC/H62pJberQlfpZyWBBjmOectCjlKh4VR qpnA== X-Gm-Message-State: ABy/qLaRHuvMCxoB5hrYxgDacat7Gq7sa9+MUssq4e13H60T07cpfHcU CdWVsQ1BhLQdRWGTk0OfhwQJtQ== X-Google-Smtp-Source: APBJJlGXXGbZRFGts7ZT7QQ6Cb6DdfW/OsBud3Kp4VAMMf97raeOffyeVJ99hrx1DJ2As1NVk0KZbQ== X-Received: by 2002:a2e:9054:0:b0:2b6:e625:ba55 with SMTP id n20-20020a2e9054000000b002b6e625ba55mr2048866ljg.41.1690880229621; Tue, 01 Aug 2023 01:57:09 -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 l27-20020a05600c1d1b00b003fe1b3e0852sm2908793wms.0.2023.08.01.01.57.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 01:57:09 -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 3/4] riscv: Make __flush_tlb_range() loop over pte instead of flushing the whole tlb Date: Tue, 1 Aug 2023 10:54:01 +0200 Message-Id: <20230801085402.1168351-4-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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230801_015711_856955_27FFB531 X-CRM114-Status: GOOD ( 17.61 ) 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. Co-developed-by: Mayuresh Chitale Signed-off-by: Mayuresh Chitale Signed-off-by: Alexandre Ghiti Reviewed-by: Andrew Jones --- arch/riscv/mm/tlbflush.c | 48 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c index d883df0dee4a..0c955c474f3a 100644 --- a/arch/riscv/mm/tlbflush.c +++ b/arch/riscv/mm/tlbflush.c @@ -7,6 +7,9 @@ #include #include +#define FLUSH_TLB_MAX_SIZE ((unsigned long)-1) +#define FLUSH_TLB_NO_ASID ((unsigned long)-1) + static inline void local_flush_tlb_all_asid(unsigned long asid) { __asm__ __volatile__ ("sfence.vma x0, %0" @@ -24,13 +27,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. + */ +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 != FLUSH_TLB_NO_ASID) + local_flush_tlb_all_asid(asid); + else + local_flush_tlb_all(); + return; + } + + for (i = 0; i < nr_ptes_in_range; ++i) { + if (asid != FLUSH_TLB_NO_ASID) + 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 == FLUSH_TLB_MAX_SIZE) local_flush_tlb_all(); + else + local_flush_tlb_range_threshold_asid(start, size, stride, + FLUSH_TLB_NO_ASID); + } static inline void local_flush_tlb_range_asid(unsigned long start, @@ -38,8 +76,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 +92,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(NULL, 0, FLUSH_TLB_MAX_SIZE); } struct flush_tlb_range_data { @@ -130,7 +170,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, 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: 13335930 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 73D12EB64DD for ; Tue, 1 Aug 2023 08:58:19 +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=B/VJ6TafbUVnVb9iqU/AxRgCXhaIQxfKK+YvXHd3EYE=; b=sXf1LG8uZ/sXAZ tsfD7eenuOB2jH5AStbleLHRtQ62Y+8iGOxaZJapjeM2qr3u11JI11ZByhbhx6jRQfCOwMA+YFrvy 3NXbVCQ1VkGOI+vhH2YOX2FPg5CH2eLpEaXOFUd/mpyqFZ7PX2Ipi0TeIZgUhjhq3Cq0BO/8pV+92 JHiqwbiQH3veqKaeJmTL/rLJHSCGfPBptticjBB/0lq83a8l9iA5xuJkD0lCMnmNnROyBGQFhchQt amdD4aKN3qjEKcejutTq9jE7/9ALTmWbgNVA8XwJvmtj2qVOAMvi2Gha5MoxjlR7rlT9tY5vXnzzC FZVIBRI+Wno7WCMmQlaQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qQlCq-000hme-0H; Tue, 01 Aug 2023 08:58:16 +0000 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qQlCn-000hlS-0B for linux-riscv@lists.infradead.org; Tue, 01 Aug 2023 08:58:14 +0000 Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-3fe2bc27029so1143465e9.3 for ; Tue, 01 Aug 2023 01:58:12 -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=AEmGA5oPp4a8fMomTxSXPdh+ghtvg0f5uIV1mMpE8rFKLfYoyKYGL8Q8mkTWaYXjXt 81z/4IRclaJGWfyeQEOqSk3nFkuDzkA5lozPQ+xiTBwQArTS/IviJfZzPAOolvPXRc9n 0E8tBLvNWnEHLU3sPsyrywNKKAQosidQhTRxJ9yEY+DO3/FEYuollGE7GL6z5IugnSnN WeTDXFUchnZ94hMvgNu+IA28RI/0hi57qjLGbn+Saw+UYUy0shhUQ9Y+eXIoTj0ULsSH T1V1I1rnMHKMNCitljqjmHsrR3g7Wk80v+NYfLDuJdhMzbVJ8+sy+RgcCK4261cNZPMr Cf0w== X-Gm-Message-State: ABy/qLZrnedXqHE2on+aFVNsK2EefeUuds8XromEQ31byUC2CJ+c1WAe /ZLYM8JrE1u9lSdxyDQ4Pwxlpw== 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230801_015813_099022_0BE0C05B X-CRM114-Status: GOOD ( 18.39 ) 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 | 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)