我在哪里可以找到 "sched_getcpu()" 的代码

Where could I find the code of "sched_getcpu()"

最近我在 Linux 上使用头文件 sched.h 中的函数 sched_getcpu()

但是,我想知道在哪里可以找到这个函数的源代码?

谢谢。

在 Linux 下,sched_getcpu() 函数是 sys_getcpu() 系统调用的 glibc 包装器,它是特定于体系结构的。

对于x86_64架构,在arch/x86/include/asm/vgtod.h下定义为__getcpu()(树4.x):

#ifdef CONFIG_X86_64

#define VGETCPU_CPU_MASK 0xfff

static inline unsigned int __getcpu(void)
{
        unsigned int p;

        /*
         * Load per CPU data from GDT.  LSL is faster than RDTSCP and
         * works on all CPUs.  This is volatile so that it orders
         * correctly wrt barrier() and to keep gcc from cleverly
         * hoisting it out of the calling function.
         */
        asm volatile ("lsl %1,%0" : "=r" (p) : "r" (__PER_CPU_SEG));

        return p;
}

#endif /* CONFIG_X86_64 */

__vdso_getcpu() 调用的函数在 arch/entry/vdso/vgetcpu.c 中声明:

notrace long
__vdso_getcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *unused)
{
        unsigned int p;

        p = __getcpu();

        if (cpu)
                *cpu = p & VGETCPU_CPU_MASK;
        if (node)
                *node = p >> 12;
        return 0;
}

(有关 vdso 前缀的详细信息,请参阅 vDSO)。

编辑 1:(回复 arm 代码位置)

ARM代码位置

可以在arch/arm/include/asm/thread_info.h文件中找到:

static inline struct thread_info *current_thread_info(void)
{
        return (struct thread_info *)
                (current_stack_pointer & ~(THREAD_SIZE - 1));
}

此函数由 raw_smp_processor_id() 使用,在文件 arch/arm/include/asm/smp.h 中定义为:

#define raw_smp_processor_id() (current_thread_info()->cpu)

它由文件kernel/sys.c:

中声明的getcpu系统调用调用
SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep, struct getcpu_cache __user *, unused)
{
        int err = 0;
        int cpu = raw_smp_processor_id();

        if (cpup)
                err |= put_user(cpu, cpup);
        if (nodep)
                err |= put_user(cpu_to_node(cpu), nodep);
        return err ? -EFAULT : 0;
}