From patchwork Wed Jan 31 15:59:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13539584 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 22DCCC4828D for ; Wed, 31 Jan 2024 16:03:44 +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=96unitUcU2l9WOy2nzNio7JtGUwwcvLDtduPKMzRmIw=; b=Iziboti9DpR38M kC6/SoCcIVE3qtlk8NCjaq+BU5LlETVRlHWjDeVvXZqv4vVkzxn/lo+xUCfsaFbXfQphdA2/EcY79 7qN2XccBdrHQ2Z0KyZG5U6GC0g+WIv8WTJKAjO8nAoU1LWkjqMPA+6/h+FZ6fBP0mz7lwIlzoybP8 4Y0/g7FFnNHqpsJM44y+uby0PpM8Yh0wYedjcSeL5N7GgEFMZH9gz1Bi+U9XG21PvsJbnwpRYldlg 7jSQnSr8DynJF+aFE/Wq+ZgFhU4nkYK2sfq5vmr5jeMf4wHmry2h9DIXLvPdDIcrlnC4+Y+KGfAGk IsX9B+KB16ck5aFi+rDQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rVD3t-00000004OVT-1j3s; Wed, 31 Jan 2024 16:03:41 +0000 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rVD3r-00000004OUD-295G for linux-riscv@lists.infradead.org; Wed, 31 Jan 2024 16:03:40 +0000 Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-33b01484cd7so752845f8f.0 for ; Wed, 31 Jan 2024 08:03:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706717018; x=1707321818; darn=lists.infradead.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=2G7N9HKTptzFvdsSy7a/wMEV20988NdsE2TTuou7VW4=; b=ihqwp96q/mrz6OEhUTdsyIXqvkMXoWroGbUxZSbe2hxe/HLjmjSLvGMsbLwD+byXvs PUwwuf/HEkeL61FIr4ePa4C0LBd5wT7YLJU92UnEzIpUhfDrxCy/H/9q8ZLxwrVq2cuB Rrrtzg3mRLFBmUSIPVbJ1UodzLft50dL37XRhfMRdcvtWMJZmkFSJS/zx+iBmJ30RNaO DXMpvTFAW5i68jqXGhQIqxNJi1O2YUqaMF69WhAcGTyn5POxvRkH5GaBS1NaJKssvH5z tPeFIMHL2hya9GG0oGzlaPfO/vlOkYlnG/seKGFNbYEHZNZeNYluzWiur1fn5z1u5a/F QRVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706717018; x=1707321818; 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=2G7N9HKTptzFvdsSy7a/wMEV20988NdsE2TTuou7VW4=; b=t5NA+rkYomL0KTiLkVNFVTsqlYqZMuPwqPfvClJxf0kmks0LapiPm+vPJ2NiJ9agka OmdcSEmeTT1e3JuvCVe7ClDZNpoUMub1BezeNrtwbHp7kswzikHsoT2rn8+gpbejFk0W fEWYHPiS3tPNY9Vqw0jw7pifjyJXsOCN9i0hg+UY2wXpVOvW9GS5o0HTckRu09NR1w5j aANaK4z5Js8e/HFbLq1SJeGuvyVvCApEemltXkBTefgL7SfpwLN1sZNym4TGkyIFkCVa QnH+OcRSh9m5z6ymAE3AyPBKYPO+U7Ol1cOasDLj1EqMgSeExIm3KtURDeeqsnl2niC+ YNTQ== X-Gm-Message-State: AOJu0YzXV19+A534Gm0aZJXsRvTtWqD7ytMydxkKDJ73RPCFwMOvZ5rO K//w0vFF6q7gCxRDkrgEaiAdYLayOPCP4gFnoG/2w7msy82A5Ya4iWIqaPyHIPk= X-Google-Smtp-Source: AGHT+IFv4RsKrg6kAntnJ/YCtPGAW1U1YNeeMXiuO/lnsK9Rn1OpwCBxYmcNw94F8PYwyQot8cihag== X-Received: by 2002:a5d:4448:0:b0:33a:e6cf:f564 with SMTP id x8-20020a5d4448000000b0033ae6cff564mr1365333wrr.28.1706717018193; Wed, 31 Jan 2024 08:03:38 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVg1J2yUOavmT1BH4y5nxg3fStQ3N4AW690uC+KVjknvwF1ll2sBXJWTCViTvOYReZkrcvAcX1OMeyRFhnXqudjaBZl/gg5Slh2qMDSCgRbs+MYgkxfwyGouM0xHRs0tgxJCpxbNgoQm1h/C/3NBDpOmt8gVIsv7E96n1TY5vhD8PUSLr9R0dCCkfWeCCPx063FNEcg1j2K+XyPJxLVqJsMLPBwMAa2yiLVZ3yCyGFm+j7Fzm78VPLhSet9sBLkuF7EhUO0JSNmhAm08ItvQD/eyizM4UaWgLJ5IfIEvoIxGnSv1g+/L7u5k1OSXo4eUZCK3WtIs5nc2sntY+iTJ0XM7Sw1HGmFv+717YWEYjUgFkTaZDBVXQ8szzSSQSMga7N2cvCO6sZEEmIdJ3WrbxjU7MsY+3d8+XL7ukOMZvPAA8ULHgKtMBLME0qs6pvJjiy+Ahi0q6IN7XN+z8czcuyGvvHVD505pmNGMbMOVBEgXzcDvj6KG+dXMI7+j2KsumizhlmmeCeIGlGoXRxI8cSqcGpx+z3xdHoptsF2bc/C5MXJ+q72d+EHk9Pq97YjxLi0+gDc1EX/D+NmdurxTc5o3aAmGs8bweDvIDgr9lY+Cqq7S0y8tugvFuJReOmvvT386mI9I7qqIrf8NA== 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 bu16-20020a056000079000b0033b0797cc14sm1358024wrb.51.2024.01.31.08.03.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 08:03:37 -0800 (PST) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Thomas Bogendoerfer , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Paul Walmsley , Palmer Dabbelt , Albert Ou , Andrew Morton , Ved Shanbhogue , Matt Evans , Dylan Jhong , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-mm@kvack.org Cc: Alexandre Ghiti Subject: [PATCH RFC/RFT v2 4/4] riscv: Stop emitting preventive sfence.vma for new userspace mappings with Svvptc Date: Wed, 31 Jan 2024 16:59:29 +0100 Message-Id: <20240131155929.169961-5-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240131155929.169961-1-alexghiti@rivosinc.com> References: <20240131155929.169961-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240131_080339_577090_F8C7412E X-CRM114-Status: GOOD ( 13.92 ) 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 The preventive sfence.vma were emitted because new mappings must be made visible to the page table walker but Svvptc guarantees that xRET act as a fence, so no need to sfence.vma for the uarchs that implement this extension. This allows to drastically reduce the number of sfence.vma emitted: * Ubuntu boot to login: Before: ~630k sfence.vma After: ~200k sfence.vma * ltp - mmapstress01 Before: ~45k After: ~6.3k * lmbench - lat_pagefault Before: ~665k After: 832 (!) * lmbench - lat_mmap Before: ~546k After: 718 (!) Signed-off-by: Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 16 +++++++++++++++- arch/riscv/mm/pgtable.c | 13 +++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 0c94260b5d0c..50986e4c4601 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -473,6 +473,9 @@ static inline void update_mmu_cache_range(struct vm_fault *vmf, struct vm_area_struct *vma, unsigned long address, pte_t *ptep, unsigned int nr) { + asm_volatile_goto(ALTERNATIVE("nop", "j %l[svvptc]", 0, RISCV_ISA_EXT_SVVPTC, 1) + : : : : svvptc); + /* * The kernel assumes that TLBs don't cache invalid entries, but * in RISC-V, SFENCE.VMA specifies an ordering constraint, not a @@ -482,12 +485,23 @@ static inline void update_mmu_cache_range(struct vm_fault *vmf, */ while (nr--) local_flush_tlb_page(address + nr * PAGE_SIZE); + +svvptc: + /* + * Svvptc guarantees that xRET act as a fence, so when the uarch does + * not cache invalid entries, we don't have to do anything. + */ + ; } #define update_mmu_cache(vma, addr, ptep) \ update_mmu_cache_range(NULL, vma, addr, ptep, 1) #define __HAVE_ARCH_UPDATE_MMU_TLB -#define update_mmu_tlb update_mmu_cache +static inline void update_mmu_tlb(struct vm_area_struct *vma, + unsigned long address, pte_t *ptep) +{ + flush_tlb_range(vma, address, address + PAGE_SIZE); +} static inline void update_mmu_cache_pmd(struct vm_area_struct *vma, unsigned long address, pmd_t *pmdp) diff --git a/arch/riscv/mm/pgtable.c b/arch/riscv/mm/pgtable.c index ef887efcb679..99ed389e4c8a 100644 --- a/arch/riscv/mm/pgtable.c +++ b/arch/riscv/mm/pgtable.c @@ -9,6 +9,9 @@ int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address, pte_t *ptep, pte_t entry, int dirty) { + asm_volatile_goto(ALTERNATIVE("nop", "j %l[svvptc]", 0, RISCV_ISA_EXT_SVVPTC, 1) + : : : : svvptc); + if (!pte_same(ptep_get(ptep), entry)) __set_pte_at(ptep, entry); /* @@ -16,6 +19,16 @@ int ptep_set_access_flags(struct vm_area_struct *vma, * the case that the PTE changed and the spurious fault case. */ return true; + +svvptc: + if (!pte_same(ptep_get(ptep), entry)) { + __set_pte_at(ptep, entry); + /* Here only not svadu is impacted */ + flush_tlb_page(vma, address); + return true; + } + + return false; } int ptep_test_and_clear_young(struct vm_area_struct *vma,