Skip to content

Commit 710da3c

Browse files
jlelliingomolnar
authored andcommitted
sched/core: Prevent race condition between cpuset and __sched_setscheduler()
No synchronisation mechanism exists between the cpuset subsystem and calls to function __sched_setscheduler(). As such, it is possible that new root domains are created on the cpuset side while a deadline acceptance test is carried out in __sched_setscheduler(), leading to a potential oversell of CPU bandwidth. Grab cpuset_rwsem read lock from core scheduler, so to prevent situations such as the one described above from happening. The only exception is normalize_rt_tasks() which needs to work under tasklist_lock and can't therefore grab cpuset_rwsem. We are fine with this, as this function is only called by sysrq and, if that gets triggered, DEADLINE guarantees are already gone out of the window anyway. Tested-by: Dietmar Eggemann <dietmar.eggemann@arm.com> Signed-off-by: Juri Lelli <juri.lelli@redhat.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: bristot@redhat.com Cc: claudio@evidence.eu.com Cc: lizefan@huawei.com Cc: longman@redhat.com Cc: luca.abeni@santannapisa.it Cc: mathieu.poirier@linaro.org Cc: rostedt@goodmis.org Cc: tj@kernel.org Cc: tommaso.cucinotta@santannapisa.it Link: https://lkml.kernel.org/r/20190719140000.31694-9-juri.lelli@redhat.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 1a763fd commit 710da3c

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎include/linux/cpuset.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ extern void cpuset_init_smp(void);
5555
extern void cpuset_force_rebuild(void);
5656
extern void cpuset_update_active_cpus(void);
5757
extern void cpuset_wait_for_hotplug(void);
58+
extern void cpuset_read_lock(void);
59+
extern void cpuset_read_unlock(void);
5860
extern void cpuset_cpus_allowed(struct task_struct *p, struct cpumask *mask);
5961
extern void cpuset_cpus_allowed_fallback(struct task_struct *p);
6062
extern nodemask_t cpuset_mems_allowed(struct task_struct *p);
@@ -176,6 +178,9 @@ static inline void cpuset_update_active_cpus(void)
176178

177179
static inline void cpuset_wait_for_hotplug(void) { }
178180

181+
static inline void cpuset_read_lock(void) { }
182+
static inline void cpuset_read_unlock(void) { }
183+
179184
static inline void cpuset_cpus_allowed(struct task_struct *p,
180185
struct cpumask *mask)
181186
{

‎kernel/cgroup/cpuset.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,17 @@ static struct cpuset top_cpuset = {
334334
*/
335335

336336
DEFINE_STATIC_PERCPU_RWSEM(cpuset_rwsem);
337+
338+
void cpuset_read_lock(void)
339+
{
340+
percpu_down_read(&cpuset_rwsem);
341+
}
342+
343+
void cpuset_read_unlock(void)
344+
{
345+
percpu_up_read(&cpuset_rwsem);
346+
}
347+
337348
static DEFINE_SPINLOCK(callback_lock);
338349

339350
static struct workqueue_struct *cpuset_migrate_mm_wq;

‎kernel/sched/core.c‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4698,6 +4698,9 @@ static int __sched_setscheduler(struct task_struct *p,
46984698
return retval;
46994699
}
47004700

4701+
if (pi)
4702+
cpuset_read_lock();
4703+
47014704
/*
47024705
* Make sure no PI-waiters arrive (or leave) while we are
47034706
* changing the priority of the task:
@@ -4772,6 +4775,8 @@ static int __sched_setscheduler(struct task_struct *p,
47724775
if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
47734776
policy = oldpolicy = -1;
47744777
task_rq_unlock(rq, p, &rf);
4778+
if (pi)
4779+
cpuset_read_unlock();
47754780
goto recheck;
47764781
}
47774782

@@ -4832,8 +4837,10 @@ static int __sched_setscheduler(struct task_struct *p,
48324837
preempt_disable();
48334838
task_rq_unlock(rq, p, &rf);
48344839

4835-
if (pi)
4840+
if (pi) {
4841+
cpuset_read_unlock();
48364842
rt_mutex_adjust_pi(p);
4843+
}
48374844

48384845
/* Run balance callbacks after we've adjusted the PI chain: */
48394846
balance_callback(rq);
@@ -4843,6 +4850,8 @@ static int __sched_setscheduler(struct task_struct *p,
48434850

48444851
unlock:
48454852
task_rq_unlock(rq, p, &rf);
4853+
if (pi)
4854+
cpuset_read_unlock();
48464855
return retval;
48474856
}
48484857

@@ -4927,10 +4936,15 @@ do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
49274936
rcu_read_lock();
49284937
retval = -ESRCH;
49294938
p = find_process_by_pid(pid);
4930-
if (p != NULL)
4931-
retval = sched_setscheduler(p, policy, &lparam);
4939+
if (likely(p))
4940+
get_task_struct(p);
49324941
rcu_read_unlock();
49334942

4943+
if (likely(p)) {
4944+
retval = sched_setscheduler(p, policy, &lparam);
4945+
put_task_struct(p);
4946+
}
4947+
49344948
return retval;
49354949
}
49364950

0 commit comments

Comments
 (0)