linux-bk/include/linux/mm.h
<<
>>
Prefs
   1#ifndef _LINUX_MM_H
   2#define _LINUX_MM_H
   3
   4#include <linux/sched.h>
   5#include <linux/errno.h>
   6
   7#ifdef __KERNEL__
   8
   9#include <linux/config.h>
  10#include <linux/gfp.h>
  11#include <linux/string.h>
  12#include <linux/list.h>
  13#include <linux/mmzone.h>
  14#include <linux/swap.h>
  15#include <linux/rbtree.h>
  16#include <linux/fs.h>
  17
  18extern unsigned long max_mapnr;
  19extern unsigned long num_physpages;
  20extern void * high_memory;
  21extern int page_cluster;
  22/* The inactive_clean lists are per zone. */
  23extern struct list_head active_list;
  24extern struct list_head inactive_list;
  25
  26#include <asm/page.h>
  27#include <asm/pgtable.h>
  28#include <asm/atomic.h>
  29
  30/*
  31 * Linux kernel virtual memory manager primitives.
  32 * The idea being to have a "virtual" mm in the same way
  33 * we have a virtual fs - giving a cleaner interface to the
  34 * mm details, and allowing different kinds of memory mappings
  35 * (from shared memory to executable loading to arbitrary
  36 * mmap() functions).
  37 */
  38
  39/*
  40 * This struct defines a memory VMM memory area. There is one of these
  41 * per VM-area/task.  A VM area is any part of the process virtual memory
  42 * space that has a special rule for the page-fault handlers (ie a shared
  43 * library, the executable area etc).
  44 */
  45struct vm_area_struct {
  46        struct mm_struct * vm_mm;       /* The address space we belong to. */
  47        unsigned long vm_start;         /* Our start address within vm_mm. */
  48        unsigned long vm_end;           /* The first byte after our end address
  49                                           within vm_mm. */
  50
  51        /* linked list of VM areas per task, sorted by address */
  52        struct vm_area_struct *vm_next;
  53
  54        pgprot_t vm_page_prot;          /* Access permissions of this VMA. */
  55        unsigned long vm_flags;         /* Flags, listed below. */
  56
  57        rb_node_t vm_rb;
  58
  59        /*
  60         * For areas with an address space and backing store,
  61         * one of the address_space->i_mmap{,shared} lists,
  62         * for shm areas, the list of attaches, otherwise unused.
  63         */
  64        list_t shared;
  65
  66        /* Function pointers to deal with this struct. */
  67        struct vm_operations_struct * vm_ops;
  68
  69        /* Information about our backing store: */
  70        unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
  71                                           units, *not* PAGE_CACHE_SIZE */
  72        struct file * vm_file;          /* File we map to (can be NULL). */
  73        unsigned long vm_raend;         /* XXX: put full readahead info here. */
  74        void * vm_private_data;         /* was vm_pte (shared mem) */
  75};
  76
  77/*
  78 * vm_flags..
  79 */
  80#define VM_READ         0x00000001      /* currently active flags */
  81#define VM_WRITE        0x00000002
  82#define VM_EXEC         0x00000004
  83#define VM_SHARED       0x00000008
  84
  85#define VM_MAYREAD      0x00000010      /* limits for mprotect() etc */
  86#define VM_MAYWRITE     0x00000020
  87#define VM_MAYEXEC      0x00000040
  88#define VM_MAYSHARE     0x00000080
  89
  90#define VM_GROWSDOWN    0x00000100      /* general info on the segment */
  91#define VM_GROWSUP      0x00000200
  92#define VM_SHM          0x00000400      /* shared memory area, don't swap out */
  93#define VM_DENYWRITE    0x00000800      /* ETXTBSY on write attempts.. */
  94
  95#define VM_EXECUTABLE   0x00001000
  96#define VM_LOCKED       0x00002000
  97#define VM_IO           0x00004000      /* Memory mapped I/O or similar */
  98
  99                                        /* Used by sys_madvise() */
 100#define VM_SEQ_READ     0x00008000      /* App will access data sequentially */
 101#define VM_RAND_READ    0x00010000      /* App will not benefit from clustered reads */
 102
 103#define VM_DONTCOPY     0x00020000      /* Do not copy this vma on fork */
 104#define VM_DONTEXPAND   0x00040000      /* Cannot expand with mremap() */
 105#define VM_RESERVED     0x00080000      /* Don't unmap it from swap_out */
 106
 107#define VM_STACK_FLAGS  (0x00000100 | VM_DATA_DEFAULT_FLAGS)
 108
 109#define VM_READHINTMASK                 (VM_SEQ_READ | VM_RAND_READ)
 110#define VM_ClearReadHint(v)             (v)->vm_flags &= ~VM_READHINTMASK
 111#define VM_NormalReadHint(v)            (!((v)->vm_flags & VM_READHINTMASK))
 112#define VM_SequentialReadHint(v)        ((v)->vm_flags & VM_SEQ_READ)
 113#define VM_RandomReadHint(v)            ((v)->vm_flags & VM_RAND_READ)
 114
 115/*
 116 * mapping from the currently active vm_flags protection bits (the
 117 * low four bits) to a page protection mask..
 118 */
 119extern pgprot_t protection_map[16];
 120
 121
 122/*
 123 * These are the virtual MM functions - opening of an area, closing and
 124 * unmapping it (needed to keep files on disk up-to-date etc), pointer
 125 * to the functions called when a no-page or a wp-page exception occurs. 
 126 */
 127struct vm_operations_struct {
 128        void (*open)(struct vm_area_struct * area);
 129        void (*close)(struct vm_area_struct * area);
 130        struct page * (*nopage)(struct vm_area_struct * area, unsigned long address, int unused);
 131};
 132
 133/* forward declaration; pte_chain is meant to be internal to rmap.c */
 134struct pte_chain;
 135
 136/*
 137 * Each physical page in the system has a struct page associated with
 138 * it to keep track of whatever it is we are using the page for at the
 139 * moment. Note that we have no way to track which tasks are using
 140 * a page.
 141 *
 142 * Try to keep the most commonly accessed fields in single cache lines
 143 * here (16 bytes or greater).  This ordering should be particularly
 144 * beneficial on 32-bit processors.
 145 *
 146 * The first line is data used in page cache lookup, the second line
 147 * is used for linear searches (eg. clock algorithm scans). 
 148 *
 149 * TODO: make this structure smaller, it could be as small as 32 bytes.
 150 */
 151struct page {
 152        struct list_head list;          /* ->mapping has some page lists. */
 153        struct address_space *mapping;  /* The inode (or ...) we belong to. */
 154        unsigned long index;            /* Our offset within mapping. */
 155        atomic_t count;                 /* Usage count, see below. */
 156        unsigned long flags;            /* atomic flags, some possibly
 157                                           updated asynchronously */
 158        struct list_head lru;           /* Pageout list, eg. active_list;
 159                                           protected by pagemap_lru_lock !! */
 160        union {
 161                struct pte_chain * chain;       /* Reverse pte mapping pointer.
 162                                         * protected by PG_chainlock */
 163                pte_t            * direct;
 164        } pte;
 165        unsigned long private;          /* mapping-private opaque data */
 166
 167        /*
 168         * On machines where all RAM is mapped into kernel address space,
 169         * we can simply calculate the virtual address. On machines with
 170         * highmem some memory is mapped into kernel virtual memory
 171         * dynamically, so we need a place to store that address.
 172         * Note that this field could be 16 bits on x86 ... ;)
 173         *
 174         * Architectures with slow multiplication can define
 175         * WANT_PAGE_VIRTUAL in asm/page.h
 176         */
 177#if defined(CONFIG_HIGHMEM) || defined(WANT_PAGE_VIRTUAL)
 178        void *virtual;                  /* Kernel virtual address (NULL if
 179                                           not kmapped, ie. highmem) */
 180#endif /* CONFIG_HIGMEM || WANT_PAGE_VIRTUAL */
 181};
 182
 183/*
 184 * Methods to modify the page usage count.
 185 *
 186 * What counts for a page usage:
 187 * - cache mapping   (page->mapping)
 188 * - private data    (page->private)
 189 * - page mapped in a task's page tables, each mapping
 190 *   is counted separately
 191 *
 192 * Also, many kernel routines increase the page count before a critical
 193 * routine so they can be sure the page doesn't go away from under them.
 194 */
 195#define get_page(p)             atomic_inc(&(p)->count)
 196#define put_page(p)             __free_page(p)
 197#define put_page_testzero(p)    atomic_dec_and_test(&(p)->count)
 198#define page_count(p)           atomic_read(&(p)->count)
 199#define set_page_count(p,v)     atomic_set(&(p)->count, v)
 200
 201/*
 202 * Multiple processes may "see" the same page. E.g. for untouched
 203 * mappings of /dev/null, all processes see the same page full of
 204 * zeroes, and text pages of executables and shared libraries have
 205 * only one copy in memory, at most, normally.
 206 *
 207 * For the non-reserved pages, page->count denotes a reference count.
 208 *   page->count == 0 means the page is free.
 209 *   page->count == 1 means the page is used for exactly one purpose
 210 *   (e.g. a private data page of one process).
 211 *
 212 * A page may be used for kmalloc() or anyone else who does a
 213 * __get_free_page(). In this case the page->count is at least 1, and
 214 * all other fields are unused but should be 0 or NULL. The
 215 * management of this page is the responsibility of the one who uses
 216 * it.
 217 *
 218 * The other pages (we may call them "process pages") are completely
 219 * managed by the Linux memory manager: I/O, buffers, swapping etc.
 220 * The following discussion applies only to them.
 221 *
 222 * A page may belong to an inode's memory mapping. In this case,
 223 * page->mapping is the pointer to the inode, and page->index is the
 224 * file offset of the page, in units of PAGE_CACHE_SIZE.
 225 *
 226 * A page contains an opaque `private' member, which belongs to the
 227 * page's address_space.  Usually, this is the address of a circular
 228 * list of the page's disk buffers.
 229 *
 230 * For pages belonging to inodes, the page->count is the number of
 231 * attaches, plus 1 if `private' contains something, plus one for
 232 * the page cache itself.
 233 *
 234 * All pages belonging to an inode are in these doubly linked lists:
 235 * mapping->clean_pages, mapping->dirty_pages and mapping->locked_pages;
 236 * using the page->list list_head. These fields are also used for
 237 * freelist managemet (when page->count==0).
 238 *
 239 * There is also a per-mapping radix tree mapping index to the page
 240 * in memory if present. The tree is rooted at mapping->root.  
 241 *
 242 * All process pages can do I/O:
 243 * - inode pages may need to be read from disk,
 244 * - inode pages which have been modified and are MAP_SHARED may need
 245 *   to be written to disk,
 246 * - private pages which have been modified may need to be swapped out
 247 *   to swap space and (later) to be read back into memory.
 248 */
 249
 250/*
 251 * FIXME: take this include out, include page-flags.h in
 252 * files which need it (119 of them)
 253 */
 254#include <linux/page-flags.h>
 255
 256/*
 257 * The zone field is never updated after free_area_init_core()
 258 * sets it, so none of the operations on it need to be atomic.
 259 */
 260#define NODE_SHIFT 4
 261#define ZONE_SHIFT (BITS_PER_LONG - 8)
 262
 263struct zone_struct;
 264extern struct zone_struct *zone_table[];
 265
 266static inline zone_t *page_zone(struct page *page)
 267{
 268        return zone_table[page->flags >> ZONE_SHIFT];
 269}
 270
 271static inline void set_page_zone(struct page *page, unsigned long zone_num)
 272{
 273        page->flags &= ~(~0UL << ZONE_SHIFT);
 274        page->flags |= zone_num << ZONE_SHIFT;
 275}
 276
 277/*
 278 * In order to avoid #ifdefs within C code itself, we define
 279 * set_page_address to a noop for non-highmem machines, where
 280 * the field isn't useful.
 281 * The same is true for page_address() in arch-dependent code.
 282 */
 283#if defined(CONFIG_HIGHMEM) || defined(WANT_PAGE_VIRTUAL)
 284
 285#define set_page_address(page, address)                 \
 286        do {                                            \
 287                (page)->virtual = (address);            \
 288        } while(0)
 289
 290#else /* CONFIG_HIGHMEM || WANT_PAGE_VIRTUAL */
 291#define set_page_address(page, address)  do { } while(0)
 292#endif /* CONFIG_HIGHMEM || WANT_PAGE_VIRTUAL */
 293
 294/*
 295 * Permanent address of a page. Obviously must never be
 296 * called on a highmem page.
 297 */
 298#if defined(CONFIG_HIGHMEM) || defined(WANT_PAGE_VIRTUAL)
 299
 300#define page_address(page) ((page)->virtual)
 301
 302#else /* CONFIG_HIGHMEM || WANT_PAGE_VIRTUAL */
 303
 304#define page_address(page)                                              \
 305        __va( (((page) - page_zone(page)->zone_mem_map) << PAGE_SHIFT)  \
 306                        + page_zone(page)->zone_start_paddr)
 307
 308#endif /* CONFIG_HIGHMEM || WANT_PAGE_VIRTUAL */
 309
 310/*
 311 * Error return values for the *_nopage functions
 312 */
 313#define NOPAGE_SIGBUS   (NULL)
 314#define NOPAGE_OOM      ((struct page *) (-1))
 315
 316/*
 317 * Different kinds of faults, as returned by handle_mm_fault().
 318 * Used to decide whether a process gets delivered SIGBUS or
 319 * just gets major/minor fault counters bumped up.
 320 */
 321#define VM_FAULT_OOM    (-1)
 322#define VM_FAULT_SIGBUS 0
 323#define VM_FAULT_MINOR  1
 324#define VM_FAULT_MAJOR  2
 325
 326/* The array of struct pages */
 327extern struct page *mem_map;
 328
 329extern void show_free_areas(void);
 330extern void show_free_areas_node(pg_data_t *pgdat);
 331
 332extern int fail_writepage(struct page *);
 333struct page * shmem_nopage(struct vm_area_struct * vma, unsigned long address, int unused);
 334struct file *shmem_file_setup(char * name, loff_t size);
 335extern void shmem_lock(struct file * file, int lock);
 336extern int shmem_zero_setup(struct vm_area_struct *);
 337
 338extern void zap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size);
 339extern int copy_page_range(struct mm_struct *dst, struct mm_struct *src, struct vm_area_struct *vma);
 340extern int remap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long to, unsigned long size, pgprot_t prot);
 341extern int zeromap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long size, pgprot_t prot);
 342
 343extern int vmtruncate(struct inode * inode, loff_t offset);
 344extern pmd_t *FASTCALL(__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address));
 345extern pte_t *FASTCALL(pte_alloc_kernel(struct mm_struct *mm, pmd_t *pmd, unsigned long address));
 346extern pte_t *FASTCALL(pte_alloc_map(struct mm_struct *mm, pmd_t *pmd, unsigned long address));
 347extern int handle_mm_fault(struct mm_struct *mm,struct vm_area_struct *vma, unsigned long address, int write_access);
 348extern int make_pages_present(unsigned long addr, unsigned long end);
 349extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
 350extern int ptrace_readdata(struct task_struct *tsk, unsigned long src, char *dst, int len);
 351extern int ptrace_writedata(struct task_struct *tsk, char * src, unsigned long dst, int len);
 352extern int ptrace_attach(struct task_struct *tsk);
 353extern int ptrace_detach(struct task_struct *, unsigned int);
 354extern void ptrace_disable(struct task_struct *);
 355extern int ptrace_check_attach(struct task_struct *task, int kill);
 356
 357int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, unsigned long start,
 358                int len, int write, int force, struct page **pages, struct vm_area_struct **vmas);
 359
 360int __set_page_dirty_buffers(struct page *page);
 361int __set_page_dirty_nobuffers(struct page *page);
 362
 363/*
 364 * If the mapping doesn't provide a set_page_dirty a_op, then
 365 * just fall through and assume that it wants buffer_heads.
 366 * FIXME: make the method unconditional.
 367 */
 368static inline int set_page_dirty(struct page *page)
 369{
 370        if (page->mapping) {
 371                int (*spd)(struct page *);
 372
 373                spd = page->mapping->a_ops->set_page_dirty;
 374                if (spd)
 375                        return (*spd)(page);
 376        }
 377        return __set_page_dirty_buffers(page);
 378}
 379
 380/*
 381 * On a two-level page table, this ends up being trivial. Thus the
 382 * inlining and the symmetry break with pte_alloc_map() that does all
 383 * of this out-of-line.
 384 */
 385static inline pmd_t *pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
 386{
 387        if (pgd_none(*pgd))
 388                return __pmd_alloc(mm, pgd, address);
 389        return pmd_offset(pgd, address);
 390}
 391
 392extern void free_area_init(unsigned long * zones_size);
 393extern void free_area_init_node(int nid, pg_data_t *pgdat, struct page *pmap,
 394        unsigned long * zones_size, unsigned long zone_start_paddr, 
 395        unsigned long *zholes_size);
 396extern void mem_init(void);
 397extern void show_mem(void);
 398extern void si_meminfo(struct sysinfo * val);
 399extern void swapin_readahead(swp_entry_t);
 400
 401extern int can_share_swap_page(struct page *);
 402extern int remove_exclusive_swap_page(struct page *);
 403
 404extern void __free_pte(pte_t);
 405
 406/* mmap.c */
 407extern void lock_vma_mappings(struct vm_area_struct *);
 408extern void unlock_vma_mappings(struct vm_area_struct *);
 409extern void insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
 410extern void __insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
 411extern void build_mmap_rb(struct mm_struct *);
 412extern void exit_mmap(struct mm_struct *);
 413
 414extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
 415
 416extern unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
 417        unsigned long len, unsigned long prot,
 418        unsigned long flag, unsigned long pgoff);
 419
 420static inline unsigned long do_mmap(struct file *file, unsigned long addr,
 421        unsigned long len, unsigned long prot,
 422        unsigned long flag, unsigned long offset)
 423{
 424        unsigned long ret = -EINVAL;
 425        if ((offset + PAGE_ALIGN(len)) < offset)
 426                goto out;
 427        if (!(offset & ~PAGE_MASK))
 428                ret = do_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
 429out:
 430        return ret;
 431}
 432
 433extern int do_munmap(struct mm_struct *, unsigned long, size_t);
 434
 435extern unsigned long do_brk(unsigned long, unsigned long);
 436
 437static inline void __vma_unlink(struct mm_struct * mm, struct vm_area_struct * vma, struct vm_area_struct * prev)
 438{
 439        prev->vm_next = vma->vm_next;
 440        rb_erase(&vma->vm_rb, &mm->mm_rb);
 441        if (mm->mmap_cache == vma)
 442                mm->mmap_cache = prev;
 443}
 444
 445static inline int can_vma_merge(struct vm_area_struct * vma, unsigned long vm_flags)
 446{
 447        if (!vma->vm_file && vma->vm_flags == vm_flags)
 448                return 1;
 449        else
 450                return 0;
 451}
 452
 453struct zone_t;
 454/* filemap.c */
 455extern void remove_inode_page(struct page *);
 456extern unsigned long page_unuse(struct page *);
 457extern void truncate_inode_pages(struct address_space *, loff_t);
 458
 459/* generic vm_area_ops exported for stackable file systems */
 460extern int filemap_sync(struct vm_area_struct *, unsigned long, size_t, unsigned int);
 461extern struct page *filemap_nopage(struct vm_area_struct *, unsigned long, int);
 462
 463/* mm/page-writeback.c */
 464int write_one_page(struct page *page, int wait);
 465
 466/* readahead.c */
 467#define VM_MAX_READAHEAD        128     /* kbytes */
 468#define VM_MIN_READAHEAD        16      /* kbytes (includes current page) */
 469int do_page_cache_readahead(struct file *file,
 470                        unsigned long offset, unsigned long nr_to_read);
 471void page_cache_readahead(struct file *file, unsigned long offset);
 472void page_cache_readaround(struct file *file, unsigned long offset);
 473void handle_ra_miss(struct file *file);
 474
 475/* vma is the first one with  address < vma->vm_end,
 476 * and even  address < vma->vm_start. Have to extend vma. */
 477static inline int expand_stack(struct vm_area_struct * vma, unsigned long address)
 478{
 479        unsigned long grow;
 480
 481        /*
 482         * vma->vm_start/vm_end cannot change under us because the caller is required
 483         * to hold the mmap_sem in write mode. We need to get the spinlock only
 484         * before relocating the vma range ourself.
 485         */
 486        address &= PAGE_MASK;
 487        grow = (vma->vm_start - address) >> PAGE_SHIFT;
 488        if (vma->vm_end - address > current->rlim[RLIMIT_STACK].rlim_cur ||
 489            ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) > current->rlim[RLIMIT_AS].rlim_cur)
 490                return -ENOMEM;
 491        spin_lock(&vma->vm_mm->page_table_lock);
 492        vma->vm_start = address;
 493        vma->vm_pgoff -= grow;
 494        vma->vm_mm->total_vm += grow;
 495        if (vma->vm_flags & VM_LOCKED)
 496                vma->vm_mm->locked_vm += grow;
 497        spin_unlock(&vma->vm_mm->page_table_lock);
 498        return 0;
 499}
 500
 501/* Look up the first VMA which satisfies  addr < vm_end,  NULL if none. */
 502extern struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr);
 503extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr,
 504                                             struct vm_area_struct **pprev);
 505
 506/* Look up the first VMA which intersects the interval start_addr..end_addr-1,
 507   NULL if none.  Assume start_addr < end_addr. */
 508static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * mm, unsigned long start_addr, unsigned long end_addr)
 509{
 510        struct vm_area_struct * vma = find_vma(mm,start_addr);
 511
 512        if (vma && end_addr <= vma->vm_start)
 513                vma = NULL;
 514        return vma;
 515}
 516
 517extern struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr);
 518
 519extern struct page * vmalloc_to_page(void *addr);
 520extern unsigned long get_page_cache_size(void);
 521
 522#endif /* __KERNEL__ */
 523
 524#endif
 525
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.