User mode memory mapping and kernel mode kernel mapping

Time:2022-4-1

User mode memory mapping

Principle of MMAP

In the virtual address space section, we know that each process has a list VM_ area_ Struct, which points to different memory blocks in the virtual address space. The name of this variable is MMAP.

In fact, memory mapping is not justMapping between physical memory and virtual memoryIt also includesThe contents of the file are mapped to virtual memory space。 At this time, you can access the data in the file by accessing the memory space. The mapping of only physical memory and virtual memory is a special case.

User mode memory mapping and kernel mode kernel mapping

We have almost solved the memory mapping mechanism in user mode. Let’s summarize. The memory mapping mechanism in user mode includes the following parts.

  • User mode memory mapping function MMAP, including anonymous mapping and file mapping.

  • Page table structure in user mode, stored in mm_ Struct.

  • Accessing unmapped memory in user mode will cause page missing exceptions. Allocate physical page tables and supplement page tables. If it is an anonymous mapping, allocate physical memory; If it is swap, read in the swap file; If it is a file mapping, the file is read in.

User mode memory mapping and kernel mode kernel mapping

Kernel memory mapping

Physical memory according toNUMA architectureSub node. Each node is divided into regions. Page in each area.

Physical page passPartner systemAssign. The allocated physical page should be changed into a virtual address so that the upper layer can access it. Kswapd can exchange the page in and out according to the usage of the physical page.

The memory allocation requirements may come from kernel state or user state.

  • For kernel stateKmalloc is allocating large memoryWhen vmalloc allocates discontinuous physical pages, it directly uses the partner system and converts them into virtual addresses after allocation. When accessing, it needs to be mapped through the kernel page table.
    For kmem_ When the cache and kmalloc allocate small memory, the slub allocator is used to cut the large memory allocated by the partner system into small pieces for allocation
    kmem_ The parts of cache and kmalloc will not be swapped out, because the memory allocated by these two functions is mostly used to maintain the key data structure of the kernel. The part allocated by vmalloc in the kernel state will be swapped out. Therefore, when accessing and finding that it is not available, do will be called_ page_ fault。

  • For user status, orDirectly call MMAP, system call allocation, or call malloc。 When malloc is called, if a small amount of memory is allocated, Sys is used_ BRK system call; If you allocate a large amount of memory, use sys_ MMAP system call. Under normal circumstances, the memory in user mode can be swapped out. Therefore, once it is found that the memory does not exist, do will be called_ page_ fault。

User mode memory mapping and kernel mode kernel mapping