madvise.c 20 KB

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