Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp5470661rwb; Tue, 1 Aug 2023 03:12:10 -0700 (PDT) X-Google-Smtp-Source: APBJJlHFQauBVrQpgrTeNnDniVWM2b9ooCizy3VFfcmSfxAYemnifxi2pZ/zgORwi5RVUbXMoiG6 X-Received: by 2002:a05:6a21:788d:b0:132:cb97:83e2 with SMTP id bf13-20020a056a21788d00b00132cb9783e2mr12476298pzc.50.1690884729930; Tue, 01 Aug 2023 03:12:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690884729; cv=none; d=google.com; s=arc-20160816; b=wHCaDCibl4/kqYfMU4BFhoSHPAKsDp487W6xpIofg7OEnd9ayW52QqgmKBvJtuDY4Z jZsS6f8RGCjOP7a8dvf2GQgGI2XgYyMt5Sy7KsFv7wVk+cNmW1luXIXR0KvfvE4w9CJu p9ve9cZVvXjkwkkjSMckpBlw29zqW8N9T7HEJtY/hGxcbW1w0/BHuEoGyS+Xly5ltQYy PtePaFJIZpDpoao22q+YTWSwLiX/fEHuE38mf6avs2fHeICnRr6dE6JjCNigNsOj38ta Ouwpsp7lgAdo/ER+hYK4PyWrxSYb35YrCJEXuQx5QAmdUN9oNMeLhx4FiGnS+1ELCTsM 9/fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=C8YprRlaP4W+8yj964kuFlSBdOjyOpODn6Kzt6jcAnQ=; fh=FUpKYc+jGKezHChimvGHso2Y2jew/UgGSnq0l63BhsQ=; b=L78zs2LFlBZGz1UsMy1ciTLdstitYijW9WGyMBZLe1Q4/wyIq+TuKzCQG5QI4gycDa 9iQvQtkO0Rt7ChMy4N98rfZeKdiJlCa5WHy4LRKANDWZfZ9+X5/sWvwsx5d098GY/shc pHfQCX+nHw93eoNYVNH2MOBrdTTaCJFvv50s/jKchgsGQ57ymVzJCB9RQYELAfuuFxe4 z22dDaC4BW3lGXy54g6oXYedVcylcVu+B2LESGOqpecKTi/BF1fT54BYegOJ97OP68mp bf2pvG9Wd5U2oNEtK1AVSaZQyqc62mc65etsMTqika9CZEiQWRvvRfxbTCbst4g6DWQF fiUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=qdlxfR2U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j15-20020a633c0f000000b00564737b426csi392514pga.198.2023.08.01.03.11.58; Tue, 01 Aug 2023 03:12:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=qdlxfR2U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231344AbjHAI53 (ORCPT + 99 others); Tue, 1 Aug 2023 04:57:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230094AbjHAI51 (ORCPT ); Tue, 1 Aug 2023 04:57:27 -0400 Received: from mail-lj1-x233.google.com (mail-lj1-x233.google.com [IPv6:2a00:1450:4864:20::233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BF471724 for ; Tue, 1 Aug 2023 01:57:11 -0700 (PDT) Received: by mail-lj1-x233.google.com with SMTP id 38308e7fff4ca-2b9db1de50cso42357361fa.3 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=DnnQf9IQ4dcoGcyd5153ZYoGWc/G1TK43U5Tf55/PNOtHMz5HK2XplWqTiGt+bp6uj wxl5SCJap17IrF71Nx504H7JIp14BmK1PizUqY3nEfKm/pVvp0dJND5S42uen93dKFGV c2Q6UwkCxKwK9E2njRfdxhX9A1uedxfLHbp7p8SgNQnOAAebalWJKHBsuB70/4vjQis/ lmAEr167pzyklYpeBVUg8lWywd+kHNKnbdVFe+IO3Ou71ZVyern0XwUEMzNb1UyOvqmV zAOHanUHL8AcdxZttmwOYwtSz20kGwz0rKqabIWjUP/ubnOT32nYdhssB94qglvbSCuA NFnQ== X-Gm-Message-State: ABy/qLaOSwtpmtDNErpKunNnP8IDk9u3bEF9WnO/i+SNlu/rQozzHsDl 6pwlgHQ/SEXRwthBCLS6RoW/7Q== 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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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, -- 2.39.2