madvise.c 12 KB

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