Skip to content

Commit 1243dc5

Browse files
jlelliingomolnar
authored andcommitted
cgroup/cpuset: Convert cpuset_mutex to percpu_rwsem
Holding cpuset_mutex means that cpusets are stable (only the holder can make changes) and this is required for fixing a synchronization issue between cpusets and scheduler core. However, grabbing cpuset_mutex from setscheduler() hotpath (as implemented in a later patch) is a no-go, as it would create a bottleneck for tasks concurrently calling setscheduler(). Convert cpuset_mutex to be a percpu_rwsem (cpuset_rwsem), so that setscheduler() will then be able to read lock it and avoid concurrency issues. 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-6-juri.lelli@redhat.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 59d06ce commit 1243dc5

1 file changed

Lines changed: 35 additions & 33 deletions

File tree

‎kernel/cgroup/cpuset.c‎

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ static struct cpuset top_cpuset = {
333333
* guidelines for accessing subsystem state in kernel/cgroup.c
334334
*/
335335

336-
static DEFINE_MUTEX(cpuset_mutex);
336+
DEFINE_STATIC_PERCPU_RWSEM(cpuset_rwsem);
337337
static DEFINE_SPINLOCK(callback_lock);
338338

339339
static struct workqueue_struct *cpuset_migrate_mm_wq;
@@ -913,7 +913,7 @@ static void rebuild_root_domains(void)
913913
struct cpuset *cs = NULL;
914914
struct cgroup_subsys_state *pos_css;
915915

916-
lockdep_assert_held(&cpuset_mutex);
916+
percpu_rwsem_assert_held(&cpuset_rwsem);
917917
lockdep_assert_cpus_held();
918918
lockdep_assert_held(&sched_domains_mutex);
919919

@@ -973,7 +973,7 @@ static void rebuild_sched_domains_locked(void)
973973
cpumask_var_t *doms;
974974
int ndoms;
975975

976-
lockdep_assert_held(&cpuset_mutex);
976+
percpu_rwsem_assert_held(&cpuset_rwsem);
977977
get_online_cpus();
978978

979979
/*
@@ -1005,9 +1005,9 @@ static void rebuild_sched_domains_locked(void)
10051005

10061006
void rebuild_sched_domains(void)
10071007
{
1008-
mutex_lock(&cpuset_mutex);
1008+
percpu_down_write(&cpuset_rwsem);
10091009
rebuild_sched_domains_locked();
1010-
mutex_unlock(&cpuset_mutex);
1010+
percpu_up_write(&cpuset_rwsem);
10111011
}
10121012

10131013
/**
@@ -1113,7 +1113,7 @@ static int update_parent_subparts_cpumask(struct cpuset *cpuset, int cmd,
11131113
int deleting; /* Moving cpus from subparts_cpus to effective_cpus */
11141114
bool part_error = false; /* Partition error? */
11151115

1116-
lockdep_assert_held(&cpuset_mutex);
1116+
percpu_rwsem_assert_held(&cpuset_rwsem);
11171117

11181118
/*
11191119
* The parent must be a partition root.
@@ -2101,7 +2101,7 @@ static int cpuset_can_attach(struct cgroup_taskset *tset)
21012101
cpuset_attach_old_cs = task_cs(cgroup_taskset_first(tset, &css));
21022102
cs = css_cs(css);
21032103

2104-
mutex_lock(&cpuset_mutex);
2104+
percpu_down_write(&cpuset_rwsem);
21052105

21062106
/* allow moving tasks into an empty cpuset if on default hierarchy */
21072107
ret = -ENOSPC;
@@ -2125,7 +2125,7 @@ static int cpuset_can_attach(struct cgroup_taskset *tset)
21252125
cs->attach_in_progress++;
21262126
ret = 0;
21272127
out_unlock:
2128-
mutex_unlock(&cpuset_mutex);
2128+
percpu_up_write(&cpuset_rwsem);
21292129
return ret;
21302130
}
21312131

@@ -2135,9 +2135,9 @@ static void cpuset_cancel_attach(struct cgroup_taskset *tset)
21352135

21362136
cgroup_taskset_first(tset, &css);
21372137

2138-
mutex_lock(&cpuset_mutex);
2138+
percpu_down_write(&cpuset_rwsem);
21392139
css_cs(css)->attach_in_progress--;
2140-
mutex_unlock(&cpuset_mutex);
2140+
percpu_up_write(&cpuset_rwsem);
21412141
}
21422142

21432143
/*
@@ -2160,7 +2160,7 @@ static void cpuset_attach(struct cgroup_taskset *tset)
21602160
cgroup_taskset_first(tset, &css);
21612161
cs = css_cs(css);
21622162

2163-
mutex_lock(&cpuset_mutex);
2163+
percpu_down_write(&cpuset_rwsem);
21642164

21652165
/* prepare for attach */
21662166
if (cs == &top_cpuset)
@@ -2214,7 +2214,7 @@ static void cpuset_attach(struct cgroup_taskset *tset)
22142214
if (!cs->attach_in_progress)
22152215
wake_up(&cpuset_attach_wq);
22162216

