From patchwork Wed Jan 8 06:57:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13930203 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 303FFE77199 for ; Wed, 8 Jan 2025 07:02:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B7E176B00A8; Wed, 8 Jan 2025 02:02:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B05C76B00A9; Wed, 8 Jan 2025 02:02:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 97F856B00AA; Wed, 8 Jan 2025 02:02:25 -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 38A5A6B00A8 for ; Wed, 8 Jan 2025 02:02:25 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E917E16059C for ; Wed, 8 Jan 2025 07:02:24 +0000 (UTC) X-FDA: 82983391008.22.494CFBA Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by imf11.hostedemail.com (Postfix) with ESMTP id 1AAD24000E for ; Wed, 8 Jan 2025 07:02:22 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="j3/kd9iU"; spf=pass (imf11.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.51 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=1736319743; 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=DRkfj+TNyCYVuLoiDWLKznOk7pFXzbzTll42IhJYX5s=; b=m53Cxup8KX+6QqvXPOF63FEHqIPiQgG2sduZpjKLGU/qB7Te0JSOaeRBZMjPey5RscdLXZ n6kf9VnYcm2IzuC4TDeo1j6bRtT41vSeDx/nFuNln4TtYyJH7wqwS+WLuAZqGesEjyc9sP 8Fsr1nzgE4QUJqmbHscDKadIiFBBIQE= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="j3/kd9iU"; spf=pass (imf11.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.51 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=1736319743; a=rsa-sha256; cv=none; b=C8ODbD+aiZoxaC1tM+CTQhAmiM8XBXenx2boCujQEAoHpgqR0TADZEc+6OziAXABl/voDl hQRijaPfl4lo2F8sMDy0UijjSwNdpz4YZ9ktAv/96KhezL5GWLVWzAAd+1/i81URAV8kQp y024J8SM26r03ebNSZnZUIbqkZW65OI= Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-2ee50ffcf14so841090a91.0 for ; Tue, 07 Jan 2025 23:02:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1736319742; x=1736924542; 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=DRkfj+TNyCYVuLoiDWLKznOk7pFXzbzTll42IhJYX5s=; b=j3/kd9iU4IuMGNnap01jSPfZ6N/wJsKru4YY9BLSOtccTrWVNyJeFDdWw4OwfN15qd n5zr5OH3ZjQLK7ouTBmDCuX8vF20cRTBvJBiQuKJ5BAYdC3I65CtfkHfCa1YDNzrbgcv SiBipnYJxpmmsu+gds6wJ797JdePi6fkrH5KeydTf8uYxk70MuilSPX7vvtoODgi+eTf 4Qx2t4JXEveHA/r1fOIPJ7Ivd+uROnQ++/lraQisDV1/nJDIWPUgu4vfQNG/KbuAyHwb SjxH3ojqrLfmPRAxgcquRyi+RHRPdX/GwNjF6ug9NCjHAWmktNABp67v5aq6rX2SMUsI Rhjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736319742; x=1736924542; 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=DRkfj+TNyCYVuLoiDWLKznOk7pFXzbzTll42IhJYX5s=; b=T7SXPhcS3KQPHj2mUvB5XqvF8c1SziICtvoOn0KUQn7mggGaLdr5yhVtiwpQZ2sOCU GHyAAoOO+Kcrnay3XUXMtZ+E45E2GGJR1jlTwBS202vudw0nwKtoEt8iNZPbrJcjUnyP CLbbWm2X/NSSPOAwkChlJ5oQzwTlmao+V6aFCgExYWXt6Uj6n7AKDvYN/+u1NBFzz/R4 q/RP6rQaRyfIhAd4Mkb3YVn4lc9ADGPnKzehqKdpW/IKJr0DHa6GIZhjGtBSbj3H6fcE TZzvG274P3tXdpk5FB7cJ6H+odAN7s7FuKjoJVeMVIrCLKSZ/onV94Dd9vPh+giNV+C1 37YA== X-Gm-Message-State: AOJu0YxwohUbbtZTO7r1/PtqMEJkiV2tRI6wln/tKo58mO188eU1ICVE EDYqcgulPFdTDaJ9bZDsO2ukMrAL9Bmpdnp6qmSAknLx40+W+3sizr6x0pKegf4= X-Gm-Gg: ASbGncsO9Cq2+zwGW4rWmk6Cg0RUi3H81sf7686zgtakkZOcps3MBQ+zy8QNaoS0odW gICW0CHE0n/UVOv/r8ETvFWSfJQeFhao2poIQ3Kwg3WffRxNi/hrMwlrWbKLHIhmVqf1QODgvdy Ac1fstJ7MECSYrNjN3KuIgDDZ6ivSrHMx3TOi9wDJmvF/u5xVPQihdFpVMWSitT8Vy4qTAfdcSb lXMnpK+LXNqW5BYKHVbDpnb5z+iSXkl1JmFsI2ZqVYRz8ND1YpafIdU2UZmqiXKbmo2RBtORjYP 5l+d5GyIlje57j8YL29+qIzUJxA= X-Google-Smtp-Source: AGHT+IEyar1kSnzqQimnz8qjA8akoGYyLgYubsk1qekCNbk2Sp0If2dwICCDFtvO4jCrehuyEyMuwg== X-Received: by 2002:a17:90a:dfc4:b0:2ee:8cbb:de28 with SMTP id 98e67ed59e1d1-2f548602842mr3182102a91.8.1736319741885; Tue, 07 Jan 2025 23:02:21 -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.02.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jan 2025 23:02:21 -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 12/17] x86: pgtable: move pagetable_dtor() to __tlb_remove_table() Date: Wed, 8 Jan 2025 14:57:28 +0800 Message-Id: <27b3cdc8786bebd4f748380bf82f796482718504.1736317725.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-Rspamd-Queue-Id: 1AAD24000E X-Stat-Signature: ngy96sit6qo85f5h7e7rs9ywgwxanwbq X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1736319742-805075 X-HE-Meta: U2FsdGVkX1+aEpoUmdp2MTSjVFackfKgsaPGpeR4XufcG+XWeibU419Dbv7jCM7oa4wNnW+EEx9KAognp31eYCJo4E4mjqrQJustXpfVdpUqWtqw6QQ9e5mxxM20dCSIRApZBpUzPnBbnAYnE96SFb1vfc9mHkDAj+xni7/afnCOP/I9hhtUJT2hmbTe1/Wm5KgBH3rZRdZv9DQp40DgYcyjH66pgKZbDVlgkJIE89UwSDhwhtgohvbAwLAYwWP1bmEbAX/qt7g/YcD5VaIunVwpFZz0iJUDVECHciF1HVZ13KFNwMRtO8yn1W6Rj01fAieJdNadM+G/vUiGtMQLLlfZetm/x/D8pLgGMpkJCPvO5ShTTWFJXzWib5EN9lMlMq8GKlj5uWXurhkeKRcVGhiSIfmWomuQ3FYlNZn8eHXsyDs1CJzCpQB/ZFNlvBFdxzXLaGSmB+jjJoJ1CD/w7NTcMPSfKTcmdKXuiBc4Tdua9rOKBJ7T3qSf/sAH249PxyrIj3v2uwptAszwsIZxF/5QY7rtZZK0QKZwYHM0lfPjYXxQhXuTQ5b9bc0ZfuGrAKv6V4mBE3HsO5Zr5cmW/l2aHfqTpELlS6/5j9dzleP1S8Ih0vUacRyAzIhVufgOW9h7Y8+rLNLVaqwLXasuOM9ni07uEYFcK24Et0Jyj0uKEwVdMgTBsxDeiVxLhaGJGMmZKcz423f/DgiosZMXQ7PLRiuk0ZhpCj2s7udoJ3+ybCIeHsGd4vcUJXICWK30guD/t4+qVYHurAJmH2m5vnmH0S7d6l4AIpcAvA4UCFATF92LrXkT0rnAdMUM/eB04WSnlLhVfnWUU6QGAERVENYpI7iQe94N3/BeJAydQGbGhUi6OCD4bAO9Y779t78yP/fBASaB6lFcIwwF68gvCa6h6CZJSsizwyyv6gwcp+aP9mPcixlQRiZgp30kVxfTMrKUuhLfiXbGiD8msPN F4pqbuCi 8k7/vNGjkn6CuX2SfdtZ9vY44RFKlzgrJc9+yShS9chvZ/faMotHZK+bOr01wLPW4btK8ct+2BLOJQUL7GhT+UAvFb+HT1zOpAP5dAHnrskM77zoQ96yhHP5lnha6OyBWe2crTvre3KkvVqqWiSjoZ+S4RDSWdC+wY9HldRxxUz0XhG6An62FTa0GNwCoipzK9kSvRG01mjf7zwKRpUVMc/zsXGKpVnmcRCzGz97cJxl1I6CYOuNmp3+7fiBOIFB83Q2o/qQetRtjY2dHZtI7flgL82GggMwweiXllvBI2fFir+vT1dLWZ04i+zO+O+16bt7KVQ67BTPvLWrgaEQoE8hkvL56eQkEE+XcE0hRTRInoi/J1O9V0GQAKizJOyS7ySTmPHl0uht9VkUNhiUOCQA8o00xAPVU7hhgf3fLdS5JpQosNt4rNFo7JaeOavEuvvwlfhgjAz34pfHQh66sbA9qzPGHG9Wf59A7LV2F1UDfVg5bFKQ8b7vOIzCZH+f7VoP0Q/mgAd62TECTCd2kWYT6vZ/eePXG4vNRL1vfuQ+tx57HfPgLXd+A8BWrsHtLdiT8 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. Signed-off-by: Qi Zheng Suggested-by: Peter Zijlstra (Intel) Reviewed-by: Kevin Brodsky Cc: x86@kernel.org --- arch/x86/include/asm/tlb.h | 1 + arch/x86/kernel/paravirt.c | 1 + arch/x86/mm/pgtable.c | 16 ++++------------ 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/tlb.h b/arch/x86/include/asm/tlb.h index 680ec3d47915a..f64730be5ad67 100644 --- a/arch/x86/include/asm/tlb.h +++ b/arch/x86/include/asm/tlb.h @@ -33,6 +33,7 @@ static inline void __tlb_remove_table(void *table) { struct ptdesc *ptdesc = (struct ptdesc *)table; + pagetable_dtor(ptdesc); pagetable_free(ptdesc); } diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c index dc1ab9301d2c6..1ccaa3397a670 100644 --- a/arch/x86/kernel/paravirt.c +++ b/arch/x86/kernel/paravirt.c @@ -64,6 +64,7 @@ static void native_tlb_remove_table(struct mmu_gather *tlb, void *table) { struct ptdesc *ptdesc = (struct ptdesc *)table; + pagetable_dtor(ptdesc); tlb_remove_page(tlb, ptdesc_page(ptdesc)); } #else diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index f9516024cbe5d..c02aa0427a6a5 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -25,6 +25,7 @@ void paravirt_tlb_remove_table(struct mmu_gather *tlb, void *table) { struct ptdesc *ptdesc = (struct ptdesc *)table; + pagetable_dtor(ptdesc); tlb_remove_page(tlb, ptdesc_page(ptdesc)); } #else @@ -62,7 +63,6 @@ early_param("userpte", setup_userpte); void ___pte_free_tlb(struct mmu_gather *tlb, struct page *pte) { - pagetable_dtor(page_ptdesc(pte)); paravirt_release_pte(page_to_pfn(pte)); paravirt_tlb_remove_table(tlb, page_ptdesc(pte)); } @@ -70,7 +70,6 @@ void ___pte_free_tlb(struct mmu_gather *tlb, struct page *pte) #if CONFIG_PGTABLE_LEVELS > 2 void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd) { - struct ptdesc *ptdesc = virt_to_ptdesc(pmd); paravirt_release_pmd(__pa(pmd) >> PAGE_SHIFT); /* * NOTE! For PAE, any changes to the top page-directory-pointer-table @@ -79,28 +78,21 @@ void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd) #ifdef CONFIG_X86_PAE tlb->need_flush_all = 1; #endif - pagetable_dtor(ptdesc); - paravirt_tlb_remove_table(tlb, ptdesc); + paravirt_tlb_remove_table(tlb, virt_to_ptdesc(pmd)); } #if CONFIG_PGTABLE_LEVELS > 3 void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud) { - struct ptdesc *ptdesc = virt_to_ptdesc(pud); - - pagetable_dtor(ptdesc); paravirt_release_pud(__pa(pud) >> PAGE_SHIFT); - paravirt_tlb_remove_table(tlb, ptdesc); + paravirt_tlb_remove_table(tlb, virt_to_ptdesc(pud)); } #if CONFIG_PGTABLE_LEVELS > 4 void ___p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d) { - struct ptdesc *ptdesc = virt_to_ptdesc(p4d); - - pagetable_dtor(ptdesc); paravirt_release_p4d(__pa(p4d) >> PAGE_SHIFT); - paravirt_tlb_remove_table(tlb, ptdesc); + paravirt_tlb_remove_table(tlb, virt_to_ptdesc(p4d)); } #endif /* CONFIG_PGTABLE_LEVELS > 4 */ #endif /* CONFIG_PGTABLE_LEVELS > 3 */