Linux 内核:在 paging_init 时零页分配的作用
我正在尝试了解 arch/arm 启动时的内核内存预留.
I am trying to understand the kernel memory reservation at bootup for arch/arm.
在setup_arch()
中有一个调用 paging_init() 用于设置页表、区域内存映射的初始化等.在分配实际的mem_map
之前,它还分配了一个零页
.
There's a call paging_init() for setting page tables, initialization of zone memory map etc in setup_arch()
. It also allocate one zero page
before allocating actual mem_map
.
void __init paging_init(const struct machine_desc *mdesc)
{
void *zero_page;
---
zero_page = early_alloc(PAGE_SIZE);
---
empty_zero_page = virt_to_page(zero_page);
__flush_dcache_page(NULL, empty_zero_page);
}
谁能解释一下零页
的作用?
这个问题是这个的一部分.
零页是用零填充的页.您可以映射到此页面并获得宽归零的虚拟区域.每当您写入此页面之一时,COW 都会起作用,您将获得一个新的.反之亦然:如果您有一个零数据的内存区域,您可以将此数据映射到零页并用0"数据释放这些页.换句话说,这是关于内核如何节省内存的.
Zero page is a page filled with zeros. You can make a mapping to this page and get wide zeroed virtual region. Whenever you will write to one of this pages the COW will work and you will get a new one. The reverse is true too: if you have a memory region with zero data, you can map this data to zero page and free these pages with "0" data. In other words that is about how kernel can save memory.
附言注意COW与零页没有直接联系,它是一个更广泛和更笼统的概念
p.s. Note that COW is not directly connected with zero pages, it is a more wide and general concept
来自@artless_noise 的补充:
它还允许分配大数组,但不消耗内存.所有页面最初都是零页面并映射到相同的物理零页面.如果数组是稀疏的,那么只有少数条目(4k 大小)会消耗内存.内核不需要清理(零)分配的内存.'tlb' 和 'cache' 不会浪费在填充条目上.
It also allows a large array to be allocated, but not consume memory. All pages are initially the zero page and map to the same physical zero page. If the array is sparse, then only the few entries (in 4k size) will consume memory. The kernel doesn't need to sanitize (zero) allocated memory. The 'tlb' and 'cache' are not wasted in filling entries.