Linux Kernel memory allocation

Source: Internet
Author: User

Experimental requirements:

1. Write a kernel module, allocate memory in the module and access

2, understand and verify the difference between Kmalloc, Vmalloc and other functions.

Background knowledge:

1, Linux memory page management

The Linux kernel manages physical memory through a paging mechanism that divides the entire memory into 4K size pages as the basic unit for allocating and reclaiming memory. Allocate memory as much as possible, to avoid a TLB refresh rate too high. Therefore, Linux uses a "partner" relationship to manage the free page box. Therefore, you also need to follow the partnership when you allocate free pages. The smallest unit is a power-doubling page size of 2. The basic function for allocating free page boxes in the kernel is get_free_page/get_free_pages, which either assigns a single page or assigns a specified page box.

2, Slab Technology Introduction

allocating memory in the smallest unit of a page is really convenient for kernel management system physical memory, but the kernel itself is often using a small memory block (much less than 1 pages), and in order to satisfy the need of the kernel for this small memory block, the Linux system uses a technique called the slab allocator. The implementation of slab allocator is quite complicated, and its core idea is the application of "Storage Pool". Memory fragments (small chunks of memory) are treated as objects, and when they are used, they are not released directly but are cached in the "Storage Pool" for the next use, which undoubtedly avoids the extra load created by destroying objects frequently.

Slab is to shred pages (from a chain of free page frames from partnership management) into many small chunks of memory for allocation, object allocation and destruction in slab using Kmem_cache_alloc and Kmem_cache_free.

3, Kmalloc

In general, requests for small chunks of memory smaller than one page in kernel programs are done through the interface Kmalloc provided by the slab allocator (although it allocates 32 to 131072 bytes of memory). From the kernel memory allocation point of view, Kmalloc can be seen as an effective complement to get_free_page (s), with a more flexible memory allocation granularity.

4, Vmalloc

There is a concept of "Shard" in memory management theory. Shards are also divided into external shards and internal shards. The so-called internal Shard is said that the system in order to meet the needs of a small area of memory (continuous), it has to allocate a large area of continuous memory to it, resulting in space waste; external sharding means that the system has enough memory but is fragmented and unable to meet the need for chunks of "contiguous memory". Slab technology solves internal shards, but the partnership only mitigates external shards, but is completely eliminated.

To avoid external shards, the Linux kernel allows kernel programs to allocate virtual addresses in the core address space, and also uses page tables (kernel page tables) to map virtual addresses to distributed memory pages. The kernel provides the VMALLOC function to allocate kernel virtual memory, which, unlike Kmalloc, can allocate much larger memory space than Kmalloc (which can be larger than 128KB, but must be a multiple of the page size). However, compared to Kmalloc, Vmalloc needs to remap the kernel virtual address, the kernel page table must be updated, so the allocation is less efficient (time-space-changing).

Linux Kernel memory allocation

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.