fremap.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. 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. set_pte_at(mm, addr, pte, mk_pte(page, prot));
  74. page_add_file_rmap(page);
  75. pte_val = *pte;
  76. update_mmu_cache(vma, addr, pte_val);
  77. err = 0;
  78. unlock:
  79. pte_unmap_unlock(pte, ptl);
  80. out:
  81. return err;
  82. }
  83. EXPORT_SYMBOL(install_page);
  84. /*
  85. * Install a file pte to a given virtual memory address, release any
  86. * previously existing mapping.
  87. */
  88. int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
  89. unsigned long addr, unsigned long pgoff, pgprot_t prot)
  90. {
  91. int err = -ENOMEM;
  92. pte_t *pte;
  93. pte_t pte_val;
  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. pte_val = *pte;
  104. update_mmu_cache(vma, addr, pte_val);
  105. pte_unmap_unlock(pte, ptl);
  106. err = 0;
  107. out:
  108. return err;
  109. }
  110. /***
  111. * sys_remap_file_pages - remap arbitrary pages of a shared backing store
  112. * file within an existing vma.
  113. * @start: start of the remapped virtual memory range
  114. * @size: size of the remapped virtual memory range
  115. * @prot: new protection bits of the range
  116. * @pgoff: to be mapped page of the backing store file
  117. * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
  118. *
  119. * this syscall works purely via pagetables, so it's the most efficient
  120. * way to map the same (large) file into a given virtual window. Unlike
  121. * mmap()/mremap() it does not create any new vmas. The new mappings are
  122. * also safe across swapout.
  123. *
  124. * NOTE: the 'prot' parameter right now is ignored, and the vma's default
  125. * protection is used. Arbitrary protections might be implemented in the
  126. * future.
  127. */
  128. asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
  129. unsigned long __prot, unsigned long pgoff, unsigned long flags)
  130. {
  131. struct mm_struct *mm = current->mm;
  132. struct address_space *mapping;
  133. unsigned long end = start + size;
  134. struct vm_area_struct *vma;
  135. int err = -EINVAL;
  136. int has_write_lock = 0;
  137. if (__prot)
  138. return err;
  139. /*
  140. * Sanitize the syscall parameters:
  141. */
  142. start = start & PAGE_MASK;
  143. size = size & PAGE_MASK;
  144. /* Does the address range wrap, or is the span zero-sized? */
  145. if (start + size <= start)
  146. return err;
  147. /* Can we represent this offset inside this architecture's pte's? */
  148. #if PTE_FILE_MAX_BITS < BITS_PER_LONG
  149. if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
  150. return err;
  151. #endif
  152. /* We need down_write() to change vma->vm_flags. */
  153. down_read(&mm->mmap_sem);
  154. retry:
  155. vma = find_vma(mm, start);
  156. /*
  157. * Make sure the vma is shared, that it supports prefaulting,
  158. * and that the remapped range is valid and fully within
  159. * the single existing vma. vm_private_data is used as a
  160. * swapout cursor in a VM_NONLINEAR vma.
  161. */
  162. if (vma && (vma->vm_flags & VM_SHARED) &&
  163. (!vma->vm_private_data || (vma->vm_flags & VM_NONLINEAR)) &&
  164. vma->vm_ops && vma->vm_ops->populate &&
  165. end > start && start >= vma->vm_start &&
  166. end <= vma->vm_end) {
  167. /* Must set VM_NONLINEAR before any pages are populated. */
  168. if (pgoff != linear_page_index(vma, start) &&
  169. !(vma->vm_flags & VM_NONLINEAR)) {
  170. if (!has_write_lock) {
  171. up_read(&mm->mmap_sem);
  172. down_write(&mm->mmap_sem);
  173. has_write_lock = 1;
  174. goto retry;
  175. }
  176. mapping = vma->vm_file->f_mapping;
  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 = vma->vm_ops->populate(vma, start, size,
  186. vma->vm_page_prot,
  187. pgoff, flags & MAP_NONBLOCK);
  188. /*
  189. * We can't clear VM_NONLINEAR because we'd have to do
  190. * it after ->populate completes, and that would prevent
  191. * downgrading the lock. (Locks can't be upgraded).
  192. */
  193. }
  194. if (likely(!has_write_lock))
  195. up_read(&mm->mmap_sem);
  196. else
  197. up_write(&mm->mmap_sem);
  198. return err;
  199. }