madvise.c 19 KB

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