madvise.c 12 KB

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