天天看点

linux内核源码解析–page数据结构

作者:内核中文社区

几个问题:

1.当开启了MMU之后,CPU访问内存的最小单位是多少呢?

page

2.linux怎样描述这个页呢?

3.linux内核里,怎么理解和使用这个页?

linux内核用stuct page来描述一个物理页面:

1.简化版的page结构体

/*
 * page描述一个物理页面
 */
struct page {
    unsigned long flags;
    atomic_t      _refcount;
    atomic_t      _mapcount;
    unsigned long private;
    struct address_space *mapping; 
    pgoff_t       index;
    struct list_head lru;
    void *        *virtual;
} _struct_page_alignment;           

flags字段:

enum pageflags {
    PG_locked,      /* Page is locked. Don't touch. */ ///表示页面已经上锁;如果该比特位置位,说明已经被锁,内存管理其他模块不能访问这个页面,防止竞争
    PG_referenced, ///同PG_active一起,用于控制页面的活跃程度,在kswapd页面回收中使用;
    PG_uptodate,   ///表示页面的数据已经从块设备成功读取到内存页面;
    PG_dirty,      ///表示页面内容发生改变,这个页面为脏的,即页面内容被改写,还没同步到外部存储器
    PG_lru,        ///表示页面加入了LRU链表中,内核使用LRU链表来管理活跃和不活跃页面;
    PG_active,
    PG_workingset,
    PG_waiters,     /* Page has waiters, check its waitqueue. Must be bit #7 and in the same byte as "PG_locked" */
    PG_error,       /////表示页面操作过程中发生错误时会设置该位;
    PG_slab,        ///页面用于slab分配器
    PG_owner_priv_1,    /* Owner use. If pagecache, fs may use*/
    PG_arch_1,
    PG_reserved,
    PG_private,     /* If pagecache, has fs-private data */
    PG_private_2,       /* If pagecache, has fs aux data */
    PG_writeback,       /* Page is under writeback */  ///表示页面的内容正在向块设备进行会写
    PG_head,        /* A head page */
    PG_mappedtodisk,    /* Has blocks allocated on-disk */
    PG_reclaim,     /* To be reclaimed asap */           ///表示这个页面马上要被回收
    PG_swapbacked,      /* Page is backed by RAM/swap */ ///表示页面具有swap缓存功能,通过匿名页面才可以写回swap分区
    PG_unevictable,     /* Page is "unevictable"  */     ///表示这个页面不能回收
#ifdef CONFIG_MMU
    PG_mlocked,     /* Page is vma mlocked */   ///表示页面对应的vma处于mlocked状态;
#endif
#ifdef CONFIG_ARCH_USES_PG_UNCACHED
    PG_uncached,        /* Page has been mapped as uncached */
#endif
#ifdef CONFIG_MEMORY_FAILURE
    PG_hwpoison,        /* hardware poisoned page. Don't touch */
#endif
#if defined(CONFIG_IDLE_PAGE_TRACKING) && defined(CONFIG_64BIT)
    PG_young,
    PG_idle,
#endif
#ifdef CONFIG_64BIT
    PG_arch_2,
#endif
    __NR_PAGEFLAGS,
 
    /* Filesystems */
    PG_checked = PG_owner_priv_1,
 
    /* SwapBacked */
    ///表示页面处于交换缓存
    PG_swapcache = PG_owner_priv_1, /* Swap page: swp_entry_t in private */
 
    /* Two page bits are conscripted by FS-Cache to maintain local caching
     * state.  These bits are set on pages belonging to the netfs's inodes
     * when those inodes are being locally cached.
     */
    PG_fscache = PG_private_2,  /* page backed by cache */
 
    /* XEN */
    /* Pinned in Xen as a read-only pagetable page. */
    PG_pinned = PG_owner_priv_1,
    /* Pinned as part of domain save (see xen_mm_pin_all()). */
    PG_savepinned = PG_dirty,
    /* Has a grant mapping of another (foreign) domain's page. */
    PG_foreign = PG_owner_priv_1,
    /* Remapped by swiotlb-xen. */
    PG_xen_remapped = PG_owner_priv_1,
 
    /* SLOB */
    PG_slob_free = PG_private,
 
    /* Compound pages. Stored in first tail page's flags */
    PG_double_map = PG_workingset,
 
    /* non-lru isolated movable page */
    PG_isolated = PG_reclaim,
 
    /* Only valid for buddy pages. Used to track pages that are reported */
    PG_reported = PG_uptodate,
};           
linux内核源码解析–page数据结构

需要的小伙伴私信回复内核免费领取

flags布局

flags成员除了上述重要标志位之外,还有个重要作用,就是存放SECTION编号,node节点编号,zone编号等;

比如在ARM Vexpress平台中page->flags布局图,bit[0:43]用来存放页面标志位,bit[44:59]用于NUMA平很算法中的LAST_CPUPID,bit[60:61]用于存放zone编号bit[62:63]存放node编号;

|63__node__62|61__zone__60|59__LAST_CPUPID__44|43__flags___0|

