Skip to content

Commit d329605

Browse files
htejunPeter Zijlstra
authored andcommitted
sched/fair: set_load_weight() must also call reweight_task() for SCHED_IDLE tasks
When a task's weight is being changed, set_load_weight() is called with @update_load set. As weight changes aren't trivial for the fair class, set_load_weight() calls fair.c::reweight_task() for fair class tasks. However, set_load_weight() first tests task_has_idle_policy() on entry and skips calling reweight_task() for SCHED_IDLE tasks. This is buggy as SCHED_IDLE tasks are just fair tasks with a very low weight and they would incorrectly skip load, vlag and position updates. Fix it by updating reweight_task() to take struct load_weight as idle weight can't be expressed with prio and making set_load_weight() call reweight_task() for SCHED_IDLE tasks too when @update_load is set. Fixes: 9059393 ("sched/fair: Use reweight_entity() for set_user_nice()") Suggested-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: stable@vger.kernel.org # v4.15+ Link: http://lkml.kernel.org/r/20240624102331.GI31592@noisy.programming.kicks-ass.net
1 parent 0ec208c commit d329605

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

kernel/sched/core.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,27 +1328,24 @@ int tg_nop(struct task_group *tg, void *data)
13281328
void set_load_weight(struct task_struct *p, bool update_load)
13291329
{
13301330
int prio = p->static_prio - MAX_RT_PRIO;
1331-
struct load_weight *load = &p->se.load;
1331+
struct load_weight lw;
13321332

1333-
/*
1334-
* SCHED_IDLE tasks get minimal weight:
1335-
*/
13361333
if (task_has_idle_policy(p)) {
1337-
load->weight = scale_load(WEIGHT_IDLEPRIO);
1338-
load->inv_weight = WMULT_IDLEPRIO;
1339-
return;
1334+
lw.weight = scale_load(WEIGHT_IDLEPRIO);
1335+
lw.inv_weight = WMULT_IDLEPRIO;
1336+
} else {
1337+
lw.weight = scale_load(sched_prio_to_weight[prio]);
1338+
lw.inv_weight = sched_prio_to_wmult[prio];
13401339
}
13411340

13421341
/*
13431342
* SCHED_OTHER tasks have to update their load when changing their
13441343
* weight
13451344
*/
1346-
if (update_load && p->sched_class == &fair_sched_class) {
1347-
reweight_task(p, prio);
1348-
} else {
1349-
load->weight = scale_load(sched_prio_to_weight[prio]);
1350-
load->inv_weight = sched_prio_to_wmult[prio];
1351-
}
1345+
if (update_load && p->sched_class == &fair_sched_class)
1346+
reweight_task(p, &lw);
1347+
else
1348+
p->se.load = lw;
13521349
}
13531350

13541351
#ifdef CONFIG_UCLAMP_TASK

kernel/sched/fair.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3835,15 +3835,14 @@ static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
38353835
}
38363836
}
38373837

3838-
void reweight_task(struct task_struct *p, int prio)
3838+
void reweight_task(struct task_struct *p, const struct load_weight *lw)
38393839
{
38403840
struct sched_entity *se = &p->se;
38413841
struct cfs_rq *cfs_rq = cfs_rq_of(se);
38423842
struct load_weight *load = &se->load;
3843-
unsigned long weight = scale_load(sched_prio_to_weight[prio]);
38443843

3845-
reweight_entity(cfs_rq, se, weight);
3846-
load->inv_weight = sched_prio_to_wmult[prio];
3844+
reweight_entity(cfs_rq, se, lw->weight);
3845+
load->inv_weight = lw->inv_weight;
38473846
}
38483847

38493848
static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);

kernel/sched/sched.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2509,7 +2509,7 @@ extern void init_sched_dl_class(void);
25092509
extern void init_sched_rt_class(void);
25102510
extern void init_sched_fair_class(void);
25112511

2512-
extern void reweight_task(struct task_struct *p, int prio);
2512+
extern void reweight_task(struct task_struct *p, const struct load_weight *lw);
25132513

25142514
extern void resched_curr(struct rq *rq);
25152515
extern void resched_cpu(int cpu);

0 commit comments

Comments
 (0)