pagewalk.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/mm.h>
  3. #include <linux/highmem.h>
  4. #include <linux/sched.h>
  5. #include <linux/hugetlb.h>
  6. static int walk_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
  7. struct mm_walk *walk)
  8. {
  9. pte_t *pte;
  10. int err = 0;
  11. pte = pte_offset_map(pmd, addr);
  12. for (;;) {
  13. err = walk->pte_entry(pte, addr, addr + PAGE_SIZE, walk);
  14. if (err)
  15. break;
  16. addr += PAGE_SIZE;
  17. if (addr == end)
  18. break;
  19. pte++;
  20. }
  21. pte_unmap(pte);
  22. return err;
  23. }
  24. static int walk_pmd_range(pud_t *pud, unsigned long addr, unsigned long end,
  25. struct mm_walk *walk)
  26. {
  27. pmd_t *pmd;
  28. unsigned long next;
  29. int err = 0;
  30. pmd = pmd_offset(pud, addr);
  31. do {
  32. again:
  33. next = pmd_addr_end(addr, end);
  34. if (pmd_none(*pmd) || !walk->vma) {
  35. if (walk->pte_hole)
  36. err = walk->pte_hole(addr, next, walk);
  37. if (err)
  38. break;
  39. continue;
  40. }
  41. /*
  42. * This implies that each ->pmd_entry() handler
  43. * needs to know about pmd_trans_huge() pmds
  44. */
  45. if (walk->pmd_entry)
  46. err = walk->pmd_entry(pmd, addr, next, walk);
  47. if (err)
  48. break;
  49. /*
  50. * Check this here so we only break down trans_huge
  51. * pages when we _need_ to
  52. */
  53. if (!walk->pte_entry)
  54. continue;
  55. split_huge_pmd(walk->vma, pmd, addr);
  56. if (pmd_trans_unstable(pmd))
  57. goto again;
  58. err = walk_pte_range(pmd, addr, next, walk);
  59. if (err)
  60. break;
  61. } while (pmd++, addr = next, addr != end);
  62. return err;
  63. }
  64. static int walk_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end,
  65. struct mm_walk *walk)
  66. {
  67. pud_t *pud;
  68. unsigned long next;
  69. int err = 0;
  70. pud = pud_offset(p4d, addr);
  71. do {
  72. again:
  73. next = pud_addr_end(addr, end);
  74. if (pud_none(*pud) || !walk->vma) {
  75. if (walk->pte_hole)
  76. err = walk->pte_hole(addr, next, walk);
  77. if (err)
  78. break;
  79. continue;
  80. }
  81. if (walk->pud_entry) {
  82. spinlock_t *ptl = pud_trans_huge_lock(pud, walk->vma);
  83. if (ptl) {
  84. err = walk->pud_entry(pud, addr, next, walk);
  85. spin_unlock(ptl);
  86. if (err)
  87. break;
  88. continue;
  89. }
  90. }
  91. split_huge_pud(walk->vma, pud, addr);
  92. if (pud_none(*pud))
  93. goto again;
  94. if (walk->pmd_entry || walk->pte_entry)
  95. err = walk_pmd_range(pud, addr, next, walk);
  96. if (err)
  97. break;
  98. } while (pud++, addr = next, addr != end);
  99. return err;
  100. }
  101. static int walk_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end,
  102. struct mm_walk *walk)
  103. {
  104. p4d_t *p4d;
  105. unsigned long next;
  106. int err = 0;
  107. p4d = p4d_offset(pgd, addr);
  108. do {
  109. next = p4d_addr_end(addr, end);
  110. if (p4d_none_or_clear_bad(p4d)) {
  111. if (walk->pte_hole)
  112. err = walk->pte_hole(addr, next, walk);
  113. if (err)
  114. break;
  115. continue;
  116. }
  117. if (walk->pmd_entry || walk->pte_entry)
  118. err = walk_pud_range(p4d, addr, next, walk);
  119. if (err)
  120. break;
  121. } while (p4d++, addr = next, addr != end);
  122. return err;
  123. }
  124. static int walk_pgd_range(unsigned long addr, unsigned long end,
  125. struct mm_walk *walk)
  126. {
  127. pgd_t *pgd;
  128. unsigned long next;
  129. int err = 0;
  130. pgd = pgd_offset(walk->mm, addr);
  131. do {
  132. next = pgd_addr_end(addr, end);
  133. if (pgd_none_or_clear_bad(pgd)) {
  134. if (walk->pte_hole)
  135. err = walk->pte_hole(addr, next, walk);
  136. if (err)
  137. break;
  138. continue;
  139. }
  140. if (walk->pmd_entry || walk->pte_entry)
  141. err = walk_p4d_range(pgd, addr, next, walk);
  142. if (err)
  143. break;
  144. } while (pgd++, addr = next, addr != end);
  145. return err;
  146. }
  147. #ifdef CONFIG_HUGETLB_PAGE
  148. static unsigned long hugetlb_entry_end(struct hstate *h, unsigned long addr,
  149. unsigned long end)
  150. {
  151. unsigned long boundary = (addr & huge_page_mask(h)) + huge_page_size(h);
  152. return boundary < end ? boundary : end;
  153. }
  154. static int walk_hugetlb_range(unsigned long addr, unsigned long end,
  155. struct mm_walk *walk)
  156. {
  157. struct vm_area_struct *vma = walk->vma;
  158. struct hstate *h = hstate_vma(vma);
  159. unsigned long next;
  160. unsigned long hmask = huge_page_mask(h);
  161. unsigned long sz = huge_page_size(h);
  162. pte_t *pte;
  163. int err = 0;
  164. do {
  165. next = hugetlb_entry_end(h, addr, end);
  166. pte = huge_pte_offset(walk->mm, addr & hmask, sz);
  167. if (pte)
  168. err = walk->hugetlb_entry(pte, hmask, addr, next, walk);
  169. else if (walk->pte_hole)
  170. err = walk->pte_hole(addr, next, walk);
  171. if (err)
  172. break;
  173. } while (addr = next, addr != end);
  174. return err;
  175. }
  176. #else /* CONFIG_HUGETLB_PAGE */
  177. static int walk_hugetlb_range(unsigned long addr, unsigned long end,
  178. struct mm_walk *walk)
  179. {
  180. return 0;
  181. }
  182. #endif /* CONFIG_HUGETLB_PAGE */
  183. /*
  184. * Decide whether we really walk over the current vma on [@start, @end)
  185. * or skip it via the returned value. Return 0 if we do walk over the
  186. * current vma, and return 1 if we skip the vma. Negative values means
  187. * error, where we abort the current walk.
  188. */
  189. static int walk_page_test(unsigned long start, unsigned long end,
  190. struct mm_walk *walk)
  191. {
  192. struct vm_area_struct *vma = walk->vma;
  193. if (walk->test_walk)
  194. return walk->test_walk(start, end, walk);
  195. /*
  196. * vma(VM_PFNMAP) doesn't have any valid struct pages behind VM_PFNMAP
  197. * range, so we don't walk over it as we do for normal vmas. However,
  198. * Some callers are interested in handling hole range and they don't
  199. * want to just ignore any single address range. Such users certainly
  200. * define their ->pte_hole() callbacks, so let's delegate them to handle
  201. * vma(VM_PFNMAP).
  202. */
  203. if (vma->vm_flags & VM_PFNMAP) {
  204. int err = 1;
  205. if (walk->pte_hole)
  206. err = walk->pte_hole(start, end, walk);
  207. return err ? err : 1;
  208. }
  209. return 0;
  210. }
  211. static int __walk_page_range(unsigned long start, unsigned long end,
  212. struct mm_walk *walk)
  213. {
  214. int err = 0;
  215. struct vm_area_struct *vma = walk->vma;
  216. if (vma && is_vm_hugetlb_page(vma)) {
  217. if (walk->hugetlb_entry)
  218. err = walk_hugetlb_range(start, end, walk);
  219. } else
  220. err = walk_pgd_range(start, end, walk);
  221. return err;
  222. }
  223. /**
  224. * walk_page_range - walk page table with caller specific callbacks
  225. * @start: start address of the virtual address range
  226. * @end: end address of the virtual address range
  227. * @walk: mm_walk structure defining the callbacks and the target address space
  228. *
  229. * Recursively walk the page table tree of the process represented by @walk->mm
  230. * within the virtual address range [@start, @end). During walking, we can do
  231. * some caller-specific works for each entry, by setting up pmd_entry(),
  232. * pte_entry(), and/or hugetlb_entry(). If you don't set up for some of these
  233. * callbacks, the associated entries/pages are just ignored.
  234. * The return values of these callbacks are commonly defined like below:
  235. *
  236. * - 0 : succeeded to handle the current entry, and if you don't reach the
  237. * end address yet, continue to walk.
  238. * - >0 : succeeded to handle the current entry, and return to the caller
  239. * with caller specific value.
  240. * - <0 : failed to handle the current entry, and return to the caller
  241. * with error code.
  242. *
  243. * Before starting to walk page table, some callers want to check whether
  244. * they really want to walk over the current vma, typically by checking
  245. * its vm_flags. walk_page_test() and @walk->test_walk() are used for this
  246. * purpose.
  247. *
  248. * struct mm_walk keeps current values of some common data like vma and pmd,
  249. * which are useful for the access from callbacks. If you want to pass some
  250. * caller-specific data to callbacks, @walk->private should be helpful.
  251. *
  252. * Locking:
  253. * Callers of walk_page_range() and walk_page_vma() should hold
  254. * @walk->mm->mmap_sem, because these function traverse vma list and/or
  255. * access to vma's data.
  256. */
  257. int walk_page_range(unsigned long start, unsigned long end,
  258. struct mm_walk *walk)
  259. {
  260. int err = 0;
  261. unsigned long next;
  262. struct vm_area_struct *vma;
  263. if (start >= end)
  264. return -EINVAL;
  265. if (!walk->mm)
  266. return -EINVAL;
  267. VM_BUG_ON_MM(!rwsem_is_locked(&walk->mm->mmap_sem), walk->mm);
  268. vma = find_vma(walk->mm, start);
  269. do {
  270. if (!vma) { /* after the last vma */
  271. walk->vma = NULL;
  272. next = end;
  273. } else if (start < vma->vm_start) { /* outside vma */
  274. walk->vma = NULL;
  275. next = min(end, vma->vm_start);
  276. } else { /* inside vma */
  277. walk->vma = vma;
  278. next = min(end, vma->vm_end);
  279. vma = vma->vm_next;
  280. err = walk_page_test(start, next, walk);
  281. if (err > 0) {
  282. /*
  283. * positive return values are purely for
  284. * controlling the pagewalk, so should never
  285. * be passed to the callers.
  286. */
  287. err = 0;
  288. continue;
  289. }
  290. if (err < 0)
  291. break;
  292. }
  293. if (walk->vma || walk->pte_hole)
  294. err = __walk_page_range(start, next, walk);
  295. if (err)
  296. break;
  297. } while (start = next, start < end);
  298. return err;
  299. }
  300. int walk_page_vma(struct vm_area_struct *vma, struct mm_walk *walk)
  301. {
  302. int err;
  303. if (!walk->mm)
  304. return -EINVAL;
  305. VM_BUG_ON(!rwsem_is_locked(&walk->mm->mmap_sem));
  306. VM_BUG_ON(!vma);
  307. walk->vma = vma;
  308. err = walk_page_test(vma->vm_start, vma->vm_end, walk);
  309. if (err > 0)
  310. return 0;
  311. if (err < 0)
  312. return err;
  313. return __walk_page_range(vma->vm_start, vma->vm_end, walk);
  314. }