Skip to content

Commit

Permalink
sched/core: Use kfree_rcu() in do_set_cpus_allowed()
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
Waiman-Long authored and Ingo Molnar committed Jan 9, 2023
1 parent 87ca4f9 commit 9a5418b
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions kernel/sched/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2604,9 +2604,29 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
.user_mask = NULL,
.flags = SCA_USER, /* clear the user requested mask */
};
union cpumask_rcuhead {
cpumask_t cpumask;
struct rcu_head rcu;
};

__do_set_cpus_allowed(p, &ac);
kfree(ac.user_mask);

/*
* Because this is called with p->pi_lock held, it is not possible
* to use kfree() here (when PREEMPT_RT=y), therefore punt to using
* kfree_rcu().
*/
kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);
}

static cpumask_t *alloc_user_cpus_ptr(int node)
{
/*
* See do_set_cpus_allowed() above for the rcu_head usage.
*/
int size = max_t(int, cpumask_size(), sizeof(struct rcu_head));

return kmalloc_node(size, GFP_KERNEL, node);
}

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

user_mask = kmalloc_node(cpumask_size(), GFP_KERNEL, node);
user_mask = alloc_user_cpus_ptr(node);
if (!user_mask)
return -ENOMEM;

Expand Down Expand Up @@ -3605,6 +3625,11 @@ static inline bool rq_has_pinned_tasks(struct rq *rq)
return false;
}

static inline cpumask_t *alloc_user_cpus_ptr(int node)
{
return NULL;
}

#endif /* !CONFIG_SMP */

static void
Expand Down Expand Up @@ -8265,8 +8290,8 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
if (retval)
goto out_put_task;

user_mask = kmalloc(cpumask_size(), GFP_KERNEL);
if (!user_mask) {
user_mask = alloc_user_cpus_ptr(NUMA_NO_NODE);
if (IS_ENABLED(CONFIG_SMP) && !user_mask) {
retval = -ENOMEM;
goto out_put_task;
}
Expand Down

0 comments on commit 9a5418b

Please sign in to comment.