madvise.c 12 KB

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