Article count:1086 Read by:1552441

Account Entry

Linux memory management vmalloc

Latest update time:2021-08-31 20:06
    Reads:

Enter vmalloc

According to the previous series of articles, we know that the buddy system is based on the page frame allocator, kmalloc is based on the slab allocator, and these allocated addresses are all continuous physical memory. However, with the accumulation of fragmentation, the allocation of continuous physical memory becomes difficult. For those non-DMA accesses, continuous physical memory is not necessarily required. It is completely possible to map discontinuous physical memory page frames to continuous virtual address space like malloc. This is the origin of vmap) (provides mapping of discrete pages to continuous virtual address space). The allocation of vmalloc is based on this mechanism.

Vmalloc allocates at least one page, and the allocated pages are not guaranteed to be continuous because vmalloc calls alloc_page multiple times internally to allocate a single page.

The vmalloc area is between VMALLOC_START - VMALLOC_END in the above figure, which can be viewed through /proc/vmallocinfo.

data structure

  • vmap_area describes an area of ​​a virtual address. The struct vm_struct can be constructed into a linked list to maintain multiple mappings.
struct vmap_area {
 unsigned long va_start; //vmalloc申请虚拟地址返回的起始地址
 unsigned long va_end; //vmalloc申请申请虚拟地址返回的结束地址
 unsigned long flags;
  //挂接到vmap_area_root红黑树
 struct rb_node rb_node;         /* address sorted rbtree */
  //挂接到vmap_area_list链表
 struct list_head list;          /* address sorted list */
 struct llist_node purge_list;    /* "lazy purge" list */
 //如果当前VA处于使用状态(即在vmap_area_root为根的红黑树中和vmap_area_list链表中),vm有效,指向用于管理虚拟地址和物理页之间的映射关系的描述符
 struct vm_struct *vm;
 struct rcu_head rcu_head;
};
  • vm_struct manages the mapping between virtual addresses and physical pages
struct vm_struct {
 struct vm_struct *next; //指向下一个vm结构体
 void   *addr; //当前vmalloc区域的虚拟地址的起始地址
 unsigned long  size; //当前vmalloc区域的虚拟地址的大小
 unsigned long  flags;
 //vamlloc分配获取的各个物理页面并是不连续的,每个物理页面用struct page描述,一个vm_struct对用到的管理所有物理页面的struct page构成一个数组,而pages就是指向这个数组的指针。
 struct page  **pages;
 unsigned int  nr_pages; //vmalloc映射的page数目
 phys_addr_t  phys_addr; //用来映射硬件设备的IO共享内存,其他情况下为0
 const void  *caller; //调用vmalloc函数的函数的地址
};

vmalloc

It mainly consists of three steps:

  1. Find the free virtual address space (hole) from VMALLOC_START to VMALLOC_END
  2. Call alloc_page to allocate individual pages in turn, depending on the size being allocated.
  3. Map the single allocated page to the consecutive virtual addresses found in step 1. Map the single allocated page to the consecutive virtual addresses found in step 1.


5T technical resources are available for free! Including but not limited to: C/C++, Arm, Linux, Android, artificial intelligence, microcontrollers, Raspberry Pi, etc. Reply " peter " in the official account to get them for free! !


Remember to click Share , Like and Watching , give me some power

 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us About Us Service Contact us Device Index Site Map Latest Updates Mobile Version

Site Related: TI Training

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

EEWORLD all rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2021 EEWORLD.com.cn, Inc. All rights reserved