mincore.c 7.3 KB

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