From nobody Thu Dec 18 20:17:53 2025 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 683C21D9A63 for ; Wed, 18 Dec 2024 13:06:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734527174; cv=none; b=VuIcdt/Kuxsdon4IWLwv9aspKC9WdEcdZ0q38PF0TM6KtBDlDAaYPhMOFVW8XCnC9td1sVFdTEr1U3bU9lPqfltrBzhPiXCmGbpAciJBfa7zGWNHLNBJmyHWgKf3Y1u0NHxwFSz4MCAgPaSmWAdgQhlfXRz3Cw6UGGROTL3YUps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734527174; c=relaxed/simple; bh=0DrkqJj4wQeX5ghzMY0LbiVqPzUVuk7cM17qWFCGuf4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=k68eg7dzk0hvd2w4OlGsX1B/ZtrOvw/buuayue2EUfmwLXbYuxDVPBOE1/kfwdRxg8VgAFOjJIl15nVgvyyB0q4Eck63vBY4MJL3cF86pnm+w3M2ME0HT0xq6f40Onebnbs+nXs5JLvpxqGNR46mBolyo0P2+nHV6KvFjAQy9iM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=YOuTY/Bk; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="YOuTY/Bk" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-216426b0865so56770525ad.0 for ; Wed, 18 Dec 2024 05:06:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1734527172; x=1735131972; darn=vger.kernel.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=Rs6Lkouarv58ZoKb+WtHLVo9G63kikGgJGrD7ursbxc=; b=YOuTY/BkplvGenXkNyhw0r/LDzIgbDvBeTnEXlYPlDhkOE1ItQZbACM2T0qrT14zNn YMsEBjIEvdJgxbVKbo6zpGljjNcJcg9BnIanqN3zctgnrvX1DD9MmiaMHjG3i/r8PVzT 0a+H5hoLQZehJtdb3SbzPvuGGDklrPTdYmMw0ABk1AKe5o2fV+DIZAYAudRGOzpM5Sly EN7klHjcS6x3YLrJgtpOA19EeX0X5knaoDwoOv9K7ZFdeAa5QLciDdeGVg5yA6HUjnqA Jj8rzUNX1RT+loEFR/9NOVLr8wmQDWk0cgEUuqcfH7DzOJFdgklUML7W/H0ESLWIDSMU VHEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734527172; x=1735131972; 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=Rs6Lkouarv58ZoKb+WtHLVo9G63kikGgJGrD7ursbxc=; b=Jp/Qp1xE37df9WevDjQXyXpAY2+cYk63qlcYJw3r9P9hR5EyHNwCknMNLkqmScuGVu v+ZZi/47ThWRzKzN9TP6tJdJUGkyr5iz6Cy+liBygFueTc7Z3QuDpKWn6Kt/q1i3Toak AJB9VIYZFK2LGgC1aJeDBXF68F6Bs5wnSmaqhtb5ZUEx1uD0h34P0ccuRnNdaWuyUV0Q RnPA9poAln4RVTaSRZxmE3SLIXR+isCIjiR9/Z3XIt7uJQfzv2DXRUlQDKOJZw4O6NpS 1bl42gacc4NfsJmwb0NEfws7nbwpaptCxAw+GYvhQ5TcGKDAtQwaOzIjIYSs+tzhDBpv ORHg== X-Forwarded-Encrypted: i=1; AJvYcCWL7R/VgNLDUHeIUSUKVziQeDHo1Uz3QwqUosuBbGzXINIbO4ZV1rKDfBDc/UjO8XsHBILvDIzSLNnQR7w=@vger.kernel.org X-Gm-Message-State: AOJu0YzXhU+uBsJxxxffwsJ1gwVnj0hM3v3TH+2ZF8QHaJtTUaktA9RO HF9ULDrIPyDIJnvlhTwQBJ1N5/J6B5VBu9ND6p/wHDeOlbyor7qyrwvzm4hg+bs= X-Gm-Gg: ASbGnctA+m4KhWXNNxnREao3e87qhEcXMjOIvhzJjx9RJPU/QhDaASBcijjwGy+cBi7 /oo4f1yiGI0lPuMh2lQ3TaRYY0PB4pYMmg6DcLsLQZUJFpDo9f3dlr8ILxpOOdH1uZ4rwAf2/Zu KeuWYj9H5soA4T46YohAicPiEXj3FzPfCsbruzVts61VofqN2RvtDGn5nB1ZrNPe3IArwfe8nZu rMcrW0IJxM4vycA1p4BO/i6gAfLcLB/xz1SY0DZlQUWVqvnwjgRTfn3PCiFFlqfLUGJi8Z/2lM0 UzEvMe4YUF2pSQU80EJxsQ== X-Google-Smtp-Source: AGHT+IEwCZCDbOJ1SqMAwJwdz2DwBH3whjOpJ7w3Lr+F6G2curZkg5Sqfpo28wJQmsWqUUWcuoXMow== X-Received: by 2002:a17:902:cec3:b0:212:6011:594a with SMTP id d9443c01a7336-218d6fbe5b2mr36617165ad.3.1734527171662; Wed, 18 Dec 2024 05:06:11 -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.06.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Dec 2024 05:06:11 -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 , x86@kernel.org Subject: [PATCH v2 09/15] x86: pgtable: move pagetable_dtor() to __tlb_remove_table() Date: Wed, 18 Dec 2024 21:04:45 +0800 Message-Id: <8c0d97d2a538cafb94cf30f5c94a84a0f93f2f4f.1734526570.git.zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Suggested-by: Peter Zijlstra (Intel) 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 =3D (struct ptdesc *)table; + + pagetable_dtor(ptdesc); + pagetable_free(ptdesc); } =20 #ifdef CONFIG_PT_RECLAIM static inline void __tlb_remove_table_one_rcu(struct rcu_head *head) { - struct page *page; + struct ptdesc *ptdesc; =20 - page =3D container_of(head, struct page, rcu_head); - put_page(page); + ptdesc =3D container_of(head, struct ptdesc, pt_rcu_head); + __tlb_remove_table(ptdesc); } =20 static inline void __tlb_remove_table_one(void *table) { - struct page *page; + struct ptdesc *ptdesc; =20 - page =3D table; - call_rcu(&page->rcu_head, __tlb_remove_table_one_rcu); + ptdesc =3D 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); =20 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 =3D 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 =3D 1; #endif - pagetable_dtor(ptdesc); - paravirt_tlb_remove_table(tlb, ptdesc_page(ptdesc)); + paravirt_tlb_remove_table(tlb, virt_to_page(pmd)); } =20 #if CONFIG_PGTABLE_LEVELS > 3 void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud) { - struct ptdesc *ptdesc =3D 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 =3D virt_to_ptdesc(p4d); - - pagetable_dtor(ptdesc); paravirt_release_p4d(__pa(p4d) >> PAGE_SHIFT); paravirt_tlb_remove_table(tlb, virt_to_page(p4d)); } --=20 2.20.1