mincore.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. * However when tmpfs moves the page from pagecache and into swapcache,
  68. * it is still in core, but the find_get_page below won't find it.
  69. * No big deal, but make a note of it.
  70. */
  71. page = find_get_page(mapping, pgoff);
  72. if (page) {
  73. present = PageUptodate(page);
  74. page_cache_release(page);
  75. }
  76. return present;
  77. }
  78. static void mincore_unmapped_range(struct vm_area_struct *vma,
  79. unsigned long addr, unsigned long end,
  80. unsigned char *vec)
  81. {
  82. unsigned long nr = (end - addr) >> PAGE_SHIFT;
  83. int i;
  84. if (vma->vm_file) {
  85. pgoff_t pgoff;
  86. pgoff = linear_page_index(vma, addr);
  87. for (i = 0; i < nr; i++, pgoff++)
  88. vec[i] = mincore_page(vma->vm_file->f_mapping, pgoff);
  89. } else {
  90. for (i = 0; i < nr; i++)
  91. vec[i] = 0;
  92. }
  93. }
  94. static void mincore_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  95. unsigned long addr, unsigned long end,
  96. unsigned char *vec)
  97. {
  98. unsigned long next;
  99. spinlock_t *ptl;
  100. pte_t *ptep;
  101. ptep = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  102. do {
  103. pte_t pte = *ptep;
  104. pgoff_t pgoff;
  105. next = addr + PAGE_SIZE;
  106. if (pte_none(pte))
  107. mincore_unmapped_range(vma, addr, next, vec);
  108. else if (pte_present(pte))
  109. *vec = 1;
  110. else if (pte_file(pte)) {
  111. pgoff = pte_to_pgoff(pte);
  112. *vec = mincore_page(vma->vm_file->f_mapping, pgoff);
  113. } else { /* pte is a swap entry */
  114. swp_entry_t entry = pte_to_swp_entry(pte);
  115. if (is_migration_entry(entry)) {
  116. /* migration entries are always uptodate */
  117. *vec = 1;
  118. } else {
  119. #ifdef CONFIG_SWAP
  120. pgoff = entry.val;
  121. *vec = mincore_page(&swapper_space, pgoff);
  122. #else
  123. WARN_ON(1);
  124. *vec = 1;
  125. #endif
  126. }
  127. }
  128. vec++;
  129. } while (ptep++, addr = next, addr != end);
  130. pte_unmap_unlock(ptep - 1, ptl);
  131. }
  132. static void mincore_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  133. unsigned long addr, unsigned long end,
  134. unsigned char *vec)
  135. {
  136. unsigned long next;
  137. pmd_t *pmd;
  138. pmd = pmd_offset(pud, addr);
  139. do {
  140. next = pmd_addr_end(addr, end);
  141. if (pmd_trans_huge(*pmd)) {
  142. if (mincore_huge_pmd(vma, pmd, addr, next, vec)) {
  143. vec += (next - addr) >> PAGE_SHIFT;
  144. continue;
  145. }
  146. /* fall through */
  147. }
  148. if (pmd_none_or_trans_huge_or_clear_bad(pmd))
  149. mincore_unmapped_range(vma, addr, next, vec);
  150. else
  151. mincore_pte_range(vma, pmd, addr, next, vec);
  152. vec += (next - addr) >> PAGE_SHIFT;
  153. } while (pmd++, addr = next, addr != end);
  154. }
  155. static void mincore_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  156. unsigned long addr, unsigned long end,
  157. unsigned char *vec)
  158. {
  159. unsigned long next;
  160. pud_t *pud;
  161. pud = pud_offset(pgd, addr);
  162. do {
  163. next = pud_addr_end(addr, end);
  164. if (pud_none_or_clear_bad(pud))
  165. mincore_unmapped_range(vma, addr, next, vec);
  166. else
  167. mincore_pmd_range(vma, pud, addr, next, vec);
  168. vec += (next - addr) >> PAGE_SHIFT;
  169. } while (pud++, addr = next, addr != end);
  170. }
  171. static void mincore_page_range(struct vm_area_struct *vma,
  172. unsigned long addr, unsigned long end,
  173. unsigned char *vec)
  174. {
  175. unsigned long next;
  176. pgd_t *pgd;
  177. pgd = pgd_offset(vma->vm_mm, addr);
  178. do {
  179. next = pgd_addr_end(addr, end);
  180. if (pgd_none_or_clear_bad(pgd))
  181. mincore_unmapped_range(vma, addr, next, vec);
  182. else
  183. mincore_pud_range(vma, pgd, addr, next, vec);
  184. vec += (next - addr) >> PAGE_SHIFT;
  185. } while (pgd++, addr = next, addr != end);
  186. }
  187. /*
  188. * Do a chunk of "sys_mincore()". We've already checked
  189. * all the arguments, we hold the mmap semaphore: we should
  190. * just return the amount of info we're asked for.
  191. */
  192. static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *vec)
  193. {
  194. struct vm_area_struct *vma;
  195. unsigned long end;
  196. vma = find_vma(current->mm, addr);
  197. if (!vma || addr < vma->vm_start)
  198. return -ENOMEM;
  199. end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
  200. if (is_vm_hugetlb_page(vma)) {
  201. mincore_hugetlb_page_range(vma, addr, end, vec);
  202. return (end - addr) >> PAGE_SHIFT;
  203. }
  204. end = pmd_addr_end(addr, end);
  205. if (is_vm_hugetlb_page(vma))
  206. mincore_hugetlb_page_range(vma, addr, end, vec);
  207. else
  208. mincore_page_range(vma, addr, end, vec);
  209. return (end - addr) >> PAGE_SHIFT;
  210. }
  211. /*
  212. * The mincore(2) system call.
  213. *
  214. * mincore() returns the memory residency status of the pages in the
  215. * current process's address space specified by [addr, addr + len).
  216. * The status is returned in a vector of bytes. The least significant
  217. * bit of each byte is 1 if the referenced page is in memory, otherwise
  218. * it is zero.
  219. *
  220. * Because the status of a page can change after mincore() checks it
  221. * but before it returns to the application, the returned vector may
  222. * contain stale information. Only locked pages are guaranteed to
  223. * remain in memory.
  224. *
  225. * return values:
  226. * zero - success
  227. * -EFAULT - vec points to an illegal address
  228. * -EINVAL - addr is not a multiple of PAGE_CACHE_SIZE
  229. * -ENOMEM - Addresses in the range [addr, addr + len] are
  230. * invalid for the address space of this process, or
  231. * specify one or more pages which are not currently
  232. * mapped
  233. * -EAGAIN - A kernel resource was temporarily unavailable.
  234. */
  235. SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
  236. unsigned char __user *, vec)
  237. {
  238. long retval;
  239. unsigned long pages;
  240. unsigned char *tmp;
  241. /* Check the start address: needs to be page-aligned.. */
  242. if (start & ~PAGE_CACHE_MASK)
  243. return -EINVAL;
  244. /* ..and we need to be passed a valid user-space range */
  245. if (!access_ok(VERIFY_READ, (void __user *) start, len))
  246. return -ENOMEM;
  247. /* This also avoids any overflows on PAGE_CACHE_ALIGN */
  248. pages = len >> PAGE_SHIFT;
  249. pages += (len & ~PAGE_MASK) != 0;
  250. if (!access_ok(VERIFY_WRITE, vec, pages))
  251. return -EFAULT;
  252. tmp = (void *) __get_free_page(GFP_USER);
  253. if (!tmp)
  254. return -EAGAIN;
  255. retval = 0;
  256. while (pages) {
  257. /*
  258. * Do at most PAGE_SIZE entries per iteration, due to
  259. * the temporary buffer size.
  260. */
  261. down_read(&current->mm->mmap_sem);
  262. retval = do_mincore(start, min(pages, PAGE_SIZE), tmp);
  263. up_read(&current->mm->mmap_sem);
  264. if (retval <= 0)
  265. break;
  266. if (copy_to_user(vec, tmp, retval)) {
  267. retval = -EFAULT;
  268. break;
  269. }
  270. pages -= retval;
  271. vec += retval;
  272. start += retval << PAGE_SHIFT;
  273. retval = 0;
  274. }
  275. free_page((unsigned long) tmp);
  276. return retval;
  277. }