blob: 1b5b6f662dcfd40e3c27122a58c2c1a17e93e295 [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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 struct page *page;
31
Hugh Dickins0c942a42005-10-29 18:15:53 -070032 if (progress >= 64) {
33 progress = 0;
Hugh Dickins705e87c2005-10-29 18:16:27 -070034 if (need_resched() || need_lockbreak(ptl))
Hugh Dickins0c942a42005-10-29 18:15:53 -070035 break;
36 }
37 progress++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 if (!pte_present(*pte))
39 continue;
Abhijit Karmarkarb4955ce2005-06-21 17:15:13 -070040 if (!pte_maybe_dirty(*pte))
41 continue;
Linus Torvalds6aab3412005-11-28 14:34:23 -080042 page = vm_normal_page(vma, addr, *pte);
43 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (ptep_clear_flush_dirty(vma, addr, pte) ||
46 page_test_and_clear_dirty(page))
47 set_page_dirty(page);
Hugh Dickins0c942a42005-10-29 18:15:53 -070048 progress += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickins705e87c2005-10-29 18:16:27 -070050 pte_unmap_unlock(pte - 1, ptl);
51 cond_resched();
Hugh Dickins0c942a42005-10-29 18:15:53 -070052 if (addr != end)
53 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070056static inline void msync_pmd_range(struct vm_area_struct *vma, pud_t *pud,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 unsigned long addr, unsigned long end)
58{
59 pmd_t *pmd;
60 unsigned long next;
61
62 pmd = pmd_offset(pud, addr);
63 do {
64 next = pmd_addr_end(addr, end);
65 if (pmd_none_or_clear_bad(pmd))
66 continue;
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070067 msync_pte_range(vma, pmd, addr, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 } while (pmd++, addr = next, addr != end);
69}
70
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070071static inline void msync_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 unsigned long addr, unsigned long end)
73{
74 pud_t *pud;
75 unsigned long next;
76
77 pud = pud_offset(pgd, addr);
78 do {
79 next = pud_addr_end(addr, end);
80 if (pud_none_or_clear_bad(pud))
81 continue;
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070082 msync_pmd_range(vma, pud, addr, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 } while (pud++, addr = next, addr != end);
84}
85
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -070086static void msync_page_range(struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 unsigned long addr, unsigned long end)
88{
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 pgd_t *pgd;
90 unsigned long next;
91
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
Nick Pigginb5810032005-10-29 18:16:12 -070094 * to do anything more on an msync().
Nick Pigginb5810032005-10-29 18:16:12 -070095 */
Linus Torvalds6aab3412005-11-28 14:34:23 -080096 if (vma->vm_flags & VM_HUGETLB)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return;
98
99 BUG_ON(addr >= end);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700100 pgd = pgd_offset(vma->vm_mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 flush_cache_range(vma, addr, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 do {
103 next = pgd_addr_end(addr, end);
104 if (pgd_none_or_clear_bad(pgd))
105 continue;
OGAWA Hirofumib57b98d2005-10-29 18:15:50 -0700106 msync_pud_range(vma, pgd, addr, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 } while (pgd++, addr = next, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
111 * MS_SYNC syncs the entire file - including mappings.
112 *
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
119 * applications.
120 */
121static int msync_interval(struct vm_area_struct *vma,
122 unsigned long addr, unsigned long end, int flags)
123{
124 int ret = 0;
125 struct file *file = vma->vm_file;
126
127 if ((flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED))
128 return -EBUSY;
129
130 if (file && (vma->vm_flags & VM_SHARED)) {
Hugh Dickins0c942a42005-10-29 18:15:53 -0700131 msync_page_range(vma, addr, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 if (flags & MS_SYNC) {
134 struct address_space *mapping = file->f_mapping;
135 int err;
136
137 ret = filemap_fdatawrite(mapping);
138 if (file->f_op && file->f_op->fsync) {
139 /*
140 * We don't take i_sem here because mmap_sem
141 * is already held.
142 */
143 err = file->f_op->fsync(file,file->f_dentry,1);
144 if (err && !ret)
145 ret = err;
146 }
147 err = filemap_fdatawait(mapping);
148 if (!ret)
149 ret = err;
150 }
151 }
152 return ret;
153}
154
155asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
156{
157 unsigned long end;
158 struct vm_area_struct *vma;
159 int unmapped_error, error = -EINVAL;
160
161 if (flags & MS_SYNC)
162 current->flags |= PF_SYNCWRITE;
163
164 down_read(&current->mm->mmap_sem);
165 if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
166 goto out;
167 if (start & ~PAGE_MASK)
168 goto out;
169 if ((flags & MS_ASYNC) && (flags & MS_SYNC))
170 goto out;
171 error = -ENOMEM;
172 len = (len + ~PAGE_MASK) & PAGE_MASK;
173 end = start + len;
174 if (end < start)
175 goto out;
176 error = 0;
177 if (end == start)
178 goto out;
179 /*
180 * If the interval [start,end) covers some unmapped address ranges,
181 * just ignore them, but return -ENOMEM at the end.
182 */
183 vma = find_vma(current->mm, start);
184 unmapped_error = 0;
185 for (;;) {
186 /* Still start < end. */
187 error = -ENOMEM;
188 if (!vma)
189 goto out;
190 /* Here start < vma->vm_end. */
191 if (start < vma->vm_start) {
192 unmapped_error = -ENOMEM;
193 start = vma->vm_start;
194 }
195 /* Here vma->vm_start <= start < vma->vm_end. */
196 if (end <= vma->vm_end) {
197 if (start < end) {
198 error = msync_interval(vma, start, end, flags);
199 if (error)
200 goto out;
201 }
202 error = unmapped_error;
203 goto out;
204 }
205 /* Here vma->vm_start <= start < vma->vm_end < end. */
206 error = msync_interval(vma, start, vma->vm_end, flags);
207 if (error)
208 goto out;
209 start = vma->vm_end;
210 vma = vma->vm_next;
211 }
212out:
213 up_read(&current->mm->mmap_sem);
214 current->flags &= ~PF_SYNCWRITE;
215 return error;
216}