Skip to content

Commit 13765de

Browse files
tstrukPeter Zijlstra
authored andcommitted
sched/fair: Fix fault in reweight_entity
Syzbot found a GPF in reweight_entity. This has been bisected to commit 4ef0c5c ("kernel/sched: Fix sched_fork() access an invalid sched_task_group") There is a race between sched_post_fork() and setpriority(PRIO_PGRP) within a thread group that causes a null-ptr-deref in reweight_entity() in CFS. The scenario is that the main process spawns number of new threads, which then call setpriority(PRIO_PGRP, 0, -20), wait, and exit. For each of the new threads the copy_process() gets invoked, which adds the new task_struct and calls sched_post_fork() for it. In the above scenario there is a possibility that setpriority(PRIO_PGRP) and set_one_prio() will be called for a thread in the group that is just being created by copy_process(), and for which the sched_post_fork() has not been executed yet. This will trigger a null pointer dereference in reweight_entity(), as it will try to access the run queue pointer, which hasn't been set. Before the mentioned change the cfs_rq pointer for the task has been set in sched_fork(), which is called much earlier in copy_process(), before the new task is added to the thread_group. Now it is done in the sched_post_fork(), which is called after that. To fix the issue the remove the update_load param from the update_load param() function and call reweight_task() only if the task flag doesn't have the TASK_NEW flag set. Fixes: 4ef0c5c ("kernel/sched: Fix sched_fork() access an invalid sched_task_group") Reported-by: [email protected] Signed-off-by: Tadeusz Struk <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Dietmar Eggemann <[email protected]> Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent 26291c5 commit 13765de

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

kernel/sched/core.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,8 +1214,9 @@ int tg_nop(struct task_group *tg, void *data)
12141214
}
12151215
#endif
12161216

1217-
static void set_load_weight(struct task_struct *p, bool update_load)
1217+
static void set_load_weight(struct task_struct *p)
12181218
{
1219+
bool update_load = !(READ_ONCE(p->__state) & TASK_NEW);
12191220
int prio = p->static_prio - MAX_RT_PRIO;
12201221
struct load_weight *load = &p->se.load;
12211222

@@ -4406,7 +4407,7 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p)
44064407
p->static_prio = NICE_TO_PRIO(0);
44074408

44084409
p->prio = p->normal_prio = p->static_prio;
4409-
set_load_weight(p, false);
4410+
set_load_weight(p);
44104411

44114412
/*
44124413
* We don't need the reset flag anymore after the fork. It has
@@ -6921,7 +6922,7 @@ void set_user_nice(struct task_struct *p, long nice)
69216922
put_prev_task(rq, p);
69226923

69236924
p->static_prio = NICE_TO_PRIO(nice);
6924-
set_load_weight(p, true);
6925+
set_load_weight(p);
69256926
old_prio = p->prio;
69266927
p->prio = effective_prio(p);
69276928

@@ -7212,7 +7213,7 @@ static void __setscheduler_params(struct task_struct *p,
72127213
*/
72137214
p->rt_priority = attr->sched_priority;
72147215
p->normal_prio = normal_prio(p);
7215-
set_load_weight(p, true);
7216+
set_load_weight(p);
72167217
}
72177218

72187219
/*
@@ -9445,7 +9446,7 @@ void __init sched_init(void)
94459446
#endif
94469447
}
94479448

9448-
set_load_weight(&init_task, false);
9449+
set_load_weight(&init_task);
94499450

94509451
/*
94519452
* The boot idle thread does lazy MMU switching as well:

0 commit comments

Comments
 (0)