mincore.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/swapops.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. static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr,
  23. unsigned long end, struct mm_walk *walk)
  24. {
  25. #ifdef CONFIG_HUGETLB_PAGE
  26. unsigned char present;
  27. unsigned char *vec = walk->private;
  28. /*
  29. * Hugepages under user process are always in RAM and never
  30. * swapped out, but theoretically it needs to be checked.
  31. */
  32. present = pte && !huge_pte_none_mostly(huge_ptep_get(pte));
  33. for (; addr != end; vec++, addr += PAGE_SIZE)
  34. *vec = present;
  35. walk->private = vec;
  36. #else
  37. BUG();
  38. #endif
  39. return 0;
  40. }
  41. /*
  42. * Later we can get more picky about what "in core" means precisely.
  43. * For now, simply check to see if the page is in the page cache,
  44. * and is up to date; i.e. that no page-in operation would be required
  45. * at this time if an application were to map and access this page.
  46. */
  47. static unsigned char mincore_page(struct address_space *mapping, pgoff_t index)
  48. {
  49. unsigned char present = 0;
  50. struct page *page;
  51. /*
  52. * When tmpfs swaps out a page from a file, any process mapping that
  53. * file will not get a swp_entry_t in its pte, but rather it is like
  54. * any other file mapping (ie. marked !present and faulted in with
  55. * tmpfs's .fault). So swapped out tmpfs mappings are tested here.
  56. */
  57. page = find_get_incore_page(mapping, index);
  58. if (page) {
  59. present = PageUptodate(page);
  60. put_page(page);
  61. }
  62. return present;
  63. }
  64. static int __mincore_unmapped_range(unsigned long addr, unsigned long end,
  65. struct vm_area_struct *vma, unsigned char *vec)
  66. {
  67. unsigned long nr = (end - addr) >> PAGE_SHIFT;
  68. int i;
  69. if (vma->vm_file) {
  70. pgoff_t pgoff;
  71. pgoff = linear_page_index(vma, addr);
  72. for (i = 0; i < nr; i++, pgoff++)
  73. vec[i] = mincore_page(vma->vm_file->f_mapping, pgoff);
  74. } else {
  75. for (i = 0; i < nr; i++)
  76. vec[i] = 0;
  77. }
  78. return nr;
  79. }
  80. static int mincore_unmapped_range(unsigned long addr, unsigned long end,
  81. __always_unused int depth,
  82. struct mm_walk *walk)
  83. {
  84. walk->private += __mincore_unmapped_range(addr, end,
  85. walk->vma, walk->private);
  86. return 0;
  87. }
  88. static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
  89. struct mm_walk *walk)
  90. {
  91. spinlock_t *ptl;
  92. struct vm_area_struct *vma = walk->vma;
  93. pte_t *ptep;
  94. unsigned char *vec = walk->private;
  95. int nr = (end - addr) >> PAGE_SHIFT;
  96. ptl = pmd_trans_huge_lock(pmd, vma);
  97. if (ptl) {
  98. memset(vec, 1, nr);
  99. spin_unlock(ptl);
  100. goto out;
  101. }
  102. if (pmd_trans_unstable(pmd)) {
  103. __mincore_unmapped_range(addr, end, vma, vec);
  104. goto out;
  105. }
  106. ptep = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
  107. for (; addr != end; ptep++, addr += PAGE_SIZE) {
  108. pte_t pte = *ptep;
  109. /* We need to do cache lookup too for pte markers */
  110. if (pte_none_mostly(pte))
  111. __mincore_unmapped_range(addr, addr + PAGE_SIZE,
  112. vma, vec);
  113. else if (pte_present(pte))
  114. *vec = 1;
  115. else { /* pte is a swap entry */
  116. swp_entry_t entry = pte_to_swp_entry(pte);
  117. if (non_swap_entry(entry)) {
  118. /*
  119. * migration or hwpoison entries are always
  120. * uptodate
  121. */
  122. *vec = 1;
  123. } else {
  124. #ifdef CONFIG_SWAP
  125. *vec = mincore_page(swap_address_space(entry),
  126. swp_offset(entry));
  127. #else
  128. WARN_ON(1);
  129. *vec = 1;
  130. #endif
  131. }
  132. }
  133. vec++;
  134. }
  135. pte_unmap_unlock(ptep - 1, ptl);
  136. out:
  137. walk->private += nr;
  138. cond_resched();
  139. return 0;
  140. }
  141. static inline bool can_do_mincore(struct vm_area_struct *vma)
  142. {
  143. if (vma_is_anonymous(vma))
  144. return true;
  145. if (!vma->vm_file)
  146. return false;
  147. /*
  148. * Reveal pagecache information only for non-anonymous mappings that
  149. * correspond to the files the calling process could (if tried) open
  150. * for writing; otherwise we'd be including shared non-exclusive
  151. * mappings, which opens a side channel.
  152. */
  153. return inode_owner_or_capable(&init_user_ns,
  154. file_inode(vma->vm_file)) ||
  155. file_permission(vma->vm_file, MAY_WRITE) == 0;
  156. }
  157. static const struct mm_walk_ops mincore_walk_ops = {
  158. .pmd_entry = mincore_pte_range,
  159. .pte_hole = mincore_unmapped_range,
  160. .hugetlb_entry = mincore_hugetlb,
  161. .walk_lock = PGWALK_RDLOCK,
  162. };
  163. /*
  164. * Do a chunk of "sys_mincore()". We've already checked
  165. * all the arguments, we hold the mmap semaphore: we should
  166. * just return the amount of info we're asked for.
  167. */
  168. static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *vec)
  169. {
  170. struct vm_area_struct *vma;
  171. unsigned long end;
  172. int err;
  173. vma = find_vma(current->mm, addr);
  174. if (!vma || addr < vma->vm_start)
  175. return -ENOMEM;
  176. end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
  177. if (!can_do_mincore(vma)) {
  178. unsigned long pages = DIV_ROUND_UP(end - addr, PAGE_SIZE);
  179. memset(vec, 1, pages);
  180. return pages;
  181. }
  182. err = walk_page_range(vma->vm_mm, addr, end, &mincore_walk_ops, vec);
  183. if (err < 0)
  184. return err;
  185. return (end - addr) >> PAGE_SHIFT;
  186. }
  187. /*
  188. * The mincore(2) system call.
  189. *
  190. * mincore() returns the memory residency status of the pages in the
  191. * current process's address space specified by [addr, addr + len).
  192. * The status is returned in a vector of bytes. The least significant
  193. * bit of each byte is 1 if the referenced page is in memory, otherwise
  194. * it is zero.
  195. *
  196. * Because the status of a page can change after mincore() checks it
  197. * but before it returns to the application, the returned vector may
  198. * contain stale information. Only locked pages are guaranteed to
  199. * remain in memory.
  200. *
  201. * return values:
  202. * zero - success
  203. * -EFAULT - vec points to an illegal address
  204. * -EINVAL - addr is not a multiple of PAGE_SIZE
  205. * -ENOMEM - Addresses in the range [addr, addr + len] are
  206. * invalid for the address space of this process, or
  207. * specify one or more pages which are not currently
  208. * mapped
  209. * -EAGAIN - A kernel resource was temporarily unavailable.
  210. */
  211. SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
  212. unsigned char __user *, vec)
  213. {
  214. long retval;
  215. unsigned long pages;
  216. unsigned char *tmp;
  217. start = untagged_addr(start);
  218. /* Check the start address: needs to be page-aligned.. */
  219. if (start & ~PAGE_MASK)
  220. return -EINVAL;
  221. /* ..and we need to be passed a valid user-space range */
  222. if (!access_ok((void __user *) start, len))
  223. return -ENOMEM;
  224. /* This also avoids any overflows on PAGE_ALIGN */
  225. pages = len >> PAGE_SHIFT;
  226. pages += (offset_in_page(len)) != 0;
  227. if (!access_ok(vec, pages))
  228. return -EFAULT;
  229. tmp = (void *) __get_free_page(GFP_USER);
  230. if (!tmp)
  231. return -EAGAIN;
  232. retval = 0;
  233. while (pages) {
  234. /*
  235. * Do at most PAGE_SIZE entries per iteration, due to
  236. * the temporary buffer size.
  237. */
  238. mmap_read_lock(current->mm);
  239. retval = do_mincore(start, min(pages, PAGE_SIZE), tmp);
  240. mmap_read_unlock(current->mm);
  241. if (retval <= 0)
  242. break;
  243. if (copy_to_user(vec, tmp, retval)) {
  244. retval = -EFAULT;
  245. break;
  246. }
  247. pages -= retval;
  248. vec += retval;
  249. start += retval << PAGE_SHIFT;
  250. retval = 0;
  251. }
  252. free_page((unsigned long) tmp);
  253. return retval;
  254. }