中移(苏州)软件技术有限公司面试问题与解答(7)—— kmalloc与vmalloc的区别与联系及使用场景

接前一篇文章:中移(苏州)软件技术有限公司面试问题与解答(0)—— 面试感悟与问题记录

本文参考以下文章:

kmalloc与vmalloc如何选择

Vmalloc与kmalloc的区别

特此致谢!

本文对于中移(苏州)软件技术有限公司面试问题中的“(6)vmalloc和kmalloc的区别?什么时候会用kmalloc、什么时候用vmalloc?”进行解答与解析。

1. kmalloc和vmalloc的区别

先来回答kmalloc和vmalloc的相同点和不同点:

  • 相同点

kmalloc和vmalloc都是Linux内核中用于内存分配的函数。

  • 不同点

1)kmalloc保证分配的内存在物理上是连续的,vmalloc保证的是在虚拟地址空间上的连续。也就是说kmalloc函数分配的内存,虚拟地址连续、物理地址也连续;而vmalloc函数分配的内存,虚拟地址连续、物理地址并不连续。通过页表来建立物理内存与虚拟内存之间的关系,从而可以将不连续的物理内存映射到连续的虚拟内存。

2)kmalloc能分配的大小有限,vmalloc能分配的大小相对较大。kmalloc函数用于在内核空间中分配小块(通常小于一个页面大小)的连续内存区域,这些内存区域可以用于存储内核数据结构和缓冲区等;vmalloc函数用于在内核空间中分配大块的虚拟内存区域,通常用于存储大型缓冲区和内存映射文件等。

3)kmalloc函数分配速度较快,vmalloc比kmalloc函数要慢。kmalloc分配的物理地址与虚拟地址只有一个__PAGE_OFFSET(即TASK_SIZE)偏移,不需要为地址段修改页表;vmalloc函数获得的物理内存是不连续的,因此它只能将这些物理内存页一个一个地进行映射。因此,在性能开销上会比直接映射大得多。

4)kmallloc使用的是slab内存分配机制,而vmalloc使用的是伙伴系统分配机制,这也是造成它们区别的根本所在

5)kmalloc可用于原子上下文,而vmalloc不可以。vmalloc函数中调用了kmalloc(GFP_KERNEL),因此也不能应用于原子上下文。

2. 什么时候用kmalloc、什么时候用vmalloc

由上边kmalloc和vmalloc的特点可知,它们适用于不同的内存分配场景。在Linux内核编程中,需要根据具体的内存分配需求选择适合的函数。

  • kmalloc的使用场景

如果需要分配小块连续内存,可以使用kmalloc函数。

  • vmalloc的使用场景

如果需要分配大块非连续虚拟内存,则需要使用vmalloc函数。vmalloc使用的正确场合是分配一大块、(虚拟地址)连续的,只在软件中存在的、用于缓冲的内存区域,不能在微处理器之外使用。

关于kmalloc和vmalloc的正确使用方法在Linux内核源码/Documentation/translations/zh_CN\core-api/memory-allocation.rst中做了详细说明,内容如下:

选择内存分配器

==============

分配内存的最直接的方法是使用kmalloc()系列的函数。而且,为了安全起见,最好使用将内存设置为零的例程,如kzalloc()。如果你需要为一个数组分配内存,有kmalloc_array()和kcalloc()辅助程序。辅助程序struct_size()、array_size()和array3_size()可以用来安全地计算对象的大小而不会溢出。

可以用 `kmalloc` 分配的块的最大尺寸是有限的。实际的限制取决于硬件和内核配置,但是对于小于页面大小的对象,使用 `kmalloc` 是一个好的做法。

用 `kmalloc` 分配的块的地址至少要对齐到ARCH_KMALLOC_MINALIGN字节。对于2的幂的大小,

对齐方式也被保证为至少是各自的大小。

用kmalloc()分配的块可以用krealloc()调整大小。与kmalloc_array()类似:以krealloc_array()

的形式提供了一个用于调整数组大小的辅助工具。

对于大量的分配,你可以使用vmalloc()和vzalloc(),或者直接向页面分配器请求页面。由vmalloc和相关函数分配的内存在物理上是不连续的。

如果你不确定分配的大小对 `kmalloc` 来说是否太大,可以使用kvmalloc()及其派生函数。它将尝试用kmalloc分配内存,如果分配失败,将用 `vmalloc` 重新尝试。对于哪些GFP标志可以与 `kvmalloc`一起使用是有限制的;请看kvmalloc_node()参考文档。注意, `kvmalloc` 可能会返回物理上不连续的内存。

如果你需要分配许多相同的对象,你可以使用slab缓存分配器。在使用缓存之前,应该用

kmem_cache_create()或kmem_cache_create_usercopy()来设置缓存。如果缓存的一部分可能被复制到用户空间,应该使用第二个函数。在缓存被创建后,kmem_cache_alloc()和它的封装可以从该缓存中分配内存。

