From patchwork Mon Sep 11 13:12:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13379316 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E3F78EEB57D for ; Mon, 11 Sep 2023 13:13:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5110C6B0293; Mon, 11 Sep 2023 09:13:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4C1C46B0294; Mon, 11 Sep 2023 09:13:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 38B0C6B0295; Mon, 11 Sep 2023 09:13:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 266E86B0293 for ; Mon, 11 Sep 2023 09:13:32 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C0EA740A4B for ; Mon, 11 Sep 2023 13:13:31 +0000 (UTC) X-FDA: 81224358222.30.CAF9A7D Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) by imf04.hostedemail.com (Postfix) with ESMTP id D4A2E4002E for ; Mon, 11 Sep 2023 13:13:29 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=CBoEHvze; dmarc=none; spf=pass (imf04.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.128.43 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694438010; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=VJvQhuwass6T7QyQKJFHTMKHPeCPiB8wQ+ZaCt6WMq4=; b=MLFv/XpdSf51yMBcvPm8GFcP7OQgqJIS04lP30LX3UyfKbFpMdRqLECnAgm8F+nyp09LqS IJD1pBnACD0H8pOXN94LWLBZLysvX72yy8EJHh+npKSWjWbV18k3ga80koNePuHdlRxUKL xxam9Z1t4VsOS2bhyUAKk4piVObDrqE= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=CBoEHvze; dmarc=none; spf=pass (imf04.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.128.43 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694438010; a=rsa-sha256; cv=none; b=eyEgCUlM0V708pcqGYPkI0lFDgsQD8w0zScYSsmIbXCZgY/xlorw0yS4HOZavfejZRsaZs Cmvadjp6W1btWnALQUh8oRUoi9+6OjUMy4dKd4jM7q3GNBDp6YedONQHGT/xG9CBThZzCF Xt3f/D8hJgxqQnGvzoIdMkyQt5qPY84= Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-400a087b0bfso47216695e9.2 for ; Mon, 11 Sep 2023 06:13:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1694438008; x=1695042808; darn=kvack.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=VJvQhuwass6T7QyQKJFHTMKHPeCPiB8wQ+ZaCt6WMq4=; b=CBoEHvzeHX10bKQkPqtjxM/o2Q+ljQDKQxYAs9wt73hDtt9v3HdD4Z73yqlbPgu+MH DoTOJDgTsh+Nj4ZXh7Z7crWy2pc6G1SHqmddak3U9xohyhET4mfupdIxwY50EAKQvjXr egNbzzPrLTn96y/6wb4fGQmCTslhzqZNNdwKLrIbGazNCzTq5ABvb7gXnXhcAV1qXber ON7j7U2QzcshrF62C0Wr6JXM1mvoYexJR/owiayj4N2Yk37NhYRC5s3QRj9Xy4IcplZ1 QnAv3CWjwjZzzyUDdpD8jqCjGSV8x8dnUWmscBWSIXrmzOGFiw09adEFSL1eUrRDw3Wo 2nGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694438008; x=1695042808; 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=M4ZJA53/q3dwqX1J1aVFKDUgIcLHaqr2HaEXC1XVyGTre0KVsf29nuhRNVslilfRZt S5RKzEEEiU0ZctVeOeQfwOhCOlt2+97qJz4mB1PvHDU4ixva1hP69ehfNKFZEL3cPrN6 /YGVjXQ/Pld8lwDTWx6Db+BFqi/ypryRqa0J/VcdVjyVpRiZAN5l2P0r24VOX65cp4zO M801boWstg7ztZOOnRfAPZ99NExQ6Q77zpXtDBHmcdP0l5iEmwX0ttC5DycfTkcSvhDK 5NprBCaYIjkdGpEGklFKp6s4jjSKtB6jDsuI3FAizv0pPXXQKRAsUUIl2C11tkKaXpcV yQWA== X-Gm-Message-State: AOJu0YyV6+ZrRGv1TEop5ZcDfB4X6UUO7JmiLDobqMG2AbRYNvRXPN0q 1u6RR7Xmz/I/NWrFLW6FSnNnFg== X-Google-Smtp-Source: AGHT+IHDNb5CqLim35ose2q7WmJ+9tXpgSAwI6QOLAumXk8SMHKSu15qIwBWXUEtVo37x5UyrTFz2g== X-Received: by 2002:a05:6000:118f:b0:317:6d9d:1250 with SMTP id g15-20020a056000118f00b003176d9d1250mr7674314wrx.61.1694438008262; Mon, 11 Sep 2023 06:13:28 -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 b14-20020a5d550e000000b0031f34a395e7sm10072174wrv.45.2023.09.11.06.13.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 06:13:27 -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 Subject: [PATCH v4 1/4] riscv: Improve flush_tlb() Date: Mon, 11 Sep 2023 15:12:21 +0200 Message-Id: <20230911131224.61924-2-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230911131224.61924-1-alexghiti@rivosinc.com> References: <20230911131224.61924-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: D4A2E4002E X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: hgo6ipyjyd1t3cgxxpfybs1qrrs4nhsa X-HE-Tag: 1694438009-622289 X-HE-Meta: U2FsdGVkX198mHWjmLKgku2tqBHA9gXGBwXun9HenjuXWWYc462GME5j4ukz8pMs2Y2kzwOmOBxXlb6efXUh9PEr06uKxb0bH3x1kTArpfEEq97kvaYUiMQYISZUyT7ySKgQ2i5dNBFPso8NKcU4WOWZrKxCfPbam3vcOcobjgBu5RUQZFu3AE+SiiHURq0gJ5IeKKBi60aD54SEEVBVgmaxy6g9pa/z/gpl5Uu2RfbtvqEx+5Xcc6LGQon+P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61AUJiwYlVmr+SlFmr3H2kycIfB6ywJJNhNxn1OaVwDWK0Lzq4B3K6x6AmWewZdEfdhZt7jMmiggD7Ors+QDiIdzVFVAeJiBO0l7lJmAAmb01XmDt2NU2rR6N1VuVwpDFl51FGTpCwzoIC0MaTZJZY6Dn8GmF4b5fNMblffQFKXeRWZ8PLF6O2aVAFahmJTZCuS5jVdk9+HaRegXuJJLt5KNWWdnLK4Ucu0fXes3nPWTvL1YZiMTBAzjiNppI1RpoD7jm78NTz+EhJJnpyPhisw5P3cyU1KjWRA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 Tested-by: Lad Prabhakar # Reviewed-by: Samuel Holland --- 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 Mon Sep 11 13:12:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13379326 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25329EE57DF for ; Mon, 11 Sep 2023 13:14:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A1B326B0074; Mon, 11 Sep 2023 09:14:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9CAD86B007E; Mon, 11 Sep 2023 09:14:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 86C3C6B0080; Mon, 11 Sep 2023 09:14:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 73FFB6B0074 for ; Mon, 11 Sep 2023 09:14:33 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 485A3B3B17 for ; Mon, 11 Sep 2023 13:14:33 +0000 (UTC) X-FDA: 81224360826.23.8E95992 Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) by imf05.hostedemail.com (Postfix) with ESMTP id 5B9C410001F for ; Mon, 11 Sep 2023 13:14:31 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=QHShZum3; spf=pass (imf05.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.42 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694438071; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jTDHLs2EOQtTQ3uvgmHng/nVHNHrVDMEJR/1UpGvkHQ=; b=G7rmEZophibnajLDnPWN1/sxq16rYZuNNjGH0w/VtB7CX/jsV9x0m59Jbe2t1ZTTa9Q4sH BExH2ZKzXM5F5/c9aMXuiMvMMtVHvPIWeMJJT18ItDGqqiNvi9jdRHRV00JhpVIItYVe2Z SFojDWlBmC2mrPpeiObSj4kB0aHBc7I= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=QHShZum3; spf=pass (imf05.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.42 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694438071; a=rsa-sha256; cv=none; b=FAtTI3SVTn8v4J6XvJj3+CLW7dh59eGrzc4OQBPPREUEeRfu00n4W83lO8trvFW2yf9x5d 1Fyv0h/vk08+v7GlvkORNBbFf6WQ9tjCpb2Xw44krQBCieeHITtTgCBUpnHavgfHOHC3U/ HbSO1hwOKn5GuoSM7Ndd3g1UifFy+0g= Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-31dcf18f9e2so4448283f8f.0 for ; Mon, 11 Sep 2023 06:14:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1694438070; x=1695042870; darn=kvack.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=jTDHLs2EOQtTQ3uvgmHng/nVHNHrVDMEJR/1UpGvkHQ=; b=QHShZum3XI/lL30ln9fhFq88dCc6q4FWrucEDTt125R6hfsmeVBRC7rwsCzvHKX8WK veNwJWr2SlKKCVsFdJmnjc890PDGQgm25ePCN9fFZlYTFG9TpNBJ02kGH0YhbU6lboE/ gsJRXXv3hVMXni94Nl7VAMPL2e8NNlrMnBlGChRHqIEF9fV9xTU5G3vkwVjFpwcP0oDq hKTxMlYlg92eEn37xEzxm1fG16jmjncHcL9MVF4TThhr4gCUncQFg49V3dfn3tMbLoCM 1q0NcntLLBg3cdd3J0Q6+Cco410a5x0PmEaqstvSRTpkqheZIV8A4SxrA5dKO35k38g0 AwvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694438070; x=1695042870; 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=jTDHLs2EOQtTQ3uvgmHng/nVHNHrVDMEJR/1UpGvkHQ=; b=i8sMiCp0giN1hB4iHsOsAkShaLraxBT0yaaTBUfYDRtSWX1BYHZK6UNgyd5QIAk+TH gY0pp1l4xQssGP+IcrMnl2s+q5fSCoT4l1/IcKCUhE0/SKz0l8fHl3wUsaoOsMMZp6zK e+hCALAszDrOsIXUhRgx6b8tg+/zvUKHeIbVmc1+ly6iW1jA8f6MwIR58PwerBYKquNY z+8kAgbnWzDWQImcCr9VfW26AIDAyfpiiHdxovmug4bqUOmXmko/c941YS5jCqdlpAA3 Yj1FYO8B0IZNiwu7v2WN0t13tAHH0lvbg8bStAxPs1RBe+ExJed3zJp3zblkbtWhmo9K DBnA== X-Gm-Message-State: AOJu0Yy6FX4pPCS8B9xKMEQ/+Bcq3cIjlwC+R1YXEyybWtq8ppaTHCjX d6N//iW6WOZTbrRAlFPmIdXXBg== X-Google-Smtp-Source: AGHT+IGYcFnpNiP+FLLZHYSCC5nHXUyn1lvxXozVLynaGkKKRW3cVE8D6L3lkbbcr2jik100ZVPpZw== X-Received: by 2002:a5d:6510:0:b0:317:ec04:ee0c with SMTP id x16-20020a5d6510000000b00317ec04ee0cmr8022431wru.47.1694438069661; Mon, 11 Sep 2023 06:14:29 -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 v11-20020a1cf70b000000b00401d8810c8bsm13230128wmh.15.2023.09.11.06.14.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 06:14:29 -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 Subject: [PATCH v4 2/4] riscv: Improve flush_tlb_range() for hugetlb pages Date: Mon, 11 Sep 2023 15:12:22 +0200 Message-Id: <20230911131224.61924-3-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230911131224.61924-1-alexghiti@rivosinc.com> References: <20230911131224.61924-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 5B9C410001F X-Rspam-User: X-Stat-Signature: rpzknaknddr3waurhhpkn9wn8paakkdq X-Rspamd-Server: rspam01 X-HE-Tag: 1694438071-809451 X-HE-Meta: U2FsdGVkX1+9xR5FSxffknD9dWcNevrFLZS4n48KCE7ILjrMuW3qVW7E9cVdh1WYjB70Yl+oivQMk2hvlTg40g7PyTzQy8/+zpdGsUyM4UiTN+q+IKF3mNG0Ag/mZ6c5fNU45HCCB3stOKKQIhtNvfgHghN9JxQ9SM5wzh6Mdezeyxd0WP4vL6cTFmHncCklukZEvr0gjK82ouHsRwKzpa0phzOSJVbnH+NRplu3WajIlNp5QVb8qRTCt4ajg0nO9AZiR4NKr3UjRK/GsUM7sT2Y+BwUceX/6hWNB/lzduAG34aGlkBm9fhWWPFhp+Ctm0X0/bxAXDzUwkxnEIMKeLJWqnaBucYea2AJOv39GsyIw0o98oLhggbuFHekcvPDCFVJCkWTPIOym23/2SZns7d5KvkzJyYXbqSg3N2gSx39gPuRpZlXix1AggkYdiRLSk09BaSWDj/VJ3oYlpO8VBuUdkTmW+o3JLErJBYR0wAwhbMrghWcMARirJkE2XaPV+F3DuiG68jRz6/8PHBkDL3IzdtBwbc/wQ7ZKNyX2JnCoXuCm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000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 Reviewed-by: Andrew Jones Tested-by: Lad Prabhakar # --- arch/riscv/mm/tlbflush.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c index fa03289853d8..5bda6d4fed90 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,43 @@ 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; + +#ifdef CONFIG_RISCV_ISA_SVNAPOT + /* + * 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()) { + unsigned long order, napot_size; + + for_each_napot_order(order) { + napot_size = napot_cont_size(order); + + if (stride_size != napot_size) + continue; + + if (napot_size >= PGDIR_SIZE) + stride_size = PGDIR_SIZE; + else if (napot_size >= P4D_SIZE) + stride_size = P4D_SIZE; + else if (napot_size >= PUD_SIZE) + stride_size = PUD_SIZE; + else if (napot_size >= PMD_SIZE) + stride_size = PMD_SIZE; + else + stride_size = PAGE_SIZE; + + break; + } + } +#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, From patchwork Mon Sep 11 13:12:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13379327 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 83DCBEE7FF4 for ; Mon, 11 Sep 2023 13:15:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 033686B027E; Mon, 11 Sep 2023 09:15:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F25666B0281; Mon, 11 Sep 2023 09:15:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA0C86B0282; Mon, 11 Sep 2023 09:15:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C3C506B027E for ; Mon, 11 Sep 2023 09:15:34 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 896BE1CA56F for ; Mon, 11 Sep 2023 13:15:34 +0000 (UTC) X-FDA: 81224363388.24.0778C20 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) by imf07.hostedemail.com (Postfix) with ESMTP id 7582D4000E for ; Mon, 11 Sep 2023 13:15:32 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=oRxjkJeg; dmarc=none; spf=pass (imf07.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.128.50 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694438132; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=nqYb5Vu8dCDUBKbfeLnWwY3UtdJspJqfkbvYUWs56pc=; b=P+E0AISvfDeijZZ/Ly5uAJ09e1VKD37Unc0/Tl8Tp4KwYS4OpNDrnAQkiWLx7BRYBbDhBF ZpgLMB4w+SsEHe1NeiLYKK1F4lEfGLTp2RV3ytW5EKr3t8dTCFY8djb5fLxmY/Qzj/Okmv KPOsSepxNgodgzyJ37gpIFhtpOzh9eI= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=oRxjkJeg; dmarc=none; spf=pass (imf07.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.128.50 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694438132; a=rsa-sha256; cv=none; b=a+7t5uWylc2mQPQ1T7dTGiBw2PczR7wT6cMEqHLDNnWvLHekWW+jlNWZ17z6jBZ5AWK6AQ dhgzkjSDs/c2jPT6Ha178kcxn8U1O90GDA0UHy6x4q4kWYFlwL1fG+Ls3VwtTzVQMoR0Gc 9PPgURJEaJYqCFZzpm8LflxNgOakIZo= Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-403012f27e1so23418315e9.1 for ; Mon, 11 Sep 2023 06:15:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1694438131; x=1695042931; darn=kvack.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=nqYb5Vu8dCDUBKbfeLnWwY3UtdJspJqfkbvYUWs56pc=; b=oRxjkJegG1S1gukEae0wGN8nibpp+8xMGMM07f7mZkURd8rBpF+2/SvyQ5MkYm8qLH 3SCaLLOPetXw+hJ77vvewAa4S+E4kyMDuA5Atsvg4cwoUy+CX88i2lXnTgqAI1BhRbhr U3sxNChYMpGtp9bpIm5F6Ep3VBXC2V6VJqqD1rGfRBUxJTwbXkB2AD3Ol4InpOI12Yju IcofFUtusym65Zi2KKtyfH8sqIQtJAIsXXVAGSYEsptmLI4B9FAshSstmfDguEmTOmpY SNC6Io4mf2AZWKhpprwvKC9baOVIUupjhWuzAsLka7xouO4k7cA7Y/Ioe7fXmtGk7L0J Y7WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694438131; x=1695042931; 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=nqYb5Vu8dCDUBKbfeLnWwY3UtdJspJqfkbvYUWs56pc=; b=QII47cTKXIwCv/0jBNx32388MR7IwW7Qg8y+jYKmlQkpcIVwstWNy1dKmbWN0nv2p5 icWt0ztMyfPnTSEkkQdFE6QslCpT68v8jOQ52Xubg16l06Z4GiCVPC8alNl9pi7g7CXy fQ1yg8YzXQHN0vRWtAq6/q5H4Q2sv27u97+WE2vtagRnF54bLfddd6xui4kzol46mMET GLFjuGMr4b0vIKFvKdyCPn0n78UtxfoHchrk3qaFNgAk0yH5E/fx4DESUtjyrRpZgNJZ wJB5fS0GI8yPqlvm2NfEAblpFSFRVYBDJu/RXdsE364n2N+NYJLRS0Q5xG3+e+846clI RLxA== X-Gm-Message-State: AOJu0YxeJt8OK//WXi3GSeooquMNAisjAwbIUysiuu+wF9AaxiXnVMh5 zCdGVkNfCcyf530pF75RiDL+fQ== X-Google-Smtp-Source: AGHT+IGSM7mk4P/JNlSdasDtYzZnQXEd3SbuOn7pwwi1IoVXpt21uzh3Nx2y9SMVd5/QCh9kvlN/TA== X-Received: by 2002:a05:600c:2301:b0:400:ce4f:f184 with SMTP id 1-20020a05600c230100b00400ce4ff184mr8883368wmo.41.1694438130933; Mon, 11 Sep 2023 06:15:30 -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 m31-20020a05600c3b1f00b00402f7e473b7sm9878654wms.15.2023.09.11.06.15.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 06:15:30 -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 Subject: [PATCH v4 3/4] riscv: Make __flush_tlb_range() loop over pte instead of flushing the whole tlb Date: Mon, 11 Sep 2023 15:12:23 +0200 Message-Id: <20230911131224.61924-4-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230911131224.61924-1-alexghiti@rivosinc.com> References: <20230911131224.61924-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 7582D4000E X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: oqto5389s43jztzoarhwkhqnbhehbh8b X-HE-Tag: 1694438132-631699 X-HE-Meta: U2FsdGVkX1+WUsc0eR/hpME+PQxycGLCUZmvvGY64ajZE8S3RNXU+ky5I5WoccygaZZ81k3wgBvWOwDKfS+1eVZ98s/aUQO0csBSL87VvkhPsvBDBsP4eomlFMaCbsPSK5rlHPKXd2y3uNdd3H0Hd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yU4mfx2oN0FpNs8AXYhYqR/NSA5pGBAXq9C7QoyXcF6ZmE1T3D+QtF/5MdELh0fChjasYeNjypxX3oOikPAMHR4vux39utr21flJt4lfejg+3a18YWNSrnnFTTeZdWS6R4AUZkkCzByK4PQO1lBZZfUO0NZafSJPXBZHNiSti496mvn9TGuS86XddMpWBfrd46sThYtvQcir4R/BGpS/OZR3gn72cfZ3xV4+1VCesov/ZYibzTjkzR36rFwOnQ/j3e6qNjTaDaS3O7wePgyEjFzMh0g80HXD0ov+HuHfIZo+4y+1+j1ItOAYCX1DJvXgqwE6opIetxbDWh5qM4lQzQytDJpAMMMiC4aGylIh9cokYcBJpS+On+jYn+fiDLK4go645DbT10g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 # --- 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 5b4a1bf5f439..b79d0228144f 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -273,9 +273,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 5bda6d4fed90..2c1136d73411 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); + 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, From patchwork Mon Sep 11 13:12:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13379328 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7F172EE57DF for ; Mon, 11 Sep 2023 13:16:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 22CE16B0299; Mon, 11 Sep 2023 09:16:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1DC6D6B029A; Mon, 11 Sep 2023 09:16:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0A54A6B029B; Mon, 11 Sep 2023 09:16:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id EEE306B0299 for ; Mon, 11 Sep 2023 09:16:35 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id BB6E31CA503 for ; Mon, 11 Sep 2023 13:16:35 +0000 (UTC) X-FDA: 81224365950.26.562C456 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) by imf03.hostedemail.com (Postfix) with ESMTP id BF53420018 for ; Mon, 11 Sep 2023 13:16:33 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VTPQTeKL; spf=pass (imf03.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.128.41 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694438193; a=rsa-sha256; cv=none; b=KsX/dP54j6iCoRzybIjzfjCRVuN5Y0GqNsGACE1DN0TdUa/Irwk71jRvYpkMxK52Tn1AIl Y/9jGXHmnjL12driMdJmTsGzOxYKR5VkQP3n3ZPdjMVnCXJqSnJ9g54j8PYleoyY1tqJmi Q13NCMxrTBJSGe3p365rw7ykB2AsnLc= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VTPQTeKL; spf=pass (imf03.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.128.41 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694438193; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=JvOXQ7FYd4bs7bkUZT+Erh5aGWPCjNF5TJ6uV/v/MCg=; b=5fzBIHJsRJjMWFimt0tMs3En9QBtBCQs1WRpEvYyoGUE0iTulhfp70s3SuhqqoBT3bu7sx 0rY6TnrNRO7+1QkshdviPKOpo9lSIFG7KW8BP++mlRKahWCiasioQ4/snzQ0kfhE8MLT2m jw8JYp74Ct85iIEX8CynHQMO+3hwH2M= Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-401b0d97850so49314605e9.2 for ; Mon, 11 Sep 2023 06:16:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1694438192; x=1695042992; darn=kvack.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=JvOXQ7FYd4bs7bkUZT+Erh5aGWPCjNF5TJ6uV/v/MCg=; b=VTPQTeKLIrmDTVc74zCmRLQ6JefLpekz+OQ6b4s0YIoFrRm+HO6ToY8Y7vf3y1tmwP SdfNvwsU6A9yM3HGsHuzV8HHKNNdM2csVsqU4giIQMKUwbfPxL0V46+hBQtZj+VddT3w XiVue4Hh1pBeq3xSDdYrTvEG8co70d6+fqvGsGwmvepA1cpLhIqKE+rWMc3NrCeI2j4o 7HruaBz7ufln6lXYHjpe0Ba2lKGn3bmU98LhFSFsqaRtF7Bb7lxsAka296bN31IYlGMi RPA6M/ay/zl/ydk1YkmbFepTzjO1Fy3mTwQNGWweXhcRLyDzEtG8+hJzx/Gs2gokQfaQ IH1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694438192; x=1695042992; 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=JvOXQ7FYd4bs7bkUZT+Erh5aGWPCjNF5TJ6uV/v/MCg=; b=YrYGaC/ChxzYxXITXFMG/2q6F2qWeyeZ2HLhDIzAjeNOwiMmo3t0aLYWPr3EkKx+Kl JcYH6FGONlWiHO55nfqemUHRCfMbvQuXgIti+35lHbFXC1ZCXCU9bEx9QRmflymt0fdF Nctdl9UQMURq3ny0M1puNi18QPmhnoX5FDIgLgYfm1UBRfyQxMgftetXxi8LIR0SQ8BS dW9aKzRqTy/VdupYRsZz17N4qd0cQ2FScmUTGskw1zjzcxasqoOYkt6Bs2LG0T8to4xU 9M84i9QL815aGrnZx5tqkfp32tCpkVtf0BXi8P1SBeM4EcYsnwsFYPfaG5csuyDWjFgu PDGg== X-Gm-Message-State: AOJu0Yz1J111DuNjq0nFkrtkhsaNtZF6WciTWV/Q0YsFMLp5I6PQ6mLN IzgSsIEooEg6ZRSSWVbUtYLEddINka7m4gPslHI= X-Google-Smtp-Source: AGHT+IFRHd7EuaFwA1K1cfF+a13jKeLBoTHGF7TEiz4nS9tv95i5qHxXArflq3jWzwYQ4//q7JLqPw== X-Received: by 2002:a05:600c:1c8f:b0:402:f55c:faee with SMTP id k15-20020a05600c1c8f00b00402f55cfaeemr7265932wms.26.1694438192211; Mon, 11 Sep 2023 06:16:32 -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 b11-20020a5d4d8b000000b0031416362e23sm10134340wru.3.2023.09.11.06.16.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 06:16:31 -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 Subject: [PATCH v4 4/4] riscv: Improve flush_tlb_kernel_range() Date: Mon, 11 Sep 2023 15:12:24 +0200 Message-Id: <20230911131224.61924-5-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230911131224.61924-1-alexghiti@rivosinc.com> References: <20230911131224.61924-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: BF53420018 X-Stat-Signature: orqt1wpcox7pibjbqi37hb3pjcdna9n5 X-Rspam-User: X-HE-Tag: 1694438193-546983 X-HE-Meta: U2FsdGVkX1+hQUnWKrnH5WbbidpeXfij+hvHQDWGQ9ShAVxr3c7QnPslVUa7WKw+kjhZYndp16ZnW5DZmNYlGS3rpUvHvbDDwc+N9e10T8RjK1kpcPiI8exvmpJPUizDKW8fndd81gPbG1LtqOd1Z9jCc/N29xQdPRU5guoogHUeXLFFqw71j9IwxsYe/CCi8kvuRwDqaXlX+8M/pxXVcQypn2jcVQjir1RJvPsL8X5V+2sYX0tjXaf/9VJJYE2iVi4jrUiIeARkj557Fr9Ahib/eu5sQ5ZKJJAZNDMFPqwzknJiUEZ14tu/LW1y4jyxDFhG8H7dy78ifmbyCor1bgopWJfYStHE6XFOt1V9IIqEcxjlWYtEQcxo+7a/MjZXT9eG72tF6Cpe6c63rm1fZRw038AdTKuZp+kGLMEhL70pvRrjXMTMQ3/zy1CLAXATH5/3Gt3TiVMXEFQAP4zBrwC49w0HMuoho0GthjlfA05FHm2+3ilsHfUsUh8xaQ9x/WkQcVIls4xnLbvhp1rlQ5SQZe4Ded/5a/KOnOxFKEaqWgx7aOdsxhq8Yb4tq3Z6nbaJmt4eW+z87sCJ4IyfR2QQ5z6RQnNbDDoNyDkskE+AzEK5frVtKdsJLeeBVNy4vImEhu9Y7K91908zYagmxpxLKeEbqYyxVZpncWu6z6whT3Dj5JM8T6bmzaIxRqtT2ko80B2Y5pDkMJ6ghiklKQN5U6lMllYqNCNgNlKOgg2u3L7jTrQatn8amBPByLcu7rt0XfLsnMrUUfKKBkPTsztkU4QgM9bIbulgAa3tI8Qy/keJrvqk9ERPu9KdIzZtYvlJYz11scbHwI4E4KHVSnzE1CrTV6wB0f9yZeYeH2WBGZWgYsvYepjr9Cd4ONumxsoNsya8TQw83Sdeym3km2g6pgpiRMnKDzEDYZfKpuH//dhKdKpTwgLcH/M2g3OcQv7uSb/S1xnkgpSaxGO TB9azXIp q6JZZ3+zrVMLY9u6saSTb4MqINO58lM/bVW61VLs+g+b+VsomEGDofaXmfYGyaqASByUvQAwpHbnKnD3rhTq51H9TIIEqo7++K0c/LkyJbX+NkiKoPw8NOVbiDk89An2TIVwqlrmLv8q0gbcVDiR1Pchcix6l3CxMPa7mM685DOa4wPO4z4mJASUWwiBooNAaKTgCchFMEq65yb325BwRV4Un+3/9N3kEJxMYQifGfMKoFlBvt/HREbK/6ChkKdmgFJ1LvpltVJylgfjSrlg4OPbFHt6OL/e8SLf/joJiQrW6eGN++q+7wPu/u5JUaGV78JUIh7BtBlI9zoHYhkr43tqEjfkTFjMsVieQiFf6TfckUFcg9K9HeKR5cvgsrOd7qtlaBhMAXdDE8iRTv7vgk3wNaVjG0VRMSS8nco6bsuIwVePcbFqV2TkPWYaDzGArRBRpUfJ7StLka6DKKONTEBt5n/spZwA6GyKSgQlmkMLFVF1j+f9SO0IpyugA8hxVj2kESRH6CJphVgP3IWiznBeGQzEDHV4EHf4cMqGS8bc6K/exeG71shvdRua6zUXh7t+Z+xX+vVD4HmP2pSqvYeU0iKbIBephhTIew3zcNd9qvy0THTra2bOXLw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 # --- 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 2c1136d73411..28cd8539b575 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) @@ -189,6 +198,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)