blob: 0e040e9c39d835a7cf938468be6a9b7bccb95c2d [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
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070020static void msync_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 unsigned long addr, unsigned long end)
22{
23 pte_t *pte;
Hugh Dickins705e87c2005-10-29 18:16:27 -070024 spinlock_t *ptl;
Hugh Dickins0c942a42005-10-29 18:15:53 -070025 int progress = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Hugh Dickins0c942a42005-10-29 18:15:53 -070027again:
Hugh Dickins705e87c2005-10-29 18:16:27 -070028 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 do {
30 unsigned long pfn;
31 struct page *page;
32
Hugh Dickins0c942a42005-10-29 18:15:53 -070033 if (progress >= 64) {
34 progress = 0;
Hugh Dickins705e87c2005-10-29 18:16:27 -070035 if (need_resched() || need_lockbreak(ptl))
Hugh Dickins0c942a42005-10-29 18:15:53 -070036 break;
37 }
38 progress++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 if (!pte_present(*pte))
40 continue;
Abhijit Karmarkarb4955ce2005-06-21 17:15:13 -070041 if (!pte_maybe_dirty(*pte))
42 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 pfn = pte_pfn(*pte);
Nick Pigginb5810032005-10-29 18:16:12 -070044 if (unlikely(!pfn_valid(pfn))) {
45 print_bad_pte(vma, *pte, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 continue;
Nick Pigginb5810032005-10-29 18:16:12 -070047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 page = pfn_to_page(pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 if (ptep_clear_flush_dirty(vma, addr, pte) ||
51 page_test_and_clear_dirty(page))
52 set_page_dirty(page);
Hugh Dickins0c942a42005-10-29 18:15:53 -070053 progress += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickins705e87c2005-10-29 18:16:27 -070055 pte_unmap_unlock(pte - 1, ptl);
56 cond_resched();
Hugh Dickins0c942a42005-10-29 18:15:53 -070057 if (addr != end)
58 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070061static inline void msync_pmd_range(struct vm_area_struct *vma, pud_t *pud,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 unsigned long addr, unsigned long end)
63{
64 pmd_t *pmd;
65 unsigned long next;
66
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;
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070072 msync_pte_range(vma, pmd, addr, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 } while (pmd++, addr = next, addr != end);
74}
75
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070076static inline void msync_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unsigned long addr, unsigned long end)
78{
79 pud_t *pud;
80 unsigned long next;
81
82 pud = pud_offset(pgd, addr);
83 do {
84 next = pud_addr_end(addr, end);
85 if (pud_none_or_clear_bad(pud))
86 continue;
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070087 msync_pmd_range(vma, pud, addr, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 } while (pud++, addr = next, addr != end);
89}
90
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070091static void msync_page_range(struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 unsigned long addr, unsigned long end)
93{
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 pgd_t *pgd;
95 unsigned long next;
96
97 /* For hugepages we can't go walking the page table normally,
98 * but that's ok, hugetlbfs is memory based, so we don't need
Nick Pigginb5810032005-10-29 18:16:12 -070099 * to do anything more on an msync().
100 * Can't do anything with VM_RESERVED regions either.
101 */
102 if (vma->vm_flags & (VM_HUGETLB|VM_RESERVED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return;
104
105 BUG_ON(addr >= end);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700106 pgd = pgd_offset(vma->vm_mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 flush_cache_range(vma, addr, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 do {
109 next = pgd_addr_end(addr, end);
110 if (pgd_none_or_clear_bad(pgd))
111 continue;
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -0700112 msync_pud_range(vma, pgd, addr, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 } while (pgd++, addr = next, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/*
117 * MS_SYNC syncs the entire file - including mappings.
118 *
119 * MS_ASYNC does not start I/O (it used to, up to 2.5.67). Instead, it just
120 * marks the relevant pages dirty. The application may now run fsync() to
121 * write out the dirty pages and wait on the writeout and check the result.
122 * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
123 * async writeout immediately.
124 * So my _not_ starting I/O in MS_ASYNC we provide complete flexibility to
125 * applications.
126 */
127static int msync_interval(struct vm_area_struct *vma,
128 unsigned long addr, unsigned long end, int flags)
129{
130 int ret = 0;
131 struct file *file = vma->vm_file;
132
133 if ((flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED))
134 return -EBUSY;
135
136 if (file && (vma->vm_flags & VM_SHARED)) {
Hugh Dickins0c942a42005-10-29 18:15:53 -0700137 msync_page_range(vma, addr, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 if (flags & MS_SYNC) {
140 struct address_space *mapping = file->f_mapping;
141 int err;
142
143 ret = filemap_fdatawrite(mapping);
144 if (file->f_op && file->f_op->fsync) {
145 /*
146 * We don't take i_sem here because mmap_sem
147 * is already held.
148 */
149 err = file->f_op->fsync(file,file->f_dentry,1);
150 if (err && !ret)
151 ret = err;
152 }
153 err = filemap_fdatawait(mapping);
154 if (!ret)
155 ret = err;
156 }
157 }
158 return ret;
159}
160
161asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
162{
163 unsigned long end;
164 struct vm_area_struct *vma;
165 int unmapped_error, error = -EINVAL;
166
167 if (flags & MS_SYNC)
168 current->flags |= PF_SYNCWRITE;
169
170 down_read(&current->mm->mmap_sem);
171 if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
172 goto out;
173 if (start & ~PAGE_MASK)
174 goto out;
175 if ((flags & MS_ASYNC) && (flags & MS_SYNC))
176 goto out;
177 error = -ENOMEM;
178 len = (len + ~PAGE_MASK) & PAGE_MASK;
179 end = start + len;
180 if (end < start)
181 goto out;
182 error = 0;
183 if (end == start)
184 goto out;
185 /*
186 * If the interval [start,end) covers some unmapped address ranges,
187 * just ignore them, but return -ENOMEM at the end.
188 */
189 vma = find_vma(current->mm, start);
190 unmapped_error = 0;
191 for (;;) {
192 /* Still start < end. */
193 error = -ENOMEM;
194 if (!vma)
195 goto out;
196 /* Here start < vma->vm_end. */
197 if (start < vma->vm_start) {
198 unmapped_error = -ENOMEM;
199 start = vma->vm_start;
200 }
201 /* Here vma->vm_start <= start < vma->vm_end. */
202 if (end <= vma->vm_end) {
203 if (start < end) {
204 error = msync_interval(vma, start, end, flags);
205 if (error)
206 goto out;
207 }
208 error = unmapped_error;
209 goto out;
210 }
211 /* Here vma->vm_start <= start < vma->vm_end < end. */
212 error = msync_interval(vma, start, vma->vm_end, flags);
213 if (error)
214 goto out;
215 start = vma->vm_end;
216 vma = vma->vm_next;
217 }
218out:
219 up_read(&current->mm->mmap_sem);
220 current->flags &= ~PF_SYNCWRITE;
221 return error;
222}