mincore.c 7.0 KB

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