[PATCH 1/3] sched: add sched_task_call()

Josh Poimboeuf
2015-02-16T18:53:31Z

Add a sched_task_call() to allow a callback function to be called with
the task's rq locked. It guarantees that a task remains either active
or inactive during the function call, without having to expose rq
locking details outside of the scheduler.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
---
include/linux/sched.h | 4 ++++
kernel/sched/core.c | 17 +++++++++++++++++
2 files changed, 21 insertions(+)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index 41c60e5..9b61e66 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2506,6 +2506,10 @@ static inline void set_task_comm(struct task_struct *tsk, const char *from)
}
extern char *get_task_comm(char *to, struct task_struct *tsk);

+typedef void (*sched_task_call_func_t)(struct task_struct *tsk, void *data);
+extern void sched_task_call(sched_task_call_func_t func,
+ struct task_struct *tsk, void *data);
+
#ifdef CONFIG_SMP
void scheduler_ipi(void);
extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 13049aa..c83a451 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -1338,6 +1338,23 @@ void kick_process(struct task_struct *p)
EXPORT_SYMBOL_GPL(kick_process);
#endif /* CONFIG_SMP */

+/***
+ * sched_task_call - call a function with a task's state locked
+ *
+ * The task is guaranteed to remain either active or inactive during the
+ * function call.
+ */
+void sched_task_call(sched_task_call_func_t func, struct task_struct *p,
+ void *data)
+{
+ unsigned long flags;
+ struct rq *rq;
+
+ rq = task_rq_lock(p, &flags);
+ func(p, data);
+ task_rq_unlock(rq, p, &flags);
+}
+
#ifdef CONFIG_SMP
/*
* ->cpus_allowed is protected by both rq->lock and p->pi_lock
--
2.1.0