Linux如何为其物理分配器分配内存? [英] How does Linux allocate memory for its physical allocator?
问题描述
我最近想研究Linux内存管理的细节,因为我想为自己的玩具内核实现类似的东西,所以我希望熟悉这些细节的人能帮助我理解一件事.显然,物理内存管理器是一种伙伴算法,该算法还专门用于返回特定顺序的页面块(0至9,其中0只是单个页面).对于每个顺序,块都存储为链接列表.假设如果请求了5阶块,但未在5阶列表中找到该算法,则算法将搜索6阶块,将其分成两部分,给出所请求的一半,然后将另一半移低一个阶(如它只有一半大小). 我没有得到的是内核如何存储这些结构,或者它如何为它们分配空间.由于对于订单0页面,您将需要1M条目(每个页面都是4KiB页面),这是否意味着内核分配了1MiB * sizeof(结构页面)?那么1级以上的块呢?内核是否通过将已分配的块标记为更高的顺序来重用分配的块,并且当需要将其分成两部分时,只需返回该块并获得未使用的块?
I was recently delving into the details of Linux's memory management as I want to implement something similar for my own toy kernel, so I was hoping if someone who's familiar with the details could help me understand one thing. Apparently the physical memory manager is a buddy algorithm, which is further specialised to return blocks of pages of a particular order (0 to 9, with 0 being just a single page). For each order the blocks are stored as a linked list. Say if a block of order 5 is requested but is not found on the list of order 5 blocks, the algorithm searches for a block in order 6, splits it into two, gives the requested half and moves the other half an order lower (as it is half in size). What I don't get is how the kernel stores these structures, or how it allocates space for them. Since for order 0 pages you would need 1M entries (each is a 4KiB page), does it mean that the kernel allocates 1MiB * sizeof(struct page)? What about the blocks of order 1 and above? Does the kernel reuse allocated blocks by marking them as a higher order, and when it needs to split it in two just return the block and get one that is unused?
推荐答案
我没有得到的是内核如何存储这些结构,或者它如何为它们分配空间.由于对于订单0页面,您将需要1M条目(每个页面都是4KiB页面),这是否意味着内核分配了1MiB * sizeof(结构页面)?
What I don't get is how the kernel stores these structures, or how it allocates space for them. Since for order 0 pages you would need 1M entries (each is a 4KiB page), does it mean that the kernel allocates 1MiB * sizeof(struct page)?
通过调用 paging_init()
(arch/x86/mm/init_32.c;一些说明- https://www.kernel.org/doc/gorman/html/understand/understand005.html 2.3区域初始化和 setup_arch()
通过( native_pagetable_init()
和间接调用1166 x86_init.paging.pagetable_init();
):
Initialization of zones is done by calling paging_init()
(arch/x86/mm/init_32.c; some descriptions - https://www.kernel.org/doc/gorman/html/understand/understand005.html 2.3 Zone Initialisation and http://repo.hackerzvoice.net/depot_madchat/ebooks/Mem_virtuelle/linux-mm/vminit.html Initializing the Kernel Page Tables) from setup_arch()
via (native_pagetable_init()
and indirect call 1166 x86_init.paging.pagetable_init();
):
690 /*
691 * paging_init() sets up the page tables - note that the first 8MB are
692 * already mapped by head.S.
...*/
697 void __init paging_init(void)
698 {
699 pagetable_init();
...
711 zone_sizes_init();
712 }
pagetable_init()
在1024个pgd_t
s的swapper_pg_dir
数组中创建内核页表.
pagetable_init()
creates kernel page tables in swapper_pg_dir
array of 1024 pgd_t
s.
zone_sizes_init()
实际上定义了物理内存区域,并调用 free_area_init_nodes()
来初始化它们,并在for_each_online_node(nid) {...}) "rel =" nofollow> free_area_init_node()
,它调用三个函数:
zone_sizes_init()
actually defines zones of physical memory and calls free_area_init_nodes()
to initialize them with actual work done (for each NUMA node for_each_online_node(nid) {...}
) in free_area_init_node()
which calls three functions:
-
calculate_node_totalpages()
打印dmesg中每个节点的页数 -
alloc_node_mem_map()
做实际的工作为该节点中的每个物理页面分配struct page
的方法;它们的内存由bootmem分配器 doc1 doc2 (您可以使用bootmem_debug=1
内核启动选项查看其调试) :
calculate_node_totalpages()
prints page counts for every node in dmesgalloc_node_mem_map()
does actual job of allocatingstruct page
for every physical page in this node; memory for them is allocated by bootmem allocator doc1 doc2 (you can see its debug withbootmem_debug=1
kernel boot option):
4936 size = (end - start) * sizeof(struct page);
4937 map = alloc_remap(pgdat->node_id, size);
if (!map) map = memblock_virt_alloc_node_nopanic(size, pgdat->node_id);
-
free_area_init_core()
(带填充struct zone
中的位图数量) .在 http:/中为较早版本的内核描述的free_area_init_core
功能. /repo.hackerzvoice.net/depot_madchat/ebooks/Mem_virtuelle/linux-mm/zonealloc.html#INITIALIZE 为:
free_area_init_core()
(with filling of bitmaps instruct zone
). Functionality offree_area_init_core
described for older kernels in http://repo.hackerzvoice.net/depot_madchat/ebooks/Mem_virtuelle/linux-mm/zonealloc.html#INITIALIZE as:
free_area_init_core()
在free_area_init_core()中构建内存映射,并初始化空闲列表和伙伴位图.
free_area_init_core()
The memory map is built, and the freelists and buddy bitmaps initialized, in free_area_init_core().
初始化每个区域中的免费订单列表,并将订单标记为没有任何空闲页面:free_area_init_core()
-> zone_init_free_lists
:
Free lists of orders in each zone are initialized and orders are marked as having no any free page: free_area_init_core()
-> init_currently_empty_zone()
-> zone_init_free_lists
:
4147 static void __meminit zone_init_free_lists(struct zone *zone)
4148 {
4149 unsigned int order, t;
4150 for_each_migratetype_order(order, t) {
4151 INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
4152 zone->free_area[order].nr_free = 0;
4153 }
4154 }
PS: There is init() in kernel, it is called start_kernel()
, and LXR (Linux cross-reference) will help you to navigate between functions (I posted links to lxr.free-electrons.com, but there are several online LXRs):
501 asmlinkage __visible void __init start_kernel(void)
...
528 boot_cpu_init();
529 page_address_init();
530 pr_notice("%s", linux_banner);
531 setup_arch(&command_line);
这篇关于Linux如何为其物理分配器分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!