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: 13539573 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 F1FC5C47DB3 for ; Wed, 31 Jan 2024 16:03:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 87FC48D0003; Wed, 31 Jan 2024 11:03:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 82F588D0001; Wed, 31 Jan 2024 11:03:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D0B38D0003; Wed, 31 Jan 2024 11:03:42 -0500 (EST) 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 5B0A18D0001 for ; Wed, 31 Jan 2024 11:03:42 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 25067140D6C for ; Wed, 31 Jan 2024 16:03:42 +0000 (UTC) X-FDA: 81740076684.06.B94B19C Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) by imf25.hostedemail.com (Postfix) with ESMTP id C53F5A0003 for ; Wed, 31 Jan 2024 16:03:39 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ASOQtlhq; dmarc=none; spf=pass (imf25.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.48 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=1706717019; 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=2G7N9HKTptzFvdsSy7a/wMEV20988NdsE2TTuou7VW4=; b=h1KONVrH4zuEhXMiB5Pk1bVI11I5Zyvp1MqSm74E8xiLXeORMWQpcYqF5yHTxVP2luOCOi n8zlpZ7Z8mAhI57xhfdVksWg/AeU+k4PoaYorkT9IiVrDJBSPKpFG0cKgjpvpB4avqhuRS rDoBegj4odqIfPQPuylN7AsEG7YK9Nw= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ASOQtlhq; dmarc=none; spf=pass (imf25.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.48 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706717019; a=rsa-sha256; cv=none; b=xaCrezZxQzXmwo4da9VeBn4ipD6ObN56wlAc/JGH/N5kU4rG8r3leQ/whzaE1OXUXFySZB eGK3fn+zpUG9Svc8ID1jV0Txe/StyEiXtbuKcDxtT0Z7XbO5RFxlhdSdp4NQJZZ6+pn5G2 vbA8TaHrol+EV+j+OAQVAM8TpnrmjNk= Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-33929364bdaso3555030f8f.2 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=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=2G7N9HKTptzFvdsSy7a/wMEV20988NdsE2TTuou7VW4=; b=ASOQtlhq78MgahqqlLLYbXJiNOfrc5yZNkYVUv7cWY+GHsM4p+TDFIQziy3wEN9yqK IrU7Vs1BbXPpVnFavZJqGHdx1VmhS7cN5xK/kNWtAflhWzo3Xh/8niqhjNdQy7zdc2ks zfi1ckKfrb4lwSWSFKxde8Yg0AuftV6PbknmkgjDhUHSTTgz/7eBFk/r/dU3oiYeMj4j FvjuWJRYfXoPNCmMqxFJx6hJF4BNGHc7lzk8VRBmxZzW6x2Qvp5TmWyDt8nMfbXt3Kxj Ct83WanCGyt+nxMvyPc5f6m7EexnLkMzqBUzR6T2iOXaC4Z+8PefMvUq+UpYn+LONhSk nK2g== 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=DjcAfcWUF7pUOUWGX9ghUv3e3Ohrgc5yvkDlbLlOg2vwp3qqCL1gnAWTBxRxEotKUX /XXAsghAnFikAVpfygx/vBIVfSUEBHUl+P389tUS20p3LpXg7b9xbA8+3INUploKztJ7 a7YdGXeRHE8NUNvhyXlCFmlYAewM1yVOr+p1qIjUqig5NCGetJVIVSM3eQbg9fbPW8m8 2oCfTZ8v2dl30Kty+jp12yausxd0wWDOdEFKQEi6jevO1GCeeUkeuFEfEVJogHDewVUK ERPU8aPH6l5SeuhvahrtFqowTB7fjYe6bKF6nrvtLQUOEM4kz5R73giV0U78ZpWFZGMA yzww== X-Gm-Message-State: AOJu0YznbtriXOuxmgmdnx1Hg1mYoq/CAfueyXPlgYg6NNDi3Ekoj7r5 1xP9PbZISAFHTi9nKUxizYYpm/ncWe8591EBdCiMxmbG65RtxOlyX3IMT2/uzLo= 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-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: C53F5A0003 X-Stat-Signature: h5qsisqm8bhp8dqr3dt189acoo57io15 X-HE-Tag: 1706717019-56161 X-HE-Meta: U2FsdGVkX19vq+D38tcw3K4pvFbhE8A0/pEPs5fRAWjOri9ww3EYaOHczdggfHl1ieftPC7c49hH1oPQkI0Er2YyZMS00J7ccjBO8x9j16Y4y7X/ABkXu1RJD+5pxSe6QkrdsSZP1RHyTYlCSAnGwh3YTp+hRn51S48sojXa/FtfV9enhOmLPxyOqQezjzP07rVEJeKyMNzk1F8fdfrOYP+SqEcmdJ45liFdLYNy25x35I7bKe+3L/xD7p3IieK4f+YE+hPWGNYKtHLeSZNsewvO0Pg0w/MWwxmNB0q9dr05OFbrnEDe9T3c7lqpNT96Q3FpI3WznBfsqOACaOUep1BIPOJRiV277p8VMcngBq4aVn2mswJBOdKkQvCG4Vny7G08zJXyEoLhvoI8wN1QAHKrf8HHMHt6VIsF7ziyz/xYzYN43zlj04hPyMDEZuDa7RD6yu0nIvGyGTLGBXZLd13G36AYjyoS0mnxRsAMV/Jkd/Y/WyJb95IpKTaiXvJUagZMYl8zXIIblcKXRJal/uRHBICceHajtbsmI2RJt3Nc4R1dQjyOlbrABcyPUSxc6mWXkiQGUbGduHschifo2fAPFJVxviL1x88BLW+T32diDW592OV344pVhsoL9yCMbB4Oet/pRPcsW2/GrQkQkQfW0sbinDxl/Mk6a3yOYSV3Twx/TAbQQ9N3AQdnegdPDiXXj1dfWIRBMfsxikHIPOVQGLm77mYGPe4lVAlQzLofGYChbLh4yvUTKeqO6wtB4Sc2ge28E+h6mT+UQQUqXDKD07xUgT7HsVdpHrw2+NB8gxqXi4eL8Asll9RfIvsayiCVw8Z/rdCcQrhhmi+w3EkyTd5iRPgB/UUAKk1mcvCqLUw9siQNieLtbQHNuCqEt8bc/mIr1FAgTWmi2bNYlOAkXnODZ0rWsVM/psrrAkc/wrWm6xFcQPOmaobXg8pNTmrEddp40j6h3oO7vu/ xr35wPI8 Yn7p0DXbNK7zzBQGOJw0UvElazbK4/i0s9WrGnn8h1wffaJE9WTLQcnjxvxl2E2py2arCGU9Tl25gySg1gbJsKzHS6UN1mElqxoBfGDu7gt9xPj4tqbWTbjCQV8aalD8HzFuMpHhTkVZlnS9Ujjfc+QmhfavcWVBxBkO9tBDYOBkGFylEMsNYLDzdThjbTl8/1Sckx+eCKUuN/HmvHaLitAQai0IaIrdjoks0GiXj+DLoheay/oLn3VEVgCYF4Z4FhZnIlcm9/aVZTxAe//FuTjMfgL4EnejyfQMeTXep1y40xivQEa056hkqdin5X0XLo8QO2pfF0LiPTHdA1j2yLcSVofhZ5IuSLUuNIshMb3uiS84o8VaBewoP5fgprSsksVKihyjmLKJ1wG8QFpE5zRRKurZqXiS1sPkXIwZskjMo+Tm9acgUTLO/mCjLS/p1avId 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: List-Subscribe: List-Unsubscribe: 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,