2217-
mutex_unlock(&cpuset_mutex);
2217+
percpu_up_write(&cpuset_rwsem);
22182218
}
22192219

22202220
/* The various types of files and directories in a cpuset file system */
@@ -2245,7 +2245,7 @@ static int cpuset_write_u64(struct cgroup_subsys_state *css, struct cftype *cft,
22452245
cpuset_filetype_t type = cft->private;
22462246
int retval = 0;
22472247

2248-
mutex_lock(&cpuset_mutex);
2248+
percpu_down_write(&cpuset_rwsem);
22492249
if (!is_cpuset_online(cs)) {
22502250
retval = -ENODEV;
22512251
goto out_unlock;
@@ -2281,7 +2281,7 @@ static int cpuset_write_u64(struct cgroup_subsys_state *css, struct cftype *cft,
22812281
break;
22822282
}
22832283
out_unlock:
2284-
mutex_unlock(&cpuset_mutex);
2284+
percpu_up_write(&cpuset_rwsem);
22852285
return retval;
22862286
}
22872287

@@ -2292,7 +2292,7 @@ static int cpuset_write_s64(struct cgroup_subsys_state *css, struct cftype *cft,
22922292
cpuset_filetype_t type = cft->private;
22932293
int retval = -ENODEV;
22942294

2295-
mutex_lock(&cpuset_mutex);
2295+
percpu_down_write(&cpuset_rwsem);
22962296
if (!is_cpuset_online(cs))
22972297
goto out_unlock;
22982298

@@ -2305,7 +2305,7 @@ static int cpuset_write_s64(struct cgroup_subsys_state *css, struct cftype *cft,
23052305
break;
23062306
}
23072307
out_unlock:
2308-
mutex_unlock(&cpuset_mutex);
2308+
percpu_up_write(&cpuset_rwsem);
23092309
return retval;
23102310
}
23112311

@@ -2344,7 +2344,7 @@ static ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
23442344
kernfs_break_active_protection(of->kn);
23452345
flush_work(&cpuset_hotplug_work);
23462346

2347-
mutex_lock(&cpuset_mutex);
2347+
percpu_down_write(&cpuset_rwsem);
23482348
if (!is_cpuset_online(cs))
23492349
goto out_unlock;
23502350

@@ -2368,7 +2368,7 @@ static ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
23682368

23692369
free_cpuset(trialcs);
23702370
out_unlock:
2371-
mutex_unlock(&cpuset_mutex);
2371+
percpu_up_write(&cpuset_rwsem);
23722372
kernfs_unbreak_active_protection(of->kn);
23732373
css_put(&cs->css);
23742374
flush_workqueue(cpuset_migrate_mm_wq);
@@ -2499,13 +2499,13 @@ static ssize_t sched_partition_write(struct kernfs_open_file *of, char *buf,
24992499
return -EINVAL;
25002500

25012501
css_get(&cs->css);
2502-
mutex_lock(&cpuset_mutex);
2502+
percpu_down_write(&cpuset_rwsem);
25032503
if (!is_cpuset_online(cs))
25042504
goto out_unlock;
25052505

25062506
retval = update_prstate(cs, val);
25072507
out_unlock:
2508-
mutex_unlock(&cpuset_mutex);
2508+
percpu_up_write(&cpuset_rwsem);
25092509
css_put(&cs->css);
25102510
return retval ?: nbytes;
25112511
}
@@ -2711,7 +2711,7 @@ static int cpuset_css_online(struct cgroup_subsys_state *css)
27112711
if (!parent)
27122712
return 0;
27132713

2714-
mutex_lock(&cpuset_mutex);
2714+
percpu_down_write(&cpuset_rwsem);
27152715

27162716
set_bit(CS_ONLINE, &cs->flags);
27172717
if (is_spread_page(parent))
@@ -2762,7 +2762,7 @@ static int cpuset_css_online(struct cgroup_subsys_state *css)
27622762
cpumask_copy(cs->effective_cpus, parent->cpus_allowed);
27632763
spin_unlock_irq(&callback_lock);
27642764
out_unlock:
2765-
mutex_unlock(&cpuset_mutex);
2765+
percpu_up_write(&cpuset_rwsem);
27662766
return 0;
27672767
}
27682768

