Skip to content

Commit a3e6bd0

Browse files
Peter Zijlstragregkh
authored andcommitted
sched/rt: Fix double enqueue caused by rt_effective_prio
commit f558c2b upstream. Double enqueues in rt runqueues (list) have been reported while running a simple test that spawns a number of threads doing a short sleep/run pattern while being concurrently setscheduled between rt and fair class. WARNING: CPU: 3 PID: 2825 at kernel/sched/rt.c:1294 enqueue_task_rt+0x355/0x360 CPU: 3 PID: 2825 Comm: setsched__13 RIP: 0010:enqueue_task_rt+0x355/0x360 Call Trace: __sched_setscheduler+0x581/0x9d0 _sched_setscheduler+0x63/0xa0 do_sched_setscheduler+0xa0/0x150 __x64_sys_sched_setscheduler+0x1a/0x30 do_syscall_64+0x33/0x40 entry_SYSCALL_64_after_hwframe+0x44/0xae list_add double add: new=ffff9867cb629b40, prev=ffff9867cb629b40, next=ffff98679fc67ca0. kernel BUG at lib/list_debug.c:31! invalid opcode: 0000 [raspberrypi#1] PREEMPT_RT SMP PTI CPU: 3 PID: 2825 Comm: setsched__13 RIP: 0010:__list_add_valid+0x41/0x50 Call Trace: enqueue_task_rt+0x291/0x360 __sched_setscheduler+0x581/0x9d0 _sched_setscheduler+0x63/0xa0 do_sched_setscheduler+0xa0/0x150 __x64_sys_sched_setscheduler+0x1a/0x30 do_syscall_64+0x33/0x40 entry_SYSCALL_64_after_hwframe+0x44/0xae __sched_setscheduler() uses rt_effective_prio() to handle proper queuing of priority boosted tasks that are setscheduled while being boosted. rt_effective_prio() is however called twice per each __sched_setscheduler() call: first directly by __sched_setscheduler() before dequeuing the task and then by __setscheduler() to actually do the priority change. If the priority of the pi_top_task is concurrently being changed however, it might happen that the two calls return different results. If, for example, the first call returned the same rt priority the task was running at and the second one a fair priority, the task won't be removed by the rt list (on_list still set) and then enqueued in the fair runqueue. When eventually setscheduled back to rt it will be seen as enqueued already and the WARNING/BUG be issued. Fix this by calling rt_effective_prio() only once and then reusing the return value. While at it refactor code as well for clarity. Concurrent priority inheritance handling is still safe and will eventually converge to a new state by following the inheritance chain(s). Fixes: 0782e63 ("sched: Handle priority boosted tasks proper in setscheduler()") [squashed Peterz changes; added changelog] Reported-by: Mark Simmons <msimmons@redhat.com> Signed-off-by: Juri Lelli <juri.lelli@redhat.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/20210803104501.38333-1-juri.lelli@redhat.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent c797b88 commit a3e6bd0

1 file changed

Lines changed: 35 additions & 55 deletions

File tree

‎kernel/sched/core.c‎

Lines changed: 35 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,12 +1598,18 @@ void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
15981598
dequeue_task(rq, p, flags);
15991599
}
16001600

1601-
/*
1602-
* __normal_prio - return the priority that is based on the static prio
1603-
*/
1604-
static inline int __normal_prio(struct task_struct *p)
1601+
static inline int __normal_prio(int policy, int rt_prio, int nice)
16051602
{
1606-
return p->static_prio;
1603+
int prio;
1604+
1605+
if (dl_policy(policy))
1606+
prio = MAX_DL_PRIO - 1;
1607+
else if (rt_policy(policy))
1608+
prio = MAX_RT_PRIO - 1 - rt_prio;
1609+
else
1610+
prio = NICE_TO_PRIO(nice);
1611+
1612+
return prio;
16071613
}
16081614

16091615
/*
@@ -1615,15 +1621,7 @@ static inline int __normal_prio(struct task_struct *p)
16151621
*/
16161622
static inline int normal_prio(struct task_struct *p)
16171623
{
1618-
int prio;
1619-
1620-
if (task_has_dl_policy(p))
1621-
prio = MAX_DL_PRIO-1;
1622-
else if (task_has_rt_policy(p))
1623-
prio = MAX_RT_PRIO-1 - p->rt_priority;
1624-
else
1625-
prio = __normal_prio(p);
1626-
return prio;
1624+
return __normal_prio(p->policy, p->rt_priority, PRIO_TO_NICE(p->static_prio));
16271625
}
16281626

