1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/rmap.h>
4 #include <linux/hugetlb.h>
5 #include <linux/swap.h>
6 #include <linux/swapops.h>
10 static inline bool not_found(struct page_vma_mapped_walk
*pvmw
)
12 page_vma_mapped_walk_done(pvmw
);
16 static bool map_pte(struct page_vma_mapped_walk
*pvmw
)
18 pvmw
->pte
= pte_offset_map(pvmw
->pmd
, pvmw
->address
);
19 if (!(pvmw
->flags
& PVMW_SYNC
)) {
20 if (pvmw
->flags
& PVMW_MIGRATION
) {
21 if (!is_swap_pte(*pvmw
->pte
))
24 if (!pte_present(*pvmw
->pte
))
28 pvmw
->ptl
= pte_lockptr(pvmw
->vma
->vm_mm
, pvmw
->pmd
);
34 * check_pte - check if @pvmw->page is mapped at the @pvmw->pte
36 * page_vma_mapped_walk() found a place where @pvmw->page is *potentially*
37 * mapped. check_pte() has to validate this.
39 * @pvmw->pte may point to empty PTE, swap PTE or PTE pointing to arbitrary
42 * If PVMW_MIGRATION flag is set, returns true if @pvmw->pte contains migration
43 * entry that points to @pvmw->page or any subpage in case of THP.
45 * If PVMW_MIGRATION flag is not set, returns true if @pvmw->pte points to
46 * @pvmw->page or any subpage in case of THP.
48 * Otherwise, return false.
51 static bool check_pte(struct page_vma_mapped_walk
*pvmw
)
55 if (pvmw
->flags
& PVMW_MIGRATION
) {
57 if (!is_swap_pte(*pvmw
->pte
))
59 entry
= pte_to_swp_entry(*pvmw
->pte
);
61 if (!is_migration_entry(entry
))
64 pfn
= migration_entry_to_pfn(entry
);
65 } else if (is_swap_pte(*pvmw
->pte
)) {
68 /* Handle un-addressable ZONE_DEVICE memory */
69 entry
= pte_to_swp_entry(*pvmw
->pte
);
70 if (!is_device_private_entry(entry
))
73 pfn
= device_private_entry_to_pfn(entry
);
75 if (!pte_present(*pvmw
->pte
))
78 pfn
= pte_pfn(*pvmw
->pte
);
81 if (pfn
< page_to_pfn(pvmw
->page
))
84 /* THP can be referenced by any subpage */
85 if (pfn
- page_to_pfn(pvmw
->page
) >= hpage_nr_pages(pvmw
->page
))
92 * page_vma_mapped_walk - check if @pvmw->page is mapped in @pvmw->vma at
94 * @pvmw: pointer to struct page_vma_mapped_walk. page, vma, address and flags
95 * must be set. pmd, pte and ptl must be NULL.
97 * Returns true if the page is mapped in the vma. @pvmw->pmd and @pvmw->pte point
98 * to relevant page table entries. @pvmw->ptl is locked. @pvmw->address is
99 * adjusted if needed (for PTE-mapped THPs).
101 * If @pvmw->pmd is set but @pvmw->pte is not, you have found PMD-mapped page
102 * (usually THP). For PTE-mapped THP, you should run page_vma_mapped_walk() in
103 * a loop to find all PTEs that map the THP.
105 * For HugeTLB pages, @pvmw->pte is set to the relevant page table entry
106 * regardless of which page table level the page is mapped at. @pvmw->pmd is
109 * Retruns false if there are no more page table entries for the page in
110 * the vma. @pvmw->ptl is unlocked and @pvmw->pte is unmapped.
112 * If you need to stop the walk before page_vma_mapped_walk() returned false,
113 * use page_vma_mapped_walk_done(). It will do the housekeeping.
115 bool page_vma_mapped_walk(struct page_vma_mapped_walk
*pvmw
)
117 struct mm_struct
*mm
= pvmw
->vma
->vm_mm
;
118 struct page
*page
= pvmw
->page
;
124 /* The only possible pmd mapping has been handled on last iteration */
125 if (pvmw
->pmd
&& !pvmw
->pte
)
126 return not_found(pvmw
);
131 if (unlikely(PageHuge(pvmw
->page
))) {
132 /* when pud is not present, pte will be NULL */
133 pvmw
->pte
= huge_pte_offset(mm
, pvmw
->address
,
134 PAGE_SIZE
<< compound_order(page
));
138 pvmw
->ptl
= huge_pte_lockptr(page_hstate(page
), mm
, pvmw
->pte
);
139 spin_lock(pvmw
->ptl
);
140 if (!check_pte(pvmw
))
141 return not_found(pvmw
);
145 pgd
= pgd_offset(mm
, pvmw
->address
);
146 if (!pgd_present(*pgd
))
148 p4d
= p4d_offset(pgd
, pvmw
->address
);
149 if (!p4d_present(*p4d
))
151 pud
= pud_offset(p4d
, pvmw
->address
);
152 if (!pud_present(*pud
))
154 pvmw
->pmd
= pmd_offset(pud
, pvmw
->address
);
156 * Make sure the pmd value isn't cached in a register by the
157 * compiler and used as a stale value after we've observed a
160 pmde
= READ_ONCE(*pvmw
->pmd
);
161 if (pmd_trans_huge(pmde
) || is_pmd_migration_entry(pmde
)) {
162 pvmw
->ptl
= pmd_lock(mm
, pvmw
->pmd
);
163 if (likely(pmd_trans_huge(*pvmw
->pmd
))) {
164 if (pvmw
->flags
& PVMW_MIGRATION
)
165 return not_found(pvmw
);
166 if (pmd_page(*pvmw
->pmd
) != page
)
167 return not_found(pvmw
);
169 } else if (!pmd_present(*pvmw
->pmd
)) {
170 if (thp_migration_supported()) {
171 if (!(pvmw
->flags
& PVMW_MIGRATION
))
172 return not_found(pvmw
);
173 if (is_migration_entry(pmd_to_swp_entry(*pvmw
->pmd
))) {
174 swp_entry_t entry
= pmd_to_swp_entry(*pvmw
->pmd
);
176 if (migration_entry_to_page(entry
) != page
)
177 return not_found(pvmw
);
181 return not_found(pvmw
);
183 /* THP pmd was split under us: handle on pte level */
184 spin_unlock(pvmw
->ptl
);
187 } else if (!pmd_present(pmde
)) {
196 /* Seek to next pte only makes sense for THP */
197 if (!PageTransHuge(pvmw
->page
) || PageHuge(pvmw
->page
))
198 return not_found(pvmw
);
200 pvmw
->address
+= PAGE_SIZE
;
201 if (pvmw
->address
>= pvmw
->vma
->vm_end
||
203 __vma_address(pvmw
->page
, pvmw
->vma
) +
204 hpage_nr_pages(pvmw
->page
) * PAGE_SIZE
)
205 return not_found(pvmw
);
206 /* Did we cross page table boundary? */
207 if (pvmw
->address
% PMD_SIZE
== 0) {
208 pte_unmap(pvmw
->pte
);
210 spin_unlock(pvmw
->ptl
);
217 } while (pte_none(*pvmw
->pte
));
220 pvmw
->ptl
= pte_lockptr(mm
, pvmw
->pmd
);
221 spin_lock(pvmw
->ptl
);
227 * page_mapped_in_vma - check whether a page is really mapped in a VMA
228 * @page: the page to test
229 * @vma: the VMA to test
231 * Returns 1 if the page is mapped into the page tables of the VMA, 0
232 * if the page is not mapped into the page tables of this VMA. Only
233 * valid for normal file or anonymous VMAs.
235 int page_mapped_in_vma(struct page
*page
, struct vm_area_struct
*vma
)
237 struct page_vma_mapped_walk pvmw
= {
242 unsigned long start
, end
;
244 start
= __vma_address(page
, vma
);
245 end
= start
+ PAGE_SIZE
* (hpage_nr_pages(page
) - 1);
247 if (unlikely(end
< vma
->vm_start
|| start
>= vma
->vm_end
))
249 pvmw
.address
= max(start
, vma
->vm_start
);
250 if (!page_vma_mapped_walk(&pvmw
))
252 page_vma_mapped_walk_done(&pvmw
);