当分配的内存不再需要时,它必须被释放。你可以使用kvfree()来处理用 `kmalloc` 、 `vmalloc`和 `kvmalloc` 分配的内存。slab缓存应该用kmem_cache_free()来释放。不要忘记用kmem_cache_destroy()来销毁缓存。

3. 函数源码

  • kmalloc函数源码

kmalloc函数在include/linux/slab.h中,代码如下:

/**
 * kmalloc - allocate kernel memory
 * @size: how many bytes of memory are required.
 * @flags: describe the allocation context
 *
 * kmalloc is the normal method of allocating memory
 * for objects smaller than page size in the kernel.
 *
 * The allocated object address is aligned to at least ARCH_KMALLOC_MINALIGN
 * bytes. For @size of power of two bytes, the alignment is also guaranteed
 * to be at least to the size.
 *
 * The @flags argument may be one of the GFP flags defined at
 * include/linux/gfp_types.h and described at
 * :ref:`Documentation/core-api/mm-api.rst <mm-api-gfp-flags>`
 *
 * The recommended usage of the @flags is described at
 * :ref:`Documentation/core-api/memory-allocation.rst <memory_allocation>`
 *
 * Below is a brief outline of the most useful GFP flags
 *
 * %GFP_KERNEL
 *	Allocate normal kernel ram. May sleep.
 *
 * %GFP_NOWAIT
 *	Allocation will not sleep.
 *
 * %GFP_ATOMIC
 *	Allocation will not sleep.  May use emergency pools.
 *
 * Also it is possible to set different flags by OR'ing
 * in one or more of the following additional @flags:
 *
 * %__GFP_ZERO
 *	Zero the allocated memory before returning. Also see kzalloc().
 *
 * %__GFP_HIGH
 *	This allocation has high priority and may use emergency pools.
 *
 * %__GFP_NOFAIL
 *	Indicate that this allocation is in no way allowed to fail
 *	(think twice before using).
 *
 * %__GFP_NORETRY
 *	If memory is not immediately available,
 *	then give up at once.
 *
 * %__GFP_NOWARN
 *	If allocation fails, don't issue any warnings.
 *
 * %__GFP_RETRY_MAYFAIL
 *	Try really hard to succeed the allocation but fail
 *	eventually.
 */
static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t flags)
{
	if (__builtin_constant_p(size) && size) {
		unsigned int index;

		if (size > KMALLOC_MAX_CACHE_SIZE)
			return kmalloc_large(size, flags);

		index = kmalloc_index(size);
		return kmalloc_trace(
				kmalloc_caches[kmalloc_type(flags, _RET_IP_)][index],
				flags, size);
	}
	return __kmalloc(size, flags);
}
  • vmalloc函数源码

vmalloc函数在mm/vmalloc.c中,代码如下:

/**
 * vmalloc - allocate virtually contiguous memory
 * @size:    allocation size
 *
 * Allocate enough pages to cover @size from the page level
 * allocator and map them into contiguous kernel virtual space.
 *
 * For tight control over page level allocator and protection flags
 * use __vmalloc() instead.
 *
 * Return: pointer to the allocated memory or %NULL on error
 */
void *vmalloc(unsigned long size)
{
	return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE,
				__builtin_return_address(0));
}
EXPORT_SYMBOL(vmalloc);

至此, 中移(苏州)软件技术有限公司面试问题中的“(6)vmalloc和kmalloc的区别?什么时候会用kmalloc、什么时候用vmalloc?”就回答并解析完毕了。

最近更新

  1. TCP协议是安全的吗?

    2024-02-01 06:32:01       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-01 06:32:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-01 06:32:01       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-01 06:32:01       20 阅读

热门阅读

  1. 设计模式全览:编程艺术的精髓!

    2024-02-01 06:32:01       25 阅读
  2. 设计模式——策略模式

    2024-02-01 06:32:01       29 阅读
  3. Spring Boot + Vue3 实现七牛云大视频上传

    2024-02-01 06:32:01       33 阅读
  4. 3D人体运动重建

    2024-02-01 06:32:01       41 阅读
  5. 【后端】HTTP

    2024-02-01 06:32:01       32 阅读
  6. 小程序接入企业微信「联系我」功能

    2024-02-01 06:32:01       40 阅读
  7. Visual Studio 和Clion配置Cocos2d-x环境

    2024-02-01 06:32:01       42 阅读
  8. unity 使用数字图片来代替数字0到9显示

    2024-02-01 06:32:01       29 阅读
  9. Tomcat调优教程

    2024-02-01 06:32:01       26 阅读
  10. 梯度下降法解决2D映射3D

    2024-02-01 06:32:01       34 阅读
  11. tomcat

    tomcat

    2024-02-01 06:32:01      25 阅读