From patchwork Thu Jan 23 13:26:16 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13948263 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 C36D1C0218B for ; Thu, 23 Jan 2025 13:28:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 57891280002; Thu, 23 Jan 2025 08:28:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5290B280001; Thu, 23 Jan 2025 08:28:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3CA8C280002; Thu, 23 Jan 2025 08:28:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1D9E5280001 for ; Thu, 23 Jan 2025 08:28:52 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id DE5EA1C869D for ; Thu, 23 Jan 2025 13:28:51 +0000 (UTC) X-FDA: 83038796862.05.3FB9A63 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf25.hostedemail.com (Postfix) with ESMTP id EDE75A000B for ; Thu, 23 Jan 2025 13:28:49 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=RBQG4Vme; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf25.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737638930; a=rsa-sha256; cv=none; b=ErGyFeXZfTsWisQqwNZcK30qk4y1eBRu3Z/pD3cFiT/ym9Fk6ejubK6pFJFoCAPZjaKVSB nS3Ki/NxPIs6BxHbn6pkl5Lf0ZciSYH0AZSXDlnpRA9z0C27B8ksMmnN262r/5P/HbjCs5 q4YtfzCa+MdB5X+XjPKDmOZ1ixRU+QM= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=RBQG4Vme; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf25.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737638930; 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=zIKiL/cvy76O78IwclMxfA3FUb4EAj4nzgq2isZtk2Q=; b=iS7r2k7mTNOfzSA3MgEGuWR/YXVzuTTaTeqweFiVAi5nT9uDNTf/wZIqFAS0hcOWGTWhmy KUbpC3x/THkHy3y74uOUXd32BCR8/vyixSuFMu5LIxQqv+wvnwZe5AQka7Z/s1mideioDv jauQOFe2pFvFFrXD7NV6hBfW/wiDqZs= Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-216395e151bso12324125ad.0 for ; Thu, 23 Jan 2025 05:28:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1737638929; x=1738243729; 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=zIKiL/cvy76O78IwclMxfA3FUb4EAj4nzgq2isZtk2Q=; b=RBQG4VmeG2yE5mLu1akhbNnFAyiaQfuUpUNz4NYiYTWwzJXdIxsD8qKSmM445p9P40 k23xVdWLpsUyNyWHtXgZVutN8Ngv68Ap3dS8lcDl1bZKoRlTQToYhlLAJPVHjihPt0z7 XZOfUBjQki7WsjTKFZ937fB5AQ9s/Jn+AceSTcNKqmykN7d1Dp1v0Hem0Ml15k9IE+jV gH0z01GIemVqqVXOKRjm0LhfWN/S/oM/4RQgjeMbnBe9ezcYb6J+e3wvzh1R5fO/ZftQ O1v+DERhjNaJD4GZ2+VacO5MEYN8hCDM06eeh/bxF7ylU1Hd73gVHrvZkk8IhbrQzZ3E mBQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737638929; x=1738243729; 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=zIKiL/cvy76O78IwclMxfA3FUb4EAj4nzgq2isZtk2Q=; b=CTA66XEynwMJdYuHZ02KmXHaeSr8dMOJ9dUhDFfofdmqh8xyHitVee0ipgLBQY9wDb G+h9MzLZ7VHAIIA15eSS9HRVhTLD19t3A3++Iw0o3XkY1I8+4PYlXTR43WbCUAyz1LNM wIH6VbE7Upg5Qhw2Eu6s0SrkJWXJezGSle80PI6XMz+uFCplEw3yblksD2HWVJ4YIwZm tybXB5EPg2rq/LL39cO8ofMfo7BHKXEr4NMzVvxi1lT/iW53dW+mHnl64feeT62lF9pr puLAF+tD4Q3Mypa423cs3vZTLHXGr5jGWI4K5/e5hD0kzITcxz4gyufXkEBUWYfI90nD eflg== X-Gm-Message-State: AOJu0Yw5tLRnPfyamWC1J+k0uVMuRVf6aqJ+6pHRxndJXT6PTnJzl+Gh pNSWB01c3T2/ejXPr4xKNllkOvOSO2kfSTB4ggzWCzbYPlVtYp6IchGzYnDpsTQ= X-Gm-Gg: ASbGncsJG672AkL/VKwpYB5VtNYnZ8JsuLCiPoI7sNyM0lLi+IazavlIkXmisilSwan dmb4tsi8Zuno5pNGFIWYUjZHFc1hs21Zs1MttUjnwYLXk8v+iFjpTB2G/dLvp/KTHAIbsqx2YYu c3ZFc/w9wBPXWUR7S4L8HlKVYgSlhLMqxDKuYrWQ1AvLK5zK+s16ai3xe5it8+AbKCTaqmQ6vc8 +N97znx61qKA2QvAivBLHbBsn7kb+szNtmEZuwjD7Y0O1RSIfM7ozp0tPz3uqh2PNEcxyw2X0xN 0PyzyqUQ1e6a0bFTQanLBs8itRD59RSrnRU99ZZbK9vM X-Google-Smtp-Source: AGHT+IEu4HBG3SpWH9djQXZ235PefY3+YNVKU7kJShvdW8r3VF7F38seuaHQ6/TN21qUw52tcuL1Ag== X-Received: by 2002:a17:902:f541:b0:216:5db1:5dc1 with SMTP id d9443c01a7336-21d993172e2mr55309915ad.1.1737638928616; Thu, 23 Jan 2025 05:28:48 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([63.216.146.178]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21c2d3ac3e2sm111583655ad.139.2025.01.23.05.28.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jan 2025 05:28:47 -0800 (PST) From: Qi Zheng To: peterz@infradead.org, kevin.brodsky@arm.com, riel@surriel.com, vishal.moola@gmail.com, david@redhat.com, jannh@google.com, hughd@google.com, willy@infradead.org, yuzhao@google.com, muchun.song@linux.dev, akpm@linux-foundation.org, will@kernel.org, aneesh.kumar@kernel.org, npiggin@gmail.com, arnd@arndb.de, dave.hansen@linux.intel.com, rppt@kernel.org, alexghiti@rivosinc.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-csky@vger.kernel.org, linux-hexagon@vger.kernel.org, loongarch@lists.linux.dev, linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-openrisc@vger.kernel.org, linux-sh@vger.kernel.org, linux-um@lists.infradead.org, x86@kernel.org, linux-riscv@lists.infradead.org, Qi Zheng Subject: [PATCH 3/5] riscv: pgtable: unconditionally use tlb_remove_ptdesc() Date: Thu, 23 Jan 2025 21:26:16 +0800 Message-Id: <86e9fbf586912f3689e98c4dee2180b69b399638.1737637631.git.zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: EDE75A000B X-Stat-Signature: 859gd8uag3quwwzzpm4h4ikm1w5nyjr6 X-HE-Tag: 1737638929-289117 X-HE-Meta: U2FsdGVkX18avAj+EbZq8Dev3DlR/OkPzMM6PaZmv5yIlkMvgZyhycz7R2WecgWXivOuk4J4jDx/21SX8il1nW8L66+ATitT2ugeaDLEZRe9UOkt903iX5a5E4IG8dxFpeYL+eKj0oq20baeG/GWgab45ioqPjTbJNoTy+kOoB0aRt4iz4BJ7cNFBwQqyiwLDyITx/ZpOi6S4I4/c146tYAGhBWcc7Gzcjas+mJ0B0rVwRb9pG9z+9+j+t0AsC+eJ79HXp2fvwi1sTmZw1PS2S4jY15U90Ckk/57Z3lg3mzsuvhF/PX0QNyArzhnsSIaNamy/N9J26lgy7WxHGiWn+V+shJmxzulpwj59FJXTB9SM5zmfiOxX69nbKaPBWeJuU1Vprrs01I7/92DCtL+FKG/OVE2gA4T0z0oFjsU3TB0O9geUPHtvFS9auwIIUwPgXRRSLT3E7q8IFf8crDyut9elag//5HMVHHgDXvaZeqKxwf+MQfM6dODb3CjtsEw1FXRpaL1UQ8LLNlLn6dMSaRk6eqGIBuIDhb3CWZwWGO/OLatPO7PCkayg4PFYIeGA+c64X/3KK87qHs50lyoFfbFrAGWb2qeryHLqOtks+L0qo7pZX9gO9hmMFQI/k63HIPS9Vk+xsjqxjbsqDKVmH2PoRg4F8bL8aL8qPFYmFZ5Da+oH2iVrQvCDsmNTPnFEYPfSUG3pUBRDbjH9CjT2ouZ6e+ewVcP3OgXuO4dFzY4PtD7DwiuE02lrJbrlFkJ32kWmfU8cErQTdXhCq1bKUhhGucMuOnIKSmVenaeIwvy5eKZbioAAtJFd6nsO1YyVS7M6RRQO0Ox0q6wZgpR4E/nonFsnGE2z6S/pH6eTQ5tgnM9UeWbARtmzWbFymLGt/+ynRSWlsUxKXIz1SLpJ/d7X0VcM+xKZQzYoU/GWBWQv2KBGsz56pWY/tMOR2AEr+cz+VauNLBN/qX10qy voWXH8Eb piGobNdntUBltcPqMOl6vrY6gfwG3gA7pQO04cxP/KWcPTFScsHZdz1e+BOyZkj3PfwRZOHlvILIA/8Zs1gvNqnQNzeoJZHgphtkJ0R+gIstjVJoKD3aJuQcEH0nG9/M4GKHIW9kxGc2JwT6UFMlPJUkIzvyoxqQpVpQFSxHkzrkwqfLUScnJnrRQNFJPH/ruhxGVs0AzOd0yLtl5zYtEFo590/TrZmuCYOtC61G8lD7Mqi+KD1LtNniZ7Ik/ElPtvIfmFepcQSZjNGrEIklSJVHRjuCgZoDRXDsDuXhbo2JOV3uyuRSjGBmC0pOQZgnBw0iJpJPEydeVCyCKvcWH9k7d4I8dn/4oxfFdWDbVD2tCZA2Gm94OlW+8pJkE6TqkGcBDiDY/f6lIhH4PGtAp908Ajf5VueBXKGl+EoWvmRbzYBOlmZYFTOhQ5VGokl1moejhhIDK+7T/gaGT/S8CskZm01Lc3dvHrCAaBnANXL9S8VI= 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: To support fast gup, the commit 69be3fb111e7 ("riscv: enable MMU_GATHER_RCU_TABLE_FREE for SMP && MMU") did the following: 1) use tlb_remove_page_ptdesc() for those platforms which use IPI to perform TLB shootdown 2) use tlb_remove_ptdesc() for those platforms which use SBI to perform TLB shootdown The tlb_remove_page_ptdesc() is the wrapper of the tlb_remove_page(). By design, the tlb_remove_page() should be used to remove a normal page from a page table entry, and should not be used for page table pages. The tlb_remove_ptdesc() is the wrapper of the tlb_remove_table(), which is designed specifically for freeing page table pages. If the CONFIG_MMU_GATHER_TABLE_FREE is enabled, the tlb_remove_table() will use semi RCU to free page table pages, that is: - batch table freeing: asynchronous free by RCU - single table freeing: IPI + synchronous free If the CONFIG_MMU_GATHER_TABLE_FREE is disabled, the tlb_remove_table() will fall back to pagetable_dtor() + tlb_remove_page(). For case 1), since we need to perform TLB shootdown before freeing the page table page, the local_irq_save() in fast gup can block the freeing and protect the fast gup page walker. Therefore we can ensure safety by just using tlb_remove_page_ptdesc(). In addition, we can also the tlb_remove_ptdesc()/tlb_remove_table() to achieve it, and it doesn't matter whether CONFIG_MMU_GATHER_RCU_TABLE_FREE is selected. And in theory, the performance of freeing pages asynchronously via RCU will not be lower than synchronous free. For case 2), since local_irq_save() only disable S-privilege IPI irq but not M-privilege's, which is used by the SBI implementation to perform TLB shootdown, so we must select CONFIG_MMU_GATHER_RCU_TABLE_FREE and use tlb_remove_ptdesc() to ensure safety. The riscv selects this config for SMP && MMU, the CONFIG_RISCV_SBI is dependent on MMU. Therefore, only the UP system may have the situation where CONFIG_MMU_GATHER_RCU_TABLE_FREE is disabled but CONFIG_RISCV_SBI is enabled. But there is no freeing vs fast gup race in the UP system. So, in summary, we can use tlb_remove_ptdesc() to support fast gup in all cases, and this interface is specifically designed for page table pages. So let's use it unconditionally. Signed-off-by: Qi Zheng Suggested-by: Peter Zijlstra (Intel) --- arch/riscv/include/asm/pgalloc.h | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/arch/riscv/include/asm/pgalloc.h b/arch/riscv/include/asm/pgalloc.h index 3e2aebea6312d..770ce18a7328b 100644 --- a/arch/riscv/include/asm/pgalloc.h +++ b/arch/riscv/include/asm/pgalloc.h @@ -15,24 +15,6 @@ #define __HAVE_ARCH_PUD_FREE #include -/* - * While riscv platforms with riscv_ipi_for_rfence as true require an IPI to - * perform TLB shootdown, some platforms with riscv_ipi_for_rfence as false use - * SBI to perform TLB shootdown. To keep software pagetable walkers safe in this - * case we switch to RCU based table free (MMU_GATHER_RCU_TABLE_FREE). See the - * comment below 'ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE' in include/asm-generic/tlb.h - * for more details. - */ -static inline void riscv_tlb_remove_ptdesc(struct mmu_gather *tlb, void *pt) -{ - if (riscv_use_sbi_for_rfence()) { - tlb_remove_ptdesc(tlb, pt); - } else { - pagetable_dtor(pt); - tlb_remove_page_ptdesc(tlb, pt); - } -} - static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd, pte_t *pte) { @@ -108,14 +90,14 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud, unsigned long addr) { if (pgtable_l4_enabled) - riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(pud)); + tlb_remove_ptdesc(tlb, virt_to_ptdesc(pud)); } static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d, unsigned long addr) { if (pgtable_l5_enabled) - riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(p4d)); + tlb_remove_ptdesc(tlb, virt_to_ptdesc(p4d)); } #endif /* __PAGETABLE_PMD_FOLDED */ @@ -143,7 +125,7 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm) static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd, unsigned long addr) { - riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(pmd)); + tlb_remove_ptdesc(tlb, virt_to_ptdesc(pmd)); } #endif /* __PAGETABLE_PMD_FOLDED */ @@ -151,7 +133,7 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd, static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte, unsigned long addr) { - riscv_tlb_remove_ptdesc(tlb, page_ptdesc(pte)); + tlb_remove_ptdesc(tlb, page_ptdesc(pte)); } #endif /* CONFIG_MMU */