Linux – How is the page cache mapped in the kernel on 64-bit x86 architectures

linuxlinux-kernelmemory managementmmap

On a modern 64-bit x86 Linux, how is the mapping between virtual and physical pages set up, kernel side? On the user side, you can mmap in pages from the page cache, and this will map 4K pages in directly into user space – but I am interesting in how the pages are mapped in the kernel side.

Does it make use of the "whole ram identity mapping" or something else? Is that whole ram identity mapping generally using 1GB pages?

Best Answer

On a modern 64-bit x86 Linux?

Yes. It calls kmap() or kmap_atomic(), but on x86-64 these will always use the identity mapping. x86-32 has a specific definition of it, but I think x86-64 uses a generic definition in include/linux/highmem.h.

And yes, the identity mapping uses 1GB hugepages.

LWN article which mentions kmap_atomic.

I found kmap_atomic() by looking at the PIO code.[*]

Finally, when read() / write() copy data from/to the page cache:

generic_file_buffered_read -> copy_page_to_iter -> kmap_atomic() again.


[*] I looked at PIO, because I realized that when performing DMA to/from the page cache, the kernel could avoid using any mapping. The kernel could just resolve the physical address and pass it to the hardware :-). (Subject to IOMMU). Although, the kernel will need a mapping if it wants to checksum or encrypt the data first.

Related Question