Chapter3:MemoryManagement
mm/slab.c
static struct cache_sizes malloc_sizes[] = {
#define CACHE(x) { .cs_size = (x) },
#if (PAGE_SIZE == 4096)
CACHE(32)
#endif
CACHE(64)
#if L1_CACHE_BYTES < 64
CACHE(96)
#endif
CACHE(128)
#if L1_CACHE_BYTES < 128
CACHE(192)
#endif
CACHE(256)
CACHE(512)
CACHE(1024)
CACHE(2048)
CACHE(4096)
CACHE(8192)
CACHE(16384)
CACHE(32768)
CACHE(65536)
CACHE(131072)
#if KMALLOC_MAX_SIZE >= 262144
CACHE(262144)
#endif
#if KMALLOC_MAX_SIZE >= 524288
CACHE(524288)
#endif
...
#if KMALLOC_MAX_SIZE >= 33554432
CACHE(33554432)
CACHE(ULONG_MAX)
There is always a cache for allocations up to the maximum size that can be represented in anunsigned
longvariable. However, this cache (in contrast to all others) is not filled with elements in advance; this
allows the kernel to ensure that each giant allocationis satisfied with freshly allocated memory pages. As
allocations of this size can request the entire memory of the system, a corresponding cache would not be
particularly useful. However, this kernel approach makes sure that very large allocation requests can be
satisfied if sufficient memory is available.
The pointers to the corresponding caches are not initially filled. They are assigned their correct value
when initialization is performed withkmem_cache_init.
kmallocfrom<slab_def.h>first checks whether a constant is specified as the memory size; in this case,
the required cache can be determined statically at compilation time, and this delivers speed gains. If not,
__kmallocis invoked to find the cache of matching size. The function is a parameter-conversion front
end for__do_kmalloc:
mm/slab.c
void *__do_kmalloc(size_t size, gfp_t flags)
{
kmem_cache_t *cachep;