2 #include <linux/rmap.h>
3 #include <linux/hugetlb.h>
4 #include <linux/swap.h>
5 #include <linux/swapops.h>
9 static inline bool check_pmd(struct page_vma_mapped_walk
*pvmw
)
13 * Make sure we don't re-load pmd between present and !trans_huge check.
14 * We need a consistent view.
16 pmde
= READ_ONCE(*pvmw
->pmd
);
17 return pmd_present(pmde
) && !pmd_trans_huge(pmde
);
20 static inline bool not_found(struct page_vma_mapped_walk
*pvmw
)
22 page_vma_mapped_walk_done(pvmw
);
26 static bool map_pte(struct page_vma_mapped_walk
*pvmw
)
28 pvmw
->pte
= pte_offset_map(pvmw
->pmd
, pvmw
->address
);
29 if (!(pvmw
->flags
& PVMW_SYNC
)) {
30 if (pvmw
->flags
& PVMW_MIGRATION
) {
31 if (!is_swap_pte(*pvmw
->pte
))
34 if (!pte_present(*pvmw
->pte
))
38 pvmw
->ptl
= pte_lockptr(pvmw
->vma
->vm_mm
, pvmw
->pmd
);
43 static bool check_pte(struct page_vma_mapped_walk
*pvmw
)
45 if (pvmw
->flags
& PVMW_MIGRATION
) {
46 #ifdef CONFIG_MIGRATION
48 if (!is_swap_pte(*pvmw
->pte
))
50 entry
= pte_to_swp_entry(*pvmw
->pte
);
52 if (!is_migration_entry(entry
))
54 if (migration_entry_to_page(entry
) - pvmw
->page
>=
55 hpage_nr_pages(pvmw
->page
)) {
58 if (migration_entry_to_page(entry
) < pvmw
->page
)
64 if (is_swap_pte(*pvmw
->pte
)) {
67 entry
= pte_to_swp_entry(*pvmw
->pte
);
68 if (is_device_private_entry(entry
) &&
69 device_private_entry_to_page(entry
) == pvmw
->page
)
73 if (!pte_present(*pvmw
->pte
))
76 /* THP can be referenced by any subpage */
77 if (pte_page(*pvmw
->pte
) - pvmw
->page
>=
78 hpage_nr_pages(pvmw
->page
)) {
81 if (pte_page(*pvmw
->pte
) < pvmw
->page
)
89 * page_vma_mapped_walk - check if @pvmw->page is mapped in @pvmw->vma at
91 * @pvmw: pointer to struct page_vma_mapped_walk. page, vma, address and flags
92 * must be set. pmd, pte and ptl must be NULL.
94 * Returns true if the page is mapped in the vma. @pvmw->pmd and @pvmw->pte point
95 * to relevant page table entries. @pvmw->ptl is locked. @pvmw->address is
96 * adjusted if needed (for PTE-mapped THPs).
98 * If @pvmw->pmd is set but @pvmw->pte is not, you have found PMD-mapped page
99 * (usually THP). For PTE-mapped THP, you should run page_vma_mapped_walk() in
100 * a loop to find all PTEs that map the THP.
102 * For HugeTLB pages, @pvmw->pte is set to the relevant page table entry
103 * regardless of which page table level the page is mapped at. @pvmw->pmd is
106 * Retruns false if there are no more page table entries for the page in
107 * the vma. @pvmw->ptl is unlocked and @pvmw->pte is unmapped.
109 * If you need to stop the walk before page_vma_mapped_walk() returned false,
110 * use page_vma_mapped_walk_done(). It will do the housekeeping.
112 bool page_vma_mapped_walk(struct page_vma_mapped_walk
*pvmw
)
114 struct mm_struct
*mm
= pvmw
->vma
->vm_mm
;
115 struct page
*page
= pvmw
->page
;
120 /* The only possible pmd mapping has been handled on last iteration */
121 if (pvmw
->pmd
&& !pvmw
->pte
)
122 return not_found(pvmw
);
127 if (unlikely(PageHuge(pvmw
->page
))) {
128 /* when pud is not present, pte will be NULL */
129 pvmw
->pte
= huge_pte_offset(mm
, pvmw
->address
,
130 PAGE_SIZE
<< compound_order(page
));
134 pvmw
->ptl
= huge_pte_lockptr(page_hstate(page
), mm
, pvmw
->pte
);
135 spin_lock(pvmw
->ptl
);
136 if (!check_pte(pvmw
))
137 return not_found(pvmw
);
141 pgd
= pgd_offset(mm
, pvmw
->address
);
142 if (!pgd_present(*pgd
))
144 p4d
= p4d_offset(pgd
, pvmw
->address
);
145 if (!p4d_present(*p4d
))
147 pud
= pud_offset(p4d
, pvmw
->address
);
148 if (!pud_present(*pud
))
150 pvmw
->pmd
= pmd_offset(pud
, pvmw
->address
);
151 if (pmd_trans_huge(*pvmw
->pmd
) || is_pmd_migration_entry(*pvmw
->pmd
)) {
152 pvmw
->ptl
= pmd_lock(mm
, pvmw
->pmd
);
153 if (likely(pmd_trans_huge(*pvmw
->pmd
))) {
154 if (pvmw
->flags
& PVMW_MIGRATION
)
155 return not_found(pvmw
);
156 if (pmd_page(*pvmw
->pmd
) != page
)
157 return not_found(pvmw
);
159 } else if (!pmd_present(*pvmw
->pmd
)) {
160 if (thp_migration_supported()) {
161 if (!(pvmw
->flags
& PVMW_MIGRATION
))
162 return not_found(pvmw
);
163 if (is_migration_entry(pmd_to_swp_entry(*pvmw
->pmd
))) {
164 swp_entry_t entry
= pmd_to_swp_entry(*pvmw
->pmd
);
166 if (migration_entry_to_page(entry
) != page
)
167 return not_found(pvmw
);
171 WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!");
172 return not_found(pvmw
);
174 /* THP pmd was split under us: handle on pte level */
175 spin_unlock(pvmw
->ptl
);
179 if (!check_pmd(pvmw
))
188 /* Seek to next pte only makes sense for THP */
189 if (!PageTransHuge(pvmw
->page
) || PageHuge(pvmw
->page
))
190 return not_found(pvmw
);
192 pvmw
->address
+= PAGE_SIZE
;
193 if (pvmw
->address
>= pvmw
->vma
->vm_end
||
195 __vma_address(pvmw
->page
, pvmw
->vma
) +
196 hpage_nr_pages(pvmw
->page
) * PAGE_SIZE
)
197 return not_found(pvmw
);
198 /* Did we cross page table boundary? */
199 if (pvmw
->address
% PMD_SIZE
== 0) {
200 pte_unmap(pvmw
->pte
);
202 spin_unlock(pvmw
->ptl
);
209 } while (pte_none(*pvmw
->pte
));
212 pvmw
->ptl
= pte_lockptr(mm
, pvmw
->pmd
);
213 spin_lock(pvmw
->ptl
);
219 * page_mapped_in_vma - check whether a page is really mapped in a VMA
220 * @page: the page to test
221 * @vma: the VMA to test
223 * Returns 1 if the page is mapped into the page tables of the VMA, 0
224 * if the page is not mapped into the page tables of this VMA. Only
225 * valid for normal file or anonymous VMAs.
227 int page_mapped_in_vma(struct page
*page
, struct vm_area_struct
*vma
)
229 struct page_vma_mapped_walk pvmw
= {
234 unsigned long start
, end
;
236 start
= __vma_address(page
, vma
);
237 end
= start
+ PAGE_SIZE
* (hpage_nr_pages(page
) - 1);
239 if (unlikely(end
< vma
->vm_start
|| start
>= vma
->vm_end
))
241 pvmw
.address
= max(start
, vma
->vm_start
);
242 if (!page_vma_mapped_walk(&pvmw
))
244 page_vma_mapped_walk_done(&pvmw
);