Linux memory management uses page-Based Management and multi-level page tables. The dynamic address translation mechanism implements virtual memory together with the Primary and Secondary memory: each user process has a 4 GB virtual address space, the process can be dynamically scaled during running, providing users with transparent, flexible, and effective memory usage methods. The following describes the basic features and main implementation technologies of Linux virtual memory management:
I. Basic Features of Linux Virtual Memory Management
1. A larger address space.
The virtual memory can be several times the physical memory actually owned by the system. Therefore, it makes the operating system look much larger than the actual memory.
2. reasonable physical memory allocation.
Linux enables various processes to share memory fairly through sharing and switching policies. 3. Protection.
The Linux Storage Management Subsystem sets a "lock bit" for each memory page and a "read/write" bit for the linear address and Table Page items on each page, this ensures that a process is not disturbed by other processes. Even if a process fails, other processes and the operating system itself will not be affected.
4. shared virtual memory
Linux-based virtual memory allows two processes to share memory with each other, for example, a shared library. In this case, the library code only exists in one process, and does not need to be copied to every application.
Ii. Main Implementation Technologies of Linux virtual storage management 1. Request paging and memory expansion
When a user process is created, it does not allocate all pages required by the process to the corresponding physical page. At the beginning, only the first page of the process in the page is loaded. Other pages are transferred from the external store to the required page according to the request of the process running process, when a process accesses an address on a page table with a P-bit of 0, it indicates that this page is not in the primary storage, and page disconnection occurs. The system calls handle_mm_fault () to handle access exceptions, after the corresponding physical page is allocated to it, it calls the swap_in () function to read the page from the External Store.
Linux is a request-based paging storage management that allows it to run processes larger than the primary storage space.
2. when pages are insufficient in the memory of the Page Swap policy, Linux uses the least recently used (LRU) algorithm: that is, the oldest Page always chooses to be swapped out during each swap, for non-dirty (not dirty) pages that are easy to obtain from other devices. In Linux, the discarding technology is used. If a write operation is performed, the page is written to the system's swapfile to speed up the switch-in.
3. Memory Sharing
Linux divides memory into 4 K pages, providing the basis for Memory sharing:
(1) When pages are shared between different processes, the page table items (PTE) of the processes that share the page point to the page. (2) to share the kernel code and data segments, map the kernel code and data segments to the 3 GB ~ 4 GB space, so each process can share the code and Data Segment of the kernel in a certain way.
4. Memory Protection
The "hole" technology, the protection of virtual storage segments, address conversion mechanism, and the R/W Bit of page table access control space are used to implement memory protection.
1) "hole" technology the first 4 K of physical memory is an empty page (empty_zero_page), which is used to capture abnormal access to null pointers. After each virtual storage segment of a process, there is a "4 K" "hole" to capture cross-border access to the virtual storage segment. (In the memory layout of C language, hole exists between stack and heap)
2) The protection mode of the virtual storage segment can be set to the protection mode to prevent unauthorized access. (3) page table item access control bit (R/W) page table items use "r/W" to indicate that the access permission of this page is "1" and can be read/written, "0" indicates that the read/write operations are not allowed. It can be used to prevent unauthorized access.
4) in the address translation organization and paging storage management method, the page image of the address translation organization actually prevents the main memory blocks of each process from interfering with each other and serves as process isolation.
5. Dynamic Address Translation
In Linux, The i386 address translation mechanism (logical address translation to physical address-implemented by MMU) is used to implement dynamic address translation, the corresponding physical address is determined only when a process accesses a virtual address during execution. This method provides the foundation for dynamic and dynamic expansion of process storage blocks.
Iii. Main data structures of Linux virtual storage management 1. 32-bit virtual addresses in Linux, 4 GB virtual storage needs to be addressed through 32-bit addresses. (In Linux, virtual addresses and linear addresses are the same concept.) virtual addresses are divided into three sub-segments, where two sub-segments contain 10 and one sub-segment contains 12
The three child segments indicate different meanings: child segment 1 points to a table called the page Directory (PGD), and child segment 2 points to a table called the page table (PTE, child segment 3 points to the address on the page.
2. Multi-level page table structure in Linux
Standard Linux virtual storage page tables are three levels of page directories (page Directory -- PGD), page middle directory -- PMD), and page tables (page table) -PTE ).
On the i386 machine, the structure of the Linux page table is actually two levels, and the PGD and PMD page tables are combined into one. All operations related to PMD are actually PGD operations. Therefore, functions such as *-PGD-* () and *-pMD-* () in the original code are the same.
The page Directory (PGD) is a 4 K table, each process has only one page Directory, with 4 bytes as a table item, it is divided into 1024 table items (or entry points). The value of this table item is the starting address of the page table. The 32-bit virtual address has a total of 10 sub-segments. The value ranges from 0 to 1st, corresponding to an entry point in the page Directory.
The value of each entry point of the page Directory (PTE) refers to a page frame referred to in this table item. The 32-bit virtual address contains 10 sub-segments, the value range is from 0 to 1023.
A page frame is not a physical page. It refers to a virtual address space.
3. page table item format
The format of each table item in the page table in Linux,
The meanings of the paragraphs are as follows:
P:
Existence bits indicate whether the table item is valid for address conversion. When P = 0, the i386 processor does not explain any bit in the table item. The meaning of these bits is completely explained by the software. P-BITs provide crucial attributes to support paging. If p = 1, the page box corresponding to the virtual address exists in the physical memory, and the program accessing the virtual address can run normally. p = 0, it indicates that the page box corresponding to the virtual address does not exist in the physical memory. The program accessing the virtual address will cause page access exceptions and cause page interruption. This allows the operating system to read missing pages from the disk into the memory, store the read pages into table items, and mark the page as exist, then, the program that causes the exception continues to be executed.
R/W:
Read/write bits indicate that you can read, write, or perform operations on the page to which the table item points. R/W = 1: The page is writable, readable, and executable. R/W = 0: the page is readable, executable, but not writable. When the processor is in the privileged level 0 ~ 2, R/W Bit is ignored. If this table item is located in the page directory, it will act on all the pages mapped to this table item.
U/s:
User/system bit. U/S = 1 This page can be accessed at any processor privilege level; U/S = 0 this page can only be at processor privilege level 0 ~ 2. If this table item is located in the page directory, it will act on all the pages mapped to this table item.
D:
The written flag. Before writing access to the page mapped to this table item, the processor sets this position to 1. If this table item is a table item in the page directory, the processor does not modify the D-bit.
Address:
The physical address of the page is 20 characters in height. The system divides the physical memory into 4 K memory page boxes. Address actually represents the frame number of the page boxes.
4. Dynamic Address ing
Linus virtual storage uses dynamic address ing, that is, the correspondence between the process address space and the bucket is implemented during the execution of the program: every time a process uses an address, the address translation mechanism that needs to be saved to convert the virtual address to the actual address of the memory. Shows its address ing:
Dynamic Address ing allows Linux to achieve dynamic migration of processes in the primary storage, dynamic expansion and movement of virtual storage segments, and also provides the basis for the implementation of virtual storage.