Linux Kernel Architecture

(Jacob Rumans) #1

Chapter3:MemoryManagement


various lists. As this is not necessary for pages on the slab cache, the pointers can be used for other
purposes:

❑ page->list.nextpoints to the management structure of the cache in which the page resides.
❑ page->list.prevpoints to the management structure of the slab on which the page is held.

Management
area

Figure 3-48: Slab with external
(off-slab)slabheader.

Setting or reading this information is concealed behind theset_page_slabandget_page_slab,respec-
tively,_cachefunctions to lower thehack valueof this convention.

mm/slab.c
void page_set_cache(struct page *page, struct kmem_cache *cache)
struct kmem_cache *page_get_cache(struct page *page)

void page_set_slab(struct page *page, struct slab *slab)
struct slab *page_get_slab(struct page *page)

Additionally, the kernel sets the page flagPG_SLABfor each physical page, that is allocated for the slab
allocator.

3.6.4 Implementation


Various data structures are used to implement the slab allocator as described above. Although this does
not appear to be difficult, the code is not always easy to read or understand. This is because many mem-
ory areas need to be manipulated using pointer arithmetic and type-casting — not necessarily one of the
areas of C famed for its clarity. The code is also pervaded with pre-processor statements because the slab
system features numerous debugging options.^31 Some of these are listed below:

❑ Red Zoning— An additional memory area filled with a known byte pattern is placed at the start
and end of each object. If this pattern is overwritten, programmers will note when analyzing
kernel memory that their code accesses memory areas that don’t belong to them.
❑ Object Poisoning— Objects are filled with a predefined pattern when a slab is created and
freed. If it is noted at object allocation that this pattern is changed, programmers know that
unauthorized access has already taken place.

For the sake of simplicity and to focus attention on the big picture rather than minor details, let’s restrict
our description below to a ‘‘pure‘‘ slab allocator that doesn’t make use of the above options.

(^31) TheCONFIG_DEBUG_SLABconfiguration option must be set at compilation time to enable debugging. However, this significantly
slows allocator performance.

Free download pdf