madvise.c 7.2 KB

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