linux内存管理之页描述符

  • Post author:
  • Post category:linux


数据结构定义

struct page *mem_map;
是一个数组,每个元素对应一个页框,长度为32个字节,由于lru的存在,mem_map既是数组,又是链表

定义如下:

struct address_space;

/*
 * Each physical page in the system has a struct page associated with
 * it to keep track of whatever it is we are using the page for at the
 * moment. Note that we have no way to track which tasks are using
 * a page, though if it is a pagecache page, rmap structures can tell us
 * who is mapping it.
 */ 
struct page { //与物理页相关
	unsigned long flags;		/* Atomic flags, some possibly
					 * updated asynchronously */表示页框当前的状态
	atomic_t _count;		/* Usage count, see below. */页引用计数器,-1表示空闲,>=0的话表示被使用
	union {
		atomic_t _mapcount;	/* Count of ptes mapped in mms,
					 * to show when page is mapped
					 * & limit reverse map searches.
					 */
		struct {	/* SLUB uses */
			short unsigned int inuse;
			short unsigned int offset;
		};
	};
	union {
	    struct {
		unsigned long private;		/* Mapping-private opaque data:
					 	 * usually used for buffer_heads
						 * if PagePrivate set; used for
						 * swp_entry_t if PageSwapCache;
						 * indicates order in the buddy
						 * system if PG_buddy is set.
						 */
		struct address_space *mapping;	/* If low bit clear, points to
						 * inode address_space, or NULL.
						 * If page mapped as anonymous
						 * memory, low bit is set, and
						 * it points to anon_vma object:
						 * see PAGE_MAPPING_ANON below.
						 */
	    };
#if NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS
	    spinlock_t ptl;
#endif
	    struct {			/* SLUB uses */
	    	void **lockless_freelist;
		struct kmem_cache *slab;	/* Pointer to slab */
	    };
	    struct {
		struct page *first_page;	/* Compound pages */
	    };
	};
	union {
		pgoff_t index;		/* Our offset within mapping. */
		void *freelist;		/* SLUB: freelist req. slab lock */
	};
	struct list_head lru;		/* Pageout list, eg. active_list
					 * protected by zone->lru_lock !
					 */ page通过lru链接,page空闲的时候,lru指针做这个用途。当在buddy伙伴的空闲链表里,通过lru以order为单位串联
	/*
	 * On machines where all RAM is mapped into kernel address space,
	 * we can simply calculate the virtual address. On machines with
	 * highmem some memory is mapped into kernel virtual memory
	 * dynamically, so we need a place to store that address.
	 * Note that this field could be 16 bits on x86 ... ;)
	 *
	 * Architectures with slow multiplication can define
	 * WANT_PAGE_VIRTUAL in asm/page.h
	 */
#if defined(WANT_PAGE_VIRTUAL)
	void *virtual;			/* Kernel virtual address (NULL if
					   not kmapped, ie. highmem) */
#endif /* WANT_PAGE_VIRTUAL */
};

元素count的详细说明

count元素通过如下函数可以获取
static inline int page_count(struct page *page)
{
	return atomic_read(&compound_head(page)->_count);
}

flag元素的详细说明

#define PG_locked	 	 0	/* Page is locked. Don't touch. */
#define PG_error		 1
#define PG_referenced		 2
#define PG_uptodate		 3

#define PG_dirty	 	 4
#define PG_lru			 5
#define PG_active		 6
#define PG_slab			 7	/* slab debug (Suparna wants this) */

#define PG_owner_priv_1		 8	/* Owner use. If pagecache, fs may use*/
#define PG_arch_1		 9
#define PG_reserved		10
#define PG_private		11	/* If pagecache, has fs-private data */

#define PG_writeback		12	/* Page is under writeback */
#define PG_compound		14	/* Part of a compound page */
#define PG_swapcache		15	/* Swap page: swp_entry_t in private */

#define PG_mappedtodisk		16	/* Has blocks allocated on-disk */
#define PG_reclaim		17	/* To be reclaimed asap */
#define PG_buddy		19	/* Page is free, on buddy lists */

/* PG_readahead is only used for file reads; PG_reclaim is only for writes */
#define PG_readahead		PG_reclaim /* Reminder to do async read-ahead */

/* PG_owner_priv_1 users should have descriptive aliases */
#define PG_checked		PG_owner_priv_1 /* Used by some filesystems */
#define PG_pinned		PG_owner_priv_1	/* Xen pinned pagetable */

设置和获取标记的方法
#define PageLocked(page)		\
		test_bit(PG_locked, &(page)->flags)
#define SetPageLocked(page)		\
		set_bit(PG_locked, &(page)->flags)


#define pte_page(x) pfn_to_page(pte_pfn(x))
x是页表项值,通过pte_pfn得到其对应的物理页框号,最后通过pfn_to_page得到对应的物理页描述符

follow_page
	=>pgd = pgd_offset(mm, address);
	=>pud = pud_offset(pgd, address);
	=>pmd = pmd_offset(pud, address);
	=>page = vm_normal_page(vma, address, pte);
		=>unsigned long pfn = pte_pfn(pte);
		=>return pfn_to_page(pfn);

linux内核研究笔记(一) – page介绍

https://blog.csdn.net/chenliujiang1989/article/details/18263419



版权声明:本文为shipinsky原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。