Linux – How extended (4MB) and regular (4KB) paging coexist

linux-kernelmemoryvirtual-memory

On x86 architecture, "Extended paging" allows page frames (physical memory) to be 4 MB instead of 4 KB.

The book "Understanding the Linux Kernel, 3rd" at chapter 2 "Memory Addressing", sub-chapter "Paging in Linux", section "Kernel Page Tables", explains that for the final kernel Page Table when RAM size is less than 896 MB:

[…]the kernel can address the RAM by making use of large pages (see the section
"Extended Paging" earlier in this chapter).

However, in the section "Extended Paging" (sub-chapter "Paging in Hardware"), it is written :

Extended paging coexists with regular paging.

I don't actually get how extended and regular paging coexist. Can please someone explain those questions:

  • In which cases the kernel uses 4 MB pages? or 4 KB pages?
  • Which page frame size will be used for kmalloc operations? for vmalloc?
  • If we assume that the initial code & data (kernel's segments, provisional Page Tables, and 128 KB for dynamic data) fit in the first 8 MB of RAM (as the example given by the book), what if the real amount of code & data is 5MB only: will the kernel waste 8 – 5 = 3 MB?

Best Answer

1. In which cases the kernel uses 4 MB pages? or 4 KB pages?

Application can apply for huge page, kernel will not determine the page size unless compile the PAGE_SIZE into the kernel source code. Using mmap flags can determine the page size in the source code of application.

2. Which page frame size will be used for kmalloc operations? for vmalloc?

kmalloc use default page size in Linux kernel, that is PAGE_SIZE in the kernel, which is compiled or runtime determined. Same for vmalloc.

3. If we assume that the initial code & data (kernel's segments, provisional Page Tables, and 128 KB for dynamic data) fit in the first 8 MB of RAM (as the example given by the book), what if the real amount of code & data is 5MB only: will the kernel waste 8 - 5 = 3 MB?

The size of waste memory is determined by PAGE_SIZE and the data, if page size is 4MB, data is 5MB, the wasted memory size will be (PAGE_SIZE*N) - 5MB= 3MB.

Related Question