fremap.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/export.h>
  9. #include <linux/backing-dev.h>
  10. #include <linux/mm.h>
  11. #include <linux/swap.h>
  12. #include <linux/file.h>
  13. #include <linux/mman.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/swapops.h>
  16. #include <linux/rmap.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, MM_FILEPAGES);
  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, ptfile;
  55. spinlock_t *ptl;
  56. pte = get_locked_pte(mm, addr, &ptl);
  57. if (!pte)
  58. goto out;
  59. ptfile = pgoff_to_pte(pgoff);
  60. if (!pte_none(*pte)) {
  61. if (pte_present(*pte) && pte_soft_dirty(*pte))
  62. pte_file_mksoft_dirty(ptfile);
  63. zap_pte(mm, vma, addr, pte);
  64. }
  65. set_pte_at(mm, addr, pte, ptfile);
  66. /*
  67. * We don't need to run update_mmu_cache() here because the "file pte"
  68. * being installed by install_file_pte() is not a real pte - it's a
  69. * non-present entry (like a swap entry), noting what file offset should
  70. * be mapped there when there's a fault (in a non-linear vma where
  71. * that's not obvious).
  72. */
  73. pte_unmap_unlock(pte, ptl);
  74. err = 0;
  75. out:
  76. return err;
  77. }
  78. int generic_file_remap_pages(struct vm_area_struct *vma, unsigned long addr,
  79. unsigned long size, pgoff_t pgoff)
  80. {
  81. struct mm_struct *mm = vma->vm_mm;
  82. int err;
  83. do {
  84. err = install_file_pte(mm, vma, addr, pgoff, vma->vm_page_prot);
  85. if (err)
  86. return err;
  87. size -= PAGE_SIZE;
  88. addr += PAGE_SIZE;
  89. pgoff++;
  90. } while (size);
  91. return 0;
  92. }
  93. EXPORT_SYMBOL(generic_file_remap_pages);
  94. /**
  95. * sys_remap_file_pages - remap arbitrary pages of an existing VM_SHARED vma
  96. * @start: start of the remapped virtual memory range
  97. * @size: size of the remapped virtual memory range
  98. * @prot: new protection bits of the range (see NOTE)
  99. * @pgoff: to-be-mapped page of the backing store file
  100. * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
  101. *
  102. * sys_remap_file_pages remaps arbitrary pages of an existing VM_SHARED vma
  103. * (shared backing store file).
  104. *
  105. * This syscall works purely via pagetables, so it's the most efficient
  106. * way to map the same (large) file into a given virtual window. Unlike
  107. * mmap()/mremap() it does not create any new vmas. The new mappings are
  108. * also safe across swapout.
  109. *
  110. * NOTE: the @prot parameter right now is ignored (but must be zero),
  111. * and the vma's default protection is used. Arbitrary protections
  112. * might be implemented in the future.
  113. */
  114. SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
  115. unsigned long, prot, unsigned long, pgoff, unsigned long, flags)
  116. {
  117. struct mm_struct *mm = current->mm;
  118. struct address_space *mapping;
  119. struct vm_area_struct *vma;
  120. int err = -EINVAL;
  121. int has_write_lock = 0;
  122. vm_flags_t vm_flags = 0;
  123. if (prot)
  124. return err;
  125. /*
  126. * Sanitize the syscall parameters:
  127. */
  128. start = start & PAGE_MASK;
  129. size = size & PAGE_MASK;
  130. /* Does the address range wrap, or is the span zero-sized? */
  131. if (start + size <= start)
  132. return err;
  133. /* Does pgoff wrap? */
  134. if (pgoff + (size >> PAGE_SHIFT) < pgoff)
  135. return err;
  136. /* Can we represent this offset inside this architecture's pte's? */
  137. #if PTE_FILE_MAX_BITS < BITS_PER_LONG
  138. if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
  139. return err;
  140. #endif
  141. /* We need down_write() to change vma->vm_flags. */
  142. down_read(&mm->mmap_sem);
  143. retry:
  144. vma = find_vma(mm, start);
  145. /*
  146. * Make sure the vma is shared, that it supports prefaulting,
  147. * and that the remapped range is valid and fully within
  148. * the single existing vma.
  149. */
  150. if (!vma || !(vma->vm_flags & VM_SHARED))
  151. goto out;
  152. if (!vma->vm_ops || !vma->vm_ops->remap_pages)
  153. goto out;
  154. if (start < vma->vm_start || start + size > vma->vm_end)
  155. goto out;
  156. /* Must set VM_NONLINEAR before any pages are populated. */
  157. if (!(vma->vm_flags & VM_NONLINEAR)) {
  158. /*
  159. * vm_private_data is used as a swapout cursor
  160. * in a VM_NONLINEAR vma.
  161. */
  162. if (vma->vm_private_data)
  163. goto out;
  164. /* Don't need a nonlinear mapping, exit success */
  165. if (pgoff == linear_page_index(vma, start)) {
  166. err = 0;
  167. goto out;
  168. }
  169. if (!has_write_lock) {
  170. get_write_lock:
  171. up_read(&mm->mmap_sem);
  172. down_write(&mm->mmap_sem);
  173. has_write_lock = 1;
  174. goto retry;
  175. }
  176. mapping = vma->vm_file->f_mapping;
  177. /*
  178. * page_mkclean doesn't work on nonlinear vmas, so if
  179. * dirty pages need to be accounted, emulate with linear
  180. * vmas.
  181. */
  182. if (mapping_cap_account_dirty(mapping)) {
  183. unsigned long addr;
  184. struct file *file = get_file(vma->vm_file);
  185. addr = mmap_region(file, start, size,
  186. vma->vm_flags, pgoff);
  187. fput(file);
  188. if (IS_ERR_VALUE(addr)) {
  189. err = addr;
  190. } else {
  191. BUG_ON(addr != start);
  192. err = 0;
  193. }
  194. goto out;
  195. }
  196. mutex_lock(&mapping->i_mmap_mutex);
  197. flush_dcache_mmap_lock(mapping);
  198. vma->vm_flags |= VM_NONLINEAR;
  199. vma_interval_tree_remove(vma, &mapping->i_mmap);
  200. vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
  201. flush_dcache_mmap_unlock(mapping);
  202. mutex_unlock(&mapping->i_mmap_mutex);
  203. }
  204. if (vma->vm_flags & VM_LOCKED) {
  205. /*
  206. * drop PG_Mlocked flag for over-mapped range
  207. */
  208. if (!has_write_lock)
  209. goto get_write_lock;
  210. vm_flags = vma->vm_flags;
  211. munlock_vma_pages_range(vma, start, start + size);
  212. vma->vm_flags = vm_flags;
  213. }
  214. mmu_notifier_invalidate_range_start(mm, start, start + size);
  215. err = vma->vm_ops->remap_pages(vma, start, size, pgoff);
  216. mmu_notifier_invalidate_range_end(mm, start, start + size);
  217. /*
  218. * We can't clear VM_NONLINEAR because we'd have to do
  219. * it after ->populate completes, and that would prevent
  220. * downgrading the lock. (Locks can't be upgraded).
  221. */
  222. out:
  223. if (vma)
  224. vm_flags = vma->vm_flags;
  225. if (likely(!has_write_lock))
  226. up_read(&mm->mmap_sem);
  227. else
  228. up_write(&mm->mmap_sem);
  229. if (!err && ((vm_flags & VM_LOCKED) || !(flags & MAP_NONBLOCK)))
  230. mm_populate(start, size);
  231. return err;
  232. }