msync.c 5.0 KB

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