Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | #include <linux/syscalls.h> |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 11 | #include <linux/mempolicy.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/hugetlb.h> |
| 13 | |
| 14 | /* |
| 15 | * We can potentially split a vm area into separate |
| 16 | * areas, each area with its own behavior. |
| 17 | */ |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
| 22 | struct mm_struct * mm = vma->vm_mm; |
| 23 | int error = 0; |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 24 | pgoff_t pgoff; |
Prasanna Meda | e798c6e | 2005-06-21 17:14:36 -0700 | [diff] [blame] | 25 | int new_flags = vma->vm_flags & ~VM_READHINTMASK; |
| 26 | |
| 27 | switch (behavior) { |
| 28 | case MADV_SEQUENTIAL: |
| 29 | new_flags |= VM_SEQ_READ; |
| 30 | break; |
| 31 | case MADV_RANDOM: |
| 32 | new_flags |= VM_RAND_READ; |
| 33 | break; |
| 34 | default: |
| 35 | break; |
| 36 | } |
| 37 | |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 38 | if (new_flags == vma->vm_flags) { |
| 39 | *prev = vma; |
Hugh Dickins | 836d5ff | 2005-09-03 15:54:53 -0700 | [diff] [blame] | 40 | goto out; |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 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)); |
| 46 | if (*prev) { |
| 47 | vma = *prev; |
| 48 | goto success; |
| 49 | } |
| 50 | |
| 51 | *prev = vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | if (start != vma->vm_start) { |
| 54 | error = split_vma(mm, vma, start, 1); |
| 55 | if (error) |
| 56 | goto out; |
| 57 | } |
| 58 | |
| 59 | if (end != vma->vm_end) { |
| 60 | error = split_vma(mm, vma, end, 0); |
| 61 | if (error) |
| 62 | goto out; |
| 63 | } |
| 64 | |
Hugh Dickins | 836d5ff | 2005-09-03 15:54:53 -0700 | [diff] [blame] | 65 | success: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | /* |
| 67 | * vm_flags is protected by the mmap_sem held in write mode. |
| 68 | */ |
Prasanna Meda | e798c6e | 2005-06-21 17:14:36 -0700 | [diff] [blame] | 69 | vma->vm_flags = new_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | out: |
| 72 | if (error == -ENOMEM) |
| 73 | error = -EAGAIN; |
| 74 | return error; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Schedule all required I/O operations. Do not wait for completion. |
| 79 | */ |
| 80 | static long madvise_willneed(struct vm_area_struct * vma, |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 81 | struct vm_area_struct ** prev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | unsigned long start, unsigned long end) |
| 83 | { |
| 84 | struct file *file = vma->vm_file; |
| 85 | |
Suzuki | 1bef400 | 2005-10-11 08:29:06 -0700 | [diff] [blame] | 86 | if (!file) |
| 87 | return -EBADF; |
| 88 | |
Carsten Otte | fe77ba6 | 2005-06-23 22:05:29 -0700 | [diff] [blame] | 89 | if (file->f_mapping->a_ops->get_xip_page) { |
| 90 | /* no bad return value, but ignore advice */ |
| 91 | return 0; |
| 92 | } |
| 93 | |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 94 | *prev = vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
| 96 | if (end > vma->vm_end) |
| 97 | end = vma->vm_end; |
| 98 | end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
| 99 | |
| 100 | force_page_cache_readahead(file->f_mapping, |
| 101 | file, start, max_sane_readahead(end - start)); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Application no longer needs these pages. If the pages are dirty, |
| 107 | * it's OK to just throw them away. The app will be more careful about |
| 108 | * data it wants to keep. Be sure to free swap resources too. The |
| 109 | * zap_page_range call sets things up for refill_inactive to actually free |
| 110 | * these pages later if no one else has touched them in the meantime, |
| 111 | * although we could add these pages to a global reuse list for |
| 112 | * refill_inactive to pick up before reclaiming other pages. |
| 113 | * |
| 114 | * NB: This interface discards data rather than pushes it out to swap, |
| 115 | * as some implementations do. This has performance implications for |
| 116 | * applications like large transactional databases which want to discard |
| 117 | * pages in anonymous maps after committing to backing store the data |
| 118 | * that was kept in them. There is no reason to write this data out to |
| 119 | * the swap area if the application is discarding it. |
| 120 | * |
| 121 | * An interface that causes the system to free clean pages and flush |
| 122 | * dirty pages is already available as msync(MS_INVALIDATE). |
| 123 | */ |
| 124 | static long madvise_dontneed(struct vm_area_struct * vma, |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 125 | struct vm_area_struct ** prev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | unsigned long start, unsigned long end) |
| 127 | { |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 128 | *prev = vma; |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 129 | if (vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | return -EINVAL; |
| 131 | |
| 132 | if (unlikely(vma->vm_flags & VM_NONLINEAR)) { |
| 133 | struct zap_details details = { |
| 134 | .nonlinear_vma = vma, |
| 135 | .last_index = ULONG_MAX, |
| 136 | }; |
| 137 | zap_page_range(vma, start, end - start, &details); |
| 138 | } else |
| 139 | zap_page_range(vma, start, end - start, NULL); |
| 140 | return 0; |
| 141 | } |
| 142 | |
Badari Pulavarty | f6b3ec2 | 2006-01-06 00:10:38 -0800 | [diff] [blame] | 143 | /* |
| 144 | * Application wants to free up the pages and associated backing store. |
| 145 | * This is effectively punching a hole into the middle of a file. |
| 146 | * |
| 147 | * NOTE: Currently, only shmfs/tmpfs is supported for this operation. |
| 148 | * Other filesystems return -ENOSYS. |
| 149 | */ |
| 150 | static long madvise_remove(struct vm_area_struct *vma, |
| 151 | unsigned long start, unsigned long end) |
| 152 | { |
| 153 | struct address_space *mapping; |
| 154 | loff_t offset, endoff; |
| 155 | |
| 156 | if (vma->vm_flags & (VM_LOCKED|VM_NONLINEAR|VM_HUGETLB)) |
| 157 | return -EINVAL; |
| 158 | |
| 159 | if (!vma->vm_file || !vma->vm_file->f_mapping |
| 160 | || !vma->vm_file->f_mapping->host) { |
| 161 | return -EINVAL; |
| 162 | } |
| 163 | |
| 164 | mapping = vma->vm_file->f_mapping; |
| 165 | |
| 166 | offset = (loff_t)(start - vma->vm_start) |
| 167 | + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); |
| 168 | endoff = (loff_t)(end - vma->vm_start - 1) |
| 169 | + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); |
| 170 | return vmtruncate_range(mapping->host, offset, endoff); |
| 171 | } |
| 172 | |
suzuki | 165cd40 | 2005-07-27 11:43:59 -0700 | [diff] [blame] | 173 | static long |
| 174 | madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev, |
| 175 | unsigned long start, unsigned long end, int behavior) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
Suzuki | 1bef400 | 2005-10-11 08:29:06 -0700 | [diff] [blame] | 177 | long error; |
suzuki | 165cd40 | 2005-07-27 11:43:59 -0700 | [diff] [blame] | 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | switch (behavior) { |
| 180 | case MADV_NORMAL: |
| 181 | case MADV_SEQUENTIAL: |
| 182 | case MADV_RANDOM: |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 183 | error = madvise_behavior(vma, prev, start, end, behavior); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | break; |
Badari Pulavarty | f6b3ec2 | 2006-01-06 00:10:38 -0800 | [diff] [blame] | 185 | case MADV_REMOVE: |
| 186 | error = madvise_remove(vma, start, end); |
| 187 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | case MADV_WILLNEED: |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 190 | error = madvise_willneed(vma, prev, start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | break; |
| 192 | |
| 193 | case MADV_DONTNEED: |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 194 | error = madvise_dontneed(vma, prev, start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | break; |
| 196 | |
| 197 | default: |
| 198 | error = -EINVAL; |
| 199 | break; |
| 200 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | return error; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * The madvise(2) system call. |
| 206 | * |
| 207 | * Applications can use madvise() to advise the kernel how it should |
| 208 | * handle paging I/O in this VM area. The idea is to help the kernel |
| 209 | * use appropriate read-ahead and caching techniques. The information |
| 210 | * provided is advisory only, and can be safely disregarded by the |
| 211 | * kernel without affecting the correct operation of the application. |
| 212 | * |
| 213 | * behavior values: |
| 214 | * MADV_NORMAL - the default behavior is to read clusters. This |
| 215 | * results in some read-ahead and read-behind. |
| 216 | * MADV_RANDOM - the system should read the minimum amount of data |
| 217 | * on any access, since it is unlikely that the appli- |
| 218 | * cation will need more than what it asks for. |
| 219 | * MADV_SEQUENTIAL - pages in the given range will probably be accessed |
| 220 | * once, so they can be aggressively read ahead, and |
| 221 | * can be freed soon after they are accessed. |
| 222 | * MADV_WILLNEED - the application is notifying the system to read |
| 223 | * some pages ahead. |
| 224 | * MADV_DONTNEED - the application is finished with the given range, |
| 225 | * so the kernel can free resources associated with it. |
Badari Pulavarty | f6b3ec2 | 2006-01-06 00:10:38 -0800 | [diff] [blame] | 226 | * MADV_REMOVE - the application wants to free up the given range of |
| 227 | * pages and associated backing store. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | * |
| 229 | * return values: |
| 230 | * zero - success |
| 231 | * -EINVAL - start + len < 0, start is not page-aligned, |
| 232 | * "behavior" is not a valid value, or application |
| 233 | * is attempting to release locked or shared pages. |
| 234 | * -ENOMEM - addresses in the specified range are not currently |
| 235 | * mapped, or are outside the AS of the process. |
| 236 | * -EIO - an I/O error occurred while paging in data. |
| 237 | * -EBADF - map exists, but area maps something that isn't a file. |
| 238 | * -EAGAIN - a kernel resource was temporarily unavailable. |
| 239 | */ |
| 240 | asmlinkage long sys_madvise(unsigned long start, size_t len_in, int behavior) |
| 241 | { |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 242 | unsigned long end, tmp; |
| 243 | struct vm_area_struct * vma, *prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | int unmapped_error = 0; |
| 245 | int error = -EINVAL; |
| 246 | size_t len; |
| 247 | |
| 248 | down_write(¤t->mm->mmap_sem); |
| 249 | |
| 250 | if (start & ~PAGE_MASK) |
| 251 | goto out; |
| 252 | len = (len_in + ~PAGE_MASK) & PAGE_MASK; |
| 253 | |
| 254 | /* Check to see whether len was rounded up from small -ve to zero */ |
| 255 | if (len_in && !len) |
| 256 | goto out; |
| 257 | |
| 258 | end = start + len; |
| 259 | if (end < start) |
| 260 | goto out; |
| 261 | |
| 262 | error = 0; |
| 263 | if (end == start) |
| 264 | goto out; |
| 265 | |
| 266 | /* |
| 267 | * If the interval [start,end) covers some unmapped address |
| 268 | * ranges, just ignore them, but return -ENOMEM at the end. |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 269 | * - different from the way of handling in mlock etc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | */ |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 271 | vma = find_vma_prev(current->mm, start, &prev); |
Hugh Dickins | 836d5ff | 2005-09-03 15:54:53 -0700 | [diff] [blame] | 272 | if (vma && start > vma->vm_start) |
| 273 | prev = vma; |
| 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | for (;;) { |
| 276 | /* Still start < end. */ |
| 277 | error = -ENOMEM; |
| 278 | if (!vma) |
| 279 | goto out; |
| 280 | |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 281 | /* Here start < (end|vma->vm_end). */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | if (start < vma->vm_start) { |
| 283 | unmapped_error = -ENOMEM; |
| 284 | start = vma->vm_start; |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 285 | if (start >= end) |
| 286 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 289 | /* Here vma->vm_start <= start < (end|vma->vm_end) */ |
| 290 | tmp = vma->vm_end; |
| 291 | if (end < tmp) |
| 292 | tmp = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 294 | /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */ |
| 295 | error = madvise_vma(vma, &prev, start, tmp, behavior); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | if (error) |
| 297 | goto out; |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 298 | start = tmp; |
| 299 | if (start < prev->vm_end) |
| 300 | start = prev->vm_end; |
| 301 | error = unmapped_error; |
| 302 | if (start >= end) |
| 303 | goto out; |
| 304 | vma = prev->vm_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | out: |
| 307 | up_write(¤t->mm->mmap_sem); |
| 308 | return error; |
| 309 | } |