One: Vmalloc
Http://www.360doc.com/content/14/0614/13/18127083_386524093.shtml
The kernel entry function for 1,vmalloc () is the void *vmalloc (unsigned long size) in KERNEL/MM/VMALLOC.C, which represents the number of bytes allocated by the request kernel.
[CPP]View Plaincopy
- void *vmalloc (unsigned long size)
- {
- return __vmalloc_node_flags (SIZE,-1,GFP_KERNEL|__GFP_HIGHMEM);
- }
From the above code, VMALLOC () is mainly from the high-end memory area to allocate memory.
The Vmalloc () function will eventually be called to __vmalloc_node_range (unsigned long size,unsigned long align,unsigned long start,unsigned long END,GFP _t gfp_mask,,,,) function
The above function size represents the size of a specific allocated byte, align represents the size of the alignment, the default is 1,start and end respectively represents the starting and ending addresses of the Vmalloc partition area, and the ARM architecture defaults to the following:
[CPP]View Plaincopy
- #define VMALLOC_OFFSET (8*1024*1024)
- #define Vmalloc_start ((unsigned long) high_memory+vmalloc_offset) &~ (vmalloc_offset-1))
- #define Vmalloc_end 0xff000000ul//4080m
The things __vmalloc_node_range do are as follows:
1. First check that the requested allocated memory size has exceeded the maximum number of physical pages. If more than 0 is returned, the allocation fails.
Size = page_align (size);
if (!size | | (Size >> Page_shift) > Num_physpages)
return NULL;
2, call the __get_vm_area_node () function,
This function, using Kmalloc in slab, allocates vm_struct data structures.
[CPP]View Plaincopy
- Area = Kmalloc_node (sizeof (*area), Gfp_kernel, node);
- UCT Vm_struct {
- void *addr; //start of virtual address
- unsigned long size; //Allocation size
- unsigned long flags; //Flag bit
- struct page **pages; //The corresponding page
- unsigned int nr_pages; //number of pages
- unsigned long phys_addr; //Physical Address
- struct Vm_struct *next; //single-linked list, pointing to the next VM node
Next, find the appropriate location in the single-linked list vmlist and insert the new VM node into the single-linked list.
[CPP]View Plaincopy
- for (P = &vmlist; (TMP = *p)! = NULL;p = &tmp->next) {
- if ((unsigned long) tmp->addr < addr) {
- if ((unsigned long) tmp->addr + tmp->size >= addr)
- Addr = ALIGN (tmp->size +
- (unsigned long) tmp->addr, align);
- continue;
- }
- if ((size + addr) < addr)
- goto out; //Address out of bounds
- if (size + addr <= (unsigned long) tmp->addr)
- Goto found; //Find success. To insert an operation
- Addr = ALIGN (tmp->size + (unsigned long) tmp->addr, ALIGN);
- if (addr > End-size)
- goto out; //Address out of bounds
- }
- Insert the new VM node into the vmlist.
- Area->next = *p;
- *p = area;
- Initializing new nodes
- Area->flags = flags;
- Area->addr = (void *) addr;
- area->size = size;
- Physical memory is not yet assigned.
- Area->pages = NULL;
- area->nr_pages = 0;
- area->phys_addr = 0;
3, call the __vmalloc_area_node () function
Next, the pages and Nr_pages fields in the VM_STRUCT structure are initialized.
A) Initialize the Nr_pages field
Nr_pages = (area->size-page_size) >> page_shift;
b) initialize the pages array
Calculate array size
Array_size = (nr_pages * sizeof (struct page *));
If the array size is greater than 1 pages, it is allocated in the discontinuous zone, otherwise it is allocated in the contiguous area
[CPP]View Plaincopy
- if (array_size > Page_size)
- Pages = __vmalloc_node (array_size, Gfp_mask, Page_kernel, node);
- Else
- Pages = Kmalloc_node (Array_size, (Gfp_mask & ~__gfp_highmem), node);
- area->pages = pages;
Allocating physical memory pages from a partner system
[CPP]View Plaincopy
- for (i = 0; i < area->nr_pages; i++) {
- if (node < 0)
- Area->pages[i] = Alloc_page (gfp_mask); //For Uma
- Else
- Area->pages[i] = alloc_pages_node (node, gfp_mask, 0); //For NUMA
- if (unlikely (!area->pages[i])) {
- /* Successfully allocated I pages, free them in __vunmap () */
- Area->nr_pages = i;
- Goto fail;
- }
- }
Finally, a page table mapping is established based on the physical memory that is actually applied. Flush TLB Flag
4, call the Insert_vmalloc_vmlist () function
Inserting the VM_STRUCT structure into the vmlist linked list
5, call the Kmemleak_alloc () function.
[Linux Memory]vmalloc Learning notes