sched/deadline: bandwidth accounting fixes and selftests
Subsystems (7)
[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
Re: [PATCH v13 30/36] selftests/sched: Test parameter changes in deadline_bw_test
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
PATCH v13 31/36 sched/deadline: Count a task that departs while a rebuild walks the tasks
In reply to Mathieu Desnoyersdl_rebuild_rd_accounting() rebuilds the root domains' accounting in two steps. First, dl_clear_root_domains() clears the root domains. Then it puts back the bandwidth of the departed tasks, which left SCHED_DEADLINE or died before their 0-lag time. Second, the walk of the task list counts each deadline task.
A task can leave the class after the clear, before the walk reaches
it: priority inheritance can deboost it. The walk then finds a task out of the class, and skips it. Neither step counts its bandwidth. The comment above dl_departed_list lists this as a known gap.
In this example, the deadline task W waits with a timeout on an rt_mutex that T holds. T was a deadline task too. While W boosted it, sched_setattr() switched T to SCHED_OTHER. The boost keeps T in the class, with its bandwidth. W runs on CPU 1. CPU 0 rebuilds the accounting, for example after a write to cpuset.cpus.partition.
CPU 0 CPU 1
----- -----
dl_rebuild_rd_accounting()
dl_clear_root_domains()
clear total_bw
put back the departed tasks
W times out: remove_waiter()
rt_mutex_setprio(T)
switched_from_dl(T)
dl_task_depart(T)
T joins the departed list
walk of the task list reaches T
T is out of the class: skip it
total_bw then misses the bandwidth of T until its 0-lag time. Meanwhile, sched_setattr() can admit tasks beyond the capacity of the root domain.
Fix this by counting such a task in dl_task_depart(), as the walk would have. dl_bw_move_blocked() already tells whether the walk has yet to reach the task. Count the task on the CPU it departs from, or else on the CPU recorded with it, or else on an active CPU of its mask. If none is active, leave it out, as the walk does.
A task that dies in the same window departs too. The walk counted it from the departed list, unless release_task() had taken it off the task list. dl_task_depart() now counts it in both cases.
This was found by reading the code, and by a Spin model of the accounting. The pi_deboost_during_rebuild case of deadline_bw_test races the timeout with the rebuild. It failed in 35 of 43 runs without this fix, and passed in 35 of 35 with it. "selftests/sched: Test PI against hotplug and rebuilds in deadline_bw_test", later in this series, adds the case.
--- kernel/sched/deadline.c | 54 ++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 9 deletions(-)
kernel/sched/deadline.c +45 -9
@@ -253,12 +253,14 @@ __dl_overflow(struct dl_bw *dl_b, unsigned long cap, u64 old_bw, u64 new_bw)* harmless. * * A rebuild drops the bandwidth when the CPU recorded is no longer -* active. It also drops the bandwidth of a task that dies while the -* rebuild runs. The walk of the task list does not find that task, and -* it was not dead yet when the list was put back. The same happens to a -* task that priority inheritance deboosts while the rebuild runs. It was -* in the class when the list was put back, and out of it when the walk -* reaches it. +* active. It also drops the bandwidth of a task that release_task() +* took off the task list, if its last schedule comes after the walk. +* The walk does not find that task, and it was not on the departed +* list when the list was put back. +* +* A task can also depart after the list was put back, and before the +* walk reaches it. Priority inheritance can deboost it, or it can die. +* dl_task_depart() then counts it, as the walk would have. * * dl_departed_lock protects the list and both counts. Take it with * interrupts disabled, under the task's rq lock or pi_lock or in the
@@ -376,6 +378,23 @@ bool dl_task_holds_bw(struct task_struct *p)return dl_se_holds_bw (&p->dl); } +/* +* Add the bandwidth of @p to the root domain of @cpu, for a task that no +* root domain counts now. Record @cpu, and date the record with the +* current dl_bw_gen. The caller holds dl_departed_lock. +*/ +static void dl_task_count_bw (struct task_struct *p,int cpu) +{ +struct dl_bw *dl_b =dl_bw_of (cpu); + +lockdep_assert_held (&dl_departed_lock); +raw_spin_lock (&dl_b->lock); +__dl_add (dl_b, p->dl.dl_bw,dl_bw_cpus (cpu)); +raw_spin_unlock (&dl_b->lock); +dl_record_bw_cpu (&p->dl, cpu); + p->dl.dl_bw_gen = dl_bw_gen; +} +/* * Put @p on dl_departed_list. The root domain of the CPU recorded with it * keeps its bandwidth. @p leaves SCHED_DEADLINE, or dies, before its
@@ -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. +*/ +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); }
@@ -4539,9 +4576,8 @@ void dl_add_task_root_domain(struct task_struct *p)* dl_clear_root_domains() put it back as a departed task, and * priority inheritance boosted it into the class since. Or it died * since the rebuild started, and dl_task_release_bw() released its -* bandwidth. A task that died since with its 0-lag time ahead is on -* the departed list. Count it in the root domain of the CPU -* recorded, if that CPU is active. +* bandwidth. Or it departed before the walk reached it, and +* dl_task_depart() counted it. */ if (p->dl.dl_bw_gen == dl_bw_gen) goto unlock;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v13 31/36] sched/deadline: Count a task that departs while a rebuild walks the tasks
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 --
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
Re: [PATCH v13 31/36] sched/deadline: Count a task that departs while a rebuild walks the tasks
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
PATCH v13 32/36 sched/deadline: Count again a departed task that a boost brings back
In reply to Mathieu DesnoyersA 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 in the root domain of the CPU of the task. A migration does the same for a task that a rebuild left out (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. pi_departed_offline_boost, a test case that a later patch of this series adds, shows it. Without this fix, it failed in 33 of 33 runs. With it, it passed in 25 of 25.
--- include/linux/sched.h | 8 ++++---- kernel/sched/deadline.c | 44 ++++++++++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 15 deletions(-)
include/linux/sched.h +4 -4
@@ -771,10 +771,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.
kernel/sched/deadline.c +33 -11
100 lines not on this page · raw message
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v13 32/36] sched/deadline: Count again a departed task that a boost brings back
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 --
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
Re: [PATCH v13 32/36] sched/deadline: Count again a departed task that a boost brings back
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
PATCH v13 33/36 sched/deadline: Keep a reaped deadline task in a rebuild
In reply to Mathieu DesnoyersA rebuild of the root domains' accounting counts tasks from two places:dl_departed_list, and the task list. The walk of the task list counts a dying deadline task until its last schedule. That schedule releases the bandwidth, or puts the task on dl_departed_list until its 0-lag time.
But release_task() can take a task off the task list before its last schedule. A thread other than the group leader reaps itself in exit_notify(). A parent can reap a zombie child while the child still runs. A rebuild in that window finds the task in neither place, and drops its bandwidth. total_bw then misses it until the last schedule, or until the 0-lag time if that is later. Meanwhile, sched_setattr() would admit tasks beyond the capacity of the root domain.
Fix this by putting such a task on dl_departed_list from release_task(), in the new dl_task_unhash(). It runs under tasklist_lock, before the task leaves the task list, so no walk runs in between. A rebuild then counts the task with the departed tasks. Its last schedule releases the bandwidth and takes it off the list, or leaves both to the inactive timer.
A reap after dl_clear_root_domains() and before the walk is covered by "sched/deadline: Count a task that departs while a rebuild walks the tasks". dl_task_depart() counts the task then, as the walk would have.
A boost back into the class takes a task off the list, in dl_task_undepart(). Keep a task that has exited on it: it may be off the task list already.
A Spin model of the accounting found this case. It was not reproduced:
the window from release_task() to the last schedule is short. With this fix and the one named above, the model finds no error for two tasks and one root domain.
--- include/linux/sched/deadline.h | 1 + kernel/exit.c | 2 ++ kernel/sched/deadline.c | 37 +++++++++++++++++++++++++++++----- 3 files changed, 35 insertions(+), 5 deletions(-)
include/linux/sched/deadline.h +1 -0
@@ -36,6 +36,7 @@ extern void dl_clear_root_domains(void);extern void dl_bw_rebuild_begin (void );extern void dl_rebuild_rd_accounting (void );extern bool dl_task_holds_bw (struct task_struct *p); +extern void dl_task_unhash (struct task_struct *p);/* * Keep a rebuild of the root domains' accounting and an admission of * bandwidth from running at the same time. With cpusets, cpuset_mutex
kernel/exit.c +2 -0
@@ -13,6 +13,7 @@#include <linux/sched/task.h> #include <linux/sched/task_stack.h> #include <linux/sched/cputime.h> +#include <linux/sched/deadline.h> #include <linux/interrupt.h> #include <linux/module.h> #include <linux/capability.h>
@@ -264,6 +265,7 @@ void release_task(struct task_struct *p)write_lock_irq (&tasklist_lock);ptrace_release_task (p); +dl_task_unhash (p);__exit_signal (&post, p);/*
kernel/sched/deadline.c +32 -5
@@ -253,10 +253,11 @@ __dl_overflow(struct dl_bw *dl_b, unsigned long cap, u64 old_bw, u64 new_bw)* harmless. * * A rebuild drops the bandwidth when the CPU recorded is no longer -* active. It also drops the bandwidth of a task that release_task() -* took off the task list, if its last schedule comes after the walk. -* The walk does not find that task, and it was not on the departed -* list when the list was put back. +* active. +* +* release_task() can take a task off the task list before its last +* schedule. The walk would then miss it. So dl_task_unhash() puts a +* reaped deadline task on the list, while it is still in the class. * * A task can also depart after the list was put back, and before the * walk reaches it. Priority inheritance can deboost it, or it can die.
@@ -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 +* 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 ; + 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); +} +/* * Take @p off dl_departed_list. @p comes back to the deadline class, and * its bandwidth is that of a deadline task again. @cpu is the CPU of @p.
@@ -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); -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);
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v13 33/36] sched/deadline: Keep a reaped deadline task in a rebuild
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…
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 33/36] sched/deadline: Keep a reaped deadline task in a rebuild
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
PATCH v13 34/36 sched/deadline: Warn when a runqueue keeps the bandwidth of a task
In reply to Mathieu DesnoyersThe this_bw of a runqueue counts the bandwidth of each task whose dl_bw_oncpu is set. add_rq_bw() sets the flag and sub_rq_bw() clears it. Two states would leave a bandwidth in this_bw for ever:
- A task which left the deadline class still has its flag set, and moves to another CPU. Only the deadline class takes the bandwidth off the old runqueue: dequeue_task_dl() for a queued task, and migrate_task_rq_dl() for a waking one. So nothing takes it off.
- task_non_contending() or inactive_task_timer() releases the bandwidth of a task while its flag is still set. They then call __dl_clear_params(), which clears both the flag and dl_bw. After that, nothing can take the bandwidth off the runqueue.
Without a check, a test sees such a leak only later, as a this_bw which does not return to its starting value. It cannot tell which path leaked.
Warn about the first state in set_task_cpu(). Warn about the second in task_non_contending() and inactive_task_timer(), before they call __dl_clear_params(). No warning fires in the selftests of this series.
A review of the accounting code by an LLM suggested these checks.
--- kernel/sched/core.c | 3 +++ kernel/sched/deadline.c | 4 ++++ 2 files changed, 7 insertions(+)
kernel/sched/core.c +3 -0
@@ -3517,6 +3517,9 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)*/ WARN_ON_ONCE (!cpu_online (new_cpu)); +/* A task of another class would leave its bandwidth in this_bw. */ +WARN_ON_ONCE (!dl_task (p) && p->dl.dl_bw_oncpu); +/* * Proxy execution can move a blocked task's scheduling context to any * CPU without moving its migration-disabled execution context. The
kernel/sched/deadline.c +4 -0
@@ -904,6 +904,8 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task)if (!dl_task ||READ_ONCE (p->__state) ==TASK_DEAD ) {if (dl_task &&READ_ONCE (p->__state) ==TASK_DEAD )sub_rq_bw (dl_se, &rq->dl); +/* No runqueue may still count dl_bw. */ +WARN_ON_ONCE (dl_se->dl_bw_oncpu);dl_task_release_bw (p);cancel_replenish_timer (dl_se);__dl_clear_params (dl_se);
@@ -2848,6 +2850,8 @@ static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer) dl_se->dl_non_contending =0 ; } +/* No runqueue may still count dl_bw. */ +WARN_ON_ONCE (dl_se->dl_bw_oncpu);dl_task_release_bw (p);cancel_replenish_timer (dl_se);__dl_clear_params (dl_se);
Signature · 1 line
2.47.3
PATCH v13 35/36 selftests/sched: Test PI against hotplug and rebuilds in deadline_bw_test
In reply to Mathieu DesnoyersAdd two cases to deadline_bw_test. The comment above each case gives its scenario. Each case fails without one fix of this series:
- pi_departed_offline_boost. A task leaves SCHED_DEADLINE while its root domain still counts its bandwidth. Then the CPU recorded with that bandwidth goes offline. The rebuild of the accounting that follows drops the bandwidth. A deadline waiter then boosts the task back into the class. The case checks that total_bw counts the task again. The fix is "sched/deadline: Count again a departed task that a boost brings back". Without it, the case failed in every run.
- pi_deboost_during_rebuild. The timeout of a deadline waiter deboosts a task while a rebuild of the accounting walks the task list. The case checks that total_bw still counts the task right after the rebuild. The fix is "sched/deadline: Count a task that departs while a rebuild walks the tasks".
pi_deboost_during_rebuild tests a race. It runs rounds for 25s, and fails at the first round where total_bw misses the task. A filler process of 1000 threads is forked before the task. The walk meets these threads first, so the race window is wider. Without the fix, 35 of 43 runs failed.
pi_departed_offline_boost needs 4 housekeeping CPUs, two of them hotpluggable. pi_deboost_during_rebuild needs 5. Each case skips without them.
With the whole series, both cases pass in every run. No kernel warning is logged.
--- .../sched/deadline/deadline_bw_test.c | 487 +++++++++++++++++- 1 file changed, 481 insertions(+), 6 deletions(-)
tools/testing/selftests/sched/deadline/deadline_bw_test.c +481 -6
@@ -40,15 +40,16 @@* hotplug cases, and the cases with no_hp set, which take no CPU * offline. The test skips the hotplug cases if no CPU can go offline. It * still runs the no_hp ones. -* death_zombie_hotplug takes CPUs offline too, but it is in p_cases[]: it -* needs the cpuset setup of those cases. +* death_zombie_hotplug and pi_departed_offline_boost take CPUs offline +* too, but they are in p_cases[]: they need the cpuset setup of those +* cases. * * The server cases change the fair server of a CPU through debugfs, and * the test then puts it back. * * The test needs root, deadline admission control and 3 housekeeping -* CPUs. Some cases need 4. Most also need the cgroup v2 cpuset -* controller. Some need debugfs or CPU hotplug. Twenty-nine need +* CPUs. Some cases need 4, one needs 5. Most also need the cgroup v2 +* cpuset controller. Some need debugfs or CPU hotplug. Twenty-nine need * isolcpus="domain" at boot. */ #define _GNU_SOURCE
@@ -8338,8 +8339,9 @@ static int random_mix(void) }/* -* CPU hotplug, from the main process, with no P. Two cases below run with -* P instead: death_zombie_hotplug and pi_departed_moved. +* CPU hotplug, from the main process, with no P. Three cases below run +* with P instead: death_zombie_hotplug, pi_departed_moved and +* pi_departed_offline_boost. */ static int hp_cpu =-1 ;
@@ -8928,6 +8930,163 @@ static int pi_departed_moved(void)return ret; } +/* +* Q is a partition root of CPUs X and Y. The owner is a deadline task of +* 200ms every 2s in Q. The steps: +* 1. The owner takes shm_lock, runs 180ms and sleeps on X. Its 0-lag +* time is about 1.6s ahead. +* 2. The case switches the owner to SCHED_OTHER. The root domain of Q +* keeps its bandwidth until the 0-lag time. The kernel records X with +* that bandwidth. +* 3. The case pins the owner to Y and wakes it up. It runs on Y, then +* sleeps there. The record stays X. +* 4. The case takes X offline, which rebuilds the accounting. The +* recorded CPU is no longer active, so the rebuild drops the +* bandwidth of the owner. +* 5. A deadline waiter of 5ms every 50ms, outside Q, blocks on shm_lock. +* It boosts the owner back into the class. +* +* The boost must add the bandwidth of the owner to this_bw of Y, and to +* total_bw of Q. Once the owner and the waiter are gone, past the 0-lag +* time, total_bw of Q must be what it was at the start. +*/ +static int pi_departed_offline_boost (void ) +{ +int cpu[2 ] = {top_cpu (3 ),top_cpu (2 ) }, x, y, ret =KSFT_FAIL ; +long long base, bw, off, boosted; +long this0, this1, running; +pid_t owner =-1 , waiter =-1 ; +char cpus[32 ]; +cpu_set_t set; +uint64_t t; + +if (ncpus <4 || !cpu_hotpluggable (cpu[0 ]) || !cpu_hotpluggable (cpu[1 ])) { +ksft_print_msg ("needs 4 non-isolated CPUs, 2 hotpluggable beside P\n" ); +return KSFT_SKIP ; + } +snprintf (cpus,sizeof (cpus),"%d,%d" , cpu[0 ], cpu[1 ]); +if (make_partition_cpus (cg_q, cpus)) { +ksft_print_msg ("no partition root Q of CPUs %s\n" , cpus); +remove_partition (cg_q); +return KSFT_SKIP ; + } + base =total_bw_of (cpu[0 ]); +if (base <0 ||rq_bw_of (cpu[0 ], &this0, &running)) { +ksft_print_msg ("no total_bw or this_bw in /sys/kernel/debug/sched/debug\n" ); +remove_partition (cg_q); +return KSFT_SKIP ; + } +if (shm_lock_init ()) { +remove_partition (cg_q); +return KSFT_SKIP ; + } + bw = (ZOMBIE_RUNTIME <<20 ) /ZOMBIE_PERIOD ;/* as to_ratio() */ + mv_state[1 ] =0 ; + mv_state[3 ] =0 ; + owner =fork_child ("the owner of the lock" ); +if (!owner) { +signal (SIGUSR1 , no_op); +if (enter (cg_q,getpid ()) ||pthread_mutex_lock (shm_lock) || +set_deadline (0 ,ZOMBIE_RUNTIME ,ZOMBIE_PERIOD )) { + mv_state[0 ] =-1 ; +_exit (1 ); + } +/* 20ms left: the 0-lag time is 200ms before the deadline. */ +spin_ns (ZOMBIE_RUNTIME -20 *NSEC_PER_MSEC ); + mv_state[0 ] =1 ; +while (!mv_state[3 ]) +pause (); + mv_state[1 ] =1 ; +for (;;) +pause (); + } +if (owner <0 ||child_up ("the owner of the lock" ,0 )) + goto out; + t =now_ns (); +if (wait_for (100 ,proc_state_of (owner) =='S' )) { +ksft_print_msg ("the owner is not asleep\n" ); + goto out; + } +/* Step 2. Asleep, the owner departs from the CPU it sleeps on. */ +if (set_other (owner)) { +ksft_print_msg ("the owner cannot leave SCHED_DEADLINE\n" ); + goto out; + } + x =task_cpu_of (owner); +if (x != cpu[0 ] && x != cpu[1 ]) { +ksft_print_msg ("the owner sleeps on CPU %d, not in Q\n" , x); + goto out; + } + y = x == cpu[0 ] ? cpu[1 ] : cpu[0 ]; +/* Step 3 */ +CPU_ZERO (&set); +CPU_SET (y, &set); +if (sched_setaffinity (owner,sizeof (set), &set)) { +ksft_print_msg ("the owner cannot be pinned to CPU %d: %s\n" , y,strerror (errno)); + goto out; + } + mv_state[3 ] =1 ; +kill (owner,SIGUSR1 ); +if (wait_state (&mv_state[1 ],1 ) ||wait_for (500 ,proc_state_of (owner) =='S' ) || +task_cpu_of (owner) != y) { +ksft_print_msg ("the owner did not move to CPU %d\n" , y); + goto out; + } +/* Step 4 */ +if (cpu_online (x,0 )) { +ksft_print_msg ("CPU %d cannot go offline\n" , x); + goto out; + } + off =total_bw_of (y); +if (rq_bw_of (y, &this0, &running)) { +ksft_print_msg ("no this_bw of CPU %d\n" , y); + goto online; + } +/* Step 5 */ + waiter =lock_waiter (); +if (waiter <0 ||wait_state (&mv_state[2 ],1 )) { +ksft_print_msg ("no waiter\n" ); + goto online; + } +sleep_ns (50 *NSEC_PER_MSEC );/* blocked, boosting the owner */ +if (prio_of (owner) !=DL_TASK_PRIO ) { +ksft_print_msg ("the owner is not boosted: prio %d\n" ,prio_of (owner)); + goto online; + } + boosted =total_bw_of (y); +if (rq_bw_of (y, &this1, &running)) { +ksft_print_msg ("no this_bw of CPU %d\n" , y); + }else if (now_ns () - t >NSEC_PER_SEC ) { +ksft_print_msg ("the steps took over 1s: the 0-lag time may be past\n" ); + ret =KSFT_SKIP ; + }else if (this1 - this0 != bw) { +ksft_print_msg ("the boost adds %ld to this_bw of CPU %d, %lld expected\n" , + this1 - this0, y, bw); + }else if (boosted != off + bw) { +ksft_print_msg ("total_bw of Q: %lld boosted, %lld expected%s\n" , + boosted, off + bw, + boosted == off ?": the owner is not counted" :"" ); + }else { + ret =KSFT_PASS ; + } +online: +reap (waiter); +cpu_up (x); +out: +reap (owner); +if (ret ==KSFT_PASS ) { +sleep_ns (ZOMBIE_PERIOD );/* past the 0-lag time of the owner */ + off =total_bw_of (cpu[0 ]); +if (off != base) { +ksft_print_msg ("total_bw of Q past the 0-lag time: %lld, %lld expected\n" , + off, base); + ret =KSFT_FAIL ; + } + } +remove_partition (cg_q); +return ret; +} +/* * The bandwidth of a fair server must leave the root domain with its CPU * and come back with it, whatever its size. Probe the housekeeping root
@@ -12596,6 +12755,320 @@ static int affinity_vs_setattr(void)return ret; } +/* A deboost against a rebuild of the accounting */ + +#define RACE_T_RUNTIME (20 * NSEC_PER_MSEC) +#define RACE_T_PERIOD (200 * NSEC_PER_MSEC) +#define RACE_W_RUNTIME (5 * NSEC_PER_MSEC) +#define RACE_W_PERIOD (50 * NSEC_PER_MSEC) +#define RACE_FILLERS 1000 +#define RACE_SECS 25 + +/* +* The state that pi_deboost_during_rebuild() shares with its processes, +* in shared memory. T and W are the threads that its comment describes. +* go and w_go count the rounds of T and W. t_ready and w_done tell the +* case that a round of T or of W is done. +*/ +static struct race_shm { +atomic_int go, t_ready, w_go, w_blocked, w_done, w_err, fill; + _Atomicpid_t t_tid, w_tid; + _Atomicint64_t w_abs, w_ret; +} *rs; + +/* The priority inheritance mutex of T and W, threads of one process */ +static pthread_mutex_t race_m; + +static int64_t realtime_ns (void ) +{ +struct timespec ts; + +clock_gettime (CLOCK_REALTIME , &ts); +return (int64_t )ts.tv_sec *NSEC_PER_SEC + ts.tv_nsec; +} + +/* Wait for the futex @f, in shared memory, to change from @val. */ +static void wait_change (atomic_int *f,int val) +{ +while (*f == val) +syscall (SYS_futex, f,FUTEX_WAIT , val,NULL ,NULL ,0 ); +} + +static void *race_filler (void *arg) +{ +for (;;) +pause (); +return arg; +} + +/* +* W: become a deadline task. In each round, block on race_m until the +* time rs->w_abs, CLOCK_REALTIME. The wait must time out: T holds +* race_m. +*/ +static void *race_w (void *arg) +{ +int i =0 ; + + rs->w_tid =gettid_ (); +if (set_deadline (0 ,RACE_W_RUNTIME ,RACE_W_PERIOD )) { +set_flag (&rs->w_err,-1 ); +return arg; + } +for (;;) { +struct timespec ts; +int err; + +wait_change (&rs->w_go, i); + i = rs->w_go; +if (i <0 ) +return arg; + ts.tv_sec = rs->w_abs /NSEC_PER_SEC ; + ts.tv_nsec = rs->w_abs %NSEC_PER_SEC ; + rs->w_blocked = i; + err =pthread_mutex_timedlock (&race_m, &ts); + rs->w_ret =realtime_ns (); +if (!err) { +pthread_mutex_unlock (&race_m); + rs->w_err =1 ; + }else if (err !=ETIMEDOUT ) { + rs->w_err =2 ; + } +set_flag (&rs->w_done, i); + } +} + +/* +* Fork the process of T and W, in the cgroup @cg. T, its main thread, +* takes race_m and starts W. In each round, T makes itself a deadline +* task, runs most of its runtime and sleeps. Return the pid, or -1. +*/ +static pid_t race_t (const char *cg) +{ +pid_t pid =fork_child ("T and W" ); + +if (!pid) { +pthread_mutexattr_t a; +pthread_t w; +int i =0 ; + +if (enter (cg,getpid ()) ||pthread_mutexattr_init (&a) || +pthread_mutexattr_setprotocol (&a,PTHREAD_PRIO_INHERIT ) || +pthread_mutex_init (&race_m, &a) ||pthread_mutex_lock (&race_m) || +pthread_create (&w,NULL , race_w,NULL )) { +set_flag (&rs->w_err,-1 ); +_exit (1 ); + } + rs->t_tid =gettid_ (); +for (;;) { +wait_change (&rs->go, i); + i = rs->go; +if (i <0 ) +_exit (0 ); +if (set_deadline (0 ,RACE_T_RUNTIME ,RACE_T_PERIOD )) { +set_flag (&rs->t_ready, -i); +continue ; + } +/* 2ms left: the 0-lag time is 20ms before the deadline. */ +spin_ns (RACE_T_RUNTIME -2 *NSEC_PER_MSEC ); +set_flag (&rs->t_ready, i); + } + } +return pid; +} + +/* +* Fork a process of RACE_FILLERS threads that sleep. Return its pid, or +* -1. It sets rs->fill to the count of threads it made, or to -1. +*/ +static pid_t race_fill (void ) +{ +pid_t pid =fork_child ("the filler" ); + +if (!pid) { +pthread_attr_t at; +pthread_t t; +int n; + +pthread_attr_init (&at); +pthread_attr_setstacksize (&at,THREAD_STACK ); +for (n =0 ; n <RACE_FILLERS ; n++) { +if (pthread_create (&t, &at, race_filler,NULL )) +break ; + } +set_flag (&rs->fill, n ? n :-1 ); +for (;;) +pause (); + } +return pid; +} + +/* Toggle Q between a partition root and a member. Return 0, or -errno. */ +static int toggle_q (int *q_root) +{ + *q_root = !*q_root; +return write_file (cg_q,"cpuset.cpus.partition" , *q_root ?"root" :"member" ); +} + +/* +* A rebuild of the accounting clears the bandwidth of the root domains, +* then walks the task list and adds back each deadline task. A task can +* leave the class in the window between the clear and the walk reaching +* it. It must still be counted until its 0-lag time. +* +* R is a partition root of two CPUs. Q is a partition root of one CPU. T +* and W are threads of one process in R. T holds race_m, a priority +* inheritance mutex. Before T and W, the case forks a filler process of +* RACE_FILLERS threads that sleep. The walk meets them before T, which +* makes the window wider. In each round: +* 1. T makes itself a deadline task of 20ms every 200ms. It runs 18ms +* and sleeps. Its 0-lag time is about 160ms ahead. +* 2. W, a deadline task of 5ms every 50ms, blocks on race_m with a +* timeout. It boosts T. +* 3. The case switches T to SCHED_OTHER. The boost keeps T in the class. +* 4. The case switches Q between member and partition root. This +* rebuilds the accounting. The case times the write so that the +* timeout of W falls in it, at a random point. The timeout ends the +* boost: T leaves the class before its 0-lag time. +* 5. Right after, total_bw of R must count W and T. +* 6. Past the 0-lag time of T, total_bw of R must count W only. +* +* The rounds run for RACE_SECS seconds. The case fails at the first +* round where total_bw of R misses T. +*/ +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 ; + } +snprintf (cg_r,sizeof (cg_r),"%s/deadline_bw_r" , root); +snprintf (cpus,sizeof (cpus),"%d,%d" , cpu[0 ], cpu[1 ]); +if (make_partition_cpus (cg_r, cpus) ||make_partition (cg_q,top_cpu (2 ))) { +ksft_print_msg ("no partition roots R of CPUs %s and Q of CPU %d\n" , + cpus,top_cpu (2 )); + ret =KSFT_SKIP ; + goto out_cg; + } + rs =mmap (NULL ,sizeof (*rs),PROT_READ |PROT_WRITE ,MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); +if (rs ==MAP_FAILED ) { +ksft_print_msg ("mmap: %s\n" ,strerror (errno)); + goto out_cg; + } +memset (rs,0 ,sizeof (*rs)); + bw_t =bw_of (RACE_T_RUNTIME ,RACE_T_PERIOD ); + bw_w =bw_of (RACE_W_RUNTIME ,RACE_W_PERIOD ); + + filler =race_fill (); +if (filler <0 ||wait_for (10000 , rs->fill) || rs->fill <0 ) { +ksft_print_msg ("no filler threads\n" ); + goto out; + } +/* The longest of 8 writes to Q sets the range of the timeouts. */ +for (i =0 ; i <8 ; i++) { +uint64_t t =now_ns (); + +if (toggle_q (&q_root)) { +ksft_print_msg ("Q cannot change between root and member\n" ); + ret =KSFT_SKIP ; + goto out; + } + d =now_ns () - t; +if (d > dmax) + dmax = d; + } +if (total_bw_of (cpu[0 ]) <0 ) { +ksft_print_msg ("no total_bw in /sys/kernel/debug/sched/debug\n" ); + ret =KSFT_SKIP ; + goto out; + } + proc =race_t (cg_r); +if (proc <0 ||wait_for (2000 , (rs->w_tid && rs->t_tid) || rs->w_err) || rs->w_err) { +ksft_print_msg ("no T and W\n" ); + goto out; + } +sleep_ns (100 *NSEC_PER_MSEC ); + base =total_bw_of (cpu[0 ]) - bw_w; + +srand (getpid () ^now_ns ()); + end =now_ns () +RACE_SECS *NSEC_PER_SEC ; +for (i =1 ;now_ns () < end; i++) { +int64_t abs, lead, w0; +struct timespec ts; + +set_flag (&rs->go, i); +if (wait_for_step (1000 ,100 *NSEC_PER_USEC , + rs->t_ready == i || rs->t_ready == -i) || + rs->t_ready != i) { +ksft_print_msg ("round %d: T cannot become a deadline task\n" , i); + goto out; + } +if (wait_for_step (100 ,100 *NSEC_PER_USEC ,proc_state_of (rs->t_tid) =='S' )) +continue ; +/* Leave W 20ms to block, then time out within the write. */ + lead = (int64_t )((double )rand () /RAND_MAX *1.2 * dmax); + abs =realtime_ns () +20 *NSEC_PER_MSEC + dmax; + rs->w_abs = abs; +set_flag (&rs->w_go, i); +if (wait_for_step (100 ,100 *NSEC_PER_USEC , rs->w_blocked == i) || +wait_for_step (100 ,100 *NSEC_PER_USEC ,proc_state_of (rs->w_tid) =='S' )) { +ksft_print_msg ("round %d: W did not block\n" , i); + goto out; + } +if (set_other (rs->t_tid)) { +ksft_print_msg ("round %d: T cannot leave SCHED_DEADLINE\n" , i); + goto out; + } +if (prio_of (rs->t_tid) !=DL_TASK_PRIO ) { +ksft_print_msg ("round %d: W does not boost T\n" , i); + goto out; + } + ts.tv_sec = (abs - lead) /NSEC_PER_SEC ; + ts.tv_nsec = (abs - lead) %NSEC_PER_SEC ; +while (clock_nanosleep (CLOCK_REALTIME ,TIMER_ABSTIME , &ts,NULL ) ==EINTR ) + ; + w0 =realtime_ns (); +toggle_q (&q_root); + d =realtime_ns () - w0; +if (wait_for_step (1000 ,100 *NSEC_PER_USEC , rs->w_done == i) || rs->w_err) { +ksft_print_msg ("round %d: the wait of W did not time out\n" , i); + goto out; + } + tb =total_bw_of (cpu[0 ]); +if (tb != base + bw_w + bw_t) { +ksft_print_msg ("round %d: total_bw of R %lld, %lld expected%s\n" , + i, tb, base + bw_w + bw_t, + tb == base + bw_w ?": T is missing" :"" ); +ksft_print_msg ("round %d: timeout %lldus into a write of %lldus\n" , + i, (long long )(rs->w_ret - w0) /1000 , (long long )d /1000 ); + goto out; + } +/* Past the 0-lag time of T: R counts W only. */ +if (wait_for_step (1000 ,5 *NSEC_PER_MSEC ,total_bw_of (cpu[0 ]) == base + bw_w)) { +ksft_print_msg ("round %d: past the 0-lag time of T, total_bw of R %lld, %lld expected\n" , + i,total_bw_of (cpu[0 ]), base + bw_w); + goto out; + } + } + ret =KSFT_PASS ; +out: +set_flag (&rs->go,-1 ); +set_flag (&rs->w_go,-1 ); +reap (proc); +reap (filler); +munmap (rs,sizeof (*rs)); +out_cg: +remove_partition (cg_q); +remove_partition (cg_r); +return ret; +} +/* The tables of the cases, and main() */ enum where {
@@ -12614,6 +13087,7 @@ static const struct p_case { { "pi_unlock_running", pi_unlock_running, MEASURED_IN_P }, { "pi_chain_two_owners", pi_chain_two_owners, MEASURED_IN_P }, { "pi_departed_moved", pi_departed_moved, FROM_OUTSIDE }, + { "pi_departed_offline_boost", pi_departed_offline_boost, FROM_OUTSIDE }, { "params_asleep", params_asleep, IN_P }, { "params_non_contending", params_non_contending, MEASURED_IN_P }, { "params_switched_asleep", params_switched_asleep, MEASURED_IN_P },
@@ -12703,6 +13177,7 @@ static const struct p_case { { "params_swap_overload", params_swap_overload, FROM_OUTSIDE }, { "random_mix", random_mix, FROM_OUTSIDE }, { "move_normalized", move_normalized, FROM_OUTSIDE }, + { "pi_deboost_during_rebuild", pi_deboost_during_rebuild, FROM_OUTSIDE }, };#define N_P_CASES ((int)ARRAY_SIZE(p_cases))
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v13 35/36] selftests/sched: Test PI against hotplug and rebuilds in deadline_bw_test
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() --
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
Re: [PATCH v13 35/36] selftests/sched: Test PI against hotplug and rebuilds in deadline_bw_test
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
PATCH v13 36/36 selftests/sched: Share the helpers of the deadline tests
In reply to Mathieu Desnoyersdeadline_bw_test, deadline_rebuild_test and deadline_switch_test each have a copy of the same helpers. Move these to deadline_util.h:
- now_ns(), thread_time_ns() and sleep_ns();
- report();
- read_file(), write_file() and enter().
deadline_rebuild_test uses thread_time_ns() in place of its own cpu_time_ns(). The helpers that differ from one test to the other stay in each test. The Makefile adds the header to LOCAL_HDRS, so that lib.mk rebuilds the tests when the header changes.
The Sashiko AI review of this series asked for this. No case changes.
--- .../testing/selftests/sched/deadline/Makefile | 2 + .../sched/deadline/deadline_bw_test.c | 86 +------------- .../sched/deadline/deadline_rebuild_test.c | 94 +--------------- .../sched/deadline/deadline_switch_test.c | 36 +----- .../selftests/sched/deadline/deadline_util.h | 106 ++++++++++++++++++ 5 files changed, 116 insertions(+), 208 deletions(-) create mode 100644 tools/testing/selftests/sched/deadline/deadline_util.h
tools/testing/selftests/sched/deadline/Makefile +2 -0
@@ -5,4 +5,6 @@ LDLIBS += -lpthread TEST_GEN_PROGS := deadline_switch_test deadline_rebuild_test deadline_bw_test +LOCAL_HDRS += deadline_util.h + include ../../lib.mk
tools/testing/selftests/sched/deadline/deadline_bw_test.c +2 -84
110 lines not on this page · raw message
tools/testing/selftests/sched/deadline/deadline_rebuild_test.c +4 -90
130 lines not on this page · raw message
tools/testing/selftests/sched/deadline/deadline_switch_test.c +2 -34
@@ -51,6 +51,8 @@#define NSEC_PER_MSEC 1000000ULL#define NSEC_PER_SEC 1000000000ULL +#include "deadline_util.h" +struct dl_attr {uint32_t size;uint32_t sched_policy;
@@ -70,22 +72,6 @@ struct shared {uint64_t sleeps;/* sleeps begun */ }; -static uint64_t now_ns (void ) -{ -struct timespec ts; - -clock_gettime (CLOCK_MONOTONIC , &ts); -return ts.tv_sec *NSEC_PER_SEC + ts.tv_nsec; -} - -static void sleep_ns (uint64_t ns) -{ -struct timespec ts = { .tv_sec = ns /NSEC_PER_SEC , .tv_nsec = ns %NSEC_PER_SEC }; - -while (nanosleep (&ts, &ts) && errno ==EINTR ) - ; -} -static void sleep_until (uint64_t t) {struct timespec ts = { .tv_sec = t /NSEC_PER_SEC , .tv_nsec = t %NSEC_PER_SEC };
@@ -518,14 +504,6 @@ static sem_t owner_locked, owner_go;static pid_t owner_tid;static int owner_ms;/* the owner's CPU time in its first period */ -static uint64_t thread_time_ns (void ) -{ -struct timespec ts; - -clock_gettime (CLOCK_THREAD_CPUTIME_ID , &ts); -return ts.tv_sec *NSEC_PER_SEC + ts.tv_nsec; -} -/* * Wait on @s for at most @ms, retrying on signals. Return 0, or -1 on a * timeout or an error.
@@ -1081,16 +1059,6 @@ static int test_sysrq_running_owner(int normalized)return ret; } -static void report (const char *name,int ret) -{ -if (ret ==KSFT_PASS ) -ksft_test_result_pass ("%s\n" , name); -else if (ret ==KSFT_SKIP ) -ksft_test_result_skip ("%s\n" , name); -else -ksft_test_result_fail ("%s\n" , name); -} -int main (void ) {int ret;
tools/testing/selftests/sched/deadline/deadline_util.h new file +106 -0
106 lines not on this page · raw message
Signature · 1 line
2.47.3