Memory Management API: vmalloc details, memory management apivmalloc
Void * vmalloc (unsigned long size) is used to allocate memory for a segment of virtual address contact. Note that only the virtual address is consecutive and the physical address cannot be consecutive. The source code analysis is as follows:
Void * vmalloc (unsigned long size) {return _ vmalloc_node_flags (size, NUMA_NO_NODE, GFP_KERNEL);} call _ Empty inline void * _ Empty (unsigned long size, int node, gfp_t flags) {return _ vmalloc_node (size, 1, flags, PAGE_KERNEL, node, _ builtin_return_address (0);} Here _ builtin_return_address (0) the address of _ vmalloc_node_flags is saved as static void * _ vmalloc_node (unsigned long size, uns Igned long align, gfp_t gfp_mask, pgprot_t prot, int node, const void * caller) {return _ partition (size, align, VMALLOC_START, VMALLOC_END, gfp_mask, prot, 0, node, caller);} the virtual address can be found in VMALLOC_START ~ Void * _ forward (unsigned long size, unsigned long align, unsigned long start, unsigned long end, gfp_t gfp_mask, pgprot_t prot, unsigned long vm_flags, int node, const void * caller) {struct vm_struct * area; void * addr; unsigned long real_size = size; # Here we can know that the virtual address applied for by vmalloc is also the size of page alignment = PAGE_ALIGN (size); # exit if the size is zero or exceeds the maximum size, the maximum size is totalram_pagesif (! Size | (size> PAGE_SHIFT)> totalram_pages) goto fail; # apply for the body memory of a struct vm_struct * area = _ get_vm_area_node (size, align, VM_ALLOC | VM_UNINITIALIZED | vm_flags, start, end, node, gfp_mask, caller); if (! Area) goto fail; # apply for virtual continuous memory addr = _ vmalloc_area_node (area, gfp_mask, prot, node); if (! Addr) return NULL;/** In this function, newly allocated vm_struct has VM_UNINITIALIZED * flag. it means that vm_struct is not fully initialized. * Now, it is fully initialized, so remove this flag here. */# Clear the VM_UNINITIALIZED flag clear_vm_uninitialized_flag (area); # kmemleak_vmalloc (area, size, gfp_mask) used to detect vmalloc Memory leakage; # return the applied virtual address return addr; fail: # If the log fails, print the log and return the null address warn_alloc (gfp_mask, NULL, "vmalloc: allocation failure: % lu bytes", real_size); return NULL ;}