http://scriptmatrix.net/cs2106/wiki/index.php/Slab_AllocatorSlab AllocatorFrom The Linux Memory WikiAlright, so now we are able to allocate 2^x of contiguous page frames. But often we don't really need that exact amount of page frames. For instance, the kernel very often needs to allocate small chunks of memory to store all kinds of descriptors such as process descriptors and so on. We can notice some characteristics of the allocation: Most the time these descriptors have fixed size and the descriptors get destroyed and created again and again continuously. Hence we can think of a new system to handle this kind of request: the Slab Allocator.
Cache, Slab, and ObjectThe Slab Allocator contains groups of cache. A cache contains one or more slab. Each slab contains a number of object. The basic concept is like this: each cache is responsible to allocate a fixed size of page frames. For instance, there maybe one cache that is allocating memory for 10-page frames, and another is allocating 25 page frames, and so on. But how these cache allocate memory? they do so by first request for a contiguous memory, that is the slab, from the buddy allocator. the slab then divide the memory into a number of smaller contiguous memory, which is the objects. When a fixed size memory is requested, the slab allocator will call the corresponding cache for the memory. The cache will look at it slabs to see if there is any free object. If there is free object the cache will return the address of that object for use. If no free object is available, the cache will create a new slab and take one of the object from this new slab.
General and Specific CachesThere are 2 kinds caches, which are general and specific caches. Specific caches can be created to store objects of certain sizes, and it is commonly used by kernel and modules to allocate frequently used descriptors. A specific cache can be given a name to indicate its usage. General caches are used by the slab allocator itself to store cache descriptors of other caches. These caches are stored in a cache called kmem_cache which is the first cache of general cache. (Actually I don't quite understand why this is also called general because this cache is not general at all.) Other than that, general caches also contains 13 geometrically distributed cache, starting from 32 bytes up to 131,072 bytes increased in power of 2. We can see that these 13 caches are almost similar to the blocks in the buddy allocator, other than the size of these caches are much smaller (compared to minimum of 4KB size in buddy allocator). The general caches will be used by the famous kmalloc() function to allocate normal memory, which we will see later. Slab Allocation Functionskmem_cache_create()This function is used to create a specific page. It has the following signature: struct kmem_cache * kmem_cache_create( const char *name, size_t size, size_t align, unsigned long flags; void (*ctor)(void*, struct kmem_cache *, unsigned long), void (*dtor)(void*, struct kmem_cache *, unsigned long)); As we can see, the function creates a cache of the given name and given size and returns the cache descriptor of that cache. Notice that this function only creates an empty cache, there is no slab or objects in the cache yet. Normally this function will be used by module developers when they need to create specific cache for their own descriptors. kmem_cache_alloc()void * kmem_cache_alloc( struct kmem_cache *cachep, gfp_t flags ); This function allocate and return an object from the specified cache described in the cachep cache descriptor. The function will goes through the slabs of the cache and find if there is any free object. If the cache contains no free object it will call the cache_alloc_refill() function to allocate new slabs for the cache from the buddy allocator. The gfp_t flags will also be passed to the buddy allocator at this time to specify the type of memory required. Finally, a new object is again created from this new slab and be returned. kmalloc()void * kmalloc(size_t size, int flags) Those who have studied the kernel source code before may have notice the frequent use of this function. This function will find the closest size of object from the 13 general caches and return that object. If the cache for that particular size is full a new slab will be allocated according to the flags given.
|