page_vma_mapped.c 8.6 KB

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