From patchwork Tue Mar 25 21:48:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for David Hildenbrand X-Patchwork-Id: 14029570 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 3ECC2C36005 for ; Tue, 25 Mar 2025 21:48:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A35CD28001E; Tue, 25 Mar 2025 17:48:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BE36280005; Tue, 25 Mar 2025 17:48:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8378128001E; Tue, 25 Mar 2025 17:48:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 61D6C280005 for ; Tue, 25 Mar 2025 17:48:52 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2E42D1CC058 for ; Tue, 25 Mar 2025 21:48:52 +0000 (UTC) X-FDA: 83261413704.15.8B9EA19 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by imf17.hostedemail.com (Postfix) with ESMTP id 10F9740002 for ; Tue, 25 Mar 2025 21:48:49 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linutronix.de header.s=2020 header.b="S/qVrLN+"; dkim=pass header.d=linutronix.de header.s=2020e header.b=8FzytFPU; dmarc=pass (policy=none) header.from=linutronix.de; spf=pass (imf17.hostedemail.com: domain of tip-bot2@linutronix.de designates 193.142.43.55 as permitted sender) smtp.mailfrom=tip-bot2@linutronix.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1742939330; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=PozTjzZIHXCoE2XsH1fqAnijL42WT5+rQ4TjUP/om0I=; b=1Xrmm6fP4MAmO7OtX4kktPLdR538Jb81aw8PKNgOPnNspx+/ff1PoCRC+VOqLwJZw2zUBs qyyIXLTTKrwbLyYMBqo4wvskFsHNY8INQIz2ujDR9Yc9oA9JgWDY3uvAj4hug6iB9a5J98 QzKSV8QijPWE8s9LUG3t+H/hFGaoWSY= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=linutronix.de header.s=2020 header.b="S/qVrLN+"; dkim=pass header.d=linutronix.de header.s=2020e header.b=8FzytFPU; dmarc=pass (policy=none) header.from=linutronix.de; spf=pass (imf17.hostedemail.com: domain of tip-bot2@linutronix.de designates 193.142.43.55 as permitted sender) smtp.mailfrom=tip-bot2@linutronix.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1742939330; a=rsa-sha256; cv=none; b=pCIztzZFM64vPJ/TVqd9Yma0Nu/Fi75bifXrgLZdQ/uUmrhERY/qFFwtGqIkzAUp+FL0Xv ZjC1vAWSV+BBRI0vrynIdMYPUoQJl7jzAxoGd9jbOt98y4IKEYz79vCdoQpUoZqtZlBdi8 4yCGtki3Bvc/00aeifQRJZ5rnxx5IM4= Date: Tue, 25 Mar 2025 21:48:42 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742939327; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PozTjzZIHXCoE2XsH1fqAnijL42WT5+rQ4TjUP/om0I=; b=S/qVrLN+cYqKGTefHY4cJ3MPHWGeab1Ste74O2SYEbcWrD88q6hJt1JlKg+v2kybfZMA7k rcpQn/ojiFa/eDIrWjFR1ALMt5aXgokX3V08SYUUgewNZazrXzhuDD3OC6l/FkfZJlsukE AVg8mhTFv/OTLsTg7PNiFo73Ae9TeDjVUFkkLomZd9c+iuoq7mXDRA+x0ZAJkBe8fhnonM bMo4c0aQDlBkC1MeyQ0b2akC/1DGTKl5sXIDB2RmwCky0bOAdGDg9Y9TBwvW7DjuQOvbiB Y9ETttg6Z6QoYInHa3CqPLbNLKWC6Bd/O+qGkOOL5dn5DVhy4ZMN6QE80IB2nw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742939327; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PozTjzZIHXCoE2XsH1fqAnijL42WT5+rQ4TjUP/om0I=; b=8FzytFPUoWyIqTAN5XT17kffWUEUfM9gdf50W57fSP3FIWcF785CCoicX4BZcFVcYaRe/u R7R4tXD5rBGk/7Dg== From: "tip-bot2 for David Hildenbrand" Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/urgent] x86/mm/pat: Fix VM_PAT handling when fork() fails in copy_page_range() Cc: xingwei lee , yuxin wang , Marius Fleischer , David Hildenbrand , Ingo Molnar , Andy Lutomirski , Peter Zijlstra , Rik van Riel , "H. Peter Anvin" , Linus Torvalds , Andrew Morton , linux-mm@kvack.org, x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20250321112323.153741-1-david@redhat.com> References: <20250321112323.153741-1-david@redhat.com> MIME-Version: 1.0 Message-ID: <174293932297.14745.17814988946096623463.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Rspamd-Server: rspam01 X-Stat-Signature: 34um438n5ac6q8dh5zaca3hd1rgckn4x X-Rspam-User: X-Rspamd-Queue-Id: 10F9740002 X-HE-Bulk: true X-HE-Tag: 1742939329-886852 X-HE-Meta: U2FsdGVkX18rCZswwwwDqz2YmLc4yho9V1ZbIm9WJ0CMyCLpkuX9nJU+hJ57F6Es32u5u0ZJG0pIyKAxlc4n0kp1dvJJ63j3MCvDre0mLALKWQHOSbpno0LScBvrZNhrp1WJ4ewoHEfm7hNGXNI+E7tOJDi7XUDtD5C+S1XjaDy6v7V0dUCYBRuRW5E37aKt6NKdqbz652haOv++fbauUicmpkQtiEClIwqA8jXQ9NpwAXTc1P8ngK2HCnfaFaTga0K2H28B4VMeKLapbSjbK9JNTC7WOUsgHWfjbHrVK7kdZ9gPSCSaz0uIw4x5BqJ8WWXhGmx7EFLYUvYrMaOvsgajCZ92HNctv1inlV/4AuuX/nLqNtwF/tOPcSuhLuAnx2+vQMIGYFwCloVp4dUNVsRg/wWSZfGYZDV2g4tnN+I1hkwuMW8+1jtOdW3k5YUPbXu3o74/5vdN+iWLHFhg8DHScR8NJCy47m4cVik/1PtxoqvIbRWu5wSanA2gQJ32Gfxgbo7eBwSRjCK5P6tP8PvlD3rhBv6hr/3NMmJeRu4ycfqKXqH0CvchSGuZ/C7wcZVInTjUys7uDJ42SYxIPGeA/jgy/X+6f3HBWN6llE1YpVPCfqB9ieXkuowLn9jL1UhUQMhMV5XeCHvXAYEz2juCz5kprQMXuz7aqGEtItm3NQheuwcmXx+LK6FL1h4bTkpk8qj9LWaQ8qNIu9AvnJK3TRR/aIAdTD92LKVVsJ96tDLf8I++VIdWtV2YeCDqoIuUW0iSA62TdIlkJjv2HnbByPZkGIQ2aAmZqeaAvJxSTzE6Z46jrN2e2qFsyiwRiJ0KAqBZmy+PLUehP0M7aVhR9ci+XlsnIXkAFMzfzbuUAa/T5PNyXmgsb/jjIoWjDQOBNoXKzS48Zu2k++T+F7OU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vack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The following commit has been merged into the x86/urgent branch of tip: Commit-ID: dc84bc2aba85a1508f04a936f9f9a15f64ebfb31 Gitweb: https://git.kernel.org/tip/dc84bc2aba85a1508f04a936f9f9a15f64ebfb31 Author: David Hildenbrand AuthorDate: Fri, 21 Mar 2025 12:23:23 +01:00 Committer: Ingo Molnar CommitterDate: Tue, 25 Mar 2025 22:35:14 +01:00 x86/mm/pat: Fix VM_PAT handling when fork() fails in copy_page_range() If track_pfn_copy() fails, we already added the dst VMA to the maple tree. As fork() fails, we'll cleanup the maple tree, and stumble over the dst VMA for which we neither performed any reservation nor copied any page tables. Consequently untrack_pfn() will see VM_PAT and try obtaining the PAT information from the page table -- which fails because the page table was not copied. The easiest fix would be to simply clear the VM_PAT flag of the dst VMA if track_pfn_copy() fails. However, the whole thing is about "simply" clearing the VM_PAT flag is shaky as well: if we passed track_pfn_copy() and performed a reservation, but copying the page tables fails, we'll simply clear the VM_PAT flag, not properly undoing the reservation ... which is also wrong. So let's fix it properly: set the VM_PAT flag only if the reservation succeeded (leaving it clear initially), and undo the reservation if anything goes wrong while copying the page tables: clearing the VM_PAT flag after undoing the reservation. Note that any copied page table entries will get zapped when the VMA will get removed later, after copy_page_range() succeeded; as VM_PAT is not set then, we won't try cleaning VM_PAT up once more and untrack_pfn() will be happy. Note that leaving these page tables in place without a reservation is not a problem, as we are aborting fork(); this process will never run. A reproducer can trigger this usually at the first try: https://gitlab.com/davidhildenbrand/scratchspace/-/raw/main/reproducers/pat_fork.c WARNING: CPU: 26 PID: 11650 at arch/x86/mm/pat/memtype.c:983 get_pat_info+0xf6/0x110 Modules linked in: ... CPU: 26 UID: 0 PID: 11650 Comm: repro3 Not tainted 6.12.0-rc5+ #92 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-2.fc40 04/01/2014 RIP: 0010:get_pat_info+0xf6/0x110 ... Call Trace: ... untrack_pfn+0x52/0x110 unmap_single_vma+0xa6/0xe0 unmap_vmas+0x105/0x1f0 exit_mmap+0xf6/0x460 __mmput+0x4b/0x120 copy_process+0x1bf6/0x2aa0 kernel_clone+0xab/0x440 __do_sys_clone+0x66/0x90 do_syscall_64+0x95/0x180 Likely this case was missed in: d155df53f310 ("x86/mm/pat: clear VM_PAT if copy_p4d_range failed") ... and instead of undoing the reservation we simply cleared the VM_PAT flag. Keep the documentation of these functions in include/linux/pgtable.h, one place is more than sufficient -- we should clean that up for the other functions like track_pfn_remap/untrack_pfn separately. Fixes: d155df53f310 ("x86/mm/pat: clear VM_PAT if copy_p4d_range failed") Fixes: 2ab640379a0a ("x86: PAT: hooks in generic vm code to help archs to track pfnmap regions - v3") Reported-by: xingwei lee Reported-by: yuxin wang Reported-by: Marius Fleischer Signed-off-by: David Hildenbrand Signed-off-by: Ingo Molnar Cc: Andy Lutomirski Cc: Peter Zijlstra Cc: Rik van Riel Cc: "H. Peter Anvin" Cc: Linus Torvalds Cc: Andrew Morton Cc: linux-mm@kvack.org Link: https://lore.kernel.org/r/20250321112323.153741-1-david@redhat.com Closes: https://lore.kernel.org/lkml/CABOYnLx_dnqzpCW99G81DmOr+2UzdmZMk=T3uxwNxwz+R1RAwg@mail.gmail.com/ Closes: https://lore.kernel.org/lkml/CAJg=8jwijTP5fre8woS4JVJQ8iUA6v+iNcsOgtj9Zfpc3obDOQ@mail.gmail.com/ --- arch/x86/mm/pat/memtype.c | 52 ++++++++++++++++++++------------------ include/linux/pgtable.h | 28 +++++++++++++++----- kernel/fork.c | 4 +++- mm/memory.c | 11 ++------ 4 files changed, 58 insertions(+), 37 deletions(-) diff --git a/arch/x86/mm/pat/memtype.c b/arch/x86/mm/pat/memtype.c index e40861c..72d8cbc 100644 --- a/arch/x86/mm/pat/memtype.c +++ b/arch/x86/mm/pat/memtype.c @@ -984,29 +984,42 @@ static int get_pat_info(struct vm_area_struct *vma, resource_size_t *paddr, return -EINVAL; } -/* - * track_pfn_copy is called when vma that is covering the pfnmap gets - * copied through copy_page_range(). - * - * If the vma has a linear pfn mapping for the entire range, we get the prot - * from pte and reserve the entire vma range with single reserve_pfn_range call. - */ -int track_pfn_copy(struct vm_area_struct *vma) +int track_pfn_copy(struct vm_area_struct *dst_vma, + struct vm_area_struct *src_vma, unsigned long *pfn) { + const unsigned long vma_size = src_vma->vm_end - src_vma->vm_start; resource_size_t paddr; - unsigned long vma_size = vma->vm_end - vma->vm_start; pgprot_t pgprot; + int rc; - if (vma->vm_flags & VM_PAT) { - if (get_pat_info(vma, &paddr, &pgprot)) - return -EINVAL; - /* reserve the whole chunk covered by vma. */ - return reserve_pfn_range(paddr, vma_size, &pgprot, 1); - } + if (!(src_vma->vm_flags & VM_PAT)) + return 0; + + /* + * Duplicate the PAT information for the dst VMA based on the src + * VMA. + */ + if (get_pat_info(src_vma, &paddr, &pgprot)) + return -EINVAL; + rc = reserve_pfn_range(paddr, vma_size, &pgprot, 1); + if (rc) + return rc; + /* Reservation for the destination VMA succeeded. */ + vm_flags_set(dst_vma, VM_PAT); + *pfn = PHYS_PFN(paddr); return 0; } +void untrack_pfn_copy(struct vm_area_struct *dst_vma, unsigned long pfn) +{ + untrack_pfn(dst_vma, pfn, dst_vma->vm_end - dst_vma->vm_start, true); + /* + * Reservation was freed, any copied page tables will get cleaned + * up later, but without getting PAT involved again. + */ +} + /* * prot is passed in as a parameter for the new mapping. If the vma has * a linear pfn mapping for the entire range, or no vma is provided, @@ -1095,15 +1108,6 @@ void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn, } } -/* - * untrack_pfn_clear is called if the following situation fits: - * - * 1) while mremapping a pfnmap for a new region, with the old vma after - * its pfnmap page table has been removed. The new vma has a new pfnmap - * to the same pfn & cache type with VM_PAT set. - * 2) while duplicating vm area, the new vma fails to copy the pgtable from - * old vma. - */ void untrack_pfn_clear(struct vm_area_struct *vma) { vm_flags_clear(vma, VM_PAT); diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 94d267d..4c107e1 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -1508,15 +1508,26 @@ static inline void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot, } /* - * track_pfn_copy is called when vma that is covering the pfnmap gets - * copied through copy_page_range(). + * track_pfn_copy is called when a VM_PFNMAP VMA is about to get the page + * tables copied during copy_page_range(). On success, stores the pfn to be + * passed to untrack_pfn_copy(). */ -static inline int track_pfn_copy(struct vm_area_struct *vma) +static inline int track_pfn_copy(struct vm_area_struct *dst_vma, + struct vm_area_struct *src_vma, unsigned long *pfn) { return 0; } /* + * untrack_pfn_copy is called when a VM_PFNMAP VMA failed to copy during + * copy_page_range(), but after track_pfn_copy() was already called. + */ +static inline void untrack_pfn_copy(struct vm_area_struct *dst_vma, + unsigned long pfn) +{ +} + +/* * untrack_pfn is called while unmapping a pfnmap for a region. * untrack can be called for a specific region indicated by pfn and size or * can be for the entire vma (in which case pfn, size are zero). @@ -1528,8 +1539,10 @@ static inline void untrack_pfn(struct vm_area_struct *vma, } /* - * untrack_pfn_clear is called while mremapping a pfnmap for a new region - * or fails to copy pgtable during duplicate vm area. + * untrack_pfn_clear is called in the following cases on a VM_PFNMAP VMA: + * + * 1) During mremap() on the src VMA after the page tables were moved. + * 2) During fork() on the dst VMA, immediately after duplicating the src VMA. */ static inline void untrack_pfn_clear(struct vm_area_struct *vma) { @@ -1540,7 +1553,10 @@ extern int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot, unsigned long size); extern void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot, pfn_t pfn); -extern int track_pfn_copy(struct vm_area_struct *vma); +extern int track_pfn_copy(struct vm_area_struct *dst_vma, + struct vm_area_struct *src_vma, unsigned long *pfn); +extern void untrack_pfn_copy(struct vm_area_struct *dst_vma, + unsigned long pfn); extern void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn, unsigned long size, bool mm_wr_locked); extern void untrack_pfn_clear(struct vm_area_struct *vma); diff --git a/kernel/fork.c b/kernel/fork.c index f11ac96..91171e5 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -504,6 +504,10 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) vma_numab_state_init(new); dup_anon_vma_name(orig, new); + /* track_pfn_copy() will later take care of copying internal state. */ + if (unlikely(new->vm_flags & VM_PFNMAP)) + untrack_pfn_clear(new); + return new; } diff --git a/mm/memory.c b/mm/memory.c index 4f6d976..53f7b0a 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1362,12 +1362,12 @@ int copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) { pgd_t *src_pgd, *dst_pgd; - unsigned long next; unsigned long addr = src_vma->vm_start; unsigned long end = src_vma->vm_end; struct mm_struct *dst_mm = dst_vma->vm_mm; struct mm_struct *src_mm = src_vma->vm_mm; struct mmu_notifier_range range; + unsigned long next, pfn; bool is_cow; int ret; @@ -1378,11 +1378,7 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) return copy_hugetlb_page_range(dst_mm, src_mm, dst_vma, src_vma); if (unlikely(src_vma->vm_flags & VM_PFNMAP)) { - /* - * We do not free on error cases below as remove_vma - * gets called on error from higher level routine - */ - ret = track_pfn_copy(src_vma); + ret = track_pfn_copy(dst_vma, src_vma, &pfn); if (ret) return ret; } @@ -1419,7 +1415,6 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) continue; if (unlikely(copy_p4d_range(dst_vma, src_vma, dst_pgd, src_pgd, addr, next))) { - untrack_pfn_clear(dst_vma); ret = -ENOMEM; break; } @@ -1429,6 +1424,8 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) raw_write_seqcount_end(&src_mm->write_protect_seq); mmu_notifier_invalidate_range_end(&range); } + if (ret && unlikely(src_vma->vm_flags & VM_PFNMAP)) + untrack_pfn_copy(dst_vma, pfn); return ret; }