From patchwork Sat Dec 14 09:02:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13908434 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 D44B7E77180 for ; Sat, 14 Dec 2024 09:05:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6A4B66B00A0; Sat, 14 Dec 2024 04:05:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 654F06B00A1; Sat, 14 Dec 2024 04:05:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4F4DB6B00A2; Sat, 14 Dec 2024 04:05:03 -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 28D366B00A0 for ; Sat, 14 Dec 2024 04:05:03 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id BE6731C6CAB for ; Sat, 14 Dec 2024 09:05:02 +0000 (UTC) X-FDA: 82892979792.29.FD869E7 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf16.hostedemail.com (Postfix) with ESMTP id A21F4180005 for ; Sat, 14 Dec 2024 09:04:33 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=THeJCdVb; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf16.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734167076; a=rsa-sha256; cv=none; b=c4l3SZELN5eU83i52/HQ4yE5Z8jS4DLN7TUu7SQu1A3GuGTePQZ8uFJkHnQ+iHi7PiWNYU yrjHwVVGZnDy1ROgEXevqW/MmK8QDkiE4tMnHnYar1T53bcsRZrGzyE0yKXmwmlBJrpBFo tlK4OorRCvaJ7Hi6LJVaVwOrHglfk/I= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=THeJCdVb; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf16.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.210.172 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=1734167076; 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=RMCudVMtlfI7I8C+EWzj20f+Ae2SGvA6pNu/2M7K/mw=; b=m16WMGFAKdV717uk3SEuhkybtRpwcIjeGWtLgMxihTK1iup6U2JTkwY6+HHMj2JQIHMIPD Tfn9gHfsOHfcdzFSg03FjAxuDadGid+JORklQWcBiSJKBTJzUUMLp6oYaROSQqYCS8f370 VeU7oc1AyoXf+/znB+CfVGtpUnhqmJE= Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-728e1799d95so3059221b3a.2 for ; Sat, 14 Dec 2024 01:05:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1734167099; x=1734771899; 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=RMCudVMtlfI7I8C+EWzj20f+Ae2SGvA6pNu/2M7K/mw=; b=THeJCdVbwCvsy+ZnLSfI4huQmpybCMZHwoqseyMcrfCiWQPB//QJyHAJpG5Yt5ffJm zlewrmh/iTRT3ZKOgQPqzBEalJUvbYsYPrQAd9aFpPldI8x90wrbgSGScCyEvr6GASYg px1lTtm6sYHdj52NgYZzZm9FWjMIYQ5EqcbRukEAodAhQRw4Wh6vEA7aIUyuYwZMw9JE 9vI3cQzHH04KJBIJtQWneOPqUEq16FvItAaHlgjDDz5V2zXDd1KVTSPtfV/hx0/jL1dl 6lVVGi++2O+AyBU777ICHEJQOE9bNQjfLieXjTEgpHH6uEP6CELVNythbydw0Zy4wxR9 4NfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734167099; x=1734771899; 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=RMCudVMtlfI7I8C+EWzj20f+Ae2SGvA6pNu/2M7K/mw=; b=J1ZajFQkGAdLc2z8ia5zNh0YRTIhWHqRaZ2/AJj9Iz84264ifiEX+CV4fRnNFjihEe WZbbl73Jy3v2uhY80Ulto3qjG7Kzsv9F9NQwTJYcdjViHPosXMym4ES2XWFJyslZurAy bMvo1jOG3th6KO13LefFBS21gVXZR3Lppk4SACX5DxIwOE4U7mBwSl718W+jWtLce6Up ZgY47TgVKLFqXSu87+vd59rx3dpMNsw+AZ7NA10XZq7+fa496EJMhfKPOK/ZfEAXBdzh z+zfHxWssNVWtFvXCz/zlb7y1lg2j975V5XRcMvyhWetgmwLWO2MvVwTmOp3WmB9MyUI inqg== X-Gm-Message-State: AOJu0YyzVh4vhV0f+nAPumMbqmgPpPmXjS35VSJGzVe48JpqaS62TUZK FoKfTqIEqPJMjwb9oS+senrk/3kbiFiXRGXxRuaBnS1+wl4BIN8RQSh54kINphs= X-Gm-Gg: ASbGncvfiGeDg+ON9v3rdGFCOb20K+AMURNP/TPToW6fs/cXVqYhQTFqZQvg+58/V6B wSTmOBhicetG+IuoroFiyClguZH8TkCTePMk27m0D7ADFXG4Ctx8qbn8Yc63+4wMkHCGyX7OH8a fcN9w1rqjZcPD3LDkqw7QF95ScJEh5HGOIVXgF2MBEIKsJmQ6ctU1R50fh/p3KXrKW7NbC0f3im 1b6Ss6mryRkPI3dnCFvREuuIFxmFSyeVwVqc8waWCowAglvG5Ix3Kht8PXq2eNDTOv1F0DtSJAp XI0/uOzGhWV19djIk3YKUTOM4dg+ig== X-Google-Smtp-Source: AGHT+IHNImhO/lIwU1CxZtuKJT3uG8XCXYTC5vUDdEevSxX9VuCSFNAuRKGmdb9G/Urv3IN3FNQNtw== X-Received: by 2002:a05:6a20:d808:b0:1e1:c0d7:7502 with SMTP id adf61e73a8af0-1e1dfe1d265mr6847659637.37.1734167099625; Sat, 14 Dec 2024 01:04:59 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([2409:8a28:f4f:a9a4:5d46:4ef0:daa9:1db8]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-801d5a90ba0sm905731a12.16.2024.12.14.01.04.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Dec 2024 01:04:59 -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 Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qi Zheng , x86@kernel.org Subject: [PATCH 09/12] x86: pgtable: move pagetable_dtor() to __tlb_remove_table() Date: Sat, 14 Dec 2024 17:02:55 +0800 Message-Id: <01a0ce994f5d475230117b2991566cd7e2ff1dcf.1734164094.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-Stat-Signature: xhqh8iri9kd8cswt7bpkn4u6h1xfmj73 X-Rspam-User: X-Rspamd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DjuAbsg+MFy2VcCkUqz4Kq0nr7a3mJ0EjAcye9ZscqB0UhtkFdMPVTmXIsU4MwJECqyc9xOlm/pYe9s9ifJlDIk09p/4MjBafSKupQBlTegf7aVpVJrKpxo4h/kE2XnqSC3T/nrucwggJz0X/bpcd1hYRvFHoip41wRCqA2oueJ+//1cvHmshe5ZLkjqqYUfC8LIp+XfLUtr0TpoNUIZbMN+v+2S5Kt44sYG4l6YCFb3gXNoRj0uSMOqMB3lsCj8xm/Iuw3D6gT+VYRMAGtZM3GKAomwNABhLVdLNDHyUxZupqquDrUkrJb9T/cUmUDLXSB3TQni7GPU9OLGxoCYBFsIRgZr+bnYmbCQlw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000006, 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. Signed-off-by: Qi Zheng Cc: x86@kernel.org --- arch/x86/include/asm/tlb.h | 17 ++++++++++------- arch/x86/kernel/paravirt.c | 1 + arch/x86/mm/pgtable.c | 12 ++---------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/arch/x86/include/asm/tlb.h b/arch/x86/include/asm/tlb.h index 73f0786181cc9..f64730be5ad67 100644 --- a/arch/x86/include/asm/tlb.h +++ b/arch/x86/include/asm/tlb.h @@ -31,24 +31,27 @@ static inline void tlb_flush(struct mmu_gather *tlb) */ 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); } #ifdef CONFIG_PT_RECLAIM static inline void __tlb_remove_table_one_rcu(struct rcu_head *head) { - struct page *page; + struct ptdesc *ptdesc; - page = container_of(head, struct page, rcu_head); - put_page(page); + ptdesc = container_of(head, struct ptdesc, pt_rcu_head); + __tlb_remove_table(ptdesc); } static inline void __tlb_remove_table_one(void *table) { - struct page *page; + struct ptdesc *ptdesc; - page = table; - call_rcu(&page->rcu_head, __tlb_remove_table_one_rcu); + ptdesc = table; + call_rcu(&ptdesc->pt_rcu_head, __tlb_remove_table_one_rcu); } #define __tlb_remove_table_one __tlb_remove_table_one #endif /* CONFIG_PT_RECLAIM */ diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c index 7bdcf152778c0..46d5d325483b0 100644 --- a/arch/x86/kernel/paravirt.c +++ b/arch/x86/kernel/paravirt.c @@ -62,6 +62,7 @@ void __init native_pv_lock_init(void) #ifndef CONFIG_PT_RECLAIM static void native_tlb_remove_table(struct mmu_gather *tlb, void *table) { + pagetable_dtor(table); tlb_remove_page(tlb, table); } #else diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index a6cd9660e29ec..a0b0e501ba663 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -23,6 +23,7 @@ EXPORT_SYMBOL(physical_mask); static inline void paravirt_tlb_remove_table(struct mmu_gather *tlb, void *table) { + pagetable_dtor(table); tlb_remove_page(tlb, table); } #else @@ -60,7 +61,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, pte); } @@ -68,7 +68,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 @@ -77,16 +76,12 @@ 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_page(ptdesc)); + paravirt_tlb_remove_table(tlb, virt_to_page(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, virt_to_page(pud)); } @@ -94,9 +89,6 @@ void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *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, virt_to_page(p4d)); }