blob: 17aaf3e1644983a5a4fac14bef8f009f71fe866c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Prasanna Meda05b74382005-06-21 17:14:37 -070011#include <linux/mempolicy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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 */
Prasanna Meda05b74382005-06-21 17:14:37 -070018static long madvise_behavior(struct vm_area_struct * vma,
19 struct vm_area_struct **prev,
20 unsigned long start, unsigned long end, int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
22 struct mm_struct * mm = vma->vm_mm;
23 int error = 0;
Prasanna Meda05b74382005-06-21 17:14:37 -070024 pgoff_t pgoff;
Prasanna Medae798c6e2005-06-21 17:14:36 -070025 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
Prasanna Meda05b74382005-06-21 17:14:37 -070038 if (new_flags == vma->vm_flags) {
39 *prev = vma;
Hugh Dickins836d5ff2005-09-03 15:54:53 -070040 goto out;
Prasanna Meda05b74382005-06-21 17:14:37 -070041 }
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
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
Hugh Dickins836d5ff2005-09-03 15:54:53 -070065success:
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 /*
67 * vm_flags is protected by the mmap_sem held in write mode.
68 */
Prasanna Medae798c6e2005-06-21 17:14:36 -070069 vma->vm_flags = new_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71out:
72 if (error == -ENOMEM)
73 error = -EAGAIN;
74 return error;
75}
76
77/*
78 * Schedule all required I/O operations. Do not wait for completion.
79 */
80static long madvise_willneed(struct vm_area_struct * vma,
Prasanna Meda05b74382005-06-21 17:14:37 -070081 struct vm_area_struct ** prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 unsigned long start, unsigned long end)
83{
84 struct file *file = vma->vm_file;
85
Suzuki1bef4002005-10-11 08:29:06 -070086 if (!file)
87 return -EBADF;
88
Carsten Ottefe77ba62005-06-23 22:05:29 -070089 if (file->f_mapping->a_ops->get_xip_page) {
90 /* no bad return value, but ignore advice */
91 return 0;
92 }
93
Prasanna Meda05b74382005-06-21 17:14:37 -070094 *prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
96 if (end > vma->vm_end)
97 end = vma->vm_end;
98 end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
99
100 force_page_cache_readahead(file->f_mapping,
101 file, start, max_sane_readahead(end - start));
102 return 0;
103}
104
105/*
106 * Application no longer needs these pages. If the pages are dirty,
107 * it's OK to just throw them away. The app will be more careful about
108 * data it wants to keep. Be sure to free swap resources too. The
109 * zap_page_range call sets things up for refill_inactive to actually free
110 * these pages later if no one else has touched them in the meantime,
111 * although we could add these pages to a global reuse list for
112 * refill_inactive to pick up before reclaiming other pages.
113 *
114 * NB: This interface discards data rather than pushes it out to swap,
115 * as some implementations do. This has performance implications for
116 * applications like large transactional databases which want to discard
117 * pages in anonymous maps after committing to backing store the data
118 * that was kept in them. There is no reason to write this data out to
119 * the swap area if the application is discarding it.
120 *
121 * An interface that causes the system to free clean pages and flush
122 * dirty pages is already available as msync(MS_INVALIDATE).
123 */
124static long madvise_dontneed(struct vm_area_struct * vma,
Prasanna Meda05b74382005-06-21 17:14:37 -0700125 struct vm_area_struct ** prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned long start, unsigned long end)
127{
Prasanna Meda05b74382005-06-21 17:14:37 -0700128 *prev = vma;
Nick Pigginb5810032005-10-29 18:16:12 -0700129 if (vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_RESERVED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -EINVAL;
131
132 if (unlikely(vma->vm_flags & VM_NONLINEAR)) {
133 struct zap_details details = {
134 .nonlinear_vma = vma,
135 .last_index = ULONG_MAX,
136 };
137 zap_page_range(vma, start, end - start, &details);
138 } else
139 zap_page_range(vma, start, end - start, NULL);
140 return 0;
141}
142
suzuki165cd402005-07-27 11:43:59 -0700143static long
144madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
145 unsigned long start, unsigned long end, int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Suzuki1bef4002005-10-11 08:29:06 -0700147 long error;
suzuki165cd402005-07-27 11:43:59 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 switch (behavior) {
150 case MADV_NORMAL:
151 case MADV_SEQUENTIAL:
152 case MADV_RANDOM:
Prasanna Meda05b74382005-06-21 17:14:37 -0700153 error = madvise_behavior(vma, prev, start, end, behavior);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 break;
155
156 case MADV_WILLNEED:
Prasanna Meda05b74382005-06-21 17:14:37 -0700157 error = madvise_willneed(vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 break;
159
160 case MADV_DONTNEED:
Prasanna Meda05b74382005-06-21 17:14:37 -0700161 error = madvise_dontneed(vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 break;
163
164 default:
165 error = -EINVAL;
166 break;
167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return error;
169}
170
171/*
172 * The madvise(2) system call.
173 *
174 * Applications can use madvise() to advise the kernel how it should
175 * handle paging I/O in this VM area. The idea is to help the kernel
176 * use appropriate read-ahead and caching techniques. The information
177 * provided is advisory only, and can be safely disregarded by the
178 * kernel without affecting the correct operation of the application.
179 *
180 * behavior values:
181 * MADV_NORMAL - the default behavior is to read clusters. This
182 * results in some read-ahead and read-behind.
183 * MADV_RANDOM - the system should read the minimum amount of data
184 * on any access, since it is unlikely that the appli-
185 * cation will need more than what it asks for.
186 * MADV_SEQUENTIAL - pages in the given range will probably be accessed
187 * once, so they can be aggressively read ahead, and
188 * can be freed soon after they are accessed.
189 * MADV_WILLNEED - the application is notifying the system to read
190 * some pages ahead.
191 * MADV_DONTNEED - the application is finished with the given range,
192 * so the kernel can free resources associated with it.
193 *
194 * return values:
195 * zero - success
196 * -EINVAL - start + len < 0, start is not page-aligned,
197 * "behavior" is not a valid value, or application
198 * is attempting to release locked or shared pages.
199 * -ENOMEM - addresses in the specified range are not currently
200 * mapped, or are outside the AS of the process.
201 * -EIO - an I/O error occurred while paging in data.
202 * -EBADF - map exists, but area maps something that isn't a file.
203 * -EAGAIN - a kernel resource was temporarily unavailable.
204 */
205asmlinkage long sys_madvise(unsigned long start, size_t len_in, int behavior)
206{
Prasanna Meda05b74382005-06-21 17:14:37 -0700207 unsigned long end, tmp;
208 struct vm_area_struct * vma, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 int unmapped_error = 0;
210 int error = -EINVAL;
211 size_t len;
212
213 down_write(&current->mm->mmap_sem);
214
215 if (start & ~PAGE_MASK)
216 goto out;
217 len = (len_in + ~PAGE_MASK) & PAGE_MASK;
218
219 /* Check to see whether len was rounded up from small -ve to zero */
220 if (len_in && !len)
221 goto out;
222
223 end = start + len;
224 if (end < start)
225 goto out;
226
227 error = 0;
228 if (end == start)
229 goto out;
230
231 /*
232 * If the interval [start,end) covers some unmapped address
233 * ranges, just ignore them, but return -ENOMEM at the end.
Prasanna Meda05b74382005-06-21 17:14:37 -0700234 * - different from the way of handling in mlock etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
Prasanna Meda05b74382005-06-21 17:14:37 -0700236 vma = find_vma_prev(current->mm, start, &prev);
Hugh Dickins836d5ff2005-09-03 15:54:53 -0700237 if (vma && start > vma->vm_start)
238 prev = vma;
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 for (;;) {
241 /* Still start < end. */
242 error = -ENOMEM;
243 if (!vma)
244 goto out;
245
Prasanna Meda05b74382005-06-21 17:14:37 -0700246 /* Here start < (end|vma->vm_end). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (start < vma->vm_start) {
248 unmapped_error = -ENOMEM;
249 start = vma->vm_start;
Prasanna Meda05b74382005-06-21 17:14:37 -0700250 if (start >= end)
251 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
Prasanna Meda05b74382005-06-21 17:14:37 -0700254 /* Here vma->vm_start <= start < (end|vma->vm_end) */
255 tmp = vma->vm_end;
256 if (end < tmp)
257 tmp = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Prasanna Meda05b74382005-06-21 17:14:37 -0700259 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
260 error = madvise_vma(vma, &prev, start, tmp, behavior);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (error)
262 goto out;
Prasanna Meda05b74382005-06-21 17:14:37 -0700263 start = tmp;
264 if (start < prev->vm_end)
265 start = prev->vm_end;
266 error = unmapped_error;
267 if (start >= end)
268 goto out;
269 vma = prev->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271out:
272 up_write(&current->mm->mmap_sem);
273 return error;
274}