madvise.c 9.5 KB

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