Skip to content

Commit ea66bf8

Browse files
committed
Merge tag 'sched-urgent-2023-01-12' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull scheduler fixes from Ingo Molnar: - Fix scheduler frequency invariance bug related to overly long tickless periods triggering an integer overflow and disabling the feature. - Fix use-after-free bug in dup_user_cpus_ptr(). - Fix do_set_cpus_allowed() deadlock scenarios related to calling kfree() with the pi_lock held. NOTE: the rcu_free() is the 'lazy' solution here - we looked at patches to free the structure after the pi_lock got dropped, but that looked quite a bit messier - and none of this is truly performance critical. We can revisit this if it's too lazy of a solution ... * tag 'sched-urgent-2023-01-12' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: sched/core: Use kfree_rcu() in do_set_cpus_allowed() sched/core: Fix use-after-free bug in dup_user_cpus_ptr() sched/core: Fix arch_scale_freq_tick() on tickless systems
2 parents cf4d5be + 9a5418b commit ea66bf8

File tree

1 file changed

+60
-9
lines changed

1 file changed

+60
-9
lines changed

kernel/sched/core.c

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,27 +2604,71 @@ 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,
26132633
int node)
26142634
{
2635+
cpumask_t *user_mask;
26152636
unsigned long flags;
26162637

2617-
if (!src->user_cpus_ptr)
2638+
/*
2639+
* Always clear dst->user_cpus_ptr first as their user_cpus_ptr's
2640+
* may differ by now due to racing.
2641+
*/
2642+
dst->user_cpus_ptr = NULL;
2643+
2644+
/*
2645+
* This check is racy and losing the race is a valid situation.
2646+
* It is not worth the extra overhead of taking the pi_lock on
2647+
* every fork/clone.
2648+
*/
2649+
if (data_race(!src->user_cpus_ptr))
26182650
return 0;
26192651

2620-
dst->user_cpus_ptr = kmalloc_node(cpumask_size(), GFP_KERNEL, node);
2621-
if (!dst->user_cpus_ptr)
2652+
user_mask = alloc_user_cpus_ptr(node);
2653+
if (!user_mask)
26222654
return -ENOMEM;
26232655

2624-
/* Use pi_lock to protect content of user_cpus_ptr */
2656+
/*
2657+
* Use pi_lock to protect content of user_cpus_ptr
2658+
*
2659+
* Though unlikely, user_cpus_ptr can be reset to NULL by a concurrent
2660+
* do_set_cpus_allowed().
2661+
*/
26252662
raw_spin_lock_irqsave(&src->pi_lock, flags);
2626-
cpumask_copy(dst->user_cpus_ptr, src->user_cpus_ptr);
2663+
if (src->user_cpus_ptr) {
2664+
swap(dst->user_cpus_ptr, user_mask);
2665+
cpumask_copy(dst->user_cpus_ptr, src->user_cpus_ptr);
2666+
}
26272667
raw_spin_unlock_irqrestore(&src->pi_lock, flags);
2668+
2669+
if (unlikely(user_mask))
2670+
kfree(user_mask);
2671+
26282672
return 0;
26292673
}
26302674

@@ -3581,6 +3625,11 @@ static inline bool rq_has_pinned_tasks(struct rq *rq)
35813625
return false;
35823626
}
35833627

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

35863635
static void
@@ -5504,7 +5553,9 @@ void scheduler_tick(void)
55045553
unsigned long thermal_pressure;
55055554
u64 resched_latency;
55065555

5507-
arch_scale_freq_tick();
5556+
if (housekeeping_cpu(cpu, HK_TYPE_TICK))
5557+
arch_scale_freq_tick();
5558+
55085559
sched_clock_tick();
55095560

55105561
rq_lock(rq, &rf);
@@ -8239,8 +8290,8 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
82398290
if (retval)
82408291
goto out_put_task;
82418292

8242-
user_mask = kmalloc(cpumask_size(), GFP_KERNEL);
8243-
if (!user_mask) {
8293+
user_mask = alloc_user_cpus_ptr(NUMA_NO_NODE);
8294+
if (IS_ENABLED(CONFIG_SMP) && !user_mask) {
82448295
retval = -ENOMEM;
82458296
goto out_put_task;
82468297
}

0 commit comments

Comments
 (0)