在“fork"一个进程时,为什么Linux内核会为每个新创建的进程复制内核页表的内容? [英] While "fork"ing a process, why does Linux kernel copy the content of kernel page table for every newly created process?
问题描述
以下讨论适用于 32 位 ARM Linux 内核.
The discussion below applies to 32-bit ARM Linux kernel.
我注意到在 fork 过程中,Linux 内核将内核页表(主页表,即 swapper_pg_dir)的内容复制到每个新创建的进程的页表中.
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.
问题是:
- 为什么要这么做?
- 为什么不能所有进程共享内核页面的单个副本表(关于 32 位 ARM Linux 的更高 1G 部分),而不是 memcpy每个新创建的进程的交换页表?
- 是否浪费内存?
相关源码(-->"代表函数调用):
do_fork --> copy_process --> copy_mm --> dup_mm --> mm_init --> mm_alloc_pgd --> pgd_alloc -->
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));
推荐答案
每个进程都有自己的内核页表副本(更高1GB)是为了避免用户/内核时的L1页表切换(即避免更新TTBR)土地正在转换.请注意,用户/内核土地切换发生得非常频繁.
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.
为什么要避免更新 TTBR?详细信息可以在这里找到:更新ARM TTBR的缺点是什么(翻译表基址寄存器)?
Why avoiding updating TTBR? Details can be found here: What is the downside of updating ARM TTBR(Translate Table Base Register)?
这篇关于在“fork"一个进程时,为什么Linux内核会为每个新创建的进程复制内核页表的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!