linux-bk/mm/madvise.c
<<
>>
Prefs
   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 */
  16static 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        vma->vm_raend = 0;
  36        VM_ClearReadHint(vma);
  37
  38        switch (behavior) {
  39        case MADV_SEQUENTIAL:
  40                vma->vm_flags |= VM_SEQ_READ;
  41                break;
  42        case MADV_RANDOM:
  43                vma->vm_flags |= VM_RAND_READ;
  44                break;
  45        default:
  46                break;
  47        }
  48        spin_unlock(&mm->page_table_lock);
  49
  50        return 0;
  51}
  52
  53/*
  54 * Schedule all required I/O operations, then run the disk queue
  55 * to make sure they are started.  Do not wait for completion.
  56 */
  57static long madvise_willneed(struct vm_area_struct * vma,
  58                             unsigned long start, unsigned long end)
  59{
  60        long error = -EBADF;
  61        struct file * file;
  62        unsigned long size, rlim_rss;
  63
  64        /* Doesn't work if there's no mapped file. */
  65        if (!vma->vm_file)
  66                return error;
  67        file = vma->vm_file;
  68        size = (file->f_dentry->d_inode->i_size + PAGE_CACHE_SIZE - 1) >>
  69                                                        PAGE_CACHE_SHIFT;
  70
  71        start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  72        if (end > vma->vm_end)
  73                end = vma->vm_end;
  74        end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  75
  76        /* Make sure this doesn't exceed the process's max rss. */
  77        error = -EIO;
  78        rlim_rss = current->rlim ?  current->rlim[RLIMIT_RSS].rlim_cur :
  79                                LONG_MAX; /* default: see resource.h */
  80        if ((vma->vm_mm->rss + (end - start)) > rlim_rss)
  81                return error;
  82
  83        do_page_cache_readahead(file, start, end - start);
  84        return 0;
  85}
  86
  87/*
  88 * Application no longer needs these pages.  If the pages are dirty,
  89 * it's OK to just throw them away.  The app will be more careful about
  90 * data it wants to keep.  Be sure to free swap resources too.  The
  91 * zap_page_range call sets things up for refill_inactive to actually free
  92 * these pages later if no one else has touched them in the meantime,
  93 * although we could add these pages to a global reuse list for
  94 * refill_inactive to pick up before reclaiming other pages.
  95 *
  96 * NB: This interface discards data rather than pushes it out to swap,
  97 * as some implementations do.  This has performance implications for
  98 * applications like large transactional databases which want to discard
  99 * pages in anonymous maps after committing to backing store the data
 100 * that was kept in them.  There is no reason to write this data out to
 101 * the swap area if the application is discarding it.
 102 *
 103 * An interface that causes the system to free clean pages and flush
 104 * dirty pages is already available as msync(MS_INVALIDATE).
 105 */
 106static long madvise_dontneed(struct vm_area_struct * vma,
 107                             unsigned long start, unsigned long end)
 108{
 109        if (vma->vm_flags & VM_LOCKED)
 110                return -EINVAL;
 111
 112        zap_page_range(vma, start, end - start);
 113        return 0;
 114}
 115
 116static long madvise_vma(struct vm_area_struct * vma, unsigned long start,
 117                        unsigned long end, int behavior)
 118{
 119        long error = -EBADF;
 120
 121        switch (behavior) {
 122        case MADV_NORMAL:
 123        case MADV_SEQUENTIAL:
 124        case MADV_RANDOM:
 125                error = madvise_behavior(vma, start, end, behavior);
 126                break;
 127
 128        case MADV_WILLNEED:
 129                error = madvise_willneed(vma, start, end);
 130                break;
 131
 132        case MADV_DONTNEED:
 133                error = madvise_dontneed(vma, start, end);
 134                break;
 135
 136        default:
 137                error = -EINVAL;
 138                break;
 139        }
 140                
 141        return error;
 142}
 143
 144/*
 145 * The madvise(2) system call.
 146 *
 147 * Applications can use madvise() to advise the kernel how it should
 148 * handle paging I/O in this VM area.  The idea is to help the kernel
 149 * use appropriate read-ahead and caching techniques.  The information
 150 * provided is advisory only, and can be safely disregarded by the
 151 * kernel without affecting the correct operation of the application.
 152 *
 153 * behavior values:
 154 *  MADV_NORMAL - the default behavior is to read clusters.  This
 155 *              results in some read-ahead and read-behind.
 156 *  MADV_RANDOM - the system should read the minimum amount of data
 157 *              on any access, since it is unlikely that the appli-
 158 *              cation will need more than what it asks for.
 159 *  MADV_SEQUENTIAL - pages in the given range will probably be accessed
 160 *              once, so they can be aggressively read ahead, and
 161 *              can be freed soon after they are accessed.
 162 *  MADV_WILLNEED - the application is notifying the system to read
 163 *              some pages ahead.
 164 *  MADV_DONTNEED - the application is finished with the given range,
 165 *              so the kernel can free resources associated with it.
 166 *
 167 * return values:
 168 *  zero    - success
 169 *  -EINVAL - start + len < 0, start is not page-aligned,
 170 *              "behavior" is not a valid value, or application
 171 *              is attempting to release locked or shared pages.
 172 *  -ENOMEM - addresses in the specified range are not currently
 173 *              mapped, or are outside the AS of the process.
 174 *  -EIO    - an I/O error occurred while paging in data.
 175 *  -EBADF  - map exists, but area maps something that isn't a file.
 176 *  -EAGAIN - a kernel resource was temporarily unavailable.
 177 */
 178asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior)
 179{
 180        unsigned long end;
 181        struct vm_area_struct * vma;
 182        int unmapped_error = 0;
 183        int error = -EINVAL;
 184
 185        down_write(&current->mm->mmap_sem);
 186
 187        if (start & ~PAGE_MASK)
 188                goto out;
 189        len = (len + ~PAGE_MASK) & PAGE_MASK;
 190        end = start + len;
 191        if (end < start)
 192                goto out;
 193
 194        error = 0;
 195        if (end == start)
 196                goto out;
 197
 198        /*
 199         * If the interval [start,end) covers some unmapped address
 200         * ranges, just ignore them, but return -ENOMEM at the end.
 201         */
 202        vma = find_vma(current->mm, start);
 203        for (;;) {
 204                /* Still start < end. */
 205                error = -ENOMEM;
 206                if (!vma)
 207                        goto out;
 208
 209                /* Here start < vma->vm_end. */
 210                if (start < vma->vm_start) {
 211                        unmapped_error = -ENOMEM;
 212                        start = vma->vm_start;
 213                }
 214
 215                /* Here vma->vm_start <= start < vma->vm_end. */
 216                if (end <= vma->vm_end) {
 217                        if (start < end) {
 218                                error = madvise_vma(vma, start, end,
 219                                                        behavior);
 220                                if (error)
 221                                        goto out;
 222                        }
 223                        error = unmapped_error;
 224                        goto out;
 225                }
 226
 227                /* Here vma->vm_start <= start < vma->vm_end < end. */
 228                error = madvise_vma(vma, start, vma->vm_end, behavior);
 229                if (error)
 230                        goto out;
 231                start = vma->vm_end;
 232                vma = vma->vm_next;
 233        }
 234
 235out:
 236        up_write(&current->mm->mmap_sem);
 237        return error;
 238}
 239
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.