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. #include "internal.h"
  26. /*
  27. * Any behaviour which results in changes to the vma->vm_flags needs to
  28. * take mmap_sem for writing. Others, which simply traverse vmas, need
  29. * to only take it for reading.
  30. */
  31. static int madvise_need_mmap_write(int behavior)
  32. {
  33. switch (behavior) {
  34. case MADV_REMOVE:
  35. case MADV_WILLNEED:
  36. case MADV_DONTNEED:
  37. case MADV_FREE:
  38. return 0;
  39. default:
  40. /* be safe, default to 1. list exceptions explicitly */
  41. return 1;
  42. }
  43. }
  44. /*
  45. * We can potentially split a vm area into separate
  46. * areas, each area with its own behavior.
  47. */
  48. static long madvise_behavior(struct vm_area_struct *vma,
  49. struct vm_area_struct **prev,
  50. unsigned long start, unsigned long end, int behavior)
  51. {
  52. struct mm_struct *mm = vma->vm_mm;
  53. int error = 0;
  54. pgoff_t pgoff;
  55. unsigned long new_flags = vma->vm_flags;
  56. switch (behavior) {
  57. case MADV_NORMAL:
  58. new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
  59. break;
  60. case MADV_SEQUENTIAL:
  61. new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
  62. break;
  63. case MADV_RANDOM:
  64. new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
  65. break;
  66. case MADV_DONTFORK:
  67. new_flags |= VM_DONTCOPY;
  68. break;
  69. case MADV_DOFORK:
  70. if (vma->vm_flags & VM_IO) {
  71. error = -EINVAL;
  72. goto out;
  73. }
  74. new_flags &= ~VM_DONTCOPY;
  75. break;
  76. case MADV_DONTDUMP:
  77. new_flags |= VM_DONTDUMP;
  78. break;
  79. case MADV_DODUMP:
  80. if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) {
  81. error = -EINVAL;
  82. goto out;
  83. }
  84. new_flags &= ~VM_DONTDUMP;
  85. break;
  86. case MADV_MERGEABLE:
  87. case MADV_UNMERGEABLE:
  88. error = ksm_madvise(vma, start, end, behavior, &new_flags);
  89. if (error)
  90. goto out;
  91. break;
  92. case MADV_HUGEPAGE:
  93. case MADV_NOHUGEPAGE:
  94. error = hugepage_madvise(vma, &new_flags, behavior);
  95. if (error)
  96. goto out;
  97. break;
  98. }
  99. if (new_flags == vma->vm_flags) {
  100. *prev = vma;
  101. goto out;
  102. }
  103. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  104. *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
  105. vma->vm_file, pgoff, vma_policy(vma),
  106. vma->vm_userfaultfd_ctx, vma_get_anon_name(vma));
  107. if (*prev) {
  108. vma = *prev;
  109. goto success;
  110. }
  111. *prev = vma;
  112. if (start != vma->vm_start) {
  113. error = split_vma(mm, vma, start, 1);
  114. if (error)
  115. goto out;
  116. }
  117. if (end != vma->vm_end) {
  118. error = split_vma(mm, vma, end, 0);
  119. if (error)
  120. goto out;
  121. }
  122. success:
  123. /*
  124. * vm_flags is protected by the mmap_sem held in write mode.
  125. */
  126. vma->vm_flags = new_flags;
  127. out:
  128. if (error == -ENOMEM)
  129. error = -EAGAIN;
  130. return error;
  131. }
  132. #ifdef CONFIG_SWAP
  133. static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
  134. unsigned long end, struct mm_walk *walk)
  135. {
  136. pte_t *orig_pte;
  137. struct vm_area_struct *vma = walk->private;
  138. unsigned long index;
  139. if (pmd_none_or_trans_huge_or_clear_bad(pmd))
  140. return 0;
  141. for (index = start; index != end; index += PAGE_SIZE) {
  142. pte_t pte;
  143. swp_entry_t entry;
  144. struct page *page;
  145. spinlock_t *ptl;
  146. orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl);
  147. pte = *(orig_pte + ((index - start) / PAGE_SIZE));
  148. pte_unmap_unlock(orig_pte, ptl);
  149. if (pte_present(pte) || pte_none(pte))
  150. continue;
  151. entry = pte_to_swp_entry(pte);
  152. if (unlikely(non_swap_entry(entry)))
  153. continue;
  154. page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE,
  155. vma, index);
  156. if (page)
  157. put_page(page);
  158. }
  159. return 0;
  160. }
  161. static void force_swapin_readahead(struct vm_area_struct *vma,
  162. unsigned long start, unsigned long end)
  163. {
  164. struct mm_walk walk = {
  165. .mm = vma->vm_mm,
  166. .pmd_entry = swapin_walk_pmd_entry,
  167. .private = vma,
  168. };
  169. walk_page_range(start, end, &walk);
  170. lru_add_drain(); /* Push any new pages onto the LRU now */
  171. }
  172. static void force_shm_swapin_readahead(struct vm_area_struct *vma,
  173. unsigned long start, unsigned long end,
  174. struct address_space *mapping)
  175. {
  176. pgoff_t index;
  177. struct page *page;
  178. swp_entry_t swap;
  179. for (; start < end; start += PAGE_SIZE) {
  180. index = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  181. page = find_get_entry(mapping, index);
  182. if (!radix_tree_exceptional_entry(page)) {
  183. if (page)
  184. put_page(page);
  185. continue;
  186. }
  187. swap = radix_to_swp_entry(page);
  188. page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE,
  189. NULL, 0);
  190. if (page)
  191. put_page(page);
  192. }
  193. lru_add_drain(); /* Push any new pages onto the LRU now */
  194. }
  195. #endif /* CONFIG_SWAP */
  196. /*
  197. * Schedule all required I/O operations. Do not wait for completion.
  198. */
  199. static long madvise_willneed(struct vm_area_struct *vma,
  200. struct vm_area_struct **prev,
  201. unsigned long start, unsigned long end)
  202. {
  203. struct file *file = vma->vm_file;
  204. *prev = vma;
  205. #ifdef CONFIG_SWAP
  206. if (!file) {
  207. force_swapin_readahead(vma, start, end);
  208. return 0;
  209. }
  210. if (shmem_mapping(file->f_mapping)) {
  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. 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. orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  248. flush_tlb_batched_pending(mm);
  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. unlock_page(page);
  293. put_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 (!can_madv_dontneed_vma(vma))
  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. struct zone *zone;
  467. if (!capable(CAP_SYS_ADMIN))
  468. return -EPERM;
  469. for (; start < end; start += PAGE_SIZE <<
  470. compound_order(compound_head(p))) {
  471. int ret;
  472. ret = get_user_pages_fast(start, 1, 0, &p);
  473. if (ret != 1)
  474. return ret;
  475. if (PageHWPoison(p)) {
  476. put_page(p);
  477. continue;
  478. }
  479. if (bhv == MADV_SOFT_OFFLINE) {
  480. pr_info("Soft offlining page %#lx at %#lx\n",
  481. page_to_pfn(p), start);
  482. ret = soft_offline_page(p, MF_COUNT_INCREASED);
  483. if (ret)
  484. return ret;
  485. continue;
  486. }
  487. pr_info("Injecting memory failure for page %#lx at %#lx\n",
  488. page_to_pfn(p), start);
  489. ret = memory_failure(page_to_pfn(p), 0, MF_COUNT_INCREASED);
  490. if (ret)
  491. return ret;
  492. }
  493. /* Ensure that all poisoned pages are removed from per-cpu lists */
  494. for_each_populated_zone(zone)
  495. drain_all_pages(zone);
  496. return 0;
  497. }
  498. #endif
  499. static long
  500. madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
  501. unsigned long start, unsigned long end, int behavior)
  502. {
  503. switch (behavior) {
  504. case MADV_REMOVE:
  505. return madvise_remove(vma, prev, start, end);
  506. case MADV_WILLNEED:
  507. return madvise_willneed(vma, prev, start, end);
  508. case MADV_FREE:
  509. /*
  510. * XXX: In this implementation, MADV_FREE works like
  511. * MADV_DONTNEED on swapless system or full swap.
  512. */
  513. if (get_nr_swap_pages() > 0)
  514. return madvise_free(vma, prev, start, end);
  515. /* passthrough */
  516. case MADV_DONTNEED:
  517. return madvise_dontneed(vma, prev, start, end);
  518. default:
  519. return madvise_behavior(vma, prev, start, end, behavior);
  520. }
  521. }
  522. static bool
  523. madvise_behavior_valid(int behavior)
  524. {
  525. switch (behavior) {
  526. case MADV_DOFORK:
  527. case MADV_DONTFORK:
  528. case MADV_NORMAL:
  529. case MADV_SEQUENTIAL:
  530. case MADV_RANDOM:
  531. case MADV_REMOVE:
  532. case MADV_WILLNEED:
  533. case MADV_DONTNEED:
  534. case MADV_FREE:
  535. #ifdef CONFIG_KSM
  536. case MADV_MERGEABLE:
  537. case MADV_UNMERGEABLE:
  538. #endif
  539. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  540. case MADV_HUGEPAGE:
  541. case MADV_NOHUGEPAGE:
  542. #endif
  543. case MADV_DONTDUMP:
  544. case MADV_DODUMP:
  545. return true;
  546. default:
  547. return false;
  548. }
  549. }
  550. /*
  551. * The madvise(2) system call.
  552. *
  553. * Applications can use madvise() to advise the kernel how it should
  554. * handle paging I/O in this VM area. The idea is to help the kernel
  555. * use appropriate read-ahead and caching techniques. The information
  556. * provided is advisory only, and can be safely disregarded by the
  557. * kernel without affecting the correct operation of the application.
  558. *
  559. * behavior values:
  560. * MADV_NORMAL - the default behavior is to read clusters. This
  561. * results in some read-ahead and read-behind.
  562. * MADV_RANDOM - the system should read the minimum amount of data
  563. * on any access, since it is unlikely that the appli-
  564. * cation will need more than what it asks for.
  565. * MADV_SEQUENTIAL - pages in the given range will probably be accessed
  566. * once, so they can be aggressively read ahead, and
  567. * can be freed soon after they are accessed.
  568. * MADV_WILLNEED - the application is notifying the system to read
  569. * some pages ahead.
  570. * MADV_DONTNEED - the application is finished with the given range,
  571. * so the kernel can free resources associated with it.
  572. * MADV_FREE - the application marks pages in the given range as lazy free,
  573. * where actual purges are postponed until memory pressure happens.
  574. * MADV_REMOVE - the application wants to free up the given range of
  575. * pages and associated backing store.
  576. * MADV_DONTFORK - omit this area from child's address space when forking:
  577. * typically, to avoid COWing pages pinned by get_user_pages().
  578. * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
  579. * MADV_HWPOISON - trigger memory error handler as if the given memory range
  580. * were corrupted by unrecoverable hardware memory failure.
  581. * MADV_SOFT_OFFLINE - try to soft-offline the given range of memory.
  582. * MADV_MERGEABLE - the application recommends that KSM try to merge pages in
  583. * this area with pages of identical content from other such areas.
  584. * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
  585. * MADV_HUGEPAGE - the application wants to back the given range by transparent
  586. * huge pages in the future. Existing pages might be coalesced and
  587. * new pages might be allocated as THP.
  588. * MADV_NOHUGEPAGE - mark the given range as not worth being backed by
  589. * transparent huge pages so the existing pages will not be
  590. * coalesced into THP and new pages will not be allocated as THP.
  591. * MADV_DONTDUMP - the application wants to prevent pages in the given range
  592. * from being included in its core dump.
  593. * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
  594. *
  595. * return values:
  596. * zero - success
  597. * -EINVAL - start + len < 0, start is not page-aligned,
  598. * "behavior" is not a valid value, or application
  599. * is attempting to release locked or shared pages.
  600. * -ENOMEM - addresses in the specified range are not currently
  601. * mapped, or are outside the AS of the process.
  602. * -EIO - an I/O error occurred while paging in data.
  603. * -EBADF - map exists, but area maps something that isn't a file.
  604. * -EAGAIN - a kernel resource was temporarily unavailable.
  605. */
  606. SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
  607. {
  608. unsigned long end, tmp;
  609. struct vm_area_struct *vma, *prev;
  610. int unmapped_error = 0;
  611. int error = -EINVAL;
  612. int write;
  613. size_t len;
  614. struct blk_plug plug;
  615. #ifdef CONFIG_MEMORY_FAILURE
  616. if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
  617. return madvise_hwpoison(behavior, start, start+len_in);
  618. #endif
  619. if (!madvise_behavior_valid(behavior))
  620. return error;
  621. if (start & ~PAGE_MASK)
  622. return error;
  623. len = (len_in + ~PAGE_MASK) & PAGE_MASK;
  624. /* Check to see whether len was rounded up from small -ve to zero */
  625. if (len_in && !len)
  626. return error;
  627. end = start + len;
  628. if (end < start)
  629. return error;
  630. error = 0;
  631. if (end == start)
  632. return error;
  633. write = madvise_need_mmap_write(behavior);
  634. if (write) {
  635. if (down_write_killable(&current->mm->mmap_sem))
  636. return -EINTR;
  637. } else {
  638. down_read(&current->mm->mmap_sem);
  639. }
  640. /*
  641. * If the interval [start,end) covers some unmapped address
  642. * ranges, just ignore them, but return -ENOMEM at the end.
  643. * - different from the way of handling in mlock etc.
  644. */
  645. vma = find_vma_prev(current->mm, start, &prev);
  646. if (vma && start > vma->vm_start)
  647. prev = vma;
  648. blk_start_plug(&plug);
  649. for (;;) {
  650. /* Still start < end. */
  651. error = -ENOMEM;
  652. if (!vma)
  653. goto out;
  654. /* Here start < (end|vma->vm_end). */
  655. if (start < vma->vm_start) {
  656. unmapped_error = -ENOMEM;
  657. start = vma->vm_start;
  658. if (start >= end)
  659. goto out;
  660. }
  661. /* Here vma->vm_start <= start < (end|vma->vm_end) */
  662. tmp = vma->vm_end;
  663. if (end < tmp)
  664. tmp = end;
  665. /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
  666. error = madvise_vma(vma, &prev, start, tmp, behavior);
  667. if (error)
  668. goto out;
  669. start = tmp;
  670. if (prev && start < prev->vm_end)
  671. start = prev->vm_end;
  672. error = unmapped_error;
  673. if (start >= end)
  674. goto out;
  675. if (prev)
  676. vma = prev->vm_next;
  677. else /* madvise_remove dropped mmap_sem */
  678. vma = find_vma(current->mm, start);
  679. }
  680. out:
  681. blk_finish_plug(&plug);
  682. if (write)
  683. up_write(&current->mm->mmap_sem);
  684. else
  685. up_read(&current->mm->mmap_sem);
  686. return error;
  687. }