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=1796310005; b=nzckq3EZ60JQcg59LydTNQ/2GIGZgWhUzCLYWndQM2IqirBoHFj3C0eWhjOUL5iTqnb0 IoCPPNGD5gPvUTgGp/a5lYsQp+9wNbWCHfjN16FmHn+qnkeJ56zGBvMDXadV4QmWl1SG/ JnT0PfTASnBlqh6C/a6p6Bj5WbR8/mAEr7My+rMKXIA+bX3NsFn1hyh/GqlO9R/LZa1MW WoX7ieNDY6trcyDb/vm+4J17PbSMP1vREQcVemLpat7pqpbxPtHbANn69xz2RiTgQlwRz vhpN/wZ4vk/fBNq7Vu9cPPA0wnRbJAoTNA+s7ixeBptkKREGVQF5wifDlf5+ffvdZGQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796310005; 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=xWo4+5A0H6eZn8tYfAWtuUATFPhbOeYqY6fkeS05O2I=; b=vcJjzDGUh8tasNF0PK1Chy7/2TBBzB72FXd/9Efe/i5V+2I+SQd8NdkUvlGsEb/6rcbd +AEBYlyLEROAvjKaEjpszJOdAQP3XIg/sbvR37TSjgh7Gd60ooAjS2Vuhw0Z6Yh/Po4FQ f1r+75QZ4jmm6IvfJ7P2UW1JuP/CyOnzAV3R3G1/k1DMO1esXuUp9oJcY6HA62MF5in0b pCAtc555TSRjbOCTgVCOX6M5hpluXglXEeShstfXL9AJf8E2jZli/WXqWR3nOZKVw1z6p 7Dtbu6qtApTxZf51Fh5YaXgnAuKkduRp2Z4Ud0cGX70CilbiJIUeIshb0iq83uby9pg== 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=bvZ7bxdx; 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 4AD2F6070A8D; Thu, 3 Dec 2026 15:00:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4AD2F6070A8D 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=bvZ7bxdx DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=smtpout1; t=1796310004; bh=xWo4+5A0H6eZn8tYfAWtuUATFPhbOeYqY6fkeS05O2I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bvZ7bxdxXXV1N9TAnRDDHT3vhIyTXxdFYt5YXbollc42EQwbmW5bhxdJX6/HvXkJ1 ZKRP/YlfWg5nXZHFr4QX3DGq/6BVSfBge6a+66zBdXKThtHwo0shRAVr84OdrEWbfJ 8sB/ViqZapWYYKMIS7gVQIztjC8LYcqZDj2JQIMgBnOwS+0QZAR55DjIe+QvJLUd0G FuuiU/oHwnMeq0Izc5Ezzv01f6NIhnwb3NGauKtw78mGvzrJ6BRGxb/FslIB+LSfBB TjN+EYykKJKdNGtRFgzDt1UjTOtOGMFbmUFQRcVWjTFHKXaW+5Rdfwz4l0jSaEsf2g xu5GTGz6pXF6g== Received: from thinkos.internal.efficios.com (unknown [IPv6:2605:8d80:7180:6739:ac39:5d4:4093:61c6]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4jgHq65drmzZqv; Thu, 03 Dec 2026 10:00:02 -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 35/39] sched/deadline: Count again a departed task that a boost brings back Date: Thu, 3 Dec 2026 09:58:49 -0500 Message-ID: <47cd9c920674eb112c1800ea6568370eaf4db6f0.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 task that leaves SCHED_DEADLINE before its 0-lag time is a departed task. Its root domain keeps its bandwidth until then, and the task waits on dl_departed_list. A rebuild of the root domains' accounting puts this bandwidth back in the root domain of the CPU recorded with it. If that CPU is no longer active, the rebuild drops the bandwidth. A CPU that goes offline is in the default root domain by then, so the root domain that counted the task cannot be found. dl_bw_gen then keeps the release of the task from removing anything. But priority inheritance can bring such a task back into the class. Here a partition root has CPUs X and Y: 1. A deadline task T holds an rt_mutex, and sleeps on X. X is the CPU recorded with its bandwidth. 2. sched_setattr() makes T a SCHED_OTHER task, before its 0-lag time. T goes on the departed list. 3. T wakes up on Y, then sleeps there. The record stays X. 4. X goes offline. The rebuild that follows drops the bandwidth of T. 5. A deadline task blocks on the rt_mutex. It boosts T back into the class, before the 0-lag time of T. switched_to_dl() takes T off the departed list. this_bw of Y counts T again, but no root domain counts it in total_bw. Until the deboost, sched_setattr() can admit tasks beyond the capacity of the root domain. Fix this in dl_task_undepart(). If a rebuild dropped the bandwidth, count it again on the CPU that the walk of the task list would pick (dl_task_count_cpu()). Do not use the CPU of the task. The task can sleep on a CPU that went offline, or on one that its mask no longer holds. If no CPU of its mask is active, the move of the task to an active CPU counts it (dl_task_move_bw()). dl_task_undepart() leaves the task to the walk of the task list after a rebuild, if the walk has not reached it yet (dl_bw_move_blocked()). But the walk counts only the tasks in the class, so it can pass a departed task. The walk marks in dl_bw_skip_gen the tasks it leaves out, and dl_bw_move_blocked() lets them through. Mark in the same way the departed tasks that dl_clear_root_domains() and dl_task_depart() leave out. This was found by reading the code. The pi_departed_offline_boost case of deadline_bw_test shows the bug. "selftests/sched: Test PI against hotplug and rebuilds in deadline_bw_test", later in this series, adds the case. The runs used v13 of this series, which had an earlier version of this fix. The case failed in 33 of 33 runs without the fix, and passed in 25 of 25 with it. Fixes: f9a25f776d78 ("cpusets: Rebuild root domain deadline accounting information") Signed-off-by: Mathieu Desnoyers Cc: stable@vger.kernel.org --- include/linux/sched.h | 8 ++++---- kernel/sched/deadline.c | 38 +++++++++++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index b32421cd0107..e27d341ca763 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -762,10 +762,10 @@ struct sched_dl_entity { * @dl_bw_gen is the value of dl_bw_gen when the bandwidth was last * counted. If a rebuild dropped it since, there is nothing to remove. * - * @dl_bw_skip_gen is the value of dl_bw_gen when the walk of the - * task list after a rebuild left the task out: no CPU of it was - * active. A move of the task then counts the bandwidth, even before - * the walk ends. + * @dl_bw_skip_gen is the value of dl_bw_gen when a rebuild of the + * accounting left the task out. Before the walk of the task list + * ends, a move of the task can then count the bandwidth. So can a + * boost of a departed task back into the class. * * @dl_bw_resv_cpu is a CPU of the root domain where a cpuset attach * in progress reserved room for the bandwidth, or -1. diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c index 02c6e2c20027..f2429672358c 100644 --- a/kernel/sched/deadline.c +++ b/kernel/sched/deadline.c @@ -249,7 +249,7 @@ static unsigned int dl_task_count_cpu(struct task_struct *p); * it back. * - Older: an earlier rebuild dropped it. A task on the departed list * stays out, and its release removes nothing. A task that is in the - * class is counted again. + * class, or that a boost brings back into it, is counted again. * * Only equalities are compared, so the wrap of the counter is * harmless. @@ -305,8 +305,8 @@ static inline bool dl_bw_rebuilding(void) * Return true if the bandwidth of @p must not move now. Either the sched * domains are rebuilt, or the walk of the task list that follows has not * reached @p yet. The walk then counts @p. A move must not count it - * first. A task the walk left out, with no active CPU, can move: no one - * else counts it. + * first. A task that the rebuild left out can move, or come back into + * the class by a boost. No one else counts it. dl_bw_skip_gen marks it. * * The answer is exact under dl_departed_lock. Without it, the answer is a * hint: dl_task_move_bw() tests again under the lock. @@ -434,6 +434,8 @@ static void dl_task_depart(struct task_struct *p) cpu = dl_task_count_cpu(p); if (cpu < nr_cpu_ids) dl_task_count_bw(p, cpu); + else + WRITE_ONCE(dl_se->dl_bw_skip_gen, dl_bw_gen); } list_add(&dl_se->dl_departed_node, &dl_departed_list); } @@ -442,6 +444,12 @@ static void dl_task_depart(struct task_struct *p) * Take @p off dl_departed_list. @p comes back to the deadline class, and * its bandwidth is that of a deadline task again. The caller holds the rq * lock of @p. + * + * A rebuild may have dropped the bandwidth while @p was departed. No root + * domain counts it then, but this_bw of its CPU does again once @p is + * back. Count it on the CPU the walk of the task list would pick. Leave it + * to the walk if the walk has not reached @p yet. Leave it to the move of + * @p to an active CPU if no CPU of its mask is active. */ static void dl_task_undepart(struct task_struct *p) { @@ -452,6 +460,12 @@ static void dl_task_undepart(struct task_struct *p) return; guard(raw_spinlock)(&dl_departed_lock); list_del_init(&dl_se->dl_departed_node); + if (dl_se->dl_bw_gen != dl_bw_gen && !dl_bw_move_blocked(p)) { + unsigned int cpu = dl_task_count_cpu(p); + + if (cpu < nr_cpu_ids) + dl_task_count_bw(p, cpu); + } } /* @@ -4767,16 +4781,17 @@ void dl_clear_root_domains(void) * (dl_task_release_bw()). */ if (dl_se->dl_bw_gen != dl_bw_gen - 1) - continue; + goto left_out; /* * Drop the bandwidth of a task whose CPU is inactive. A CPU * that goes offline is in the default root domain by then. * The root domain that counted the task cannot be found * from the recorded number. The release then removes - * nothing, and no later rebuild restores it. + * nothing. A boost of the task back into the class counts + * it again (dl_task_undepart()). */ if (cpu < 0 || !cpu_active(cpu)) - continue; + goto left_out; dl_b = dl_bw_of(cpu); raw_spin_lock(&dl_b->lock); __dl_add(dl_b, dl_se->dl_bw, dl_bw_cpus(cpu)); @@ -4787,6 +4802,14 @@ void dl_clear_root_domains(void) */ dl_record_bw_cpu(dl_se, cpu); dl_se->dl_bw_gen = dl_bw_gen; + continue; +left_out: + /* + * The walk counts only the tasks in the class. It can pass + * this task before a boost brings it back. Mark the task, so + * that dl_bw_move_blocked() lets the boost count it at once. + */ + WRITE_ONCE(dl_se->dl_bw_skip_gen, dl_bw_gen); } } @@ -4884,7 +4907,8 @@ static void switched_to_dl(struct rq *rq, struct task_struct *p) * meanwhile, its next wakeup or migration there first moves the * bandwidth and the record, as for any deadline task. A rebuild while * it is boosted counts it like any deadline task, and records the CPU - * it picks. + * it picks. If a rebuild dropped the bandwidth meanwhile, + * dl_task_undepart() counts it again, and records the CPU it picks. */ dl_task_undepart(p); -- 2.47.3