Allocator API

As far as the interface to the buddy system is concerned, it makes no difference whether a NUMA or a UMA architecture is used as the call syntax is the same for both. Common to all functions is the fact that pages can only be allocated in integer powers of 2. For this reason, the desired memory size is not specified as parameter as it would be in the malloc function of the C standard library or in the bootmem allocator. Instead, the order of the allocation must be specified, and this causes the buddy system to reserve 2order pages in memory. Finer-grained allocation in the kernel is only possible with the help of the slab allocator (or alternatively, the slub or slob allocators), which builds on the buddy system (Section 3.6 gives further details).

□ alloc_pages(mask, order) allocates 2order pages and returns an instance of struct page to represent the start of the reserved block. alloc_page(mask) is a shorter notation for order =0 if only one page is requested.

□ get_zeroed_page(mask) allocates a page and returns a page instance but fills the page with zeros (with all other functions, page contents are undefined after allocation).

□ _get_free_pages(mask, order) and_get_free_page(mask) work in the same way as the above functions but return the virtual address of the reserved memory chunk instead of a page instance.

□ get_dma_pages(gfp_mask,order) allows for obtaining pages suitable for DMA.

If allocation fails because insufficient memory is free to satisfy the request, all the above functions return either a null pointer (alloc_pages and alloc_page) or the value 0 (get_zeroed_page,

_get_free_pages, and_get_free_page). The kernel must therefore check the result returned after every allocation attempt. This practice is not different from any well-designed userland applications, but neglecting the check in the kernel will lead to much more severe failures.

The kernel provides other memory management functions in addition to the buddy system functions. They build on layers that are used as a basis by the buddy system but do not belong to the buddy allocator itself. These functions are vmalloc and vmalloc_32, which use page tables to map discontiguous memory into kernel address space so that it appears to be contiguous. There is also a set of functions of the kmalloc type to reserve memory areas smaller than a complete page. Their implementation is discussed separately in later sections of this chapter.

Four slightly different functions are defined to return pages no longer needed in memory to the kernel.

□ free_page(struct page*) and free_pages(struct page*, order) return one or 2order pages to memory management. The start of the memory area is indicated by means of a pointer to the first page instance of the area.

□ _free_page(addr) and_free_pages(addr, order) operate in the same way as the functions just mentioned but use a virtual memory address instead of a page instance to select the memory area to be returned.

Continue reading here: Info

Was this article helpful?

0 0