page_vma_mapped.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. /**
  51. * check_pte - check if @pvmw->page is mapped at the @pvmw->pte
  52. *
  53. * page_vma_mapped_walk() found a place where @pvmw->page is *potentially*
  54. * mapped. check_pte() has to validate this.
  55. *
  56. * @pvmw->pte may point to empty PTE, swap PTE or PTE pointing to arbitrary
  57. * page.
  58. *
  59. * If PVMW_MIGRATION flag is set, returns true if @pvmw->pte contains migration
  60. * entry that points to @pvmw->page or any subpage in case of THP.
  61. *
  62. * If PVMW_MIGRATION flag is not set, returns true if @pvmw->pte points to
  63. * @pvmw->page or any subpage in case of THP.
  64. *
  65. * Otherwise, return false.
  66. *
  67. */
  68. static bool check_pte(struct page_vma_mapped_walk *pvmw)
  69. {
  70. unsigned long pfn;
  71. if (pvmw->flags & PVMW_MIGRATION) {
  72. swp_entry_t entry;
  73. if (!is_swap_pte(*pvmw->pte))
  74. return false;
  75. entry = pte_to_swp_entry(*pvmw->pte);
  76. if (!is_migration_entry(entry))
  77. return false;
  78. pfn = migration_entry_to_pfn(entry);
  79. } else if (is_swap_pte(*pvmw->pte)) {
  80. swp_entry_t entry;
  81. /* Handle un-addressable ZONE_DEVICE memory */
  82. entry = pte_to_swp_entry(*pvmw->pte);
  83. if (!is_device_private_entry(entry))
  84. return false;
  85. pfn = device_private_entry_to_pfn(entry);
  86. } else {
  87. if (!pte_present(*pvmw->pte))
  88. return false;
  89. pfn = pte_pfn(*pvmw->pte);
  90. }
  91. if (pfn < page_to_pfn(pvmw->page))
  92. return false;
  93. /* THP can be referenced by any subpage */
  94. if (pfn - page_to_pfn(pvmw->page) >= hpage_nr_pages(pvmw->page))
  95. return false;
  96. return true;
  97. }
  98. static void step_forward(struct page_vma_mapped_walk *pvmw, unsigned long size)
  99. {
  100. pvmw->address = (pvmw->address + size) & ~(size - 1);
  101. if (!pvmw->address)
  102. pvmw->address = ULONG_MAX;
  103. }
  104. /**
  105. * page_vma_mapped_walk - check if @pvmw->page is mapped in @pvmw->vma at
  106. * @pvmw->address
  107. * @pvmw: pointer to struct page_vma_mapped_walk. page, vma, address and flags
  108. * must be set. pmd, pte and ptl must be NULL.
  109. *
  110. * Returns true if the page is mapped in the vma. @pvmw->pmd and @pvmw->pte point
  111. * to relevant page table entries. @pvmw->ptl is locked. @pvmw->address is
  112. * adjusted if needed (for PTE-mapped THPs).
  113. *
  114. * If @pvmw->pmd is set but @pvmw->pte is not, you have found PMD-mapped page
  115. * (usually THP). For PTE-mapped THP, you should run page_vma_mapped_walk() in
  116. * a loop to find all PTEs that map the THP.
  117. *
  118. * For HugeTLB pages, @pvmw->pte is set to the relevant page table entry
  119. * regardless of which page table level the page is mapped at. @pvmw->pmd is
  120. * NULL.
  121. *
  122. * Retruns false if there are no more page table entries for the page in
  123. * the vma. @pvmw->ptl is unlocked and @pvmw->pte is unmapped.
  124. *
  125. * If you need to stop the walk before page_vma_mapped_walk() returned false,
  126. * use page_vma_mapped_walk_done(). It will do the housekeeping.
  127. */
  128. bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
  129. {
  130. struct mm_struct *mm = pvmw->vma->vm_mm;
  131. struct page *page = pvmw->page;
  132. unsigned long end;
  133. pgd_t *pgd;
  134. p4d_t *p4d;
  135. pud_t *pud;
  136. pmd_t pmde;
  137. /* The only possible pmd mapping has been handled on last iteration */
  138. if (pvmw->pmd && !pvmw->pte)
  139. return not_found(pvmw);
  140. if (unlikely(PageHuge(page))) {
  141. /* The only possible mapping was handled on last iteration */
  142. if (pvmw->pte)
  143. return not_found(pvmw);
  144. /* when pud is not present, pte will be NULL */
  145. pvmw->pte = huge_pte_offset(mm, pvmw->address,
  146. PAGE_SIZE << compound_order(page));
  147. if (!pvmw->pte)
  148. return false;
  149. pvmw->ptl = huge_pte_lockptr(page_hstate(page), mm, pvmw->pte);
  150. spin_lock(pvmw->ptl);
  151. if (!check_pte(pvmw))
  152. return not_found(pvmw);
  153. return true;
  154. }
  155. /*
  156. * Seek to next pte only makes sense for THP.
  157. * But more important than that optimization, is to filter out
  158. * any PageKsm page: whose page->index misleads vma_address()
  159. * and vma_address_end() to disaster.
  160. */
  161. end = PageTransCompound(page) ?
  162. vma_address_end(page, pvmw->vma) :
  163. pvmw->address + PAGE_SIZE;
  164. if (pvmw->pte)
  165. goto next_pte;
  166. restart:
  167. do {
  168. pgd = pgd_offset(mm, pvmw->address);
  169. if (!pgd_present(*pgd)) {
  170. step_forward(pvmw, PGDIR_SIZE);
  171. continue;
  172. }
  173. p4d = p4d_offset(pgd, pvmw->address);
  174. if (!p4d_present(*p4d)) {
  175. step_forward(pvmw, P4D_SIZE);
  176. continue;
  177. }
  178. pud = pud_offset(p4d, pvmw->address);
  179. if (!pud_present(*pud)) {
  180. step_forward(pvmw, PUD_SIZE);
  181. continue;
  182. }
  183. pvmw->pmd = pmd_offset(pud, pvmw->address);
  184. /*
  185. * Make sure the pmd value isn't cached in a register by the
  186. * compiler and used as a stale value after we've observed a
  187. * subsequent update.
  188. */
  189. pmde = READ_ONCE(*pvmw->pmd);
  190. if (pmd_trans_huge(pmde) || is_pmd_migration_entry(pmde)) {
  191. pvmw->ptl = pmd_lock(mm, pvmw->pmd);
  192. pmde = *pvmw->pmd;
  193. if (likely(pmd_trans_huge(pmde))) {
  194. if (pvmw->flags & PVMW_MIGRATION)
  195. return not_found(pvmw);
  196. if (pmd_page(pmde) != page)
  197. return not_found(pvmw);
  198. return true;
  199. }
  200. if (!pmd_present(pmde)) {
  201. swp_entry_t entry;
  202. if (!thp_migration_supported() ||
  203. !(pvmw->flags & PVMW_MIGRATION))
  204. return not_found(pvmw);
  205. entry = pmd_to_swp_entry(pmde);
  206. if (!is_migration_entry(entry) ||
  207. migration_entry_to_page(entry) != page)
  208. return not_found(pvmw);
  209. return true;
  210. }
  211. /* THP pmd was split under us: handle on pte level */
  212. spin_unlock(pvmw->ptl);
  213. pvmw->ptl = NULL;
  214. } else if (!pmd_present(pmde)) {
  215. /*
  216. * If PVMW_SYNC, take and drop THP pmd lock so that we
  217. * cannot return prematurely, while zap_huge_pmd() has
  218. * cleared *pmd but not decremented compound_mapcount().
  219. */
  220. if ((pvmw->flags & PVMW_SYNC) &&
  221. PageTransCompound(page)) {
  222. spinlock_t *ptl = pmd_lock(mm, pvmw->pmd);
  223. spin_unlock(ptl);
  224. }
  225. step_forward(pvmw, PMD_SIZE);
  226. continue;
  227. }
  228. if (!map_pte(pvmw))
  229. goto next_pte;
  230. this_pte:
  231. if (check_pte(pvmw))
  232. return true;
  233. next_pte:
  234. do {
  235. pvmw->address += PAGE_SIZE;
  236. if (pvmw->address >= end)
  237. return not_found(pvmw);
  238. /* Did we cross page table boundary? */
  239. if ((pvmw->address & (PMD_SIZE - PAGE_SIZE)) == 0) {
  240. if (pvmw->ptl) {
  241. spin_unlock(pvmw->ptl);
  242. pvmw->ptl = NULL;
  243. }
  244. pte_unmap(pvmw->pte);
  245. pvmw->pte = NULL;
  246. goto restart;
  247. }
  248. pvmw->pte++;
  249. if ((pvmw->flags & PVMW_SYNC) && !pvmw->ptl) {
  250. pvmw->ptl = pte_lockptr(mm, pvmw->pmd);
  251. spin_lock(pvmw->ptl);
  252. }
  253. } while (pte_none(*pvmw->pte));
  254. if (!pvmw->ptl) {
  255. pvmw->ptl = pte_lockptr(mm, pvmw->pmd);
  256. spin_lock(pvmw->ptl);
  257. }
  258. goto this_pte;
  259. } while (pvmw->address < end);
  260. return false;
  261. }
  262. /**
  263. * page_mapped_in_vma - check whether a page is really mapped in a VMA
  264. * @page: the page to test
  265. * @vma: the VMA to test
  266. *
  267. * Returns 1 if the page is mapped into the page tables of the VMA, 0
  268. * if the page is not mapped into the page tables of this VMA. Only
  269. * valid for normal file or anonymous VMAs.
  270. */
  271. int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma)
  272. {
  273. struct page_vma_mapped_walk pvmw = {
  274. .page = page,
  275. .vma = vma,
  276. .flags = PVMW_SYNC,
  277. };
  278. pvmw.address = vma_address(page, vma);
  279. if (pvmw.address == -EFAULT)
  280. return 0;
  281. if (!page_vma_mapped_walk(&pvmw))
  282. return 0;
  283. page_vma_mapped_walk_done(&pvmw);
  284. return 1;
  285. }