page_vma_mapped.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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)
  14. {
  15. pvmw->pte = pte_offset_map(pvmw->pmd, pvmw->address);
  16. if (!(pvmw->flags & PVMW_SYNC)) {
  17. if (pvmw->flags & PVMW_MIGRATION) {
  18. if (!is_swap_pte(*pvmw->pte))
  19. return false;
  20. } else {
  21. /*
  22. * We get here when we are trying to unmap a private
  23. * device page from the process address space. Such
  24. * page is not CPU accessible and thus is mapped as
  25. * a special swap entry, nonetheless it still does
  26. * count as a valid regular mapping for the page (and
  27. * is accounted as such in page maps count).
  28. *
  29. * So handle this special case as if it was a normal
  30. * page mapping ie lock CPU page table and returns
  31. * true.
  32. *
  33. * For more details on device private memory see HMM
  34. * (include/linux/hmm.h or mm/hmm.c).
  35. */
  36. if (is_swap_pte(*pvmw->pte)) {
  37. swp_entry_t entry;
  38. /* Handle un-addressable ZONE_DEVICE memory */
  39. entry = pte_to_swp_entry(*pvmw->pte);
  40. if (!is_device_private_entry(entry))
  41. return false;
  42. } else if (!pte_present(*pvmw->pte))
  43. return false;
  44. }
  45. }
  46. pvmw->ptl = pte_lockptr(pvmw->vma->vm_mm, pvmw->pmd);
  47. spin_lock(pvmw->ptl);
  48. return true;
  49. }
  50. static inline bool pfn_in_hpage(struct page *hpage, unsigned long pfn)
  51. {
  52. unsigned long hpage_pfn = page_to_pfn(hpage);
  53. /* THP can be referenced by any subpage */
  54. return pfn >= hpage_pfn && pfn - hpage_pfn < hpage_nr_pages(hpage);
  55. }
  56. /**
  57. * check_pte - check if @pvmw->page is mapped at the @pvmw->pte
  58. *
  59. * page_vma_mapped_walk() found a place where @pvmw->page is *potentially*
  60. * mapped. check_pte() has to validate this.
  61. *
  62. * @pvmw->pte may point to empty PTE, swap PTE or PTE pointing to arbitrary
  63. * page.
  64. *
  65. * If PVMW_MIGRATION flag is set, returns true if @pvmw->pte contains migration
  66. * entry that points to @pvmw->page or any subpage in case of THP.
  67. *
  68. * If PVMW_MIGRATION flag is not set, returns true if @pvmw->pte points to
  69. * @pvmw->page or any subpage in case of THP.
  70. *
  71. * Otherwise, return false.
  72. *
  73. */
  74. static bool check_pte(struct page_vma_mapped_walk *pvmw)
  75. {
  76. unsigned long pfn;
  77. if (pvmw->flags & PVMW_MIGRATION) {
  78. swp_entry_t entry;
  79. if (!is_swap_pte(*pvmw->pte))
  80. return false;
  81. entry = pte_to_swp_entry(*pvmw->pte);
  82. if (!is_migration_entry(entry))
  83. return false;
  84. pfn = migration_entry_to_pfn(entry);
  85. } else if (is_swap_pte(*pvmw->pte)) {
  86. swp_entry_t entry;
  87. /* Handle un-addressable ZONE_DEVICE memory */
  88. entry = pte_to_swp_entry(*pvmw->pte);
  89. if (!is_device_private_entry(entry))
  90. return false;
  91. pfn = device_private_entry_to_pfn(entry);
  92. } else {
  93. if (!pte_present(*pvmw->pte))
  94. return false;
  95. pfn = pte_pfn(*pvmw->pte);
  96. }
  97. return pfn_in_hpage(pvmw->page, pfn);
  98. }
  99. /**
  100. * page_vma_mapped_walk - check if @pvmw->page is mapped in @pvmw->vma at
  101. * @pvmw->address
  102. * @pvmw: pointer to struct page_vma_mapped_walk. page, vma, address and flags
  103. * must be set. pmd, pte and ptl must be NULL.
  104. *
  105. * Returns true if the page is mapped in the vma. @pvmw->pmd and @pvmw->pte point
  106. * to relevant page table entries. @pvmw->ptl is locked. @pvmw->address is
  107. * adjusted if needed (for PTE-mapped THPs).
  108. *
  109. * If @pvmw->pmd is set but @pvmw->pte is not, you have found PMD-mapped page
  110. * (usually THP). For PTE-mapped THP, you should run page_vma_mapped_walk() in
  111. * a loop to find all PTEs that map the THP.
  112. *
  113. * For HugeTLB pages, @pvmw->pte is set to the relevant page table entry
  114. * regardless of which page table level the page is mapped at. @pvmw->pmd is
  115. * NULL.
  116. *
  117. * Retruns false if there are no more page table entries for the page in
  118. * the vma. @pvmw->ptl is unlocked and @pvmw->pte is unmapped.
  119. *
  120. * If you need to stop the walk before page_vma_mapped_walk() returned false,
  121. * use page_vma_mapped_walk_done(). It will do the housekeeping.
  122. */
  123. bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
  124. {
  125. struct mm_struct *mm = pvmw->vma->vm_mm;
  126. struct page *page = pvmw->page;
  127. pgd_t *pgd;
  128. p4d_t *p4d;
  129. pud_t *pud;
  130. pmd_t pmde;
  131. /* The only possible pmd mapping has been handled on last iteration */
  132. if (pvmw->pmd && !pvmw->pte)
  133. return not_found(pvmw);
  134. if (pvmw->pte)
  135. goto next_pte;
  136. if (unlikely(PageHuge(pvmw->page))) {
  137. /* when pud is not present, pte will be NULL */
  138. pvmw->pte = huge_pte_offset(mm, pvmw->address,
  139. PAGE_SIZE << compound_order(page));
  140. if (!pvmw->pte)
  141. return false;
  142. pvmw->ptl = huge_pte_lockptr(page_hstate(page), mm, pvmw->pte);
  143. spin_lock(pvmw->ptl);
  144. if (!check_pte(pvmw))
  145. return not_found(pvmw);
  146. return true;
  147. }
  148. restart:
  149. pgd = pgd_offset(mm, pvmw->address);
  150. if (!pgd_present(*pgd))
  151. return false;
  152. p4d = p4d_offset(pgd, pvmw->address);
  153. if (!p4d_present(*p4d))
  154. return false;
  155. pud = pud_offset(p4d, pvmw->address);
  156. if (!pud_present(*pud))
  157. return false;
  158. pvmw->pmd = pmd_offset(pud, pvmw->address);
  159. /*
  160. * Make sure the pmd value isn't cached in a register by the
  161. * compiler and used as a stale value after we've observed a
  162. * subsequent update.
  163. */
  164. pmde = READ_ONCE(*pvmw->pmd);
  165. if (pmd_trans_huge(pmde) || is_pmd_migration_entry(pmde)) {
  166. pvmw->ptl = pmd_lock(mm, pvmw->pmd);
  167. if (likely(pmd_trans_huge(*pvmw->pmd))) {
  168. if (pvmw->flags & PVMW_MIGRATION)
  169. return not_found(pvmw);
  170. if (pmd_page(*pvmw->pmd) != page)
  171. return not_found(pvmw);
  172. return true;
  173. } else if (!pmd_present(*pvmw->pmd)) {
  174. if (thp_migration_supported()) {
  175. if (!(pvmw->flags & PVMW_MIGRATION))
  176. return not_found(pvmw);
  177. if (is_migration_entry(pmd_to_swp_entry(*pvmw->pmd))) {
  178. swp_entry_t entry = pmd_to_swp_entry(*pvmw->pmd);
  179. if (migration_entry_to_page(entry) != page)
  180. return not_found(pvmw);
  181. return true;
  182. }
  183. }
  184. return not_found(pvmw);
  185. } else {
  186. /* THP pmd was split under us: handle on pte level */
  187. spin_unlock(pvmw->ptl);
  188. pvmw->ptl = NULL;
  189. }
  190. } else if (!pmd_present(pmde)) {
  191. return false;
  192. }
  193. if (!map_pte(pvmw))
  194. goto next_pte;
  195. while (1) {
  196. if (check_pte(pvmw))
  197. return true;
  198. next_pte:
  199. /* Seek to next pte only makes sense for THP */
  200. if (!PageTransHuge(pvmw->page) || PageHuge(pvmw->page))
  201. return not_found(pvmw);
  202. do {
  203. pvmw->address += PAGE_SIZE;
  204. if (pvmw->address >= pvmw->vma->vm_end ||
  205. pvmw->address >=
  206. __vma_address(pvmw->page, pvmw->vma) +
  207. hpage_nr_pages(pvmw->page) * PAGE_SIZE)
  208. return not_found(pvmw);
  209. /* Did we cross page table boundary? */
  210. if (pvmw->address % PMD_SIZE == 0) {
  211. pte_unmap(pvmw->pte);
  212. if (pvmw->ptl) {
  213. spin_unlock(pvmw->ptl);
  214. pvmw->ptl = NULL;
  215. }
  216. goto restart;
  217. } else {
  218. pvmw->pte++;
  219. }
  220. } while (pte_none(*pvmw->pte));
  221. if (!pvmw->ptl) {
  222. pvmw->ptl = pte_lockptr(mm, pvmw->pmd);
  223. spin_lock(pvmw->ptl);
  224. }
  225. }
  226. }
  227. /**
  228. * page_mapped_in_vma - check whether a page is really mapped in a VMA
  229. * @page: the page to test
  230. * @vma: the VMA to test
  231. *
  232. * Returns 1 if the page is mapped into the page tables of the VMA, 0
  233. * if the page is not mapped into the page tables of this VMA. Only
  234. * valid for normal file or anonymous VMAs.
  235. */
  236. int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma)
  237. {
  238. struct page_vma_mapped_walk pvmw = {
  239. .page = page,
  240. .vma = vma,
  241. .flags = PVMW_SYNC,
  242. };
  243. unsigned long start, end;
  244. start = __vma_address(page, vma);
  245. end = start + PAGE_SIZE * (hpage_nr_pages(page) - 1);
  246. if (unlikely(end < vma->vm_start || start >= vma->vm_end))
  247. return 0;
  248. pvmw.address = max(start, vma->vm_start);
  249. if (!page_vma_mapped_walk(&pvmw))
  250. return 0;
  251. page_vma_mapped_walk_done(&pvmw);
  252. return 1;
  253. }