madvise.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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/page-isolation.h>
  12. #include <linux/hugetlb.h>
  13. #include <linux/sched.h>
  14. #include <linux/ksm.h>
  15. #include <linux/file.h>
  16. /*
  17. * Any behaviour which results in changes to the vma->vm_flags needs to
  18. * take mmap_sem for writing. Others, which simply traverse vmas, need
  19. * to only take it for reading.
  20. */
  21. static int madvise_need_mmap_write(int behavior)
  22. {
  23. switch (behavior) {
  24. case MADV_REMOVE:
  25. case MADV_WILLNEED:
  26. case MADV_DONTNEED:
  27. return 0;
  28. default:
  29. /* be safe, default to 1. list exceptions explicitly */
  30. return 1;
  31. }
  32. }
  33. /*
  34. * We can potentially split a vm area into separate
  35. * areas, each area with its own behavior.
  36. */
  37. static long madvise_behavior(struct vm_area_struct * vma,
  38. struct vm_area_struct **prev,
  39. unsigned long start, unsigned long end, int behavior)
  40. {
  41. struct mm_struct * mm = vma->vm_mm;
  42. int error = 0;
  43. pgoff_t pgoff;
  44. unsigned long new_flags = vma->vm_flags;
  45. switch (behavior) {
  46. case MADV_NORMAL:
  47. new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
  48. break;
  49. case MADV_SEQUENTIAL:
  50. new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
  51. break;
  52. case MADV_RANDOM:
  53. new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
  54. break;
  55. case MADV_DONTFORK:
  56. new_flags |= VM_DONTCOPY;
  57. break;
  58. case MADV_DOFORK:
  59. if (vma->vm_flags & VM_IO) {
  60. error = -EINVAL;
  61. goto out;
  62. }
  63. new_flags &= ~VM_DONTCOPY;
  64. break;
  65. case MADV_DONTDUMP:
  66. new_flags |= VM_NODUMP;
  67. break;
  68. case MADV_DODUMP:
  69. new_flags &= ~VM_NODUMP;
  70. break;
  71. case MADV_MERGEABLE:
  72. case MADV_UNMERGEABLE:
  73. error = ksm_madvise(vma, start, end, behavior, &new_flags);
  74. if (error)
  75. goto out;
  76. break;
  77. case MADV_HUGEPAGE:
  78. case MADV_NOHUGEPAGE:
  79. error = hugepage_madvise(vma, &new_flags, behavior);
  80. if (error)
  81. goto out;
  82. break;
  83. }
  84. if (new_flags == vma->vm_flags) {
  85. *prev = vma;
  86. goto out;
  87. }
  88. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  89. *prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
  90. vma->vm_file, pgoff, vma_policy(vma),
  91. vma_get_anon_name(vma));
  92. if (*prev) {
  93. vma = *prev;
  94. goto success;
  95. }
  96. *prev = vma;
  97. if (start != vma->vm_start) {
  98. error = split_vma(mm, vma, start, 1);
  99. if (error)
  100. goto out;
  101. }
  102. if (end != vma->vm_end) {
  103. error = split_vma(mm, vma, end, 0);
  104. if (error)
  105. goto out;
  106. }
  107. success:
  108. /*
  109. * vm_flags is protected by the mmap_sem held in write mode.
  110. */
  111. vma->vm_flags = new_flags;
  112. out:
  113. if (error == -ENOMEM)
  114. error = -EAGAIN;
  115. return error;
  116. }
  117. /*
  118. * Schedule all required I/O operations. Do not wait for completion.
  119. */
  120. static long madvise_willneed(struct vm_area_struct * vma,
  121. struct vm_area_struct ** prev,
  122. unsigned long start, unsigned long end)
  123. {
  124. struct file *file = vma->vm_file;
  125. if (!file)
  126. return -EBADF;
  127. if (file->f_mapping->a_ops->get_xip_mem) {
  128. /* no bad return value, but ignore advice */
  129. return 0;
  130. }
  131. *prev = vma;
  132. start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  133. if (end > vma->vm_end)
  134. end = vma->vm_end;
  135. end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  136. force_page_cache_readahead(file->f_mapping, file, start, end - start);
  137. return 0;
  138. }
  139. /*
  140. * Application no longer needs these pages. If the pages are dirty,
  141. * it's OK to just throw them away. The app will be more careful about
  142. * data it wants to keep. Be sure to free swap resources too. The
  143. * zap_page_range call sets things up for shrink_active_list to actually free
  144. * these pages later if no one else has touched them in the meantime,
  145. * although we could add these pages to a global reuse list for
  146. * shrink_active_list to pick up before reclaiming other pages.
  147. *
  148. * NB: This interface discards data rather than pushes it out to swap,
  149. * as some implementations do. This has performance implications for
  150. * applications like large transactional databases which want to discard
  151. * pages in anonymous maps after committing to backing store the data
  152. * that was kept in them. There is no reason to write this data out to
  153. * the swap area if the application is discarding it.
  154. *
  155. * An interface that causes the system to free clean pages and flush
  156. * dirty pages is already available as msync(MS_INVALIDATE).
  157. */
  158. static long madvise_dontneed(struct vm_area_struct * vma,
  159. struct vm_area_struct ** prev,
  160. unsigned long start, unsigned long end)
  161. {
  162. *prev = vma;
  163. if (vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP))
  164. return -EINVAL;
  165. if (unlikely(vma->vm_flags & VM_NONLINEAR)) {
  166. struct zap_details details = {
  167. .nonlinear_vma = vma,
  168. .last_index = ULONG_MAX,
  169. };
  170. zap_page_range(vma, start, end - start, &details);
  171. } else
  172. zap_page_range(vma, start, end - start, NULL);
  173. return 0;
  174. }
  175. /*
  176. * Application wants to free up the pages and associated backing store.
  177. * This is effectively punching a hole into the middle of a file.
  178. *
  179. * NOTE: Currently, only shmfs/tmpfs is supported for this operation.
  180. * Other filesystems return -ENOSYS.
  181. */
  182. static long madvise_remove(struct vm_area_struct *vma,
  183. struct vm_area_struct **prev,
  184. unsigned long start, unsigned long end)
  185. {
  186. struct address_space *mapping;
  187. loff_t offset, endoff;
  188. int error;
  189. struct file *f;
  190. *prev = NULL; /* tell sys_madvise we drop mmap_sem */
  191. if (vma->vm_flags & (VM_LOCKED|VM_NONLINEAR|VM_HUGETLB))
  192. return -EINVAL;
  193. f = vma->vm_file;
  194. if (!f || !f->f_mapping || !f->f_mapping->host) {
  195. return -EINVAL;
  196. }
  197. if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
  198. return -EACCES;
  199. mapping = vma->vm_file->f_mapping;
  200. offset = (loff_t)(start - vma->vm_start)
  201. + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  202. endoff = (loff_t)(end - vma->vm_start - 1)
  203. + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  204. /*
  205. * vmtruncate_range may need to take i_mutex. We need to
  206. * explicitly grab a reference because the vma (and hence the
  207. * vma's reference to the file) can go away as soon as we drop
  208. * mmap_sem.
  209. */
  210. get_file(f);
  211. up_read(&current->mm->mmap_sem);
  212. error = vmtruncate_range(mapping->host, offset, endoff);
  213. fput(f);
  214. down_read(&current->mm->mmap_sem);
  215. return error;
  216. }
  217. #ifdef CONFIG_MEMORY_FAILURE
  218. /*
  219. * Error injection support for memory error handling.
  220. */
  221. static int madvise_hwpoison(int bhv, unsigned long start, unsigned long end)
  222. {
  223. int ret = 0;
  224. if (!capable(CAP_SYS_ADMIN))
  225. return -EPERM;
  226. for (; start < end; start += PAGE_SIZE) {
  227. struct page *p;
  228. int ret = get_user_pages_fast(start, 1, 0, &p);
  229. if (ret != 1)
  230. return ret;
  231. if (bhv == MADV_SOFT_OFFLINE) {
  232. printk(KERN_INFO "Soft offlining page %lx at %lx\n",
  233. page_to_pfn(p), start);
  234. ret = soft_offline_page(p, MF_COUNT_INCREASED);
  235. if (ret)
  236. break;
  237. continue;
  238. }
  239. printk(KERN_INFO "Injecting memory failure for page %lx at %lx\n",
  240. page_to_pfn(p), start);
  241. /* Ignore return value for now */
  242. memory_failure(page_to_pfn(p), 0, MF_COUNT_INCREASED);
  243. }
  244. return ret;
  245. }
  246. #endif
  247. static long
  248. madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
  249. unsigned long start, unsigned long end, int behavior)
  250. {
  251. switch (behavior) {
  252. case MADV_REMOVE:
  253. return madvise_remove(vma, prev, start, end);
  254. case MADV_WILLNEED:
  255. return madvise_willneed(vma, prev, start, end);
  256. case MADV_DONTNEED:
  257. return madvise_dontneed(vma, prev, start, end);
  258. default:
  259. return madvise_behavior(vma, prev, start, end, behavior);
  260. }
  261. }
  262. static int
  263. madvise_behavior_valid(int behavior)
  264. {
  265. switch (behavior) {
  266. case MADV_DOFORK:
  267. case MADV_DONTFORK:
  268. case MADV_NORMAL:
  269. case MADV_SEQUENTIAL:
  270. case MADV_RANDOM:
  271. case MADV_REMOVE:
  272. case MADV_WILLNEED:
  273. case MADV_DONTNEED:
  274. #ifdef CONFIG_KSM
  275. case MADV_MERGEABLE:
  276. case MADV_UNMERGEABLE:
  277. #endif
  278. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  279. case MADV_HUGEPAGE:
  280. case MADV_NOHUGEPAGE:
  281. #endif
  282. case MADV_DONTDUMP:
  283. case MADV_DODUMP:
  284. return 1;
  285. default:
  286. return 0;
  287. }
  288. }
  289. /*
  290. * The madvise(2) system call.
  291. *
  292. * Applications can use madvise() to advise the kernel how it should
  293. * handle paging I/O in this VM area. The idea is to help the kernel
  294. * use appropriate read-ahead and caching techniques. The information
  295. * provided is advisory only, and can be safely disregarded by the
  296. * kernel without affecting the correct operation of the application.
  297. *
  298. * behavior values:
  299. * MADV_NORMAL - the default behavior is to read clusters. This
  300. * results in some read-ahead and read-behind.
  301. * MADV_RANDOM - the system should read the minimum amount of data
  302. * on any access, since it is unlikely that the appli-
  303. * cation will need more than what it asks for.
  304. * MADV_SEQUENTIAL - pages in the given range will probably be accessed
  305. * once, so they can be aggressively read ahead, and
  306. * can be freed soon after they are accessed.
  307. * MADV_WILLNEED - the application is notifying the system to read
  308. * some pages ahead.
  309. * MADV_DONTNEED - the application is finished with the given range,
  310. * so the kernel can free resources associated with it.
  311. * MADV_REMOVE - the application wants to free up the given range of
  312. * pages and associated backing store.
  313. * MADV_DONTFORK - omit this area from child's address space when forking:
  314. * typically, to avoid COWing pages pinned by get_user_pages().
  315. * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
  316. * MADV_MERGEABLE - the application recommends that KSM try to merge pages in
  317. * this area with pages of identical content from other such areas.
  318. * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
  319. *
  320. * return values:
  321. * zero - success
  322. * -EINVAL - start + len < 0, start is not page-aligned,
  323. * "behavior" is not a valid value, or application
  324. * is attempting to release locked or shared pages.
  325. * -ENOMEM - addresses in the specified range are not currently
  326. * mapped, or are outside the AS of the process.
  327. * -EIO - an I/O error occurred while paging in data.
  328. * -EBADF - map exists, but area maps something that isn't a file.
  329. * -EAGAIN - a kernel resource was temporarily unavailable.
  330. */
  331. SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
  332. {
  333. unsigned long end, tmp;
  334. struct vm_area_struct * vma, *prev;
  335. int unmapped_error = 0;
  336. int error = -EINVAL;
  337. int write;
  338. size_t len;
  339. #ifdef CONFIG_MEMORY_FAILURE
  340. if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
  341. return madvise_hwpoison(behavior, start, start+len_in);
  342. #endif
  343. if (!madvise_behavior_valid(behavior))
  344. return error;
  345. write = madvise_need_mmap_write(behavior);
  346. if (write)
  347. down_write(&current->mm->mmap_sem);
  348. else
  349. down_read(&current->mm->mmap_sem);
  350. if (start & ~PAGE_MASK)
  351. goto out;
  352. len = (len_in + ~PAGE_MASK) & PAGE_MASK;
  353. /* Check to see whether len was rounded up from small -ve to zero */
  354. if (len_in && !len)
  355. goto out;
  356. end = start + len;
  357. if (end < start)
  358. goto out;
  359. error = 0;
  360. if (end == start)
  361. goto out;
  362. /*
  363. * If the interval [start,end) covers some unmapped address
  364. * ranges, just ignore them, but return -ENOMEM at the end.
  365. * - different from the way of handling in mlock etc.
  366. */
  367. vma = find_vma_prev(current->mm, start, &prev);
  368. if (vma && start > vma->vm_start)
  369. prev = vma;
  370. for (;;) {
  371. /* Still start < end. */
  372. error = -ENOMEM;
  373. if (!vma)
  374. goto out;
  375. /* Here start < (end|vma->vm_end). */
  376. if (start < vma->vm_start) {
  377. unmapped_error = -ENOMEM;
  378. start = vma->vm_start;
  379. if (start >= end)
  380. goto out;
  381. }
  382. /* Here vma->vm_start <= start < (end|vma->vm_end) */
  383. tmp = vma->vm_end;
  384. if (end < tmp)
  385. tmp = end;
  386. /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
  387. error = madvise_vma(vma, &prev, start, tmp, behavior);
  388. if (error)
  389. goto out;
  390. start = tmp;
  391. if (prev && start < prev->vm_end)
  392. start = prev->vm_end;
  393. error = unmapped_error;
  394. if (start >= end)
  395. goto out;
  396. if (prev)
  397. vma = prev->vm_next;
  398. else /* madvise_remove dropped mmap_sem */
  399. vma = find_vma(current->mm, start);
  400. }
  401. out:
  402. if (write)
  403. up_write(&current->mm->mmap_sem);
  404. else
  405. up_read(&current->mm->mmap_sem);
  406. return error;
  407. }