madvise.c 6.4 KB

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