msync.c 5.5 KB

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