@@ -2781,7 +2781,7 @@ static void cpuset_css_offline(struct cgroup_subsys_state *css)
27812781
{
27822782
struct cpuset *cs = css_cs(css);
27832783

2784-
mutex_lock(&cpuset_mutex);
2784+
percpu_down_write(&cpuset_rwsem);
27852785

27862786
if (is_partition_root(cs))
27872787
update_prstate(cs, 0);
@@ -2800,7 +2800,7 @@ static void cpuset_css_offline(struct cgroup_subsys_state *css)
28002800
cpuset_dec();
28012801
clear_bit(CS_ONLINE, &cs->flags);
28022802

2803-
mutex_unlock(&cpuset_mutex);
2803+
percpu_up_write(&cpuset_rwsem);
28042804
}
28052805

28062806
static void cpuset_css_free(struct cgroup_subsys_state *css)
@@ -2812,7 +2812,7 @@ static void cpuset_css_free(struct cgroup_subsys_state *css)
28122812

28132813
static void cpuset_bind(struct cgroup_subsys_state *root_css)
28142814
{
2815-
mutex_lock(&cpuset_mutex);
2815+
percpu_down_write(&cpuset_rwsem);
28162816
spin_lock_irq(&callback_lock);
28172817

28182818
if (is_in_v2_mode()) {
@@ -2825,7 +2825,7 @@ static void cpuset_bind(struct cgroup_subsys_state *root_css)
28252825
}
28262826

28272827
spin_unlock_irq(&callback_lock);
2828-
mutex_unlock(&cpuset_mutex);
2828+
percpu_up_write(&cpuset_rwsem);
28292829
}
28302830

28312831
/*
@@ -2867,6 +2867,8 @@ struct cgroup_subsys cpuset_cgrp_subsys = {
28672867

28682868
int __init cpuset_init(void)
28692869
{
2870+
BUG_ON(percpu_init_rwsem(&cpuset_rwsem));
2871+
28702872
BUG_ON(!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL));
28712873
BUG_ON(!alloc_cpumask_var(&top_cpuset.effective_cpus, GFP_KERNEL));
28722874
BUG_ON(!zalloc_cpumask_var(&top_cpuset.subparts_cpus, GFP_KERNEL));
@@ -2938,7 +2940,7 @@ hotplug_update_tasks_legacy(struct cpuset *cs,
29382940
is_empty = cpumask_empty(cs->cpus_allowed) ||
29392941
nodes_empty(cs->mems_allowed);
29402942

2941-
mutex_unlock(&cpuset_mutex);
2943+
percpu_up_write(&cpuset_rwsem);
29422944

29432945
/*
29442946
* Move tasks to the nearest ancestor with execution resources,
@@ -2948,7 +2950,7 @@ hotplug_update_tasks_legacy(struct cpuset *cs,
29482950
if (is_empty)
29492951
remove_tasks_in_empty_cpuset(cs);
29502952

2951-
mutex_lock(&cpuset_mutex);
2953+
percpu_down_write(&cpuset_rwsem);
29522954
}
29532955

29542956
static void
@@ -2998,14 +3000,14 @@ static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp)
29983000
retry:
29993001
wait_event(cpuset_attach_wq, cs->attach_in_progress == 0);
30003002

3001-
mutex_lock(&cpuset_mutex);
3003+
percpu_down_write(&cpuset_rwsem);
30023004

30033005
/*
30043006
* We have raced with task attaching. We wait until attaching
30053007
* is finished, so we won't attach a task to an empty cpuset.
30063008
*/
30073009
if (cs->attach_in_progress) {
3008-
mutex_unlock(&cpuset_mutex);
3010+
percpu_up_write(&cpuset_rwsem);
30093011
goto retry;
30103012
}
30113013

@@ -3073,7 +3075,7 @@ static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp)
30733075
hotplug_update_tasks_legacy(cs, &new_cpus, &new_mems,
30743076
cpus_updated, mems_updated);
30753077

3076-
mutex_unlock(&cpuset_mutex);
3078+
percpu_up_write(&cpuset_rwsem);
30773079
}
30783080

30793081
/**
@@ -3103,7 +3105,7 @@ static void cpuset_hotplug_workfn(struct work_struct *work)
31033105
if (on_dfl && !alloc_cpumasks(NULL, &tmp))
31043106
ptmp = &tmp;
31053107

3106-
mutex_lock(&cpuset_mutex);
3108+
percpu_down_write(&cpuset_rwsem);
31073109

31083110
/* fetch the available cpus/mems and find out which changed how */
31093111
cpumask_copy(&new_cpus, cpu_active_mask);
@@ -3153,7 +3155,7 @@ static void cpuset_hotplug_workfn(struct work_struct *work)
31533155
update_tasks_nodemask(&top_cpuset);
31543156
}
31553157

3156-
mutex_unlock(&cpuset_mutex);
3158+
percpu_up_write(&cpuset_rwsem);
31573159

31583160
/* if cpus or mems changed, we need to propagate to descendants */
31593161
if (cpus_updated || mems_updated) {

0 commit comments

Comments
 (0)