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>
21 * Called with mm->page_table_lock held to protect against other
22 * threads/the swapper from ripping pte's out from under us.
25 static void msync_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
26 unsigned long addr, unsigned long end)
32 pte = pte_offset_map(pmd, addr);
40 need_lockbreak(&vma->vm_mm->page_table_lock))
44 if (!pte_present(*pte))
46 if (!pte_maybe_dirty(*pte))
51 page = pfn_to_page(pfn);
52 if (PageReserved(page))
55 if (ptep_clear_flush_dirty(vma, addr, pte) ||
56 page_test_and_clear_dirty(page))
59 } while (pte++, addr += PAGE_SIZE, addr != end);
61 cond_resched_lock(&vma->vm_mm->page_table_lock);
66 static inline void msync_pmd_range(struct vm_area_struct *vma, pud_t *pud,
67 unsigned long addr, unsigned long end)
72 pmd = pmd_offset(pud, addr);
74 next = pmd_addr_end(addr, end);
75 if (pmd_none_or_clear_bad(pmd))
77 msync_pte_range(vma, pmd, addr, next);
78 } while (pmd++, addr = next, addr != end);
81 static inline void msync_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
82 unsigned long addr, unsigned long end)
87 pud = pud_offset(pgd, addr);
89 next = pud_addr_end(addr, end);
90 if (pud_none_or_clear_bad(pud))
92 msync_pmd_range(vma, pud, addr, next);
93 } while (pud++, addr = next, addr != end);
96 static void msync_page_range(struct vm_area_struct *vma,
97 unsigned long addr, unsigned long end)
99 struct mm_struct *mm = vma->vm_mm;
103 /* For hugepages we can't go walking the page table normally,
104 * but that's ok, hugetlbfs is memory based, so we don't need
105 * to do anything more on an msync() */
106 if (is_vm_hugetlb_page(vma))
110 pgd = pgd_offset(mm, addr);
111 flush_cache_range(vma, addr, end);
112 spin_lock(&mm->page_table_lock);
114 next = pgd_addr_end(addr, end);
115 if (pgd_none_or_clear_bad(pgd))
117 msync_pud_range(vma, pgd, addr, next);
118 } while (pgd++, addr = next, addr != end);
119 spin_unlock(&mm->page_table_lock);
123 * MS_SYNC syncs the entire file - including mappings.
125 * MS_ASYNC does not start I/O (it used to, up to 2.5.67). Instead, it just
126 * marks the relevant pages dirty. The application may now run fsync() to
127 * write out the dirty pages and wait on the writeout and check the result.
128 * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
129 * async writeout immediately.
130 * So my _not_ starting I/O in MS_ASYNC we provide complete flexibility to
133 static int msync_interval(struct vm_area_struct *vma,
134 unsigned long addr, unsigned long end, int flags)
137 struct file *file = vma->vm_file;
139 if ((flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED))
142 if (file && (vma->vm_flags & VM_SHARED)) {
143 msync_page_range(vma, addr, end);
145 if (flags & MS_SYNC) {
146 struct address_space *mapping = file->f_mapping;
149 ret = filemap_fdatawrite(mapping);
150 if (file->f_op && file->f_op->fsync) {
152 * We don't take i_sem here because mmap_sem
155 err = file->f_op->fsync(file,file->f_dentry,1);
159 err = filemap_fdatawait(mapping);
167 asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
170 struct vm_area_struct *vma;
171 int unmapped_error, error = -EINVAL;
174 current->flags |= PF_SYNCWRITE;
176 down_read(¤t->mm->mmap_sem);
177 if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
179 if (start & ~PAGE_MASK)
181 if ((flags & MS_ASYNC) && (flags & MS_SYNC))
184 len = (len + ~PAGE_MASK) & PAGE_MASK;
192 * If the interval [start,end) covers some unmapped address ranges,
193 * just ignore them, but return -ENOMEM at the end.
195 vma = find_vma(current->mm, start);
198 /* Still start < end. */
202 /* Here start < vma->vm_end. */
203 if (start < vma->vm_start) {
204 unmapped_error = -ENOMEM;
205 start = vma->vm_start;
207 /* Here vma->vm_start <= start < vma->vm_end. */
208 if (end <= vma->vm_end) {
210 error = msync_interval(vma, start, end, flags);
214 error = unmapped_error;
217 /* Here vma->vm_start <= start < vma->vm_end < end. */
218 error = msync_interval(vma, start, vma->vm_end, flags);
225 up_read(¤t->mm->mmap_sem);
226 current->flags &= ~PF_SYNCWRITE;