While "fork"ing a process, why does Linux kernel copy the content of kernel page table for every newly created process?

arm, c, linux, linux-kernel, memory-management

Solution

Each process having its own copy of page table for kernel part(higher 1GB) is to avoid L1 page table switching(i.e. avoid updating TTBR) when user/kernel land is being switched. Note that user/kernel land switch happens quite frequently.

Why avoiding updating TTBR? Details can be found here: What is the downside of updating ARM TTBR(Translate Table Base Register)?

Problem

The discussion below applies to 32-bit ARM Linux kernel. I noticed that during the forking process, Linux kernel copies the content of kernel page table(master page table, i.e. swapper_pg_dir) into the page table of every newly created process. Questions are: - Why bother doing that? - Why can't all processes share a single copy of kernel page table(higer 1G part regarding 32bit ARM Linux), instead of memcpy the swapper page table for each newly created process? - Is it a waste of memory? Related source code("-->" stands for function call): do_fork --> copy_process --> copy_mm --> dup_mm --> mm_init --> mm_alloc_pgd --> pgd_alloc --> ``` /* * Copy over the kernel and IO PGD entries */ init_pgd = pgd_offset_k(0); memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD, (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); ```

Original source

Related problems