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_class
should clear it up for you
#define for_each_class(class) \
for (class = sched_class_highest; class; class = class->next)
If you go on tracing, sched_class_highest
wil end up something like this
#define sched_class_highest (&stop_sched_class)
extern const struct sched_class stop_sched_class;
/*
* Simple, special scheduling class for the per-CPU stop tasks:
*/
const struct sched_class stop_sched_class = {
.next = &rt_sched_class,
.enqueue_task = enqueue_task_stop,
.dequeue_task = dequeue_task_stop,
.yield_task = yield_task_stop,
.check_preempt_curr = check_preempt_curr_stop,
.pick_next_task = pick_next_task_stop,
.put_prev_task = put_prev_task_stop,
#ifdef CONFIG_SMP
.select_task_rq = select_task_rq_stop,
#endif
.set_curr_task = set_curr_task_stop,
.task_tick = task_tick_stop,
.get_rr_interval = get_rr_interval_stop,
.prio_changed = prio_changed_stop,
.switched_to = switched_to_stop,
};
Now are you happy? :)
https://github.com/torvalds/linux/blob/v3.3/kernel/sched/sched.h#L850
Look at the expansion of for_each_class
macro. It assigns values to the class
pointer before using it.
Each sched_class
structure, and the function pointers it contains, is initialized (otherwise, it's probably a bug). For instance, the fair scheduling class is initialized in kernel/sched/fair.c
(see here):
const struct sched_class fair_sched_class = {
.next = &idle_sched_class,
/* lots of assignments */
.pick_next_task = pick_next_task_fair,
/* etc. */
};