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; |
| 40 | goto success; |
| 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 | |
| 65 | /* |
| 66 | * vm_flags is protected by the mmap_sem held in write mode. |
| 67 | */ |
Prasanna Meda | e798c6e | 2005-06-21 17:14:36 -0700 | [diff] [blame] | 68 | vma->vm_flags = new_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | out: |
| 71 | if (error == -ENOMEM) |
| 72 | error = -EAGAIN; |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 73 | success: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Carsten Otte | fe77ba6 | 2005-06-23 22:05:29 -0700 | [diff] [blame] | 86 | if (file->f_mapping->a_ops->get_xip_page) { |
| 87 | /* no bad return value, but ignore advice */ |
| 88 | return 0; |
| 89 | } |
| 90 | |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 91 | *prev = vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
| 93 | if (end > vma->vm_end) |
| 94 | end = vma->vm_end; |
| 95 | end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
| 96 | |
| 97 | force_page_cache_readahead(file->f_mapping, |
| 98 | file, start, max_sane_readahead(end - start)); |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Application no longer needs these pages. If the pages are dirty, |
| 104 | * it's OK to just throw them away. The app will be more careful about |
| 105 | * data it wants to keep. Be sure to free swap resources too. The |
| 106 | * zap_page_range call sets things up for refill_inactive to actually free |
| 107 | * these pages later if no one else has touched them in the meantime, |
| 108 | * although we could add these pages to a global reuse list for |
| 109 | * refill_inactive to pick up before reclaiming other pages. |
| 110 | * |
| 111 | * NB: This interface discards data rather than pushes it out to swap, |
| 112 | * as some implementations do. This has performance implications for |
| 113 | * applications like large transactional databases which want to discard |
| 114 | * pages in anonymous maps after committing to backing store the data |
| 115 | * that was kept in them. There is no reason to write this data out to |
| 116 | * the swap area if the application is discarding it. |
| 117 | * |
| 118 | * An interface that causes the system to free clean pages and flush |
| 119 | * dirty pages is already available as msync(MS_INVALIDATE). |
| 120 | */ |
| 121 | static long madvise_dontneed(struct vm_area_struct * vma, |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 122 | struct vm_area_struct ** prev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | unsigned long start, unsigned long end) |
| 124 | { |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 125 | *prev = vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | if ((vma->vm_flags & VM_LOCKED) || is_vm_hugetlb_page(vma)) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | if (unlikely(vma->vm_flags & VM_NONLINEAR)) { |
| 130 | struct zap_details details = { |
| 131 | .nonlinear_vma = vma, |
| 132 | .last_index = ULONG_MAX, |
| 133 | }; |
| 134 | zap_page_range(vma, start, end - start, &details); |
| 135 | } else |
| 136 | zap_page_range(vma, start, end - start, NULL); |
| 137 | return 0; |
| 138 | } |
| 139 | |
suzuki | 165cd40 | 2005-07-27 11:43:59 -0700 | [diff] [blame] | 140 | static long |
| 141 | madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev, |
| 142 | unsigned long start, unsigned long end, int behavior) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
suzuki | 165cd40 | 2005-07-27 11:43:59 -0700 | [diff] [blame] | 144 | struct file *filp = vma->vm_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | long error = -EBADF; |
| 146 | |
suzuki | 165cd40 | 2005-07-27 11:43:59 -0700 | [diff] [blame] | 147 | if (!filp) |
| 148 | goto out; |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | switch (behavior) { |
| 151 | case MADV_NORMAL: |
| 152 | case MADV_SEQUENTIAL: |
| 153 | case MADV_RANDOM: |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 154 | error = madvise_behavior(vma, prev, start, end, behavior); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | break; |
| 156 | |
| 157 | case MADV_WILLNEED: |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 158 | error = madvise_willneed(vma, prev, start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | break; |
| 160 | |
| 161 | case MADV_DONTNEED: |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 162 | error = madvise_dontneed(vma, prev, start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | break; |
| 164 | |
| 165 | default: |
| 166 | error = -EINVAL; |
| 167 | break; |
| 168 | } |
| 169 | |
suzuki | 165cd40 | 2005-07-27 11:43:59 -0700 | [diff] [blame] | 170 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | return error; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * The madvise(2) system call. |
| 176 | * |
| 177 | * Applications can use madvise() to advise the kernel how it should |
| 178 | * handle paging I/O in this VM area. The idea is to help the kernel |
| 179 | * use appropriate read-ahead and caching techniques. The information |
| 180 | * provided is advisory only, and can be safely disregarded by the |
| 181 | * kernel without affecting the correct operation of the application. |
| 182 | * |
| 183 | * behavior values: |
| 184 | * MADV_NORMAL - the default behavior is to read clusters. This |
| 185 | * results in some read-ahead and read-behind. |
| 186 | * MADV_RANDOM - the system should read the minimum amount of data |
| 187 | * on any access, since it is unlikely that the appli- |
| 188 | * cation will need more than what it asks for. |
| 189 | * MADV_SEQUENTIAL - pages in the given range will probably be accessed |
| 190 | * once, so they can be aggressively read ahead, and |
| 191 | * can be freed soon after they are accessed. |
| 192 | * MADV_WILLNEED - the application is notifying the system to read |
| 193 | * some pages ahead. |
| 194 | * MADV_DONTNEED - the application is finished with the given range, |
| 195 | * so the kernel can free resources associated with it. |
| 196 | * |
| 197 | * return values: |
| 198 | * zero - success |
| 199 | * -EINVAL - start + len < 0, start is not page-aligned, |
| 200 | * "behavior" is not a valid value, or application |
| 201 | * is attempting to release locked or shared pages. |
| 202 | * -ENOMEM - addresses in the specified range are not currently |
| 203 | * mapped, or are outside the AS of the process. |
| 204 | * -EIO - an I/O error occurred while paging in data. |
| 205 | * -EBADF - map exists, but area maps something that isn't a file. |
| 206 | * -EAGAIN - a kernel resource was temporarily unavailable. |
| 207 | */ |
| 208 | asmlinkage long sys_madvise(unsigned long start, size_t len_in, int behavior) |
| 209 | { |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 210 | unsigned long end, tmp; |
| 211 | struct vm_area_struct * vma, *prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | int unmapped_error = 0; |
| 213 | int error = -EINVAL; |
| 214 | size_t len; |
| 215 | |
| 216 | down_write(¤t->mm->mmap_sem); |
| 217 | |
| 218 | if (start & ~PAGE_MASK) |
| 219 | goto out; |
| 220 | len = (len_in + ~PAGE_MASK) & PAGE_MASK; |
| 221 | |
| 222 | /* Check to see whether len was rounded up from small -ve to zero */ |
| 223 | if (len_in && !len) |
| 224 | goto out; |
| 225 | |
| 226 | end = start + len; |
| 227 | if (end < start) |
| 228 | goto out; |
| 229 | |
| 230 | error = 0; |
| 231 | if (end == start) |
| 232 | goto out; |
| 233 | |
| 234 | /* |
| 235 | * If the interval [start,end) covers some unmapped address |
| 236 | * ranges, just ignore them, but return -ENOMEM at the end. |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 237 | * - different from the way of handling in mlock etc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | */ |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 239 | vma = find_vma_prev(current->mm, start, &prev); |
| 240 | if (!vma && prev) |
| 241 | vma = prev->vm_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | for (;;) { |
| 243 | /* Still start < end. */ |
| 244 | error = -ENOMEM; |
| 245 | if (!vma) |
| 246 | goto out; |
| 247 | |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 248 | /* Here start < (end|vma->vm_end). */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | if (start < vma->vm_start) { |
| 250 | unmapped_error = -ENOMEM; |
| 251 | start = vma->vm_start; |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 252 | if (start >= end) |
| 253 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 256 | /* Here vma->vm_start <= start < (end|vma->vm_end) */ |
| 257 | tmp = vma->vm_end; |
| 258 | if (end < tmp) |
| 259 | tmp = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 261 | /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */ |
| 262 | error = madvise_vma(vma, &prev, start, tmp, behavior); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (error) |
| 264 | goto out; |
Prasanna Meda | 05b7438 | 2005-06-21 17:14:37 -0700 | [diff] [blame] | 265 | start = tmp; |
| 266 | if (start < prev->vm_end) |
| 267 | start = prev->vm_end; |
| 268 | error = unmapped_error; |
| 269 | if (start >= end) |
| 270 | goto out; |
| 271 | vma = prev->vm_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | out: |
| 274 | up_write(¤t->mm->mmap_sem); |
| 275 | return error; |
| 276 | } |