fremap.c 6.4 KB

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