mincore.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/slab.h>
  10. #include <linux/pagemap.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 <asm/uaccess.h>
  17. #include <asm/pgtable.h>
  18. /*
  19. * Later we can get more picky about what "in core" means precisely.
  20. * For now, simply check to see if the page is in the page cache,
  21. * and is up to date; i.e. that no page-in operation would be required
  22. * at this time if an application were to map and access this page.
  23. */
  24. static unsigned char mincore_page(struct address_space *mapping, pgoff_t pgoff)
  25. {
  26. unsigned char present = 0;
  27. struct page *page;
  28. /*
  29. * When tmpfs swaps out a page from a file, any process mapping that
  30. * file will not get a swp_entry_t in its pte, but rather it is like
  31. * any other file mapping (ie. marked !present and faulted in with
  32. * tmpfs's .nopage). So swapped out tmpfs mappings are tested here.
  33. *
  34. * However when tmpfs moves the page from pagecache and into swapcache,
  35. * it is still in core, but the find_get_page below won't find it.
  36. * No big deal, but make a note of it.
  37. */
  38. page = find_get_page(mapping, pgoff);
  39. if (page) {
  40. present = PageUptodate(page);
  41. page_cache_release(page);
  42. }
  43. return present;
  44. }
  45. /*
  46. * Do a chunk of "sys_mincore()". We've already checked
  47. * all the arguments, we hold the mmap semaphore: we should
  48. * just return the amount of info we're asked for.
  49. */
  50. static long do_mincore(unsigned long addr, unsigned char *vec, unsigned long pages)
  51. {
  52. pgd_t *pgd;
  53. pud_t *pud;
  54. pmd_t *pmd;
  55. pte_t *ptep;
  56. spinlock_t *ptl;
  57. unsigned long nr;
  58. int i;
  59. pgoff_t pgoff;
  60. struct vm_area_struct *vma = find_vma(current->mm, addr);
  61. /*
  62. * find_vma() didn't find anything above us, or we're
  63. * in an unmapped hole in the address space: ENOMEM.
  64. */
  65. if (!vma || addr < vma->vm_start)
  66. return -ENOMEM;
  67. /*
  68. * Calculate how many pages there are left in the last level of the
  69. * PTE array for our address.
  70. */
  71. nr = PTRS_PER_PTE - ((addr >> PAGE_SHIFT) & (PTRS_PER_PTE-1));
  72. /*
  73. * Don't overrun this vma
  74. */
  75. nr = min(nr, (vma->vm_end - addr) >> PAGE_SHIFT);
  76. /*
  77. * Don't return more than the caller asked for
  78. */
  79. nr = min(nr, pages);
  80. pgd = pgd_offset(vma->vm_mm, addr);
  81. if (pgd_none_or_clear_bad(pgd))
  82. goto none_mapped;
  83. pud = pud_offset(pgd, addr);
  84. if (pud_none_or_clear_bad(pud))
  85. goto none_mapped;
  86. pmd = pmd_offset(pud, addr);
  87. if (pmd_none_or_clear_bad(pmd))
  88. goto none_mapped;
  89. ptep = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  90. for (i = 0; i < nr; i++, ptep++, addr += PAGE_SIZE) {
  91. unsigned char present;
  92. pte_t pte = *ptep;
  93. if (pte_present(pte)) {
  94. present = 1;
  95. } else if (pte_none(pte)) {
  96. if (vma->vm_file) {
  97. pgoff = linear_page_index(vma, addr);
  98. present = mincore_page(vma->vm_file->f_mapping,
  99. pgoff);
  100. } else
  101. present = 0;
  102. } else if (pte_file(pte)) {
  103. pgoff = pte_to_pgoff(pte);
  104. present = mincore_page(vma->vm_file->f_mapping, pgoff);
  105. } else { /* pte is a swap entry */
  106. swp_entry_t entry = pte_to_swp_entry(pte);
  107. if (is_migration_entry(entry)) {
  108. /* migration entries are always uptodate */
  109. present = 1;
  110. } else {
  111. #ifdef CONFIG_SWAP
  112. pgoff = entry.val;
  113. present = mincore_page(&swapper_space, pgoff);
  114. #else
  115. WARN_ON(1);
  116. present = 1;
  117. #endif
  118. }
  119. }
  120. vec[i] = present;
  121. }
  122. pte_unmap_unlock(ptep-1, ptl);
  123. return nr;
  124. none_mapped:
  125. if (vma->vm_file) {
  126. pgoff = linear_page_index(vma, addr);
  127. for (i = 0; i < nr; i++, pgoff++)
  128. vec[i] = mincore_page(vma->vm_file->f_mapping, pgoff);
  129. } else {
  130. for (i = 0; i < nr; i++)
  131. vec[i] = 0;
  132. }
  133. return nr;
  134. }
  135. /*
  136. * The mincore(2) system call.
  137. *
  138. * mincore() returns the memory residency status of the pages in the
  139. * current process's address space specified by [addr, addr + len).
  140. * The status is returned in a vector of bytes. The least significant
  141. * bit of each byte is 1 if the referenced page is in memory, otherwise
  142. * it is zero.
  143. *
  144. * Because the status of a page can change after mincore() checks it
  145. * but before it returns to the application, the returned vector may
  146. * contain stale information. Only locked pages are guaranteed to
  147. * remain in memory.
  148. *
  149. * return values:
  150. * zero - success
  151. * -EFAULT - vec points to an illegal address
  152. * -EINVAL - addr is not a multiple of PAGE_CACHE_SIZE
  153. * -ENOMEM - Addresses in the range [addr, addr + len] are
  154. * invalid for the address space of this process, or
  155. * specify one or more pages which are not currently
  156. * mapped
  157. * -EAGAIN - A kernel resource was temporarily unavailable.
  158. */
  159. asmlinkage long sys_mincore(unsigned long start, size_t len,
  160. unsigned char __user * vec)
  161. {
  162. long retval;
  163. unsigned long pages;
  164. unsigned char *tmp;
  165. /* Check the start address: needs to be page-aligned.. */
  166. if (start & ~PAGE_CACHE_MASK)
  167. return -EINVAL;
  168. /* ..and we need to be passed a valid user-space range */
  169. if (!access_ok(VERIFY_READ, (void __user *) start, len))
  170. return -ENOMEM;
  171. /* This also avoids any overflows on PAGE_CACHE_ALIGN */
  172. pages = len >> PAGE_SHIFT;
  173. pages += (len & ~PAGE_MASK) != 0;
  174. if (!access_ok(VERIFY_WRITE, vec, pages))
  175. return -EFAULT;
  176. tmp = (void *) __get_free_page(GFP_USER);
  177. if (!tmp)
  178. return -EAGAIN;
  179. retval = 0;
  180. while (pages) {
  181. /*
  182. * Do at most PAGE_SIZE entries per iteration, due to
  183. * the temporary buffer size.
  184. */
  185. down_read(&current->mm->mmap_sem);
  186. retval = do_mincore(start, tmp, min(pages, PAGE_SIZE));
  187. up_read(&current->mm->mmap_sem);
  188. if (retval <= 0)
  189. break;
  190. if (copy_to_user(vec, tmp, retval)) {
  191. retval = -EFAULT;
  192. break;
  193. }
  194. pages -= retval;
  195. vec += retval;
  196. start += retval << PAGE_SHIFT;
  197. retval = 0;
  198. }
  199. free_page((unsigned long) tmp);
  200. return retval;
  201. }