msync.c 5.2 KB

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