* [PATCH] cgroup/cpuset: Fix a race between cpuset_attach() and cpu hotplug
@ 2022-01-21 10:12 Zhang Qiao
2022-01-21 12:43 ` Waiman Long
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: Zhang Qiao @ 2022-01-21 10:12 UTC (permalink / raw)
To: linux-kernel, cgroups
Cc: tj, lizefan.x, hannes, matthltc, bblum, menage, akpm, longman,
mkoutny, zhangqiao22, zhaogongyi
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@huawei.com>
Signed-off-by: Zhang Qiao <zhangqiao22@huawei.com>
---
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 */
--
2.18.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] cgroup/cpuset: Fix a race between cpuset_attach() and cpu hotplug
2022-01-21 10:12 [PATCH] cgroup/cpuset: Fix a race between cpuset_attach() and cpu hotplug Zhang Qiao
@ 2022-01-21 12:43 ` Waiman Long
2022-01-22 7:54 ` Zhang Qiao
2022-01-21 15:57 ` Michal Koutný
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ messages in thread
From: Waiman Long @ 2022-01-21 12:43 UTC (permalink / raw)
To: Zhang Qiao, linux-kernel, cgroups
Cc: tj, lizefan.x, hannes, matthltc, bblum, menage, akpm, mkoutny,
zhaogongyi
On 1/21/22 05:12, Zhang Qiao wrote:
> 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@huawei.com>
> Signed-off-by: Zhang Qiao <zhangqiao22@huawei.com>
> ---
> 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 */
> --
> 2.18.0
The locking sequence looks right.
Acked-by: Waiman Long <longman@redhat.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] cgroup/cpuset: Fix a race between cpuset_attach() and cpu hotplug
2022-01-21 10:12 [PATCH] cgroup/cpuset: Fix a race between cpuset_attach() and cpu hotplug Zhang Qiao
2022-01-21 12:43 ` Waiman Long
@ 2022-01-21 15:57 ` Michal Koutný
2022-01-22 7:37 ` Zhang Qiao
2022-02-07 6:17 ` Zhang Qiao
2022-02-14 19:50 ` Tejun Heo
3 siblings, 1 reply; 7+ messages in thread
From: Michal Koutný @ 2022-01-21 15:57 UTC (permalink / raw)
To: Zhang Qiao
Cc: linux-kernel, cgroups, tj, lizefan.x, hannes, matthltc, bblum,
menage, akpm, longman, zhaogongyi
On Fri, Jan 21, 2022 at 06:12:10PM +0800, Zhang Qiao <zhangqiao22@huawei.com> wrote:
> Fixes: be367d099270 ("cgroups: let ss->can_attach and ss->attach do whole threadgroups at a time")
What a deep stratigraphy (not sure if it's this one but anything else
would likely come from eras ago too).
> Reported-by: Zhao Gongyi <zhaogongyi@huawei.com>
> Signed-off-by: Zhang Qiao <zhangqiao22@huawei.com>
> ---
> kernel/cgroup/cpuset.c | 2 ++
> 1 file changed, 2 insertions(+)
Feel free to include
Reviewed-by: Michal Koutný <mkoutny@suse.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] cgroup/cpuset: Fix a race between cpuset_attach() and cpu hotplug
2022-01-21 15:57 ` Michal Koutný
@ 2022-01-22 7:37 ` Zhang Qiao
0 siblings, 0 replies; 7+ messages in thread
From: Zhang Qiao @ 2022-01-22 7:37 UTC (permalink / raw)
To: Michal Koutný
Cc: linux-kernel, cgroups, tj, lizefan.x, hannes, matthltc, bblum,
menage, akpm, longman, zhaogongyi
在 2022/1/21 23:57, Michal Koutný 写道:
> On Fri, Jan 21, 2022 at 06:12:10PM +0800, Zhang Qiao <zhangqiao22@huawei.com> wrote:
>> Fixes: be367d099270 ("cgroups: let ss->can_attach and ss->attach do whole threadgroups at a time")
>
> What a deep stratigraphy (not sure if it's this one but anything else
> would likely come from eras ago too).
Yes, I checked again and this bug may have existed since the tag Linux-2.6.12-rc2.
>> Reported-by: Zhao Gongyi <zhaogongyi@huawei.com>
>> Signed-off-by: Zhang Qiao <zhangqiao22@huawei.com>
>> ---
>> kernel/cgroup/cpuset.c | 2 ++
>> 1 file changed, 2 insertions(+)
>
> Feel free to include
> Reviewed-by: Michal Koutný <mkoutny@suse.com>
>
Thanks for your review!
Regards,
Qiao
.
> .
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] cgroup/cpuset: Fix a race between cpuset_attach() and cpu hotplug
2022-01-21 12:43 ` Waiman Long
@ 2022-01-22 7:54 ` Zhang Qiao
0 siblings, 0 replies; 7+ messages in thread
From: Zhang Qiao @ 2022-01-22 7:54 UTC (permalink / raw)
To: Waiman Long, linux-kernel, cgroups
Cc: tj, lizefan.x, hannes, matthltc, bblum, menage, akpm, mkoutny,
zhaogongyi
在 2022/1/21 20:43, Waiman Long 写道:
> On 1/21/22 05:12, Zhang Qiao wrote:
>> 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@huawei.com>
>> Signed-off-by: Zhang Qiao <zhangqiao22@huawei.com>
>> ---
>> 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 */
>> --
>> 2.18.0
>
> The locking sequence looks right.
>
> Acked-by: Waiman Long <longman@redhat.com>
>
Thanks for your review!
Regards,
Qiao
.
> .
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] cgroup/cpuset: Fix a race between cpuset_attach() and cpu hotplug
2022-01-21 10:12 [PATCH] cgroup/cpuset: Fix a race between cpuset_attach() and cpu hotplug Zhang Qiao
2022-01-21 12:43 ` Waiman Long
2022-01-21 15:57 ` Michal Koutný
@ 2022-02-07 6:17 ` Zhang Qiao
2022-02-14 19:50 ` Tejun Heo
3 siblings, 0 replies; 7+ messages in thread
From: Zhang Qiao @ 2022-02-07 6:17 UTC (permalink / raw)
To: linux-kernel, cgroups
Cc: tj, lizefan.x, hannes, matthltc, bblum, menage, akpm, longman,
mkoutny, zhaogongyi
Gentle ping on this patch.
在 2022/1/21 18:12, Zhang Qiao 写道:
> 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@huawei.com>
> Signed-off-by: Zhang Qiao <zhangqiao22@huawei.com>
> ---
> 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 */
> --
> 2.18.0
>
> .
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] cgroup/cpuset: Fix a race between cpuset_attach() and cpu hotplug
2022-01-21 10:12 [PATCH] cgroup/cpuset: Fix a race between cpuset_attach() and cpu hotplug Zhang Qiao
` (2 preceding siblings ...)
2022-02-07 6:17 ` Zhang Qiao
@ 2022-02-14 19:50 ` Tejun Heo
3 siblings, 0 replies; 7+ messages in thread
From: Tejun Heo @ 2022-02-14 19:50 UTC (permalink / raw)
To: Zhang Qiao
Cc: linux-kernel, cgroups, lizefan.x, hannes, matthltc, bblum,
menage, akpm, longman, mkoutny, zhaogongyi
On Fri, Jan 21, 2022 at 06:12:10PM +0800, Zhang Qiao wrote:
> 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@huawei.com>
> Signed-off-by: Zhang Qiao <zhangqiao22@huawei.com>
Applied to cgroup/for-5.17-fixes w/ stable cc'd.
Thanks and sorry about the delay.
--
tejun
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2022-02-14 20:58 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
2022-01-21 12:43 ` Waiman Long
2022-01-22 7:54 ` Zhang Qiao
2022-01-21 15:57 ` Michal Koutný
2022-01-22 7:37 ` Zhang Qiao
2022-02-07 6:17 ` Zhang Qiao
2022-02-14 19:50 ` Tejun Heo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).