From: Zefan Li <lizefan@huawei.com>
Date: Tue, 9 Aug 2016 03:25:01 +0000 (+0800)
Subject: cpuset: make sure new tasks conform to the current config of the cpuset
X-Git-Tag: firefly_0821_release~176^2~4^2~28^2~73
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=06ec7a1d7646;p=firefly-linux-kernel-4.4.55.git

cpuset: make sure new tasks conform to the current config of the cpuset

commit 06f4e94898918bcad00cdd4d349313a439d6911e upstream.

A new task inherits cpus_allowed and mems_allowed masks from its parent,
but if someone changes cpuset's config by writing to cpuset.cpus/cpuset.mems
before this new task is inserted into the cgroup's task list, the new task
won't be updated accordingly.

Signed-off-by: Zefan Li <lizefan@huawei.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---

diff --git a/kernel/cpuset.c b/kernel/cpuset.c
index 11eaf14b52c2..e120bd983ad0 100644
--- a/kernel/cpuset.c
+++ b/kernel/cpuset.c
@@ -2074,6 +2074,20 @@ static void cpuset_bind(struct cgroup_subsys_state *root_css)
 	mutex_unlock(&cpuset_mutex);
 }
 
+/*
+ * Make sure the new task conform to the current state of its parent,
+ * which could have been changed by cpuset just after it inherits the
+ * state from the parent and before it sits on the cgroup's task list.
+ */
+void cpuset_fork(struct task_struct *task)
+{
+	if (task_css_is_root(task, cpuset_cgrp_id))
+		return;
+
+	set_cpus_allowed_ptr(task, &current->cpus_allowed);
+	task->mems_allowed = current->mems_allowed;
+}
+
 struct cgroup_subsys cpuset_cgrp_subsys = {
 	.css_alloc	= cpuset_css_alloc,
 	.css_online	= cpuset_css_online,
@@ -2084,6 +2098,7 @@ struct cgroup_subsys cpuset_cgrp_subsys = {
 	.attach		= cpuset_attach,
 	.post_attach	= cpuset_post_attach,
 	.bind		= cpuset_bind,
+	.fork		= cpuset_fork,
 	.legacy_cftypes	= files,
 	.early_init	= 1,
 };