madvise.c 12 KB

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