做人呢,最紧要就系开心啦

linux源码解析12–page数据结构

1,680次阅读
没有评论

几个问题:
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,
};

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 编号;
|63node62|61zone60|59LASTCPUPID44|43flags0|

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,表示没有用户 vma 映射到页面中;
_mapcount==0,表示只有父进程映射了页面,匿名页面刚分配时,_mapcount 引用计数初始化为 0;
_mapcount>0: 有多个用户 vma 映射该页

访问:

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

index

映射中的页偏移
文件页:页相对于文件起始位置的页偏移;
匿名页:页相对于 VMA 起始位置的页偏移;

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 源码解析 12–page 数据结构

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

4. 总结

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

_refcount 是 page 页面的“命根子”;
_mapcount 是 page 页面的“幸福指数”

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

正文完
 
admin
版权声明:本站原创文章,由 admin 2022-03-29发表,共计5472字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)