mincore.c 7.8 KB

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