📄 mm.h
字号:
#ifndef _LINUX_MM_H#define _LINUX_MM_H#include <linux/sched.h>#include <linux/errno.h>#include <linux/capability.h>#ifdef __KERNEL__#include <linux/gfp.h>#include <linux/list.h>#include <linux/mmzone.h>#include <linux/rbtree.h>#include <linux/prio_tree.h>#include <linux/fs.h>#include <linux/mutex.h>#include <linux/debug_locks.h>#include <linux/backing-dev.h>#include <linux/mm_types.h>struct mempolicy;struct anon_vma;#ifndef CONFIG_DISCONTIGMEM /* Don't use mapnrs, do it properly */extern unsigned long max_mapnr;#endifextern unsigned long num_physpages;extern void * high_memory;extern unsigned long vmalloc_earlyreserve;extern int page_cluster;#ifdef CONFIG_SYSCTLextern int sysctl_legacy_va_layout;#else#define sysctl_legacy_va_layout 0#endif#include <asm/page.h>#include <asm/pgtable.h>#include <asm/processor.h>#define nth_page(page,n) pfn_to_page(page_to_pfn((page)) + (n))/* * Linux kernel virtual memory manager primitives. * The idea being to have a "virtual" mm in the same way * we have a virtual fs - giving a cleaner interface to the * mm details, and allowing different kinds of memory mappings * (from shared memory to executable loading to arbitrary * mmap() functions). *//* * This struct defines a memory VMM memory area. There is one of these * per VM-area/task. A VM area is any part of the process virtual memory * space that has a special rule for the page-fault handlers (ie a shared * library, the executable area etc). */struct vm_area_struct { struct mm_struct * vm_mm; /* The address space we belong to. */ unsigned long vm_start; /* Our start address within vm_mm. */ unsigned long vm_end; /* The first byte after our end address within vm_mm. */ /* linked list of VM areas per task, sorted by address */ struct vm_area_struct *vm_next; pgprot_t vm_page_prot; /* Access permissions of this VMA. */ unsigned long vm_flags; /* Flags, listed below. */ struct rb_node vm_rb; /* * For areas with an address space and backing store, * linkage into the address_space->i_mmap prio tree, or * linkage to the list of like vmas hanging off its node, or * linkage of vma in the address_space->i_mmap_nonlinear list. */ union { struct { struct list_head list; void *parent; /* aligns with prio_tree_node parent */ struct vm_area_struct *head; } vm_set; struct raw_prio_tree_node prio_tree_node; } shared; /* * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma * list, after a COW of one of the file pages. A MAP_SHARED vma * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack * or brk vma (with NULL file) can only be in an anon_vma list. */ struct list_head anon_vma_node; /* Serialized by anon_vma->lock */ struct anon_vma *anon_vma; /* Serialized by page_table_lock */ /* Function pointers to deal with this struct. */ struct vm_operations_struct * vm_ops; /* Information about our backing store: */ unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE units, *not* PAGE_CACHE_SIZE */ struct file * vm_file; /* File we map to (can be NULL). */ void * vm_private_data; /* was vm_pte (shared mem) */ unsigned long vm_truncate_count;/* truncate_count or restart_addr */#ifndef CONFIG_MMU atomic_t vm_usage; /* refcount (VMAs shared if !MMU) */#endif#ifdef CONFIG_NUMA struct mempolicy *vm_policy; /* NUMA policy for the VMA */#endif};extern struct kmem_cache *vm_area_cachep;/* * This struct defines the per-mm list of VMAs for uClinux. If CONFIG_MMU is * disabled, then there's a single shared list of VMAs maintained by the * system, and mm's subscribe to these individually */struct vm_list_struct { struct vm_list_struct *next; struct vm_area_struct *vma;};#ifndef CONFIG_MMUextern struct rb_root nommu_vma_tree;extern struct rw_semaphore nommu_vma_sem;extern unsigned int kobjsize(const void *objp);#endif/* * vm_flags.. */#define VM_READ 0x00000001 /* currently active flags */#define VM_WRITE 0x00000002#define VM_EXEC 0x00000004#define VM_SHARED 0x00000008/* mprotect() hardcodes VM_MAYREAD >> 4 == VM_READ, and so for r/w/x bits. */#define VM_MAYREAD 0x00000010 /* limits for mprotect() etc */#define VM_MAYWRITE 0x00000020#define VM_MAYEXEC 0x00000040#define VM_MAYSHARE 0x00000080#define VM_GROWSDOWN 0x00000100 /* general info on the segment */#define VM_GROWSUP 0x00000200#define VM_PFNMAP 0x00000400 /* Page-ranges managed without "struct page", just pure PFN */#define VM_DENYWRITE 0x00000800 /* ETXTBSY on write attempts.. */#define VM_EXECUTABLE 0x00001000#define VM_LOCKED 0x00002000#define VM_IO 0x00004000 /* Memory mapped I/O or similar */ /* Used by sys_madvise() */#define VM_SEQ_READ 0x00008000 /* App will access data sequentially */#define VM_RAND_READ 0x00010000 /* App will not benefit from clustered reads */#define VM_DONTCOPY 0x00020000 /* Do not copy this vma on fork */#define VM_DONTEXPAND 0x00040000 /* Cannot expand with mremap() */#define VM_RESERVED 0x00080000 /* Count as reserved_vm like IO */#define VM_ACCOUNT 0x00100000 /* Is a VM accounted object */#define VM_HUGETLB 0x00400000 /* Huge TLB Page VM */#define VM_NONLINEAR 0x00800000 /* Is non-linear (remap_file_pages) */#define VM_MAPPED_COPY 0x01000000 /* T if mapped copy of data (nommu mmap) */#define VM_INSERTPAGE 0x02000000 /* The vma has had "vm_insert_page()" done on it */#define VM_ALWAYSDUMP 0x04000000 /* Always include in core dumps */#ifndef VM_STACK_DEFAULT_FLAGS /* arch can override this */#define VM_STACK_DEFAULT_FLAGS VM_DATA_DEFAULT_FLAGS#endif#ifdef CONFIG_STACK_GROWSUP#define VM_STACK_FLAGS (VM_GROWSUP | VM_STACK_DEFAULT_FLAGS | VM_ACCOUNT)#else#define VM_STACK_FLAGS (VM_GROWSDOWN | VM_STACK_DEFAULT_FLAGS | VM_ACCOUNT)#endif#define VM_READHINTMASK (VM_SEQ_READ | VM_RAND_READ)#define VM_ClearReadHint(v) (v)->vm_flags &= ~VM_READHINTMASK#define VM_NormalReadHint(v) (!((v)->vm_flags & VM_READHINTMASK))#define VM_SequentialReadHint(v) ((v)->vm_flags & VM_SEQ_READ)#define VM_RandomReadHint(v) ((v)->vm_flags & VM_RAND_READ)/* * mapping from the currently active vm_flags protection bits (the * low four bits) to a page protection mask.. */extern pgprot_t protection_map[16];/* * These are the virtual MM functions - opening of an area, closing and * unmapping it (needed to keep files on disk up-to-date etc), pointer * to the functions called when a no-page or a wp-page exception occurs. */struct vm_operations_struct { void (*open)(struct vm_area_struct * area); void (*close)(struct vm_area_struct * area); struct page * (*nopage)(struct vm_area_struct * area, unsigned long address, int *type); unsigned long (*nopfn)(struct vm_area_struct * area, unsigned long address); int (*populate)(struct vm_area_struct * area, unsigned long address, unsigned long len, pgprot_t prot, unsigned long pgoff, int nonblock); /* notification that a previously read-only page is about to become * writable, if an error is returned it will cause a SIGBUS */ int (*page_mkwrite)(struct vm_area_struct *vma, struct page *page);#ifdef CONFIG_NUMA int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new); struct mempolicy *(*get_policy)(struct vm_area_struct *vma, unsigned long addr); int (*migrate)(struct vm_area_struct *vma, const nodemask_t *from, const nodemask_t *to, unsigned long flags);#endif};struct mmu_gather;struct inode;#define page_private(page) ((page)->private)#define set_page_private(page, v) ((page)->private = (v))/* * FIXME: take this include out, include page-flags.h in * files which need it (119 of them) */#include <linux/page-flags.h>#ifdef CONFIG_DEBUG_VM#define VM_BUG_ON(cond) BUG_ON(cond)#else#define VM_BUG_ON(condition) do { } while(0)#endif/* * Methods to modify the page usage count. * * What counts for a page usage: * - cache mapping (page->mapping) * - private data (page->private) * - page mapped in a task's page tables, each mapping * is counted separately * * Also, many kernel routines increase the page count before a critical * routine so they can be sure the page doesn't go away from under them. *//* * Drop a ref, return true if the refcount fell to zero (the page has no users) */static inline int put_page_testzero(struct page *page){ VM_BUG_ON(atomic_read(&page->_count) == 0); return atomic_dec_and_test(&page->_count);}/* * Try to grab a ref unless the page has a refcount of zero, return false if * that is the case. */static inline int get_page_unless_zero(struct page *page){ VM_BUG_ON(PageCompound(page)); return atomic_inc_not_zero(&page->_count);}static inline int page_count(struct page *page){ if (unlikely(PageCompound(page))) page = (struct page *)page_private(page); return atomic_read(&page->_count);}static inline void get_page(struct page *page){ if (unlikely(PageCompound(page))) page = (struct page *)page_private(page); VM_BUG_ON(atomic_read(&page->_count) == 0); atomic_inc(&page->_count);}/* * Setup the page count before being freed into the page allocator for * the first time (boot or memory hotplug) */static inline void init_page_count(struct page *page){ atomic_set(&page->_count, 1);}void put_page(struct page *page);void put_pages_list(struct list_head *pages);void split_page(struct page *page, unsigned int order);/* * Compound pages have a destructor function. Provide a * prototype for that function and accessor functions. * These are _only_ valid on the head of a PG_compound page. */typedef void compound_page_dtor(struct page *);static inline void set_compound_page_dtor(struct page *page, compound_page_dtor *dtor){ page[1].lru.next = (void *)dtor;}static inline compound_page_dtor *get_compound_page_dtor(struct page *page){ return (compound_page_dtor *)page[1].lru.next;}/* * Multiple processes may "see" the same page. E.g. for untouched * mappings of /dev/null, all processes see the same page full of * zeroes, and text pages of executables and shared libraries have * only one copy in memory, at most, normally. * * For the non-reserved pages, page_count(page) denotes a reference count. * page_count() == 0 means the page is free. page->lru is then used for * freelist management in the buddy allocator. * page_count() > 0 means the page has been allocated. * * Pages are allocated by the slab allocator in order to provide memory * to kmalloc and kmem_cache_alloc. In this case, the management of the * page, and the fields in 'struct page' are the responsibility of mm/slab.c * unless a particular usage is carefully commented. (the responsibility of * freeing the kmalloc memory is the caller's, of course). * * A page may be used by anyone else who does a __get_free_page(). * In this case, page_count still tracks the references, and should only * be used through the normal accessor functions. The top bits of page->flags * and page->virtual store page management information, but all other fields * are unused and could be used privately, carefully. The management of this * page is the responsibility of the one who allocated it, and those who have * subsequently been given references to it. * * The other pages (we may call them "pagecache pages") are completely * managed by the Linux memory manager: I/O, buffers, swapping etc. * The following discussion applies only to them. * * A pagecache page contains an opaque `private' member, which belongs to the * page's address_space. Usually, this is the address of a circular list of * the page's disk buffers. PG_private must be set to tell the VM to call * into the filesystem to release these pages. * * A page may belong to an inode's memory mapping. In this case, page->mapping * is the pointer to the inode, and page->index is the file offset of the page, * in units of PAGE_CACHE_SIZE. * * If pagecache pages are not associated with an inode, they are said to be * anonymous pages. These may become associated with the swapcache, and in that * case PG_swapcache is set, and page->private is an offset into the swapcache. * * In either case (swapcache or inode backed), the pagecache itself holds one * reference to the page. Setting PG_private should also increment the * refcount. The each user mapping also has a reference to the page. * * The pagecache pages are stored in a per-mapping radix tree, which is * rooted at mapping->page_tree, and indexed by offset. * Where 2.4 and early 2.6 kernels kept dirty/clean pages in per-address_space * lists, we instead now tag pages as dirty/writeback in the radix tree. * * All pagecache pages may be subject to I/O: * - inode pages may need to be read from disk, * - inode pages which have been modified and are MAP_SHARED may need * to be written back to the inode on disk, * - anonymous pages (including MAP_PRIVATE file mappings) which have been * modified may need to be swapped out to swap space and (later) to be read * back into memory. *//* * The zone field is never updated after free_area_init_core() * sets it, so none of the operations on it need to be atomic. *//* * page->flags layout: * * There are three possibilities for how page->flags get * laid out. The first is for the normal case, without * sparsemem. The second is for sparsemem when there is * plenty of space for node and section. The last is when * we have run out of space and have to fall back to an * alternate (slower) way of determining the node.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -