Chapter3:MemoryManagement
The state ing_cpucache_upis then set toPARTIAL_AC, meaning thatarray_cacheinstances
can be allocated immediately. If the initialized size is also sufficient to allocatekmem_list3
instances, the state immediately changes toPARTIAL_L3. Otherwise, this only happens when
the next larger cache has been initialized.
The per-CPU data of the remainingkmalloccaches can now be created withkmallocas an
instance ofarraycache_init, as only the smallestkmallocarea is needed for this purpose.
mm/slab.c
struct arraycache_init {
struct array_cache cache;
void * entries[BOOT_CPUCACHE_ENTRIES];
};
- In the last step ofkmem_cache_init, all statically instantiated elements of the data struc-
tures used up to present are replaced with dynamically allocated version created using
kmalloc. The state ofg_cpucache_upis nowFULL, indicating that the slab allocator is ready
for use.
Creating Regions
kmem_cache_createmust be invoked to create a new slab cache. This function requires a large set of
parameters.
mm/slab.c
struct kmem_cache *
kmem_cache_create (const char *name, size_t size, size_t align,
unsigned long flags,
void (*ctor)(struct kmem_cache *, void *))
Besides a human-readablenamethat subsequently appears in/proc/slabinfo, the function requires the
sizeof the managed objects in bytes, an offset used when aligning data (align, in almost all cases 0), a
set of flags inflags, and constructor/destructor functions inctoranddtor.
Creation of a new cache is a lengthy procedure, as the code flow diagram forkmem_cache_createin
Figure 3-49 shows.
Several parameter checks are carried out to ensure that no invalid specifications are used (e.g., an object
size with fewer bytes than a processor word, a slab without name, etc.) before the first important step is
carried out — calculation of the required alignment. First, the object size is rounded up to a multiple of
the word length of the processor used:
mm/slab.c
kmem_cache_t *
kmem_cache_create (...) {
...
if (size & (BYTES_PER_WORD-1)) {
size += (BYTES_PER_WORD-1);
size &= ~(BYTES_PER_WORD-1);
}
Object alignment (inalign) is typically also based on the processor word length. However, if the
SLAB_HWCACHE_ALIGNflag is set, the kernel aligns the data as recommended by the architecture-specific
functioncache_line_size. It also attempts to pack as many objects as possible in a cache line by halving