Skip to content

Commit 5de61c7

Browse files
committed
mm: follow_pte() improvements
jira LE-3557 Rebuild_History Non-Buildable kernel-5.14.0-570.26.1.el9_6 commit-author David Hildenbrand <[email protected]> commit c5541ba Empty-Commit: Cherry-Pick Conflicts during history rebuild. Will be included in final tarball splat. Ref for failed cherry-pick at: ciq/ciq_backports/kernel-5.14.0-570.26.1.el9_6/c5541ba3.failed follow_pte() is now our main function to lookup PTEs in VM_PFNMAP/VM_IO VMAs. Let's perform some more sanity checks to make this exported function harder to abuse. Further, extend the doc a bit, it still focuses on the KVM use case with MMU notifiers. Drop the KVM+follow_pfn() comment, follow_pfn() is no more, and we have other users nowadays. Also extend the doc regarding refcounted pages and the interaction with MMU notifiers. KVM is one example that uses MMU notifiers and can deal with refcounted pages properly. VFIO is one example that doesn't use MMU notifiers, and to prevent use-after-free, rejects refcounted pages: pfn_valid(pfn) && !PageReserved(pfn_to_page(pfn)). Protection changes are less of a concern for users like VFIO: the behavior is similar to longterm-pinning a page, and getting the PTE protection changed afterwards. The primary concern with refcounted pages is use-after-free, which callers should be aware of. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: David Hildenbrand <[email protected]> Cc: Alex Williamson <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Fei Li <[email protected]> Cc: Gerald Schaefer <[email protected]> Cc: Heiko Carstens <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Paolo Bonzini <[email protected]> Cc: Sean Christopherson <[email protected]> Cc: Yonghua Huang <[email protected]> Signed-off-by: Andrew Morton <[email protected]> (cherry picked from commit c5541ba) Signed-off-by: Jonathan Maple <[email protected]> # Conflicts: # mm/memory.c
1 parent f3963f6 commit 5de61c7

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
mm: follow_pte() improvements
2+
3+
jira LE-3557
4+
Rebuild_History Non-Buildable kernel-5.14.0-570.26.1.el9_6
5+
commit-author David Hildenbrand <[email protected]>
6+
commit c5541ba378e3d36ea88bf5839d5b23e33e7d1627
7+
Empty-Commit: Cherry-Pick Conflicts during history rebuild.
8+
Will be included in final tarball splat. Ref for failed cherry-pick at:
9+
ciq/ciq_backports/kernel-5.14.0-570.26.1.el9_6/c5541ba3.failed
10+
11+
follow_pte() is now our main function to lookup PTEs in VM_PFNMAP/VM_IO
12+
VMAs. Let's perform some more sanity checks to make this exported
13+
function harder to abuse.
14+
15+
Further, extend the doc a bit, it still focuses on the KVM use case with
16+
MMU notifiers. Drop the KVM+follow_pfn() comment, follow_pfn() is no
17+
more, and we have other users nowadays.
18+
19+
Also extend the doc regarding refcounted pages and the interaction with
20+
MMU notifiers.
21+
22+
KVM is one example that uses MMU notifiers and can deal with refcounted
23+
pages properly. VFIO is one example that doesn't use MMU notifiers, and
24+
to prevent use-after-free, rejects refcounted pages: pfn_valid(pfn) &&
25+
!PageReserved(pfn_to_page(pfn)). Protection changes are less of a concern
26+
for users like VFIO: the behavior is similar to longterm-pinning a page,
27+
and getting the PTE protection changed afterwards.
28+
29+
The primary concern with refcounted pages is use-after-free, which callers
30+
should be aware of.
31+
32+
Link: https://lkml.kernel.org/r/[email protected]
33+
Signed-off-by: David Hildenbrand <[email protected]>
34+
Cc: Alex Williamson <[email protected]>
35+
Cc: Christoph Hellwig <[email protected]>
36+
Cc: Fei Li <[email protected]>
37+
Cc: Gerald Schaefer <[email protected]>
38+
Cc: Heiko Carstens <[email protected]>
39+
Cc: Ingo Molnar <[email protected]>
40+
Cc: Paolo Bonzini <[email protected]>
41+
Cc: Sean Christopherson <[email protected]>
42+
Cc: Yonghua Huang <[email protected]>
43+
Signed-off-by: Andrew Morton <[email protected]>
44+
(cherry picked from commit c5541ba378e3d36ea88bf5839d5b23e33e7d1627)
45+
Signed-off-by: Jonathan Maple <[email protected]>
46+
47+
# Conflicts:
48+
# mm/memory.c
49+
diff --cc mm/memory.c
50+
index e2794e3b8919,36ba94eae853..000000000000
51+
--- a/mm/memory.c
52+
+++ b/mm/memory.c
53+
@@@ -5623,8 -5947,7 +5630,12 @@@ int __pmd_alloc(struct mm_struct *mm, p
54+
* Only IO mappings and raw PFN mappings are allowed. The mmap semaphore
55+
* should be taken for read.
56+
*
57+
++<<<<<<< HEAD
58+
+ * KVM uses this function. While it is arguably less bad than ``follow_pfn``,
59+
+ * it is not a good general-purpose API.
60+
++=======
61+
+ * This function must not be used to modify PTE content.
62+
++>>>>>>> c5541ba378e3 (mm: follow_pte() improvements)
63+
*
64+
* Return: zero on success, -ve otherwise.
65+
*/
66+
@@@ -5637,6 -5961,13 +5648,16 @@@ int follow_pte(struct mm_struct *mm, un
67+
pmd_t *pmd;
68+
pte_t *ptep;
69+
70+
++<<<<<<< HEAD
71+
++=======
72+
+ mmap_assert_locked(mm);
73+
+ if (unlikely(address < vma->vm_start || address >= vma->vm_end))
74+
+ goto out;
75+
+
76+
+ if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
77+
+ goto out;
78+
+
79+
++>>>>>>> c5541ba378e3 (mm: follow_pte() improvements)
80+
pgd = pgd_offset(mm, address);
81+
if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
82+
goto out;
83+
* Unmerged path mm/memory.c

0 commit comments

Comments
 (0)