mincore.c 6.7 KB

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