madvise.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. #include <linux/sched.h>
  13. /*
  14. * Any behaviour which results in changes to the vma->vm_flags needs to
  15. * take mmap_sem for writing. Others, which simply traverse vmas, need
  16. * to only take it for reading.
  17. */
  18. static int madvise_need_mmap_write(int behavior)
  19. {
  20. switch (behavior) {
  21. case MADV_REMOVE:
  22. case MADV_WILLNEED:
  23. case MADV_DONTNEED:
  24. return 0;
  25. default:
  26. /* be safe, default to 1. list exceptions explicitly */
  27. return 1;
  28. }
  29. }
  30. /*
  31. * We can potentially split a vm area into separate
  32. * areas, each area with its own behavior.
  33. */
  34. static long madvise_behavior(struct vm_area_struct * vma,
  35. struct vm_area_struct **prev,
  36. unsigned long start, unsigned long end, int behavior)
  37. {
  38. struct mm_struct * mm = vma->vm_mm;
  39. int error = 0;
  40. pgoff_t pgoff;
  41. int new_flags = vma->vm_flags;
  42. switch (behavior) {
  43. case MADV_NORMAL:
  44. new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
  45. break;
  46. case MADV_SEQUENTIAL:
  47. new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
  48. break;
  49. case MADV_RANDOM:
  50. new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
  51. break;
  52. case MADV_DONTFORK:
  53. new_flags |= VM_DONTCOPY;
  54. break;
  55. case MADV_DOFORK:
  56. new_flags &= ~VM_DONTCOPY;
  57. break;
  58. }
  59. if (new_flags == vma->vm_flags) {
  60. *prev = vma;
  61. goto out;
  62. }
  63. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  64. *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
  65. vma->vm_file, pgoff, vma_policy(vma));
  66. if (*prev) {
  67. vma = *prev;
  68. goto success;
  69. }
  70. *prev = vma;
  71. if (start != vma->vm_start) {
  72. error = split_vma(mm, vma, start, 1);
  73. if (error)
  74. goto out;
  75. }
  76. if (end != vma->vm_end) {
  77. error = split_vma(mm, vma, end, 0);
  78. if (error)
  79. goto out;
  80. }
  81. success:
  82. /*
  83. * vm_flags is protected by the mmap_sem held in write mode.
  84. */
  85. vma->vm_flags = new_flags;
  86. out:
  87. if (error == -ENOMEM)
  88. error = -EAGAIN;
  89. return error;
  90. }
  91. /*
  92. * Schedule all required I/O operations. Do not wait for completion.
  93. */
  94. static long madvise_willneed(struct vm_area_struct * vma,
  95. struct vm_area_struct ** prev,
  96. unsigned long start, unsigned long end)
  97. {
  98. struct file *file = vma->vm_file;
  99. if (!file)
  100. return -EBADF;
  101. if (file->f_mapping->a_ops->get_xip_mem) {
  102. /* no bad return value, but ignore advice */
  103. return 0;
  104. }
  105. *prev = vma;
  106. start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  107. if (end > vma->vm_end)
  108. end = vma->vm_end;
  109. end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  110. force_page_cache_readahead(file->f_mapping, file, start, 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 shrink_active_list 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. * shrink_active_list 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. BUG();
  211. break;
  212. }
  213. return error;
  214. }
  215. static int
  216. madvise_behavior_valid(int behavior)
  217. {
  218. switch (behavior) {
  219. case MADV_DOFORK:
  220. case MADV_DONTFORK:
  221. case MADV_NORMAL:
  222. case MADV_SEQUENTIAL:
  223. case MADV_RANDOM:
  224. case MADV_REMOVE:
  225. case MADV_WILLNEED:
  226. case MADV_DONTNEED:
  227. return 1;
  228. default:
  229. return 0;
  230. }
  231. }
  232. /*
  233. * The madvise(2) system call.
  234. *
  235. * Applications can use madvise() to advise the kernel how it should
  236. * handle paging I/O in this VM area. The idea is to help the kernel
  237. * use appropriate read-ahead and caching techniques. The information
  238. * provided is advisory only, and can be safely disregarded by the
  239. * kernel without affecting the correct operation of the application.
  240. *
  241. * behavior values:
  242. * MADV_NORMAL - the default behavior is to read clusters. This
  243. * results in some read-ahead and read-behind.
  244. * MADV_RANDOM - the system should read the minimum amount of data
  245. * on any access, since it is unlikely that the appli-
  246. * cation will need more than what it asks for.
  247. * MADV_SEQUENTIAL - pages in the given range will probably be accessed
  248. * once, so they can be aggressively read ahead, and
  249. * can be freed soon after they are accessed.
  250. * MADV_WILLNEED - the application is notifying the system to read
  251. * some pages ahead.
  252. * MADV_DONTNEED - the application is finished with the given range,
  253. * so the kernel can free resources associated with it.
  254. * MADV_REMOVE - the application wants to free up the given range of
  255. * pages and associated backing store.
  256. *
  257. * return values:
  258. * zero - success
  259. * -EINVAL - start + len < 0, start is not page-aligned,
  260. * "behavior" is not a valid value, or application
  261. * is attempting to release locked or shared pages.
  262. * -ENOMEM - addresses in the specified range are not currently
  263. * mapped, or are outside the AS of the process.
  264. * -EIO - an I/O error occurred while paging in data.
  265. * -EBADF - map exists, but area maps something that isn't a file.
  266. * -EAGAIN - a kernel resource was temporarily unavailable.
  267. */
  268. SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
  269. {
  270. unsigned long end, tmp;
  271. struct vm_area_struct * vma, *prev;
  272. int unmapped_error = 0;
  273. int error = -EINVAL;
  274. int write;
  275. size_t len;
  276. if (!madvise_behavior_valid(behavior))
  277. return error;
  278. write = madvise_need_mmap_write(behavior);
  279. if (write)
  280. down_write(&current->mm->mmap_sem);
  281. else
  282. down_read(&current->mm->mmap_sem);
  283. if (start & ~PAGE_MASK)
  284. goto out;
  285. len = (len_in + ~PAGE_MASK) & PAGE_MASK;
  286. /* Check to see whether len was rounded up from small -ve to zero */
  287. if (len_in && !len)
  288. goto out;
  289. end = start + len;
  290. if (end < start)
  291. goto out;
  292. error = 0;
  293. if (end == start)
  294. goto out;
  295. /*
  296. * If the interval [start,end) covers some unmapped address
  297. * ranges, just ignore them, but return -ENOMEM at the end.
  298. * - different from the way of handling in mlock etc.
  299. */
  300. vma = find_vma_prev(current->mm, start, &prev);
  301. if (vma && start > vma->vm_start)
  302. prev = vma;
  303. for (;;) {
  304. /* Still start < end. */
  305. error = -ENOMEM;
  306. if (!vma)
  307. goto out;
  308. /* Here start < (end|vma->vm_end). */
  309. if (start < vma->vm_start) {
  310. unmapped_error = -ENOMEM;
  311. start = vma->vm_start;
  312. if (start >= end)
  313. goto out;
  314. }
  315. /* Here vma->vm_start <= start < (end|vma->vm_end) */
  316. tmp = vma->vm_end;
  317. if (end < tmp)
  318. tmp = end;
  319. /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
  320. error = madvise_vma(vma, &prev, start, tmp, behavior);
  321. if (error)
  322. goto out;
  323. start = tmp;
  324. if (prev && start < prev->vm_end)
  325. start = prev->vm_end;
  326. error = unmapped_error;
  327. if (start >= end)
  328. goto out;
  329. if (prev)
  330. vma = prev->vm_next;
  331. else /* madvise_remove dropped mmap_sem */
  332. vma = find_vma(current->mm, start);
  333. }
  334. out:
  335. if (write)
  336. up_write(&current->mm->mmap_sem);
  337. else
  338. up_read(&current->mm->mmap_sem);
  339. return error;
  340. }