mincore.c 6.7 KB

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