From patchwork Wed Jan 8 06:57:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13930201 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 4299EE77199 for ; Wed, 8 Jan 2025 07:01:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C19D18D0002; Wed, 8 Jan 2025 02:01:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ADF408D0001; Wed, 8 Jan 2025 02:01:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8BC888D0002; Wed, 8 Jan 2025 02:01:54 -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 670F88D0001 for ; Wed, 8 Jan 2025 02:01:54 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2FB4CB0827 for ; Wed, 8 Jan 2025 07:01:54 +0000 (UTC) X-FDA: 82983389748.13.6B04F2E Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf13.hostedemail.com (Postfix) with ESMTP id 5A2502000B for ; Wed, 8 Jan 2025 07:01:52 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=bxdpY66b; spf=pass (imf13.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.42 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=1736319712; a=rsa-sha256; cv=none; b=1HYzvHLninl3SVwXnO4T/h3mtZA2r+t/hDOU2SpHSERpHKFn3C5+6sCFZxuCZjTgotes2p Z25SBrj2W+AdUQiaLb84qRrUX6urP49Mjej6BQ2JmiAXWBhgMUxsrBEA2LPpSi+/71B6mV 6iCm1KWZENv7sbPFNZUGUoYbwuGkuN4= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=bxdpY66b; spf=pass (imf13.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.42 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=1736319712; 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=xJFt+T0ZGRLwl7qj4QUye9dvBL3lzNJEqvkgjrbvemg=; b=ChUmoXf0JEgb5q/sUkQpolsd/bBTfEtDGhpRMOtfuH52MKju0CMRcLdhtJ45iy2QX8nBcJ yRoffeEDpFgJZ94oG/dEFn4qF47lcK2C7YXpkkHmWQOnrJcMqYin6BRSp2NBNwBsoRlwEL bPtcFTs6dW9o9zhP3cLySpprcgPr5CA= Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2ee9a780de4so17922299a91.3 for ; Tue, 07 Jan 2025 23:01:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1736319711; x=1736924511; 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=xJFt+T0ZGRLwl7qj4QUye9dvBL3lzNJEqvkgjrbvemg=; b=bxdpY66b7INs/MU2+ej0PHeeJ8qrGlkQWzqimUNOrIu54/sXeD9vcmD1Sp4An0qoBw 7mblXIQuEByaevfNPyOw9/ZtPD5EN3hMb197PWbAE+2Xmb9dHfxXNGCJ3hfmyqRs6lF6 Ws24mnnEjkSRX5THOW0zhibrZgDYC6OhFD5AW2OXAzeMav8CKzit0ez3z0+myiXgKtLH tiizKID25QlVr09G/RM7sIRSQRZDF0kQoH3x0k2wyYQ5Axs+7WsTfCzVDipPlYp+qILs VPyTSmB0mEblCJ6XYJ2+OKOSwl8vqjA6kqWqaXN1IeDEOxUyr+aL/rQoBNyDiu4z+Qmw ljzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736319711; x=1736924511; 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=xJFt+T0ZGRLwl7qj4QUye9dvBL3lzNJEqvkgjrbvemg=; b=I+N5xu9l6o2e8NujnKEsVw5EsTN3ffTfqQzE9W1Tw2IIlqLVOky5zI4/7UX0GdFtu9 f7jMHKswNkj8u2YzGR8V2Bgb3IGeMb54l+auAVRhNlrB2qQN4v0Uhfr+PDyjLyPwImNh in5VbKEgnd1K2YcLmMy4PhH6kcAfnOi9YcAYTxktEjRWVnJua7Gt4ybIilJzZ/E+l14O 8Z6Q/OO2HOIKhS8RAu1l6wK6WFVbRPNTqQeiYDLtsgNBjEdlMZ2uDBeYwAARe9jDgUVT 1RxhIobUZVN2iWlpc8zEG8+MlBBfOGaJsTO7HOrPgRKXLeF6moNEcIOTgp/ewAXA+3/6 cGQQ== X-Gm-Message-State: AOJu0YwkTqB4UQLY5CEJ+qNoEHdajC/0VI2Vi3oloDPDsE2M6FfVps5K t632SshcbNAzg3QRCJthLGF3Ghs3swxQvansjKL0mfexpqThQbGy0oduCUlA72I= X-Gm-Gg: ASbGncvYcStM7ZyJEHni6QwJnNUOFzxoqkRVsy5xUjWZLuQi9fp5zfRP9jXMoRthl99 NKsiXolqF7q9pTWQaScAif2NqE0o/8mYnd/4zHlvPKkgWpUsFDdcMHUKS+0RjfyoJL79xBePQ5X +p8qzZGFicEb28YQU9I+LbF12dQwI2qe/YDKOX1EHB9r6aBt5f0qXHZ4mPRjQyPyc+sBHO7hmEA o5NX1rV6Z/lElkeSOfPZZc9O9qnoNULOA10r0WqVj1vfE47UE436rX1JkJ8psTunKsjCkZGZngw 4W/O1soX54CHWixnExjqmJrn3UQ= X-Google-Smtp-Source: AGHT+IHbJSdEFlMcYFI5dK7LOGyjdYP9+xvlrgbj38Eli2e60ze0npY5GY+Stk/tuNQHFcVXscZRAQ== X-Received: by 2002:a17:90b:2dc2:b0:2ee:863e:9ffc with SMTP id 98e67ed59e1d1-2f548ee5378mr2382141a91.21.1736319711135; Tue, 07 Jan 2025 23:01:51 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.244]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dca023a3sm320067275ad.250.2025.01.07.23.01.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jan 2025 23:01:50 -0800 (PST) From: Qi Zheng To: peterz@infradead.org, agordeev@linux.ibm.com, kevin.brodsky@arm.com, alex@ghiti.fr, andreas@gaisler.com, palmer@dabbelt.com, 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, arnd@arndb.de, will@kernel.org, aneesh.kumar@kernel.org, npiggin@gmail.com, dave.hansen@linux.intel.com, rppt@kernel.org, ryan.roberts@arm.com Cc: linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, linux-arch@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, Qi Zheng Subject: [PATCH v5 10/17] riscv: pgtable: move pagetable_dtor() to __tlb_remove_table() Date: Wed, 8 Jan 2025 14:57:26 +0800 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 5A2502000B X-Stat-Signature: q9aeucx54j461s4ordk5das9hs6j9g83 X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1736319712-84376 X-HE-Meta: U2FsdGVkX18HEsEkiqEKbXqZes3T3OQ6pIqhi86LC6dZsIalM6mNpT2yNbSjGJ29coxfwGUhTZXYmgHuYx7ubdnMVJR9Yz5yMrbw+NZrLOJYTe0Q0NIRAntCL7qCHv2uDz+e0Oqxo72kBCzqmx2gGVTtKD4Y8yT2bXnhYJY8L7QdrhcuWpFiA0VWg94G7olEi8mUvl3whzIwSa5ZoXpDDTclWiNDDinlb0W7xJjLjL4CUil6hvviCY+uxpIThzsfEG8RXX/AM6R7QZl8gEQQXr4UPiIs7110VCMRMFwCKQpwq0mOI5BJm5pylsTlneIuKMpNCUZMacahpVyTKuAdV/9Nyyv6neXkyYqZGNEatOa6QjtaKJsixE/X9WYGg4srFsRkxrn2vAx5KFeFyhaK2zGDkVM+pxlZCztiwNCihDcVj36TYRwn06o+k+M8HrytSvazVanBiWp6I6uMEVJu2AqtpvRbUkPyIfm0rCzg5pTbFv8AOSyBSEbLh4WTlibspbwWrST3L8T5iEiHxTb9o60jB4HlGlAKcU7TTXdbuWoADcGqL4BDphEdxU5mGMwIDk7GEsciCSZAO9sMaS2L5shmNEY3T7rrcOC0HgRd2F63tqMsPQfY4Xt1+R0maK4EiacZIER9HpoyXvZU1oQv3nQaOjU52F8T8VwBy0eBDiY5YDRw/WF8VCCx3fd2J5Pm4tl6iLKK0Y6Y6TOG307ApicQKNPyT80HDtG6Pezr1KY5Evb8VGO7w/E/01DWShHfk9rTBb2Oww+iL1pMxCcKKSTbwS5Z0N58fXA+cqc43igcC3MQ8aZ5lpPM6cYSjYOIIOIM049LtsvlYEm9niK0O3PGEWuF+cCp1hYmkR/fiz1sXEnNigaG4BYJWQP3yQ7BWCX+bEmwOvRqXRMO7+IFpHpm2/Qm13CKhtEhXcMLDk/AEWdIzes4Mjo+5gTdseSHV1Zl+iX2K+/GaUdi+ww jPJ0qvJc 6qRwn3MRyX89a1iL2xr81k6ugheBkm76baADdn7pSKFqeMo7Kqaw6Sot/KBNgwzys1qTjIiFGKEQHuPT8VOAP5JfahUP8WX/SSExUKJAYQj3glsBTjd+9ygQkOWZAf/hZ7w7hm8TVNPC8Ih6JrGH3fK+lWAK8CEMFveW4p5FHn9e9bChmZNztMmYlEmVtxP/ZBb/rT9HinWrgOnhom5+pb2OsIvI1F3VFA+tQb4VHYzBizKWSLYVvt6NRwOL+mgFDfqrOu7TmVWiRWAVNGY0nGgWWZOz8U3iNM+Fy7B3IS2i159VAveyrgOHk/nhnlEpN02+Ps7rpCn6qG7ljsrm0nZdJ+iij50b9EU9wETG54usV77FzBfwZ5gIaFLmFTAJoNEI0PehmGm9LQV/iouXB7TNGo+CVcp5/3+QfbH0z89LnCVAsEhb+Cx9sFEG66FHyJvv/K0XiniewLPxIzzkTMvV8/UmEyEUkd/fV7Doha4p3XaS0rzSlmkJEWpLETkgvdegVo3gv4TvbcUrmnAYzMa7JCKzrmWcumiI4tnjXiWucqTk= 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: 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) Reviewed-by: Kevin Brodsky 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 b6793c5c99296..c8907b8317115 100644 --- a/arch/riscv/include/asm/pgalloc.h +++ b/arch/riscv/include/asm/pgalloc.h @@ -15,12 +15,22 @@ #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()) + 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, @@ -97,23 +107,15 @@ 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)); } 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 */ @@ -142,10 +144,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 */ @@ -153,10 +152,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 */