mincore.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 void mincore_hugetlb_page_range(struct vm_area_struct *vma,
  20. unsigned long addr, unsigned long end,
  21. unsigned char *vec)
  22. {
  23. #ifdef CONFIG_HUGETLB_PAGE
  24. struct hstate *h;
  25. h = hstate_vma(vma);
  26. while (1) {
  27. unsigned char present;
  28. pte_t *ptep;
  29. /*
  30. * Huge pages are always in RAM for now, but
  31. * theoretically it needs to be checked.
  32. */
  33. ptep = huge_pte_offset(current->mm,
  34. addr & huge_page_mask(h));
  35. present = ptep && !huge_pte_none(huge_ptep_get(ptep));
  36. while (1) {
  37. *vec = present;
  38. vec++;
  39. addr += PAGE_SIZE;
  40. if (addr == end)
  41. return;
  42. /* check hugepage border */
  43. if (!(addr & ~huge_page_mask(h)))
  44. break;
  45. }
  46. }
  47. #else
  48. BUG();
  49. #endif
  50. }
  51. /*
  52. * Later we can get more picky about what "in core" means precisely.
  53. * For now, simply check to see if the page is in the page cache,
  54. * and is up to date; i.e. that no page-in operation would be required
  55. * at this time if an application were to map and access this page.
  56. */
  57. static unsigned char mincore_page(struct address_space *mapping, pgoff_t pgoff)
  58. {
  59. unsigned char present = 0;
  60. struct page *page;
  61. /*
  62. * When tmpfs swaps out a page from a file, any process mapping that
  63. * file will not get a swp_entry_t in its pte, but rather it is like
  64. * any other file mapping (ie. marked !present and faulted in with
  65. * tmpfs's .fault). So swapped out tmpfs mappings are tested here.
  66. */
  67. #ifdef CONFIG_SWAP
  68. if (shmem_mapping(mapping)) {
  69. page = find_get_entry(mapping, pgoff);
  70. /*
  71. * shmem/tmpfs may return swap: account for swapcache
  72. * page too.
  73. */
  74. if (radix_tree_exceptional_entry(page)) {
  75. swp_entry_t swp = radix_to_swp_entry(page);
  76. page = find_get_page(swap_address_space(swp), swp.val);
  77. }
  78. } else
  79. page = find_get_page(mapping, pgoff);
  80. #else
  81. page = find_get_page(mapping, pgoff);
  82. #endif
  83. if (page) {
  84. present = PageUptodate(page);
  85. page_cache_release(page);
  86. }
  87. return present;
  88. }
  89. static void mincore_unmapped_range(struct vm_area_struct *vma,
  90. unsigned long addr, unsigned long end,
  91. unsigned char *vec)
  92. {
  93. unsigned long nr = (end - addr) >> PAGE_SHIFT;
  94. int i;
  95. if (vma->vm_file) {
  96. pgoff_t pgoff;
  97. pgoff = linear_page_index(vma, addr);
  98. for (i = 0; i < nr; i++, pgoff++)
  99. vec[i] = mincore_page(vma->vm_file->f_mapping, pgoff);
  100. } else {
  101. for (i = 0; i < nr; i++)
  102. vec[i] = 0;
  103. }
  104. }
  105. static void mincore_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  106. unsigned long addr, unsigned long end,
  107. unsigned char *vec)
  108. {
  109. unsigned long next;
  110. spinlock_t *ptl;
  111. pte_t *ptep;
  112. ptep = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  113. do {
  114. pte_t pte = *ptep;
  115. pgoff_t pgoff;
  116. next = addr + PAGE_SIZE;
  117. if (pte_none(pte))
  118. mincore_unmapped_range(vma, addr, next, vec);
  119. else if (pte_present(pte))
  120. *vec = 1;
  121. else if (pte_file(pte)) {
  122. pgoff = pte_to_pgoff(pte);
  123. *vec = mincore_page(vma->vm_file->f_mapping, pgoff);
  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. pgoff = entry.val;
  135. *vec = mincore_page(swap_address_space(entry),
  136. pgoff);
  137. #else
  138. WARN_ON(1);
  139. *vec = 1;
  140. #endif
  141. }
  142. }
  143. vec++;
  144. } while (ptep++, addr = next, addr != end);
  145. pte_unmap_unlock(ptep - 1, ptl);
  146. }
  147. static void mincore_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  148. unsigned long addr, unsigned long end,
  149. unsigned char *vec)
  150. {
  151. unsigned long next;
  152. pmd_t *pmd;
  153. pmd = pmd_offset(pud, addr);
  154. do {
  155. next = pmd_addr_end(addr, end);
  156. if (pmd_trans_huge(*pmd)) {
  157. if (mincore_huge_pmd(vma, pmd, addr, next, vec)) {
  158. vec += (next - addr) >> PAGE_SHIFT;
  159. continue;
  160. }
  161. /* fall through */
  162. }
  163. if (pmd_none_or_trans_huge_or_clear_bad(pmd))
  164. mincore_unmapped_range(vma, addr, next, vec);
  165. else
  166. mincore_pte_range(vma, pmd, addr, next, vec);
  167. vec += (next - addr) >> PAGE_SHIFT;
  168. } while (pmd++, addr = next, addr != end);
  169. }
  170. static void mincore_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  171. unsigned long addr, unsigned long end,
  172. unsigned char *vec)
  173. {
  174. unsigned long next;
  175. pud_t *pud;
  176. pud = pud_offset(pgd, addr);
  177. do {
  178. next = pud_addr_end(addr, end);
  179. if (pud_none_or_clear_bad(pud))
  180. mincore_unmapped_range(vma, addr, next, vec);
  181. else
  182. mincore_pmd_range(vma, pud, addr, next, vec);
  183. vec += (next - addr) >> PAGE_SHIFT;
  184. } while (pud++, addr = next, addr != end);
  185. }
  186. static void mincore_page_range(struct vm_area_struct *vma,
  187. unsigned long addr, unsigned long end,
  188. unsigned char *vec)
  189. {
  190. unsigned long next;
  191. pgd_t *pgd;
  192. pgd = pgd_offset(vma->vm_mm, addr);
  193. do {
  194. next = pgd_addr_end(addr, end);
  195. if (pgd_none_or_clear_bad(pgd))
  196. mincore_unmapped_range(vma, addr, next, vec);
  197. else
  198. mincore_pud_range(vma, pgd, addr, next, vec);
  199. vec += (next - addr) >> PAGE_SHIFT;
  200. } while (pgd++, addr = next, addr != end);
  201. }
  202. /*
  203. * Do a chunk of "sys_mincore()". We've already checked
  204. * all the arguments, we hold the mmap semaphore: we should
  205. * just return the amount of info we're asked for.
  206. */
  207. static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *vec)
  208. {
  209. struct vm_area_struct *vma;
  210. unsigned long end;
  211. vma = find_vma(current->mm, addr);
  212. if (!vma || addr < vma->vm_start)
  213. return -ENOMEM;
  214. end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
  215. if (is_vm_hugetlb_page(vma))
  216. mincore_hugetlb_page_range(vma, addr, end, vec);
  217. else
  218. mincore_page_range(vma, addr, end, vec);
  219. return (end - addr) >> PAGE_SHIFT;
  220. }
  221. /*
  222. * The mincore(2) system call.
  223. *
  224. * mincore() returns the memory residency status of the pages in the
  225. * current process's address space specified by [addr, addr + len).
  226. * The status is returned in a vector of bytes. The least significant
  227. * bit of each byte is 1 if the referenced page is in memory, otherwise
  228. * it is zero.
  229. *
  230. * Because the status of a page can change after mincore() checks it
  231. * but before it returns to the application, the returned vector may
  232. * contain stale information. Only locked pages are guaranteed to
  233. * remain in memory.
  234. *
  235. * return values:
  236. * zero - success
  237. * -EFAULT - vec points to an illegal address
  238. * -EINVAL - addr is not a multiple of PAGE_CACHE_SIZE
  239. * -ENOMEM - Addresses in the range [addr, addr + len] are
  240. * invalid for the address space of this process, or
  241. * specify one or more pages which are not currently
  242. * mapped
  243. * -EAGAIN - A kernel resource was temporarily unavailable.
  244. */
  245. SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
  246. unsigned char __user *, vec)
  247. {
  248. long retval;
  249. unsigned long pages;
  250. unsigned char *tmp;
  251. /* Check the start address: needs to be page-aligned.. */
  252. if (start & ~PAGE_CACHE_MASK)
  253. return -EINVAL;
  254. /* ..and we need to be passed a valid user-space range */
  255. if (!access_ok(VERIFY_READ, (void __user *) start, len))
  256. return -ENOMEM;
  257. /* This also avoids any overflows on PAGE_CACHE_ALIGN */
  258. pages = len >> PAGE_SHIFT;
  259. pages += (len & ~PAGE_MASK) != 0;
  260. if (!access_ok(VERIFY_WRITE, vec, pages))
  261. return -EFAULT;
  262. tmp = (void *) __get_free_page(GFP_USER);
  263. if (!tmp)
  264. return -EAGAIN;
  265. retval = 0;
  266. while (pages) {
  267. /*
  268. * Do at most PAGE_SIZE entries per iteration, due to
  269. * the temporary buffer size.
  270. */
  271. down_read(&current->mm->mmap_sem);
  272. retval = do_mincore(start, min(pages, PAGE_SIZE), tmp);
  273. up_read(&current->mm->mmap_sem);
  274. if (retval <= 0)
  275. break;
  276. if (copy_to_user(vec, tmp, retval)) {
  277. retval = -EFAULT;
  278. break;
  279. }
  280. pages -= retval;
  281. vec += retval;
  282. start += retval << PAGE_SHIFT;
  283. retval = 0;
  284. }
  285. free_page((unsigned long) tmp);
  286. return retval;
  287. }