Buddy System Algorithm

To avoid external fragmentation, Linux adopts the approach of keeping track of all free page frames. To achieve this goal, the Kernel uses Buddy System Algorithms.

Key data structure and source code area in Kernel

struct zone
{
  struct free_area[11];
}

  struct free_area {
    struct list_headfree_list[MIGRATE_TYPES];
    unsigned longnr_free;
  };

#define MIGRATE_UNMOVABLE     0
#define MIGRATE_RECLAIMABLE   1
#define MIGRATE_MOVABLE       2
#define MIGRATE_PCPTYPES      3 /* the number of types on the pcp lists */
#define MIGRATE_RESERVE       3
#define MIGRATE_ISOLATE       4 /* can't allocate from here */
#define MIGRATE_TYPES         5

private field in struct page stores the order of the block, that is, the number k

conditions for buddy blocks
1. same size b
2. contigous
3. first page frame's physical address = multiple 2b * 4KB
TAccording to the three conditions, a block has and only has onebuddy block.

Allocating a Block
for (current_order = order; current_order < MAX_ORDER; ++current_order) {
    area = &(zone->free_area[current_order]);
    if (list_empty(&area->free_list[migratetype]))
    continue;

    page = list_entry(area->free_list[migratetype].next,
                    struct page, lru);
    list_del(&page->lru);
    rmv_page_order(page);
    area->nr_free--;
    expand(zone, page, order, current_order, area, migratetype); //reassign free page frames if current_order > order
    return page;
}

Freeing a Block
Both block allocationg and deallocating functions have local interrupts disabled.
while (order < MAX_ORDER-1) {
    buddy_idx = __find_buddy_index(page_idx, order);
    buddy = page + (buddy_idx - page_idx);
    if (!page_is_buddy(page, buddy, order))
    break;

    /* Our buddy is free, merge with it and move up one order. */
    list_del(&buddy->lru);
    zone->free_area[order].nr_free--;
    rmv_page_order(buddy);
    combined_idx = buddy_idx & page_idx;
    page = page + (combined_idx - page_idx);
    page_idx = combined_idx;
    order++;
}
set_page_order(page, order);

你可能感兴趣的:(linux,kernel,System,Buddy,Algorithm;)