Linux kernel: Role of zero page allocation at pagi

2020-04-13 04:18发布

问题:

I am trying to understand the kernel memory reservation at bootup for arch/arm.

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);
}

Can someone please explain the role of zero page?

This question is a part of this.

回答1:

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.

p.s. Note that COW is not directly connected with zero pages, it is a more wide and general concept

addition from @artless_noise:

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.