Question:

If we have a pointer to a task structure, ie, struct task_struct *p, how to know if p is running on a cpu or not (suppose we're talking about the SMP system)?

Answer from the kernel (lot's of useful infomation in this good knowledge base -- kernel source code) --

The 1st function:
static inline unsigned int task_cpu(const struct task_struct *p)
{
}

We can guess if a process (eg, the p above) has been scheduled to a cpu, then task_thread_info(p)->cpu will be the record that on which cpu the task (p) is running, so if a task is running on a cpu, we can know the index of cpu that the task is running by "task_thread_info(p)->cpu",  OK, we got the cpu number to which the task p is bound. Now we can check the cpu running task queue using cpu_curr(cpu), cpu_curr(cpu) will get the running queue belong to the cpu specified by the parameter. As a followed step, current running queue (q for short) will provide the clue what the current running task via q->curr, so :

if p = cpu_curr(task_cpu(p), then p is the current running task on the cpu.
02-05 19:42