sched/deadline: bandwidth accounting fixes and selftests
Subsystems (7)
[email protected]
AI reviewRe: [PATCH v13 05/36] sched/deadline: Keep a departing constrained task until its next period
In reply to Mathieu DesnoyersThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] sched/deadline: premature bw release in inactive_task_timer() on pending race - [Medium]…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] sched/deadline: premature bw release in inactive_task_timer() on pending race
- [Medium] sched/deadline: extra period wait when dl_task_timer() precedes inactive timer
--
commit 013654a1a833237d3e6842ffbe9cffc69bd53725
Author: Mathieu Desnoyers <[email protected]>
sched/deadline: Keep a departing constrained task until its next period
This commit retains the bandwidth and deadline of a constrained SCHED_DEADLINE task that leaves the class until the later of its 0-lag time and its next period, and adds dl_inactive_pending to coordinate with an active inactive_task_timer() handler.
Quoted 44 lines
kernel/sched/deadline.c +27
-0@@ -515,6 +515,7 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task)struct rq *rq =rq_of_dl_se (dl_se);struct dl_rq *dl_rq = &rq->dl;s64 zerolag_time; +bool running, pending;/* * If this is a non-deadline task that has been boosted, @@ -528,13 +529,50 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task)WARN_ON (dl_se->dl_non_contending); +/* +* inactive_task_timer() restarts itself for a constrained task that +* has left the class. If the task came back while the handler ran, +* switched_to_dl() could not cancel the timer, which may be queued +* again. Cancel it here. A running handler cannot be canceled. +* dl_inactive_pending then tells whether it will still act on the +* task, once it gets the rq lock or after its restart. +*/ +if (dl_server (dl_se)) { + running =hrtimer_active (timer); + pending = running; + }else { +int ret =hrtimer_try_to_cancel (timer); + +if (ret ==1 ) +put_task_struct (dl_task_of (dl_se)); + running = ret <0 ; + pending = running && dl_se->dl_inactive_pending; + } + zerolag_time =dl_time_to_zerolag (dl_se, rq); +/* +* A constrained task that leaves the class keeps its bandwidth and its +* deadline until its next period, if that comes after its 0-lag time. +* Back in the class before then, it goes on with its current period, +* as if it had stayed. A new period at once would give it more than +* its runtime in this period. A dying task does not come back, so it
kernel/sched/deadline.c static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task) · line 569
Quoted 10 lines
kernel/sched/deadline.c +10
-0+* releases its bandwidth at its 0-lag time. +*/ +if (!dl_task && !dl_server (dl_se) && !dl_is_implicit (dl_se) && + dl_se->deadline && +READ_ONCE (dl_task_of (dl_se)->__state) !=TASK_DEAD ) { +s64 next =dl_next_period (dl_se) -rq_clock (rq); + +if (next > zerolag_time) + zerolag_time = next; + }
[Severity: Medium]
Can a throttled constrained task that leaves SCHED_DEADLINE end up keeping its bandwidth and deadline for an extra unused period?
When a constrained task is throttled, start_dl_timer() arms dl_se->dl_timer for dl_next_period(dl_se). If the task switches out of SCHED_DEADLINE before that period ends, task_non_contending() arms inactive_timer for at least dl_next_period(dl_se), so dl_task_timer() expires at the period boundary before inactive_task_timer() and replenishes the departed task:
kernel/sched/deadline.c:dl_task_timer() {
...
if (!dl_task(p)) {
if (dl_se->dl_throttled && !is_dl_boosted(dl_se)) {
sched_clock_tick();
update_rq_clock(rq);
replenish_dl_entity(dl_se);
}
goto unlock;
}
...
}replenish_dl_entity() adds dl_period to dl_se->deadline and dl_runtime to dl_se->runtime. That leaves dl_time_to_zerolag() unchanged, but shifts dl_next_period(dl_se) forward by a full period. When inactive_task_timer() then runs, it sees dl_next_period(dl_se) - rq_clock(rq) > 0 for the newly replenished period and returns HRTIMER_RESTART.
The same happens here in task_non_contending() if a sleeping throttled constrained task is replenished at the period boundary by dl_task_timer() and then leaves SCHED_DEADLINE while still asleep before consuming any runtime in the new period (so zerolag_time < 0, but next > 0). In both cases, the task holds its bandwidth in total_bw for a second period in which it never ran as SCHED_DEADLINE, and if it returns to the class after the replenished dl_se->deadline in that second period, dl_check_constrained_dl() throttles it until the period ends.
Quoted 21 lines
kernel/sched/deadline.c +9
-1/* * If the "0-lag time" already passed, decrease the active * utilization now, instead of starting a timer */ -if ((zerolag_time <0 ) ||hrtimer_active (&dl_se->inactive_timer)) { +if ((zerolag_time <0 ) || (running && (dl_task || pending))) {if (dl_server (dl_se)) {sub_running_bw (dl_se, dl_rq); }else {@@ -543,6 +581,14 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task)if (dl_task)sub_running_bw (dl_se, dl_rq); +/* +* The pending handler of a task that has left the +* class releases its bandwidth, or restarts for its +* next period. +*/ +if (pending && !dl_task && +READ_ONCE (p->__state) !=TASK_DEAD ) +return ;
[ ... ]
Quoted 18 lines
kernel/sched/deadline.c +14
-0@@ -2321,6 +2369,40 @@ static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)if (dl_server (dl_se)) goto no_task; +/* +* A constrained task which has left the class keeps its bandwidth +* and its deadline until its next period, as in +* task_non_contending(). This timer may have been armed for the +* 0-lag time while the task was still in the class. +*/ +if (!dl_task (p) &&READ_ONCE (p->__state) !=TASK_DEAD && + !dl_is_implicit (dl_se)) { +s64 left =dl_next_period (dl_se) -rq_clock (rq); + +if (left >0 ) { +ktime_t at =ktime_add_ns (hrtimer_cb_get_time (timer), left); + +/*
kernel/sched/deadline.c static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer) · line 2395
Quoted 10 lines
kernel/sched/deadline.c +10
-0+* The timer is not queued: while this handler runs, +* task_non_contending() finds dl_inactive_pending set +* and does not arm it. +*/ +WARN_ON_ONCE (hrtimer_is_queued (timer)); +hrtimer_set_expires (timer, at); +task_rq_unlock (rq, p, &rf); +return HRTIMER_RESTART ; + } + }
[Severity: Medium]
Can inactive_task_timer() release a departing task's bandwidth before its 0-lag time when task_non_contending() takes the pending early return?
When pending is true for a departing task, task_non_contending() returns early for both implicit and constrained tasks, relying on the running inactive_task_timer() handler to wait until the required retention time. However, inactive_task_timer() only checks dl_next_period(dl_se) - rq_clock(rq) when !dl_is_implicit(dl_se), and never checks dl_time_to_zerolag(dl_se, rq):
CPU0 CPU1
T blocks before 0-lag time
task_non_contending() arms timerinactive_task_timer()
task_rq_lock(T) waits
T wakes up at/after 0-lag time
replenish_dl_new_period() T runs and consumes runtime
sched_setattr(T, SCHED_OTHER)
switched_from_dl()
task_non_contending()
pending is true -> returns
acquires task_rq_lock(T)
__dl_sub() / __dl_clear_params() <-- releases bandwidth before new 0-lag
If T has an implicit deadline, !dl_is_implicit(dl_se) is false, so inactive_task_timer() immediately subtracts T's bandwidth from total_bw and clears its parameters even though its new 0-lag time is in the future. If T has a constrained deadline and overran its runtime (dl_se->runtime < 0), dl_time_to_zerolag(dl_se, rq) is later than dl_next_period(dl_se) - rq_clock(rq), so inactive_task_timer() releases T's bandwidth at dl_next_period(dl_se) before the overrun is paid back. In either case, T can re-enter SCHED_DEADLINE and consume more than its reserved bandwidth.
In addition, unlike task_non_contending(), this check does not guard on dl_se->deadline != 0 before calling dl_next_period(dl_se), which can be 0 if a sleeping task enters and leaves SCHED_DEADLINE with constrained parameters while a previously restarted inactive_task_timer() is waiting for task_rq_lock().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH v13 06/36] sched/deadline: Keep departing bandwidth across a root domain rebuild
In reply to Mathieu DesnoyersThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] sched/deadline: departed bw misattributed in dl_clear_root_domains() - [Medium] sched/deadline…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] sched/deadline: departed bw misattributed in dl_clear_root_domains()
- [Medium] sched/deadline: departed bw not discounted under lock in sched_dl_overflow()
--
commit 61bc675abcdc6b82b59f9a092043cd6f4e528bc5
Author: Mathieu Desnoyers <[email protected]>
sched/deadline: Keep departing bandwidth across a root domain rebuild
This commit tracks tasks that leave SCHED_DEADLINE or die before their 0-lag time on dl_departed_list so that root domain accounting rebuilds preserve their bandwidth until their inactive timer fires.
Quoted 4 lines
kernel/sched/deadline.c +0
-0
[ ... ]
Quoted 42 lines
kernel/sched/deadline.c +35
-2@@ -3744,9 +3881,51 @@ void dl_clear_root_domain(struct root_domain *rd)dl_server_add_bw (rd, i); } -void dl_clear_root_domain_cpu (int cpu) +/* +* Clear every root domain before a rebuild of their accounting, and count +* the rebuild in dl_bw_gen. Put back the bandwidth of their servers. Put +* back the bandwidth of the tasks on dl_departed_list, which no cpuset +* holds as deadline tasks. Put each in the root domain of the CPU +* recorded for it, or drop it if that CPU is not active. +* dl_add_task_root_domain() then adds the deadline tasks of the cpusets. +*/ +void dl_clear_root_domains (void ) { -dl_clear_root_domain (cpu_rq (cpu)->rd); +struct sched_dl_entity *dl_se; +u64 cookie = ++dl_cookie; +struct dl_bw *dl_b; +int cpu; + +guard (raw_spinlock_irqsave)(&dl_departed_lock); + dl_bw_gen++; +for_each_possible_cpu (cpu) { +if (!dl_bw_visited (cpu, cookie)) +dl_clear_root_domain (cpu_rq (cpu)->rd); + } +list_for_each_entry (dl_se, &dl_departed_list, dl_departed_node) { + cpu = dl_se->dl_departed_cpu; +/* +* Skip bandwidth that an earlier rebuild dropped. It is not +* counted now, and its release removes nothing +* (dl_task_release_bw()). +*/ +if (dl_se->dl_bw_gen != dl_bw_gen -1 ) +continue ; +/* +* 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.
kernel/sched/deadline.c void dl_clear_root_domain(struct root_domain *rd) · line 3929
Quoted 10 lines
kernel/sched/deadline.c +9
-0+*/ +if (!cpu_active (cpu)) +continue ; + dl_b =dl_bw_of (cpu); +raw_spin_lock (&dl_b->lock); +__dl_add (dl_b, dl_se->dl_bw,dl_bw_cpus (cpu)); +raw_spin_unlock (&dl_b->lock); + dl_se->dl_bw_gen = dl_bw_gen; + } }
[Severity: Medium]
Can dl_clear_root_domains() (and the dl_departed_cpu branch in dl_add_task_root_domain()) charge departed bandwidth to the wrong root domain when dl_se->dl_departed_cpu is split into a new child cpuset partition before the 0-lag timer fires?
If tasks in a multi-CPU root domain leave SCHED_DEADLINE on CPU N and a new partition root is created on CPU N before their inactive timers expire, dl_bw_of(dl_se->dl_departed_cpu) resolves to the new partition's root domain. The original root domain loses that departing bandwidth and can over-admit deadline tasks, while the new partition is charged for bandwidth that never belonged to it (rejecting admissions with -EBUSY and underflowing cpu_rq(cpu)->dl.extra_bw via __dl_add() -> __dl_update() if the departed bandwidth on CPU N exceeds the new partition's max_bw).
[ ... ]
Quoted 18 lines
kernel/sched/deadline.c +5
-2@@ -4065,8 +4261,6 @@ int sched_dl_overflow(struct task_struct *p, int policy,if (dl_policy (policy) && !task_has_dl_policy (p) && !__dl_overflow (dl_b, cap,0 , new_bw)) { -if (hrtimer_active (&p->dl.inactive_timer)) -__dl_sub (dl_b, p->dl.dl_bw, cpus);__dl_add (dl_b, new_bw, cpus); err =0 ; } else if (dl_policy(policy) && task_has_dl_policy(p) &&@@ -4092,6 +4286,19 @@ int sched_dl_overflow(struct task_struct *p, int policy, }raw_spin_unlock (&dl_b->lock); +/* +* A task that comes back before its 0-lag time still has the +* bandwidth it left with. The new bandwidth replaces it, so remove the +* old one from the root domain that keeps it (dl_task_release_bw()). +* No rebuild can run meanwhile: the caller holds cpuset_mutex.
kernel/sched/deadline.c int sched_dl_overflow(struct task_struct *p, int policy, · line 4303
Quoted 10 lines
kernel/sched/deadline.c +8
-0+*/ +if (!err &&dl_policy (policy) && !task_has_dl_policy (p)) { +lockdep_assert_cpuset_lock_held (); +if (p->dl.dl_departed_cpu >=0 ) +dl_task_release_bw (p,-1 ); + p->dl.dl_bw_gen = dl_bw_gen; + } +return err; }
[Severity: Medium]
When a departed task returns to SCHED_DEADLINE in the same root domain before its 0-lag time, does deferring the old p->dl.dl_bw subtraction to dl_task_release_bw() after raw_spin_unlock(&dl_b->lock) double-count the task's bandwidth?
__dl_overflow(dl_b, cap, 0, new_bw) checks admission with old_bw = 0 even though p->dl.dl_bw is still in dl_b->total_bw, so sched_setattr() fails with -EBUSY whenever dl_b->total_bw + new_bw exceeds capacity while dl_b->total_bw - p->dl.dl_bw + new_bw would fit. In addition, between raw_spin_unlock(&dl_b->lock) and dl_task_release_bw() reacquiring dl_b->lock, both p->dl.dl_bw and new_bw are present in dl_b->total_bw and subtracted from rq->dl.extra_bw, which can cause concurrent sched_dl_global_validate() or dl_server_apply_params() calls to fail with -EBUSY and transiently skews (or underflows when admission control is disabled) rq->dl.extra_bw in grub_reclaim().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[email protected]
AI reviewRe: [PATCH v13 20/36] sched/deadline: Keep a task in the root domain that counts its bandwidth
In reply to Mathieu DesnoyersThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] sched/deadline: stale dl_bw_def for departed tasks in dl_clear_root_domains() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] sched/deadline: stale dl_bw_def for departed tasks in dl_clear_root_domains()
--
commit 951a7fe2fe695c77ff8cf06e107558af78b19d1b
Author: Mathieu Desnoyers <[email protected]>
sched/deadline: Keep a task in the root domain that counts its bandwidth
This commit introduces dl_task_bw_span() and dl_bw_def tracking to keep SCHED_DEADLINE tasks and their bandwidth in the root domain that admitted them across CPU selection, hotplug, and sched domain rebuilds.
Quoted 4 lines
kernel/sched/deadline.c +0
-0
[ ... ]
Quoted 12 lines
kernel/sched/deadline.c +1
-1@@ -4272,7 +4465,7 @@ void dl_add_task_root_domain(struct task_struct *p)raw_spin_lock (&dl_b->lock);__dl_add (dl_b, p->dl.dl_bw,cpumask_weight (rq->rd->span));raw_spin_unlock (&dl_b->lock); - p->dl.dl_bw_cpu = cpu; +dl_record_bw_cpu (&p->dl, cpu); p->dl.dl_bw_gen = dl_bw_gen; unlock:raw_spin_unlock (&dl_departed_lock);@@ -4428,7 +4621,11 @@ void dl_clear_root_domains(void)* has not reached still waits for it: see dl_bw_move_blocked(). */
kernel/sched/deadline.c void dl_clear_root_domains(void) · line 4631
Quoted 10 lines
kernel/sched/deadline.c +5
-1WARN_ON_ONCE (!(dl_bw_rebuild_seq &1 )); -WRITE_ONCE (dl_bw_rebuild_seq, dl_bw_rebuild_seq +1 ); +/* +* After the writes of rq->rd and of dl_bw_gen: pairs with the +* acquire in dl_task_bw_rd(). +*/ +smp_store_release (&dl_bw_rebuild_seq, dl_bw_rebuild_seq +1 );WRITE_ONCE (dl_bw_walking, true);for_each_possible_cpu (cpu) {if (!dl_bw_visited (cpu, cookie))
[Severity: Medium]
Should the dl_departed_list loop in dl_clear_root_domains() also call dl_record_bw_cpu(dl_se, cpu) when it re-counts a departed task's bandwidth?
kernel/sched/deadline.c:dl_clear_root_domains() {
...
list_for_each_entry(dl_se, &dl_departed_list, dl_departed_node) {
cpu = dl_se->dl_bw_cpu;
...
dl_b = dl_bw_of(cpu);
raw_spin_lock(&dl_b->lock);
__dl_add(dl_b, dl_se->dl_bw, dl_bw_cpus(cpu));
raw_spin_unlock(&dl_b->lock);
dl_se->dl_bw_gen = dl_bw_gen;
}
}If the rebuild moved cpu into or out of def_root_domain (for example, when a cpuset partition changes between root and isolated), dl_se->dl_bw_def is left stale, and dl_add_task_root_domain() skips the task because dl_se->dl_bw_gen is already equal to dl_bw_gen. If priority inheritance then boosts the task back into dl_sched_class before its 0-lag time and another rebuild runs, dl_task_rebuild_cpu() will read the stale dl_bw_def and move the bandwidth to the wrong side without admission control.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
[email protected]
AI reviewRe: [PATCH v13 18/36] sched/deadline: Make a rebuild count a task in the root domain it runs in
In reply to Mathieu DesnoyersThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] sched/deadline: missing dl_bw_gen check in __dl_task_bw_move_cpu() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] sched/deadline: missing dl_bw_gen check in __dl_task_bw_move_cpu()
--
commit 7839622ca2175dc96fddcd96c7f89dcd473e1cdd
Author: Mathieu Desnoyers <[email protected]>
sched/deadline: Make a rebuild count a task in the root domain it runs in
This commit introduces dl_task_rebuild_cpu() to select the root domain where a deadline task's bandwidth is counted during a rebuild, leaves out live tasks that have no active CPU, and switches dl_get_task_effective_cpus() to HK_TYPE_DOMAIN_BOOT.
Quoted 4 lines
kernel/sched/deadline.c +0
-0
[ ... ]
Quoted 12 lines
kernel/sched/deadline.c +2
-5@@ -3992,12 +4059,20 @@ void dl_add_task_root_domain(struct task_struct *p)return ; } - msk =this_cpu_cpumask_var_ptr (local_cpu_mask_dl); -dl_get_task_effective_cpus (p, msk); - cpu =cpumask_first_and (cpu_active_mask, msk); -BUG_ON (cpu >= nr_cpu_ids); - + cpu =dl_task_rebuild_cpu (p);raw_spin_lock (&dl_departed_lock); +if (cpu >= nr_cpu_ids &&list_empty (&p->dl.dl_departed_node)) {
kernel/sched/deadline.c void dl_add_task_root_domain(struct task_struct *p) · line 4074
Quoted 10 lines
kernel/sched/deadline.c +10
-0+/* +* No active CPU can count the bandwidth of this task. Leave it +* out of this rebuild. dl_bw_gen then keeps its release from +* removing anything. A later rebuild, or +* dl_task_offline_migration(), counts it again. Do not leave +* out a departed task: the code below counts it on the CPU +* recorded, if that CPU is active. +*/ + goto unlock; + }
[Severity: Medium]
When a rebuild leaves a sleeping task out because its allowed isolated CPUs went offline, p->dl.dl_bw_gen stays behind dl_bw_gen while p->dl.dl_bw_cpu still points to an offline CPU whose runqueue is attached to def_root_domain.
If the sleeping task is then moved via cpuset_attach to a cpuset with active CPUs in def_root_domain (such as an active isolcpus="domain" CPU or an isolated cpuset partition), can __dl_task_bw_move_cpu() and set_cpus_allowed_dl() skip accounting its bandwidth in def_root_domain?
Neither function checks that p->dl.dl_bw_gen == dl_bw_gen before treating p->dl.dl_bw_cpu's root domain as already counting the task:
kernel/sched/deadline.c:__dl_task_bw_move_cpu() {
...
cpu = dl_task_root_domain_cpu(p);
if (cpumask_intersects_and(cpu_rq(cpu)->rd->span, new_mask,
cpu_active_mask))
return -1;
...
}kernel/sched/deadline.c:set_cpus_allowed_dl() {
...
if (p->dl.dl_bw && !dl_entity_is_special(&p->dl) &&
cpu_active(ctx->dl_bw_cpu) &&
cpumask_test_cpu(ctx->dl_bw_cpu, ctx->new_mask) &&
cpu_rq(src_cpu)->rd != cpu_rq(ctx->dl_bw_cpu)->rd) {
...
}Because def_root_domain.span contains both the stale offline dl_bw_cpu and the active destination CPUs in def_root_domain, __dl_task_bw_move_cpu() returns -1 (and set_cpus_allowed_dl() sees matching root domains), so cpuset_can_attach() skips admission control and the task's bandwidth remains uncounted in def_root_domain.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
[email protected]
AI reviewRe: [PATCH v13 22/36] cgroup/cpuset: Refuse to take a deadline task out of its root domain
In reply to Mathieu DesnoyersThank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] cgroup/cpuset: unchecked CPU return on isolated partition invalidation - [Medium] cgroup/cpuset…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Medium] cgroup/cpuset: unchecked CPU return on isolated partition invalidation
- [Medium] cgroup/cpuset: missing root-domain check when shrinking partitions
- [Low] cgroup/cpuset: missing early dl_tasks_hold_bw() guard before cgroup walks
- [Low] cgroup/cpuset: missing subtree pruning on unchanged effective_cpus
--
commit 024194fef53c8203e8e662dd5e236d8d4627f15b
Author: Mathieu Desnoyers <[email protected]>
cgroup/cpuset: Refuse to take a deadline task out of its root domain
This commit refuses cpuset cpumask updates and invalidates or rejects partition updates that would leave a SCHED_DEADLINE task with no CPUs in the root domain accounting for its bandwidth.
Quoted 4 lines
kernel/cgroup/cpuset.c +0
-0
[ ... ]
Quoted 12 lines
kernel/cgroup/cpuset.c +8
-0@@ -1126,6 +1127,42 @@ 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 the rebuild takes out of +* the default root domain. Called under rcu_read_lock(), which it drops +* and takes again. +*/
kernel/cgroup/cpuset.c static void update_partition_sd_lb(struct cpuset *cs, int old_prs) · line 1147
Quoted 10 lines
kernel/cgroup/cpuset.c +10
-0+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;
[Severity: Low]
Could dl_tasks_hold_bw() be checked on entry to dl_tasks_keep_without() and dl_tasks_keep_root_domain() instead of only inside dl_tasks_keep_cpus()? Because cpuset_mutex prevents any task from acquiring deadline bandwidth during the check, testing it only at the leaf causes every cpuset mask and partition update to walk all descendant cpusets and recompute effective_cpus_below() even when no task in the system holds deadline bandwidth.
Quoted 52 lines
kernel/cgroup/cpuset.c +45
-0+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 +1185,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) +{
[ ... ]
Quoted 6 lines
kernel/cgroup/cpuset.c +6
-0+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 ; + }
kernel/cgroup/cpuset.c static void effective_cpus_below(struct cpuset *cp, struct cpuset *from, · line 1275
Quoted 10 lines
kernel/cgroup/cpuset.c +10
-0+/* 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 ;
[Severity: Low]
When cpumask_equal(cp->effective_cpus, new_cpus) is true here, in the second loop below, and in dl_tasks_keep_root_domain(), should the loop advance pos_css = css_rightmost_descendant(pos_css) before continuing? Since valid child partitions are already skipped and non-partition descendants derive their effective_cpus only from their parent's effective_cpus and their own unchanged cpus_allowed, no descendant in cp's subtree can change effective_cpus, yet a plain continue walks every descendant in unaffected subtrees and runs effective_cpus_below() on each of them.
[ ... ]
Quoted 10 lines
kernel/cgroup/cpuset.c +6
-0@@ -2035,6 +2194,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.
kernel/cgroup/cpuset.c static int update_parent_effective_cpumask(struct cpuset *cs, int cmd, · line 2212
Quoted 10 lines
kernel/cgroup/cpuset.c +9
-0+* 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); }
[Severity: Medium]
When an update to a valid PRS_ISOLATED partition invalidates it, can returning all of cs->effective_xcpus to a non-isolated parent move a deadline task out of the default root domain without being caught by dl_tasks_keep_given_back()?
dl_tasks_keep_given_back() only checks the CPUs in cs->effective_xcpus & ~trialcs->effective_xcpus before partition_cpus_change() runs. If a valid isolated partition holding a housekeeping CPU is grown (or otherwise updated without dropping that housekeeping CPU in trialcs), dl_tasks_keep_given_back() returns true, and if partition_cpus_change() then invalidates cs (for example via PERR_DLTASK or PERR_NOCPUS here or in remote_cpus_update(), or via validate_partition() or prstate_housekeeping_conflict()), all of cs->effective_xcpus are returned to the parent and taken out of the default root domain on the sched domain rebuild, allowing a deadline task counted in the default root domain to move to the parent's root domain without admission control.
[ ... ]
Quoted 15 lines
kernel/cgroup/cpuset.c +15
-0+/* +* Return false if @cs gives housekeeping CPUs back to its parent, and a +* deadline task would then leave the default root domain: see +* dl_tasks_keep_without(). @cs is checked only if it is a valid isolated +* partition root, and its parent is not isolated. Making @cs invalid +* would not help: it would give these CPUs back all the same, and the +* task would still leave. So the caller refuses the write to cpuset.cpus +* or cpuset.cpus.exclusive. The CPUs that @cs takes in the same change +* count too. If @cs gives back only CPUs that isolcpus="domain" isolates, +* they stay in the default root domain. update_parent_effective_cpumask() +* then checks the CPUs that @cs takes. +*/ +static bool dl_tasks_keep_given_back (struct cpuset *cs,struct cpuset *trialcs, +struct tmpmasks *tmp) +{
kernel/cgroup/cpuset.c static void partition_cpus_change(struct cpuset *cs, struct cpuset *trialcs, · line 2778
Quoted 10 lines
kernel/cgroup/cpuset.c +10
-0+struct cpuset *parent =is_remote_partition (cs) ? &top_cpuset :parent_cs (cs); + +if (cs->partition_root_state !=PRS_ISOLATED || + parent->partition_root_state ==PRS_ISOLATED || + !cpumask_andnot (tmp->delmask, cs->effective_xcpus, trialcs->effective_xcpus) || + !cpumask_intersects (tmp->delmask,housekeeping_cpumask (HK_TYPE_DOMAIN_BOOT ))) +return true; +cpumask_andnot (tmp->addmask, trialcs->effective_xcpus, cs->effective_xcpus); +return dl_tasks_keep_without (parent, cs, tmp->addmask, tmp->delmask, false); +}
[Severity: Medium]
Can shrinking a partition still move a deadline task in the parent's hierarchy out of its root domain in cases that dl_tasks_keep_given_back() skips?
When a partition only gives CPUs back without taking new ones, update_parent_effective_cpumask() and remote_cpus_update() do not call dl_tasks_keep_without() because deleting (or adding in remote_cpus_update()) is false. As a result, three shrink paths bypass the root-domain check:
1. A valid PRS_ROOT partition shrinks and returns a housekeeping CPU to
top_cpuset: a deadline task in top_cpuset (or a member descendant) counted in the default root domain on an isolcpus="domain" CPU, whose user_cpus_ptr or child cpuset.cpus contains only the returned housekeeping CPU, becomes restricted to that CPU and moves to top_cpuset's root domain.
2. Symmetrically, a valid PRS_ISOLATED partition shrinks and returns only an isolcpus="domain" CPU to top_cpuset: a deadline task counted in top_cpuset's housekeeping root domain whose user_cpus_ptr or child cpuset.cpus contains only the returned isolated CPU moves to the default root domain.
3. Shrinking cpuset.cpus or cpuset.cpus.exclusive on an ancestor cpuset shrinks a descendant PRS_ISOLATED partition during update_cpumasks_hier(), returning housekeeping CPUs to its non-isolated parent without dl_tasks_keep_given_back() checking the descendant partition.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
[email protected]
AI reviewRe: [PATCH v13 30/36] selftests/sched: Test parameter changes in deadline_bw_test
In reply to Mathieu DesnoyersThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] selftests/sched: dead test modes in pi_asleep(), deboost_case(), and helpers --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests/sched: dead test modes in pi_asleep(), deboost_case(), and helpers
--
commit 3c3e678183963bfa600f0de3703c72db233a7225
Author: Mathieu Desnoyers <[email protected]>
selftests/sched: Test parameter changes in deadline_bw_test
This commit adds 24 parameter-change and constrained-gap test cases to deadline_bw_test and waits for bandwidth and debt release between cases in run_p_cases().
Quoted 4 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +0
-0
[ ... ]
Quoted 16 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +7
-3@@ -893,30 +929,50 @@ static void *sleeping_owner(void *arg)/* * A SCHED_OTHER owner takes pi_lock and sleeps 400ms while holding it. -* The main thread then waits 20ms for the lock, 5 times. For each wait it -* becomes a deadline task of 5ms every 50ms. Each wait boosts the +* The main thread then waits 20ms for the lock, @rounds times. For each +* wait it becomes a deadline task of 5ms every 50ms. Each wait boosts the * sleeping owner, and its timeout deboosts it. Every wait must time out. +* +* With @was_dl, the owner was a deadline task of 30ms every 100ms before. +* It ran 1ms and slept. The main thread switched it to SCHED_OTHER 200ms +* later, past its 0-lag time. */ -static int pi_other_owner_asleep (void ) +static int pi_asleep (int was_dl,int rounds)
[ ... ]
Quoted 36 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +19
-3+static int pi_other_owner_asleep (void ) +{ +return pi_asleep (0 ,5 ); +} +/* * Run as the owner thread. Become a deadline task of 30ms every 100ms, * and take pi_lock. Hold it for 300ms, spinning 2ms and sleeping 5ms in @@ -1632,9 +1693,9 @@ static atomic_int owner_first_period_ms;static _Atomicpid_t donor_tid;/* -* Run as the owner O of deboost_resets_period(). Store the CPU time of -* step 4, in ms, in owner_first_period_ms. Return NULL, 1 on an error, or -* 2 if no waiter boosted it within 1s. +* Run as the owner O of deboost_case(). @arg is its @mode. Store the CPU +* time of step 4, in ms, in owner_first_period_ms. Return NULL, 1 on an +* error, or 2 if no waiter boosted it within 1s. */ static void *deboosted_owner (void *arg) {@@ -1642,6 +1703,17 @@ static void *deboosted_owner(void *arg)int err; owner_tid =gettid_ (); +if (arg == (void *)1L ) { + err =set_deadline (0 ,60 *NSEC_PER_MSEC ,PERIOD ); +if (err) { +ksft_print_msg ("the owner refused as a deadline task: %s\n" , +strerror (-err)); +return (void *)1 ; + } +spin_ns (NSEC_PER_MSEC ); +sem_post (&owner_ready); +sem_wait_sig (&owner_go); + }
[ ... ]
Quoted 19 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +9
-3+/* Run deboost_case() with a plain SCHED_OTHER owner. */ +static int deboost_resets_period (void ) +{ +return deboost_case (0 ); +} +/* * Return the runtime that the deadline task @tid has left, in ns, from * dl.runtime in /proc/<tid>/sched. Return -1 if it cannot be read. @@ -1779,14 +1891,16 @@ static long long dl_runtime_left(pid_t tid)#define REFILL_DONOR_RUNTIME (2 * NSEC_PER_MSEC)#define REFILL_DONOR_PERIOD (10 * NSEC_PER_MSEC) -static sem_t refill_donor_ready; -static atomic_int refill_donor_locking; -static _Atomiclong long refill_owner_left; +static sem_t refill_donor_ready, refill_measured; +static atomic_int refill_donor_locking, refill_hold_donor; +static _Atomiclong long refill_owner_left, refill_owner_read_ms;
[ ... ]
Quoted 26 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +26
-0+/* Run refill_case() with an owner that stays on its own runtime. */ +static int deboost_keeps_own_runtime (void ) +{ +return refill_case (1 ); +} + +static sem_t ret_ready, ret_go; +static _Atomicpid_t ret_tid; +static atomic_int ret_first_period_ms; + +/* +* Run as the thread of the return cases. Become a deadline task of 50ms +* every 100ms and run 20ms. 30ms of runtime are left, and the 0-lag time +* is about 20ms ahead. Then change to new parameters, as @arg says: +* - 0: the thread switches itself to SCHED_OTHER and back to +* SCHED_DEADLINE, while it runs. +* - 1: the thread sleeps. The main thread switches it to SCHED_OTHER and +* back. +* - 2: the thread changes its own parameters while it runs. It stays in +* SCHED_DEADLINE. +* - 3: as 2, while a donor boosts the thread. The thread takes pi_lock and +* wakes the donor up. The donor blocks on pi_lock. The thread then +* changes its parameters, and unlocks. +* - 4: as 2, to a constrained deadline: 8ms in 20ms, every 100ms. +* - 5: the thread sleeps. The main thread changes it to 8ms in 20ms, +* every 100ms.
[ ... ]
Quoted 27 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +27
-0+static int return_running_new_params (void ) +{ +return return_case (0 ); +} + +/* The same, for a task that leaves and comes back while it sleeps. */ +static int return_asleep_new_params (void ) +{ +return return_case (1 ); +} + +/* The same, for a task that changes its parameters within the class. */ +static int change_running_new_params (void ) +{ +return return_case (2 ); +} + +/* +* A running task of 50ms every 100ms has 30ms left. It changes itself to +* 8ms in 20ms, every 100ms. Its deadline stays 80ms ahead, further than +* the new 20ms. The check of the runtime left uses the new density over +* those 80ms. That allows 32ms, so the check keeps the 30ms. The task +* must keep no more than its new 8ms. +*/ +static int change_constrained_new_params (void ) +{ +return return_case (4 );
tools/testing/selftests/sched/deadline/deadline_bw_test.c static int return_asleep_new_params(void) · line 2286
Quoted 10 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +10
-0+} + +/* +* The same, for a task that the main thread changes while it sleeps. The +* check is at its wakeup. +*/ +static int wake_constrained_new_params (void ) +{ +return return_case (5 ); +}
[Severity: Low]
Are several new test modes in pi_asleep(), deboost_case(), refill_case(), and return_case() missing wrappers and entries in p_cases[]?
This patch adds code and documentation for five scenarios that are never called:
- pi_asleep() adds a was_dl != 0 branch, but is only called as pi_asleep(0, 5) in pi_other_owner_asleep().
- deboost_case() and deboosted_owner() add modes 1 and 2, but deboost_resets_period() only calls deboost_case(0). Also, in mode 1 of deboosted_owner(), if set_deadline() fails, the thread returns without posting owner_ready, so deboost_case(1) would wait for the full 5s timeout in sem_wait_for(&owner_ready, 5000).
- refill_case() adds a spin_ms == 8 branch and refill_donor() adds refill_hold_donor and refill_measured, but deboost_keeps_own_runtime() only calls refill_case(1) and the two new globals are never initialized or set.
- return_case() and returner() add mode 3, while wrappers and p_cases[] entries are only added for modes 0, 1, 2, 4, and 5.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=30
[email protected]
AI reviewRe: [PATCH v13 31/36] sched/deadline: Count a task that departs while a rebuild walks the tasks
In reply to Mathieu DesnoyersThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] sched/deadline: dl_task_depart() ignores cpus_mask and dl_bw_def during walk --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] sched/deadline: dl_task_depart() ignores cpus_mask and dl_bw_def during walk
--
commit 7998428425c31015887f4856ff0afd652b3061f2
Author: Mathieu Desnoyers <[email protected]>
sched/deadline: Count a task that departs while a rebuild walks the tasks
This commit counts the bandwidth of a task in dl_task_depart() when it leaves SCHED_DEADLINE during a root domain accounting rebuild after dl_clear_root_domains() has run and before the task-list walk reaches it.
Quoted 4 lines
kernel/sched/deadline.c +0
-0
[ ... ]
Quoted 14 lines
kernel/sched/deadline.c +10
-0@@ -400,6 +419,24 @@ static void dl_task_depart(struct task_struct *p)if (dl_se->dl_bw_gen == dl_bw_gen && !dl_bw_rebuilding () &&cpu_active (cpu) &&cpu_rq (cpu)->rd ==cpu_rq (dl_se->dl_bw_cpu)->rd)dl_record_bw_cpu (dl_se, cpu); +/* +* dl_clear_root_domains() counted the departed tasks, and the walk +* of the task list counts the deadline tasks. A task that departs +* after the clear, before the walk reaches it, would be counted by +* neither. Count it here, as the walk would have. Use the CPU it +* departs from, or else the CPU recorded, or else an active CPU of +* its mask. If none is active, leave it out. While the sched domains +* are rebuilt, the clear is still ahead, and finds the task on the +* list. +*/
kernel/sched/deadline.c static void dl_task_depart(struct task_struct *p) · line 441
Quoted 10 lines
kernel/sched/deadline.c +8
-0+if (dl_bw_move_blocked (p) && !dl_bw_rebuilding ()) { +if (!cpu_active (cpu)) + cpu = dl_se->dl_bw_cpu; +if (!cpu_active (cpu)) + cpu =cpumask_first_and (cpu_active_mask, &p->cpus_mask); +if (cpu < nr_cpu_ids) +dl_task_count_bw (p, cpu); + }list_add (&dl_se->dl_departed_node, &dl_departed_list); }
[Severity: Medium]
Can dl_task_depart() charge the departing task's bandwidth to the wrong root domain when task_cpu(p) or dl_se->dl_bw_cpu is still active but no longer in p->cpus_mask?
When p is sleeping while holding an rt_mutex, a cpuset partition change updates p->cpus_mask without migrating p off task_cpu(p). Unlike dl_task_rebuild_cpu(), which intersects candidate CPUs for a non-queued task with p->cpus_mask and matches dl_se->dl_bw_def, dl_task_depart() only checks cpu_active(cpu):
kernel/sched/deadline.c:dl_task_rebuild_cpu() {
...
cpumask_and(msk, &p->cpus_mask, cpu_active_mask);
cpu = task_cpu(p);
if (cpu_active(cpu) && task_on_rq_queued(p) &&
(cpu == recorded || !cpumask_test_cpu(cpu, &p->cpus_mask)))
return cpu;
...
if (recorded >= 0) {
bool def = READ_ONCE(p->dl.dl_bw_def);
unsigned int same;
if (cpumask_test_cpu(cpu, msk) && dl_cpu_in_def(cpu) == def)
return cpu;
if (cpumask_test_cpu(recorded, msk) && dl_cpu_in_def(recorded) == def)
return recorded;
...
}If the partition update that triggered dl_rebuild_rd_accounting() moved task_cpu(p) into another partition and removed it from p->cpus_mask, task_cpu(p) remains active in that other root domain. If p is deboosted before the task-list walk reaches it, dl_task_depart() picks task_cpu(p) and adds p->dl.dl_bw to the old CPU's new root domain instead of p's root domain until the 0-lag timer fires, leaving p's root domain under-counted for sched_setattr() admission and the other root domain over-counted.
When no active CPU is found (cpu >= nr_cpu_ids), this hunk also leaves dl_se->dl_bw_skip_gen unset, though commit "sched/deadline: Count again a departed task that a boost brings back" later in this series updates that case.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=31
[email protected]
AI reviewRe: [PATCH v13 32/36] sched/deadline: Count again a departed task that a boost brings back
In reply to Mathieu DesnoyersThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] sched/deadline: dl_task_undepart() misses task asleep on inactive CPU --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] sched/deadline: dl_task_undepart() misses task asleep on inactive CPU
--
commit f3cb39450dc2032dac32f84a5084f9b20d902cad
Author: Mathieu Desnoyers <[email protected]>
sched/deadline: Count again a departed task that a boost brings back
This commit re-accounts the bandwidth of a departed task when priority inheritance boosts it back into SCHED_DEADLINE after a root-domain rebuild dropped its bandwidth, and marks departed tasks left out during a rebuild in dl_bw_skip_gen.
Quoted 4 lines
kernel/sched/deadline.c +0
-0
[ ... ]
Quoted 30 lines
kernel/sched/deadline.c +12
-3@@ -436,16 +436,25 @@ static void dl_task_depart(struct task_struct *p) cpu =cpumask_first_and (cpu_active_mask, &p->cpus_mask);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); }/* * 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. +* its bandwidth is that of a deadline task again. @cpu is the CPU of @p. +* 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 @cpu does again once @p is +* back. Count it in the root domain of @cpu, as a move of @p would. If +* the walk of the task list after a rebuild has not reached @p yet, +* leave @p to the walk. If @cpu is not active, leave @p to its move to +* an active CPU. */ -static void dl_task_undepart (struct task_struct *p) +static void dl_task_undepart (struct task_struct *p,int cpu) {struct sched_dl_entity *dl_se = &p->dl;
kernel/sched/deadline.c static void dl_task_undepart(struct task_struct *p) · line 469
Quoted 7 lines
kernel/sched/deadline.c +3
-0@@ -454,6 +463,9 @@ 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) && +cpu_active (cpu)) +dl_task_count_bw (p, cpu); }
[Severity: Medium]
Can dl_task_undepart() skip dl_task_count_bw() or count the bandwidth in the wrong root domain when p is still asleep on the CPU that went offline or left p->cpus_mask?
affine_move_task() only migrates queued tasks, so if a departed task holding an rt_mutex stays asleep on CPU X while X goes offline, task_cpu(p) remains X while dl_clear_root_domains() drops its bandwidth. When a deadline waiter then boosts p before p wakes up, switched_to_dl() passes cpu_of(rq) (X) to dl_task_undepart(), where cpu_active(X) is false, leaving p's bandwidth uncounted in total_bw until p wakes up (or for the whole boost if the waiter unblocks first), allowing sched_setattr() to admit tasks past the root domain's capacity. Similarly, if p was moved to another cpuset while asleep on an active CPU X, cpu_active(X) is true even though X is no longer in p->cpus_mask, so dl_task_count_bw() charges X's root domain instead of the root domain of p->cpus_mask.
Should dl_task_undepart() resolve an active CPU in p->cpus_mask when @cpu is inactive or outside p->cpus_mask, the way dl_task_admit_cpu() does with cpumask_first_and(cpu_active_mask, &p->cpus_mask)?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=32
[email protected]
AI reviewRe: [PATCH v13 35/36] selftests/sched: Test PI against hotplug and rebuilds in deadline_bw_test
In reply to Mathieu DesnoyersThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] selftests/sched: out-of-bounds read in pi_deboost_during_rebuild() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests/sched: out-of-bounds read in pi_deboost_during_rebuild()
--
commit 03c5e3bed0b33b1aacdfd04e920a11ba863127a5
Author: Mathieu Desnoyers <[email protected]>
selftests/sched: Test PI against hotplug and rebuilds in deadline_bw_test
This commit adds the pi_departed_offline_boost and pi_deboost_during_rebuild test cases to deadline_bw_test to test priority inheritance against CPU hotplug and bandwidth accounting rebuilds.
Quoted 4 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +0
-0
[ ... ]
tools/testing/selftests/sched/deadline/deadline_bw_test.c static int affinity_vs_setattr(void) · line 12766
Quoted 13 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +12
-0@@ -12596,6 +12755,320 @@ static int affinity_vs_setattr(void) +static int pi_deboost_during_rebuild (void ) +{ +int cpu[2 ] = {top_cpu (4 ),top_cpu (3 ) }, q_root =1 , ret =KSFT_FAIL , i; +long long base, bw_t, bw_w, tb; +char cg_r[PATH_MAX +32 ], cpus[32 ]; +pid_t filler =-1 , proc =-1 ; +uint64_t end, d, dmax =0 ; + +if (ncpus <5 ) { +ksft_print_msg ("needs 5 non-isolated CPUs\n" ); +return KSFT_SKIP ; + }
[Severity: Low]
Can the cpu[2] initializer read out of bounds before the ncpus < 5 check?
top_cpu(n) returns online[ncpus - n], and main() runs p_cases[] as long as ncpus >= 3. On a machine with 3 non-isolated CPUs, top_cpu(4) reads online[-1] before the ncpus < 5 check skips the test.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=35
[email protected]
AI reviewRe: [PATCH v13 33/36] sched/deadline: Keep a reaped deadline task in a rebuild
In reply to Mathieu DesnoyersThank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] sched/deadline: stale dl_bw_cpu when dl_task_depart() skips queued task - [Medium] sched/deadline…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] sched/deadline: stale dl_bw_cpu when dl_task_depart() skips queued task
- [Medium] sched/deadline: rebuild drops bandwidth of active task on dl_departed_list
- [Medium] sched/deadline: lockless dl_bw check in dl_task_unhash() races with setattr
--
commit 4c28f4a343df6c19b2f8f610ef54bf2893d243f4
Author: Mathieu Desnoyers <[email protected]>
sched/deadline: Keep a reaped deadline task in a rebuild
This commit adds dl_task_unhash() to place a deadline task on dl_departed_list in release_task() before unhashing it, and keeps exited tasks on dl_departed_list in dl_task_undepart().
Quoted 4 lines
kernel/sched/deadline.c +0
-0
[ ... ]
Quoted 9 lines
kernel/sched/deadline.c +5
-0@@ -442,6 +443,27 @@ static void dl_task_depart(struct task_struct *p)list_add (&dl_se->dl_departed_node, &dl_departed_list); } +/* +* Put @p on dl_departed_list if it is a deadline task with a bandwidth. +* release_task() calls this before it takes @p off the task list, so +* that a rebuild still counts @p until its last schedule. That schedule, +* or the inactive timer after it, takes @p off the list. The caller holds
kernel/sched/deadline.c static void dl_task_depart(struct task_struct *p) · line 460
Quoted 10 lines
kernel/sched/deadline.c +10
-0+* tasklist_lock for writing: no walk of the task list runs meanwhile. +*/ +void dl_task_unhash (struct task_struct *p) +{ +struct rq_flags rf; +struct rq *rq; + +lockdep_assert_held_write (&tasklist_lock); +if (!READ_ONCE (p->dl.dl_bw)) +return ;
[Severity: Medium]
Can the lockless check of p->dl.dl_bw race with __sched_setscheduler() admitting p to SCHED_DEADLINE?
__sched_setscheduler() checks p->exit_state under task_rq_lock() without holding tasklist_lock, and only assigns p->dl.dl_bw later in __setparam_dl():
CPU0 CPU1__sched_setscheduler(p)
task_rq_lock(p)
if (p->exit_state) // 0
sched_dl_overflow()
do_exit()
exit_notify()
write_lock_irq(&tasklist_lock)
p->exit_state = EXIT_DEAD
release_task(p)
dl_task_unhash(p)
!READ_ONCE(p->dl.dl_bw)
--> returns early
__exit_signal()
__unhash_process()
__setparam_dl()
dl_se->dl_bw = ... task_rq_unlock(p)
p is then off the task list and not on dl_departed_list while holding deadline bandwidth, so a root domain rebuild before p's final schedule drops its bandwidth from total_bw.
kernel/sched/deadline.c static void dl_task_depart(struct task_struct *p) · line 465
Quoted 5 lines
kernel/sched/deadline.c +5
-0+ rq =task_rq_lock (p, &rf); +if (dl_task (p) &&dl_task_holds_bw (p)) +dl_task_depart (p); +task_rq_unlock (rq, p, &rf); +}
[Severity: Medium]
If p migrates to another active CPU in the same root domain between release_task() and its final schedule in do_task_dead(), does dl_task_depart() leave a stale p->dl.dl_bw_cpu?
Because the migration stays within the same root domain, dl_task_bw_elsewhere() is false and dl_task_move_bw() does not update dl_bw_cpu. When p later reaches do_task_dead() and task_non_contending() calls dl_task_depart(p), p is already on dl_departed_list from dl_task_unhash(), so dl_task_depart() returns early before reaching dl_record_bw_cpu():
kernel/sched/deadline.c:dl_task_depart() {
...
if (!list_empty(&dl_se->dl_departed_node) || dl_se->dl_bw_cpu < 0)
return;
...
if (dl_se->dl_bw_gen == dl_bw_gen && !dl_bw_rebuilding() && cpu_active(cpu) &&
cpu_rq(cpu)->rd == cpu_rq(dl_se->dl_bw_cpu)->rd)
dl_record_bw_cpu(dl_se, cpu);If the old dl_bw_cpu goes offline before p's 0-lag time while the CPU p actually departed from remains active, dl_clear_root_domains() sees an inactive dl_bw_cpu and drops p's bandwidth.
Quoted 4 lines
kernel/sched/deadline.c +0
-0@@ -462,7 +484,12 @@ static void dl_task_undepart(struct task_struct *p, int cpu)if (list_empty (&dl_se->dl_departed_node))return ;guard (raw_spinlock)(&dl_departed_lock);
kernel/sched/deadline.c static void dl_task_undepart(struct task_struct *p, int cpu) · line 495
Quoted 10 lines
kernel/sched/deadline.c +6
-1-list_del_init (&dl_se->dl_departed_node); +/* +* Keep a task that has exited on the list. It may be off the task list +* already (dl_task_unhash()), and a rebuild must still count it. +*/ +if (!p->exit_state) +list_del_init (&dl_se->dl_departed_node);if (dl_se->dl_bw_gen != dl_bw_gen && !dl_bw_move_blocked (p) &&cpu_active (cpu))dl_task_count_bw (p, cpu);
[Severity: Medium]
Can keeping an active exited task on dl_departed_list cause a rebuild to drop its bandwidth while it is still running in SCHED_DEADLINE?
Both dl_clear_root_domains() and dl_add_task_root_domain() treat any task with !list_empty(&dl_se->dl_departed_node) as a departed task: they require dl_se->dl_bw_gen == dl_bw_gen - 1 and cpu_active(dl_se->dl_bw_cpu) instead of picking an active CPU via dl_task_rebuild_cpu(p).
This drops the bandwidth of an in-class task in two cases:
1. If an earlier rebuild dropped an exited task's departed bandwidth (dl_se->dl_bw_gen != dl_bw_gen) and the task is PI-boosted back into SCHED_DEADLINE while dl_bw_rebuilding() is true, dl_bw_move_blocked(p) prevents dl_task_undepart() from calling dl_task_count_bw(). When dl_clear_root_domains() and dl_add_task_root_domain() run next, both see !list_empty(&dl_se->dl_departed_node) with dl_se->dl_bw_gen != dl_bw_gen - 1 and skip the task.
2. If an exited task on dl_departed_list (via dl_task_unhash() or kept there by dl_task_undepart()) is running on an active CPU when its recorded dl_bw_cpu goes offline, dl_clear_root_domains() and dl_add_task_root_domain() check !cpu_active(dl_se->dl_bw_cpu) and drop its bandwidth even though the task is still running in the root domain.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=33
Re: [PATCH v13 01/36] sched/deadline: Hold a reference on the deadline donor of a boosted task
In reply to Mathieu DesnoyersHello,
Quoted 42 lines · Mathieu Desnoyers
A task boosted by a
SCHED_DEADLINEwaiter runs on the parameters of a donor, dl.pi_se. Since commit2279f540ea7d("sched/deadline: Fix priority inheritance with multiple scheduling classes"),rt_mutex_setprio()copies the donor of the top waiter to the owner. So each owner along a chain points at the deadline task at its head.
rt_mutex_setprio()keeps the top waiter,pi_top_task, as a plain pointer. The top waiter is blocked, so it cannot exit. The donor further up the chain has no such guarantee. When it stops waiting, the owners below it are fixed by chain walks. A walk can be preempted, and it runs in the context of the task that started it, not the donor's. So the donor can exit before the walk reaches the last owner.M, O and P are
SCHED_OTHERtasks. W is aSCHED_DEADLINEtask:P O M W
locks L3 locks L2 locks L1
blocks on L3
blocks on L2
blocks on L1
M, O, P get W's parameters
times out on L2
O deboosted
walk from O: preempted
times out on L1
M not blocked: no walk
exits
runs out of runtime
refill readsP->dl.pi_se:
W's freedtask_structP would run on parameters read from freed memory, until M's walk reaches it. Setting up the deadline task needs
CAP_SYS_NICE. The timeout of aFUTEX_LOCK_PIneeds no privilege. This was found by reading.Fix this by holding a reference on the donor's task, for each task whose donor is another task.
dl_set_donor()gives a task the donor of its top waiter.dl_clear_donor()ends the boost. Both move the reference. A late walk can still leave P on the parameters of an exited donor for a while. P now reads them from valid memory.
...
Quoted 43 lines
kernel/sched/sched.h +43
-0+/* Reads of the donor of a waiter before the lookup falls back. */ +#define DL_DONOR_TRIES 8 + +/* +* Make the deadline donor of the waiter @pi_task the donor of @dl_se. A +* boosted entity holds a reference on the task of its donor. The donor +* is the deadline task at the head of the chain. It can stop waiting +* and exit before the chain walk reaches @dl_se: that walk can be +* preempted. +* +* The donor of @pi_task is read without the locks of @pi_task. Another +* CPU can change it meanwhile, and put the last reference on the old +* donor. That CPU stores the new donor first: read it again then. The +* old task_struct stays readable while the rq lock is held: +* put_task_struct() frees it through call_rcu(), and the rq lock holds +* off the grace period. +* +* Each failed tryget_task_struct() means that another CPU stored a newer +* donor. This holds as long as every writer of pi_se holds a reference +* on the new donor. A writer that breaks this rule could leave a dead +* donor in pi_se, and every read would fail on it. So stop after +* DL_DONOR_TRIES reads, rather than spin forever with interrupts off. +* Warn, and fall back to @pi_task itself. It waits on a lock, so it +* cannot exit. If it has no parameters of its own, +* replenish_dl_entity() handles it as a donor that left the class. +* +* The caller holds the rq lock and the pi_lock of the task of @dl_se. +*/ +static inline void dl_set_donor (struct sched_dl_entity *dl_se, +struct task_struct *pi_task) +{ +struct sched_dl_entity *pi_se; +int tries; + +for (tries =0 ; tries <DL_DONOR_TRIES ; tries++) { + pi_se =READ_ONCE (pi_task->dl.pi_se); +if (pi_se == dl_se->pi_se) +return ; +if (pi_se == dl_se || + tryget_task_struct(container_of(pi_se, struct task_struct, dl))) + goto set; +/* Pairs with the ordering of refcount_dec_and_test(). */ +smp_rmb ();
kernel/sched/sched.h extern int sched_dl_global_validate(void); · line 427
Quoted 10 lines
kernel/sched/sched.h +10
-0+ } +WARN_ON_ONCE (1 ); + pi_se = &pi_task->dl; +if (pi_se == dl_se->pi_se) +return ; +if (pi_se != dl_se) +get_task_struct (pi_task); +set: +dl_replace_donor (dl_se, pi_se); +}
This is what I had in mind, and the patch looks OK to me. Wonder what Peter/others think of it, but I can give
Thanks,
Juri
Re: [PATCH v13 05/36] sched/deadline: Keep a departing constrained task until its next period
In reply to [email protected]Quoted 2 lines
Can a throttled constrained task that leaves
SCHED_DEADLINEend up keeping its bandwidth and deadline for an extra unused period?
Yes. At the end of the period, dl_task_timer() replenishes the departed task, and starts its next period. The task then keeps its bandwidth until the end of that period. That is too long, but safe: a task that comes back before its new deadline runs in that period. Sashiko found the same case in its v12 review of "sched/deadline:
Cancel the replenishment timer with the parameters". v14 will leave it as it is.
Quoted 2 lines
Can
inactive_task_timer()release a departing task's bandwidth before its 0-lag time whentask_non_contending()takes the pending early return?
Yes. The handler can wait for the rq lock while the task wakes up, runs in a new period and leaves the class. It then acts on a time computed from the old period. This is not new: v7.4-rc2 also releases the bandwidth at once in this window. In v14, the handler will compute the 0-lag time again. For a constrained task, it will also compute the next period again, and wait for the later of the two. It will call dl_next_period() only when dl_se->deadline is not 0, as task_non_contending() does.
Thanks,
Mathieu
Re: [PATCH v13 06/36] sched/deadline: Keep departing bandwidth across a root domain rebuild
In reply to [email protected]Quoted 4 lines
Can
dl_clear_root_domains()(and thedl_departed_cpubranch indl_add_task_root_domain()) charge departed bandwidth to the wrong root domain whendl_se->dl_departed_cpuis split into a new child cpuset partition before the 0-lag timer fires?
Yes, until the 0-lag time of the departed tasks. The v12 review of this patch found the same case. The v13 cover lists it as a known limit, and v14 will keep it there.
Quoted 4 lines
When a departed task returns to
SCHED_DEADLINEin the same root domain before its 0-lag time, does deferring the oldp->dl.dl_bwsubtraction todl_task_release_bw()afterraw_spin_unlock(&dl_b->lock)double-count the task's bandwidth?
Yes. The admission check in sched_dl_overflow() still counts the old bandwidth, as on v7.4-rc2. In a full root domain, sched_setattr() can then fail with EBUSY. On success, both bandwidths count until dl_task_release_bw() removes the old one, right after the check. The v12 review of this patch found the same case. The v13 cover lists it as a known limit, and v14 will keep it there.
Thanks,
Mathieu
Re: [PATCH v13 18/36] sched/deadline: Make a rebuild count a task in the root domain it runs in
In reply to [email protected]Quoted 9 lines
When a rebuild leaves a sleeping task out because its allowed isolated CPUs went offline,
p->dl.dl_bw_genstays behinddl_bw_genwhilep->dl.dl_bw_cpustill points to an offline CPU whose runqueue is attached todef_root_domain.If the sleeping task is then moved via
cpuset_attachto a cpuset with active CPUs indef_root_domain(such as an active isolcpus="domain" CPU or an isolated cpuset partition), can__dl_task_bw_move_cpu()andset_cpus_allowed_dl()skip accounting its bandwidth indef_root_domain?
Yes. In v14, __dl_task_bw_move_cpu() will not return -1 for a task with an old dl_bw_gen. cpuset_can_attach() will then reserve room in the destination, and set_cpus_allowed_dl() will count the bandwidth there.
Thanks,
Mathieu
Re: [PATCH v13 20/36] sched/deadline: Keep a task in the root domain that counts its bandwidth
In reply to [email protected]Quoted 2 lines
Should the
dl_departed_listloop indl_clear_root_domains()also calldl_record_bw_cpu(dl_se, cpu)when it re-counts a departed task's bandwidth?
Yes. In v14, dl_clear_root_domains() will call dl_record_bw_cpu() when it counts a departed task again. That records dl_bw_def too.
Thanks,
Mathieu
Re: [PATCH v13 22/36] cgroup/cpuset: Refuse to take a deadline task out of its root domain
In reply to [email protected]Quoted 7 lines
Could
dl_tasks_hold_bw()be checked on entry todl_tasks_keep_without()anddl_tasks_keep_root_domain()instead of only insidedl_tasks_keep_cpus()? Becausecpuset_mutexprevents any task from acquiring deadline bandwidth during the check, testing it only at the leaf causes every cpuset mask and partition update to walk all descendant cpusets and recomputeeffective_cpus_below()even when no task in the system holds deadline bandwidth.
It could, but the gain is small. dl_tasks_keep_cpus() already calls dl_tasks_hold_bw() first. So with no deadline bandwidth held, the walk of the tasks does not run. Only the walk of the cpusets stays. It runs only when the cpusets change: on a write to a cpuset, or when a CPU hotplug makes a partition valid again. update_cpumasks_hier() walks the same cpusets on a write. The v12 review asked the same. v14 will leave it as it is.
Quoted 9 lines
When
cpumask_equal(cp->effective_cpus, new_cpus)is true here, in the second loop below, and indl_tasks_keep_root_domain(), should the loop advancepos_css=css_rightmost_descendant(pos_css)before continuing? Since valid child partitions are already skipped and non-partition descendants derive theireffective_cpusonly from their parent'seffective_cpusand their own unchangedcpus_allowed, no descendant in cp's subtree can changeeffective_cpus, yet a plain continue walks every descendant in unaffected subtrees and runseffective_cpus_below()on each of them.
The same answer as above. The extra cost is a walk of the cpusets, and only when the cpusets change. v14 will leave it as it is.
Quoted 4 lines
When an update to a valid
PRS_ISOLATEDpartition invalidates it, can returning all ofcs->effective_xcpusto a non-isolated parent move a deadline task out of the default root domain without being caught bydl_tasks_keep_given_back()?
Yes. In v14, such a write will fail with EBUSY if a deadline task in the partition would then change root domain. To know whether the write makes the partition invalid, the check will repeat the tests of partition_cpus_change(). A new case, partition_invalidate_refused, will test it.
Quoted 3 lines
Can shrinking a partition still move a deadline task in the parent's hierarchy out of its root domain in cases that
dl_tasks_keep_given_back()skips?
Yes. In v14, a write to cpuset.cpus or cpuset.cpus.exclusive will fail with EBUSY if a deadline task would then leave its root domain. The check will cover these writes:
- A write that only gives CPUs back. It will check the tasks of the parent (paths 1 and 2).
- A write that takes away all the CPUs of a partition root below. It will check the tasks of that partition (path 3).
A write that only shrinks a partition root below is not checked. The tasks of that partition keep the CPUs left to it. New cases will test the refusals: partition_shrink_moves_refused, partition_shrink_isolated_moves_refused and partition_child_invalidate_refused.
Thanks,
Mathieu
Re: [PATCH v13 30/36] selftests/sched: Test parameter changes in deadline_bw_test
In reply to [email protected]Quoted 2 lines
Are several new test modes in
pi_asleep(),deboost_case(),refill_case(), andreturn_case()missing wrappers and entries in p_cases[]?
Yes. These modes are for the separate series on priority inheritance, which the v13 cover mentions. That series calls them. They stayed in this patch when that series was split out. v14 will move them to that series. The missing post of owner_ready in mode 1 goes with them.
Thanks,
Mathieu
Re: [PATCH v13 31/36] sched/deadline: Count a task that departs while a rebuild walks the tasks
In reply to [email protected]Quoted 3 lines
Can
dl_task_depart()charge the departing task's bandwidth to the wrong root domain whentask_cpu(p)ordl_se->dl_bw_cpuis still active but no longer inp->cpus_mask?
Yes. In v14, dl_task_depart() will count the task on the CPU that the walk of the task list would pick. A new helper, dl_task_count_cpu(), will pick it as dl_task_rebuild_cpu() does. Unlike the walk, it will not read the CPUs of the cpuset, which needs cpuset_mutex.
The remark on dl_bw_skip_gen holds for this patch alone. The next patch of the series sets it in that case.
Thanks,
Mathieu
Re: [PATCH v13 32/36] sched/deadline: Count again a departed task that a boost brings back
In reply to [email protected]Quoted 3 lines
Can
dl_task_undepart()skipdl_task_count_bw()or count the bandwidth in the wrong root domain when p is still asleep on the CPU that went offline or leftp->cpus_mask?
Yes, for both cases. In v14, dl_task_undepart() will count the task on the CPU that the walk of the task list would pick. For a task asleep, that is an active CPU of its mask. If no CPU of its mask is active, the move of the task to an active CPU will count it.
Thanks,
Mathieu
Re: [PATCH v13 33/36] sched/deadline: Keep a reaped deadline task in a rebuild
In reply to [email protected]Quoted 2 lines
Can the lockless check of
p->dl.dl_bwrace with__sched_setscheduler()admitting p toSCHED_DEADLINE?
Yes. In v14, dl_task_unhash() will test dl_bw under the rq lock. release_task() will then take the rq lock of each task it reaps.
Quoted 3 lines
If p migrates to another active CPU in the same root domain between
release_task()and its final schedule indo_task_dead(), doesdl_task_depart()leave a stalep->dl.dl_bw_cpu?
Yes. In v14, dl_task_depart() will record the CPU the task departs from, even for a task already on the list. Also, if the CPU recorded is inactive, dl_clear_root_domains() will no longer drop a task that still runs in the class. It will count it on the CPU that the walk of the task list would pick. It reads the task without its locks, so that CPU can be wrong. The bandwidth then stays in that root domain until the release, or the next rebuild.
Quoted 2 lines
Can keeping an active exited task on
dl_departed_listcause a rebuild to drop its bandwidth while it is still running inSCHED_DEADLINE?
Yes, for both cases. The second fix above covers the second case. For the first case, dl_task_undepart() in v14 will set dl_bw_skip_gen. That asks dl_clear_root_domains(), which ends the rebuild, to count the task.
Thanks,
Mathieu
Re: [PATCH v13 35/36] selftests/sched: Test PI against hotplug and rebuilds in deadline_bw_test
In reply to [email protected]Quoted 1 line
Can the cpu[2] initializer read out of bounds before the ncpus < 5 check?
Yes. In v14, pi_deboost_during_rebuild() will read the CPUs after the check.
Thanks,
Mathieu
Juri Lelli
maintainerRe: [PATCH v13 05/36] sched/deadline: Keep a departing constrained task until its next period
In reply to Mathieu DesnoyersHello,
On 01/12/26 12:43, Mathieu Desnoyers wrote:
...
Quoted 4 lines
Fix this by keeping the bandwidth and the deadline of a constrained task that leaves the class until its next period.
task_non_contending()andinactive_task_timer()wait for the later of the 0-lag time and the next period.
I went through the patch and I don't seem to have a correctness complaint about the code. But I find the patch somewhat hard to digest:
about 100 lines, half of them comments, and a 120 line changelog :).
While thinking about it I ended up with two fixes we seem to need anyway, and a question about the approach.
The fixes first; neither is new with this series.
1. check_dl_overrun() clears dl_overrun with a plain store, without the rq lock. On x86 that is a load and store of the whole DL bitfield word, so it can write back stale dl_throttled, dl_non_contending, dl_bw_oncpu, ... values. On !RT the window is a few instructions against another CPU holding the task's rq lock (sched_setattr(), rt_mutex_setprio()). On PREEMPT_RT the task work runs with interrupts on under an rtmutex siglock, so the task can be preempted between load and store and the stale value can stay in a register for a long time. Clearing it under task_rq_lock() should do (siglock -> pi_lock -> rq lock is already a valid order, from signal_wake_up_state()).
This is also the reason dl_inactive_pending is a bool at the end of the struct (8 bytes more per task at this point of the series)? With dl_overrun fixed, it can be a bit like the others.
2. sched_dl_overflow() admits a task coming back to SCHED_DEADLINE before its 0-lag time with old_bw = 0:
if (dl_policy(policy) && !task_has_dl_policy(p) &&
!__dl_overflow(dl_b, cap, 0, new_bw)) {
if (hrtimer_active(&p->dl.inactive_timer))
__dl_sub(dl_b, p->dl.dl_bw, cpus);so the new bandwidth has to fit on top of the old one it still holds, and a round trip with the same parameters can fail with -EBUSY in a full root domain. The in-class branch just below does discount p->dl.dl_bw. Discounting it when the timer is active (and, after your patch 6, when the recorded root domain is this dl_b) would make the two consistent, and would also close the window where patch 6 counts both.
Could you add these two as separate patches ahead of this one (or I can send them, if you prefer)?
Now the approach. IIUC, the only thing a returning constrained task needs is its deadline. A constrained task that sleeps in the class and wakes after its 0-lag time never gets a new period before its next one:
before its deadline update_dl_revised_wakeup() keeps the deadline and scales the runtime down, and between the deadline and the next period dl_check_constrained_dl() throttles it. The 0-lag time only drops running_bw. A departed task loses this only because, with patches 9 and 11 of the first series, the release at the 0-lag time also clears the deadline.
So, what about keeping the release at the 0-lag time as it is now, and, for constrained tasks only, clearing the runtime but keeping the deadline when we release? Something like:
/* in the two release paths, instead of __dl_clear_params() */ u64 deadline = dl_se->deadline; bool constrained = !dl_is_implicit(dl_se); __dl_clear_params(dl_se); if (constrained) dl_se->deadline = deadline; /* runtime stays 0 */
A task coming back then goes through the in-class rules:
- after the next period: new period, as now;
- between the deadline and the next period: dl_check_constrained_dl() throttles it until the next period, for a wakeup and for a runnable return (it runs before the ENQUEUE_MOVE/setup_new_dl_entity() path);
- between the 0-lag time and the deadline: runtime 0, so nothing left to spend from the period it gave back.
The bandwidth goes back at the 0-lag time as today, so admission of the returning task is a normal check (plus fix 2 for an earlier return). Implicit tasks are unchanged: for them a new period after the 0-lag time is what CBS does in the class too, so clearing the deadline is right.
If this works, the timer restart and dl_inactive_pending shouldn't be needed, departed tasks wouldn't stay around longer than today (one less thing for patch 6 and the rebuild to deal with), and the change looks like a handful of lines.
What do you think? If you see something that breaks with it, I'd still suggest splitting the current version: the bail in inactive_task_timer() for a task back in the class looks like a standalone fix (exit race, already on v7.4-rc2), then dl_inactive_pending on its own, then the rule. Reading the state once in task_non_contending() (it is read up to four times now) would also help.
Thanks,
Juri
Re: [PATCH v13 05/36] sched/deadline: Keep a departing constrained task until its next period
Hi Juri,
On 2026-12-02 15:55, Juri Lelli wrote:
[...]
Quoted 4 lines
The fixes first; neither is new with this series.
1.
check_dl_overrun()clearsdl_overrunwith a plain store, without the rq lock.
[...]
Quoted 3 lines
Clearing it under
task_rq_lock()should do (siglock ->pi_lock-> rq lock is already a valid order, fromsignal_wake_up_state()).
Agreed. The planned next version will have it as a new patch, right after patch 4. check_dl_overrun() will call a new helper, dl_task_clear_overrun(), which clears the flag under task_rq_lock().
Quoted 3 lines
This is also the reason
dl_inactive_pendingis a bool at the end of the struct (8 bytes more per task at this point of the series)? Withdl_overrunfixed, it can be a bit like the others.
Yes, that was the reason. With your approach below, the planned next version will not need dl_inactive_pending at all.
Quoted 2 lines
2.
sched_dl_overflow()admits a task coming back toSCHED_DEADLINEbefore its 0-lag time withold_bw= 0:
[...]
Quoted 3 lines
Discounting it when the timer is active (and, after your patch 6, when the recorded root domain is this
dl_b) would make the two consistent, and would also close the window where patch 6 counts both.
Agreed. It will be the second new patch. A new case of patch 30, params_return_full, tests it. In a partition with no room for two tasks of 60%, a task of 60% leaves the class and comes back before its 0-lag time. Without the fix, the return gets EBUSY.
From patch 6 on, a departed task records the CPU whose root domain counts its old bandwidth. The old bandwidth will be discounted only if the task comes back in that root domain. sched_dl_overflow() will then swap the old and the new bandwidth in one lock section. If the task comes back in another root domain, the old bandwidth is still removed in a second lock section. The cover letter will list that as a known limit.
Quoted 2 lines
Could you add these two as separate patches ahead of this one (or I can send them, if you prefer)?
I will add them, with your Reported-by.
Quoted 2 lines
Now the approach. IIUC, the only thing a returning constrained task needs is its deadline.
[...]
Quoted 3 lines
So, what about keeping the release at the 0-lag time as it is now, and, for constrained tasks only, clearing the runtime but keeping the deadline when we release?
Yes, this works, and the planned next version will do this. The rule will be a new helper, dl_release_params(), called in the two release paths. The timer restart and dl_inactive_pending will be gone. The patch will add about 30 lines, more than half of them a comment.
reenter_asleep_late_in_gap runs the example of the changelog of this patch. The task wakes up at 155ms, after its deadline and before its next period. If dl_release_params() also clears the deadline, the case fails in 3 of 3 runs: the task runs right after its wakeup. With the deadline kept, it passes in 3 of 3 runs.
Two details:
- "Record the start of the period" (patch 28) adds a field, period_start. Patch 29 computes the next period from it. So dl_release_params() will keep period_start with the deadline.
- Your sketch leaves a runtime of 0. A task that comes back before its deadline with no runtime left is not throttled at once. It runs until the next tick, and pays that back at its next replenishment. "Keep the debt of a task across a change of parameters" (patch 29) makes enqueue_dl_entity() throttle it at once.
There are two costs, which the cover letter will list:
- A constrained task that leaves with much runtime left can reach its 0-lag time before its deadline. If it comes back between the two, it has no runtime left until its next period. Had it stayed in the class, it could have used that runtime until its deadline.
- A priority inheritance boost of a departed task replaces the deadline it kept, and the deboost clears it. If the task then comes back before its next period, it starts a new period at once. The priority inheritance series will handle this case.
One question. The inactive timer can be armed in one period. Its handler can then wait for the rq lock while the task runs a new period. If the task leaves the class then, task_non_contending() releases the bandwidth at once, as on v7.4-rc2. This is before the 0-lag time of the new period. The root domain can then admit another task into that bandwidth too early. The task itself keeps its deadline, so it gains no runtime. v13 closed this window with the timer restart. Is this window fine with you, or should the planned next version close it?
Quoted 6 lines
If you see something that breaks with it, I'd still
suggest splitting the current version: the bail ininactive_task_timer()for a task back in the class looks like a standalone fix (exit race, already on v7.4-rc2), thendl_inactive_pendingon its own, then the rule. Reading the state once intask_non_contending()(it is read up to four times now) would also help.
The bail will be a patch of its own, after the two fixes above. As you say, it fixes a race of v7.4-rc2. A task can wake up and die while the handler waits for the rq lock. Its last dequeue then leaves its bandwidth in running_bw and this_bw. The changelog will have the sequence as a diagram.
With the rework, task_non_contending() reads the task state twice, as on v7.4-rc2. Patch 6 adds a third read, for a dying task.
The full test suite passes on the reworked series.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com