4 #include <linux/sched.h>
5 #include <linux/errno.h>
9 #include <linux/config.h>
10 #include <linux/gfp.h>
11 #include <linux/list.h>
12 #include <linux/mmzone.h>
13 #include <linux/rbtree.h>
16 #ifndef CONFIG_DISCONTIGMEM /* Don't use mapnrs, do it properly */
17 extern unsigned long max_mapnr;
20 extern unsigned long num_physpages;
21 extern void * high_memory;
22 extern int page_cluster;
25 #include <asm/pgtable.h>
26 #include <asm/processor.h>
27 #include <asm/atomic.h>
30 #define MM_VM_SIZE(mm) TASK_SIZE
34 * Linux kernel virtual memory manager primitives.
35 * The idea being to have a "virtual" mm in the same way
36 * we have a virtual fs - giving a cleaner interface to the
37 * mm details, and allowing different kinds of memory mappings
38 * (from shared memory to executable loading to arbitrary
43 * This struct defines a memory VMM memory area. There is one of these
44 * per VM-area/task. A VM area is any part of the process virtual memory
45 * space that has a special rule for the page-fault handlers (ie a shared
46 * library, the executable area etc).
48 * This structure is exactly 64 bytes on ia32. Please think very, very hard
49 * before adding anything to it.
51 struct vm_area_struct {
52 struct mm_struct * vm_mm; /* The address space we belong to. */
53 unsigned long vm_start; /* Our start address within vm_mm. */
54 unsigned long vm_end; /* The first byte after our end address
57 /* linked list of VM areas per task, sorted by address */
58 struct vm_area_struct *vm_next;
60 pgprot_t vm_page_prot; /* Access permissions of this VMA. */
61 unsigned long vm_flags; /* Flags, listed below. */
66 * For areas with an address space and backing store,
67 * one of the address_space->i_mmap{,shared} lists,
68 * for shm areas, the list of attaches, otherwise unused.
70 struct list_head shared;
72 /* Function pointers to deal with this struct. */
73 struct vm_operations_struct * vm_ops;
75 /* Information about our backing store: */
76 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
77 units, *not* PAGE_CACHE_SIZE */
78 struct file * vm_file; /* File we map to (can be NULL). */
79 void * vm_private_data; /* was vm_pte (shared mem) */
85 #define VM_READ 0x00000001 /* currently active flags */
86 #define VM_WRITE 0x00000002
87 #define VM_EXEC 0x00000004
88 #define VM_SHARED 0x00000008
90 #define VM_MAYREAD 0x00000010 /* limits for mprotect() etc */
91 #define VM_MAYWRITE 0x00000020
92 #define VM_MAYEXEC 0x00000040
93 #define VM_MAYSHARE 0x00000080
95 #define VM_GROWSDOWN 0x00000100 /* general info on the segment */
96 #define VM_GROWSUP 0x00000200
97 #define VM_SHM 0x00000400 /* shared memory area, don't swap out */
98 #define VM_DENYWRITE 0x00000800 /* ETXTBSY on write attempts.. */
100 #define VM_EXECUTABLE 0x00001000
101 #define VM_LOCKED 0x00002000
102 #define VM_IO 0x00004000 /* Memory mapped I/O or similar */
104 /* Used by sys_madvise() */
105 #define VM_SEQ_READ 0x00008000 /* App will access data sequentially */
106 #define VM_RAND_READ 0x00010000 /* App will not benefit from clustered reads */
108 #define VM_DONTCOPY 0x00020000 /* Do not copy this vma on fork */
109 #define VM_DONTEXPAND 0x00040000 /* Cannot expand with mremap() */
110 #define VM_RESERVED 0x00080000 /* Don't unmap it from swap_out */
111 #define VM_ACCOUNT 0x00100000 /* Is a VM accounted object */
112 #define VM_HUGETLB 0x00400000 /* Huge TLB Page VM */
114 #ifdef CONFIG_STACK_GROWSUP
115 #define VM_STACK_FLAGS (VM_GROWSUP | VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT)
117 #define VM_STACK_FLAGS (VM_GROWSDOWN | VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT)
120 #define VM_READHINTMASK (VM_SEQ_READ | VM_RAND_READ)
121 #define VM_ClearReadHint(v) (v)->vm_flags &= ~VM_READHINTMASK
122 #define VM_NormalReadHint(v) (!((v)->vm_flags & VM_READHINTMASK))
123 #define VM_SequentialReadHint(v) ((v)->vm_flags & VM_SEQ_READ)
124 #define VM_RandomReadHint(v) ((v)->vm_flags & VM_RAND_READ)
127 * mapping from the currently active vm_flags protection bits (the
128 * low four bits) to a page protection mask..
130 extern pgprot_t protection_map[16];
134 * These are the virtual MM functions - opening of an area, closing and
135 * unmapping it (needed to keep files on disk up-to-date etc), pointer
136 * to the functions called when a no-page or a wp-page exception occurs.
138 struct vm_operations_struct {
139 void (*open)(struct vm_area_struct * area);
140 void (*close)(struct vm_area_struct * area);
141 struct page * (*nopage)(struct vm_area_struct * area, unsigned long address, int unused);
142 int (*populate)(struct vm_area_struct * area, unsigned long address, unsigned long len, pgprot_t prot, unsigned long pgoff, int nonblock);
145 /* forward declaration; pte_chain is meant to be internal to rmap.c */
151 * Each physical page in the system has a struct page associated with
152 * it to keep track of whatever it is we are using the page for at the
153 * moment. Note that we have no way to track which tasks are using
156 * Try to keep the most commonly accessed fields in single cache lines
157 * here (16 bytes or greater). This ordering should be particularly
158 * beneficial on 32-bit processors.
160 * The first line is data used in page cache lookup, the second line
161 * is used for linear searches (eg. clock algorithm scans).
163 * TODO: make this structure smaller, it could be as small as 32 bytes.
166 unsigned long flags; /* atomic flags, some possibly
167 updated asynchronously */
168 atomic_t count; /* Usage count, see below. */
169 struct list_head list; /* ->mapping has some page lists. */
170 struct address_space *mapping; /* The inode (or ...) we belong to. */
171 unsigned long index; /* Our offset within mapping. */
172 struct list_head lru; /* Pageout list, eg. active_list;
173 protected by zone->lru_lock !! */
175 struct pte_chain *chain;/* Reverse pte mapping pointer.
176 * protected by PG_chainlock */
179 unsigned long private; /* mapping-private opaque data */
182 * On machines where all RAM is mapped into kernel address space,
183 * we can simply calculate the virtual address. On machines with
184 * highmem some memory is mapped into kernel virtual memory
185 * dynamically, so we need a place to store that address.
186 * Note that this field could be 16 bits on x86 ... ;)
188 * Architectures with slow multiplication can define
189 * WANT_PAGE_VIRTUAL in asm/page.h
191 #if defined(WANT_PAGE_VIRTUAL)
192 void *virtual; /* Kernel virtual address (NULL if
193 not kmapped, ie. highmem) */
194 #endif /* CONFIG_HIGMEM || WANT_PAGE_VIRTUAL */
198 * FIXME: take this include out, include page-flags.h in
199 * files which need it (119 of them)
201 #include <linux/page-flags.h>
204 * Methods to modify the page usage count.
206 * What counts for a page usage:
207 * - cache mapping (page->mapping)
208 * - private data (page->private)
209 * - page mapped in a task's page tables, each mapping
210 * is counted separately
212 * Also, many kernel routines increase the page count before a critical
213 * routine so they can be sure the page doesn't go away from under them.
215 #define put_page_testzero(p) \
217 BUG_ON(page_count(page) == 0); \
218 atomic_dec_and_test(&(p)->count); \
221 #define page_count(p) atomic_read(&(p)->count)
222 #define set_page_count(p,v) atomic_set(&(p)->count, v)
223 #define __put_page(p) atomic_dec(&(p)->count)
225 extern void FASTCALL(__page_cache_release(struct page *));
227 #ifdef CONFIG_HUGETLB_PAGE
229 static inline void get_page(struct page *page)
231 if (PageCompound(page))
232 page = (struct page *)page->lru.next;
233 atomic_inc(&page->count);
236 static inline void put_page(struct page *page)
238 if (PageCompound(page)) {
239 page = (struct page *)page->lru.next;
240 if (put_page_testzero(page)) {
241 if (page->lru.prev) { /* destructor? */
242 (*(void (*)(struct page *))page->lru.prev)(page);
244 __page_cache_release(page);
249 if (!PageReserved(page) && put_page_testzero(page))
250 __page_cache_release(page);
253 #else /* CONFIG_HUGETLB_PAGE */
255 static inline void get_page(struct page *page)
257 atomic_inc(&page->count);
260 static inline void put_page(struct page *page)
262 if (!PageReserved(page) && put_page_testzero(page))
263 __page_cache_release(page);
266 #endif /* CONFIG_HUGETLB_PAGE */
269 * Multiple processes may "see" the same page. E.g. for untouched
270 * mappings of /dev/null, all processes see the same page full of
271 * zeroes, and text pages of executables and shared libraries have
272 * only one copy in memory, at most, normally.
274 * For the non-reserved pages, page->count denotes a reference count.
275 * page->count == 0 means the page is free.
276 * page->count == 1 means the page is used for exactly one purpose
277 * (e.g. a private data page of one process).
279 * A page may be used for kmalloc() or anyone else who does a
280 * __get_free_page(). In this case the page->count is at least 1, and
281 * all other fields are unused but should be 0 or NULL. The
282 * management of this page is the responsibility of the one who uses
285 * The other pages (we may call them "process pages") are completely
286 * managed by the Linux memory manager: I/O, buffers, swapping etc.
287 * The following discussion applies only to them.
289 * A page may belong to an inode's memory mapping. In this case,
290 * page->mapping is the pointer to the inode, and page->index is the
291 * file offset of the page, in units of PAGE_CACHE_SIZE.
293 * A page contains an opaque `private' member, which belongs to the
294 * page's address_space. Usually, this is the address of a circular
295 * list of the page's disk buffers.
297 * For pages belonging to inodes, the page->count is the number of
298 * attaches, plus 1 if `private' contains something, plus one for
299 * the page cache itself.
301 * All pages belonging to an inode are in these doubly linked lists:
302 * mapping->clean_pages, mapping->dirty_pages and mapping->locked_pages;
303 * using the page->list list_head. These fields are also used for
304 * freelist managemet (when page->count==0).
306 * There is also a per-mapping radix tree mapping index to the page
307 * in memory if present. The tree is rooted at mapping->root.
309 * All process pages can do I/O:
310 * - inode pages may need to be read from disk,
311 * - inode pages which have been modified and are MAP_SHARED may need
312 * to be written to disk,
313 * - private pages which have been modified may need to be swapped out
314 * to swap space and (later) to be read back into memory.
318 * The zone field is never updated after free_area_init_core()
319 * sets it, so none of the operations on it need to be atomic.
322 #define ZONE_SHIFT (BITS_PER_LONG - 8)
325 extern struct zone *zone_table[];
327 static inline struct zone *page_zone(struct page *page)
329 return zone_table[page->flags >> ZONE_SHIFT];
332 static inline void set_page_zone(struct page *page, unsigned long zone_num)
334 page->flags &= ~(~0UL << ZONE_SHIFT);
335 page->flags |= zone_num << ZONE_SHIFT;
338 static inline void * lowmem_page_address(struct page *page)
340 return __va( ( (page - page_zone(page)->zone_mem_map) + page_zone(page)->zone_start_pfn) << PAGE_SHIFT);
343 #if defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL)
344 #define HASHED_PAGE_VIRTUAL
347 #if defined(WANT_PAGE_VIRTUAL)
348 #define page_address(page) ((page)->virtual)
349 #define set_page_address(page, address) \
351 (page)->virtual = (address); \
353 #define page_address_init() do { } while(0)
356 #if defined(HASHED_PAGE_VIRTUAL)
357 void *page_address(struct page *page);
358 void set_page_address(struct page *page, void *virtual);
359 void page_address_init(void);
362 #if !defined(HASHED_PAGE_VIRTUAL) && !defined(WANT_PAGE_VIRTUAL)
363 #define page_address(page) lowmem_page_address(page)
364 #define set_page_address(page, address) do { } while(0)
365 #define page_address_init() do { } while(0)
369 * Return true if this page is mapped into pagetables. Subtle: test pte.direct
370 * rather than pte.chain. Because sometimes pte.direct is 64-bit, and .chain
373 static inline int page_mapped(struct page *page)
375 return page->pte.direct != 0;
379 * Error return values for the *_nopage functions
381 #define NOPAGE_SIGBUS (NULL)
382 #define NOPAGE_OOM ((struct page *) (-1))
385 * Different kinds of faults, as returned by handle_mm_fault().
386 * Used to decide whether a process gets delivered SIGBUS or
387 * just gets major/minor fault counters bumped up.
389 #define VM_FAULT_OOM (-1)
390 #define VM_FAULT_SIGBUS 0
391 #define VM_FAULT_MINOR 1
392 #define VM_FAULT_MAJOR 2
394 #ifndef CONFIG_DISCONTIGMEM
395 /* The array of struct pages - for discontigmem use pgdat->lmem_map */
396 extern struct page *mem_map;
399 extern void show_free_areas(void);
401 struct page *shmem_nopage(struct vm_area_struct * vma,
402 unsigned long address, int unused);
403 struct file *shmem_file_setup(char * name, loff_t size, unsigned long flags);
404 void shmem_lock(struct file * file, int lock);
405 int shmem_zero_setup(struct vm_area_struct *);
407 void zap_page_range(struct vm_area_struct *vma, unsigned long address,
409 int unmap_vmas(struct mmu_gather **tlbp, struct mm_struct *mm,
410 struct vm_area_struct *start_vma, unsigned long start_addr,
411 unsigned long end_addr, unsigned long *nr_accounted);
412 void unmap_page_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
413 unsigned long address, unsigned long size);
414 void clear_page_tables(struct mmu_gather *tlb, unsigned long first, int nr);
415 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
416 struct vm_area_struct *vma);
417 int zeromap_page_range(struct vm_area_struct *vma, unsigned long from,
418 unsigned long size, pgprot_t prot);
420 extern int vmtruncate(struct inode * inode, loff_t offset);
421 extern pmd_t *FASTCALL(__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address));
422 extern pte_t *FASTCALL(pte_alloc_kernel(struct mm_struct *mm, pmd_t *pmd, unsigned long address));
423 extern pte_t *FASTCALL(pte_alloc_map(struct mm_struct *mm, pmd_t *pmd, unsigned long address));
424 extern int install_page(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, struct page *page, pgprot_t prot);
425 extern int handle_mm_fault(struct mm_struct *mm,struct vm_area_struct *vma, unsigned long address, int write_access);
426 extern int make_pages_present(unsigned long addr, unsigned long end);
427 extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
428 extern long sys_remap_file_pages(unsigned long start, unsigned long size, unsigned long prot, unsigned long pgoff, unsigned long nonblock);
431 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, unsigned long start,
432 int len, int write, int force, struct page **pages, struct vm_area_struct **vmas);
434 int __set_page_dirty_buffers(struct page *page);
435 int __set_page_dirty_nobuffers(struct page *page);
436 int set_page_dirty_lock(struct page *page);
439 * Prototype to add a shrinker callback for ageable caches.
441 * These functions are passed a count `nr_to_scan' and a gfpmask. They should
442 * scan `nr_to_scan' objects, attempting to free them.
444 * The callback must the number of objects which remain in the cache.
446 * The callback will be passes nr_to_scan == 0 when the VM is querying the
447 * cache size, so a fastpath for that case is appropriate.
449 typedef int (*shrinker_t)(int nr_to_scan, unsigned int gfp_mask);
452 * Add an aging callback. The int is the number of 'seeks' it takes
453 * to recreate one of the objects that these functions age.
456 #define DEFAULT_SEEKS 2
458 extern struct shrinker *set_shrinker(int, shrinker_t);
459 extern void remove_shrinker(struct shrinker *shrinker);
462 * If the mapping doesn't provide a set_page_dirty a_op, then
463 * just fall through and assume that it wants buffer_heads.
464 * FIXME: make the method unconditional.
466 static inline int set_page_dirty(struct page *page)
469 int (*spd)(struct page *);
471 spd = page->mapping->a_ops->set_page_dirty;
475 return __set_page_dirty_buffers(page);
479 * On a two-level page table, this ends up being trivial. Thus the
480 * inlining and the symmetry break with pte_alloc_map() that does all
481 * of this out-of-line.
483 static inline pmd_t *pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
486 return __pmd_alloc(mm, pgd, address);
487 return pmd_offset(pgd, address);
490 extern void free_area_init(unsigned long * zones_size);
491 extern void free_area_init_node(int nid, pg_data_t *pgdat, struct page *pmap,
492 unsigned long * zones_size, unsigned long zone_start_pfn,
493 unsigned long *zholes_size);
494 extern void memmap_init_zone(struct page *, unsigned long, int,
495 unsigned long, unsigned long);
496 extern void mem_init(void);
497 extern void show_mem(void);
498 extern void si_meminfo(struct sysinfo * val);
499 extern void si_meminfo_node(struct sysinfo *val, int nid);
502 extern void insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
503 extern void build_mmap_rb(struct mm_struct *);
504 extern void exit_mmap(struct mm_struct *);
506 extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
508 extern unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
509 unsigned long len, unsigned long prot,
510 unsigned long flag, unsigned long pgoff);
512 static inline unsigned long do_mmap(struct file *file, unsigned long addr,
513 unsigned long len, unsigned long prot,
514 unsigned long flag, unsigned long offset)
516 unsigned long ret = -EINVAL;
517 if ((offset + PAGE_ALIGN(len)) < offset)
519 if (!(offset & ~PAGE_MASK))
520 ret = do_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
525 extern int do_munmap(struct mm_struct *, unsigned long, size_t);
527 extern unsigned long do_brk(unsigned long, unsigned long);
530 __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
531 struct vm_area_struct *prev)
533 prev->vm_next = vma->vm_next;
534 rb_erase(&vma->vm_rb, &mm->mm_rb);
535 if (mm->mmap_cache == vma)
536 mm->mmap_cache = prev;
540 can_vma_merge(struct vm_area_struct *vma, unsigned long vm_flags)
543 if (!vma->vm_file && vma->vm_flags == vm_flags)
550 extern unsigned long page_unuse(struct page *);
551 extern void truncate_inode_pages(struct address_space *, loff_t);
553 /* generic vm_area_ops exported for stackable file systems */
554 extern struct page *filemap_nopage(struct vm_area_struct *, unsigned long, int);
556 /* mm/page-writeback.c */
557 int write_one_page(struct page *page, int wait);
560 #define VM_MAX_READAHEAD 128 /* kbytes */
561 #define VM_MIN_READAHEAD 16 /* kbytes (includes current page) */
563 int do_page_cache_readahead(struct address_space *mapping, struct file *filp,
564 unsigned long offset, unsigned long nr_to_read);
565 void page_cache_readahead(struct address_space *mapping,
566 struct file_ra_state *ra,
568 unsigned long offset);
569 void page_cache_readaround(struct address_space *mapping,
570 struct file_ra_state *ra,
572 unsigned long offset);
573 void handle_ra_miss(struct address_space *mapping,
574 struct file_ra_state *ra, pgoff_t offset);
575 unsigned long max_sane_readahead(unsigned long nr);
577 /* Do stack extension */
578 extern int expand_stack(struct vm_area_struct * vma, unsigned long address);
580 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
581 extern struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr);
582 extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr,
583 struct vm_area_struct **pprev);
584 extern int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
585 unsigned long addr, int new_below);
587 /* Look up the first VMA which intersects the interval start_addr..end_addr-1,
588 NULL if none. Assume start_addr < end_addr. */
589 static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * mm, unsigned long start_addr, unsigned long end_addr)
591 struct vm_area_struct * vma = find_vma(mm,start_addr);
593 if (vma && end_addr <= vma->vm_start)
598 extern struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr);
600 extern unsigned int nr_used_zone_pages(void);
602 extern struct page * vmalloc_to_page(void *addr);
603 extern struct page * follow_page(struct mm_struct *mm, unsigned long address,
605 extern int remap_page_range(struct vm_area_struct *vma, unsigned long from,
606 unsigned long to, unsigned long size, pgprot_t prot);
607 #endif /* __KERNEL__ */
608 #endif /* _LINUX_MM_H */