https://www.cnblogs.com/tolimit/p/5287801.html
在zone_sizes_init 之后,各个node,zone'的page总数已知。但是此时的每个order的空闲链表是空的,也就是无法通过alloc_page这种接口来分配。此时page还在memblock管控,需要memblock释放。释放的函数是free_low_memory_core_early:
mm_init //init/main.c
mem_init(void) //arch/arm64/mm/init.c
memblock_free_all();
void __init memblock_free_all(void)
{
unsigned long pages;
free_unused_memmap();
reset_all_zones_managed_pages();
pages = free_low_memory_core_early();
totalram_pages_add(pages);
}
free_low_memory_core_early
for_each_free_mem_range
__free_memory_core(start, end);
__free_pages_memory(start_pfn, end_pfn);
memblock_free_pages(pfn_to_page(start), start, order);
__free_pages_core
__free_pages_ok
__free_one_page
在arch/arm64/mm/init.c 增加如下代码调试:
static void show_zone_info(struct zone *zone)
{
int i;
struct free_area * area;
if(!zone || !zone->name)
return;
printk("===show_zone_info :\n"
" name : %s\n"
" managed_pages:%llx\n"
" spanned_pages:%llx\n"
" present_pages:%llx\n",zone->name,zone->managed_pages,zone->spanned_pages,zone->present_pages);
for (i = 0;i<= MAX_ORDER;i++)
{
area=&zone->free_area[i];
printk(" MAX_ORDER %d of order :%d has nr_free %llx\n",MAX_ORDER,i,zone->free_area[i].nr_free);
}
}
static void show_pgdata_info(struct pglist_data *pgdat)
{
if (!pgdat)
return;
printk("===show_pgdata_info nodeid %d \n",pgdat->node_id);
struct zone *z;
for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
show_zone_info(z);
}
static void show_mem_info(const char *info)
{
printk("===show_mem_info %s\n",info);
struct pglist_data *pgdat;
for_each_online_pgdat(pgdat)
show_pgdata_info(pgdat);
}
void __init bootmem_init(void)
{
...
zone_sizes_init();
show_mem_info("after zone_sizes_init\n"); //调用
...
}
void __init mem_init(void)
{
bool swiotlb = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC))
swiotlb = true;
swiotlb_init(swiotlb, SWIOTLB_VERBOSE);
/* this will put all unused low memory onto the freelists */
memblock_free_all();
show_mem_info("after memblock_free_all\n"); //调用
.....
}
以下日志是基于6.6内核:
在memblock_free_all之前,各个NUMA的pglist_data的zone下面不同order的nr_free链表数为0:
[ 0.000000] ===show_mem_info after zone_sizes_init
[ 0.000000] ===show_pgdata_info nodeid 0
[ 0.000000] ===show_zone_info :
name : DMA
managed_pages:7a000
spanned_pages:80000
present_pages:7c000
[ 0.000000] MAX_ORDER 10 of order :0 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :1 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :2 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :3 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :4 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :5 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :6 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :7 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :8 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :9 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :10 has nr_free 0
[ 0.000000] ===show_zone_info :
name : DMA32
managed_pages:0
spanned_pages:0
present_pages:0
[ 0.000000] MAX_ORDER 10 of order :0 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :1 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :2 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :3 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :4 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :5 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :6 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :7 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :8 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :9 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :10 has nr_free 0
[ 0.000000] ===show_zone_info :
name : Normal
managed_pages:762000
spanned_pages:2680000
present_pages:780000
[ 0.000000] MAX_ORDER 10 of order :0 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :1 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :2 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :3 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :4 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :5 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :6 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :7 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :8 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :9 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :10 has nr_free 0
[ 0.000000] ===show_zone_info :
name : Movable
managed_pages:0
spanned_pages:0
present_pages:0
[ 0.000000] MAX_ORDER 10 of order :0 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :1 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :2 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :3 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :4 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :5 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :6 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :7 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :8 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :9 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :10 has nr_free 0
在memblock_free_all之后,才有,特别是order为10的情况最多,这是看空闲的组成大的buddy:
[ 0.000000] ===free_low_mwmory_core_early return pages 7c4864
[ 0.000000] ===show_mem_info after memblock_free_all
[ 0.000000] ===show_pgdata_info nodeid 0
[ 0.000000] ===show_zone_info :
name : DMA
managed_pages:6b9f1
spanned_pages:80000
present_pages:7c000
[ 0.000000] MAX_ORDER 10 of order :0 has nr_free 5
[ 0.000000] MAX_ORDER 10 of order :1 has nr_free 4
[ 0.000000] MAX_ORDER 10 of order :2 has nr_free 5
[ 0.000000] MAX_ORDER 10 of order :3 has nr_free 4
[ 0.000000] MAX_ORDER 10 of order :4 has nr_free b
[ 0.000000] MAX_ORDER 10 of order :5 has nr_free 4
[ 0.000000] MAX_ORDER 10 of order :6 has nr_free 6
[ 0.000000] MAX_ORDER 10 of order :7 has nr_free 6
[ 0.000000] MAX_ORDER 10 of order :8 has nr_free 6
[ 0.000000] MAX_ORDER 10 of order :9 has nr_free 5
[ 0.000000] MAX_ORDER 10 of order :10 has nr_free 1a9
[ 0.000000] ===show_zone_info :
name : DMA32
managed_pages:0
spanned_pages:0
present_pages:0
[ 0.000000] MAX_ORDER 10 of order :0 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :1 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :2 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :3 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :4 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :5 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :6 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :7 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :8 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :9 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :10 has nr_free 0
[ 0.000000] ===show_zone_info :
name : Normal
managed_pages:758e73
spanned_pages:2680000
present_pages:780000
[ 0.000000] MAX_ORDER 10 of order :0 has nr_free 1
[ 0.000000] MAX_ORDER 10 of order :1 has nr_free 1
[ 0.000000] MAX_ORDER 10 of order :2 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :3 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :4 has nr_free 1
[ 0.000000] MAX_ORDER 10 of order :5 has nr_free 1
[ 0.000000] MAX_ORDER 10 of order :6 has nr_free 1
[ 0.000000] MAX_ORDER 10 of order :7 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :8 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :9 has nr_free 1
[ 0.000000] MAX_ORDER 10 of order :10 has nr_free 1d63
[ 0.000000] ===show_zone_info :
name : Movable
managed_pages:0
spanned_pages:0
present_pages:0
[ 0.000000] MAX_ORDER 10 of order :0 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :1 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :2 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :3 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :4 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :5 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :6 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :7 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :8 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :9 has nr_free 0
[ 0.000000] MAX_ORDER 10 of order :10 has nr_free 0
可以通过/proc/buddyinfo实时观察系统上node中zone的nr_free链表情况:
UMA机器8G内存:
root@wzm-PC:~# cat /proc/buddyinfo
Node 0, zone DMA 11 5 5 8 19 14 12 12 7 10 458
Node 0, zone Normal 293 1684 583 238 76 34 12 7 2 9 3123
NUMA机器512G:
[root@libvirt-Manage ~]# cat /proc/buddyinfo
Node 0, zone DMA32 6 7 7 6 18 14 9 11 10 9 195
Node 0, zone Normal 6868 2968 2105 4510 1662 601 181 42 12 10 5322
Node 1, zone Normal 2396 3065 3978 4944 1420 401 114 34 4 8 5704
Node 2, zone Normal 8218 7366 10003 4542 1319 474 145 25 8 9 6051
Node 3, zone Normal 6676 3485 8483 4343 1651 764 239 63 21 16 5984
Node 4, zone Normal 10725 1148 469 1924 1224 559 223 90 18 19 5703
Node 5, zone Normal 5355 2206 2172 4719 2051 694 192 58 12 9 5867
Node 6, zone Normal 5864 2451 4543 550 26 8 7 2 2 31 6198
Node 7, zone Normal 13745 13678 9044 3810 1242 345 89 28 21 19 6171
Node 8, zone Normal 16210 14686 6900 2919 1467 816 422 54 45 15 5261
Node 9, zone Normal 12558 8541 6409 981 1590 593 211 79 13 11 6068
Node 10, zone Normal 19467 17940 10715 2690 388 68 17 10 5 3 5713
Node 11, zone Normal 17151 19618 10336 2632 165 46 22 10 7 7 5609
Node 12, zone Normal 13258 12289 11472 7480 2718 615 241 74 32 18 5689
Node 13, zone Normal 16238 18440 9468 3384 1126 431 134 45 22 40 4854
Node 14, zone Normal 12328 17060 9764 3654 1404 613 255 95 24 15 5778
Node 15, zone Normal 10606 14281 9219 3607 1312 536 167 26 5 12 6144