blob: 8603954860601b24807c69a5d538a7114ee6932b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/msync.c
3 *
4 * Copyright (C) 1994-1999 Linus Torvalds
5 */
6
7/*
8 * The msync() system call.
9 */
10#include <linux/slab.h>
11#include <linux/pagemap.h>
12#include <linux/mm.h>
13#include <linux/mman.h>
14#include <linux/hugetlb.h>
15#include <linux/syscalls.h>
16
17#include <asm/pgtable.h>
18#include <asm/tlbflush.h>
19
20/*
21 * Called with mm->page_table_lock held to protect against other
22 * threads/the swapper from ripping pte's out from under us.
23 */
24
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070025static void msync_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 unsigned long addr, unsigned long end)
27{
Nick Pigginb5810032005-10-29 18:16:12 -070028 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 pte_t *pte;
Hugh Dickins0c942a42005-10-29 18:15:53 -070030 int progress = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Hugh Dickins0c942a42005-10-29 18:15:53 -070032again:
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 pte = pte_offset_map(pmd, addr);
34 do {
35 unsigned long pfn;
36 struct page *page;
37
Hugh Dickins0c942a42005-10-29 18:15:53 -070038 if (progress >= 64) {
39 progress = 0;
40 if (need_resched() ||
Nick Pigginb5810032005-10-29 18:16:12 -070041 need_lockbreak(&mm->page_table_lock))
Hugh Dickins0c942a42005-10-29 18:15:53 -070042 break;
43 }
44 progress++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (!pte_present(*pte))
46 continue;
Abhijit Karmarkarb4955ce2005-06-21 17:15:13 -070047 if (!pte_maybe_dirty(*pte))
48 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 pfn = pte_pfn(*pte);
Nick Pigginb5810032005-10-29 18:16:12 -070050 if (unlikely(!pfn_valid(pfn))) {
51 print_bad_pte(vma, *pte, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 continue;
Nick Pigginb5810032005-10-29 18:16:12 -070053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 if (ptep_clear_flush_dirty(vma, addr, pte) ||
57 page_test_and_clear_dirty(page))
58 set_page_dirty(page);
Hugh Dickins0c942a42005-10-29 18:15:53 -070059 progress += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 } while (pte++, addr += PAGE_SIZE, addr != end);
61 pte_unmap(pte - 1);
Nick Pigginb5810032005-10-29 18:16:12 -070062 cond_resched_lock(&mm->page_table_lock);
Hugh Dickins0c942a42005-10-29 18:15:53 -070063 if (addr != end)
64 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070067static inline void msync_pmd_range(struct vm_area_struct *vma, pud_t *pud,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 unsigned long addr, unsigned long end)
69{
70 pmd_t *pmd;
71 unsigned long next;
72
73 pmd = pmd_offset(pud, addr);
74 do {
75 next = pmd_addr_end(addr, end);
76 if (pmd_none_or_clear_bad(pmd))
77 continue;
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070078 msync_pte_range(vma, pmd, addr, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 } while (pmd++, addr = next, addr != end);
80}
81
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070082static inline void msync_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 unsigned long addr, unsigned long end)
84{
85 pud_t *pud;
86 unsigned long next;
87
88 pud = pud_offset(pgd, addr);
89 do {
90 next = pud_addr_end(addr, end);
91 if (pud_none_or_clear_bad(pud))
92 continue;
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070093 msync_pmd_range(vma, pud, addr, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 } while (pud++, addr = next, addr != end);
95}
96
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070097static void msync_page_range(struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 unsigned long addr, unsigned long end)
99{
100 struct mm_struct *mm = vma->vm_mm;
101 pgd_t *pgd;
102 unsigned long next;
103
104 /* For hugepages we can't go walking the page table normally,
105 * but that's ok, hugetlbfs is memory based, so we don't need
Nick Pigginb5810032005-10-29 18:16:12 -0700106 * to do anything more on an msync().
107 * Can't do anything with VM_RESERVED regions either.
108 */
109 if (vma->vm_flags & (VM_HUGETLB|VM_RESERVED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return;
111
112 BUG_ON(addr >= end);
113 pgd = pgd_offset(mm, addr);
114 flush_cache_range(vma, addr, end);
115 spin_lock(&mm->page_table_lock);
116 do {
117 next = pgd_addr_end(addr, end);
118 if (pgd_none_or_clear_bad(pgd))
119 continue;
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -0700120 msync_pud_range(vma, pgd, addr, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 } while (pgd++, addr = next, addr != end);
122 spin_unlock(&mm->page_table_lock);
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/*
126 * MS_SYNC syncs the entire file - including mappings.
127 *
128 * MS_ASYNC does not start I/O (it used to, up to 2.5.67). Instead, it just
129 * marks the relevant pages dirty. The application may now run fsync() to
130 * write out the dirty pages and wait on the writeout and check the result.
131 * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
132 * async writeout immediately.
133 * So my _not_ starting I/O in MS_ASYNC we provide complete flexibility to
134 * applications.
135 */
136static int msync_interval(struct vm_area_struct *vma,
137 unsigned long addr, unsigned long end, int flags)
138{
139 int ret = 0;
140 struct file *file = vma->vm_file;
141
142 if ((flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED))
143 return -EBUSY;
144
145 if (file && (vma->vm_flags & VM_SHARED)) {
Hugh Dickins0c942a42005-10-29 18:15:53 -0700146 msync_page_range(vma, addr, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 if (flags & MS_SYNC) {
149 struct address_space *mapping = file->f_mapping;
150 int err;
151
152 ret = filemap_fdatawrite(mapping);
153 if (file->f_op && file->f_op->fsync) {
154 /*
155 * We don't take i_sem here because mmap_sem
156 * is already held.
157 */
158 err = file->f_op->fsync(file,file->f_dentry,1);
159 if (err && !ret)
160 ret = err;
161 }
162 err = filemap_fdatawait(mapping);
163 if (!ret)
164 ret = err;
165 }
166 }
167 return ret;
168}
169
170asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
171{
172 unsigned long end;
173 struct vm_area_struct *vma;
174 int unmapped_error, error = -EINVAL;
175
176 if (flags & MS_SYNC)
177 current->flags |= PF_SYNCWRITE;
178
179 down_read(&current->mm->mmap_sem);
180 if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
181 goto out;
182 if (start & ~PAGE_MASK)
183 goto out;
184 if ((flags & MS_ASYNC) && (flags & MS_SYNC))
185 goto out;
186 error = -ENOMEM;
187 len = (len + ~PAGE_MASK) & PAGE_MASK;
188 end = start + len;
189 if (end < start)
190 goto out;
191 error = 0;
192 if (end == start)
193 goto out;
194 /*
195 * If the interval [start,end) covers some unmapped address ranges,
196 * just ignore them, but return -ENOMEM at the end.
197 */
198 vma = find_vma(current->mm, start);
199 unmapped_error = 0;
200 for (;;) {
201 /* Still start < end. */
202 error = -ENOMEM;
203 if (!vma)
204 goto out;
205 /* Here start < vma->vm_end. */
206 if (start < vma->vm_start) {
207 unmapped_error = -ENOMEM;
208 start = vma->vm_start;
209 }
210 /* Here vma->vm_start <= start < vma->vm_end. */
211 if (end <= vma->vm_end) {
212 if (start < end) {
213 error = msync_interval(vma, start, end, flags);
214 if (error)
215 goto out;
216 }
217 error = unmapped_error;
218 goto out;
219 }
220 /* Here vma->vm_start <= start < vma->vm_end < end. */
221 error = msync_interval(vma, start, vma->vm_end, flags);
222 if (error)
223 goto out;
224 start = vma->vm_end;
225 vma = vma->vm_next;
226 }
227out:
228 up_read(&current->mm->mmap_sem);
229 current->flags &= ~PF_SYNCWRITE;
230 return error;
231}