gpt4 book ai didi

linux - PERCPU : allocation failed, size=256 align=256,分配新 block 失败

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:23:02 25 4
gpt4 key购买 nike

PERCPU: allocation failed, size=256 align=256, failed to allocate new chunk.

每个 CPU 分配的空间量是否有限?

我可以在 Linux 内核模块编程中使用多少 percpu 空间?


现在我正在尝试创建尽可能多的 workqueue_struct。我的内核是 3.10。

我的结果:我可以创建大约 100000 个 workqueue_struct,然后我在使用 dmesg 命令时找到错误信息(与标题中的相同)。

我的代码:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/kthread.h>//kthread_create is_err
#include <linux/slab.h>//kfree
#include <linux/sched.h>//schedule
#include <linux/delay.h>
#include <linux/list.h>
#include <linux/workqueue.h>

u64 i = 0;
static LIST_HEAD(myworkqueuehead);
static struct task_struct *task;

struct MyworkqueueType {
struct list_head entry;
struct workqueue_struct *wq;
u64 number;
};

void myfree(void)
{
struct MyworkqueueType *tempwqtype,*n;
list_for_each_entry_safe(tempwqtype, n, &myworkqueuehead, entry)
{
if(tempwqtype)
{
if(tempwqtype->wq){
//printk("myfree():number=%lld\n",tempwqtype->number);
//printk("list_del()\n");
list_del(&(tempwqtype->entry));
//printk("destroy_workqueue()\n");
destroy_workqueue(tempwqtype->wq);
//printk("free tempwqtypetype:kfree(tempwqtype)\n");
kfree(tempwqtype);
//printk("after free tempwqtypetype\n");
}else{
printk("tempwqtype->wq is null\n");
}
}else{
printk("tempwqtype is null\n");
}
}
printk("has freed all the workqueue space...\n");
}


static int test(void *data)
{
printk("kthread create_wq start to run test()...\n");
while(1)
{
struct MyworkqueueType *myworkqueue;
if(kthread_should_stop())
{
printk("create_wq kthread begin to do myfree()...\n");
myfree();
printk("create_wq kthread stop...\n");
return 0;
}
myworkqueue = kzalloc(sizeof(*myworkqueue), GFP_KERNEL);
if(myworkqueue){
struct workqueue_struct *wq = alloc_workqueue("myworkqueue",0,0);
//struct workqueue_struct *wq = create_workqueue("myworkqueue");
if(!wq)
{
struct MyworkqueueType *mytype;
kfree(myworkqueue);
printk("\ncreate workqueue fail...\n");
mytype = list_entry(myworkqueuehead.prev, struct MyworkqueueType, entry);
printk("current workqueue number=%lld.start to sleep...\n",mytype->number);
msleep(5000);
schedule();
continue;
}
++i;
myworkqueue->number = i;
myworkqueue->wq = wq;
INIT_LIST_HEAD(&myworkqueue->entry);
list_add_tail(&myworkqueue->entry,&myworkqueuehead);
printk("%lld ",i);
}
else
{
printk("\nalloc struct MyworkqueueType fail...\n");
printk("current workqueuenum = %lld",i);
kfree(myworkqueue);
msleep(5000);
schedule();
continue;
}

}
}

static int __init maxwqnum_init(void)
{
printk("-----------maxwqnum-------------\n");
task=kthread_create(test,NULL,"create_wq");
if(IS_ERR(task))
{
printk("create task_struct create_wq fail...\n");
kfree(task);
return 0;
}
printk("create task_struct create_wq success...\n");
wake_up_process(task);
return 0;
}

static void __exit maxwqnum_cleanup(void)
{
kthread_stop(task);
printk("-----------leaving maxwqnum-------------\n");
}

module_init(maxwqnum_init);
module_exit(maxwqnum_cleanup);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("mjq");
MODULE_DESCRIPTION("just a test!");
MODULE_SUPPORTED_DEVICE("WORKQUEUE");

