mincore.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_none_or_clear_bad(pmd))
  142. mincore_unmapped_range(vma, addr, next, vec);
  143. else
  144. mincore_pte_range(vma, pmd, addr, next, vec);
  145. vec += (next - addr) >> PAGE_SHIFT;
  146. } while (pmd++, addr = next, addr != end);
  147. }
  148. static void mincore_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  149. unsigned long addr, unsigned long end,
  150. unsigned char *vec)
  151. {
  152. unsigned long next;
  153. pud_t *pud;
  154. pud = pud_offset(pgd, addr);
  155. do {
  156. next = pud_addr_end(addr, end);
  157. if (pud_none_or_clear_bad(pud))
  158. mincore_unmapped_range(vma, addr, next, vec);
  159. else
  160. mincore_pmd_range(vma, pud, addr, next, vec);
  161. vec += (next - addr) >> PAGE_SHIFT;
  162. } while (pud++, addr = next, addr != end);
  163. }
  164. static void mincore_page_range(struct vm_area_struct *vma,
  165. unsigned long addr, unsigned long end,
  166. unsigned char *vec)
  167. {
  168. unsigned long next;
  169. pgd_t *pgd;
  170. pgd = pgd_offset(vma->vm_mm, addr);
  171. do {
  172. next = pgd_addr_end(addr, end);
  173. if (pgd_none_or_clear_bad(pgd))
  174. mincore_unmapped_range(vma, addr, next, vec);
  175. else
  176. mincore_pud_range(vma, pgd, addr, next, vec);
  177. vec += (next - addr) >> PAGE_SHIFT;
  178. } while (pgd++, addr = next, addr != end);
  179. }
  180. /*
  181. * Do a chunk of "sys_mincore()". We've already checked
  182. * all the arguments, we hold the mmap semaphore: we should
  183. * just return the amount of info we're asked for.
  184. */
  185. static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *vec)
  186. {
  187. struct vm_area_struct *vma;
  188. unsigned long end;
  189. vma = find_vma(current->mm, addr);
  190. if (!vma || addr < vma->vm_start)
  191. return -ENOMEM;
  192. end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
  193. if (is_vm_hugetlb_page(vma)) {
  194. mincore_hugetlb_page_range(vma, addr, end, vec);
  195. return (end - addr) >> PAGE_SHIFT;
  196. }
  197. end = pmd_addr_end(addr, end);
  198. if (is_vm_hugetlb_page(vma))
  199. mincore_hugetlb_page_range(vma, addr, end, vec);
  200. else
  201. mincore_page_range(vma, addr, end, vec);
  202. return (end - addr) >> PAGE_SHIFT;
  203. }
  204. /*
  205. * The mincore(2) system call.
  206. *
  207. * mincore() returns the memory residency status of the pages in the
  208. * current process's address space specified by [addr, addr + len).
  209. * The status is returned in a vector of bytes. The least significant
  210. * bit of each byte is 1 if the referenced page is in memory, otherwise
  211. * it is zero.
  212. *
  213. * Because the status of a page can change after mincore() checks it
  214. * but before it returns to the application, the returned vector may
  215. * contain stale information. Only locked pages are guaranteed to
  216. * remain in memory.
  217. *
  218. * return values:
  219. * zero - success
  220. * -EFAULT - vec points to an illegal address
  221. * -EINVAL - addr is not a multiple of PAGE_CACHE_SIZE
  222. * -ENOMEM - Addresses in the range [addr, addr + len] are
  223. * invalid for the address space of this process, or
  224. * specify one or more pages which are not currently
  225. * mapped
  226. * -EAGAIN - A kernel resource was temporarily unavailable.
  227. */
  228. SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
  229. unsigned char __user *, vec)
  230. {
  231. long retval;
  232. unsigned long pages;
  233. unsigned char *tmp;
  234. /* Check the start address: needs to be page-aligned.. */
  235. if (start & ~PAGE_CACHE_MASK)
  236. return -EINVAL;
  237. /* ..and we need to be passed a valid user-space range */
  238. if (!access_ok(VERIFY_READ, (void __user *) start, len))
  239. return -ENOMEM;
  240. /* This also avoids any overflows on PAGE_CACHE_ALIGN */
  241. pages = len >> PAGE_SHIFT;
  242. pages += (len & ~PAGE_MASK) != 0;
  243. if (!access_ok(VERIFY_WRITE, vec, pages))
  244. return -EFAULT;
  245. tmp = (void *) __get_free_page(GFP_USER);
  246. if (!tmp)
  247. return -EAGAIN;
  248. retval = 0;
  249. while (pages) {
  250. /*
  251. * Do at most PAGE_SIZE entries per iteration, due to
  252. * the temporary buffer size.
  253. */
  254. down_read(&current->mm->mmap_sem);
  255. retval = do_mincore(start, min(pages, PAGE_SIZE), tmp);
  256. up_read(&current->mm->mmap_sem);
  257. if (retval <= 0)
  258. break;
  259. if (copy_to_user(vec, tmp, retval)) {
  260. retval = -EFAULT;
  261. break;
  262. }
  263. pages -= retval;
  264. vec += retval;
  265. start += retval << PAGE_SHIFT;
  266. retval = 0;
  267. }
  268. free_page((unsigned long) tmp);
  269. return retval;
  270. }