From patchwork Wed Dec 18 13:04:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13913619 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 EBA26E77188 for ; Wed, 18 Dec 2024 13:06:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 798E66B009D; Wed, 18 Dec 2024 08:06:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 748516B009E; Wed, 18 Dec 2024 08:06:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C2FD6B009F; Wed, 18 Dec 2024 08:06:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 3DD986B009D for ; Wed, 18 Dec 2024 08:06:09 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id D62C180E0D for ; Wed, 18 Dec 2024 13:06:08 +0000 (UTC) X-FDA: 82908102228.04.4ED1080 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf26.hostedemail.com (Postfix) with ESMTP id 745D314001B for ; Wed, 18 Dec 2024 13:05:42 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=ca9+qcNA; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf26.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734527136; a=rsa-sha256; cv=none; b=fuZ62W6GplNKrWig7Un7gUs6A3/svziSYry8F3VcrKCDDGa5Z3flo5ei31LQxjgL3Rcu8m Cw1deZauPUZRNZ/paSTUJB3cieUzd9YCDV0xhlMZGb9aqFbiDx5w0vuaoum/+nYBnGZxZI 3ks5uavCJlxSFDPNliYBvfoqtms5jbA= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=ca9+qcNA; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf26.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.181 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=1734527136; 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=QtLDYyJyRe/cOzqFg0wwVyja+MW3+zRiiWZkoTiwjPI=; b=FnRQQWYdVMLWW0Ou0q22vp2kLIwkbtq0iTufJRSZCHT4yFqM7nw41QvEb29EO5jINxWGE8 ZO/h9x+KfMOgApKHfHYYGQe/rx09N6QyByjaQ5gR002sFhCDkWWLKY8Molu4NoeI1JtylN 6Rx5RPQlkKailHO2nZwvLhkUkAhKpBg= Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2164b662090so51898115ad.1 for ; Wed, 18 Dec 2024 05:06:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1734527165; x=1735131965; 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=QtLDYyJyRe/cOzqFg0wwVyja+MW3+zRiiWZkoTiwjPI=; b=ca9+qcNACGlwFTuzSdet28VMCCw3GnC80NnW3QFbgkXvEk4s5BWp7aHNnfvqu9tIt7 NLV6ouNAAjeqi2JIU8ToltaW+EaJyOmkXEU1mTjg5qUElpbhQPhICgXD3Fqaoh62GWba PFP3GAT1EG6Paz5ziV2XO7XgMhPY6pKe4hheDZPcFc+0pBeAxoaYAJ4FpNZak1hPKxKR lvNUXiqiiG3GVgs7Ph5W/LXaDgwGzjHdcsix5Fi0A16o26Zhs9GUQKpm67Yf1MMfu3yO BlYUGWEVMxiw6z4Uqj8RENRe55soYYXySQswSa2XrOxVTljqmKX8qcoTfTEbzNY9pJQS Ie1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734527165; x=1735131965; 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=QtLDYyJyRe/cOzqFg0wwVyja+MW3+zRiiWZkoTiwjPI=; b=fxZEChuS6cwv9Wlkol6toODOmp1cY0GJ9QEeUhtHQlQC6SJoatVm1Z1AEzpEsiRn/I Hq8Tih5Y+ErBBW2DNRTz30HmVElJ29COBVDCCeBXATu0AcqiTKOWfLP8f4VMH1tbWFcL io4q0LhllHwedSQBrDCJ27Vspu+CIVNy/ol8vzcXOt6xr6edKEzjWHvYr+0foAuj0KDT md2qi9Q0lZLvVNnVSoMzRticb71uLfy4kOoolxbZG2QNRpVFXE0HIM0jD8SetldsgvFj P+4PaqOGofmIpagPHRwRCJ4/XnPyPxUPA/BWMQHzXSkcXidnrYSc+FVbF1b6w3uS6htO m+wA== X-Gm-Message-State: AOJu0YzmKrQTZlQGibjVx4Ja/M+/c5eGWTaEHNek1mmCM3W6fGGiNIan Ewd3NIEiS7elzingoDy7F4U/4SNlkyA/QTH1mMa3msVd6XvoKVVMBGPreKKE30k= X-Gm-Gg: ASbGnctNpnPKJ9Cu3uoezz5bLlrxYZP9GbzsDISPkzLaXnTe9nKHONoA9ffZtV2W5YI 1+iTCVRSnIn1X+X7kL7z4rrGJEpuQOTZaqpueWzPZVPQkXAS7g0zoyMqOi2goMbTvPFoC6Cnu0d 9iwdPugKRDDYP5hW3BzptbVUkcm18XZyk/5dB/Eb04qg+uLse+uxsYqg50JdGgukauktNry2/D8 e7FVXgk8G7WQJ8CoLtOq23e22FBCqe0Felmyu7EG8oUfdHQ1xbfqbhRlQNSDouAMRDCWHB0TBCp 17JGFuUkLlP/dobYJJP6/Q== X-Google-Smtp-Source: AGHT+IFCnYr+lY/2cKu1iNZU0iqph0CddR5mvkArPJVeCIaobzeDvVooRJFIjfOJ9AL+45aiVbBYIg== X-Received: by 2002:a17:902:c40d:b0:215:a05d:fb05 with SMTP id d9443c01a7336-218d72161camr43962385ad.32.1734527165431; Wed, 18 Dec 2024 05:06:05 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.238]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-218a1db755dsm75751825ad.42.2024.12.18.05.05.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Dec 2024 05:06:04 -0800 (PST) From: Qi Zheng To: peterz@infradead.org, tglx@linutronix.de, david@redhat.com, jannh@google.com, hughd@google.com, yuzhao@google.com, willy@infradead.org, muchun.song@linux.dev, vbabka@kernel.org, lorenzo.stoakes@oracle.com, akpm@linux-foundation.org, rientjes@google.com, vishal.moola@gmail.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qi Zheng , linux-riscv@lists.infradead.org Subject: [PATCH v2 08/15] riscv: pgtable: move pagetable_dtor() to __tlb_remove_table() Date: Wed, 18 Dec 2024 21:04:44 +0800 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: mpsciahrdx954dr1zgpi359uzmq1es9z X-Rspam-User: X-Rspamd-Queue-Id: 745D314001B X-Rspamd-Server: rspam08 X-HE-Tag: 1734527142-460155 X-HE-Meta: U2FsdGVkX1+MF8spNkTNU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zYSUbkHO8DNhorW7OLZMnjNwPZO8pErF03YHkSpTrXUtTOAY53RSRgafTob1sQPNDtMcRyxE1yZ+RlyA/+dpnHZ6M82i34GGR7iB7BMKPO5W9x7YMdtZlM396pjBDDVJpo44Xp39Pe3S0JF+e088Xv8Q9+mTgmwUtDGBtvXbR1w/0Pdl7FlMF7XO0WuEfpEGJpJcr1qgxwGYW3aeTOTZq8BWdQTvNbRKYeF3x0j4CmhFTnP+rwVxQgxHoEHiIiDpxUYdPfm2njTP9Yt0zzgywS6fGQH7Td489sqk7Xspq3r9sY6PFGLWZ93FHj4zp+rIzQ7Lt+0qRJZxixZdMECmyrmFBijMIGr63yYrxMzohrA6nM95zZM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000261, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Move pagetable_dtor() to __tlb_remove_table(), so that ptlock and page table pages can be freed together (regardless of whether RCU is used). This prevents the use-after-free problem where the ptlock is freed immediately but the page table pages is freed later via RCU. Page tables shouldn't have swap cache, so use pagetable_free() instead of free_page_and_swap_cache() to free page table pages. By the way, move the comment above __tlb_remove_table() to riscv_tlb_remove_ptdesc(), it will be more appropriate. Signed-off-by: Qi Zheng Suggested-by: Peter Zijlstra (Intel) Cc: linux-riscv@lists.infradead.org --- arch/riscv/include/asm/pgalloc.h | 38 ++++++++++++++------------------ arch/riscv/include/asm/tlb.h | 14 ++++-------- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/arch/riscv/include/asm/pgalloc.h b/arch/riscv/include/asm/pgalloc.h index 09c2eff571a49..fc50d14010246 100644 --- a/arch/riscv/include/asm/pgalloc.h +++ b/arch/riscv/include/asm/pgalloc.h @@ -18,12 +18,22 @@ #define __HAVE_ARCH_P4D_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()) + if (riscv_use_sbi_for_rfence()) { tlb_remove_ptdesc(tlb, pt); - else + } else { + pagetable_dtor(pt); tlb_remove_page_ptdesc(tlb, pt); + } } static inline void pmd_populate_kernel(struct mm_struct *mm, @@ -109,12 +119,8 @@ static inline void pud_free(struct mm_struct *mm, pud_t *pud) static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud, unsigned long addr) { - if (pgtable_l4_enabled) { - struct ptdesc *ptdesc = virt_to_ptdesc(pud); - - pagetable_dtor(ptdesc); - riscv_tlb_remove_ptdesc(tlb, ptdesc); - } + if (pgtable_l4_enabled) + riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(pud)); } #define p4d_alloc_one p4d_alloc_one @@ -136,12 +142,8 @@ static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d) static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d, unsigned long addr) { - if (pgtable_l5_enabled) { - struct ptdesc *ptdesc = virt_to_ptdesc(p4d); - - pagetable_dtor(ptdesc); + if (pgtable_l5_enabled) riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(p4d)); - } } #endif /* __PAGETABLE_PMD_FOLDED */ @@ -170,10 +172,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) { - struct ptdesc *ptdesc = virt_to_ptdesc(pmd); - - pagetable_dtor(ptdesc); - riscv_tlb_remove_ptdesc(tlb, ptdesc); + riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(pmd)); } #endif /* __PAGETABLE_PMD_FOLDED */ @@ -181,10 +180,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) { - struct ptdesc *ptdesc = page_ptdesc(pte); - - pagetable_dtor(ptdesc); - riscv_tlb_remove_ptdesc(tlb, ptdesc); + riscv_tlb_remove_ptdesc(tlb, page_ptdesc(pte)); } #endif /* CONFIG_MMU */ diff --git a/arch/riscv/include/asm/tlb.h b/arch/riscv/include/asm/tlb.h index 1f6c38420d8e0..ded8724b3c4f7 100644 --- a/arch/riscv/include/asm/tlb.h +++ b/arch/riscv/include/asm/tlb.h @@ -11,19 +11,13 @@ struct mmu_gather; static void tlb_flush(struct mmu_gather *tlb); #ifdef CONFIG_MMU -#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 __tlb_remove_table(void *table) { - free_page_and_swap_cache(table); + struct ptdesc *ptdesc = (struct ptdesc *)table; + + pagetable_dtor(ptdesc); + pagetable_free(ptdesc); } #endif /* CONFIG_MMU */