最佳答案

per-cpu 池中模块可用的最大块取决于已加载到 Linux 内核中的其他模块的当前使用情况。

percpu 池的大小取决于内核配置中是否定义了以下配置。

  • CONFIG_HAVE_SETUP_PER_CPU_AREA
  • CONFIG_SMP

启动时每个 CPU 池的典型初始大小为 32KB per cpu .

如果定义了架构特定的 setup_per_cpu_area() 函数,它可能会有所不同。为 percpu 池保留的确切内存量会在 Linux 内核启动期间记录到控制台。例如,Linux 内核 3.2 在我的 Intel Core 2 Duo P8700 机器上,记录以下内容:

PERCPU: Embedded 13 pages/cpu @f77d1000 s31616 r0 d21632 u53248

percpu 池有 13 个页面,即每个 cpu 52KB,总共 104KB。其他数字是池的基址static_sizereserved_sizedyn_sizeunit_size分别。


更新:

编译 Linux 内核模块(来自上述问题中的代码)并对其进行insmod操作,导致以下错误:

[867955.300798] create workqueue fail...
[867955.300804] current workqueue number=198634.start to sleep...
[867960.315934] PERCPU: allocation failed, size=92 align=256, failed to allocate new chunk
[867960.315948] Pid: 26103, comm: create_wq Tainted: G O 3.2.0-51-generic #77-Ubuntu
[867960.315955] Call Trace:
[867960.315973] [<c1563ac4>] ? printk+0x2d/0x2f
[867960.315986] [<c110335e>] pcpu_alloc+0x30e/0x340
[867960.315995] [<c110339f>] __alloc_percpu+0xf/0x20
[867960.316032] [<c10641b0>] __alloc_workqueue_key+0xd0/0x430
[867960.316047] [<c1122f75>] ? kmem_cache_alloc_trace+0x105/0x140
[867960.316065] [<f93e50e6>] test+0x56/0x194 [kmod]
[867960.316078] [<f93e5090>] ? myfree+0x90/0x90 [kmod]
[867960.316091] [<c1069ddd>] kthread+0x6d/0x80
[867960.316104] [<c1069d70>] ? flush_kthread_worker+0x80/0x80
[867960.316118] [<c158033e>] kernel_thread_helper+0x6/0x10

本质上,随着请求额外的 per-cpu block ,dyn_size 可以根据需要使用 calls to pcpu_alloc_chunk() 增长.这在内部使用标准的 kmalloc() 调用来根据需要获取额外的内存。只要所需大小和对齐的内存块继续可用,这种情况就会继续。最终这将失败,具体取决于系统内存的使用/碎片化,这是当您看到 error 时。 .


pcpu_alloc() 是如何工作的?

在初始启动时,per-cpu 子系统从 Linux 内核可用的全局内存中保留一小块内存。

PERCPU: Embedded 13 pages/cpu @f77d1000 s31616 r0 d21632 u53248  

这是日志描述的内容。

Static 31616 + Dynamic 21632 = Total 53248 i.e. 52KB(13pages of 4KB each).

随着使用 pcpu_alloc() 进行越来越多的 per-cpu 分配,动态池的大小不断增长。它在内存中可以是不连续的,甚至是稀疏的。但只要满足要求的对齐和大小要求,这就会成功继续。这是因为分配是使用 kmalloc()/vmalloc() 进行的。

最终这些调用之一失败,因为满足请求的大小/对齐的内存空洞不可用。差不多就是这样。正如一个人无法预测一个 memalign()调用是否成功,很难准确判断 pcpu_alloc() 何时会失败。特别是因为甚至其他模块(和 Linux 内核本身)也可以调用 pcpu_alloc()

有关详细信息,请参阅 Linux-kernel/mm/percpu.c .

关于linux - PERCPU : allocation failed, size=256 align=256,分配新 block 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18230714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com