Linux Kernel Architecture

(Jacob Rumans) #1

Chapter3:MemoryManagement


Now that we have dealt with the slab arrangement, there are still two more things to do when creating a
new slab cache inkmem_cache_create:

❑ The per-CPU caches must be generated. This task is delegated toenable_cpucache(the layout
and structure of these caches are described in Section 3.6.4). First, the kernel defines the number
of object pointers in the cachedepending on the object size:

0 <size≤256 :120 objects
256 <size≤1024 :54 objects

1024 <size≤PAGE_SIZE:24 objects
PAGE_SIZE<size :8 objects
size>131072 :1 object
Allocation of the required memory for each processor — an instance ofarray_cacheand an
array of pointers to objects with the calculated number of elements — as well as initialization of
the data structures is delegated todo_tune_cpucache. A particularly interesting aspect is that
thebatchcountfield is always set to half the calculated number of objects in the cache.

This regulates the number of objects processed in one go when a cache is filled.

❑ To conclude initialization, the initializedkmem_cacheinstance is added to a globally linked list
whose list head (cache_chain)isdefinedinmm/slab.c.

Allocating Objects


kmem_cache_allocis invoked to obtain objects from a specific cache. Like all malloc functions, it yields
either a pointer to the reserved memory area or a null pointer if allocation fails. The function requires two
parameters — the cache from which the object is to be obtained and a flag variable to accurately describe
the allocation characteristics.

<slab.h>
void *kmem_cache_alloc (kmem_cache_t *cachep, gfp_t flags)

Any of theGFP_values mentioned in Section 3.5.4 can be specified for the flags.^35

As the code flow diagram in Figure 3-50 shows,kmem_cache_allocis based on the internal function
__cache_allocthat requires the same parameters and can be invoked without further ado (this structure
was adopted to merge the implementation ofkmallocandkmem_cache_allocas quickly as possible, as
demonstrated in Section 3.6.5). However,__cache_alllocis also only a front-end function to perform all
necessary locking operations. The actual work is delegated to____cache_alloc(with four underscores),
as shown in Figure 3-50 (actually, the functiondo_cache_allocstands between__cache_allocand
____cache_alloc, but is only required on NUMA systems).

The figure clearly shows that work can follow one of two paths; the first, which is the more frequent and
more convenient of the two, is taken if there are free objects in the per-CPU cache. However, if all objects
are in use, the cache must be refilled, and in the worst-case scenario, this means that a new slab must be
created.

(^35) Notice that the kernel used to provide a differently named set of constants (SLAB_ATOMIC,SLAB_DMA, etc.) with the same
numerical values. These have been dropped during the development of kernel 2.6.20 and cannot be used anymore.

Free download pdf