Linux节点和内存管理区的初始化


节点和管理区是内存管理中所涉及的重要概念,其数据结构在前文《linux物理内存概述》中已经介绍,现在让我们来看看linux是如何完成节点和管理区的。

在内核首先通过setup_arch()-->paging_init()-->zone_sizes_init()来初始化节点和管理区的一些数据项

  1. static void __init zone_sizes_init(void)  
  2. {  
  3.     unsigned long max_zone_pfns[MAX_NR_ZONES];  
  4.     memset(max_zone_pfns, 0, sizeof(max_zone_pfns));  
  5.   
  6.     /*分别获取三个管理区的页面数*/  
  7.     max_zone_pfns[ZONE_DMA] =  
  8.         virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;  
  9.     max_zone_pfns[ZONE_NORMAL] = max_low_pfn;  
  10. #ifdef CONFIG_HIGHMEM   
  11.     max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;  
  12. #endif   
  13.   
  14.     free_area_init_nodes(max_zone_pfns);  
  15. }  

在获取了三个管理区的页面数后,通过free_area_init_nodes()来完成后续工作

  1. void __init free_area_init_nodes(unsigned long *max_zone_pfn)  
  2. {  
  3.     unsigned long nid;  
  4.     int i;  
  5.   
  6.     /* Sort early_node_map as initialisation assumes it is sorted */  
  7.     sort_node_map();/*将所有节点按起始页框号排序*/  
  8.   
  9.     /* Record where the zone boundaries are */  
  10.     /*记录三个管理区的边界*/  
  11.     memset(arch_zone_lowest_possible_pfn, 0,  
  12.                 sizeof(arch_zone_lowest_possible_pfn));  
  13.     memset(arch_zone_highest_possible_pfn, 0,  
  14.                 sizeof(arch_zone_highest_possible_pfn));  
  15.     arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();  
  16.     arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];  
  17.     for (i = 1; i < MAX_NR_ZONES; i++) {  
  18.         if (i == ZONE_MOVABLE) /*不处理ZONE_MOVABLE*/  
  19.             continue;  
  20.         /*将下一个管理区的起始页框置为上一个管理区的结束页框*/  
  21.         arch_zone_lowest_possible_pfn[i] =  
  22.             arch_zone_highest_possible_pfn[i-1];  
  23.         arch_zone_highest_possible_pfn[i] =  
  24.             max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);  
  25.     }  
  26.     arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;  
  27.     arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;  
  28.   
  29.     /* Find the PFNs that ZONE_MOVABLE begins at in each node */  
  30.     memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));  
  31.     find_zone_movable_pfns_for_nodes(zone_movable_pfn);  
  32.   
  33.     /* Print out the zone ranges */  
  34.     printk("Zone PFN ranges:\n");  
  35.     for (i = 0; i < MAX_NR_ZONES; i++) {  
  36.         if (i == ZONE_MOVABLE)  
  37.             continue;  
  38.         printk("  %-8s %0#10lx -> %0#10lx\n",  
  39.                 zone_names[i],  
  40.                 arch_zone_lowest_possible_pfn[i],  
  41.                 arch_zone_highest_possible_pfn[i]);  
  42.     }  
  43.   
  44.     /* Print out the PFNs ZONE_MOVABLE begins at in each node */  
  45.     printk("Movable zone start PFN for each node\n");  
  46.     for (i = 0; i < MAX_NUMNODES; i++) {  
  47.         if (zone_movable_pfn[i])  
  48.             printk("  Node %d: %lu\n", i, zone_movable_pfn[i]);  
  49.     }  
  50.   
  51.     /* Print out the early_node_map[] */  
  52.     printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);  
  53.     for (i = 0; i < nr_nodemap_entries; i++)  
  54.         printk("  %3d: %0#10lx -> %0#10lx\n", early_node_map[i].nid,  
  55.                         early_node_map[i].start_pfn,  
  56.                         early_node_map[i].end_pfn);  
  57.   
  58.     /* Initialise every node */  
  59.     mminit_verify_pageflags_layout();  
  60.     setup_nr_node_ids();  
  61.     for_each_online_node(nid) {/*遍历每个节点*/  
  62.         pg_data_t *pgdat = NODE_DATA(nid);  
  63.         /*初始化节点*/  
  64.         free_area_init_node(nid, NULL,  
  65.                 find_min_pfn_for_node(nid), NULL);  
  66.   
  67.         /* Any memory on that node */  
  68.         if (pgdat->node_present_pages)  
  69.             node_set_state(nid, N_HIGH_MEMORY);  
  70.         check_for_regular_memory(pgdat);  
  71.     }  
  72. }  
  • 1
  • 2
  • 3
  • 4
  • 下一页

相关内容