Chapter3:MemoryManagement
Memory is not allocated from the buddy system in a single chunk but page-by-page.
This is a key aspect ofvmalloc. If it were certain that a contiguous allocation could
be made, there would be no need to usevmalloc. 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
vmallocwill still work even when physical memory is fragmented.
The kernel invokesmap_vm_areato map the scattered physical pages contiguously into the virtual
vmallocarea. 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 theflush_cache_vmapwhose definition is architecture-specific. Depending
on CPU type, this includes the required low-level assembler statements to flush the cache, an invoca-
tion offlush_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.
Alternative MappingMethods
Besidesvmalloc, there are other ways of creating virtuallycontiguous mappings. All are based on the
__vmallocfunction discussed above or make use of a very similar mechanism (not discussed here).
❑ vmalloc_32works in the same way asvmallocbut ensures that the physical memory used can
always be addressed by means of regular 32-bit pointers. This is important if an architecture can
address more memory than would normally be possible on the basis of its word length; this is
the case, for example, on IA-32 systems with enabled PAE.
❑ vmapuses apagearray as its starting point to create a virtually contiguous memory area. In con-
trast tovmalloc, the physical memory location is not allocated implicitly but must be passed
ready-made to the function. Mappings of this kind can be recognized by theVM_MAPflag in their
vm_mapinstance.
❑ Unlike all mapping methods described above,ioremapis a processor-specific function that must
be implemented on all architectures. It enables a chunk taken from the physical address space
used by the system buses for I/O operations to be mapped into the address space of the kernel.
This function is used predominantly in device drivers to make the address areas used for com-
munication with the peripherals available to the rest of the kernel (and, of course, to itself).
FreeingMemory
Two functions return memory to the kernel —vfreefor areas allocated byvmallocandvmalloc_32,
andvunmapfor mappings created usingvmaporioremap.Bothleadbackto__vunmap.
mm/vmalloc.c
void __vunmap(void *addr, int deallocate_pages)
addrindicates the start address of the area to be freed, anddeallocate_pagesspecifies whether the
physical pages associated with the area are to be returned to the buddy system.vfreesets the parameter