x86/mm/pat: fix VM_PAT handling
Subsystems (8)
cover x86/mm/pat: fix VM_PAT handling
[PATCH v6.1 1/2]:
required to backport [PATCH v6.1 2/2].
[PATCH v6.1 2/2]:
backport to fix VM_PAT handling when fork() fails in copy_page_range().
arch/x86/mm/pat/memtype.c | 50 +++++++++++++++++++++++---------------- include/linux/pgtable.h | 31 ++++++++++++++++++------ kernel/fork.c | 4 ++++ mm/memory.c | 10 ++++---- mm/mremap.c | 2 +- 5 files changed, 62 insertions(+), 35 deletions(-)
Signature · 1 line
2.40.4
PATCH 1/2 x86/mm/pat: clear VM_PAT if copy_p4d_range failed
From: Ma Wupeng <[email protected]>
[ Upstream commit d155df53f31068c3340733d586eb9b3ddfd70fc5 ]
Syzbot reports a warning in untrack_pfn(). Digging into the root we found that this is due to memory allocation failure in pmd_alloc_one. And this failure is produced due to failslab.
In copy_page_range(), memory alloaction for pmd failed. During the error handling process in copy_page_range(), mmput() is called to remove all vmas. While untrack_pfn this empty pfn, warning happens.
Here's a simplified flow:
dup_mm
dup_mmap
copy_page_range
copy_p4d_range
copy_pud_range
copy_pmd_range
pmd_alloc
__pmd_alloc
pmd_alloc_one
page = alloc_pages(gfp, 0);
if (!page)
return NULL;
mmput
exit_mmap
unmap_vmas
unmap_single_vma
untrack_pfn
follow_phys
WARN_ON_ONCE(1);Since this vma is not generate successfully, we can clear flag VM_PAT. In this case, untrack_pfn() will not be called while cleaning this vma.
Function untrack_pfn_moved() has also been renamed to fit the new logic.
--- arch/x86/mm/pat/memtype.c | 12 ++++++++---- include/linux/pgtable.h | 7 ++++--- mm/memory.c | 1 + mm/mremap.c | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-)
arch/x86/mm/pat/memtype.c +8 -4
@@ -1137,11 +1137,15 @@ void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn, }/* -* untrack_pfn_moved is called, 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. +* 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_moved (struct vm_area_struct *vma) +void untrack_pfn_clear (struct vm_area_struct *vma) { vma->vm_flags &= ~VM_PAT ; }
include/linux/pgtable.h +4 -3
@@ -1214,9 +1214,10 @@ static inline void untrack_pfn(struct vm_area_struct *vma, }/* -* untrack_pfn_moved is called while mremapping a pfnmap for a new region. +* untrack_pfn_clear is called while mremapping a pfnmap for a new region +* or fails to copy pgtable during duplicate vm area. */ -static inline void untrack_pfn_moved (struct vm_area_struct *vma) +static inline void untrack_pfn_clear (struct vm_area_struct *vma) { }#else
@@ -1228,7 +1229,7 @@ extern void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,extern int track_pfn_copy (struct vm_area_struct *vma);extern void untrack_pfn (struct vm_area_struct *vma,unsigned long pfn,unsigned long size); -extern void untrack_pfn_moved (struct vm_area_struct *vma); +extern void untrack_pfn_clear (struct vm_area_struct *vma);#endif #ifdef CONFIG_MMU
mm/memory.c +1 -0
@@ -1335,6 +1335,7 @@ 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 ; }
mm/mremap.c +1 -1
@@ -682,7 +682,7 @@ static unsigned long move_vma(struct vm_area_struct *vma,/* Tell pfnmap has moved from this vma */ if (unlikely (vma->vm_flags &VM_PFNMAP )) -untrack_pfn_moved (vma); +untrack_pfn_clear (vma);if (unlikely (!err && (flags &MREMAP_DONTUNMAP ))) {/* We always clear VM_LOCKED[ONFAULT] on the old vma */
Signature · 1 line
2.40.4
Patch "x86/mm/pat: clear VM_PAT if copy_p4d_range failed" has been added to the 6.1-stable tree
This is a note to let you know that I've just added the patch titled
x86/mm/pat: clear VM_PAT if copy_p4d_range failed
to the 6.1-stable tree which can be found at:
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary
The filename of the patch is:
x86-mm-pat-clear-vm_pat-if-copy_p4d_range-failed.patch
and it can be found in the queue-6.1 subdirectory.
If you, or anyone else, feels it should not be added to the stable tree, please let <[email protected]> know about it.
From [email protected] Wed Dec 24 11:43:39 2025
From: Ajay Kaher <[email protected]>
Date: Wed, 24 Dec 2025 10:24:31 +0000
Subject: x86/mm/pat: clear VM_PAT if copy_p4d_range failed
To: [email protected], [email protected]
Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Ma Wupeng <[email protected]>, [email protected], Alexander Ofitserov <[email protected]>
Message-ID: <[email protected]>
From: Ma Wupeng <[email protected]>
[ Upstream commit d155df53f31068c3340733d586eb9b3ddfd70fc5 ]
Syzbot reports a warning in untrack_pfn(). Digging into the root we found that this is due to memory allocation failure in pmd_alloc_one. And this failure is produced due to failslab.
In copy_page_range(), memory alloaction for pmd failed. During the error handling process in copy_page_range(), mmput() is called to remove all vmas. While untrack_pfn this empty pfn, warning happens.
Here's a simplified flow:
dup_mm
dup_mmap
copy_page_range
copy_p4d_range
copy_pud_range
copy_pmd_range
pmd_alloc
__pmd_alloc
pmd_alloc_one
page = alloc_pages(gfp, 0);
if (!page)
return NULL;
mmput
exit_mmap
unmap_vmas
unmap_single_vma
untrack_pfn
follow_phys
WARN_ON_ONCE(1);Since this vma is not generate successfully, we can clear flag VM_PAT. In this case, untrack_pfn() will not be called while cleaning this vma.
Function untrack_pfn_moved() has also been renamed to fit the new logic.
--- arch/x86/mm/pat/memtype.c | 12 ++++++++---- include/linux/pgtable.h | 7 ++++--- mm/memory.c | 1 + mm/mremap.c | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-)
arch/x86/mm/pat/memtype.c +8 -4
@@ -1137,11 +1137,15 @@ void untrack_pfn(struct vm_area_struct * }/* -* untrack_pfn_moved is called, 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. +* 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_moved (struct vm_area_struct *vma) +void untrack_pfn_clear (struct vm_area_struct *vma) { vma->vm_flags &= ~VM_PAT ; }
include/linux/pgtable.h +4 -3
@@ -1214,9 +1214,10 @@ static inline void untrack_pfn(struct vm }/* -* untrack_pfn_moved is called while mremapping a pfnmap for a new region. +* untrack_pfn_clear is called while mremapping a pfnmap for a new region +* or fails to copy pgtable during duplicate vm area. */ -static inline void untrack_pfn_moved (struct vm_area_struct *vma) +static inline void untrack_pfn_clear (struct vm_area_struct *vma) { }#else
@@ -1228,7 +1229,7 @@ extern void track_pfn_insert(struct vm_aextern int track_pfn_copy (struct vm_area_struct *vma);extern void untrack_pfn (struct vm_area_struct *vma,unsigned long pfn,unsigned long size); -extern void untrack_pfn_moved (struct vm_area_struct *vma); +extern void untrack_pfn_clear (struct vm_area_struct *vma);#endif #ifdef CONFIG_MMU
mm/memory.c +1 -0
@@ -1335,6 +1335,7 @@ copy_page_range(struct vm_area_struct *dcontinue ;if (unlikely (copy_p4d_range (dst_vma, src_vma, dst_pgd, src_pgd, addr, next))) { +untrack_pfn_clear (dst_vma); ret = -ENOMEM ;break ; }
mm/mremap.c +1 -1
@@ -682,7 +682,7 @@ static unsigned long move_vma(struct vm_/* Tell pfnmap has moved from this vma */ if (unlikely (vma->vm_flags &VM_PFNMAP )) -untrack_pfn_moved (vma); +untrack_pfn_clear (vma);if (unlikely (!err && (flags &MREMAP_DONTUNMAP ))) {/* We always clear VM_LOCKED[ONFAULT] on the old vma */
Patches currently in stable-queue which might be from [email protected] are
queue-6.1/usb-xhci-move-link-chain-bit-quirk-checks-into-one-helper-function.patch queue-6.1/x86-mm-pat-fix-vm_pat-handling-when-fork-fails-in-copy_page_range.patch queue-6.1/sched-fair-proportional-newidle-balance.patch queue-6.1/sched-fair-small-cleanup-to-update_newidle_cost.patch queue-6.1/rdma-core-fix-kasan-slab-use-after-free-read-in-ib_register_device-problem.patch queue-6.1/x86-mm-pat-clear-vm_pat-if-copy_p4d_range-failed.patch queue-6.1/drm-vmwgfx-fix-a-null-ptr-access-in-the-cursor-snooper.patch queue-6.1/sched-fair-small-cleanup-to-sched_balance_newidle.patch queue-6.1/usb-xhci-apply-the-link-chain-quirk-on-nec-isoc-endpoints.patch
PATCH 2/2 x86/mm/pat: Fix VM_PAT handling when fork() fails in copy_page_range()
In reply to Ajay KaherFrom: David Hildenbrand <[email protected]>
[ Upstream commit dc84bc2aba85a1508f04a936f9f9a15f64ebfb31 ]
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: <TASK> ... 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.
--- 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(-)
arch/x86/mm/pat/memtype.c +28 -24
@@ -1029,29 +1029,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. */ + dst_vma->vm_flags |=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); +/* +* 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,
@@ -1136,15 +1149,6 @@ void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn, vma->vm_flags &= ~VM_PAT ; } -/* -* 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) { vma->vm_flags &= ~VM_PAT ;
include/linux/pgtable.h +22 -6
@@ -1195,14 +1195,25 @@ 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
@@ -1214,8 +1225,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) {
@@ -1226,7 +1239,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);extern void untrack_pfn_clear (struct vm_area_struct *vma);
kernel/fork.c +4 -0
@@ -476,6 +476,10 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) *new =data_race (*orig);INIT_LIST_HEAD (&new->anon_vma_chain);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; }
mm/memory.c +4 -7
@@ -1278,12 +1278,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;
@@ -1294,11 +1294,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; }
@@ -1335,7 +1331,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 ; }
@@ -1345,6 +1340,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; }
Signature · 1 line
2.40.4
Patch "x86/mm/pat: Fix VM_PAT handling when fork() fails in copy_page_range()" has been added to the 6.1-stable tree
This is a note to let you know that I've just added the patch titled
x86/mm/pat: Fix VM_PAT handling when fork() fails in copy_page_range()
to the 6.1-stable tree which can be found at:
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary
The filename of the patch is:
x86-mm-pat-fix-vm_pat-handling-when-fork-fails-in-copy_page_range.patch
and it can be found in the queue-6.1 subdirectory.
If you, or anyone else, feels it should not be added to the stable tree, please let <[email protected]> know about it.
From [email protected] Wed Dec 24 11:43:43 2025
From: Ajay Kaher <[email protected]>
Date: Wed, 24 Dec 2025 10:24:32 +0000
Subject: x86/mm/pat: Fix VM_PAT handling when fork() fails in copy_page_range()
To: [email protected], [email protected]
Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], xingwei lee <[email protected]>, yuxin wang <[email protected]>, Marius Fleischer <[email protected]>, David Hildenbrand <[email protected]>, Ingo Molnar <[email protected]>, Rik van Riel <[email protected]>, Linus Torvalds <[email protected]>, Sasha Levin <[email protected]>
Message-ID: <[email protected]>
From: David Hildenbrand <[email protected]>
[ Upstream commit dc84bc2aba85a1508f04a936f9f9a15f64ebfb31 ]
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: <TASK> ... 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.
--- 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(-)
arch/x86/mm/pat/memtype.c +28 -24
@@ -1029,29 +1029,42 @@ static int get_pat_info(struct vm_area_sreturn -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. */ + dst_vma->vm_flags |=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); +/* +* 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,
@@ -1136,15 +1149,6 @@ void untrack_pfn(struct vm_area_struct * vma->vm_flags &= ~VM_PAT ; } -/* -* 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) { vma->vm_flags &= ~VM_PAT ;
include/linux/pgtable.h +22 -6
@@ -1195,15 +1195,26 @@ static inline void track_pfn_insert(stru }/* -* 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).
@@ -1214,8 +1225,10 @@ static inline void untrack_pfn(struct vm }/* -* 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) {
@@ -1226,7 +1239,10 @@ extern int track_pfn_remap(struct vm_are 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);extern void untrack_pfn_clear (struct vm_area_struct *vma);
kernel/fork.c +4 -0
@@ -476,6 +476,10 @@ struct vm_area_struct *vm_area_dup(struc *new =data_race (*orig);INIT_LIST_HEAD (&new->anon_vma_chain);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; }
mm/memory.c +4 -7
@@ -1278,12 +1278,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;
@@ -1294,11 +1294,7 @@ copy_page_range(struct vm_area_struct *dreturn 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; }
@@ -1335,7 +1331,6 @@ copy_page_range(struct vm_area_struct *dcontinue ;if (unlikely (copy_p4d_range (dst_vma, src_vma, dst_pgd, src_pgd, addr, next))) { -untrack_pfn_clear (dst_vma); ret = -ENOMEM ;break ; }
@@ -1345,6 +1340,8 @@ copy_page_range(struct vm_area_struct *draw_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; }
Patches currently in stable-queue which might be from [email protected] are
queue-6.1/usb-xhci-move-link-chain-bit-quirk-checks-into-one-helper-function.patch queue-6.1/x86-mm-pat-fix-vm_pat-handling-when-fork-fails-in-copy_page_range.patch queue-6.1/sched-fair-proportional-newidle-balance.patch queue-6.1/sched-fair-small-cleanup-to-update_newidle_cost.patch queue-6.1/rdma-core-fix-kasan-slab-use-after-free-read-in-ib_register_device-problem.patch queue-6.1/x86-mm-pat-clear-vm_pat-if-copy_p4d_range-failed.patch queue-6.1/drm-vmwgfx-fix-a-null-ptr-access-in-the-cursor-snooper.patch queue-6.1/sched-fair-small-cleanup-to-sched_balance_newidle.patch queue-6.1/usb-xhci-apply-the-link-chain-quirk-on-nec-isoc-endpoints.patch