4 * Copyright (C) 1994-1999 Linus Torvalds
8 * The msync() system call.
10 #include <linux/slab.h>
11 #include <linux/pagemap.h>
13 #include <linux/mman.h>
14 #include <linux/hugetlb.h>
15 #include <linux/syscalls.h>
17 #include <asm/pgtable.h>
18 #include <asm/tlbflush.h>
20 static void msync_pte_range(struct vm_area_struct
*vma
, pmd_t
*pmd
,
21 unsigned long addr
, unsigned long end
)
28 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
34 if (need_resched() || need_lockbreak(ptl
))
38 if (!pte_present(*pte
))
40 if (!pte_maybe_dirty(*pte
))
42 page
= vm_normal_page(vma
, addr
, *pte
);
45 if (ptep_clear_flush_dirty(vma
, addr
, pte
) ||
46 page_test_and_clear_dirty(page
))
49 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
50 pte_unmap_unlock(pte
- 1, ptl
);
56 static inline void msync_pmd_range(struct vm_area_struct
*vma
, pud_t
*pud
,
57 unsigned long addr
, unsigned long end
)
62 pmd
= pmd_offset(pud
, addr
);
64 next
= pmd_addr_end(addr
, end
);
65 if (pmd_none_or_clear_bad(pmd
))
67 msync_pte_range(vma
, pmd
, addr
, next
);
68 } while (pmd
++, addr
= next
, addr
!= end
);
71 static inline void msync_pud_range(struct vm_area_struct
*vma
, pgd_t
*pgd
,
72 unsigned long addr
, unsigned long end
)
77 pud
= pud_offset(pgd
, addr
);
79 next
= pud_addr_end(addr
, end
);
80 if (pud_none_or_clear_bad(pud
))
82 msync_pmd_range(vma
, pud
, addr
, next
);
83 } while (pud
++, addr
= next
, addr
!= end
);
86 static void msync_page_range(struct vm_area_struct
*vma
,
87 unsigned long addr
, unsigned long end
)
92 /* For hugepages we can't go walking the page table normally,
93 * but that's ok, hugetlbfs is memory based, so we don't need
94 * to do anything more on an msync().
96 if (vma
->vm_flags
& VM_HUGETLB
)
100 pgd
= pgd_offset(vma
->vm_mm
, addr
);
101 flush_cache_range(vma
, addr
, end
);
103 next
= pgd_addr_end(addr
, end
);
104 if (pgd_none_or_clear_bad(pgd
))
106 msync_pud_range(vma
, pgd
, addr
, next
);
107 } while (pgd
++, addr
= next
, addr
!= end
);
111 * MS_SYNC syncs the entire file - including mappings.
113 * MS_ASYNC does not start I/O (it used to, up to 2.5.67). Instead, it just
114 * marks the relevant pages dirty. The application may now run fsync() to
115 * write out the dirty pages and wait on the writeout and check the result.
116 * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
117 * async writeout immediately.
118 * So my _not_ starting I/O in MS_ASYNC we provide complete flexibility to
121 static int msync_interval(struct vm_area_struct
*vma
,
122 unsigned long addr
, unsigned long end
, int flags
)
125 struct file
*file
= vma
->vm_file
;
127 if ((flags
& MS_INVALIDATE
) && (vma
->vm_flags
& VM_LOCKED
))
130 if (file
&& (vma
->vm_flags
& VM_SHARED
)) {
131 msync_page_range(vma
, addr
, end
);
133 if (flags
& MS_SYNC
) {
134 struct address_space
*mapping
= file
->f_mapping
;
137 ret
= filemap_fdatawrite(mapping
);
138 if (file
->f_op
&& file
->f_op
->fsync
) {
140 * We don't take i_sem here because mmap_sem
143 err
= file
->f_op
->fsync(file
,file
->f_dentry
,1);
147 err
= filemap_fdatawait(mapping
);
155 asmlinkage
long sys_msync(unsigned long start
, size_t len
, int flags
)
158 struct vm_area_struct
*vma
;
159 int unmapped_error
, error
= -EINVAL
;
162 current
->flags
|= PF_SYNCWRITE
;
164 down_read(¤t
->mm
->mmap_sem
);
165 if (flags
& ~(MS_ASYNC
| MS_INVALIDATE
| MS_SYNC
))
167 if (start
& ~PAGE_MASK
)
169 if ((flags
& MS_ASYNC
) && (flags
& MS_SYNC
))
172 len
= (len
+ ~PAGE_MASK
) & PAGE_MASK
;
180 * If the interval [start,end) covers some unmapped address ranges,
181 * just ignore them, but return -ENOMEM at the end.
183 vma
= find_vma(current
->mm
, start
);
186 /* Still start < end. */
190 /* Here start < vma->vm_end. */
191 if (start
< vma
->vm_start
) {
192 unmapped_error
= -ENOMEM
;
193 start
= vma
->vm_start
;
195 /* Here vma->vm_start <= start < vma->vm_end. */
196 if (end
<= vma
->vm_end
) {
198 error
= msync_interval(vma
, start
, end
, flags
);
202 error
= unmapped_error
;
205 /* Here vma->vm_start <= start < vma->vm_end < end. */
206 error
= msync_interval(vma
, start
, vma
->vm_end
, flags
);
213 up_read(¤t
->mm
->mmap_sem
);
214 current
->flags
&= ~PF_SYNCWRITE
;