fremap.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * linux/mm/fremap.c
  3. *
  4. * Explicit pagetable population and nonlinear (random) mappings support.
  5. *
  6. * started by Ingo Molnar, Copyright (C) 2002, 2003
  7. */
  8. #include <linux/backing-dev.h>
  9. #include <linux/mm.h>
  10. #include <linux/swap.h>
  11. #include <linux/file.h>
  12. #include <linux/mman.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/swapops.h>
  15. #include <linux/rmap.h>
  16. #include <linux/module.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/mmu_notifier.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/tlbflush.h>
  22. #include "internal.h"
  23. static void zap_pte(struct mm_struct *mm, struct vm_area_struct *vma,
  24. unsigned long addr, pte_t *ptep)
  25. {
  26. pte_t pte = *ptep;
  27. if (pte_present(pte)) {
  28. struct page *page;
  29. flush_cache_page(vma, addr, pte_pfn(pte));
  30. pte = ptep_clear_flush(vma, addr, ptep);
  31. page = vm_normal_page(vma, addr, pte);
  32. if (page) {
  33. if (pte_dirty(pte))
  34. set_page_dirty(page);
  35. page_remove_rmap(page);
  36. page_cache_release(page);
  37. update_hiwater_rss(mm);
  38. dec_mm_counter(mm, file_rss);
  39. }
  40. } else {
  41. if (!pte_file(pte))
  42. free_swap_and_cache(pte_to_swp_entry(pte));
  43. pte_clear_not_present_full(mm, addr, ptep, 0);
  44. }
  45. }
  46. /*
  47. * Install a file pte to a given virtual memory address, release any
  48. * previously existing mapping.
  49. */
  50. static int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
  51. unsigned long addr, unsigned long pgoff, pgprot_t prot)
  52. {
  53. int err = -ENOMEM;
  54. pte_t *pte;
  55. spinlock_t *ptl;
  56. pte = get_locked_pte(mm, addr, &ptl);
  57. if (!pte)
  58. goto out;
  59. if (!pte_none(*pte))
  60. zap_pte(mm, vma, addr, pte);
  61. set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
  62. /*
  63. * We don't need to run update_mmu_cache() here because the "file pte"
  64. * being installed by install_file_pte() is not a real pte - it's a
  65. * non-present entry (like a swap entry), noting what file offset should
  66. * be mapped there when there's a fault (in a non-linear vma where
  67. * that's not obvious).
  68. */
  69. pte_unmap_unlock(pte, ptl);
  70. err = 0;
  71. out:
  72. return err;
  73. }
  74. static int populate_range(struct mm_struct *mm, struct vm_area_struct *vma,
  75. unsigned long addr, unsigned long size, pgoff_t pgoff)
  76. {
  77. int err;
  78. do {
  79. err = install_file_pte(mm, vma, addr, pgoff, vma->vm_page_prot);
  80. if (err)
  81. return err;
  82. size -= PAGE_SIZE;
  83. addr += PAGE_SIZE;
  84. pgoff++;
  85. } while (size);
  86. return 0;
  87. }
  88. /**
  89. * sys_remap_file_pages - remap arbitrary pages of an existing VM_SHARED vma
  90. * @start: start of the remapped virtual memory range
  91. * @size: size of the remapped virtual memory range
  92. * @prot: new protection bits of the range (see NOTE)
  93. * @pgoff: to-be-mapped page of the backing store file
  94. * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
  95. *
  96. * sys_remap_file_pages remaps arbitrary pages of an existing VM_SHARED vma
  97. * (shared backing store file).
  98. *
  99. * This syscall works purely via pagetables, so it's the most efficient
  100. * way to map the same (large) file into a given virtual window. Unlike
  101. * mmap()/mremap() it does not create any new vmas. The new mappings are
  102. * also safe across swapout.
  103. *
  104. * NOTE: the @prot parameter right now is ignored (but must be zero),
  105. * and the vma's default protection is used. Arbitrary protections
  106. * might be implemented in the future.
  107. */
  108. SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
  109. unsigned long, prot, unsigned long, pgoff, unsigned long, flags)
  110. {
  111. struct mm_struct *mm = current->mm;
  112. struct address_space *mapping;
  113. unsigned long end = start + size;
  114. struct vm_area_struct *vma;
  115. int err = -EINVAL;
  116. int has_write_lock = 0;
  117. if (prot)
  118. return err;
  119. /*
  120. * Sanitize the syscall parameters:
  121. */
  122. start = start & PAGE_MASK;
  123. size = size & PAGE_MASK;
  124. /* Does the address range wrap, or is the span zero-sized? */
  125. if (start + size <= start)
  126. return err;
  127. /* Can we represent this offset inside this architecture's pte's? */
  128. #if PTE_FILE_MAX_BITS < BITS_PER_LONG
  129. if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
  130. return err;
  131. #endif
  132. /* We need down_write() to change vma->vm_flags. */
  133. down_read(&mm->mmap_sem);
  134. retry:
  135. vma = find_vma(mm, start);
  136. /*
  137. * Make sure the vma is shared, that it supports prefaulting,
  138. * and that the remapped range is valid and fully within
  139. * the single existing vma. vm_private_data is used as a
  140. * swapout cursor in a VM_NONLINEAR vma.
  141. */
  142. if (!vma || !(vma->vm_flags & VM_SHARED))
  143. goto out;
  144. if (vma->vm_private_data && !(vma->vm_flags & VM_NONLINEAR))
  145. goto out;
  146. if (!(vma->vm_flags & VM_CAN_NONLINEAR))
  147. goto out;
  148. if (end <= start || start < vma->vm_start || end > vma->vm_end)
  149. goto out;
  150. /* Must set VM_NONLINEAR before any pages are populated. */
  151. if (!(vma->vm_flags & VM_NONLINEAR)) {
  152. /* Don't need a nonlinear mapping, exit success */
  153. if (pgoff == linear_page_index(vma, start)) {
  154. err = 0;
  155. goto out;
  156. }
  157. if (!has_write_lock) {
  158. up_read(&mm->mmap_sem);
  159. down_write(&mm->mmap_sem);
  160. has_write_lock = 1;
  161. goto retry;
  162. }
  163. mapping = vma->vm_file->f_mapping;
  164. /*
  165. * page_mkclean doesn't work on nonlinear vmas, so if
  166. * dirty pages need to be accounted, emulate with linear
  167. * vmas.
  168. */
  169. if (mapping_cap_account_dirty(mapping)) {
  170. unsigned long addr;
  171. struct file *file = vma->vm_file;
  172. flags &= MAP_NONBLOCK;
  173. get_file(file);
  174. addr = mmap_region(file, start, size,
  175. flags, vma->vm_flags, pgoff);
  176. fput(file);
  177. if (IS_ERR_VALUE(addr)) {
  178. err = addr;
  179. } else {
  180. BUG_ON(addr != start);
  181. err = 0;
  182. }
  183. goto out;
  184. }
  185. spin_lock(&mapping->i_mmap_lock);
  186. flush_dcache_mmap_lock(mapping);
  187. vma->vm_flags |= VM_NONLINEAR;
  188. vma_prio_tree_remove(vma, &mapping->i_mmap);
  189. vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
  190. flush_dcache_mmap_unlock(mapping);
  191. spin_unlock(&mapping->i_mmap_lock);
  192. }
  193. if (vma->vm_flags & VM_LOCKED) {
  194. /*
  195. * drop PG_Mlocked flag for over-mapped range
  196. */
  197. unsigned int saved_flags = vma->vm_flags;
  198. munlock_vma_pages_range(vma, start, start + size);
  199. vma->vm_flags = saved_flags;
  200. }
  201. mmu_notifier_invalidate_range_start(mm, start, start + size);
  202. err = populate_range(mm, vma, start, size, pgoff);
  203. mmu_notifier_invalidate_range_end(mm, start, start + size);
  204. if (!err && !(flags & MAP_NONBLOCK)) {
  205. if (vma->vm_flags & VM_LOCKED) {
  206. /*
  207. * might be mapping previously unmapped range of file
  208. */
  209. mlock_vma_pages_range(vma, start, start + size);
  210. } else {
  211. if (unlikely(has_write_lock)) {
  212. downgrade_write(&mm->mmap_sem);
  213. has_write_lock = 0;
  214. }
  215. make_pages_present(start, start+size);
  216. }
  217. }
  218. /*
  219. * We can't clear VM_NONLINEAR because we'd have to do
  220. * it after ->populate completes, and that would prevent
  221. * downgrading the lock. (Locks can't be upgraded).
  222. */
  223. out:
  224. if (likely(!has_write_lock))
  225. up_read(&mm->mmap_sem);
  226. else
  227. up_write(&mm->mmap_sem);
  228. return err;
  229. }