mincore.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if (nr > pages)
  73. nr = pages;
  74. pgd = pgd_offset(vma->vm_mm, addr);
  75. if (pgd_none_or_clear_bad(pgd))
  76. goto none_mapped;
  77. pud = pud_offset(pgd, addr);
  78. if (pud_none_or_clear_bad(pud))
  79. goto none_mapped;
  80. pmd = pmd_offset(pud, addr);
  81. if (pmd_none_or_clear_bad(pmd))
  82. goto none_mapped;
  83. ptep = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  84. for (i = 0; i < nr; i++, ptep++, addr += PAGE_SIZE) {
  85. unsigned char present;
  86. pte_t pte = *ptep;
  87. if (pte_present(pte)) {
  88. present = 1;
  89. } else if (pte_none(pte)) {
  90. if (vma->vm_file) {
  91. pgoff = linear_page_index(vma, addr);
  92. present = mincore_page(vma->vm_file->f_mapping,
  93. pgoff);
  94. } else
  95. present = 0;
  96. } else if (pte_file(pte)) {
  97. pgoff = pte_to_pgoff(pte);
  98. present = mincore_page(vma->vm_file->f_mapping, pgoff);
  99. } else { /* pte is a swap entry */
  100. swp_entry_t entry = pte_to_swp_entry(pte);
  101. if (is_migration_entry(entry)) {
  102. /* migration entries are always uptodate */
  103. present = 1;
  104. } else {
  105. pgoff = entry.val;
  106. present = mincore_page(&swapper_space, pgoff);
  107. }
  108. }
  109. }
  110. pte_unmap_unlock(ptep-1, ptl);
  111. return nr;
  112. none_mapped:
  113. if (vma->vm_file) {
  114. pgoff = linear_page_index(vma, addr);
  115. for (i = 0; i < nr; i++, pgoff++)
  116. vec[i] = mincore_page(vma->vm_file->f_mapping, pgoff);
  117. }
  118. return nr;
  119. }
  120. /*
  121. * The mincore(2) system call.
  122. *
  123. * mincore() returns the memory residency status of the pages in the
  124. * current process's address space specified by [addr, addr + len).
  125. * The status is returned in a vector of bytes. The least significant
  126. * bit of each byte is 1 if the referenced page is in memory, otherwise
  127. * it is zero.
  128. *
  129. * Because the status of a page can change after mincore() checks it
  130. * but before it returns to the application, the returned vector may
  131. * contain stale information. Only locked pages are guaranteed to
  132. * remain in memory.
  133. *
  134. * return values:
  135. * zero - success
  136. * -EFAULT - vec points to an illegal address
  137. * -EINVAL - addr is not a multiple of PAGE_CACHE_SIZE
  138. * -ENOMEM - Addresses in the range [addr, addr + len] are
  139. * invalid for the address space of this process, or
  140. * specify one or more pages which are not currently
  141. * mapped
  142. * -EAGAIN - A kernel resource was temporarily unavailable.
  143. */
  144. asmlinkage long sys_mincore(unsigned long start, size_t len,
  145. unsigned char __user * vec)
  146. {
  147. long retval;
  148. unsigned long pages;
  149. unsigned char *tmp;
  150. /* Check the start address: needs to be page-aligned.. */
  151. if (start & ~PAGE_CACHE_MASK)
  152. return -EINVAL;
  153. /* ..and we need to be passed a valid user-space range */
  154. if (!access_ok(VERIFY_READ, (void __user *) start, len))
  155. return -ENOMEM;
  156. /* This also avoids any overflows on PAGE_CACHE_ALIGN */
  157. pages = len >> PAGE_SHIFT;
  158. pages += (len & ~PAGE_MASK) != 0;
  159. if (!access_ok(VERIFY_WRITE, vec, pages))
  160. return -EFAULT;
  161. tmp = (void *) __get_free_page(GFP_USER);
  162. if (!tmp)
  163. return -EAGAIN;
  164. retval = 0;
  165. while (pages) {
  166. /*
  167. * Do at most PAGE_SIZE entries per iteration, due to
  168. * the temporary buffer size.
  169. */
  170. down_read(&current->mm->mmap_sem);
  171. retval = do_mincore(start, tmp, min(pages, PAGE_SIZE));
  172. up_read(&current->mm->mmap_sem);
  173. if (retval <= 0)
  174. break;
  175. if (copy_to_user(vec, tmp, retval)) {
  176. retval = -EFAULT;
  177. break;
  178. }
  179. pages -= retval;
  180. vec += retval;
  181. start += retval << PAGE_SHIFT;
  182. retval = 0;
  183. }
  184. free_page((unsigned long) tmp);
  185. return retval;
  186. }