Article Title: linux memory management-discontinuous physical address allocation (vmalloc ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
We have analyzed how linux uses the partner System and the slab distributor to allocate memory. The physical addresses in these methods are consecutive. However, sometimes, when you request memory, it is not suitable for the system to allocate physical address contiguous memory blocks. You can use small memory blocks to "Connect" large memory blocks. this is also called "memory concatenation" in the operating system design. Obviously, memory concatenation requires a large amount of memory, which is more effective when memory access is not frequent.
The interfaces used to manage memory concatenation in Linux kernel are vmalloc/vfree. The linear addresses allocated by vmalloc are smooth, but the physical addresses are non-consecutive.
I. Preparation knowledge:
In Linux, The vm_struct structure is used to represent the linear address used by vmalloc. The linear address range used by vmalloc is VMALLOC_START VMALLOC_END. Borrow < > A pair of illustrations in, as shown below:
We can see that each vmalloc_area is separated by 4 kb, so that it is easy to capture cross-border access, because the middle is a "empty ".
Ii. Related Data Structures
The following describes the data structure of vmalloc area:
Struct vm_struct {
Void * addr; // virtual address
Unsigned long size; // vm size
Unsigned long flags; // vm flag
Struct page ** pages; // page mapped by vm
Unsigned int nr_pages; // page count
Unsigned long phys_addr; // The starting physical address.
Struct vm_struct * next; // The next vm. Used to form a linked list.
}
The global variable vmlist is used to manage the linked list composed of VMS.
The global variable vmlist is used to access the semaphores used by vmlist.
There are two common operations for vm_struct: get_vm_area/remove_vm_area
Get_vm_area: used to allocate a suitable vm structure. After the vm structure is successfully allocated, it is linked to vmlist. The code is shown in mm/vmalloc. c as follows:
// Size indicates the vm size.
Struct vm_struct * get_vm_area (unsigned long size, unsigned long flags)
{
// Find a suitable space in VMALLOC_START and VMALLOC_END
Return _ get_vm_area (size, flags, VMALLOC_START, VMALLOC_END );
}
// Parameter description:
// Start: start address end: end address size
Struct vm_struct * _ get_vm_area (unsigned long size, unsigned long flags,
Unsigned long start, unsigned long end)
{
Struct vm_struct ** p, * tmp, * area;
Unsigned long align = 1;
Unsigned long addr;
// If VM_IOREMAP is specified, adjust the alignment factor.
If (flags & VM_IOREMAP ){
Int bit = fls (size );
If (bit> IOREMAP_MAX_ORDER)
Bit = IOREMAP_MAX_ORDER;
Else if (bit <PAGE_SHIFT)
Bit = PAGE_SHIFT;
Align = 1ul <bit;
}
// Align the start address with the alignment factor
Addr = ALIGN (start, align );
// Allocate a vm_struct structure space
Area = kmalloc (sizeof (* area), GFP_KERNEL );
If (unlikely (! Area ))
Return NULL;
// PAGE_SIZE: 4KB in i32, that is, the gap holes mentioned above
Size + = PAGE_SIZE;
If (unlikely (! Size )){
Kfree (area );
Return NULL;
}
Write_lock (& vmlist_lock );
// Traverse vmlist: Find the final space of the appropriate size
For (p = & vmlist; (tmp = * p )! = NULL; p = & tmp-> next ){
// If the starting address falls in a vm interval, the starting address is changed to the end of the vm interval.
If (unsigned long) tmp-> addr <addr ){
If (unsigned long) tmp-> addr + tmp-> size> = addr)
Addr = ALIGN (tmp-> size +
(Unsigned long) tmp-> addr, align );
Continue;
}
// Size + addr <addr? Unless size = 0
If (size + addr) <addr)
Goto out;
// The gap in the middle can hold the vm of the next size. This indicates that such a vm has been found.
If (size + addr <= (unsigned long) tmp-> addr)
Goto found;
// Adjust the start address to the end address of the vm
Addr = ALIGN (tmp-> size + (unsigned long) tmp-> addr, align );
// If it is out of the range
If (addr> end-size)
Goto out;
}
Found:
// Locate the appropriate space, assign the area-> addr value to addr, and then link it to the vmlist.
Area-> next = * p;
* P = area;
Area-> flags = flags;
Area-> addr = (void *) addr;
Area-> size = size;
Area-> pages = NULL;
Area-> nr_pages = 0;
Area-> phys_addr = 0;
Write_unlock (& vmlist_lock );
Return area;
Out:
// No suitable space found. An error is returned.
Write_unlock (& vmlist_lock );
Kfree (area );
If (printk_ratelimit ())
Printk (KERN_WARNING "allocation failed: out of vmalloc space-use vmalloc = To increase size. \ n ");
Return NULL;
}
[1] [2] [3] [4] Next page