madvise.c 14 KB

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