mincore.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/mm.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/uaccess.h>
  20. #include <asm/pgtable.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 pgoff)
  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. #ifdef CONFIG_SWAP
  57. if (shmem_mapping(mapping)) {
  58. page = find_get_entry(mapping, pgoff);
  59. /*
  60. * shmem/tmpfs may return swap: account for swapcache
  61. * page too.
  62. */
  63. if (radix_tree_exceptional_entry(page)) {
  64. swp_entry_t swp = radix_to_swp_entry(page);
  65. page = find_get_page(swap_address_space(swp),
  66. swp_offset(swp));
  67. }
  68. } else
  69. page = find_get_page(mapping, pgoff);
  70. #else
  71. page = find_get_page(mapping, pgoff);
  72. #endif
  73. if (page) {
  74. present = PageUptodate(page);
  75. put_page(page);
  76. }
  77. return present;
  78. }
  79. static int __mincore_unmapped_range(unsigned long addr, unsigned long end,
  80. struct vm_area_struct *vma, unsigned char *vec)
  81. {
  82. unsigned long nr = (end - addr) >> PAGE_SHIFT;
  83. int i;
  84. if (vma->vm_file) {
  85. pgoff_t pgoff;
  86. pgoff = linear_page_index(vma, addr);
  87. for (i = 0; i < nr; i++, pgoff++)
  88. vec[i] = mincore_page(vma->vm_file->f_mapping, pgoff);
  89. } else {
  90. for (i = 0; i < nr; i++)
  91. vec[i] = 0;
  92. }
  93. return nr;
  94. }
  95. static int mincore_unmapped_range(unsigned long addr, unsigned long end,
  96. struct mm_walk *walk)
  97. {
  98. walk->private += __mincore_unmapped_range(addr, end,
  99. walk->vma, walk->private);
  100. return 0;
  101. }
  102. static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
  103. struct mm_walk *walk)
  104. {
  105. spinlock_t *ptl;
  106. struct vm_area_struct *vma = walk->vma;
  107. pte_t *ptep;
  108. unsigned char *vec = walk->private;
  109. int nr = (end - addr) >> PAGE_SHIFT;
  110. ptl = pmd_trans_huge_lock(pmd, vma);
  111. if (ptl) {
  112. memset(vec, 1, nr);
  113. spin_unlock(ptl);
  114. goto out;
  115. }
  116. if (pmd_trans_unstable(pmd)) {
  117. __mincore_unmapped_range(addr, end, vma, vec);
  118. goto out;
  119. }
  120. ptep = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
  121. for (; addr != end; ptep++, addr += PAGE_SIZE) {
  122. pte_t pte = *ptep;
  123. if (pte_none(pte))
  124. __mincore_unmapped_range(addr, addr + PAGE_SIZE,
  125. vma, vec);
  126. else if (pte_present(pte))
  127. *vec = 1;
  128. else { /* pte is a swap entry */
  129. swp_entry_t entry = pte_to_swp_entry(pte);
  130. if (non_swap_entry(entry)) {
  131. /*
  132. * migration or hwpoison entries are always
  133. * uptodate
  134. */
  135. *vec = 1;
  136. } else {
  137. #ifdef CONFIG_SWAP
  138. *vec = mincore_page(swap_address_space(entry),
  139. swp_offset(entry));
  140. #else
  141. WARN_ON(1);
  142. *vec = 1;
  143. #endif
  144. }
  145. }
  146. vec++;
  147. }
  148. pte_unmap_unlock(ptep - 1, ptl);
  149. out:
  150. walk->private += nr;
  151. cond_resched();
  152. return 0;
  153. }
  154. static inline bool can_do_mincore(struct vm_area_struct *vma)
  155. {
  156. if (vma_is_anonymous(vma))
  157. return true;
  158. if (!vma->vm_file)
  159. return false;
  160. /*
  161. * Reveal pagecache information only for non-anonymous mappings that
  162. * correspond to the files the calling process could (if tried) open
  163. * for writing; otherwise we'd be including shared non-exclusive
  164. * mappings, which opens a side channel.
  165. */
  166. return inode_owner_or_capable(file_inode(vma->vm_file)) ||
  167. inode_permission(file_inode(vma->vm_file), MAY_WRITE) == 0;
  168. }
  169. /*
  170. * Do a chunk of "sys_mincore()". We've already checked
  171. * all the arguments, we hold the mmap semaphore: we should
  172. * just return the amount of info we're asked for.
  173. */
  174. static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *vec)
  175. {
  176. struct vm_area_struct *vma;
  177. unsigned long end;
  178. int err;
  179. struct mm_walk mincore_walk = {
  180. .pmd_entry = mincore_pte_range,
  181. .pte_hole = mincore_unmapped_range,
  182. .hugetlb_entry = mincore_hugetlb,
  183. .private = vec,
  184. };
  185. vma = find_vma(current->mm, addr);
  186. if (!vma || addr < vma->vm_start)
  187. return -ENOMEM;
  188. end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
  189. if (!can_do_mincore(vma)) {
  190. unsigned long pages = DIV_ROUND_UP(end - addr, PAGE_SIZE);
  191. memset(vec, 1, pages);
  192. return pages;
  193. }
  194. mincore_walk.mm = vma->vm_mm;
  195. err = walk_page_range(addr, end, &mincore_walk);
  196. if (err < 0)
  197. return err;
  198. return (end - addr) >> PAGE_SHIFT;
  199. }
  200. /*
  201. * The mincore(2) system call.
  202. *
  203. * mincore() returns the memory residency status of the pages in the
  204. * current process's address space specified by [addr, addr + len).
  205. * The status is returned in a vector of bytes. The least significant
  206. * bit of each byte is 1 if the referenced page is in memory, otherwise
  207. * it is zero.
  208. *
  209. * Because the status of a page can change after mincore() checks it
  210. * but before it returns to the application, the returned vector may
  211. * contain stale information. Only locked pages are guaranteed to
  212. * remain in memory.
  213. *
  214. * return values:
  215. * zero - success
  216. * -EFAULT - vec points to an illegal address
  217. * -EINVAL - addr is not a multiple of PAGE_SIZE
  218. * -ENOMEM - Addresses in the range [addr, addr + len] are
  219. * invalid for the address space of this process, or
  220. * specify one or more pages which are not currently
  221. * mapped
  222. * -EAGAIN - A kernel resource was temporarily unavailable.
  223. */
  224. SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
  225. unsigned char __user *, vec)
  226. {
  227. long retval;
  228. unsigned long pages;
  229. unsigned char *tmp;
  230. /* Check the start address: needs to be page-aligned.. */
  231. if (start & ~PAGE_MASK)
  232. return -EINVAL;
  233. /* ..and we need to be passed a valid user-space range */
  234. if (!access_ok(VERIFY_READ, (void __user *) start, len))
  235. return -ENOMEM;
  236. /* This also avoids any overflows on PAGE_ALIGN */
  237. pages = len >> PAGE_SHIFT;
  238. pages += (offset_in_page(len)) != 0;
  239. if (!access_ok(VERIFY_WRITE, vec, pages))
  240. return -EFAULT;
  241. tmp = (void *) __get_free_page(GFP_USER);
  242. if (!tmp)
  243. return -EAGAIN;
  244. retval = 0;
  245. while (pages) {
  246. /*
  247. * Do at most PAGE_SIZE entries per iteration, due to
  248. * the temporary buffer size.
  249. */
  250. down_read(&current->mm->mmap_sem);
  251. retval = do_mincore(start, min(pages, PAGE_SIZE), tmp);
  252. up_read(&current->mm->mmap_sem);
  253. if (retval <= 0)
  254. break;
  255. if (copy_to_user(vec, tmp, retval)) {
  256. retval = -EFAULT;
  257. break;
  258. }
  259. pages -= retval;
  260. vec += retval;
  261. start += retval << PAGE_SHIFT;
  262. retval = 0;
  263. }
  264. free_page((unsigned long) tmp);
  265. return retval;
  266. }