在linux的驱动代码中经常可以看到__weak去修饰一个函数或者变量,大多是用来修饰函数。它的作用有两个:
1.weak 顾名思义是“弱”的意思,所以如果函数名称前面加上__weak 修饰符,我们一般称这个函数为**“弱函数”**。加上了__weak 修饰符的函数,用户可以在用户文件中重新定义一个同名函数,最终编译器编译的时候,会选择用户定义的函数,如果用户没有重新定义这个函数,那么编译器就会执行__weak 声明的函数,并且编译器不会报错。
2.__weak 在回调函数的时候经常用到。这样的好处是,系统默认定义了一个空的回调函数,保证编译器不会报错。同时,如果用户自己要定义用户回调函数,那么只需要重新定义即可,不需要考虑函数重复定义的问题,使用非常方便
在linux/init/main.c中有函数smp_setup_processor_id
void __init __weak smp_setup_processor_id(void)
{
}
2.6.30内核中ARM结构没有额外定义smp_setup_processor_id(),所以ARM结构不执行任何操作。
n-ubuntu05@nubuntu05:linux-2.6.30.4$ grep -rn smp_setup_processor_id ./*
./arch/sparc/kernel/smp_64.c:1182:void __init smp_setup_processor_id(void)
./include/linux/smp.h:188:void smp_setup_processor_id(void);
./init/main.c:528:void __init __weak smp_setup_processor_id(void)
./init/main.c:541:smp_setup_processor_id();
v4.9内核在*./arch/arm/kernel/setup.c*中定义了smp_setup_processor_id():
584 void __init smp_setup_processor_id(void)
585 {
586 int i;
587 u32 mpidr = is_smp() ? read_cpuid_mpidr() & MPIDR_HWID_BITMASK : 0;
588 u32 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
589
590 cpu_logical_map(0) = cpu;
591 for (i = 1; i < nr_cpu_ids; ++i)
592 cpu_logical_map(i) = i == cpu ? 0 : i;
593
594 /*
595 * clear __my_cpu_offset on boot CPU to avoid hang caused by
596 * using percpu variable early, for example, lockdep will
597 * access percpu variable inside lock_release
598 */
599 set_my_cpu_offset(0);
600
601 pr_info("Booting Linux on physical CPU 0x%x\n", mpidr);
602 }
由于linux/init/main.c中定义的函数有__weak属性,所以ARM使用的是*./arch/arm/kernel/setup.c*中定义的smp_setup_processor_id
v4.9其他结构中相关定义:
n-ubuntu05@nubuntu05:v4.9$ grep -rn smp_setup_processor_id ./*
./arch/arm64/kernel/setup.c:95:void __init smp_setup_processor_id(void)
./arch/s390/kernel/smp.c:930:void __init smp_setup_processor_id(void)
./arch/sparc/kernel/smp_64.c:1238:void __init smp_setup_processor_id(void)
./arch/arm/kernel/devtree.c:147: * to override the map created in smp_setup_processor_id().
./arch/arm/kernel/devtree.c:185: * logical map created in smp_setup_processor_id() can be overridden
./arch/arm/kernel/setup.c:584:void __init smp_setup_processor_id(void)
./include/linux/smp.h:197:void smp_setup_processor_id(void);
./init/main.c:452:void __init __weak smp_setup_processor_id(void)
./init/main.c:487:smp_setup_processor_id();