From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=158.69.130.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796309987; b=XnbDy2VN0hClLKLJIp80j/l9e37nSVb7DxUn5eqQXC+bicV0uKfZQOM+uegfpfPc1d42 4Z3+69TefMJMPhgK37AlbBl/J3bv29EtODxS5ij2LfpVoZhkJV8XR0LtNyBuI1Gv7fso2 cg2uUU0RedC+HXe7jIRFeiyz7Q3ZKcUUZc3TgyiWY+BG3N6AHA3d4RvLOKRHsVq6QrYFq MHpINnXtn2cwEkPoxrB1uBR17NFQFi/NoHUAFR3+Hhd5GtY3i0mpmUBeSYE6KRSa6nNgi zXx1N+PldOGMM49yjkQGOADiHlbeFWZITy75y4B68CTyEJ9pioZLqc2JYLMxC+sVsSg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796309987; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=fY56OFzqkc31nMPmZNMe8j0OE8B9nRb08Ms0o0EtbO4=; b=bKeyLarSBsuJVJ76KaruIQbN8HQFNUApDPS8h3yE1j0xbjCufwqsb/0gGH1Z+KgIDHeF WM1cvxsE0klyHYrWObE4v1YDwly3LAk+5b5ufkW2mEI+H70VAT8LoyJPMFX4Uhm7FwQeS 4+kLFRSjM31vrZg1XXngY6iMBVmhnqxxSt95N3E4uyVpDLfeS4/+WETiRYRV61Gn6CruW 0LwsLHLKjLOodIRDqGeVA8RhKi0cOD+H4F/4416I1AKjSkD6bizvSUO9945IZDotqBWi4 IJPb8hCJxs0xlU4rc0ieXKXGNvj1gJrdhVL8L3emV6iYM3ofscbe6eYxg2qnrMQdNfA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=efficios.com; spf=pass smtp.mailfrom=efficios.com; dkim=pass header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b=pgjU5gjS; arc=none smtp.remote-ip=158.69.130.18 Received: from smtpout.efficios.com (smtpout.efficios.com [158.69.130.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE752606B25C; Thu, 3 Dec 2026 14:59:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DE752606B25C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=efficios.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=efficios.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b=pgjU5gjS DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=smtpout1; t=1796309986; bh=fY56OFzqkc31nMPmZNMe8j0OE8B9nRb08Ms0o0EtbO4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pgjU5gjSWekEcCRsduU2y4Y6HGwVsJswVHoesrnw3rY5FMWUEB9AYORtgiLgBcy7M XNJVfPzdUbd2KsoKka625ntDR9jzUtWgDY0MoTAhyokIQCpQt97e6+ZBA+iLXIBlrk yBSD46HQ5NAXz4pCODjfu/jFXRO72DUmDrwmrLx38pZCweJ+sXcmW97XigmbITUo+O KXjRCYNJYzLotZYPFJw98eUSOLMj4nJfSYcqUTMpbmjF75o84jQtaONnSu66xxtW+T JoRwc8g6l9EdULcrMAQHZCzm8nhtnbNMw7J6wfMnx9VZnj3qg15qr3Kc8Rxfco3/Vh E3iwzk14OPdbg== Received: from thinkos.internal.efficios.com (unknown [IPv6:2605:8d80:7180:6739:ac39:5d4:4093:61c6]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4jgHpm46Q6zZ3L; Thu, 03 Dec 2026 09:59:44 -0500 (EST) From: Mathieu Desnoyers To: juri.lelli@redhat.com, peterz@infradead.org, mingo@redhat.com, vincent.guittot@linaro.org Cc: Mathieu Desnoyers , tglx@kernel.org, anna-maria@linutronix.de, frederic@kernel.org, will@kernel.org, boqun@kernel.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, kprateek.nayak@amd.com, luca.abeni@santannapisa.it, tommaso.cucinotta@gmail.com, mathieu.poirier@linaro.org, longman@redhat.com, ridong.chen@linux.dev, tj@kernel.org, hannes@cmpxchg.org, mkoutny@suse.com, shuah@kernel.org, bigeasy@linutronix.de, clrkwllms@kernel.org, parri.andrea@gmail.com, m.szyprowski@samsung.com, kayracizmeci@gmail.com, weihengqiuu@gmail.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-rt-devel@lists.linux.dev, oleg@redhat.com, brauner@kernel.org, stable@vger.kernel.org Subject: [PATCH v14 25/39] cgroup/cpuset: Refuse to take a deadline task out of its root domain Date: Thu, 3 Dec 2026 09:58:39 -0500 Message-ID: <0df35bd7655685beb9ccdc5cfcca0ad4e50a5da9.1796296313.git.mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit A cpuset attach asks the destination root domain for room before it moves a deadline task there. A change of the CPUs of a cpuset gets a weaker check. validate_change() only checks that a partition root keeps enough capacity for its deadline bandwidth. On cgroup v1, it checks a cpu_exclusive cpuset. That check comes from commit f82f80426f7a ("sched/deadline: Ensure that updates to exclusive cpusets don't break AC"). Other cpusets get no check, but one of them can hold CPUs of two root domains. With isolcpus="domain", the isolated CPUs are in the default root domain, and the others in another one. On cgroup v1, sched_load_balance off has the same effect. Four changes can take from a cpuset every CPU of the root domain that counts one of its deadline tasks: - a write to cpuset.cpus, of that cpuset or of an ancestor; - a partition root that takes those CPUs, when it is created or when it grows; - a partition root that gives CPUs back to its parent. While the partition root holds them, a cpuset whose cpuset.cpus lists only those CPUs gets all the CPUs of its parent instead. Once they are given back, it gets only those CPUs again, and so do its tasks. So does a task whose user affinity holds only those CPUs. With isolcpus="domain", they can be in another root domain than the task. An isolated partition root can also give housekeeping CPUs back to a parent that is not isolated. The rebuild of the sched domains takes these CPUs out of the default root domain. - a write to cpuset.cpus or cpuset.cpus.exclusive that makes a partition root invalid. The write can be to the partition root itself, or to an ancestor that takes away all its CPUs. The partition root then gives all its CPUs back, and its tasks get CPUs of its parent. A deadline task counted in that root domain then runs in the other one, which did not admit it. Earlier in this series, "sched/deadline: Move a task's bandwidth to the root domain it runs in" makes its bandwidth follow it there. That root domain can then exceed its limit, and its deadline tasks lose the guarantee of admission control. Only a move to another cpuset should take a task to another root domain, since the move asks for room. Fix this by refusing the four changes when they leave a deadline task no CPU of its root domain: - A write to cpuset.cpus fails with EBUSY, and changes nothing. The cpuset.cpus file of cgroup v1 gets the same check. - The write that creates the partition root, or makes it grow, still succeeds. cpuset makes the partition root invalid. cpuset.cpus.partition then reads, for example, "root invalid (Deadline task would leave its root domain)". If the partition root holds a deadline task itself, a write that makes it grow fails with EBUSY instead, as in the last item. - The write to cpuset.cpus or cpuset.cpus.exclusive of the partition root that gives CPUs back fails with EBUSY. Making that partition invalid would not help: it would give these CPUs back all the same. A partition root that also takes CPUs in the same write is handled as one that grows. The exception is an isolated partition root that gives housekeeping CPUs back: that write is still refused. - The write to cpuset.cpus or cpuset.cpus.exclusive that would make a partition root invalid fails with EBUSY. The administrator asked for new CPUs, not for the end of the partition. To find such a write, the check repeats the tests that make a partition root invalid at that write. For a write to an ancestor, it checks each partition root below that would have no CPU left. Like __set_cpus_allowed_ptr(), the check limits the new mask to the user affinity of the task, if they intersect. So a mask that grows is checked too. A task whose user affinity holds only the new CPUs gets only those. dl_task_keeps_root_domain() does the check for one task. It accepts any mask for a dead task, and any mask without admission control. An administrator who needs the change can turn admission control off, by writing -1 to /proc/sys/kernel/sched_rt_runtime_us. cpuset skips the walk of the tasks when no task holds a deadline bandwidth. The check runs before the rebuild of the sched domains, which ends the change. The rebuild then moves CPUs between root domains. The check counts them where they will be: - The CPUs that a root or an isolated partition root has after the change count in its root domain. Otherwise a partition root that just grew could not give its old CPUs to a child partition. - The CPUs that a partition root, not isolated, gives back to its parent count in the root domain of the parent. - The housekeeping CPUs that an isolated partition root gives back do not count in the default root domain. These changes are not checked: - disabling a partition. Its tasks then count in the root domain of the parent, with no check for room, as on v7.4-rc2. A refusal would keep an administrator from ever removing a partition that holds a deadline task. - a write that makes a cpuset a partition root. The check finds that its deadline tasks keep a CPU of their root domain. The rebuild then counts them in the new root domain, with no check for room, as on v7.4-rc2. That root domain can then count more than its capacity. - a partition that a CPU offline makes invalid, and a CPU that comes online. cpuset cannot refuse these. Earlier in this series, "sched/deadline: Keep the last CPU a task has in its root domain" checks such an offline. One more change is not checked. Take a partition root P with two invalid child partitions, C1 and C2. A write to the cpuset.cpus of P can make both valid. When C1 takes its CPUs, update_sibling_cpumasks() gives the CPUs left in P to C2 and its tasks, with no check. When the walk reaches C2, its masks no longer change, so the check has nothing to test. The tasks of C2 would then run in a root domain that did not admit them. This was found by reading. A fix needs the check in update_sibling_cpumasks() too. The cases below are added to deadline_bw_test by "selftests/sched: Test the root domain of a task in deadline_bw_test", later in this series. mask_out_refused, partition_takes_refused, partition_gives_back_only_refused and partition_invalidate_refused test the four refusals. Without this patch, each write is accepted, and partition_takes_refused finds its partition root valid. These cases test the other paths: - partition_takes_fallback_refused: the cpuset of the task holds no CPU of its parent, and gets all the CPUs of its parent. - mask_grows_refused: a mask that grows. - partition_swap_refused: an isolated partition root that takes a housekeeping CPU and gives an isolated one back, in one write. - partition_gives_back_refused: an isolated partition root that takes isolated CPUs and gives a housekeeping CPU back, in one write. - partition_swap_gives_back_refused: an isolated partition root that takes a housekeeping CPU and gives another back, in one write. - partition_swap_accepted: a partition root that is not isolated takes a CPU and gives another back, in one write. The tasks of its parent keep a CPU of their root domain. - partition_refused_no_warn: an invalid partition root refused again. It holds no CPU of its parent, so it gives none back, and the kernel does not warn. - partition_grows_child_valid: a child partition made valid by CPUs that its parent just got. - partition_shrink_moves_refused: a partition root that is not isolated gives a housekeeping CPU back. A task in the default root domain would get only that CPU. - partition_shrink_isolated_moves_refused: an isolated partition gives an isolated CPU back. A task in the housekeeping root domain would get only that CPU. - partition_child_invalidate_refused: a write to a partition root takes away all the CPUs of an isolated child partition. A backport needs these earlier patches of this series: - "sched/deadline: Keep a task in the root domain that counts its bandwidth", which adds dl_task_bw_span(); - "sched/deadline: Keep the last CPU a task has in its root domain", which adds effective_cpus_below(); - "sched/deadline: Rebuild the root domains' accounting from the task list", which adds dl_nr_bw_tasks. Fixes: f82f80426f7a ("sched/deadline: Ensure that updates to exclusive cpusets don't break AC") Signed-off-by: Mathieu Desnoyers Cc: stable@vger.kernel.org --- include/linux/sched/deadline.h | 5 + kernel/cgroup/cpuset-internal.h | 1 + kernel/cgroup/cpuset.c | 464 +++++++++++++++++++++++++++++++- kernel/sched/deadline.c | 64 +++++ 4 files changed, 531 insertions(+), 3 deletions(-) diff --git a/include/linux/sched/deadline.h b/include/linux/sched/deadline.h index a555359d4164..deddfca67ec2 100644 --- a/include/linux/sched/deadline.h +++ b/include/linux/sched/deadline.h @@ -63,6 +63,11 @@ extern int dl_task_bw_reserve_cpu(struct task_struct *p, const struct cpumask *new_mask); extern int dl_task_bw_reserved_cpu(struct task_struct *p); extern void dl_task_bw_unreserve(struct task_struct *p); +extern bool dl_task_keeps_root_domain(struct task_struct *p, + const struct cpumask *new_mask, + const struct cpumask *pending, + const struct cpumask *leaving); +extern bool dl_tasks_hold_bw(void); extern bool dl_task_clear_overrun(struct task_struct *p); diff --git a/kernel/cgroup/cpuset-internal.h b/kernel/cgroup/cpuset-internal.h index a83443dfb788..8fe0225704eb 100644 --- a/kernel/cgroup/cpuset-internal.h +++ b/kernel/cgroup/cpuset-internal.h @@ -36,6 +36,7 @@ enum prs_errcode { PERR_ACCESS, PERR_REMOTE, PERR_ISOPARENT, + PERR_DLTASK, }; /* bits in struct cpuset flags field */ diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c index 9461277ed0fa..06cc442e0185 100644 --- a/kernel/cgroup/cpuset.c +++ b/kernel/cgroup/cpuset.c @@ -61,6 +61,7 @@ static const char * const perr_strings[] = { [PERR_ACCESS] = "Enable partition not permitted", [PERR_REMOTE] = "Have remote partition underneath", [PERR_ISOPARENT] = "Root partition is not allowed with isolated parent", + [PERR_DLTASK] = "Deadline task would leave its root domain", }; /* @@ -1126,6 +1127,43 @@ static void update_partition_sd_lb(struct cpuset *cs, int old_prs) cpuset_force_rebuild(); } +/* + * Return false if the CPUs of @new_cpus make a deadline task of @cp leave + * the root domain that counts its bandwidth. @pending, if not NULL, holds + * the CPUs that the rebuild at the end of the change gives to that root + * domain. @leaving, if not NULL, holds CPUs that leave their root domain + * at the rebuild: CPUs of the default root domain, or all the CPUs of a + * root partition that becomes invalid. Called under rcu_read_lock(), + * which it drops and takes again. + */ +static bool dl_tasks_keep_cpus(struct cpuset *cp, const struct cpumask *new_cpus, + const struct cpumask *pending, + const struct cpumask *leaving) +{ + struct css_task_iter it; + struct task_struct *task; + bool keep = true; + + if (!dl_tasks_hold_bw() || !css_tryget_online(&cp->css)) + return true; + rcu_read_unlock(); + + css_task_iter_start(&cp->css, 0, &it); + while (keep && (task = css_task_iter_next(&it))) { + /* The top cpuset leaves the mask of these tasks alone. */ + if (cp == &top_cpuset && + (task->flags & (PF_KTHREAD | PF_NO_SETAFFINITY))) + continue; + keep = dl_task_keeps_root_domain(task, new_cpus, pending, + leaving); + } + css_task_iter_end(&it); + + rcu_read_lock(); + css_put(&cp->css); + return keep; +} + /* * Set @new_cpus to the effective CPUs that @cp gets. On entry, @new_cpus * holds those that @from gets. @from is @cp or one of its ancestors. Work @@ -1148,6 +1186,119 @@ static void effective_cpus_below(struct cpuset *cp, struct cpuset *from, } } +/* + * Scratch masks for dl_tasks_keep_without(). Its callers still need + * each mask of their struct tmpmasks after the call. Protected by + * cpuset_mutex. + */ +static cpumask_var_t dl_keep_cpus, dl_pending_cpus, dl_leaving_cpus; + +/* + * Return false if a partition that takes the CPUs of @xcpus makes a + * deadline task leave the root domain that counts its bandwidth. A cpuset + * can hold CPUs of two root domains: with isolcpus="domain", or on cgroup + * v1 with sched_load_balance off. Such a task would run in a root domain + * that did not admit it. + * + * @root is the partition root that gives the CPUs. @part is the cpuset + * that takes them. In the same change, @part can give the CPUs of @gives + * back to @root. @gives is NULL if it gives none back. + * + * Work out the new CPUs of each cpuset of @root from the top down, as + * update_cpumasks_hier() does, down to the next partition roots. If + * @enable, @part becomes a valid partition root. Then do the same for its own + * cpusets, starting from the active CPUs of @xcpus. + */ +static bool dl_tasks_keep_without(struct cpuset *root, struct cpuset *part, + const struct cpumask *xcpus, + const struct cpumask *gives, bool enable) +{ + struct cpumask *new_cpus = dl_keep_cpus, *pending = NULL, *leaving = NULL; + struct cgroup_subsys_state *pos_css; + struct cpuset *cp; + bool keep = true; + + lockdep_assert_cpuset_lock_held(); + /* + * The CPUs of @root can have changed before the rebuild of the + * sched domains, which ends the change. Then the root domain of a + * task of @root does not span them yet. The rebuild gives the CPUs + * of a root partition to one root domain. It gives those of an + * isolated partition, and of its valid children, to the default + * root domain. + */ + if (root->partition_root_state == PRS_ROOT || + root->partition_root_state == PRS_ISOLATED) { + pending = dl_pending_cpus; + cpumask_copy(pending, root->effective_cpus); + /* + * The CPUs that a partition root which is not isolated gives + * back join that root domain too. Those of an isolated one + * are in the default root domain until the rebuild: see + * @leaving below. + */ + if (gives && part->partition_root_state != PRS_ISOLATED) { + cpumask_or(pending, pending, gives); + cpumask_and(pending, pending, cpu_active_mask); + } + if (root == &top_cpuset) + cpumask_and(pending, pending, + housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT)); + } + /* + * An isolated partition can give CPUs back to a parent that is not + * isolated. The rebuild then takes them out of the default root + * domain, except those that isolcpus="domain" isolates. A task + * counted in the default root domain must keep a CPU of its new + * mask that stays there. + */ + if (gives && part->partition_root_state == PRS_ISOLATED && + root->partition_root_state != PRS_ISOLATED) { + leaving = dl_leaving_cpus; + cpumask_and(leaving, gives, + housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT)); + } + rcu_read_lock(); + cpuset_for_each_descendant_pre(cp, pos_css, root) { + if (cp == part || (cp != root && is_partition_valid(cp))) { + pos_css = css_rightmost_descendant(pos_css); + continue; + } + /* The CPUs that @root has after the change. */ + cpumask_copy(new_cpus, root->effective_cpus); + if (gives) { + cpumask_or(new_cpus, new_cpus, gives); + cpumask_and(new_cpus, new_cpus, cpu_active_mask); + } + cpumask_andnot(new_cpus, new_cpus, xcpus); + effective_cpus_below(cp, root, new_cpus); + if (cpumask_equal(cp->effective_cpus, new_cpus)) + continue; + keep = dl_tasks_keep_cpus(cp, new_cpus, pending, leaving); + if (!keep) + break; + } + if (keep && enable) { + cpuset_for_each_descendant_pre(cp, pos_css, part) { + if (cp != part && is_partition_valid(cp)) { + pos_css = css_rightmost_descendant(pos_css); + continue; + } + cpumask_and(new_cpus, xcpus, cpu_active_mask); + effective_cpus_below(cp, part, new_cpus); + if (cpumask_equal(cp->effective_cpus, new_cpus)) + continue; + keep = dl_tasks_keep_cpus(cp, new_cpus, + root->partition_root_state == PRS_ISOLATED ? + pending : NULL, leaving); + if (!keep) + break; + } + } + rcu_read_unlock(); + return keep; +} + /* * tasks_nocpu_error - Return true if tasks will have no effective_cpus */ @@ -1698,6 +1849,8 @@ static int remote_partition_enable(struct cpuset *cs, int new_prs, return PERR_NOCPUS; if (prstate_housekeeping_conflict(new_prs, PRS_ROOT, tmp->new_cpus, NULL)) return PERR_HKEEPING; + if (!dl_tasks_keep_without(&top_cpuset, cs, tmp->new_cpus, NULL, true)) + return PERR_DLTASK; spin_lock_irq(&callback_lock); partition_xcpus_add(new_prs, NULL, tmp->new_cpus); @@ -1801,6 +1954,10 @@ static void remote_cpus_update(struct cpuset *cs, struct cpumask *xcpus, else if (prstate_housekeeping_conflict(prs, PRS_ROOT, excpus, tmp->delmask)) WRITE_ONCE(cs->prs_err, PERR_HKEEPING); + else if (!dl_tasks_keep_without(&top_cpuset, cs, tmp->addmask, + deleting ? tmp->delmask : NULL, + false)) + WRITE_ONCE(cs->prs_err, PERR_DLTASK); if (cs->prs_err) goto invalidate; } @@ -1948,6 +2105,9 @@ static int update_parent_effective_cpumask(struct cpuset *cs, int cmd, if (nocpu) return PERR_NOCPUS; + if (!dl_tasks_keep_without(parent, cs, tmp->delmask, NULL, true)) + return PERR_DLTASK; + /* * This function will only be called when all the preliminary * checks have passed. At this point, the following condition @@ -2035,6 +2195,21 @@ static int update_parent_effective_cpumask(struct cpuset *cs, int cmd, deleting = false; adding = cpumask_and(tmp->addmask, cs->effective_xcpus, parent->effective_xcpus); + } else if (deleting && + !dl_tasks_keep_without(parent, cs, tmp->delmask, + adding ? tmp->addmask : NULL, + is_partition_invalid(cs))) { + /* + * Make the partition invalid, as for PERR_NOCPUS above. + * Only a valid partition holds CPUs of its parent to + * give back. + */ + part_error = PERR_DLTASK; + deleting = false; + if (is_partition_valid(cs)) + adding = cpumask_and(tmp->addmask, + cs->effective_xcpus, + parent->effective_xcpus); } } else { /* @@ -2106,10 +2281,13 @@ static int update_parent_effective_cpumask(struct cpuset *cs, int cmd, } rcu_read_unlock(); } - if (exclusive) - deleting = !cpumask_empty(tmp->delmask); - else + if (!exclusive) part_error = PERR_NOTEXCL; + else if (!dl_tasks_keep_without(parent, cs, tmp->delmask, + NULL, true)) + part_error = PERR_DLTASK; + else + deleting = !cpumask_empty(tmp->delmask); } /* * When both cs and parent remain valid, parent's @@ -2518,6 +2696,268 @@ static void partition_cpus_change(struct cpuset *cs, struct cpuset *trialcs, } } +/* + * Set @new_cpus to the effective CPUs that @cp gets if @cs takes the CPUs + * of @trialcs. @cp is @cs or one of its descendants. + */ +static void trial_effective_cpus(struct cpuset *cp, struct cpuset *cs, + struct cpuset *trialcs, struct cpumask *new_cpus) +{ + struct cpuset *parent = parent_cs(cs); + + if (!cpumask_and(new_cpus, trialcs->cpus_allowed, parent->effective_cpus) && + is_in_v2_mode()) + cpumask_copy(new_cpus, parent->effective_cpus); + effective_cpus_below(cp, cs, new_cpus); +} + +/* + * Return false if the CPUs of @trialcs make a deadline task of @cs, or of + * a descendant, leave the root domain that counts its bandwidth. A cpuset + * can hold CPUs of two root domains: with isolcpus="domain", or on cgroup + * v1 with sched_load_balance off. The task would run in a root domain that + * did not admit it. Only a move to another cpuset can take the task + * there. The move asks the other root domain for room. + * + * A partition root takes its CPUs from its parent, which checks the same + * for its own tasks: see dl_tasks_keep_without(). + */ +static bool dl_tasks_keep_root_domain(struct cpuset *cs, struct cpuset *trialcs, + struct cpumask *new_cpus) +{ + struct cgroup_subsys_state *pos_css; + struct cpuset *cp; + bool keep = true; + + if (is_partition_valid(cs)) + return true; + rcu_read_lock(); + cpuset_for_each_descendant_pre(cp, pos_css, cs) { + if (is_partition_valid(cp)) { + pos_css = css_rightmost_descendant(pos_css); + continue; + } + trial_effective_cpus(cp, cs, trialcs, new_cpus); + /* + * Test a mask that grows too. A task whose user affinity + * holds none of the old CPUs gets only the new ones. + */ + if (cpumask_equal(cp->effective_cpus, new_cpus)) + continue; + keep = dl_tasks_keep_cpus(cp, new_cpus, NULL, NULL); + if (!keep) + break; + } + rcu_read_unlock(); + return keep; +} + +/* + * Return false if valid partition root @cs gives CPUs back to its parent, + * and a deadline task would then leave its root domain. + * dl_tasks_keep_without() does the check. Making @cs invalid would not + * help: it would give these CPUs back all the same. So the caller refuses + * the write to cpuset.cpus or cpuset.cpus.exclusive. + * + * Two cases are checked: + * - An isolated partition root gives housekeeping CPUs back to a parent + * that is not isolated. The rebuild takes them out of the default root + * domain. The CPUs that @cs takes in the same change count too. + * - @cs only gives CPUs back. While @cs holds them, a cpuset whose + * cpuset.cpus lists only those CPUs gets all the CPUs of the parent. + * Then it gets only those again, and so do its tasks. So does a task + * whose user affinity holds only those CPUs. With isolcpus="domain", + * they can be in another root domain than the task. + * When @cs also takes CPUs, update_parent_effective_cpumask() checks the + * change, and makes @cs invalid if a task of the parent would leave. + */ +static bool dl_tasks_keep_given_back(struct cpuset *cs, struct cpuset *trialcs, + struct tmpmasks *tmp) +{ + struct cpuset *parent = is_remote_partition(cs) ? &top_cpuset : parent_cs(cs); + + if (!is_partition_valid(cs) || + !cpumask_andnot(tmp->delmask, cs->effective_xcpus, trialcs->effective_xcpus)) + return true; + cpumask_andnot(tmp->addmask, trialcs->effective_xcpus, cs->effective_xcpus); + if ((cs->partition_root_state != PRS_ISOLATED || + parent->partition_root_state == PRS_ISOLATED || + !cpumask_intersects(tmp->delmask, housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT))) && + !cpumask_empty(tmp->addmask)) + return true; + return dl_tasks_keep_without(parent, cs, tmp->addmask, tmp->delmask, false); +} + +/* + * Return false if making valid partition root @cs invalid moves a + * deadline task out of the root domain that counts its bandwidth. The + * task can be in @cs or in a cpuset below it. @cs then gives all its CPUs + * back to its parent. Its child partitions become invalid too. @cs and + * each cpuset below it get CPUs of the parent. @parent_cpus holds the + * effective CPUs of the parent after the change. @cpus holds the + * cpuset.cpus of @cs. + * + * The root domain of a root partition goes away at the rebuild, so each + * deadline task that it counts would leave. The CPUs of an isolated + * partition join the root domain of its parent, unless that parent is + * isolated too, or isolcpus="domain" isolates them. A task counted in the + * default root domain must keep a CPU that stays there. + */ +static bool dl_tasks_keep_invalid(struct cpuset *cs, const struct cpumask *cpus, + const struct cpumask *parent_cpus) +{ + struct cpuset *parent = is_remote_partition(cs) ? &top_cpuset : parent_cs(cs); + struct cpumask *new_cpus = dl_keep_cpus, *leaving = dl_leaving_cpus; + struct cgroup_subsys_state *pos_css; + struct cpuset *cp; + bool keep = true; + + lockdep_assert_cpuset_lock_held(); + if (!is_partition_valid(cs) || !dl_tasks_hold_bw()) + return true; + if (cs->partition_root_state != PRS_ISOLATED) + cpumask_copy(leaving, cs->effective_xcpus); + else if (parent->partition_root_state != PRS_ISOLATED) + cpumask_and(leaving, cs->effective_xcpus, + housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT)); + else + leaving = NULL; + rcu_read_lock(); + cpuset_for_each_descendant_pre(cp, pos_css, cs) { + /* + * As compute_effective_cpumask() does for a cpuset that is + * not a partition. + */ + if (!cpumask_and(new_cpus, parent_cpus, cpus) && is_in_v2_mode()) + cpumask_copy(new_cpus, parent_cpus); + effective_cpus_below(cp, cs, new_cpus); + keep = dl_tasks_keep_cpus(cp, new_cpus, NULL, leaving); + if (!keep) + break; + } + rcu_read_unlock(); + return keep; +} + +/* + * Return true if the write of @trialcs makes valid partition root @cs + * invalid. Repeat the checks of partition_cpus_change(), in its order: + * - those of validate_partition(); + * - prstate_housekeeping_conflict(); + * - those of remote_cpus_update() for a remote partition, else those + * of update_parent_effective_cpumask() for partcmd_update. + * This overwrites tmp->addmask and tmp->delmask. + */ +static bool dl_partition_write_invalidates(struct cpuset *cs, struct cpuset *trialcs, + struct tmpmasks *tmp) +{ + struct cpumask *excpus = trialcs->effective_xcpus; + struct cpuset *parent = parent_cs(cs); + int prs = cs->partition_root_state, parent_prs; + bool adding, deleting; + + if (validate_partition(cs, trialcs)) + return true; + parent_prs = is_remote_partition(cs) ? PRS_ROOT : + parent->partition_root_state; + cpumask_andnot(tmp->delmask, cs->effective_xcpus, excpus); + if (parent_prs > 0 && + prstate_housekeeping_conflict(prs, parent_prs, excpus, tmp->delmask)) + return true; + if (is_remote_partition(cs)) { + adding = cpumask_andnot(tmp->addmask, excpus, cs->effective_xcpus); + deleting = !cpumask_empty(tmp->delmask); + return adding && + (!capable(CAP_SYS_ADMIN) || + cpumask_intersects(tmp->addmask, subpartitions_cpus) || + cpumask_subset(top_cpuset.effective_cpus, tmp->addmask) || + !dl_tasks_keep_without(&top_cpuset, cs, tmp->addmask, + deleting ? tmp->delmask : NULL, false)); + } + cpumask_andnot(tmp->addmask, cs->effective_xcpus, excpus); + adding = cpumask_and(tmp->addmask, tmp->addmask, parent->effective_xcpus); + cpumask_andnot(tmp->delmask, excpus, cs->effective_xcpus); + deleting = cpumask_and(tmp->delmask, tmp->delmask, parent->effective_xcpus); + if (tasks_nocpu_error(parent, cs, excpus) && + (!adding || !cpumask_intersects(tmp->addmask, cpu_active_mask))) + return true; + return deleting && + !dl_tasks_keep_without(parent, cs, tmp->delmask, + adding ? tmp->addmask : NULL, false); +} + +/* + * Return false if a write of @trialcs to valid partition root @cs makes + * @cs invalid, and a deadline task would then leave its root domain. The + * caller refuses such a write to cpuset.cpus or cpuset.cpus.exclusive + * with EBUSY. The administrator asked for new CPUs, not for the end of + * the partition. A CPU offline and a write of "member" to + * cpuset.cpus.partition are not checked. + */ +static bool dl_tasks_keep_valid(struct cpuset *cs, struct cpuset *trialcs, + struct tmpmasks *tmp) +{ + struct cpuset *parent = is_remote_partition(cs) ? &top_cpuset : parent_cs(cs); + + if (!is_partition_valid(cs) || !dl_tasks_hold_bw() || + !dl_partition_write_invalidates(cs, trialcs, tmp)) + return true; + cpumask_or(tmp->new_cpus, parent->effective_cpus, cs->effective_xcpus); + cpumask_and(tmp->new_cpus, tmp->new_cpus, cpu_active_mask); + return dl_tasks_keep_invalid(cs, trialcs->cpus_allowed, tmp->new_cpus); +} + +/* + * Return false if a write of @trialcs to @cs takes away all the CPUs of a + * valid partition root below @cs, and a deadline task of that partition + * would then leave its root domain. That partition becomes invalid in + * update_cpumasks_hier(), and gets CPUs of its parent: see + * dl_tasks_keep_invalid(). + * + * The CPUs that @cs offers below are its new exclusive CPUs if it is a + * partition root, else its new cpuset.cpus. The CPUs of a partition are + * within those of the partitions above it. So a partition keeps a CPU if + * it holds one of these. The CPUs that its parent then gives it are an + * estimate: those offered, within the current effective CPUs of the + * parent. If the estimate is wrong, the check can refuse a write that + * keeps the task in its root domain, or accept one that does not. + */ +static bool dl_tasks_keep_parts_below(struct cpuset *cs, struct cpuset *trialcs, + struct tmpmasks *tmp) +{ + const struct cpumask *offer = is_partition_valid(cs) ? + trialcs->effective_xcpus : trialcs->cpus_allowed; + struct cgroup_subsys_state *pos_css; + struct cpuset *cp; + bool keep = true; + + if (!dl_tasks_hold_bw()) + return true; + rcu_read_lock(); + cpuset_for_each_descendant_pre(cp, pos_css, cs) { + if (cp == cs || !is_partition_valid(cp) || + cpumask_intersects(cp->effective_xcpus, offer)) + continue; + cpumask_and(tmp->new_cpus, offer, cpu_active_mask); + if (parent_cs(cp) != cs) + cpumask_and(tmp->new_cpus, tmp->new_cpus, + parent_cs(cp)->effective_cpus); + /* dl_tasks_keep_invalid() can sleep: pin @cp. */ + if (!css_tryget_online(&cp->css)) + continue; + rcu_read_unlock(); + keep = dl_tasks_keep_invalid(cp, cp->cpus_allowed, tmp->new_cpus); + rcu_read_lock(); + /* dl_tasks_keep_invalid() checked the cpusets below @cp. */ + pos_css = css_rightmost_descendant(pos_css); + css_put(&cp->css); + if (!keep) + break; + } + rcu_read_unlock(); + return keep; +} + /** * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it * @cs: the cpuset to consider @@ -2550,6 +2990,14 @@ static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs, if (alloc_tmpmasks(&tmp)) return -ENOMEM; + if (!dl_tasks_keep_root_domain(cs, trialcs, tmp.new_cpus) || + !dl_tasks_keep_given_back(cs, trialcs, &tmp) || + !dl_tasks_keep_valid(cs, trialcs, &tmp) || + !dl_tasks_keep_parts_below(cs, trialcs, &tmp)) { + free_tmpmasks(&tmp); + return -EBUSY; + } + /* * Check all the descendants in update_cpumasks_hier() if * effective_xcpus is to be changed. @@ -2621,6 +3069,13 @@ static int update_exclusive_cpumask(struct cpuset *cs, struct cpuset *trialcs, return -ENOMEM; trialcs->prs_err = PERR_NONE; + if (!dl_tasks_keep_given_back(cs, trialcs, &tmp) || + !dl_tasks_keep_valid(cs, trialcs, &tmp) || + !dl_tasks_keep_parts_below(cs, trialcs, &tmp)) { + free_tmpmasks(&tmp); + return -EBUSY; + } + partition_cpus_change(cs, trialcs, &tmp); spin_lock_irq(&callback_lock); @@ -3983,6 +4438,9 @@ int __init cpuset_init(void) BUG_ON(!alloc_cpumask_var(&cpus_attach, GFP_KERNEL)); BUG_ON(!zalloc_cpumask_var(&dl_attach_cpus, GFP_KERNEL)); + BUG_ON(!alloc_cpumask_var(&dl_keep_cpus, GFP_KERNEL)); + BUG_ON(!alloc_cpumask_var(&dl_pending_cpus, GFP_KERNEL)); + BUG_ON(!alloc_cpumask_var(&dl_leaving_cpus, GFP_KERNEL)); if (housekeeping_enabled(HK_TYPE_DOMAIN_BOOT)) cpumask_andnot(isolated_cpus, cpu_possible_mask, diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c index c6c8495c73ca..859977bcc01f 100644 --- a/kernel/sched/deadline.c +++ b/kernel/sched/deadline.c @@ -5438,6 +5438,70 @@ int dl_bw_deactivate(int cpu) return ret; } +/* + * Return false if @p has CPUs in the root domain that counts its + * bandwidth and @new_mask holds none of them. A cpuset refuses such a + * mask: only a move to another cpuset makes room in another root domain. + * Like __set_cpus_allowed_ptr(), test @new_mask within the user affinity + * of @p, if they intersect. @pending is NULL, or holds the CPUs that + * the rebuild at the end of the change gives to the root domain of a + * partition root. @leaving is NULL, or holds CPUs that leave their root + * domain at the rebuild. They do not keep @p in its root domain. + */ +bool dl_task_keeps_root_domain(struct task_struct *p, + const struct cpumask *new_mask, + const struct cpumask *pending, + const struct cpumask *leaving) +{ + const struct cpumask *span, *user = cpu_possible_mask; + + if (!dl_bandwidth_enabled() || !task_has_dl_policy(p)) + return true; + /* A dead task does not run again, so its mask does not matter. */ + if (READ_ONCE(p->__state) == TASK_DEAD) + return true; + /* pi_lock also protects p->user_cpus_ptr. */ + guard(raw_spinlock_irqsave)(&p->pi_lock); + guard(rcu)(); + span = dl_task_bw_span(p); + /* + * Test cpus_mask: in a migrate_disable() section, cpus_ptr holds + * one CPU. + */ + if (!span || !cpumask_intersects(span, &p->cpus_mask)) + return true; + if (p->user_cpus_ptr && cpumask_intersects(new_mask, p->user_cpus_ptr)) + user = p->user_cpus_ptr; + if (leaving) { + int cpu; + + for_each_cpu_and(cpu, span, new_mask) { + if (cpumask_test_cpu(cpu, user) && + !cpumask_test_cpu(cpu, leaving)) + return true; + } + } else if (cpumask_intersects_and(span, new_mask, user)) { + return true; + } + /* + * The rebuild at the end of the change can give the root domain + * of @p the CPUs of @pending. Test those too. + */ + return pending && cpumask_intersects(span, pending) && + cpumask_intersects_and(pending, new_mask, user); +} + +/* + * Return true if admission control is on and a task holds a bandwidth. + * Otherwise dl_task_keeps_root_domain() accepts any mask. The caller must + * hold cpuset_mutex. A task gets a bandwidth only under it, in + * __sched_setscheduler(), so none gets one while the caller holds it. + */ +bool dl_tasks_hold_bw(void) +{ + return dl_bandwidth_enabled() && atomic_read(&dl_nr_bw_tasks); +} + int dl_bw_alloc(int cpu, u64 dl_bw) { return dl_bw_manage(dl_bw_req_alloc, cpu, dl_bw); -- 2.47.3