msync.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * linux/mm/msync.c
  3. *
  4. * Copyright (C) 1994-1999 Linus Torvalds
  5. */
  6. /*
  7. * The msync() system call.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/mm.h>
  12. #include <linux/mman.h>
  13. #include <linux/hugetlb.h>
  14. #include <linux/syscalls.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/tlbflush.h>
  17. /*
  18. * Called with mm->page_table_lock held to protect against other
  19. * threads/the swapper from ripping pte's out from under us.
  20. */
  21. static void sync_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  22. unsigned long addr, unsigned long end)
  23. {
  24. pte_t *pte;
  25. pte = pte_offset_map(pmd, addr);
  26. do {
  27. unsigned long pfn;
  28. struct page *page;
  29. if (!pte_present(*pte))
  30. continue;
  31. if (!pte_maybe_dirty(*pte))
  32. continue;
  33. pfn = pte_pfn(*pte);
  34. if (!pfn_valid(pfn))
  35. continue;
  36. page = pfn_to_page(pfn);
  37. if (PageReserved(page))
  38. continue;
  39. if (ptep_clear_flush_dirty(vma, addr, pte) ||
  40. page_test_and_clear_dirty(page))
  41. set_page_dirty(page);
  42. } while (pte++, addr += PAGE_SIZE, addr != end);
  43. pte_unmap(pte - 1);
  44. }
  45. static inline void sync_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  46. unsigned long addr, unsigned long end)
  47. {
  48. pmd_t *pmd;
  49. unsigned long next;
  50. pmd = pmd_offset(pud, addr);
  51. do {
  52. next = pmd_addr_end(addr, end);
  53. if (pmd_none_or_clear_bad(pmd))
  54. continue;
  55. sync_pte_range(vma, pmd, addr, next);
  56. } while (pmd++, addr = next, addr != end);
  57. }
  58. static inline void sync_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  59. unsigned long addr, unsigned long end)
  60. {
  61. pud_t *pud;
  62. unsigned long next;
  63. pud = pud_offset(pgd, addr);
  64. do {
  65. next = pud_addr_end(addr, end);
  66. if (pud_none_or_clear_bad(pud))
  67. continue;
  68. sync_pmd_range(vma, pud, addr, next);
  69. } while (pud++, addr = next, addr != end);
  70. }
  71. static void sync_page_range(struct vm_area_struct *vma,
  72. unsigned long addr, unsigned long end)
  73. {
  74. struct mm_struct *mm = vma->vm_mm;
  75. pgd_t *pgd;
  76. unsigned long next;
  77. /* For hugepages we can't go walking the page table normally,
  78. * but that's ok, hugetlbfs is memory based, so we don't need
  79. * to do anything more on an msync() */
  80. if (is_vm_hugetlb_page(vma))
  81. return;
  82. BUG_ON(addr >= end);
  83. pgd = pgd_offset(mm, addr);
  84. flush_cache_range(vma, addr, end);
  85. spin_lock(&mm->page_table_lock);
  86. do {
  87. next = pgd_addr_end(addr, end);
  88. if (pgd_none_or_clear_bad(pgd))
  89. continue;
  90. sync_pud_range(vma, pgd, addr, next);
  91. } while (pgd++, addr = next, addr != end);
  92. spin_unlock(&mm->page_table_lock);
  93. }
  94. #ifdef CONFIG_PREEMPT
  95. static inline void filemap_sync(struct vm_area_struct *vma,
  96. unsigned long addr, unsigned long end)
  97. {
  98. const size_t chunk = 64 * 1024; /* bytes */
  99. unsigned long next;
  100. do {
  101. next = addr + chunk;
  102. if (next > end || next < addr)
  103. next = end;
  104. sync_page_range(vma, addr, next);
  105. cond_resched();
  106. } while (addr = next, addr != end);
  107. }
  108. #else
  109. static inline void filemap_sync(struct vm_area_struct *vma,
  110. unsigned long addr, unsigned long end)
  111. {
  112. sync_page_range(vma, addr, end);
  113. }
  114. #endif
  115. /*
  116. * MS_SYNC syncs the entire file - including mappings.
  117. *
  118. * MS_ASYNC does not start I/O (it used to, up to 2.5.67). Instead, it just
  119. * marks the relevant pages dirty. The application may now run fsync() to
  120. * write out the dirty pages and wait on the writeout and check the result.
  121. * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
  122. * async writeout immediately.
  123. * So my _not_ starting I/O in MS_ASYNC we provide complete flexibility to
  124. * applications.
  125. */
  126. static int msync_interval(struct vm_area_struct *vma,
  127. unsigned long addr, unsigned long end, int flags)
  128. {
  129. int ret = 0;
  130. struct file *file = vma->vm_file;
  131. if ((flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED))
  132. return -EBUSY;
  133. if (file && (vma->vm_flags & VM_SHARED)) {
  134. filemap_sync(vma, addr, end);
  135. if (flags & MS_SYNC) {
  136. struct address_space *mapping = file->f_mapping;
  137. int err;
  138. ret = filemap_fdatawrite(mapping);
  139. if (file->f_op && file->f_op->fsync) {
  140. /*
  141. * We don't take i_sem here because mmap_sem
  142. * is already held.
  143. */
  144. err = file->f_op->fsync(file,file->f_dentry,1);
  145. if (err && !ret)
  146. ret = err;
  147. }
  148. err = filemap_fdatawait(mapping);
  149. if (!ret)
  150. ret = err;
  151. }
  152. }
  153. return ret;
  154. }
  155. asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
  156. {
  157. unsigned long end;
  158. struct vm_area_struct *vma;
  159. int unmapped_error, error = -EINVAL;
  160. if (flags & MS_SYNC)
  161. current->flags |= PF_SYNCWRITE;
  162. down_read(&current->mm->mmap_sem);
  163. if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
  164. goto out;
  165. if (start & ~PAGE_MASK)
  166. goto out;
  167. if ((flags & MS_ASYNC) && (flags & MS_SYNC))
  168. goto out;
  169. error = -ENOMEM;
  170. len = (len + ~PAGE_MASK) & PAGE_MASK;
  171. end = start + len;
  172. if (end < start)
  173. goto out;
  174. error = 0;
  175. if (end == start)
  176. goto out;
  177. /*
  178. * If the interval [start,end) covers some unmapped address ranges,
  179. * just ignore them, but return -ENOMEM at the end.
  180. */
  181. vma = find_vma(current->mm, start);
  182. unmapped_error = 0;
  183. for (;;) {
  184. /* Still start < end. */
  185. error = -ENOMEM;
  186. if (!vma)
  187. goto out;
  188. /* Here start < vma->vm_end. */
  189. if (start < vma->vm_start) {
  190. unmapped_error = -ENOMEM;
  191. start = vma->vm_start;
  192. }
  193. /* Here vma->vm_start <= start < vma->vm_end. */
  194. if (end <= vma->vm_end) {
  195. if (start < end) {
  196. error = msync_interval(vma, start, end, flags);
  197. if (error)
  198. goto out;
  199. }
  200. error = unmapped_error;
  201. goto out;
  202. }
  203. /* Here vma->vm_start <= start < vma->vm_end < end. */
  204. error = msync_interval(vma, start, vma->vm_end, flags);
  205. if (error)
  206. goto out;
  207. start = vma->vm_end;
  208. vma = vma->vm_next;
  209. }
  210. out:
  211. up_read(&current->mm->mmap_sem);
  212. current->flags &= ~PF_SYNCWRITE;
  213. return error;
  214. }