madvise.c 11 KB

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