Linux-3.14.12内存管理笔记【kmalloc与kfree实现】【转】
2018-05-11 09:55
351 查看
本文转载自:http://blog.chinaunix.net/uid-26859697-id-5573776.html
kmalloc()是基于slab/slob/slub分配分配算法上实现的,不少地方将其作为slab/slob/slub分配算法的入口,实际上是略有区别的。
现在分析一下其实现:
【file:/include/linux/slab.h】
/**
* kmalloc - allocate memory
* @size: how many bytes of memory are required.
* @flags: the type of memory to allocate.
*
* kmalloc is the normal method of allocating memory
* for objects smaller than page size in the kernel.
*
* The @flags argument may be one of:
*
* %GFP_USER - Allocate memory on behalf of user. May sleep.
*
* %GFP_KERNEL - Allocate normal kernel ram. May sleep.
*
* %GFP_ATOMIC - Allocation will not sleep. May use emergency pools.
* For example, use this inside interrupt handlers.
*
* %GFP_HIGHUSER - Allocate pages from high memory.
*
* %GFP_NOIO - Do not do any I/O at all while trying to get memory.
*
* %GFP_NOFS - Do not make any fs calls while trying to get memory.
*
* %GFP_NOWAIT - Allocation will not sleep.
*
* %__GFP_THISNODE - Allocate node-local memory only.
*
* %GFP_DMA - Allocation suitable for DMA.
* Should only be used for kmalloc() caches. Otherwise, use a
* slab created with SLAB_DMA.
*
* Also it is possible to set different flags by OR'ing
* in one or more of the following additional @flags:
*
* %__GFP_COLD - Request cache-cold pages instead of
* trying to return cache-warm pages.
*
* %__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_REPEAT - If allocation fails initially, try once more before failing.
*
* There are other flags available as well, but these are not intended
* for general use, and so are not documented here. For a full list of
* potential flags, always refer to linux/gfp.h.
*/
static __always_inline void *kmalloc(size_t size, gfp_t flags)
{
if (__builtin_constant_p(size)) {
if (size > KMALLOC_MAX_CACHE_SIZE)
return kmalloc_large(size, flags);
#ifndef CONFIG_SLOB
if (!(flags & GFP_DMA)) {
int index = kmalloc_index(size);
if (!index)
return ZERO_SIZE_PTR;
return kmem_cache_alloc_trace(kmalloc_caches[index],
flags, size);
}
#endif
}
return __kmalloc(size, flags);
}
kmalloc()的参数size表示申请的空间大小,而flags则表示分配标志。kamlloc的分配标志众多,各标志都分配标识特定的bit位,藉此可以多样组合。
GFP_USER:用于表示为用户空间分配内存,可能会引起休眠;
GFP_KERNEL:内核内存的常规分配,可能会引起休眠;
GFP_ATOMIC:该分配不会引起休眠,但可能会使用应急内存资源,通常用于中断处理中;
GFP_HIGHUSER:使用高端内存进行分配;
GFP_NOIO:分配内存时,禁止任何IO操作;
GFP_NOFS:分配内存时,禁止任何文件系统操作;
GFP_NOWAIT:分配内存时禁止休眠;
__GFP_THISNODE:分配内存时,仅从本地节点内存中分配;
GFP_DMA:从DMA内存中分配合适的内存,应仅使用于kmalloc的cache分配;
__GFP_COLD:用于请求分配冷热页中的冷页;
__GFP_HIGH:用于表示该分配优先级较高并可能会使用应急内存资源;
__GFP_NOFAIL:用于指示该分配不允许分配失败,该标志需要慎用;
__GFP_NORETRY:如果分配内存未能够直接获取到,则不再尝试分配,直接放弃;
__GFP_NOWARN:如果分配过程中失败,不上报任何告警;
__GFP_REPEAT:如果分配过程中失败,则尝试再次申请;
函数入口if判断内的__builtin_constant_p是Gcc内建函数,用于判断一个值是否为编译时常量,是则返回true,否则返回false。也就意味着如果调用kmalloc()传入常量且该值大于KMALLOC_MAX_CACHE_SIZE(即申请空间超过kmalloc()所能分配最大cache的大小),那么将会通过kmalloc_large()进行分配;否则都将通过__kmalloc()进行分配。如果通过kmalloc_large()进行内存分配,将会经kmalloc_large()->kmalloc_order()->__get_free_pages(),最终通过Buddy伙伴算法申请所需内存。
伙伴算法前面已经分析过了,不再赘述,接下来看__kmalloc()的实现:
【file:/mm/slub.c】
void *__kmalloc(size_t size, gfp_t flags)
{
struct kmem_cache *s;
void *ret;
if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
return kmalloc_large(size, flags);
s = kmalloc_slab(size, flags);
if (unlikely(ZERO_OR_NULL_PTR(s)))
return s;
ret = slab_alloc(s, flags, _RET_IP_);
trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
return ret;
}
该函数同样判断申请是否超过最大cache大小,如果是则通过kmalloc_large()进行分配;接着通过申请大小及申请标志调用kmalloc_slab()查找适用的kmem_cache;最后通过slab_alloc()进行slab分配。
具体看一下kmalloc_slab()的实现:
【file:/mm/slab_commmon.c】
/*
* Find the kmem_cache structure that serves a given size of
* allocation
*/
struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
{
int index;
if (unlikely(size > KMALLOC_MAX_SIZE)) {
WARN_ON_ONCE(!(flags & __GFP_NOWARN));
return NULL;
}
if (size <= 192) {
if (!size)
return ZERO_SIZE_PTR;
index = size_index[size_index_elem(size)];
} else
index = fls(size - 1);
#ifdef CONFIG_ZONE_DMA
if (unlikely((flags & GFP_DMA)))
return kmalloc_dma_caches[index];
#endif
return kmalloc_caches[index];
}
如果申请的大小超过KMALLOC_MAX_SIZE最大值,则返回NULL表示失败;如果申请大小小于192,且不为0,将通过size_index_elem宏转换为下标后,经size_index全局数组取得索引值,否则将直接通过fls()取得索引值;最后如果开启了DMA内存配置且设置了GFP_DMA标志,将结合索引值通过kmalloc_dma_caches返回kmem_cache管理结构信息,否则将通过kmalloc_caches返回该结构。
由此可以看出kmalloc()实现较为简单,起分配所得的内存不仅是虚拟地址上的连续存储空间,同时也是物理地址上的连续存储空间。这是有别于后面将会分析到的vmalloc()申请所得的内存。
此外再过一下kfree()的接口实现,该函数在多处均有实现,主要是在slab.c/slob.c/slub.c中,所以也说kmalloc()和kfree()是基于slab/slob/slub实现的。这里接前面的slub算法,主要分析一下slub.c中的kfree()实现:
【file:/mm/slub.c】
void kfree(const void *x)
{
struct page *page;
void *object = (void *)x;
trace_kfree(_RET_IP_, x);
if (unlikely(ZERO_OR_NULL_PTR(x)))
return;
page = virt_to_head_page(x);
if (unlikely(!PageSlab(page))) {
BUG_ON(!PageCompound(page));
kfree_hook(x);
__free_memcg_kmem_pages(page, compound_order(page));
return;
}
slab_free(page->slab_cache, page, object, _RET_IP_);
}
该函数实现简单,首先是经过trace_kfree()记录kfree轨迹,然后if (unlikely(ZERO_OR_NULL_PTR(x)))对地址做非零判断,接着virt_to_head_page(x)将虚拟地址转换到页面;再是判断if (unlikely(!PageSlab(page)))判断该页面是否作为slab分配管理,如果是的话则转为通过slab_free()进行释放,否则将进入if分支中;在if分支中,将会kfree_hook()做释放前kmemleak处理(该函数主要是封装了kmemleak_free()),完了之后将会__free_memcg_kmem_pages()将页面释放,同时该函数内也将cgroup释放处理。
kmalloc()和kfree()也就这么简单了。
kmalloc()是基于slab/slob/slub分配分配算法上实现的,不少地方将其作为slab/slob/slub分配算法的入口,实际上是略有区别的。
现在分析一下其实现:
【file:/include/linux/slab.h】
/**
* kmalloc - allocate memory
* @size: how many bytes of memory are required.
* @flags: the type of memory to allocate.
*
* kmalloc is the normal method of allocating memory
* for objects smaller than page size in the kernel.
*
* The @flags argument may be one of:
*
* %GFP_USER - Allocate memory on behalf of user. May sleep.
*
* %GFP_KERNEL - Allocate normal kernel ram. May sleep.
*
* %GFP_ATOMIC - Allocation will not sleep. May use emergency pools.
* For example, use this inside interrupt handlers.
*
* %GFP_HIGHUSER - Allocate pages from high memory.
*
* %GFP_NOIO - Do not do any I/O at all while trying to get memory.
*
* %GFP_NOFS - Do not make any fs calls while trying to get memory.
*
* %GFP_NOWAIT - Allocation will not sleep.
*
* %__GFP_THISNODE - Allocate node-local memory only.
*
* %GFP_DMA - Allocation suitable for DMA.
* Should only be used for kmalloc() caches. Otherwise, use a
* slab created with SLAB_DMA.
*
* Also it is possible to set different flags by OR'ing
* in one or more of the following additional @flags:
*
* %__GFP_COLD - Request cache-cold pages instead of
* trying to return cache-warm pages.
*
* %__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_REPEAT - If allocation fails initially, try once more before failing.
*
* There are other flags available as well, but these are not intended
* for general use, and so are not documented here. For a full list of
* potential flags, always refer to linux/gfp.h.
*/
static __always_inline void *kmalloc(size_t size, gfp_t flags)
{
if (__builtin_constant_p(size)) {
if (size > KMALLOC_MAX_CACHE_SIZE)
return kmalloc_large(size, flags);
#ifndef CONFIG_SLOB
if (!(flags & GFP_DMA)) {
int index = kmalloc_index(size);
if (!index)
return ZERO_SIZE_PTR;
return kmem_cache_alloc_trace(kmalloc_caches[index],
flags, size);
}
#endif
}
return __kmalloc(size, flags);
}
kmalloc()的参数size表示申请的空间大小,而flags则表示分配标志。kamlloc的分配标志众多,各标志都分配标识特定的bit位,藉此可以多样组合。
GFP_USER:用于表示为用户空间分配内存,可能会引起休眠;
GFP_KERNEL:内核内存的常规分配,可能会引起休眠;
GFP_ATOMIC:该分配不会引起休眠,但可能会使用应急内存资源,通常用于中断处理中;
GFP_HIGHUSER:使用高端内存进行分配;
GFP_NOIO:分配内存时,禁止任何IO操作;
GFP_NOFS:分配内存时,禁止任何文件系统操作;
GFP_NOWAIT:分配内存时禁止休眠;
__GFP_THISNODE:分配内存时,仅从本地节点内存中分配;
GFP_DMA:从DMA内存中分配合适的内存,应仅使用于kmalloc的cache分配;
__GFP_COLD:用于请求分配冷热页中的冷页;
__GFP_HIGH:用于表示该分配优先级较高并可能会使用应急内存资源;
__GFP_NOFAIL:用于指示该分配不允许分配失败,该标志需要慎用;
__GFP_NORETRY:如果分配内存未能够直接获取到,则不再尝试分配,直接放弃;
__GFP_NOWARN:如果分配过程中失败,不上报任何告警;
__GFP_REPEAT:如果分配过程中失败,则尝试再次申请;
函数入口if判断内的__builtin_constant_p是Gcc内建函数,用于判断一个值是否为编译时常量,是则返回true,否则返回false。也就意味着如果调用kmalloc()传入常量且该值大于KMALLOC_MAX_CACHE_SIZE(即申请空间超过kmalloc()所能分配最大cache的大小),那么将会通过kmalloc_large()进行分配;否则都将通过__kmalloc()进行分配。如果通过kmalloc_large()进行内存分配,将会经kmalloc_large()->kmalloc_order()->__get_free_pages(),最终通过Buddy伙伴算法申请所需内存。
伙伴算法前面已经分析过了,不再赘述,接下来看__kmalloc()的实现:
【file:/mm/slub.c】
void *__kmalloc(size_t size, gfp_t flags)
{
struct kmem_cache *s;
void *ret;
if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
return kmalloc_large(size, flags);
s = kmalloc_slab(size, flags);
if (unlikely(ZERO_OR_NULL_PTR(s)))
return s;
ret = slab_alloc(s, flags, _RET_IP_);
trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
return ret;
}
该函数同样判断申请是否超过最大cache大小,如果是则通过kmalloc_large()进行分配;接着通过申请大小及申请标志调用kmalloc_slab()查找适用的kmem_cache;最后通过slab_alloc()进行slab分配。
具体看一下kmalloc_slab()的实现:
【file:/mm/slab_commmon.c】
/*
* Find the kmem_cache structure that serves a given size of
* allocation
*/
struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
{
int index;
if (unlikely(size > KMALLOC_MAX_SIZE)) {
WARN_ON_ONCE(!(flags & __GFP_NOWARN));
return NULL;
}
if (size <= 192) {
if (!size)
return ZERO_SIZE_PTR;
index = size_index[size_index_elem(size)];
} else
index = fls(size - 1);
#ifdef CONFIG_ZONE_DMA
if (unlikely((flags & GFP_DMA)))
return kmalloc_dma_caches[index];
#endif
return kmalloc_caches[index];
}
如果申请的大小超过KMALLOC_MAX_SIZE最大值,则返回NULL表示失败;如果申请大小小于192,且不为0,将通过size_index_elem宏转换为下标后,经size_index全局数组取得索引值,否则将直接通过fls()取得索引值;最后如果开启了DMA内存配置且设置了GFP_DMA标志,将结合索引值通过kmalloc_dma_caches返回kmem_cache管理结构信息,否则将通过kmalloc_caches返回该结构。
由此可以看出kmalloc()实现较为简单,起分配所得的内存不仅是虚拟地址上的连续存储空间,同时也是物理地址上的连续存储空间。这是有别于后面将会分析到的vmalloc()申请所得的内存。
此外再过一下kfree()的接口实现,该函数在多处均有实现,主要是在slab.c/slob.c/slub.c中,所以也说kmalloc()和kfree()是基于slab/slob/slub实现的。这里接前面的slub算法,主要分析一下slub.c中的kfree()实现:
【file:/mm/slub.c】
void kfree(const void *x)
{
struct page *page;
void *object = (void *)x;
trace_kfree(_RET_IP_, x);
if (unlikely(ZERO_OR_NULL_PTR(x)))
return;
page = virt_to_head_page(x);
if (unlikely(!PageSlab(page))) {
BUG_ON(!PageCompound(page));
kfree_hook(x);
__free_memcg_kmem_pages(page, compound_order(page));
return;
}
slab_free(page->slab_cache, page, object, _RET_IP_);
}
该函数实现简单,首先是经过trace_kfree()记录kfree轨迹,然后if (unlikely(ZERO_OR_NULL_PTR(x)))对地址做非零判断,接着virt_to_head_page(x)将虚拟地址转换到页面;再是判断if (unlikely(!PageSlab(page)))判断该页面是否作为slab分配管理,如果是的话则转为通过slab_free()进行释放,否则将进入if分支中;在if分支中,将会kfree_hook()做释放前kmemleak处理(该函数主要是封装了kmemleak_free()),完了之后将会__free_memcg_kmem_pages()将页面释放,同时该函数内也将cgroup释放处理。
kmalloc()和kfree()也就这么简单了。
相关文章推荐
- Linux-3.14.12内存管理笔记【内存泄漏检测kmemleak示例】【转】
- 笔记:linux内存管理机制
- linux 内存管理笔记
- linux红黑树实现内存管理系统
- shell脚本学习笔记:通过shell实现linux用户管理和监控
- Linux学习笔记12-RPM软件管理
- Linux 驱动学习笔记05--字符驱动实例,实现一个共享内存设备的驱动
- 【学习笔记】嵌入式Linux之内存管理
- [Linux内存]slab分配器学习笔记(三)--实现
- linux笔记05(软件包管理和文件的压缩与解压)
- 利用模版类实现指针内存的分配管理
- Linux 学习笔记(1) 程序与资源管理
- 写得蛮好的linux学习笔记六-帐号管理(收藏)
- 利用模版类实现指针内存的分配管理
- linux笔记06(进程管理)
- 在嵌入式Linux 系统中实现基于Web 的配置管理
- 卡卡笔记之Linux Service 服务管理
- Linux电源管理研究笔记—动态电源管理(DPM)
- Linux电源管理研究笔记—动态电源管理(DPM)
- Linux电源管理研究笔记—acpid工作原理