The Buddy System Algorithm

目錄

1. Why need the Buddy System Algorithm

2. What's the Buddy System Algorithm

2.1 how to find buddy

3. Code implementation with comments

4. buddy system algorithm in linux kernel


1. Why need the Buddy System Algorithm

Memory consists of contiguous page frames, and kernel need to manage these page frames. Kernel need a robust and efficient strategy for allocating groups of contiguous page frames. In doing so, kernel need deal with the well-known external fragmentation. To address external fragmentation, kernel try to avoid as much as possible to split large free block to satisfy a request for a smaller one (otherwise, externel fragmentation happens if split a large free block for a smaller block request when there is smaller block can satisfy this request). The algorithm is the Buddy System Algorithm.

2. What's the Buddy System Algorithm

The buddy system algorithm (ULK p312),

1. groups all free page frames into several lists (shown in upper picture), each list has several blocks, each block has 2^n page frames. So if we group all page frames into 4 lists (L0, L1, L2, L3), then each list consists of blocks with 1, 2, 4, 8 page frames, that is each block in list L2 has 2^2 (4) page frames.

2. when allocating, assume we need a block of 2 page frames. It first check whether a list (L1) with blocks of 2 page frames has a free block, if yes, return this block. If no, it checks whether a list (L2) with blocks of 4 pages frames has a free block; if still no, it check whether a list (L3) with blocks of 8 pages frames has a free block. If no, return error for no memory. If yes, it return first 2 page frames of the block, then add next 2 page frames to the list (L1) with blocks of 2 page frames, and add the last 4 page frames to the list (L2) with blocks of 4 page frames.

As upper picture shows, at first only L3 has 2 blocks of 2^3 page frames, L0,L1,L2 all has no blocks. So if we need a block of 2 page frames, we first check L1 and find out no block available, then check L2, then L3. Since block in L3 is larger than requested, we split it into several smaller blocks, as picture shows, page frame 0/1 is returned; 2/3 is moved to L1, 4-7 is moved to L2. Future request for block of 2 page frames can be fulfilled directly by L1 since L1 now has a block.

3. when free, assume we free a block of 2 page frames. It first put this block to list (L1) with blocks of 2 page frames. Now the name 'buddy' comes into play. It then checks if we can merge this block with another block (buddy) of same list, into a larger block with 4 page frames. If no, nothing to be done. If yes, it remove these 2 blocks from L1, and insert to L2 as a single block. It continues merge blocks to larger block until the largest block.

Assuming we free block of page frame 0/1 which is requested as shown by last picture, we first put it in L1 and find out that block with page frame 2/3 is a buddy, so merge these 2 blocks into a larger one, then remove it from L1 and insert it to L2. In L2, we find out that block with page frame 4-7 is a buddy, so merge these 2 blocks into a larger one and remove it from L2 and insert it to L3.

2.1 how to find buddy

Maybe the hard part is how to find a buddy when return block of page frames to buddy system. Like uppper picture shows, assume L0 has 2 blocks of pf 0 & pf 1, now we need return/free block of pf 1. So which one is buddy of pf 1? pf 0 or pf 2? The rule is the starting address of new merged block should be a multiple of block size (and of course the buddy block need locate at contiguous address as pf 1). Assume each page frame has size 1 for simplicity (it is actually 4k normally). So the address of each page frame is 0, 1, and 2. And L0 has block of size 1 (2^0), L1 has block of size 2 (2^1). For pf1, if we choose pf 0 as buddy to form a larger block, then the starting address of new block is 0 which is the address of pf 0. Address 0 is a multiple of new block size 2, so pf 0 is a valid buddy. If we choose pf2, the new starting addresss is 1 which is the address of pf 1, clearly 1 is not a multiple of new block size 2, so pf2 is not a valid buddy.

3. Code implementation with comments

code for split large block into smaller ones

size = 1 << curr_order;
while (curr_order > order) {
    area--;
    curr_order--;
    
	/* this is how to split large block into smaller one */
    size >>= 1;
    buddy = page + size;
	
    /* insert buddy as first element in the list */
    list_add(&buddy->lru, &area->free_list);
    
    area->nr_free++;
    buddy->private = curr_order;
    SetPagePrivate(buddy);
}

code for merge blocks into larger one

while (order < 10) {
    /* this is how to find buddy */
    buddy_idx = page_idx ^ (1 << order);
    buddy = base + buddy_idx;
    if (!page_is_buddy(buddy, order))
        break;
		
    list_del(&buddy->lru);
    zone->free_area[order].nr_free--;
    ClearPagePrivate(buddy);
    buddy->private = 0;
	
	/* new block's start address */
    page_idx &= buddy_idx;
    order++;
}

4. buddy system algorithm in linux kernel

In linux kernel, memory is divided into several nodes due to access time by cpu, each node is divide into several zones due to some hardware limitation. Each zone has its own buddy system. (need a picture to better illustrate it)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章