Home > OS >  Five minutes to thoroughly understand you didn't understand the Linux memory management
Five minutes to thoroughly understand you didn't understand the Linux memory management

Time:11-19

Now most of the server is running on Linux, so simply as a programmer is necessary to understand how the system worked, for memory part need to know:
Address mapping
The way of memory management
Abnormal missing page

Start with some basic knowledge in the process, the memory part is divided into user mode and kernel mode, classic proportion is as follows:


From user mode to kernel mode generally through the system call, interrupt, user mode memory is divided into different regions for different purposes:


Kernel mode, of course, also won't used indifferently, so its classification is as follows:


Below to see how the memory management,


The address

In the Linux internal address mapping process for logical addresses - & gt; Linear addresses - & gt; The simplest: physical address, physical address in the address bus transmission of digital signal, and the linear address and logical address represents is a kind of transformation rules, rules of linear addresses is as follows:



This part of work by the MMU, which involves the main register CR0, CR3, and the machine instruction is logical address, logical address rules are as follows:



In Linux the logical address is equal to the linear, that is to say system compatible to bring things is very complex, a lazy Linux simplified by the way,


The way of memory management

Boot the system to detect when the size of the memory and, before building complex structure, need to use a simple way to manage these memory, this is bootmem, is simply a bitmap, but there are also some optimization idea,

Bootmem and how to optimize, the efficiency is not high, when to allocate memory, after all, is going to traverse the buddy system just to solve the problem: in internal keep some power of 2 times the size of the free memory fragments, if you want to assign 3 page, go to take a 4 page list, distribution of the remaining 1 after three back, release the memory is just a reverse process, the process of using a graph to represent:



Can see 0,4,5,6,7 are being used, so, 1, 2 was released, they would merge?
The static inline unsigned long
__find_buddy_index (unsigned long page_idx, unsigned int order)
{
Return page_idx ^ (1 & lt; }

This code you can see from the above 0, 1 is the buddy, 2, 3 is the buddy, although 1, 2, but they are not, memory fragments is an enemy of system running, the partner system mechanism can to a certain extent, prevent debris ~ ~ in addition, we can get to by cat/proc/buddyinfo spare the number of pages in each order,

Partner system allocates memory every time on page (4 KB) as the unit, but the system use most of the data structure is very small, for a small object allocation 4 KB is obviously not cost-effective, in Linux using the distribution of the slab to solve the small object:



At runtime, the slab to buddy "wholesale" some memory, processing cutting later spread to sell go out, with large-scale multiprocessor systems and the NUMA system widespread application, slab is finally revealed:
Complex queue management
And queue management data storage overhead
Long running partial queue can be very long
Support for NUMA is very complicated

: in order to solve these experts developed a slub cutting slab page structure management overhead structure, each CPU has a local activity of the slab (kmem_cache_cpu), etc., for small embedded systems is a slab to simulate layer slob, it has more advantages in this system,

Small memory problem is solved, but there is a large memory problem: with the partner system allocated 10 x 4 KB of data, will go to 16 x 4 KB free list to find the physical memory (this is a continuous), but there is a memory, is likely to system but partner system allocation not to come out, because they are divided into small pieces, so vmalloc is to use these fragments to piece together a big memory, equivalent to collect some "leftover material", "sell" after assemble into a finished product:



Before the memory is direct mapping, the existence of the first feel paging: D in addition to high memory, provides kmap method page for a linear address distribution,

Process is composed of different length of period: code, dynamic library code, a global variable and dynamic data of heap, stack, etc., in Linux, a set of virtual address space for each process management:



After we finish writing code malloc and didn't immediately take up the size of physical memory, but simply to maintain the virtual address space above, only needs in the real time distribution of physical memory, this is the COW (COPY - ON - WRITE: WRITE in COPY) technology, the physical distribution process is the most complex page fault exception handling link, see below!


Abnormal missing page

Before the actual needs, a virtual memory area data, and the mapping relationship between physical memory will not build, if a process to access the virtual address space of the part has not been linked to page frames, processor automatically trigger a page fault is unusual, can get when the kernel page fault exception handling information is as follows:
Cr2: access to the linear address
Err_code: exception occurs by pressing the control unit into the stack, the cause of the abnormity said
Regs: when an exception occurs the value of the register

Handling process is as follows:



Missing page exception occurs, may be because they do not often use the swap to disk, swap related commands are as follows:

If memory is mmap mapped to in memory, then reading and writing the corresponding memory also can produce a page fault is unusual,

CodePudding user response:

More learning exchange penguin, 3524659088
  • Related