Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp1751339rdb; Wed, 31 Jan 2024 08:03:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IGgDfLYBH4kaNKQ+G4sw0QqYVQZDCtOkB4zGlWwh9Mn7OZ8i9PCENx1ciWAUDrjeA/aLNXy X-Received: by 2002:a05:622a:1895:b0:42a:8c39:fbbf with SMTP id v21-20020a05622a189500b0042a8c39fbbfmr2466360qtc.41.1706717030064; Wed, 31 Jan 2024 08:03:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706717030; cv=pass; d=google.com; s=arc-20160816; b=z60mxWop5WWXVdkq24IFnVD9+Ww+/AyuClIHctiUZri+kg9noHlnytWppWV3X7injG Xy2XGKxyMzreEJaEiejuzuyHwCKYcIM0bsrwX3/cm92wI3Xr2xKDSz6uMUpukOl92ir4 9/TtTbXB3Nh++8t/mF7TOI75uN2SZTly/dIBw0jm2uIKgtAgdb5aM2mlL/U3cbPv+PPO woohgKRMMaoEgboC2emEml35hg8ePQ6g9dnwUM0uwq7iZeVgvqsgZfkbomBAqIFBsVTt T375ev3yi4iUs70qISAXaIG+liiaEvY+5f8L+Ng1vQdN/4DdKbYn/PxiT/E+1wmWrQWO B5jA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=2G7N9HKTptzFvdsSy7a/wMEV20988NdsE2TTuou7VW4=; fh=D4oFq1ETBcNMCUwjj0fKd+PYR8QNl0c1c7RAmXB7/Zs=; b=WQ44V77zkCQg4Ji1GcRxjuOyUmUePPEqD3qKYYOfKCejGYactSDllKIYwbfj2tLOaU 1qMaZhJCRwjSSXOnswkX6Da0GSBcvpxTLK7h6Dxm12zDmcSIjm1+Ylp1Nol8LvTYR4aT O1t009tePZY+OvwThOvugm4muvPIYP3r1l63Wfrn6FHvcuo4Pe/BgbaDYhknI+eDsnQd 4nax5RlxtE5DCvX68+qur9pvFyeSde66z8F5fEyZLWGHA4wtnwBGcFZk8shQcQXUwOxW YZnbw4ftO8CW9o9PZ1GDlqf9lOHaVw7vG9nyEZSau25VGMQBp0Ggi46ttmLpCkfuqjxz 4OUQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=eIGL1Lud; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-46754-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46754-linux.lists.archive=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCWVyTqhEJGbXBasFuJyn2cX2GsOrDVy3aYrzSqEvugenFSKUaXGpob/6c3Rul6X1XdtXl+9wLusrj5qLx9eDrAI2DI9FNXltVHey4W3cw== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id u3-20020ac858c3000000b0042aab5245a7si5618974qta.267.2024.01.31.08.03.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 08:03:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46754-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=eIGL1Lud; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-46754-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46754-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id BFCDB1C23F9D for ; Wed, 31 Jan 2024 16:03:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A116012A16E; Wed, 31 Jan 2024 16:03:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="eIGL1Lud" Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0BEC12837F for ; Wed, 31 Jan 2024 16:03:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706717021; cv=none; b=HD6VmarQJ3kMIDerFvGpZACUqiET0vd0yoFYZcot7/ADTBvyxhdyi58skSjGbmq6DZt1m0xVmS4ghNeXTh2N5OI9/hD1+wQ0fh5/rL+n/kaVnLO7AFbZopbJbGegZbq7C8zJxPbM6Q3bTvUHPTyDTtgpnW/71S0l42qBikq736M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706717021; c=relaxed/simple; bh=2mXgFzRq0Ox+M+gSvM+xy16HnlbLzrNafWWDZOAbpLI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aEu+y2d10wF/O/eCSqBUHKTF3RzG/qnpd++05fCo6HMwS3h/LVuzXL8GnZAdGszC5WKvd/KhTTX8NRDM3zsHm20ge12OiyWV/IK2Wzg7p4jHXrrO+afpSb98WpVmaYfeTquKxWVCjrZ812L7NOUrETgFLnh50mvgrzVT4CrO5J8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=eIGL1Lud; arc=none smtp.client-ip=209.85.221.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-33ae6dfa923so2567613f8f.1 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=vger.kernel.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=eIGL1LudcuGFTrr5RG4FeVDTObnA0dOg7m/GNoTlUuCkYiHRu7v8zhC6/5eJ6QsqPo 1yPpTNvOxhkbPJXiAf6CEPLOMPFJGSHCrRhML+7YOp6b0bB6s4LJ7l4e39giqjqckSQk YxZ8qzqFvIZS3nLvXSjk4wYNp1LRRLkW/UHlWbO3yOnPvJ9sl1bprx+oJG0c6+SdoQg7 ipqVD078sl9wEH8hVE+anbSSc01j8iHXJln7HkWZXMCpAwizTAAJibKBosQ7GY1fn8eU TLhqrNN1AfyLHwYlehLbdv6xo0gAg8UiiqGum70keeNP7B56mEfyUBc4EbIrfY9n5EBi D9sg== 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=jL+/pezcE2wy4UOAc5ynCHMw15qYJ1ddMxs46qcWLOcxXNApoVHF+Vrc78Pu3uH4YS OiydEywWwMaRvh4r8KjPd8bumQXQm0yt9UvFA+C0spjcn4zdd8HTFtnuUlGK60WTm/Kv 9py4zHa6yyKgtegwTayFPpBGga0qY9Gw8rzlyukBfGMHljkCINseFRyM224OSWCDHxJM OaQK674akrPAgtPdOs34gQYi1YXR6PXKIYVaR3H1t5FwIhPXsMSLf0iALoQIJ6dH3SNr ofdfKUBVQ6ncRuwJZlPJG+pOaNXwhBYrr8uKiAt4cqsQCYNSqURAqoaCDHo6aMtn7quk K3wg== X-Gm-Message-State: AOJu0Yz/IM2iPlQEG3h34FtsbKC9OpYLZDwN8juAzjYiUCdYZbZX3AXP WkIIUzdJrTdVBxd4GRlzS07pH4yhRi70ABcD84Mi+FhBZBxqHcqThML/MAC9NE0= 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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, -- 2.39.2