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/atomic.h>
29 * Linux kernel virtual memory manager primitives.
30 * The idea being to have a "virtual" mm in the same way
31 * we have a virtual fs - giving a cleaner interface to the
32 * mm details, and allowing different kinds of memory mappings
33 * (from shared memory to executable loading to arbitrary
38 * This struct defines a memory VMM memory area. There is one of these
39 * per VM-area/task. A VM area is any part of the process virtual memory
40 * space that has a special rule for the page-fault handlers (ie a shared
41 * library, the executable area etc).
43 * This structure is exactly 64 bytes on ia32. Please think very, very hard
44 * before adding anything to it.
46 struct vm_area_struct {
47 struct mm_struct * vm_mm; /* The address space we belong to. */
48 unsigned long vm_start; /* Our start address within vm_mm. */
49 unsigned long vm_end; /* The first byte after our end address
52 /* linked list of VM areas per task, sorted by address */
53 struct vm_area_struct *vm_next;
55 pgprot_t vm_page_prot; /* Access permissions of this VMA. */
56 unsigned long vm_flags; /* Flags, listed below. */
61 * For areas with an address space and backing store,
62 * one of the address_space->i_mmap{,shared} lists,
63 * for shm areas, the list of attaches, otherwise unused.
65 struct list_head shared;
67 /* Function pointers to deal with this struct. */
68 struct vm_operations_struct * vm_ops;
70 /* Information about our backing store: */
71 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
72 units, *not* PAGE_CACHE_SIZE */
73 struct file * vm_file; /* File we map to (can be NULL). */
74 void * vm_private_data; /* was vm_pte (shared mem) */
80 #define VM_READ 0x00000001 /* currently active flags */
81 #define VM_WRITE 0x00000002
82 #define VM_EXEC 0x00000004
83 #define VM_SHARED 0x00000008
85 #define VM_MAYREAD 0x00000010 /* limits for mprotect() etc */
86 #define VM_MAYWRITE 0x00000020
87 #define VM_MAYEXEC 0x00000040
88 #define VM_MAYSHARE 0x00000080
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.. */
95 #define VM_EXECUTABLE 0x00001000
96 #define VM_LOCKED 0x00002000
97 #define VM_IO 0x00004000 /* Memory mapped I/O or similar */
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 */
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 #define VM_ACCOUNT 0x00100000 /* Is a VM accounted object */
107 #define VM_HUGETLB 0x00400000 /* Huge TLB Page VM */
109 #ifdef CONFIG_STACK_GROWSUP
110 #define VM_STACK_FLAGS (VM_GROWSUP | VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT)
112 #define VM_STACK_FLAGS (VM_GROWSDOWN | VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT)
115 #define VM_READHINTMASK (VM_SEQ_READ | VM_RAND_READ)
116 #define VM_ClearReadHint(v) (v)->vm_flags &= ~VM_READHINTMASK
117 #define VM_NormalReadHint(v) (!((v)->vm_flags & VM_READHINTMASK))
118 #define VM_SequentialReadHint(v) ((v)->vm_flags & VM_SEQ_READ)
119 #define VM_RandomReadHint(v) ((v)->vm_flags & VM_RAND_READ)
122 * mapping from the currently active vm_flags protection bits (the
123 * low four bits) to a page protection mask..
125 extern pgprot_t protection_map[16];
129 * These are the virtual MM functions - opening of an area, closing and
130 * unmapping it (needed to keep files on disk up-to-date etc), pointer
131 * to the functions called when a no-page or a wp-page exception occurs.
133 struct vm_operations_struct {
134 void (*open)(struct vm_area_struct * area);
135 void (*close)(struct vm_area_struct * area);
136 struct page * (*nopage)(struct vm_area_struct * area, unsigned long address, int unused);
137 int (*populate)(struct vm_area_struct * area, unsigned long address, unsigned long len, pgprot_t prot, unsigned long pgoff, int nonblock);
140 /* forward declaration; pte_chain is meant to be internal to rmap.c */
146 * Each physical page in the system has a struct page associated with
147 * it to keep track of whatever it is we are using the page for at the
148 * moment. Note that we have no way to track which tasks are using
151 * Try to keep the most commonly accessed fields in single cache lines
152 * here (16 bytes or greater). This ordering should be particularly
153 * beneficial on 32-bit processors.
155 * The first line is data used in page cache lookup, the second line
156 * is used for linear searches (eg. clock algorithm scans).
158 * TODO: make this structure smaller, it could be as small as 32 bytes.
161 unsigned long flags; /* atomic flags, some possibly
162 updated asynchronously */
163 atomic_t count; /* Usage count, see below. */
164 struct list_head list; /* ->mapping has some page lists. */
165 struct address_space *mapping; /* The inode (or ...) we belong to. */
166 unsigned long index; /* Our offset within mapping. */
167 struct list_head lru; /* Pageout list, eg. active_list;
168 protected by zone->lru_lock !! */
170 struct pte_chain *chain;/* Reverse pte mapping pointer.
171 * protected by PG_chainlock */
174 unsigned long private; /* mapping-private opaque data */
177 * On machines where all RAM is mapped into kernel address space,
178 * we can simply calculate the virtual address. On machines with
179 * highmem some memory is mapped into kernel virtual memory
180 * dynamically, so we need a place to store that address.
181 * Note that this field could be 16 bits on x86 ... ;)
183 * Architectures with slow multiplication can define
184 * WANT_PAGE_VIRTUAL in asm/page.h
186 #if defined(WANT_PAGE_VIRTUAL)
187 void *virtual; /* Kernel virtual address (NULL if
188 not kmapped, ie. highmem) */
189 #endif /* CONFIG_HIGMEM || WANT_PAGE_VIRTUAL */
193 * FIXME: take this include out, include page-flags.h in
194 * files which need it (119 of them)
196 #include <linux/page-flags.h>
199 * Methods to modify the page usage count.
201 * What counts for a page usage:
202 * - cache mapping (page->mapping)
203 * - private data (page->private)
204 * - page mapped in a task's page tables, each mapping
205 * is counted separately
207 * Also, many kernel routines increase the page count before a critical
208 * routine so they can be sure the page doesn't go away from under them.
210 #define put_page_testzero(p) \
212 BUG_ON(page_count(page) == 0); \
213 atomic_dec_and_test(&(p)->count); \
216 #define page_count(p) atomic_read(&(p)->count)
217 #define set_page_count(p,v) atomic_set(&(p)->count, v)
218 #define __put_page(p) atomic_dec(&(p)->count)
220 extern void FASTCALL(__page_cache_release(struct page *));
222 #ifdef CONFIG_HUGETLB_PAGE
224 static inline void get_page(struct page *page)
226 if (PageCompound(page))
227 page = (struct page *)page->lru.next;
228 atomic_inc(&page->count);
231 static inline void put_page(struct page *page)
233 if (PageCompound(page)) {
234 page = (struct page *)page->lru.next;
235 if (put_page_testzero(page)) {
236 if (page->lru.prev) { /* destructor? */
237 (*(void (*)(struct page *))page->lru.prev)(page);
239 __page_cache_release(page);
244 if (!PageReserved(page) && put_page_testzero(page))
245 __page_cache_release(page);
248 #else /* CONFIG_HUGETLB_PAGE */
250 static inline void get_page(struct page *page)
252 atomic_inc(&page->count);
255 static inline void put_page(struct page *page)
257 if (!PageReserved(page) && put_page_testzero(page))
258 __page_cache_release(page);
261 #endif /* CONFIG_HUGETLB_PAGE */
264 * Multiple processes may "see" the same page. E.g. for untouched
265 * mappings of /dev/null, all processes see the same page full of
266 * zeroes, and text pages of executables and shared libraries have
267 * only one copy in memory, at most, normally.
269 * For the non-reserved pages, page->count denotes a reference count.
270 * page->count == 0 means the page is free.
271 * page->count == 1 means the page is used for exactly one purpose
272 * (e.g. a private data page of one process).
274 * A page may be used for kmalloc() or anyone else who does a
275 * __get_free_page(). In this case the page->count is at least 1, and
276 * all other fields are unused but should be 0 or NULL. The
277 * management of this page is the responsibility of the one who uses
280 * The other pages (we may call them "process pages") are completely
281 * managed by the Linux memory manager: I/O, buffers, swapping etc.
282 * The following discussion applies only to them.
284 * A page may belong to an inode's memory mapping. In this case,
285 * page->mapping is the pointer to the inode, and page->index is the
286 * file offset of the page, in units of PAGE_CACHE_SIZE.
288 * A page contains an opaque `private' member, which belongs to the
289 * page's address_space. Usually, this is the address of a circular
290 * list of the page's disk buffers.
292 * For pages belonging to inodes, the page->count is the number of
293 * attaches, plus 1 if `private' contains something, plus one for
294 * the page cache itself.
296 * All pages belonging to an inode are in these doubly linked lists:
297 * mapping->clean_pages, mapping->dirty_pages and mapping->locked_pages;
298 * using the page->list list_head. These fields are also used for
299 * freelist managemet (when page->count==0).
301 * There is also a per-mapping radix tree mapping index to the page
302 * in memory if present. The tree is rooted at mapping->root.
304 * All process pages can do I/O:
305 * - inode pages may need to be read from disk,
306 * - inode pages which have been modified and are MAP_SHARED may need
307 * to be written to disk,
308 * - private pages which have been modified may need to be swapped out
309 * to swap space and (later) to be read back into memory.
313 * The zone field is never updated after free_area_init_core()
314 * sets it, so none of the operations on it need to be atomic.
317 #define ZONE_SHIFT (BITS_PER_LONG - 8)
320 extern struct zone *zone_table[];
322 static inline struct zone *page_zone(struct page *page)
324 return zone_table[page->flags >> ZONE_SHIFT];
327 static inline void set_page_zone(struct page *page, unsigned long zone_num)
329 page->flags &= ~(~0UL << ZONE_SHIFT);
330 page->flags |= zone_num << ZONE_SHIFT;
333 static inline void * lowmem_page_address(struct page *page)
335 return __va( ( (page - page_zone(page)->zone_mem_map) + page_zone(page)->zone_start_pfn) << PAGE_SHIFT);
338 #if defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL)
339 #define HASHED_PAGE_VIRTUAL
342 #if defined(WANT_PAGE_VIRTUAL)
343 #define page_address(page) ((page)->virtual)
344 #define set_page_address(page, address) \
346 (page)->virtual = (address); \
348 #define page_address_init() do { } while(0)
351 #if defined(HASHED_PAGE_VIRTUAL)
352 void *page_address(struct page *page);
353 void set_page_address(struct page *page, void *virtual);
354 void page_address_init(void);
357 #if !defined(HASHED_PAGE_VIRTUAL) && !defined(WANT_PAGE_VIRTUAL)
358 #define page_address(page) lowmem_page_address(page)
359 #define set_page_address(page, address) do { } while(0)
360 #define page_address_init() do { } while(0)
364 * Return true if this page is mapped into pagetables. Subtle: test pte.direct
365 * rather than pte.chain. Because sometimes pte.direct is 64-bit, and .chain
368 static inline int page_mapped(struct page *page)
370 return page->pte.direct != 0;
374 * Error return values for the *_nopage functions
376 #define NOPAGE_SIGBUS (NULL)
377 #define NOPAGE_OOM ((struct page *) (-1))
380 * Different kinds of faults, as returned by handle_mm_fault().
381 * Used to decide whether a process gets delivered SIGBUS or
382 * just gets major/minor fault counters bumped up.
384 #define VM_FAULT_OOM (-1)
385 #define VM_FAULT_SIGBUS 0
386 #define VM_FAULT_MINOR 1
387 #define VM_FAULT_MAJOR 2
389 #ifndef CONFIG_DISCONTIGMEM
390 /* The array of struct pages - for discontigmem use pgdat->lmem_map */
391 extern struct page *mem_map;
394 extern void show_free_areas(void);
396 struct page *shmem_nopage(struct vm_area_struct * vma,
397 unsigned long address, int unused);
398 struct file *shmem_file_setup(char * name, loff_t size, unsigned long flags);
399 void shmem_lock(struct file * file, int lock);
400 int shmem_zero_setup(struct vm_area_struct *);
402 void zap_page_range(struct vm_area_struct *vma, unsigned long address,
404 int unmap_vmas(struct mmu_gather **tlbp, struct mm_struct *mm,
405 struct vm_area_struct *start_vma, unsigned long start_addr,
406 unsigned long end_addr, unsigned long *nr_accounted);
407 void unmap_page_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
408 unsigned long address, unsigned long size);
409 void clear_page_tables(struct mmu_gather *tlb, unsigned long first, int nr);
410 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
411 struct vm_area_struct *vma);
412 int zeromap_page_range(struct vm_area_struct *vma, unsigned long from,
413 unsigned long size, pgprot_t prot);
415 extern int vmtruncate(struct inode * inode, loff_t offset);
416 extern pmd_t *FASTCALL(__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address));
417 extern pte_t *FASTCALL(pte_alloc_kernel(struct mm_struct *mm, pmd_t *pmd, unsigned long address));
418 extern pte_t *FASTCALL(pte_alloc_map(struct mm_struct *mm, pmd_t *pmd, unsigned long address));
419 extern int install_page(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, struct page *page, pgprot_t prot);
420 extern int handle_mm_fault(struct mm_struct *mm,struct vm_area_struct *vma, unsigned long address, int write_access);
421 extern int make_pages_present(unsigned long addr, unsigned long end);
422 extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
423 extern long sys_remap_file_pages(unsigned long start, unsigned long size, unsigned long prot, unsigned long pgoff, unsigned long nonblock);
426 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, unsigned long start,
427 int len, int write, int force, struct page **pages, struct vm_area_struct **vmas);
429 int __set_page_dirty_buffers(struct page *page);
430 int __set_page_dirty_nobuffers(struct page *page);
431 int set_page_dirty_lock(struct page *page);
434 * Prototype to add a shrinker callback for ageable caches.
436 * These functions are passed a count `nr_to_scan' and a gfpmask. They should
437 * scan `nr_to_scan' objects, attempting to free them.
439 * The callback must the number of objects which remain in the cache.
441 * The callback will be passes nr_to_scan == 0 when the VM is querying the
442 * cache size, so a fastpath for that case is appropriate.
444 typedef int (*shrinker_t)(int nr_to_scan, unsigned int gfp_mask);
447 * Add an aging callback. The int is the number of 'seeks' it takes
448 * to recreate one of the objects that these functions age.
451 #define DEFAULT_SEEKS 2
453 extern struct shrinker *set_shrinker(int, shrinker_t);
454 extern void remove_shrinker(struct shrinker *shrinker);
457 * If the mapping doesn't provide a set_page_dirty a_op, then
458 * just fall through and assume that it wants buffer_heads.
459 * FIXME: make the method unconditional.
461 static inline int set_page_dirty(struct page *page)
464 int (*spd)(struct page *);
466 spd = page->mapping->a_ops->set_page_dirty;
470 return __set_page_dirty_buffers(page);
474 * On a two-level page table, this ends up being trivial. Thus the
475 * inlining and the symmetry break with pte_alloc_map() that does all
476 * of this out-of-line.
478 static inline pmd_t *pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
481 return __pmd_alloc(mm, pgd, address);
482 return pmd_offset(pgd, address);
485 extern void free_area_init(unsigned long * zones_size);
486 extern void free_area_init_node(int nid, pg_data_t *pgdat, struct page *pmap,
487 unsigned long * zones_size, unsigned long zone_start_pfn,
488 unsigned long *zholes_size);
489 extern void memmap_init_zone(struct page *, unsigned long, int,
490 unsigned long, unsigned long);
491 extern void mem_init(void);
492 extern void show_mem(void);
493 extern void si_meminfo(struct sysinfo * val);
494 extern void si_meminfo_node(struct sysinfo *val, int nid);
497 extern void insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
498 extern void build_mmap_rb(struct mm_struct *);
499 extern void exit_mmap(struct mm_struct *);
501 extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
503 extern unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
504 unsigned long len, unsigned long prot,
505 unsigned long flag, unsigned long pgoff);
507 static inline unsigned long do_mmap(struct file *file, unsigned long addr,
508 unsigned long len, unsigned long prot,
509 unsigned long flag, unsigned long offset)
511 unsigned long ret = -EINVAL;
512 if ((offset + PAGE_ALIGN(len)) < offset)
514 if (!(offset & ~PAGE_MASK))
515 ret = do_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
520 extern int do_munmap(struct mm_struct *, unsigned long, size_t);
522 extern unsigned long do_brk(unsigned long, unsigned long);
525 __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
526 struct vm_area_struct *prev)
528 prev->vm_next = vma->vm_next;
529 rb_erase(&vma->vm_rb, &mm->mm_rb);
530 if (mm->mmap_cache == vma)
531 mm->mmap_cache = prev;
535 can_vma_merge(struct vm_area_struct *vma, unsigned long vm_flags)
538 if (!vma->vm_file && vma->vm_flags == vm_flags)
545 extern unsigned long page_unuse(struct page *);
546 extern void truncate_inode_pages(struct address_space *, loff_t);
548 /* generic vm_area_ops exported for stackable file systems */
549 extern struct page *filemap_nopage(struct vm_area_struct *, unsigned long, int);
551 /* mm/page-writeback.c */
552 int write_one_page(struct page *page, int wait);
555 #define VM_MAX_READAHEAD 128 /* kbytes */
556 #define VM_MIN_READAHEAD 16 /* kbytes (includes current page) */
558 int do_page_cache_readahead(struct address_space *mapping, struct file *filp,
559 unsigned long offset, unsigned long nr_to_read);
560 void page_cache_readahead(struct address_space *mapping,
561 struct file_ra_state *ra,
563 unsigned long offset);
564 void page_cache_readaround(struct address_space *mapping,
565 struct file_ra_state *ra,
567 unsigned long offset);
568 void handle_ra_miss(struct address_space *mapping,
569 struct file_ra_state *ra, pgoff_t offset);
570 unsigned long max_sane_readahead(unsigned long nr);
572 /* Do stack extension */
573 extern int expand_stack(struct vm_area_struct * vma, unsigned long address);
575 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
576 extern struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr);
577 extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr,
578 struct vm_area_struct **pprev);
579 extern int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
580 unsigned long addr, int new_below);
582 /* Look up the first VMA which intersects the interval start_addr..end_addr-1,
583 NULL if none. Assume start_addr < end_addr. */
584 static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * mm, unsigned long start_addr, unsigned long end_addr)
586 struct vm_area_struct * vma = find_vma(mm,start_addr);
588 if (vma && end_addr <= vma->vm_start)
593 extern struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr);
595 extern unsigned int nr_used_zone_pages(void);
597 extern struct page * vmalloc_to_page(void *addr);
598 extern struct page * follow_page(struct mm_struct *mm, unsigned long address,
600 extern int remap_page_range(struct vm_area_struct *vma, unsigned long from,
601 unsigned long to, unsigned long size, pgprot_t prot);
602 #endif /* __KERNEL__ */
603 #endif /* _LINUX_MM_H */