4 * Copyright (C) 1999 Linus Torvalds
5 * Copyright (C) 2002 Christoph Hellwig
8 #include <linux/mman.h>
9 #include <linux/pagemap.h>
10 #include <linux/syscalls.h>
11 #include <linux/hugetlb.h>
14 * We can potentially split a vm area into separate
15 * areas, each area with its own behavior.
17 static long madvise_behavior(struct vm_area_struct * vma, unsigned long start,
18 unsigned long end, int behavior)
20 struct mm_struct * mm = vma->vm_mm;
22 int new_flags = vma->vm_flags & ~VM_READHINTMASK;
26 new_flags |= VM_SEQ_READ;
29 new_flags |= VM_RAND_READ;
35 if (new_flags == vma->vm_flags)
38 if (start != vma->vm_start) {
39 error = split_vma(mm, vma, start, 1);
44 if (end != vma->vm_end) {
45 error = split_vma(mm, vma, end, 0);
51 * vm_flags is protected by the mmap_sem held in write mode.
53 VM_ClearReadHint(vma);
54 vma->vm_flags = new_flags;
63 * Schedule all required I/O operations. Do not wait for completion.
65 static long madvise_willneed(struct vm_area_struct * vma,
66 unsigned long start, unsigned long end)
68 struct file *file = vma->vm_file;
73 start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
74 if (end > vma->vm_end)
76 end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
78 force_page_cache_readahead(file->f_mapping,
79 file, start, max_sane_readahead(end - start));
84 * Application no longer needs these pages. If the pages are dirty,
85 * it's OK to just throw them away. The app will be more careful about
86 * data it wants to keep. Be sure to free swap resources too. The
87 * zap_page_range call sets things up for refill_inactive to actually free
88 * these pages later if no one else has touched them in the meantime,
89 * although we could add these pages to a global reuse list for
90 * refill_inactive to pick up before reclaiming other pages.
92 * NB: This interface discards data rather than pushes it out to swap,
93 * as some implementations do. This has performance implications for
94 * applications like large transactional databases which want to discard
95 * pages in anonymous maps after committing to backing store the data
96 * that was kept in them. There is no reason to write this data out to
97 * the swap area if the application is discarding it.
99 * An interface that causes the system to free clean pages and flush
100 * dirty pages is already available as msync(MS_INVALIDATE).
102 static long madvise_dontneed(struct vm_area_struct * vma,
103 unsigned long start, unsigned long end)
105 if ((vma->vm_flags & VM_LOCKED) || is_vm_hugetlb_page(vma))
108 if (unlikely(vma->vm_flags & VM_NONLINEAR)) {
109 struct zap_details details = {
110 .nonlinear_vma = vma,
111 .last_index = ULONG_MAX,
113 zap_page_range(vma, start, end - start, &details);
115 zap_page_range(vma, start, end - start, NULL);
119 static long madvise_vma(struct vm_area_struct * vma, unsigned long start,
120 unsigned long end, int behavior)
126 case MADV_SEQUENTIAL:
128 error = madvise_behavior(vma, start, end, behavior);
132 error = madvise_willneed(vma, start, end);
136 error = madvise_dontneed(vma, start, end);
148 * The madvise(2) system call.
150 * Applications can use madvise() to advise the kernel how it should
151 * handle paging I/O in this VM area. The idea is to help the kernel
152 * use appropriate read-ahead and caching techniques. The information
153 * provided is advisory only, and can be safely disregarded by the
154 * kernel without affecting the correct operation of the application.
157 * MADV_NORMAL - the default behavior is to read clusters. This
158 * results in some read-ahead and read-behind.
159 * MADV_RANDOM - the system should read the minimum amount of data
160 * on any access, since it is unlikely that the appli-
161 * cation will need more than what it asks for.
162 * MADV_SEQUENTIAL - pages in the given range will probably be accessed
163 * once, so they can be aggressively read ahead, and
164 * can be freed soon after they are accessed.
165 * MADV_WILLNEED - the application is notifying the system to read
167 * MADV_DONTNEED - the application is finished with the given range,
168 * so the kernel can free resources associated with it.
172 * -EINVAL - start + len < 0, start is not page-aligned,
173 * "behavior" is not a valid value, or application
174 * is attempting to release locked or shared pages.
175 * -ENOMEM - addresses in the specified range are not currently
176 * mapped, or are outside the AS of the process.
177 * -EIO - an I/O error occurred while paging in data.
178 * -EBADF - map exists, but area maps something that isn't a file.
179 * -EAGAIN - a kernel resource was temporarily unavailable.
181 asmlinkage long sys_madvise(unsigned long start, size_t len_in, int behavior)
184 struct vm_area_struct * vma;
185 int unmapped_error = 0;
189 down_write(¤t->mm->mmap_sem);
191 if (start & ~PAGE_MASK)
193 len = (len_in + ~PAGE_MASK) & PAGE_MASK;
195 /* Check to see whether len was rounded up from small -ve to zero */
208 * If the interval [start,end) covers some unmapped address
209 * ranges, just ignore them, but return -ENOMEM at the end.
211 vma = find_vma(current->mm, start);
213 /* Still start < end. */
218 /* Here start < vma->vm_end. */
219 if (start < vma->vm_start) {
220 unmapped_error = -ENOMEM;
221 start = vma->vm_start;
224 /* Here vma->vm_start <= start < vma->vm_end. */
225 if (end <= vma->vm_end) {
227 error = madvise_vma(vma, start, end,
232 error = unmapped_error;
236 /* Here vma->vm_start <= start < vma->vm_end < end. */
237 error = madvise_vma(vma, start, vma->vm_end, behavior);
245 up_write(¤t->mm->mmap_sem);