Management of physical memory
The Linux management physical memory is implemented using the paging mechanism. To make the paging mechanism work efficiently in 32-bit and 64-bit architectures, Linux employs a four-level paging strategy .
Linux supports a variety of memory allocation mechanisms. The main mechanism for allocating physical memory page frames is the page allocator, which uses the well-known partner algorithm as the physical memory allocation mechanism.
The basic idea of managing a piece of memory is as follows. At first, the memory consists of a contiguous fragment, with a simple example of 64 pages. When a memory request arrives, it first rounds upward to a power of 2, such as 8 pages. The entire memory is then split into two halves, as shown in B. Because these fragments are still too large, the lower fragments are again divided by two (c), and then two (d). Now we have a piece of memory of the right size, so assign it to the requestor, as shown in D.
Now assume that the second request for 8 pages has arrived. This request has (e) been directly met. At this point the third request for 4 pages arrived. The smallest available block is split (f), and then half of it is assigned (g). Next, the second block in Page 8 is released (h). Finally, another block of Page 8 is also released. Because the two contiguous 8 page blocks that were just released come from the same 16 page block, they are combined to get a block of 16 pages (i).
Virtual address space
Linux maintains a separate virtual address space for each process. As shown in.
The virtual address space is split into homogeneous contiguous page-aligned regions. In other words, each region consists of a series of contiguous pages with the same protection and paging properties. Code snippets, data segments, heaps, shared library segments, and user stacks are all different zones. Every virtual page that exists is saved in a zone, and the virtual does not belong to a region. There can be gaps between the areas of the virtual address space. All references to these voids can cause a serious page failure.
In the kernel, each zone is described with a vm_area_struct entry. All vm_area_struct of a process are concatenated with a linked list and sorted by virtual address so that all the pages can be found. When the list is too long (more than 32 items), a tree is created to speed up the search. The Vm_area_struct item lists the properties of the zone. These properties include: Protected mode (e.g., read-only or readable writable), fixed in-memory (not swapped out), in which direction to grow (data segment up, stack down).
Vm_area_struct also records whether the zone is private or shared with one or more other processes. After fork, Linux copies a list of extents for the child process, but in order for the parent-child process to point to the same table. The zone mark is readable and writable, but the page is marked as read-only. If any process attempts to write a page, a protection failure occurs, and the kernel discovers that the memory area is logically writable, but the page is not, so it marks a copy of the page as readable and writable for the current process. This mechanism illustrates how replication is implemented at write-time.
Vm_area_struct also records whether the area has backup storage on disk, if so, where. The code snippet stores the executable binaries as backups, and the memory-mapped files store the disk files as backups. Other zones, such as stacks, are allocated until they have to be swapped out, otherwise no backup storage is assigned.
A top-level memory descriptor Mm_struct collects information about all the virtual memory areas belonging to an address space, as well as information about different segments (code, data, stacks), and user-shared address spaces. All vm_area_struct elements of an address space can be accessed in two ways through memory descriptors. First, they are organized in a linked list in the order of virtual addresses. The usefulness of this approach is when all virtual address areas need to be accessed, or when the kernel finds a region of virtual memory assigned a specified size. In addition, theVm_area_struct project was organized into a binary "red-black" tree . This method is used to access a specified virtual memory address. To be able to access the elements of the process address space in both ways, Linux uses more state for each process, but allows different kernel operations to use these access states, which is more efficient for the process.
The kernel data structure that records the virtual memory area in a process is emphasized. The kernel maintains a separate task structure for each process in the system (task_struct in the source code). The elements in the task structure contain or point to all the information that the kernel needs to run the process (for example, a PID, a pointer to the user stack, the name of the executable target file, and the program counter).
An entry in task_struct points to Mm_struct, which describes the current state of the virtual storage. The two fields we are interested in are PDG and Mmap, where the PDF points to the base address of the first-level page table (the page global catalog), and mmap points to a vm_area_struct (the regional structure list), where each Vm_area_ Strcuts describes a region (area) of the current virtual address space. When the kernel runs this process, it stores the PDF in the CR3 control register.
For our purposes, a region-specific area structure contains the following fields:
Vm_struct: Point to the beginning of the area.
Vm_end: Point at the end of the area.
Vm_prot: Describes the read and Write permission permissions for all pages contained within this zone.
Vm_flags: Describes whether pages in this area are shared with other processes, or whether the process is private,
Vm_next: Points to the next area structure in the list.
Mapping of space to physical address space in virtual land
The implementation of virtual storage relies on hardware support, which is different for different CPUs. But almost all of the hardware is mapped using a part of the MMU (memmory Management unit), as shown in.
In page-map mode, the CPU sends out the virtual address, which is what our program sees as an avatar. After the MMU conversion, it became the physical Address. The general MMU is integrated inside the CPU and does not exist as a standalone component.
How Linux manages memory