Linux memory: the essence of Memory Management

Source: Internet
Author: User

Http://bbs.chinaunix.net/thread-2055231-1-1.html

1. kernel initialization:

* The kernel has established the kernel page Directory page table database. Assuming the physical memory size is Len, [3g--3g + Len] is created: [0 -- Len] linear correspondence between virtual address vaddr and physical address paddr;
* The kernel creates a page array. The page array corresponds linearly to the physical page series. The page is used to manage the status of the physical page. The virtual address of each physical page is stored in page-> virtual;
* Create a free_list in the kernel and put the pages corresponding to the unused physical pages into it;

2. the kernel module applies for memory vaddr = get_free_pages (mask, order ):

* The Memory Management Module finds a page from free_list and uses page-> virtual as the return value. The returned value is the virtual address of the corresponding physical page;
* Remove the page from free_list;
* The module uses this virtual address to operate the corresponding physical memory;

3. the kernel module uses vaddr, such as executing the command mov (eax, vaddr ):

* The CPU obtains the virtual address vaddr and uses the created page Directory and page table database to find the corresponding physical memory address;
* Write the eax content to the physical memory address corresponding to vaddr;

4. the kernel module releases the memory free_pages (vaddr, order ):

* Find the corresponding page based on vaddr;
* Add the page to free_list;

5. User process applied for memory vaddr = malloc (size ):

* The Memory Management Module finds a unused space vm_area_struct (start -- end) from the user's process memory space (0--3g );
* Insert it to the task-> MM-> MMAP linked list;

6. the user process writes data to vaddr (0-3g), for example, executing the command mov (eax, vaddr ):

* The virtual IP address vaddr is obtained by the CPU. The virtual IP Address should have been set by the glibc library. It must be in a region of 3G, check the page table database of the current process's page Directory based on the current-> PGD pointed to by the Cr 3 register, and find that the page Directory table item corresponding to the vaddr is 0, so an exception occurs;
* During Exception Handling, it is found that vm_area_struct corresponding to the vaddr already exists, and a page table is allocated to the page Directory table item corresponding to the vaddr;
* Find a page from free_list and assign the physical first address of the physical page corresponding to the page to the table entry corresponding to vaddr. Obviously, in this case, the vaddr and paddr do not have a linear relationship;
* Remove the page from free_list;
* Exception Handling returns;
* The CPU re-executes the abnormal command mov (eax, vaddr );
* The CPU obtains the virtual address vaddr. Based on the current-> PGD directed by the Cr 3 register, it uses the established page Directory page table database to find the corresponding physical memory address;
* Write the eax content to the physical memory address corresponding to vaddr;

7. The user process releases the memory vaddr, free (vaddr ):

* Find the vm_area_struct of the vaddr;
* Find the page table items of all the page directories corresponding to vm_area_struct: start -- end, and clear all the corresponding page table items;
* Release these page table items to the page corresponding to the physical page and add these pages to the free_list queue;
* It is necessary to clear some page Directory table items and release the page tables pointed to by these page Directory table items;
* Delete the vm_area_struct from the task-> MM-> MMAP chain and release it;

Description:

* The available physical memory is the physical memory corresponding to each page in free_list;
* The main purpose of the page Directory page table database is to convert vaddr to paddr when the CPU accesses the physical memory --> it is not used to allocate or release the memory, however, the kernel memory management system is required to set up the library for the CPU at the right time;
* There are two page table items corresponding to the physical page obtained by the user process in Step 6. One is a PTE [I] of the kernel page Directory page table database. one is a specific PTE [J] of the current process kernel page Directory page table database, but only one page corresponds to it. If another process is scheduled to another process at this time and other processes apply for and access a memory, this physical page will not be involved because a page needs to be found from free_list during its allocation, the page corresponding to the physical page has been removed from free_list, so the physical page is not rewritten by other processes. This physical page is not involved in obtaining memory through get_free_pages or other methods in the kernel. The principles are described earlier.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.