4.6. Memory Request PathUntil now, we have approached the description of the slab allocator as though it were independent of any actual memory request. With the exception of the cache initialization functions, we have not tied together how all these functions come to be called. Now, we look at the flow of control associated with memory requests. When the kernel must obtain memory in byte-sized groupings, it uses the kmalloc() function, which eventually makes the call to kmem_getpages as follows: kmalloc()->__cache_alloc()->kmem_cache_grow()->kmem_getpages() 4.6.1. kmalloc()The kmalloc() function allocates memory objects in the kernel: ----------------------------------------------------------------------------- mm/slab.c 2098 void * __kmalloc (size_t size, int flags) 2099 { 2100 struct cache_sizes *csizep = malloc_sizes; 2101 2102 for (; csizep->cs_size; csizep++) { 2103 if (size > csizep->cs_size) 2104 continue; ... 2112 return __cache_alloc(flags & GFP_DMA ? 2113 csizep->cs_dmacachep : csizep->cs_cachep, flags); 2114 } 2115 return NULL; 2116 } ----------------------------------------------------------------------------- 4.6.1.1. sizeThis is the number of bytes requested. 4.6.1.2. flagsIndicates the type of memory requested. These flags are passed on to the buddy system without affecting the behavior of kmalloc().Table 4.6 shows the flags, and they are covered in detail in the "Buddy System" section.
Lines 21022104Find the first cache with objects greater than the size requested. Lines 21122113Allocate an object from the memory zone specified by the flags parameter. 4.6.2. kmem_cache_alloc()This is a wrapper function around __cache_alloc(). It does not perform any additional functionality because its parameters are passed as is: ----------------------------------------------------------------------------- mm/slab.c 2070 void * kmem_cache_alloc (kmem_cache_t *cachep, int flags) 2071 { 2072 return __cache_alloc(cachep, flags); 2073 } ----------------------------------------------------------------------------- 4.6.2.1. cachepThe cachep parameter is the cache descriptor of the cache from which we want to allocate objects. 4.6.2.2. flagsThe type of memory requested. This is passed directly as indicated to kmalloc(). To free byte-sized memory allocated with kmalloc(), the kernel provides the kfree() interface, which takes as a parameter the pointer to the memory returned by kmalloc(). Figure 4.9 illustrates the flow from kfree to kmem_freepages. Figure 4.9. kfree() Call Graph |