madvise.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. *prev = vma;
  204. #ifdef CONFIG_SWAP
  205. if (!file) {
  206. force_swapin_readahead(vma, start, end);
  207. return 0;
  208. }
  209. if (shmem_mapping(file->f_mapping)) {
  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. start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  223. if (end > vma->vm_end)
  224. end = vma->vm_end;
  225. end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  226. force_page_cache_readahead(file->f_mapping, file, start, end - start);
  227. return 0;
  228. }
  229. static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
  230. unsigned long end, struct mm_walk *walk)
  231. {
  232. struct mmu_gather *tlb = walk->private;
  233. struct mm_struct *mm = tlb->mm;
  234. struct vm_area_struct *vma = walk->vma;
  235. spinlock_t *ptl;
  236. pte_t *orig_pte, *pte, ptent;
  237. struct page *page;
  238. int nr_swap = 0;
  239. unsigned long next;
  240. next = pmd_addr_end(addr, end);
  241. if (pmd_trans_huge(*pmd))
  242. if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next))
  243. goto next;
  244. if (pmd_trans_unstable(pmd))
  245. return 0;
  246. orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  247. flush_tlb_batched_pending(mm);
  248. arch_enter_lazy_mmu_mode();
  249. for (; addr != end; pte++, addr += PAGE_SIZE) {
  250. ptent = *pte;
  251. if (pte_none(ptent))
  252. continue;
  253. /*
  254. * If the pte has swp_entry, just clear page table to
  255. * prevent swap-in which is more expensive rather than
  256. * (page allocation + zeroing).
  257. */
  258. if (!pte_present(ptent)) {
  259. swp_entry_t entry;
  260. entry = pte_to_swp_entry(ptent);
  261. if (non_swap_entry(entry))
  262. continue;
  263. nr_swap--;
  264. free_swap_and_cache(entry);
  265. pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
  266. continue;
  267. }
  268. page = vm_normal_page(vma, addr, ptent);
  269. if (!page)
  270. continue;
  271. /*
  272. * If pmd isn't transhuge but the page is THP and
  273. * is owned by only this process, split it and
  274. * deactivate all pages.
  275. */
  276. if (PageTransCompound(page)) {
  277. if (page_mapcount(page) != 1)
  278. goto out;
  279. get_page(page);
  280. if (!trylock_page(page)) {
  281. put_page(page);
  282. goto out;
  283. }
  284. pte_unmap_unlock(orig_pte, ptl);
  285. if (split_huge_page(page)) {
  286. unlock_page(page);
  287. put_page(page);
  288. pte_offset_map_lock(mm, pmd, addr, &ptl);
  289. goto out;
  290. }
  291. unlock_page(page);
  292. put_page(page);
  293. pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  294. pte--;
  295. addr -= PAGE_SIZE;
  296. continue;
  297. }
  298. VM_BUG_ON_PAGE(PageTransCompound(page), page);
  299. if (PageSwapCache(page) || PageDirty(page)) {
  300. if (!trylock_page(page))
  301. continue;
  302. /*
  303. * If page is shared with others, we couldn't clear
  304. * PG_dirty of the page.
  305. */
  306. if (page_mapcount(page) != 1) {
  307. unlock_page(page);
  308. continue;
  309. }
  310. if (PageSwapCache(page) && !try_to_free_swap(page)) {
  311. unlock_page(page);
  312. continue;
  313. }
  314. ClearPageDirty(page);
  315. unlock_page(page);
  316. }
  317. if (pte_young(ptent) || pte_dirty(ptent)) {
  318. /*
  319. * Some of architecture(ex, PPC) don't update TLB
  320. * with set_pte_at and tlb_remove_tlb_entry so for
  321. * the portability, remap the pte with old|clean
  322. * after pte clearing.
  323. */
  324. ptent = ptep_get_and_clear_full(mm, addr, pte,
  325. tlb->fullmm);
  326. ptent = pte_mkold(ptent);
  327. ptent = pte_mkclean(ptent);
  328. set_pte_at(mm, addr, pte, ptent);
  329. if (PageActive(page))
  330. deactivate_page(page);
  331. tlb_remove_tlb_entry(tlb, pte, addr);
  332. }
  333. }
  334. out:
  335. if (nr_swap) {
  336. if (current->mm == mm)
  337. sync_mm_rss(mm);
  338. add_mm_counter(mm, MM_SWAPENTS, nr_swap);
  339. }
  340. arch_leave_lazy_mmu_mode();
  341. pte_unmap_unlock(orig_pte, ptl);
  342. cond_resched();
  343. next:
  344. return 0;
  345. }
  346. static void madvise_free_page_range(struct mmu_gather *tlb,
  347. struct vm_area_struct *vma,
  348. unsigned long addr, unsigned long end)
  349. {
  350. struct mm_walk free_walk = {
  351. .pmd_entry = madvise_free_pte_range,
  352. .mm = vma->vm_mm,
  353. .private = tlb,
  354. };
  355. tlb_start_vma(tlb, vma);
  356. walk_page_range(addr, end, &free_walk);
  357. tlb_end_vma(tlb, vma);
  358. }
  359. static int madvise_free_single_vma(struct vm_area_struct *vma,
  360. unsigned long start_addr, unsigned long end_addr)
  361. {
  362. unsigned long start, end;
  363. struct mm_struct *mm = vma->vm_mm;
  364. struct mmu_gather tlb;
  365. if (vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP))
  366. return -EINVAL;
  367. /* MADV_FREE works for only anon vma at the moment */
  368. if (!vma_is_anonymous(vma))
  369. return -EINVAL;
  370. start = max(vma->vm_start, start_addr);
  371. if (start >= vma->vm_end)
  372. return -EINVAL;
  373. end = min(vma->vm_end, end_addr);
  374. if (end <= vma->vm_start)
  375. return -EINVAL;
  376. lru_add_drain();
  377. tlb_gather_mmu(&tlb, mm, start, end);
  378. update_hiwater_rss(mm);
  379. mmu_notifier_invalidate_range_start(mm, start, end);
  380. madvise_free_page_range(&tlb, vma, start, end);
  381. mmu_notifier_invalidate_range_end(mm, start, end);
  382. tlb_finish_mmu(&tlb, start, end);
  383. return 0;
  384. }
  385. static long madvise_free(struct vm_area_struct *vma,
  386. struct vm_area_struct **prev,
  387. unsigned long start, unsigned long end)
  388. {
  389. *prev = vma;
  390. return madvise_free_single_vma(vma, start, end);
  391. }
  392. /*
  393. * Application no longer needs these pages. If the pages are dirty,
  394. * it's OK to just throw them away. The app will be more careful about
  395. * data it wants to keep. Be sure to free swap resources too. The
  396. * zap_page_range call sets things up for shrink_active_list to actually free
  397. * these pages later if no one else has touched them in the meantime,
  398. * although we could add these pages to a global reuse list for
  399. * shrink_active_list to pick up before reclaiming other pages.
  400. *
  401. * NB: This interface discards data rather than pushes it out to swap,
  402. * as some implementations do. This has performance implications for
  403. * applications like large transactional databases which want to discard
  404. * pages in anonymous maps after committing to backing store the data
  405. * that was kept in them. There is no reason to write this data out to
  406. * the swap area if the application is discarding it.
  407. *
  408. * An interface that causes the system to free clean pages and flush
  409. * dirty pages is already available as msync(MS_INVALIDATE).
  410. */
  411. static long madvise_dontneed(struct vm_area_struct *vma,
  412. struct vm_area_struct **prev,
  413. unsigned long start, unsigned long end)
  414. {
  415. *prev = vma;
  416. if (vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP))
  417. return -EINVAL;
  418. zap_page_range(vma, start, end - start, NULL);
  419. return 0;
  420. }
  421. /*
  422. * Application wants to free up the pages and associated backing store.
  423. * This is effectively punching a hole into the middle of a file.
  424. */
  425. static long madvise_remove(struct vm_area_struct *vma,
  426. struct vm_area_struct **prev,
  427. unsigned long start, unsigned long end)
  428. {
  429. loff_t offset;
  430. int error;
  431. struct file *f;
  432. *prev = NULL; /* tell sys_madvise we drop mmap_sem */
  433. if (vma->vm_flags & VM_LOCKED)
  434. return -EINVAL;
  435. f = vma->vm_file;
  436. if (!f || !f->f_mapping || !f->f_mapping->host) {
  437. return -EINVAL;
  438. }
  439. if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
  440. return -EACCES;
  441. offset = (loff_t)(start - vma->vm_start)
  442. + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  443. /*
  444. * Filesystem's fallocate may need to take i_mutex. We need to
  445. * explicitly grab a reference because the vma (and hence the
  446. * vma's reference to the file) can go away as soon as we drop
  447. * mmap_sem.
  448. */
  449. get_file(f);
  450. up_read(&current->mm->mmap_sem);
  451. error = vfs_fallocate(f,
  452. FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
  453. offset, end - start);
  454. fput(f);
  455. down_read(&current->mm->mmap_sem);
  456. return error;
  457. }
  458. #ifdef CONFIG_MEMORY_FAILURE
  459. /*
  460. * Error injection support for memory error handling.
  461. */
  462. static int madvise_hwpoison(int bhv, unsigned long start, unsigned long end)
  463. {
  464. struct page *p;
  465. struct zone *zone;
  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. /* Ensure that all poisoned pages are removed from per-cpu lists */
  493. for_each_populated_zone(zone)
  494. drain_all_pages(zone);
  495. return 0;
  496. }
  497. #endif
  498. static long
  499. madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
  500. unsigned long start, unsigned long end, int behavior)
  501. {
  502. switch (behavior) {
  503. case MADV_REMOVE:
  504. return madvise_remove(vma, prev, start, end);
  505. case MADV_WILLNEED:
  506. return madvise_willneed(vma, prev, start, end);
  507. case MADV_FREE:
  508. /*
  509. * XXX: In this implementation, MADV_FREE works like
  510. * MADV_DONTNEED on swapless system or full swap.
  511. */
  512. if (get_nr_swap_pages() > 0)
  513. return madvise_free(vma, prev, start, end);
  514. /* passthrough */
  515. case MADV_DONTNEED:
  516. return madvise_dontneed(vma, prev, start, end);
  517. default:
  518. return madvise_behavior(vma, prev, start, end, behavior);
  519. }
  520. }
  521. static bool
  522. madvise_behavior_valid(int behavior)
  523. {
  524. switch (behavior) {
  525. case MADV_DOFORK:
  526. case MADV_DONTFORK:
  527. case MADV_NORMAL:
  528. case MADV_SEQUENTIAL:
  529. case MADV_RANDOM:
  530. case MADV_REMOVE:
  531. case MADV_WILLNEED:
  532. case MADV_DONTNEED:
  533. case MADV_FREE:
  534. #ifdef CONFIG_KSM
  535. case MADV_MERGEABLE:
  536. case MADV_UNMERGEABLE:
  537. #endif
  538. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  539. case MADV_HUGEPAGE:
  540. case MADV_NOHUGEPAGE:
  541. #endif
  542. case MADV_DONTDUMP:
  543. case MADV_DODUMP:
  544. return true;
  545. default:
  546. return false;
  547. }
  548. }
  549. /*
  550. * The madvise(2) system call.
  551. *
  552. * Applications can use madvise() to advise the kernel how it should
  553. * handle paging I/O in this VM area. The idea is to help the kernel
  554. * use appropriate read-ahead and caching techniques. The information
  555. * provided is advisory only, and can be safely disregarded by the
  556. * kernel without affecting the correct operation of the application.
  557. *
  558. * behavior values:
  559. * MADV_NORMAL - the default behavior is to read clusters. This
  560. * results in some read-ahead and read-behind.
  561. * MADV_RANDOM - the system should read the minimum amount of data
  562. * on any access, since it is unlikely that the appli-
  563. * cation will need more than what it asks for.
  564. * MADV_SEQUENTIAL - pages in the given range will probably be accessed
  565. * once, so they can be aggressively read ahead, and
  566. * can be freed soon after they are accessed.
  567. * MADV_WILLNEED - the application is notifying the system to read
  568. * some pages ahead.
  569. * MADV_DONTNEED - the application is finished with the given range,
  570. * so the kernel can free resources associated with it.
  571. * MADV_FREE - the application marks pages in the given range as lazy free,
  572. * where actual purges are postponed until memory pressure happens.
  573. * MADV_REMOVE - the application wants to free up the given range of
  574. * pages and associated backing store.
  575. * MADV_DONTFORK - omit this area from child's address space when forking:
  576. * typically, to avoid COWing pages pinned by get_user_pages().
  577. * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
  578. * MADV_HWPOISON - trigger memory error handler as if the given memory range
  579. * were corrupted by unrecoverable hardware memory failure.
  580. * MADV_SOFT_OFFLINE - try to soft-offline the given range of memory.
  581. * MADV_MERGEABLE - the application recommends that KSM try to merge pages in
  582. * this area with pages of identical content from other such areas.
  583. * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
  584. * MADV_HUGEPAGE - the application wants to back the given range by transparent
  585. * huge pages in the future. Existing pages might be coalesced and
  586. * new pages might be allocated as THP.
  587. * MADV_NOHUGEPAGE - mark the given range as not worth being backed by
  588. * transparent huge pages so the existing pages will not be
  589. * coalesced into THP and new pages will not be allocated as THP.
  590. * MADV_DONTDUMP - the application wants to prevent pages in the given range
  591. * from being included in its core dump.
  592. * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
  593. *
  594. * return values:
  595. * zero - success
  596. * -EINVAL - start + len < 0, start is not page-aligned,
  597. * "behavior" is not a valid value, or application
  598. * is attempting to release locked or shared pages.
  599. * -ENOMEM - addresses in the specified range are not currently
  600. * mapped, or are outside the AS of the process.
  601. * -EIO - an I/O error occurred while paging in data.
  602. * -EBADF - map exists, but area maps something that isn't a file.
  603. * -EAGAIN - a kernel resource was temporarily unavailable.
  604. */
  605. SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
  606. {
  607. unsigned long end, tmp;
  608. struct vm_area_struct *vma, *prev;
  609. int unmapped_error = 0;
  610. int error = -EINVAL;
  611. int write;
  612. size_t len;
  613. struct blk_plug plug;
  614. #ifdef CONFIG_MEMORY_FAILURE
  615. if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
  616. return madvise_hwpoison(behavior, start, start+len_in);
  617. #endif
  618. if (!madvise_behavior_valid(behavior))
  619. return error;
  620. if (start & ~PAGE_MASK)
  621. return error;
  622. len = (len_in + ~PAGE_MASK) & PAGE_MASK;
  623. /* Check to see whether len was rounded up from small -ve to zero */
  624. if (len_in && !len)
  625. return error;
  626. end = start + len;
  627. if (end < start)
  628. return error;
  629. error = 0;
  630. if (end == start)
  631. return error;
  632. write = madvise_need_mmap_write(behavior);
  633. if (write) {
  634. if (down_write_killable(&current->mm->mmap_sem))
  635. return -EINTR;
  636. } else {
  637. down_read(&current->mm->mmap_sem);
  638. }
  639. /*
  640. * If the interval [start,end) covers some unmapped address
  641. * ranges, just ignore them, but return -ENOMEM at the end.
  642. * - different from the way of handling in mlock etc.
  643. */
  644. vma = find_vma_prev(current->mm, start, &prev);
  645. if (vma && start > vma->vm_start)
  646. prev = vma;
  647. blk_start_plug(&plug);
  648. for (;;) {
  649. /* Still start < end. */
  650. error = -ENOMEM;
  651. if (!vma)
  652. goto out;
  653. /* Here start < (end|vma->vm_end). */
  654. if (start < vma->vm_start) {
  655. unmapped_error = -ENOMEM;
  656. start = vma->vm_start;
  657. if (start >= end)
  658. goto out;
  659. }
  660. /* Here vma->vm_start <= start < (end|vma->vm_end) */
  661. tmp = vma->vm_end;
  662. if (end < tmp)
  663. tmp = end;
  664. /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
  665. error = madvise_vma(vma, &prev, start, tmp, behavior);
  666. if (error)
  667. goto out;
  668. start = tmp;
  669. if (prev && start < prev->vm_end)
  670. start = prev->vm_end;
  671. error = unmapped_error;
  672. if (start >= end)
  673. goto out;
  674. if (prev)
  675. vma = prev->vm_next;
  676. else /* madvise_remove dropped mmap_sem */
  677. vma = find_vma(current->mm, start);
  678. }
  679. out:
  680. blk_finish_plug(&plug);
  681. if (write)
  682. up_write(&current->mm->mmap_sem);
  683. else
  684. up_read(&current->mm->mmap_sem);
  685. return error;
  686. }