madvise.c 11 KB

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