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