Allocating Objects

kmem_cache_alloc is 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.

void *kmem_cache_alloc (kmem_cache_t *cachep, gfp_t flags) Any of the GFP_ 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_alloc is based on the internal function __cache_alloc that requires the same parameters and can be invoked without further ado (this structure was adopted to merge the implementation of kmalloc and kmem_cache_alloc as quickly as possible, as demonstrated in Section 3.6.5). However,_cache_allloc is 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 function do_cache_alloc stands between_cache_alloc and

_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.

35Notice 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.

kmem_cache_alloc

_cache_alloc

_cache_alloc

Object in per-CPU Cache

Nein

Take object from cache cache_alloc_refill

Find object and take it from the cache

Return object

Insufficient space in available slabs?

cache_grow

Return object

Continue reading here: Selecting a Cached Object

Was this article helpful?

0 0