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