4

Different processes have different different page tables for their address spaces.

Does the Linux kernel have its own page table? If not, how are the virtual addresses of kernel translated to physical memory addresses?

In the sense that the address space of each process is divided into user space and kernel space, does the page table of each process contain the "page table" of the kernel?

Thanks.

Related Do page fault, page replacement, and paging happen to the kernel or just to processes, and to the kernel space or the user space?

4 Answers 4

7

You are correct. The VM of a process consists of the user address space and kernel address space. The kernel address space is the same across all processes but protected from access. If you've heard the 2/2 or 3/1 memory split (from x32), it was the user/kernel amount of memory in GBs. So for a 4GB address space you could (e.g.) have 3G for processes and 1G for the kernel.

I would recommend reading the "Understanding the Linux Kernel" book which will answer this and many other of your questions.

3
  • Here is the focus of my questions: Does the Linux kernel have its own page table? does the page table of each process contain the "page table" of the kernel, i.e. is the "page table" of the kernel redundantly repeated in the page table of every process?
    – Tim
    Oct 15, 2018 at 23:02
  • @Tim AFAIK, it's one per process and includes the kernel part
    – V13
    Oct 15, 2018 at 23:31
  • Do you mean repetition? Isn't that redundant and waste space?
    – Tim
    Oct 16, 2018 at 0:05
2

"Page tables" are actually a type of tree structure. E.g. see "Four-level page tables" [LWN.net, 2004].

The top level is a single page (4096 bytes). The entries which map the kernel range, are set to the same values in all processes, and hence are shared. Kernel temporary mappings happen at a lower level of the tree, so they only need to modify inside the shared kernel page tables, and they do not need to modify each process page table separately.

At least, that's my high-level overview. I don't have all the words.

The above is cross-posted from my answer here: https://unix.stackexchange.com/a/513184/29483

1

For your question, Linux kernel does not have its own pagetable. But if we make that specific to each process, it will have two pagetable type designed for using in kernel mode and user mode. When a process call the system call or go into a trap or interupt, it will tranfer to kernel mode, and kernel will take the run control, it will use the kernel page table for this process. So each process have its own kernel pagetable.

refer to this link for KPTI:

KPTI fixes these leaks by separating user-space and kernel-space page tables entirely. One set of page tables includes both kernel-space and user-space addresses same as before, but it is only used when the system is running in kernel mode. The second set of page tables for use in user mode contains a copy of user-space and a minimal set of kernel-space mappings that provides the information needed to enter or exit system calls, interrupts and exceptions

0

This is an interesting question that I was also curious about. I'll try to answer the question on following condition:

  • kernel version 5.10.7
  • arch x86
  • CONFIG_PGTABLE_LEVELS is 2 (using two level page tables)
  • kernel address space is 1G

For each process, the first level page table is called "page global directory", or pgd. Entry index #768 to index #1023 in pgd is used to map kernel address. Each process has its own unique 4K-byte page that is used for pgd. And yes, to your question, entries from #768 to #1023 in pgd are the same and are duplicated by each process. But this is only a very small part. The second level page table for kernel are still shared across all processes.

Please refer to arch/x86/mm/pgtable.c, function pgd_ctor(), to understand how the duplicate part of the first level page table(pgd) is created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.