blob: ecde40a401c1911df933949c26e35529e446cbcc [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>
Andi Kleenafcf9382009-12-16 12:20:00 +010012#include <linux/page-isolation.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/hugetlb.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/sched.h>
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070015#include <linux/ksm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/*
Nick Piggin0a27a142007-05-06 14:49:53 -070018 * Any behaviour which results in changes to the vma->vm_flags needs to
19 * take mmap_sem for writing. Others, which simply traverse vmas, need
20 * to only take it for reading.
21 */
22static int madvise_need_mmap_write(int behavior)
23{
24 switch (behavior) {
25 case MADV_REMOVE:
26 case MADV_WILLNEED:
27 case MADV_DONTNEED:
28 return 0;
29 default:
30 /* be safe, default to 1. list exceptions explicitly */
31 return 1;
32 }
33}
34
35/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * We can potentially split a vm area into separate
37 * areas, each area with its own behavior.
38 */
Prasanna Meda05b74382005-06-21 17:14:37 -070039static long madvise_behavior(struct vm_area_struct * vma,
40 struct vm_area_struct **prev,
41 unsigned long start, unsigned long end, int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 struct mm_struct * mm = vma->vm_mm;
44 int error = 0;
Prasanna Meda05b74382005-06-21 17:14:37 -070045 pgoff_t pgoff;
Hugh Dickins3866ea92009-09-21 17:01:52 -070046 unsigned long new_flags = vma->vm_flags;
Prasanna Medae798c6e2005-06-21 17:14:36 -070047
48 switch (behavior) {
Michael S. Tsirkinf8225662006-02-14 13:53:08 -080049 case MADV_NORMAL:
50 new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
51 break;
Prasanna Medae798c6e2005-06-21 17:14:36 -070052 case MADV_SEQUENTIAL:
Michael S. Tsirkinf8225662006-02-14 13:53:08 -080053 new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
Prasanna Medae798c6e2005-06-21 17:14:36 -070054 break;
55 case MADV_RANDOM:
Michael S. Tsirkinf8225662006-02-14 13:53:08 -080056 new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
Prasanna Medae798c6e2005-06-21 17:14:36 -070057 break;
Michael S. Tsirkinf8225662006-02-14 13:53:08 -080058 case MADV_DONTFORK:
59 new_flags |= VM_DONTCOPY;
60 break;
61 case MADV_DOFORK:
Hugh Dickins3866ea92009-09-21 17:01:52 -070062 if (vma->vm_flags & VM_IO) {
63 error = -EINVAL;
64 goto out;
65 }
Michael S. Tsirkinf8225662006-02-14 13:53:08 -080066 new_flags &= ~VM_DONTCOPY;
Prasanna Medae798c6e2005-06-21 17:14:36 -070067 break;
Hugh Dickinsf8af4da2009-09-21 17:01:57 -070068 case MADV_MERGEABLE:
69 case MADV_UNMERGEABLE:
70 error = ksm_madvise(vma, start, end, behavior, &new_flags);
71 if (error)
72 goto out;
73 break;
Andrea Arcangeli0af4e982011-01-13 15:46:55 -080074 case MADV_HUGEPAGE:
75 error = hugepage_madvise(&new_flags);
76 if (error)
77 goto out;
78 break;
Prasanna Medae798c6e2005-06-21 17:14:36 -070079 }
80
Prasanna Meda05b74382005-06-21 17:14:37 -070081 if (new_flags == vma->vm_flags) {
82 *prev = vma;
Hugh Dickins836d5ff2005-09-03 15:54:53 -070083 goto out;
Prasanna Meda05b74382005-06-21 17:14:37 -070084 }
85
86 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
87 *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
88 vma->vm_file, pgoff, vma_policy(vma));
89 if (*prev) {
90 vma = *prev;
91 goto success;
92 }
93
94 *prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 if (start != vma->vm_start) {
97 error = split_vma(mm, vma, start, 1);
98 if (error)
99 goto out;
100 }
101
102 if (end != vma->vm_end) {
103 error = split_vma(mm, vma, end, 0);
104 if (error)
105 goto out;
106 }
107
Hugh Dickins836d5ff2005-09-03 15:54:53 -0700108success:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /*
110 * vm_flags is protected by the mmap_sem held in write mode.
111 */
Prasanna Medae798c6e2005-06-21 17:14:36 -0700112 vma->vm_flags = new_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114out:
115 if (error == -ENOMEM)
116 error = -EAGAIN;
117 return error;
118}
119
120/*
121 * Schedule all required I/O operations. Do not wait for completion.
122 */
123static long madvise_willneed(struct vm_area_struct * vma,
Prasanna Meda05b74382005-06-21 17:14:37 -0700124 struct vm_area_struct ** prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned long start, unsigned long end)
126{
127 struct file *file = vma->vm_file;
128
Suzuki1bef4002005-10-11 08:29:06 -0700129 if (!file)
130 return -EBADF;
131
Nick Piggin70688e42008-04-28 02:13:02 -0700132 if (file->f_mapping->a_ops->get_xip_mem) {
Carsten Ottefe77ba62005-06-23 22:05:29 -0700133 /* no bad return value, but ignore advice */
134 return 0;
135 }
136
Prasanna Meda05b74382005-06-21 17:14:37 -0700137 *prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
139 if (end > vma->vm_end)
140 end = vma->vm_end;
141 end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
142
Wu Fengguangf7e839d2009-06-16 15:31:20 -0700143 force_page_cache_readahead(file->f_mapping, file, start, end - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return 0;
145}
146
147/*
148 * Application no longer needs these pages. If the pages are dirty,
149 * it's OK to just throw them away. The app will be more careful about
150 * data it wants to keep. Be sure to free swap resources too. The
Fernando Luis Vazquez Cao7e6cbea2008-07-29 22:33:39 -0700151 * zap_page_range call sets things up for shrink_active_list to actually free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * these pages later if no one else has touched them in the meantime,
153 * although we could add these pages to a global reuse list for
Fernando Luis Vazquez Cao7e6cbea2008-07-29 22:33:39 -0700154 * shrink_active_list to pick up before reclaiming other pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 *
156 * NB: This interface discards data rather than pushes it out to swap,
157 * as some implementations do. This has performance implications for
158 * applications like large transactional databases which want to discard
159 * pages in anonymous maps after committing to backing store the data
160 * that was kept in them. There is no reason to write this data out to
161 * the swap area if the application is discarding it.
162 *
163 * An interface that causes the system to free clean pages and flush
164 * dirty pages is already available as msync(MS_INVALIDATE).
165 */
166static long madvise_dontneed(struct vm_area_struct * vma,
Prasanna Meda05b74382005-06-21 17:14:37 -0700167 struct vm_area_struct ** prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned long start, unsigned long end)
169{
Prasanna Meda05b74382005-06-21 17:14:37 -0700170 *prev = vma;
Linus Torvalds6aab3412005-11-28 14:34:23 -0800171 if (vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return -EINVAL;
173
174 if (unlikely(vma->vm_flags & VM_NONLINEAR)) {
175 struct zap_details details = {
176 .nonlinear_vma = vma,
177 .last_index = ULONG_MAX,
178 };
179 zap_page_range(vma, start, end - start, &details);
180 } else
181 zap_page_range(vma, start, end - start, NULL);
182 return 0;
183}
184
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800185/*
186 * Application wants to free up the pages and associated backing store.
187 * This is effectively punching a hole into the middle of a file.
188 *
189 * NOTE: Currently, only shmfs/tmpfs is supported for this operation.
190 * Other filesystems return -ENOSYS.
191 */
192static long madvise_remove(struct vm_area_struct *vma,
Nick Piggin00e9fa22007-03-16 13:38:10 -0800193 struct vm_area_struct **prev,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800194 unsigned long start, unsigned long end)
195{
196 struct address_space *mapping;
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700197 loff_t offset, endoff;
198 int error;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800199
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700200 *prev = NULL; /* tell sys_madvise we drop mmap_sem */
Nick Piggin00e9fa22007-03-16 13:38:10 -0800201
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800202 if (vma->vm_flags & (VM_LOCKED|VM_NONLINEAR|VM_HUGETLB))
203 return -EINVAL;
204
205 if (!vma->vm_file || !vma->vm_file->f_mapping
206 || !vma->vm_file->f_mapping->host) {
207 return -EINVAL;
208 }
209
Hugh Dickins69cf0fa2006-04-17 22:46:32 +0100210 if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
211 return -EACCES;
212
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800213 mapping = vma->vm_file->f_mapping;
214
215 offset = (loff_t)(start - vma->vm_start)
216 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
217 endoff = (loff_t)(end - vma->vm_start - 1)
218 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700219
220 /* vmtruncate_range needs to take i_mutex and i_alloc_sem */
Nick Piggin0a27a142007-05-06 14:49:53 -0700221 up_read(&current->mm->mmap_sem);
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700222 error = vmtruncate_range(mapping->host, offset, endoff);
Nick Piggin0a27a142007-05-06 14:49:53 -0700223 down_read(&current->mm->mmap_sem);
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700224 return error;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800225}
226
Andi Kleen9893e492009-09-16 11:50:17 +0200227#ifdef CONFIG_MEMORY_FAILURE
228/*
229 * Error injection support for memory error handling.
230 */
Andi Kleenafcf9382009-12-16 12:20:00 +0100231static int madvise_hwpoison(int bhv, unsigned long start, unsigned long end)
Andi Kleen9893e492009-09-16 11:50:17 +0200232{
233 int ret = 0;
234
235 if (!capable(CAP_SYS_ADMIN))
236 return -EPERM;
237 for (; start < end; start += PAGE_SIZE) {
238 struct page *p;
Andi Kleend15f1072009-12-16 12:20:00 +0100239 int ret = get_user_pages_fast(start, 1, 0, &p);
Andi Kleen9893e492009-09-16 11:50:17 +0200240 if (ret != 1)
241 return ret;
Andi Kleenafcf9382009-12-16 12:20:00 +0100242 if (bhv == MADV_SOFT_OFFLINE) {
243 printk(KERN_INFO "Soft offlining page %lx at %lx\n",
244 page_to_pfn(p), start);
245 ret = soft_offline_page(p, MF_COUNT_INCREASED);
246 if (ret)
247 break;
248 continue;
249 }
Andi Kleen9893e492009-09-16 11:50:17 +0200250 printk(KERN_INFO "Injecting memory failure for page %lx at %lx\n",
251 page_to_pfn(p), start);
252 /* Ignore return value for now */
Andi Kleen82ba0112009-12-16 12:19:57 +0100253 __memory_failure(page_to_pfn(p), 0, MF_COUNT_INCREASED);
Andi Kleen9893e492009-09-16 11:50:17 +0200254 }
255 return ret;
256}
257#endif
258
suzuki165cd402005-07-27 11:43:59 -0700259static long
260madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
261 unsigned long start, unsigned long end, int behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 switch (behavior) {
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800264 case MADV_REMOVE:
Hugh Dickins3866ea92009-09-21 17:01:52 -0700265 return madvise_remove(vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 case MADV_WILLNEED:
Hugh Dickins3866ea92009-09-21 17:01:52 -0700267 return madvise_willneed(vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 case MADV_DONTNEED:
Hugh Dickins3866ea92009-09-21 17:01:52 -0700269 return madvise_dontneed(vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 default:
Hugh Dickins3866ea92009-09-21 17:01:52 -0700271 return madvise_behavior(vma, prev, start, end, behavior);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Nick Piggin75927af2009-06-16 15:32:38 -0700275static int
276madvise_behavior_valid(int behavior)
277{
278 switch (behavior) {
279 case MADV_DOFORK:
280 case MADV_DONTFORK:
281 case MADV_NORMAL:
282 case MADV_SEQUENTIAL:
283 case MADV_RANDOM:
284 case MADV_REMOVE:
285 case MADV_WILLNEED:
286 case MADV_DONTNEED:
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700287#ifdef CONFIG_KSM
288 case MADV_MERGEABLE:
289 case MADV_UNMERGEABLE:
290#endif
Andrea Arcangeli0af4e982011-01-13 15:46:55 -0800291#ifdef CONFIG_TRANSPARENT_HUGEPAGE
292 case MADV_HUGEPAGE:
293#endif
Nick Piggin75927af2009-06-16 15:32:38 -0700294 return 1;
295
296 default:
297 return 0;
298 }
299}
Hugh Dickins3866ea92009-09-21 17:01:52 -0700300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/*
302 * The madvise(2) system call.
303 *
304 * Applications can use madvise() to advise the kernel how it should
305 * handle paging I/O in this VM area. The idea is to help the kernel
306 * use appropriate read-ahead and caching techniques. The information
307 * provided is advisory only, and can be safely disregarded by the
308 * kernel without affecting the correct operation of the application.
309 *
310 * behavior values:
311 * MADV_NORMAL - the default behavior is to read clusters. This
312 * results in some read-ahead and read-behind.
313 * MADV_RANDOM - the system should read the minimum amount of data
314 * on any access, since it is unlikely that the appli-
315 * cation will need more than what it asks for.
316 * MADV_SEQUENTIAL - pages in the given range will probably be accessed
317 * once, so they can be aggressively read ahead, and
318 * can be freed soon after they are accessed.
319 * MADV_WILLNEED - the application is notifying the system to read
320 * some pages ahead.
321 * MADV_DONTNEED - the application is finished with the given range,
322 * so the kernel can free resources associated with it.
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800323 * MADV_REMOVE - the application wants to free up the given range of
324 * pages and associated backing store.
Hugh Dickins3866ea92009-09-21 17:01:52 -0700325 * MADV_DONTFORK - omit this area from child's address space when forking:
326 * typically, to avoid COWing pages pinned by get_user_pages().
327 * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
Hugh Dickinsf8af4da2009-09-21 17:01:57 -0700328 * MADV_MERGEABLE - the application recommends that KSM try to merge pages in
329 * this area with pages of identical content from other such areas.
330 * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 *
332 * return values:
333 * zero - success
334 * -EINVAL - start + len < 0, start is not page-aligned,
335 * "behavior" is not a valid value, or application
336 * is attempting to release locked or shared pages.
337 * -ENOMEM - addresses in the specified range are not currently
338 * mapped, or are outside the AS of the process.
339 * -EIO - an I/O error occurred while paging in data.
340 * -EBADF - map exists, but area maps something that isn't a file.
341 * -EAGAIN - a kernel resource was temporarily unavailable.
342 */
Heiko Carstens3480b252009-01-14 14:14:16 +0100343SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Prasanna Meda05b74382005-06-21 17:14:37 -0700345 unsigned long end, tmp;
346 struct vm_area_struct * vma, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int unmapped_error = 0;
348 int error = -EINVAL;
Jason Baronf7977792007-07-15 23:38:21 -0700349 int write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 size_t len;
351
Andi Kleen9893e492009-09-16 11:50:17 +0200352#ifdef CONFIG_MEMORY_FAILURE
Andi Kleenafcf9382009-12-16 12:20:00 +0100353 if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
354 return madvise_hwpoison(behavior, start, start+len_in);
Andi Kleen9893e492009-09-16 11:50:17 +0200355#endif
Nick Piggin75927af2009-06-16 15:32:38 -0700356 if (!madvise_behavior_valid(behavior))
357 return error;
358
Jason Baronf7977792007-07-15 23:38:21 -0700359 write = madvise_need_mmap_write(behavior);
360 if (write)
Nick Piggin0a27a142007-05-06 14:49:53 -0700361 down_write(&current->mm->mmap_sem);
362 else
363 down_read(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 if (start & ~PAGE_MASK)
366 goto out;
367 len = (len_in + ~PAGE_MASK) & PAGE_MASK;
368
369 /* Check to see whether len was rounded up from small -ve to zero */
370 if (len_in && !len)
371 goto out;
372
373 end = start + len;
374 if (end < start)
375 goto out;
376
377 error = 0;
378 if (end == start)
379 goto out;
380
381 /*
382 * If the interval [start,end) covers some unmapped address
383 * ranges, just ignore them, but return -ENOMEM at the end.
Prasanna Meda05b74382005-06-21 17:14:37 -0700384 * - different from the way of handling in mlock etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 */
Prasanna Meda05b74382005-06-21 17:14:37 -0700386 vma = find_vma_prev(current->mm, start, &prev);
Hugh Dickins836d5ff2005-09-03 15:54:53 -0700387 if (vma && start > vma->vm_start)
388 prev = vma;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 for (;;) {
391 /* Still start < end. */
392 error = -ENOMEM;
393 if (!vma)
394 goto out;
395
Prasanna Meda05b74382005-06-21 17:14:37 -0700396 /* Here start < (end|vma->vm_end). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (start < vma->vm_start) {
398 unmapped_error = -ENOMEM;
399 start = vma->vm_start;
Prasanna Meda05b74382005-06-21 17:14:37 -0700400 if (start >= end)
401 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
Prasanna Meda05b74382005-06-21 17:14:37 -0700404 /* Here vma->vm_start <= start < (end|vma->vm_end) */
405 tmp = vma->vm_end;
406 if (end < tmp)
407 tmp = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Prasanna Meda05b74382005-06-21 17:14:37 -0700409 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
410 error = madvise_vma(vma, &prev, start, tmp, behavior);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (error)
412 goto out;
Prasanna Meda05b74382005-06-21 17:14:37 -0700413 start = tmp;
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700414 if (prev && start < prev->vm_end)
Prasanna Meda05b74382005-06-21 17:14:37 -0700415 start = prev->vm_end;
416 error = unmapped_error;
417 if (start >= end)
418 goto out;
Hugh Dickins90ed52e2007-03-29 01:20:38 -0700419 if (prev)
420 vma = prev->vm_next;
421 else /* madvise_remove dropped mmap_sem */
422 vma = find_vma(current->mm, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424out:
Jason Baronf7977792007-07-15 23:38:21 -0700425 if (write)
Nick Piggin0a27a142007-05-06 14:49:53 -0700426 up_write(&current->mm->mmap_sem);
427 else
428 up_read(&current->mm->mmap_sem);
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return error;
431}