200e2fa43ad9cf16ed18b49cce00d78fcc369698
[linux-flexiantxendom0-3.2.10.git] / mm / madvise.c
1 /*
2  *      linux/mm/madvise.c
3  *
4  * Copyright (C) 1999  Linus Torvalds
5  * Copyright (C) 2002  Christoph Hellwig
6  */
7
8 #include <linux/mman.h>
9 #include <linux/pagemap.h>
10
11
12 /*
13  * We can potentially split a vm area into separate
14  * areas, each area with its own behavior.
15  */
16 static long madvise_behavior(struct vm_area_struct * vma, unsigned long start,
17                              unsigned long end, int behavior)
18 {
19         struct mm_struct * mm = vma->vm_mm;
20         int error;
21
22         if (start != vma->vm_start) {
23                 error = split_vma(mm, vma, start, 1);
24                 if (error)
25                         return -EAGAIN;
26         }
27
28         if (end != vma->vm_end) {
29                 error = split_vma(mm, vma, end, 0);
30                 if (error)
31                         return -EAGAIN;
32         }
33
34         spin_lock(&mm->page_table_lock);
35         VM_ClearReadHint(vma);
36
37         switch (behavior) {
38         case MADV_SEQUENTIAL:
39                 vma->vm_flags |= VM_SEQ_READ;
40                 break;
41         case MADV_RANDOM:
42                 vma->vm_flags |= VM_RAND_READ;
43                 break;
44         default:
45                 break;
46         }
47         spin_unlock(&mm->page_table_lock);
48
49         return 0;
50 }
51
52 /*
53  * Schedule all required I/O operations.  Do not wait for completion.
54  */
55 static long madvise_willneed(struct vm_area_struct * vma,
56                              unsigned long start, unsigned long end)
57 {
58         struct file *file = vma->vm_file;
59
60         if (!file)
61                 return -EBADF;
62
63         start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
64         if (end > vma->vm_end)
65                 end = vma->vm_end;
66         end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
67
68         do_page_cache_readahead(file->f_dentry->d_inode->i_mapping,
69                         file, start, max_sane_readahead(end - start));
70         return 0;
71 }
72
73 /*
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.
81  *
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.
88  *
89  * An interface that causes the system to free clean pages and flush
90  * dirty pages is already available as msync(MS_INVALIDATE).
91  */
92 static long madvise_dontneed(struct vm_area_struct * vma,
93                              unsigned long start, unsigned long end)
94 {
95         if (vma->vm_flags & VM_LOCKED)
96                 return -EINVAL;
97
98         zap_page_range(vma, start, end - start);
99         return 0;
100 }
101
102 static long madvise_vma(struct vm_area_struct * vma, unsigned long start,
103                         unsigned long end, int behavior)
104 {
105         long error = -EBADF;
106
107         switch (behavior) {
108         case MADV_NORMAL:
109         case MADV_SEQUENTIAL:
110         case MADV_RANDOM:
111                 error = madvise_behavior(vma, start, end, behavior);
112                 break;
113
114         case MADV_WILLNEED:
115                 error = madvise_willneed(vma, start, end);
116                 break;
117
118         case MADV_DONTNEED:
119                 error = madvise_dontneed(vma, start, end);
120                 break;
121
122         default:
123                 error = -EINVAL;
124                 break;
125         }
126                 
127         return error;
128 }
129
130 /*
131  * The madvise(2) system call.
132  *
133  * Applications can use madvise() to advise the kernel how it should
134  * handle paging I/O in this VM area.  The idea is to help the kernel
135  * use appropriate read-ahead and caching techniques.  The information
136  * provided is advisory only, and can be safely disregarded by the
137  * kernel without affecting the correct operation of the application.
138  *
139  * behavior values:
140  *  MADV_NORMAL - the default behavior is to read clusters.  This
141  *              results in some read-ahead and read-behind.
142  *  MADV_RANDOM - the system should read the minimum amount of data
143  *              on any access, since it is unlikely that the appli-
144  *              cation will need more than what it asks for.
145  *  MADV_SEQUENTIAL - pages in the given range will probably be accessed
146  *              once, so they can be aggressively read ahead, and
147  *              can be freed soon after they are accessed.
148  *  MADV_WILLNEED - the application is notifying the system to read
149  *              some pages ahead.
150  *  MADV_DONTNEED - the application is finished with the given range,
151  *              so the kernel can free resources associated with it.
152  *
153  * return values:
154  *  zero    - success
155  *  -EINVAL - start + len < 0, start is not page-aligned,
156  *              "behavior" is not a valid value, or application
157  *              is attempting to release locked or shared pages.
158  *  -ENOMEM - addresses in the specified range are not currently
159  *              mapped, or are outside the AS of the process.
160  *  -EIO    - an I/O error occurred while paging in data.
161  *  -EBADF  - map exists, but area maps something that isn't a file.
162  *  -EAGAIN - a kernel resource was temporarily unavailable.
163  */
164 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior)
165 {
166         unsigned long end;
167         struct vm_area_struct * vma;
168         int unmapped_error = 0;
169         int error = -EINVAL;
170
171         down_write(&current->mm->mmap_sem);
172
173         if (start & ~PAGE_MASK)
174                 goto out;
175         len = (len + ~PAGE_MASK) & PAGE_MASK;
176         end = start + len;
177         if (end < start)
178                 goto out;
179
180         error = 0;
181         if (end == start)
182                 goto out;
183
184         /*
185          * If the interval [start,end) covers some unmapped address
186          * ranges, just ignore them, but return -ENOMEM at the end.
187          */
188         vma = find_vma(current->mm, start);
189         for (;;) {
190                 /* Still start < end. */
191                 error = -ENOMEM;
192                 if (!vma)
193                         goto out;
194
195                 /* Here start < vma->vm_end. */
196                 if (start < vma->vm_start) {
197                         unmapped_error = -ENOMEM;
198                         start = vma->vm_start;
199                 }
200
201                 /* Here vma->vm_start <= start < vma->vm_end. */
202                 if (end <= vma->vm_end) {
203                         if (start < end) {
204                                 error = madvise_vma(vma, start, end,
205                                                         behavior);
206                                 if (error)
207                                         goto out;
208                         }
209                         error = unmapped_error;
210                         goto out;
211                 }
212
213                 /* Here vma->vm_start <= start < vma->vm_end < end. */
214                 error = madvise_vma(vma, start, vma->vm_end, behavior);
215                 if (error)
216                         goto out;
217                 start = vma->vm_end;
218                 vma = vma->vm_next;
219         }
220
221 out:
222         up_write(&current->mm->mmap_sem);
223         return error;
224 }