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/mempolicy.h>
12 #include <linux/hugetlb.h>
15 * We can potentially split a vm area into separate
16 * areas, each area with its own behavior.
18 static long madvise_behavior(struct vm_area_struct
* vma
,
19 struct vm_area_struct
**prev
,
20 unsigned long start
, unsigned long end
, int behavior
)
22 struct mm_struct
* mm
= vma
->vm_mm
;
25 int new_flags
= vma
->vm_flags
& ~VM_READHINTMASK
;
29 new_flags
|= VM_SEQ_READ
;
32 new_flags
|= VM_RAND_READ
;
38 if (new_flags
== vma
->vm_flags
) {
43 pgoff
= vma
->vm_pgoff
+ ((start
- vma
->vm_start
) >> PAGE_SHIFT
);
44 *prev
= vma_merge(mm
, *prev
, start
, end
, new_flags
, vma
->anon_vma
,
45 vma
->vm_file
, pgoff
, vma_policy(vma
));
53 if (start
!= vma
->vm_start
) {
54 error
= split_vma(mm
, vma
, start
, 1);
59 if (end
!= vma
->vm_end
) {
60 error
= split_vma(mm
, vma
, end
, 0);
66 * vm_flags is protected by the mmap_sem held in write mode.
68 vma
->vm_flags
= new_flags
;
78 * Schedule all required I/O operations. Do not wait for completion.
80 static long madvise_willneed(struct vm_area_struct
* vma
,
81 struct vm_area_struct
** prev
,
82 unsigned long start
, unsigned long end
)
84 struct file
*file
= vma
->vm_file
;
90 start
= ((start
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
91 if (end
> vma
->vm_end
)
93 end
= ((end
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
95 force_page_cache_readahead(file
->f_mapping
,
96 file
, start
, max_sane_readahead(end
- start
));
101 * Application no longer needs these pages. If the pages are dirty,
102 * it's OK to just throw them away. The app will be more careful about
103 * data it wants to keep. Be sure to free swap resources too. The
104 * zap_page_range call sets things up for refill_inactive to actually free
105 * these pages later if no one else has touched them in the meantime,
106 * although we could add these pages to a global reuse list for
107 * refill_inactive to pick up before reclaiming other pages.
109 * NB: This interface discards data rather than pushes it out to swap,
110 * as some implementations do. This has performance implications for
111 * applications like large transactional databases which want to discard
112 * pages in anonymous maps after committing to backing store the data
113 * that was kept in them. There is no reason to write this data out to
114 * the swap area if the application is discarding it.
116 * An interface that causes the system to free clean pages and flush
117 * dirty pages is already available as msync(MS_INVALIDATE).
119 static long madvise_dontneed(struct vm_area_struct
* vma
,
120 struct vm_area_struct
** prev
,
121 unsigned long start
, unsigned long end
)
124 if ((vma
->vm_flags
& VM_LOCKED
) || is_vm_hugetlb_page(vma
))
127 if (unlikely(vma
->vm_flags
& VM_NONLINEAR
)) {
128 struct zap_details details
= {
129 .nonlinear_vma
= vma
,
130 .last_index
= ULONG_MAX
,
132 zap_page_range(vma
, start
, end
- start
, &details
);
134 zap_page_range(vma
, start
, end
- start
, NULL
);
138 static long madvise_vma(struct vm_area_struct
*vma
, struct vm_area_struct
**prev
,
139 unsigned long start
, unsigned long end
, int behavior
)
145 case MADV_SEQUENTIAL
:
147 error
= madvise_behavior(vma
, prev
, start
, end
, behavior
);
151 error
= madvise_willneed(vma
, prev
, start
, end
);
155 error
= madvise_dontneed(vma
, prev
, start
, end
);
167 * The madvise(2) system call.
169 * Applications can use madvise() to advise the kernel how it should
170 * handle paging I/O in this VM area. The idea is to help the kernel
171 * use appropriate read-ahead and caching techniques. The information
172 * provided is advisory only, and can be safely disregarded by the
173 * kernel without affecting the correct operation of the application.
176 * MADV_NORMAL - the default behavior is to read clusters. This
177 * results in some read-ahead and read-behind.
178 * MADV_RANDOM - the system should read the minimum amount of data
179 * on any access, since it is unlikely that the appli-
180 * cation will need more than what it asks for.
181 * MADV_SEQUENTIAL - pages in the given range will probably be accessed
182 * once, so they can be aggressively read ahead, and
183 * can be freed soon after they are accessed.
184 * MADV_WILLNEED - the application is notifying the system to read
186 * MADV_DONTNEED - the application is finished with the given range,
187 * so the kernel can free resources associated with it.
191 * -EINVAL - start + len < 0, start is not page-aligned,
192 * "behavior" is not a valid value, or application
193 * is attempting to release locked or shared pages.
194 * -ENOMEM - addresses in the specified range are not currently
195 * mapped, or are outside the AS of the process.
196 * -EIO - an I/O error occurred while paging in data.
197 * -EBADF - map exists, but area maps something that isn't a file.
198 * -EAGAIN - a kernel resource was temporarily unavailable.
200 asmlinkage
long sys_madvise(unsigned long start
, size_t len_in
, int behavior
)
202 unsigned long end
, tmp
;
203 struct vm_area_struct
* vma
, *prev
;
204 int unmapped_error
= 0;
208 down_write(¤t
->mm
->mmap_sem
);
210 if (start
& ~PAGE_MASK
)
212 len
= (len_in
+ ~PAGE_MASK
) & PAGE_MASK
;
214 /* Check to see whether len was rounded up from small -ve to zero */
227 * If the interval [start,end) covers some unmapped address
228 * ranges, just ignore them, but return -ENOMEM at the end.
229 * - different from the way of handling in mlock etc.
231 vma
= find_vma_prev(current
->mm
, start
, &prev
);
235 /* Still start < end. */
240 /* Here start < (end|vma->vm_end). */
241 if (start
< vma
->vm_start
) {
242 unmapped_error
= -ENOMEM
;
243 start
= vma
->vm_start
;
248 /* Here vma->vm_start <= start < (end|vma->vm_end) */
253 /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
254 error
= madvise_vma(vma
, &prev
, start
, tmp
, behavior
);
258 if (start
< prev
->vm_end
)
259 start
= prev
->vm_end
;
260 error
= unmapped_error
;
266 up_write(¤t
->mm
->mmap_sem
);