Skip to content

Commit 7ab85d4

Browse files
committed
Merge branch 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull scheduler fixes from Thomas Gleixner: "Three small fixes in the scheduler/core: - use after free in the numa code - crash in the numa init code - a simple spelling fix" * 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: pid: Fix spelling in comments sched/numa: Fix use-after-free bug in the task_numa_compare sched: Fix crash in sched_init_numa()
2 parents 29d14f0 + 840d6fe commit 7ab85d4

3 files changed

Lines changed: 25 additions & 9 deletions

File tree

‎kernel/pid.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ void __init pidhash_init(void)
588588

589589
void __init pidmap_init(void)
590590
{
591-
/* Veryify no one has done anything silly */
591+
/* Verify no one has done anything silly: */
592592
BUILD_BUG_ON(PID_MAX_LIMIT >= PIDNS_HASH_ADDING);
593593

594594
/* bump default and minimum pid_max based on number of cpus */

‎kernel/sched/core.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6840,7 +6840,7 @@ static void sched_init_numa(void)
68406840

68416841
sched_domains_numa_masks[i][j] = mask;
68426842

6843-
for (k = 0; k < nr_node_ids; k++) {
6843+
for_each_node(k) {
68446844
if (node_distance(j, k) > sched_domains_numa_distance[i])
68456845
continue;
68466846

‎kernel/sched/fair.c‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,8 +1220,6 @@ static void task_numa_assign(struct task_numa_env *env,
12201220
{
12211221
if (env->best_task)
12221222
put_task_struct(env->best_task);
1223-
if (p)
1224-
get_task_struct(p);
12251223

12261224
env->best_task = p;
12271225
env->best_imp = imp;
@@ -1289,20 +1287,30 @@ static void task_numa_compare(struct task_numa_env *env,
12891287
long imp = env->p->numa_group ? groupimp : taskimp;
12901288
long moveimp = imp;
12911289
int dist = env->dist;
1290+
bool assigned = false;
12921291

12931292
rcu_read_lock();
12941293

12951294
raw_spin_lock_irq(&dst_rq->lock);
12961295
cur = dst_rq->curr;
12971296
/*
1298-
* No need to move the exiting task, and this ensures that ->curr
1299-
* wasn't reaped and thus get_task_struct() in task_numa_assign()
1300-
* is safe under RCU read lock.
1301-
* Note that rcu_read_lock() itself can't protect from the final
1302-
* put_task_struct() after the last schedule().
1297+
* No need to move the exiting task or idle task.
13031298
*/
13041299
if ((cur->flags & PF_EXITING) || is_idle_task(cur))
13051300
cur = NULL;
1301+
else {
1302+
/*
1303+
* The task_struct must be protected here to protect the
1304+
* p->numa_faults access in the task_weight since the
1305+
* numa_faults could already be freed in the following path:
1306+
* finish_task_switch()
1307+
* --> put_task_struct()
1308+
* --> __put_task_struct()
1309+
* --> task_numa_free()
1310+
*/
1311+
get_task_struct(cur);
1312+
}
1313+
13061314
raw_spin_unlock_irq(&dst_rq->lock);
13071315

13081316
/*
@@ -1386,6 +1394,7 @@ static void task_numa_compare(struct task_numa_env *env,
13861394
*/
13871395
if (!load_too_imbalanced(src_load, dst_load, env)) {
13881396
imp = moveimp - 1;
1397+
put_task_struct(cur);
13891398
cur = NULL;
13901399
goto assign;
13911400
}
@@ -1411,9 +1420,16 @@ static void task_numa_compare(struct task_numa_env *env,
14111420
env->dst_cpu = select_idle_sibling(env->p, env->dst_cpu);
14121421

14131422
assign:
1423+
assigned = true;
14141424
task_numa_assign(env, cur, imp);
14151425
unlock:
14161426
rcu_read_unlock();
1427+
/*
1428+
* The dst_rq->curr isn't assigned. The protection for task_struct is
1429+
* finished.
1430+
*/
1431+
if (cur && !assigned)
1432+
put_task_struct(cur);
14171433
}
14181434

14191435
static void task_numa_find_cpu(struct task_numa_env *env,

0 commit comments

Comments
 (0)