Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/mm/madvise.c
0003  *
0004  * Copyright (C) 1999  Linus Torvalds
0005  * Copyright (C) 2002  Christoph Hellwig
0006  */
0007 
0008 #include <linux/mman.h>
0009 #include <linux/pagemap.h>
0010 #include <linux/syscalls.h>
0011 #include <linux/mempolicy.h>
0012 #include <linux/page-isolation.h>
0013 #include <linux/hugetlb.h>
0014 #include <linux/falloc.h>
0015 #include <linux/sched.h>
0016 #include <linux/ksm.h>
0017 #include <linux/fs.h>
0018 #include <linux/file.h>
0019 #include <linux/blkdev.h>
0020 #include <linux/backing-dev.h>
0021 #include <linux/swap.h>
0022 #include <linux/swapops.h>
0023 #include <linux/mmu_notifier.h>
0024 
0025 #include <asm/tlb.h>
0026 
0027 /*
0028  * Any behaviour which results in changes to the vma->vm_flags needs to
0029  * take mmap_sem for writing. Others, which simply traverse vmas, need
0030  * to only take it for reading.
0031  */
0032 static int madvise_need_mmap_write(int behavior)
0033 {
0034     switch (behavior) {
0035     case MADV_REMOVE:
0036     case MADV_WILLNEED:
0037     case MADV_DONTNEED:
0038     case MADV_FREE:
0039         return 0;
0040     default:
0041         /* be safe, default to 1. list exceptions explicitly */
0042         return 1;
0043     }
0044 }
0045 
0046 /*
0047  * We can potentially split a vm area into separate
0048  * areas, each area with its own behavior.
0049  */
0050 static long madvise_behavior(struct vm_area_struct *vma,
0051              struct vm_area_struct **prev,
0052              unsigned long start, unsigned long end, int behavior)
0053 {
0054     struct mm_struct *mm = vma->vm_mm;
0055     int error = 0;
0056     pgoff_t pgoff;
0057     unsigned long new_flags = vma->vm_flags;
0058 
0059     switch (behavior) {
0060     case MADV_NORMAL:
0061         new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
0062         break;
0063     case MADV_SEQUENTIAL:
0064         new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
0065         break;
0066     case MADV_RANDOM:
0067         new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
0068         break;
0069     case MADV_DONTFORK:
0070         new_flags |= VM_DONTCOPY;
0071         break;
0072     case MADV_DOFORK:
0073         if (vma->vm_flags & VM_IO) {
0074             error = -EINVAL;
0075             goto out;
0076         }
0077         new_flags &= ~VM_DONTCOPY;
0078         break;
0079     case MADV_DONTDUMP:
0080         new_flags |= VM_DONTDUMP;
0081         break;
0082     case MADV_DODUMP:
0083         if (new_flags & VM_SPECIAL) {
0084             error = -EINVAL;
0085             goto out;
0086         }
0087         new_flags &= ~VM_DONTDUMP;
0088         break;
0089     case MADV_MERGEABLE:
0090     case MADV_UNMERGEABLE:
0091         error = ksm_madvise(vma, start, end, behavior, &new_flags);
0092         if (error)
0093             goto out;
0094         break;
0095     case MADV_HUGEPAGE:
0096     case MADV_NOHUGEPAGE:
0097         error = hugepage_madvise(vma, &new_flags, behavior);
0098         if (error)
0099             goto out;
0100         break;
0101     }
0102 
0103     if (new_flags == vma->vm_flags) {
0104         *prev = vma;
0105         goto out;
0106     }
0107 
0108     pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
0109     *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
0110               vma->vm_file, pgoff, vma_policy(vma),
0111               vma->vm_userfaultfd_ctx);
0112     if (*prev) {
0113         vma = *prev;
0114         goto success;
0115     }
0116 
0117     *prev = vma;
0118 
0119     if (start != vma->vm_start) {
0120         error = split_vma(mm, vma, start, 1);
0121         if (error)
0122             goto out;
0123     }
0124 
0125     if (end != vma->vm_end) {
0126         error = split_vma(mm, vma, end, 0);
0127         if (error)
0128             goto out;
0129     }
0130 
0131 success:
0132     /*
0133      * vm_flags is protected by the mmap_sem held in write mode.
0134      */
0135     vma->vm_flags = new_flags;
0136 
0137 out:
0138     if (error == -ENOMEM)
0139         error = -EAGAIN;
0140     return error;
0141 }
0142 
0143 #ifdef CONFIG_SWAP
0144 static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
0145     unsigned long end, struct mm_walk *walk)
0146 {
0147     pte_t *orig_pte;
0148     struct vm_area_struct *vma = walk->private;
0149     unsigned long index;
0150 
0151     if (pmd_none_or_trans_huge_or_clear_bad(pmd))
0152         return 0;
0153 
0154     for (index = start; index != end; index += PAGE_SIZE) {
0155         pte_t pte;
0156         swp_entry_t entry;
0157         struct page *page;
0158         spinlock_t *ptl;
0159 
0160         orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl);
0161         pte = *(orig_pte + ((index - start) / PAGE_SIZE));
0162         pte_unmap_unlock(orig_pte, ptl);
0163 
0164         if (pte_present(pte) || pte_none(pte))
0165             continue;
0166         entry = pte_to_swp_entry(pte);
0167         if (unlikely(non_swap_entry(entry)))
0168             continue;
0169 
0170         page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE,
0171                                 vma, index);
0172         if (page)
0173             put_page(page);
0174     }
0175 
0176     return 0;
0177 }
0178 
0179 static void force_swapin_readahead(struct vm_area_struct *vma,
0180         unsigned long start, unsigned long end)
0181 {
0182     struct mm_walk walk = {
0183         .mm = vma->vm_mm,
0184         .pmd_entry = swapin_walk_pmd_entry,
0185         .private = vma,
0186     };
0187 
0188     walk_page_range(start, end, &walk);
0189 
0190     lru_add_drain();    /* Push any new pages onto the LRU now */
0191 }
0192 
0193 static void force_shm_swapin_readahead(struct vm_area_struct *vma,
0194         unsigned long start, unsigned long end,
0195         struct address_space *mapping)
0196 {
0197     pgoff_t index;
0198     struct page *page;
0199     swp_entry_t swap;
0200 
0201     for (; start < end; start += PAGE_SIZE) {
0202         index = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
0203 
0204         page = find_get_entry(mapping, index);
0205         if (!radix_tree_exceptional_entry(page)) {
0206             if (page)
0207                 put_page(page);
0208             continue;
0209         }
0210         swap = radix_to_swp_entry(page);
0211         page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE,
0212                                 NULL, 0);
0213         if (page)
0214             put_page(page);
0215     }
0216 
0217     lru_add_drain();    /* Push any new pages onto the LRU now */
0218 }
0219 #endif      /* CONFIG_SWAP */
0220 
0221 /*
0222  * Schedule all required I/O operations.  Do not wait for completion.
0223  */
0224 static long madvise_willneed(struct vm_area_struct *vma,
0225                  struct vm_area_struct **prev,
0226                  unsigned long start, unsigned long end)
0227 {
0228     struct file *file = vma->vm_file;
0229 
0230 #ifdef CONFIG_SWAP
0231     if (!file) {
0232         *prev = vma;
0233         force_swapin_readahead(vma, start, end);
0234         return 0;
0235     }
0236 
0237     if (shmem_mapping(file->f_mapping)) {
0238         *prev = vma;
0239         force_shm_swapin_readahead(vma, start, end,
0240                     file->f_mapping);
0241         return 0;
0242     }
0243 #else
0244     if (!file)
0245         return -EBADF;
0246 #endif
0247 
0248     if (IS_DAX(file_inode(file))) {
0249         /* no bad return value, but ignore advice */
0250         return 0;
0251     }
0252 
0253     *prev = vma;
0254     start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
0255     if (end > vma->vm_end)
0256         end = vma->vm_end;
0257     end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
0258 
0259     force_page_cache_readahead(file->f_mapping, file, start, end - start);
0260     return 0;
0261 }
0262 
0263 static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
0264                 unsigned long end, struct mm_walk *walk)
0265 
0266 {
0267     struct mmu_gather *tlb = walk->private;
0268     struct mm_struct *mm = tlb->mm;
0269     struct vm_area_struct *vma = walk->vma;
0270     spinlock_t *ptl;
0271     pte_t *orig_pte, *pte, ptent;
0272     struct page *page;
0273     int nr_swap = 0;
0274     unsigned long next;
0275 
0276     next = pmd_addr_end(addr, end);
0277     if (pmd_trans_huge(*pmd))
0278         if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next))
0279             goto next;
0280 
0281     if (pmd_trans_unstable(pmd))
0282         return 0;
0283 
0284     tlb_remove_check_page_size_change(tlb, PAGE_SIZE);
0285     orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
0286     arch_enter_lazy_mmu_mode();
0287     for (; addr != end; pte++, addr += PAGE_SIZE) {
0288         ptent = *pte;
0289 
0290         if (pte_none(ptent))
0291             continue;
0292         /*
0293          * If the pte has swp_entry, just clear page table to
0294          * prevent swap-in which is more expensive rather than
0295          * (page allocation + zeroing).
0296          */
0297         if (!pte_present(ptent)) {
0298             swp_entry_t entry;
0299 
0300             entry = pte_to_swp_entry(ptent);
0301             if (non_swap_entry(entry))
0302                 continue;
0303             nr_swap--;
0304             free_swap_and_cache(entry);
0305             pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
0306             continue;
0307         }
0308 
0309         page = vm_normal_page(vma, addr, ptent);
0310         if (!page)
0311             continue;
0312 
0313         /*
0314          * If pmd isn't transhuge but the page is THP and
0315          * is owned by only this process, split it and
0316          * deactivate all pages.
0317          */
0318         if (PageTransCompound(page)) {
0319             if (page_mapcount(page) != 1)
0320                 goto out;
0321             get_page(page);
0322             if (!trylock_page(page)) {
0323                 put_page(page);
0324                 goto out;
0325             }
0326             pte_unmap_unlock(orig_pte, ptl);
0327             if (split_huge_page(page)) {
0328                 unlock_page(page);
0329                 put_page(page);
0330                 pte_offset_map_lock(mm, pmd, addr, &ptl);
0331                 goto out;
0332             }
0333             put_page(page);
0334             unlock_page(page);
0335             pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
0336             pte--;
0337             addr -= PAGE_SIZE;
0338             continue;
0339         }
0340 
0341         VM_BUG_ON_PAGE(PageTransCompound(page), page);
0342 
0343         if (PageSwapCache(page) || PageDirty(page)) {
0344             if (!trylock_page(page))
0345                 continue;
0346             /*
0347              * If page is shared with others, we couldn't clear
0348              * PG_dirty of the page.
0349              */
0350             if (page_mapcount(page) != 1) {
0351                 unlock_page(page);
0352                 continue;
0353             }
0354 
0355             if (PageSwapCache(page) && !try_to_free_swap(page)) {
0356                 unlock_page(page);
0357                 continue;
0358             }
0359 
0360             ClearPageDirty(page);
0361             unlock_page(page);
0362         }
0363 
0364         if (pte_young(ptent) || pte_dirty(ptent)) {
0365             /*
0366              * Some of architecture(ex, PPC) don't update TLB
0367              * with set_pte_at and tlb_remove_tlb_entry so for
0368              * the portability, remap the pte with old|clean
0369              * after pte clearing.
0370              */
0371             ptent = ptep_get_and_clear_full(mm, addr, pte,
0372                             tlb->fullmm);
0373 
0374             ptent = pte_mkold(ptent);
0375             ptent = pte_mkclean(ptent);
0376             set_pte_at(mm, addr, pte, ptent);
0377             if (PageActive(page))
0378                 deactivate_page(page);
0379             tlb_remove_tlb_entry(tlb, pte, addr);
0380         }
0381     }
0382 out:
0383     if (nr_swap) {
0384         if (current->mm == mm)
0385             sync_mm_rss(mm);
0386 
0387         add_mm_counter(mm, MM_SWAPENTS, nr_swap);
0388     }
0389     arch_leave_lazy_mmu_mode();
0390     pte_unmap_unlock(orig_pte, ptl);
0391     cond_resched();
0392 next:
0393     return 0;
0394 }
0395 
0396 static void madvise_free_page_range(struct mmu_gather *tlb,
0397                  struct vm_area_struct *vma,
0398                  unsigned long addr, unsigned long end)
0399 {
0400     struct mm_walk free_walk = {
0401         .pmd_entry = madvise_free_pte_range,
0402         .mm = vma->vm_mm,
0403         .private = tlb,
0404     };
0405 
0406     tlb_start_vma(tlb, vma);
0407     walk_page_range(addr, end, &free_walk);
0408     tlb_end_vma(tlb, vma);
0409 }
0410 
0411 static int madvise_free_single_vma(struct vm_area_struct *vma,
0412             unsigned long start_addr, unsigned long end_addr)
0413 {
0414     unsigned long start, end;
0415     struct mm_struct *mm = vma->vm_mm;
0416     struct mmu_gather tlb;
0417 
0418     if (vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP))
0419         return -EINVAL;
0420 
0421     /* MADV_FREE works for only anon vma at the moment */
0422     if (!vma_is_anonymous(vma))
0423         return -EINVAL;
0424 
0425     start = max(vma->vm_start, start_addr);
0426     if (start >= vma->vm_end)
0427         return -EINVAL;
0428     end = min(vma->vm_end, end_addr);
0429     if (end <= vma->vm_start)
0430         return -EINVAL;
0431 
0432     lru_add_drain();
0433     tlb_gather_mmu(&tlb, mm, start, end);
0434     update_hiwater_rss(mm);
0435 
0436     mmu_notifier_invalidate_range_start(mm, start, end);
0437     madvise_free_page_range(&tlb, vma, start, end);
0438     mmu_notifier_invalidate_range_end(mm, start, end);
0439     tlb_finish_mmu(&tlb, start, end);
0440 
0441     return 0;
0442 }
0443 
0444 static long madvise_free(struct vm_area_struct *vma,
0445                  struct vm_area_struct **prev,
0446                  unsigned long start, unsigned long end)
0447 {
0448     *prev = vma;
0449     return madvise_free_single_vma(vma, start, end);
0450 }
0451 
0452 /*
0453  * Application no longer needs these pages.  If the pages are dirty,
0454  * it's OK to just throw them away.  The app will be more careful about
0455  * data it wants to keep.  Be sure to free swap resources too.  The
0456  * zap_page_range call sets things up for shrink_active_list to actually free
0457  * these pages later if no one else has touched them in the meantime,
0458  * although we could add these pages to a global reuse list for
0459  * shrink_active_list to pick up before reclaiming other pages.
0460  *
0461  * NB: This interface discards data rather than pushes it out to swap,
0462  * as some implementations do.  This has performance implications for
0463  * applications like large transactional databases which want to discard
0464  * pages in anonymous maps after committing to backing store the data
0465  * that was kept in them.  There is no reason to write this data out to
0466  * the swap area if the application is discarding it.
0467  *
0468  * An interface that causes the system to free clean pages and flush
0469  * dirty pages is already available as msync(MS_INVALIDATE).
0470  */
0471 static long madvise_dontneed(struct vm_area_struct *vma,
0472                  struct vm_area_struct **prev,
0473                  unsigned long start, unsigned long end)
0474 {
0475     *prev = vma;
0476     if (vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP))
0477         return -EINVAL;
0478 
0479     zap_page_range(vma, start, end - start, NULL);
0480     return 0;
0481 }
0482 
0483 /*
0484  * Application wants to free up the pages and associated backing store.
0485  * This is effectively punching a hole into the middle of a file.
0486  */
0487 static long madvise_remove(struct vm_area_struct *vma,
0488                 struct vm_area_struct **prev,
0489                 unsigned long start, unsigned long end)
0490 {
0491     loff_t offset;
0492     int error;
0493     struct file *f;
0494 
0495     *prev = NULL;   /* tell sys_madvise we drop mmap_sem */
0496 
0497     if (vma->vm_flags & VM_LOCKED)
0498         return -EINVAL;
0499 
0500     f = vma->vm_file;
0501 
0502     if (!f || !f->f_mapping || !f->f_mapping->host) {
0503             return -EINVAL;
0504     }
0505 
0506     if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
0507         return -EACCES;
0508 
0509     offset = (loff_t)(start - vma->vm_start)
0510             + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
0511 
0512     /*
0513      * Filesystem's fallocate may need to take i_mutex.  We need to
0514      * explicitly grab a reference because the vma (and hence the
0515      * vma's reference to the file) can go away as soon as we drop
0516      * mmap_sem.
0517      */
0518     get_file(f);
0519     up_read(&current->mm->mmap_sem);
0520     error = vfs_fallocate(f,
0521                 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
0522                 offset, end - start);
0523     fput(f);
0524     down_read(&current->mm->mmap_sem);
0525     return error;
0526 }
0527 
0528 #ifdef CONFIG_MEMORY_FAILURE
0529 /*
0530  * Error injection support for memory error handling.
0531  */
0532 static int madvise_hwpoison(int bhv, unsigned long start, unsigned long end)
0533 {
0534     struct page *p;
0535     if (!capable(CAP_SYS_ADMIN))
0536         return -EPERM;
0537     for (; start < end; start += PAGE_SIZE <<
0538                 compound_order(compound_head(p))) {
0539         int ret;
0540 
0541         ret = get_user_pages_fast(start, 1, 0, &p);
0542         if (ret != 1)
0543             return ret;
0544 
0545         if (PageHWPoison(p)) {
0546             put_page(p);
0547             continue;
0548         }
0549         if (bhv == MADV_SOFT_OFFLINE) {
0550             pr_info("Soft offlining page %#lx at %#lx\n",
0551                 page_to_pfn(p), start);
0552             ret = soft_offline_page(p, MF_COUNT_INCREASED);
0553             if (ret)
0554                 return ret;
0555             continue;
0556         }
0557         pr_info("Injecting memory failure for page %#lx at %#lx\n",
0558                page_to_pfn(p), start);
0559         ret = memory_failure(page_to_pfn(p), 0, MF_COUNT_INCREASED);
0560         if (ret)
0561             return ret;
0562     }
0563     return 0;
0564 }
0565 #endif
0566 
0567 static long
0568 madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
0569         unsigned long start, unsigned long end, int behavior)
0570 {
0571     switch (behavior) {
0572     case MADV_REMOVE:
0573         return madvise_remove(vma, prev, start, end);
0574     case MADV_WILLNEED:
0575         return madvise_willneed(vma, prev, start, end);
0576     case MADV_FREE:
0577         /*
0578          * XXX: In this implementation, MADV_FREE works like
0579          * MADV_DONTNEED on swapless system or full swap.
0580          */
0581         if (get_nr_swap_pages() > 0)
0582             return madvise_free(vma, prev, start, end);
0583         /* passthrough */
0584     case MADV_DONTNEED:
0585         return madvise_dontneed(vma, prev, start, end);
0586     default:
0587         return madvise_behavior(vma, prev, start, end, behavior);
0588     }
0589 }
0590 
0591 static bool
0592 madvise_behavior_valid(int behavior)
0593 {
0594     switch (behavior) {
0595     case MADV_DOFORK:
0596     case MADV_DONTFORK:
0597     case MADV_NORMAL:
0598     case MADV_SEQUENTIAL:
0599     case MADV_RANDOM:
0600     case MADV_REMOVE:
0601     case MADV_WILLNEED:
0602     case MADV_DONTNEED:
0603     case MADV_FREE:
0604 #ifdef CONFIG_KSM
0605     case MADV_MERGEABLE:
0606     case MADV_UNMERGEABLE:
0607 #endif
0608 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
0609     case MADV_HUGEPAGE:
0610     case MADV_NOHUGEPAGE:
0611 #endif
0612     case MADV_DONTDUMP:
0613     case MADV_DODUMP:
0614         return true;
0615 
0616     default:
0617         return false;
0618     }
0619 }
0620 
0621 /*
0622  * The madvise(2) system call.
0623  *
0624  * Applications can use madvise() to advise the kernel how it should
0625  * handle paging I/O in this VM area.  The idea is to help the kernel
0626  * use appropriate read-ahead and caching techniques.  The information
0627  * provided is advisory only, and can be safely disregarded by the
0628  * kernel without affecting the correct operation of the application.
0629  *
0630  * behavior values:
0631  *  MADV_NORMAL - the default behavior is to read clusters.  This
0632  *      results in some read-ahead and read-behind.
0633  *  MADV_RANDOM - the system should read the minimum amount of data
0634  *      on any access, since it is unlikely that the appli-
0635  *      cation will need more than what it asks for.
0636  *  MADV_SEQUENTIAL - pages in the given range will probably be accessed
0637  *      once, so they can be aggressively read ahead, and
0638  *      can be freed soon after they are accessed.
0639  *  MADV_WILLNEED - the application is notifying the system to read
0640  *      some pages ahead.
0641  *  MADV_DONTNEED - the application is finished with the given range,
0642  *      so the kernel can free resources associated with it.
0643  *  MADV_FREE - the application marks pages in the given range as lazy free,
0644  *      where actual purges are postponed until memory pressure happens.
0645  *  MADV_REMOVE - the application wants to free up the given range of
0646  *      pages and associated backing store.
0647  *  MADV_DONTFORK - omit this area from child's address space when forking:
0648  *      typically, to avoid COWing pages pinned by get_user_pages().
0649  *  MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
0650  *  MADV_HWPOISON - trigger memory error handler as if the given memory range
0651  *      were corrupted by unrecoverable hardware memory failure.
0652  *  MADV_SOFT_OFFLINE - try to soft-offline the given range of memory.
0653  *  MADV_MERGEABLE - the application recommends that KSM try to merge pages in
0654  *      this area with pages of identical content from other such areas.
0655  *  MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
0656  *  MADV_HUGEPAGE - the application wants to back the given range by transparent
0657  *      huge pages in the future. Existing pages might be coalesced and
0658  *      new pages might be allocated as THP.
0659  *  MADV_NOHUGEPAGE - mark the given range as not worth being backed by
0660  *      transparent huge pages so the existing pages will not be
0661  *      coalesced into THP and new pages will not be allocated as THP.
0662  *  MADV_DONTDUMP - the application wants to prevent pages in the given range
0663  *      from being included in its core dump.
0664  *  MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
0665  *
0666  * return values:
0667  *  zero    - success
0668  *  -EINVAL - start + len < 0, start is not page-aligned,
0669  *      "behavior" is not a valid value, or application
0670  *      is attempting to release locked or shared pages.
0671  *  -ENOMEM - addresses in the specified range are not currently
0672  *      mapped, or are outside the AS of the process.
0673  *  -EIO    - an I/O error occurred while paging in data.
0674  *  -EBADF  - map exists, but area maps something that isn't a file.
0675  *  -EAGAIN - a kernel resource was temporarily unavailable.
0676  */
0677 SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
0678 {
0679     unsigned long end, tmp;
0680     struct vm_area_struct *vma, *prev;
0681     int unmapped_error = 0;
0682     int error = -EINVAL;
0683     int write;
0684     size_t len;
0685     struct blk_plug plug;
0686 
0687 #ifdef CONFIG_MEMORY_FAILURE
0688     if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
0689         return madvise_hwpoison(behavior, start, start+len_in);
0690 #endif
0691     if (!madvise_behavior_valid(behavior))
0692         return error;
0693 
0694     if (start & ~PAGE_MASK)
0695         return error;
0696     len = (len_in + ~PAGE_MASK) & PAGE_MASK;
0697 
0698     /* Check to see whether len was rounded up from small -ve to zero */
0699     if (len_in && !len)
0700         return error;
0701 
0702     end = start + len;
0703     if (end < start)
0704         return error;
0705 
0706     error = 0;
0707     if (end == start)
0708         return error;
0709 
0710     write = madvise_need_mmap_write(behavior);
0711     if (write) {
0712         if (down_write_killable(&current->mm->mmap_sem))
0713             return -EINTR;
0714     } else {
0715         down_read(&current->mm->mmap_sem);
0716     }
0717 
0718     /*
0719      * If the interval [start,end) covers some unmapped address
0720      * ranges, just ignore them, but return -ENOMEM at the end.
0721      * - different from the way of handling in mlock etc.
0722      */
0723     vma = find_vma_prev(current->mm, start, &prev);
0724     if (vma && start > vma->vm_start)
0725         prev = vma;
0726 
0727     blk_start_plug(&plug);
0728     for (;;) {
0729         /* Still start < end. */
0730         error = -ENOMEM;
0731         if (!vma)
0732             goto out;
0733 
0734         /* Here start < (end|vma->vm_end). */
0735         if (start < vma->vm_start) {
0736             unmapped_error = -ENOMEM;
0737             start = vma->vm_start;
0738             if (start >= end)
0739                 goto out;
0740         }
0741 
0742         /* Here vma->vm_start <= start < (end|vma->vm_end) */
0743         tmp = vma->vm_end;
0744         if (end < tmp)
0745             tmp = end;
0746 
0747         /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
0748         error = madvise_vma(vma, &prev, start, tmp, behavior);
0749         if (error)
0750             goto out;
0751         start = tmp;
0752         if (prev && start < prev->vm_end)
0753             start = prev->vm_end;
0754         error = unmapped_error;
0755         if (start >= end)
0756             goto out;
0757         if (prev)
0758             vma = prev->vm_next;
0759         else    /* madvise_remove dropped mmap_sem */
0760             vma = find_vma(current->mm, start);
0761     }
0762 out:
0763     blk_finish_plug(&plug);
0764     if (write)
0765         up_write(&current->mm->mmap_sem);
0766     else
0767         up_read(&current->mm->mmap_sem);
0768 
0769     return error;
0770 }