Re: [PATCH] sched/rt: Fix double enqueue caused by rt_effective_prio

From: Peter Zijlstra
Date: Thu Jul 08 2021 - 06:26:49 EST


On Thu, Jul 08, 2021 at 12:06:27PM +0200, Peter Zijlstra wrote:
> Slightly larger patch, but perhaps a little cleaner.. still pondering if
> we can share a little more between __sched_setscheduler() and
> rt_mutex_setprio().

Best I can seem to come up with...

---
kernel/sched/core.c | 45 +++++++++++++++++----------------------------
1 file changed, 17 insertions(+), 28 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index ede10642612c..c686f0c70656 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -6341,6 +6341,18 @@ int default_wake_function(wait_queue_entry_t *curr, unsigned mode, int wake_flag
}
EXPORT_SYMBOL(default_wake_function);

+static void __setscheduler_prio(struct task_struct *p, int prio)
+{
+ if (dl_prio(prio))
+ p->sched_class = &dl_sched_class;
+ else if (rt_prio(prio))
+ p->sched_class = &rt_sched_class;
+ else
+ p->sched_class = &fair_sched_class;
+
+ p->prio = prio;
+}
+
#ifdef CONFIG_RT_MUTEXES

static inline int __rt_effective_prio(struct task_struct *pi_task, int prio)
@@ -6456,22 +6468,19 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)
} else {
p->dl.pi_se = &p->dl;
}
- p->sched_class = &dl_sched_class;
} else if (rt_prio(prio)) {
if (dl_prio(oldprio))
p->dl.pi_se = &p->dl;
if (oldprio < prio)
queue_flag |= ENQUEUE_HEAD;
- p->sched_class = &rt_sched_class;
} else {
if (dl_prio(oldprio))
p->dl.pi_se = &p->dl;
if (rt_prio(oldprio))
p->rt.timeout = 0;
- p->sched_class = &fair_sched_class;
}

- p->prio = prio;
+ __setscheduler_prio(p, prio);

if (queued)
enqueue_task(rq, p, queue_flag);
@@ -6824,29 +6833,6 @@ static void __setscheduler_params(struct task_struct *p,
set_load_weight(p, true);
}

-/* Actually do priority change: must hold pi & rq lock. */
-static void __setscheduler(struct rq *rq, struct task_struct *p,
- const struct sched_attr *attr, int newprio)
-{
- /*
- * If params can't change scheduling class changes aren't allowed
- * either.
- */
- if (attr->sched_flags & SCHED_FLAG_KEEP_PARAMS)
- return;
-
- __setscheduler_params(p, attr);
-
- p->prio = newprio;
-
- if (dl_prio(p->prio))
- p->sched_class = &dl_sched_class;
- else if (rt_prio(p->prio))
- p->sched_class = &rt_sched_class;
- else
- p->sched_class = &fair_sched_class;
-}
-
/*
* Check the target process has a UID that matches the current process's:
*/
@@ -7089,7 +7075,10 @@ static int __sched_setscheduler(struct task_struct *p,

prev_class = p->sched_class;

- __setscheduler(rq, p, attr, newprio);
+ if (!(attr->sched_flags & SCHED_FLAG_KEEP_PARAMS)) {
+ __setscheduler_params(p, attr);
+ __setscheduler_prio(p, newprio);
+ }
__setscheduler_uclamp(p, attr);

if (queued) {