`
tomhibolu
  • 浏览: 1431425 次
文章分类
社区版块
存档分类
最新评论

kernel hacker修炼之道之内存管理-SLUB(分配SLAB对象kmem_cache_alloc())

 
阅读更多

分配SLAB对象kmem_cache_alloc()

作者:李万鹏 于北京 borqs


void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
{
void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
trace_kmem_cache_alloc(_RET_IP_, ret, s->objsize, s->size, gfpflags);
return ret;
}
EXPORT_SYMBOL(kmem_cache_alloc);

static __always_inline void *slab_alloc(struct kmem_cache *s,
gfp_t gfpflags, int node, unsigned long addr)
{
void **object;
struct kmem_cache_cpu *c;
#ifdef CONFIG_CMPXCHG_LOCAL
unsigned long tid;
#else
unsigned long flags;
#endif

if (slab_pre_alloc_hook(s, gfpflags))
return NULL;

#ifndef CONFIG_CMPXCHG_LOCAL
local_irq_save(flags);
#else
redo:
#endif

/*获得cpu local slab*/
c = __this_cpu_ptr(s->cpu_slab);


#ifdef CONFIG_CMPXCHG_LOCAL
/*
* The transaction ids are globally unique per cpu and per operation on
* a per cpu queue. Thus they can be guarantee that the cmpxchg_double
* occurs on the right processor and that there was no operation on the
* linked list in between.
*/
tid = c->tid;
barrier();
#endif

/*走fastpath,从c->freelist获得空闲对象*/
object = c->freelist;
/*如果c->freelist没有空闲对象或者与所要求的节点不匹配,只能走slowpath*/
if (unlikely(!object || !node_match(c, node)))

object = __slab_alloc(s, gfpflags, node, addr, c);

else {
#ifdef CONFIG_CMPXCHG_LOCAL

if (unlikely(!irqsafe_cpu_cmpxchg_double(
s->cpu_slab->freelist, s->cpu_slab->tid,
object, tid,
get_freepointer_safe(s, object), next_tid(tid)))) {

note_cmpxchg_failure("slab_alloc", s, tid);
goto redo;
}
#else
/*指向下一个空闲对象*/
c->freelist = get_freepointer(s, object);
#endif
stat(s, ALLOC_FASTPATH);
}

#ifndef CONFIG_CMPXCHG_LOCAL
local_irq_restore(flags);
#endif
/*如果需要就将object清空*/
if (unlikely(gfpflags & __GFP_ZERO) && object)
memset(object, 0, s->objsize);

slab_post_alloc_hook(s, gfpflags, object);

return object;
}

走slowpath:
static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
unsigned long addr, struct kmem_cache_cpu *c)
{
void **object;
struct page *new;
#ifdef CONFIG_CMPXCHG_LOCAL
unsigned long flags;

local_irq_save(flags);
#ifdef CONFIG_PREEMPT
/*
* We may have been preempted and rescheduled on a different
* cpu before disabling interrupts. Need to reload cpu area
* pointer.
*/
/*获得cpu local slab*/
c = this_cpu_ptr(s->cpu_slab);
#endif
#endif

/* We handle __GFP_ZERO in the caller */
gfpflags &= ~__GFP_ZERO;
/*如果c->page也就是cpu local slab不存在就新分配一个*/
if (!c->page)
goto new_slab;

slab_lock(c->page);
/*如果是节点不匹配就去掉激活cpu local slab*/
if (unlikely(!node_match(c, node)))
goto another_slab;

stat(s, ALLOC_REFILL);
/*从c->page_freelist装入c->freelist*/
load_freelist:
object = c->page->freelist;
/*如果c->page也就是cpu local slab不存在就新分配一个*/
if (unlikely(!object))
goto another_slab;
if (kmem_cache_debug(s))
goto debug;
/*c->freelist指向了这个空闲对象的链表*/
c->freelist = get_freepointer(s, object);
c->page->inuse = c->page->objects;
/*c->page->freelist已经完全赋给了c->freelist,这里置空*/
c->page->freelist = NULL;
c->node = page_to_nid(c->page);
unlock_out:
slab_unlock(c->page);
#ifdef CONFIG_CMPXCHG_LOCAL
c->tid = next_tid(c->tid);
local_irq_restore(flags);
#endif
stat(s, ALLOC_SLOWPATH);
return object;

another_slab:
/*去掉激活cpu local slab,其实就是将cpu local slab释放回slabs_partial*/
deactivate_slab(s, c);

new_slab:
/*从slabs_partial获得一个新的slab*/
new = get_partial(s, gfpflags, node);
if (new) {
c->page = new;
stat(s, ALLOC_FROM_PARTIAL);
/*如果获得成功,重试载入c->freelist*/
goto load_freelist;
}

gfpflags &= gfp_allowed_mask;
if (gfpflags & __GFP_WAIT)
local_irq_enable();
/*运行到这里说明从slabs_partial获得slab失败了,则从buddy system分配一个新的slab块*/
new = new_slab(s, gfpflags, node);

if (gfpflags & __GFP_WAIT)
local_irq_disable();

if (new) {
c = __this_cpu_ptr(s->cpu_slab);
stat(s, ALLOC_SLAB);
/*清除旧的local slab*/
if (c->page)
flush_slab(s, c);
slab_lock(new);
/*冻结slab,表示他已经成为某个cpu的local slab*/
__SetPageSlubFrozen(new);
c->page = new;
/*如果成功,重试载入c->freelist*/
goto load_freelist;
}
if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
slab_out_of_memory(s, gfpflags, node);
#ifdef CONFIG_CMPXCHG_LOCAL
local_irq_restore(flags);
#endif
return NULL;
debug:
if (!alloc_debug_processing(s, c->page, object, addr))
goto another_slab;

c->page->inuse++;
c->page->freelist = get_freepointer(s, object);
c->node = NUMA_NO_NODE;
goto unlock_out;
}

static inline void *get_freepointer(struct kmem_cache *s, void *object)
{
return *(void **)(object + s->offset);
}

static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
__releases(bitlock)
{
/*获得cpu local slab*/
struct page *page = c->page;
int tail = 1;

if (page->freelist)
stat(s, DEACTIVATE_REMOTE_FREES);
/*
* Merge cpu freelist into slab freelist. Typically we get here
* because both freelists are empty. So this is unlikely
* to occur.
*/
/*如果是因为节点不匹配而不是因为c->page为空,则c->freelist有可能不为空,将c->freelist对应的空闲对象链表还给c->page->freelist*/
while (unlikely(c->freelist)) {
void **object;

tail = 0; /* Hot objects. Put the slab first */

/* Retrieve object from cpu_freelist */
object = c->freelist;
c->freelist = get_freepointer(s, c->freelist);

/* And put onto the regular freelist */
set_freepointer(s, object, page->freelist);
page->freelist = object;
page->inuse--;
}
c->page = NULL;
#ifdef CONFIG_CMPXCHG_LOCAL
c->tid = next_tid(c->tid);
#endif
/*解冻local slab*/
unfreeze_slab(s, page, tail);
}

static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
__releases(bitlock)
{
struct kmem_cache_node *n = get_node(s, page_to_nid(page));
/*清除页被冻结标志*/
__ClearPageSlubFrozen(page);
/*如果还有页在被使用*/
if (page->inuse) {
/*如果page->freelist不为空,将这个local slab添加到slabs_partial*/
if (page->freelist) {
add_partial(n, page, tail);
stat(s, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
} else {
stat(s, DEACTIVATE_FULL);
if (kmem_cache_debug(s) && (s->flags & SLAB_STORE_USER))
add_full(n, page);
}
slab_unlock(page);
} else {
/*如果没有object被使用了,判但如果节点slabs_partial上slab的数量小于cache的slab最小数量则添加到slabs_partial*/
stat(s, DEACTIVATE_EMPTY);
if (n->nr_partial < s->min_partial) {
/*
* Adding an empty slab to the partial slabs in order
* to avoid page allocator overhead. This slab needs
* to come after the other slabs with objects in
* so that the others get filled first. That way the
* size of the partial list stays small.
*
* kmem_cache_shrink can reclaim any empty slabs from
* the partial list.
*/
add_partial(n, page, 1);
slab_unlock(page);
} else {
/*否则销毁这个slab*/
slab_unlock(page);
stat(s, FREE_SLAB);
discard_slab(s, page);
}
}
}

static void discard_slab(struct kmem_cache *s, struct page *page)
{
dec_slabs_node(s, page_to_nid(page), page->objects);
/*释放给buddy system*/
free_slab(s, page);
}

分享到:
评论

相关推荐

    Linux常见驱动源码分析(kernel hacker修炼之道全集)--李万鹏

    Linux常见驱动源码分析(kernel hacker修炼之道)--李万鹏 李万鹏 IBM Linux Technology Center kernel team 驱动资料清单内容如下: Linux设备模型(中)之上层容器.pdf Linux设备模型(上)之底层模型.pdf Linux...

    常见驱动源码分析(kernel hacker修炼之道)-李万鹏

    这本书是“Linux kernel hacker修炼之道”的一部分,通过深入剖析各种常见的驱动源码,帮助读者提升在Linux系统中的驱动开发能力。 在Linux操作系统中,驱动程序是连接硬件与内核的桥梁,它们负责管理和控制硬件...

    常见驱动源码分析(kernel hacker修炼之道)

    《常见驱动源码分析(kernel hacker修炼之道)》这本书或课程很可能深入探讨了如何理解和编写这些驱动,旨在帮助开发者提升对Linux内核和驱动编程的理解。在这个过程中,我们将会涉及到几个关键的知识点: 1. **Linux...

    BotnetWebSocket-master_chancexvw_hacker_websocket_botnet_sangerp

    【标题解析】:“BotnetWebSocket-master_chancexvw_hacker_websocket_botnet_sangerp”这个标题中的关键词“BotnetWebSocket-master”暗示了一个与僵尸网络(Botnet)相关的项目,特别是利用WebSocket协议的。...

    网鼎杯-第三场-杂项-track_hacker

    【网鼎杯-第三场-杂项-track_hacker】是一个典型的网络安全竞赛中的挑战,涉及到的是CTF(Capture The Flag)比赛中的杂项类别。在CTF比赛中,参赛者需要运用各种安全技能解决难题,获取“旗标”(代表分数或解题...

    通杀漏洞利用回显方法-linux平台 - 00theway _ blog _ hacker _ exp _ exploit _

    作者提出了一个有趣的思考点:既然Java的JVM中所有的对象都存储在堆内存中,那么理论上可能存在一种方式直接获取存储在堆内存中的Socket对象,从而实现回显。这需要深入理解JVM的工作原理和内存管理机制。同时,他...

    linux kernel修炼之道

    如果刚刚对linux的kernel有兴趣,想了解点什么的话,请先看看此书吧,她风趣幽默的介绍了linux的发展趣事,让你开心快乐之余慢慢领会linux的魅力,让你了解学习掌握kernel的方法。其中的很多建议经过我的实践和摸索...

    re-ad-to-se.zip_hacker

    super hacker tools pour tester beta

    Java编译-The_Hacker’s_Guide_to_Javac1

    1. parse: Reads a set of *.java source files and maps the resulting token 2. ent

    matt-blaze-hacker-v1-0__1-13306

    骇客V1.0 描述 一个令人兴奋的游戏,将您带到了热门的位置,试图使MI6总部免遭破坏。 使用您的技能来克服这种情况。 记得保持冷静。 请下载并对其评分,以免错过。 即将推出进一步的升级! 更多信息 ...

    Cmake-3.22.0-linux-x86_64安装包

    CMake是一款跨平台的构建工具,它用于管理软件构建过程,尤其适合大型项目或包含多个子项目的工程。CMake不依赖于任何特定的构建系统,而是生成本地构建文件(如Unix Makefiles、Visual Studio项目等),然后使用...

    HackerRank---The-Linux-Shell-Problems_Solutions:问题可以通过-https

    HackerRank平台上的"The Linux Shell"挑战旨在帮助开发者熟悉和精通Shell脚本编写,提高他们在终端环境中解决问题的能力。在这个项目中,我们将深入探讨Shell编程的各个方面,并提供一系列解决方案来解决这些问题。 ...

    process-timer.zip_Process_ProcessTimer_monitoring_zip

    在Windows操作系统中,进程管理是系统性能监控的重要组成部分。"Process Timer"是一个工具,它用于监视和分析系统中运行的进程的性能。本篇文章将详细探讨"Process ProcessTimer Monitoring"的相关知识点,包括进程...

    leetcode和oj-Leet_Code-Hacker_Rank:Leet_Code-Hacker_Rank

    《LeetCode与Hacker Rank:在线编程挑战的宝库》 LeetCode和Hacker Rank是两个备受程序员喜爱的在线编程挑战平台,它们为提升技能、准备面试和了解最新技术趋势提供了丰富的资源。这两个平台不仅有助于巩固基础,还...

    Algorithmic-Problem-Solving.rar_problem solving

    在计算机科学领域,算法问题求解是核心技能之一,它涉及到如何用计算机程序解决复杂的问题。本课程“CSCI-算法问题求解”源自新加坡,旨在提升学生对算法设计和分析的理解,从而提高其在计算机编程中的问题解决能力...

    Linux内核驱动笔记

    内存管理负责分配、回收内存资源,管理虚拟内存和物理内存的映射关系,并实现内存保护机制。 Linux内核支持多种文件系统,例如ext2、fat、isofs等。虚拟文件系统(VFS)是Linux内核中的一个重要概念,它为不同文件...

    2021-10-12T15_44_13.522866+00_00ARE_YOU_SDPD.zip

    【描述】中的"CTF附件题hacker"揭示了这个压缩包的内容性质,它是一个CTF挑战的一部分,具体可能涉及到黑客技术或者安全破解。CTF是一种流行的网络安全竞赛形式,参赛者需要通过解谜、逆向工程和漏洞利用等技巧来...

    HackerRank_2019-2018_Developer-Skills-Report.pdf

    这份标题为"HackerRank_2019-2018_Developer-Skills-Report.pdf"的报告,即HackerRank发布的2019-2018开发者技能报告,详细地探讨了开发者社区中的技术趋势和技能发展情况。根据描述,报告提出了React、物联网(IOT)...

    Resource_Hacker_v5.1.6绿色版.zip

    资源 Hacker 是一款强大的Windows应用程序资源编辑工具,主要用于查看、修改、添加、删除以及替换应用程序中的资源,如图标、对话框、菜单、字符串表等。在本文中,我们将深入探讨Resource Hacker的功能、使用方法...

Global site tag (gtag.js) - Google Analytics