msync.c 5.4 KB

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