Chat Linux memory Management (1) __linux

Source: Internet
Author: User

There's still time today, and I've always wanted to take the time to write about what the Linux kernel principle is about, the focus is not in the code, but in the internal principles and mechanisms, so that everyone on the Linux kernel has a general perceptual knowledge, personally think it is necessary, to the seemingly complex, unfathomable core implementation, in the way that everyone can understand , the way to tell the story, tell everyone, if someone listens, have the original feeling, then my goal is achieved.

# Memory Management

Where do we start? Let's start with the most basic memory management.

Memory management is the most basic and significant part of the Linux kernel. Understand the relevant principles, whether it is the understanding of memory, or to write user state code is very helpful. Many books, many articles have written related content, but the individual always feel that the content is too complex, not too easy to understand, here want to use my own understanding of the simple way to describe, hoping to help, according to their own ideas, may be a little messy, forgive me. start from memory allocation

Everyone write code, should be allocated memory, different languages, different levels, the use of different interfaces, regardless of the use of the way, in the Linux system, basically will call to the C library of the malloc interface, then the allocation of memory from the malloc start.

malloc is used to allocate a segment of memory, but the memory allocated here is not physical memory, but virtual memory, there is no strict distinction between the virtual address, linear address, such as the concept, will only add to the burden, also do not delve into the physical memory and virtual memory concept, the book usually has a large amount of space, We can simply understand this:

Virtual memory is from the perspective of the process, the concept of logic, does not actually exist;

The physical memory corresponds to the memory on the physical memory strip, the virtual memory and the physical inside have the corresponding relation, the corresponding physical memory is not allocated when the virtual memory allocation;

Since virtual memory does not actually exist, what is the use of allocating it, if you want to write data to it, how the data is written. Where to write it.

Of course, after the memory allocation is used, if it can not be used (such as writing data), it is meaningless. Previously said virtual memory and physical inside the corresponding relationship, when the virtual memory allocation, the corresponding physical memory has not been allocated, there are several key issues: the virtual memory and physical memory of the corresponding relationship between who is responsible for maintenance, how to correspond. When physical memory is allocated. the mapping of virtual memory to physical memory

Let's answer question 1 first.

The mapping relationship between virtual memory and physical memory is established by the page table. What is the page table?

In a nutshell, the page table is a table in memory (not detailing the format of the page table. Read a book can be simply as a hash table, the record is the corresponding relationship between the virtual address and physical address, each virtual address corresponding to a table entry, through this table, you can convert virtual address to physical address, You can also establish a mapping relationship between virtual memory and physical memory. who uses the page table.

The page table has, then who will use it. It can't be the application itself, I don't seem to have seen the page before when I write code.

Of course, the user sees only the virtual address (virtual memory), the other is transparent to the user ~

There is a hardware unit in the CPU, called MMU (Memory Management Unit), the page table is for MMU hardware, MMU use the page table to map virtual address to physical address. That is, the address map is done by the hardware, and the software, including the operating system kernel itself, cares no matter.

All say that the software does not care, then we need to talk about the page table.

The software just doesn't use page tables and, however, the creation and maintenance of page tables is the responsibility of the software (the operating system kernel), that is, we (software) create a mapping relationship between virtual and physical memory, then the hardware to automatically address mapping (conversion), we do not need to care about the specific conversion process.

As I said, the page table is in memory, and the page table is created by the software, so MMU if you know where the page table is.

Simply put, we need (software) to tell it where, how to tell. Of course, write registers. CPU has a special register (CR3), to write the address of the page table, MMU know, and then the hardware itself to use, we will ignore. how many sheets of pages are there?

Looks like a page table can complete all the address mapping.

Of course not, if so, there is no need for virtual memory to exist.

Here is a new concept: the process, which is the most basic concept in the operating system, is actually not "new".

System, all tasks are run in a process, each process has its own independent virtual address space, as if again complex, simply said that each process has its own virtual memory, independent representatives, other processes do not see their own virtual memory, then means that Each process requires separate virtual memory to the mapping of physical memory, which means that each process needs its own page table.

So, how many processes there are in the system, how many page tables there are. Page Table Creation

As I said earlier, the page table creation and maintenance is done by the operating system kernel, when the page table is created.

As mentioned earlier, each process needs its own page table, and obviously the page table needs to be established when the process is created. The specific process and principle of creation will not be spoken. Maintenance of page tables

If the page table is maintained.

Of course, the process itself maintains, if maintained.

Take the previous example, malloc allocated virtual memory, and did not allocate physical memory, also did not establish a mapping relationship, did not modify the page table, then exactly when, by WHO to do mapping.

The answer is: "Abnormal page faults." Page faults

The page fault is another professional term that looks like a bad idea on the surface.

The simple look is an anomaly. What is an exception. This is the concept of the hardware, specifically read the book

Simply put, is a mechanism on the hardware, when the hardware detects some "wrong", the active trigger, and then automatically jump to the exception handler processing. Exceptions are similar to interrupts, except that interrupts are asynchronous, are triggered by peripherals, and the exception is synchronized, triggered by the CPU itself; it seems to be pulling away.

The page-fault exception is a particular exception, triggered by the condition that the MMU detects that an entry in the page table (item ~) does not exist. When to trigger a page fault exception.

When MMU will go to detect page table entries.

When the CPU needs to access a virtual address, for example, to a virtual address to write data (memset), you need to convert the virtual address to physical address, this time MMU will automatically go to the page table to find the corresponding page table entries, if found that the corresponding page table entries (that is, virtual address to the Physical address mapping relationship) does not exist, Then it will automatically trigger the page fault from the hardware level.

It is said that the fault is the hardware itself triggered, if you need to access a virtual address, and the virtual address does not have the corresponding page table entries.

A typical scenario is to perform a memset operation on the corresponding virtual address after malloc. what is the fault in the page?

After the page fault is abnormal, jumps to the appropriate exception handler, which is registered in the kernel in advance, where the main action is to allocate physical memory and then modify the page table of the corresponding process to establish the mapping relationship between the physical memory and the virtual Memory (page table entry).

Of course, the actual implementation is much more complex and is not described in detail here. Look back .

Let's start over. Memory allocation process: The User state program uses the malloc interface to assign virtual addresses. The user program accesses the virtual address, such as memset. Hardware (MMU) needs to convert virtual addresses to physical addresses. The hardware reads the page table. Hardware found that the corresponding page table entries do not exist, the hardware automatically triggers the fault pages exception. The hardware automatically jumps to page fault's handler (the kernel is registered), executes the page fault handler in the kernel, allocates the physical memory, and then modifies the page table (creating the Page table entry) exception processing completes, returns the program user state, continues to perform memset the corresponding operation.

At this point, both virtual and physical memory are assigned to completion and the mapping is completed.

Another point of view, if the malloc allocated memory, has not been used, then the physical memory will not be allocated, this memory allocation strategy is called delay allocation, has its corresponding benefits, understand it yourself ~ next.

This chapter, from the memory allocation point of view of the Linux kernel memory management of the key principles, has been as simple as possible to describe the way, I hope not to bring a burden on everyone ~

Linux memory management also involves many other aspects, such as: Memory used by the kernel itself (slab, Vmalloc) partner system process address space management

After taking time to speak slowly, but all hope to be as simple as possible, I hope you can see it.


Original address: http://happyseeker.github.io/kernel/2016/11/10/memory-management-in-kernel.html

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.