madvise.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/mm/madvise.c
  4. *
  5. * Copyright (C) 1999 Linus Torvalds
  6. * Copyright (C) 2002 Christoph Hellwig
  7. */
  8. #include <linux/mman.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/syscalls.h>
  11. #include <linux/mempolicy.h>
  12. #include <linux/page-isolation.h>
  13. #include <linux/page_idle.h>
  14. #include <linux/userfaultfd_k.h>
  15. #include <linux/hugetlb.h>
  16. #include <linux/falloc.h>
  17. #include <linux/fadvise.h>
  18. #include <linux/sched.h>
  19. #include <linux/sched/mm.h>
  20. #include <linux/mm_inline.h>
  21. #include <linux/string.h>
  22. #include <linux/uio.h>
  23. #include <linux/ksm.h>
  24. #include <linux/fs.h>
  25. #include <linux/file.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/backing-dev.h>
  28. #include <linux/pagewalk.h>
  29. #include <linux/swap.h>
  30. #include <linux/swapops.h>
  31. #include <linux/shmem_fs.h>
  32. #include <linux/mmu_notifier.h>
  33. #include <trace/hooks/mm.h>
  34. #include <asm/tlb.h>
  35. #include "internal.h"
  36. #include "swap.h"
  37. struct madvise_walk_private {
  38. struct mmu_gather *tlb;
  39. bool pageout;
  40. };
  41. /*
  42. * Any behaviour which results in changes to the vma->vm_flags needs to
  43. * take mmap_lock for writing. Others, which simply traverse vmas, need
  44. * to only take it for reading.
  45. */
  46. static int madvise_need_mmap_write(int behavior)
  47. {
  48. switch (behavior) {
  49. case MADV_REMOVE:
  50. case MADV_WILLNEED:
  51. case MADV_DONTNEED:
  52. case MADV_DONTNEED_LOCKED:
  53. case MADV_COLD:
  54. case MADV_PAGEOUT:
  55. case MADV_FREE:
  56. case MADV_POPULATE_READ:
  57. case MADV_POPULATE_WRITE:
  58. case MADV_COLLAPSE:
  59. return 0;
  60. default:
  61. /* be safe, default to 1. list exceptions explicitly */
  62. return 1;
  63. }
  64. }
  65. #ifdef CONFIG_ANON_VMA_NAME
  66. struct anon_vma_name *anon_vma_name_alloc(const char *name)
  67. {
  68. struct anon_vma_name *anon_name;
  69. size_t count;
  70. /* Add 1 for NUL terminator at the end of the anon_name->name */
  71. count = strlen(name) + 1;
  72. anon_name = kmalloc(struct_size(anon_name, name, count), GFP_KERNEL);
  73. if (anon_name) {
  74. kref_init(&anon_name->kref);
  75. memcpy(anon_name->name, name, count);
  76. }
  77. return anon_name;
  78. }
  79. void anon_vma_name_free(struct kref *kref)
  80. {
  81. struct anon_vma_name *anon_name =
  82. container_of(kref, struct anon_vma_name, kref);
  83. kfree(anon_name);
  84. }
  85. struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma)
  86. {
  87. mmap_assert_locked(vma->vm_mm);
  88. if (vma->vm_file)
  89. return NULL;
  90. return vma->anon_name;
  91. }
  92. /* mmap_lock should be write-locked */
  93. static int replace_anon_vma_name(struct vm_area_struct *vma,
  94. struct anon_vma_name *anon_name)
  95. {
  96. struct anon_vma_name *orig_name = anon_vma_name(vma);
  97. if (!anon_name) {
  98. vma->anon_name = NULL;
  99. anon_vma_name_put(orig_name);
  100. return 0;
  101. }
  102. if (anon_vma_name_eq(orig_name, anon_name))
  103. return 0;
  104. vma->anon_name = anon_vma_name_reuse(anon_name);
  105. anon_vma_name_put(orig_name);
  106. return 0;
  107. }
  108. #else /* CONFIG_ANON_VMA_NAME */
  109. static int replace_anon_vma_name(struct vm_area_struct *vma,
  110. struct anon_vma_name *anon_name)
  111. {
  112. if (anon_name)
  113. return -EINVAL;
  114. return 0;
  115. }
  116. #endif /* CONFIG_ANON_VMA_NAME */
  117. /*
  118. * Update the vm_flags on region of a vma, splitting it or merging it as
  119. * necessary. Must be called with mmap_sem held for writing;
  120. * Caller should ensure anon_name stability by raising its refcount even when
  121. * anon_name belongs to a valid vma because this function might free that vma.
  122. */
  123. static int madvise_update_vma(struct vm_area_struct *vma,
  124. struct vm_area_struct **prev, unsigned long start,
  125. unsigned long end, unsigned long new_flags,
  126. struct anon_vma_name *anon_name)
  127. {
  128. struct mm_struct *mm = vma->vm_mm;
  129. int error;
  130. pgoff_t pgoff;
  131. if (new_flags == vma->vm_flags && anon_vma_name_eq(anon_vma_name(vma), anon_name)) {
  132. *prev = vma;
  133. return 0;
  134. }
  135. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  136. *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
  137. vma->vm_file, pgoff, vma_policy(vma),
  138. vma->vm_userfaultfd_ctx, anon_name);
  139. if (*prev) {
  140. vma = *prev;
  141. goto success;
  142. }
  143. *prev = vma;
  144. if (start != vma->vm_start) {
  145. if (unlikely(mm->map_count >= sysctl_max_map_count))
  146. return -ENOMEM;
  147. error = __split_vma(mm, vma, start, 1);
  148. if (error)
  149. return error;
  150. }
  151. if (end != vma->vm_end) {
  152. if (unlikely(mm->map_count >= sysctl_max_map_count))
  153. return -ENOMEM;
  154. error = __split_vma(mm, vma, end, 0);
  155. if (error)
  156. return error;
  157. }
  158. success:
  159. /* vm_flags is protected by the mmap_lock held in write mode. */
  160. vma_start_write(vma);
  161. vm_flags_reset(vma, new_flags);
  162. if (!vma->vm_file) {
  163. error = replace_anon_vma_name(vma, anon_name);
  164. if (error)
  165. return error;
  166. }
  167. return 0;
  168. }
  169. #ifdef CONFIG_SWAP
  170. static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
  171. unsigned long end, struct mm_walk *walk)
  172. {
  173. struct vm_area_struct *vma = walk->private;
  174. unsigned long index;
  175. struct swap_iocb *splug = NULL;
  176. if (pmd_none_or_trans_huge_or_clear_bad(pmd))
  177. return 0;
  178. for (index = start; index != end; index += PAGE_SIZE) {
  179. pte_t pte;
  180. swp_entry_t entry;
  181. struct page *page;
  182. spinlock_t *ptl;
  183. pte_t *ptep;
  184. ptep = pte_offset_map_lock(vma->vm_mm, pmd, index, &ptl);
  185. pte = *ptep;
  186. pte_unmap_unlock(ptep, ptl);
  187. if (!is_swap_pte(pte))
  188. continue;
  189. entry = pte_to_swp_entry(pte);
  190. if (unlikely(non_swap_entry(entry)))
  191. continue;
  192. trace_android_vh_madvise_swapin_walk_pmd_entry(entry);
  193. page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE,
  194. vma, index, &splug);
  195. if (page)
  196. put_page(page);
  197. }
  198. swap_read_unplug(splug);
  199. return 0;
  200. }
  201. static const struct mm_walk_ops swapin_walk_ops = {
  202. .pmd_entry = swapin_walk_pmd_entry,
  203. .walk_lock = PGWALK_RDLOCK,
  204. };
  205. static void force_shm_swapin_readahead(struct vm_area_struct *vma,
  206. unsigned long start, unsigned long end,
  207. struct address_space *mapping)
  208. {
  209. XA_STATE(xas, &mapping->i_pages, linear_page_index(vma, start));
  210. pgoff_t end_index = linear_page_index(vma, end + PAGE_SIZE - 1);
  211. struct page *page;
  212. struct swap_iocb *splug = NULL;
  213. rcu_read_lock();
  214. xas_for_each(&xas, page, end_index) {
  215. swp_entry_t swap;
  216. if (!xa_is_value(page))
  217. continue;
  218. swap = radix_to_swp_entry(page);
  219. /* There might be swapin error entries in shmem mapping. */
  220. if (non_swap_entry(swap))
  221. continue;
  222. xas_pause(&xas);
  223. rcu_read_unlock();
  224. page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE,
  225. NULL, 0, &splug);
  226. if (page)
  227. put_page(page);
  228. rcu_read_lock();
  229. }
  230. rcu_read_unlock();
  231. swap_read_unplug(splug);
  232. lru_add_drain(); /* Push any new pages onto the LRU now */
  233. }
  234. #endif /* CONFIG_SWAP */
  235. /*
  236. * Schedule all required I/O operations. Do not wait for completion.
  237. */
  238. static long madvise_willneed(struct vm_area_struct *vma,
  239. struct vm_area_struct **prev,
  240. unsigned long start, unsigned long end)
  241. {
  242. struct mm_struct *mm = vma->vm_mm;
  243. struct file *file = vma->vm_file;
  244. loff_t offset;
  245. *prev = vma;
  246. #ifdef CONFIG_SWAP
  247. if (!file) {
  248. walk_page_range(vma->vm_mm, start, end, &swapin_walk_ops, vma);
  249. lru_add_drain(); /* Push any new pages onto the LRU now */
  250. return 0;
  251. }
  252. if (shmem_mapping(file->f_mapping)) {
  253. force_shm_swapin_readahead(vma, start, end,
  254. file->f_mapping);
  255. return 0;
  256. }
  257. #else
  258. if (!file)
  259. return -EBADF;
  260. #endif
  261. if (IS_DAX(file_inode(file))) {
  262. /* no bad return value, but ignore advice */
  263. return 0;
  264. }
  265. /*
  266. * Filesystem's fadvise may need to take various locks. We need to
  267. * explicitly grab a reference because the vma (and hence the
  268. * vma's reference to the file) can go away as soon as we drop
  269. * mmap_lock.
  270. */
  271. *prev = NULL; /* tell sys_madvise we drop mmap_lock */
  272. get_file(file);
  273. offset = (loff_t)(start - vma->vm_start)
  274. + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  275. mmap_read_unlock(mm);
  276. vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED);
  277. fput(file);
  278. mmap_read_lock(mm);
  279. return 0;
  280. }
  281. static inline bool can_do_file_pageout(struct vm_area_struct *vma)
  282. {
  283. if (!vma->vm_file)
  284. return false;
  285. /*
  286. * paging out pagecache only for non-anonymous mappings that correspond
  287. * to the files the calling process could (if tried) open for writing;
  288. * otherwise we'd be including shared non-exclusive mappings, which
  289. * opens a side channel.
  290. */
  291. return inode_owner_or_capable(&init_user_ns,
  292. file_inode(vma->vm_file)) ||
  293. file_permission(vma->vm_file, MAY_WRITE) == 0;
  294. }
  295. static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
  296. unsigned long addr, unsigned long end,
  297. struct mm_walk *walk)
  298. {
  299. struct madvise_walk_private *private = walk->private;
  300. struct mmu_gather *tlb = private->tlb;
  301. bool pageout = private->pageout;
  302. struct mm_struct *mm = tlb->mm;
  303. struct vm_area_struct *vma = walk->vma;
  304. pte_t *orig_pte, *pte, ptent;
  305. spinlock_t *ptl;
  306. struct page *page = NULL;
  307. LIST_HEAD(page_list);
  308. bool pageout_anon_only_filter;
  309. swp_entry_t entry;
  310. if (fatal_signal_pending(current))
  311. return -EINTR;
  312. pageout_anon_only_filter = pageout && !vma_is_anonymous(vma) &&
  313. !can_do_file_pageout(vma);
  314. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  315. if (pmd_trans_huge(*pmd)) {
  316. pmd_t orig_pmd;
  317. unsigned long next = pmd_addr_end(addr, end);
  318. tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
  319. ptl = pmd_trans_huge_lock(pmd, vma);
  320. if (!ptl)
  321. return 0;
  322. orig_pmd = *pmd;
  323. if (is_huge_zero_pmd(orig_pmd))
  324. goto huge_unlock;
  325. if (unlikely(!pmd_present(orig_pmd))) {
  326. VM_BUG_ON(thp_migration_supported() &&
  327. !is_pmd_migration_entry(orig_pmd));
  328. goto huge_unlock;
  329. }
  330. page = pmd_page(orig_pmd);
  331. /* Do not interfere with other mappings of this page */
  332. if (page_mapcount(page) != 1)
  333. goto huge_unlock;
  334. if (pageout_anon_only_filter && !PageAnon(page))
  335. goto huge_unlock;
  336. if (next - addr != HPAGE_PMD_SIZE) {
  337. int err;
  338. get_page(page);
  339. spin_unlock(ptl);
  340. lock_page(page);
  341. err = split_huge_page(page);
  342. unlock_page(page);
  343. put_page(page);
  344. if (!err)
  345. goto regular_page;
  346. return 0;
  347. }
  348. if (pmd_young(orig_pmd)) {
  349. pmdp_invalidate(vma, addr, pmd);
  350. orig_pmd = pmd_mkold(orig_pmd);
  351. set_pmd_at(mm, addr, pmd, orig_pmd);
  352. tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
  353. }
  354. ClearPageReferenced(page);
  355. test_and_clear_page_young(page);
  356. if (pageout) {
  357. if (!isolate_lru_page(page)) {
  358. if (PageUnevictable(page))
  359. putback_lru_page(page);
  360. else
  361. list_add(&page->lru, &page_list);
  362. }
  363. } else
  364. deactivate_page(page);
  365. huge_unlock:
  366. spin_unlock(ptl);
  367. if (pageout)
  368. reclaim_pages(&page_list);
  369. return 0;
  370. }
  371. regular_page:
  372. if (pmd_trans_unstable(pmd))
  373. return 0;
  374. #endif
  375. tlb_change_page_size(tlb, PAGE_SIZE);
  376. orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  377. flush_tlb_batched_pending(mm);
  378. arch_enter_lazy_mmu_mode();
  379. for (; addr < end; pte++, addr += PAGE_SIZE) {
  380. bool need_skip = false;
  381. ptent = *pte;
  382. if (pte_none(ptent))
  383. continue;
  384. if (!pte_present(ptent)) {
  385. entry = pte_to_swp_entry(ptent);
  386. trace_android_vh_madvise_pageout_swap_entry(entry,
  387. swp_swapcount(entry));
  388. continue;
  389. }
  390. page = vm_normal_page(vma, addr, ptent);
  391. if (!page || is_zone_device_page(page))
  392. continue;
  393. trace_android_vh_madvise_cold_pageout_skip(vma, page, pageout,
  394. &need_skip);
  395. if (need_skip)
  396. continue;
  397. /*
  398. * Creating a THP page is expensive so split it only if we
  399. * are sure it's worth. Split it if we are only owner.
  400. */
  401. if (PageTransCompound(page)) {
  402. if (page_mapcount(page) != 1)
  403. break;
  404. if (pageout_anon_only_filter && !PageAnon(page))
  405. break;
  406. get_page(page);
  407. if (!trylock_page(page)) {
  408. put_page(page);
  409. break;
  410. }
  411. pte_unmap_unlock(orig_pte, ptl);
  412. if (split_huge_page(page)) {
  413. unlock_page(page);
  414. put_page(page);
  415. orig_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  416. break;
  417. }
  418. unlock_page(page);
  419. put_page(page);
  420. orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  421. pte--;
  422. addr -= PAGE_SIZE;
  423. continue;
  424. }
  425. /*
  426. * Do not interfere with other mappings of this page and
  427. * non-LRU page.
  428. */
  429. if (!PageLRU(page) || page_mapcount(page) != 1)
  430. continue;
  431. if (pageout_anon_only_filter && !PageAnon(page))
  432. continue;
  433. VM_BUG_ON_PAGE(PageTransCompound(page), page);
  434. if (pte_young(ptent)) {
  435. ptent = ptep_get_and_clear_full(mm, addr, pte,
  436. tlb->fullmm);
  437. ptent = pte_mkold(ptent);
  438. set_pte_at(mm, addr, pte, ptent);
  439. tlb_remove_tlb_entry(tlb, pte, addr);
  440. }
  441. /*
  442. * We are deactivating a page for accelerating reclaiming.
  443. * VM couldn't reclaim the page unless we clear PG_young.
  444. * As a side effect, it makes confuse idle-page tracking
  445. * because they will miss recent referenced history.
  446. */
  447. ClearPageReferenced(page);
  448. test_and_clear_page_young(page);
  449. if (pageout) {
  450. if (!isolate_lru_page(page)) {
  451. if (PageUnevictable(page))
  452. putback_lru_page(page);
  453. else
  454. list_add(&page->lru, &page_list);
  455. }
  456. } else
  457. deactivate_page(page);
  458. }
  459. arch_leave_lazy_mmu_mode();
  460. pte_unmap_unlock(orig_pte, ptl);
  461. if (pageout)
  462. reclaim_pages(&page_list);
  463. cond_resched();
  464. return 0;
  465. }
  466. static const struct mm_walk_ops cold_walk_ops = {
  467. .pmd_entry = madvise_cold_or_pageout_pte_range,
  468. .walk_lock = PGWALK_RDLOCK,
  469. };
  470. static void madvise_cold_page_range(struct mmu_gather *tlb,
  471. struct vm_area_struct *vma,
  472. unsigned long addr, unsigned long end)
  473. {
  474. struct madvise_walk_private walk_private = {
  475. .pageout = false,
  476. .tlb = tlb,
  477. };
  478. tlb_start_vma(tlb, vma);
  479. walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
  480. tlb_end_vma(tlb, vma);
  481. }
  482. static inline bool can_madv_lru_vma(struct vm_area_struct *vma)
  483. {
  484. return !(vma->vm_flags & (VM_LOCKED|VM_PFNMAP|VM_HUGETLB));
  485. }
  486. static long madvise_cold(struct vm_area_struct *vma,
  487. struct vm_area_struct **prev,
  488. unsigned long start_addr, unsigned long end_addr)
  489. {
  490. struct mm_struct *mm = vma->vm_mm;
  491. struct mmu_gather tlb;
  492. *prev = vma;
  493. if (!can_madv_lru_vma(vma))
  494. return -EINVAL;
  495. lru_add_drain();
  496. tlb_gather_mmu(&tlb, mm);
  497. madvise_cold_page_range(&tlb, vma, start_addr, end_addr);
  498. tlb_finish_mmu(&tlb);
  499. return 0;
  500. }
  501. static void madvise_pageout_page_range(struct mmu_gather *tlb,
  502. struct vm_area_struct *vma,
  503. unsigned long addr, unsigned long end)
  504. {
  505. struct madvise_walk_private walk_private = {
  506. .pageout = true,
  507. .tlb = tlb,
  508. };
  509. tlb_start_vma(tlb, vma);
  510. walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
  511. tlb_end_vma(tlb, vma);
  512. }
  513. static long madvise_pageout(struct vm_area_struct *vma,
  514. struct vm_area_struct **prev,
  515. unsigned long start_addr, unsigned long end_addr)
  516. {
  517. struct mm_struct *mm = vma->vm_mm;
  518. struct mmu_gather tlb;
  519. *prev = vma;
  520. if (!can_madv_lru_vma(vma))
  521. return -EINVAL;
  522. /*
  523. * If the VMA belongs to a private file mapping, there can be private
  524. * dirty pages which can be paged out if even this process is neither
  525. * owner nor write capable of the file. We allow private file mappings
  526. * further to pageout dirty anon pages.
  527. */
  528. if (!vma_is_anonymous(vma) && (!can_do_file_pageout(vma) &&
  529. (vma->vm_flags & VM_MAYSHARE)))
  530. return 0;
  531. lru_add_drain();
  532. tlb_gather_mmu(&tlb, mm);
  533. madvise_pageout_page_range(&tlb, vma, start_addr, end_addr);
  534. tlb_finish_mmu(&tlb);
  535. return 0;
  536. }
  537. static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
  538. unsigned long end, struct mm_walk *walk)
  539. {
  540. struct mmu_gather *tlb = walk->private;
  541. struct mm_struct *mm = tlb->mm;
  542. struct vm_area_struct *vma = walk->vma;
  543. spinlock_t *ptl;
  544. pte_t *orig_pte, *pte, ptent;
  545. struct folio *folio;
  546. struct page *page;
  547. int nr_swap = 0;
  548. unsigned long next;
  549. next = pmd_addr_end(addr, end);
  550. if (pmd_trans_huge(*pmd))
  551. if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next))
  552. goto next;
  553. if (pmd_trans_unstable(pmd))
  554. return 0;
  555. tlb_change_page_size(tlb, PAGE_SIZE);
  556. orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  557. flush_tlb_batched_pending(mm);
  558. arch_enter_lazy_mmu_mode();
  559. for (; addr != end; pte++, addr += PAGE_SIZE) {
  560. ptent = *pte;
  561. if (pte_none(ptent))
  562. continue;
  563. /*
  564. * If the pte has swp_entry, just clear page table to
  565. * prevent swap-in which is more expensive rather than
  566. * (page allocation + zeroing).
  567. */
  568. if (!pte_present(ptent)) {
  569. swp_entry_t entry;
  570. entry = pte_to_swp_entry(ptent);
  571. if (!non_swap_entry(entry)) {
  572. nr_swap--;
  573. free_swap_and_cache(entry);
  574. pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
  575. } else if (is_hwpoison_entry(entry) ||
  576. is_swapin_error_entry(entry)) {
  577. pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
  578. }
  579. continue;
  580. }
  581. page = vm_normal_page(vma, addr, ptent);
  582. if (!page || is_zone_device_page(page))
  583. continue;
  584. folio = page_folio(page);
  585. /*
  586. * If pmd isn't transhuge but the folio is large and
  587. * is owned by only this process, split it and
  588. * deactivate all pages.
  589. */
  590. if (folio_test_large(folio)) {
  591. if (folio_estimated_sharers(folio) != 1)
  592. break;
  593. folio_get(folio);
  594. if (!folio_trylock(folio)) {
  595. folio_put(folio);
  596. goto out;
  597. }
  598. pte_unmap_unlock(orig_pte, ptl);
  599. if (split_folio(folio)) {
  600. folio_unlock(folio);
  601. folio_put(folio);
  602. orig_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  603. goto out;
  604. }
  605. folio_unlock(folio);
  606. folio_put(folio);
  607. orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  608. pte--;
  609. addr -= PAGE_SIZE;
  610. continue;
  611. }
  612. if (folio_test_swapcache(folio) || folio_test_dirty(folio)) {
  613. if (!folio_trylock(folio))
  614. continue;
  615. /*
  616. * If folio is shared with others, we mustn't clear
  617. * the folio's dirty flag.
  618. */
  619. if (folio_mapcount(folio) != 1) {
  620. folio_unlock(folio);
  621. continue;
  622. }
  623. if (folio_test_swapcache(folio) &&
  624. !folio_free_swap(folio)) {
  625. folio_unlock(folio);
  626. continue;
  627. }
  628. folio_clear_dirty(folio);
  629. folio_unlock(folio);
  630. }
  631. if (pte_young(ptent) || pte_dirty(ptent)) {
  632. /*
  633. * Some of architecture(ex, PPC) don't update TLB
  634. * with set_pte_at and tlb_remove_tlb_entry so for
  635. * the portability, remap the pte with old|clean
  636. * after pte clearing.
  637. */
  638. ptent = ptep_get_and_clear_full(mm, addr, pte,
  639. tlb->fullmm);
  640. ptent = pte_mkold(ptent);
  641. ptent = pte_mkclean(ptent);
  642. set_pte_at(mm, addr, pte, ptent);
  643. tlb_remove_tlb_entry(tlb, pte, addr);
  644. }
  645. mark_page_lazyfree(&folio->page);
  646. }
  647. out:
  648. if (nr_swap) {
  649. if (current->mm == mm)
  650. sync_mm_rss(mm);
  651. add_mm_counter(mm, MM_SWAPENTS, nr_swap);
  652. }
  653. arch_leave_lazy_mmu_mode();
  654. pte_unmap_unlock(orig_pte, ptl);
  655. cond_resched();
  656. next:
  657. return 0;
  658. }
  659. static const struct mm_walk_ops madvise_free_walk_ops = {
  660. .pmd_entry = madvise_free_pte_range,
  661. .walk_lock = PGWALK_RDLOCK,
  662. };
  663. static int madvise_free_single_vma(struct vm_area_struct *vma,
  664. unsigned long start_addr, unsigned long end_addr)
  665. {
  666. struct mm_struct *mm = vma->vm_mm;
  667. struct mmu_notifier_range range;
  668. struct mmu_gather tlb;
  669. /* MADV_FREE works for only anon vma at the moment */
  670. if (!vma_is_anonymous(vma))
  671. return -EINVAL;
  672. range.start = max(vma->vm_start, start_addr);
  673. if (range.start >= vma->vm_end)
  674. return -EINVAL;
  675. range.end = min(vma->vm_end, end_addr);
  676. if (range.end <= vma->vm_start)
  677. return -EINVAL;
  678. mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
  679. range.start, range.end);
  680. lru_add_drain();
  681. tlb_gather_mmu(&tlb, mm);
  682. update_hiwater_rss(mm);
  683. mmu_notifier_invalidate_range_start(&range);
  684. tlb_start_vma(&tlb, vma);
  685. walk_page_range(vma->vm_mm, range.start, range.end,
  686. &madvise_free_walk_ops, &tlb);
  687. tlb_end_vma(&tlb, vma);
  688. mmu_notifier_invalidate_range_end(&range);
  689. tlb_finish_mmu(&tlb);
  690. return 0;
  691. }
  692. /*
  693. * Application no longer needs these pages. If the pages are dirty,
  694. * it's OK to just throw them away. The app will be more careful about
  695. * data it wants to keep. Be sure to free swap resources too. The
  696. * zap_page_range_single call sets things up for shrink_active_list to actually
  697. * free these pages later if no one else has touched them in the meantime,
  698. * although we could add these pages to a global reuse list for
  699. * shrink_active_list to pick up before reclaiming other pages.
  700. *
  701. * NB: This interface discards data rather than pushes it out to swap,
  702. * as some implementations do. This has performance implications for
  703. * applications like large transactional databases which want to discard
  704. * pages in anonymous maps after committing to backing store the data
  705. * that was kept in them. There is no reason to write this data out to
  706. * the swap area if the application is discarding it.
  707. *
  708. * An interface that causes the system to free clean pages and flush
  709. * dirty pages is already available as msync(MS_INVALIDATE).
  710. */
  711. static long madvise_dontneed_single_vma(struct vm_area_struct *vma,
  712. unsigned long start, unsigned long end)
  713. {
  714. zap_page_range_single(vma, start, end - start, NULL);
  715. return 0;
  716. }
  717. static bool madvise_dontneed_free_valid_vma(struct vm_area_struct *vma,
  718. unsigned long start,
  719. unsigned long *end,
  720. int behavior)
  721. {
  722. if (!is_vm_hugetlb_page(vma)) {
  723. unsigned int forbidden = VM_PFNMAP;
  724. if (behavior != MADV_DONTNEED_LOCKED)
  725. forbidden |= VM_LOCKED;
  726. return !(vma->vm_flags & forbidden);
  727. }
  728. if (behavior != MADV_DONTNEED && behavior != MADV_DONTNEED_LOCKED)
  729. return false;
  730. if (start & ~huge_page_mask(hstate_vma(vma)))
  731. return false;
  732. /*
  733. * Madvise callers expect the length to be rounded up to PAGE_SIZE
  734. * boundaries, and may be unaware that this VMA uses huge pages.
  735. * Avoid unexpected data loss by rounding down the number of
  736. * huge pages freed.
  737. */
  738. *end = ALIGN_DOWN(*end, huge_page_size(hstate_vma(vma)));
  739. return true;
  740. }
  741. static long madvise_dontneed_free(struct vm_area_struct *vma,
  742. struct vm_area_struct **prev,
  743. unsigned long start, unsigned long end,
  744. int behavior)
  745. {
  746. struct mm_struct *mm = vma->vm_mm;
  747. *prev = vma;
  748. if (!madvise_dontneed_free_valid_vma(vma, start, &end, behavior))
  749. return -EINVAL;
  750. if (start == end)
  751. return 0;
  752. if (!userfaultfd_remove(vma, start, end)) {
  753. *prev = NULL; /* mmap_lock has been dropped, prev is stale */
  754. mmap_read_lock(mm);
  755. vma = find_vma(mm, start);
  756. if (!vma)
  757. return -ENOMEM;
  758. if (start < vma->vm_start) {
  759. /*
  760. * This "vma" under revalidation is the one
  761. * with the lowest vma->vm_start where start
  762. * is also < vma->vm_end. If start <
  763. * vma->vm_start it means an hole materialized
  764. * in the user address space within the
  765. * virtual range passed to MADV_DONTNEED
  766. * or MADV_FREE.
  767. */
  768. return -ENOMEM;
  769. }
  770. /*
  771. * Potential end adjustment for hugetlb vma is OK as
  772. * the check below keeps end within vma.
  773. */
  774. if (!madvise_dontneed_free_valid_vma(vma, start, &end,
  775. behavior))
  776. return -EINVAL;
  777. if (end > vma->vm_end) {
  778. /*
  779. * Don't fail if end > vma->vm_end. If the old
  780. * vma was split while the mmap_lock was
  781. * released the effect of the concurrent
  782. * operation may not cause madvise() to
  783. * have an undefined result. There may be an
  784. * adjacent next vma that we'll walk
  785. * next. userfaultfd_remove() will generate an
  786. * UFFD_EVENT_REMOVE repetition on the
  787. * end-vma->vm_end range, but the manager can
  788. * handle a repetition fine.
  789. */
  790. end = vma->vm_end;
  791. }
  792. VM_WARN_ON(start >= end);
  793. }
  794. if (behavior == MADV_DONTNEED || behavior == MADV_DONTNEED_LOCKED)
  795. return madvise_dontneed_single_vma(vma, start, end);
  796. else if (behavior == MADV_FREE)
  797. return madvise_free_single_vma(vma, start, end);
  798. else
  799. return -EINVAL;
  800. }
  801. static long madvise_populate(struct vm_area_struct *vma,
  802. struct vm_area_struct **prev,
  803. unsigned long start, unsigned long end,
  804. int behavior)
  805. {
  806. const bool write = behavior == MADV_POPULATE_WRITE;
  807. struct mm_struct *mm = vma->vm_mm;
  808. unsigned long tmp_end;
  809. int locked = 1;
  810. long pages;
  811. *prev = vma;
  812. while (start < end) {
  813. /*
  814. * We might have temporarily dropped the lock. For example,
  815. * our VMA might have been split.
  816. */
  817. if (!vma || start >= vma->vm_end) {
  818. vma = vma_lookup(mm, start);
  819. if (!vma)
  820. return -ENOMEM;
  821. }
  822. tmp_end = min_t(unsigned long, end, vma->vm_end);
  823. /* Populate (prefault) page tables readable/writable. */
  824. pages = faultin_vma_page_range(vma, start, tmp_end, write,
  825. &locked);
  826. if (!locked) {
  827. mmap_read_lock(mm);
  828. locked = 1;
  829. *prev = NULL;
  830. vma = NULL;
  831. }
  832. if (pages < 0) {
  833. switch (pages) {
  834. case -EINTR:
  835. return -EINTR;
  836. case -EINVAL: /* Incompatible mappings / permissions. */
  837. return -EINVAL;
  838. case -EHWPOISON:
  839. return -EHWPOISON;
  840. case -EFAULT: /* VM_FAULT_SIGBUS or VM_FAULT_SIGSEGV */
  841. return -EFAULT;
  842. default:
  843. pr_warn_once("%s: unhandled return value: %ld\n",
  844. __func__, pages);
  845. fallthrough;
  846. case -ENOMEM:
  847. return -ENOMEM;
  848. }
  849. }
  850. start += pages * PAGE_SIZE;
  851. }
  852. return 0;
  853. }
  854. /*
  855. * Application wants to free up the pages and associated backing store.
  856. * This is effectively punching a hole into the middle of a file.
  857. */
  858. static long madvise_remove(struct vm_area_struct *vma,
  859. struct vm_area_struct **prev,
  860. unsigned long start, unsigned long end)
  861. {
  862. loff_t offset;
  863. int error;
  864. struct file *f;
  865. struct mm_struct *mm = vma->vm_mm;
  866. *prev = NULL; /* tell sys_madvise we drop mmap_lock */
  867. if (vma->vm_flags & VM_LOCKED)
  868. return -EINVAL;
  869. f = vma->vm_file;
  870. if (!f || !f->f_mapping || !f->f_mapping->host) {
  871. return -EINVAL;
  872. }
  873. if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
  874. return -EACCES;
  875. offset = (loff_t)(start - vma->vm_start)
  876. + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  877. /*
  878. * Filesystem's fallocate may need to take i_rwsem. We need to
  879. * explicitly grab a reference because the vma (and hence the
  880. * vma's reference to the file) can go away as soon as we drop
  881. * mmap_lock.
  882. */
  883. get_file(f);
  884. if (userfaultfd_remove(vma, start, end)) {
  885. /* mmap_lock was not released by userfaultfd_remove() */
  886. mmap_read_unlock(mm);
  887. }
  888. error = vfs_fallocate(f,
  889. FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
  890. offset, end - start);
  891. fput(f);
  892. mmap_read_lock(mm);
  893. return error;
  894. }
  895. /*
  896. * Apply an madvise behavior to a region of a vma. madvise_update_vma
  897. * will handle splitting a vm area into separate areas, each area with its own
  898. * behavior.
  899. */
  900. static int madvise_vma_behavior(struct vm_area_struct *vma,
  901. struct vm_area_struct **prev,
  902. unsigned long start, unsigned long end,
  903. unsigned long behavior)
  904. {
  905. int error;
  906. struct anon_vma_name *anon_name;
  907. unsigned long new_flags = vma->vm_flags;
  908. switch (behavior) {
  909. case MADV_REMOVE:
  910. return madvise_remove(vma, prev, start, end);
  911. case MADV_WILLNEED:
  912. return madvise_willneed(vma, prev, start, end);
  913. case MADV_COLD:
  914. return madvise_cold(vma, prev, start, end);
  915. case MADV_PAGEOUT:
  916. return madvise_pageout(vma, prev, start, end);
  917. case MADV_FREE:
  918. case MADV_DONTNEED:
  919. case MADV_DONTNEED_LOCKED:
  920. return madvise_dontneed_free(vma, prev, start, end, behavior);
  921. case MADV_POPULATE_READ:
  922. case MADV_POPULATE_WRITE:
  923. return madvise_populate(vma, prev, start, end, behavior);
  924. case MADV_NORMAL:
  925. new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
  926. break;
  927. case MADV_SEQUENTIAL:
  928. new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
  929. break;
  930. case MADV_RANDOM:
  931. new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
  932. break;
  933. case MADV_DONTFORK:
  934. new_flags |= VM_DONTCOPY;
  935. break;
  936. case MADV_DOFORK:
  937. if (vma->vm_flags & VM_IO)
  938. return -EINVAL;
  939. new_flags &= ~VM_DONTCOPY;
  940. break;
  941. case MADV_WIPEONFORK:
  942. /* MADV_WIPEONFORK is only supported on anonymous memory. */
  943. if (vma->vm_file || vma->vm_flags & VM_SHARED)
  944. return -EINVAL;
  945. new_flags |= VM_WIPEONFORK;
  946. break;
  947. case MADV_KEEPONFORK:
  948. new_flags &= ~VM_WIPEONFORK;
  949. break;
  950. case MADV_DONTDUMP:
  951. new_flags |= VM_DONTDUMP;
  952. break;
  953. case MADV_DODUMP:
  954. if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL)
  955. return -EINVAL;
  956. new_flags &= ~VM_DONTDUMP;
  957. break;
  958. case MADV_MERGEABLE:
  959. case MADV_UNMERGEABLE:
  960. error = ksm_madvise(vma, start, end, behavior, &new_flags);
  961. if (error)
  962. goto out;
  963. break;
  964. case MADV_HUGEPAGE:
  965. case MADV_NOHUGEPAGE:
  966. error = hugepage_madvise(vma, &new_flags, behavior);
  967. if (error)
  968. goto out;
  969. break;
  970. case MADV_COLLAPSE:
  971. return madvise_collapse(vma, prev, start, end);
  972. }
  973. anon_name = anon_vma_name(vma);
  974. anon_vma_name_get(anon_name);
  975. error = madvise_update_vma(vma, prev, start, end, new_flags,
  976. anon_name);
  977. anon_vma_name_put(anon_name);
  978. out:
  979. /*
  980. * madvise() returns EAGAIN if kernel resources, such as
  981. * slab, are temporarily unavailable.
  982. */
  983. if (error == -ENOMEM)
  984. error = -EAGAIN;
  985. return error;
  986. }
  987. #ifdef CONFIG_MEMORY_FAILURE
  988. /*
  989. * Error injection support for memory error handling.
  990. */
  991. static int madvise_inject_error(int behavior,
  992. unsigned long start, unsigned long end)
  993. {
  994. unsigned long size;
  995. if (!capable(CAP_SYS_ADMIN))
  996. return -EPERM;
  997. for (; start < end; start += size) {
  998. unsigned long pfn;
  999. struct page *page;
  1000. int ret;
  1001. ret = get_user_pages_fast(start, 1, 0, &page);
  1002. if (ret != 1)
  1003. return ret;
  1004. pfn = page_to_pfn(page);
  1005. /*
  1006. * When soft offlining hugepages, after migrating the page
  1007. * we dissolve it, therefore in the second loop "page" will
  1008. * no longer be a compound page.
  1009. */
  1010. size = page_size(compound_head(page));
  1011. if (behavior == MADV_SOFT_OFFLINE) {
  1012. pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
  1013. pfn, start);
  1014. ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
  1015. } else {
  1016. pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
  1017. pfn, start);
  1018. ret = memory_failure(pfn, MF_COUNT_INCREASED | MF_SW_SIMULATED);
  1019. if (ret == -EOPNOTSUPP)
  1020. ret = 0;
  1021. }
  1022. if (ret)
  1023. return ret;
  1024. }
  1025. return 0;
  1026. }
  1027. #endif
  1028. static bool
  1029. madvise_behavior_valid(int behavior)
  1030. {
  1031. switch (behavior) {
  1032. case MADV_DOFORK:
  1033. case MADV_DONTFORK:
  1034. case MADV_NORMAL:
  1035. case MADV_SEQUENTIAL:
  1036. case MADV_RANDOM:
  1037. case MADV_REMOVE:
  1038. case MADV_WILLNEED:
  1039. case MADV_DONTNEED:
  1040. case MADV_DONTNEED_LOCKED:
  1041. case MADV_FREE:
  1042. case MADV_COLD:
  1043. case MADV_PAGEOUT:
  1044. case MADV_POPULATE_READ:
  1045. case MADV_POPULATE_WRITE:
  1046. #ifdef CONFIG_KSM
  1047. case MADV_MERGEABLE:
  1048. case MADV_UNMERGEABLE:
  1049. #endif
  1050. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1051. case MADV_HUGEPAGE:
  1052. case MADV_NOHUGEPAGE:
  1053. case MADV_COLLAPSE:
  1054. #endif
  1055. case MADV_DONTDUMP:
  1056. case MADV_DODUMP:
  1057. case MADV_WIPEONFORK:
  1058. case MADV_KEEPONFORK:
  1059. #ifdef CONFIG_MEMORY_FAILURE
  1060. case MADV_SOFT_OFFLINE:
  1061. case MADV_HWPOISON:
  1062. #endif
  1063. return true;
  1064. default:
  1065. return false;
  1066. }
  1067. }
  1068. static bool process_madvise_behavior_valid(int behavior)
  1069. {
  1070. switch (behavior) {
  1071. case MADV_COLD:
  1072. case MADV_PAGEOUT:
  1073. case MADV_WILLNEED:
  1074. case MADV_COLLAPSE:
  1075. return true;
  1076. default:
  1077. return false;
  1078. }
  1079. }
  1080. /*
  1081. * Walk the vmas in range [start,end), and call the visit function on each one.
  1082. * The visit function will get start and end parameters that cover the overlap
  1083. * between the current vma and the original range. Any unmapped regions in the
  1084. * original range will result in this function returning -ENOMEM while still
  1085. * calling the visit function on all of the existing vmas in the range.
  1086. * Must be called with the mmap_lock held for reading or writing.
  1087. */
  1088. static
  1089. int madvise_walk_vmas(struct mm_struct *mm, unsigned long start,
  1090. unsigned long end, unsigned long arg,
  1091. int (*visit)(struct vm_area_struct *vma,
  1092. struct vm_area_struct **prev, unsigned long start,
  1093. unsigned long end, unsigned long arg))
  1094. {
  1095. struct vm_area_struct *vma;
  1096. struct vm_area_struct *prev;
  1097. unsigned long tmp;
  1098. int unmapped_error = 0;
  1099. /*
  1100. * If the interval [start,end) covers some unmapped address
  1101. * ranges, just ignore them, but return -ENOMEM at the end.
  1102. * - different from the way of handling in mlock etc.
  1103. */
  1104. vma = find_vma_prev(mm, start, &prev);
  1105. if (vma && start > vma->vm_start)
  1106. prev = vma;
  1107. for (;;) {
  1108. int error;
  1109. /* Still start < end. */
  1110. if (!vma)
  1111. return -ENOMEM;
  1112. /* Here start < (end|vma->vm_end). */
  1113. if (start < vma->vm_start) {
  1114. unmapped_error = -ENOMEM;
  1115. start = vma->vm_start;
  1116. if (start >= end)
  1117. break;
  1118. }
  1119. /* Here vma->vm_start <= start < (end|vma->vm_end) */
  1120. tmp = vma->vm_end;
  1121. if (end < tmp)
  1122. tmp = end;
  1123. /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
  1124. error = visit(vma, &prev, start, tmp, arg);
  1125. if (error)
  1126. return error;
  1127. start = tmp;
  1128. if (prev && start < prev->vm_end)
  1129. start = prev->vm_end;
  1130. if (start >= end)
  1131. break;
  1132. if (prev)
  1133. vma = find_vma(mm, prev->vm_end);
  1134. else /* madvise_remove dropped mmap_lock */
  1135. vma = find_vma(mm, start);
  1136. }
  1137. return unmapped_error;
  1138. }
  1139. #ifdef CONFIG_ANON_VMA_NAME
  1140. static int madvise_vma_anon_name(struct vm_area_struct *vma,
  1141. struct vm_area_struct **prev,
  1142. unsigned long start, unsigned long end,
  1143. unsigned long anon_name)
  1144. {
  1145. int error;
  1146. /* Only anonymous mappings can be named */
  1147. if (vma->vm_file)
  1148. return -EBADF;
  1149. error = madvise_update_vma(vma, prev, start, end, vma->vm_flags,
  1150. (struct anon_vma_name *)anon_name);
  1151. /*
  1152. * madvise() returns EAGAIN if kernel resources, such as
  1153. * slab, are temporarily unavailable.
  1154. */
  1155. if (error == -ENOMEM)
  1156. error = -EAGAIN;
  1157. return error;
  1158. }
  1159. int madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
  1160. unsigned long len_in, struct anon_vma_name *anon_name)
  1161. {
  1162. unsigned long end;
  1163. unsigned long len;
  1164. if (start & ~PAGE_MASK)
  1165. return -EINVAL;
  1166. len = (len_in + ~PAGE_MASK) & PAGE_MASK;
  1167. /* Check to see whether len was rounded up from small -ve to zero */
  1168. if (len_in && !len)
  1169. return -EINVAL;
  1170. end = start + len;
  1171. if (end < start)
  1172. return -EINVAL;
  1173. if (end == start)
  1174. return 0;
  1175. return madvise_walk_vmas(mm, start, end, (unsigned long)anon_name,
  1176. madvise_vma_anon_name);
  1177. }
  1178. #endif /* CONFIG_ANON_VMA_NAME */
  1179. /*
  1180. * The madvise(2) system call.
  1181. *
  1182. * Applications can use madvise() to advise the kernel how it should
  1183. * handle paging I/O in this VM area. The idea is to help the kernel
  1184. * use appropriate read-ahead and caching techniques. The information
  1185. * provided is advisory only, and can be safely disregarded by the
  1186. * kernel without affecting the correct operation of the application.
  1187. *
  1188. * behavior values:
  1189. * MADV_NORMAL - the default behavior is to read clusters. This
  1190. * results in some read-ahead and read-behind.
  1191. * MADV_RANDOM - the system should read the minimum amount of data
  1192. * on any access, since it is unlikely that the appli-
  1193. * cation will need more than what it asks for.
  1194. * MADV_SEQUENTIAL - pages in the given range will probably be accessed
  1195. * once, so they can be aggressively read ahead, and
  1196. * can be freed soon after they are accessed.
  1197. * MADV_WILLNEED - the application is notifying the system to read
  1198. * some pages ahead.
  1199. * MADV_DONTNEED - the application is finished with the given range,
  1200. * so the kernel can free resources associated with it.
  1201. * MADV_FREE - the application marks pages in the given range as lazy free,
  1202. * where actual purges are postponed until memory pressure happens.
  1203. * MADV_REMOVE - the application wants to free up the given range of
  1204. * pages and associated backing store.
  1205. * MADV_DONTFORK - omit this area from child's address space when forking:
  1206. * typically, to avoid COWing pages pinned by get_user_pages().
  1207. * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
  1208. * MADV_WIPEONFORK - present the child process with zero-filled memory in this
  1209. * range after a fork.
  1210. * MADV_KEEPONFORK - undo the effect of MADV_WIPEONFORK
  1211. * MADV_HWPOISON - trigger memory error handler as if the given memory range
  1212. * were corrupted by unrecoverable hardware memory failure.
  1213. * MADV_SOFT_OFFLINE - try to soft-offline the given range of memory.
  1214. * MADV_MERGEABLE - the application recommends that KSM try to merge pages in
  1215. * this area with pages of identical content from other such areas.
  1216. * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
  1217. * MADV_HUGEPAGE - the application wants to back the given range by transparent
  1218. * huge pages in the future. Existing pages might be coalesced and
  1219. * new pages might be allocated as THP.
  1220. * MADV_NOHUGEPAGE - mark the given range as not worth being backed by
  1221. * transparent huge pages so the existing pages will not be
  1222. * coalesced into THP and new pages will not be allocated as THP.
  1223. * MADV_COLLAPSE - synchronously coalesce pages into new THP.
  1224. * MADV_DONTDUMP - the application wants to prevent pages in the given range
  1225. * from being included in its core dump.
  1226. * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
  1227. * MADV_COLD - the application is not expected to use this memory soon,
  1228. * deactivate pages in this range so that they can be reclaimed
  1229. * easily if memory pressure happens.
  1230. * MADV_PAGEOUT - the application is not expected to use this memory soon,
  1231. * page out the pages in this range immediately.
  1232. * MADV_POPULATE_READ - populate (prefault) page tables readable by
  1233. * triggering read faults if required
  1234. * MADV_POPULATE_WRITE - populate (prefault) page tables writable by
  1235. * triggering write faults if required
  1236. *
  1237. * return values:
  1238. * zero - success
  1239. * -EINVAL - start + len < 0, start is not page-aligned,
  1240. * "behavior" is not a valid value, or application
  1241. * is attempting to release locked or shared pages,
  1242. * or the specified address range includes file, Huge TLB,
  1243. * MAP_SHARED or VMPFNMAP range.
  1244. * -ENOMEM - addresses in the specified range are not currently
  1245. * mapped, or are outside the AS of the process.
  1246. * -EIO - an I/O error occurred while paging in data.
  1247. * -EBADF - map exists, but area maps something that isn't a file.
  1248. * -EAGAIN - a kernel resource was temporarily unavailable.
  1249. */
  1250. int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior)
  1251. {
  1252. unsigned long end;
  1253. int error;
  1254. int write;
  1255. size_t len;
  1256. struct blk_plug plug;
  1257. start = untagged_addr(start);
  1258. if (!madvise_behavior_valid(behavior))
  1259. return -EINVAL;
  1260. if (!PAGE_ALIGNED(start))
  1261. return -EINVAL;
  1262. len = PAGE_ALIGN(len_in);
  1263. /* Check to see whether len was rounded up from small -ve to zero */
  1264. if (len_in && !len)
  1265. return -EINVAL;
  1266. end = start + len;
  1267. if (end < start)
  1268. return -EINVAL;
  1269. if (end == start)
  1270. return 0;
  1271. #ifdef CONFIG_MEMORY_FAILURE
  1272. if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
  1273. return madvise_inject_error(behavior, start, start + len_in);
  1274. #endif
  1275. write = madvise_need_mmap_write(behavior);
  1276. if (write) {
  1277. if (mmap_write_lock_killable(mm))
  1278. return -EINTR;
  1279. } else {
  1280. mmap_read_lock(mm);
  1281. }
  1282. blk_start_plug(&plug);
  1283. error = madvise_walk_vmas(mm, start, end, behavior,
  1284. madvise_vma_behavior);
  1285. blk_finish_plug(&plug);
  1286. if (write)
  1287. mmap_write_unlock(mm);
  1288. else
  1289. mmap_read_unlock(mm);
  1290. return error;
  1291. }
  1292. SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
  1293. {
  1294. return do_madvise(current->mm, start, len_in, behavior);
  1295. }
  1296. SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
  1297. size_t, vlen, int, behavior, unsigned int, flags)
  1298. {
  1299. ssize_t ret;
  1300. struct iovec iovstack[UIO_FASTIOV], iovec;
  1301. struct iovec *iov = iovstack;
  1302. struct iov_iter iter;
  1303. struct task_struct *task;
  1304. struct mm_struct *mm;
  1305. size_t total_len;
  1306. unsigned int f_flags;
  1307. if (flags != 0) {
  1308. ret = -EINVAL;
  1309. goto out;
  1310. }
  1311. ret = import_iovec(ITER_DEST, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
  1312. if (ret < 0)
  1313. goto out;
  1314. task = pidfd_get_task(pidfd, &f_flags);
  1315. if (IS_ERR(task)) {
  1316. ret = PTR_ERR(task);
  1317. goto free_iov;
  1318. }
  1319. if (!process_madvise_behavior_valid(behavior)) {
  1320. ret = -EINVAL;
  1321. goto release_task;
  1322. }
  1323. /* Require PTRACE_MODE_READ to avoid leaking ASLR metadata. */
  1324. mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
  1325. if (IS_ERR_OR_NULL(mm)) {
  1326. ret = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
  1327. goto release_task;
  1328. }
  1329. /*
  1330. * Require CAP_SYS_NICE for influencing process performance. Note that
  1331. * only non-destructive hints are currently supported.
  1332. */
  1333. if (!capable(CAP_SYS_NICE)) {
  1334. ret = -EPERM;
  1335. goto release_mm;
  1336. }
  1337. total_len = iov_iter_count(&iter);
  1338. while (iov_iter_count(&iter)) {
  1339. iovec = iov_iter_iovec(&iter);
  1340. ret = do_madvise(mm, (unsigned long)iovec.iov_base,
  1341. iovec.iov_len, behavior);
  1342. if (ret < 0)
  1343. break;
  1344. iov_iter_advance(&iter, iovec.iov_len);
  1345. }
  1346. trace_android_vh_process_madvise_end(behavior, &ret);
  1347. ret = (total_len - iov_iter_count(&iter)) ? : ret;
  1348. release_mm:
  1349. mmput(mm);
  1350. release_task:
  1351. put_task_struct(task);
  1352. free_iov:
  1353. kfree(iov);
  1354. out:
  1355. return ret;
  1356. }