Linux memory management (thirty-three): vmalloc detailed explanation

Source code based on: Linux 5.4

0. Preface

The three functions kmalloc(), vmalloc(), and malloc() are commonly used memory allocation functions, but they have essential differences.

kmalloc() is based on the slab allocator and is built on a large memory block with continuous physical addresses, so the memory allocated by kmalloc() is physically continuous, and the virtual memory mapped by kmalloc() is also continuous in the linear area. For details, please refer to Section 5.1.2 of the article "kmem_cache_alloc of slub allocator" .

Compared with kmalloc(), vmalloc() is implemented for virtual memory contiguousness, while physical memory does not need to be contiguous. It is precisely because the physical memory is not continuous, but the virtual memory needs to be continuous, so each page needs to be mapped, so the efficiency of vmalloc() is not very high, and it will only be used when it is necessary (for example, a large memory needs to be used). In addition, vmalloc() can sleep during the process of applying for memory, so it cannot be used in interrupt context.

Compared with the above two functions, malloc() is the memory allocation function of the user layer, and will eventually make system calls through brk() and mmap().

1. vmalloc initialization

init/main.c

static vo

Guess you like

Origin blog.csdn.net/jingerppp/article/details/130194482