public inbox for cgroups@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cgroup/cpuset: Fix a race between cpuset_attach() and cpu hotplug
@ 2022-01-21 10:12 Zhang Qiao
       [not found] ` <20220121101210.84926-1-zhangqiao22-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
  2022-02-07  6:17 ` Zhang Qiao
  0 siblings, 2 replies; 7+ messages in thread
From: Zhang Qiao @ 2022-01-21 10:12 UTC (permalink / raw)
  To: linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	cgroups-u79uwXL29TY76Z2rM5mHXA
  Cc: tj-DgEjT+Ai2ygdnm+yROfE0A, lizefan.x-EC8Uxl6Npydl57MIdRCFDg,
	hannes-druUgvl0LCNAfugRpC6u6w, matthltc-r/Jw6+rmf7HQT0dZR+AlfA,
	bblum-hpIqsD4AKlfQT0dZR+AlfA, menage-hpIqsD4AKlfQT0dZR+AlfA,
	akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b,
	longman-H+wXaHxf7aLQT0dZR+AlfA, mkoutny-IBi9RG/b67k,
	zhangqiao22-hv44wF8Li93QT0dZR+AlfA,
	zhaogongyi-hv44wF8Li93QT0dZR+AlfA

As previously discussed(https://lkml.org/lkml/2022/1/20/51),
cpuset_attach() is affected with similar cpu hotplug race,
as follow scenario:

     cpuset_attach()				cpu hotplug
    ---------------------------            ----------------------
    down_write(cpuset_rwsem)
    guarantee_online_cpus() // (load cpus_attach)
					sched_cpu_deactivate
					  set_cpu_active()
					  // will change cpu_active_mask
    set_cpus_allowed_ptr(cpus_attach)
      __set_cpus_allowed_ptr_locked()
       // (if the intersection of cpus_attach and
         cpu_active_mask is empty, will return -EINVAL)
    up_write(cpuset_rwsem)

To avoid races such as described above, protect cpuset_attach() call
with cpu_hotplug_lock.

Fixes: be367d099270 ("cgroups: let ss->can_attach and ss->attach do whole threadgroups at a time")
Reported-by: Zhao Gongyi <zhaogongyi-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
Signed-off-by: Zhang Qiao <zhangqiao22-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
---
 kernel/cgroup/cpuset.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index dc653ab26e50..0af5725cc1df 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -2252,6 +2252,7 @@ static void cpuset_attach(struct cgroup_taskset *tset)
 	cgroup_taskset_first(tset, &css);
 	cs = css_cs(css);

+	cpus_read_lock();
 	percpu_down_write(&cpuset_rwsem);

 	guarantee_online_mems(cs, &cpuset_attach_nodemask_to);
@@ -2305,6 +2306,7 @@ static void cpuset_attach(struct cgroup_taskset *tset)
 		wake_up(&cpuset_attach_wq);

 	percpu_up_write(&cpuset_rwsem);
+	cpus_read_unlock();
 }

 /* The various types of files and directories in a cpuset file system */

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-02-14 19:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-01-21 10:12 [PATCH] cgroup/cpuset: Fix a race between cpuset_attach() and cpu hotplug Zhang Qiao
     [not found] ` <20220121101210.84926-1-zhangqiao22-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
2022-01-21 12:43   ` Waiman Long
     [not found]     ` <a166a39f-aa9f-95af-3f3f-f4e17e7c3305-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2022-01-22  7:54       ` Zhang Qiao
2022-01-21 15:57   ` Michal Koutný
     [not found]     ` <20220121155705.GA2394-9OudH3eul5jcvrawFnH+a6VXKuFTiq87@public.gmane.org>
2022-01-22  7:37       ` Zhang Qiao
2022-02-14 19:50   ` Tejun Heo
2022-02-07  6:17 ` Zhang Qiao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox