Kernel virtual memory space and process virtual memory space

I was reading the textbook:Computer Systems A Programmer’s Perspective, in chapter 9.7.2:Linux Virtual Memory System (third edition) that talks about virtual memory.

I was a bit confused by the structure of virtual memory for linux process as shown below:

memory schematic

My question is: does kernel virtual memory preserve for kernel to run and rest of the virtual memory preserve for user process? What does kernel code and data do? And what does the physical memory in kernel virtual memory?


does kernel virtual memory preserve for kernel to run and rest of the virtual memory preserve for user process?

Yes, there is a part of virtual memory that is always reserved for the kernel and another part that is left available to userspace processes. Every single process has its own virtual memory, but the kernel is always mapped in the higher part (higher addresses) of virtual memory. Whether or not this mapping is visible to the process depends on Kernel Page Table Isolation.

See also: Do the virtual address spaces of all the processes have the same content in their “Kernel” parts?

What does kernel code and data do?

Part of the high virtual memory is a direct mapping of the actual kernel image. That is, the kernel executable and all its data. You can see it in more detail here in this page of the kernel documentation, marked as "kernel text mapping, mapped to physical address 0".

See also: What's the use of having a kernel part in the virtual memory space of Linux processes?

And what does the physical memory in kernel virtual memory?

That part of the image is totally misleading. I don't know precisely what information the authors of the book were trying to convey, but physical memory is definitely not a part of kernel virtual memory. They were probably trying to address the fact that there is a direct mapping of all physical memory in the kernel virtual memory, which can be seen again on the same page of the kernel documentation, marked as "direct mapping of all physical memory".

Physical memory refers to the real memory of the system (i.e. the RAM). Each region of virtual memory is mapped to some region of physical memory. This virtual-to-physical mapping is totally transparent to processes and is managed by the kernel. For example, two executables that have the same file open in read-only mode are usually sharing the same physical memory region, while seeing two different virtual address.

This is a more accurate depiction of the relationship between virtual and physical memory:

virtual memory mapping

Source: https://computationstructures.org/lectures/vm/vm.html