fremap.c 6.0 KB

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