"Research task" Linux memory management mechanism--kernel space

Source: Internet
Author: User
Tags modifier modifiers

Linuxthe linear address in memory is4G,0~3gfor user space,3~4gfor kernel space

First, Kernel space

kernel space is the memory address of 3~4g, which is used primarily to store high-priority code

in the There are three types of kernel addresses in the X86 structure:

ZONE_DMA memory starts at 16m

Zone_normal 16m~896m

Zone_highmem 896m~

ZONE_DMA Yes DMA pages Used ( DMA is direct path access without cpu cache and direct access to memory) zone_normal zone_highmem is a dynamically mapped page. linux Divide the pages of the system into different zones, forming different pools of memory, This makes it possible to allocate according to the purpose. There are also zone_dma32 32 bit device access.

low-end memory allows for a one-to-many linear mapping of physical memory (only 1G), and high-end memory is used to temporarily store addresses that point to the rest of the physical memory space.

the Linux kernel divides high-end memory into three parts:vmalloc_start~vmalloc_end, Kmap_base~fixaddr_start, and Fixaddr_start ~4G, the Alloc_page ()can be used to obtain a page of high-end memory corresponding to the function of only the linear address. High-end memory mappings are temporary storage of non-linear memory addresses.

physical pages in Linux the representation in

Linux represents a physical page through a struct page structure, which is the smallest unit of system memory

struct page {unsigned long flags;//flag is used to represent the state of the pages, such as whether it is a dirty page (a total of 32 bits can be seen in page-flags.h) atomic_t _coun T _count indicates that the count of this physical page, if 1, means that the kernel has not yet used this page, he is calling the Page_count () function to check the field (returning 0 means idle, positive integer representing being used) void *virtual;//This is used to point to the page in virtual memory Address, used for dynamic mapping of high-end memory ...}

Dynamic mapping of high-end memory

the memory addresses of 0~896m are called low-end memory, and they are mapped to each other in physical memory. the memory address of 896M~1G is called high-end memory, which implements dynamic mapping to access physical memory outside of one-to-one mappings in the kernel. Process A has applied for 4G of memory space, and memory can only implement 0~896m mapping, so this address needs to be temporarily stored in high-end memory, and so on after process A is used and then released. This gives you access to all the physical memory addresses.

Memory Request

the methods to request memory in the kernel are:kmalloc (),kzalloc(), Vmalloc (),alloc_page ()

1,kmalloc for the application of physical memory, the address without continuous, so the performance is faster. The corresponding memory deallocation function is kfree ().

2,kzalloc with respect to Kmalloc more than a __gfp_zero flag, this flag bit will apply to the memory content zeroed. The corresponding memory deallocation function is also kfree (). get_zeroed_page () is the same effect.

3. Vmalloc is used to request virtual memory, because the virtual memory needs to ensure a certain degree of continuity, so the performance is slow (may cause relatively large TLB jitter). Because the object is virtual memory, the requested memory size is not limited and is typically used to request large memory space. The corresponding memory deallocation function is vfree ().

4,alloc_page () to apply for a continuous physical page, you can use Page_address () to convert the specified page into a logical address.

Kmalloc () and __get_free_pages () cannot allocate high-end memory because the two functions return a logical address on a physical address and may not yet be mapped to a virtual address, not a page structure. Only alloc_page () can allocate high-end memory

Allocation flags for low-level pages

Linux the memory management interface involves a flag bit, such as unsigned long get_zeroed_page (unsigned int gfp_mask) . gfp_mask is a collection of flags that can be divided into two main categories: the behavior modifier and the zone modifier. The behavior modifier indicates how the kernel allocates the required memory. The zone modifier indicates where memory is allocated.

Common behavior Modifiers

Flag Description __gfp_wait Allocator can sleep __gfp_io dispenser can start disk I/O __gfp_fs allocator can start file system I/O __gfp_h IGH allocator can access emergency event buffer pool

Common zone modifiers

Flag Description __GFP_DMA assigned only from ZONE_DMA __gfp_highmem from Zone_highmem or zone_normal, but high-end memory is preferred

Slab Layer

slab The allocator is dedicated to allocating small memory. where slab splitter will Slab slab< Span style= "font-family: Arial" and the normal slab slab For specific occasions ( Tcp Slab tcp When a module requires small memory, it is assigned from its own slab) slab< Span style= "font-family: Song Body" is the time for regular distribution. We can see /proc/slabinfo see slab status

for kmalloc-8 These ordinary SLAB, there is a corresponding dma-kmalloc-8 of This type of ordinary SLAB, this type is specifically used by the the memory of the ZONE-DMA zone facilitates the application of memory in DMA mode.

in Slab , the allocated block of memory is called an object. the size of the objects contained in the slab is also different. such as kmalloc-8 this ordinary SLAB, all of the objects are 8B size, in the same vein, kmalloc-16 objects are in 16B To size. When memory is requested, it is divided by these objects, which can reduce memory fragmentation. The object in which the application is released will be returned to his slab .

         When the object owner releases an object, Slab is only to mark the object as free, do not do much processing, and the applicant to apply for the corresponding size of the object, slab Will prioritize the recently freed objects so that the object might even be in the hardware cache, a bit like the practice of each cpu

         kmem_cache ( Slab cache) is the top level of the slab allocator. kmem_cache structure is used to describe a slab Kmalloc-8 kmalloc-16 slab< span style= all objects in "font-family: Arial". All kmem_cache structure is saved in a linked list with the slab_caches kmem_cache by kmem_cache_create that belongs to your own module Slab kmalloc () slab

There are three states in the Slab: full, partially full, empty. When allocating memory, it is prioritized from partially full slab . Each of the three linked lists is represented by:

struct List_head slabs_full;

struct List_head slabs_partial;

struct List_head Slabs_free;

Second, User Space

(see "Linux Memory management mechanism-user space" for details)

user space is the memory address of 0~3g, mainly used to store low-priority code, composed of the following


"Research task" Linux memory management mechanism--kernel space

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.