Chapter3:MemoryManagement
rmv_page_order(page);
__mod_zone_page_state(zone, NR_FREE_PAGES,
-(1UL << order));
...
expand(zone, page, order, current_order, area, migratetype);
return page;
}
}
...
Notice that the new migrate type is used byexpandif the kernel has decided to change it before. Other-
wise, the remainders are put back onto their original migrate list.
Finally, one more scenario must be considered: What if the allocation cannot be satisfied despite all page
orders and all migrate types have been taken into account? In this case, the kernel can try to fulfill the
allocation from theMIGRATE_RESERVElist, which serves as a last resort:
mm/page_alloc.c
/* Use MIGRATE_RESERVE rather than fail an allocation */
return __rmqueue_smallest(zone, order, MIGRATE_RESERVE);
}
3.5.6 Freeing Pages
__free_pagesis the base function used to implement all functions of the kernel API. Its code flow dia-
gram is shown in Figure 3-35.
Yes
No
__free_pages
_ _free_pages_ok __free_one_page
Single page? free_hot_page
Figure 3-35: Code flow diagram for__free_pages.
__free_pagesfirst establishes whether a single page or a larger contiguous block is to be freed. If a single
page is freed, it is not returned to the buddy system but is placed in the per-CPU cache — in thewarmlist
for all pages that are highly likely to reside in the CPU cache. For this purpose, the kernel provides the
free_hot_pagehelper function, which is a parameter conversion function forfree_hot_cold_pagethat
is invoked in turn.
Iffree_hot_cold_pagedetermines that the number of pages in the per-CPU cache exceeds the limit
set bypcp->count, a whole batch of pages — whose size is specified bypcp->batch—isreturned
to the buddy system. This strategy is known aslazy coalescing.Itpreventslargenumbersofwasteful
coalescing operations that would be carried out if single pages were returned to the buddy system and
then immediately split to satisfy subsequent allocation requests. Thefree_pages_bulkfunction is used
to return pages to the buddy system.