]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - mm/madvise.c
mm/page_alloc: fix nodes for reclaim in fast path
[mirror_ubuntu-bionic-kernel.git] / mm / madvise.c
CommitLineData
1da177e4
LT
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>
05b74384 11#include <linux/mempolicy.h>
afcf938e 12#include <linux/page-isolation.h>
05ce7724 13#include <linux/userfaultfd_k.h>
1da177e4 14#include <linux/hugetlb.h>
3f31d075 15#include <linux/falloc.h>
e8edc6e0 16#include <linux/sched.h>
f8af4da3 17#include <linux/ksm.h>
3f31d075 18#include <linux/fs.h>
9ab4233d 19#include <linux/file.h>
1998cc04 20#include <linux/blkdev.h>
66114cad 21#include <linux/backing-dev.h>
1998cc04
SL
22#include <linux/swap.h>
23#include <linux/swapops.h>
854e9ed0
MK
24#include <linux/mmu_notifier.h>
25
26#include <asm/tlb.h>
1da177e4 27
23519073
KS
28#include "internal.h"
29
0a27a14a
NP
30/*
31 * Any behaviour which results in changes to the vma->vm_flags needs to
32 * take mmap_sem for writing. Others, which simply traverse vmas, need
33 * to only take it for reading.
34 */
35static int madvise_need_mmap_write(int behavior)
36{
37 switch (behavior) {
38 case MADV_REMOVE:
39 case MADV_WILLNEED:
40 case MADV_DONTNEED:
854e9ed0 41 case MADV_FREE:
0a27a14a
NP
42 return 0;
43 default:
44 /* be safe, default to 1. list exceptions explicitly */
45 return 1;
46 }
47}
48
1da177e4
LT
49/*
50 * We can potentially split a vm area into separate
51 * areas, each area with its own behavior.
52 */
ec9bed9d 53static long madvise_behavior(struct vm_area_struct *vma,
05b74384
PM
54 struct vm_area_struct **prev,
55 unsigned long start, unsigned long end, int behavior)
1da177e4 56{
ec9bed9d 57 struct mm_struct *mm = vma->vm_mm;
1da177e4 58 int error = 0;
05b74384 59 pgoff_t pgoff;
3866ea90 60 unsigned long new_flags = vma->vm_flags;
e798c6e8
PM
61
62 switch (behavior) {
f8225661
MT
63 case MADV_NORMAL:
64 new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
65 break;
e798c6e8 66 case MADV_SEQUENTIAL:
f8225661 67 new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
e798c6e8
PM
68 break;
69 case MADV_RANDOM:
f8225661 70 new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
e798c6e8 71 break;
f8225661
MT
72 case MADV_DONTFORK:
73 new_flags |= VM_DONTCOPY;
74 break;
75 case MADV_DOFORK:
3866ea90
HD
76 if (vma->vm_flags & VM_IO) {
77 error = -EINVAL;
78 goto out;
79 }
f8225661 80 new_flags &= ~VM_DONTCOPY;
e798c6e8 81 break;
accb61fe 82 case MADV_DONTDUMP:
0103bd16 83 new_flags |= VM_DONTDUMP;
accb61fe
JB
84 break;
85 case MADV_DODUMP:
0103bd16
KK
86 if (new_flags & VM_SPECIAL) {
87 error = -EINVAL;
88 goto out;
89 }
90 new_flags &= ~VM_DONTDUMP;
accb61fe 91 break;
f8af4da3
HD
92 case MADV_MERGEABLE:
93 case MADV_UNMERGEABLE:
94 error = ksm_madvise(vma, start, end, behavior, &new_flags);
def5efe0
DR
95 if (error) {
96 /*
97 * madvise() returns EAGAIN if kernel resources, such as
98 * slab, are temporarily unavailable.
99 */
100 if (error == -ENOMEM)
101 error = -EAGAIN;
f8af4da3 102 goto out;
def5efe0 103 }
f8af4da3 104 break;
0af4e98b 105 case MADV_HUGEPAGE:
a664b2d8 106 case MADV_NOHUGEPAGE:
60ab3244 107 error = hugepage_madvise(vma, &new_flags, behavior);
def5efe0
DR
108 if (error) {
109 /*
110 * madvise() returns EAGAIN if kernel resources, such as
111 * slab, are temporarily unavailable.
112 */
113 if (error == -ENOMEM)
114 error = -EAGAIN;
0af4e98b 115 goto out;
def5efe0 116 }
0af4e98b 117 break;
e798c6e8
PM
118 }
119
05b74384
PM
120 if (new_flags == vma->vm_flags) {
121 *prev = vma;
836d5ffd 122 goto out;
05b74384
PM
123 }
124
125 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
126 *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
19a809af
AA
127 vma->vm_file, pgoff, vma_policy(vma),
128 vma->vm_userfaultfd_ctx);
05b74384
PM
129 if (*prev) {
130 vma = *prev;
131 goto success;
132 }
133
134 *prev = vma;
1da177e4
LT
135
136 if (start != vma->vm_start) {
def5efe0
DR
137 if (unlikely(mm->map_count >= sysctl_max_map_count)) {
138 error = -ENOMEM;
1da177e4 139 goto out;
def5efe0
DR
140 }
141 error = __split_vma(mm, vma, start, 1);
142 if (error) {
143 /*
144 * madvise() returns EAGAIN if kernel resources, such as
145 * slab, are temporarily unavailable.
146 */
147 if (error == -ENOMEM)
148 error = -EAGAIN;
149 goto out;
150 }
1da177e4
LT
151 }
152
153 if (end != vma->vm_end) {
def5efe0
DR
154 if (unlikely(mm->map_count >= sysctl_max_map_count)) {
155 error = -ENOMEM;
1da177e4 156 goto out;
def5efe0
DR
157 }
158 error = __split_vma(mm, vma, end, 0);
159 if (error) {
160 /*
161 * madvise() returns EAGAIN if kernel resources, such as
162 * slab, are temporarily unavailable.
163 */
164 if (error == -ENOMEM)
165 error = -EAGAIN;
166 goto out;
167 }
1da177e4
LT
168 }
169
836d5ffd 170success:
1da177e4
LT
171 /*
172 * vm_flags is protected by the mmap_sem held in write mode.
173 */
e798c6e8 174 vma->vm_flags = new_flags;
1da177e4 175out:
1da177e4
LT
176 return error;
177}
178
1998cc04
SL
179#ifdef CONFIG_SWAP
180static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
181 unsigned long end, struct mm_walk *walk)
182{
183 pte_t *orig_pte;
184 struct vm_area_struct *vma = walk->private;
185 unsigned long index;
186
187 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
188 return 0;
189
190 for (index = start; index != end; index += PAGE_SIZE) {
191 pte_t pte;
192 swp_entry_t entry;
193 struct page *page;
194 spinlock_t *ptl;
195
196 orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl);
197 pte = *(orig_pte + ((index - start) / PAGE_SIZE));
198 pte_unmap_unlock(orig_pte, ptl);
199
0661a336 200 if (pte_present(pte) || pte_none(pte))
1998cc04
SL
201 continue;
202 entry = pte_to_swp_entry(pte);
203 if (unlikely(non_swap_entry(entry)))
204 continue;
205
206 page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE,
207 vma, index);
208 if (page)
09cbfeaf 209 put_page(page);
1998cc04
SL
210 }
211
212 return 0;
213}
214
215static void force_swapin_readahead(struct vm_area_struct *vma,
216 unsigned long start, unsigned long end)
217{
218 struct mm_walk walk = {
219 .mm = vma->vm_mm,
220 .pmd_entry = swapin_walk_pmd_entry,
221 .private = vma,
222 };
223
224 walk_page_range(start, end, &walk);
225
226 lru_add_drain(); /* Push any new pages onto the LRU now */
227}
228
229static void force_shm_swapin_readahead(struct vm_area_struct *vma,
230 unsigned long start, unsigned long end,
231 struct address_space *mapping)
232{
233 pgoff_t index;
234 struct page *page;
235 swp_entry_t swap;
236
237 for (; start < end; start += PAGE_SIZE) {
238 index = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
239
55231e5c 240 page = find_get_entry(mapping, index);
1998cc04
SL
241 if (!radix_tree_exceptional_entry(page)) {
242 if (page)
09cbfeaf 243 put_page(page);
1998cc04
SL
244 continue;
245 }
246 swap = radix_to_swp_entry(page);
247 page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE,
248 NULL, 0);
249 if (page)
09cbfeaf 250 put_page(page);
1998cc04
SL
251 }
252
253 lru_add_drain(); /* Push any new pages onto the LRU now */
254}
255#endif /* CONFIG_SWAP */
256
1da177e4
LT
257/*
258 * Schedule all required I/O operations. Do not wait for completion.
259 */
ec9bed9d
VC
260static long madvise_willneed(struct vm_area_struct *vma,
261 struct vm_area_struct **prev,
1da177e4
LT
262 unsigned long start, unsigned long end)
263{
264 struct file *file = vma->vm_file;
265
1998cc04 266#ifdef CONFIG_SWAP
97b713ba 267 if (!file) {
1998cc04 268 *prev = vma;
97b713ba 269 force_swapin_readahead(vma, start, end);
1998cc04
SL
270 return 0;
271 }
1998cc04 272
97b713ba
CH
273 if (shmem_mapping(file->f_mapping)) {
274 *prev = vma;
275 force_shm_swapin_readahead(vma, start, end,
276 file->f_mapping);
277 return 0;
278 }
279#else
1bef4003
S
280 if (!file)
281 return -EBADF;
97b713ba 282#endif
1bef4003 283
e748dcd0 284 if (IS_DAX(file_inode(file))) {
fe77ba6f
CO
285 /* no bad return value, but ignore advice */
286 return 0;
287 }
288
05b74384 289 *prev = vma;
1da177e4
LT
290 start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
291 if (end > vma->vm_end)
292 end = vma->vm_end;
293 end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
294
f7e839dd 295 force_page_cache_readahead(file->f_mapping, file, start, end - start);
1da177e4
LT
296 return 0;
297}
298
854e9ed0
MK
299static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
300 unsigned long end, struct mm_walk *walk)
301
302{
303 struct mmu_gather *tlb = walk->private;
304 struct mm_struct *mm = tlb->mm;
305 struct vm_area_struct *vma = walk->vma;
306 spinlock_t *ptl;
307 pte_t *orig_pte, *pte, ptent;
308 struct page *page;
64b42bc1 309 int nr_swap = 0;
b8d3c4c3
MK
310 unsigned long next;
311
312 next = pmd_addr_end(addr, end);
313 if (pmd_trans_huge(*pmd))
314 if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next))
315 goto next;
854e9ed0 316
854e9ed0
MK
317 if (pmd_trans_unstable(pmd))
318 return 0;
319
07e32661 320 tlb_remove_check_page_size_change(tlb, PAGE_SIZE);
854e9ed0
MK
321 orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
322 arch_enter_lazy_mmu_mode();
323 for (; addr != end; pte++, addr += PAGE_SIZE) {
324 ptent = *pte;
325
64b42bc1 326 if (pte_none(ptent))
854e9ed0 327 continue;
64b42bc1
MK
328 /*
329 * If the pte has swp_entry, just clear page table to
330 * prevent swap-in which is more expensive rather than
331 * (page allocation + zeroing).
332 */
333 if (!pte_present(ptent)) {
334 swp_entry_t entry;
335
336 entry = pte_to_swp_entry(ptent);
337 if (non_swap_entry(entry))
338 continue;
339 nr_swap--;
340 free_swap_and_cache(entry);
341 pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
342 continue;
343 }
854e9ed0
MK
344
345 page = vm_normal_page(vma, addr, ptent);
346 if (!page)
347 continue;
348
349 /*
350 * If pmd isn't transhuge but the page is THP and
351 * is owned by only this process, split it and
352 * deactivate all pages.
353 */
354 if (PageTransCompound(page)) {
355 if (page_mapcount(page) != 1)
356 goto out;
357 get_page(page);
358 if (!trylock_page(page)) {
359 put_page(page);
360 goto out;
361 }
362 pte_unmap_unlock(orig_pte, ptl);
363 if (split_huge_page(page)) {
364 unlock_page(page);
365 put_page(page);
366 pte_offset_map_lock(mm, pmd, addr, &ptl);
367 goto out;
368 }
369 put_page(page);
370 unlock_page(page);
371 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
372 pte--;
373 addr -= PAGE_SIZE;
374 continue;
375 }
376
377 VM_BUG_ON_PAGE(PageTransCompound(page), page);
378
379 if (PageSwapCache(page) || PageDirty(page)) {
380 if (!trylock_page(page))
381 continue;
382 /*
383 * If page is shared with others, we couldn't clear
384 * PG_dirty of the page.
385 */
386 if (page_mapcount(page) != 1) {
387 unlock_page(page);
388 continue;
389 }
390
391 if (PageSwapCache(page) && !try_to_free_swap(page)) {
392 unlock_page(page);
393 continue;
394 }
395
396 ClearPageDirty(page);
397 unlock_page(page);
398 }
399
400 if (pte_young(ptent) || pte_dirty(ptent)) {
401 /*
402 * Some of architecture(ex, PPC) don't update TLB
403 * with set_pte_at and tlb_remove_tlb_entry so for
404 * the portability, remap the pte with old|clean
405 * after pte clearing.
406 */
407 ptent = ptep_get_and_clear_full(mm, addr, pte,
408 tlb->fullmm);
409
410 ptent = pte_mkold(ptent);
411 ptent = pte_mkclean(ptent);
412 set_pte_at(mm, addr, pte, ptent);
10853a03
MK
413 if (PageActive(page))
414 deactivate_page(page);
854e9ed0
MK
415 tlb_remove_tlb_entry(tlb, pte, addr);
416 }
417 }
418out:
64b42bc1
MK
419 if (nr_swap) {
420 if (current->mm == mm)
421 sync_mm_rss(mm);
422
423 add_mm_counter(mm, MM_SWAPENTS, nr_swap);
424 }
854e9ed0
MK
425 arch_leave_lazy_mmu_mode();
426 pte_unmap_unlock(orig_pte, ptl);
427 cond_resched();
b8d3c4c3 428next:
854e9ed0
MK
429 return 0;
430}
431
432static void madvise_free_page_range(struct mmu_gather *tlb,
433 struct vm_area_struct *vma,
434 unsigned long addr, unsigned long end)
435{
436 struct mm_walk free_walk = {
437 .pmd_entry = madvise_free_pte_range,
438 .mm = vma->vm_mm,
439 .private = tlb,
440 };
441
442 tlb_start_vma(tlb, vma);
443 walk_page_range(addr, end, &free_walk);
444 tlb_end_vma(tlb, vma);
445}
446
447static int madvise_free_single_vma(struct vm_area_struct *vma,
448 unsigned long start_addr, unsigned long end_addr)
449{
450 unsigned long start, end;
451 struct mm_struct *mm = vma->vm_mm;
452 struct mmu_gather tlb;
453
454 if (vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP))
455 return -EINVAL;
456
457 /* MADV_FREE works for only anon vma at the moment */
458 if (!vma_is_anonymous(vma))
459 return -EINVAL;
460
461 start = max(vma->vm_start, start_addr);
462 if (start >= vma->vm_end)
463 return -EINVAL;
464 end = min(vma->vm_end, end_addr);
465 if (end <= vma->vm_start)
466 return -EINVAL;
467
468 lru_add_drain();
469 tlb_gather_mmu(&tlb, mm, start, end);
470 update_hiwater_rss(mm);
471
472 mmu_notifier_invalidate_range_start(mm, start, end);
473 madvise_free_page_range(&tlb, vma, start, end);
474 mmu_notifier_invalidate_range_end(mm, start, end);
475 tlb_finish_mmu(&tlb, start, end);
476
477 return 0;
478}
479
480static long madvise_free(struct vm_area_struct *vma,
481 struct vm_area_struct **prev,
482 unsigned long start, unsigned long end)
483{
484 *prev = vma;
485 return madvise_free_single_vma(vma, start, end);
486}
487
1da177e4
LT
488/*
489 * Application no longer needs these pages. If the pages are dirty,
490 * it's OK to just throw them away. The app will be more careful about
491 * data it wants to keep. Be sure to free swap resources too. The
7e6cbea3 492 * zap_page_range call sets things up for shrink_active_list to actually free
1da177e4
LT
493 * these pages later if no one else has touched them in the meantime,
494 * although we could add these pages to a global reuse list for
7e6cbea3 495 * shrink_active_list to pick up before reclaiming other pages.
1da177e4
LT
496 *
497 * NB: This interface discards data rather than pushes it out to swap,
498 * as some implementations do. This has performance implications for
499 * applications like large transactional databases which want to discard
500 * pages in anonymous maps after committing to backing store the data
501 * that was kept in them. There is no reason to write this data out to
502 * the swap area if the application is discarding it.
503 *
504 * An interface that causes the system to free clean pages and flush
505 * dirty pages is already available as msync(MS_INVALIDATE).
506 */
ec9bed9d
VC
507static long madvise_dontneed(struct vm_area_struct *vma,
508 struct vm_area_struct **prev,
1da177e4
LT
509 unsigned long start, unsigned long end)
510{
05b74384 511 *prev = vma;
23519073 512 if (!can_madv_dontneed_vma(vma))
1da177e4
LT
513 return -EINVAL;
514
d811914d 515 userfaultfd_remove(vma, prev, start, end);
ecf1385d 516 zap_page_range(vma, start, end - start);
1da177e4
LT
517 return 0;
518}
519
f6b3ec23
BP
520/*
521 * Application wants to free up the pages and associated backing store.
522 * This is effectively punching a hole into the middle of a file.
f6b3ec23
BP
523 */
524static long madvise_remove(struct vm_area_struct *vma,
00e9fa2d 525 struct vm_area_struct **prev,
f6b3ec23
BP
526 unsigned long start, unsigned long end)
527{
3f31d075 528 loff_t offset;
90ed52eb 529 int error;
9ab4233d 530 struct file *f;
f6b3ec23 531
90ed52eb 532 *prev = NULL; /* tell sys_madvise we drop mmap_sem */
00e9fa2d 533
72079ba0 534 if (vma->vm_flags & VM_LOCKED)
f6b3ec23
BP
535 return -EINVAL;
536
9ab4233d
AL
537 f = vma->vm_file;
538
539 if (!f || !f->f_mapping || !f->f_mapping->host) {
f6b3ec23
BP
540 return -EINVAL;
541 }
542
69cf0fac
HD
543 if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
544 return -EACCES;
545
f6b3ec23
BP
546 offset = (loff_t)(start - vma->vm_start)
547 + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
90ed52eb 548
9ab4233d
AL
549 /*
550 * Filesystem's fallocate may need to take i_mutex. We need to
551 * explicitly grab a reference because the vma (and hence the
552 * vma's reference to the file) can go away as soon as we drop
553 * mmap_sem.
554 */
555 get_file(f);
a6bf53eb 556 userfaultfd_remove(vma, prev, start, end);
0a27a14a 557 up_read(&current->mm->mmap_sem);
72c72bdf 558 error = vfs_fallocate(f,
3f31d075
HD
559 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
560 offset, end - start);
9ab4233d 561 fput(f);
0a27a14a 562 down_read(&current->mm->mmap_sem);
90ed52eb 563 return error;
f6b3ec23
BP
564}
565
9893e49d
AK
566#ifdef CONFIG_MEMORY_FAILURE
567/*
568 * Error injection support for memory error handling.
569 */
afcf938e 570static int madvise_hwpoison(int bhv, unsigned long start, unsigned long end)
9893e49d 571{
20cb6cab 572 struct page *p;
9893e49d
AK
573 if (!capable(CAP_SYS_ADMIN))
574 return -EPERM;
20cb6cab
WL
575 for (; start < end; start += PAGE_SIZE <<
576 compound_order(compound_head(p))) {
325c4ef5
AM
577 int ret;
578
579 ret = get_user_pages_fast(start, 1, 0, &p);
9893e49d
AK
580 if (ret != 1)
581 return ret;
325c4ef5 582
29b4eede
WL
583 if (PageHWPoison(p)) {
584 put_page(p);
585 continue;
586 }
afcf938e 587 if (bhv == MADV_SOFT_OFFLINE) {
b194b8cd 588 pr_info("Soft offlining page %#lx at %#lx\n",
afcf938e
AK
589 page_to_pfn(p), start);
590 ret = soft_offline_page(p, MF_COUNT_INCREASED);
591 if (ret)
8302423b 592 return ret;
afcf938e
AK
593 continue;
594 }
b194b8cd 595 pr_info("Injecting memory failure for page %#lx at %#lx\n",
9893e49d 596 page_to_pfn(p), start);
23a003bf
NH
597 ret = memory_failure(page_to_pfn(p), 0, MF_COUNT_INCREASED);
598 if (ret)
599 return ret;
9893e49d 600 }
325c4ef5 601 return 0;
9893e49d
AK
602}
603#endif
604
165cd402 605static long
606madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
607 unsigned long start, unsigned long end, int behavior)
1da177e4 608{
1da177e4 609 switch (behavior) {
f6b3ec23 610 case MADV_REMOVE:
3866ea90 611 return madvise_remove(vma, prev, start, end);
1da177e4 612 case MADV_WILLNEED:
3866ea90 613 return madvise_willneed(vma, prev, start, end);
854e9ed0
MK
614 case MADV_FREE:
615 /*
616 * XXX: In this implementation, MADV_FREE works like
617 * MADV_DONTNEED on swapless system or full swap.
618 */
619 if (get_nr_swap_pages() > 0)
620 return madvise_free(vma, prev, start, end);
621 /* passthrough */
1da177e4 622 case MADV_DONTNEED:
3866ea90 623 return madvise_dontneed(vma, prev, start, end);
1da177e4 624 default:
3866ea90 625 return madvise_behavior(vma, prev, start, end, behavior);
1da177e4 626 }
1da177e4
LT
627}
628
1ecef9ed 629static bool
75927af8
NP
630madvise_behavior_valid(int behavior)
631{
632 switch (behavior) {
633 case MADV_DOFORK:
634 case MADV_DONTFORK:
635 case MADV_NORMAL:
636 case MADV_SEQUENTIAL:
637 case MADV_RANDOM:
638 case MADV_REMOVE:
639 case MADV_WILLNEED:
640 case MADV_DONTNEED:
854e9ed0 641 case MADV_FREE:
f8af4da3
HD
642#ifdef CONFIG_KSM
643 case MADV_MERGEABLE:
644 case MADV_UNMERGEABLE:
0af4e98b
AA
645#endif
646#ifdef CONFIG_TRANSPARENT_HUGEPAGE
647 case MADV_HUGEPAGE:
a664b2d8 648 case MADV_NOHUGEPAGE:
f8af4da3 649#endif
accb61fe
JB
650 case MADV_DONTDUMP:
651 case MADV_DODUMP:
1ecef9ed 652 return true;
75927af8
NP
653
654 default:
1ecef9ed 655 return false;
75927af8
NP
656 }
657}
3866ea90 658
1da177e4
LT
659/*
660 * The madvise(2) system call.
661 *
662 * Applications can use madvise() to advise the kernel how it should
663 * handle paging I/O in this VM area. The idea is to help the kernel
664 * use appropriate read-ahead and caching techniques. The information
665 * provided is advisory only, and can be safely disregarded by the
666 * kernel without affecting the correct operation of the application.
667 *
668 * behavior values:
669 * MADV_NORMAL - the default behavior is to read clusters. This
670 * results in some read-ahead and read-behind.
671 * MADV_RANDOM - the system should read the minimum amount of data
672 * on any access, since it is unlikely that the appli-
673 * cation will need more than what it asks for.
674 * MADV_SEQUENTIAL - pages in the given range will probably be accessed
675 * once, so they can be aggressively read ahead, and
676 * can be freed soon after they are accessed.
677 * MADV_WILLNEED - the application is notifying the system to read
678 * some pages ahead.
679 * MADV_DONTNEED - the application is finished with the given range,
680 * so the kernel can free resources associated with it.
d7206a70
NH
681 * MADV_FREE - the application marks pages in the given range as lazy free,
682 * where actual purges are postponed until memory pressure happens.
f6b3ec23
BP
683 * MADV_REMOVE - the application wants to free up the given range of
684 * pages and associated backing store.
3866ea90
HD
685 * MADV_DONTFORK - omit this area from child's address space when forking:
686 * typically, to avoid COWing pages pinned by get_user_pages().
687 * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
d7206a70
NH
688 * MADV_HWPOISON - trigger memory error handler as if the given memory range
689 * were corrupted by unrecoverable hardware memory failure.
690 * MADV_SOFT_OFFLINE - try to soft-offline the given range of memory.
f8af4da3
HD
691 * MADV_MERGEABLE - the application recommends that KSM try to merge pages in
692 * this area with pages of identical content from other such areas.
693 * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
d7206a70
NH
694 * MADV_HUGEPAGE - the application wants to back the given range by transparent
695 * huge pages in the future. Existing pages might be coalesced and
696 * new pages might be allocated as THP.
697 * MADV_NOHUGEPAGE - mark the given range as not worth being backed by
698 * transparent huge pages so the existing pages will not be
699 * coalesced into THP and new pages will not be allocated as THP.
700 * MADV_DONTDUMP - the application wants to prevent pages in the given range
701 * from being included in its core dump.
702 * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
1da177e4
LT
703 *
704 * return values:
705 * zero - success
706 * -EINVAL - start + len < 0, start is not page-aligned,
707 * "behavior" is not a valid value, or application
708 * is attempting to release locked or shared pages.
709 * -ENOMEM - addresses in the specified range are not currently
710 * mapped, or are outside the AS of the process.
711 * -EIO - an I/O error occurred while paging in data.
712 * -EBADF - map exists, but area maps something that isn't a file.
713 * -EAGAIN - a kernel resource was temporarily unavailable.
714 */
3480b257 715SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
1da177e4 716{
05b74384 717 unsigned long end, tmp;
ec9bed9d 718 struct vm_area_struct *vma, *prev;
1da177e4
LT
719 int unmapped_error = 0;
720 int error = -EINVAL;
f7977793 721 int write;
1da177e4 722 size_t len;
1998cc04 723 struct blk_plug plug;
1da177e4 724
9893e49d 725#ifdef CONFIG_MEMORY_FAILURE
afcf938e
AK
726 if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
727 return madvise_hwpoison(behavior, start, start+len_in);
9893e49d 728#endif
75927af8
NP
729 if (!madvise_behavior_valid(behavior))
730 return error;
731
1da177e4 732 if (start & ~PAGE_MASK)
84d96d89 733 return error;
1da177e4
LT
734 len = (len_in + ~PAGE_MASK) & PAGE_MASK;
735
736 /* Check to see whether len was rounded up from small -ve to zero */
737 if (len_in && !len)
84d96d89 738 return error;
1da177e4
LT
739
740 end = start + len;
741 if (end < start)
84d96d89 742 return error;
1da177e4
LT
743
744 error = 0;
745 if (end == start)
84d96d89
RV
746 return error;
747
748 write = madvise_need_mmap_write(behavior);
dc0ef0df
MH
749 if (write) {
750 if (down_write_killable(&current->mm->mmap_sem))
751 return -EINTR;
752 } else {
84d96d89 753 down_read(&current->mm->mmap_sem);
dc0ef0df 754 }
1da177e4
LT
755
756 /*
757 * If the interval [start,end) covers some unmapped address
758 * ranges, just ignore them, but return -ENOMEM at the end.
05b74384 759 * - different from the way of handling in mlock etc.
1da177e4 760 */
05b74384 761 vma = find_vma_prev(current->mm, start, &prev);
836d5ffd
HD
762 if (vma && start > vma->vm_start)
763 prev = vma;
764
1998cc04 765 blk_start_plug(&plug);
1da177e4
LT
766 for (;;) {
767 /* Still start < end. */
768 error = -ENOMEM;
769 if (!vma)
84d96d89 770 goto out;
1da177e4 771
05b74384 772 /* Here start < (end|vma->vm_end). */
1da177e4
LT
773 if (start < vma->vm_start) {
774 unmapped_error = -ENOMEM;
775 start = vma->vm_start;
05b74384 776 if (start >= end)
84d96d89 777 goto out;
1da177e4
LT
778 }
779
05b74384
PM
780 /* Here vma->vm_start <= start < (end|vma->vm_end) */
781 tmp = vma->vm_end;
782 if (end < tmp)
783 tmp = end;
1da177e4 784
05b74384
PM
785 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
786 error = madvise_vma(vma, &prev, start, tmp, behavior);
1da177e4 787 if (error)
84d96d89 788 goto out;
05b74384 789 start = tmp;
90ed52eb 790 if (prev && start < prev->vm_end)
05b74384
PM
791 start = prev->vm_end;
792 error = unmapped_error;
793 if (start >= end)
84d96d89 794 goto out;
90ed52eb
HD
795 if (prev)
796 vma = prev->vm_next;
797 else /* madvise_remove dropped mmap_sem */
798 vma = find_vma(current->mm, start);
1da177e4 799 }
1da177e4 800out:
84d96d89 801 blk_finish_plug(&plug);
f7977793 802 if (write)
0a27a14a
NP
803 up_write(&current->mm->mmap_sem);
804 else
805 up_read(&current->mm->mmap_sem);
806
1da177e4
LT
807 return error;
808}