


Linux memory management related functions: 1. kmalloc(), used for memory allocation in kernel mode; 2. vmalloc(), generally used for functions that only exist in software (no corresponding hardware meaning) Larger sequential buffers allocate memory; 3. alloc_page() and alloc_pages() functions can be allocated in the kernel space; 4. __get_free_pages() series of functions return the virtual address of one or more pages; 5. kmem_cache_alloc(), etc. .
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
This article describes several common memory allocation functions in the Linux kernel and their similarities and differences, so as to have a better understanding of the underlying memory allocation mechanism of Linux.
1.kmalloc()
kmalloc() function is similar to our common malloc() function. The former is used for kernel state memory. allocation, the latter is used in user mode.
The kmalloc() function allocates a continuous storage space in physical memory. Like the malloc() function, it will not clear the original data inside. If the memory is sufficient, its allocation speed is very fast. . Its prototype is as follows:
static inline void *kmalloc(size_t size, gfp_t flags); /*返回的是虚拟地址*/
- size: The memory size to be allocated. Due to the Linux memory management mechanism, memory can only be allocated according to the page size (generally 4KB for 32-bit machines and 8KB for 64-bit machines). This results in the system still returning when we only need a few bytes of memory. One page of memory is obviously extremely wasteful. Therefore, unlike malloc, kmalloc's processing method is: the kernel first allocates a series of memory pools of different sizes (32B, 64B, 128B,..., 128KB). When memory needs to be allocated, the system will allocate memory pools greater than or equal to Give it the smallest memory pool that requires memory. That is, the memory allocated by kmalloc has a minimum of 32 bytes and a maximum of 128KB. If it exceeds 128KB, you need to sample other memory allocation functions, such as vmalloc().
-
flag: This parameter is used to control the behavior of the function. The most commonly used one is GFP_KERNEL, which means that when there is currently not enough memory allocated, the process goes to sleep and waits for the system to store the contents of the buffer. After SWAP to the hard disk, obtain enough memory and then wake up the process to allocate it. See the figure below for more flags:
- When using the GFP_ KERNEL flag to apply for memory, if it cannot be satisfied temporarily, the process will sleep and wait for the page, which will cause blocking, so it cannot be used in the interrupt context or Use GFP_KERNE to apply for memory when holding a spin lock. Therefore, it cannot block in non-process contexts such as interrupt handlers, tasklets, and kernel timers. In this case, the driver should use the GFP_ATOMIC flag to apply for memory. When using the GFP_ATOMIC flag to apply for memory, if there is no free page, it will return directly without waiting.
- In addition to the flags listed in the above table, it also includes the following :
- _ _GFP_DMA (required to be allocated in a memory area capable of DMA)
- _ _GFP_HIGHMEM (Indicates that the allocated memory can be located in high memory)
- _ _GFP_COLD (Requests a page that has not been accessed for a long time)
- _ _GFP_NOWARN (Prevents the kernel from issuing a warning when an allocation cannot be satisfied)
- _ _GFP_HIGH (high priority request, allows to obtain the last memory page reserved by the kernel for emergency use)
- _ _GFP_REPEAT (retry best effort if allocation fails)
- _ _GFP_NOFAIL (mark only allows successful application, not recommended)
- _ _GFP_NORETRY (if the application cannot be obtained, give up immediately)
- Memory applied for using kmalloc() should use
kfree ()
Release, the usage of this function is similar to free() in user space.
2. vmalloc()
vmalloc()
is generally used only in software (has no corresponding hardware meaning) allocates memory in a larger sequential buffer. When the memory does not have a large enough continuous physical space to allocate, this function can be used to allocate continuous virtual addresses but non-continuous physical addresses of memory. Since a new page table needs to be created, its overhead is much greater than kmalloc and the __get_free_pages()
function that will be discussed later. And vmalloc()
cannot be used in an atomic context because its internal implementation uses kmalloc()
with the flag GFP_KERNEL. Its function prototype is as follows:
void *vmalloc(unsigned long size); void vfree(const void *addr);
- 使用 vmalloc 函数的一个例子函数是
create_module()
系统调用,它利用 vmalloc()函数来获取被创建模块需要的内存空间。 - 内存分配是一项要求严格的任务,无论什么时候,都应该对返回值进行检测。
- 在驱动编程中可以使用
copy_from_user()
对内存进行使用。下面举一个使用vmalloc函数的示例:
static int xxx(...) { ... cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent); if(!cpuid_entries) goto out; if(copy_from_user(cpuid_entries, entries, cpuid->nent * sizeof(struct kvm_cpuid_entry))) goto out_free; for(i=0; i<cpuid->nent; i++){ vcpuid->arch.cpuid_entries[i].eax = cpuid_entries[i].eax; ... vcpuid->arch.cpuid_entries[i].index = 0; } ... out_free: vfree(cpuid_entries); out: return r; }
3、页分配函数
在linux中,内存分配是以页为单位的,32位机中一页为4KB,64位机中,一页为8KB,但具体还有根据平台而定。
根据返回值类型的不同,页分配函数分为两类,一是返回物理页地址,二是返回虚拟地址。虚拟地址和物理地址起始相差一个固定的偏移量。
#define __pa(x) ((x) - PAGE_OFFSET) static inline unsigned long virt_to_phys(volatile void *address) { return __pa((void *)address); } #define __va(x) ((x) + PAGE_OFFSET) static inline void* phys_to_virt(unsigned long address) { return __va(address); }
根据返回页面数目分类,分为仅返回单页面的函数和返回多页面的函数。
3.1 alloc_page()和alloc_pages()函数
该函数定义在头文件/include/linux/gfp.h
中,它既可以在内核空间分配,也可以在用户空间分配,它返回分配的第一个页的描述符而非首地址,其原型为:
#define alloc_page(gfp_mask) alloc_pages(gfp_mask, 0) #define alloc_pages(gfp_mask, order) alloc_pages_node(numa_node_id(), gfp_mask, order) //分配连续2^order个页面 static inline struct page *alloc_pages_node(int nid, gfp_t gfp_mask, unsigned int order) { if(unlikely(order >= MAX_ORDER)) return NULL; if(nid < 0) nid = numa_node_id(); return __alloc_pages(gfp_mask, order, noed_zonelist(nid, gfp_mask)); }
3.2 __get_free_pages()系列函数
它是kmalloc函数实现的基础,返回一个或多个页面的虚拟地址。该系列函数/宏包括 get_zeroed_page()
、_ _get_free_page()
和_ _get_free_pages()
。在使用时,其申请标志的值及含义与 kmalloc()
完全一样,最常用的是 GFP_KERNEL 和 GFP_ATOMIC。
/*分配多个页并返回分配内存的首地址,分配的页数为2^order,分配的页不清零。 order 允许的最大值是 10(即 1024 页)或者 11(即 2048 页),依赖于具体 的硬件平台。*/ unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order) { struct page *page; page = alloc_pages(gfp_mask, order); if(!page) return 0; return (unsigned long)page_address(page); } #define __get_free_page(gfp_mask) __get_free_pages(gfp_mask, 0) /*该函数返回一个指向新页的指针并且将该页清零*/ unsigned long get_zeroed_page(unsigned int flags);
- 使用
_ _get_free_pages()
系列函数/宏申请的内存应使用free_page(addr)
或free_pages(addr, order)
函数释放:
#define __free_page(page) __free_pages((page), 0) #define free_page(addr) free_pages((addr), 0) void free_pages(unsigned long addr, unsigned int order) { if(addr != 0){ VM_BUG_ON(!virt_addr_valid((void*)addr)); __free_pages(virt_to_page((void *)addr), order); } } void __free_pages(struct page *page, unsigned int order) { if(put_page_testzero(page)){ if(order == 0) free_hot_page(page); else __free_pages_ok(page, order); } }
free_pages()函数是调用__free_pages()函数完成内存释放的。
4、slab缓存
- 当在驱动程序中,遇到反复分配、释放同一大小的内存块时(例如,inode、task_struct等),建议使用内存池技术(对象在前后两次被使用时均分配在同一块内存或同一类内存空间,且保留了基本的数据结构,这大大提高了效率)。在linux中,有一个叫做slab分配器的内存池管理技术,内存池使用的内存区叫做后备高速缓存。
- salb相关头文件在linux/slab.h中,在使用后备高速缓存前,需要创建一个
kmem_cache
的结构体。
4.1 创建slab缓存区
该函数创建一个slab缓存(后备高速缓冲区),它是一个可以驻留任意数目全部同样大小的后备缓存。其原型如下:
struct kmem_cache *kmem_cache_create(const char *name, size_t size, \ size_t align, unsigned long flags,\ void (*ctor)(void *, struct kmem_cache *, unsigned long),\ void (*dtor)(void *, struct kmem_cache *, unsigned ong)));
其中:
name:创建的缓存名;
size:可容纳的缓存块个数;
align:后备高速缓冲区中第一个内存块的偏移量(一般置为0);
flags:控制如何进行分配的位掩码,包括 SLAB_NO_REAP
(即使内存紧缺也不自动收缩这块缓存)、SLAB_HWCACHE_ALIGN
( 每 个 数 据 对 象 被 对 齐 到 一 个 缓 存 行 )、SLAB_CACHE_DMA
(要求数据对象在 DMA 内存区分配)等);
ctor:是可选的内存块对象构造函数(初始化函数);
dtor:是可选的内存对象块析构函数(释放函数)。
4.2 分配slab缓存函数
一旦创建完后备高速缓冲区后,就可以调用kmem_cache_alloc()
在缓存区分配一个内存块对象了,其原型如下:
void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags);
cachep指向开始分配的后备高速缓存,flags与传给kmalloc函数的参数相同,一般为GFP_KERNEL。
4.3 释放slab缓存
该函数释放一个内存块对象:
void *kmem_cache_free(struct kmem_cache *cachep, void *objp);
4.4 销毁slab缓存
与kmem_cache_create
对应的是销毁函数,释放一个后备高速缓存:
int kmem_cache_destroy(struct kmem_cache *cachep);
它必须等待所有已经分配的内存块对象被释放后才能释放后备高速缓存区。
4.5 slab缓存使用举例
创建一个存放线程结构体(struct thread_info)的后备高速缓存,因为在linux中涉及频繁的线程创建与释放,如果使用__get_free_page()函数会造成内存的大量浪费,效率也不高。所以在linux内核的初始化阶段就创建了一个名为thread_info的后备高速缓存,代码如下:
/* 创建slab缓存 */ static struct kmem_cache *thread_info_cache; thread_info_cache = kmem_cache_create("thread_info", sizeof(struct thread_info), \ SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); /* 分配slab缓存 */ struct thread_info *ti; ti = kmem_cache_alloc(thread_info_cache, GFP_KERNEL); /* 使用slab缓存 */ ... /* 释放slab缓存 */ kmem_cache_free(thread_info_cache, ti); kmem_cache_destroy(thread_info_cache);
5、内存池
在 Linux 内核中还包含对内存池的支持,内存池技术也是一种非常经典的用于分配大量小对象的后备缓存技术。
5.1 创建内存池
mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn, \ mempool_free_t *free_fn, void *pool_data);
mempool_create()函数用于创建一个内存池,min_nr 参数是需要预分配对象的数目,alloc_fn 和 free_fn 是指向内存池机制提供的标准对象分配和回收函数的指针,其原型分别为:
typedef void *(mempool_alloc_t)(int gfp_mask, void *pool_data); typedef void (mempool_free_t)(void *element, void *pool_data);
pool_data 是分配和回收函数用到的指针,gfp_mask 是分配标记。只有当_ _GFP_WAIT
标记被指定时,分配函数才会休眠。
5.2 分配和回收对象
在内存池中分配和回收对象需由以下函数来完成:
void *mempool_alloc(mempool_t *pool, int gfp_mask); void mempool_free(void *element, mempool_t *pool);
mempool_alloc()用来分配对象,如果内存池分配器无法提供内存,那么就可以用预分配的池。
5.3 销毁内存池
void mempool_destroy(mempool_t *pool);
mempool_create()函数创建的内存池需由 mempool_destroy()来回收。
相关推荐:《Linux视频教程》
The above is the detailed content of Several functions related to linux memory management. For more information, please follow other related articles on the PHP Chinese website!

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

