What is the difference between ”current“ and “prev” in the “__schedule” function, why not use current directly?

Viewed 125

From the kernel source code:

static void __sched notrace __schedule(bool preempt)
{
        struct task_struct *prev, *next;
        unsigned long *switch_count;
        struct rq_flags rf;
        struct rq *rq;
        int cpu;

        cpu = smp_processor_id();
        rq = cpu_rq(cpu);
        prev = rq->curr;  // <=================(1)
        .....

What is the difference between current and prev at the line (1) above?

Adding a printk() after that line:

static void __sched notrace __schedule(bool preempt)
{
        struct task_struct *prev, *next;
        unsigned long *switch_count;
        struct rq_flags rf;
        struct rq *rq;
        int cpu;

        cpu = smp_processor_id();
        rq = cpu_rq(cpu);
        prev = rq->curr;
        //if(prev->pid != current->pid)
        printk("the prev->pid =%d, The current = %d \n", prev->pid, current->pid); // <===========(2)

        schedule_debug(prev);

        if (sched_feat(HRTICK))
                hrtick_clear(rq);

The resulting logs from (2) are:

[ 4184.164038] the prev->pid =0, The current = 0 
[ 4184.165365] the prev->pid =12, The current = 12 
[ 4186.008068] the prev->pid =0, The current = 0 
[ 4186.009396] the prev->pid =30, The current = 30 
[ 4188.008046] the prev->pid =0, The current = 0 
[ 4188.009334] the prev->pid =30, The current = 30 
[ 4188.164041] the prev->pid =0, The current = 0 
[ 4188.165315] the prev->pid =12, The current = 12 
[ 4190.008047] the prev->pid =0, The current = 0 
[ 4190.009431] the prev->pid =30, The current = 30 

The displays show that the result is the same.

  • why not use current instead of
    cpu = smp_processor_id(); 
    rq = cpu_rq(cpu); 
    prev = rq->curr; 
1 Answers

prev is a variable used for temporarily remembering the current task when it's being swapped with a new one in the runqueue. Of course, if you look at prev exactly right after doing prev = rq->curr you will have prev == rq->curr == current, since it was just assigned to it.

If you want to inspect which task is being picked to run next, you'll have to look after that, when __schedule() does:

next = pick_next_task(rq, prev, &rf);

Now next will be different from prev.

Related