madvise.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. /*
  23. * Any behaviour which results in changes to the vma->vm_flags needs to
  24. * take mmap_sem for writing. Others, which simply traverse vmas, need
  25. * to only take it for reading.
  26. */
  27. static int madvise_need_mmap_write(int behavior)
  28. {
  29. switch (behavior) {
  30. case MADV_REMOVE:
  31. case MADV_WILLNEED:
  32. case MADV_DONTNEED:
  33. return 0;
  34. default:
  35. /* be safe, default to 1. list exceptions explicitly */
  36. return 1;
  37. }
  38. }
  39. /*
  40. * We can potentially split a vm area into separate
  41. * areas, each area with its own behavior.
  42. */
  43. static long madvise_behavior(struct vm_area_struct *vma,
  44. struct vm_area_struct **prev,
  45. unsigned long start, unsigned long end, int behavior)
  46. {
  47. struct mm_struct *mm = vma->vm_mm;
  48. int error = 0;
  49. pgoff_t pgoff;
  50. unsigned long new_flags = vma->vm_flags;
  51. switch (behavior) {
  52. case MADV_NORMAL:
  53. new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
  54. break;
  55. case MADV_SEQUENTIAL:
  56. new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
  57. break;
  58. case MADV_RANDOM:
  59. new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
  60. break;
  61. case MADV_DONTFORK:
  62. new_flags |= VM_DONTCOPY;
  63. break;
  64. case MADV_DOFORK:
  65. if (vma->vm_flags & VM_IO) {
  66. error = -EINVAL;
  67. goto out;
  68. }
  69. new_flags &= ~VM_DONTCOPY;
  70. break;
  71. case MADV_DONTDUMP:
  72. new_flags |= VM_DONTDUMP;
  73. break;
  74. case MADV_DODUMP:
  75. if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL) {
  76. error = -EINVAL;
  77. goto out;
  78. }
  79. new_flags &= ~VM_DONTDUMP;
  80. break;
  81. case MADV_MERGEABLE:
  82. case MADV_UNMERGEABLE:
  83. error = ksm_madvise(vma, start, end, behavior, &new_flags);
  84. if (error)
  85. goto out;
  86. break;
  87. case MADV_HUGEPAGE:
  88. case MADV_NOHUGEPAGE:
  89. error = hugepage_madvise(vma, &new_flags, behavior);
  90. if (error)
  91. goto out;
  92. break;
  93. }
  94. if (new_flags == vma->vm_flags) {
  95. *prev = vma;
  96. goto out;
  97. }
  98. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  99. *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
  100. vma->vm_file, pgoff, vma_policy(vma),
  101. vma->vm_userfaultfd_ctx);
  102. if (*prev) {
  103. vma = *prev;
  104. goto success;
  105. }
  106. *prev = vma;
  107. if (start != vma->vm_start) {
  108. error = split_vma(mm, vma, start, 1);
  109. if (error)
  110. goto out;
  111. }
  112. if (end != vma->vm_end) {
  113. error = split_vma(mm, vma, end, 0);
  114. if (error)
  115. goto out;
  116. }
  117. success:
  118. /*
  119. * vm_flags is protected by the mmap_sem held in write mode.
  120. */
  121. vma->vm_flags = new_flags;
  122. out:
  123. if (error == -ENOMEM)
  124. error = -EAGAIN;
  125. return error;
  126. }
  127. #ifdef CONFIG_SWAP
  128. static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
  129. unsigned long end, struct mm_walk *walk)
  130. {
  131. pte_t *orig_pte;
  132. struct vm_area_struct *vma = walk->private;
  133. unsigned long index;
  134. if (pmd_none_or_trans_huge_or_clear_bad(pmd))
  135. return 0;
  136. for (index = start; index != end; index += PAGE_SIZE) {
  137. pte_t pte;
  138. swp_entry_t entry;
  139. struct page *page;
  140. spinlock_t *ptl;
  141. orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl);
  142. pte = *(orig_pte + ((index - start) / PAGE_SIZE));
  143. pte_unmap_unlock(orig_pte, ptl);
  144. if (pte_present(pte) || pte_none(pte))
  145. continue;
  146. entry = pte_to_swp_entry(pte);
  147. if (unlikely(non_swap_entry(entry)))
  148. continue;
  149. page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE,
  150. vma, index);
  151. if (page)
  152. page_cache_release(page);
  153. }
  154. return 0;
  155. }
  156. static void force_swapin_readahead(struct vm_area_struct *vma,
  157. unsigned long start, unsigned long end)
  158. {
  159. struct mm_walk walk = {
  160. .mm = vma->vm_mm,
  161. .pmd_entry = swapin_walk_pmd_entry,
  162. .private = vma,
  163. };
  164. walk_page_range(start, end, &walk);
  165. lru_add_drain(); /* Push any new pages onto the LRU now */
  166. }
  167. static void force_shm_swapin_readahead(struct vm_area_struct *vma,
  168. unsigned long start, unsigned long end,
  169. struct address_space *mapping)
  170. {
  171. pgoff_t index;
  172. struct page *page;
  173. swp_entry_t swap;
  174. for (; start < end; start += PAGE_SIZE) {
  175. index = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  176. page = find_get_entry(mapping, index);
  177. if (!radix_tree_exceptional_entry(page)) {
  178. if (page)
  179. page_cache_release(page);
  180. continue;
  181. }
  182. swap = radix_to_swp_entry(page);
  183. page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE,
  184. NULL, 0);
  185. if (page)
  186. page_cache_release(page);
  187. }
  188. lru_add_drain(); /* Push any new pages onto the LRU now */
  189. }
  190. #endif /* CONFIG_SWAP */
  191. /*
  192. * Schedule all required I/O operations. Do not wait for completion.
  193. */
  194. static long madvise_willneed(struct vm_area_struct *vma,
  195. struct vm_area_struct **prev,
  196. unsigned long start, unsigned long end)
  197. {
  198. struct file *file = vma->vm_file;
  199. *prev = vma;
  200. #ifdef CONFIG_SWAP
  201. if (!file) {
  202. force_swapin_readahead(vma, start, end);
  203. return 0;
  204. }
  205. if (shmem_mapping(file->f_mapping)) {
  206. force_shm_swapin_readahead(vma, start, end,
  207. file->f_mapping);
  208. return 0;
  209. }
  210. #else
  211. if (!file)
  212. return -EBADF;
  213. #endif
  214. if (IS_DAX(file_inode(file))) {
  215. /* no bad return value, but ignore advice */
  216. return 0;
  217. }
  218. start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  219. if (end > vma->vm_end)
  220. end = vma->vm_end;
  221. end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  222. force_page_cache_readahead(file->f_mapping, file, start, end - start);
  223. return 0;
  224. }
  225. /*
  226. * Application no longer needs these pages. If the pages are dirty,
  227. * it's OK to just throw them away. The app will be more careful about
  228. * data it wants to keep. Be sure to free swap resources too. The
  229. * zap_page_range call sets things up for shrink_active_list to actually free
  230. * these pages later if no one else has touched them in the meantime,
  231. * although we could add these pages to a global reuse list for
  232. * shrink_active_list to pick up before reclaiming other pages.
  233. *
  234. * NB: This interface discards data rather than pushes it out to swap,
  235. * as some implementations do. This has performance implications for
  236. * applications like large transactional databases which want to discard
  237. * pages in anonymous maps after committing to backing store the data
  238. * that was kept in them. There is no reason to write this data out to
  239. * the swap area if the application is discarding it.
  240. *
  241. * An interface that causes the system to free clean pages and flush
  242. * dirty pages is already available as msync(MS_INVALIDATE).
  243. */
  244. static long madvise_dontneed(struct vm_area_struct *vma,
  245. struct vm_area_struct **prev,
  246. unsigned long start, unsigned long end)
  247. {
  248. *prev = vma;
  249. if (vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP))
  250. return -EINVAL;
  251. zap_page_range(vma, start, end - start, NULL);
  252. return 0;
  253. }
  254. /*
  255. * Application wants to free up the pages and associated backing store.
  256. * This is effectively punching a hole into the middle of a file.
  257. */
  258. static long madvise_remove(struct vm_area_struct *vma,
  259. struct vm_area_struct **prev,
  260. unsigned long start, unsigned long end)
  261. {
  262. loff_t offset;
  263. int error;
  264. struct file *f;
  265. *prev = NULL; /* tell sys_madvise we drop mmap_sem */
  266. if (vma->vm_flags & VM_LOCKED)
  267. return -EINVAL;
  268. f = vma->vm_file;
  269. if (!f || !f->f_mapping || !f->f_mapping->host) {
  270. return -EINVAL;
  271. }
  272. if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
  273. return -EACCES;
  274. offset = (loff_t)(start - vma->vm_start)
  275. + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  276. /*
  277. * Filesystem's fallocate may need to take i_mutex. We need to
  278. * explicitly grab a reference because the vma (and hence the
  279. * vma's reference to the file) can go away as soon as we drop
  280. * mmap_sem.
  281. */
  282. get_file(f);
  283. up_read(&current->mm->mmap_sem);
  284. error = vfs_fallocate(f,
  285. FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
  286. offset, end - start);
  287. fput(f);
  288. down_read(&current->mm->mmap_sem);
  289. return error;
  290. }
  291. #ifdef CONFIG_MEMORY_FAILURE
  292. /*
  293. * Error injection support for memory error handling.
  294. */
  295. static int madvise_hwpoison(int bhv, unsigned long start, unsigned long end)
  296. {
  297. struct page *p;
  298. if (!capable(CAP_SYS_ADMIN))
  299. return -EPERM;
  300. for (; start < end; start += PAGE_SIZE <<
  301. compound_order(compound_head(p))) {
  302. int ret;
  303. ret = get_user_pages_fast(start, 1, 0, &p);
  304. if (ret != 1)
  305. return ret;
  306. if (PageHWPoison(p)) {
  307. put_page(p);
  308. continue;
  309. }
  310. if (bhv == MADV_SOFT_OFFLINE) {
  311. pr_info("Soft offlining page %#lx at %#lx\n",
  312. page_to_pfn(p), start);
  313. ret = soft_offline_page(p, MF_COUNT_INCREASED);
  314. if (ret)
  315. return ret;
  316. continue;
  317. }
  318. pr_info("Injecting memory failure for page %#lx at %#lx\n",
  319. page_to_pfn(p), start);
  320. /* Ignore return value for now */
  321. memory_failure(page_to_pfn(p), 0, MF_COUNT_INCREASED);
  322. }
  323. return 0;
  324. }
  325. #endif
  326. static long
  327. madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
  328. unsigned long start, unsigned long end, int behavior)
  329. {
  330. switch (behavior) {
  331. case MADV_REMOVE:
  332. return madvise_remove(vma, prev, start, end);
  333. case MADV_WILLNEED:
  334. return madvise_willneed(vma, prev, start, end);
  335. case MADV_DONTNEED:
  336. return madvise_dontneed(vma, prev, start, end);
  337. default:
  338. return madvise_behavior(vma, prev, start, end, behavior);
  339. }
  340. }
  341. static bool
  342. madvise_behavior_valid(int behavior)
  343. {
  344. switch (behavior) {
  345. case MADV_DOFORK:
  346. case MADV_DONTFORK:
  347. case MADV_NORMAL:
  348. case MADV_SEQUENTIAL:
  349. case MADV_RANDOM:
  350. case MADV_REMOVE:
  351. case MADV_WILLNEED:
  352. case MADV_DONTNEED:
  353. #ifdef CONFIG_KSM
  354. case MADV_MERGEABLE:
  355. case MADV_UNMERGEABLE:
  356. #endif
  357. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  358. case MADV_HUGEPAGE:
  359. case MADV_NOHUGEPAGE:
  360. #endif
  361. case MADV_DONTDUMP:
  362. case MADV_DODUMP:
  363. return true;
  364. default:
  365. return false;
  366. }
  367. }
  368. /*
  369. * The madvise(2) system call.
  370. *
  371. * Applications can use madvise() to advise the kernel how it should
  372. * handle paging I/O in this VM area. The idea is to help the kernel
  373. * use appropriate read-ahead and caching techniques. The information
  374. * provided is advisory only, and can be safely disregarded by the
  375. * kernel without affecting the correct operation of the application.
  376. *
  377. * behavior values:
  378. * MADV_NORMAL - the default behavior is to read clusters. This
  379. * results in some read-ahead and read-behind.
  380. * MADV_RANDOM - the system should read the minimum amount of data
  381. * on any access, since it is unlikely that the appli-
  382. * cation will need more than what it asks for.
  383. * MADV_SEQUENTIAL - pages in the given range will probably be accessed
  384. * once, so they can be aggressively read ahead, and
  385. * can be freed soon after they are accessed.
  386. * MADV_WILLNEED - the application is notifying the system to read
  387. * some pages ahead.
  388. * MADV_DONTNEED - the application is finished with the given range,
  389. * so the kernel can free resources associated with it.
  390. * MADV_REMOVE - the application wants to free up the given range of
  391. * pages and associated backing store.
  392. * MADV_DONTFORK - omit this area from child's address space when forking:
  393. * typically, to avoid COWing pages pinned by get_user_pages().
  394. * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
  395. * MADV_MERGEABLE - the application recommends that KSM try to merge pages in
  396. * this area with pages of identical content from other such areas.
  397. * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
  398. *
  399. * return values:
  400. * zero - success
  401. * -EINVAL - start + len < 0, start is not page-aligned,
  402. * "behavior" is not a valid value, or application
  403. * is attempting to release locked or shared pages.
  404. * -ENOMEM - addresses in the specified range are not currently
  405. * mapped, or are outside the AS of the process.
  406. * -EIO - an I/O error occurred while paging in data.
  407. * -EBADF - map exists, but area maps something that isn't a file.
  408. * -EAGAIN - a kernel resource was temporarily unavailable.
  409. */
  410. SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
  411. {
  412. unsigned long end, tmp;
  413. struct vm_area_struct *vma, *prev;
  414. int unmapped_error = 0;
  415. int error = -EINVAL;
  416. int write;
  417. size_t len;
  418. struct blk_plug plug;
  419. #ifdef CONFIG_MEMORY_FAILURE
  420. if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
  421. return madvise_hwpoison(behavior, start, start+len_in);
  422. #endif
  423. if (!madvise_behavior_valid(behavior))
  424. return error;
  425. if (start & ~PAGE_MASK)
  426. return error;
  427. len = (len_in + ~PAGE_MASK) & PAGE_MASK;
  428. /* Check to see whether len was rounded up from small -ve to zero */
  429. if (len_in && !len)
  430. return error;
  431. end = start + len;
  432. if (end < start)
  433. return error;
  434. error = 0;
  435. if (end == start)
  436. return error;
  437. write = madvise_need_mmap_write(behavior);
  438. if (write)
  439. down_write(&current->mm->mmap_sem);
  440. else
  441. down_read(&current->mm->mmap_sem);
  442. /*
  443. * If the interval [start,end) covers some unmapped address
  444. * ranges, just ignore them, but return -ENOMEM at the end.
  445. * - different from the way of handling in mlock etc.
  446. */
  447. vma = find_vma_prev(current->mm, start, &prev);
  448. if (vma && start > vma->vm_start)
  449. prev = vma;
  450. blk_start_plug(&plug);
  451. for (;;) {
  452. /* Still start < end. */
  453. error = -ENOMEM;
  454. if (!vma)
  455. goto out;
  456. /* Here start < (end|vma->vm_end). */
  457. if (start < vma->vm_start) {
  458. unmapped_error = -ENOMEM;
  459. start = vma->vm_start;
  460. if (start >= end)
  461. goto out;
  462. }
  463. /* Here vma->vm_start <= start < (end|vma->vm_end) */
  464. tmp = vma->vm_end;
  465. if (end < tmp)
  466. tmp = end;
  467. /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
  468. error = madvise_vma(vma, &prev, start, tmp, behavior);
  469. if (error)
  470. goto out;
  471. start = tmp;
  472. if (prev && start < prev->vm_end)
  473. start = prev->vm_end;
  474. error = unmapped_error;
  475. if (start >= end)
  476. goto out;
  477. if (prev)
  478. vma = prev->vm_next;
  479. else /* madvise_remove dropped mmap_sem */
  480. vma = find_vma(current->mm, start);
  481. }
  482. out:
  483. blk_finish_plug(&plug);
  484. if (write)
  485. up_write(&current->mm->mmap_sem);
  486. else
  487. up_read(&current->mm->mmap_sem);
  488. return error;
  489. }