Skip to content

Commit 9a5418b

Browse files
Waiman-LongIngo Molnar
authored andcommitted
sched/core: Use kfree_rcu() in do_set_cpus_allowed()
Commit 851a723 ("sched: Always clear user_cpus_ptr in do_set_cpus_allowed()") may call kfree() if user_cpus_ptr was previously set. Unfortunately, some of the callers of do_set_cpus_allowed() may have pi_lock held when calling it. So the following splats may be printed especially when running with a PREEMPT_RT kernel: WARNING: possible circular locking dependency detected BUG: sleeping function called from invalid context To avoid these problems, kfree_rcu() is used instead. An internal cpumask_rcuhead union is created for the sole purpose of facilitating the use of kfree_rcu() to free the cpumask. Since user_cpus_ptr is not being used in non-SMP configs, the newly introduced alloc_user_cpus_ptr() helper will return NULL in this case and sched_setaffinity() is modified to handle this special case. Fixes: 851a723 ("sched: Always clear user_cpus_ptr in do_set_cpus_allowed()") Suggested-by: Peter Zijlstra <[email protected]> Signed-off-by: Waiman Long <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Reviewed-by: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 87ca4f9 commit 9a5418b

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

kernel/sched/core.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,9 +2604,29 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
26042604
.user_mask = NULL,
26052605
.flags = SCA_USER, /* clear the user requested mask */
26062606
};
2607+
union cpumask_rcuhead {
2608+
cpumask_t cpumask;
2609+
struct rcu_head rcu;
2610+
};
26072611

26082612
__do_set_cpus_allowed(p, &ac);
2609-
kfree(ac.user_mask);
2613+
2614+
/*
2615+
* Because this is called with p->pi_lock held, it is not possible
2616+
* to use kfree() here (when PREEMPT_RT=y), therefore punt to using
2617+
* kfree_rcu().
2618+
*/
2619+
kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);
2620+
}
2621+
2622+
static cpumask_t *alloc_user_cpus_ptr(int node)
2623+
{
2624+
/*
2625+
* See do_set_cpus_allowed() above for the rcu_head usage.
2626+
*/
2627+
int size = max_t(int, cpumask_size(), sizeof(struct rcu_head));
2628+
2629+
return kmalloc_node(size, GFP_KERNEL, node);
26102630
}
26112631

26122632
int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src,
@@ -2629,7 +2649,7 @@ int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src,
26292649
if (data_race(!src->user_cpus_ptr))
26302650
return 0;
26312651

2632-
user_mask = kmalloc_node(cpumask_size(), GFP_KERNEL, node);
2652+
user_mask = alloc_user_cpus_ptr(node);
26332653
if (!user_mask)
26342654
return -ENOMEM;
26352655

@@ -3605,6 +3625,11 @@ static inline bool rq_has_pinned_tasks(struct rq *rq)
36053625
return false;
36063626
}
36073627

3628+
static inline cpumask_t *alloc_user_cpus_ptr(int node)
3629+
{
3630+
return NULL;
3631+
}
3632+
36083633
#endif /* !CONFIG_SMP */
36093634

36103635
static void
@@ -8265,8 +8290,8 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
82658290
if (retval)
82668291
goto out_put_task;
82678292

8268-
user_mask = kmalloc(cpumask_size(), GFP_KERNEL);
8269-
if (!user_mask) {
8293+
user_mask = alloc_user_cpus_ptr(NUMA_NO_NODE);
8294+
if (IS_ENABLED(CONFIG_SMP) && !user_mask) {
82708295
retval = -ENOMEM;
82718296
goto out_put_task;
82728297
}

0 commit comments

Comments
 (0)