vmalloc_init() 函数中的for_each_possible_cpu 宏,代码是否只在一个cpu 中运行?还是在每个 CPU 中? [英] for_each_possible_cpu macro in vmalloc_init() function, does the code run in only one cpu? or in every cpu?

查看:85
本文介绍了vmalloc_init() 函数中的for_each_possible_cpu 宏,代码是否只在一个cpu 中运行?还是在每个 CPU 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与编程无关,但我在这里问..在 linux start_kernel() 函数中,在 mm_init() 函数中,我看到了 vmalloc_init() 函数.在函数内部,我看到这样的代码.

this is not about programming, but I ask it here.. in linux start_kernel() function, in the mm_init() function, I see vmalloc_init() function. inside the function I see codes like this.

void __init vmalloc_init(void)
{
    struct vmap_area *va;
    struct vm_struct *tmp;
    int i;
        
    /*  
     * Create the cache for vmap_area objects.
     */
    vmap_area_cachep = KMEM_CACHE(vmap_area, SLAB_PANIC);

    for_each_possible_cpu(i) {
        struct vmap_block_queue *vbq;
        struct vfree_deferred *p;
                    
        vbq = &per_cpu(vmap_block_queue, i);
        spin_lock_init(&vbq->lock);
        INIT_LIST_HEAD(&vbq->free);
        p = &per_cpu(vfree_deferred, i);
        init_llist_head(&p->list);
        INIT_WORK(&p->wq, free_work);
    }

    /* Import existing vmlist entries. */
    for (tmp = vmlist; tmp; tmp = tmp->next) {
        va = kmem_cache_zalloc(vmap_area_cachep, GFP_NOWAIT);
        if (WARN_ON_ONCE(!va))
            continue;

        va->va_start = (unsigned long)tmp->addr;
        va->va_end = va->va_start + tmp->size;
        va->vm = tmp;
        insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
    }

    /*
     * Now we can initialize a free vmap space.
     */
    vmap_init_free_space();
    vmap_initialized = true;
}

我不确定此代码是在每个 cpu(核心)上运行还是仅在第一个 cpu 上运行?
如果此代码在每个 smp 内核上运行,那么 for_each_possible_cpu 循环中的此代码如何运行?
smp 设置好像是在这个函数之前完成的.

I'm not sure if this code is run on every cpu(core) or just on the first cpu?
if this code runs on every smp core, how is this code inside for_each_possible_cpu loop run?
The smp setup seems to be done before this function.

推荐答案

start_kernel() 调用 mm_init() 调用 vmalloc_init().此时只有第一个(引导)CPU 处于活动状态.之后,start_kernel() 调用了 arch_call_rest_init(),后者调用了 rest_init().

start_kernel() calls mm_init() which calls vmalloc_init(). Only the first (boot) CPU is active at that point. Later, start_kernel() calls arch_call_rest_init() which calls rest_init().

rest_init() 使用入口点 kernel_init() 为 init 任务创建内核线程.kernel_init() 调用 kernel_init_freeable().kernel_init_freeable() 最终调用 smp_init() 来激活剩余的 CPU.

rest_init() creates a kernel thread for the init task with entry point kernel_init(). kernel_init() calls kernel_init_freeable(). kernel_init_freeable() eventually calls smp_init() to activate the remaining CPUs.

这篇关于vmalloc_init() 函数中的for_each_possible_cpu 宏,代码是否只在一个cpu 中运行?还是在每个 CPU 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