madvise.c 12 KB

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