Normally we have to do like this to invoke a function from a function pointer:
int foo()
{
}
int main()
{
int (*pFoo)() = foo; // pFoo points to function foo()
foo();
return 0;
}
In the Linux kernel code, sched_class has many function pointers:
struct sched_class {
const struct sched_class *next;
void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
void (*yield_task) (struct rq *rq);
bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
.....
}
In pick_next_task function, it defines a local instance of sched_class named class, and directly invoke the function in it without assigning to external functions with the same signature (start from for_each_class):
static inline struct task_struct *
pick_next_task(struct rq *rq)
{
const struct sched_class *class;
struct task_struct *p;
/*
* Optimization: we know that if all tasks are in
* the fair class we can call that function directly:
*/
if (likely(rq->nr_running == rq->cfs.h_nr_running)) {
p = fair_sched_class.pick_next_task(rq);
if (likely(p))
return p;
}
for_each_class(class) {
p = class->pick_next_task(rq);
if (p)
return p;
}
BUG(); /* the idle class will always have a runnable task */
}
Is this because each function pointer in the sched_class has the same name as the actual implemented function, so every time a call is made via a function pointer of sched_class, it will automatically find the matching symbol in the kernel address space?
The definition of
for_each_classshould clear it up for youIf you go on tracing,
sched_class_highestwil end up something like thisNow are you happy? 🙂