Slab allocator in Linux memory management

Source: Internet
Author: User

The partition page box allocator implemented in the Linux kernel based on the partner algorithm is suitable for large chunks of memory, and its allocated memory area is based on the page frame. For requests for small chunks of contiguous memory in the kernel, such as a few bytes or hundreds of bytes, if a page box is still allocated to satisfy the request, then it is obviously a waste to produce internal fragments (internal fragmentation)

To address the allocation of small chunks of memory, the Linux kernel implements its own slab allocator based on the slab allocation algorithm in Solaris 2.4. In addition, another major feature of the slab allocator is as a cache, which is used to store objects that are often allocated and disposed of in the kernel.

Basic principle of 1.slab dispenser

The concept of objects is used in the slab allocator, where the object is the data structure in the kernel and the operation of creating and revoking the data structure. The basic idea is to put objects that are used frequently in the kernel into the cache, and that the system remains in its initial available state. such as the process descriptor, this data is frequently applied and released in the kernel. When a new process is created, the kernel gets an initialized object directly from the cache of the slab allocator, and when the process ends, the page box occupied by the structure is not freed, but returned to the slab allocator. Without an object-based slab allocator, the kernel spends more time allocating, initializing, and releasing an object.

The slab allocator has the following three basic objectives:

1. Reduce the internal fragmentation generated by the partner algorithm in allocating small blocks of contiguous memory;

2. Cache frequently used objects to reduce the time overhead of allocating, initializing, and releasing objects.

3. Adjust the object by shading technology to better use the hardware cache;

Structure of the 2.slab splitter

The slab allocator allocates a cache for each object, which can be seen as a reserve for the same type of object. Each cache occupies a memory area that is divided into multiple slab, each of which consists of one or more contiguous page boxes. Each page box contains several objects, both objects that have been allocated and objects that are idle. The approximate composition of the slab splitter is shown below:

Each cache is described by the Kmem_cache structure, which contains descriptions of the various attribute information for the current cache. All caches are organized together by double-linked lists to form a cache linked list Cache_chain. Each Kmem_cache structure does not contain a description of the specific slab, but rather organizes each slab through the KMEM_LIST3 structure. The structure is defined as follows:

struct KMEM_LIST3 {
struct List_head slabs_partial;
struct List_head slabs_full;
struct List_head slabs_free;
unsigned long free_objects;
unsigned int free_limit;
unsigned int colour_next;
spinlock_t List_lock;
struct Array_cache *shared;
struct Array_cache **alien;
unsigned long next_reap;
int free_touched;
};

As you can see, the structure divides all slab in the current cache into three collections: the slab linked list Slabs_free of the Idle object, the slab linked list slabs_full for non-idle objects, and the slab list slabs_partial of some idle objects. Each slab has a corresponding slab descriptor, the slab structure, which is defined as follows:

struct Slab {
struct List_head list;
unsigned long colouroff;
void *s_mem;
unsigned int inuse;
kmem_bufctl_t free;
unsigned short Nodeid;
};

The list field in the slab descriptor indicates that the current slab is in one of the three slab linked lists. We will refine the above slab allocator to get the following structure diagram:

3. Cache classification

The slab cache is divided into two main categories, normal cache and dedicated cache. The normal cache does not target specific objects in the kernel, it first provides the cache for the Kmem_cache structure itself, which is stored in the Cache_cache variable, which represents the first element in the Cache_chain list, on the other hand, It provides a general-purpose cache for the kernel. The dedicated cache is created by specifying specific objects, as required by the kernel.

3.1 Normal Cache

The Kmem_cache in the slab allocator is used to describe the structure of the cache, so it also needs to be cached by the slab allocator itself. The Cache_cache variable holds a cache of cache descriptors.

static struct Kmem_cache Cache_cache = {
. Batchcount = 1,
. Limit = Boot_cpucache_entries,
. Shared = 1,
. buffer_size = sizeof (struct Kmem_cache),
. Name = "Kmem_cache",
};

The allocation of small blocks of contiguous memory provided by the slab allocator is achieved through the universal cache. The universal cache provides objects that have a geometric distribution size ranging from 32 to 131072 bytes. The kernel provides kmalloc () and Kfree () two interfaces for memory application and release.

3.2 Dedicated Cache

The kernel provides a complete set of interfaces for the application and release of dedicated caches, assigning slab caches to specific objects based on the parameters passed in.

Cache requests and releases

Kmem_cache_create () is used to create a cache for a specified object. It assigns a cache descriptor to the new proprietary cache from the Cache_cache general cache and inserts this descriptor into the cache_chain linked list formed by the cache descriptor. Kmem_cache_destory () is used to revoke a cache and remove it from the Cache_chain list.

Application and release of Slab

Kmem_cache_alloc () assigns a slab in the cache specified by its parameters. Instead, Kmem_cache_free () releases a slab in the cache specified by its arguments.

Slab allocator in Linux 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.