diff mbox

[tip/core/rcu,4/9] sched: add is_idle_task() to handle invalidated uses of idle_cpu()

Message ID 1321388885-11211-4-git-send-email-paulmck@linux.vnet.ibm.com
State New
Headers show

Commit Message

Paul E. McKenney Nov. 15, 2011, 8:28 p.m. UTC
From: Paul E. McKenney <paul.mckenney@linaro.org>

Commit 908a3283 (Fix idle_cpu()) invalidated some uses of idle_cpu(),
which used to say whether or not the CPU was running the idle task,
but now instead says whether or not the CPU is running the idle task
in the absence of pending wakeups.  Although this new implementation
gives a better answer to the question "is this CPU idle?", it also
invalidates other uses that were made of idle_cpu().

This commit therefore introduces a new is_idle_task() API member
that determines whether or not the specified task is one of the
idle tasks, allowing open-coded "->pid == 0" sequences to be replaced
by something more meaningful.

Suggested-by: Josh Triplett <josh@joshtriplett.org>
Suggested-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Paul E. McKenney <paul.mckenney@linaro.org>
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
---
 include/linux/sched.h |    1 +
 kernel/sched.c        |    9 +++++++++
 2 files changed, 10 insertions(+), 0 deletions(-)

Comments

Josh Triplett Nov. 15, 2011, 9:13 p.m. UTC | #1
On Tue, Nov 15, 2011 at 12:28:00PM -0800, Paul E. McKenney wrote:
> From: Paul E. McKenney <paul.mckenney@linaro.org>
> 
> Commit 908a3283 (Fix idle_cpu()) invalidated some uses of idle_cpu(),
> which used to say whether or not the CPU was running the idle task,
> but now instead says whether or not the CPU is running the idle task
> in the absence of pending wakeups.  Although this new implementation
> gives a better answer to the question "is this CPU idle?", it also
> invalidates other uses that were made of idle_cpu().
> 
> This commit therefore introduces a new is_idle_task() API member
> that determines whether or not the specified task is one of the
> idle tasks, allowing open-coded "->pid == 0" sequences to be replaced
> by something more meaningful.
> 
> Suggested-by: Josh Triplett <josh@joshtriplett.org>
> Suggested-by: Peter Zijlstra <peterz@infradead.org>
> Signed-off-by: Paul E. McKenney <paul.mckenney@linaro.org>
> Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
> ---
>  include/linux/sched.h |    1 +
>  kernel/sched.c        |    9 +++++++++
>  2 files changed, 10 insertions(+), 0 deletions(-)
> 
> diff --git a/include/linux/sched.h b/include/linux/sched.h
> index 68daf4f..4b1077b 100644
> --- a/include/linux/sched.h
> +++ b/include/linux/sched.h
> @@ -2071,6 +2071,7 @@ extern int sched_setscheduler(struct task_struct *, int,
>  extern int sched_setscheduler_nocheck(struct task_struct *, int,
>  				      const struct sched_param *);
>  extern struct task_struct *idle_task(int cpu);
> +extern int is_idle_task(struct task_struct *p);

The kernel has a bool type; please use it here rather than int, to make
the return value more obvious.

Also, any particular reason not to make this a static inline?

- Josh Triplett
Paul E. McKenney Nov. 16, 2011, 7:54 p.m. UTC | #2
On Tue, Nov 15, 2011 at 01:13:02PM -0800, Josh Triplett wrote:
> On Tue, Nov 15, 2011 at 12:28:00PM -0800, Paul E. McKenney wrote:
> > From: Paul E. McKenney <paul.mckenney@linaro.org>
> > 
> > Commit 908a3283 (Fix idle_cpu()) invalidated some uses of idle_cpu(),
> > which used to say whether or not the CPU was running the idle task,
> > but now instead says whether or not the CPU is running the idle task
> > in the absence of pending wakeups.  Although this new implementation
> > gives a better answer to the question "is this CPU idle?", it also
> > invalidates other uses that were made of idle_cpu().
> > 
> > This commit therefore introduces a new is_idle_task() API member
> > that determines whether or not the specified task is one of the
> > idle tasks, allowing open-coded "->pid == 0" sequences to be replaced
> > by something more meaningful.
> > 
> > Suggested-by: Josh Triplett <josh@joshtriplett.org>
> > Suggested-by: Peter Zijlstra <peterz@infradead.org>
> > Signed-off-by: Paul E. McKenney <paul.mckenney@linaro.org>
> > Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
> > ---
> >  include/linux/sched.h |    1 +
> >  kernel/sched.c        |    9 +++++++++
> >  2 files changed, 10 insertions(+), 0 deletions(-)
> > 
> > diff --git a/include/linux/sched.h b/include/linux/sched.h
> > index 68daf4f..4b1077b 100644
> > --- a/include/linux/sched.h
> > +++ b/include/linux/sched.h
> > @@ -2071,6 +2071,7 @@ extern int sched_setscheduler(struct task_struct *, int,
> >  extern int sched_setscheduler_nocheck(struct task_struct *, int,
> >  				      const struct sched_param *);
> >  extern struct task_struct *idle_task(int cpu);
> > +extern int is_idle_task(struct task_struct *p);
> 
> The kernel has a bool type; please use it here rather than int, to make
> the return value more obvious.
> 
> Also, any particular reason not to make this a static inline?

That does work for all the uses thus far, so made both suggested changes.

							Thanx, Paul
diff mbox

Patch

diff --git a/include/linux/sched.h b/include/linux/sched.h
index 68daf4f..4b1077b 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2071,6 +2071,7 @@  extern int sched_setscheduler(struct task_struct *, int,
 extern int sched_setscheduler_nocheck(struct task_struct *, int,
 				      const struct sched_param *);
 extern struct task_struct *idle_task(int cpu);
+extern int is_idle_task(struct task_struct *p);
 extern struct task_struct *curr_task(int cpu);
 extern void set_curr_task(int cpu, struct task_struct *p);
 
diff --git a/kernel/sched.c b/kernel/sched.c
index 0e9344a..c9c1bb3 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -5185,6 +5185,15 @@  struct task_struct *idle_task(int cpu)
 }
 
 /**
+ * is_idle_task - is the specified task an idle task?
+ * @tsk: the task in question.
+ */
+int is_idle_task(struct task_struct *p)
+{
+	return p->pid == 0;
+}
+
+/**
  * find_process_by_pid - find a process with a matching PID value.
  * @pid: the pid in question.
  */