mincore.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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(swap_address_space(swap), 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(swap_address_space(entry),
  125. pgoff);
  126. #else
  127. WARN_ON(1);
  128. *vec = 1;
  129. #endif
  130. }
  131. }
  132. vec++;
  133. } while (ptep++, addr = next, addr != end);
  134. pte_unmap_unlock(ptep - 1, ptl);
  135. }
  136. static void mincore_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  137. unsigned long addr, unsigned long end,
  138. unsigned char *vec)
  139. {
  140. unsigned long next;
  141. pmd_t *pmd;
  142. pmd = pmd_offset(pud, addr);
  143. do {
  144. next = pmd_addr_end(addr, end);
  145. if (pmd_trans_huge(*pmd)) {
  146. if (mincore_huge_pmd(vma, pmd, addr, next, vec)) {
  147. vec += (next - addr) >> PAGE_SHIFT;
  148. continue;
  149. }
  150. /* fall through */
  151. }
  152. if (pmd_none_or_trans_huge_or_clear_bad(pmd))
  153. mincore_unmapped_range(vma, addr, next, vec);
  154. else
  155. mincore_pte_range(vma, pmd, addr, next, vec);
  156. vec += (next - addr) >> PAGE_SHIFT;
  157. } while (pmd++, addr = next, addr != end);
  158. }
  159. static void mincore_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  160. unsigned long addr, unsigned long end,
  161. unsigned char *vec)
  162. {
  163. unsigned long next;
  164. pud_t *pud;
  165. pud = pud_offset(pgd, addr);
  166. do {
  167. next = pud_addr_end(addr, end);
  168. if (pud_none_or_clear_bad(pud))
  169. mincore_unmapped_range(vma, addr, next, vec);
  170. else
  171. mincore_pmd_range(vma, pud, addr, next, vec);
  172. vec += (next - addr) >> PAGE_SHIFT;
  173. } while (pud++, addr = next, addr != end);
  174. }
  175. static void mincore_page_range(struct vm_area_struct *vma,
  176. unsigned long addr, unsigned long end,
  177. unsigned char *vec)
  178. {
  179. unsigned long next;
  180. pgd_t *pgd;
  181. pgd = pgd_offset(vma->vm_mm, addr);
  182. do {
  183. next = pgd_addr_end(addr, end);
  184. if (pgd_none_or_clear_bad(pgd))
  185. mincore_unmapped_range(vma, addr, next, vec);
  186. else
  187. mincore_pud_range(vma, pgd, addr, next, vec);
  188. vec += (next - addr) >> PAGE_SHIFT;
  189. } while (pgd++, addr = next, addr != end);
  190. }
  191. static inline bool can_do_mincore(struct vm_area_struct *vma)
  192. {
  193. if (vma_is_anonymous(vma))
  194. return true;
  195. if (!vma->vm_file)
  196. return false;
  197. /*
  198. * Reveal pagecache information only for non-anonymous mappings that
  199. * correspond to the files the calling process could (if tried) open
  200. * for writing; otherwise we'd be including shared non-exclusive
  201. * mappings, which opens a side channel.
  202. */
  203. return inode_owner_or_capable(vma->vm_file->f_path.dentry->d_inode) ||
  204. inode_permission(vma->vm_file->f_path.dentry->d_inode, MAY_WRITE) == 0;
  205. }
  206. /*
  207. * Do a chunk of "sys_mincore()". We've already checked
  208. * all the arguments, we hold the mmap semaphore: we should
  209. * just return the amount of info we're asked for.
  210. */
  211. static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *vec)
  212. {
  213. struct vm_area_struct *vma;
  214. unsigned long end;
  215. vma = find_vma(current->mm, addr);
  216. if (!vma || addr < vma->vm_start)
  217. return -ENOMEM;
  218. end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
  219. if (!can_do_mincore(vma)) {
  220. unsigned long pages = DIV_ROUND_UP(end - addr, PAGE_SIZE);
  221. memset(vec, 1, pages);
  222. return pages;
  223. }
  224. if (is_vm_hugetlb_page(vma)) {
  225. mincore_hugetlb_page_range(vma, addr, end, vec);
  226. return (end - addr) >> PAGE_SHIFT;
  227. }
  228. end = pmd_addr_end(addr, end);
  229. if (is_vm_hugetlb_page(vma))
  230. mincore_hugetlb_page_range(vma, addr, end, vec);
  231. else
  232. mincore_page_range(vma, addr, end, vec);
  233. return (end - addr) >> PAGE_SHIFT;
  234. }
  235. /*
  236. * The mincore(2) system call.
  237. *
  238. * mincore() returns the memory residency status of the pages in the
  239. * current process's address space specified by [addr, addr + len).
  240. * The status is returned in a vector of bytes. The least significant
  241. * bit of each byte is 1 if the referenced page is in memory, otherwise
  242. * it is zero.
  243. *
  244. * Because the status of a page can change after mincore() checks it
  245. * but before it returns to the application, the returned vector may
  246. * contain stale information. Only locked pages are guaranteed to
  247. * remain in memory.
  248. *
  249. * return values:
  250. * zero - success
  251. * -EFAULT - vec points to an illegal address
  252. * -EINVAL - addr is not a multiple of PAGE_CACHE_SIZE
  253. * -ENOMEM - Addresses in the range [addr, addr + len] are
  254. * invalid for the address space of this process, or
  255. * specify one or more pages which are not currently
  256. * mapped
  257. * -EAGAIN - A kernel resource was temporarily unavailable.
  258. */
  259. SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
  260. unsigned char __user *, vec)
  261. {
  262. long retval;
  263. unsigned long pages;
  264. unsigned char *tmp;
  265. /* Check the start address: needs to be page-aligned.. */
  266. if (start & ~PAGE_CACHE_MASK)
  267. return -EINVAL;
  268. /* ..and we need to be passed a valid user-space range */
  269. if (!access_ok(VERIFY_READ, (void __user *) start, len))
  270. return -ENOMEM;
  271. /* This also avoids any overflows on PAGE_CACHE_ALIGN */
  272. pages = len >> PAGE_SHIFT;
  273. pages += (len & ~PAGE_MASK) != 0;
  274. if (!access_ok(VERIFY_WRITE, vec, pages))
  275. return -EFAULT;
  276. tmp = (void *) __get_free_page(GFP_USER);
  277. if (!tmp)
  278. return -EAGAIN;
  279. retval = 0;
  280. while (pages) {
  281. /*
  282. * Do at most PAGE_SIZE entries per iteration, due to
  283. * the temporary buffer size.
  284. */
  285. down_read(&current->mm->mmap_sem);
  286. retval = do_mincore(start, min(pages, PAGE_SIZE), tmp);
  287. up_read(&current->mm->mmap_sem);
  288. if (retval <= 0)
  289. break;
  290. if (copy_to_user(vec, tmp, retval)) {
  291. retval = -EFAULT;
  292. break;
  293. }
  294. pages -= retval;
  295. vec += retval;
  296. start += retval << PAGE_SHIFT;
  297. retval = 0;
  298. }
  299. free_page((unsigned long) tmp);
  300. return retval;
  301. }