Skip to contentSkip to conversation

x86/mm/pat: (un)track_pfn_copy() fix + doc improvements

PATCH Applied David Hildenbrand 4 participants 7 messages Apr 8, 2025 – Apr 10, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/mm/pat: (un)track_pfn_copy() fix + doc improvements

We got a late smatch warning and some additional review feedback.

smatch warnings:
mm/memory.c:1428 copy_page_range() error: uninitialized symbol 'pfn'.

We actually use the pfn only when it is properly initialized; however, we may pass an uninitialized value to a function -- although it will not use it that likely still is UB in C.

So let's just fix it by always initializing pfn in the caller of track_pfn_copy(), and improving the documentation of track_pfn_copy().

While at it, clarify the doc of untrack_pfn_copy(), that internal checks make sure if we actually have to untrack anything.

Fixes dc84bc2aba85 ("x86/mm/pat: Fix VM_PAT handling when fork() fails in copy_page_range()")
Cc Andrew Morton <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Rik van Riel <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Linus Torvalds <[email protected]>

---

v1 -> v2:
* Adjust the doc instead of initializing the pfn whenever returning 0
* Decided to keep Lorenzo's RB :)
* Retested

---
 include/linux/pgtable.h | 9 ++++++---
 mm/memory.c             | 2 +-
 2 files changed, 7 insertions(+), 4 deletions(-)
include/linux/pgtable.h +6 -3
index e2b705c149454..b50447ef1c921 100644
@@ -1511,8 +1511,9 @@ static inline void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,
 
 /*
  * 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().
+ * tables copied during copy_page_range(). Will store the pfn to be
+ * passed to untrack_pfn_copy() only if there is something to be untracked.
+ * Callers should initialize the pfn to 0.
  */
 static inline int track_pfn_copy(struct vm_area_struct *dst_vma,
 		struct vm_area_struct *src_vma, unsigned long *pfn)
@@ -1522,7 +1523,9 @@ static inline int track_pfn_copy(struct vm_area_struct *dst_vma,
 
 /*
  * 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.
+ * copy_page_range(), but after track_pfn_copy() was already called. Can
+ * be called even if track_pfn_copy() did not actually track anything:
+ * handled internally.
  */
 static inline void untrack_pfn_copy(struct vm_area_struct *dst_vma,
 		unsigned long pfn)
mm/memory.c +1 -1
index 2d8c265fc7d60..1a35165622e1c 100644
@@ -1361,7 +1361,7 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
 	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;
+	unsigned long next, pfn = 0;
 	bool is_cow;
 	int ret;
 
Signature · 1 line
2.48.1
#1 lore raw
Quoted 32 lines · David Hildenbrand

We got a late smatch warning and some additional review feedback.

smatch warnings:
mm/memory.c:1428 copy_page_range() error: uninitialized symbol 'pfn'.

We actually use the pfn only when it is properly initialized; however, we may pass an uninitialized value to a function -- although it will not use it that likely still is UB in C.

So let's just fix it by always initializing pfn in the caller of track_pfn_copy(), and improving the documentation of track_pfn_copy().

While at it, clarify the doc of untrack_pfn_copy(), that internal checks make sure if we actually have to untrack anything.

Fixes dc84bc2aba85 ("x86/mm/pat: Fix VM_PAT handling when fork() fails in copy_page_range()")
Cc Andrew Morton <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Rik van Riel <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Linus Torvalds <[email protected]>
Quoted 5 lines

---

v1 -> v2:
* Adjust the doc instead of initializing the pfn whenever returning 0
* Decided to keep Lorenzo's RB :)

All good with me :) the patch in this form is fine!

Quoted 50 lines

* Retested

---
 include/linux/pgtable.h | 9 ++++++---
 mm/memory.c             | 2 +-
 2 files changed, 7 insertions(+), 4 deletions(-)
include/linux/pgtable.h +6 -3
index e2b705c149454..b50447ef1c921 100644
@@ -1511,8 +1511,9 @@ static inline void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot,
 
 /*
  * 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().
+ * tables copied during copy_page_range(). Will store the pfn to be
+ * passed to untrack_pfn_copy() only if there is something to be untracked.
+ * Callers should initialize the pfn to 0.
  */
 static inline int track_pfn_copy(struct vm_area_struct *dst_vma,
 		struct vm_area_struct *src_vma, unsigned long *pfn)
