madvise.c 9.5 KB

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