4

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.

Community
  • 1
  • 1
enfinet
  • 810
  • 8
  • 18

1 Answers1

8

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.

Alex Hoppus
  • 3,821
  • 4
  • 28
  • 47
  • 1
    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. I think that is three additional reasons, feel free to add it to your answer and I will delete my comment. – artless noise Feb 23 '16 at 13:29
  • 1
    I can understand that this technique can save cache entries as caches are indexed with physical addresses, but TLB entries will still be wasted, as TLBs are indexed with virtual addresses, right? – blaze9 Jun 15 '20 at 13:51