@@ -1522,7 +1523,9 @@ static inline int track_pfn_copy(struct vm_area_struct *dst_vma,
 
 /*
  * 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.
+ * copy_page_range(), but after track_pfn_copy() was already called. Can
+ * be called even if track_pfn_copy() did not actually track anything:
+ * handled internally.
  */
 static inline void untrack_pfn_copy(struct vm_area_struct *dst_vma,
 		unsigned long pfn)
mm/memory.c +1 -2
index 2d8c265fc7d60..1a35165622e1c 100644
@@ -1361,7 +1361,7 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
 	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;
+	unsigned long next, pfn = 0;
 	bool is_cow;
 	int ret;
 
--

2.48.1

#2 lore raw
In reply to David Hildenbrand
Quoted 9 lines · David Hildenbrand

We got a late smatch warning and some additional review feedback.

smatch warnings:
mm/memory.c:1428 copy_page_range() error: uninitialized symbol 'pfn'.

We actually use the pfn only when it is properly initialized; however, we may pass an uninitialized value to a function -- although it will not use it that likely still is UB in C.

Quoted 5 lines

So let's just fix it by always initializing pfn in the caller of track_pfn_copy(), and improving the documentation of track_pfn_copy().

While at it, clarify the doc of untrack_pfn_copy(), that internal checks make sure if we actually have to untrack anything.

Note that the title isn't accurate anymore, it's not an 'x86/mm/pat' patch, but an 'mm' patch.

Thanks,

Ingo

#3 lore raw
Quoted 12 lines · Ingo Molnar
Quoted 9 lines · David Hildenbrand

We got a late smatch warning and some additional review feedback.

smatch warnings:
mm/memory.c:1428 copy_page_range() error: uninitialized symbol 'pfn'.

We actually use the pfn only when it is properly initialized; however, we may pass an uninitialized value to a function -- although it will not use it that likely still is UB in C.

Quoted 8 lines
Quoted 5 lines

So let's just fix it by always initializing pfn in the caller of track_pfn_copy(), and improving the documentation of track_pfn_copy().

While at it, clarify the doc of untrack_pfn_copy(), that internal checks make sure if we actually have to untrack anything.

Note that the title isn't accurate anymore, it's not an 'x86/mm/pat' patch, but an 'mm' patch.

Agreed. Who will take this patch? If it's Andrew, can you fixup the subject please?

Signature · 3 lines
Cheers,

David / dhildenb
Acked-by
#4 lore raw
Quoted 16 lines · David Hildenbrand
Quoted 15 lines · Ingo Molnar
Quoted 12 lines · David Hildenbrand

We got a late smatch warning and some additional review feedback.

smatch warnings:
mm/memory.c:1428 copy_page_range() error: uninitialized symbol 'pfn'.

We actually use the pfn only when it is properly initialized; however, we may pass an uninitialized value to a function -- although it will not use it that likely still is UB in C.

So let's just fix it by always initializing pfn in the caller of track_pfn_copy(), and improving the documentation of track_pfn_copy().

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

While at it, clarify the doc of untrack_pfn_copy(), that internal checks make sure if we actually have to untrack anything.

Note that the title isn't accurate anymore, it's not an 'x86/mm/pat' patch, but an 'mm' patch.

Agreed. Who will take this patch? If it's Andrew, can you fixup the subject please?

I was assuming Andrew would pick it up:

But can pick it up too via tip:x86/urgent.

Thanks,

Ingo

maintainer
#5 lore raw
In reply to David Hildenbrand
Quoted 5 lines · On Wed, 9 Apr 2025 12:34:47 +0200 David Hildenbrand <[email protected]> wrote:
Quoted 2 lines

Note that the title isn't accurate anymore, it's not an 'x86/mm/pat' patch, but an 'mm' patch.

Agreed. Who will take this patch? If it's Andrew, can you fixup the subject please?

I edited the mm.git copy.

#6 lore raw
Quoted 9 lines · Andrew Morton
Quoted 5 lines · On Wed, 9 Apr 2025 12:34:47 +0200 David Hildenbrand <[email protected]> wrote:
Quoted 2 lines

Note that the title isn't accurate anymore, it's not an 'x86/mm/pat' patch, but an 'mm' patch.

Agreed. Who will take this patch? If it's Andrew, can you fixup the subject please?

I edited the mm.git copy.

Thanks!

Ingo