For memory management , Let's first briefly understand the following concepts : Logical address , Linear address and physical address .

Those who have studied the principle of microcomputer should know ( I control my birth ), Usually we say logical address comes from segment memory management , A logical address , Is an offset from a segment identifier plus a relative address within a specified segment , Expressed as
[ segment identifier : Within segment offset ]. Physical address is the actual address of physical memory , Linear address is the intermediate layer between logical address and physical address transformation , The linear address is the offset address in the middle of the logical address , Add the base address of the corresponding segment ( The base address needs to be moved left and then added (Intel)). Paging is enabled , Linear addresses are transformed into physical addresses using entries in the page directory and page table .

The memory address that appears in a machine language instruction , They're all logical addresses , Needs to be converted to a linear address , Go through again MMU(CPU Memory management unit in ) Only when converted to a physical address can it be accessed .

stay Linux in , Its logical address is equal to a linear address . because Linux All the segments ( User code snippet , User data segment , Kernel code snippet , Kernel data segment ) The baseline addresses of the segments are all from 0x00000000
start , length 4G, This is a linear address = 0 + Offset address , In other words, logical address is equal to linear address , Be more specific , That is, the value of the offset field of the logical address is always the same as that of the linear address .


The starting linear address of the segment in the graph is the base address of the segment , This is equivalent to Linux The segment was bypassed , therefore Linux The main way to achieve memory management is paging .

Let's take a look at virtual memory and physical memory

Physical memory is the real memory , Where the program ends up running . Current memory management methods introduce the concept of virtual memory between program and physical memory , Virtual memory is between program and physical memory , The program can only see virtual memory , Physical memory cannot be accessed directly , Like ours
Functions such as open up virtual memory space . Each process has its own independent process address space ( Virtual address ), In this way, process isolation is achieved . In the end, you need to map the virtual address to the physical address . The kernel maintains different page tables for each process , Different processes can have the same virtual address , But the mapped physical address is different .

The previous section briefly introduces the segmentation mechanism , Here's a quick look at paging , We will not discuss the secondary mode which is more complicated in the middle .

Paging in hardware

Paging units translate linear addresses into physical addresses , For the sake of efficiency , Linear addresses are divided into groups of fixed length , It's called a page . Consecutive linear addresses within a page are mapped to consecutive physical addresses . Paging units put all RAM( physical memory ) Page boxes divided into fixed length ( Also called physics page ), Each page box contains a page , In other words, the length of a page box is the same as that of a page , The page box is part of main memory , So it's also a storage area .

Paging mechanism is to divide the memory address space into several small fixed size pages ,Linux The size of the general page in is
4KB, We split the address space of the process into pages , Load common data and code pages into memory , Infrequently used code and data are stored on disk . The kernel just creates virtual memory ( Initializing memory related linked list in process control table ), In fact, the program data and code corresponding to the virtual memory are not immediately copied into the physical memory , It's just the mapping between virtual memory and disk files , Wait until the corresponding program is run , Will pass the page missing exception , Calling page missing exception handler , Copy data from disk to corresponding physical memory .

The following understanding Linux How does the kernel manage memory :

The kernel takes physical pages as the basic unit of memory management . Memory management unit (MMU) Manage the page table in the system by page size , From the perspective of virtual memory , Page is the smallest unit . Different architectures , The supported page sizes are also different .

For kernel struct page Structure represents each physical page in the system

