Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the relationship between kernel page table and process page table in Linux

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article is about the relationship between kernel page table and process page table in Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

When we first learn the kernel, we are often confused by the "kernel page table" and the "process page table". I don't know what this is and what it has to do with the page table we usually understand.

Kernel page table: the main kernel page table in the book is actually a piece of memory in the kernel. It is stored in the init_mm.pgd (swapper_pg_dir) of the main kernel page, and the hardware is not used directly.

Process page table: each process's own page table, placed in the process's own page directory task_struct.pgd.

In the protected mode, from the hardware point of view, the basic object of its operation is the "process" (or thread), while the addressing depends on the "process page table". When the process is scheduled to switch the context, the page table will be switched: the pgd (page directory) of the new process will be loaded into the CR3 register. From this point of view, in fact, "kernel page table" is not used at all, so what is the use of "kernel page table"? What does it have to do with the process page table?

1. The content in the kernel page table is shared by all processes, and each process has its own "process page table". The linear address mapped in the "process page table" includes two parts:

User mode

Kernel state

Among them, the relevant page table entry corresponding to the kernel state address is the same for all processes (because the kernel space is shared for all processes), and this part of the page table content actually comes from the "kernel page table". That is, the page table items related to the kernel state address in the process page table of each process are a copy of the kernel page table.

2. The kernel page table is maintained and updated by the kernel itself. When page fault occurs in the vmalloc area, the kernel page table is synchronized to the process page table. Taking a 32-bit system as an example, the kernel page table mainly consists of two parts:

Linear mapping region

Vmalloc area

Among them, the linear mapping area is the area mapped by TASK_SIZE offset, which is the part of 0-896m for 32 system, and the corresponding virtual address area is TASK_SIZE-TASK_SIZE+896M. This part of the area has been mapped at the time of kernel initialization, and the corresponding page table has been created, that is, this part of the virtual memory area will not have page fault.

The vmalloc area, 896M-896M+128M, is used to map high-end memory. There are three mapping methods: vmalloc, fixed, and temporary, which is not described here.

Take vmalloc as an example (most commonly used). When the kernel allocates memory using vmalloc, the corresponding linear address has already allocated the corresponding physical memory, made corresponding mappings, and established the corresponding page table items, but the relevant page table items are only written into the "kernel page table" and are not updated to the "process page table" in real time. Here, the kernel uses the strategy of "deferred update". Defer the real update of the process Page Table until the relevant linear address is accessed for the first time, and when page fault occurs, the process Page Table is updated in the page fault process:

/ *

* the missing page address is located in kernel space. Does not mean that the exception occurs in kernel space, it may be the user

The * state accesses the address of the kernel space.

, /

If (unlikely (fault_in_kernel_space (address) {

If (! (error_code & (PF_RSVD | PF_USER | PF_PROT) {

/ / check whether the address of the missing page is in the vmallock. if so, deal with it accordingly.

If (vmalloc_fault (address) > = 0)

Return

/ *

* for the case where the pointer to the page fault exception is located in the vmalloc area, the main purpose is to set the

* synchronize the main kernel page table with the kernel page table of the current process.

, /

Static noinline _ kprobes int vmalloc_fault (unsigned long address)

{

Unsigned long pgd_paddr

Pmd_t * pmd_k

Pte_t * pte_k

/ * Make sure we are in vmalloc area: * /

/ * area check * /

If (! (address > = VMALLOC_START & & address < VMALLOC_END))

Return-1

WARN_ON_ONCE (in_nmi ())

/ *

* Synchronize this task's top level page-table

* with the 'reference' page table.

*

* Do _ not_ use "current" here. We might be inside

* an interrupt in the middle of a task switch..

, /

/ * get the pgd (top-level page directory) address and read it directly from the CR3 register.

* do not get it through current, because a page fault exception may occur during context switching

* if you get it through current at this time, there may be a problem * /

Pgd_paddr = read_cr3 ()

/ / from the main kernel page table, synchronize the page table corresponding to the page fault exception address in the vmalloc area.

Pmd_k = vmalloc_sync_one (_ _ va (pgd_paddr), address)

If (! pmd_k)

Return-1

/ / if the corresponding PTE does not exist after synchronization, there is something wrong with the address

Pte_k = pte_offset_kernel (pmd_k, address)

If (! pte_present (* pte_k))

Return-1

Return 0

}

Thank you for reading! This is the end of the article on "what is the relationship between the kernel page table and the process page table in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report