fremap.c 6.8 KB

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