blob: 8a66f5d5d4f0d5cda1a14bb265ddef30ca6e9ac0 [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>
Andrew Morton9c50823e2006-03-24 03:18:12 -080015#include <linux/writeback.h>
16#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
18
19#include <asm/pgtable.h>
20#include <asm/tlbflush.h>
21
Andrew Morton9c50823e2006-03-24 03:18:12 -080022static unsigned long msync_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 unsigned long addr, unsigned long end)
24{
25 pte_t *pte;
Hugh Dickins705e87c2005-10-29 18:16:27 -070026 spinlock_t *ptl;
Hugh Dickins0c942a42005-10-29 18:15:53 -070027 int progress = 0;
Andrew Morton9c50823e2006-03-24 03:18:12 -080028 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Hugh Dickins0c942a42005-10-29 18:15:53 -070030again:
Hugh Dickins705e87c2005-10-29 18:16:27 -070031 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 struct page *page;
34
Hugh Dickins0c942a42005-10-29 18:15:53 -070035 if (progress >= 64) {
36 progress = 0;
Hugh Dickins705e87c2005-10-29 18:16:27 -070037 if (need_resched() || need_lockbreak(ptl))
Hugh Dickins0c942a42005-10-29 18:15:53 -070038 break;
39 }
40 progress++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if (!pte_present(*pte))
42 continue;
Abhijit Karmarkarb4955ce2005-06-21 17:15:13 -070043 if (!pte_maybe_dirty(*pte))
44 continue;
Linus Torvalds6aab3412005-11-28 14:34:23 -080045 page = vm_normal_page(vma, addr, *pte);
46 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 if (ptep_clear_flush_dirty(vma, addr, pte) ||
Andrew Morton9c50823e2006-03-24 03:18:12 -080049 page_test_and_clear_dirty(page))
50 ret += set_page_dirty(page);
Hugh Dickins0c942a42005-10-29 18:15:53 -070051 progress += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickins705e87c2005-10-29 18:16:27 -070053 pte_unmap_unlock(pte - 1, ptl);
54 cond_resched();
Hugh Dickins0c942a42005-10-29 18:15:53 -070055 if (addr != end)
56 goto again;
Andrew Morton9c50823e2006-03-24 03:18:12 -080057 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Andrew Morton9c50823e2006-03-24 03:18:12 -080060static inline unsigned long msync_pmd_range(struct vm_area_struct *vma,
61 pud_t *pud, unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 pmd_t *pmd;
64 unsigned long next;
Andrew Morton9c50823e2006-03-24 03:18:12 -080065 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 pmd = pmd_offset(pud, addr);
68 do {
69 next = pmd_addr_end(addr, end);
70 if (pmd_none_or_clear_bad(pmd))
71 continue;
Andrew Morton9c50823e2006-03-24 03:18:12 -080072 ret += msync_pte_range(vma, pmd, addr, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 } while (pmd++, addr = next, addr != end);
Andrew Morton9c50823e2006-03-24 03:18:12 -080074 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Andrew Morton9c50823e2006-03-24 03:18:12 -080077static inline unsigned long msync_pud_range(struct vm_area_struct *vma,
78 pgd_t *pgd, unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 pud_t *pud;
81 unsigned long next;
Andrew Morton9c50823e2006-03-24 03:18:12 -080082 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 pud = pud_offset(pgd, addr);
85 do {
86 next = pud_addr_end(addr, end);
87 if (pud_none_or_clear_bad(pud))
88 continue;
Andrew Morton9c50823e2006-03-24 03:18:12 -080089 ret += msync_pmd_range(vma, pud, addr, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 } while (pud++, addr = next, addr != end);
Andrew Morton9c50823e2006-03-24 03:18:12 -080091 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Andrew Morton9c50823e2006-03-24 03:18:12 -080094static unsigned long msync_page_range(struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 unsigned long addr, unsigned long end)
96{
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 pgd_t *pgd;
98 unsigned long next;
Andrew Morton9c50823e2006-03-24 03:18:12 -080099 unsigned long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 /* For hugepages we can't go walking the page table normally,
102 * but that's ok, hugetlbfs is memory based, so we don't need
Nick Pigginb5810032005-10-29 18:16:12 -0700103 * to do anything more on an msync().
Nick Pigginb5810032005-10-29 18:16:12 -0700104 */
Linus Torvalds6aab3412005-11-28 14:34:23 -0800105 if (vma->vm_flags & VM_HUGETLB)
Andrew Morton9c50823e2006-03-24 03:18:12 -0800106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 BUG_ON(addr >= end);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700109 pgd = pgd_offset(vma->vm_mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 flush_cache_range(vma, addr, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 do {
112 next = pgd_addr_end(addr, end);
113 if (pgd_none_or_clear_bad(pgd))
114 continue;
Andrew Morton9c50823e2006-03-24 03:18:12 -0800115 ret += msync_pud_range(vma, pgd, addr, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 } while (pgd++, addr = next, addr != end);
Andrew Morton9c50823e2006-03-24 03:18:12 -0800117 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/*
121 * MS_SYNC syncs the entire file - including mappings.
122 *
123 * MS_ASYNC does not start I/O (it used to, up to 2.5.67). Instead, it just
124 * marks the relevant pages dirty. The application may now run fsync() to
125 * write out the dirty pages and wait on the writeout and check the result.
126 * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
127 * async writeout immediately.
128 * So my _not_ starting I/O in MS_ASYNC we provide complete flexibility to
129 * applications.
130 */
Andrew Morton9c50823e2006-03-24 03:18:12 -0800131static int msync_interval(struct vm_area_struct *vma, unsigned long addr,
132 unsigned long end, int flags,
133 unsigned long *nr_pages_dirtied)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 int ret = 0;
136 struct file *file = vma->vm_file;
137
138 if ((flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED))
139 return -EBUSY;
140
141 if (file && (vma->vm_flags & VM_SHARED)) {
Andrew Morton9c50823e2006-03-24 03:18:12 -0800142 *nr_pages_dirtied = msync_page_range(vma, addr, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 if (flags & MS_SYNC) {
145 struct address_space *mapping = file->f_mapping;
146 int err;
147
148 ret = filemap_fdatawrite(mapping);
149 if (file->f_op && file->f_op->fsync) {
150 /*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800151 * We don't take i_mutex here because mmap_sem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * is already held.
153 */
154 err = file->f_op->fsync(file,file->f_dentry,1);
155 if (err && !ret)
156 ret = err;
157 }
158 err = filemap_fdatawait(mapping);
159 if (!ret)
160 ret = err;
161 }
162 }
163 return ret;
164}
165
166asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
167{
168 unsigned long end;
169 struct vm_area_struct *vma;
170 int unmapped_error, error = -EINVAL;
Andrew Morton9c50823e2006-03-24 03:18:12 -0800171 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
174 goto out;
175 if (start & ~PAGE_MASK)
176 goto out;
177 if ((flags & MS_ASYNC) && (flags & MS_SYNC))
178 goto out;
179 error = -ENOMEM;
180 len = (len + ~PAGE_MASK) & PAGE_MASK;
181 end = start + len;
182 if (end < start)
183 goto out;
184 error = 0;
185 if (end == start)
186 goto out;
187 /*
188 * If the interval [start,end) covers some unmapped address ranges,
189 * just ignore them, but return -ENOMEM at the end.
190 */
Andrew Morton9c50823e2006-03-24 03:18:12 -0800191 down_read(&current->mm->mmap_sem);
192 if (flags & MS_SYNC)
193 current->flags |= PF_SYNCWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 vma = find_vma(current->mm, start);
195 unmapped_error = 0;
Andrew Morton9c50823e2006-03-24 03:18:12 -0800196 do {
197 unsigned long nr_pages_dirtied = 0;
198 struct file *file;
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* Still start < end. */
201 error = -ENOMEM;
202 if (!vma)
Andrew Morton9c50823e2006-03-24 03:18:12 -0800203 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* Here start < vma->vm_end. */
205 if (start < vma->vm_start) {
206 unmapped_error = -ENOMEM;
207 start = vma->vm_start;
208 }
209 /* Here vma->vm_start <= start < vma->vm_end. */
210 if (end <= vma->vm_end) {
211 if (start < end) {
Andrew Morton9c50823e2006-03-24 03:18:12 -0800212 error = msync_interval(vma, start, end, flags,
213 &nr_pages_dirtied);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (error)
Andrew Morton9c50823e2006-03-24 03:18:12 -0800215 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217 error = unmapped_error;
Andrew Morton9c50823e2006-03-24 03:18:12 -0800218 done = 1;
219 } else {
220 /* Here vma->vm_start <= start < vma->vm_end < end. */
221 error = msync_interval(vma, start, vma->vm_end, flags,
222 &nr_pages_dirtied);
223 if (error)
224 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Andrew Morton9c50823e2006-03-24 03:18:12 -0800226 file = vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 start = vma->vm_end;
Andrew Morton9c50823e2006-03-24 03:18:12 -0800228 if ((flags & MS_ASYNC) && file && nr_pages_dirtied) {
229 get_file(file);
230 up_read(&current->mm->mmap_sem);
231 balance_dirty_pages_ratelimited_nr(file->f_mapping,
232 nr_pages_dirtied);
233 fput(file);
234 down_read(&current->mm->mmap_sem);
235 vma = find_vma(current->mm, start);
236 } else {
237 vma = vma->vm_next;
238 }
239 } while (!done);
240out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 current->flags &= ~PF_SYNCWRITE;
Andrew Morton9c50823e2006-03-24 03:18:12 -0800242 up_read(&current->mm->mmap_sem);
243out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return error;
245}