[PATCH] mm: msync_pte_range progress
[safe/jmp/linux-2.6] / mm / msync.c
1 /*
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
25 static void msync_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
26                                 unsigned long addr, unsigned long end)
27 {
28         pte_t *pte;
29         int progress = 0;
30
31 again:
32         pte = pte_offset_map(pmd, addr);
33         do {
34                 unsigned long pfn;
35                 struct page *page;
36
37                 if (progress >= 64) {
38                         progress = 0;
39                         if (need_resched() ||
40                             need_lockbreak(&vma->vm_mm->page_table_lock))
41                                 break;
42                 }
43                 progress++;
44                 if (!pte_present(*pte))
45                         continue;
46                 if (!pte_maybe_dirty(*pte))
47                         continue;
48                 pfn = pte_pfn(*pte);
49                 if (!pfn_valid(pfn))
50                         continue;
51                 page = pfn_to_page(pfn);
52                 if (PageReserved(page))
53                         continue;
54
55                 if (ptep_clear_flush_dirty(vma, addr, pte) ||
56                     page_test_and_clear_dirty(page))
57                         set_page_dirty(page);
58                 progress += 3;
59         } while (pte++, addr += PAGE_SIZE, addr != end);
60         pte_unmap(pte - 1);
61         cond_resched_lock(&vma->vm_mm->page_table_lock);
62         if (addr != end)
63                 goto again;
64 }
65
66 static inline void msync_pmd_range(struct vm_area_struct *vma, pud_t *pud,
67                                 unsigned long addr, unsigned long end)
68 {
69         pmd_t *pmd;
70         unsigned long next;
71
72         pmd = pmd_offset(pud, addr);
73         do {
74                 next = pmd_addr_end(addr, end);
75                 if (pmd_none_or_clear_bad(pmd))
76                         continue;
77                 msync_pte_range(vma, pmd, addr, next);
78         } while (pmd++, addr = next, addr != end);
79 }
80
81 static inline void msync_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
82                                 unsigned long addr, unsigned long end)
83 {
84         pud_t *pud;
85         unsigned long next;
86
87         pud = pud_offset(pgd, addr);
88         do {
89                 next = pud_addr_end(addr, end);
90                 if (pud_none_or_clear_bad(pud))
91                         continue;
92                 msync_pmd_range(vma, pud, addr, next);
93         } while (pud++, addr = next, addr != end);
94 }
95
96 static void msync_page_range(struct vm_area_struct *vma,
97                                 unsigned long addr, unsigned long end)
98 {
99         struct mm_struct *mm = vma->vm_mm;
100         pgd_t *pgd;
101         unsigned long next;
102
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))
107                 return;
108
109         BUG_ON(addr >= end);
110         pgd = pgd_offset(mm, addr);
111         flush_cache_range(vma, addr, end);
112         spin_lock(&mm->page_table_lock);
113         do {
114                 next = pgd_addr_end(addr, end);
115                 if (pgd_none_or_clear_bad(pgd))
116                         continue;
117                 msync_pud_range(vma, pgd, addr, next);
118         } while (pgd++, addr = next, addr != end);
119         spin_unlock(&mm->page_table_lock);
120 }
121
122 /*
123  * MS_SYNC syncs the entire file - including mappings.
124  *
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
131  * applications.
132  */
133 static int msync_interval(struct vm_area_struct *vma,
134                         unsigned long addr, unsigned long end, int flags)
135 {
136         int ret = 0;
137         struct file *file = vma->vm_file;
138
139         if ((flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED))
140                 return -EBUSY;
141
142         if (file && (vma->vm_flags & VM_SHARED)) {
143                 msync_page_range(vma, addr, end);
144
145                 if (flags & MS_SYNC) {
146                         struct address_space *mapping = file->f_mapping;
147                         int err;
148
149                         ret = filemap_fdatawrite(mapping);
150                         if (file->f_op && file->f_op->fsync) {
151                                 /*
152                                  * We don't take i_sem here because mmap_sem
153                                  * is already held.
154                                  */
155                                 err = file->f_op->fsync(file,file->f_dentry,1);
156                                 if (err && !ret)
157                                         ret = err;
158                         }
159                         err = filemap_fdatawait(mapping);
160                         if (!ret)
161                                 ret = err;
162                 }
163         }
164         return ret;
165 }
166
167 asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
168 {
169         unsigned long end;
170         struct vm_area_struct *vma;
171         int unmapped_error, error = -EINVAL;
172
173         if (flags & MS_SYNC)
174                 current->flags |= PF_SYNCWRITE;
175
176         down_read(&current->mm->mmap_sem);
177         if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
178                 goto out;
179         if (start & ~PAGE_MASK)
180                 goto out;
181         if ((flags & MS_ASYNC) && (flags & MS_SYNC))
182                 goto out;
183         error = -ENOMEM;
184         len = (len + ~PAGE_MASK) & PAGE_MASK;
185         end = start + len;
186         if (end < start)
187                 goto out;
188         error = 0;
189         if (end == start)
190                 goto out;
191         /*
192          * If the interval [start,end) covers some unmapped address ranges,
193          * just ignore them, but return -ENOMEM at the end.
194          */
195         vma = find_vma(current->mm, start);
196         unmapped_error = 0;
197         for (;;) {
198                 /* Still start < end. */
199                 error = -ENOMEM;
200                 if (!vma)
201                         goto out;
202                 /* Here start < vma->vm_end. */
203                 if (start < vma->vm_start) {
204                         unmapped_error = -ENOMEM;
205                         start = vma->vm_start;
206                 }
207                 /* Here vma->vm_start <= start < vma->vm_end. */
208                 if (end <= vma->vm_end) {
209                         if (start < end) {
210                                 error = msync_interval(vma, start, end, flags);
211                                 if (error)
212                                         goto out;
213                         }
214                         error = unmapped_error;
215                         goto out;
216                 }
217                 /* Here vma->vm_start <= start < vma->vm_end < end. */
218                 error = msync_interval(vma, start, vma->vm_end, flags);
219                 if (error)
220                         goto out;
221                 start = vma->vm_end;
222                 vma = vma->vm_next;
223         }
224 out:
225         up_read(&current->mm->mmap_sem);
226         current->flags &= ~PF_SYNCWRITE;
227         return error;
228 }