madvise.c 9.0 KB

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