madvise.c 12 KB

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