madvise.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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_DONTDUMP:
  65. new_flags |= VM_NODUMP;
  66. break;
  67. case MADV_DODUMP:
  68. new_flags &= ~VM_NODUMP;
  69. break;
  70. case MADV_MERGEABLE:
  71. case MADV_UNMERGEABLE:
  72. error = ksm_madvise(vma, start, end, behavior, &new_flags);
  73. if (error)
  74. goto out;
  75. break;
  76. case MADV_HUGEPAGE:
  77. case MADV_NOHUGEPAGE:
  78. error = hugepage_madvise(vma, &new_flags, behavior);
  79. if (error)
  80. goto out;
  81. break;
  82. }
  83. if (new_flags == vma->vm_flags) {
  84. *prev = vma;
  85. goto out;
  86. }
  87. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  88. *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
  89. vma->vm_file, pgoff, vma_policy(vma));
  90. if (*prev) {
  91. vma = *prev;
  92. goto success;
  93. }
  94. *prev = vma;
  95. if (start != vma->vm_start) {
  96. error = split_vma(mm, vma, start, 1);
  97. if (error)
  98. goto out;
  99. }
  100. if (end != vma->vm_end) {
  101. error = split_vma(mm, vma, end, 0);
  102. if (error)
  103. goto out;
  104. }
  105. success:
  106. /*
  107. * vm_flags is protected by the mmap_sem held in write mode.
  108. */
  109. vma->vm_flags = new_flags;
  110. out:
  111. if (error == -ENOMEM)
  112. error = -EAGAIN;
  113. return error;
  114. }
  115. /*
  116. * Schedule all required I/O operations. Do not wait for completion.
  117. */
  118. static long madvise_willneed(struct vm_area_struct * vma,
  119. struct vm_area_struct ** prev,
  120. unsigned long start, unsigned long end)
  121. {
  122. struct file *file = vma->vm_file;
  123. if (!file)
  124. return -EBADF;
  125. if (file->f_mapping->a_ops->get_xip_mem) {
  126. /* no bad return value, but ignore advice */
  127. return 0;
  128. }
  129. *prev = vma;
  130. start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  131. if (end > vma->vm_end)
  132. end = vma->vm_end;
  133. end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  134. force_page_cache_readahead(file->f_mapping, file, start, end - start);
  135. return 0;
  136. }
  137. /*
  138. * Application no longer needs these pages. If the pages are dirty,
  139. * it's OK to just throw them away. The app will be more careful about
  140. * data it wants to keep. Be sure to free swap resources too. The
  141. * zap_page_range call sets things up for shrink_active_list to actually free
  142. * these pages later if no one else has touched them in the meantime,
  143. * although we could add these pages to a global reuse list for
  144. * shrink_active_list to pick up before reclaiming other pages.
  145. *
  146. * NB: This interface discards data rather than pushes it out to swap,
  147. * as some implementations do. This has performance implications for
  148. * applications like large transactional databases which want to discard
  149. * pages in anonymous maps after committing to backing store the data
  150. * that was kept in them. There is no reason to write this data out to
  151. * the swap area if the application is discarding it.
  152. *
  153. * An interface that causes the system to free clean pages and flush
  154. * dirty pages is already available as msync(MS_INVALIDATE).
  155. */
  156. static long madvise_dontneed(struct vm_area_struct * vma,
  157. struct vm_area_struct ** prev,
  158. unsigned long start, unsigned long end)
  159. {
  160. *prev = vma;
  161. if (vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP))
  162. return -EINVAL;
  163. if (unlikely(vma->vm_flags & VM_NONLINEAR)) {
  164. struct zap_details details = {
  165. .nonlinear_vma = vma,
  166. .last_index = ULONG_MAX,
  167. };
  168. zap_page_range(vma, start, end - start, &details);
  169. } else
  170. zap_page_range(vma, start, end - start, NULL);
  171. return 0;
  172. }
  173. /*
  174. * Application wants to free up the pages and associated backing store.
  175. * This is effectively punching a hole into the middle of a file.
  176. *
  177. * NOTE: Currently, only shmfs/tmpfs is supported for this operation.
  178. * Other filesystems return -ENOSYS.
  179. */
  180. static long madvise_remove(struct vm_area_struct *vma,
  181. struct vm_area_struct **prev,
  182. unsigned long start, unsigned long end)
  183. {
  184. struct address_space *mapping;
  185. loff_t offset, endoff;
  186. int error;
  187. *prev = NULL; /* tell sys_madvise we drop mmap_sem */
  188. if (vma->vm_flags & (VM_LOCKED|VM_NONLINEAR|VM_HUGETLB))
  189. return -EINVAL;
  190. if (!vma->vm_file || !vma->vm_file->f_mapping
  191. || !vma->vm_file->f_mapping->host) {
  192. return -EINVAL;
  193. }
  194. if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
  195. return -EACCES;
  196. mapping = vma->vm_file->f_mapping;
  197. offset = (loff_t)(start - vma->vm_start)
  198. + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  199. endoff = (loff_t)(end - vma->vm_start - 1)
  200. + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  201. /* vmtruncate_range needs to take i_mutex */
  202. up_read(&current->mm->mmap_sem);
  203. error = vmtruncate_range(mapping->host, offset, endoff);
  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. }