页描述符 。内核使用页描速符来跟踪管理物理内存 。每个物理页框都用一个页描遂符表示 。页描述符用struct page的结构描遂 include/linux/mm_types.h / 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. */ struet page unsigned long flags:Atomic flags,some possibly updated asynchronously atomic_t_count:/Usage count,see below./ struct list_head Iru:Pageout list,eg.active_list ·protected by zone->lru_lock!·/ 1口4四1是4P刀00 陈香兰(x1 anchenoustc,edu,cn)(计算丸应【inux操作系统分新Chapter8内存管理 0 ctober31.20149/47
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 页描述符 . 内核使用页描述符来跟踪管理物理内存 每个物理页框都用一个页描述符表示 页描述符用struct page的结构描述 . include/linux/mm_types.h . . /* * 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. */ ... struct list_head lru; /* Pageout list, eg. active_list * protected by zone->lru_lock ! */ ... } 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 8 内存管理 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) October 31, 2014 9 / 47
页描述符 。所有物理页框描述符以(物理页号)自然序组织在mem map数组中 em_mapl」 page0 page 1 page 2 page 3 page 4 page 5 page6 page N physical memory mm/memory.c #ifndef CONFIG_NEED_MULTIPLE_NODES use the per-pgdat data instead for discontigmem-mbligh unsigned long max_mapnr; struct page "mem_map: EXPORT_SYMBOL(max_mapnr); EXPORT_SYMBOL(mem_map): #endif 4口4四是4P在刀0C 陈香兰(x1 anchenoustc,edu.en)(计界丸应Linx操件系统分折Chapter8内存管理 0 ctober31.20149/47
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 页描述符 . 所有物理页框描述符以(物理页号)自然序组织在mem_map数组中 physical memory mem_map[] · · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · ·· · · page 0 1 2 3 4 5 6 page page page page page page · · · page N . mm/memory.c . . #ifndef CONFIG_NEED_MULTIPLE_NODES /* use the per-pgdat data instead for discontigmem - mbligh */ unsigned long max_mapnr; struct page *mem_map; EXPORT_SYMBOL(max_mapnr); EXPORT_SYMBOL(mem_map); #endif 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 8 内存管理 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) October 31, 2014 9 / 47
页描述符 ●mem_map数组的空间分配和初始化,mm/page_al1oc.c static void_init_refok alloc_node_mem_map(struct pglist_data "pgdat){ /Skip empty nodes " if (!pgdat->node_spanned_pages) return; #ifdef CONFIG_FLAT_NODE_MEM_MAP ia64 gets its own node_mem_map.before this,without bootmem"/ if (!pgdat->node_mem_map){ unsigned long size,start,end: struct page "map: /P *The zone's endpoints aren't required to be MAX_ORDER aligned but the node mem_map endpoints must be in order for the buddy allocator to function correctly. / start pgdat->node_start_pfn &(MAX_ORDER_NR_PAGES -1): end pgdat->node_start_pfn+pgdat->node_spanned_pages: end ALIGN(end,MAX_ORDER_NR_PAGES): size =(end -start).sizeof(struct page): map alloc_remap(pgdat->node_id,size): 4口4四是42刀00 陈香兰(x1 anchenoustc,edu.cn)(计算丸应【inux操作系统分斯Chapter8内存管理 0 ctober31.20149/47
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 页描述符 . mem_map数组的空间分配和初始化,mm/page_alloc.c . . static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat) { /* Skip empty nodes */ if (!pgdat->node_spanned_pages) return; #ifdef CONFIG_FLAT_NODE_MEM_MAP /* ia64 gets its own node_mem_map, before this, without bootmem */ if (!pgdat->node_mem_map) { unsigned long size, start, end; struct page *map; /* * The zone’s endpoints aren’t required to be MAX_ORDER * aligned but the node_mem_map endpoints must be in order * for the buddy allocator to function correctly. */ start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1); end = pgdat->node_start_pfn + pgdat->node_spanned_pages; end = ALIGN(end, MAX_ORDER_NR_PAGES); size = (end - start) * sizeof(struct page); map = alloc_remap(pgdat->node_id, size); 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 8 内存管理 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) October 31, 2014 9 / 47
页描述符 if (!map) map alloc_bootmem_node(pgdat,size); pgdat->node_mem_mapmap (pgdat->node_start_pfn -start); } #ifndef CONFIG_NEED_MULTIPLE_NODES / With no DISCONTIG.the global mem_map is just set as node 0's */ if (pgdat =NODE_DATA(0)){ mem_map NODE DATA(0)->node_mem_map: #endif #endif CONFIG_FLAT_NODE_MEM_MAP/ 1口t4四1是42刀00 陈香兰(x1 anchenoustc,edu,cn)(计算丸应Linux操作系统分折Chapter8内存管理 0 ctober31.20149/47
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 页描述符 . . . if (!map) map = alloc_bootmem_node(pgdat, size); pgdat->node_mem_map = map + (pgdat->node_start_pfn - start); } #ifndef CONFIG_NEED_MULTIPLE_NODES /* * With no DISCONTIG, the global mem_map is just set as node 0’s */ if (pgdat == NODE_DATA(0)) { mem_map = NODE_DATA(0)->node_mem_map; ... } #endif #endif /* CONFIG_FLAT_NODE_MEM_MAP */ } 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 8 内存管理 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) October 31, 2014 9 / 47
页描述符 ·页描述符中的各个字段将在用到时再逐步介绍,首先看一下 。count:页的使用引用计数器 。0:空闲 ●>0:页已经分配给一个或多个进程或用户某些内核数据结构 。f1ags:页框状态,最多可以有32个,每个使用一个位表示 1口t4四1是4200 陈香兰(x1 anchenoustc,edu,cn)(计算丸应【inux操作系统分斯Chapter8内存管理 0 ctober31.20149/47
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 页描述符 . 页描述符中的各个字段将在用到时再逐步介绍,首先看一下 count:页的使用引用计数器 0:空闲 >0:页已经分配给一个或多个进程或用户某些内核数据结构 flags:页框状态,最多可以有32个,每个使用一个位表示 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 8 内存管理 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) October 31, 2014 9 / 47