cgroup/cpuset: Change nr_deadline_tasks to an atomic_t value
Subsystems (2)
Waiman Long
maintainerPATCH 1/1 cgroup/cpuset: Change nr_deadline_tasks to an atomic_t value
The nr_deadline_tasks field in cpuset structure was introduced by commit 6c24849f5515 ("sched/cpuset: Keep track of SCHED_DEADLINE task in cpusets"). Unlike nr_migrate_dl_tasks which is only modified under cpuset_mutex, nr_deadline_tasks can be updated in various contexts under different locks. As a result, data races may happen that cause incorrect value to be stored in nr_deadline_tasks leading to incorrect exit from dl_update_tasks_root_domain(). Fix that data race problem by making nr_deadline_tasks an atomic_t value.
--- kernel/cgroup/cpuset.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
kernel/cgroup/cpuset.c +6 -6
@@ -174,7 +174,7 @@ struct cpuset {* number of SCHED_DEADLINE tasks attached to this cpuset, so that we * know when to rebuild associated root domain bandwidth information. */ -int nr_deadline_tasks; +atomic_t nr_deadline_tasks;int nr_migrate_dl_tasks;u64 sum_migrate_dl_bw;
@@ -234,14 +234,14 @@ void inc_dl_tasks_cs(struct task_struct *p) {struct cpuset *cs =task_cs (p); - cs->nr_deadline_tasks++; +atomic_inc (&cs->nr_deadline_tasks); }void dec_dl_tasks_cs (struct task_struct *p) {struct cpuset *cs =task_cs (p); - cs->nr_deadline_tasks--; +atomic_dec (&cs->nr_deadline_tasks); }/* bits in struct cpuset flags field */
@@ -1071,7 +1071,7 @@ static void dl_update_tasks_root_domain(struct cpuset *cs)struct css_task_iter it;struct task_struct *task; -if (cs->nr_deadline_tasks ==0 ) +if (atomic_read (&cs->nr_deadline_tasks) ==0 )return ;css_task_iter_start (&cs->css,0 , &it);
@@ -2721,8 +2721,8 @@ static void cpuset_attach(struct cgroup_taskset *tset) cs->old_mems_allowed = cpuset_attach_nodemask_to;if (cs->nr_migrate_dl_tasks) { - cs->nr_deadline_tasks += cs->nr_migrate_dl_tasks; - oldcs->nr_deadline_tasks -= cs->nr_migrate_dl_tasks; +atomic_add (cs->nr_migrate_dl_tasks, &cs->nr_deadline_tasks); +atomic_sub (cs->nr_migrate_dl_tasks, &oldcs->nr_deadline_tasks);reset_migrate_dl_data (cs); }
Signature · 1 line
2.39.3
Hi,
Quoted 6 lines · Waiman Long
The
nr_deadline_tasksfield in cpuset structure was introduced by commit6c24849f5515("sched/cpuset: Keep track ofSCHED_DEADLINEtask in cpusets"). Unlikenr_migrate_dl_taskswhich is only modified undercpuset_mutex,nr_deadline_taskscan be updated in various contexts under different locks. As a result, data races may happen that cause incorrect value to be stored innr_deadline_tasksleading to incorrect
Could you please make an example of such data races?
Thanks!
Juri
Waiman Long
maintainerQuoted 10 lines · Juri Lelli
Hi,
Quoted 6 lines · Waiman Long
The
nr_deadline_tasksfield in cpuset structure was introduced by commit6c24849f5515("sched/cpuset: Keep track ofSCHED_DEADLINEtask in cpusets"). Unlikenr_migrate_dl_taskswhich is only modified undercpuset_mutex,nr_deadline_taskscan be updated in various contexts under different locks. As a result, data races may happen that cause incorrect value to be stored innr_deadline_tasksleading to incorrectCould you please make an example of such data races?
Since update to cs->nr_deadline_tasks is not protected by a single lock, it is possible that multiple CPUs may try to modify it at the same time. It is possible that nr_deadline_tasks++ and nr_deadline_tasks-- can be done in a single instruction like in x86 and hence atomic. However, operation like "cs->nr_deadline_tasks += cs->nr_migrate_dl_tasks" is likely a RMW operation and so is subjected to racing. It is mostly theoretical, but probably not impossible.
Cheers,
Longman
Waiman Long
maintainerQuoted 11 lines · Waiman Long
Quoted 9 lines · Juri Lelli
Hi,
Quoted 6 lines · Waiman Long
The
nr_deadline_tasksfield in cpuset structure was introduced by commit6c24849f5515("sched/cpuset: Keep track ofSCHED_DEADLINEtask in cpusets"). Unlikenr_migrate_dl_taskswhich is only modified undercpuset_mutex,nr_deadline_taskscan be updated in various contexts under different locks. As a result, data races may happen that cause incorrect value to be stored innr_deadline_tasksleading to incorrect
Quoted 3 lines
Quoted 1 line
Could you please make an example of such data races?
Since update to
cs->nr_deadline_tasksis not protected by a single lock, it is possible that multiple CPUs may try to modify it at the same time. It is possible that nr_deadline_tasks++ and nr_deadline_tasks-- can be done in a single instruction like in x86 and hence atomic. However, operation like "cs->nr_deadline_tasks+=cs->nr_migrate_dl_tasks" is likely a RMW operation and so is subjected to racing. It is mostly theoretical, but probably not impossible.
Sorry, even increment and decrement operators are not atomic.
inc_dl_tasks_cs() is only called from switched_to_dl() in deadline.c which is protected by the rq_lock, but there are multiple rq's. dec_dl_tasks_cs() is called from switched_from_dl() in deadline.c and cgroup_exit() in cgroup.c. The later one is protected by css_set_lock. The other place where nr_deadline_tasks can be changed is in cpuset_attach() protected by cpuset_mutex.
Cheers,
Longman
Quoted 23 lines · Waiman Long
Quoted 13 lines · Waiman Long
Quoted 10 lines · Juri Lelli
Hi,
Quoted 6 lines · Waiman Long
The
nr_deadline_tasksfield in cpuset structure was introduced by commit6c24849f5515("sched/cpuset: Keep track ofSCHED_DEADLINEtask in cpusets"). Unlikenr_migrate_dl_taskswhich is only modified undercpuset_mutex,nr_deadline_taskscan be updated in various contexts under different locks. As a result, data races may happen that cause incorrect value to be stored innr_deadline_tasksleading to incorrectCould you please make an example of such data races?
Quoted 7 lines
Since update to
cs->nr_deadline_tasksis not protected by a single lock, it is possible that multiple CPUs may try to modify it at the same time. It is possible that nr_deadline_tasks++ and nr_deadline_tasks-- can be done in a single instruction like in x86 and hence atomic. However, operation like "cs->nr_deadline_tasks+=cs->nr_migrate_dl_tasks" is likely a RMW operation and so is subjected to racing. It is mostly theoretical, but probably not impossible.
Quoted 8 lines
Sorry, even increment and decrement operators are not atomic.
inc_dl_tasks_cs()is only called fromswitched_to_dl()indeadline.cwhich is protected by therq_lock, but there are multiple rq's.dec_dl_tasks_cs()is called fromswitched_from_dl()indeadline.candcgroup_exit()incgroup.c. The later one is protected bycss_set_lock. The other place wherenr_deadline_taskscan be changed is incpuset_attach()protected bycpuset_mutex.
So, let's see. :)
switched_to_dl(), switched_from_dl() and cpuset_attach() should all be protected (for DEADLINE tasks) by cpuset_mutex, see [1] for the former two.
What leaves me perplexed is indeed cgroup_exit(), which seems to operate under css_set_lock as you say. I however wonder why is that not racy already wrt, say, cpuset_attach() which AFAIU uses css information w/o holding css_set_lock?
Thanks,
Juri
1 - https://elixir.bootlin.com/linux/latest/source/kernel/sched/core.c#L7688
Waiman Long
maintainerQuoted 28 lines · Juri Lelli
Quoted 19 lines · Waiman Long
Quoted 10 lines · Waiman Long
Quoted 9 lines · Juri Lelli
Hi,
Quoted 6 lines · Waiman Long
The
nr_deadline_tasksfield in cpuset structure was introduced by commit6c24849f5515("sched/cpuset: Keep track ofSCHED_DEADLINEtask in cpusets"). Unlikenr_migrate_dl_taskswhich is only modified undercpuset_mutex,nr_deadline_taskscan be updated in various contexts under different locks. As a result, data races may happen that cause incorrect value to be stored innr_deadline_tasksleading to incorrectQuoted 8 lines
Quoted 1 line
Could you please make an example of such data races?
Since update to
cs->nr_deadline_tasksis not protected by a single lock, it is possible that multiple CPUs may try to modify it at the same time. It is possible that nr_deadline_tasks++ and nr_deadline_tasks-- can be done in a single instruction like in x86 and hence atomic. However, operation like "cs->nr_deadline_tasks+=cs->nr_migrate_dl_tasks" is likely a RMW operation and so is subjected to racing. It is mostly theoretical, but probably not impossible.Quoted 8 lines
Sorry, even increment and decrement operators are not atomic.
inc_dl_tasks_cs()is only called fromswitched_to_dl()indeadline.cwhich is protected by therq_lock, but there are multiple rq's.dec_dl_tasks_cs()is called fromswitched_from_dl()indeadline.candcgroup_exit()incgroup.c. The later one is protected bycss_set_lock. The other place wherenr_deadline_taskscan be changed is incpuset_attach()protected bycpuset_mutex.
Quoted 5 lines
So, let's see. :)
switched_to_dl(),switched_from_dl()andcpuset_attach()should all be protected (for DEADLINE tasks) bycpuset_mutex, see [1] for the former two.
Yes, I missed the cpuset_lock() call.
Quoted 4 lines
What leaves me perplexed is indeed
cgroup_exit(), which seems to operate undercss_set_lockas you say. I however wonder why is that not racy already wrt, say,cpuset_attach()which AFAIU uses css information w/o holdingcss_set_lock?
The css_set_lock protects changes made to css_set. Looking at cgroup_migrate_execute(), css_set_lock is taken when the tasks are actually moving from one css_set to another one. cpuset_attach() is called just to update the CPU and node affinity and cpuset_mutex is taken to ensure stability of the CPU and node masks. There is no change to css_set and so css_set_lock isn't needed.
We can argue that there can be racing between cgroup_exit() and the iteration of tasks in cpuset_attach() or cpuset_can_attach(). An rcu_read_lock() is probably needed. I am stilling investigating that.
Cheers,
Longman
Quoted 6 lines
Thanks,
Juri1 - https://elixir.bootlin.com/linux/latest/source/kernel/sched/core.c#L7688
Waiman Long
maintainerQuoted 36 lines · Waiman Long
Quoted 28 lines · Juri Lelli
Quoted 19 lines · Waiman Long
Quoted 10 lines · Waiman Long
Quoted 9 lines · Juri Lelli
Hi,
Quoted 6 lines · Waiman Long
The
nr_deadline_tasksfield in cpuset structure was introduced by commit6c24849f5515("sched/cpuset: Keep track ofSCHED_DEADLINEtask in cpusets"). Unlikenr_migrate_dl_taskswhich is only modified undercpuset_mutex,nr_deadline_taskscan be updated in various contexts under different locks. As a result, data races may happen that cause incorrect value to be stored innr_deadline_tasksleading to incorrectQuoted 8 lines
Quoted 1 line
Could you please make an example of such data races?
Since update to
cs->nr_deadline_tasksis not protected by a single lock, it is possible that multiple CPUs may try to modify it at the same time. It is possible that nr_deadline_tasks++ and nr_deadline_tasks-- can be done in a single instruction like in x86 and hence atomic. However, operation like "cs->nr_deadline_tasks+=cs->nr_migrate_dl_tasks" is likely a RMW operation and so is subjected to racing. It is mostly theoretical, but probably not impossible.Quoted 8 lines
Sorry, even increment and decrement operators are not atomic.
inc_dl_tasks_cs()is only called fromswitched_to_dl()indeadline.cwhich is protected by therq_lock, but there are multiple rq's.dec_dl_tasks_cs()is called fromswitched_from_dl()indeadline.candcgroup_exit()incgroup.c. The later one is protected bycss_set_lock. The other place wherenr_deadline_taskscan be changed is incpuset_attach()protected bycpuset_mutex.Quoted 5 lines
So, let's see. :)
switched_to_dl(),switched_from_dl()andcpuset_attach()should all be protected (for DEADLINE tasks) bycpuset_mutex, see [1] for the former two.Yes, I missed the
cpuset_lock()call.
Quoted 8 lines
Quoted 4 lines
What leaves me perplexed is indeed
cgroup_exit(), which seems to operate undercss_set_lockas you say. I however wonder why is that not racy already wrt, say,cpuset_attach()which AFAIU uses css information w/o holdingcss_set_lock?The
css_set_lockprotects changes made tocss_set. Looking atcgroup_migrate_execute(),css_set_lockis taken when the tasks are actually moving from onecss_setto another one.cpuset_attach()is called just to update the CPU and node affinity andcpuset_mutexis taken to ensure stability of the CPU and node masks. There is no change tocss_setand socss_set_lockisn't needed.We can argue that there can be racing between
cgroup_exit()and the iteration of tasks incpuset_attach()orcpuset_can_attach(). Anrcu_read_lock()is probably needed. I am stilling investigating that.
Cgroup has a rather complex task migration and iteration scheme. According to the following comments in include/linux/cgroup-defs.h:
/*
* Lists running through all tasks using this cgroup group.
* mg_tasks lists tasks which belong to this cset but are in the
* process of being migrated out or in. Protected by
* css_set_lock, but, during migration, once tasks are moved to
* mg_tasks, it can be read safely while holding cgroup_mutex. */
struct list_head tasks;
struct list_head mg_tasks;
struct list_head dying_tasks;
I haven't fully figured out how that protection works yet. Assuming that is the case, task iteration in cpuset_attach() should be fine since cgroup_mutex is indeed held when it is invoked. That protection, however, does not applied to nr_deadline_tasks. It may be too costly to acquire cpuset_mutex before updating nr_deadline_tasks in cgroup_exit(). So changing it to an atomic_t should be the easy way out of the potential racing problem.
I can update the commit log with these new analysis if you have no further objection to this change.
Cheers,
Longman
Quoted 1 line · Waiman Long
On 10/11/23 08:54, Waiman Long wrote:
...
Quoted 18 lines
Quoted 3 lines
We can argue that there can be racing between
cgroup_exit()and the iteration of tasks incpuset_attach()orcpuset_can_attach(). Anrcu_read_lock()is probably needed. I am stilling investigating that.Cgroup has a rather complex task migration and iteration scheme. According to the following comments in
include/linux/cgroup-defs.h:/*
* Lists running through all tasks using this cgroup group.
*mg_taskslists tasks which belong to this cset but are in the
* process of being migrated out or in. Protected by
*css_set_lock, but, during migration, once tasks are moved to
*mg_tasks, it can be read safely while holdingcgroup_mutex. */
structlist_headtasks;
structlist_headmg_tasks;
structlist_headdying_tasks;
Quoted 7 lines
I haven't fully figured out how that protection works yet. Assuming that is the case, task iteration in
cpuset_attach()should be fine sincecgroup_mutexis indeed held when it is invoked. That protection, however, does not applied tonr_deadline_tasks. It may be too costly to acquirecpuset_mutexbefore updatingnr_deadline_tasksincgroup_exit(). So changing it to anatomic_tshould be the easy way out of the potential racing problem.
My biggest perplexity is/was still about dl_rebuild_rd_accounting() and cgroup_exit(); I wonder if the latter, operating outside cpuset_mutex guard, might still be racy wrt the former (even if we change to atomic_t).
However, looking again at it, dl_rebuild_rd_accounting() operates on css(es) via css_task_iter_start(), which grabs css_set_lock. So maybe we are OK already also for this case?
Apologies for being pedantic, but we fought already several times with races around these bits and now I'm probably over-suspicious. :)
Thanks,
Juri