xref: /linux/mm/page_vma_mapped.c (revision 908dd0101e462e317b980bd8c139f47461499540)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/mm.h>
3 #include <linux/rmap.h>
4 #include <linux/hugetlb.h>
5 #include <linux/swap.h>
6 #include <linux/leafops.h>
7 
8 #include "internal.h"
9 
10 static inline bool not_found(struct page_vma_mapped_walk *pvmw)
11 {
12 	page_vma_mapped_walk_done(pvmw);
13 	return false;
14 }
15 
16 static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
17 		    spinlock_t **ptlp)
18 {
19 	bool is_migration;
20 	pte_t ptent;
21 
22 	if (pvmw->flags & PVMW_SYNC) {
23 		/* Use the stricter lookup */
24 		pvmw->pte = pte_offset_map_lock(pvmw->vma->vm_mm, pvmw->pmd,
25 						pvmw->address, &pvmw->ptl);
26 		*ptlp = pvmw->ptl;
27 		return !!pvmw->pte;
28 	}
29 
30 	is_migration = pvmw->flags & PVMW_MIGRATION;
31 again:
32 	/*
33 	 * It is important to return the ptl corresponding to pte,
34 	 * in case *pvmw->pmd changes underneath us; so we need to
35 	 * return it even when choosing not to lock, in case caller
36 	 * proceeds to loop over next ptes, and finds a match later.
37 	 * Though, in most cases, page lock already protects this.
38 	 */
39 	pvmw->pte = pte_offset_map_rw_nolock(pvmw->vma->vm_mm, pvmw->pmd,
40 					     pvmw->address, pmdvalp, ptlp);
41 	if (!pvmw->pte)
42 		return false;
43 
44 	ptent = ptep_get_lockless(pvmw->pte);
45 
46 	if (pte_none(ptent)) {
47 		return false;
48 	} else if (pte_present(ptent)) {
49 		if (is_migration)
50 			return false;
51 	} else if (!is_migration) {
52 		softleaf_t entry;
53 
54 		/*
55 		 * Handle un-addressable ZONE_DEVICE memory.
56 		 *
57 		 * We get here when we are trying to unmap a private
58 		 * device page from the process address space. Such
59 		 * page is not CPU accessible and thus is mapped as
60 		 * a special swap entry, nonetheless it still does
61 		 * count as a valid regular mapping for the page
62 		 * (and is accounted as such in page maps count).
63 		 *
64 		 * So handle this special case as if it was a normal
65 		 * page mapping ie lock CPU page table and return true.
66 		 *
67 		 * For more details on device private memory see HMM
68 		 * (include/linux/hmm.h or mm/hmm.c).
69 		 */
70 		entry = softleaf_from_pte(ptent);
71 		if (!softleaf_is_device_private(entry) &&
72 		    !softleaf_is_device_exclusive(entry))
73 			return false;
74 	}
75 	spin_lock(*ptlp);
76 	if (unlikely(!pmd_same(*pmdvalp, pmdp_get_lockless(pvmw->pmd)))) {
77 		pte_unmap_unlock(pvmw->pte, *ptlp);
78 		goto again;
79 	}
80 	pvmw->ptl = *ptlp;
81 
82 	return true;
83 }
84 
85 /**
86  * check_pte - check if [pvmw->pfn, @pvmw->pfn + @pvmw->nr_pages) is
87  * mapped at the @pvmw->pte
88  * @pvmw: page_vma_mapped_walk struct, includes a pair pte and pfn range
89  * for checking
90  * @pte_nr: the number of small pages described by @pvmw->pte.
91  *
92  * page_vma_mapped_walk() found a place where pfn range is *potentially*
93  * mapped. check_pte() has to validate this.
94  *
95  * pvmw->pte may point to empty PTE, swap PTE or PTE pointing to
96  * arbitrary page.
97  *
98  * If PVMW_MIGRATION flag is set, returns true if @pvmw->pte contains migration
99  * entry that points to [pvmw->pfn, @pvmw->pfn + @pvmw->nr_pages)
100  *
101  * If PVMW_MIGRATION flag is not set, returns true if pvmw->pte points to
102  * [pvmw->pfn, @pvmw->pfn + @pvmw->nr_pages)
103  *
104  * Otherwise, return false.
105  *
106  */
107 static bool check_pte(struct page_vma_mapped_walk *pvmw, unsigned long pte_nr)
108 {
109 	unsigned long pfn;
110 	pte_t ptent;
111 
112 	if (is_vm_hugetlb_page(pvmw->vma))
113 		ptent = huge_ptep_get(pvmw->vma->vm_mm, pvmw->address,
114 				      pvmw->pte);
115 	else
116 		ptent = ptep_get(pvmw->pte);
117 
118 	if (pvmw->flags & PVMW_MIGRATION) {
119 		const softleaf_t entry = softleaf_from_pte(ptent);
120 
121 		if (!softleaf_is_migration(entry))
122 			return false;
123 
124 		pfn = softleaf_to_pfn(entry);
125 	} else if (pte_present(ptent)) {
126 		pfn = pte_pfn(ptent);
127 	} else {
128 		const softleaf_t entry = softleaf_from_pte(ptent);
129 
130 		/* Handle un-addressable ZONE_DEVICE memory */
131 		if (!softleaf_is_device_private(entry) &&
132 		    !softleaf_is_device_exclusive(entry))
133 			return false;
134 
135 		pfn = softleaf_to_pfn(entry);
136 	}
137 
138 	if ((pfn + pte_nr - 1) < pvmw->pfn)
139 		return false;
140 	if (pfn > (pvmw->pfn + pvmw->nr_pages - 1))
141 		return false;
142 	return true;
143 }
144 
145 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
146 /* Returns true if the two ranges overlap.  Careful to not overflow. */
147 static bool check_pmd(unsigned long pfn, struct page_vma_mapped_walk *pvmw)
148 {
149 	if ((pfn + HPAGE_PMD_NR - 1) < pvmw->pfn)
150 		return false;
151 	if (pfn > pvmw->pfn + pvmw->nr_pages - 1)
152 		return false;
153 	return true;
154 }
155 #else
156 static bool check_pmd(unsigned long pfn, struct page_vma_mapped_walk *pvmw)
157 {
158 	return false;
159 }
160 #endif
161 
162 static void step_forward(struct page_vma_mapped_walk *pvmw, unsigned long size)
163 {
164 	pvmw->address = (pvmw->address + size) & ~(size - 1);
165 	if (!pvmw->address)
166 		pvmw->address = ULONG_MAX;
167 }
168 
169 /**
170  * page_vma_mapped_walk - check if @pvmw->pfn is mapped in @pvmw->vma at
171  * @pvmw->address
172  * @pvmw: pointer to struct page_vma_mapped_walk. page, vma, address and flags
173  * must be set. pmd, pte and ptl must be NULL.
174  *
175  * Returns true if the page is mapped in the vma. @pvmw->pmd and @pvmw->pte point
176  * to relevant page table entries. @pvmw->ptl is locked. @pvmw->address is
177  * adjusted if needed (for PTE-mapped THPs).
178  *
179  * If @pvmw->pmd is set but @pvmw->pte is not, you have found PMD-mapped page
180  * (usually THP). For PTE-mapped THP, you should run page_vma_mapped_walk() in
181  * a loop to find all PTEs that map the THP.
182  *
183  * For HugeTLB pages, @pvmw->pte is set to the relevant page table entry
184  * regardless of which page table level the page is mapped at. @pvmw->pmd is
185  * NULL.
186  *
187  * Returns false if there are no more page table entries for the page in
188  * the vma. @pvmw->ptl is unlocked and @pvmw->pte is unmapped.
189  *
190  * If you need to stop the walk before page_vma_mapped_walk() returned false,
191  * use page_vma_mapped_walk_done(). It will do the housekeeping.
192  */
193 bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
194 {
195 	struct vm_area_struct *vma = pvmw->vma;
196 	struct mm_struct *mm = vma->vm_mm;
197 	unsigned long end;
198 	spinlock_t *ptl;
199 	pte_t pteval;
200 	pgd_t *pgd;
201 	p4d_t *p4d;
202 	pud_t *pud;
203 	pmd_t pmde;
204 
205 	/* The only possible pmd mapping has been handled on last iteration */
206 	if (pvmw->pmd && !pvmw->pte)
207 		return not_found(pvmw);
208 
209 	if (unlikely(is_vm_hugetlb_page(vma))) {
210 		struct hstate *hstate = hstate_vma(vma);
211 		unsigned long size = huge_page_size(hstate);
212 		/* The only possible mapping was handled on last iteration */
213 		if (pvmw->pte)
214 			return not_found(pvmw);
215 		/*
216 		 * All callers that get here will already hold the
217 		 * i_mmap_rwsem.  Therefore, no additional locks need to be
218 		 * taken before calling hugetlb_walk().
219 		 */
220 		pvmw->pte = hugetlb_walk(vma, pvmw->address, size);
221 		if (!pvmw->pte)
222 			return false;
223 
224 		pvmw->ptl = huge_pte_lock(hstate, mm, pvmw->pte);
225 		if (!check_pte(pvmw, pages_per_huge_page(hstate)))
226 			return not_found(pvmw);
227 		return true;
228 	}
229 
230 	end = vma_address_end(pvmw);
231 	if (pvmw->pte)
232 		goto next_pte;
233 restart:
234 	do {
235 		pgd = pgd_offset(mm, pvmw->address);
236 		if (!pgd_present(*pgd)) {
237 			step_forward(pvmw, PGDIR_SIZE);
238 			continue;
239 		}
240 		p4d = p4d_offset(pgd, pvmw->address);
241 		if (!p4d_present(*p4d)) {
242 			step_forward(pvmw, P4D_SIZE);
243 			continue;
244 		}
245 		pud = pud_offset(p4d, pvmw->address);
246 		if (!pud_present(*pud)) {
247 			step_forward(pvmw, PUD_SIZE);
248 			continue;
249 		}
250 
251 		pvmw->pmd = pmd_offset(pud, pvmw->address);
252 		/*
253 		 * Make sure the pmd value isn't cached in a register by the
254 		 * compiler and used as a stale value after we've observed a
255 		 * subsequent update.
256 		 */
257 		pmde = pmdp_get_lockless(pvmw->pmd);
258 
259 		if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
260 		    (pmd_trans_huge(pmde) || pmd_is_migration_entry(pmde) ||
261 		    pmd_is_device_private_entry(pmde))) {
262 			pvmw->ptl = pmd_lock(mm, pvmw->pmd);
263 			pmde = *pvmw->pmd;
264 			if (pmd_is_migration_entry(pmde)) {
265 				softleaf_t entry;
266 
267 				if (!(pvmw->flags & PVMW_MIGRATION))
268 					return not_found(pvmw);
269 				entry = softleaf_from_pmd(pmde);
270 				if (!check_pmd(softleaf_to_pfn(entry), pvmw))
271 					return not_found(pvmw);
272 				return true;
273 			} else if (pmd_is_device_private_entry(pmde)) {
274 				softleaf_t entry;
275 
276 				if (pvmw->flags & PVMW_MIGRATION)
277 					return not_found(pvmw);
278 				entry = softleaf_from_pmd(pmde);
279 				if (!check_pmd(softleaf_to_pfn(entry), pvmw))
280 					return not_found(pvmw);
281 				return true;
282 			} else if (!pmd_present(pmde)) {
283 				return not_found(pvmw);
284 			}
285 			if (likely(pmd_trans_huge(pmde))) {
286 				if (pvmw->flags & PVMW_MIGRATION)
287 					return not_found(pvmw);
288 				if (!check_pmd(pmd_pfn(pmde), pvmw))
289 					return not_found(pvmw);
290 				return true;
291 			}
292 			/* THP/device-private pmd was split under us: handle on pte level */
293 			spin_unlock(pvmw->ptl);
294 			pvmw->ptl = NULL;
295 		} else if (!pmd_present(pmde)) {
296 			if ((pvmw->flags & PVMW_SYNC) &&
297 			    thp_vma_suitable_order(vma, pvmw->address,
298 						   PMD_ORDER) &&
299 			    (pvmw->nr_pages >= HPAGE_PMD_NR))
300 				sync_with_folio_pmd_zap(mm, pvmw->pmd);
301 
302 			step_forward(pvmw, PMD_SIZE);
303 			continue;
304 		}
305 		if (!map_pte(pvmw, &pmde, &ptl)) {
306 			if (!pvmw->pte)
307 				goto restart;
308 			goto next_pte;
309 		}
310 this_pte:
311 		if (check_pte(pvmw, 1))
312 			return true;
313 next_pte:
314 		do {
315 			pvmw->address += PAGE_SIZE;
316 			if (pvmw->address >= end)
317 				return not_found(pvmw);
318 			/* Did we cross page table boundary? */
319 			if ((pvmw->address & (PMD_SIZE - PAGE_SIZE)) == 0) {
320 				if (pvmw->ptl) {
321 					spin_unlock(pvmw->ptl);
322 					pvmw->ptl = NULL;
323 				}
324 				pte_unmap(pvmw->pte);
325 				pvmw->pte = NULL;
326 				pvmw->flags |= PVMW_PGTABLE_CROSSED;
327 				goto restart;
328 			}
329 			pvmw->pte++;
330 			if (!pvmw->ptl)
331 				pteval = ptep_get_lockless(pvmw->pte);
332 			else
333 				pteval = ptep_get(pvmw->pte);
334 		} while (pte_none(pteval));
335 
336 		if (!pvmw->ptl) {
337 			spin_lock(ptl);
338 			if (unlikely(!pmd_same(pmde, pmdp_get_lockless(pvmw->pmd)))) {
339 				pte_unmap_unlock(pvmw->pte, ptl);
340 				pvmw->pte = NULL;
341 				goto restart;
342 			}
343 			pvmw->ptl = ptl;
344 		}
345 		goto this_pte;
346 	} while (pvmw->address < end);
347 
348 	return false;
349 }
350 
351 #ifdef CONFIG_MEMORY_FAILURE
352 /**
353  * page_mapped_in_vma - check whether a page is really mapped in a VMA
354  * @page: the page to test
355  * @vma: the VMA to test
356  *
357  * Return: The address the page is mapped at if the page is in the range
358  * covered by the VMA and present in the page table.  If the page is
359  * outside the VMA or not present, returns -EFAULT.
360  * Only valid for normal file or anonymous VMAs.
361  */
362 unsigned long page_mapped_in_vma(const struct page *page,
363 		struct vm_area_struct *vma)
364 {
365 	const struct folio *folio = page_folio(page);
366 	const pgoff_t pgoff = page_pgoff(folio, page);
367 	struct page_vma_mapped_walk pvmw = {
368 		.pfn = page_to_pfn(page),
369 		.nr_pages = 1,
370 		.vma = vma,
371 		.flags = PVMW_SYNC,
372 	};
373 
374 	if (folio_test_anon(folio))
375 		pvmw.address = vma_anon_address(vma, pgoff, 1);
376 	else
377 		pvmw.address = vma_filebacked_address(vma, pgoff, 1);
378 	if (pvmw.address == -EFAULT)
379 		goto out;
380 	if (!page_vma_mapped_walk(&pvmw))
381 		return -EFAULT;
382 	page_vma_mapped_walk_done(&pvmw);
383 out:
384 	return pvmw.address;
385 }
386 #endif
387