madvise.c 9.9 KB

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