Linux maintenance mode is entered by adding init=/bin/bash or single parameters at startup. 1. Enter maintenance mode: Edit the GRUB menu and add startup parameters. 2. Remount the file system to read and write mode: mount-oremount,rw/. 3. Repair the file system: Use the fsck command, such as fsck/dev/sda1. 4. Back up the data and operate with caution to avoid data loss.

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

When choosing a Hadoop version suitable for Debian system, the following key factors need to be considered: 1. Stability and long-term support: For users who pursue stability and security, it is recommended to choose a Debian stable version, such as Debian11 (Bullseye). This version has been fully tested and has a support cycle of up to five years, which can ensure the stable operation of the system. 2. Package update speed: If you need to use the latest Hadoop features and features, you can consider Debian's unstable version (Sid). However, it should be noted that unstable versions may have compatibility issues and stability risks. 3. Community support and resources: Debian has huge community support, which can provide rich documentation and

This article describes how to use TigerVNC to share files on Debian systems. You need to install the TigerVNC server first and then configure it. 1. Install the TigerVNC server and open the terminal. Update the software package list: sudoaptupdate to install TigerVNC server: sudoaptinstalltigervnc-standalone-servertigervnc-common 2. Configure TigerVNC server to set VNC server password: vncpasswd Start VNC server: vncserver:1-localhostno

Configuring a Debian mail server's firewall is an important step in ensuring server security. The following are several commonly used firewall configuration methods, including the use of iptables and firewalld. Use iptables to configure firewall to install iptables (if not already installed): sudoapt-getupdatesudoapt-getinstalliptablesView current iptables rules: sudoiptables-L configuration

The steps to install an SSL certificate on the Debian mail server are as follows: 1. Install the OpenSSL toolkit First, make sure that the OpenSSL toolkit is already installed on your system. If not installed, you can use the following command to install: sudoapt-getupdatesudoapt-getinstallopenssl2. Generate private key and certificate request Next, use OpenSSL to generate a 2048-bit RSA private key and a certificate request (CSR): openss


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment