Chapter3:MemoryManagement
kmalloc kmem_cache_alloc
__cache_alloc
__ __cache_alloc
cache_alloc_refill
cache_grow
Object in per-CPU Cache
Ja
Take object from cache Return object
Return object
Nein
Find object and take
it from the cache
Insufficient space in available slabs?
Figure 3-50: Code flow diagram forkmem_cache_alloc.
Selecting a Cached Object
____cache_alloccan check relatively easily if an object is in the per-CPU cache, as the following code
excerpt shows:
mm/slab.c
static inline void *____cache_alloc(kmem_cache_t *cachep, gfp_t flags)
{
ac = ac_data(cachep);
if (likely(ac->avail)) {
ac->touched = 1;
objp = ac->entry[--ac->avail];
}
else {
objp = cache_alloc_refill(cachep, flags);
}
return objp;
cachepis a pointer to thekmem_cache_tinstance of the cache used. Theac_datamacro yields the asso-
ciatedarray_cacheinstance for the currently active CPU by returningcachep->array[smp_processor_
id()].
As the objects in memory immediately follow thearray_cacheinstance, the kernel can access them
easily with the help of the dummy array at the end of the structure without the explicit need for pointer
arithmetic. The object is removed from the cache by decrementingac->avail.
Refilling the Per-CPU Cache
The workload is heavier when there are no more objects in the per-CPU cache. The refill operations
needed in this situation are located incache_alloc_refill, which is invoked when the allocation cannot
be satisfied directly from the per-CPU cache.