struct page { unsigned long flags; /* The state of the page where it is stored */ atomic_t _count; /* The reference count of the page */
union { atomic_t _mapcount; /* Count of ptes mapped in mms, * to show when page
is mapped * & limit reverse map searches. */ struct { /* SLUB */ u16 inuse; u16
objects; }; }; union { struct { unsigned long private; /* Mapping-private
opaque data: * usually used for buffer_heads * if PagePrivate set; used for *
swp_entry_t if PageSwapCache; * indicates order in the buddy * system if
PG_buddy is set. */ struct address_space *mapping; /* If low bit clear, points
to * inode address_space, or NULL. * If page mapped as anonymous * memory, low
bit is set, and * it points to anon_vma object: * see PAGE_MAPPING_ANON below.
*/ }; void *virtual; /* The virtual address of the page Kernel virtual address (NULL if not kmapped, ie.
highmem) */ …… };
The kernel uses this structure to manage all the pages in the system , Because the kernel needs to know if a page is idle , Is the page allocated . If the page is already assigned , The kernel also needs to know who owns the page , The owner may be a user space process , Dynamically allocated kernel data , Static kernel code or page cache, etc .

Each physical page in the system is assigned one such structure , The kernel only uses this data structure to describe what is stored in the relevant physical page at the current time , The purpose of this data structure is to describe the physical memory itself , Instead of describing the data contained in it .

Get page

The kernel provides an underlying mechanism for requesting memory , It also provides several interfaces to access it , All of these interfaces allocate memory on a page by page basis ( Function defined in linux/gfp.h) in

In the function interface above alloc* The physical address of the memory is returned ,get* Returns the logical address of the allocated physical page . The pages requested to be allocated are continuous .

The kernel also provides a function to convert a given page to its logical address

void* page_address(struct page *page) // Returns the logical address that points to the current location of a given physical page

Request memory in kernel mode : The kernel thinks that once a kernel function requests memory , Then the application must be met immediately ; When applying in user mode , The kernel always tries to delay the allocation of physical memory , The user process always gets access to a virtual memory first , Finally, a piece of real physical memory is obtained by page missing exception .

Release page

Page resources are also limited , When you no longer need pages, you can use the following function to release them :
void __free_pages(struct page *page, unsigned int order) /*
* This function checks first page The page descriptor pointed to , If the page box is not reserved , Just put the descriptor's count Field minus 1.
* If count The value changes to 0, It is assumed that page Starting from the corresponding page box 1<<order Consecutive page boxes ( Physical page ) No longer in use */ void
free_pages(unsigned long addr, unsigned int order) /*
* be similar to __free_pages, However, the parameter it receives is the linear address of the first page box to be released addr */ void free_page(unsigned long
addr) /* *free_pages(addr, 0) */

When you need a family of continuous physical pages in pages , Especially if you only need one or two pages , These low-level page functions are useful , For commonly used byte based allocation , The functions provided by the kernel are kmalloc()

kmalloc() stay <linux/slab.h> Statement in , It can be used to obtain a block of kernel memory in bytes :

void* kmalloc(size_t size, gfp_t flags)
// Returns a pointer to a memory block , Its memory block must have at least size size , The allocated memory areas are physically contiguous , On error , return NULL void kfree(const *ptr)
// Release by kmalloc() Allocated memory block .PS:kfree(NULL) It's safe

kmalloc/kfree Working in slab On the basis of distribution , When the kmalloc
When applying for memory , The kernel selects the most appropriate buffer pool from the general buffer pool for memory allocation based on the requested size , The so-called most appropriate is all buffer pools larger than or equal to the size of the request slab
The smallest object size . In other words, the kernel can only allocate some predefined , Fixed size byte array .kmalloc The smallest block of memory that can be processed is 32 or 64 byte ( Architecture dependency ).

The kernel also provides vmalloc() function , It works like this kmalloc(), It's just vmalloc
The allocated memory virtual addresses are contiguous , Physical addresses need not be continuous . This is also how user control assignment functions work : from malloc()
The pages returned are contiguous in the virtual address space of the process , But that doesn't guarantee that they're in physics RAM It's also continuous . and kmalloc
Function to ensure that pages are contiguous at the physical address ( Virtual addresses are naturally continuous ).vmalloc()
Function values ensure that pages are contiguous within the virtual address space , It allocates non contiguous blocks of physical memory , again “ correct ” Page table , Map memory to a contiguous area of the logical address space .

reference material :《Linux Design and implementation of kernel 》

©2019-2020 Toolsou All rights reserved,
VUE+Canvas Achieve desktop Pinball brick elimination games C/C++ Memory model 2019PHP Interview questions ( Continuously updated )PHPspringboot2 Separation of front and rear platforms ,token Put in header Pit for verification Vue SpringBoot conduct Excel download element-ui Step on pit record 45 The 12-year-old programmer was turned down , Is the workplace wrong ?Python Web frame Pandas Fundamentals of statistical analysis _ data processing (DataFrame Common operations )Java Misunderstanding —— Method overloading is a manifestation of polymorphism ?