fremap.c 6.3 KB

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