[PATCH] mm: VM_BUG_ON
[pandora-kernel.git] / include / linux / mm.h
1 #ifndef _LINUX_MM_H
2 #define _LINUX_MM_H
3
4 #include <linux/sched.h>
5 #include <linux/errno.h>
6 #include <linux/capability.h>
7
8 #ifdef __KERNEL__
9
10 #include <linux/gfp.h>
11 #include <linux/list.h>
12 #include <linux/mmzone.h>
13 #include <linux/rbtree.h>
14 #include <linux/prio_tree.h>
15 #include <linux/fs.h>
16 #include <linux/mutex.h>
17 #include <linux/debug_locks.h>
18
19 struct mempolicy;
20 struct anon_vma;
21
22 #ifndef CONFIG_DISCONTIGMEM          /* Don't use mapnrs, do it properly */
23 extern unsigned long max_mapnr;
24 #endif
25
26 extern unsigned long num_physpages;
27 extern void * high_memory;
28 extern unsigned long vmalloc_earlyreserve;
29 extern int page_cluster;
30
31 #ifdef CONFIG_SYSCTL
32 extern int sysctl_legacy_va_layout;
33 #else
34 #define sysctl_legacy_va_layout 0
35 #endif
36
37 #include <asm/page.h>
38 #include <asm/pgtable.h>
39 #include <asm/processor.h>
40
41 #define nth_page(page,n) pfn_to_page(page_to_pfn((page)) + (n))
42
43 /*
44  * Linux kernel virtual memory manager primitives.
45  * The idea being to have a "virtual" mm in the same way
46  * we have a virtual fs - giving a cleaner interface to the
47  * mm details, and allowing different kinds of memory mappings
48  * (from shared memory to executable loading to arbitrary
49  * mmap() functions).
50  */
51
52 /*
53  * This struct defines a memory VMM memory area. There is one of these
54  * per VM-area/task.  A VM area is any part of the process virtual memory
55  * space that has a special rule for the page-fault handlers (ie a shared
56  * library, the executable area etc).
57  */
58 struct vm_area_struct {
59         struct mm_struct * vm_mm;       /* The address space we belong to. */
60         unsigned long vm_start;         /* Our start address within vm_mm. */
61         unsigned long vm_end;           /* The first byte after our end address
62                                            within vm_mm. */
63
64         /* linked list of VM areas per task, sorted by address */
65         struct vm_area_struct *vm_next;
66
67         pgprot_t vm_page_prot;          /* Access permissions of this VMA. */
68         unsigned long vm_flags;         /* Flags, listed below. */
69
70         struct rb_node vm_rb;
71
72         /*
73          * For areas with an address space and backing store,
74          * linkage into the address_space->i_mmap prio tree, or
75          * linkage to the list of like vmas hanging off its node, or
76          * linkage of vma in the address_space->i_mmap_nonlinear list.
77          */
78         union {
79                 struct {
80                         struct list_head list;
81                         void *parent;   /* aligns with prio_tree_node parent */
82                         struct vm_area_struct *head;
83                 } vm_set;
84
85                 struct raw_prio_tree_node prio_tree_node;
86         } shared;
87
88         /*
89          * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
90          * list, after a COW of one of the file pages.  A MAP_SHARED vma
91          * can only be in the i_mmap tree.  An anonymous MAP_PRIVATE, stack
92          * or brk vma (with NULL file) can only be in an anon_vma list.
93          */
94         struct list_head anon_vma_node; /* Serialized by anon_vma->lock */
95         struct anon_vma *anon_vma;      /* Serialized by page_table_lock */
96
97         /* Function pointers to deal with this struct. */
98         struct vm_operations_struct * vm_ops;
99
100         /* Information about our backing store: */
101         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
102                                            units, *not* PAGE_CACHE_SIZE */
103         struct file * vm_file;          /* File we map to (can be NULL). */
104         void * vm_private_data;         /* was vm_pte (shared mem) */
105         unsigned long vm_truncate_count;/* truncate_count or restart_addr */
106
107 #ifndef CONFIG_MMU
108         atomic_t vm_usage;              /* refcount (VMAs shared if !MMU) */
109 #endif
110 #ifdef CONFIG_NUMA
111         struct mempolicy *vm_policy;    /* NUMA policy for the VMA */
112 #endif
113 };
114
115 /*
116  * This struct defines the per-mm list of VMAs for uClinux. If CONFIG_MMU is
117  * disabled, then there's a single shared list of VMAs maintained by the
118  * system, and mm's subscribe to these individually
119  */
120 struct vm_list_struct {
121         struct vm_list_struct   *next;
122         struct vm_area_struct   *vma;
123 };
124
125 #ifndef CONFIG_MMU
126 extern struct rb_root nommu_vma_tree;
127 extern struct rw_semaphore nommu_vma_sem;
128
129 extern unsigned int kobjsize(const void *objp);
130 #endif
131
132 /*
133  * vm_flags..
134  */
135 #define VM_READ         0x00000001      /* currently active flags */
136 #define VM_WRITE        0x00000002
137 #define VM_EXEC         0x00000004
138 #define VM_SHARED       0x00000008
139
140 /* mprotect() hardcodes VM_MAYREAD >> 4 == VM_READ, and so for r/w/x bits. */
141 #define VM_MAYREAD      0x00000010      /* limits for mprotect() etc */
142 #define VM_MAYWRITE     0x00000020
143 #define VM_MAYEXEC      0x00000040
144 #define VM_MAYSHARE     0x00000080
145
146 #define VM_GROWSDOWN    0x00000100      /* general info on the segment */
147 #define VM_GROWSUP      0x00000200
148 #define VM_PFNMAP       0x00000400      /* Page-ranges managed without "struct page", just pure PFN */
149 #define VM_DENYWRITE    0x00000800      /* ETXTBSY on write attempts.. */
150
151 #define VM_EXECUTABLE   0x00001000
152 #define VM_LOCKED       0x00002000
153 #define VM_IO           0x00004000      /* Memory mapped I/O or similar */
154
155                                         /* Used by sys_madvise() */
156 #define VM_SEQ_READ     0x00008000      /* App will access data sequentially */
157 #define VM_RAND_READ    0x00010000      /* App will not benefit from clustered reads */
158
159 #define VM_DONTCOPY     0x00020000      /* Do not copy this vma on fork */
160 #define VM_DONTEXPAND   0x00040000      /* Cannot expand with mremap() */
161 #define VM_RESERVED     0x00080000      /* Count as reserved_vm like IO */
162 #define VM_ACCOUNT      0x00100000      /* Is a VM accounted object */
163 #define VM_HUGETLB      0x00400000      /* Huge TLB Page VM */
164 #define VM_NONLINEAR    0x00800000      /* Is non-linear (remap_file_pages) */
165 #define VM_MAPPED_COPY  0x01000000      /* T if mapped copy of data (nommu mmap) */
166 #define VM_INSERTPAGE   0x02000000      /* The vma has had "vm_insert_page()" done on it */
167
168 #ifndef VM_STACK_DEFAULT_FLAGS          /* arch can override this */
169 #define VM_STACK_DEFAULT_FLAGS VM_DATA_DEFAULT_FLAGS
170 #endif
171
172 #ifdef CONFIG_STACK_GROWSUP
173 #define VM_STACK_FLAGS  (VM_GROWSUP | VM_STACK_DEFAULT_FLAGS | VM_ACCOUNT)
174 #else
175 #define VM_STACK_FLAGS  (VM_GROWSDOWN | VM_STACK_DEFAULT_FLAGS | VM_ACCOUNT)
176 #endif
177
178 #define VM_READHINTMASK                 (VM_SEQ_READ | VM_RAND_READ)
179 #define VM_ClearReadHint(v)             (v)->vm_flags &= ~VM_READHINTMASK
180 #define VM_NormalReadHint(v)            (!((v)->vm_flags & VM_READHINTMASK))
181 #define VM_SequentialReadHint(v)        ((v)->vm_flags & VM_SEQ_READ)
182 #define VM_RandomReadHint(v)            ((v)->vm_flags & VM_RAND_READ)
183
184 /*
185  * mapping from the currently active vm_flags protection bits (the
186  * low four bits) to a page protection mask..
187  */
188 extern pgprot_t protection_map[16];
189
190
191 /*
192  * These are the virtual MM functions - opening of an area, closing and
193  * unmapping it (needed to keep files on disk up-to-date etc), pointer
194  * to the functions called when a no-page or a wp-page exception occurs. 
195  */
196 struct vm_operations_struct {
197         void (*open)(struct vm_area_struct * area);
198         void (*close)(struct vm_area_struct * area);
199         struct page * (*nopage)(struct vm_area_struct * area, unsigned long address, int *type);
200         int (*populate)(struct vm_area_struct * area, unsigned long address, unsigned long len, pgprot_t prot, unsigned long pgoff, int nonblock);
201
202         /* notification that a previously read-only page is about to become
203          * writable, if an error is returned it will cause a SIGBUS */
204         int (*page_mkwrite)(struct vm_area_struct *vma, struct page *page);
205 #ifdef CONFIG_NUMA
206         int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new);
207         struct mempolicy *(*get_policy)(struct vm_area_struct *vma,
208                                         unsigned long addr);
209         int (*migrate)(struct vm_area_struct *vma, const nodemask_t *from,
210                 const nodemask_t *to, unsigned long flags);
211 #endif
212 };
213
214 struct mmu_gather;
215 struct inode;
216
217 /*
218  * Each physical page in the system has a struct page associated with
219  * it to keep track of whatever it is we are using the page for at the
220  * moment. Note that we have no way to track which tasks are using
221  * a page.
222  */
223 struct page {
224         unsigned long flags;            /* Atomic flags, some possibly
225                                          * updated asynchronously */
226         atomic_t _count;                /* Usage count, see below. */
227         atomic_t _mapcount;             /* Count of ptes mapped in mms,
228                                          * to show when page is mapped
229                                          * & limit reverse map searches.
230                                          */
231         union {
232             struct {
233                 unsigned long private;          /* Mapping-private opaque data:
234                                                  * usually used for buffer_heads
235                                                  * if PagePrivate set; used for
236                                                  * swp_entry_t if PageSwapCache;
237                                                  * indicates order in the buddy
238                                                  * system if PG_buddy is set.
239                                                  */
240                 struct address_space *mapping;  /* If low bit clear, points to
241                                                  * inode address_space, or NULL.
242                                                  * If page mapped as anonymous
243                                                  * memory, low bit is set, and
244                                                  * it points to anon_vma object:
245                                                  * see PAGE_MAPPING_ANON below.
246                                                  */
247             };
248 #if NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS
249             spinlock_t ptl;
250 #endif
251         };
252         pgoff_t index;                  /* Our offset within mapping. */
253         struct list_head lru;           /* Pageout list, eg. active_list
254                                          * protected by zone->lru_lock !
255                                          */
256         /*
257          * On machines where all RAM is mapped into kernel address space,
258          * we can simply calculate the virtual address. On machines with
259          * highmem some memory is mapped into kernel virtual memory
260          * dynamically, so we need a place to store that address.
261          * Note that this field could be 16 bits on x86 ... ;)
262          *
263          * Architectures with slow multiplication can define
264          * WANT_PAGE_VIRTUAL in asm/page.h
265          */
266 #if defined(WANT_PAGE_VIRTUAL)
267         void *virtual;                  /* Kernel virtual address (NULL if
268                                            not kmapped, ie. highmem) */
269 #endif /* WANT_PAGE_VIRTUAL */
270 };
271
272 #define page_private(page)              ((page)->private)
273 #define set_page_private(page, v)       ((page)->private = (v))
274
275 /*
276  * FIXME: take this include out, include page-flags.h in
277  * files which need it (119 of them)
278  */
279 #include <linux/page-flags.h>
280
281 #ifdef CONFIG_DEBUG_VM
282 #define VM_BUG_ON(cond) BUG_ON(cond)
283 #else
284 #define VM_BUG_ON(condition) do { } while(0)
285 #endif
286
287 /*
288  * Methods to modify the page usage count.
289  *
290  * What counts for a page usage:
291  * - cache mapping   (page->mapping)
292  * - private data    (page->private)
293  * - page mapped in a task's page tables, each mapping
294  *   is counted separately
295  *
296  * Also, many kernel routines increase the page count before a critical
297  * routine so they can be sure the page doesn't go away from under them.
298  */
299
300 /*
301  * Drop a ref, return true if the logical refcount fell to zero (the page has
302  * no users)
303  */
304 static inline int put_page_testzero(struct page *page)
305 {
306         VM_BUG_ON(atomic_read(&page->_count) == 0);
307         return atomic_dec_and_test(&page->_count);
308 }
309
310 /*
311  * Try to grab a ref unless the page has a refcount of zero, return false if
312  * that is the case.
313  */
314 static inline int get_page_unless_zero(struct page *page)
315 {
316         VM_BUG_ON(PageCompound(page));
317         return atomic_inc_not_zero(&page->_count);
318 }
319
320 extern void FASTCALL(__page_cache_release(struct page *));
321
322 static inline int page_count(struct page *page)
323 {
324         if (unlikely(PageCompound(page)))
325                 page = (struct page *)page_private(page);
326         return atomic_read(&page->_count);
327 }
328
329 static inline void get_page(struct page *page)
330 {
331         if (unlikely(PageCompound(page)))
332                 page = (struct page *)page_private(page);
333         VM_BUG_ON(atomic_read(&page->_count) == 0);
334         atomic_inc(&page->_count);
335 }
336
337 /*
338  * Setup the page count before being freed into the page allocator for
339  * the first time (boot or memory hotplug)
340  */
341 static inline void init_page_count(struct page *page)
342 {
343         atomic_set(&page->_count, 1);
344 }
345
346 void put_page(struct page *page);
347 void put_pages_list(struct list_head *pages);
348
349 void split_page(struct page *page, unsigned int order);
350
351 /*
352  * Multiple processes may "see" the same page. E.g. for untouched
353  * mappings of /dev/null, all processes see the same page full of
354  * zeroes, and text pages of executables and shared libraries have
355  * only one copy in memory, at most, normally.
356  *
357  * For the non-reserved pages, page_count(page) denotes a reference count.
358  *   page_count() == 0 means the page is free. page->lru is then used for
359  *   freelist management in the buddy allocator.
360  *   page_count() == 1 means the page is used for exactly one purpose
361  *   (e.g. a private data page of one process).
362  *
363  * A page may be used for kmalloc() or anyone else who does a
364  * __get_free_page(). In this case the page_count() is at least 1, and
365  * all other fields are unused but should be 0 or NULL. The
366  * management of this page is the responsibility of the one who uses
367  * it.
368  *
369  * The other pages (we may call them "process pages") are completely
370  * managed by the Linux memory manager: I/O, buffers, swapping etc.
371  * The following discussion applies only to them.
372  *
373  * A page may belong to an inode's memory mapping. In this case,
374  * page->mapping is the pointer to the inode, and page->index is the
375  * file offset of the page, in units of PAGE_CACHE_SIZE.
376  *
377  * A page contains an opaque `private' member, which belongs to the
378  * page's address_space.  Usually, this is the address of a circular
379  * list of the page's disk buffers.
380  *
381  * For pages belonging to inodes, the page_count() is the number of
382  * attaches, plus 1 if `private' contains something, plus one for
383  * the page cache itself.
384  *
385  * Instead of keeping dirty/clean pages in per address-space lists, we instead
386  * now tag pages as dirty/under writeback in the radix tree.
387  *
388  * There is also a per-mapping radix tree mapping index to the page
389  * in memory if present. The tree is rooted at mapping->root.  
390  *
391  * All process pages can do I/O:
392  * - inode pages may need to be read from disk,
393  * - inode pages which have been modified and are MAP_SHARED may need
394  *   to be written to disk,
395  * - private pages which have been modified may need to be swapped out
396  *   to swap space and (later) to be read back into memory.
397  */
398
399 /*
400  * The zone field is never updated after free_area_init_core()
401  * sets it, so none of the operations on it need to be atomic.
402  */
403
404
405 /*
406  * page->flags layout:
407  *
408  * There are three possibilities for how page->flags get
409  * laid out.  The first is for the normal case, without
410  * sparsemem.  The second is for sparsemem when there is
411  * plenty of space for node and section.  The last is when
412  * we have run out of space and have to fall back to an
413  * alternate (slower) way of determining the node.
414  *
415  *        No sparsemem: |       NODE     | ZONE | ... | FLAGS |
416  * with space for node: | SECTION | NODE | ZONE | ... | FLAGS |
417  *   no space for node: | SECTION |     ZONE    | ... | FLAGS |
418  */
419 #ifdef CONFIG_SPARSEMEM
420 #define SECTIONS_WIDTH          SECTIONS_SHIFT
421 #else
422 #define SECTIONS_WIDTH          0
423 #endif
424
425 #define ZONES_WIDTH             ZONES_SHIFT
426
427 #if SECTIONS_WIDTH+ZONES_WIDTH+NODES_SHIFT <= FLAGS_RESERVED
428 #define NODES_WIDTH             NODES_SHIFT
429 #else
430 #define NODES_WIDTH             0
431 #endif
432
433 /* Page flags: | [SECTION] | [NODE] | ZONE | ... | FLAGS | */
434 #define SECTIONS_PGOFF          ((sizeof(unsigned long)*8) - SECTIONS_WIDTH)
435 #define NODES_PGOFF             (SECTIONS_PGOFF - NODES_WIDTH)
436 #define ZONES_PGOFF             (NODES_PGOFF - ZONES_WIDTH)
437
438 /*
439  * We are going to use the flags for the page to node mapping if its in
440  * there.  This includes the case where there is no node, so it is implicit.
441  */
442 #define FLAGS_HAS_NODE          (NODES_WIDTH > 0 || NODES_SHIFT == 0)
443
444 #ifndef PFN_SECTION_SHIFT
445 #define PFN_SECTION_SHIFT 0
446 #endif
447
448 /*
449  * Define the bit shifts to access each section.  For non-existant
450  * sections we define the shift as 0; that plus a 0 mask ensures
451  * the compiler will optimise away reference to them.
452  */
453 #define SECTIONS_PGSHIFT        (SECTIONS_PGOFF * (SECTIONS_WIDTH != 0))
454 #define NODES_PGSHIFT           (NODES_PGOFF * (NODES_WIDTH != 0))
455 #define ZONES_PGSHIFT           (ZONES_PGOFF * (ZONES_WIDTH != 0))
456
457 /* NODE:ZONE or SECTION:ZONE is used to lookup the zone from a page. */
458 #if FLAGS_HAS_NODE
459 #define ZONETABLE_SHIFT         (NODES_SHIFT + ZONES_SHIFT)
460 #else
461 #define ZONETABLE_SHIFT         (SECTIONS_SHIFT + ZONES_SHIFT)
462 #endif
463 #define ZONETABLE_PGSHIFT       ZONES_PGSHIFT
464
465 #if SECTIONS_WIDTH+NODES_WIDTH+ZONES_WIDTH > FLAGS_RESERVED
466 #error SECTIONS_WIDTH+NODES_WIDTH+ZONES_WIDTH > FLAGS_RESERVED
467 #endif
468
469 #define ZONES_MASK              ((1UL << ZONES_WIDTH) - 1)
470 #define NODES_MASK              ((1UL << NODES_WIDTH) - 1)
471 #define SECTIONS_MASK           ((1UL << SECTIONS_WIDTH) - 1)
472 #define ZONETABLE_MASK          ((1UL << ZONETABLE_SHIFT) - 1)
473
474 static inline unsigned long page_zonenum(struct page *page)
475 {
476         return (page->flags >> ZONES_PGSHIFT) & ZONES_MASK;
477 }
478
479 struct zone;
480 extern struct zone *zone_table[];
481
482 static inline int page_zone_id(struct page *page)
483 {
484         return (page->flags >> ZONETABLE_PGSHIFT) & ZONETABLE_MASK;
485 }
486 static inline struct zone *page_zone(struct page *page)
487 {
488         return zone_table[page_zone_id(page)];
489 }
490
491 static inline unsigned long page_to_nid(struct page *page)
492 {
493         if (FLAGS_HAS_NODE)
494                 return (page->flags >> NODES_PGSHIFT) & NODES_MASK;
495         else
496                 return page_zone(page)->zone_pgdat->node_id;
497 }
498 static inline unsigned long page_to_section(struct page *page)
499 {
500         return (page->flags >> SECTIONS_PGSHIFT) & SECTIONS_MASK;
501 }
502
503 static inline void set_page_zone(struct page *page, unsigned long zone)
504 {
505         page->flags &= ~(ZONES_MASK << ZONES_PGSHIFT);
506         page->flags |= (zone & ZONES_MASK) << ZONES_PGSHIFT;
507 }
508 static inline void set_page_node(struct page *page, unsigned long node)
509 {
510         page->flags &= ~(NODES_MASK << NODES_PGSHIFT);
511         page->flags |= (node & NODES_MASK) << NODES_PGSHIFT;
512 }
513 static inline void set_page_section(struct page *page, unsigned long section)
514 {
515         page->flags &= ~(SECTIONS_MASK << SECTIONS_PGSHIFT);
516         page->flags |= (section & SECTIONS_MASK) << SECTIONS_PGSHIFT;
517 }
518
519 static inline void set_page_links(struct page *page, unsigned long zone,
520         unsigned long node, unsigned long pfn)
521 {
522         set_page_zone(page, zone);
523         set_page_node(page, node);
524         set_page_section(page, pfn_to_section_nr(pfn));
525 }
526
527 /*
528  * Some inline functions in vmstat.h depend on page_zone()
529  */
530 #include <linux/vmstat.h>
531
532 #ifndef CONFIG_DISCONTIGMEM
533 /* The array of struct pages - for discontigmem use pgdat->lmem_map */
534 extern struct page *mem_map;
535 #endif
536
537 static __always_inline void *lowmem_page_address(struct page *page)
538 {
539         return __va(page_to_pfn(page) << PAGE_SHIFT);
540 }
541
542 #if defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL)
543 #define HASHED_PAGE_VIRTUAL
544 #endif
545
546 #if defined(WANT_PAGE_VIRTUAL)
547 #define page_address(page) ((page)->virtual)
548 #define set_page_address(page, address)                 \
549         do {                                            \
550                 (page)->virtual = (address);            \
551         } while(0)
552 #define page_address_init()  do { } while(0)
553 #endif
554
555 #if defined(HASHED_PAGE_VIRTUAL)
556 void *page_address(struct page *page);
557 void set_page_address(struct page *page, void *virtual);
558 void page_address_init(void);
559 #endif
560
561 #if !defined(HASHED_PAGE_VIRTUAL) && !defined(WANT_PAGE_VIRTUAL)
562 #define page_address(page) lowmem_page_address(page)
563 #define set_page_address(page, address)  do { } while(0)
564 #define page_address_init()  do { } while(0)
565 #endif
566
567 /*
568  * On an anonymous page mapped into a user virtual memory area,
569  * page->mapping points to its anon_vma, not to a struct address_space;
570  * with the PAGE_MAPPING_ANON bit set to distinguish it.
571  *
572  * Please note that, confusingly, "page_mapping" refers to the inode
573  * address_space which maps the page from disk; whereas "page_mapped"
574  * refers to user virtual address space into which the page is mapped.
575  */
576 #define PAGE_MAPPING_ANON       1
577
578 extern struct address_space swapper_space;
579 static inline struct address_space *page_mapping(struct page *page)
580 {
581         struct address_space *mapping = page->mapping;
582
583         if (unlikely(PageSwapCache(page)))
584                 mapping = &swapper_space;
585         else if (unlikely((unsigned long)mapping & PAGE_MAPPING_ANON))
586                 mapping = NULL;
587         return mapping;
588 }
589
590 static inline int PageAnon(struct page *page)
591 {
592         return ((unsigned long)page->mapping & PAGE_MAPPING_ANON) != 0;
593 }
594
595 /*
596  * Return the pagecache index of the passed page.  Regular pagecache pages
597  * use ->index whereas swapcache pages use ->private
598  */
599 static inline pgoff_t page_index(struct page *page)
600 {
601         if (unlikely(PageSwapCache(page)))
602                 return page_private(page);
603         return page->index;
604 }
605
606 /*
607  * The atomic page->_mapcount, like _count, starts from -1:
608  * so that transitions both from it and to it can be tracked,
609  * using atomic_inc_and_test and atomic_add_negative(-1).
610  */
611 static inline void reset_page_mapcount(struct page *page)
612 {
613         atomic_set(&(page)->_mapcount, -1);
614 }
615
616 static inline int page_mapcount(struct page *page)
617 {
618         return atomic_read(&(page)->_mapcount) + 1;
619 }
620
621 /*
622  * Return true if this page is mapped into pagetables.
623  */
624 static inline int page_mapped(struct page *page)
625 {
626         return atomic_read(&(page)->_mapcount) >= 0;
627 }
628
629 /*
630  * Error return values for the *_nopage functions
631  */
632 #define NOPAGE_SIGBUS   (NULL)
633 #define NOPAGE_OOM      ((struct page *) (-1))
634
635 /*
636  * Different kinds of faults, as returned by handle_mm_fault().
637  * Used to decide whether a process gets delivered SIGBUS or
638  * just gets major/minor fault counters bumped up.
639  */
640 #define VM_FAULT_OOM    0x00
641 #define VM_FAULT_SIGBUS 0x01
642 #define VM_FAULT_MINOR  0x02
643 #define VM_FAULT_MAJOR  0x03
644
645 /* 
646  * Special case for get_user_pages.
647  * Must be in a distinct bit from the above VM_FAULT_ flags.
648  */
649 #define VM_FAULT_WRITE  0x10
650
651 #define offset_in_page(p)       ((unsigned long)(p) & ~PAGE_MASK)
652
653 extern void show_free_areas(void);
654
655 #ifdef CONFIG_SHMEM
656 struct page *shmem_nopage(struct vm_area_struct *vma,
657                         unsigned long address, int *type);
658 int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new);
659 struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
660                                         unsigned long addr);
661 int shmem_lock(struct file *file, int lock, struct user_struct *user);
662 #else
663 #define shmem_nopage filemap_nopage
664
665 static inline int shmem_lock(struct file *file, int lock,
666                              struct user_struct *user)
667 {
668         return 0;
669 }
670
671 static inline int shmem_set_policy(struct vm_area_struct *vma,
672                                    struct mempolicy *new)
673 {
674         return 0;
675 }
676
677 static inline struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
678                                                  unsigned long addr)
679 {
680         return NULL;
681 }
682 #endif
683 struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags);
684 extern int shmem_mmap(struct file *file, struct vm_area_struct *vma);
685
686 int shmem_zero_setup(struct vm_area_struct *);
687
688 #ifndef CONFIG_MMU
689 extern unsigned long shmem_get_unmapped_area(struct file *file,
690                                              unsigned long addr,
691                                              unsigned long len,
692                                              unsigned long pgoff,
693                                              unsigned long flags);
694 #endif
695
696 static inline int can_do_mlock(void)
697 {
698         if (capable(CAP_IPC_LOCK))
699                 return 1;
700         if (current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur != 0)
701                 return 1;
702         return 0;
703 }
704 extern int user_shm_lock(size_t, struct user_struct *);
705 extern void user_shm_unlock(size_t, struct user_struct *);
706
707 /*
708  * Parameter block passed down to zap_pte_range in exceptional cases.
709  */
710 struct zap_details {
711         struct vm_area_struct *nonlinear_vma;   /* Check page->index if set */
712         struct address_space *check_mapping;    /* Check page->mapping if set */
713         pgoff_t first_index;                    /* Lowest page->index to unmap */
714         pgoff_t last_index;                     /* Highest page->index to unmap */
715         spinlock_t *i_mmap_lock;                /* For unmap_mapping_range: */
716         unsigned long truncate_count;           /* Compare vm_truncate_count */
717 };
718
719 struct page *vm_normal_page(struct vm_area_struct *, unsigned long, pte_t);
720 unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address,
721                 unsigned long size, struct zap_details *);
722 unsigned long unmap_vmas(struct mmu_gather **tlb,
723                 struct vm_area_struct *start_vma, unsigned long start_addr,
724                 unsigned long end_addr, unsigned long *nr_accounted,
725                 struct zap_details *);
726 void free_pgd_range(struct mmu_gather **tlb, unsigned long addr,
727                 unsigned long end, unsigned long floor, unsigned long ceiling);
728 void free_pgtables(struct mmu_gather **tlb, struct vm_area_struct *start_vma,
729                 unsigned long floor, unsigned long ceiling);
730 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
731                         struct vm_area_struct *vma);
732 int zeromap_page_range(struct vm_area_struct *vma, unsigned long from,
733                         unsigned long size, pgprot_t prot);
734 void unmap_mapping_range(struct address_space *mapping,
735                 loff_t const holebegin, loff_t const holelen, int even_cows);
736
737 static inline void unmap_shared_mapping_range(struct address_space *mapping,
738                 loff_t const holebegin, loff_t const holelen)
739 {
740         unmap_mapping_range(mapping, holebegin, holelen, 0);
741 }
742
743 extern int vmtruncate(struct inode * inode, loff_t offset);
744 extern int vmtruncate_range(struct inode * inode, loff_t offset, loff_t end);
745 extern int install_page(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, struct page *page, pgprot_t prot);
746 extern int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, unsigned long pgoff, pgprot_t prot);
747
748 #ifdef CONFIG_MMU
749 extern int __handle_mm_fault(struct mm_struct *mm,struct vm_area_struct *vma,
750                         unsigned long address, int write_access);
751
752 static inline int handle_mm_fault(struct mm_struct *mm,
753                         struct vm_area_struct *vma, unsigned long address,
754                         int write_access)
755 {
756         return __handle_mm_fault(mm, vma, address, write_access) &
757                                 (~VM_FAULT_WRITE);
758 }
759 #else
760 static inline int handle_mm_fault(struct mm_struct *mm,
761                         struct vm_area_struct *vma, unsigned long address,
762                         int write_access)
763 {
764         /* should never happen if there's no MMU */
765         BUG();
766         return VM_FAULT_SIGBUS;
767 }
768 #endif
769
770 extern int make_pages_present(unsigned long addr, unsigned long end);
771 extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
772 void install_arg_page(struct vm_area_struct *, struct page *, unsigned long);
773
774 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, unsigned long start,
775                 int len, int write, int force, struct page **pages, struct vm_area_struct **vmas);
776 void print_bad_pte(struct vm_area_struct *, pte_t, unsigned long);
777
778 int __set_page_dirty_buffers(struct page *page);
779 int __set_page_dirty_nobuffers(struct page *page);
780 int redirty_page_for_writepage(struct writeback_control *wbc,
781                                 struct page *page);
782 int FASTCALL(set_page_dirty(struct page *page));
783 int set_page_dirty_lock(struct page *page);
784 int clear_page_dirty_for_io(struct page *page);
785
786 extern unsigned long do_mremap(unsigned long addr,
787                                unsigned long old_len, unsigned long new_len,
788                                unsigned long flags, unsigned long new_addr);
789
790 /*
791  * Prototype to add a shrinker callback for ageable caches.
792  * 
793  * These functions are passed a count `nr_to_scan' and a gfpmask.  They should
794  * scan `nr_to_scan' objects, attempting to free them.
795  *
796  * The callback must return the number of objects which remain in the cache.
797  *
798  * The callback will be passed nr_to_scan == 0 when the VM is querying the
799  * cache size, so a fastpath for that case is appropriate.
800  */
801 typedef int (*shrinker_t)(int nr_to_scan, gfp_t gfp_mask);
802
803 /*
804  * Add an aging callback.  The int is the number of 'seeks' it takes
805  * to recreate one of the objects that these functions age.
806  */
807
808 #define DEFAULT_SEEKS 2
809 struct shrinker;
810 extern struct shrinker *set_shrinker(int, shrinker_t);
811 extern void remove_shrinker(struct shrinker *shrinker);
812
813 extern pte_t *FASTCALL(get_locked_pte(struct mm_struct *mm, unsigned long addr, spinlock_t **ptl));
814
815 int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address);
816 int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address);
817 int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address);
818 int __pte_alloc_kernel(pmd_t *pmd, unsigned long address);
819
820 /*
821  * The following ifdef needed to get the 4level-fixup.h header to work.
822  * Remove it when 4level-fixup.h has been removed.
823  */
824 #if defined(CONFIG_MMU) && !defined(__ARCH_HAS_4LEVEL_HACK)
825 static inline pud_t *pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
826 {
827         return (unlikely(pgd_none(*pgd)) && __pud_alloc(mm, pgd, address))?
828                 NULL: pud_offset(pgd, address);
829 }
830
831 static inline pmd_t *pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
832 {
833         return (unlikely(pud_none(*pud)) && __pmd_alloc(mm, pud, address))?
834                 NULL: pmd_offset(pud, address);
835 }
836 #endif /* CONFIG_MMU && !__ARCH_HAS_4LEVEL_HACK */
837
838 #if NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS
839 /*
840  * We tuck a spinlock to guard each pagetable page into its struct page,
841  * at page->private, with BUILD_BUG_ON to make sure that this will not
842  * overflow into the next struct page (as it might with DEBUG_SPINLOCK).
843  * When freeing, reset page->mapping so free_pages_check won't complain.
844  */
845 #define __pte_lockptr(page)     &((page)->ptl)
846 #define pte_lock_init(_page)    do {                                    \
847         spin_lock_init(__pte_lockptr(_page));                           \
848 } while (0)
849 #define pte_lock_deinit(page)   ((page)->mapping = NULL)
850 #define pte_lockptr(mm, pmd)    ({(void)(mm); __pte_lockptr(pmd_page(*(pmd)));})
851 #else
852 /*
853  * We use mm->page_table_lock to guard all pagetable pages of the mm.
854  */
855 #define pte_lock_init(page)     do {} while (0)
856 #define pte_lock_deinit(page)   do {} while (0)
857 #define pte_lockptr(mm, pmd)    ({(void)(pmd); &(mm)->page_table_lock;})
858 #endif /* NR_CPUS < CONFIG_SPLIT_PTLOCK_CPUS */
859
860 #define pte_offset_map_lock(mm, pmd, address, ptlp)     \
861 ({                                                      \
862         spinlock_t *__ptl = pte_lockptr(mm, pmd);       \
863         pte_t *__pte = pte_offset_map(pmd, address);    \
864         *(ptlp) = __ptl;                                \
865         spin_lock(__ptl);                               \
866         __pte;                                          \
867 })
868
869 #define pte_unmap_unlock(pte, ptl)      do {            \
870         spin_unlock(ptl);                               \
871         pte_unmap(pte);                                 \
872 } while (0)
873
874 #define pte_alloc_map(mm, pmd, address)                 \
875         ((unlikely(!pmd_present(*(pmd))) && __pte_alloc(mm, pmd, address))? \
876                 NULL: pte_offset_map(pmd, address))
877
878 #define pte_alloc_map_lock(mm, pmd, address, ptlp)      \
879         ((unlikely(!pmd_present(*(pmd))) && __pte_alloc(mm, pmd, address))? \
880                 NULL: pte_offset_map_lock(mm, pmd, address, ptlp))
881
882 #define pte_alloc_kernel(pmd, address)                  \
883         ((unlikely(!pmd_present(*(pmd))) && __pte_alloc_kernel(pmd, address))? \
884                 NULL: pte_offset_kernel(pmd, address))
885
886 extern void free_area_init(unsigned long * zones_size);
887 extern void free_area_init_node(int nid, pg_data_t *pgdat,
888         unsigned long * zones_size, unsigned long zone_start_pfn, 
889         unsigned long *zholes_size);
890 extern void memmap_init_zone(unsigned long, int, unsigned long, unsigned long);
891 extern void setup_per_zone_pages_min(void);
892 extern void mem_init(void);
893 extern void show_mem(void);
894 extern void si_meminfo(struct sysinfo * val);
895 extern void si_meminfo_node(struct sysinfo *val, int nid);
896
897 #ifdef CONFIG_NUMA
898 extern void setup_per_cpu_pageset(void);
899 #else
900 static inline void setup_per_cpu_pageset(void) {}
901 #endif
902
903 /* prio_tree.c */
904 void vma_prio_tree_add(struct vm_area_struct *, struct vm_area_struct *old);
905 void vma_prio_tree_insert(struct vm_area_struct *, struct prio_tree_root *);
906 void vma_prio_tree_remove(struct vm_area_struct *, struct prio_tree_root *);
907 struct vm_area_struct *vma_prio_tree_next(struct vm_area_struct *vma,
908         struct prio_tree_iter *iter);
909
910 #define vma_prio_tree_foreach(vma, iter, root, begin, end)      \
911         for (prio_tree_iter_init(iter, root, begin, end), vma = NULL;   \
912                 (vma = vma_prio_tree_next(vma, iter)); )
913
914 static inline void vma_nonlinear_insert(struct vm_area_struct *vma,
915                                         struct list_head *list)
916 {
917         vma->shared.vm_set.parent = NULL;
918         list_add_tail(&vma->shared.vm_set.list, list);
919 }
920
921 /* mmap.c */
922 extern int __vm_enough_memory(long pages, int cap_sys_admin);
923 extern void vma_adjust(struct vm_area_struct *vma, unsigned long start,
924         unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert);
925 extern struct vm_area_struct *vma_merge(struct mm_struct *,
926         struct vm_area_struct *prev, unsigned long addr, unsigned long end,
927         unsigned long vm_flags, struct anon_vma *, struct file *, pgoff_t,
928         struct mempolicy *);
929 extern struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *);
930 extern int split_vma(struct mm_struct *,
931         struct vm_area_struct *, unsigned long addr, int new_below);
932 extern int insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
933 extern void __vma_link_rb(struct mm_struct *, struct vm_area_struct *,
934         struct rb_node **, struct rb_node *);
935 extern void unlink_file_vma(struct vm_area_struct *);
936 extern struct vm_area_struct *copy_vma(struct vm_area_struct **,
937         unsigned long addr, unsigned long len, pgoff_t pgoff);
938 extern void exit_mmap(struct mm_struct *);
939 extern int may_expand_vm(struct mm_struct *mm, unsigned long npages);
940
941 extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
942
943 extern unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
944         unsigned long len, unsigned long prot,
945         unsigned long flag, unsigned long pgoff);
946
947 static inline unsigned long do_mmap(struct file *file, unsigned long addr,
948         unsigned long len, unsigned long prot,
949         unsigned long flag, unsigned long offset)
950 {
951         unsigned long ret = -EINVAL;
952         if ((offset + PAGE_ALIGN(len)) < offset)
953                 goto out;
954         if (!(offset & ~PAGE_MASK))
955                 ret = do_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
956 out:
957         return ret;
958 }
959
960 extern int do_munmap(struct mm_struct *, unsigned long, size_t);
961
962 extern unsigned long do_brk(unsigned long, unsigned long);
963
964 /* filemap.c */
965 extern unsigned long page_unuse(struct page *);
966 extern void truncate_inode_pages(struct address_space *, loff_t);
967 extern void truncate_inode_pages_range(struct address_space *,
968                                        loff_t lstart, loff_t lend);
969
970 /* generic vm_area_ops exported for stackable file systems */
971 extern struct page *filemap_nopage(struct vm_area_struct *, unsigned long, int *);
972 extern int filemap_populate(struct vm_area_struct *, unsigned long,
973                 unsigned long, pgprot_t, unsigned long, int);
974
975 /* mm/page-writeback.c */
976 int write_one_page(struct page *page, int wait);
977
978 /* readahead.c */
979 #define VM_MAX_READAHEAD        128     /* kbytes */
980 #define VM_MIN_READAHEAD        16      /* kbytes (includes current page) */
981 #define VM_MAX_CACHE_HIT        256     /* max pages in a row in cache before
982                                          * turning readahead off */
983
984 int do_page_cache_readahead(struct address_space *mapping, struct file *filp,
985                         pgoff_t offset, unsigned long nr_to_read);
986 int force_page_cache_readahead(struct address_space *mapping, struct file *filp,
987                         pgoff_t offset, unsigned long nr_to_read);
988 unsigned long page_cache_readahead(struct address_space *mapping,
989                           struct file_ra_state *ra,
990                           struct file *filp,
991                           pgoff_t offset,
992                           unsigned long size);
993 void handle_ra_miss(struct address_space *mapping, 
994                     struct file_ra_state *ra, pgoff_t offset);
995 unsigned long max_sane_readahead(unsigned long nr);
996
997 /* Do stack extension */
998 extern int expand_stack(struct vm_area_struct *vma, unsigned long address);
999 #ifdef CONFIG_IA64
1000 extern int expand_upwards(struct vm_area_struct *vma, unsigned long address);
1001 #endif
1002
1003 /* Look up the first VMA which satisfies  addr < vm_end,  NULL if none. */
1004 extern struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr);
1005 extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr,
1006                                              struct vm_area_struct **pprev);
1007
1008 /* Look up the first VMA which intersects the interval start_addr..end_addr-1,
1009    NULL if none.  Assume start_addr < end_addr. */
1010 static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * mm, unsigned long start_addr, unsigned long end_addr)
1011 {
1012         struct vm_area_struct * vma = find_vma(mm,start_addr);
1013
1014         if (vma && end_addr <= vma->vm_start)
1015                 vma = NULL;
1016         return vma;
1017 }
1018
1019 static inline unsigned long vma_pages(struct vm_area_struct *vma)
1020 {
1021         return (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
1022 }
1023
1024 pgprot_t vm_get_page_prot(unsigned long vm_flags);
1025 struct vm_area_struct *find_extend_vma(struct mm_struct *, unsigned long addr);
1026 struct page *vmalloc_to_page(void *addr);
1027 unsigned long vmalloc_to_pfn(void *addr);
1028 int remap_pfn_range(struct vm_area_struct *, unsigned long addr,
1029                         unsigned long pfn, unsigned long size, pgprot_t);
1030 int vm_insert_page(struct vm_area_struct *, unsigned long addr, struct page *);
1031
1032 struct page *follow_page(struct vm_area_struct *, unsigned long address,
1033                         unsigned int foll_flags);
1034 #define FOLL_WRITE      0x01    /* check pte is writable */
1035 #define FOLL_TOUCH      0x02    /* mark page accessed */
1036 #define FOLL_GET        0x04    /* do get_page on page */
1037 #define FOLL_ANON       0x08    /* give ZERO_PAGE if no pgtable */
1038
1039 #ifdef CONFIG_PROC_FS
1040 void vm_stat_account(struct mm_struct *, unsigned long, struct file *, long);
1041 #else
1042 static inline void vm_stat_account(struct mm_struct *mm,
1043                         unsigned long flags, struct file *file, long pages)
1044 {
1045 }
1046 #endif /* CONFIG_PROC_FS */
1047
1048 #ifndef CONFIG_DEBUG_PAGEALLOC
1049 static inline void
1050 kernel_map_pages(struct page *page, int numpages, int enable)
1051 {
1052         if (!PageHighMem(page) && !enable)
1053                 debug_check_no_locks_freed(page_address(page),
1054                                            numpages * PAGE_SIZE);
1055 }
1056 #endif
1057
1058 extern struct vm_area_struct *get_gate_vma(struct task_struct *tsk);
1059 #ifdef  __HAVE_ARCH_GATE_AREA
1060 int in_gate_area_no_task(unsigned long addr);
1061 int in_gate_area(struct task_struct *task, unsigned long addr);
1062 #else
1063 int in_gate_area_no_task(unsigned long addr);
1064 #define in_gate_area(task, addr) ({(void)task; in_gate_area_no_task(addr);})
1065 #endif  /* __HAVE_ARCH_GATE_AREA */
1066
1067 /* /proc/<pid>/oom_adj set to -17 protects from the oom-killer */
1068 #define OOM_DISABLE -17
1069
1070 int drop_caches_sysctl_handler(struct ctl_table *, int, struct file *,
1071                                         void __user *, size_t *, loff_t *);
1072 unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask,
1073                         unsigned long lru_pages);
1074 void drop_pagecache(void);
1075 void drop_slab(void);
1076
1077 #ifndef CONFIG_MMU
1078 #define randomize_va_space 0
1079 #else
1080 extern int randomize_va_space;
1081 #endif
1082
1083 const char *arch_vma_name(struct vm_area_struct *vma);
1084
1085 #endif /* __KERNEL__ */
1086 #endif /* _LINUX_MM_H */