page_vma_mapped.c 9.7 KB

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