/**
  * update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
  * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
+ * @new_cpus: the temp variable for the new effective_cpus mask
  *
  * Iterate through each task of @cs updating its cpus_allowed to the
  * effective cpuset's.  As this function is called with cpuset_rwsem held,
  * cpuset membership stays stable.
  */
-static void update_tasks_cpumask(struct cpuset *cs)
+static void update_tasks_cpumask(struct cpuset *cs, struct cpumask *new_cpus)
 {
        struct css_task_iter it;
        struct task_struct *task;
                if (top_cs && (task->flags & PF_KTHREAD) &&
                    kthread_is_per_cpu(task))
                        continue;
-               set_cpus_allowed_ptr(task, cs->effective_cpus);
+
+               cpumask_and(new_cpus, cs->effective_cpus,
+                           task_cpu_possible_mask(task));
+               set_cpus_allowed_ptr(task, new_cpus);
        }
        css_task_iter_end(&it);
 }
        spin_unlock_irq(&callback_lock);
 
        if (adding || deleting)
-               update_tasks_cpumask(parent);
+               update_tasks_cpumask(parent, tmp->new_cpus);
 
        /*
         * Set or clear CS_SCHED_LOAD_BALANCE when partcmd_update, if necessary.
                WARN_ON(!is_in_v2_mode() &&
                        !cpumask_equal(cp->cpus_allowed, cp->effective_cpus));
 
-               update_tasks_cpumask(cp);
+               update_tasks_cpumask(cp, tmp->new_cpus);
 
                /*
                 * On legacy hierarchy, if the effective cpumask of any non-
                }
        }
 
-       update_tasks_cpumask(parent);
+       update_tasks_cpumask(parent, tmpmask.new_cpus);
 
        if (parent->child_ecpus_count)
                update_sibling_cpumasks(parent, cs, &tmpmask);
         * as the tasks will be migrated to an ancestor.
         */
        if (cpus_updated && !cpumask_empty(cs->cpus_allowed))
-               update_tasks_cpumask(cs);
+               update_tasks_cpumask(cs, new_cpus);
        if (mems_updated && !nodes_empty(cs->mems_allowed))
                update_tasks_nodemask(cs);
 
        spin_unlock_irq(&callback_lock);
 
        if (cpus_updated)
-               update_tasks_cpumask(cs);
+               update_tasks_cpumask(cs, new_cpus);
        if (mems_updated)
                update_tasks_nodemask(cs);
 }