16291627
/*
@@ -3248,7 +3246,7 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p)
32483246
} else if (PRIO_TO_NICE(p->static_prio) < 0)
32493247
p->static_prio = NICE_TO_PRIO(0);
32503248

3251-
p->prio = p->normal_prio = __normal_prio(p);
3249+
p->prio = p->normal_prio = p->static_prio;
32523250
set_load_weight(p, false);
32533251

32543252
/*
@@ -4799,6 +4797,18 @@ int default_wake_function(wait_queue_entry_t *curr, unsigned mode, int wake_flag
47994797
}
48004798
EXPORT_SYMBOL(default_wake_function);
48014799

4800+
static void __setscheduler_prio(struct task_struct *p, int prio)
4801+
{
4802+
if (dl_prio(prio))
4803+
p->sched_class = &dl_sched_class;
4804+
else if (rt_prio(prio))
4805+
p->sched_class = &rt_sched_class;
4806+
else
4807+
p->sched_class = &fair_sched_class;
4808+
4809+
p->prio = prio;
4810+
}
4811+
48024812
#ifdef CONFIG_RT_MUTEXES
48034813

48044814
static inline int __rt_effective_prio(struct task_struct *pi_task, int prio)
@@ -4914,22 +4924,19 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)
49144924
} else {
49154925
p->dl.pi_se = &p->dl;
49164926
}
4917-
p->sched_class = &dl_sched_class;
49184927
} else if (rt_prio(prio)) {
49194928
if (dl_prio(oldprio))
49204929
p->dl.pi_se = &p->dl;
49214930
if (oldprio < prio)
49224931
queue_flag |= ENQUEUE_HEAD;
4923-
p->sched_class = &rt_sched_class;
49244932
} else {
49254933
if (dl_prio(oldprio))
49264934
p->dl.pi_se = &p->dl;
49274935
if (rt_prio(oldprio))
49284936
p->rt.timeout = 0;
4929-
p->sched_class = &fair_sched_class;
49304937
}
49314938

4932-
p->prio = prio;
4939+
__setscheduler_prio(p, prio);
49334940

49344941
if (queued)
49354942
enqueue_task(rq, p, queue_flag);
@@ -5162,35 +5169,6 @@ static void __setscheduler_params(struct task_struct *p,
51625169
set_load_weight(p, true);
51635170
}
51645171

5165-
/* Actually do priority change: must hold pi & rq lock. */
5166-
static void __setscheduler(struct rq *rq, struct task_struct *p,
5167-
const struct sched_attr *attr, bool keep_boost)
5168-
{
5169-
/*
5170-
* If params can't change scheduling class changes aren't allowed
5171-
* either.
5172-
*/
5173-
if (attr->sched_flags & SCHED_FLAG_KEEP_PARAMS)
5174-
return;
5175-
5176-
__setscheduler_params(p, attr);
5177-
5178-
/*
5179-
* Keep a potential priority boosting if called from
5180-
* sched_setscheduler().
5181-
*/
5182-
p->prio = normal_prio(p);
5183-
if (keep_boost)
5184-
p->prio = rt_effective_prio(p, p->prio);
5185-
5186-
if (dl_prio(p->prio))
5187-
p->sched_class = &dl_sched_class;
5188-
else if (rt_prio(p->prio))
5189-
p->sched_class = &rt_sched_class;
5190-
else
5191-
p->sched_class = &fair_sched_class;
5192-
}
5193-
51945172
/*
51955173
* Check the target process has a UID that matches the current process's:
51965174
*/
@@ -5211,10 +5189,8 @@ static int __sched_setscheduler(struct task_struct *p,
52115189
const struct sched_attr *attr,
52125190
bool user, bool pi)
52135191
{
5214-
int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
5215-
MAX_RT_PRIO - 1 - attr->sched_priority;
5216-
int retval, oldprio, oldpolicy = -1, queued, running;
5217-
int new_effective_prio, policy = attr->sched_policy;
5192+
int oldpolicy = -1, policy = attr->sched_policy;
5193+
int retval, oldprio, newprio, queued, running;
52185194
const struct sched_class *prev_class;
52195195
struct rq_flags rf;
52205196
int reset_on_fork;
@@ -5412,6 +5388,7 @@ static int __sched_setscheduler(struct task_struct *p,
54125388
p->sched_reset_on_fork = reset_on_fork;
54135389
oldprio = p->prio;
54145390

5391+
newprio = __normal_prio(policy, attr->sched_priority, attr->sched_nice);
54155392
if (pi) {
54165393
/*
54175394
* Take priority boosted tasks into account. If the new
@@ -5420,8 +5397,8 @@ static int __sched_setscheduler(struct task_struct *p,
54205397
* the runqueue. This will be done when the task deboost
54215398
* itself.
54225399
*/
5423-
new_effective_prio = rt_effective_prio(p, newprio);
5424-
if (new_effective_prio == oldprio)
5400+
newprio = rt_effective_prio(p, newprio);
5401+
if (newprio == oldprio)
54255402
queue_flags &= ~DEQUEUE_MOVE;
54265403
}
54275404

@@ -5434,7 +5411,10 @@ static int __sched_setscheduler(struct task_struct *p,
54345411

54355412
prev_class = p->sched_class;
54365413

5437-
__setscheduler(rq, p, attr, pi);
5414+
if (!(attr->sched_flags & SCHED_FLAG_KEEP_PARAMS)) {
5415+
__setscheduler_params(p, attr);
5416+
__setscheduler_prio(p, newprio);
5417+
}
54385418
__setscheduler_uclamp(p, attr);
54395419

54405420
if (queued) {

0 commit comments

Comments
 (0)