4 * Copyright (C) 1999 Linus Torvalds
5 * Copyright (C) 2002 Christoph Hellwig
8 #include <linux/mman.h>
9 #include <linux/pagemap.h>
13 * We can potentially split a vm area into separate
14 * areas, each area with its own behavior.
16 static long madvise_behavior(struct vm_area_struct * vma, unsigned long start,
17 unsigned long end, int behavior)
19 struct mm_struct * mm = vma->vm_mm;
22 if (start != vma->vm_start) {
23 error = split_vma(mm, vma, start, 1);
28 if (end != vma->vm_end) {
29 error = split_vma(mm, vma, end, 0);
34 spin_lock(&mm->page_table_lock);
35 VM_ClearReadHint(vma);
39 vma->vm_flags |= VM_SEQ_READ;
42 vma->vm_flags |= VM_RAND_READ;
47 spin_unlock(&mm->page_table_lock);
53 * Schedule all required I/O operations. Do not wait for completion.
55 static long madvise_willneed(struct vm_area_struct * vma,
56 unsigned long start, unsigned long end)
58 struct file *file = vma->vm_file;
63 start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
64 if (end > vma->vm_end)
66 end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
68 force_page_cache_readahead(file->f_mapping,
69 file, start, max_sane_readahead(end - start));
74 * Application no longer needs these pages. If the pages are dirty,
75 * it's OK to just throw them away. The app will be more careful about
76 * data it wants to keep. Be sure to free swap resources too. The
77 * zap_page_range call sets things up for refill_inactive to actually free
78 * these pages later if no one else has touched them in the meantime,
79 * although we could add these pages to a global reuse list for
80 * refill_inactive to pick up before reclaiming other pages.
82 * NB: This interface discards data rather than pushes it out to swap,
83 * as some implementations do. This has performance implications for
84 * applications like large transactional databases which want to discard
85 * pages in anonymous maps after committing to backing store the data
86 * that was kept in them. There is no reason to write this data out to
87 * the swap area if the application is discarding it.
89 * An interface that causes the system to free clean pages and flush
90 * dirty pages is already available as msync(MS_INVALIDATE).
92 static long madvise_dontneed(struct vm_area_struct * vma,
93 unsigned long start, unsigned long end)
95 struct zap_details details;
97 if (vma->vm_flags & VM_LOCKED)
100 if (unlikely(vma->vm_flags & VM_NONLINEAR)) {
101 details.check_mapping = NULL;
102 details.nonlinear_vma = vma;
103 details.first_index = 0;
104 details.last_index = ULONG_MAX;
105 zap_page_range(vma, start, end - start, &details);
107 zap_page_range(vma, start, end - start, NULL);
111 static long madvise_vma(struct vm_area_struct * vma, unsigned long start,
112 unsigned long end, int behavior)
118 case MADV_SEQUENTIAL:
120 error = madvise_behavior(vma, start, end, behavior);
124 error = madvise_willneed(vma, start, end);
128 error = madvise_dontneed(vma, start, end);
140 * The madvise(2) system call.
142 * Applications can use madvise() to advise the kernel how it should
143 * handle paging I/O in this VM area. The idea is to help the kernel
144 * use appropriate read-ahead and caching techniques. The information
145 * provided is advisory only, and can be safely disregarded by the
146 * kernel without affecting the correct operation of the application.
149 * MADV_NORMAL - the default behavior is to read clusters. This
150 * results in some read-ahead and read-behind.
151 * MADV_RANDOM - the system should read the minimum amount of data
152 * on any access, since it is unlikely that the appli-
153 * cation will need more than what it asks for.
154 * MADV_SEQUENTIAL - pages in the given range will probably be accessed
155 * once, so they can be aggressively read ahead, and
156 * can be freed soon after they are accessed.
157 * MADV_WILLNEED - the application is notifying the system to read
159 * MADV_DONTNEED - the application is finished with the given range,
160 * so the kernel can free resources associated with it.
164 * -EINVAL - start + len < 0, start is not page-aligned,
165 * "behavior" is not a valid value, or application
166 * is attempting to release locked or shared pages.
167 * -ENOMEM - addresses in the specified range are not currently
168 * mapped, or are outside the AS of the process.
169 * -EIO - an I/O error occurred while paging in data.
170 * -EBADF - map exists, but area maps something that isn't a file.
171 * -EAGAIN - a kernel resource was temporarily unavailable.
173 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior)
176 struct vm_area_struct * vma;
177 int unmapped_error = 0;
180 down_write(¤t->mm->mmap_sem);
182 if (start & ~PAGE_MASK)
184 len = (len + ~PAGE_MASK) & PAGE_MASK;
194 * If the interval [start,end) covers some unmapped address
195 * ranges, just ignore them, but return -ENOMEM at the end.
197 vma = find_vma(current->mm, start);
199 /* Still start < end. */
204 /* Here start < vma->vm_end. */
205 if (start < vma->vm_start) {
206 unmapped_error = -ENOMEM;
207 start = vma->vm_start;
210 /* Here vma->vm_start <= start < vma->vm_end. */
211 if (end <= vma->vm_end) {
213 error = madvise_vma(vma, start, end,
218 error = unmapped_error;
222 /* Here vma->vm_start <= start < vma->vm_end < end. */
223 error = madvise_vma(vma, start, vma->vm_end, behavior);
231 up_write(¤t->mm->mmap_sem);