Linux Kernel Architecture

(Jacob Rumans) #1

Chapter3:MemoryManagement


ralign = align;
}
/* 4) Store it. */
align = ralign;
...

A new instance ofstruct kmem_cacheis allocated once the data alignment has been calculated (a separate
slab cache namedcache_cacheis provided to perform allocation).


The decision as to whether to store the slab head on-slab or off-slab (see Section 3.6.3) is relatively simple.
If the object size is more than one-eighth of a page frame, the head is stored off-slab, otherwise on the
slab itself.


mm/slab.c
if (size >= (PAGE_SIZE>>3))
/*
* Size is large, assume best to place the slab management obj
* off-slab (should allow better packing of objs).
*/
flags |= CFLGS_OFF_SLAB;

size = ALIGN(size, align);
...

The slab header can also be stored off-slab for smaller objects by explicitly settingCFLGS_OFF_SLABin the
kmem_cache_createcall.


Finally, the objectsizeis increased until it corresponds to the alignment calculated above.


Up to now we have only defined the size of the objects but not of the slab. In the next step, an attempt is
therefore made to find a suitable number of pages that is neither too small nor too big. Too few objects on
a slab increase administrative overhead and render the method less efficient, while overlarge slab areas
are detrimental to the buddy system.


The kernel tries to find the ideal slab size in an iterative process implemented incalculate_slab_order.
Based on the given object size,cache_estimatecalculates the number of objects, the wasted space, and
the space needed for coloring for a specific number of pages. The function is invoked in a loop until the
kernel is satisfied with the results.


By systematic trial and error,cache_estimatefindsaslabarrangementthatcanbedescribedbythe
following elements:sizeis the object size,gfp_orderthe order for page allocation,numthe number of
objects on the slab, andwastagethe space that is ‘‘wasted‘‘ with this order and is therefore no longer
available for useful data (of course,wastage < sizealways applies; otherwise, another object could be
fitted on the slab).headspecifies how much space is required for the slab head. This layout corresponds
to the following formula:


PAGE_SIZE<<gfp_order = head + num*size + left_over

If the slab head is stored off-slab, the value ofheadis 0 because no space need be reserved for head. If it
is stored on-slab, the value is calculated as follows:


head = sizeof(struct slab) + num*sizeof(kmem_bufctl_t)
Free download pdf