msync.c 5.7 KB

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