mincore.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/mm/mincore.c
  4. *
  5. * Copyright (C) 1994-2006 Linus Torvalds
  6. */
  7. /*
  8. * The mincore() system call.
  9. */
  10. #include <linux/pagemap.h>
  11. #include <linux/gfp.h>
  12. #include <linux/pagewalk.h>
  13. #include <linux/mman.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/swap.h>
  16. #include <linux/leafops.h>
  17. #include <linux/shmem_fs.h>
  18. #include <linux/hugetlb.h>
  19. #include <linux/pgtable.h>
  20. #include <linux/uaccess.h>
  21. #include "swap.h"
  22. #include "internal.h"
  23. static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr,
  24. unsigned long end, struct mm_walk *walk)
  25. {
  26. #ifdef CONFIG_HUGETLB_PAGE
  27. unsigned char present;
  28. unsigned char *vec = walk->private;
  29. spinlock_t *ptl;
  30. ptl = huge_pte_lock(hstate_vma(walk->vma), walk->mm, pte);
  31. /*
  32. * Hugepages under user process are always in RAM and never
  33. * swapped out, but theoretically it needs to be checked.
  34. */
  35. if (!pte) {
  36. present = 0;
  37. } else {
  38. const pte_t ptep = huge_ptep_get(walk->mm, addr, pte);
  39. if (huge_pte_none(ptep) || pte_is_marker(ptep))
  40. present = 0;
  41. else
  42. present = 1;
  43. }
  44. for (; addr != end; vec++, addr += PAGE_SIZE)
  45. *vec = present;
  46. walk->private = vec;
  47. spin_unlock(ptl);
  48. #else
  49. BUG();
  50. #endif
  51. return 0;
  52. }
  53. static unsigned char mincore_swap(swp_entry_t entry, bool shmem)
  54. {
  55. struct swap_info_struct *si;
  56. struct folio *folio = NULL;
  57. unsigned char present = 0;
  58. if (!IS_ENABLED(CONFIG_SWAP)) {
  59. WARN_ON(1);
  60. return 0;
  61. }
  62. /*
  63. * Shmem mapping may contain swapin error entries, which are
  64. * absent. Page table may contain migration or hwpoison
  65. * entries which are always uptodate.
  66. */
  67. if (!softleaf_is_swap(entry))
  68. return !shmem;
  69. /*
  70. * Shmem mapping lookup is lockless, so we need to grab the swap
  71. * device. mincore page table walk locks the PTL, and the swap
  72. * device is stable, avoid touching the si for better performance.
  73. */
  74. if (shmem) {
  75. si = get_swap_device(entry);
  76. if (!si)
  77. return 0;
  78. }
  79. folio = swap_cache_get_folio(entry);
  80. if (shmem)
  81. put_swap_device(si);
  82. /* The swap cache space contains either folio, shadow or NULL */
  83. if (folio && !xa_is_value(folio)) {
  84. present = folio_test_uptodate(folio);
  85. folio_put(folio);
  86. }
  87. return present;
  88. }
  89. /*
  90. * Later we can get more picky about what "in core" means precisely.
  91. * For now, simply check to see if the page is in the page cache,
  92. * and is up to date; i.e. that no page-in operation would be required
  93. * at this time if an application were to map and access this page.
  94. */
  95. static unsigned char mincore_page(struct address_space *mapping, pgoff_t index)
  96. {
  97. unsigned char present = 0;
  98. struct folio *folio;
  99. /*
  100. * When tmpfs swaps out a page from a file, any process mapping that
  101. * file will not get a swp_entry_t in its pte, but rather it is like
  102. * any other file mapping (ie. marked !present and faulted in with
  103. * tmpfs's .fault). So swapped out tmpfs mappings are tested here.
  104. */
  105. folio = filemap_get_entry(mapping, index);
  106. if (folio) {
  107. if (xa_is_value(folio)) {
  108. if (shmem_mapping(mapping))
  109. return mincore_swap(radix_to_swp_entry(folio),
  110. true);
  111. else
  112. return 0;
  113. }
  114. present = folio_test_uptodate(folio);
  115. folio_put(folio);
  116. }
  117. return present;
  118. }
  119. static int __mincore_unmapped_range(unsigned long addr, unsigned long end,
  120. struct vm_area_struct *vma, unsigned char *vec)
  121. {
  122. unsigned long nr = (end - addr) >> PAGE_SHIFT;
  123. int i;
  124. if (vma->vm_file) {
  125. pgoff_t pgoff;
  126. pgoff = linear_page_index(vma, addr);
  127. for (i = 0; i < nr; i++, pgoff++)
  128. vec[i] = mincore_page(vma->vm_file->f_mapping, pgoff);
  129. } else {
  130. for (i = 0; i < nr; i++)
  131. vec[i] = 0;
  132. }
  133. return nr;
  134. }
  135. static int mincore_unmapped_range(unsigned long addr, unsigned long end,
  136. __always_unused int depth,
  137. struct mm_walk *walk)
  138. {
  139. walk->private += __mincore_unmapped_range(addr, end,
  140. walk->vma, walk->private);
  141. return 0;
  142. }
  143. static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
  144. struct mm_walk *walk)
  145. {
  146. spinlock_t *ptl;
  147. struct vm_area_struct *vma = walk->vma;
  148. pte_t *ptep;
  149. unsigned char *vec = walk->private;
  150. int nr = (end - addr) >> PAGE_SHIFT;
  151. int step, i;
  152. ptl = pmd_trans_huge_lock(pmd, vma);
  153. if (ptl) {
  154. memset(vec, 1, nr);
  155. spin_unlock(ptl);
  156. goto out;
  157. }
  158. ptep = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
  159. if (!ptep) {
  160. walk->action = ACTION_AGAIN;
  161. return 0;
  162. }
  163. for (; addr != end; ptep += step, addr += step * PAGE_SIZE) {
  164. pte_t pte = ptep_get(ptep);
  165. step = 1;
  166. /* We need to do cache lookup too for markers */
  167. if (pte_none(pte) || pte_is_marker(pte))
  168. __mincore_unmapped_range(addr, addr + PAGE_SIZE,
  169. vma, vec);
  170. else if (pte_present(pte)) {
  171. unsigned int batch = pte_batch_hint(ptep, pte);
  172. if (batch > 1) {
  173. unsigned int max_nr = (end - addr) >> PAGE_SHIFT;
  174. step = min_t(unsigned int, batch, max_nr);
  175. }
  176. for (i = 0; i < step; i++)
  177. vec[i] = 1;
  178. } else { /* pte is a swap entry */
  179. const softleaf_t entry = softleaf_from_pte(pte);
  180. *vec = mincore_swap(entry, false);
  181. }
  182. vec += step;
  183. }
  184. pte_unmap_unlock(ptep - 1, ptl);
  185. out:
  186. walk->private += nr;
  187. cond_resched();
  188. return 0;
  189. }
  190. static inline bool can_do_mincore(struct vm_area_struct *vma)
  191. {
  192. if (vma_is_anonymous(vma))
  193. return true;
  194. if (!vma->vm_file)
  195. return false;
  196. /*
  197. * Reveal pagecache information only for non-anonymous mappings that
  198. * correspond to the files the calling process could (if tried) open
  199. * for writing; otherwise we'd be including shared non-exclusive
  200. * mappings, which opens a side channel.
  201. */
  202. return inode_owner_or_capable(&nop_mnt_idmap,
  203. file_inode(vma->vm_file)) ||
  204. file_permission(vma->vm_file, MAY_WRITE) == 0;
  205. }
  206. static const struct mm_walk_ops mincore_walk_ops = {
  207. .pmd_entry = mincore_pte_range,
  208. .pte_hole = mincore_unmapped_range,
  209. .hugetlb_entry = mincore_hugetlb,
  210. .walk_lock = PGWALK_RDLOCK,
  211. };
  212. /*
  213. * Do a chunk of "sys_mincore()". We've already checked
  214. * all the arguments, we hold the mmap semaphore: we should
  215. * just return the amount of info we're asked for.
  216. */
  217. static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *vec)
  218. {
  219. struct vm_area_struct *vma;
  220. unsigned long end;
  221. int err;
  222. vma = vma_lookup(current->mm, addr);
  223. if (!vma)
  224. return -ENOMEM;
  225. end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
  226. if (!can_do_mincore(vma)) {
  227. unsigned long pages = DIV_ROUND_UP(end - addr, PAGE_SIZE);
  228. memset(vec, 1, pages);
  229. return pages;
  230. }
  231. err = walk_page_range(vma->vm_mm, addr, end, &mincore_walk_ops, vec);
  232. if (err < 0)
  233. return err;
  234. return (end - addr) >> PAGE_SHIFT;
  235. }
  236. /*
  237. * The mincore(2) system call.
  238. *
  239. * mincore() returns the memory residency status of the pages in the
  240. * current process's address space specified by [addr, addr + len).
  241. * The status is returned in a vector of bytes. The least significant
  242. * bit of each byte is 1 if the referenced page is in memory, otherwise
  243. * it is zero.
  244. *
  245. * Because the status of a page can change after mincore() checks it
  246. * but before it returns to the application, the returned vector may
  247. * contain stale information. Only locked pages are guaranteed to
  248. * remain in memory.
  249. *
  250. * return values:
  251. * zero - success
  252. * -EFAULT - vec points to an illegal address
  253. * -EINVAL - addr is not a multiple of PAGE_SIZE
  254. * -ENOMEM - Addresses in the range [addr, addr + len] are
  255. * invalid for the address space of this process, or
  256. * specify one or more pages which are not currently
  257. * mapped
  258. * -EAGAIN - A kernel resource was temporarily unavailable.
  259. */
  260. SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
  261. unsigned char __user *, vec)
  262. {
  263. long retval;
  264. unsigned long pages;
  265. unsigned char *tmp;
  266. start = untagged_addr(start);
  267. /* Check the start address: needs to be page-aligned.. */
  268. if (unlikely(start & ~PAGE_MASK))
  269. return -EINVAL;
  270. /* ..and we need to be passed a valid user-space range */
  271. if (!access_ok((void __user *) start, len))
  272. return -ENOMEM;
  273. /* This also avoids any overflows on PAGE_ALIGN */
  274. pages = len >> PAGE_SHIFT;
  275. pages += (offset_in_page(len)) != 0;
  276. if (!access_ok(vec, pages))
  277. return -EFAULT;
  278. tmp = (void *) __get_free_page(GFP_USER);
  279. if (!tmp)
  280. return -EAGAIN;
  281. retval = 0;
  282. while (pages) {
  283. /*
  284. * Do at most PAGE_SIZE entries per iteration, due to
  285. * the temporary buffer size.
  286. */
  287. mmap_read_lock(current->mm);
  288. retval = do_mincore(start, min(pages, PAGE_SIZE), tmp);
  289. mmap_read_unlock(current->mm);
  290. if (retval <= 0)
  291. break;
  292. if (copy_to_user(vec, tmp, retval)) {
  293. retval = -EFAULT;
  294. break;
  295. }
  296. pages -= retval;
  297. vec += retval;
  298. start += retval << PAGE_SHIFT;
  299. retval = 0;
  300. }
  301. free_page((unsigned long) tmp);
  302. return retval;
  303. }