The Linux Kernel Primer. A Top-Down Approach for x86 and PowerPC Architectures

4.6. Memory Request Path

Until 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. size

This is the number of bytes requested.

4.6.1.2. flags

Indicates 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.

Table 4.6. vm_area_struct->vm_flags Values

Flag

Description

VM_READ

Pages in this region can be read.

VM_WRITE

Pages in this region can be written.

VM_EXEC

Pages in this region can be executed.

VM_SHARED

Pages in this region are shared with another process.

VM_GROWSDOWN

The linear addresses are added onto the low side.

VM_GROWSUP

The linear addresses are added onto the high side.

VM_DENYWRITE

These pages cannot be written.

VM_EXECUTABLE

Pages in this region consist of executable code.

VM_LOCKED

Pages are locked.

VM_DONTCOPY

These pages cannot be cloned.

VM_DNTEXPAND

Do not expand this virtual memory area.

Lines 21022104

Find the first cache with objects greater than the size requested.

Lines 21122113

Allocate 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. cachep

The cachep parameter is the cache descriptor of the cache from which we want to allocate objects.

4.6.2.2. flags

The 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

Категории