mincore.c 6.6 KB

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