[PATCH] remove redundant vm_flags clearing from madvise.c
[safe/jmp/linux-2.6] / mm / madvise.c
1 /*
2  *      linux/mm/madvise.c
3  *
4  * Copyright (C) 1999  Linus Torvalds
5  * Copyright (C) 2002  Christoph Hellwig
6  */
7
8 #include <linux/mman.h>
9 #include <linux/pagemap.h>
10 #include <linux/syscalls.h>
11 #include <linux/mempolicy.h>
12 #include <linux/hugetlb.h>
13
14 /*
15  * We can potentially split a vm area into separate
16  * areas, each area with its own behavior.
17  */
18 static long madvise_behavior(struct vm_area_struct * vma,
19                      struct vm_area_struct **prev,
20                      unsigned long start, unsigned long end, int behavior)
21 {
22         struct mm_struct * mm = vma->vm_mm;
23         int error = 0;
24         pgoff_t pgoff;
25         int new_flags = vma->vm_flags & ~VM_READHINTMASK;
26
27         switch (behavior) {
28         case MADV_SEQUENTIAL:
29                 new_flags |= VM_SEQ_READ;
30                 break;
31         case MADV_RANDOM:
32                 new_flags |= VM_RAND_READ;
33                 break;
34         default:
35                 break;
36         }
37
38         if (new_flags == vma->vm_flags) {
39                 *prev = vma;
40                 goto success;
41         }
42
43         pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
44         *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
45                                 vma->vm_file, pgoff, vma_policy(vma));
46         if (*prev) {
47                 vma = *prev;
48                 goto success;
49         }
50
51         *prev = vma;
52
53         if (start != vma->vm_start) {
54                 error = split_vma(mm, vma, start, 1);
55                 if (error)
56                         goto out;
57         }
58
59         if (end != vma->vm_end) {
60                 error = split_vma(mm, vma, end, 0);
61                 if (error)
62                         goto out;
63         }
64
65         /*
66          * vm_flags is protected by the mmap_sem held in write mode.
67          */
68         vma->vm_flags = new_flags;
69
70 out:
71         if (error == -ENOMEM)
72                 error = -EAGAIN;
73 success:
74         return error;
75 }
76
77 /*
78  * Schedule all required I/O operations.  Do not wait for completion.
79  */
80 static long madvise_willneed(struct vm_area_struct * vma,
81                              struct vm_area_struct ** prev,
82                              unsigned long start, unsigned long end)
83 {
84         struct file *file = vma->vm_file;
85
86         if (!file)
87                 return -EBADF;
88
89         *prev = vma;
90         start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
91         if (end > vma->vm_end)
92                 end = vma->vm_end;
93         end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
94
95         force_page_cache_readahead(file->f_mapping,
96                         file, start, max_sane_readahead(end - start));
97         return 0;
98 }
99
100 /*
101  * Application no longer needs these pages.  If the pages are dirty,
102  * it's OK to just throw them away.  The app will be more careful about
103  * data it wants to keep.  Be sure to free swap resources too.  The
104  * zap_page_range call sets things up for refill_inactive to actually free
105  * these pages later if no one else has touched them in the meantime,
106  * although we could add these pages to a global reuse list for
107  * refill_inactive to pick up before reclaiming other pages.
108  *
109  * NB: This interface discards data rather than pushes it out to swap,
110  * as some implementations do.  This has performance implications for
111  * applications like large transactional databases which want to discard
112  * pages in anonymous maps after committing to backing store the data
113  * that was kept in them.  There is no reason to write this data out to
114  * the swap area if the application is discarding it.
115  *
116  * An interface that causes the system to free clean pages and flush
117  * dirty pages is already available as msync(MS_INVALIDATE).
118  */
119 static long madvise_dontneed(struct vm_area_struct * vma,
120                              struct vm_area_struct ** prev,
121                              unsigned long start, unsigned long end)
122 {
123         *prev = vma;
124         if ((vma->vm_flags & VM_LOCKED) || is_vm_hugetlb_page(vma))
125                 return -EINVAL;
126
127         if (unlikely(vma->vm_flags & VM_NONLINEAR)) {
128                 struct zap_details details = {
129                         .nonlinear_vma = vma,
130                         .last_index = ULONG_MAX,
131                 };
132                 zap_page_range(vma, start, end - start, &details);
133         } else
134                 zap_page_range(vma, start, end - start, NULL);
135         return 0;
136 }
137
138 static long madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
139                         unsigned long start, unsigned long end, int behavior)
140 {
141         long error = -EBADF;
142
143         switch (behavior) {
144         case MADV_NORMAL:
145         case MADV_SEQUENTIAL:
146         case MADV_RANDOM:
147                 error = madvise_behavior(vma, prev, start, end, behavior);
148                 break;
149
150         case MADV_WILLNEED:
151                 error = madvise_willneed(vma, prev, start, end);
152                 break;
153
154         case MADV_DONTNEED:
155                 error = madvise_dontneed(vma, prev, start, end);
156                 break;
157
158         default:
159                 error = -EINVAL;
160                 break;
161         }
162                 
163         return error;
164 }
165
166 /*
167  * The madvise(2) system call.
168  *
169  * Applications can use madvise() to advise the kernel how it should
170  * handle paging I/O in this VM area.  The idea is to help the kernel
171  * use appropriate read-ahead and caching techniques.  The information
172  * provided is advisory only, and can be safely disregarded by the
173  * kernel without affecting the correct operation of the application.
174  *
175  * behavior values:
176  *  MADV_NORMAL - the default behavior is to read clusters.  This
177  *              results in some read-ahead and read-behind.
178  *  MADV_RANDOM - the system should read the minimum amount of data
179  *              on any access, since it is unlikely that the appli-
180  *              cation will need more than what it asks for.
181  *  MADV_SEQUENTIAL - pages in the given range will probably be accessed
182  *              once, so they can be aggressively read ahead, and
183  *              can be freed soon after they are accessed.
184  *  MADV_WILLNEED - the application is notifying the system to read
185  *              some pages ahead.
186  *  MADV_DONTNEED - the application is finished with the given range,
187  *              so the kernel can free resources associated with it.
188  *
189  * return values:
190  *  zero    - success
191  *  -EINVAL - start + len < 0, start is not page-aligned,
192  *              "behavior" is not a valid value, or application
193  *              is attempting to release locked or shared pages.
194  *  -ENOMEM - addresses in the specified range are not currently
195  *              mapped, or are outside the AS of the process.
196  *  -EIO    - an I/O error occurred while paging in data.
197  *  -EBADF  - map exists, but area maps something that isn't a file.
198  *  -EAGAIN - a kernel resource was temporarily unavailable.
199  */
200 asmlinkage long sys_madvise(unsigned long start, size_t len_in, int behavior)
201 {
202         unsigned long end, tmp;
203         struct vm_area_struct * vma, *prev;
204         int unmapped_error = 0;
205         int error = -EINVAL;
206         size_t len;
207
208         down_write(&current->mm->mmap_sem);
209
210         if (start & ~PAGE_MASK)
211                 goto out;
212         len = (len_in + ~PAGE_MASK) & PAGE_MASK;
213
214         /* Check to see whether len was rounded up from small -ve to zero */
215         if (len_in && !len)
216                 goto out;
217
218         end = start + len;
219         if (end < start)
220                 goto out;
221
222         error = 0;
223         if (end == start)
224                 goto out;
225
226         /*
227          * If the interval [start,end) covers some unmapped address
228          * ranges, just ignore them, but return -ENOMEM at the end.
229          * - different from the way of handling in mlock etc.
230          */
231         vma = find_vma_prev(current->mm, start, &prev);
232         if (!vma && prev)
233                 vma = prev->vm_next;
234         for (;;) {
235                 /* Still start < end. */
236                 error = -ENOMEM;
237                 if (!vma)
238                         goto out;
239
240                 /* Here start < (end|vma->vm_end). */
241                 if (start < vma->vm_start) {
242                         unmapped_error = -ENOMEM;
243                         start = vma->vm_start;
244                         if (start >= end)
245                                 goto out;
246                 }
247
248                 /* Here vma->vm_start <= start < (end|vma->vm_end) */
249                 tmp = vma->vm_end;
250                 if (end < tmp)
251                         tmp = end;
252
253                 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
254                 error = madvise_vma(vma, &prev, start, tmp, behavior);
255                 if (error)
256                         goto out;
257                 start = tmp;
258                 if (start < prev->vm_end)
259                         start = prev->vm_end;
260                 error = unmapped_error;
261                 if (start >= end)
262                         goto out;
263                 vma = prev->vm_next;
264         }
265 out:
266         up_write(&current->mm->mmap_sem);
267         return error;
268 }