From patchwork Tue Feb 25 03:45:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13989221 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 B52F2C021BC for ; Tue, 25 Feb 2025 03:47:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B976280003; Mon, 24 Feb 2025 22:47:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4691D6B0092; Mon, 24 Feb 2025 22:47:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E2F8280003; Mon, 24 Feb 2025 22:47:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 0A3166B008C for ; Mon, 24 Feb 2025 22:47:02 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id BFF59C0881 for ; Tue, 25 Feb 2025 03:47:01 +0000 (UTC) X-FDA: 83157081042.09.43BDF3F Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by imf15.hostedemail.com (Postfix) with ESMTP id EEBDCA0006 for ; Tue, 25 Feb 2025 03:46:59 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=dD2ToK3D; spf=pass (imf15.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.176 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740455220; 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=sizjJMN18CFgPA2Tk6DSLCCdGtkkhhDSym+HxOvkr+sxhAatlCMc2Hjp9PODmCtzDX4ihZ SrzOgsJFcPh8Kt+EYOpmBzJH0tIMFwBL7+wBS0X1IvGO6ql4jbC2oQYOsqGSrvIF86VrJb 009XwHUdlDtEOTE7XUcmEllFgW8VJKw= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=dD2ToK3D; spf=pass (imf15.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.176 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740455220; a=rsa-sha256; cv=none; b=zSpGgaSrEFQZwDPlC0BCq2mv9j91Me+afEmlx2ta0ekDHNLR8tTGB1Yue/4muiQ1vL4vyU 2r5lodov3wN8TfQatHbBOA4S9qlJGsJr+om9X4IMWGiHY1m9QOF0kuFgBxKcNvDEL+post jvgKonWJu65pvlmsD/1Qns0H4KUElgU= Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-220dc3831e3so95475985ad.0 for ; Mon, 24 Feb 2025 19:46:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1740455219; x=1741060019; 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=dD2ToK3D8zlCPM0BCXrtjUpJ6+8U0awL2L4pz525iaa3VUtwrGzo2n0Hy2ubhKJrqW fi7FH3DHVvhEegK7dVpVdOLJEkjjPpkpNhOaENS5aqZOK+R62fwjdi+axAshrBuGFcfb a4yFACVWs7zacJ0sS8PvZEn9iApSBIZ316nbFKsFwTIBwpJOpy7fOweJq8uvuQVFjYIf kwxtY1mmsGveRuSYPSAFXXLZlN447ogtayiVh/+9OBMrF4Yp5vReF/MGQPZO53B6rKie PByXvI1QNgUoi4SWqGQUMHW02noPVE7TpVFQR1vXegHVRHkcYzDAjNXjSvO7wqYoYTud MfmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740455219; x=1741060019; 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=IbzkO56aTjodx/Q1mYt7Dc989PUBOOwEy0Vjke5eID7SP9TS0W3WTv9SiE/Y5CwMV5 eVAnaHm0fSLOR3UAeTgfsLiYFzCrDlZKwkpL0or1jfuT/4dWJ7HxtBYxlLhoz9MLN63F 8C/tlfenrBdk/zElbwt1DL4RRTHVHoF2w+SRSw0yGh1m3H/hgwYCMEZp1y3Kq5TfZD4r SeHIBkDafWn699G/37b1hdiZYPjuE++QMmdIi2nVBfKUfTBqomEYGt6kTc50ydEDvybC DlhOh1evhY+jBfN3pl9B61cGfexGu4ujsda/6fl7l5sB81IwhJTSI28r7h8cIqwxUC7H 83Wg== X-Gm-Message-State: AOJu0YzjgQR8qIuhDhLwmqVVoEvKYZyfQWYhfiykagvdvXgGXdoZWixF 2EzcDs/nBWyV7vhXHwMTtZ8KAFLhrF7M5hyBiYZABCzWRtDEgns8DPFwCkbLSCk= X-Gm-Gg: ASbGncs+cYnyVwPhNhJ7Kdae7MyhpoL7OSqFXCZqJSXNsQg/Yon8eTDlcl8nqmewltf y/cBZZyzYxBjjrPVNWb0QwhiHlSUjtil1UHkLtGXw1SixxBZAEBd2SMtqIBgKStGofz6672uOsX mzQlUDbtsufiO6K19iFk3f0yH8O/AFZOZAGEiCLbB3IC94Oh3ZIhtf1X5AFI8FMSYjkZBj65+YH g5RLAoWbjpqbGhqNSvdNFRKUmDwlo1r8QEWH5IsLx0xCB32jvMt7jKm2Xi6nHVzQDB608iqxHwc KHsUHsdg6S29SRhiqIlD8rH+ATJ05PVLfWCA15d5Z/7yV5kJ6KkOl1oaWYe/ChcMOA== X-Google-Smtp-Source: AGHT+IGrbcobmtkudsLXXtQABX/QCxCZukDHXrGqT8EvNNCQnozfEzflYDWPRR+Q4O9sUaua1jgg6Q== X-Received: by 2002:a05:6a00:3cd5:b0:725:41c4:dbc7 with SMTP id d2e1a72fcca58-73425b7fd45mr26417726b3a.4.1740455218800; Mon, 24 Feb 2025 19:46:58 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([63.216.146.179]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7347a81f014sm409429b3a.156.2025.02.24.19.46.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Feb 2025 19:46:58 -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 v2 4/6] riscv: pgtable: unconditionally use tlb_remove_ptdesc() Date: Tue, 25 Feb 2025 11:45:54 +0800 Message-Id: <9025595e895515515c95e48db54b29afa489c41d.1740454179.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: rspam08 X-Rspamd-Queue-Id: EEBDCA0006 X-Stat-Signature: tfu7e6sdgre1w8et7amwgrpefwe96u6y X-HE-Tag: 1740455219-373374 X-HE-Meta: U2FsdGVkX1/L/OUiKBdBZa8B1ob1gCn8zv+kidu67XE68jiIdmsVHZiRuYsNhYJg1JZSXq5+J1qz942Nvg0pd9Re/R6OsSA7d44/Jcle9KLtoR+NCgH5XvACzcCvZEhChLDHEbRkjfmKvYOCp0e5M2aRM4JKlGjKd+deT+Nmfung9E9f02k/Rj7hQ8dHqNcn34sbmxpvLf23yIV/3mm1L70A1j25xV5oA9oD1nMpbDgpDzdSEKZ6p1fUAeIhiYjqWilgqBc7bC3nOqiguFkBmXmUtbW06Ez29JILXnVmDIUq5GOOE1a/rQ1rEUoYsW9SB2y167O2MJoN8JTx9RgwvaOqBJf32zFR8yp2EjNQ557A63Gf6rtD1OqD+ufqS4Aj36bSPpdVmV7RSPHHPMkEQHvAGMGr8v0JLvnHN9Fx7yn+em4GCeaAkeBXe2bVS2cwUswZC6s5ul/xnPnIc3zvETmzTXSy544dwTESl2i5N6M5aw4MelV0NnMzpD2FVw0+wVcwvhFErKAUf0dQixJdQOWX86agKVcv+fOKuEexsNFiMpLl0+AYYrTWnU+7Uo+zYX6FIGPZsnpop1bcjD9zgyund3QnFQCqbMoRgGUqQ8sb3ls+N4v3oiVskqw0ZBuJIjusgyQkgH2vH0aXtG5JykydcjW07TaWeMJEQdhs6Zw5twfeLIeJPlRajiw3nWMwawKIucAKQGHmXQomH7hBNYdMZyZy1DOttjJ09craNb/anbslvrkYq80bcRk3lBTWrHMBgde9bqSWZSQluv4Z5P39pu/7TKDkg5e/bs1P8vswta4Ns3Sp8ySxP+eJ9S9TSCA5GkNSkEgVZDmUnK/VJCcOjPF9y/s5TU72Cm6q23hcAS61k36+sa41UoRl2WyIzUMbVSakmyKnFBvgBLD65cW9dVJuEfB2Ea7qf/a0gLq2U/GFbfv+vJz6g4X7KGY1miwUALypODeKOqSN58e oivWhKnv /29hpn9NpiQ81fLoWfKRhf8VMuFkoE+Eghr2qDbIplA0d1m3yVYNKnoJEPIff2TcgZeObyS/TAUrnMAcE3hA/ALbZlSkyRH7i6HCa/Lg5ZGOsHGhIxHFEh2HyCQPqB4lM92MJHV6Yt7pE83a8bKsrpx02YuyCiCGlcYApMviUivy3SJxuSUFzCYIBluWJXQ/QkGSMaerVs43HyXqRMa/vsMf2+461OubXDv0OI6TiJvIrk787TppESwtA9dFGcviXFXBU13EFF6WSkz9hbRmfAbwFK5y5V6w2hfMvEyME7U43JeTwJlqFbR+ASmk7ypyF9LkO6bB7sJ7NszqAVi7Swn+VOdSNbCyB1UOxMNwbyUTyBS9fSGVbeAcorZesXCvqIjFfOJ8M3CzyRIfmjKLalOGeKUM7w35i1sfbw+WDtHjBlog9D0uo80z6XDBK2BkjBQaeVnQpoNWmor03IX4yxJRvQXWvTfmWH8gefA2Pc2zi4Vs= 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 */