[RFC PATCH v2 1/5] sched: get effective policy and rt_prio

From: Julien Desfossez
Date: Fri Sep 23 2016 - 12:49:44 EST


Helper functions to get the effective policy and rt_priority from the
prio and policy values. This is useful in PI situations because these
fields are not updated in the task, only the sched_class is temporarily
modified.

Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Steven Rostedt (Red Hat) <rostedt@xxxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
Signed-off-by: Julien Desfossez <jdesfossez@xxxxxxxxxxxx>
---
include/linux/sched.h | 2 ++
kernel/sched/core.c | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 38 insertions(+)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index af39baf..0c03595 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2594,6 +2594,8 @@ static inline bool is_idle_task(const struct task_struct *p)
struct thread_info thread_info;
unsigned long stack[THREAD_SIZE/sizeof(long)];
};
+extern int effective_policy(int policy, int prio);
+extern int effective_rt_prio(int prio);

#ifndef __HAVE_ARCH_KSTACK_END
static inline int kstack_end(void *addr)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index f5f7b3c..f3817b5 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -916,6 +916,42 @@ static int effective_prio(struct task_struct *p)
return p->prio;
}

+/*
+ * Get the effective policy based on the current prio value.
+ */
+int effective_policy(int policy, int prio)
+{
+ if (dl_prio(prio))
+ return SCHED_DEADLINE;
+
+ /* With RT, the default class is SCHED_FIFO. */
+ if (rt_prio(prio)) {
+ if (policy == SCHED_RR)
+ return SCHED_RR;
+ return SCHED_FIFO;
+ }
+
+ /* With fair, the default class is SCHED_NORMAL. */
+ switch (policy) {
+ case SCHED_NORMAL:
+ case SCHED_IDLE:
+ case SCHED_BATCH:
+ return policy;
+ }
+ return SCHED_NORMAL;
+}
+
+/*
+ * Get the effective rt priority based on the current prio value.
+ */
+int effective_rt_prio(int prio)
+{
+ if (!rt_prio(prio))
+ return 0;
+
+ return MAX_RT_PRIO - 1 - prio;
+}
+
/**
* task_curr - is this task currently executing on a CPU?
* @p: the task in question.
--
1.9.1