linux内核里一般不直接操作变量,linux提供了一些列的接口函数,尽量使用这些封装好的接口;

static inline struct zone *page_zone(const struct page *page)
    static inline void set_page_zone(struct page *page, enum zone_type zone)           

_refcount字段:

当_refcount等于0时,表示该page页面为空闲或即将要被释放的页面;

当_refcount大于0时,表示该page页面已经被分配且内核正在使用,暂时不会被释放

static inline void get_page(struct page *page)  ///_refcount加一
static inline void put_page(struct page *page) ///_refcount减一,若_refcount==0,会释放该页面
static inline int page_count(struct page *page)     ///统计_count个数           

_refcount使用场景

(1)初始状态,空闲页面,_refcount=0;

(2)分配页面时,_refcount会变成1;

(3)加入LRU链表时,页面会被kswapd内核线程使用,_refcount会加1;

添加到LRU链表后,_refcount减1,防止页面在添加到LRU过程中被释放;

(4)被映射到其他用户进程的PTE时,_refcount会加1.

(5)页面的private成员指向私有数据;

对于PG_swapable的页面,__add_to_swap_cache()增加_refcount;

对于PG_private页面,主要在块设备的buffer_head中使用,如buffer_migrate_page()会增加_refcount;

(6)内核对页表进行操作等关键路径上也会使_refcount加1.

_mapcount字段:

_mapcount引用计数,表示这个页面被进程映射的个数,即已经映射了多少个用户pte页表。

在ARM64的Linux内核中,每个用户进程都拥有一个独立的虚拟地址空间和独立的页表,所以有可能出现多个用户进程虚拟地址映射到一个物理页面,RMAP反向映射系统就是利用这个特性来实现的。

_mapcount引用计数,主要用于RMAP反向映射系统中;

_mapcount**-1,表示没有pte映射到页面中;**

_mapcount0,表示只有父进程映射了页面,匿名页面刚分配时,_mapcount引用计数初始化为0;

访问:

page_dup_rmap();///增加_refcount
static inline int page_mapcount(struct page *page); ///统计_mapcount个数           

mmaping字段:

对于匿名页面,mapping指向VMA的anon_vma结构;

对于交换高速缓存页面,mapping指向交换分区的swapper_spaces;

对于文件映射页面,mapping指向该文件所属的address_space结构,它包含文件所属的介质相关信息,如inode节点,节点对应操作方法等;

address_space为8字节对齐,低两位用于其它用途:

bit[0]:判断该页面是否为匿名页面;

bit[1]:判断该页面是否为非LRU页面;

bit[01]0b11,表示这是一个KSM页面;

static __always_inline int PageAnon(struct page *page)        ///判断是否为匿名页面
static __always_inline int __PageMovable(struct page *page)   ///判断是否为非LRU页面
static __always_inline int PageKsm(struct page *page)         ///判断是否为KSM页面
 
void *page_rmapping(struct page *page)                        ///返回mapping成员,清除低2位;
struct address_space *page_mapping(struct page *page)      ///返回mapping成员指向的地址空间
bool page_mapped(struct page *page)                         ///判断该页面是否映射到用户PTE           

lru字段:

用在页面回收的LRU链表算法中,LRU链表算法定义了多个链表;

用来把一个slab添加到slab满链表、slab空闲链表或slab部分链表中;

virtual字段:

一个指向页所对应的虚拟地址的指针;

页面锁PG_Locked:

struct page数据结构成员flags定义了一个标志PG_locked;内核通常用PG_locked来设置一个页面锁;

static inline void lock_page(struct page *page)  ///用于申请页面锁,如果页面锁被其他进程占用,那么会睡眠等待;
 
static inline int trylock_page(struct page *page)   ///如果返回false表示获取锁失败,返回true表示获取锁成功;不会睡眠           

2.struct page 的意义;

内核使用struct page来描述一个物理页面,我们看到了管理这些页面的信息,比如:

(1)内核知道当前这个页面的状态(通过flags字段);

(2)内核需要知道一个页面是否空闲,即有没有分配出去,有多少个进程(_count)或内存路径访问了这个页面(_mapcount);

(3)内核知道谁在使用这个页面,使用者是用户空间进程的匿名页,还是page cache(mapping);

(4)内核知道这个页面是否被slab即使使用(lru, s_mem等字段);

(5)内核知道这个页面是否线性映射(virtual);

3.struct page数据结构存放在哪里?

page存放在一个全局数组mem_map[]中;

linux内核源码解析–page数据结构

注意:存放的是struct page结构体,不是指针;

4.总结

Linux内核的内存管理以page页面为核心,struct page数据结构提供了很多字段,其中_refcount和_mapcount是两个非常重要的引用计数,正确理解它们是理解Linux内核内存管理的基石。

_refcount是page页面的“命根子”;

_mapcount是page页面的“幸福指数”

struct page是Linux内核最重要的数据结构之一,想深入研究Linux内存管理,有必要慢慢研究struct page中重要成员的含义和用法。

继续阅读