Linux device driver--memory management

Source: Internet
Author: User

MMU has physical address and virtual address translation, memory access rights protection and other functions. This allows the Linux operating system to independently allocate separate memory space for each user process and to ensure that user space does not have access to the address of the kernel space, providing the hardware foundation for the operating system virtual memory management module. Linux Memory Managementin the Linux operating system, the 4G space of a process is divided into two parts----user space and kernel space. The user space is typically 0~3GB (that is, Page_offset, which equals 0xc0000000 in the X86 system), while the remaining 3GB~4GB is the kernel space. The user process typically only accesses the virtual address of the user space and cannot access the virtual address of the kernel space. The user can only access the system call through theKernel space.the user space for each process is completely independent and irrelevant, and the user process has separate page tables. The kernel process is mapped by the kernel, and it does not change with the user process and is fixed. The virtual address of the kernel space is independent of the space of other programs. The kernel address of 1GB in the Linux kernel is divided into physical memory mapping area, virtual memory allocation area, high-end page map area, dedicated page map area and reserved area. between the physical area and the high-end mapping area is the virtual memory allocation area, for the Vmalloc () function, his front and physical area there is a barrier, his back and high-end memory mapping area there is a barrier. Memory Access 1. Dynamic application of user space memoryThe function for Malloc,malloc () must be free (), and the two functions are used in pairs, otherwise it will cause a memory leak. For the Linux kernel, the malloc () function of the C library is usually implemented through the underlying BRK () and mmap (). 2. Kernel Space memory Dynamic requestthe functions involved in the kernel application are mainly Kmalloc (), __get_free_page (), and Vmalloc (), Kmalloc (), and __get_free_page () (and their related functions) for the memory in the physical memory map area. are also physically contiguous, they differ from the actual physical address by a fixed offset. The Vmalloc () application memory is located in a block of memory in the virtual memory map area where there is not necessarily continuous physical memory, and there is no simple conversion relationship between the virtual memory and the physical memory requested by Vmalloc (). (1) kmalloc ()          the first parameter of Kmalloc () is the size of the memory block to allocate, the second parameter is the flag bit and is used to control the behavior of the Kmalloc () function. The most commonly used flag bit is gfp_kernerl, meaning to request memory in the kernel's virtual space. The bottom of the Kmalloc relies on __get_free_page () to implement. When you use __get_free_page (), the process sleeps if it is temporarily not satisfied. Therefore, you can no longer interrupt the context or hold a spin lock when you cannot use the GFP_KERNERL flag.          The gfp_atomic flag bit can be used in interrupt handling functions, or when non-process contexts, such as tasklet and timers, cannot be blocked. When applying this flag, if the kernel is not idle, it will not wait. memory requested using Kmalloc () is freed by Kfree ().         (2) __get_free_page ()This function is the most basic method that the Linux kernel uses to get free memory. Because the underlying algorithm manages memory by 2 of the page's n-th side. So the bottom-level implementation is in page units.         This series of functions contains __get_zeroed_page () returns a pointer to a new page and zeros the page. __get_free_page () Returns a pointer to a new page, but the page is not zeroed. __get_free_pages () This function returns the first address of a pointer to a few pages, the number of pages allocated is 2 oder, and the number of pages allocated is not zeroed. The maximum allowable value for Oder is 10 (that is, 1024 pages) or 11 (2048), depending on the hardware platform. When released, use Free_page (). If the released Oder is inconsistent with the requested Oder, it can cause memory clutter. (3) Vmalloc () generally only exists in the software in the larger order buffer allocated memory, vmalloc () much larger than __get_free_page () overhead in order to complete the Vmalloc, the new page table needs to be built. Therefore, it is inappropriate to apply vmalloc to complete a small amount of memory. Vmalloc cannot be used in an atomic context, because his internal implementation calls the __GFP_KERNERL flag bit of kmalloc to implement. (4) Slab and memory poolusing pages as a unit to apply and free memory in a Linux system can easily lead to waste. In addition, in the operating system, the process involves a large number of duplicate generation, release issues. For example: Inode,task_struct and so on. Slab is to make the allocation in the same memory area, or the same class of memory space, and retain the basic data structure before and after two times, can achieve the efficiency improvement. the slab process is as follows:Note: Slab relies on __get_free_page () at the bottom, but the requested page-by-unit memory space is managed by dividing it into smaller memory units, which does not cause a waste of memory. The use of memory pools in Linux is also used for a large number of small object fallback buffering techniques.            

Linux device driver--memory management

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.