madvise.c 9.8 KB

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