Linux Kernel Architecture

(Jacob Rumans) #1

Chapter3:MemoryManagement


Checking for a suitable chunk of memory is very simple: If an element is present in the examined list, it
can be used because it contains as many contiguous pages as needed. Otherwise, the kernel selects the
next higher allocation order and continues the search there.

Once a memory chunk has been removed from the list withlist_del,itsremovalmustbenotedby
decrementing thenr_freeelement ofstruct free_areaby 1. The per-zone statistics of the current
zone must also be updated accordingly with__mod_zone_page_state.rmv_page_orderis a helper func-
tion that deletes thePG_buddybit from the page flags — the page is not contained in the buddy system
anymore — and sets theprivateelement ofstruct pageto 0.

If the memory chunk to be allocated is smaller than the selected range of contiguous pages, that is, if the
pages stem from a higher allocation order than required because no suitable smaller block was available,
it must be split into smaller segments in accordance with the principles of the buddy system. This is done
by theexpandfunction.

mm/page_alloc.c
static inline struct page *
expand(struct zone *zone, struct page *page,
int low, int high, struct free_area *area)
int migratetype)
{
unsigned long size = 1 << high;

while (high > low) {
area--;
high--;
size >>= 1;
list_add(&page[size].lru, &area->free_list[migratetype]);
area->nr_free++;
set_page_order(&page[size], high);
}
return page;
}
This function uses a whole range of parameters. The meanings ofpage,zone,andareaare obvious.index
specifies the index position of the buddy pair in the allocation bitmap,lowis the desired allocation order,
andhighindicates the order from which the memory found was taken.migratetypesticks to its name
and denotes the migrate type.

It is best to look at the code step-by-step to understand how it works. Let us assume the following sit-
uation: A block withorder = 3is to be allocated. There is no block of this size in RAM, so the kernel
selects a block withorder = 5instead. For the sake of simplicity, this is located atindex=0. The function
is therefore invoked with the following parameters.

expand(page,index=0,low=3,high=5,area)

Figure 3-34 illustrates the steps described below that are needed to split the page (the previous contents
of thefree_arealists are not shown, only the new pages).


  1. The value ofsizeis initialized to 2high= 25 =32. The allocated memory area has already
    been removed from thefree_arealist in__rmqueueand is therefore shown with dashed
    lines in Figure 3-34.

  2. In the first loop pass, the kernel switches to the migrate-type-specificfree_arealist with the
    next smaller memory units, namely,area=4. Analogously, the area size reduces tosize=16

Free download pdf