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/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. /*
  153. * Do a chunk of "sys_mincore()". We've already checked
  154. * all the arguments, we hold the mmap semaphore: we should
  155. * just return the amount of info we're asked for.
  156. */
  157. static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *vec)
  158. {
  159. struct vm_area_struct *vma;
  160. unsigned long end;
  161. int err;
  162. struct mm_walk mincore_walk = {
  163. .pmd_entry = mincore_pte_range,
  164. .pte_hole = mincore_unmapped_range,
  165. .hugetlb_entry = mincore_hugetlb,
  166. .private = vec,
  167. };
  168. vma = find_vma(current->mm, addr);
  169. if (!vma || addr < vma->vm_start)
  170. return -ENOMEM;
  171. mincore_walk.mm = vma->vm_mm;
  172. end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
  173. err = walk_page_range(addr, end, &mincore_walk);
  174. if (err < 0)
  175. return err;
  176. return (end - addr) >> PAGE_SHIFT;
  177. }
  178. /*
  179. * The mincore(2) system call.
  180. *
  181. * mincore() returns the memory residency status of the pages in the
  182. * current process's address space specified by [addr, addr + len).
  183. * The status is returned in a vector of bytes. The least significant
  184. * bit of each byte is 1 if the referenced page is in memory, otherwise
  185. * it is zero.
  186. *
  187. * Because the status of a page can change after mincore() checks it
  188. * but before it returns to the application, the returned vector may
  189. * contain stale information. Only locked pages are guaranteed to
  190. * remain in memory.
  191. *
  192. * return values:
  193. * zero - success
  194. * -EFAULT - vec points to an illegal address
  195. * -EINVAL - addr is not a multiple of PAGE_SIZE
  196. * -ENOMEM - Addresses in the range [addr, addr + len] are
  197. * invalid for the address space of this process, or
  198. * specify one or more pages which are not currently
  199. * mapped
  200. * -EAGAIN - A kernel resource was temporarily unavailable.
  201. */
  202. SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
  203. unsigned char __user *, vec)
  204. {
  205. long retval;
  206. unsigned long pages;
  207. unsigned char *tmp;
  208. /* Check the start address: needs to be page-aligned.. */
  209. if (start & ~PAGE_MASK)
  210. return -EINVAL;
  211. /* ..and we need to be passed a valid user-space range */
  212. if (!access_ok(VERIFY_READ, (void __user *) start, len))
  213. return -ENOMEM;
  214. /* This also avoids any overflows on PAGE_ALIGN */
  215. pages = len >> PAGE_SHIFT;
  216. pages += (offset_in_page(len)) != 0;
  217. if (!access_ok(VERIFY_WRITE, vec, pages))
  218. return -EFAULT;
  219. tmp = (void *) __get_free_page(GFP_USER);
  220. if (!tmp)
  221. return -EAGAIN;
  222. retval = 0;
  223. while (pages) {
  224. /*
  225. * Do at most PAGE_SIZE entries per iteration, due to
  226. * the temporary buffer size.
  227. */
  228. down_read(&current->mm->mmap_sem);
  229. retval = do_mincore(start, min(pages, PAGE_SIZE), tmp);
  230. up_read(&current->mm->mmap_sem);
  231. if (retval <= 0)
  232. break;
  233. if (copy_to_user(vec, tmp, retval)) {
  234. retval = -EFAULT;
  235. break;
  236. }
  237. pages -= retval;
  238. vec += retval;
  239. start += retval << PAGE_SHIFT;
  240. retval = 0;
  241. }
  242. free_page((unsigned long) tmp);
  243. return retval;
  244. }