Linux Kernel Architecture

(Jacob Rumans) #1

Chapter3:MemoryManagement


Include object into cache

Move remaining cache entries upward

Yes

No

kmem_cache_free

_ _cache_free

cache_flush_array

free_block

Space in per-CPU array available?

Figure 3-52: Code flow diagram forkmem_cache_free.

mm/slab.c
static inline void __cache_free(kmem_cache_t *cachep, void *objp)
{
...
if (likely(ac->avail < ac->limit)) {
ac->entry[ac->avail++] = objp;
return;
} else {
cache_flusharray(cachep, ac);
ac->entry[ac->avail++] = objp;
}
}

If not, some objects (the exact number is given byarray_cache->batchcount) must be moved from the
cache back into the slabs starting with the array elements with the lowest numbers β€” because the cache
implementation applies the LIFO principle, these are objects that have been in the array longest and
whose data are therefore least likely still to be held in the CPU cache.


Implementation is delegated tocache_flusharray. In turn, this function invokesfree_blockto move
the objects from the cache to their original slabs and shifts the remaining objects in the cache to the start
of the array. For example, if there is space for 30 objects in the cache and thebatchcountis 15, the objects
at positions 0 to 14 are moved back into the slabs. The remaining objects numbered 15 to 29 are shifted
upward in the cache so that they now occupy positions 0 to 14.


Moving objects from the cache back onto the slabs is instructive, so it’s well worth taking a closer look
atfree_block. The arguments required by this function are thekmem_cache_tinstance of the cache, a
pointer to an array consisting of pointers to the objects in the cache, an integer to indicate the number of
objects in the array, and the node whose memory is just being processed.


The function iterates over all objects inobjppafter the number of unused objects in the cache data struc-
ture has been updated.


mm/slab.c
static void free_block(kmem_cache_t *cachep, void **objpp, int nr_objects,
int node)
{
int i;
Free download pdf