madvise.c 7.1 KB

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