start_kernel——boot_cpu_init及PER_CPU

init/main.c

/*
 *  Activate the first processor.
 */

static void __init boot_cpu_init(void)
{
    int cpu = smp_processor_id();
    /* Mark the boot cpu "present", "online" etc for SMP and UP case */
    set_cpu_online(cpu, true);
    set_cpu_active(cpu, true);
    set_cpu_present(cpu, true);
    set_cpu_possible(cpu, true);
}

active第一个CPU,默认第一个CPU为boot CPU.
include/linux/smp.h

/*
 * smp_processor_id(): get the current CPU ID.
 *
 * if DEBUG_PREEMPT is enabled then we check whether it is
 * used in a preemption-safe way. (smp_processor_id() is safe
 * if it's used in a preemption-off critical section, or in
 * a thread that is bound to the current CPU.)
 *
 * NOTE: raw_smp_processor_id() is for internal use only
 * (smp_processor_id() is the preferred variant), but in rare
 * instances it might also be used to turn off false positives
 * (i.e. smp_processor_id() use that the debugging code reports but
 * which use for some reason is legal). Don't use this to hack around
 * the warning message, as your code might not work under PREEMPT.
 */
#ifdef CONFIG_DEBUG_PREEMPT
  extern unsigned int debug_smp_processor_id(void);
# define smp_processor_id() debug_smp_processor_id()
#else
# define smp_processor_id() raw_smp_processor_id()
#endif

得到当前CPU的ID
在我的代码中定义了CONFIG_DEBUG_PREEMPT宏,所以调用
lib/smp_processor.c

vnotrace unsigned int debug_smp_processor_id(void)
{
    int this_cpu = raw_smp_processor_id();

    if (likely(preempt_count()))
        goto out;

    if (irqs_disabled())
        goto out;

    /*
     * Kernel threads bound to a single CPU can safely use
     * smp_processor_id():
     */
    if (cpumask_equal(tsk_cpus_allowed(current), cpumask_of(this_cpu)))
        goto out;

    /*
     * It is valid to assume CPU-locality during early bootup:
     */
    if (system_state != SYSTEM_RUNNING)
        goto out;

    /*
     * Avoid recursion:
     */
    pre
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值