Figure 339 Code flow diagram for vmalloc

If an explicit node is specified from which the pages are to be allocated, the kernel invokes alloc_pages_node. Otherwise, pages are taken from the current node using alloc_page.

The pages are removed from the buddy system of the relevant node; when this is done, vmalloc sets gfp_mask to gfp_kernel |_gfp_highmem — the kernel instructs memory management to take the pages from zone_highmem if possible. The reasons for this were given above: Pages from the lower-memory areas are more valuable and should therefore not be wasted for vmalloc allocations that could just as well be satisfied with high-memory pages.

Memory is taken from the buddy system, and gfp_mask is set to gfp_kernel | _gfp_highmem so that the kernel instructs memory management to take the pages from zone_highmem if possible. We have already seen the reasons.

Memory is not allocated from the buddy system in a single chunk but page-by-page. This is a key aspect of vmalloc. If it were certain that a contiguous allocation could be made, there would be no need to use vmalloc. After all, the whole purpose of the function is to reserve large memory chunks even though they may not be contiguous owing to fragmentation of the available memory. Splitting the allocation into the smallest possible units — in other words, individual pages — ensures that vmalloc will still work even when physical memory is fragmented.

The kernel invokes map_vm_area to map the scattered physical pages contiguously into the virtual vmalloc area. This function iterates over the reserved physical pages and allocates the required number of entries in the various page directories and in the page tables.

Some architectures require flushing of the CPU caches after the page tables have been modified. The kernel therefore invokes the flush_cache_vmap whose definition is architecture-specific. Depending on CPU type, this includes the required low-level assembler statements to flush the cache, an invocation of flush_cache_all (if there is no function to flush selective virtually mapped areas), or an empty procedure if the CPU is not reliant on cache flushing, as is the case with IA-32.

Continue reading here: Alternative Mapping Methods

Was this article helpful?

0 0