sched/deadline: bandwidth accounting fixes and selftests
Subsystems (7)
v10 cover sched/deadline: bandwidth accounting fixes and selftests
Hi Ingo, Juri,
This is v10 of this series. It applies on top of the first series [1], with its updated patch 10 [3]. v10 fixes the findings of the Sashiko AI review of v9, and the older bugs that v9 left for a follow-up. It adds six fixes and 14 cases. The changes since v9 are at the end.
This series fixes 32 bugs of SCHED_DEADLINE. Most of them are in its bandwidth accounting. As Juri asked, the patches come in groups, one per theme. Each group ends with the cases of deadline_bw_test that test its fixes. The fixes carry Cc: stable. The test patches do not. Patch 23 needs commit 8671bad873eb ("sched: Do not call __put_task_struct() on rt if pi_blocked_on is set") on stable trees older than v6.17.
One of the bugs takes the kernel down (patch 13). Three let an owner in the rtmutex chain walk keep a donor that can exit. The owner would then read freed memory (patches 21 to 23). These also close the known limit that patch 11 of the first series describes. One leaves a task that never runs again (patch 26). Most of the others leave total_bw of a root domain, or this_bw of a runqueue, wrong. The root domain then admits deadline tasks beyond its capacity, or refuses every one.
Juri, your fix 1/4 that Ingo dropped from sched/urgent is patch 3. The other three are in the first series.
The groups
==========
The rebuild of the root domains' accounting (1-6):
1 Keep the bandwidth of departing tasks across a root domain rebuild. 2 Rebuild the root domains' accounting from the task list. 3 Record the root domain that holds a task's bandwidth. 4 Leave out of a rebuild a dying task that released its bandwidth. 5 Add deadline_rebuild_test. 6 Add the 7 cases of deadline_bw_test for this group.
cpuset attach (7-12):
7 Move a task's bandwidth only where an attach made room for it. 8 Wait for an attach before rebuilding the sched domains. 9 Make a policy or affinity change of a deadline task wait for a
cpuset attach in progress.
10 Give back the bandwidth an attach did not move.
11 Reserve an attach's bandwidth in the root domain each task moves to. 12 Add the 24 cases of deadline_bw_test for this group.
The root domain of a task, with partitions and isolcpus=domain (13-18):
13 Make a rebuild count a task in the root domain it runs in. 14 Move a task's bandwidth to the root domain it runs in. 15 Pick a CPU for a task in the root domain that counts its
bandwidth, when that root domain has one.
16 Refuse to take offline the last CPU a task has in the root domain
that counts its bandwidth. Also refuse an offline that moves a deadline task of a child partition to another root domain.
17 Refuse a cpuset change that leaves a deadline task no CPU in the
root domain that counts its bandwidth.
18 Add the 28 cases of deadline_bw_test for this group.
extra_bw (19-20):
19 Count only the active CPUs when a change of total_bw is spread over
extra_bw, at an offline migration and at a rebuild.
20 Divide that change on 64 bits: a sum of bandwidths can pass S32_MAX.
The rtmutex chain walk (21-24):
21 Continue the chain walk when the deadline donor of a waiter
changes, so that no owner keeps a donor that can exit.
22 Do not stop the chain walk at an owner whose top waiter changed.
The next owner then gets the new priority or donor.
23 Hold a reference on the deadline donor of a boosted task. 24 Add the 2 cases of deadline_bw_test for this group.
Deadline boosts (25-31):
25 Start a new period at a deboost in the class, if the boost
refilled the task's runtime from its donor.
26 Replenish a throttled task that a waiter boosts while it sleeps, so
that it runs again.
27 Count in this_bw the bandwidth a boosted task is given. 28 Give a deadline to a task that enters the class while it waits on
an rt_mutex.
29 Start a new period at a deboost out of the class, if the boost
refilled the task's runtime from its donor. Its bandwidth is then released at once.
30 Give a new period to a sleeping task that FUTEX_CMP_REQUEUE_PI
requeues onto an rt_mutex, if its deadline is 0 or has passed.
31 Add the 10 cases of deadline_bw_test for this group.
Parameter changes (32-39):
32 Cancel the replenishment timer with the parameters.
33 Cap the runtime left of a constrained task at dl_runtime when its
parameters change.
34 Check the runtime left of a task that comes back to the class
runnable, as a wakeup does.
35 Check the runtime left of a task whose parameters change. 36 Reschedule a running task that its enqueue throttles. 37 Keep the start of the period across a change of dl_deadline. 38 Boost an owner that cannot run on its own reservation: one that is
throttled, or past its deadline and before its next period.
39 Add the 15 cases of deadline_bw_test for this group.
Patch 40 moves the helpers that the three deadline selftests share to a header.
Patches 3 to 17 go together. 3 records the root domain that counts a task's bandwidth. The fixes from 4 to 17 fix the paths that the record exposes. Patch 19 changes code that these fixes rework, so it comes after them. Patch 27 completes "sched/deadline: Fix this_bw when a task is changed during a remote wakeup", of the first series. Patch 38 needs patches 36 and 37, so it comes after them.
Peter, these patches change the rtmutex code or the PI boost:
- 21: a waiter records its deadline donor with its sort keys.
- 22: two early stops of the chain walk are removed. The early stop also hits RT priorities.
- 23: a boosted task holds a reference on its donor. dl_set_donor() takes it with tryget_task_struct(), since it reads the donor without the waiter's locks.
- 30: task_blocks_on_rt_mutex() refreshes the deadline of a task that FUTEX_CMP_REQUEUE_PI requeues.
- 38: rt_mutex_setprio() boosts a deadline owner that cannot run on its own reservation.
These patches also touch the core scheduler:
- 3: the affinity check of a deadline task in __sched_setscheduler().
- 9: __sched_setscheduler() and sched_setaffinity() wait for a cpuset attach in progress.
- 11: __set_cpus_allowed_ptr_locked() steers a task to the CPU that an attach reserved.
- 15: select_fallback_rq() and __set_cpus_allowed_ptr_locked() keep a deadline task in its root domain.
Waiman, patches 16 and 17 add refusals. A CPU offline or a cpuset.cpus write fails with EBUSY if it leaves a deadline task no CPU in the root domain that counts it. A write that makes a partition root, or makes it grow, still succeeds. cpuset then makes the partition invalid, with a new reason. This needs a cpuset with CPUs of two root domains, and admission control on. Patch 16 also refuses an offline that makes a child or a sibling partition invalid, when a deadline task in it would change root domain.
Known limits
============
These cases stay open:
- Patch 1: a task that left the class keeps its bandwidth until its 0-lag time, unless a rebuild drops it earlier.
- Patch 14: a task can run in a root domain that does not count it. Its bandwidth then moves there with no check for room, as at an offline migration upstream.
- Patch 15: a wakeup can pick a CPU while the sched domains are rebuilt. The task's bandwidth can then move to the other root domain with no check for room. This was found by reading.
- Patches 16 and 17 refuse nothing during a suspend, or with admission control off. A task left with no CPU in its root domain then moves, with its bandwidth.
- Patch 17: a partition that grows can make its child partition valid. The parent's tasks were not admitted on the child's CPUs. A rebuild accepts the same split today.
- A cpuset with isolated and housekeeping CPUs still admits a deadline
task: the XXX in dl_bw_capacity() that Juri pointed at in the v5 thread.
- A waiter of an rt_mutex keeps the deadline it blocked with, as on v7.4-rc2. A constrained waiter woken past its deadline is throttled. The lock then has no owner to boost for up to dl_period - dl_deadline. Juri, should a waiter that gets the lock go on with its late job, as a queued task past its deadline does? That changes the CBS rules, so I left it out.
- A waiter does not boost an owner that runs out of runtime after the waiter blocked. Nor one that wakes up in its constrained gap then. The owner stays throttled until its next period, as on v7.4-rc2.
Testing
=======
On top of the first series, the 141 cases pass wherever they run, with lockdep and no kernel warning. They ran in 20 QEMU guests of 4 to 8 vCPUs:
- with proxy execution on and off.
- with PREEMPT_RT.
- without CONFIG_CPUSETS.
- with CONFIG_RT_GROUP_SCHED.
- booted with isolcpus=domain on CPUs 1-2, 0-1 or 6-7, some with PREEMPT_RT.
The 26 cases that need isolated CPUs skip in the guests without them. No
guest combines isolcpus=domain with a kernel without CONFIG_CPUSETS:
such a kernel hits a BUG at boot on v7.4-rc2 [2].
At each test patch, the cases added so far pass in 12 guests of the same kinds. Three guests with kmemleak ran the whole series: no leak.
Each fix names the cases that test it. The test patches list the cases that fail on v7.4-rc2. Three changes have no case:
- patch 20: it needs more than 2048 CPUs of deadline bandwidth.
- patch 23: it needs a chain walk preempted at the right step.
- the change of patch 15 in dl_task_offline_migration(): it needs a timer that fires between a CPU going inactive and the cpuset update.
Each of the 40 patches builds alone, on top of the first series. At each test patch, the series also builds without CONFIG_CPUSETS, with PREEMPT_RT and with CONFIG_RT_GROUP_SCHED. The selftests build there with no warning. Each patch also builds with tinyconfig, without CONFIG_RT_MUTEXES. The cpuset selftest test_cpuset_prs.sh passes, with and without PREEMPT_RT.
These were not tested:
- cgroup v1 cpusets.
- threaded cgroups: every move is a write to cgroup.procs.
- the sched_ext server.
- CPUs of unequal capacity.
How this was developed
======================
An LLM coding assistant wrote these patches, their messages and the tests, under my direction and review. I know the concepts behind SCHED_DEADLINE, but not its implementation well. So I ask those who know this code for a careful reading of the fixes. A reproducer that stops failing shows that the symptom goes away. It does not show that the fix is the one you would have written.
The bugs came from:
- flaky cgroup selftests of a feature I am developing.
- the review of Juri's four fixes, and his review of v5.
- building deadline_bw_test, and reading the code.
- the Sashiko AI reviews of v1 to v9, and of the first series. Each finding was checked against the code first.
- second LLM reads of the fixes and of the harder findings. They found the gaps fixed in patches 15, 23 and 28. One of them also proposed the design of patch 38.
- kmemleak, which found a leaked task_struct in an early version of patch 23.
Changes since v9
================
From the Sashiko AI review of v9:
- 16: an offline that makes a sibling partition invalid is refused too. The partition left with tasks and no CPU makes its parent take the CPUs of a later child. hotplug_sibling_partition_refused tests it.
- 17: the check reads the CPU mask of the task. In a migrate_disable() section, cpus_ptr holds one CPU only.
- 22: the second check that stopped the walk early is removed too. The subject changed with it.
- 23 (new): a boosted task holds a reference on its donor. A chain walk can be preempted, and the donor can exit before it reaches the owner.
- 25: a deboost in the class cancels the inactive timer that the donor's refill armed. deboost_in_asleep_after_refill tests it.
- 28: a new donor is detected from the donor that dl_set_donor() installed, not from a second read of the waiter's donor.
- 31: in deboost_out_asleep_after_refill, the main thread reads total_bw, not the donor. A slow read could move the donor's 0-lag time past the check.
- 35: rt_mutex_setprio() deboosts a task with new parameters before it compares it with its top waiter. sched_setattr() checks a task blocked on an rt_mutex at once, also when it comes back to the class. A task asleep on anything else keeps the check for its wakeup.
- 36 (new): a running task throttled at its enqueue is rescheduled.
- 37 (new): the start of a period is kept across a change of dl_deadline.
- 38 (new): a waiter boosts an owner that cannot run on its own reservation. At the deboost, the owner gets back its own deadline and runtime.
From the follow-up list of v9, and older reviews:
- 3: sched_setattr() can make a deadline task of a fair task moved asleep to another partition. It admits it in the root domain of an active CPU of its new mask. So in move_became_deadline_no_room, a change after the move now fails with EBUSY, not EPERM.
- 15: an offline migration keeps a task in its root domain before it warns.
- 17: a valid partition recovery is no longer refused with PERR_DLTASK.
- 20 (new): the 32-bit cast of a bandwidth sum.
- 30 (new): the deadline of a task requeued onto an rt_mutex.
- 35: a comment in dl_time_to_zerolag() explains the 0-lag time between a change of parameters and its check, as Juri asked.
- 40 (new): the shared helpers of the deadline selftests.
Based on the first series [1], with its updated patch 10 [3]. That series is based on tip sched/urgent, ec6f6731538e (v7.4-rc2).
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/all/d975f317a4f9f79d2300b68ad76664755459ba36.1795181168.git.mathieu.desnoyers@efficios.com/
v9: https://lore.kernel.org/all/[email protected]/
v8: https://lore.kernel.org/all/[email protected]/
v7: https://lore.kernel.org/all/[email protected]/
v6: https://lore.kernel.org/all/[email protected]/
v5: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/
Mathieu Desnoyers (40):
sched/deadline: Keep departing bandwidth across a root domain rebuild sched/deadline: Rebuild the root domains' accounting from the task
list
sched/deadline: Record the root domain holding a task's bandwidth sched/deadline: Leave a dying task out of a rebuild of the accounting selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain
rebuild
selftests/sched: Test the root domain rebuild in deadline_bw_test sched/deadline: Move a task's bandwidth only where room was made cgroup/cpuset: Wait for an attach before rebuilding the sched domains
sched: Make deadline policy and affinity changes wait for a cpuset
attach
cgroup/cpuset: Give back the bandwidth an attach did not move cgroup/cpuset: Reserve the bandwidth in the root domain a task moves
to
selftests/sched: Test cpuset moves in deadline_bw_test sched/deadline: Make a rebuild count a task in the root domain it runs
in
sched/deadline: Move a task's bandwidth to the root domain it runs in sched/deadline: Keep a task in the root domain that counts its
bandwidth
sched/deadline: Keep the last CPU a task has in its root domain cgroup/cpuset: Refuse to take a deadline task out of its root domain selftests/sched: Test the root domain of a task in deadline_bw_test sched/deadline: Count only active CPUs when spreading extra_bw sched/deadline: Do not truncate a bandwidth to 32 bits in
__dl_add/sub()
rtmutex: Continue the chain walk when a waiter's deadline donor
changes
rtmutex: Do not stop the chain walk at an owner whose top waiter
changed
sched/deadline: Hold a reference on the deadline donor of a boosted
task
selftests/sched: Test the rtmutex chain walk in deadline_bw_test sched/deadline: Reset the donor's runtime at a deboost in the class sched/deadline: Replenish a throttled task boosted while asleep sched/deadline: Count the bandwidth a boosted task is given sched/deadline: Start a period for a task that enters the class
blocked
sched/deadline: Start a period at a deboost out of the class sched/deadline: Start a period for a task requeued onto an rt_mutex selftests/sched: Test deadline boosts in deadline_bw_test sched/deadline: Cancel the replenishment timer with the parameters sched/deadline: Cap the runtime left of a constrained task at
dl_runtime
sched/deadline: Check the runtime of a task that comes back runnable sched/deadline: Check the runtime of a task whose parameters change sched/deadline: Reschedule a running task throttled at its enqueue sched/deadline: Keep the start of the period across a change of
dl_deadline
sched/deadline: Boost an owner that cannot run on its own reservation selftests/sched: Test parameter changes in deadline_bw_test selftests/sched: Share the helpers of the deadline tests
include/linux/cpuset.h | 28 +- include/linux/sched.h | 64 + include/linux/sched/deadline.h | 41 +- kernel/cgroup/cgroup.c | 3 - kernel/cgroup/cpuset-internal.h | 15 +- kernel/cgroup/cpuset.c | 792 +- kernel/locking/rtmutex.c | 52 +- kernel/locking/rtmutex_api.c | 2 +- kernel/locking/rtmutex_common.h | 3 + kernel/sched/core.c | 164 +- kernel/sched/deadline.c | 1721 +++- kernel/sched/sched.h | 81 + kernel/sched/syscalls.c | 103 +- kernel/sched/topology.c | 11 + .../selftests/sched/deadline/.gitignore | 1 + .../testing/selftests/sched/deadline/Makefile | 5 +- .../sched/deadline/deadline_bw_test.c | 8231 ++++++++++++++++- .../sched/deadline/deadline_rebuild_test.c | 504 + .../sched/deadline/deadline_switch_test.c | 36 +- .../selftests/sched/deadline/deadline_util.h | 106 + 20 files changed, 11432 insertions(+), 531 deletions(-) create mode 100644 tools/testing/selftests/sched/deadline/deadline_rebuild_test.c create mode 100644 tools/testing/selftests/sched/deadline/deadline_util.h
base-commit: ec6f6731538e0d45872eb279fdeb477bed152d6f
prerequisite-patch-id: 5e2478ffdfb7583751bf44bdf87fb930bc23551b
prerequisite-patch-id: d5e5ef986ff2d9bc39a87b56e96a2acbd3158687
prerequisite-patch-id: 99e47f80396008ee2b2576157d37842481ce52d2
prerequisite-patch-id: a10824f98c11a4c902dad55cab3ec03b3dbe2c8a
prerequisite-patch-id: 8959e5a408641b4fa81bc79737cd17d5f1b03389
prerequisite-patch-id: 16acf1d18f01d20310d6db767dfbec2f072e0372
prerequisite-patch-id: e1946bb015a7b14ed4e2cd0421e5e30b0c7afdda
prerequisite-patch-id: 4daf48a89779fbd8c8bc5fc1d7a7d6df9bca2d9d
prerequisite-patch-id: 980d8045f9fe8c3757bb3d82c61d68e321cf74bb
prerequisite-patch-id: b2d75d7b3eebbda11af43b3917b1c99e0cce2725
prerequisite-patch-id: fb69a0665dc38ca9022cc2bd75234a90e2035b7f
prerequisite-patch-id: 7a115ab83ad96ffaf8e833e0eaeb75f859af2e79
prerequisite-patch-id: 8574b50d81a09ec6091e42d65338adcab2986d71
prerequisite-patch-id: 1aa25df96d60d30f549c71fdfba5589cb239928f
prerequisite-patch-id: d38384e0b7d043a3556ee02fb09316a6227038da
prerequisite-patch-id: 83ded04a76ecf973e2b8c6e04ff83c2b7278915a
prerequisite-patch-id: b8f5d9f9146e887050bbd8ad30463fd81cad9411
Signature · 1 line
2.47.3
PATCH v10 1/40 sched/deadline: Keep departing bandwidth across a root domain rebuild
A task that leaves SCHED_DEADLINE, or dies, keeps its bandwidth in total_bw until its 0-lag time. This keeps admission control on the safe side. Then inactive_task_timer() removes it from the root domain of the task's CPU. Two things break this:
- A rebuild of the accounting clears every root domain, then adds back the deadline tasks of the cpusets. It misses a task that left SCHED_DEADLINE, or a dead one. Their bandwidth is dropped, and the timer removes it again.
- A task that left SCHED_DEADLINE can also move to another root domain. The timer then takes total_bw from that one, which never had it.
Either way, a root domain admits deadline tasks beyond its capacity until the next rebuild.
Fix this with dl_departed_list. A departing task goes on it with the CPU it is on. A rebuild puts its bandwidth back in that CPU's root domain. The timer removes it from there, and so does sched_dl_overflow() for a task that returns to SCHED_DEADLINE.
Priority inheritance can boost a departed task back into the class, after it moved to another root domain. The boost cancels the timer and takes the task off the list. Keep the CPU recorded then. When the boost ends, the bandwidth is taken from the root domain that counts it, not from the one the task runs in. The pi_departed_moved case, added to deadline_bw_test later in this series, tests this.
A rebuild still drops the bandwidth of a task whose CPU is inactive, or that dies during the rebuild. Each task records a count of the rebuilds, dl_bw_gen, with its bandwidth. The timer removes nothing if a rebuild dropped the bandwidth since.
Make dl_cookie and dl_bw_visited() static: only deadline.c uses them.
deadline_rebuild_test, added later in this series, fails every time on v7.3 and v7.4-rc2 kernels. A one-CPU partition root admits two tasks of 600ms every second there, where it has room for one. It passes with this patch.
--- include/linux/sched.h | 16 +++ include/linux/sched/deadline.h | 5 +- kernel/cgroup/cpuset.c | 11 +- kernel/sched/deadline.c | 254 +++++++++++++++++++++++++++++---- 4 files changed, 249 insertions(+), 37 deletions(-)
include/linux/sched.h +16 -0
@@ -739,6 +739,22 @@ struct sched_dl_entity {*/ struct hrtimer inactive_timer; +/* +* A task that leaves SCHED_DEADLINE, or dies, before its "0-lag +* time" keeps its bandwidth in its root domain until the inactive +* timer removes it. Meanwhile, @dl_departed_node links the task on +* the list that a rebuild of the root domains' accounting reads. +* @dl_departed_cpu is a CPU of the root domain that counts the +* bandwidth. It stays recorded while priority inheritance boosts the +* task back into the class. Otherwise @dl_departed_cpu is -1. +* +* @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. +*/ +struct list_head dl_departed_node; +int dl_departed_cpu; +unsigned int dl_bw_gen; +/* * Bits for DL-server functionality. Also see the comment near * dl_server_update().
include/linux/sched/deadline.h +1 -4
@@ -32,7 +32,7 @@ static inline bool dl_time_before(u64 a, u64 b)struct root_domain ;extern void dl_add_task_root_domain (struct task_struct *p);extern void dl_clear_root_domain (struct root_domain *rd); -extern void dl_clear_root_domain_cpu (int cpu); +extern void dl_clear_root_domains (void );/* * Return whether moving DL task @p to @new_mask requires moving DL * bandwidth accounting between root domains. This helper is specific to
@@ -43,9 +43,6 @@ extern void dl_clear_root_domain_cpu(int cpu);extern bool dl_task_needs_bw_move (struct task_struct *p,const struct cpumask *new_mask); -extern u64 dl_cookie; -extern bool dl_bw_visited (int cpu,u64 cookie); -static inline bool dl_server (struct sched_dl_entity *dl_se) {return dl_se->dl_server;
kernel/cgroup/cpuset.c +2 -9
@@ -985,22 +985,15 @@ void dl_rebuild_rd_accounting(void) {struct cpuset *cs =NULL ;struct cgroup_subsys_state *pos_css; -int cpu; -u64 cookie = ++dl_cookie;lockdep_assert_cpuset_lock_held ();lockdep_assert_cpus_held ();lockdep_assert_held (&sched_domains_mutex); +dl_clear_root_domains (); +rcu_read_lock (); -for_each_possible_cpu (cpu) { -if (dl_bw_visited (cpu, cookie)) -continue ; - -dl_clear_root_domain_cpu (cpu); - } -cpuset_for_each_descendant_pre (cs, pos_css, &top_cpuset) {if (cpumask_empty (cs->effective_cpus)) {
kernel/sched/deadline.c +230 -24
349 lines not on this page · raw message
Signature · 1 line
2.47.3
PATCH v10 2/40 sched/deadline: Rebuild the root domains' accounting from the task list
In reply to Mathieu DesnoyersA rebuild of the root domains, as on CPU hotplug, clears their deadline accounting. dl_rebuild_rd_accounting() then adds back the deadline tasks of each cpuset. Without CONFIG_CPUSETS, it is a stub. So the first hotplug drops every deadline task's bandwidth, and their releases wrap total_bw around. In testing, a root domain admitted 9 tasks of 10ms every 100ms before a hotplug with deadline sleepers, and none after.
With cpusets, the rebuild skips a cpuset whose nr_deadline_tasks is 0. This count spares suspend and resume a costly walk of all tasks. An attach moves it by a tally taken in cpuset_can_attach(). A task that leaves the class between the tally and the move is taken off the old cpuset twice. Its count can then reach 0 with a deadline task in it. Every rebuild would then drop that task's bandwidth.
Fix both by walking the task list, with or without cpusets. The walk finds every deadline task, so the count and its hooks go. It holds tasklist_lock for reading, so fork and exit wait meanwhile. It takes the pi_lock of deadline tasks only. The walk that the count avoided took the pi_lock of every task. It leaves out an exited task with a dl_bw of 0. It also leaves out a zombie whose bandwidth an earlier rebuild dropped, as dl_clear_root_domains() does.
The walk visits every thread, and a suspend or a resume runs one rebuild for each CPU. So keep one count for the whole system,dl_nr_bw_tasks: the number of tasks whose dl_bw is not 0. __setparam_dl() and __dl_clear_params() are the only places that change the dl_bw of a task, and they update the count. With a count of 0, the rebuild does not walk the task list, as Juri Lelli suggested.
In testing, a 64-vCPU guest held 10000 threads and no deadline task. A rebuild took 0.7 to 1.2 ms without the count, and 0.03 ms with it.
Without cpusets, a new dl_accounting_mutex keeps admissions out of a rebuild, as cpuset_mutex does with them.
Three hotplug cases, added to deadline_bw_test later in this series, fail without cpusets. With this patch, its five hotplug cases pass, in 4 VMs. Its death_zombie_hotplug case tests the zombie. The Sashiko AI reviews of this series found the wrong count, the exited tasks and the zombie.
--- include/linux/cpuset.h | 9 -- include/linux/sched/deadline.h | 16 ++++ kernel/cgroup/cgroup.c | 3 - kernel/cgroup/cpuset-internal.h | 6 +- kernel/cgroup/cpuset.c | 67 +------------- kernel/sched/deadline.c | 154 ++++++++++++++++++++++++++------ kernel/sched/syscalls.c | 14 +-- kernel/sched/topology.c | 8 ++ 8 files changed, 163 insertions(+), 114 deletions(-)
include/linux/cpuset.h +0 -9
@@ -72,8 +72,6 @@ extern int cpuset_init(void);extern void cpuset_init_smp (void );extern void cpuset_force_rebuild (void );extern void cpuset_update_active_cpus (void ); -extern void inc_dl_tasks_cs (struct task_struct *task); -extern void dec_dl_tasks_cs (struct task_struct *task);extern void cpuset_lock (void );extern void cpuset_unlock (void );extern void lockdep_assert_cpuset_lock_held (void );
@@ -129,7 +127,6 @@ static inline int cpuset_do_page_mem_spread(void)extern bool current_cpuset_is_being_rebound (void ); -extern void dl_rebuild_rd_accounting (void );extern void rebuild_sched_domains (void );extern void cpuset_print_current_mems_allowed (void );
@@ -194,8 +191,6 @@ static inline void cpuset_update_active_cpus(void)partition_sched_domains (1 ,NULL ,NULL ); } -static inline void inc_dl_tasks_cs (struct task_struct *task) { } -static inline void dec_dl_tasks_cs (struct task_struct *task) { }static inline void cpuset_lock (void ) { }static inline void cpuset_unlock (void ) { }static inline void lockdep_assert_cpuset_lock_held (void ) { }
@@ -273,10 +268,6 @@ static inline bool current_cpuset_is_being_rebound(void)return false; } -static inline void dl_rebuild_rd_accounting (void ) -{ -} -static inline void rebuild_sched_domains (void ) {guard (cpus_read_lock)();
include/linux/sched/deadline.h +16 -0
@@ -33,6 +33,22 @@ struct root_domain;extern void dl_add_task_root_domain (struct task_struct *p);extern void dl_clear_root_domain (struct root_domain *rd);extern void dl_clear_root_domains (void ); +extern void dl_rebuild_rd_accounting (void ); +/* +* Keep a rebuild of the root domains' accounting and an admission of +* bandwidth from running at the same time. With cpusets, cpuset_mutex +* does it, since every rebuild comes from the cpuset code. Without +* cpusets, dl_accounting_lock() takes a mutex of its own. +*/ +#ifdef CONFIG_CPUSETS +static inline void dl_accounting_lock (void ) { } +static inline void dl_accounting_unlock (void ) { } +static inline void lockdep_assert_dl_accounting_held (void ) { } +#else +extern void dl_accounting_lock (void ); +extern void dl_accounting_unlock (void ); +extern void lockdep_assert_dl_accounting_held (void ); +#endif /* * Return whether moving DL task @p to @new_mask requires moving DL * bandwidth accounting between root domains. This helper is specific to
kernel/cgroup/cgroup.c +0 -3
@@ -7240,9 +7240,6 @@ static void do_cgroup_task_dead(struct task_struct *tsk)if (thread_group_leader (tsk) &&atomic_read (&tsk->signal->live))list_add_tail (&tsk->cg_list, &cset->dying_tasks); -if (dl_task (tsk)) -dec_dl_tasks_cs (tsk); -WARN_ON_ONCE (cgroup_task_frozen (tsk));if (unlikely (!(tsk->flags &PF_KTHREAD ) &&test_bit (CGRP_FREEZE , &task_dfl_cgroup (tsk)->flags)))
kernel/cgroup/cpuset-internal.h +1 -5
@@ -166,11 +166,7 @@ struct cpuset {*/ bool remote_partition_updating; -/* -* number of SCHED_DEADLINE tasks attached to this cpuset, so that we -* know when to rebuild associated root domain bandwidth information. -*/ -atomic_t nr_deadline_tasks; +/* Number of SCHED_DEADLINE tasks the attach in progress moves here. */ int nr_migrate_dl_tasks;/* DL bandwidth that needs destination reservation for this attach. */ u64 sum_migrate_dl_bw;
kernel/cgroup/cpuset.c +1 -66
91 lines not on this page · raw message
kernel/sched/deadline.c +128 -26
250 lines not on this page · raw message
kernel/sched/syscalls.c +9 -5
@@ -568,6 +568,7 @@ int __sched_setscheduler(struct task_struct *p,if (pi && (dl_policy (policy) ||dl_policy (p->policy))) { cpuset_locked = true;cpuset_lock (); +dl_accounting_lock (); }/*
@@ -596,9 +597,7 @@ int __sched_setscheduler(struct task_struct *p,* Reject SCHED_DEADLINE on exited tasks. A zombie will never be * scheduled again, so sched_dl_overflow() would charge total_bw * that nothing will ever release. And reject a change of policy of -* one in the class: cgroup_task_dead() took it off the count of -* deadline tasks of its cpuset, which switched_from_dl() would -* decrement a second time. +* one in the class: its bandwidth went at its last schedule. */ if ((dl_policy (policy) ||dl_task (p)) && p->exit_state) { retval = -ESRCH ;
@@ -669,6 +668,7 @@ int __sched_setscheduler(struct task_struct *p, policy = oldpolicy =-1 ;task_rq_unlock (rq, p, &rf);if (cpuset_locked) { +dl_accounting_unlock ();cpuset_unlock (); cpuset_locked = false; }
@@ -779,8 +779,10 @@ int __sched_setscheduler(struct task_struct *p,task_rq_unlock (rq, p, &rf);if (pi) { -if (cpuset_locked) +if (cpuset_locked) { +dl_accounting_unlock ();cpuset_unlock (); + }rt_mutex_adjust_pi (p); }
@@ -792,8 +794,10 @@ int __sched_setscheduler(struct task_struct *p, unlock:task_rq_unlock (rq, p, &rf); -if (cpuset_locked) +if (cpuset_locked) { +dl_accounting_unlock ();cpuset_unlock (); + }return retval; }
kernel/sched/topology.c +8 -0
@@ -3528,7 +3528,15 @@ static void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_newvoid partition_sched_domains (int ndoms_new,cpumask_var_t doms_new[],struct sched_domain_attr *dattr_new) { +/* +* The root domains attached here count no task's bandwidth until +* dl_rebuild_rd_accounting() at the end. No admission of bandwidth may +* run meanwhile. With cpusets, the callers hold cpuset_mutex, which +* keeps admissions out. Without cpusets, dl_accounting_lock() does it. +*/ +dl_accounting_lock ();sched_domains_mutex_lock ();partition_sched_domains_locked (ndoms_new, doms_new, dattr_new);sched_domains_mutex_unlock (); +dl_accounting_unlock (); }
Signature · 1 line
2.47.3
PATCH v10 3/40 sched/deadline: Record the root domain holding a task's bandwidth
In reply to Mathieu DesnoyersAdmission, moves and releases of a deadline task's bandwidth find the root domain that counts it from the CPU the task is on. That CPU can be in another root domain. Two cases show it.
Commit 7f51412a415d ("sched/deadline: Fix bandwidth check/update when migrating tasks between exclusive cpusets") moves a deadline task's bandwidth in two halves. cpuset_can_attach() reserves it in the destination. set_cpus_allowed_dl() frees it in the root domain of the task's runqueue. Since commit 5dd74441cbf4 ("cgroup/cpuset: Reserve DL bandwidth only for root-domain moves"), both halves move nothing when that root domain overlaps the destination. But a sleeping task stays on the CPU it last ran on. So a sleeping task moved to a partition and back leaves its bandwidth in the partition:
admin task T
makes P a partition root of CPU 3
CPU 3 is now in root domain B
CPUs 0-2 are in root domain A
sched_setattr(SCHED_DEADLINE)
A: total_bw += bw of T
runs on CPU 0, then sleeps
echo T > P/cgroup.procs
cpuset_can_attach()
dl_task_needs_bw_move(): true,
A does not overlap P
B: total_bw += bw of T
set_cpus_allowed_dl()
dl_task_needs_bw_move(): true
A: total_bw -= bw of T
still asleep on CPU 0
echo T > cgroup.procs of the root cgroup
cpuset_can_attach()
dl_task_needs_bw_move(): false,
A overlaps the root cpuset
set_cpus_allowed_dl()
dl_task_needs_bw_move(): false
kill -9 T
wakes up on a CPU of A exits
A: total_bw -= bw of T
B keeps the bandwidth of T. A does not count T while T is in A, so A admits beyond its capacity. After T exits, the total_bw of A is short by the bandwidth of T, and can underflow.
The second case needs no move:
1. A deadline task T sleeps on a CPU of root domain A.
2. That CPU goes offline, and joins the default root domain.
3. The rebuild counts T in A, the root domain of its cpuset.
4. sched_setattr() takes the sleeping T out of SCHED_DEADLINE.
5. The release takes the bandwidth from the root domain of the CPU T is on: the default one.
A keeps the bandwidth of T until the next rebuild, and refuses deadline tasks that would fit.
The first case was reproduced on v7.4-rc2. The second needs "sched/deadline: Release the bandwidth of a task that leaves the class asleep", of the first series. Without it, nothing releases the bandwidth of that sleeping task. The hotplug_sleeper_switched case, added to deadline_bw_test later in this series, tests it. Without this patch and with the CPU still offline, A then admitted 63 tasks of 10% where it had room for 66, in each of 3 guests.
Fix this by recording, in dl_bw_cpu, a CPU of the root domain that counts the bandwidth. The record replaces dl_departed_cpu. It is used as follows:
- Each site that counts the bandwidth sets the record.
- Each release or move reads it. The release clears it.
- If dl_bw_gen shows that a rebuild dropped the bandwidth, nothing is taken back. set_cpus_allowed_dl() makes that test under dl_departed_lock, so that a rebuild cannot change dl_bw_gen meanwhile.
- A move skips a bandwidth of 0, and does not update its record. So sched_dl_overflow() uses the CPU the task is on for it. The move_zero_bw case of deadline_bw_test tests this.
- __sched_setscheduler() checks the affinity against the root domain of the record, as sched_dl_overflow() does. A task moved asleep into a partition got EPERM for any change until it woke up (move_asleep_setattr). A task that enters the class is checked against an active CPU of its new mask (move_asleep_fair_setattr).
- sched_setaffinity() reads the record too. Testing the root domain of the CPU the task is on let a new mask take a task moved asleep out of the root domain that counts it (affinity_moved_asleep).
- A rebuild drops departed bandwidth whose CPU is inactive. So a departing task records the CPU it departs from, if that CPU is active and in the same root domain.
A move would take the bandwidth of a sugov kthread, or of a task boosted into the class with no bandwidth of its own, from a root domain that never held it. The rebuild leaves these tasks out. A move now leaves them out too.
Weiheng Qiu reported the moved sleeper, and Juri Lelli analysed it. His fix, linked below, took the first active CPU of the task's mask. A mask can have none during CPU hotplug. Tip dropped the fix after a sugov kthread hit its BUG_ON() there. The Sashiko AI reviews of this series found the other cases.
--- include/linux/sched.h | 10 +- kernel/sched/deadline.c | 271 +++++++++++++++++++++++++++++----------- kernel/sched/syscalls.c | 22 +++- 3 files changed, 221 insertions(+), 82 deletions(-)
include/linux/sched.h +6 -4
@@ -744,15 +744,17 @@ struct sched_dl_entity {* time" keeps its bandwidth in its root domain until the inactive * timer removes it. Meanwhile, @dl_departed_node links the task on * the list that a rebuild of the root domains' accounting reads. -* @dl_departed_cpu is a CPU of the root domain that counts the -* bandwidth. It stays recorded while priority inheritance boosts the -* task back into the class. Otherwise @dl_departed_cpu is -1. +* Otherwise that node is empty. +* +* @dl_bw_cpu is a CPU of the root domain that counts the bandwidth, +* or -1. Each site that counts the bandwidth sets it. Each site that +* removes or moves the bandwidth reads it. * * @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. */ struct list_head dl_departed_node; -int dl_departed_cpu; +int dl_bw_cpu;unsigned int dl_bw_gen;/*
kernel/sched/deadline.c +196 -75
484 lines not on this page · raw message
kernel/sched/syscalls.c +19 -3
@@ -642,7 +642,14 @@ int __sched_setscheduler(struct task_struct *p,#endif /* CONFIG_RT_GROUP_SCHED */ if (dl_bandwidth_enabled () &&dl_policy (policy) && !(attr->sched_flags &SCHED_FLAG_SUGOV )) { -cpumask_t *span = rq->rd->span; +/* +* The root domain that admits the change: that of the +* CPU recorded with the bandwidth of a deadline task. +* A task moved asleep to another cpuset is still on a +* CPU of the root domain it left. +*/ +struct root_domain *rd =cpu_rq (dl_task_admit_cpu (p))->rd; +cpumask_t *span = rd->span;/* * Don't allow tasks with an affinity mask smaller than
@@ -650,7 +657,7 @@ int __sched_setscheduler(struct task_struct *p,* will also fail if there's no bandwidth available. */ if (!cpumask_subset (span, &p->cpus_mask) || - rq->rd->dl_bw.bw ==0 ) { + rd->dl_bw.bw ==0 ) { retval = -EPERM ; goto unlock; }
@@ -1185,6 +1192,8 @@ SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,int dl_task_check_affinity (struct task_struct *p,const struct cpumask *mask) { +unsigned int cpu; +/* * If the task isn't a deadline task or admission control is * disabled then we don't care about affinity changes.
@@ -1204,9 +1213,16 @@ int dl_task_check_affinity(struct task_struct *p, const struct cpumask *mask)* if admission test is enabled, we only admit -deadline * tasks allowed to run on all the CPUs in the task's * root_domain. +* +* That is the root domain that counts the bandwidth of the task: +* the one of the CPU recorded with it. A task moved asleep to +* another cpuset is still on a CPU of the root domain it left. A +* bandwidth of 0 is counted nowhere, and no move updates its +* record: use the CPU the task is on. */ + cpu =READ_ONCE (p->dl.dl_bw) ?dl_task_root_domain_cpu (p) :task_cpu (p);guard (rcu)(); -if (!cpumask_subset (task_rq (p)->rd->span, mask)) +if (!cpumask_subset (cpu_rq (cpu)->rd->span, mask))return -EBUSY ;return 0 ;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v10 03/40] sched/deadline: Record the root domain holding a task's bandwidth
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] sched/deadline: dl_task_admit_cpu() returns offline CPU in p->cpus_mask - [Medium] sched/deadline…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] sched/deadline: dl_task_admit_cpu() returns offline CPU in p->cpus_mask
- [Medium] sched/deadline: dl_task_check_affinity() uses stale CPU when dl_bw is 0
--
commit fc9f04f79ff6b3431efcbfa8c33fd7eb5355e667
Author: Mathieu Desnoyers <[email protected]>
sched/deadline: Record the root domain holding a task's bandwidth
This commit records in dl_bw_cpu a CPU of the root domain that accounts for a deadline task's bandwidth, replacing dl_departed_cpu so that admission, affinity checks, moves, and releases operate on the root domain where the bandwidth is counted even when a sleeping task remains on a CPU from another root domain.
Quoted 4 lines
kernel/sched/deadline.c +0
-0
[ ... ]
Quoted 1 line
kernel/sched/deadline.c +0
-0@@ -3592,14 +3618,52 @@ static void task_woken_dl(struct rq *rq, struct task_struct *p)
[ ... ]
Quoted 6 lines
kernel/sched/deadline.c +6
-0+static unsigned int dl_task_admit_cpu (struct task_struct *p) +{ +unsigned int cpu; + +if (task_has_dl_policy (p) && p->dl.dl_bw && p->dl.dl_bw_cpu >=0 && + p->dl.dl_bw_gen == dl_bw_gen)
kernel/sched/deadline.c static void task_woken_dl(struct rq *rq, struct task_struct *p) · line 3660
Quoted 10 lines
kernel/sched/deadline.c +10
-0+return p->dl.dl_bw_cpu; + cpu =task_cpu (p); +if (!cpumask_test_cpu (cpu, &p->cpus_mask)) { +unsigned int other =cpumask_first_and (cpu_active_mask, &p->cpus_mask); + +if (other < nr_cpu_ids) + cpu = other; + } +return cpu; +}
[Severity: Medium]
Can dl_task_admit_cpu() return an offline CPU when p is asleep on a CPU that went offline?
For tasks in top_cpuset (or when CONFIG_CPUSETS is disabled), CPU hot-unplug does not clear the offline CPU from p->cpus_mask. If p is asleep on that CPU when sched_setattr() enters SCHED_DEADLINE (or updates a zero-bandwidth task), cpumask_test_cpu(cpu, &p->cpus_mask) is still true, so dl_task_admit_cpu() returns the offline CPU whose runqueue is attached to def_root_domain. __sched_setscheduler() and sched_dl_overflow() then check and charge def_root_domain instead of the active root domain where p will run, failing with -EBUSY when def_root_domain has no active CPUs or accounting the bandwidth in the wrong root domain.
Should the fallback also trigger when !cpu_active(cpu)?
Quoted 4 lines
kernel/sched/syscalls.c +0
-0
[ ... ]
Quoted 8 lines
kernel/sched/syscalls.c +4
-0@@ -1204,9 +1213,16 @@ int dl_task_check_affinity(struct task_struct *p, const struct cpumask *mask)* if admission test is enabled, we only admit -deadline * tasks allowed to run on all the CPUs in the task's * root_domain. +* +* That is the root domain that counts the bandwidth of the task: +* the one of the CPU recorded with it. A task moved asleep to +* another cpuset is still on a CPU of the root domain it left. A
kernel/sched/syscalls.c int dl_task_check_affinity(struct task_struct *p, const struct cpumask *mask) · line 1228
Quoted 10 lines
kernel/sched/syscalls.c +4
-1+* bandwidth of 0 is counted nowhere, and no move updates its +* record: use the CPU the task is on. */ + cpu =READ_ONCE (p->dl.dl_bw) ?dl_task_root_domain_cpu (p) :task_cpu (p);guard (rcu)(); -if (!cpumask_subset (task_rq (p)->rd->span, mask)) +if (!cpumask_subset (cpu_rq (cpu)->rd->span, mask))return -EBUSY ;return 0 ;
[Severity: Medium]
When p->dl.dl_bw is 0 and p was moved to another cpuset partition while asleep, doesn't task_cpu(p) still point to the old root domain that p left?
Because dl_task_needs_bw_move() skips zero-bandwidth tasks, p->dl.dl_bw_cpu is not updated on the move, and while p remains asleep task_cpu(p) is still a CPU of the old partition outside p->cpus_mask. Calling sched_setaffinity() before p wakes up intersects the requested mask with the new cpuset's allowed CPUs and then checks cpumask_subset() against the old root domain's span, causing sched_setaffinity() to fail with -EBUSY.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v10 03/40] sched/deadline: Record the root domain holding a task's bandwidth
Quoted 2 lines
Can
dl_task_admit_cpu()return an offline CPU when p is asleep on a CPU that went offline?
Yes. In the top cpuset, or without cpusets, the offline does not remove the CPU from cpus_mask. v11 will also fall back to an active CPU of the mask when task_cpu() is not active. A new case, hotplug_asleep_setattr, will make a deadline task of a task asleep on a CPU that went offline. Without the fix, sched_setattr() fails with EBUSY. v7.4-rc2 charges the root domain of task_cpu() too.
Quoted 2 lines
When
p->dl.dl_bwis 0 and p was moved to another cpuset partition while asleep, doesn'ttask_cpu(p)still point to the old root domain that p left?
Yes, by reading. v11 will check such a task against dl_task_admit_cpu(), as sched_setattr() does: the CPU the task runs on once it wakes. It needs a bandwidth that rounds to 0, so no case will test it.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
PATCH v10 4/40 sched/deadline: Leave a dying task out of a rebuild of the accounting
In reply to Mathieu DesnoyersA rebuild of the root domains' accounting takes the pi_lock of each deadline task, not its rq lock. A dying task past its 0-lag time releases its bandwidth at its last schedule. It holds its rq lock there, not its pi_lock. Then it clears dl_bw. The rebuild can find the task between the two steps.
In this diagram, T is a deadline task that exits on CPU 0 past its 0-lag time. bw is its dl_bw. CPU 1 rebuilds the accounting:
CPU 0 (T) CPU 1 (rebuild)
do_task_dead(): TASK_DEAD, under pi_lock
__schedule(): T's rq lock taken
task_non_contending(T)
dl_task_release_bw(T)
total_bw -= bw
dl_bw_gen recorded
record cleared
dl_rebuild_rd_accounting()
dl_clear_root_domains()
dl_bw_gen++
total_bw reset
dl_add_task_root_domain(T)
T's pi_lock taken
dl_bw != 0, dl_bw_gen old
total_bw += bw
__dl_clear_params(T): dl_bw = 0dl_bw_gen does not help, since the release came before the rebuild. The rebuild would count a bandwidth that nothing releases again. The root domain would then refuse deadline tasks it has room for, until the next rebuild.
Fix this with the record of the bandwidth, dl_bw_cpu. The release clears it under dl_departed_lock, which the walk holds. So leave out a dead task with no record. A dead task with a record has not reached its last schedule. The walk counts it as a live task. That schedule then releases the bandwidth, or puts the task on the departed list. A dead task on the departed list keeps its bandwidth until its timer, and the rebuild counts it as before.
This patch completes "sched/deadline: Rebuild the root domains' accounting from the task list". The walk of that patch finds exiting tasks, which the cpuset iterator skipped. The fix needs the record that "sched/deadline: Record the root domain holding a task's bandwidth" adds. The three go to stable together.
The Sashiko AI review of this series found this window. It also found a gap in the first version of this fix. That version also left out a task between TASK_DEAD and its last schedule, with its 0-lag time ahead.
--- kernel/sched/deadline.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
kernel/sched/deadline.c +11 -0
@@ -3830,6 +3830,17 @@ void dl_add_task_root_domain(struct task_struct *p)if (recorded <0 || !cpu_active (recorded)) goto unlock; cpu = recorded; + }else if (READ_ONCE (p->__state) ==TASK_DEAD && p->dl.dl_bw_cpu <0 ) { +/* +* Count nothing for a dead task with no record. Its last +* schedule released the bandwidth under its rq lock, and has +* not cleared dl_bw yet. The release clears the record under +* dl_departed_lock, which this walk holds. A dead task with a +* record has not reached its last schedule. Count it as a +* live task. That schedule then releases what the walk +* counted, or puts the task on the departed list. +*/ + goto unlock; } rq =cpu_rq (cpu); dl_b = &rq->rd->dl_bw;
Signature · 1 line
2.47.3
PATCH v10 5/40 selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild
In reply to Mathieu DesnoyersA task that leaves SCHED_DEADLINE, or dies, keeps its bandwidth until its 0-lag time. No selftest checks what happens to this bandwidth meanwhile.
Add deadline_rebuild_test, with three cases. In each case, a task of 400ms every second runs 380ms, then leaves SCHED_DEADLINE. Its 0-lag time is then about 570ms ahead. P is a partition root of one CPU. Before the 0-lag time:
- dies_before_rebuild: the task exits in P. Then a second partition root is made, which rebuilds the accounting of every root domain.
- switched_out_before_rebuild: the same, but the task switches to SCHED_OTHER and sleeps in P.
- moved_after_switch_out: the task switches to SCHED_OTHER outside P. Then it moves into P and dies there.
After the 0-lag time, P must admit one task of 600ms every second, and refuse a second one with EBUSY. A kernel with the bug removes the bandwidth from P's root domain at the 0-lag time. But the rebuild dropped it already, or P never had it. total_bw then wraps around, and P admits both tasks.
P and Q take the two highest online CPUs that are not isolated. The test skips without root, 3 such CPUs, the cgroup v2 cpuset controller or deadline admission control. Admission control is off when sched_rt_runtime_us is -1.
On v7.4-rc2, in 8-CPU VMs, the three cases fail. "sched/deadline: Keep departing bandwidth across a root domain rebuild" fixes them. With the whole series, the three cases pass, with and without PREEMPT_RT.
--- .../selftests/sched/deadline/.gitignore | 1 + .../testing/selftests/sched/deadline/Makefile | 2 +- .../sched/deadline/deadline_rebuild_test.c | 574 ++++++++++++++++++ 3 files changed, 576 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/sched/deadline/deadline_rebuild_test.c
tools/testing/selftests/sched/deadline/.gitignore +1 -0
@@ -1,2 +1,3 @@ deadline_switch_test +deadline_rebuild_test deadline_bw_test
tools/testing/selftests/sched/deadline/Makefile +1 -1
@@ -3,6 +3,6 @@ CFLAGS += -O2 -Wall -g -I./ $(KHDR_INCLUDES) LDLIBS += -lpthread -TEST_GEN_PROGS := deadline_switch_test deadline_bw_test +TEST_GEN_PROGS := deadline_switch_test deadline_rebuild_test deadline_bw_test include ../../lib.mk
tools/testing/selftests/sched/deadline/deadline_rebuild_test.c new file +574 -0
574 lines not on this page · raw message
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v10 05/40] selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] selftests/sched: missing sched_setaffinity() in deadline_rebuild_test --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests/sched: missing sched_setaffinity() in deadline_rebuild_test
--
commit f6538c4af07769165daa29a5b3577ecba3fd01c9
Author: Mathieu Desnoyers <[email protected]>
selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild
This commit adds deadline_rebuild_test to verify that a SCHED_DEADLINE task's remaining bandwidth before its 0-lag time is accounted properly across root domain rebuilds and cpuset partition migrations.
Quoted 5 lines
tools/testing/selftests/sched/deadline/deadline_rebuild_test.c new file +0
-0
[ ... ]
Quoted 17 lines
tools/testing/selftests/sched/deadline/deadline_rebuild_test.c new file +17
-0+/* +* Pick the two highest online CPUs that are not isolated, for P and Q. +* Return the number of such CPUs. +*/ +static int pick_cpus (void ) +{ +static char online[CPU_SETSIZE ], isol[CPU_SETSIZE ]; +char buf[4096 ]; +int cpu, n =0 ; + +if (read_file ("/sys/devices/system/cpu/online" , buf,sizeof (buf))) +return 0 ; +parse_cpulist (buf, online,CPU_SETSIZE ); +if (!read_file ("/sys/devices/system/cpu/isolated" , buf,sizeof (buf))) +parse_cpulist (buf, isol,CPU_SETSIZE ); + cpu_p = cpu_q =-1 ; +for (cpu =CPU_SETSIZE -1 ; cpu >=0 ; cpu--) {
tools/testing/selftests/sched/deadline/deadline_rebuild_test.c line 508
Quoted 10 lines
tools/testing/selftests/sched/deadline/deadline_rebuild_test.c new file +10
-0+if (!online[cpu] || isol[cpu]) +continue ; +if (cpu_p <0 ) + cpu_p = cpu; +else if (cpu_q <0 ) + cpu_q = cpu; + n++; + } +return n; +}
[Severity: Low]
Should pick_cpus() or main() also call sched_setaffinity() to pin the test process to the non-isolated online CPUs, as deadline_bw_test does?
On a system booted with isolcpus=domain, creating or removing partition roots under the root cpuset resets tasks without a user affinity mask (p->user_cpus_ptr) to all CPUs in the root cpuset, including the isolated CPUs. In test_move(), departing(NULL, SWITCHED_OUT, fd) can then run on an isolated CPU in def_root_domain rather than the housekeeping root domain, causing set_deadline() to either fail or test bandwidth departure from the wrong root domain.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH v10 05/40] selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild
Quoted 2 lines
Should
pick_cpus()ormain()also callsched_setaffinity()to pin the test process to the non-isolated online CPUs, asdeadline_bw_testdoes?
Yes. In v11, deadline_rebuild_test will run on the online CPUs that are not isolated.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
PATCH v10 6/40 selftests/sched: Test the root domain rebuild in deadline_bw_test
In reply to Mathieu DesnoyersAdd seven cases to deadline_bw_test. Most test the rebuild of the root domains' accounting, earlier in this series. They cover:
- CPU hotplug with sleeping deadline tasks, and with tasks that left the class before their 0-lag time
- a zombie deadline task during a CPU hotplug (death_zombie_hotplug)
- a priority inheritance owner that left the class, then moves (pi_departed_moved)
- sched_setaffinity() racing with sched_setattr() (affinity_vs_setattr)
The comment above each case gives its scenario. Two cases follow reproducers of Juri Lelli's sched-deadline-tests:hotplug_sleeper_switched and hotplug_no_admission_pinned.
The message of each fix names the cases that test it. These cases are not named there:
- hotplug_departed and hotplug_no_admission_pinned: "sched/deadline: Keep departing bandwidth across a root domain rebuild"
- affinity_vs_setattr tests "sched/deadline: Check SCHED_DEADLINE affinity under the rq lock", of the first series. It comes here because it fails without CONFIG_CPUSETS before these fixes.
On v7.4-rc2, in 8 VMs of 8 CPUs, six of these cases fail in every VM:
- affinity_vs_setattr
- death_zombie_hotplug
- hotplug_departed
- hotplug_no_admission_pinned
- hotplug_sleeper_switched
- pi_departed_moved
hotplug_sleepers passes there. It fails on v7.4-rc2 and with the first series without CONFIG_CPUSETS. It also fails there when isolcpus=domain,0,1 isolates the boot CPU. The rebuild fixes make it pass.
With the fixes up to here, every case passes wherever it runs.
--- .../sched/deadline/deadline_bw_test.c | 654 +++++++++++++++++- 1 file changed, 650 insertions(+), 4 deletions(-)
tools/testing/selftests/sched/deadline/deadline_bw_test.c +650 -4
@@ -31,8 +31,11 @@* housekeeping CPU. P is made anew for each case. Making it rebuilds the * accounting, which clears what an earlier case left. * -* The cases of hp_cases[] run after them, without P. They take CPUs -* offline. The test skips them if no CPU can go offline. +* The cases of hp_cases[] run after them, without P. These are the +* hotplug cases, and affinity_vs_setattr. The test skips the hotplug +* cases if no CPU can go offline. It still runs affinity_vs_setattr. +* death_zombie_hotplug takes CPUs offline too, but it is in p_cases[]: it +* needs 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.
@@ -4554,7 +4557,10 @@ static int params_waking(void)return ret; } -/* CPU hotplug, from the main process, with no P */ +/* +* CPU hotplug, from the main process, with no P. Two cases below run with +* P instead: death_zombie_hotplug and pi_departed_moved. +*/ static int hp_cpu =-1 ;
@@ -4584,6 +4590,213 @@ static void cpu_up(int cpu)ksft_print_msg ("CPU %d cannot come back online: %s\n" , cpu,strerror (-err)); } +/* +* Four deadline tasks of 30% sleep. Take hp_cpu offline and back. Once +* the sleepers are gone, the housekeeping root domain must admit what it +* did before. Skip unless 22 probe tasks fit: the sleepers take the room +* of 12, and the offline CPU that of about 10. +*/ +static int hotplug_sleepers (void ) +{ +int base =probe (NULL ), after, i, ret =KSFT_PASS ; +pid_t pid[4 ]; + +if (base <22 ) { +ksft_print_msg ("needs room for 22 probe tasks\n" ); +return KSFT_SKIP ; + } +for (i =0 ; i <4 ; i++) { + mv_state[0 ] =0 ; + pid[i] =mover (MV_ASLEEP ,30 *NSEC_PER_MSEC ); +if (pid[i] <0 ||child_up ("a deadline sleeper" ,0 )) { +while (i >=0 ) +reap (pid[i--]); +return KSFT_FAIL ; + } + } +if (cpu_online (hp_cpu,0 ) ||cpu_online (hp_cpu,1 )) { +ksft_print_msg ("offline and online of CPU %d with deadline sleepers failed\n" , + hp_cpu); + ret =KSFT_FAIL ; +cpu_up (hp_cpu); + } +for (i =0 ; i <4 ; i++) +reap (pid[i]); +sleep_ns (3 *PERIOD ); + after =probe (NULL ); +if (ret ==KSFT_PASS && after != base) { +ksft_print_msg ("hotplug with sleepers: %d probe tasks admitted before, %d after\n" , + base, after); + ret =KSFT_FAIL ; + } +return ret; +} + +/* +* Fork a task that runs on @cpu, then restores its affinity, becomes a +* deadline task of 30% and sleeps. A deadline task cannot have an +* affinity of one CPU, so the task goes to @cpu first. It sets +* mv_state[0] as mover() does. Return its pid, or -1. +*/ +static pid_t sleeper_on (int cpu) +{ +pid_t pid =fork_child ("a deadline sleeper" ); + +if (!pid) { +cpu_set_t all, one; + +CPU_ZERO (&one); +CPU_SET (cpu, &one); +if (sched_getaffinity (0 ,sizeof (all), &all) || +sched_setaffinity (0 ,sizeof (one), &one)) +_exit (1 ); +spin_ns (NSEC_PER_MSEC ); +if (sched_setaffinity (0 ,sizeof (all), &all) || +set_deadline (0 ,30 *NSEC_PER_MSEC ,PERIOD )) { + mv_state[0 ] =-1 ; +_exit (1 ); + } + mv_state[0 ] =1 ; +for (;;) +pause (); + } +return pid; +} + +/* +* A deadline task of 30% sleeps on hp_cpu. Take hp_cpu offline, then +* switch the task to SCHED_OTHER. hp_cpu has left the housekeeping root +* domain by then, but that root domain still counts the task. The switch +* must release the bandwidth there. +* +* Check this with hp_cpu still offline: bringing it back rebuilds the +* accounting, which would hide a bandwidth left behind. The reference is +* what the housekeeping root domain admits with hp_cpu offline and no +* task. Once the CPU is back and the task is gone, that root domain must +* also admit what it did at first. +*/ +static int hotplug_sleeper_switched (void ) +{ +int base =probe (NULL ), base_off, after, cpu, ret =KSFT_PASS ; +pid_t pid; + +if (cpu_online (hp_cpu,0 )) { +ksft_print_msg ("CPU %d cannot go offline\n" , hp_cpu); +cpu_up (hp_cpu); +return KSFT_SKIP ; + } + base_off =probe (NULL ); +cpu_up (hp_cpu); + mv_state[0 ] =0 ; + pid =sleeper_on (hp_cpu); +if (pid <0 ||child_up ("a deadline sleeper" ,0 )) { +reap (pid); +return KSFT_FAIL ; + } +sleep_ns (PERIOD ); + cpu =task_cpu_of (pid); +if (cpu != hp_cpu) { +ksft_print_msg ("the sleeper is on CPU %d, not on CPU %d\n" , cpu, hp_cpu); +reap (pid); +return KSFT_SKIP ; + } +if (cpu_online (hp_cpu,0 )) { +ksft_print_msg ("CPU %d cannot go offline under a deadline sleeper\n" , hp_cpu); + ret =KSFT_FAIL ; + }else if (set_other (pid)) { +ksft_print_msg ("the sleeper cannot leave SCHED_DEADLINE\n" ); + ret =KSFT_FAIL ; + }else { +sleep_ns (3 *PERIOD ); + after =probe (NULL ); +if (after != base_off) { +ksft_print_msg ("a sleeper switched out, its CPU still offline: %d probe tasks admitted, %d expected\n" , + after, base_off); + ret =KSFT_FAIL ; + } + } +cpu_up (hp_cpu); +reap (pid); +sleep_ns (3 *PERIOD ); + after =probe (NULL ); +if (ret ==KSFT_PASS && after != base) { +ksft_print_msg ("a sleeper switched out on an offline CPU: %d probe tasks admitted before, %d after\n" , + base, after); + ret =KSFT_FAIL ; + } +return ret; +} + +/* +* Turn admission control off (sched_rt_runtime_us = -1). A task pins +* itself to hp_cpu, becomes a deadline task of 50% and spins. Without +* admission control, a deadline task can have an affinity of one CPU. +* Take hp_cpu offline: the mask of the task holds no active CPU. The +* task must go on running on another CPU. Once the CPU is back, the task +* is gone and admission control is on again, the housekeeping root domain +* must admit what it did before. +*/ +static int hotplug_no_admission_pinned (void ) +{ +int base =probe (NULL ), after, ret =KSFT_PASS , last; +char saved[64 ]; +pid_t pid; + +if (read_file ("/proc/sys/kernel/sched_rt_runtime_us" , saved,sizeof (saved)) || +write_file ("/proc/sys/kernel" ,"sched_rt_runtime_us" ,"-1" )) { +ksft_print_msg ("admission control cannot be turned off\n" ); +return KSFT_SKIP ; + } + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =fork_child ("a pinned deadline task" ); +if (!pid) { +cpu_set_t one; + +CPU_ZERO (&one); +CPU_SET (hp_cpu, &one); +if (sched_setaffinity (0 ,sizeof (one), &one) || +set_deadline (0 ,50 *NSEC_PER_MSEC ,PERIOD )) { + mv_state[0 ] =-1 ; +_exit (1 ); + } + mv_state[0 ] =1 ; +for (;;) + mv_state[1 ]++; + } +if (pid <0 ||child_up ("a pinned deadline task" ,0 )) { + ret =KSFT_FAIL ; + goto out; + } +sleep_ns (PERIOD ); +if (cpu_online (hp_cpu,0 )) { +ksft_print_msg ("CPU %d cannot go offline under a pinned deadline task\n" , hp_cpu); + ret =KSFT_FAIL ; + goto out; + } + last = mv_state[1 ]; +sleep_ns (3 *PERIOD ); +if (mv_state[1 ] == last) { +ksft_print_msg ("the pinned task does not run after its CPU went offline\n" ); + ret =KSFT_FAIL ; + } +out: +cpu_up (hp_cpu); +reap (pid); +if (write_file ("/proc/sys/kernel" ,"sched_rt_runtime_us" , saved)) { +ksft_print_msg ("admission control cannot be turned on again\n" ); +return KSFT_FAIL ; + } +sleep_ns (3 *PERIOD ); + after =probe (NULL ); +if (ret ==KSFT_PASS && after != base) { +ksft_print_msg ("a pinned task without admission control: %d probe tasks admitted before, %d after\n" , + base, after); + ret =KSFT_FAIL ; + } +return ret; +} +/* * Deadline sleepers take most of the free bandwidth. Taking hp_cpu * offline must then fail with EBUSY. Once the sleepers are gone, the
@@ -4638,6 +4851,303 @@ static int hotplug_refused(void)return ret; } +/* +* A deadline task of 40% spins. The case switches it to SCHED_OTHER +* before its 0-lag time. Then it takes the CPU that the task is on +* offline, before that time too. Past the 0-lag time, with the CPU still +* offline, the housekeeping root domain must admit what it admits with a +* CPU offline and no task. Once the CPU is back and the task is gone, it +* must admit what it did at the start. +*/ +static int hotplug_departed (void ) +{ +int base =probe (NULL ), base_off, after, cpu, ret =KSFT_PASS ; +pid_t pid; + +/* +* Take the reference with hp_cpu offline and no task of ours. The +* task may be on another CPU: the case assumes that any one CPU +* offline leaves the same room. The case compares while that CPU +* is still offline. Bringing it back rebuilds the accounting, +* which would hide an error. +*/ +if (cpu_online (hp_cpu,0 )) { +ksft_print_msg ("offline of CPU %d refused\n" , hp_cpu); +return KSFT_FAIL ; + } + base_off =probe (NULL ); +cpu_up (hp_cpu); +if (base <=0 || base_off <=0 ) { +ksft_print_msg ("no room to probe with the CPU online or offline\n" ); +return KSFT_SKIP ; + } + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =mover (MV_THROTTLED ,40 *NSEC_PER_MSEC ); +if (pid <0 ||child_up ("a departing deadline task" ,0 )) { +reap (pid); +return KSFT_FAIL ; + } +sleep_ns (PERIOD /2 ); +/* Not asleep: its bandwidth stays until its 0-lag time. */ +if (set_other (pid)) + ret =KSFT_FAIL ; + cpu =task_cpu_of (pid); +if (cpu <0 || !cpu_hotpluggable (cpu)) { +cpu_set_t set; + +/* Out of the class now, it may be pinned to hp_cpu. */ + cpu = hp_cpu; +CPU_ZERO (&set); +CPU_SET (cpu, &set); +if (sched_setaffinity (pid,sizeof (set), &set)) + ret =KSFT_FAIL ; + } +if (ret ==KSFT_PASS &&cpu_online (cpu,0 )) { +ksft_print_msg ("offline of CPU %d refused\n" , cpu); + ret =KSFT_FAIL ; + } +sleep_ns (3 *PERIOD );/* its 0-lag time, the CPU offline */ +if (ret ==KSFT_PASS ) { + after =probe (NULL ); +if (after != base_off) { +ksft_print_msg ("departed, CPU %d offline: %d admitted, %d expected\n" , + cpu, after, base_off); + ret =KSFT_FAIL ; + } + } +cpu_up (cpu); +reap (pid); +sleep_ns (3 *PERIOD ); + after =probe (NULL ); +if (ret ==KSFT_PASS && after != base) { +ksft_print_msg ("departed, CPU back online: %d admitted before, %d after\n" , + base, after); + ret =KSFT_FAIL ; + } +return ret; +} + +#define ZOMBIE_RUNTIME (200 * NSEC_PER_MSEC) +#define ZOMBIE_PERIOD (2 * NSEC_PER_SEC) + +/* +* Q is a partition root of two CPUs. The kernel records, with the +* bandwidth of a task, a CPU of the root domain that counts it. The steps: +* 1. A task enters Q and becomes a deadline task of 200ms every 2s. +* 2. It runs 190ms and exits, its 0-lag time about 1.7s ahead. The case +* does not reap it yet. A rebuild of the accounting still finds a +* zombie in the task list. +* 3. total_bw of Q must count the zombie. +* 4. The case takes each CPU of Q offline and back, one after the other. +* One of them is the CPU recorded with the bandwidth of the zombie. +* The rebuild at its offline drops that bandwidth. +* 5. Before the 0-lag time, total_bw of Q must be what it was without +* the task. No later rebuild may count the zombie again: the release +* at the 0-lag time removes nothing by then. +* 6. The case reaps the zombie. Past the 0-lag time, total_bw of Q must +* still be that value. +* +* The case runs with the cases in P, from the main process: Q needs the +* cpuset controller. Q has two CPUs because the last CPU of a root domain +* that holds deadline bandwidth cannot go offline. +*/ +static int death_zombie_hotplug (void ) +{ +int cpu[2 ] = {top_cpu (3 ),top_cpu (2 ) }, i, ret =KSFT_PASS ; +long long base, bw, held, after; +char cpus[32 ]; +uint64_t t; +pid_t pid; + +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 ) { +ksft_print_msg ("no total_bw in /sys/kernel/debug/sched/debug\n" ); +remove_partition (cg_q); +return KSFT_SKIP ; + } + bw = (ZOMBIE_RUNTIME <<20 ) /ZOMBIE_PERIOD ;/* as to_ratio() */ + mv_state[0 ] =0 ; + pid =fork_child ("a deadline task that exits" ); +if (!pid) { +if (enter (cg_q,getpid ()) ||set_deadline (0 ,ZOMBIE_RUNTIME ,ZOMBIE_PERIOD )) +_exit (1 ); +/* 10ms left: the 0-lag time is 100ms before the deadline. */ +spin_ns (ZOMBIE_RUNTIME -10 *NSEC_PER_MSEC ); + mv_state[0 ] =1 ; +_exit (0 ); + } +if (pid <0 ||child_up ("a task of Q that exits" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='Z' )) { +ksft_print_msg ("no zombie deadline task\n" ); +reap (pid); +remove_partition (cg_q); +return KSFT_FAIL ; + } +/* Its 0-lag time is about 1.7s ahead. Check total_bw within 1s. */ + t =now_ns (); + held =total_bw_of (cpu[0 ]); +if (held != base + bw) { +ksft_print_msg ("total_bw of Q: %lld with the zombie, %lld expected\n" , + held, base + bw); + ret =KSFT_FAIL ; + } +for (i =0 ; i <2 && ret ==KSFT_PASS ; i++) { +if (cpu_online (cpu[i],0 ) ||cpu_online (cpu[i],1 )) { +ksft_print_msg ("offline and online of CPU %d failed\n" , cpu[i]); +cpu_up (cpu[i]); + ret =KSFT_FAIL ; + } + } + after =total_bw_of (cpu[0 ]); +if (ret ==KSFT_PASS &&now_ns () - t >NSEC_PER_SEC ) { +ksft_print_msg ("the hotplugs took over 1s: the 0-lag time may be past\n" ); + ret =KSFT_SKIP ; + }else if (ret ==KSFT_PASS && after != base) { +ksft_print_msg ("total_bw of Q after the hotplugs: %lld, %lld expected%s\n" , + after, base, + after == base + bw ?": the zombie counted again" :"" ); + ret =KSFT_FAIL ; + } +reap (pid); +sleep_ns (ZOMBIE_PERIOD );/* past its 0-lag time */ + after =total_bw_of (cpu[0 ]); +if (ret ==KSFT_PASS && after != base) { +ksft_print_msg ("total_bw of Q past the 0-lag time: %lld, %lld expected\n" , + after, base); + ret =KSFT_FAIL ; + } +remove_partition (cg_q); +return ret; +} + +/* +* Fork the owner of pi_departed_moved(). It takes shm_lock, becomes a +* deadline task and runs most of its runtime. It sets mv_state[0] to 1 +* and sleeps 300ms. Then it sets mv_state[1] to 1 and sleeps with the +* lock. Return its pid, or -1. +*/ +static pid_t departed_owner (void ) +{ +pid_t pid =fork_child ("the owner of the lock" ); + +if (!pid) { +if (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 ; +sleep_ns (300 *NSEC_PER_MSEC ); + mv_state[1 ] =1 ; +for (;;) +pause (); + } +return pid; +} + +/* +* The owner is a deadline task of 200ms every 2s, outside P. It holds +* shm_lock, runs 180ms and sleeps, its 0-lag time about 1.6s ahead. The +* steps: +* 1. The case switches the sleeping owner to SCHED_OTHER. The +* housekeeping root domain keeps its bandwidth until the 0-lag time. +* 2. The case moves the owner into P. The owner wakes up and runs there. +* 3. A deadline waiter of 5ms every 50ms, outside P, blocks on shm_lock +* and boosts the owner. +* 4. The case kills the waiter, which ends the boost. +* +* After step 4, total_bw of the housekeeping root domain must still count +* the bandwidth of the owner. total_bw of P never counted it and must be +* unchanged: the end of the boost must not subtract it there. Past the +* 0-lag time, both must be what they were at the start. +*/ +static int pi_departed_moved (void ) +{ +int p_cpu =top_cpu (1 ), r_cpu = online[0 ], ret =KSFT_PASS ; +long long base_p, base_r, bw, in_p, in_r; +pid_t owner, waiter =-1 ; + + base_p =total_bw_of (p_cpu); + base_r =total_bw_of (r_cpu); +if (base_p <0 || base_r <0 ) { +ksft_print_msg ("no total_bw in /sys/kernel/debug/sched/debug\n" ); +return KSFT_SKIP ; + } +if (shm_lock_init ()) +return KSFT_SKIP ; + bw = (ZOMBIE_RUNTIME <<20 ) /ZOMBIE_PERIOD ;/* as to_ratio() */ + mv_state[1 ] =0 ; + owner =departed_owner (); +if (owner <0 ) +return KSFT_FAIL ; +if (child_up ("the owner of the lock" ,0 )) { +reap (owner); +return KSFT_FAIL ; + } +/* The owner is asleep, about 1.6s before its 0-lag time. */ +sleep_ns (20 *NSEC_PER_MSEC ); +if (set_other (owner) ||enter (cg_p, owner)) { +ksft_print_msg ("the owner cannot leave the class and enter P\n" ); + ret =KSFT_FAIL ; + goto out; + } +if (wait_state (&mv_state[1 ],1 ) ||task_cpu_of (owner) != p_cpu) { +ksft_print_msg ("the owner did not run in P\n" ); + ret =KSFT_FAIL ; + goto out; + } + waiter =lock_waiter (); +if (waiter <0 ) { + ret =KSFT_FAIL ; + goto out; + } +if (wait_state (&mv_state[2 ],1 )) { +ksft_print_msg ("no waiter\n" ); + ret =KSFT_FAIL ; + goto out; + } +sleep_ns (50 *NSEC_PER_MSEC );/* blocked, boosting the owner */ +reap (waiter); + waiter =-1 ; +sleep_ns (150 *NSEC_PER_MSEC );/* the waiter is past its 0-lag time */ + in_p =total_bw_of (p_cpu); + in_r =total_bw_of (r_cpu); +if (in_p != base_p || in_r != base_r + bw) { +ksft_print_msg ("after the boost: total_bw of P %lld, %lld expected\n" , + in_p, base_p); +ksft_print_msg ("after the boost: total_bw outside %lld, %lld expected\n" , + in_r, base_r + bw); + ret =KSFT_FAIL ; + } +out: +reap (waiter); +reap (owner); +sleep_ns (ZOMBIE_PERIOD );/* past the owner's 0-lag time */ + in_p =total_bw_of (p_cpu); + in_r =total_bw_of (r_cpu); +if (ret ==KSFT_PASS && (in_p != base_p || in_r != base_r)) { +ksft_print_msg ("past the 0-lag time: total_bw of P %lld, %lld expected\n" , + in_p, base_p); +ksft_print_msg ("past the 0-lag time: total_bw outside %lld, %lld expected\n" , + in_r, base_r); + ret =KSFT_FAIL ; + } +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
@@ -4743,6 +5253,134 @@ static void report(const char *name, int ret)ksft_test_result_fail ("%s\n" , name); } +/* sched_setaffinity() against sched_setattr(), with no P */ + +/* +* The state that affinity_racer() shares with the case. pid and one are +* its target and its mask. go and done count the rounds. ret is the +* result of its last call. +*/ +static struct { +pid_t pid; +cpu_set_t one; +atomic_int go; +atomic_int done; +atomic_int stop; +int ret; +} aff; + +/* +* Give aff.pid the affinity aff.one, once for each new value of aff.go. +* Return when aff.stop is set. +*/ +static void *affinity_racer (void *arg) +{ +int round; + +for (round =1 ; ; round++) { +while (aff.go < round && !aff.stop) + ; +if (aff.stop) +break ; + aff.ret =sched_setaffinity (aff.pid,sizeof (aff.one), &aff.one) ? -errno :0 ; + aff.done = round; + } +return arg; +} + +/* +* Race sched_setaffinity() against sched_setattr() on a sleeping task. +* One thread gives the task an affinity of one CPU. Another one makes it +* a deadline task. A deadline task must be allowed on its whole root +* domain, so at most one of the two calls may succeed: +* - sched_setattr() fails with EPERM if the affinity came first; +* - sched_setaffinity() fails with EBUSY if the policy came first. +* +* Do it 1000 times. Each round starts with the task back in SCHED_OTHER +* with its full affinity. Once the task is gone, the free bandwidth must +* be what it was. The case needs no cpuset: it runs without P, with the +* hotplug cases. +*/ +static int affinity_vs_setattr (void ) +{ +const char *name ="affinity_vs_setattr" ; +int base, i, cpu, both =0 , ret =KSFT_PASS ; +cpu_set_t all; +pthread_t t; +pid_t pid; + + base =probe (NULL ); + pid =fork_child (name); +if (!pid) { +for (;;) +pause (); + } +if (pid <0 ) +return KSFT_FAIL ; +/* +* Wait until the task sleeps in pause(). A task that still runs +* when it becomes a deadline task can overrun its 2us. Its +* bandwidth then stays counted until its 0-lag time, seconds after +* it is gone, and the next cases would see it. +*/ +if (wait_for (1000 ,proc_state_of (pid) =='S' ) || +sched_getaffinity (pid,sizeof (all), &all)) { +reap (pid); +return KSFT_FAIL ; + } +CPU_ZERO (&aff.one); +for (cpu =0 ; cpu <CPU_SETSIZE ; cpu++) { +if (CPU_ISSET (cpu, &all)) { +CPU_SET (cpu, &aff.one); +break ; + } + } + aff.pid = pid; + aff.go =0 ; + aff.done =0 ; + aff.stop =0 ; +if (pthread_create (&t,NULL , affinity_racer,NULL )) { +reap (pid); +return KSFT_FAIL ; + } + +for (i =1 ; i <=1000 && ret ==KSFT_PASS ; i++) { +int err; + +if (set_other (pid) ||sched_setaffinity (pid,sizeof (all), &all)) { +ksft_print_msg ("%s: the task cannot be reset: %s\n" , name,strerror (errno)); + ret =KSFT_FAIL ; +break ; + } + aff.go = i; + err =set_deadline (pid,2 *NSEC_PER_USEC ,PERIOD ); +while (aff.done < i) + ; +if (!err && !aff.ret) { + both++; + }else if ((err && err != -EPERM ) || (aff.ret && aff.ret != -EBUSY )) { +ksft_print_msg ("%s: round %d: sched_setattr: %s, sched_setaffinity: %s\n" , + name, i,strerror (-err),strerror (-aff.ret)); + ret =KSFT_FAIL ; + } + } + aff.stop =1 ; +join (t); +if (both) { +ksft_print_msg ("%s: %d rounds of %d: a deadline task with an affinity of one CPU\n" , + name, both, i -1 ); + ret =KSFT_FAIL ; + } +/* A deadline task of 2us every 100ms would take seconds to exit. */ +if (set_other (pid)) +ksft_print_msg ("%s: the task stays a deadline task\n" , name); +reap (pid); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS &&admits (base,"after the task is gone" )) + ret =KSFT_FAIL ; +return ret; +} +/* The tables of the cases, and main() */ enum where {
@@ -4760,6 +5398,7 @@ static const struct p_case { { "pi_other_owner_asleep", pi_other_owner_asleep, MEASURED_IN_P }, { "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 }, { "params_asleep", params_asleep, IN_P }, { "params_non_contending", params_non_contending, MEASURED_IN_P }, { "params_switched_asleep", params_switched_asleep, MEASURED_IN_P },
@@ -4790,6 +5429,7 @@ static const struct p_case { { "death_pi_deadline_owner", death_pi_deadline_owner, MEASURED_IN_P }, { "death_group", death_group, MEASURED_IN_P }, { "death_zombie_switched", death_zombie_switched, FROM_OUTSIDE }, + { "death_zombie_hotplug", death_zombie_hotplug, FROM_OUTSIDE }, { "yield_switched_out", yield_switched_out, IN_P }, { "yield_killed", yield_killed, MEASURED_IN_P }, { "overrun_caught", overrun_caught, IN_P },
@@ -4808,9 +5448,15 @@ static const struct p_case {static const struct hp_case {const char *name;int (*fn)(void ); +int no_hp;/* takes no CPU offline */ } hp_cases[] = { + {"hotplug_sleepers" , hotplug_sleepers }, + {"hotplug_sleeper_switched" , hotplug_sleeper_switched }, + {"hotplug_no_admission_pinned" , hotplug_no_admission_pinned }, {"hotplug_refused" , hotplug_refused }, + {"hotplug_departed" , hotplug_departed }, {"hotplug_server" , hotplug_server }, + {"affinity_vs_setattr" , affinity_vs_setattr,1 }, };#define N_HP_CASES ((int)ARRAY_SIZE(hp_cases))
@@ -4949,7 +5595,7 @@ int main(void)if (hp_cpu <0 )ksft_print_msg ("no CPU hotplug: hotplug cases skipped\n" );for (i =0 ; i <N_HP_CASES ; i++) { -if (hp_cpu <0 ) { +if (hp_cpu <0 && !hp_cases[i].no_hp) {ksft_test_result_skip ("%s\n" , hp_cases[i].name);continue ; }
Signature · 1 line
2.47.3
PATCH v10 7/40 sched/deadline: Move a task's bandwidth only where room was made
In reply to Mathieu DesnoyersCommit 7f51412a415d ("sched/deadline: Fix bandwidth check/update when migrating tasks between exclusive cpusets") moves a deadline task's bandwidth in two halves. cpuset_can_attach() reserves it in the destination root domain. set_cpus_allowed_dl() then frees it in the source, when the task's affinity changes. Its comment relies on the first half: "We already made space for us in the destination domain".
But set_cpus_allowed_dl() runs on every change of affinity. It frees the bandwidth whenever the new mask leaves the task's root domain, with or without a reservation. sched_setaffinity() gets there too, if the task's cpuset spans two root domains. With isolcpus="domain", any cpuset that is not a partition root can hold isolated CPUs and other CPUs, which are in two root domains. With admission control on, __sched_setaffinity() refuses such a mask. With sched_rt_runtime_us at -1, nothing refuses it. The bandwidth is then freed in the source and added nowhere: no root domain counts it any more. The destination would underflow when the task leaves.
Fix this by flagging the affinity changes that come with a reservation, as SCA_DL_BW_RESERVED. cpuset_attach_task() sets it, through the new set_cpus_allowed_bw_reserved(). set_cpus_allowed_dl() moves the bandwidth only with the flag. Without it, the bandwidth stays where it is counted, until the next rebuild of the accounting counts it anew. The flag also skips the early exit of __set_cpus_allowed_ptr_locked() for a mask that does not change. The task may already have that mask from a change that moved no bandwidth, such as a sched_setaffinity() without admission control.
The attach still flags a task that sched_setattr() made a deadline task after cpuset_can_attach(), with nothing reserved for it. "sched: Make deadline policy and affinity changes wait for a cpuset attach" fixes that.
No case of deadline_bw_test fails before this patch and passes with it. no_admission_affinity_moves, added later in this series, covers this path together with "sched/deadline: Move a task's bandwidth to the root domain it runs in".
--- include/linux/sched.h | 2 ++ kernel/cgroup/cpuset.c | 13 ++++++++++++- kernel/sched/core.c | 26 +++++++++++++++++++++++++- kernel/sched/deadline.c | 14 +++++++++----- kernel/sched/sched.h | 2 ++ 5 files changed, 50 insertions(+), 7 deletions(-)
include/linux/sched.h +2 -0
@@ -1966,6 +1966,8 @@ extern void set_cpus_allowed_force(struct task_struct *p, const struct cpumask ** Return: zero if successful, or a negative error code */ extern int set_cpus_allowed_ptr (struct task_struct *p,const struct cpumask *new_mask); +extern int set_cpus_allowed_bw_reserved (struct task_struct *p, +const struct cpumask *new_mask);extern int dup_user_cpus_ptr (struct task_struct *dst,struct task_struct *src,int node);extern void release_user_cpus_ptr (struct task_struct *p);extern int dl_task_check_affinity (struct task_struct *p,const struct cpumask *mask);
kernel/cgroup/cpuset.c +12 -1
@@ -3261,6 +3261,7 @@ static cpumask_var_t cpus_attach;static void cpuset_attach_task (struct cpuset *cs,struct task_struct *task) { +bool reserved = false;struct mm_struct *mm;lockdep_assert_cpuset_lock_held ();
@@ -3270,11 +3271,21 @@ static void cpuset_attach_task(struct cpuset *cs, struct task_struct *task)else cpumask_andnot (cpus_attach,task_cpu_possible_mask (task), subpartitions_cpus); +/* +* cpuset_reserve_dl_bw() made room in @cs for the bandwidth of the +* deadline tasks that cpuset_can_attach() counted. Flag the move, so +* that the bandwidth of this task moves with it. +*/ +if (dl_task (task) &&dl_task_needs_bw_move (task, cs->effective_cpus)) + reserved = true;/* * can_attach beforehand should guarantee that this doesn't * fail. TODO: have a better way to handle failure here */ -WARN_ON_ONCE (set_cpus_allowed_ptr (task, cpus_attach)); +if (reserved) +WARN_ON_ONCE (set_cpus_allowed_bw_reserved (task, cpus_attach)); +else +WARN_ON_ONCE (set_cpus_allowed_ptr (task, cpus_attach));if (cpuset_v2 () && !attach_ctx.mems_updated)return ;
kernel/sched/core.c +25 -1
@@ -3207,7 +3207,15 @@ static int __set_cpus_allowed_ptr_locked(struct task_struct *p, }if (!(ctx->flags &SCA_MIGRATE_ENABLE )) { -if (cpumask_equal (&p->cpus_mask, ctx->new_mask)) { +/* +* A move with SCA_DL_BW_RESERVED must reach +* set_cpus_allowed_dl(), even if the mask does not change. +* @p may have this mask from a change that moved no +* bandwidth, such as sched_setaffinity() without admission +* control. +*/ +if (cpumask_equal (&p->cpus_mask, ctx->new_mask) && + !(ctx->flags &SCA_DL_BW_RESERVED )) {if (ctx->flags &SCA_USER )swap (p->user_cpus_ptr, ctx->user_mask); goto out;
@@ -3280,6 +3288,22 @@ int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask) }EXPORT_SYMBOL_GPL (set_cpus_allowed_ptr); +/* +* Like set_cpus_allowed_ptr(), for a caller that has made room for the +* deadline bandwidth of @p in the root domain of @new_mask. Move that +* bandwidth there with @p. Return 0 on success, or a negative error +* code. +*/ +int set_cpus_allowed_bw_reserved (struct task_struct *p,const struct cpumask *new_mask) +{ +struct affinity_context ac = { + .new_mask = new_mask, + .flags =SCA_DL_BW_RESERVED , + }; + +return __set_cpus_allowed_ptr (p, &ac); +} +/* * Change a given task's CPU affinity to the intersection of its current * affinity mask and @subset_mask, writing the resulting mask to @new_mask.
kernel/sched/deadline.c +9 -5
@@ -3665,12 +3665,16 @@ static void set_cpus_allowed_dl(struct task_struct *p,WARN_ON_ONCE (!dl_task (p));/* -* Migrating a SCHED_DEADLINE task between exclusive -* cpusets (different root_domains) entails a bandwidth -* update. We already made space for us in the destination -* domain (see cpuset_can_attach()). +* Migrating a SCHED_DEADLINE task between exclusive cpusets +* (different root_domains) entails a bandwidth update. Move the +* bandwidth only if the caller made room for it in the destination, +* as SCA_DL_BW_RESERVED says. Only cpuset_attach_task() sets it. +* Otherwise, the bandwidth stays where it is counted, until the next +* rebuild of the accounting. Moving it without room would leave no +* root domain counting it. */ -if (dl_task_needs_bw_move (p, ctx->new_mask)) { +if ((ctx->flags &SCA_DL_BW_RESERVED ) && +dl_task_needs_bw_move (p, ctx->new_mask)) {unsigned int src_cpu =dl_task_root_domain_cpu (p);struct dl_bw *src_dl_b = &cpu_rq (src_cpu)->rd->dl_bw;unsigned int cpu;
kernel/sched/sched.h +2 -0
@@ -2973,6 +2973,8 @@ extern struct task_struct *pick_task_idle(struct rq *rq, struct rq_flags *rf);#define SCA_MIGRATE_ENABLE 0x04#define SCA_USER 0x08#define SCA_DL_CHECK 0x10 +/* The caller made room for the deadline bandwidth in the new root domain. */ +#define SCA_DL_BW_RESERVED 0x20extern void update_group_capacity (struct sched_domain *sd,int cpu);
Signature · 1 line
2.47.3
PATCH v10 8/40 cgroup/cpuset: Wait for an attach before rebuilding the sched domains
In reply to Mathieu Desnoyerscpuset_can_attach() reserves the bandwidth of the deadline tasks it moves in the destination root domain. cpuset_attach() moves their bandwidth later, on the strength of that reservation.
Commit 75f7a25ec6bb ("cgroup/cpuset: Prevent race between task attach and cpuset state change") lets cpuset_attach() rely on what cpuset_can_attach() set up. The writes of cpus, mems and cpus.partition wait for an attach in progress, in wait_attach_done_lock(). CPU hotplug cannot run in between either: it takes cpus_write_lock, and the attach holds cpus_read_lock throughout.
But a rebuild of the sched domains does not wait. A write of sched_rt_runtime_us and the energy model rebuild them through rebuild_sched_domains(). The cgroup v1 writes of sched_load_balance and sched_relax_domain_level, and hk_sd_workfn(), use cpuset_full_lock().
In this example, a rebuild drops the reservation. T is a deadline task asleep in root domain R. Thread A moves T to a partition root P, which has a root domain of its own. Thread B writes sched_rt_runtime_us:
A B
cpuset_can_attach()
takes cpuset_mutex
cpuset_reserve_dl_bw()
P's total_bw += T's bandwidth
drops cpuset_mutex
rebuild_sched_domains()
takes cpuset_mutex dl_rebuild_rd_accounting()
clears R's and P's total_bw counts T in R (not moved yet)
drops cpuset_mutex
cpuset_attach()
takes cpuset_mutex
cpuset_attach_task(T)
set_cpus_allowed_dl(T)
R's total_bw -= T's bandwidth
records a CPU of P
P now lacks the bandwidth of T, and admits deadline tasks beyond its capacity. When T leaves P, P's total_bw underflows. In testing, the race left a destination admitting 3 tasks of 10% too many, or P admitting no task at all.
Fix this by waiting for an attach in progress in rebuild_sched_domains_cpuslocked() and in cpuset_full_lock(). The latter then takes its locks in the order of the writes of cpus and cpus.partition. A rebuild from CPU hotplug finds no attach in progress. The online, offline and kill of a cpuset never wait: they hold cgroup_mutex, which the attach holds throughout.
The move_vs_rebuild case, added to deadline_bw_test later in this series, tests this race. Without this patch, it fails in 5 of 8 VMs with 8 CPUs. With it, the case passes in 8 of 8. The next patch needs this wait too: without it, it would give back a reservation that a rebuild dropped already.
The Sashiko AI reviews of this series found the race and the cgroup v1 writes.
--- kernel/cgroup/cpuset.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
kernel/cgroup/cpuset.c +13 -3
@@ -296,17 +296,20 @@ void lockdep_assert_cpuset_lock_held(void)lockdep_assert_held (&cpuset_mutex); } +static void wait_attach_done_lock(void) __acquires(&cpuset_mutex); +/** * cpuset_full_lock - Acquire full protection for cpuset modification * * Takes both CPU hotplug read lock (cpus_read_lock()) and cpuset mutex -* to safely modify cpuset data. +* to safely modify cpuset data. Waits for an attach in progress before +* it takes cpuset mutex. */ void cpuset_full_lock (void ) {mutex_lock (&cpuset_top_mutex);cpus_read_lock (); -mutex_lock (&cpuset_mutex); +wait_attach_done_lock (); }void cpuset_full_unlock (void )
@@ -1005,7 +1008,14 @@ void rebuild_sched_domains_locked(void)static void rebuild_sched_domains_cpuslocked (void ) { -mutex_lock (&cpuset_mutex); +/* +* Wait for an attach in progress. cpuset_can_attach() has reserved +* the deadline bandwidth of the moving tasks in their new root +* domain, and cpuset_attach() moves it on the strength of that +* reservation. A rebuild of the accounting in between would drop +* the reservation. +*/ +wait_attach_done_lock ();rebuild_sched_domains_locked ();mutex_unlock (&cpuset_mutex); }
Signature · 1 line
2.47.3
PATCH v10 9/40 sched: Make deadline policy and affinity changes wait for a cpuset attach
In reply to Mathieu Desnoyers__sched_setscheduler() takes cpuset_mutex for a change that involves SCHED_DEADLINE. Commit 710da3c8ea7d ("sched/core: Prevent race condition between cpuset and __sched_setscheduler()") added that lock. It keeps the cpuset code from changing the root domains while the deadline admission test runs.
But a cpuset attach drops cpuset_mutex between cpuset_can_attach() and cpuset_attach(). A sched_setattr() can run in between. Take a process in the root cgroup. It has deadline threads, and a thread T that is not a deadline task. The process is moved into a partition root P:
echo PID > P/cgroup.procs sched_setattr(T, SCHED_DEADLINE)
cpuset_can_attach()
lock cpuset_mutex
reserve room in P for the deadline
threads: T is not one of them
unlock cpuset_mutex
lock cpuset_mutex admit T in the root domain it
is still in
unlock cpuset_mutex
cpuset_attach()
lock cpuset_mutex
move T to P, where nothing is
reserved for it
unlock cpuset_mutex
The attach still moves the bandwidth of T. It is freed in the root domain T left, and P has no room reserved to count it. No root domain counts it any more, and P can admit deadline tasks beyond its capacity.
In testing, P was a one-CPU partition root, which admits 95%. The process had two sleeping deadline threads of 30% each, and sched_setattr() made T a deadline task of 40%. The move and the sched_setattr() both succeeded, with no room in P for the three. With threads of 20% and a T of 30%, P then admitted five more tasks of 10%, where it had room for two.
Fix this by taking cpuset_mutex in __sched_setscheduler() only when no attach is in progress. cpuset_partition_write() and cpuset_hotplug_update_tasks() already wait for an attach that way. A deadline policy change then comes before the attach, and is part of its reservation, or after it.
With this patch, and no room in P for the three, the two never both succeed. If the sched_setattr() comes first, the move is refused with EBUSY. If it comes after, T has its new CPUs and has not run on them. The call is then refused with EPERM, as for any sleeping task moved to another partition.
sched_setaffinity() on a deadline task can run in between as well. A later patch of this series reserves the bandwidth of the task on one CPU of its new affinity, and records that CPU with the task. It is "cgroup/cpuset: Reserve the bandwidth in the root domain a task moves to". A new affinity in between could leave that CPU out. cpuset_attach() would then move no bandwidth, and the task would stay counted in the root domain it left. So sched_setaffinity() waits the same way when the task is a deadline task.
sched_setaffinity() tests the policy with no lock, before it decides to wait. A sched_setattr() can make the task a deadline task right after that test. So test the policy again under the runqueue lock, where the mask changes. If the task is a deadline task there and the caller did not wait, the call starts over, and waits. The new SCA_DL_NOWAIT flag asks for that test. After a race with a cpuset update, __sched_setaffinity() sets the mask a second time, within the cpuset. That second change waits for an attach too, so that the test cannot skip it. It reads the cpuset mask again after the wait, since the attach can move the task.
For user-space, these calls now wait until a cpuset attach in progress ends:
- sched_setattr() and sched_setscheduler() on a deadline task
- sched_setattr() that makes a task a deadline task
- sched_setaffinity() on a deadline task
The attach can be the move of any process. The wait is not
interruptible: a signal does not end it. Outside that window, the results of these calls are unchanged.
Three cases, added to deadline_bw_test later in this series, test this. move_became_deadline and move_became_deadline_no_room fail without this patch. move_affinity_changed fails once the bandwidth is reserved on one CPU, if sched_setaffinity() does not wait.
A fourth case, move_affinity_became_deadline, covers the second test of the policy. Its window is a few instructions wide. A delay added after the first test, for testing only, widens it. The case then fails without the second test in 3 guests of 3. It passes with it.
--- include/linux/cpuset.h | 2 ++ kernel/cgroup/cpuset.c | 11 ++++++++ kernel/sched/core.c | 10 +++++++ kernel/sched/sched.h | 2 ++ kernel/sched/syscalls.c | 59 +++++++++++++++++++++++++++++++++++------ 5 files changed, 76 insertions(+), 8 deletions(-)
include/linux/cpuset.h +2 -0
@@ -73,6 +73,7 @@ extern void cpuset_init_smp(void);extern void cpuset_force_rebuild (void );extern void cpuset_update_active_cpus (void );extern void cpuset_lock (void ); +extern void cpuset_lock_no_attach (void );extern void cpuset_unlock (void );extern void lockdep_assert_cpuset_lock_held (void );extern void cpuset_cpus_allowed_locked (struct task_struct *p,struct cpumask *mask);
@@ -192,6 +193,7 @@ static inline void cpuset_update_active_cpus(void) }static inline void cpuset_lock (void ) { } +static inline void cpuset_lock_no_attach (void ) { }static inline void cpuset_unlock (void ) { }static inline void lockdep_assert_cpuset_lock_held (void ) { }
kernel/cgroup/cpuset.c +11 -0
@@ -378,6 +378,17 @@ static void wait_attach_done_lock(void) } } +/** +* cpuset_lock_no_attach - Acquire cpuset_mutex with no task attach in progress +* +* A task attach drops cpuset_mutex between cpuset_can_attach() and +* cpuset_attach(). Use this to keep a change out of that window. +*/ +void cpuset_lock_no_attach (void ) +{ +wait_attach_done_lock (); +} +static inline void check_insane_mems_config (nodemask_t *nodes) { if (!cpusets_insane_config() &&
kernel/sched/core.c +10 -0
@@ -3195,6 +3195,16 @@ static int __set_cpus_allowed_ptr_locked(struct task_struct *p, goto out; } +/* +* sched_setaffinity() tested the policy with no lock, and did not +* wait for a cpuset attach in progress. @p became a deadline task +* since: let the caller wait and try again. +*/ +if ((ctx->flags &SCA_DL_NOWAIT ) &&task_has_dl_policy (p)) { + ret = -EAGAIN ; + goto out; + } +/* * Check under the same lock that __sched_setscheduler() holds for * its admission test, so that p cannot become SCHED_DEADLINE
kernel/sched/sched.h +2 -0
@@ -2975,6 +2975,8 @@ extern struct task_struct *pick_task_idle(struct rq *rq, struct rq_flags *rf);#define SCA_DL_CHECK 0x10/* The caller made room for the deadline bandwidth in the new root domain. */ #define SCA_DL_BW_RESERVED 0x20 +/* The caller did not wait for a cpuset attach: -EAGAIN for a deadline task. */ +#define SCA_DL_NOWAIT 0x40extern void update_group_capacity (struct sched_domain *sd,int cpu);
kernel/sched/syscalls.c +51 -8
92 lines not on this page · raw message
Signature · 1 line
2.47.3
PATCH v10 10/40 cgroup/cpuset: Give back the bandwidth an attach did not move
In reply to Mathieu DesnoyersCommit 2ef269ef1ac0 ("cgroup/cpuset: Free DL BW in case can_attach() fails") reserves the bandwidth of an attach as one sum. cpuset_can_attach() adds up the bandwidth of the deadline tasks it moves. Once all tasks pass their checks, it reserves the sum in the destination root domain. If another controller refuses the attach, cpuset_cancel_attach() frees the sum. If the attach goes through, the move of each task uses its part of the sum.
But a task can leave SCHED_DEADLINE between cpuset_can_attach() and cpuset_attach(). The previous patch makes sched_setattr() wait for the attach. sysrq-n does not wait: it cannot sleep. The task then moves no bandwidth. Its part of the reservation stays in the destination's total_bw, until the next rebuild of the accounting. The destination can then refuse deadline tasks that would fit.
Fix this by keeping the tally of what the attach moves, in sum_moved_dl_bw. Once the attach is through, give back what it did not move.
set_cpus_allowed_dl() decides the move under the runqueue lock. cpuset_attach_task() does not hold it. So the tally counts what set_cpus_allowed_dl() reports in the affinity context.
The random_mix case, added to deadline_bw_test later in this series, found the bug. It could no longer lower a fair server in a partition root with no room left.
The move_normalized case, added with that test, fires sysrq-n while a process with a sleeping deadline thread of 30% moves into a one-CPU partition root. Without this patch, the partition kept the 30% in 3 guests of 3. It admitted 6 tasks of 10% where it had room for 9.
--- include/linux/sched.h | 3 ++- kernel/cgroup/cpuset-internal.h | 2 ++ kernel/cgroup/cpuset.c | 30 +++++++++++++++++++++++++----- kernel/sched/core.c | 12 ++++++++---- kernel/sched/deadline.c | 4 ++++ kernel/sched/sched.h | 2 ++ 6 files changed, 43 insertions(+), 10 deletions(-)
include/linux/sched.h +2 -1
@@ -1967,7 +1967,8 @@ extern void set_cpus_allowed_force(struct task_struct *p, const struct cpumask **/ extern int set_cpus_allowed_ptr (struct task_struct *p,const struct cpumask *new_mask);extern int set_cpus_allowed_bw_reserved (struct task_struct *p, -const struct cpumask *new_mask); +const struct cpumask *new_mask, +u64 *moved);extern int dup_user_cpus_ptr (struct task_struct *dst,struct task_struct *src,int node);extern void release_user_cpus_ptr (struct task_struct *p);extern int dl_task_check_affinity (struct task_struct *p,const struct cpumask *mask);
kernel/cgroup/cpuset-internal.h +2 -0
@@ -170,6 +170,8 @@ struct cpuset {int nr_migrate_dl_tasks;/* DL bandwidth that needs destination reservation for this attach. */ u64 sum_migrate_dl_bw; +/* The part of sum_migrate_dl_bw moved. The rest is given back. */ +u64 sum_moved_dl_bw;/* * CPU used for temporary DL bandwidth allocation during attach; * -1 if no DL bandwidth was allocated in the current attach.
kernel/cgroup/cpuset.c +25 -5
@@ -3140,10 +3140,24 @@ static void clear_attach_data(struct llist_head *head, bool cancel)llist_for_each_entry_safe (cs, next, lnode, attach_node) {init_llist_node (&cs->attach_node);if (cs->nr_migrate_dl_tasks) { -if (cancel && cs->dl_bw_cpu >=0 ) -dl_bw_free (cs->dl_bw_cpu, cs->sum_migrate_dl_bw); +/* +* Give back the bandwidth the attach did not move. +* If the attach is cancelled, that is all of the +* reservation. Otherwise, it is what no move used, +* such as the part for a task that left +* SCHED_DEADLINE. The class it left releases its own +* bandwidth, from the root domain that counts it. +*/ +if (cs->dl_bw_cpu >=0 ) { +u64 bw = cancel ? cs->sum_migrate_dl_bw : + cs->sum_migrate_dl_bw - cs->sum_moved_dl_bw; + +if (bw) +dl_bw_free (cs->dl_bw_cpu, bw); + } cs->nr_migrate_dl_tasks =0 ; cs->sum_migrate_dl_bw =0 ; + cs->sum_moved_dl_bw =0 ; cs->dl_bw_cpu =-1 ; } }
@@ -3303,10 +3317,16 @@ static void cpuset_attach_task(struct cpuset *cs, struct task_struct *task)* can_attach beforehand should guarantee that this doesn't * fail. TODO: have a better way to handle failure here */ -if (reserved) -WARN_ON_ONCE (set_cpus_allowed_bw_reserved (task, cpus_attach)); -else +if (reserved) { +u64 moved; + +/* Count what the move reports it moved. */ +WARN_ON_ONCE (set_cpus_allowed_bw_reserved (task, cpus_attach, + &moved)); + cs->sum_moved_dl_bw += moved; + }else {WARN_ON_ONCE (set_cpus_allowed_ptr (task, cpus_attach)); + }if (cpuset_v2 () && !attach_ctx.mems_updated)return ;
kernel/sched/core.c +8 -4
@@ -3301,17 +3301,21 @@ EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);/* * Like set_cpus_allowed_ptr(), for a caller that has made room for the * deadline bandwidth of @p in the root domain of @new_mask. Move that -* bandwidth there with @p. Return 0 on success, or a negative error -* code. +* bandwidth there with @p, and set @moved to the bandwidth moved, 0 +* if none. Return 0 on success, or a negative error code. */ -int set_cpus_allowed_bw_reserved (struct task_struct *p,const struct cpumask *new_mask) +int set_cpus_allowed_bw_reserved (struct task_struct *p, +const struct cpumask *new_mask,u64 *moved) {struct affinity_context ac = { .new_mask = new_mask, .flags =SCA_DL_BW_RESERVED , }; +int ret; -return __set_cpus_allowed_ptr (p, &ac); + ret =__set_cpus_allowed_ptr (p, &ac); + *moved = ac.dl_bw_moved; +return ret; }/*
kernel/sched/deadline.c +4 -0
@@ -3672,6 +3672,9 @@ static void set_cpus_allowed_dl(struct task_struct *p,* Otherwise, the bandwidth stays where it is counted, until the next * rebuild of the accounting. Moving it without room would leave no * root domain counting it. +* +* Report in @ctx the bandwidth moved. The caller made its own test +* without the runqueue lock, so it must count only what this reports. */ if ((ctx->flags &SCA_DL_BW_RESERVED ) &&dl_task_needs_bw_move (p, ctx->new_mask)) {
@@ -3700,6 +3703,7 @@ static void set_cpus_allowed_dl(struct task_struct *p, cpu =cpumask_any_and (ctx->new_mask, cpu_active_mask); p->dl.dl_bw_cpu = cpu < nr_cpu_ids ? (int )cpu :-1 ; p->dl.dl_bw_gen = dl_bw_gen; + ctx->dl_bw_moved = p->dl.dl_bw; }set_cpus_allowed_common (p, ctx);
kernel/sched/sched.h +2 -0
@@ -2678,6 +2678,8 @@ extern const u32 sched_prio_to_wmult[40];struct affinity_context {const struct cpumask *new_mask;struct cpumask *user_mask; +/* Out: the deadline bandwidth moved to the new root domain. */ +u64 dl_bw_moved;unsigned int flags; };
Signature · 1 line
2.47.3
PATCH v10 11/40 cgroup/cpuset: Reserve the bandwidth in the root domain a task moves to
In reply to Mathieu DesnoyersCommit 7f51412a415d ("sched/deadline: Fix bandwidth check/update when migrating tasks between exclusive cpusets") moves a deadline task's bandwidth in two halves. cpuset_can_attach() reserves it in the destination. set_cpus_allowed_dl() frees it in the source when the task's affinity changes. The reservation goes to the root domain of the first active CPU of the destination cpuset. So cpuset_can_attach() treats the cpuset as one root domain.
But a cpuset can span more than one root domain. With isolcpus=domain, the root cpuset holds the isolated CPUs, which are in the default root domain. A task also keeps the affinity set by sched_setaffinity(). Its new CPUs can then all be in a root domain that got no reservation. The task runs there, and its bandwidth is reserved in the other root domain.
Take a machine with CPUs 0-4, booted with isolcpus=domain,1,2. P is a partition root of CPU 4. A deadline task T, pinned to CPUs 1-2, is moved into P and back to the root cgroup:
total_bw of the root domain of
admin, task T CPUs 0, 3 CPUs 1, 2 CPU 4 (P)
sched_setaffinity(T, CPUs 1-2)
sched_setattr(T, SCHED_DEADLINE) + bw
echo T > P/cgroup.procs
cpuset_can_attach()
reserves on CPU 4 + bw
set_cpus_allowed_dl()
frees where recorded, CPU 1 - bw
records CPU 4
T runs on CPU 4
echo T > cgroup.procs of the root
cpuset_can_attach()
reserves on CPU 0 + bw
__set_cpus_allowed_ptr()
keeps T's affinity: CPUs 1-2
set_cpus_allowed_dl()
frees where recorded, CPU 4 - bw
records CPU 1
T runs on CPU 1
T exits
releases where recorded, CPU 1 - bw
--------- --------- ---------
left after T exits + bw - bw 0T ran in the root domain of CPUs 1 and 2 without being counted there. In testing, the total_bw of that root domain went below zero when T died. It then refused every deadline task, until the next rebuild of the accounting.
Fix this by reserving the bandwidth on the CPU that the move records with the task. The new dl_task_bw_reserve_cpu() works out that CPU and records it with the task. It is the CPU the task is on, if the task can stay there. Else it is the first active CPU of the new mask, a housekeeping one if any. The rebuild of the accounting makes the same choice, later in this series. cpuset_can_attach() reserves the bandwidth per such CPU, and refuses with EBUSY as before. cpuset_attach_task() moves the bandwidth to the CPU recorded, and does not work it out again:
the task may have run on another CPU since. cpuset_attach() gives back the rest.
The record is a new field of the task, dl_bw_resv_cpu. cpuset_attach_task() moves the bandwidth of a task only if the task has that record. Juri Lelli suggested to track the tasks that cpuset_can_attach() counts. A task it did not count cannot use the room made for another task.
A new mask can also span both root domains. A running task would then go to the CPU that cpumask_any_and_distribute() picks. A sleeping one would wake up where select_fallback_rq() picks. Either CPU can be in the root domain without the reservation. So __set_cpus_allowed_ptr_locked() takes the CPU with the reservation, if cpumask_any_and_distribute() picked a CPU of the other root domain. A sleeping task gets the CPU chosen as its wakeup CPU.
A new mask that still holds an active CPU of the root domain counting the task moves no bandwidth at the attach. The task can still land in the other root domain, if it must leave its CPU. A later patch of this series moves the bandwidth there, without a check for room. It is "sched/deadline: Move a task's bandwidth to the root domain it runs in".
The sums are kept per CPU, not per cpuset: one cpuset can need room in two root domains. One set of sums is enough, since cgroup_mutex lets one attach run at a time. guarantee_active_cpus() now takes the cpuset to start from.
Later in this series, deadline_bw_test gets four cases for this:move_isolated, move_isolated_refused, move_isolated_asleep and move_spanning. Without this patch, all four fail with isolcpus=domain,1,2. With isolcpus=domain,0,1, move_isolated, move_spanning and six other move cases fail. The case move_back_cpu_offline tests a mask that holds an offline CPU.
--- include/linux/sched.h | 8 +- include/linux/sched/deadline.h | 15 ++- kernel/cgroup/cpuset-internal.h | 12 -- kernel/cgroup/cpuset.c | 216 +++++++++++++++++++------------- kernel/sched/core.c | 27 +++- kernel/sched/deadline.c | 133 ++++++++++++++++---- kernel/sched/sched.h | 2 + kernel/sched/syscalls.c | 5 +- 8 files changed, 282 insertions(+), 136 deletions(-)
include/linux/sched.h +7 -1
@@ -752,10 +752,16 @@ 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_resv_cpu is a CPU of the root domain where a cpuset attach +* in progress reserved room for the bandwidth, or -1. +* cpuset_can_attach() sets it, and cpuset_attach() moves the +* bandwidth there. */ struct list_head dl_departed_node;int dl_bw_cpu;unsigned int dl_bw_gen; +int dl_bw_resv_cpu;/* * Bits for DL-server functionality. Also see the comment near
@@ -1968,7 +1974,7 @@ extern void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *extern int set_cpus_allowed_ptr (struct task_struct *p,const struct cpumask *new_mask);extern int set_cpus_allowed_bw_reserved (struct task_struct *p,const struct cpumask *new_mask, -u64 *moved); +int cpu,u64 *moved);extern int dup_user_cpus_ptr (struct task_struct *dst,struct task_struct *src,int node);extern void release_user_cpus_ptr (struct task_struct *p);extern int dl_task_check_affinity (struct task_struct *p,const struct cpumask *mask);
include/linux/sched/deadline.h +9 -6
@@ -50,14 +50,17 @@ extern void dl_accounting_unlock(void);extern void lockdep_assert_dl_accounting_held (void );#endif /* -* Return whether moving DL task @p to @new_mask requires moving DL -* bandwidth accounting between root domains. This helper is specific to -* DL bandwidth move accounting semantics and is shared by -* cpuset_can_attach() and set_cpus_allowed_dl() so both paths use the -* same source root-domain test. +* Return the CPU that a move of DL task @p to the CPUs of @new_mask +* records with its DL bandwidth. Return -1 if the bandwidth stays in the +* root domain that counts it. Like the move, keep the CPUs within the +* affinity set by the user. cpuset_can_attach() reserves the bandwidth in +* the root domain of that CPU. cpuset_attach_task() then moves it there, +* and set_cpus_allowed_dl() makes the same test. */ -extern bool dl_task_needs_bw_move (struct task_struct *p, +extern int dl_task_bw_reserve_cpu (struct task_struct *p,const struct cpumask *new_mask); +extern int dl_task_bw_reserved_cpu (struct task_struct *p); +extern void dl_task_bw_unreserve (struct task_struct *p);static inline bool dl_server (struct sched_dl_entity *dl_se) {
kernel/cgroup/cpuset-internal.h +0 -12
@@ -166,18 +166,6 @@ struct cpuset {*/ bool remote_partition_updating; -/* Number of SCHED_DEADLINE tasks the attach in progress moves here. */ -int nr_migrate_dl_tasks; -/* DL bandwidth that needs destination reservation for this attach. */ -u64 sum_migrate_dl_bw; -/* The part of sum_migrate_dl_bw moved. The rest is given back. */ -u64 sum_moved_dl_bw; -/* -* CPU used for temporary DL bandwidth allocation during attach; -* -1 if no DL bandwidth was allocated in the current attach. -*/ -int dl_bw_cpu; -/* Invalid partition error code, not lock protected */ enum prs_errcode prs_err;
kernel/cgroup/cpuset.c +132 -84
342 lines not on this page · raw message
kernel/sched/core.c +23 -4
@@ -3250,6 +3250,22 @@ static int __set_cpus_allowed_ptr_locked(struct task_struct *p, goto out; } +/* +* The caller made room for the deadline bandwidth of @p in the root +* domain of ctx->dl_bw_cpu. The new mask can span two root domains: +* keep @p in that one. Also wake a sleeping @p up on dest_cpu, since +* select_fallback_rq() could pick a CPU of the other one. +*/ +if ((ctx->flags &SCA_DL_BW_RESERVED ) && +cpumask_test_cpu (ctx->dl_bw_cpu, cpu_valid_mask) && +cpumask_test_cpu (ctx->dl_bw_cpu, ctx->new_mask)) { +if (!cpumask_test_cpu (dest_cpu, +cpu_rq (ctx->dl_bw_cpu)->rd->span)) + dest_cpu = ctx->dl_bw_cpu; +if (!task_on_rq_queued (p)) + p->wake_cpu = dest_cpu; + } +do_set_cpus_allowed (p, ctx);return affine_move_task (rq, p, rf, dest_cpu, ctx->flags);
@@ -3300,15 +3316,18 @@ EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);/* * Like set_cpus_allowed_ptr(), for a caller that has made room for the -* deadline bandwidth of @p in the root domain of @new_mask. Move that -* bandwidth there with @p, and set @moved to the bandwidth moved, 0 -* if none. Return 0 on success, or a negative error code. +* deadline bandwidth of @p in the root domain of @cpu. If the move records +* @cpu with that bandwidth, move the bandwidth there with @p. Set @moved +* to the bandwidth moved, 0 if none. Return 0 on success, or a negative +* error code. */ int set_cpus_allowed_bw_reserved (struct task_struct *p, -const struct cpumask *new_mask,u64 *moved) +const struct cpumask *new_mask,int cpu, +u64 *moved) {struct affinity_context ac = { .new_mask = new_mask, + .dl_bw_cpu = cpu, .flags =SCA_DL_BW_RESERVED , };int ret;
kernel/sched/deadline.c +106 -27
166 lines not on this page · raw message
kernel/sched/sched.h +2 -0
@@ -2680,6 +2680,8 @@ struct affinity_context {struct cpumask *user_mask;/* Out: the deadline bandwidth moved to the new root domain. */ u64 dl_bw_moved; +/* In, with SCA_DL_BW_RESERVED: the CPU the caller made room on. */ +int dl_bw_cpu;unsigned int flags; };
kernel/sched/syscalls.c +3 -2
@@ -1353,8 +1353,9 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)/* * Wait for a cpuset attach in progress before changing the affinity * of a deadline task. cpuset_can_attach() reserved the bandwidth of -* the task for the CPUs it moves to, and cpuset_attach() moves it -* there. A new affinity in between could change those CPUs. +* the task on one CPU, and recorded that CPU with the task. +* cpuset_attach() moves the bandwidth there. A new affinity in +* between could leave that CPU out, and the bandwidth would not move. * * The policy is tested here with no lock. A task that becomes a * deadline task after this test is found under its runqueue lock,
Signature · 1 line
2.47.3
PATCH v10 12/40 selftests/sched: Test cpuset moves in deadline_bw_test
In reply to Mathieu DesnoyersAdd the 24 cases of deadline_bw_test that test the fixes of cpuset attach, earlier in this series. They cover cpuset moves, and what can happen during them:
- a rebuild of the sched domains, a policy change, an affinity change, a wakeup, the death of a thread, a refusal by another controller
- a task asleep, throttled, yielded, frozen or reclaiming
- moves between the housekeeping CPUs and CPUs isolated by isolcpus=domain
- sysrq-n during a move (move_normalized)
- a random mix of all of the above, with affinity changes (random_mix)
The comment above each case gives its scenario. random_mix also compares the this_bw of its CPUs. Five cases need CPUs isolated by isolcpus=domain. move_normalized fires sysrq-n, which changes the whole system, so it needs DEADLINE_TEST_SYSRQ=1. A case skips without what it needs.
The attach fixes are the five patches before this one, from "sched/deadline: Move a task's bandwidth only where room was made" to "cgroup/cpuset: Reserve the bandwidth in the root domain a task moves to".
The message of each fix names the cases that test it. These cases are not named there:
- move_asleep, move_asleep_setattr, move_non_contending, move_died, move_woken and move_woken_full test "sched/deadline: Record the root domain holding a task's bandwidth". They also need the attach fixes, so they come here.
- frozen_moved_switched, move_throttled, reclaim_moved and yield_moved fail with isolcpus=domain,0,1 before the attach fixes.
On v7.4-rc2, in 8 VMs of 8 CPUs, twelve of these cases fail in every VM:
move_affinity_became_deadline move_died move_affinity_changed move_non_contending move_asleep move_vs_rebuild move_back_cpu_offline move_woken move_became_deadline move_woken_full move_became_deadline_no_room random_mix
These cases did not run there: those that need isolcpus=domain (they skip), move_normalized, move_asleep_setattr and move_asleep_fair_setattr.
With the fixes up to here, every case passes wherever it runs.
--- .../sched/deadline/deadline_bw_test.c | 2516 ++++++++++++++++- 1 file changed, 2511 insertions(+), 5 deletions(-)
tools/testing/selftests/sched/deadline/deadline_bw_test.c +2511 -5
@@ -5,9 +5,11 @@* - priority inheritance * - parameter changes * - cpuset moves +* - affinity changes * - deaths, yields and runtime overruns * - bandwidth reclaiming and fair server changes * - CPU hotplug +* - a random mix of most of these * * Most cases measure the free bandwidth of a root domain before and after * they run. probe() counts the tasks of 10ms every 100ms that the root
@@ -25,7 +27,8 @@* The CPUs that isolcpus="domain" isolates are in the default root * domain: the root domain of the isolated CPUs. The other CPUs are the * housekeeping CPUs, in the housekeeping root domain. The test leaves the -* isolated CPUs out, and keeps its tasks off them. +* isolated CPUs out, and keeps its tasks off them. Only the cases that +* need them use them. * * The cases of p_cases[] run with P, a partition root of the last * housekeeping CPU. P is made anew for each case. Making it rebuilds the
@@ -42,7 +45,8 @@* * 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. +* controller. Some need debugfs or CPU hotplug. Five need +* isolcpus="domain" at boot. */ #define _GNU_SOURCE#include <errno.h>
@@ -106,6 +110,7 @@ struct dl_attr {static char root[PATH_MAX ], cg_p[PATH_MAX ], cg_q[PATH_MAX ];/* The online CPUs at the start, ascending, except the isolated ones */ static int ncpus, *online; +static int nisol, *isol;/* the online CPUs that isolcpus="domain" isolates */ static char isolated[4096 ];/* the CPU list of /sys/devices/system/cpu/isolated */ /* Return the @n-th highest online CPU, counting from 1. */
@@ -2539,7 +2544,7 @@ static int child_up(const char *what, int fast)return 0 ; } -enum mv_mode {MV_ASLEEP ,MV_THROTTLED ,MV_NON_CONTENDING }; +enum mv_mode {MV_ASLEEP ,MV_THROTTLED ,MV_YIELDED ,MV_NON_CONTENDING };/* * Fork a process in the caller's cgroup. The process becomes a deadline
@@ -2547,6 +2552,8 @@ enum mv_mode { MV_ASLEEP, MV_THROTTLED, MV_NON_CONTENDING };* - MV_ASLEEP: run 1ms, set mv_state[0] to 1, then sleep. * - MV_THROTTLED: set mv_state[0] to 1, then spin and count in * mv_state[1]. +* - MV_YIELDED: set mv_state[0] to 1. Then spin 0.5ms, count in +* mv_state[1] and yield, in a loop. * - MV_NON_CONTENDING: sleep a period, run a quarter of its runtime, set * mv_state[0] to 1, then sleep. Its 0-lag time is still ahead. * If it is refused SCHED_DEADLINE, the process sets mv_state[0] to -1 and
@@ -2574,6 +2581,14 @@ static pid_t mover(enum mv_mode mode, uint64_t runtime)for (;;)pause (); } +if (mode ==MV_YIELDED ) { + *mv_state =1 ; +for (;;) { +spin_ns (NSEC_PER_MSEC /2 ); + mv_state[1 ]++; +sched_yield (); + } + } *mv_state =1 ;for (;;) mv_state[1 ]++;
@@ -2581,6 +2596,69 @@ static pid_t mover(enum mv_mode mode, uint64_t runtime)return pid; } +static int wait_spinner (int moving); + +/* +* Move a deadline task into P and back, from outside P. The task is a +* mover() process of 30ms every 100ms, forked in the cgroup of the main +* process. Wait until it is in the state of @mode: +* - MV_ASLEEP: asleep for two periods, past its 0-lag time. +* - MV_NON_CONTENDING: asleep for 1ms, before its 0-lag time. +* - MV_THROTTLED, MV_YIELDED: its counter has stopped for 5ms. +* Then write its pid to cgroup.procs of P, then of the root cgroup. Both +* moves must succeed. A throttled or yielded task must run again within +* three periods. Then kill the task. P and the root cgroup must each +* admit as many probe tasks as before the case. +*/ +static int moves (enum mv_mode mode,const char *name) +{ +int base_p, base_r, after_p, after_r, ret =KSFT_PASS ; +pid_t pid; + + base_p =probe (cg_p); + base_r =probe (root); + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =mover (mode,30 *NSEC_PER_MSEC ); +if (pid <0 ||child_up (name, mode ==MV_NON_CONTENDING )) { +reap (pid); +return KSFT_FAIL ; + } +if (mode ==MV_NON_CONTENDING ) { +sleep_ns (NSEC_PER_MSEC ); + }else if (mode ==MV_ASLEEP ) { +sleep_ns (2 *PERIOD ); + }else if (wait_spinner (0 )) { +/* Throttled, or yielded: its counter stops. */ +ksft_print_msg ("%s: never stopped\n" , name); +reap (pid); +return KSFT_FAIL ; + } +if (enter (cg_p, pid) ||enter (root, pid)) { +ksft_print_msg ("%s: a move refused\n" , name); + ret =KSFT_FAIL ; + } +if (mode ==MV_THROTTLED || mode ==MV_YIELDED ) { +int last = mv_state[1 ]; + +sleep_ns (3 *PERIOD ); +if (mv_state[1 ] == last) { +ksft_print_msg ("%s: moved, never ran again\n" , name); + ret =KSFT_FAIL ; + } + } +reap (pid); +sleep_ns (3 *PERIOD ); + after_p =probe (cg_p); + after_r =probe (root); +if (ret ==KSFT_PASS && (after_p != base_p || after_r != base_r)) { +ksft_print_msg ("%s: probe tasks admitted, P %d -> %d, the other CPUs %d -> %d\n" , + name, base_p, after_p, base_r, after_r); + ret =KSFT_FAIL ; + } +return ret; +} +/* * A deadline task of 60ms every 100ms sleeps in P. Move a second sleeping * task of 60ms every 100ms into P from outside. The move must fail with
@@ -2632,6 +2710,136 @@ static int move_refused(void)return ret; } +/* +* A task moved asleep into P is still on a CPU of the root domain it +* left. It sleeps outside P as a deadline task of 10%. The main process +* moves it into P, and changes it to 20%. P has room for that, so +* sched_setattr() must accept it. The kernel must check the request +* against P's root domain, which counts the task. The task then runs on +* P's CPU. +*/ +static int move_asleep_setattr (void ) +{ +int base_p, base_r, ret =KSFT_PASS , err, sfd[2 ]; +pid_t pid; + + base_p =probe (cg_p); + base_r =probe (root); +if (base_p <0 || base_r <0 ||pipe (sfd)) +return KSFT_FAIL ; + mv_state[0 ] =0 ; + pid =fork_child ("a task to move into P" ); +if (!pid) { +char c; + +close (sfd[1 ]); + mv_state[0 ] =1 ; +while (read (sfd[0 ], &c,1 ) ==1 ) + ; +_exit (0 ); + } +if (pid <0 ||child_up ("a task to move into P" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' )) { +reap (pid); + ret =KSFT_FAIL ; + goto out; + } + err =set_deadline (pid,10 *NSEC_PER_MSEC ,PERIOD ); +if (err) { +ksft_print_msg ("a task of 10%% outside P refused: %s\n" ,strerror (-err)); + ret =KSFT_FAIL ; + }else if (enter (cg_p, pid)) { +ksft_print_msg ("the move into P of a task of 10%% refused\n" ); + ret =KSFT_FAIL ; + }else { + err =set_deadline (pid,20 *NSEC_PER_MSEC ,PERIOD ); +if (err) { +ksft_print_msg ("10%% to 20%% for a task moved asleep into P: %s\n" , +strerror (-err)); + ret =KSFT_FAIL ; + }else if (write (sfd[1 ],"w" ,1 ) !=1 || +wait_for (1000 ,task_cpu_of (pid) ==top_cpu (1 ))) { +ksft_print_msg ("a task moved into P did not wake on P's CPU\n" ); + ret =KSFT_FAIL ; + } + } +set_other (pid); +reap (pid); +out: +close (sfd[0 ]); +close (sfd[1 ]); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS && (probe (cg_p) != base_p ||probe (root) != base_r)) { +ksft_print_msg ("the free bandwidth changed after the case\n" ); + ret =KSFT_FAIL ; + } +return ret; +} + +/* +* The same, for a fair task: it sleeps outside P, and the main process +* moves it into P. Then it makes it a deadline task of 10%. P has room +* for that, so sched_setattr() must accept it, and P must count the task. +* The root domain the task left must not. The task then runs on P's CPU. +*/ +static int move_asleep_fair_setattr (void ) +{ +int base_p, base_r, ret =KSFT_PASS , err, sfd[2 ]; +pid_t pid; + + base_p =probe (cg_p); + base_r =probe (root); +if (base_p <0 || base_r <0 ||pipe (sfd)) +return KSFT_FAIL ; + mv_state[0 ] =0 ; + pid =fork_child ("a fair task to move into P" ); +if (!pid) { +char c; + +close (sfd[1 ]); + mv_state[0 ] =1 ; +while (read (sfd[0 ], &c,1 ) ==1 ) + ; +_exit (0 ); + } +if (pid <0 ||child_up ("a fair task to move into P" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' )) { +reap (pid); + ret =KSFT_FAIL ; + goto out; + } +if (enter (cg_p, pid)) { +ksft_print_msg ("the move into P of a fair task refused\n" ); + ret =KSFT_FAIL ; + goto stop; + } + err =set_deadline (pid,10 *NSEC_PER_MSEC ,PERIOD ); +if (err) { +ksft_print_msg ("10%% for a fair task moved asleep into P: %s\n" , +strerror (-err)); + ret =KSFT_FAIL ; + }else if (probe (cg_p) != base_p -1 ||probe (root) != base_r) { +ksft_print_msg ("P does not count the task, or the root domain it left does\n" ); + ret =KSFT_FAIL ; + }else if (write (sfd[1 ],"w" ,1 ) !=1 || +wait_for (1000 ,task_cpu_of (pid) ==top_cpu (1 ))) { +ksft_print_msg ("a task moved into P did not wake on P's CPU\n" ); + ret =KSFT_FAIL ; + } +stop: +set_other (pid); +reap (pid); +out: +close (sfd[0 ]); +close (sfd[1 ]); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS && (probe (cg_p) != base_p ||probe (root) != base_r)) { +ksft_print_msg ("the free bandwidth changed after the case\n" ); + ret =KSFT_FAIL ; + } +return ret; +} +/* * A runtime of 1024ns every 2s gives a bandwidth of 0. A cpuset move * skips such a task: it has no bandwidth to move.
@@ -2722,6 +2930,44 @@ static int move_zero_bw(void)return ret; } +static int move_asleep (void ) +{ +return moves (MV_ASLEEP ,"move_asleep" ); +} + +static int move_throttled (void ) +{ +return moves (MV_THROTTLED ,"move_throttled" ); +} + +static int move_non_contending (void ) +{ +return moves (MV_NON_CONTENDING ,"move_non_contending" ); +} + +/* +* Fork a process that reads sched_rt_runtime_us and writes the same value +* back @n times, 5ms apart. Then it exits. Each write rebuilds the sched +* domains. Return its pid, or -1. +*/ +static pid_t rebuilder (int n) +{ +pid_t pid =fork_child ("a sysctl writer" ); + +if (!pid) { +char buf[64 ]; + +if (read_file ("/proc/sys/kernel/sched_rt_runtime_us" , buf,sizeof (buf))) +_exit (1 ); +while (n-- >0 ) { +write_file ("/proc/sys/kernel" ,"sched_rt_runtime_us" , buf); +sleep_ns (5 *NSEC_PER_MSEC ); + } +_exit (0 ); + } +return pid; +} +static void *pauser (void *arg) {for (;;)
@@ -2729,9 +2975,200 @@ static void *pauser(void *arg)return arg; } -/* What late_setter() does, and the result. */ +/* +* Run as the deadline thread of wide_mover(): a deadline task of 30% that +* runs 1ms, sets mv_state[0] to 1 and sleeps. +*/ +static void *dl_pauser (void *arg) +{ +if (set_deadline (0 ,30 *NSEC_PER_MSEC ,PERIOD )) { + mv_state[0 ] =-1 ; +return arg; + } +spin_ns (NSEC_PER_MSEC ); + *mv_state =1 ; +for (;;) +pause (); +return arg; +} + +/* +* Fork a process of @threads sleeping threads, then one deadline thread, +* dl_pauser(). A cgroup move of the process moves its threads one by one, +* between cpuset_can_attach() and cpuset_attach(). The deadline thread +* moves last, so that a rebuild in that window can still find it in the +* cgroup it leaves. Return the pid, or -1. +*/ +static pid_t wide_mover (int threads) +{ +pid_t pid =fork_child ("a wide deadline process" ); + +if (!pid) { +pthread_attr_t a; +pthread_t t; +int i; + +if (pthread_attr_init (&a) ||pthread_attr_setstacksize (&a,THREAD_STACK )) + goto no_threads; +for (i =0 ; i < threads; i++) { +if (pthread_create (&t, &a, pauser,NULL )) + goto no_threads; + } +if (pthread_create (&t,NULL , dl_pauser,NULL )) + goto no_threads; +for (;;) +pause (); +no_threads: + mv_state[0 ] =-2 ; +_exit (1 ); + } +return pid; +} + +/* +* Move a process in and out of P while rebuilder() rebuilds the sched +* domains. The process is wide_mover(): 2048 sleeping threads, then one +* sleeping deadline thread of 30%. rebuilder() writes the +* sched_rt_runtime_us sysctl 20 times, 5ms apart. Each write is a +* rebuild. Without the 5ms gaps, the rebuilds would be over before the +* first move reaches the window below. +* +* A rebuild between cpuset_can_attach() and cpuset_attach() would drop +* the bandwidth that cpuset_can_attach() reserved in the destination. +* cpuset_attach() would then move the bandwidth of the thread all the +* same. So a rebuild must wait for the end of a move in progress. +* +* A later rebuild corrects the accounting. So move until rebuilder() +* exits: no rebuild follows the last move. The free bandwidth of P and +* of the other CPUs must then be what it was with the process asleep in +* the root cgroup. Run six rounds. +*/ +static int move_vs_rebuild (void ) +{ +int base_p, base_r, with_p, with_r, after_p, after_r, round, ret =KSFT_PASS ; +const char *name ="move_vs_rebuild" ; +pid_t pid, writer; + + base_p =probe (cg_p); + base_r =probe (root); + mv_state[0 ] =0 ; + pid =wide_mover (2048 ); +if (pid <0 ||child_up (name,0 )) { +reap (pid); +return mv_state[0 ] ==-2 ?KSFT_SKIP :KSFT_FAIL ; + } +/* The reference for the rounds: the task asleep in the root cgroup. */ +sleep_ns (2 *PERIOD ); + with_p =probe (cg_p); + with_r =probe (root); +for (round =0 ; round <6 && ret ==KSFT_PASS ; round++) { +int status, err =0 ; + + writer =rebuilder (20 ); +if (writer <0 ) { + ret =KSFT_FAIL ; +break ; + } +while (!err &&waitpid (writer, &status,WNOHANG ) ==0 ) { + err =enter (cg_p, pid); +if (!err) + err =enter (root, pid); + } +if (err) { +ksft_print_msg ("%s: a move refused: %s\n" , name,strerror (-err)); +reap (writer); + ret =KSFT_FAIL ; +break ; + } +sleep_ns (3 *PERIOD ); + after_p =probe (cg_p); + after_r =probe (root); +if (after_p != with_p || after_r != with_r) { +ksft_print_msg ("%s: round %d: P %d -> %d probe tasks, the rest %d -> %d\n" , + name, round, with_p, after_p, with_r, after_r); + ret =KSFT_FAIL ; + } + } +reap (pid); +sleep_ns (3 *PERIOD ); + after_p =probe (cg_p); + after_r =probe (root); +if (ret ==KSFT_PASS && (after_p != base_p || after_r != base_r)) { +ksft_print_msg ("%s: task gone: P %d -> %d probe tasks, the rest %d -> %d\n" , + name, base_p, after_p, base_r, after_r); + ret =KSFT_FAIL ; + } +return ret; +} + +/* The runtime every 100ms of the deadline threads of late_mover() */ +static uint64_t late_thread_runtime; + +/* +* Run as a deadline thread of late_mover(): a deadline task of +* late_thread_runtime that counts itself in mv_state[1] and sleeps. +*/ +static void *dl_sleeper (void *arg) +{ +if (set_deadline (0 , late_thread_runtime,PERIOD )) { + mv_state[0 ] =-1 ; +return arg; + } + mv_state[1 ]++; +for (;;) +pause (); +return arg; +} + +/* +* Fork a process of two deadline threads, dl_sleeper(), then @threads +* sleeping threads. Its main thread, the thread group leader, sleeps and +* is not a deadline task. A cgroup move of the process handles the +* leader first. The sleeping threads make the move long. Return the pid, +* or -1. +*/ +static pid_t late_mover (int threads) +{ +pid_t pid =fork_child ("a wide process" ); + +if (!pid) { +pthread_attr_t a; +pthread_t t; +int i; + +if (pthread_attr_init (&a) ||pthread_attr_setstacksize (&a,THREAD_STACK )) + goto no_threads; +for (i =0 ; i <2 ; i++) { +if (pthread_create (&t, &a, dl_sleeper,NULL )) + goto no_threads; + } +for (i =0 ; i < threads; i++) { +if (pthread_create (&t, &a, pauser,NULL )) + goto no_threads; + } +if (wait_for_step (5000 ,NSEC_PER_MSEC , mv_state[1 ] ==2 || mv_state[0 ])) + goto no_threads; +if (!mv_state[0 ]) + mv_state[0 ] =1 ; +for (;;) +pause (); +no_threads: + mv_state[0 ] =-2 ; +_exit (1 ); + } +return pid; +} + +/* +* What late_setter(), late_pinner() and late_killer() do, and the result. +* Each acts on thread @pid, @delay ns after @go is set. It stores its +* result in @ret and the time it returned in @done. late_setter() uses +* @runtime. late_killer() uses @tgid and @kill. +*/ static struct { pid_t pid; + pid_t tgid; + int kill; uint64_t runtime; uint64_t delay; atomic_int go;
@@ -2758,18 +3195,167 @@ static void *late_setter(void *arg)return arg; } +/* +* Make the main thread of a process a deadline task while the process +* moves from the root cgroup into P. +* +* The process is late_mover(). Its main thread sleeps and is not a +* deadline task. Two of its threads are sleeping deadline tasks of +* @thread percent each. 2048 more sleeping threads make the move long. +* A thread of the test, late_setter(), calls sched_setattr() on the main +* thread to make it a deadline task of @leader percent. +* +* cpuset_can_attach() reserves room in P for the two deadline threads. +* If the main thread became a deadline task after that, no room would be +* reserved for it. cpuset_attach() handles the main thread first. +* +* sched_setattr() waits for the end of a move in progress. So it runs +* before the move or after it: +* - Before: the move counts three deadline tasks. With room in P for +* the three, the move must succeed. P must then count the three, and +* the root domain the process left must count none. Without room, +* the move must be refused with EBUSY. +* - After: the main thread is allowed on P's CPU only. It is still +* asleep on a CPU of the root domain it left. sched_setattr() asks P +* for room. Without room, it fails with EBUSY. +* Without room in P for the three, the case fails if the move and +* sched_setattr() both succeed. +* +* Try 30 delays of sched_setattr(), spread over the time a move takes. +*/ +static int became_deadline (int thread,int leader,const char *name) +{ +int base_p, base_r, ret =KSFT_PASS , i, raced =0 ; +int need = (2 * thread + leader) /10 , fits; +uint64_t start, len; +pid_t pid; + + base_p =probe (cg_p); + base_r =probe (root); + fits = base_p >= need; +if (base_p <2 * thread /10 ) { +ksft_print_msg ("%s: P admits %d probe tasks, %d needed\n" , name, base_p, +2 * thread /10 ); +return KSFT_SKIP ; + } + late_thread_runtime = thread *NSEC_PER_MSEC ; + late.runtime = leader *NSEC_PER_MSEC ; + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =late_mover (2048 ); +if (pid <0 ||child_up (name,0 )) { +reap (pid); +return mv_state[0 ] ==-2 ?KSFT_SKIP :KSFT_FAIL ; + } +/* Time one move, to spread the delays over it. */ + start =now_ns (); +if (enter (cg_p, pid) ||enter (root, pid)) { +ksft_print_msg ("%s: a move refused\n" , name); +reap (pid); +return KSFT_FAIL ; + } + len = (now_ns () - start) /2 ; +sleep_ns (2 *PERIOD ); + + late.pid = pid; +for (i =0 ; i <30 && ret ==KSFT_PASS ; i++) { +pthread_t t; +int err; + + late.go =0 ; + late.delay = len * i /30 ; +if (pthread_create (&t,NULL , late_setter,NULL )) { + ret =KSFT_FAIL ; +break ; + } +sleep_ns (NSEC_PER_MSEC ); + late.go =1 ; + err =enter (cg_p, pid); +join (t); +if (err && (fits || err != -EBUSY || late.ret)) { +ksft_print_msg ("%s: the move refused: %s\n" , name,strerror (-err)); + ret =KSFT_FAIL ; + }else if (!err && !late.ret && !fits) { +ksft_print_msg ("%s: the move and the change both succeeded, with no room in P\n" , + name); + ret =KSFT_FAIL ; + }else if (!err && !late.ret) { + raced++; +if (admits_in (cg_p, base_p - need,"P, with the three tasks" ) || +admits_in (root, base_r,"the root domain the tasks left" )) + ret =KSFT_FAIL ; + }else if (late.ret && (fits || late.ret != -EBUSY )) { +ksft_print_msg ("%s: sched_setattr: %s\n" , name,strerror (-late.ret)); + ret =KSFT_FAIL ; + } +if (ret ==KSFT_FAIL ) +ksft_print_msg ("%s: try %d, the change %llu us into a move of %llu us\n" , + name, i, (unsigned long long )(late.delay /NSEC_PER_USEC ), + (unsigned long long )(len /NSEC_PER_USEC )); +if ((!late.ret &&set_other (pid)) ||enter (root, pid)) { +ksft_print_msg ("%s: the process cannot go back\n" , name); + ret =KSFT_FAIL ; + } +sleep_ns (2 *PERIOD ); + } +if (ret ==KSFT_PASS && fits && !raced) +ksft_print_msg ("%s: no change succeeded with a move\n" , name); +reap (pid); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS && (probe (cg_p) != base_p ||probe (root) != base_r)) { +ksft_print_msg ("%s: the free bandwidth changed\n" , name); + ret =KSFT_FAIL ; + } +return ret; +} + +/* Run it with room in P: two threads of 20% and a main thread of 30%. */ +static int move_became_deadline (void ) +{ +return became_deadline (20 ,30 ,"move_became_deadline" ); +} + +/* Run it without room in P: two threads of 30% and a main thread of 40%. */ +static int move_became_deadline_no_room (void ) +{ +return became_deadline (30 ,40 ,"move_became_deadline_no_room" ); +} +static void no_op (int sig) { } +/* The CPU that dl_sleeper_tid() first runs on, or -1 for any CPU */ +static int sleeper_cpu =-1 ; +/* * Run as the deadline thread of pinned_mover(): a deadline task of 30%. * Store the tid in mv_state[3], set mv_state[0] to 1 and sleep. A signal * wakes the thread up. It then sleeps again. If mv_state[4] is set, this * thread exits instead, and the process stays. +* +* With sleeper_cpu set, first run 1ms pinned on that CPU, then restore +* the previous affinity. Do this before sched_setattr(): the affinity of +* a deadline task must include every CPU of its root domain. */ static void *dl_sleeper_tid (void *arg) { +if (sleeper_cpu >=0 ) { +cpu_set_t all, one; + +CPU_ZERO (&one); +CPU_SET (sleeper_cpu, &one); +if (sched_getaffinity (0 ,sizeof (all), &all) || +sched_setaffinity (0 ,sizeof (one), &one)) { + mv_state[0 ] =-2 ; +return arg; + } +spin_ns (NSEC_PER_MSEC ); +if (sched_setaffinity (0 ,sizeof (all), &all)) { + mv_state[0 ] =-2 ; +return arg; + } + }if (set_deadline (0 ,30 *NSEC_PER_MSEC ,PERIOD )) { mv_state[0 ] =-1 ;return arg;
@@ -2834,6 +3420,618 @@ static pid_t pinned_mover(int threads, int fifo)return pid; } +/* +* Restrict late.pid to the CPU of P and the CPU in @arg, late.delay ns +* after late.go. +*/ +static void *late_pinner (void *arg) +{ +uint64_t start; +cpu_set_t set; + +CPU_ZERO (&set); +CPU_SET (top_cpu (1 ), &set); +CPU_SET ((long )arg, &set); +while (!late.go) + ; + start =now_ns (); +while (now_ns () - start < late.delay) + ; + late.ret =sched_setaffinity (late.pid,sizeof (set), &set) ? -errno :0 ; + late.done =now_ns (); +return NULL ; +} + +/* +* Change the affinity of a sleeping deadline thread while its process +* moves from P to the root cgroup. +* +* The process is pinned_mover(): 2048 sleeping threads, then a deadline +* thread of 30%. That thread went to sleep on a CPU of the root cgroup, +* and was moved into P asleep. So P counts it, and it is still on that +* CPU. A thread of the test, late_pinner(), calls sched_setaffinity() +* on it. +* +* cpuset_can_attach() reserves the bandwidth of the thread on one CPU of +* the root cgroup. The move records that CPU. It is the CPU the thread +* is on, if its affinity allows that CPU. If not, it is the first CPU of +* the root cgroup that its affinity allows. +* +* sched_setaffinity() restricts the thread to two CPUs: the CPU of P, +* and a CPU of the root cgroup that the thread is not on. The affinity +* of a deadline task must include every CPU of the root domain that +* counts it, so the mask needs the CPU of P. The move then records the +* second CPU. +* +* sched_setaffinity() waits for the end of a move in progress. So it +* runs before the move or after it: +* - Before: the bandwidth is reserved on the CPU the move records. The +* root domain of that CPU must count the thread once the move is +* done, and P must no longer count it. +* - After: the thread is counted in the root domain of the root +* cgroup. The mask does not include every CPU of that root domain. +* sched_setaffinity() is refused with EBUSY. +* +* Try 20 delays of sched_setaffinity(), spread over the time a move +* takes. +*/ +static int move_affinity_changed (void ) +{ +int with_p, with_r, ret =KSFT_PASS , i, raced =0 ; +const char *name ="move_affinity_changed" ; +uint64_t start, len; +cpu_set_t all; +pid_t pid; + +CPU_ZERO (&all); +for (i =0 ; i < ncpus; i++) +CPU_SET (online[i], &all); + mv_state[0 ] =0 ; + mv_state[3 ] =0 ; + mv_state[4 ] =0 ; + pid =pinned_mover (2048 ,0 ); +if (pid <0 ||child_up (name,0 )) { +reap (pid); +return mv_state[0 ] ==-2 ?KSFT_SKIP :KSFT_FAIL ; + } +/* The reference: the thread asleep in the root cgroup. */ +sleep_ns (2 *PERIOD ); + with_p =probe (cg_p); + with_r =probe (root); +/* Time one move, to spread the delays over it. */ + start =now_ns (); +if (enter (cg_p, pid) ||enter (root, pid)) { +ksft_print_msg ("%s: a move refused\n" , name); +reap (pid); +return KSFT_FAIL ; + } + len = (now_ns () - start) /2 ; + + late.pid = mv_state[3 ]; +for (i =0 ; i <20 && ret ==KSFT_PASS ; i++) { +pthread_t t; +long cpu; +int err; + +if (sched_setaffinity (late.pid,sizeof (all), &all) ||enter (cg_p, pid)) { +ksft_print_msg ("%s: the process cannot go into P\n" , name); + ret =KSFT_FAIL ; +break ; + } +/* Not the CPU the thread is on: the move would record that one. */ + cpu =task_cpu_of (late.pid) == online[1 ] ? online[0 ] : online[1 ]; + late.go =0 ; + late.delay = len * i /20 ; +if (pthread_create (&t,NULL , late_pinner, (void *)cpu)) { + ret =KSFT_FAIL ; +break ; + } +sleep_ns (NSEC_PER_MSEC ); + late.go =1 ; + err =enter (root, pid); +join (t); +if (err) { +ksft_print_msg ("%s: the move refused: %s\n" , name,strerror (-err)); + ret =KSFT_FAIL ; + }else if (!late.ret) { + raced++; +sleep_ns (2 *PERIOD ); +if (admits_in (cg_p, with_p,"P, which the thread left" ) || +admits_in (root, with_r,"the root domain of the thread" )) { +ksft_print_msg ("%s: try %d, the change %llu us into a move of %llu us\n" , + name, i, (unsigned long long )(late.delay /NSEC_PER_USEC ), + (unsigned long long )(len /NSEC_PER_USEC )); + ret =KSFT_FAIL ; + } + }else if (late.ret && late.ret != -EBUSY ) { +ksft_print_msg ("%s: sched_setaffinity: %s\n" , name,strerror (-late.ret)); + ret =KSFT_FAIL ; + } + } +if (ret ==KSFT_PASS && !raced) +ksft_print_msg ("%s: no change succeeded\n" , name); +reap (pid); +return ret; +} + +/* +* Make a thread a deadline task while sched_setaffinity() runs on it, and +* move its process from P to the root cgroup meanwhile. +* +* sched_setaffinity() waits for the end of a move in progress when the +* task is a deadline task. It tests the policy first, with no lock. Here +* the thread T is SCHED_OTHER at that test: +* +* sched_setaffinity(T) sched_setattr(T) echo PID > cgroup.procs +* +* T is SCHED_OTHER: no wait +* T is a deadline task +* cpuset_can_attach() +* reserves room for T +* changes the affinity of T +* cpuset_attach() +* moves T +* +* The new affinity holds the CPU of P and one CPU of the root cgroup. +* The room was reserved on another CPU. Once the move is done, the +* root domain of the root cgroup must count T, and P must not. +* +* Delay injection: the window after the test of the policy is a few +* instructions wide. The case only reaches it on a kernel that can make +* sched_setaffinity() sleep there. It passes without reaching it +* otherwise. +*/ +static int move_affinity_became_deadline (void ) +{ +const char *name ="move_affinity_became_deadline" ; +int with_p, with_r, ret =KSFT_PASS , i; +uint64_t start, len, delay_ms; +cpu_set_t all; +pid_t pid; + +CPU_ZERO (&all); +for (i =0 ; i < ncpus; i++) +CPU_SET (online[i], &all); + mv_state[0 ] =0 ; + mv_state[3 ] =0 ; + mv_state[4 ] =0 ; + pid =pinned_mover (2048 ,0 ); +if (pid <0 ||child_up (name,0 )) { +reap (pid); +return mv_state[0 ] ==-2 ?KSFT_SKIP :KSFT_FAIL ; + } +/* The reference: the thread a deadline task, asleep in the root cgroup. */ +sleep_ns (2 *PERIOD ); + with_p =probe (cg_p); + with_r =probe (root); +/* Time one move, to place the change inside it. */ + start =now_ns (); +if (enter (cg_p, pid) ||enter (root, pid)) { +ksft_print_msg ("%s: a move refused\n" , name); +reap (pid); +return KSFT_FAIL ; + } + len = (now_ns () - start) /2 ; + delay_ms =3 * len /NSEC_PER_MSEC +30 ; + + late.pid = mv_state[3 ]; +for (i =0 ; i <10 && ret ==KSFT_PASS ; i++) { +uint64_t t0, at; +pthread_t t; +int err; + +if (sched_setaffinity (late.pid,sizeof (all), &all) ||enter (cg_p, pid) || +set_other (late.pid)) { +ksft_print_msg ("%s: the thread cannot go into P as SCHED_OTHER\n" , name); + ret =KSFT_FAIL ; +break ; + } +/* +* Wake the thread once, so that it runs on the CPU of P. A +* thread moved asleep is refused SCHED_DEADLINE with EPERM. +*/ +if (syscall (SYS_tgkill, pid, late.pid,SIGUSR1 )) { + ret =KSFT_FAIL ; +break ; + } +sleep_ns (5 *NSEC_PER_MSEC ); +/* +* Delay injection: sched_setaffinity() would be asked here to +* sleep delay_ms after its test of the policy. +*/ + late.go =0 ; + late.delay =0 ; +if (pthread_create (&t,NULL , late_pinner, (void *)(long )online[1 ])) { + ret =KSFT_FAIL ; +break ; + } +sleep_ns (NSEC_PER_MSEC ); + t0 =now_ns (); + late.go =1 ; +/* With the delay, the change has tested the policy and sleeps. */ +sleep_ns (5 *NSEC_PER_MSEC ); + err =set_deadline (late.pid,30 *NSEC_PER_MSEC ,PERIOD ); +if (err) { +ksft_print_msg ("%s: sched_setattr: %s\n" , name,strerror (-err)); + ret =KSFT_FAIL ; + } +/* Start the move so that the change wakes up inside it. */ + at = t0 + delay_ms *NSEC_PER_MSEC - len * (i +1 ) /11 ; +while (now_ns () < at) + ; + err =enter (root, pid); +join (t); +if (err) { +ksft_print_msg ("%s: the move refused: %s\n" , name,strerror (-err)); + ret =KSFT_FAIL ; + }else if (!late.ret) { +sleep_ns (2 *PERIOD ); +if (admits_in (cg_p, with_p,"P, which the thread left" ) || +admits_in (root, with_r,"the root domain of the thread" )) { +ksft_print_msg ("%s: try %d, the change landed in a move of %llu us\n" , + name, i, (unsigned long long )(len /NSEC_PER_USEC )); + ret =KSFT_FAIL ; + } + }else if (late.ret && late.ret != -EBUSY ) { +ksft_print_msg ("%s: sched_setaffinity: %s\n" , name,strerror (-late.ret)); + ret =KSFT_FAIL ; + } + } +reap (pid); +return ret; +} + +/* +* Wake thread late.pid of process late.tgid up with a signal, late.delay +* ns after late.go. With late.kill, the thread then exits. +*/ +static void *late_killer (void *arg) +{ +uint64_t start; + +while (!late.go) + ; + start =now_ns (); +while (now_ns () - start < late.delay) + ; + mv_state[4 ] = late.kill; + late.ret =syscall (SYS_tgkill, late.tgid, late.pid,SIGUSR1 ) ? -errno :0 ; + late.done =now_ns (); +return arg; +} + +/* +* Kill a sleeping deadline thread of 30% while its process moves into +* P. A thread that is part of a move cannot exit before the move ends: +* its exit waits for it. So a thread killed during the move dies right +* after it, and must release its bandwidth where the move left it. Once the process +* is gone, the free bandwidth of P and of the root domain the process +* left must be what it was. +* +* Try 12 delays spread over the time a move takes, each with a new +* process. +*/ +static int move_died (void ) +{ +int base_p, base_r, ret =KSFT_PASS , i; +const char *name ="move_died" ; +uint64_t len =0 ; + + base_p =probe (cg_p); + base_r =probe (root); +for (i =0 ; i <12 && ret ==KSFT_PASS ; i++) { +pthread_t t; +pid_t pid; +int err; + + mv_state[0 ] =0 ; + mv_state[3 ] =0 ; + mv_state[4 ] =0 ; + pid =pinned_mover (2048 ,0 ); +if (pid <0 ||child_up (name,0 )) { +reap (pid); +return mv_state[0 ] ==-2 ?KSFT_SKIP :KSFT_FAIL ; + } +if (!i) { +/* Time one move, to spread the delays over it. */ +uint64_t start =now_ns (); + +if (enter (cg_p, pid) ||enter (root, pid)) { +ksft_print_msg ("%s: a move refused\n" , name); +reap (pid); +return KSFT_FAIL ; + } + len = (now_ns () - start) /2 ; + } + late.pid = mv_state[3 ]; + late.tgid = pid; + late.kill =1 ; + late.go =0 ; + late.delay = len * i /12 ; +if (pthread_create (&t,NULL , late_killer,NULL )) { +reap (pid); +return KSFT_FAIL ; + } +sleep_ns (NSEC_PER_MSEC ); + late.go =1 ; + err =enter (cg_p, pid); +join (t); +if (err) { +ksft_print_msg ("%s: the move refused: %s\n" , name,strerror (-err)); + ret =KSFT_FAIL ; + } +reap (pid); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS && + (admits_in (cg_p, base_p,"P, after the thread died" ) || +admits_in (root, base_r,"the root domain the process left" ))) { +ksft_print_msg ("%s: try %d, the death %llu us into a move of %llu us\n" , + name, i, (unsigned long long )(late.delay /NSEC_PER_USEC ), + (unsigned long long )(len /NSEC_PER_USEC )); + ret =KSFT_FAIL ; + } + } +return ret; +} + +static int late_sysrq_fd; + +/* +* Write 'n' to /proc/sysrq-trigger, late.delay ns after late.go. sysrq-n +* makes every real-time and deadline task SCHED_OTHER. +*/ +static void *late_sysrq (void *arg) +{ +uint64_t start; + +while (!late.go) + ; + start =now_ns (); +while (now_ns () - start < late.delay) + ; + late.ret =write (late_sysrq_fd,"n" ,1 ) ==1 ?0 : -errno; + late.done =now_ns (); +return arg; +} + +/* +* Make sysrq-n take a sleeping deadline thread of 30% out of the class +* while its process moves into P. cpuset_can_attach() reserves room in P +* for the thread. sysrq-n does not wait for the move. If it comes after +* the reservation, the thread moves no bandwidth, and cpuset_attach() +* must give the room back. Once the process is gone, the free bandwidth of P and of +* the root domain the process left must be what it was. +* +* sysrq-n changes the whole system. So the case runs last of the cases +* with P, and only with DEADLINE_TEST_SYSRQ set. +* +* Try 12 delays spread over the time a move takes, each with a new +* process. +*/ +static int move_normalized (void ) +{ +int base_p, base_r, ret =KSFT_PASS , i; +const char *name ="move_normalized" ; +uint64_t len =0 ; + +if (!getenv ("DEADLINE_TEST_SYSRQ" )) { +ksft_print_msg ("sysrq-n renices every real-time task: set DEADLINE_TEST_SYSRQ=1\n" ); +return KSFT_SKIP ; + } + late_sysrq_fd =open ("/proc/sysrq-trigger" ,O_WRONLY ); +if (late_sysrq_fd <0 ) { +ksft_print_msg ("/proc/sysrq-trigger: %s\n" ,strerror (errno)); +return KSFT_SKIP ; + } + base_p =probe (cg_p); + base_r =probe (root); +for (i =0 ; i <12 && ret ==KSFT_PASS ; i++) { +pthread_t t; +pid_t pid; +int err; + + mv_state[0 ] =0 ; + mv_state[3 ] =0 ; + mv_state[4 ] =0 ; + pid =pinned_mover (2048 ,0 ); +if (pid <0 ||child_up (name,0 )) { +reap (pid); +close (late_sysrq_fd); +return mv_state[0 ] ==-2 ?KSFT_SKIP :KSFT_FAIL ; + } +if (!i) { +/* Time one move, to spread the delays over it. */ +uint64_t start =now_ns (); + +if (enter (cg_p, pid) ||enter (root, pid)) { +ksft_print_msg ("%s: a move refused\n" , name); +reap (pid); +close (late_sysrq_fd); +return KSFT_FAIL ; + } + len = (now_ns () - start) /2 ; + } + late.go =0 ; + late.delay = len * i /12 ; +if (pthread_create (&t,NULL , late_sysrq,NULL )) { +reap (pid); +close (late_sysrq_fd); +return KSFT_FAIL ; + } +sleep_ns (NSEC_PER_MSEC ); + late.go =1 ; + err =enter (cg_p, pid); +join (t); +if (err) { +ksft_print_msg ("%s: the move refused: %s\n" , name,strerror (-err)); + ret =KSFT_FAIL ; + }else if (late.ret) { +ksft_print_msg ("%s: sysrq-n: %s\n" , name,strerror (-late.ret)); + ret =KSFT_SKIP ; + } +reap (pid); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS && + (admits_in (cg_p, base_p,"P, after sysrq-n" ) || +admits_in (root, base_r,"the root domain the process left" ))) { +ksft_print_msg ("%s: try %d, sysrq-n %llu us into a move of %llu us\n" , + name, i, (unsigned long long )(late.delay /NSEC_PER_USEC ), + (unsigned long long )(len /NSEC_PER_USEC )); + ret =KSFT_FAIL ; + } + } +close (late_sysrq_fd); +return ret; +} + +static int hold (int n); +static void release_held (void ); + +/* +* Wake a sleeping deadline thread of 30% up while its process moves from +* P to the root cgroup. +* +* The process is pinned_mover(): 2048 sleeping threads, then the deadline +* thread. That thread first ran on the second CPU of the root cgroup, +* went to sleep there, and was moved into P asleep. So P counts it, and +* it is still on that CPU. A thread of the test, late_killer(), wakes it +* up with a signal. +* +* cpuset_can_attach() reserves room for the thread on the CPU that the +* move back records: the CPU the thread is on. If the thread wakes up +* after that, it runs on the CPU of P. cpuset_attach() must still move +* the bandwidth to the CPU recorded. Working the CPU out again would +* give the first CPU of the root cgroup, where nothing is reserved. +* +* Both CPUs are in the same root domain. So that root domain must count +* the thread once the move is done, and P must no longer count it. +* +* With @full, hold() first fills that root domain with deadline threads +* of the test, until 40% is free. The thread of 30% then fits once, and +* not twice. +* +* Try 12 delays spread over the time a move takes, each with a new +* process. +*/ +static int woken (int full,const char *name) +{ +int base_p, base_r, free_r, ret =KSFT_PASS , i; +uint64_t len =0 ; + + base_p =probe (cg_p); + base_r =probe (root); +if (base_r <5 ) { +ksft_print_msg ("%s: no room for the thread\n" , name); +return KSFT_SKIP ; + } + free_r = base_r; +if (full) { +if (hold (base_r -4 )) { +release_held (); +return KSFT_FAIL ; + } + free_r =4 ; + } +for (i =0 ; i <12 && ret ==KSFT_PASS ; i++) { +pid_t pid, tid; +pthread_t t; +int err; + + mv_state[0 ] =0 ; + mv_state[3 ] =0 ; + mv_state[4 ] =0 ; +/* Not the first CPU: the move falls back to that one. */ + sleeper_cpu = online[1 ]; + pid =pinned_mover (2048 ,0 ); + sleeper_cpu =-1 ; +if (pid <0 ||child_up (name,0 )) { +reap (pid); +release_held (); +return mv_state[0 ] ==-2 ?KSFT_SKIP :KSFT_FAIL ; + } + tid = mv_state[3 ]; +sleep_ns (PERIOD ); +if (!i) { +/* +* Time the move into P, to spread the delays over +* a move. +*/ +uint64_t start =now_ns (); + + err =enter (cg_p, pid); +if (!err) { + len =now_ns () - start; + err =enter (root, pid); + } +if (err) { +ksft_print_msg ("%s: a move refused\n" , name); +reap (pid); +release_held (); +return KSFT_FAIL ; + } + } +if (enter (cg_p, pid)) { +ksft_print_msg ("%s: the process cannot go into P\n" , name); +reap (pid); +release_held (); +return KSFT_FAIL ; + } +if (task_cpu_of (tid) != online[1 ]) { +ksft_print_msg ("%s: the thread is on CPU %d, not on CPU %d\n" , name, +task_cpu_of (tid), online[1 ]); +reap (pid); +release_held (); +return KSFT_SKIP ; + } + late.pid = tid; + late.tgid = pid; + late.kill =0 ; + late.go =0 ; + late.delay = len * i /12 ; +if (pthread_create (&t,NULL , late_killer,NULL )) { +reap (pid); +release_held (); +return KSFT_FAIL ; + } +sleep_ns (NSEC_PER_MSEC ); + late.go =1 ; + err =enter (root, pid); +join (t); +if (err) { +ksft_print_msg ("%s: the move refused: %s\n" , name,strerror (-err)); + ret =KSFT_FAIL ; + } +sleep_ns (2 *PERIOD ); +if (ret ==KSFT_PASS && + (admits_in (cg_p, base_p,"P, which the thread left" ) || +admits_in (root, free_r -3 ,"the root domain of the thread" ))) { +ksft_print_msg ("%s: try %d, the wakeup %llu us into a move of %llu us\n" , + name, i, (unsigned long long )(late.delay /NSEC_PER_USEC ), + (unsigned long long )(len /NSEC_PER_USEC )); + ret =KSFT_FAIL ; + } +reap (pid); +sleep_ns (3 *PERIOD ); + } +release_held (); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS && + (admits_in (cg_p, base_p,"P, at the end" ) || +admits_in (root, base_r,"the root domain of the rest, at the end" ))) + ret =KSFT_FAIL ; +return ret; +} + +static int move_woken (void ) +{ +return woken (0 ,"move_woken" ); +} + +static int move_woken_full (void ) +{ +return woken (1 ,"move_woken_full" ); +} +/* * Have the cpu controller refuse a move after cpuset_can_attach() * reserved room. cpuset_cancel_attach() must then give the room back.
@@ -2959,6 +4157,78 @@ static int move_cancelled(void)return ret; } +/* Return whether every task of @cg is frozen. */ +static int cg_frozen (const char *cg) +{ +char path[PATH_MAX +32 ], buf[256 ]; + +snprintf (path,sizeof (path),"%s/cgroup.events" , cg); +return !read_file (path, buf,sizeof (buf)) &&strstr (buf,"frozen 1" ); +} + +/* +* Freeze P with a running deadline task of 30% in it. Move the frozen +* task to the root cgroup, switch it to SCHED_OTHER, thaw P and kill the +* task. Do it three times. The free bandwidth of P and of the root +* domain of the other CPUs must then be what it was. +* +* A frozen task sleeps in its signal handling. The root cgroup is not +* frozen, so the move thaws the task. It is woken on P's CPU, before +* cpuset_attach() gives it the CPUs of the root cgroup. +*/ +static int frozen_moved_switched (void ) +{ +const char *what ="a deadline task to freeze" ; +int base_p, base_r, ret =KSFT_PASS , i; + + base_p =probe (cg_p); + base_r =probe (root); +for (i =0 ; i <3 && ret ==KSFT_PASS ; i++) { +pid_t pid; + + mv_state[0 ] =0 ; + pid =fork_child (what); +if (!pid) { +if (enter (cg_p,getpid ()) || +set_deadline (0 ,30 *NSEC_PER_MSEC ,PERIOD )) { + mv_state[0 ] =-1 ; +_exit (1 ); + } + mv_state[0 ] =1 ; +for (;;) +spin_ns (NSEC_PER_MSEC ); + } +if (pid <0 ||child_up (what,0 )) { +reap (pid); +return KSFT_FAIL ; + } +if (write_file (cg_p,"cgroup.freeze" ,"1" )) { +ksft_print_msg ("P cannot be frozen\n" ); +reap (pid); +return KSFT_SKIP ; + } +if (wait_for (2000 ,cg_frozen (cg_p))) { +ksft_print_msg ("P does not freeze\n" ); + ret =KSFT_FAIL ; + }else if (enter (root, pid)) { +ksft_print_msg ("the frozen task cannot leave P\n" ); + ret =KSFT_FAIL ; + }else if (set_other (pid)) { +ksft_print_msg ("the task cannot leave SCHED_DEADLINE\n" ); + ret =KSFT_FAIL ; + } +if (write_file (cg_p,"cgroup.freeze" ,"0" )) +ksft_print_msg ("P cannot be thawed\n" ); +reap (pid); + } +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS && + (admits_in (cg_p, base_p,"P, after the frozen tasks" ) || +admits_in (root, base_r,"the root domain of the rest" ))) + ret =KSFT_FAIL ; +return ret; +} +/* Deaths, in P */ enum die_mode {DIE_RUNNING ,DIE_SWITCHED_OUT ,DIE_SHRUNK };
@@ -3558,6 +4828,11 @@ static int yield_switched_out(void)return ret; } +static int yield_moved (void ) +{ +return moves (MV_YIELDED ,"yield_moved" ); +} +/* Kill a deadline task while sched_yield() throttles it. */ static int yield_killed (void ) {
@@ -3833,6 +5108,109 @@ static int reclaim_switched(void)return ret; } +/* +* Fork a reclaiming deadline task of 10%. It spins, and publishes in +* mv_state[1] the CPU time it has used, in milliseconds. It idles while +* mv_state[2] is set, and the moves happen then. A cpuset move waits for +* an RCU grace period. Where RCU runs in kthreads (PREEMPT_RT), a task +* that reclaims its whole CPU keeps that grace period from ending. Return +* the pid, or -1. +*/ +static pid_t reclaim_mover (void ) +{ +pid_t pid =fork_child ("a reclaiming task" ); + +if (!pid) { +if (set_deadline_flags (0 ,10 *NSEC_PER_MSEC ,PERIOD , +SCHED_FLAG_RECLAIM )) { + mv_state[0 ] =-1 ; +_exit (1 ); + } + mv_state[0 ] =1 ; +for (;;) { +if (mv_state[2 ]) { +sleep_ns (10 *NSEC_PER_MSEC ); +continue ; + } +spin_ns (NSEC_PER_MSEC ); + mv_state[1 ] = (int )(thread_time_ns () /NSEC_PER_MSEC ); + } + } +return pid; +} + +/* Idle the reclaiming task, then move it to @cg. Return 0, or -errno. */ +static int move_idle (const char *cg,pid_t pid) +{ +int err; + + mv_state[2 ] =1 ; +sleep_ns (20 *NSEC_PER_MSEC );/* it stops spinning */ + err =enter (cg, pid); + mv_state[2 ] =0 ; +return err; +} + +/* Return the CPU share, in percent, of the reclaiming task over @ns. */ +static int mover_share (uint64_t ns) +{ +uint64_t wall =now_ns (); +int cpu = mv_state[1 ]; + +sleep_ns (ns); + cpu = mv_state[1 ] - cpu; + wall =now_ns () - wall; +return (int )(100 * (uint64_t )cpu *NSEC_PER_MSEC / wall); +} + +/* +* Move a reclaiming task into P, back out, and into P again. It must +* reclaim as much of P's CPU as it did the first time. Once it is gone, +* the free bandwidth of both root domains must be what it was. +*/ +static int reclaim_moved (void ) +{ +int base_p, base_r, in_p, again =0 , ret =KSFT_PASS ; +pid_t pid; + + base_p =probe (cg_p); + base_r =probe (root); + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + mv_state[2 ] =1 ;/* idle until it is in P */ + pid =reclaim_mover (); +if (pid <0 ) +return KSFT_FAIL ; +if (child_up ("a reclaiming task" ,0 ) ||move_idle (cg_p, pid)) { + mv_state[2 ] =1 ; +reap (pid); +return KSFT_FAIL ; + } +sleep_ns (2 *PERIOD ); + in_p =mover_share (SHARE_NS ); +if (move_idle (root, pid) ||move_idle (cg_p, pid)) { +ksft_print_msg ("a move of a reclaiming task refused\n" ); + ret =KSFT_FAIL ; + }else { +sleep_ns (2 *PERIOD ); + again =mover_share (SHARE_NS ); +if (in_p <RECLAIM_MIN || again < in_p -SHARE_SLACK || + again > in_p +SHARE_SLACK ) { +ksft_print_msg ("reclaiming: %d%% of P's CPU, %d%% after a move\n" , + in_p, again); + ret =KSFT_FAIL ; + } + } + mv_state[2 ] =1 ; +reap (pid); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS && (probe (cg_p) != base_p ||probe (root) != base_r)) { +ksft_print_msg ("a reclaiming task moved: the free bandwidth changed\n" ); + ret =KSFT_FAIL ; + } +return ret; +} +/* Deadline servers */ #define SERVER_RUNTIME (100 * NSEC_PER_MSEC)/* the cases start at 10% */
@@ -4557,6 +5935,281 @@ static int params_waking(void)return ret; } +/* A random mix of the transitions above */ + +#define MIX_WORKERS 6 +#define MIX_NS (3 * NSEC_PER_SEC) +#define MIX_SEED 20261103/* fixed: a rerun starts with the same draws */ + +/* +* random_mix: six workers change their own state at random for 3s, and +* the case changes them from outside too. Q is a partition root of two +* CPUs, next to P. The case needs 4 housekeeping CPUs. +* +* Each worker does one of these, over and over: +* - become a deadline task, with random parameters and flags +* - switch to SCHED_OTHER +* - sleep +* - spin, at times past its runtime +* - call sched_yield() +* - wait up to 5ms for shm_lock and hold it 1ms. It is the mutex of the +* priority inheritance cases. A deadline waiter boosts its owner. +* - exit +* +* The case does one of these, over and over: +* - set the deadline parameters of a random worker +* - switch it to SCHED_OTHER +* - change its affinity +* - move it into P, into Q or into the root cgroup +* - kill it +* - change the fair server of P's CPU or of one CPU of Q +* It starts anew every worker it finds dead. +* +* The case does not check what a single transition does: the other cases +* do that. It checks the accounting afterwards. Once every worker is gone +* and its 0-lag time is past: +* - P, Q and the housekeeping root domain must each admit what they +* admitted before; +* - this_bw of P's CPU and of the two CPUs of Q must be what it was. +* An over- or underflow of running_bw or this_bw also shows as a warning +* in the kernel log, which the test does not read. +*/ +static atomic_int *mix_state;/* 0: stop, 1 + i: worker i's rounds */ + +static pid_t mix_worker (int idx,unsigned int seed) +{ +static const uint64_t runtime[] = {5 ,10 ,20 ,30 }; +static const uint64_t period[] = {50 ,100 }; +static const uint64_t flag[] = {0 ,SCHED_FLAG_RECLAIM ,SCHED_FLAG_DL_OVERRUN }; +pid_t pid =fork_child ("a worker of the mix" ); + +if (!pid) { +struct sigaction sa = { .sa_handler = catch_xcpu }; +struct timespec ts; +int err; + +if (sigaction (SIGXCPU , &sa,NULL )) +_exit (1 ); + seed += idx; +/* +* Ignore the errors. Each call below is made in whatever +* state the worker is in, and the kernel may refuse it. +*/ +while (!mix_state[0 ]) { +unsigned int r =rand_r (&seed), v = r /8 ; + +switch (r %8 ) { +case 0 : +set_deadline_flags (0 , runtime[v %4 ] *NSEC_PER_MSEC , + period[(v /4 ) %2 ] *NSEC_PER_MSEC , + flag[(v /8 ) %3 ]); +break ; +case 1 : +set_other (0 ); +break ; +case 2 : +sleep_ns ((v %20 +1 ) *NSEC_PER_MSEC ); +break ; +case 3 : +spin_ns ((v %15 +1 ) *NSEC_PER_MSEC ); +break ; +case 4 : +sched_yield (); +break ; +case 5 : +clock_gettime (CLOCK_REALTIME , &ts); + ts.tv_nsec +=5 *NSEC_PER_MSEC ; +if (ts.tv_nsec >=NSEC_PER_SEC ) { + ts.tv_nsec -=NSEC_PER_SEC ; + ts.tv_sec++; + } + err =pthread_mutex_timedlock (shm_lock, &ts); +if (err ==EOWNERDEAD )/* its owner died */ + err =pthread_mutex_consistent (shm_lock); +if (!err) { +spin_ns (NSEC_PER_MSEC ); +if (pthread_mutex_unlock (shm_lock)) +_exit (1 ); + } +break ; +case 6 : +_exit (0 );/* the case starts another */ +default : +sleep_ns (NSEC_PER_MSEC /2 ); +break ; + } + mix_state[1 + idx]++; + } +_exit (0 ); + } +return pid; +} + +static cpu_set_t mix_mask[4 ]; + +static int random_mix (void ) +{ +static const uint64_t runtime[] = {5 ,10 ,20 ,30 }; +int cpu_a =top_cpu (3 ), cpu_b =top_cpu (2 ), cpu_p =top_cpu (1 ); +int base_p, base_q, base_r, i, ret =KSFT_PASS , rounds =0 , sums; +long ta =0 , ra, tb =0 , rb, tp =0 , rp, ta2, ra2, tb2, rb2, tp2, rp2; +pid_t pid[MIX_WORKERS ] = {-1 ,-1 ,-1 ,-1 ,-1 ,-1 }; +unsigned int seed =MIX_SEED ; +uint64_t end; +char cpus[32 ]; + +if (ncpus <4 ) { +ksft_print_msg ("needs 4 non-isolated CPUs\n" ); +return KSFT_SKIP ; + } +snprintf (cpus,sizeof (cpus),"%d,%d" , cpu_a, cpu_b); +if (make_partition_cpus (cg_q, cpus)) { +remove_partition (cg_q); +ksft_print_msg ("no partition root of two CPUs\n" ); +return KSFT_SKIP ; + } +/* +* The affinities of the mix: every CPU of the test, those of Q and +* P, those of P and the rest, and all but the first. +*/ +for (i =0 ; i <4 ; i++) +CPU_ZERO (&mix_mask[i]); +for (i =0 ; i < ncpus; i++) { +int c = online[i]; + +CPU_SET (c, &mix_mask[0 ]); +if (c == cpu_a || c == cpu_b || c == cpu_p) +CPU_SET (c, &mix_mask[1 ]); +if (c != cpu_a && c != cpu_b) +CPU_SET (c, &mix_mask[2 ]); +if (i) +CPU_SET (c, &mix_mask[3 ]); + } +if (shm_lock_init ()) { +remove_partition (cg_q); +return KSFT_SKIP ;/* no shared mutex for the workers */ + } +/* Without this_bw in sched_debug, check the admissions only. */ + sums = !rq_bw_of (cpu_a, &ta, &ra) && !rq_bw_of (cpu_b, &tb, &rb) && + !rq_bw_of (cpu_p, &tp, &rp); + base_p =probe (cg_p); + base_q =probe (cg_q); + base_r =probe (root); +if (base_p <=0 || base_q <=0 || base_r <=0 ) { +remove_partition (cg_q); +ksft_print_msg ("no room to probe P, Q or the rest\n" ); +return KSFT_SKIP ; + } + mix_state = mv_state +16 ; +for (i =0 ; i <=MIX_WORKERS ; i++) + mix_state[i] =0 ; +for (i =0 ; i <MIX_WORKERS ; i++) { + pid[i] =mix_worker (i, seed); +if (pid[i] <0 ) { + ret =KSFT_FAIL ; + goto out; + } + } + + end =now_ns () +MIX_NS ; +while (now_ns () < end) { +unsigned int r =rand_r (&seed); +int w = r %MIX_WORKERS ; + +switch ((r /MIX_WORKERS ) %8 ) { +case 0 : +set_deadline (pid[w], runtime[r %4 ] *NSEC_PER_MSEC ,PERIOD ); +break ; +case 7 : +/* +* Refused with EBUSY if the mask leaves out a CPU +* of the root domain that counts a deadline task. +*/ +sched_setaffinity (pid[w],sizeof (mix_mask[0 ]), &mix_mask[r %4 ]); +break ; +case 1 : +set_other (pid[w]); +break ; +case 2 : +enter (cg_p, pid[w]); +break ; +case 3 : +enter (cg_q, pid[w]); +break ; +case 4 : +enter (root, pid[w]); +break ; +case 5 : +/* An exited worker is a zombie: the kill succeeds. */ +if (kill (pid[w],SIGKILL ) && errno !=ESRCH ) +ksft_print_msg ("kill %d: %s\n" , pid[w],strerror (errno)); +break ; +default : +server_set (r %2 ? cpu_p : cpu_a, + (r %3 +1 ) *SERVER_RUNTIME ,SERVER_PERIOD ); +break ; + } +sleep_ns ((r %2000 +100 ) *NSEC_PER_USEC ); +/* Start anew each worker that exited or was killed. */ +for (i =0 ; i <MIX_WORKERS ; i++) { +pid_t done =waitpid (pid[i],NULL ,WNOHANG ); + +if (done <0 && errno !=EINTR ) { +ksft_print_msg ("waitpid %d: %s\n" , pid[i],strerror (errno)); + ret =KSFT_FAIL ; + goto out; + } +if (done == pid[i]) { + pid[i] =mix_worker (i, seed + rounds); +if (pid[i] <0 ) { + ret =KSFT_FAIL ; + goto out; + } + rounds++; + } + } + } +out: + mix_state[0 ] =1 ; +for (i =0 ; i <MIX_WORKERS ; i++) +reap (pid[i]); +/* +* Wait for the 0-lag times of the workers. Putting the fair servers +* back needs the room they held. +*/ +sleep_ns (5 *PERIOD ); +server_restore (); +if (ret !=KSFT_PASS ) { +remove_partition (cg_q); +return ret; + } +for (i =0 , rounds =0 ; i <MIX_WORKERS ; i++) + rounds += mix_state[1 + i]; +if (!rounds) { +ksft_print_msg ("the workers of the mix took no transition\n" ); + ret =KSFT_FAIL ; + } +if (ret ==KSFT_PASS && sums) { +if (rq_bw_of (cpu_a, &ta2, &ra2) ||rq_bw_of (cpu_b, &tb2, &rb2) || +rq_bw_of (cpu_p, &tp2, &rp2)) { + ret =KSFT_FAIL ; + }else if (ta2 != ta || tb2 != tb || tp2 != tp) { +ksft_print_msg ("this_bw %ld %ld %ld, was %ld %ld %ld\n" , + ta2, tb2, tp2, ta, tb, tp); + ret =KSFT_FAIL ; + } + } +if (ret ==KSFT_PASS && + (admits_in (cg_p, base_p,"P after the mix" ) || +admits_in (cg_q, base_q,"Q after the mix" ) || +admits_in (root, base_r,"the root domain of the rest after the mix" ))) + ret =KSFT_FAIL ; +if (ret !=KSFT_PASS ) +ksft_print_msg ("seed %u, %d transitions\n" ,MIX_SEED , rounds); +remove_partition (cg_q); +return ret; +} +/* * CPU hotplug, from the main process, with no P. Two cases below run with * P instead: death_zombie_hotplug and pi_departed_moved.
@@ -5223,6 +6876,834 @@ static int in_cpu_list(const char *list, int cpu)return 0 ; } +/* +* Wake @pid, a task that sleeps in read() on a pipe: write a byte to @fd. +* The task counts its wakeups in mv_state[1]. Wait until it has run and +* sleeps again. Return 0, or -1. +*/ +static int fallback_wake (int fd,pid_t pid) +{ +int n = mv_state[1 ]; + +if (write (fd,"w" ,1 ) !=1 ) { +ksft_print_msg ("write to the deadline task: %s\n" ,strerror (errno)); +return -1 ; + } +if (wait_for (1000 , mv_state[1 ] != n) || +wait_for (1000 ,proc_state_of (pid) =='S' )) { +ksft_print_msg ("the deadline task did not wake up and sleep again\n" ); +return -1 ; + } +return 0 ; +} + +/* +* The cpusets of the cases below: B and Q in the root cgroup, C in B. A +* case makes those it needs. cs_setup() names them and enables the cpuset +* controller of the root cgroup if it is off. cs_cleanup() removes them, +* and turns the controller off again if cs_setup() turned it on. +*/ +static char cs_b[PATH_MAX ], cs_c[PATH_MAX ], cs_q[PATH_MAX ]; +static int cs_added; + +static int cs_setup (void ) +{ +char path[PATH_MAX +32 ], buf[256 ]; + + cs_added =0 ; +if (!root[0 ] || +snprintf (cs_b,sizeof (cs_b),"%s/deadline_bw_b" , root) >= (int )sizeof (cs_b) || +snprintf (cs_c,sizeof (cs_c),"%s/c" , cs_b) >= (int )sizeof (cs_c) || +snprintf (cs_q,sizeof (cs_q),"%s/deadline_bw_q" , root) >= (int )sizeof (cs_q)) +return -1 ; +snprintf (path,sizeof (path),"%s/cgroup.subtree_control" , root); +if (read_file (path, buf,sizeof (buf))) +return -1 ; +if (!strstr (buf,"cpuset" )) { +if (write_file (root,"cgroup.subtree_control" ,"+cpuset" )) +return -1 ; + cs_added =1 ; + } +return 0 ; +} + +/* Make the cpuset @cg, and give it the CPUs of @cpus. Return 0, or -1. */ +static int cs_make (const char *cg,const char *cpus) +{ +int err =0 ; + +if (mkdir (cg,0755 ) && errno !=EEXIST ) + err = -errno; +if (!err) + err =write_file (cg,"cpuset.cpus" , cpus); +if (err) +ksft_print_msg ("cpuset %s of CPUs %s: %s\n" , cg, cpus,strerror (-err)); +return err ?-1 :0 ; +} + +static void cs_cleanup (void ) +{ +remove_partition (cs_c); +remove_partition (cs_b); +remove_partition (cs_q); +if (cs_added &&write_file (root,"cgroup.subtree_control" ,"-cpuset" )) +ksft_print_msg ("the cpuset controller stays enabled\n" ); +} + +/* +* Return whether the partition file of @cg holds @what. If not, and +* @tell, print what it holds. +*/ +static int partition_has (const char *cg,const char *what,int tell) +{ +char path[PATH_MAX +32 ], buf[256 ]; + +snprintf (path,sizeof (path),"%s/cpuset.cpus.partition" , cg); +if (read_file (path, buf,sizeof (buf))) +return 0 ; +if (strstr (buf, what)) +return 1 ; +if (tell) +ksft_print_msg ("%s/cpuset.cpus.partition: %s" , cg, buf); +return 0 ; +} + +/* +* Read total_bw of the root domain of @d into @def, and of the root +* domain of @h into @hk. Return 0, or -1. +*/ +static int totals_of (int d,int h,long long *def,long long *hk) +{ + *def =total_bw_of (d); + *hk =total_bw_of (h); +if (*def ==-1 || *hk ==-1 ) { +ksft_print_msg ("no total_bw in /sys/kernel/debug/sched/debug\n" ); +return -1 ; + } +return 0 ; +} + +/* +* Return KSFT_PASS if total_bw of the root domains of @d and @h is +* @def and @hk, else KSFT_FAIL. @what names the step in the message. +*/ +static int totals_check (const char *what,int d,int h,long long def,long long hk) +{ +long long now_def, now_hk; + +if (totals_of (d, h, &now_def, &now_hk)) +return KSFT_FAIL ; +if (now_def == def && now_hk == hk) +return KSFT_PASS ; +ksft_print_msg ("%s: total_bw %lld, %lld, not %lld, %lld\n" , + what, now_def, now_hk, def, hk); +return KSFT_FAIL ; +} + +/* +* Fork a task that runs on @cpu. Then it allows every CPU if @all. If +* not, it allows the housekeeping CPUs that were online at the start, +* even those offline now. It becomes a deadline task of @runtime every +* @period. If @fd is -1, it spins and counts in mv_state[1]. Else it +* sleeps in read() on @fd and counts in mv_state[1] each byte that wakes +* it. On the byte 'x', it spins 90ms and exits. It sets mv_state[0] as +* mover() does. Return its pid, or -1. +*/ +static pid_t task_from (int cpu,int all,uint64_t runtime,uint64_t period,int fd) +{ +pid_t pid =fork_child ("a deadline task" ); + +if (!pid) { +cpu_set_t set, one; +int i; +char c; + +CPU_ZERO (&one); +CPU_SET (cpu, &one); +if (sched_setaffinity (0 ,sizeof (one), &one)) +_exit (1 ); +spin_ns (NSEC_PER_MSEC ); +CPU_ZERO (&set); +for (i =0 ; i < (all ?CPU_SETSIZE : ncpus); i++) +CPU_SET (all ? i : online[i], &set); +if (sched_setaffinity (0 ,sizeof (set), &set) || +set_deadline (0 , runtime, period)) { + mv_state[0 ] =-1 ; +_exit (1 ); + } + mv_state[0 ] =1 ; +while (fd <0 ) + mv_state[1 ]++; +while (read (fd, &c,1 ) ==1 ) { +if (c =='x' ) { +spin_ns (90 *NSEC_PER_MSEC ); +_exit (0 ); + } + mv_state[1 ]++; + } +_exit (0 ); + } +return pid; +} + +/* +* The default root domain spans every CPU that is offline. It also spans +* the CPU of an isolated partition. The steps: +* 1. Take hp_cpu offline. Make Q an isolated partition of Y, another +* housekeeping CPU. +* 2. A task in the root cgroup becomes a deadline task of 10% and sleeps. +* The housekeeping root domain counts it. Its affinity holds the +* housekeeping CPUs, hp_cpu too. +* 3. Move it asleep into Q. Wake it: it runs on Y. The default root +* domain counts it. +* 4. Move it asleep back to the root cgroup. Its mask holds hp_cpu, but +* no active CPU of the default root domain. +* 5. Wake it. It runs on a housekeeping CPU. +* Check after step 4 and again after step 5. The housekeeping root +* domain must count the task. The default root domain must not. An +* offline CPU of the default root domain is no CPU where the task can +* stay. +* +* The references of total_bw are taken after step 1. Once the task is +* gone, total_bw must be back to them. +*/ +static int move_back_cpu_offline (void ) +{ +int y = hp_cpu ==top_cpu (1 ) ?top_cpu (2 ) :top_cpu (1 ), h = online[0 ]; +int fd[2 ] = {-1 ,-1 }, cpu, err, ret =KSFT_SKIP ; +long long def0, hk0, def, hk, bw; +char buf[16 ]; +pid_t pid =-1 ; + +if (cs_setup ()) +return KSFT_SKIP ; +if (cpu_online (hp_cpu,0 )) { +ksft_print_msg ("CPU %d cannot go offline\n" , hp_cpu); + goto out; + } +snprintf (buf,sizeof (buf),"%d" , y); +if (cs_make (cs_q, buf) ||write_file (cs_q,"cpuset.cpus.partition" ,"isolated" ) || + !partition_has (cs_q,"isolated" ,1 ) ||partition_has (cs_q,"invalid" ,0 )) { +ksft_print_msg ("no isolated partition Q of CPU %d\n" , y); + goto out; + } +if (totals_of (y, h, &def0, &hk0) ||pipe (fd)) + goto out; + ret =KSFT_FAIL ; + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =task_from (h,0 ,10 *NSEC_PER_MSEC ,PERIOD , fd[0 ]); +if (pid <0 ||child_up ("move_back_cpu_offline" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' ) ||totals_of (y, h, &def, &hk)) + goto out; + bw = hk - hk0; +if (bw <=0 || def != def0) { +ksft_print_msg ("the deadline task adds %lld, %lld to total_bw\n" , + def - def0, bw); + goto out; + } + err =enter (cs_q, pid); +if (err) { +ksft_print_msg ("a move into Q refused: %s\n" ,strerror (-err)); + goto out; + } +if (fallback_wake (fd[1 ], pid) || +totals_check ("in Q" , y, h, def0 + bw, hk0)) + goto out; + cpu =task_cpu_of (pid); +if (cpu != y) { +ksft_print_msg ("woken in Q on CPU %d, not %d\n" , cpu, y); + goto out; + } + err =enter (root, pid); +if (err) { +ksft_print_msg ("the move back refused: %s\n" ,strerror (-err)); + goto out; + } +if (totals_check ("moved back asleep" , y, h, def0, hk0 + bw) || +fallback_wake (fd[1 ], pid) || +totals_check ("woken" , y, h, def0, hk0 + bw)) + goto out; + cpu =task_cpu_of (pid); +if (cpu == y ||in_cpu_list (isolated, cpu)) { +ksft_print_msg ("woken on CPU %d\n" , cpu); + goto out; + } +reap (pid); + pid =-1 ; +sleep_ns (3 *PERIOD ); + ret =totals_check ("task gone" , y, h, def0, hk0); +out: +reap (pid); +if (fd[0 ] >=0 ) { +close (fd[0 ]); +close (fd[1 ]); + } +cs_cleanup (); +cpu_up (hp_cpu); +return ret; +} + +/* Moves between two root domains, from the main process, with P */ + +/* +* Fork a deadline task of 30ms every 100ms, pinned to the @n CPUs of +* @cpus. It runs 1ms, then sleeps. If @fd is not -1, each byte read from +* @fd wakes it up, and it counts the wakeups in mv_state[1]. It sets +* mv_state[0] as mover() does. Return its pid, or -1. +*/ +static pid_t pinned_sleeper (const int *cpus,int n,int fd) +{ +pid_t pid =fork_child ("a pinned deadline task" ); + +if (!pid) { +cpu_set_t set; +char c; +int i; + +CPU_ZERO (&set); +for (i =0 ; i < n; i++) +CPU_SET (cpus[i], &set); +if (sched_setaffinity (0 ,sizeof (set), &set) || +set_deadline (0 ,30 *NSEC_PER_MSEC ,PERIOD )) { + mv_state[0 ] =-1 ; +_exit (1 ); + } +spin_ns (NSEC_PER_MSEC ); + mv_state[0 ] =1 ; +while (fd >=0 &&read (fd, &c,1 ) ==1 ) + mv_state[1 ]++; +for (;;) +pause (); + } +return pid; +} + +/* +* Read total_bw of three root domains into @bw: the root domain of the +* isolated CPUs, the housekeeping root domain, and P. Return 0, or -1. +*/ +static int split_totals (long long bw[3 ]) +{ + bw[0 ] =total_bw_of (isol[0 ]); + bw[1 ] =total_bw_of (online[0 ]); + bw[2 ] =total_bw_of (top_cpu (1 )); +if (bw[0 ] ==-1 || bw[1 ] ==-1 || bw[2 ] ==-1 ) { +ksft_print_msg ("no total_bw in /sys/kernel/debug/sched/debug\n" ); +return -1 ; + } +return 0 ; +} + +/* +* Return KSFT_PASS if total_bw is now @want, else KSFT_FAIL. The message +* gives total_bw of the root domain of the isolated CPUs, then of the +* housekeeping root domain, then of P. +*/ +static int split_check (const char *what,const long long want[3 ]) +{ +long long bw[3 ]; + +if (split_totals (bw)) +return KSFT_FAIL ; +if (bw[0 ] == want[0 ] && bw[1 ] == want[1 ] && bw[2 ] == want[2 ]) +return KSFT_PASS ; +ksft_print_msg ("%s: total_bw %lld, %lld, %lld, not %lld, %lld, %lld\n" , + what, bw[0 ], bw[1 ], bw[2 ], want[0 ], want[1 ], want[2 ]); +return KSFT_FAIL ; +} + +/* +* Read total_bw into @base, for the cases below. Return 0, or nonzero if +* the case must skip. It needs CPUs isolated by isolcpus="domain". +*/ +static int split_skip (long long base[3 ]) +{ +if (!nisol) { +ksft_print_msg ("needs CPUs isolated by isolcpus=domain\n" ); +return 1 ; + } +return split_totals (base); +} + +/* +* The root cpuset holds the CPUs that isolcpus="domain" isolates, so it +* spans two root domains. The affinity of a task can keep it in either. +* A move into the root cpuset must reserve the bandwidth of the task in +* the root domain that the task gets. +* +* Run twice, with a deadline sleeper of 30% pinned to the isolated CPUs, +* then with another one pinned to the housekeeping CPUs except P's. Move +* the sleeper into P and back to the root cgroup, then kill it. Once it +* is gone, total_bw of the three root domains must be what it was. +*/ +static int move_isolated (void ) +{ +long long base[3 ]; +int round, err; +pid_t pid; + +if (split_skip (base)) +return KSFT_SKIP ; +for (round =0 ; round <2 ; round++) { +const char *what = round ?"pinned to the other CPUs" : +"pinned to the isolated CPUs" ; + + mv_state[0 ] =0 ; + pid = round ?pinned_sleeper (online, ncpus -1 ,-1 ) : +pinned_sleeper (isol, nisol,-1 ); +if (pid <0 ||child_up (what,0 )) { +reap (pid); +return KSFT_FAIL ; + } + err =enter (cg_p, pid); +if (!err) + err =enter (root, pid); +reap (pid); +if (err) { +ksft_print_msg ("%s: a move refused: %s\n" , what,strerror (-err)); +return KSFT_FAIL ; + } +sleep_ns (3 *PERIOD ); +if (split_check (what, base)) +return KSFT_FAIL ; + } +return KSFT_PASS ; +} + +/* +* Fork a process pinned to the isolated CPUs. Its threads become deadline +* tasks of 10ms every 100ms, as in probe(), until one is refused. Then it +* sets mv_state[1] to 1, or to -1 on an error. The threads sleep until the +* process is killed. +*/ +static pid_t fill_isolated (void ) +{ +pid_t pid =fork_child ("the tasks that fill the isolated CPUs" ); + +if (!pid) { +int i, max =16 * nisol +16 ; +struct probe_slot *s =calloc (max,sizeof (*s)); +pthread_attr_t a; +cpu_set_t set; + +CPU_ZERO (&set); +for (i =0 ; i < nisol; i++) +CPU_SET (isol[i], &set); +if (!s ||sched_setaffinity (0 ,sizeof (set), &set) || +sem_init (&probe_done,0 ,0 ) ||pipe (probe_hold) || +pthread_attr_init (&a) ||pthread_attr_setstacksize (&a,THREAD_STACK )) { + mv_state[1 ] =-1 ; +_exit (1 ); + } +for (i =0 ; i < max; i++) { +if (pthread_create (&s[i].t, &a, probe_thread, &s[i]) || +sem_wait_for (&probe_done,10000 )) { + mv_state[1 ] =-1 ; +_exit (1 ); + } +if (s[i].err) +break ; + } + mv_state[1 ] = i < max && s[i].err == -EBUSY ?1 :-1 ; +for (;;) +pause (); + } +return pid; +} + +/* +* Move a deadline sleeper pinned to the isolated CPUs into P. Fill the +* isolated CPUs with deadline tasks of 10%, until one is refused. Moving +* the sleeper back must then be refused with EBUSY. +*/ +static int move_isolated_refused (void ) +{ +pid_t pid, fill =-1 ; +long long base[3 ]; +int err, ret =KSFT_FAIL ; + +if (split_skip (base)) +return KSFT_SKIP ; + mv_state[0 ] =0 ; + pid =pinned_sleeper (isol, nisol,-1 ); +if (pid <0 ||child_up ("a sleeper pinned to the isolated CPUs" ,0 )) + goto out; + err =enter (cg_p, pid); +if (err) { +ksft_print_msg ("a move into P refused: %s\n" ,strerror (-err)); + goto out; + } + mv_state[1 ] =0 ; + fill =fill_isolated (); +if (fill <0 ||wait_for (10000 , mv_state[1 ]) || mv_state[1 ] !=1 ) { +ksft_print_msg ("the isolated CPUs cannot be filled\n" ); + goto out; + } + err =enter (root, pid); +if (err == -EBUSY ) { + ret =KSFT_PASS ; + }else { +ksft_print_msg ("a move back to the full isolated CPUs: %s, not EBUSY\n" , + err ?strerror (-err) :"accepted" ); + } +out: +reap (fill); +reap (pid); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =split_check ("the isolated CPUs filled" , base); +return ret; +} + +/* +* A deadline sleeper is pinned to the isolated CPUs. The case allows it +* all CPUs, then moves it into P and back, without waking it. So it stays +* on an isolated CPU, which its mask holds again. The root domain of the +* isolated CPUs must still count its bandwidth. Then wake it: it must run +* on an isolated CPU, and that root domain must still count it. +*/ +static int move_isolated_asleep (void ) +{ +long long base[3 ], want[3 ]; +int fd[2 ], err, cpu, i, ret =KSFT_FAIL ; +cpu_set_t set; +pid_t pid; + +if (split_skip (base)) +return KSFT_SKIP ; +if (pipe (fd)) { +ksft_print_msg ("pipe: %s\n" ,strerror (errno)); +return KSFT_FAIL ; + } + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =pinned_sleeper (isol, nisol, fd[0 ]); +if (pid <0 ||child_up ("a sleeper pinned to the isolated CPUs" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' ) ||split_totals (want)) + goto out; +CPU_ZERO (&set); +for (i =0 ; i <CPU_SETSIZE ; i++) +CPU_SET (i, &set); + err =sched_setaffinity (pid,sizeof (set), &set) ? -errno :0 ; +if (!err) + err =enter (cg_p, pid); +if (!err) + err =enter (root, pid); +if (err) { +ksft_print_msg ("the affinity of all CPUs, or a move: %s\n" ,strerror (-err)); + goto out; + } +for (i =0 ; i <2 ; i++) { +const char *what = i ?"woken" :"moved back asleep" ; + + cpu =task_cpu_of (pid); +if (!in_cpu_list (isolated, cpu)) { +ksft_print_msg ("%s on CPU %d, not an isolated one\n" , what, cpu); + goto out; + } +if (split_check (what, want)) + goto out; +if (!i &&fallback_wake (fd[1 ], pid)) + goto out; + } + ret =KSFT_PASS ; +out: +reap (pid); +close (fd[0 ]); +close (fd[1 ]); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =split_check ("task gone" , base); +return ret; +} + +/* The CPUs of move_spanning(): two root domains, an isolated CPU first */ +static cpu_set_t span_set; + +/* +* Fork a task that pins itself to the isolated CPUs, becomes a deadline +* task of 30ms every 100ms, then takes the CPUs of span_set. If @fd is +* -1, it spins and counts in mv_state[1]. Else it sleeps, each byte read +* from @fd wakes it up, and it counts the wakeups there. It sets +* mv_state[0] as mover() does. Return its pid, or -1. +*/ +static pid_t spanning_task (int fd) +{ +pid_t pid =fork_child ("a deadline task on two root domains" ); + +if (!pid) { +cpu_set_t set; +char c; +int i; + +CPU_ZERO (&set); +for (i =0 ; i < nisol; i++) +CPU_SET (isol[i], &set); +if (sched_setaffinity (0 ,sizeof (set), &set) || +set_deadline (0 ,30 *NSEC_PER_MSEC ,PERIOD ) || +sched_setaffinity (0 ,sizeof (span_set), &span_set)) { + mv_state[0 ] =-1 ; +_exit (1 ); + } + mv_state[0 ] =1 ; +while (fd <0 ) + mv_state[1 ]++; +while (read (fd, &c,1 ) ==1 ) + mv_state[1 ]++; +for (;;) +pause (); + } +return pid; +} + +/* +* A task becomes a deadline task on the isolated CPUs, where it is +* counted. Then it takes a wider affinity: the isolated CPUs, and the +* housekeeping CPUs above the first isolated one except P's. In the root +* cpuset, its CPUs span two root domains, and the lowest one is isolated. +* The case moves it into P, where it runs, and back to the root cgroup. +* The task must then run on a housekeeping CPU, and the housekeeping root +* domain must count its bandwidth. The root domain of the isolated CPUs +* must count it no more. Run twice: with a running task, then with a +* sleeper woken in P and again after the move back. +* +* The kernel counts a task that has a housekeeping CPU in the +* housekeeping root domain. The move reserves there, and a rebuild of the +* accounting makes the same choice. The lowest CPU of the task is +* isolated, so a move that takes the first CPU of the mask reserves in +* the wrong root domain. +*/ +static int move_spanning (void ) +{ +int fd[2 ] = {-1 ,-1 }, round, cpu, err, i, n =0 , ret =KSFT_PASS ; +long long base[3 ], want[3 ]; +pid_t pid; + +if (split_skip (base)) +return KSFT_SKIP ; +CPU_ZERO (&span_set); +for (i =0 ; i < nisol; i++) +CPU_SET (isol[i], &span_set); +for (i =0 ; i < ncpus -1 ; i++) { +if (online[i] > isol[0 ]) { +CPU_SET (online[i], &span_set); + n++; + } + } +if (!n) { +ksft_print_msg ("needs a CPU above the first isolated one, besides P\n" ); +return KSFT_SKIP ; + } +for (round =0 ; round <2 && ret ==KSFT_PASS ; round++) { +const char *what = round ?"a sleeper" :"a running task" ; + + ret =KSFT_FAIL ; +if (round &&pipe (fd)) { +ksft_print_msg ("pipe: %s\n" ,strerror (errno)); +break ; + } + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =spanning_task (fd[0 ]); +if (pid <0 ||child_up (what,0 ) ||split_totals (want)) + goto next; +/* +* Moved back, its bandwidth goes to the housekeeping root +* domain, not to the root domain of the isolated CPUs. +*/ + want[1 ] += want[0 ] - base[0 ]; + want[0 ] = base[0 ]; + err =enter (cg_p, pid); +if (err) { +ksft_print_msg ("%s: a move into P refused: %s\n" , what,strerror (-err)); + goto next; + } +if ((round &&fallback_wake (fd[1 ], pid)) || +wait_for (1000 ,task_cpu_of (pid) ==top_cpu (1 ))) { +ksft_print_msg ("%s: not on P's CPU\n" , what); + goto next; + } + err =enter (root, pid); +if (err) { +ksft_print_msg ("%s: the move back refused: %s\n" , what,strerror (-err)); + goto next; + } +if (round ?fallback_wake (fd[1 ], pid) : +wait_for (1000 ,task_cpu_of (pid) !=top_cpu (1 ))) + goto next; +sleep_ns (PERIOD ); + cpu =task_cpu_of (pid); +if (in_cpu_list (isolated, cpu)) { +ksft_print_msg ("%s: moved back to isolated CPU %d\n" , what, cpu); + goto next; + } + ret =split_check (what, want); +next: +reap (pid); +if (round) { +close (fd[0 ]); +close (fd[1 ]); + } +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =split_check ("task gone" , base); + } +return ret; +} + +/* +* A deadline task moved asleep into another cpuset still sleeps on a CPU +* of the root domain it left. The root domain it moved to counts its +* bandwidth. sched_setaffinity() must check a new mask against the root +* domain that counts the task, not the one it sleeps in. +* +* H is a cpuset here, not the CPU of fallback_case(). It holds the +* housekeeping CPUs except P's. The steps: +* 1. Pin a fair task to the isolated CPUs. +* 2. Move it into H. Its affinity has no CPU of H, so it gets all the +* CPUs of H. +* 3. Make it a deadline task there, and let it sleep. +* 4. Move it asleep into the root cgroup. Its affinity gives it the +* isolated CPUs again, and their root domain counts it. +* 5. Set its affinity to the CPUs of H. That would take it out of the +* root domain that counts it, so it must fail with EBUSY. +* +* If the change is accepted, wake the task and report where it runs. +*/ +static int affinity_moved_asleep (void ) +{ +char cg_h[PATH_MAX ], cpus[4096 ], what[64 ]; +int fd[2 ], i, n =0 , cpu, err, made =0 , ret =KSFT_SKIP ; +long long base[3 ], want[3 ], bw; +cpu_set_t set; +pid_t pid =-1 ; + +if (split_skip (base)) +return KSFT_SKIP ; +if (pipe (fd)) { +ksft_print_msg ("pipe: %s\n" ,strerror (errno)); +return KSFT_FAIL ; + } +/* +* H holds the housekeeping CPUs except P's. A task there can become +* a deadline task: its mask holds its whole root domain. +*/ +CPU_ZERO (&set); +for (i =0 ; i < ncpus -1 && n < (int )sizeof (cpus); i++) { + n +=snprintf (cpus + n,sizeof (cpus) - n,"%s%d" , i ?"," :"" , online[i]); +CPU_SET (online[i], &set); + } +if (snprintf (cg_h,sizeof (cg_h),"%s/deadline_bw_h" , root) >= (int )sizeof (cg_h)) + goto out; +if (mkdir (cg_h,0755 ) && errno !=EEXIST ) { +ksft_print_msg ("mkdir %s: %s\n" , cg_h,strerror (errno)); + goto out; + } + made =1 ; +if (n >= (int )sizeof (cpus) ||write_file (cg_h,"cpuset.cpus" , cpus)) { +ksft_print_msg ("cpuset H cannot be made\n" ); + goto out; + } + ret =KSFT_FAIL ; + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =fork_child ("a task moved asleep" ); +if (!pid) { +cpu_set_t iso; +char c; + +CPU_ZERO (&iso); +for (i =0 ; i < nisol; i++) +CPU_SET (isol[i], &iso); +if (sched_setaffinity (0 ,sizeof (iso), &iso)) { + mv_state[1 ] =-1 ; +_exit (1 ); + } + mv_state[1 ] =1 ; +/* Wait for the move into H. Become a deadline task there. */ +if (read (fd[0 ], &c,1 ) !=1 ||set_deadline (0 ,30 *NSEC_PER_MSEC ,PERIOD )) { + mv_state[0 ] =-1 ; +_exit (1 ); + } +spin_ns (NSEC_PER_MSEC ); + mv_state[0 ] =1 ; +while (read (fd[0 ], &c,1 ) ==1 ) + mv_state[1 ]++; +for (;;) +pause (); + } +if (pid <0 ) + goto out; +if (wait_for (1000 , mv_state[1 ]) || mv_state[1 ] !=1 ) { +ksft_print_msg ("the task cannot take the isolated CPUs\n" ); + goto out; + } + err =enter (cg_h, pid); +if (err) { +ksft_print_msg ("a move into H refused: %s\n" ,strerror (-err)); + goto out; + } +if (write (fd[1 ],"d" ,1 ) !=1 ) { +ksft_print_msg ("write to the task: %s\n" ,strerror (errno)); + goto out; + } +if (child_up ("a task moved into H" ,0 ) ||wait_for (1000 ,proc_state_of (pid) =='S' )) + goto out; + bw = (30 *NSEC_PER_MSEC <<20 ) /PERIOD ;/* as to_ratio() */ +memcpy (want, base,sizeof (want)); + want[1 ] += bw; +if (split_check ("a deadline task in H" , want)) + goto out; + +/* +* Moved asleep, it stays on its CPU. The root domain of the +* isolated CPUs counts it. +*/ + err =enter (root, pid); +if (err) { +ksft_print_msg ("the move into the root cgroup refused: %s\n" ,strerror (-err)); + goto out; + } + cpu =task_cpu_of (pid); +if (in_cpu_list (isolated, cpu)) { +ksft_print_msg ("moved asleep on CPU %d, an isolated one\n" , cpu); + goto out; + } + want[0 ] += bw; + want[1 ] -= bw; +if (split_check ("moved asleep" , want)) + goto out; + +/* The CPUs of H leave out the root domain that counts the task. */ + err =sched_setaffinity (pid,sizeof (set), &set) ? -errno :0 ; +if (err == -EBUSY ) { + ret =KSFT_PASS ; + goto out; + } +ksft_print_msg ("the affinity of the CPUs of H: %s, not EBUSY\n" , + err ?strerror (-err) :"accepted" ); +if (err ||fallback_wake (fd[1 ], pid)) + goto out; +/* Report its CPU, and whether the root domain of that CPU counts it. */ + cpu =task_cpu_of (pid); +memcpy (want, base,sizeof (want)); + want[in_cpu_list (isolated, cpu) ?0 :1 ] += bw; +snprintf (what,sizeof (what),"woken on CPU %d" , cpu); +if (split_check (what, want) ==KSFT_PASS ) +ksft_print_msg ("%s: its root domain counts it\n" , what); +out: +reap (pid); +close (fd[0 ]); +close (fd[1 ]); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =split_check ("task gone" , base); +if (made &&wait_for (1000 , !rmdir (cg_h) || errno ==ENOENT )) +ksft_print_msg ("rmdir %s: %s\n" , cg_h,strerror (errno)); +return ret; +} +/* Setup */ static int find_cgroup2 (void )
@@ -5413,9 +7894,28 @@ static const struct p_case { { "return_asleep_new_params", return_asleep_new_params, MEASURED_IN_P }, { "deboost_blocked_keeps_order", deboost_blocked_keeps_order, MEASURED_IN_P }, { "pi_chain_donor_leaves", pi_chain_donor_leaves, MEASURED_IN_P }, + { "move_asleep", move_asleep, FROM_OUTSIDE }, + { "move_throttled", move_throttled, FROM_OUTSIDE }, + { "move_non_contending", move_non_contending, FROM_OUTSIDE }, { "move_refused", move_refused, FROM_OUTSIDE }, { "move_zero_bw", move_zero_bw, FROM_OUTSIDE }, + { "move_asleep_setattr", move_asleep_setattr, FROM_OUTSIDE }, + { "move_asleep_fair_setattr", move_asleep_fair_setattr, FROM_OUTSIDE }, + { "move_vs_rebuild", move_vs_rebuild, FROM_OUTSIDE }, + { "move_became_deadline", move_became_deadline, FROM_OUTSIDE }, + { "move_became_deadline_no_room", move_became_deadline_no_room, FROM_OUTSIDE }, + { "move_affinity_changed", move_affinity_changed, FROM_OUTSIDE }, + { "move_affinity_became_deadline", move_affinity_became_deadline, FROM_OUTSIDE }, + { "move_died", move_died, FROM_OUTSIDE }, + { "move_woken", move_woken, FROM_OUTSIDE }, + { "move_woken_full", move_woken_full, FROM_OUTSIDE }, { "move_cancelled", move_cancelled, FROM_OUTSIDE }, + { "frozen_moved_switched", frozen_moved_switched, FROM_OUTSIDE }, + { "move_isolated", move_isolated, FROM_OUTSIDE }, + { "move_isolated_refused", move_isolated_refused, FROM_OUTSIDE }, + { "move_isolated_asleep", move_isolated_asleep, FROM_OUTSIDE }, + { "move_spanning", move_spanning, FROM_OUTSIDE }, + { "affinity_moved_asleep", affinity_moved_asleep, FROM_OUTSIDE }, { "death_running", death_running, MEASURED_IN_P }, { "death_switched_out", death_switched_out, MEASURED_IN_P }, { "death_shrunk", death_shrunk, MEASURED_IN_P },
@@ -5431,16 +7931,20 @@ static const struct p_case { { "death_zombie_switched", death_zombie_switched, FROM_OUTSIDE }, { "death_zombie_hotplug", death_zombie_hotplug, FROM_OUTSIDE }, { "yield_switched_out", yield_switched_out, IN_P }, + { "yield_moved", yield_moved, FROM_OUTSIDE }, { "yield_killed", yield_killed, MEASURED_IN_P }, { "overrun_caught", overrun_caught, IN_P }, { "overrun_group_death", overrun_group_death, MEASURED_IN_P }, { "reclaim_runs", reclaim_runs, MEASURED_IN_P }, { "reclaim_switched", reclaim_switched, MEASURED_IN_P }, + { "reclaim_moved", reclaim_moved, FROM_OUTSIDE }, { "server_params", server_params, IN_P }, { "server_rebuilds", server_rebuilds, FROM_OUTSIDE }, { "server_states", server_states, FROM_OUTSIDE }, { "server_reclaim", server_reclaim, MEASURED_IN_P }, { "params_waking", params_waking, FROM_OUTSIDE }, + { "random_mix", random_mix, FROM_OUTSIDE }, + { "move_normalized", move_normalized, FROM_OUTSIDE }, };#define N_P_CASES ((int)ARRAY_SIZE(p_cases))
@@ -5457,6 +7961,7 @@ static const struct hp_case { { "hotplug_departed", hotplug_departed }, { "hotplug_server", hotplug_server }, { "affinity_vs_setattr", affinity_vs_setattr, 1 }, + { "move_back_cpu_offline", move_back_cpu_offline }, };#define N_HP_CASES ((int)ARRAY_SIZE(hp_cases))
@@ -5560,7 +8065,8 @@ int main(void)if (read_file ("/sys/devices/system/cpu/isolated" , isolated,sizeof (isolated))) isolated[0 ] =0 ; ncpus =read_online (&online,0 ); -if (ncpus <0 ) + nisol =read_online (&isol,1 ); +if (ncpus <0 || nisol <0 )ksft_exit_fail_msg ("the online CPUs cannot be read\n" );if (ncpus <3 )ksft_exit_skip ("needs 3 CPUs not isolated by isolcpus=domain\n" );
Signature · 1 line
2.47.3
PATCH v10 13/40 sched/deadline: Make a rebuild count a task in the root domain it runs in
In reply to Mathieu DesnoyersA rebuild of the accounting counts each deadline task on the first active CPU of its cpuset. Commit 318e18ed22e8 ("sched/deadline: Walk up cpuset hierarchy to decide root domain when hot-unplug") made this choice. It relies on the bandwidth check to keep such a CPU active. This guess would go wrong in three ways:
- A cpuset, like the root one, can hold CPUs isolated by isolcpus="domain" beside housekeeping CPUs. If an isolated CPU comes first, the task would be counted in the default root domain. Its own root domain would then be short of that bandwidth.
- A task confined to isolated CPUs can see them all go offline. The rebuild would then find no active CPU, and hit its BUG_ON().
- dl_task_offline_migration() moves a throttled task, with its bandwidth, off CPUs that went offline. The rebuild would count it in its cpuset, not where it moved.
In the first and third cases, a root domain would admit deadline tasks beyond its capacity.
Fix this by choosing the CPU in this order:
1. The CPU the task is on, if it is active and in the task's mask. A mask can hold CPUs of two root domains, for instance after select_fallback_rq() widened it. Also the CPU the task is queued on out of its mask, if it is active and recorded with the bandwidth. Without admission control, dl_task_offline_migration() can put a throttled task there. It runs there until it blocks.
2. dl_bw_cpu, the CPU recorded with the bandwidth, if it is active and in the mask. A task moved asleep is not on its new CPUs yet.
3. The first active CPU of the mask, a housekeeping one if any.
4. With no active CPU in the mask: the task falls back to its cpuset. Count it there. Keep dl_bw_cpu if the cpuset holds it.
The mask of the task comes before its cpuset. A partition change sets the task masks before its rebuild. dl_get_task_effective_cpus() reads the CPUs isolated at boot, HK_TYPE_DOMAIN_BOOT. HK_TYPE_DOMAIN also leaves out the CPUs of isolated partitions, and is updated after the rebuild.
Leave out a task with no active CPU, instead of the BUG_ON(). dl_bw_gen then keeps its release from removing anything. dl_task_offline_migration() counts it again when it moves it.
A task that select_fallback_rq() moves to another root domain still runs where its bandwidth is not counted. The next patch moves the bandwidth with it.
The Sashiko AI reviews of this series found these cases. The case rebuild_record_taken, added to deadline_bw_test later in this series, tests the rule for a task with no active CPU in its mask.
--- kernel/sched/deadline.c | 100 ++++++++++++++++++++++++++++++++++------ 1 file changed, 87 insertions(+), 13 deletions(-)
kernel/sched/deadline.c +87 -13
@@ -953,14 +953,20 @@ static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p__dl_sub (dl_b, p->dl.dl_bw,cpumask_weight (src_rq->rd->span));raw_spin_unlock (&dl_b->lock); - - dl_b = &later_rq->rd->dl_bw; -raw_spin_lock (&dl_b->lock); -__dl_add (dl_b, p->dl.dl_bw, -cpumask_weight (later_rq->rd->span)); -raw_spin_unlock (&dl_b->lock); } +/* +* Count the bandwidth in the root domain the task moves to. +* Do it also when a rebuild left the task out, for want of an +* active CPU. A rebuild that reaches the task after this finds +* its dl_bw_gen current, and leaves it alone. +*/ + dl_b = &later_rq->rd->dl_bw; +raw_spin_lock (&dl_b->lock); +__dl_add (dl_b, p->dl.dl_bw, +cpumask_weight (later_rq->rd->span)); +raw_spin_unlock (&dl_b->lock); p->dl.dl_bw_cpu = later_rq->cpu; + p->dl.dl_bw_gen = dl_bw_gen; }set_task_cpu (p, later_rq->cpu);
@@ -3843,8 +3849,14 @@ static void dl_get_task_effective_cpus(struct task_struct *p, struct cpumask *cp {const struct cpumask *hk_msk; - hk_msk =housekeeping_cpumask (HK_TYPE_DOMAIN ); -if (housekeeping_enabled (HK_TYPE_DOMAIN )) { +/* +* Use the CPUs isolated at boot. HK_TYPE_DOMAIN also leaves out the +* CPUs of isolated partitions, and is updated after the rebuild. A +* CPU that just left such a partition is then in a new root domain, +* but HK_TYPE_DOMAIN still says it is isolated. +*/ + hk_msk =housekeeping_cpumask (HK_TYPE_DOMAIN_BOOT ); +if (housekeeping_enabled (HK_TYPE_DOMAIN_BOOT )) {if (!cpumask_intersects (p->cpus_ptr, hk_msk)) {/* * CPUs isolated by isolcpu="domain" always belong to
@@ -3863,6 +3875,62 @@ static void dl_get_task_effective_cpus(struct task_struct *p, struct cpumask *cpcpuset_cpus_allowed_locked (p, cpus); } +/* +* Return a CPU of the root domain where a rebuild of the accounting should +* count the bandwidth of @p, or nr_cpu_ids if there is none. +* +* Prefer the CPU @p is on, if it is active and in the mask of @p. The +* task runs, or wakes up, in the root domain of that CPU. A mask can hold +* CPUs of two root domains: housekeeping CPUs and CPUs isolated by +* isolcpus="domain". When every CPU of a mask goes offline, +* select_fallback_rq() can widen it to both. It moves no bandwidth. +* Prefer that CPU also if @p is queued there, out of its mask, and the +* CPU is the one recorded. Without admission control, +* dl_task_offline_migration() can put a throttled task and its bandwidth +* on any active CPU. The task runs there until it blocks. +* +* Else prefer the CPU recorded with the bandwidth, if it is active and in +* the mask. It is in the root domain that admitted the bandwidth, or that +* its last move put it in. A task moved asleep is still on a CPU of the +* root domain it left, out of its mask. Else take the first active CPU of +* the mask, a housekeeping one if there is one. If no CPU of the mask is +* active, use the CPUs of the cpuset instead of the mask. A task on +* isolated CPUs that all went offline can have no active CPU at all. +*/ +static unsigned int dl_task_rebuild_cpu (struct task_struct *p) +{ +struct cpumask *msk =this_cpu_cpumask_var_ptr (local_cpu_mask_dl); +/* +* Read the record once. The last schedule of a dead task clears it +* under its rq lock, and the caller holds only the pi_lock. +*/ +int recorded =READ_ONCE (p->dl.dl_bw_cpu); +unsigned int cpu; + +/* +* Look at the mask of the task first. A change of the cpusets sets +* the mask of each task before its rebuild. If no CPU of the mask +* is active, ask the cpuset. The task wakes up on one of its CPUs. +* Take the CPU recorded only if it is one of them: a partition root +* can have taken it since it was recorded. +*/ +cpumask_and (msk, &p->cpus_mask, cpu_active_mask); + cpu =task_cpu (p); +if (cpumask_test_cpu (cpu, msk) || + (cpu == recorded &&cpu_active (cpu) &&task_on_rq_queued (p))) +return cpu; +if (cpumask_empty (msk)) { +dl_get_task_effective_cpus (p, msk); +cpumask_and (msk, msk, cpu_active_mask); + } +if (recorded >=0 &&cpumask_test_cpu (recorded, msk)) +return recorded; + cpu =cpumask_first_and (msk,housekeeping_cpumask (HK_TYPE_DOMAIN_BOOT )); +if (cpu < nr_cpu_ids) +return cpu; +return cpumask_first (msk); +} +/* From dl_rebuild_rd_accounting(), under the locks it asserts. */ void dl_add_task_root_domain (struct task_struct *p) {
@@ -3870,7 +3938,6 @@ void dl_add_task_root_domain(struct task_struct *p)struct rq *rq;struct dl_bw *dl_b;unsigned int cpu; -struct cpumask *msk;raw_spin_lock_irqsave (&p->pi_lock, rf.flags);/*
@@ -3885,10 +3952,17 @@ 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); +if (cpu >= nr_cpu_ids) { +/* +* 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. +*/ +raw_spin_unlock_irqrestore (&p->pi_lock, rf.flags); +return ; + }raw_spin_lock (&dl_departed_lock);/*
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v10 13/40] sched/deadline: Make a rebuild count a task in the root domain it runs in
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] sched/deadline: dl_add_task_root_domain() drops departed out-of-mask task bw --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] sched/deadline: dl_add_task_root_domain() drops departed out-of-mask task bw
--
commit 39db1557a3e0cb285c7dd82b57ad0144aeff4d00
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 CPU and root domain where a deadline task's bandwidth is counted during a root domain accounting rebuild, and leaves out tasks with no active CPU instead of triggering a BUG_ON().
Quoted 4 lines
kernel/sched/deadline.c +0
-0
[ ... ]
Quoted 7 lines
kernel/sched/deadline.c +3
-0@@ -3863,6 +3875,62 @@ static void dl_get_task_effective_cpus(struct task_struct *p, struct cpumask *cpcpuset_cpus_allowed_locked (p, cpus); } +/* +* Return a CPU of the root domain where a rebuild of the accounting should +* count the bandwidth of @p, or nr_cpu_ids if there is none.
[ ... ]
Quoted 54 lines
kernel/sched/deadline.c +35
-5+static unsigned int dl_task_rebuild_cpu (struct task_struct *p) +{ +struct cpumask *msk =this_cpu_cpumask_var_ptr (local_cpu_mask_dl); +/* +* Read the record once. The last schedule of a dead task clears it +* under its rq lock, and the caller holds only the pi_lock. +*/ +int recorded =READ_ONCE (p->dl.dl_bw_cpu); +unsigned int cpu; + +/* +* Look at the mask of the task first. A change of the cpusets sets +* the mask of each task before its rebuild. If no CPU of the mask +* is active, ask the cpuset. The task wakes up on one of its CPUs. +* Take the CPU recorded only if it is one of them: a partition root +* can have taken it since it was recorded. +*/ +cpumask_and (msk, &p->cpus_mask, cpu_active_mask); + cpu =task_cpu (p); +if (cpumask_test_cpu (cpu, msk) || + (cpu == recorded &&cpu_active (cpu) &&task_on_rq_queued (p))) +return cpu; +if (cpumask_empty (msk)) { +dl_get_task_effective_cpus (p, msk); +cpumask_and (msk, msk, cpu_active_mask); + } +if (recorded >=0 &&cpumask_test_cpu (recorded, msk)) +return recorded; + cpu =cpumask_first_and (msk,housekeeping_cpumask (HK_TYPE_DOMAIN_BOOT )); +if (cpu < nr_cpu_ids) +return cpu; +return cpumask_first (msk); +} +/* From dl_rebuild_rd_accounting(), under the locks it asserts. */ void dl_add_task_root_domain (struct task_struct *p) {@@ -3870,7 +3938,6 @@ void dl_add_task_root_domain(struct task_struct *p)struct rq *rq;struct dl_bw *dl_b;unsigned int cpu; -struct cpumask *msk;raw_spin_lock_irqsave (&p->pi_lock, rf.flags);/* @@ -3885,10 +3952,17 @@ 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);
kernel/sched/deadline.c void dl_add_task_root_domain(struct task_struct *p) · line 3965
Quoted 10 lines
kernel/sched/deadline.c +10
-0+if (cpu >= nr_cpu_ids) { +/* +* 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. +*/ +raw_spin_unlock_irqrestore (&p->pi_lock, rf.flags); +return ; + }
[Severity: Medium]
Can this early return drop the departing bandwidth of a task that was migrated outside its mask by dl_task_offline_migration() and exited during the rebuild?
When admission control is disabled and all CPUs in a task's mask and cpuset are offline, dl_task_offline_migration() moves a throttled task and its bandwidth to an active fallback CPU (recording it in p->dl.dl_bw_cpu). If a subsequent rebuild runs dl_clear_root_domains() and the task then exits on that fallback CPU with its 0-lag time in the future before dl_add_task_root_domain() visits it, the task is dequeued and placed on the departed list.
Because task_on_rq_queued(p) is now false and neither p->cpus_mask nor dl_get_task_effective_cpus() contains an active CPU, dl_task_rebuild_cpu() returns nr_cpu_ids. Returning before acquiring dl_departed_lock skips the !list_empty(&p->dl.dl_departed_node) branch right below, which would otherwise count the departed task on its active recorded p->dl.dl_bw_cpu until the inactive timer fires:
kernel/sched/deadline.c:dl_add_task_root_domain() {
...
if (!list_empty(&p->dl.dl_departed_node)) {
int recorded = p->dl.dl_bw_cpu;
if (p->dl.dl_bw_gen != dl_bw_gen - 1)
goto unlock;
if (recorded < 0 || !cpu_active(recorded))
goto unlock;
cpu = recorded;
}
...
}Deferring the cpu >= nr_cpu_ids check (or dl_task_rebuild_cpu() itself) until after the departed-list check under dl_departed_lock would preserve the departing bandwidth on the recorded CPU's root domain.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Re: [PATCH v10 13/40] sched/deadline: Make a rebuild count a task in the root domain it runs in
Quoted 3 lines
Can this early return drop the departing bandwidth of a task that was migrated outside its mask by
dl_task_offline_migration()and exited during the rebuild?
Yes, by reading. It needs admission control off, every CPU of the mask offline, and an exit during the rebuild. In v11, the check for a task with no active CPU will come after the check for a departed task, under dl_departed_lock. A departed task will then be counted on the CPU recorded with it. No case will test it.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
PATCH v10 14/40 sched/deadline: Move a task's bandwidth to the root domain it runs in
In reply to Mathieu DesnoyersWhen a CPU goes offline, select_fallback_rq() picks a new CPU for a deadline task asleep on it. balance_push() moves a queued task the same way. The new CPU can be in another root domain. With isolcpus="domain", the isolated CPUs are in the default root domain. A cpuset that is not a partition can hold CPUs of both root domains.
Commit 59d06cea1198 ("sched/deadline: Fix bandwidth accounting at all levels after offline migration") fixed this for a throttled task. dl_task_offline_migration() moves its bandwidth with it. A task that select_fallback_rq() places still moves no bandwidth. So it runs in a root domain that does not count its bandwidth, until the next rebuild. That root domain can admit deadline tasks beyond its capacity.
Fix this in migrate_task_rq_dl(). The CPU recorded with the bandwidth gives the root domain that counts it. If the new CPU is in another root domain, move the bandwidth there. Record the new CPU. As dl_task_offline_migration() does, add the bandwidth even without room, since the task runs there. Add it also if a rebuild left the task out. Do not move it to a CPU that is not active. A migration-disabled task can wake up on a CPU that goes offline. That CPU is in the default root domain by then. The task leaves it before it runs anywhere else.
Compare the root domains, not the CPU the task leaves. That CPU can still be in the mask when select_fallback_rq() picks a CPU of the other root domain. A task can also wake up on its CPU with no migration while another root domain counts its bandwidth. A cpuset move or a rebuild can move the bandwidth while the task sleeps. A change of the mask can then give that CPU back. So task_woken_dl() makes the same check.
Both checks read the record without dl_departed_lock, so a move inside a root domain takes no lock. dl_task_move_bw() checks again under dl_departed_lock. Its caller holds the pi_lock or the rq lock of the task. Together, the two locks keep the record stable.
Move no bandwidth while the sched domains are rebuilt. The CPUs then change root domains one by one. Two CPUs can be in different root domains only because one of them was attached to its new root domain first. dl_bw_rebuild_begin() opens this window in partition_sched_domains_locked(). dl_clear_root_domains() closes it. Then the walk of the task list counts each task. Until it reaches a task, no move of that task is made: the walk counts it where it is. Once the walk counted the task, its bandwidth moves again.
These cases, added to deadline_bw_test later in this series, fail without this patch: - hotplug_fallback, hotplug_fallback_moved andhotplug_fallback_in_mask: a task woken on an isolated CPU stays counted in the housekeeping root domain. - no_admission_affinity_moves: without admission control, a task given an affinity of one isolated CPU stays counted in the housekeeping root domain.
This follows the Sashiko AI reviews of this series.
--- include/linux/sched.h | 6 + include/linux/sched/deadline.h | 1 + kernel/sched/deadline.c | 207 ++++++++++++++++++++++++++++++--- kernel/sched/topology.c | 3 + 4 files changed, 200 insertions(+), 17 deletions(-)
include/linux/sched.h +6 -0
@@ -753,6 +753,11 @@ 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_resv_cpu is a CPU of the root domain where a cpuset attach * in progress reserved room for the bandwidth, or -1. * cpuset_can_attach() sets it, and cpuset_attach() moves the
@@ -761,6 +766,7 @@ struct sched_dl_entity {struct list_head dl_departed_node;int dl_bw_cpu;unsigned int dl_bw_gen; +unsigned int dl_bw_skip_gen;int dl_bw_resv_cpu;/*
include/linux/sched/deadline.h +1 -0
@@ -33,6 +33,7 @@ struct root_domain;extern void dl_add_task_root_domain (struct task_struct *p);extern void dl_clear_root_domain (struct root_domain *rd);extern void dl_clear_root_domains (void ); +extern void dl_bw_rebuild_begin (void );extern void dl_rebuild_rd_accounting (void );/* * Keep a rebuild of the root domains' accounting and an admission of
kernel/sched/deadline.c +190 -17
273 lines not on this page · raw message
kernel/sched/topology.c +3 -0
@@ -3430,6 +3430,9 @@ static void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new n = ndoms_new; } +/* dl_clear_root_domains() ends it, in dl_rebuild_rd_accounting(). */ +dl_bw_rebuild_begin (); +/* Destroy deleted domains: */ for (i =0 ; i < ndoms_cur; i++) {for (j =0 ; j < n && !new_topology; j++) {
Signature · 1 line
2.47.3
PATCH v10 15/40 sched/deadline: Keep a task in the root domain that counts its bandwidth
In reply to Mathieu DesnoyersThe root domain that admits a deadline task counts its bandwidth. The mask of the task can hold CPUs of another root domain. With isolcpus="domain", the isolated CPUs are in the default root domain. A cpuset that is not a partition root can hold isolated and housekeeping CPUs.
The code that picks a CPU for a task does not look at the root domain that counts it:
- select_fallback_rq() and dl_task_offline_migration() pick a CPU when the CPU of the task goes offline.
- __set_cpus_allowed_ptr_locked() picks one when the mask changes.
- select_task_rq_dl() can leave a task on the CPU it slept on, which can be in the other root domain.
- dl_task_rebuild_cpu() picks the CPU that a rebuild counts a task with. It prefers the CPU the task is on, then a housekeeping CPU.
Each can take the task, or its bandwidth, to the other root domain while its own root domain still has a CPU for it. The bandwidth then moves with no admission test, and the root domain it arrives in can exceed its limit.
Add dl_task_bw_span(), which returns the CPUs of the root domain that counts a task. Make each of these places try one of those CPUs first. After a rebuild the root domains are new, and the CPU recorded with the bandwidth can be offline. So dl_task_rebuild_cpu() cannot use the root domain of that CPU. Record in dl_bw_def whether the default root domain counted the task. dl_task_rebuild_cpu() then prefers a CPU of the mask on the same side: in the default root domain or not. The side cannot come from the CPUs isolated at boot: a CPU of an isolated partition is in the default root domain too.
The CPU recorded can be the one going offline. Its root domain still counts the task until the rebuild attaches that CPU to the default root domain. So dl_task_bw_span() still returns that root domain until then. This covers the common case: a task that runs on the CPU when it goes offline, which balance_push() moves.
select_fallback_rq() can also widen the mask of the task. It first gives the task the CPUs of its cpuset, then the housekeeping CPUs. At an offline, this happens before the cpuset update. The wider mask can then hold no active CPU of the root domain that counts the task. The task then leaves that root domain, and its bandwidth moves with no admission test. It stays out after the cpuset update. In testing, a deadline task with a period of 2ms that spun on the CPU going offline left its root domain this way in every run. So when the wider mask has no active CPU of that root domain, allow the CPUs of that root domain instead. The cpuset update that comes next sets the real mask. The task gets a copy of the span, made where no rebuild of the sched domains changes it. If no CPU of the span is allowed, select_fallback_rq() still tries its fallback mask.
dl_task_offline_migration() has the same window, for a throttled task whose timer fires on the CPU going offline. When no CPU of its mask is active, it warned and took any active CPU. It now takes an active CPU of the root domain that counts the task first. It warns only if that root domain has none either.
dl_task_bw_span() returns NULL while the sched domains are rebuilt. The CPUs then change root domains one by one. A housekeeping CPU passes through the default root domain, which holds the isolated CPUs. Its span would send a housekeeping task to an isolated CPU. So inside a rebuild, the CPU is picked as it was before this patch. If that CPU is in the other root domain, the next wakeup brings the task back. dl_task_offline_migration() moves no bandwidth inside a rebuild, nor until the walk of the task list counts the task. That walk counts it on the side of the CPU recorded.
A known limit remains. A wakeup can pick its CPU inside a rebuild, before the walk of the task list that follows reaches the task. If it is queued after the walk counted the task, its bandwidth can still move to the other root domain, with no admission test. This was found by reading, not reproduced.
A task with no CPU left in its root domain still moves, and its bandwidth with it. The next two patches refuse the changes that lead to that.
The case hotplug_keeps_root_domain, added to deadline_bw_test later in this series, tests this. A cpuset holds two isolated CPUs and two housekeeping CPUs, H and K. A deadline task counted in the housekeeping root domain sleeps on H. Then H goes offline. The task must wake up on K. Without this patch it woke up on an isolated CPU: CPU 0, in a guest with isolcpus=domain,0,1.
These cases test the other paths:
- hotplug_spinner_keeps: a running task whose CPU goes offline.
- hotplug_fallback_running: the same, when the cpuset of the task has no CPU left.
- hotplug_waking_keeps: a task that wakes up often, at the offline.
- hotplug_isolated_keeps: the same, for a task counted on the isolated side.
- rebuild_waking_keeps: a wakeup inside a rebuild. It fails in most runs if dl_task_bw_span() does not return NULL there. That needs isolcpus=domain,0,1.
- hotplug_isolated_partition_falls_back: a task counted on the CPU of an isolated partition, which goes offline. When the side came from the CPUs isolated at boot, the rebuild moved the bandwidth to the housekeeping root domain. This failed in each of 4 guests with isolcpus=domain.
--- include/linux/sched.h | 7 ++ kernel/sched/core.c | 70 +++++++++++ kernel/sched/deadline.c | 271 ++++++++++++++++++++++++++++++++++------ kernel/sched/sched.h | 3 + 4 files changed, 314 insertions(+), 37 deletions(-)
include/linux/sched.h +7 -0
@@ -762,6 +762,12 @@ struct sched_dl_entity {* in progress reserved room for the bandwidth, or -1. * cpuset_can_attach() sets it, and cpuset_attach() moves the * bandwidth there. +* +* @dl_bw_def is true if def_root_domain counted the bandwidth when +* @dl_bw_cpu was recorded. A rebuild changes the root domains, and +* the CPU recorded can go offline. A rebuild then keeps the +* bandwidth on that side: in def_root_domain or not. It is the last +* member, so that no hole is left. */ struct list_head dl_departed_node;int dl_bw_cpu;
@@ -786,6 +792,7 @@ struct sched_dl_entity {*/ struct sched_dl_entity *pi_se;#endif +bool dl_bw_def; };#ifdef CONFIG_UCLAMP_TASK
kernel/sched/core.c +70 -0
96 lines not on this page · raw message
kernel/sched/deadline.c +234 -37
409 lines not on this page · raw message
kernel/sched/sched.h +3 -0
@@ -361,6 +361,9 @@ extern bool __checkparam_dl(const struct sched_attr *attr);extern bool dl_param_changed (struct task_struct *p,const struct sched_attr *attr);extern int dl_cpuset_cpumask_can_shrink (const struct cpumask *cur,const struct cpumask *trial);extern int dl_bw_deactivate (int cpu); +extern const struct cpumask *dl_task_bw_span (struct task_struct *p); +extern bool dl_task_bw_span_copy (struct task_struct *p,struct cpumask *dst); +extern struct cpumask *dl_fallback_mask (void );extern s64 dl_scaled_delta_exec (struct rq *rq,struct sched_dl_entity *dl_se,s64 delta_exec);/* * SCHED_DEADLINE supports servers (nested scheduling) with the following
Signature · 1 line
2.47.3
PATCH v10 16/40 sched/deadline: Keep the last CPU a task has in its root domain
In reply to Mathieu Desnoyersdl_bw_deactivate() refuses to take a CPU offline when the rest of its root domain cannot hold the deadline bandwidth counted there. It also keeps the last CPU of a root domain that counts a deadline task. That test comes from commit d4742f6ed7ea ("sched/deadline: Correctly account for allocated bandwidth during hotplug").
That test looks at the root domain, not at the mask of each task. With isolcpus="domain", a cpuset that is not a partition root can hold one housekeeping CPU and some isolated CPUs. A deadline task in it can be counted in the housekeeping root domain. When that housekeeping CPU goes offline, the root domain keeps its other CPUs, so the offline is accepted. But the task has only isolated CPUs left. It runs in the default root domain, which did not admit it. Its bandwidth follows with no admission test, and the default root domain can exceed its limit.
Refuse the offline with EBUSY in that case. Walk the deadline tasks counted in the root domain of the CPU. Refuse if one of them has CPUs left to run on, but none in that root domain. Skip the walk if no task holds a bandwidth. A task whose mask holds no other CPU gets the CPUs its cpuset has after the offline. With no CPU left in a cpuset, cgroup v2 gives it the CPUs of its parent, worked out from the top down. A partition root that holds a task and has no CPU of its own left takes the CPUs of a child partition instead. cgroup v1 moves its tasks to the nearest ancestor with CPUs. Add cpuset_cpus_allowed_without(), which works out the CPUs the same way. Like __set_cpus_allowed_ptr(), it keeps them within the affinity set by the user, if they intersect. If the cpuset gives no CPU the task can run on, select_fallback_rq() picks one. The previous patch makes it keep a deadline task in its root domain when it can. Skip a dead task: it runs no more.
When a partition root takes the CPUs of a child partition, cpuset makes that child invalid, with its own child partitions. Their CPUs then join the root domain of the parent, with their tasks. So a deadline task can change root domain even when it is counted in a root domain other than that of the CPU going offline. The walk above does not see it. In testing without this check, an offline put three deadline tasks of 40% in a root domain of one CPU. That is over its limit. Refuse the offline if one of these cpusets holds a deadline task. Add cpuset_offline_moves_dl() for this.
cpuset also makes invalid a partition left with tasks and no CPU of its own. Its tasks then count in its parent. The parent can then take the CPUs of a child partition that cpuset visits after it. cpuset_cpus_allowed_without() and cpuset_offline_moves_dl() follow this.
The offline is accepted when the CPUs keep their root domain:
- the parent and the child partition are both isolated: the CPUs stay in the default root domain.
- neither is isolated, and the parent takes all the CPUs of the child:
its root domain then spans the same CPUs.
When the last
CPU left to the top cpuset goes offline, cpuset makes every partition invalid. Refuse that offline if a partition holds a deadline task.
Do not refuse during a suspend. It takes the CPUs offline with the tasks frozen, and the resume puts them back. Do not refuse without admission control either: there is no limit to exceed.
An administrator who needs the CPU offline can first give the cpuset of the task another CPU of that root domain. The offline is then accepted, and the task wakes up on that CPU: the hotplug_keeps_root_domain case tests this. The administrator can also turn admission control off, by writing -1 to /proc/sys/kernel/sched_rt_runtime_us.
The walk cannot miss a task. sched_setattr() refuses the deadline policy to a task whose mask does not hold its whole root domain. sched_setaffinity() only gives a deadline task such a mask. A mask also gets smaller by a cpuset change or a cpuset move, and both exclude CPU hotplug.
For user-space, the refused offline is a write of 0 to /sys/devices/system/cpu/cpuN/online that fails with EBUSY. An offline that leaves each deadline task a CPU in its root domain is accepted as before.
The case hotplug_last_refused, added to deadline_bw_test later in this series, tests this. Without this patch the offline is accepted. These cases test the other paths:
- hotplug_user_mask_refused: the affinity set by the user.
- hotplug_nested_refused: nested cpusets.
- hotplug_dead_accepted: a dead task.
- hotplug_child_partition_kept and hotplug_partition_invalid_refused: a child partition made invalid.
- hotplug_sibling_partition_refused: a sibling partition that the offline makes invalid.
- hotplug_top_last_refused: the last CPU of the top cpuset.
--- include/linux/cpuset.h | 17 +++ kernel/cgroup/cpuset.c | 275 ++++++++++++++++++++++++++++++++++++++++ kernel/sched/deadline.c | 72 ++++++++++- 3 files changed, 363 insertions(+), 1 deletion(-)
include/linux/cpuset.h +17 -0
@@ -16,6 +16,7 @@#include <linux/nodemask.h> #include <linux/mm.h> #include <linux/mmu_context.h> +#include <linux/sched/isolation.h> #include <linux/jump_label.h> extern bool lockdep_is_cpuset_held (void );
@@ -79,6 +80,9 @@ extern void lockdep_assert_cpuset_lock_held(void);extern void cpuset_cpus_allowed_locked (struct task_struct *p,struct cpumask *mask);extern void cpuset_cpus_allowed (struct task_struct *p,struct cpumask *mask);extern bool cpuset_cpus_allowed_fallback (struct task_struct *p); +extern bool cpuset_cpus_allowed_without (struct task_struct *p,int cpu, +struct cpumask *mask); +extern bool cpuset_offline_moves_dl (int cpu);extern int cpuset_num_cpus (struct cgroup *cgroup);extern nodemask_t cpuset_mems_allowed (struct task_struct *p);#define cpuset_current_mems_allowed (current->mems_allowed)
@@ -214,6 +218,19 @@ static inline bool cpuset_cpus_allowed_fallback(struct task_struct *p)return false; } +static inline bool cpuset_cpus_allowed_without (struct task_struct *p,int cpu, +struct cpumask *mask) +{ +cpumask_and (mask,task_cpu_fallback_mask (p), cpu_active_mask); +cpumask_clear_cpu (cpu, mask); +return false; +} + +static inline bool cpuset_offline_moves_dl (int cpu) +{ +return false; +} +static inline int cpuset_num_cpus (struct cgroup *cgroup) {return num_online_cpus ();
kernel/cgroup/cpuset.c +275 -0
287 lines not on this page · raw message
kernel/sched/deadline.c +71 -1
80 lines not on this page · raw message
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v10 16/40] sched/deadline: Keep the last CPU a task has in its root domain
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [Medium] cgroup/cpuset: partition_child_lost() picks last sibling after gone not first - [Medium]…
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [Medium] cgroup/cpuset: partition_child_lost() picks last sibling after gone not first
- [Medium] cgroup/cpuset: upward cascade of gone across unpopulated parent partitions
- [Medium] cgroup/cpuset: missed root domain move for tasks in isolated partition gone
- [Medium] cgroup/cpuset: skipping a == lost in cpuset_offline_moves_dl() ignores subpartitions
- [Medium] cgroup/cpuset: dl_task_in_cpuset() falsely matches SCHED_FLAG_SUGOV kthreads
--
commit 38c9a6cf11adbb513d6b034865c486d38c14c195
Author: Mathieu Desnoyers <[email protected]>
sched/deadline: Keep the last CPU a task has in its root domain
This commit refuses to take a CPU offline with -EBUSY when doing so would leave a deadline task running in a different root domain that did not admit its bandwidth, either because the task loses its last CPU in its root domain or because the offline invalidates a cpuset partition.
Quoted 4 lines
kernel/cgroup/cpuset.c +0
-0
[ ... ]
Quoted 3 lines
kernel/cgroup/cpuset.c +0
-0@@ -4312,6 +4334,259 @@ void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)spin_unlock_irqrestore (&callback_lock, flags); }
[ ... ]
Quoted 17 lines
kernel/cgroup/cpuset.c +17
-0+static struct cpuset *partition_child_lost (struct cpuset *p,int cpu, +struct cpumask *new_cpus, +struct cpuset *gone) +{ +bool populated =partition_populated_offline (p); +struct cgroup_subsys_state *css; +struct cpuset *child, *lost =NULL ; + +cpumask_and (new_cpus, p->effective_xcpus, cpu_active_mask); +cpumask_clear_cpu (cpu, new_cpus); +if (cpumask_empty (new_cpus)) +return NULL ; +cpuset_for_each_child (child, css, p) { +if (child == gone) { + populated = true; +continue ; + }
kernel/cgroup/cpuset.c void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask) · line 4403
Quoted 10 lines
kernel/cgroup/cpuset.c +10
-0+if (!is_partition_valid (child)) +continue ; +if (populated &&cpumask_subset (new_cpus, child->effective_xcpus)) { + lost = child; +continue ; + } +cpumask_andnot (new_cpus, new_cpus, child->effective_xcpus); + } +return lost; +}
[Severity: Medium]
When gone is set and p was initially unpopulated with no active CPUs of its own, does partition_child_lost() pick the last valid sibling partition after gone instead of the first?
During the pre-order walk in cpuset_handle_hotplug(), p is visited before gone is invalidated, so p->effective_cpus remains empty. Once gone is visited and invalidated, p becomes populated while p->effective_cpus is still empty. When cpuset_hotplug_update_tasks() visits the first valid sibling partition after gone, tasks_nocpu_error() checks cpumask_subset(parent->effective_cpus, xcpus), which is immediately true for an empty parent->effective_cpus:
kernel/cgroup/cpuset.c:tasks_nocpu_error() {
return (cpumask_subset(parent->effective_cpus, xcpus) &&
partition_is_populated(parent, cs)) ||
(!cpumask_intersects(xcpus, cpu_active_mask) &&
partition_is_populated(cs, NULL));
}That invalidates the first valid sibling partition after gone and gives its active CPUs to p->effective_cpus, leaving later sibling partitions valid. Here, new_cpus still holds the union of all remaining children's CPUs when visiting the first sibling after gone, so cpumask_subset(new_cpus, child->effective_xcpus) is false until the last valid sibling. If p has two valid sibling partitions after gone where the first holds a deadline task and the second does not, cpuset_offline_moves_dl() and cpuset_cpus_allowed_without() inspect the second sibling and accept the offline, merging the deadline tasks of gone and the first sibling into p's root domain without admission control.
Also, if every active CPU in p->effective_xcpus belongs to child partitions visited before gone, new_cpus becomes empty before gone, and cpumask_subset(new_cpus, child->effective_xcpus) then matches a later valid child partition whose effective_xcpus are all offline.
Quoted 8 lines
kernel/cgroup/cpuset.c +8
-0+bool cpuset_cpus_allowed_without (struct task_struct *tsk,int cpu, +struct cpumask *pmask) +{ +const struct cpumask *possible =task_cpu_possible_mask (tsk); +struct cpuset *cs =task_cs (tsk), *from, *gone =NULL ; + +guard (rcu)(); +if (!is_in_v2_mode ()) {
[ ... ]
Quoted 14 lines
kernel/cgroup/cpuset.c +14
-0+ }else { +/* +* cgroup v2 works out the CPUs of each cpuset from the top +* down. A cpuset keeps those of its parent that cpuset.cpus +* holds, or takes all of them if there are none. Start from +* the nearest partition root that keeps an active CPU, or from +* the top cpuset. A partition root left with no CPU of its own +* can take those of a child partition. One left with tasks and +* no CPU becomes invalid, and its tasks count in its parent. +*/ +for (from = cs; ; from =parent_cs (from)) { +if (from != &top_cpuset && !is_partition_valid (from)) +continue ; +cpumask_and (pmask, from->effective_cpus, cpu_active_mask);
kernel/cgroup/cpuset.c void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask) · line 4465
Quoted 10 lines
kernel/cgroup/cpuset.c +10
-0+cpumask_clear_cpu (cpu, pmask); +if (cpumask_empty (pmask) && from != &top_cpuset && + !partition_child_lost (from, cpu, pmask, gone)) +cpumask_clear (pmask); +if (!cpumask_empty (pmask) || from == &top_cpuset) +break ; + gone = gone ||partition_populated_offline (from) ? from :NULL ; + } +effective_cpus_below (cs, from, pmask); + }
[Severity: Medium]
Can gone cascade upward across an unpopulated parent partition here and in cpuset_offline_moves_dl()?
Suppose an unpopulated partition P1 has no active CPUs of its own and only one valid child partition P2, which is populated and loses its last active CPU. cpuset_handle_hotplug() walks descendants in pre-order and visits P1 before P2; at that point P2 is still valid, so partition_is_populated(P1, NULL) is false and P1 remains a valid partition with empty effective_cpus when P2 is later invalidated. With gone || partition_populated_offline(from), once gone is set to P2, the next iteration at P1 sets gone = P1 and climbs to parent_cs(P1) (or top_cpuset), so cpuset_cpus_allowed_without() returns the ancestor's CPUs instead of taking the empty-mask fallback path, and cpuset_offline_moves_dl() checks sibling partitions of P1 that are not invalidated.
Quoted 1 line
kernel/cgroup/cpuset.c +1
-0+if (!cpumask_and (pmask, pmask, possible)) {
[ ... ]
kernel/cgroup/cpuset.c void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask) · line 4504
Quoted 13 lines
kernel/cgroup/cpuset.c +13
-0+static bool dl_task_in_cpuset (struct cpuset *cs) +{ +struct css_task_iter it; +struct task_struct *task; +bool found = false; + +css_task_iter_start (&cs->css,0 , &it); +while (!found && (task =css_task_iter_next (&it))) + found = task->policy ==SCHED_DEADLINE && +READ_ONCE (task->__state) !=TASK_DEAD ; +css_task_iter_end (&it); +return found; +}
[Severity: Medium]
Does dl_task_in_cpuset() match sugov kthreads in top_cpuset that do not hold root-domain deadline bandwidth?
sugov_kthread_create() creates SCHED_DEADLINE kernel threads with SCHED_FLAG_SUGOV in top_cpuset, which dl_se_holds_bw() excludes from root-domain bandwidth accounting. When the last active CPU of top_cpuset goes offline while a partition exists and dl_nr_bw_tasks is non-zero, cpuset_offline_moves_dl() sets lost = &top_cpuset and calls dl_task_in_cpuset(&top_cpuset), refusing the offline with -EBUSY whenever a sugov kthread is present.
Quoted 33 lines
kernel/cgroup/cpuset.c +33
-0+bool cpuset_offline_moves_dl (int cpu) +{ +struct cgroup_subsys_state *pos_css; +struct cpuset *cp, *a, *p = &top_cpuset, *lost =NULL ; +bool found = false; + +if (!is_in_v2_mode ()) +return false; +rcu_read_lock (); +cpuset_for_each_descendant_pre (cp, pos_css, &top_cpuset) { +if (cp != &top_cpuset &&is_partition_valid (cp) && +cpumask_test_cpu (cpu, cp->effective_cpus)) { + p = cp; +break ; + } + } +if (p != &top_cpuset) { +struct cpuset *gone =NULL ; + +/* +* A partition left with tasks and no CPU becomes invalid. Its +* tasks then count in its parent, which can take the CPUs of a +* later child partition. The parent is then the one that takes +* CPUs. +*/ +while (!(lost =partition_child_lost (p, cpu, &dl_offline_cpus, gone)) && +cpumask_empty (&dl_offline_cpus) && + (gone ||partition_populated_offline (p)) && +parent_cs (p) != &top_cpuset &&is_partition_valid (parent_cs (p))) { + gone = p; + p =parent_cs (p); + } + }else {
kernel/cgroup/cpuset.c void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask) · line 4567
Quoted 10 lines
kernel/cgroup/cpuset.c +10
-0+cpumask_and (&dl_offline_cpus, top_cpuset.effective_cpus, cpu_active_mask); +cpumask_clear_cpu (cpu, &dl_offline_cpus); +if (cpumask_empty (&dl_offline_cpus) && !cpumask_empty (subpartitions_cpus)) + lost = &top_cpuset; + } +if (!lost) { +rcu_read_unlock (); +return false; + } +cpuset_for_each_descendant_pre (cp, pos_css, lost) {
[Severity: Medium]
Can deadline tasks in gone change root domain undetected when gone is an isolated partition and its parent p is a non-isolated partition?
Suppose p is PRS_ROOT with no active CPUs of its own and has two PRS_ISOLATED child partitions: C1 on CPU 1 with deadline tasks (counted in def_root_domain) and C2 on CPU 2 with no deadline tasks. When CPU 1 goes offline, C1 becomes gone and C2 becomes lost. cpuset_offline_moves_dl() only walks descendants of lost (C2) and finds no deadline tasks, while dl_task_leaves_span() checks C1's tasks against the current def_root_domain span, which still includes CPU 2 because C2 has not been invalidated yet. Both checks pass, and hotplug then invalidates C1 and C2 into p, moving CPU 2 and C1's deadline tasks out of def_root_domain into p's PRS_ROOT root domain without admission control.
kernel/cgroup/cpuset.c void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask) · line 4576
Quoted 9 lines
kernel/cgroup/cpuset.c +9
-0+/* The partition that gives @cp its CPUs. */ +for (a = cp; a != lost && !is_partition_valid (a); a =parent_cs (a)) + ; +if (lost != &top_cpuset && + ((a->partition_root_state ==PRS_ISOLATED && + p->partition_root_state ==PRS_ISOLATED ) || + (a == lost && a->partition_root_state !=PRS_ISOLATED && + p->partition_root_state !=PRS_ISOLATED ))) +continue ;
[Severity: Medium]
Does skipping a == lost when neither lost nor p is PRS_ISOLATED hold when lost has a valid child partition of its own?
When lost is invalidated, any valid child partition under lost is also invalidated, so p's root domain takes all active CPUs of lost->effective_xcpus rather than just lost->effective_cpus. If lost had effective_xcpus = {1, 2} and effective_cpus = {1} because a valid child partition held CPU 2 (with no deadline tasks), a member cpuset under lost configured with cpuset.cpus = 2 previously had an empty intersection with lost->effective_cpus and inherited {1}, where its deadline task was admitted. Skipping a == lost lets the offline of p's last CPU succeed, and once p's effective_cpus becomes {1, 2}, the member cpuset's effective_cpus switches from {1} to {2} (or a deadline task in lost pinned to {1} no longer spans its {1, 2} root domain).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
Re: [PATCH v10 16/40] sched/deadline: Keep the last CPU a task has in its root domain
Quoted 3 lines
When gone is set and p was initially unpopulated with no active CPUs of its own, does
partition_child_lost()pick the last valid sibling partition after gone instead of the first?
Yes. cpuset invalidates the siblings after gone in the order of its hotplug walk, until one gives p an active CPU. The model picked the last sibling that covers the CPUs left. With mixed partition types, a deadline task could then change root domain unadmitted.
Your second, fourth and fifth points come from the same model. So v11 will stop modelling the order of the walk. Take a partition left with tasks and no CPU, under a parent with no CPU of its own. The parent can also stay valid with tasks and no CPU. A later hotplug event can then make it invalid. So v11 will take the topmost partition above that child that has no CPU of its own. The offline will be refused if a deadline task is in one of its cpusets. That can refuse an offline that would be harmless. Three new cases will test it:hotplug_first_sibling_refused (your first point), hotplug_nested_partition_refused (your second) and hotplug_isolated_child_refused (your fourth).
One hole stays. The check sees the deadline tasks present at the offline. A deadline task can enter such a partition after an accepted offline. A later hotplug event can then move it to another root domain with no check for room. v11 will list it as a known limit.
Quoted 2 lines
Can gone cascade upward across an unpopulated parent partition here and in
cpuset_offline_moves_dl()?
Yes. But that climb refused a real move, by luck. A later hotplug event can make cpuset invalidate the parent left valid with no CPU. In v11, cpuset_offline_moves_dl() will climb on purpose, as above. cpuset_cpus_allowed_without() will climb one level at most.
Quoted 2 lines
Does
dl_task_in_cpuset()match sugov kthreads intop_cpusetthat do not hold root-domain deadline bandwidth?
Yes. In v11, it will skip a task whose bandwidth no root domain counts, with a new dl_task_holds_bw(). It needs schedutil, so no case will test it.
Quoted 2 lines
Can deadline tasks in gone change root domain undetected when gone is an isolated partition and its parent p is a non-isolated partition?
Yes. The rule above refuses it. hotplug_isolated_child_refused will test it.
Quoted 2 lines
Does skipping a == lost when neither lost nor p is
PRS_ISOLATEDhold when lost has a valid child partition of its own?
The root domain of p does span lost's exclusive CPUs, not only its effective ones. But the bandwidth that moves was all admitted on fewer CPUs, so nothing is overcommitted. The child partitions of lost are checked on their own. v11 will correct the comment and the message, which said "the same CPUs".
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
PATCH v10 17/40 cgroup/cpuset: Refuse to take a deadline task out of its root domain
In reply to Mathieu DesnoyersA cpuset attach asks the destination root domain for room before it moves a deadline task there. A change of the CPUs of a cpuset asks nothing. validate_change() only checks that a partition root keeps enough capacity for its deadline bandwidth. That check comes from commit f82f80426f7a ("sched/deadline: Ensure that updates to exclusive cpusets don't break AC").
With isolcpus="domain", a cpuset that is not a partition root can hold housekeeping CPUs and isolated CPUs, which are in two root domains. Two changes can take every CPU of one root domain away from that cpuset:
- a write to cpuset.cpus, of that cpuset or of an ancestor;
- a partition root that takes those CPUs, when it is created or when it grows.
A deadline task counted in that root domain then runs in the other one, which did not admit it. Its bandwidth follows with no admission test, and that root domain can exceed its limit.
Refuse both:
- A write to cpuset.cpus fails with EBUSY, and changes nothing. The cpuset.cpus file of cgroup v1 gets the same check.
- The write that makes the partition root, or makes it grow, still succeeds. cpuset makes the partition root invalid. For a "root" partition, cpuset.cpus.partition then reads "root invalid (Deadline task would leave its root domain)".
A task goes to another root domain by a move to another cpuset, which asks for room.
A change that leaves each deadline task a CPU in its root domain is accepted as before.
Like __set_cpus_allowed_ptr(), the check keeps the new mask within the affinity set by the user, if they intersect. So a mask that grows can also take a task out of its root domain, and it is checked too. cpuset cannot refuse some changes, so they are not checked: disabling a partition, a partition that cpuset makes invalid, and a CPU that comes online.
dl_task_keeps_root_domain() tells whether a new mask keeps a task in the root domain that counts it. Without admission control it accepts any mask: there is no limit to exceed. An administrator who needs the change can turn admission control off, by writing -1 to /proc/sys/kernel/sched_rt_runtime_us.
The check runs before the rebuild of the sched domains, which ends the change. A partition root whose CPUs grew can then have tasks counted in a root domain that does not span the new CPUs yet. A check of that span alone would refuse a valid child partition. It would stay invalid until the next change. So for a root partition, the CPUs it has after the change count too: the rebuild gives them to its root domain.
The cases mask_out_refused and partition_takes_refused, added to deadline_bw_test later in this series, test this. Without this patch the write is accepted and the partition root is valid. These cases test the other paths:
- partition_takes_fallback_refused: the CPUs of an ancestor;
- mask_grows_refused: a mask that grows;
- partition_swap_refused: a partition root that takes and gives CPUs in one change;
- partition_refused_no_warn: a refusal with no warning;
- partition_grows_child_valid: a child partition made valid by CPUs that its parent just got.
--- include/linux/sched/deadline.h | 3 + kernel/cgroup/cpuset-internal.h | 1 + kernel/cgroup/cpuset.c | 213 +++++++++++++++++++++++++++++++- kernel/sched/deadline.c | 36 ++++++ 4 files changed, 250 insertions(+), 3 deletions(-)
include/linux/sched/deadline.h +3 -0
@@ -62,6 +62,9 @@ extern int dl_task_bw_reserve_cpu(struct task_struct *p, const struct cpumask *new_mask);extern int dl_task_bw_reserved_cpu (struct task_struct *p);extern void dl_task_bw_unreserve (struct task_struct *p); +extern bool dl_task_keeps_root_domain (struct task_struct *p, +const struct cpumask *new_mask, +const struct cpumask *pending);static inline bool dl_server (struct sched_dl_entity *dl_se) {
kernel/cgroup/cpuset-internal.h +1 -0
@@ -36,6 +36,7 @@ enum prs_errcode {PERR_ACCESS ,PERR_REMOTE ,PERR_ISOPARENT , + PERR_DLTASK, };/* bits in struct cpuset flags field */
kernel/cgroup/cpuset.c +210 -3
280 lines not on this page · raw message
kernel/sched/deadline.c +36 -0
@@ -5287,6 +5287,42 @@ int dl_bw_deactivate(int cpu)return ret; } +/* +* Return false if @p has CPUs in the root domain that counts its +* bandwidth and @new_mask holds none of them. A cpuset refuses such a +* mask: only a move to another cpuset makes room in another root domain. +* Like __set_cpus_allowed_ptr(), keep @new_mask within the affinity set by +* the user, if they intersect. @pending is NULL, or holds the CPUs that +* the rebuild at the end of the change gives to the root domain of a +* partition root. +*/ +bool dl_task_keeps_root_domain (struct task_struct *p, +const struct cpumask *new_mask, +const struct cpumask *pending) +{ +const struct cpumask *span, *user = cpu_possible_mask; + +if (!dl_bandwidth_enabled () || !task_has_dl_policy (p)) +return true; +/* pi_lock also protects p->user_cpus_ptr. */ +guard (raw_spinlock_irqsave)(&p->pi_lock); +guard (rcu)(); + span =dl_task_bw_span (p); +/* A task in a migrate_disable() section has a cpus_ptr of one CPU. */ +if (!span || !cpumask_intersects (span, &p->cpus_mask)) +return true; +if (p->user_cpus_ptr &&cpumask_intersects (new_mask, p->user_cpus_ptr)) + user = p->user_cpus_ptr; +if (cpumask_intersects_and (span, new_mask, user)) +return true; +/* +* The rebuild at the end of the change can give the root domain +* of @p the CPUs of @pending. Test those too. +*/ +return pending &&cpumask_intersects (span, pending) && +cpumask_intersects_and (pending, new_mask, user); +} +int dl_bw_alloc (int cpu,u64 dl_bw) {return dl_bw_manage (dl_bw_req_alloc, cpu, dl_bw);
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v10 17/40] cgroup/cpuset: Refuse to take a deadline task out of its root domain
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] cgroup/cpuset: dl_tasks_keep_without() omits pending CPUs for PRS_ISOLATED - [Low] sched/deadline…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] cgroup/cpuset: dl_tasks_keep_without() omits pending CPUs for PRS_ISOLATED
- [Low] sched/deadline: dl_task_keeps_root_domain() does not skip TASK_DEAD tasks
--
commit 70ebc9e9b00f4d77522df69295e46140ef69f4bc
Author: Mathieu Desnoyers <[email protected]>
cgroup/cpuset: Refuse to take a deadline task out of its root domain
This commit rejects cpuset mask changes with -EBUSY or invalidates partitions with PERR_DLTASK when the new effective CPU mask would leave a SCHED_DEADLINE task without any CPUs in the root domain that accounts its bandwidth.
Quoted 4 lines
kernel/cgroup/cpuset.c +0
-0
[ ... ]
Quoted 40 lines
kernel/cgroup/cpuset.c +36
-0@@ -1148,6 +1181,91 @@ static void effective_cpus_below(struct cpuset *cp, struct cpuset *from, } } +/* +* A scratch mask 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; + +/* +* 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 partition root. Then do the same for its own +* cpusets, starting from the active CPUs of @xcpus. +*/ +static bool dl_tasks_keep_without (struct cpuset *root,struct cpuset *part, +const struct cpumask *xcpus, +const struct cpumask *gives,bool enable) +{ +struct cpumask *new_cpus = dl_keep_cpus, *pending =NULL ; +struct cgroup_subsys_state *pos_css; +struct cpuset *cp; +bool keep = true; + +lockdep_assert_cpuset_lock_held (); +/* +* The CPUs of @root can have changed before the rebuild of the +* sched domains, which ends the change. Then the root domain of a
kernel/cgroup/cpuset.c static void effective_cpus_below(struct cpuset *cp, struct cpuset *from, · line 1229
Quoted 10 lines
kernel/cgroup/cpuset.c +10
-0+* task of @root does not span them yet. The rebuild gives the CPUs +* of a root partition to one root domain. +*/ +if (root->partition_root_state ==PRS_ROOT ) { + pending = dl_pending_cpus; +cpumask_copy (pending, root->effective_cpus); +if (root == &top_cpuset) +cpumask_and (pending, pending, +housekeeping_cpumask (HK_TYPE_DOMAIN_BOOT )); + }
[Severity: Medium]
Should pending also be populated when root->partition_root_state is PRS_ISOLATED (for example via is_partition_valid(root))?
An isolated partition root can have an isolated child partition and hold SCHED_DEADLINE tasks whose bandwidth is counted in def_root_domain. When an isolated parent partition holding a deadline task grows by acquiring a new CPU, that CPU is not added to def_root_domain.span until rebuild_sched_domains_locked() runs at the end of the change. While update_cpumasks_hier() re-evaluates an invalid child isolated partition via update_parent_effective_cpumask() -> dl_tasks_keep_without(), pending is NULL, so dl_task_keeps_root_domain() only checks the pre-rebuild def_root_domain.span and leaves the child partition invalid with PERR_DLTASK.
Quoted 22 lines
kernel/sched/deadline.c +14
-0@@ -5287,6 +5287,42 @@ int dl_bw_deactivate(int cpu)return ret; } +/* +* Return false if @p has CPUs in the root domain that counts its +* bandwidth and @new_mask holds none of them. A cpuset refuses such a +* mask: only a move to another cpuset makes room in another root domain. +* Like __set_cpus_allowed_ptr(), keep @new_mask within the affinity set by +* the user, if they intersect. @pending is NULL, or holds the CPUs that +* the rebuild at the end of the change gives to the root domain of a +* partition root. +*/ +bool dl_task_keeps_root_domain (struct task_struct *p, +const struct cpumask *new_mask, +const struct cpumask *pending) +{ +const struct cpumask *span, *user = cpu_possible_mask;
kernel/sched/deadline.c int dl_bw_deactivate(int cpu) · line 5313
Quoted 10 lines
kernel/sched/deadline.c +10
-0+ +if (!dl_bandwidth_enabled () || !task_has_dl_policy (p)) +return true; +/* pi_lock also protects p->user_cpus_ptr. */ +guard (raw_spinlock_irqsave)(&p->pi_lock); +guard (rcu)(); + span =dl_task_bw_span (p); +/* A task in a migrate_disable() section has a cpus_ptr of one CPU. */ +if (!span || !cpumask_intersects (span, &p->cpus_mask)) +return true;
[Severity: Low]
Should dl_task_keeps_root_domain() ignore tasks with READ_ONCE(p->__state) == TASK_DEAD, the way dl_task_leaves_span() and dl_task_in_cpuset() do?
When a SCHED_DEADLINE thread in a multi-threaded process exits before its 0-lag time, it stays on dl_departed_list with a non-NULL dl_task_bw_span(p) and remains visible to css_task_iter_next() until do_cgroup_task_dead() unlinks it from cset->tasks. Evaluating that dead thread here can cause a cpuset.cpus write to fail with -EBUSY or a partition update to fail with PERR_DLTASK.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
Re: [PATCH v10 17/40] cgroup/cpuset: Refuse to take a deadline task out of its root domain
Quoted 2 lines
Should pending also be populated when
root->partition_root_stateisPRS_ISOLATED(for example viais_partition_valid(root))?
Yes, by reading. It is a false refusal: the child stays invalid until its next update. It refuses too much, but it never lets a task move unadmitted. v11 will keep it for isolated roots. Extending the pending span to them is left for later.
Quoted 3 lines
Should
dl_task_keeps_root_domain()ignore tasks withREAD_ONCE(p->__state)==TASK_DEAD, the waydl_task_leaves_span()anddl_task_in_cpuset()do?
Yes. A dead task runs no more. v11 will accept it.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
PATCH v10 18/40 selftests/sched: Test the root domain of a task in deadline_bw_test
In reply to Mathieu DesnoyersEarlier in this series, fixes keep a deadline task in the root domain that counts its bandwidth. Add the 28 cases of deadline_bw_test that test them. They cover CPU hotplug and cpuset changes with partition roots, and with CPUs isolated by isolcpus=domain:
- the CPU a task falls back to when its CPU goes offline
- a task that runs, sleeps or wakes while its CPU goes offline
- the refusals of a CPU offline or a cpuset change that would leave a deadline task no CPU in its root domain
- a rebuild that counts a task in the root domain it runs in
- a CPU offline that would take more bandwidth than the root domain has free, with the CPU's fair server (hotplug_server_refused)
The comment above each case gives its scenario. Twenty-one of these cases need CPUs isolated by isolcpus=domain, and skip without them.
The message of each fix names the cases that test it. hotplug_server_refused is not named there. It fails with isolcpus=domain,0,1 before these fixes.
On v7.4-rc2, in 8 VMs of 8 CPUs, hotplug_partition_invalid_refused fails in every VM. hotplug_child_partition_kept fails in 2 of 8. The cases that need isolcpus=domain skip there. With the first series alone, rebuild_record_taken fails with isolcpus=domain,0,1 in a guest of 7 CPUs.
These cases did not run on v7.4-rc2:
- hotplug_sibling_partition_refused fails if cpuset_offline_moves_dl() ignores a partition left with tasks and no CPU.
- hotplug_top_last_refused tests the offline of the last CPU of the top cpuset.
- partition_grows_child_valid fails without the check of the CPUs a partition root just got, in "cgroup/cpuset: Refuse to take a deadline task out of its root domain".
With the fixes up to here, every case passes wherever it runs.
--- .../sched/deadline/deadline_bw_test.c | 2358 ++++++++++++++++- 1 file changed, 2355 insertions(+), 3 deletions(-)
tools/testing/selftests/sched/deadline/deadline_bw_test.c +2355 -3
@@ -35,8 +35,13 @@* accounting, which clears what an earlier case left. * * The cases of hp_cases[] run after them, without P. These are the -* hotplug cases, and affinity_vs_setattr. The test skips the hotplug -* cases if no CPU can go offline. It still runs affinity_vs_setattr. +* hotplug cases, and ten other cases: mask_out_refused, +* partition_takes_refused, no_admission_affinity_moves, +* affinity_vs_setattr, rebuild_waking_keeps, +* partition_takes_fallback_refused, mask_grows_refused, +* partition_swap_refused, partition_refused_no_warn and +* partition_grows_child_valid. The test skips the hotplug cases if no +* CPU can go offline. It still runs these ten. * death_zombie_hotplug takes CPUs offline too, but it is in p_cases[]: it * needs the cpuset setup of those cases. *
@@ -45,7 +50,7 @@* * 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. Five need +* controller. Some need debugfs or CPU hotplug. Twenty-five need * isolcpus="domain" at boot. */ #define _GNU_SOURCE
@@ -6858,6 +6863,73 @@ static int hotplug_server(void)return KSFT_PASS ; } +/* +* A CPU that goes offline takes its capacity and the bandwidth of its +* fair server out of the root domain. Set the server of hp_cpu to the +* admission limit less 10%. The offline then costs the root domain the +* room of one probe task. Hold every probe task that the root domain +* admits but one: the CPU can still go offline. Hold one more: the +* offline must fail with EBUSY. +*/ +static int hotplug_server_refused (void ) +{ +uint64_t rt_runtime, rt_period, runtime; +int n, err, ret =KSFT_PASS ; +char buf[32 ]; + +if (ext_switched_all ()) { +ksft_print_msg ("a hotplug can end the sched_ext scheduler\n" ); +return KSFT_SKIP ; + } +if (read_file ("/proc/sys/kernel/sched_rt_runtime_us" , buf,sizeof (buf)) || +read_file ("/proc/sys/kernel/sched_rt_period_us" , buf,sizeof (buf))) { +ksft_print_msg ("no sched_rt_runtime_us or sched_rt_period_us\n" ); +return KSFT_SKIP ; + } +read_file ("/proc/sys/kernel/sched_rt_runtime_us" , buf,sizeof (buf)); + rt_runtime =atoll (buf); +read_file ("/proc/sys/kernel/sched_rt_period_us" , buf,sizeof (buf)); + rt_period =atoll (buf); +if (!rt_period ||5 * rt_runtime < rt_period) { +ksft_print_msg ("the admission limit is below a fifth of a CPU\n" ); +return KSFT_SKIP ; + } + runtime =SERVER_PERIOD * rt_runtime / rt_period -SERVER_PERIOD /10 ; +if (server_set (hp_cpu, runtime,SERVER_PERIOD )) { +ksft_print_msg ("the fair server of CPU %d cannot be set\n" , hp_cpu); +return KSFT_SKIP ; + } + n =probe (NULL ); +if (n <2 ) { +ksft_print_msg ("no room for two probe tasks\n" ); +return KSFT_SKIP ; + } +if (hold (n -1 )) { +release_held (); +return KSFT_FAIL ; + } + err =cpu_online (hp_cpu,0 ); +if (err) { +ksft_print_msg ("CPU %d, room for one probe task left: offline refused (%s)\n" , + hp_cpu,strerror (-err)); + ret =KSFT_FAIL ; + }else if (cpu_online (hp_cpu,1 ) ||hold (1 )) { +cpu_up (hp_cpu); + ret =KSFT_FAIL ; + }else { + err =cpu_online (hp_cpu,0 ); +if (err != -EBUSY ) { +ksft_print_msg ("CPU %d, no room left: offline %s, not EBUSY\n" , + hp_cpu, err ?strerror (-err) :"accepted" ); +if (!err) +cpu_up (hp_cpu); + ret =KSFT_FAIL ; + } + } +release_held (); +return ret; +} +/* Return whether @cpu is in @list, a CPU list as sysfs prints it. */ static int in_cpu_list (const char *list,int cpu) {
@@ -6897,6 +6969,485 @@ static int fallback_wake(int fd, pid_t pid)return 0 ; } +/* +* The CPUs of fallback_case() and no_admission_affinity_moves(). I1 and I2 +* are isolated by isolcpus="domain". H and K are housekeeping CPUs: CPUs +* that isolcpus does not isolate. H has a higher number than I2. K is +* another one. +* +* H has a higher number than I2 because of select_fallback_rq(). It +* gives a task that +* must leave its CPU the first CPU of its mask. A task allowed on I2 and +* H then goes to I2, unless the kernel keeps it in its root domain. +* +* fb_i1_hp says whether I1 can go offline. +*/ +static int fb_i1 =-1 , fb_i2 =-1 , fb_h =-1 , fb_k =-1 , fb_i1_hp; + +/* +* Read total_bw of the root domain of the isolated CPUs into @def, and of +* the housekeeping root domain into @hk. Return 0, or -1. +*/ +static int fallback_totals (long long *def,long long *hk) +{ + *def =total_bw_of (fb_i2); + *hk =total_bw_of (fb_h); +if (*def ==-1 || *hk ==-1 ) { +ksft_print_msg ("no total_bw in /sys/kernel/debug/sched/debug\n" ); +return -1 ; + } +return 0 ; +} + +/* +* Find I1, I2, H and K. Take a hotpluggable CPU as I1 if there is one. +* With @offline, I1 must be hotpluggable. Return 0, or -1 if the CPUs do +* not allow the case. fb_k is set only once all four are found. +*/ +static int fallback_cpus (int offline) +{ +int i; + +if (fb_k >=0 ) +return offline && !fb_i1_hp ?-1 :0 ; +/* A failed scan can leave I1, I2 or H set. Scan from scratch. */ + fb_i1 =-1 ; + fb_i2 =-1 ; + fb_h =-1 ; +for (i =0 ; i < nisol; i++) { +if (fb_i1 <0 &&cpu_hotpluggable (isol[i])) + fb_i1 = isol[i]; +else if (fb_i2 <0 ) + fb_i2 = isol[i]; + } + fb_i1_hp = fb_i1 >=0 ; +if (fb_i1 <0 && nisol >=2 ) { + fb_i1 = isol[0 ]; + fb_i2 = isol[1 ]; + } +for (i =0 ; fb_i2 >=0 && i < ncpus && fb_h <0 ; i++) { +if (online[i] > fb_i2) + fb_h = online[i]; + } +for (i =0 ; fb_h >=0 && i < ncpus && fb_k <0 ; i++) { +if (online[i] != fb_h) + fb_k = online[i]; + } +if (fb_i1 >=0 && fb_i2 >=0 && fb_k >=0 ) +return offline && !fb_i1_hp ?-1 :0 ; + fb_k =-1 ; +return -1 ; +} + +enum fb_mode {FB_RUN_I1 ,FB_ON_I1 ,FB_ON_K ,FB_ON_H ,FB_MASK_H ,FB_LAST_H ,FB_PART_H ,FB_STAY_K }; + +/* +* B is a cpuset of I1, I2 and H. C is a cpuset of I1, in B. Neither is a +* partition. A deadline task of 10% is allowed on all CPUs. It sleeps on +* K or on H, and the housekeeping root domain counts it. The case moves +* it, asleep, into C or into B. +* +* In C, the task has I1 only. The move asks the root domain of the +* isolated CPUs for room, and that root domain counts the task. In B, the +* task still has H, a CPU of the housekeeping root domain. That root +* domain goes on counting it. +* +* The case then does one thing, and wakes the task. The table gives the +* cpuset the task is in, the CPU it sleeps on, what the case does, and +* the CPU the task must wake up on: +* +* case in asleep on then wakes on +* hotplug_fallback C I1 I1 goes offline I2 +* hotplug_fallback_running C I1, runs I1 goes offline I2 +* hotplug_fallback_moved C K I1 goes offline I2 +* hotplug_fallback_in_mask C H I1 goes offline I2 +* mask_out_refused C H C is given H only: I1 +* EBUSY +* hotplug_last_refused B H H offline: EBUSY H +* hotplug_keeps_root_domain B H H goes offline K +* partition_takes_refused B H Q takes H: invalid H +* +* hotplug_fallback wakes the task once after the move, so that it sleeps +* on I1. In hotplug_fallback_running, the task then spins on I1 with a +* period of 2ms. It is moved off I1 before the cpuset update of the +* offline, by select_fallback_rq(). The case checks where it runs after +* the offline. hotplug_keeps_root_domain gives B the CPU K as well. In +* partition_takes_refused, Q is a cpuset of H next to B. The case makes +* it a partition root, which must be invalid. +* +* With I1 offline, C has no CPU left, and the task gets the CPUs of B: +* I2 and H. It must wake up on I2, in the root domain that counts it. On +* H, it would run in a root domain that did not admit it. The three +* refusals have one reason: each change would leave the task no CPU in +* the root domain that counts it. +* +* In every case, the root domain of the CPU the task wakes on must count +* it, and the other one must not. The case checks the root domain of that +* CPU, not its number. The reference values of total_bw are taken with no +* task. If the case takes a CPU offline, they are taken with that CPU +* offline. Once the task is gone, total_bw must be +* back to them. The messages give total_bw of the root domain of the +* isolated CPUs first, then of the housekeeping one. +*/ +static int fallback_case (enum fb_mode mode) +{ +int in_b = mode ==FB_LAST_H || mode ==FB_PART_H || mode ==FB_STAY_K ; +int off_cpu; +int on_h = mode ==FB_ON_H || mode ==FB_MASK_H || in_b; +int no_offline = mode ==FB_MASK_H || mode ==FB_LAST_H || mode ==FB_PART_H ; +char cg_q[PATH_MAX ]; +int running = mode ==FB_RUN_I1 ; +int on_i1 = mode ==FB_ON_I1 || running; +char cg_b[PATH_MAX ], cg_c[PATH_MAX ], path[PATH_MAX +32 ], buf[256 ], one[16 ]; +long long def0, hk0, def_off =0 , hk_off =0 , def_ref, hk_ref, def, hk, bw; +int fd[2 ], ret =KSFT_SKIP , added =0 , made =0 , w, at, err; +pid_t pid =-1 ; + +/* hotplug_keeps_root_domain takes H offline, not I1. */ +if (!root[0 ] ||fallback_cpus (!no_offline && mode !=FB_STAY_K )) { +ksft_print_msg ("needs isolcpus=domain: 2 isolated, 2 housekeeping CPUs%s\n" , + no_offline ?"" :", CPU hotplug" ); +return KSFT_SKIP ; + } + off_cpu = mode ==FB_STAY_K ? fb_h : fb_i1; +if ((mode ==FB_LAST_H || mode ==FB_STAY_K ) && !cpu_hotpluggable (fb_h)) { +ksft_print_msg ("CPU %d cannot go offline\n" , fb_h); +return KSFT_SKIP ; + } +snprintf (path,sizeof (path),"%s/cgroup.subtree_control" , root); +if (read_file (path, buf,sizeof (buf))) +return KSFT_SKIP ; +if (!strstr (buf,"cpuset" )) { +if (write_file (root,"cgroup.subtree_control" ,"+cpuset" )) +return KSFT_SKIP ; + added =1 ; + } +if (snprintf (cg_q,sizeof (cg_q),"%s/deadline_bw_q" , root) >= (int )sizeof (cg_q) || +snprintf (cg_b,sizeof (cg_b),"%s/deadline_bw_b" , root) >= (int )sizeof (cg_b) || +snprintf (cg_c,sizeof (cg_c),"%s/c" , cg_b) >= (int )sizeof (cg_c)) + goto out; +if (mkdir (cg_b,0755 ) && errno !=EEXIST ) { +ksft_print_msg ("mkdir %s: %s\n" , cg_b,strerror (errno)); + goto out; + } + made =1 ; +if (mode ==FB_STAY_K ) +snprintf (buf,sizeof (buf),"%d,%d,%d,%d" , fb_i1, fb_i2, fb_h, fb_k); +else +snprintf (buf,sizeof (buf),"%d,%d,%d" , fb_i1, fb_i2, fb_h); +snprintf (one,sizeof (one),"%d" , fb_i1); +if (write_file (cg_b,"cpuset.cpus" , buf) || +write_file (cg_b,"cgroup.subtree_control" ,"+cpuset" ) || + (mkdir (cg_c,0755 ) && errno !=EEXIST ) || +write_file (cg_c,"cpuset.cpus" , one)) { +ksft_print_msg ("cpusets B and C cannot be made\n" ); + goto out; + } +if (fallback_totals (&def0, &hk0)) + goto out; + ret =KSFT_FAIL ; +if (!no_offline) { +if (cpu_online (off_cpu,0 )) { +ksft_print_msg ("offline of CPU %d refused\n" , off_cpu); + goto out; + } +/* H may be the offline CPU: read the housekeeping total_bw on K. */ + def_off =total_bw_of (fb_i2); + hk_off =total_bw_of (fb_k); +if (def_off ==-1 || hk_off ==-1 ) + ret =KSFT_SKIP ; +cpu_up (off_cpu); +if (ret ==KSFT_SKIP ) + goto out; + } + +if (pipe (fd)) { +ksft_print_msg ("pipe: %s\n" ,strerror (errno)); + goto out; + } + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =fork_child ("a deadline task" ); +if (!pid) { +cpu_set_t set; +int i, err; +char c; + +/* Run on K or H, then allow every CPU again, and stay there. */ +close (fd[1 ]); +CPU_ZERO (&set); +CPU_SET (on_h ? fb_h : fb_k, &set); + err =sched_setaffinity (0 ,sizeof (set), &set); +for (i =0 ; i <CPU_SETSIZE ; i++) +CPU_SET (i, &set); +if (err ||sched_setaffinity (0 ,sizeof (set), &set) || + (running ?set_deadline (0 ,1 *NSEC_PER_MSEC ,2 *NSEC_PER_MSEC ) : +set_deadline (0 ,10 *NSEC_PER_MSEC ,PERIOD ))) { + mv_state[0 ] =-1 ; +_exit (1 ); + } + mv_state[0 ] =1 ; +/* hotplug_fallback_running: spin from the first wakeup on. */ +if (running) { +if (read (fd[0 ], &c,1 ) ==1 ) + mv_state[1 ]++; +for (;;) + ; + } +while (read (fd[0 ], &c,1 ) ==1 ) + mv_state[1 ]++; +_exit (0 ); + } +close (fd[0 ]); +if (pid <0 ||child_up ("hotplug_fallback" ,0 )) + goto out_fd; +if (wait_for (1000 ,proc_state_of (pid) =='S' ) ||fallback_totals (&def, &hk)) + goto out_fd; + bw = def + hk - def0 - hk0; +if (bw <=0 ) { +ksft_print_msg ("the deadline task adds %lld to total_bw\n" , bw); + goto out_fd; + } + +/* +* Move it asleep into C, or into B. For hotplug_fallback, wake it +* once so that it runs on I1. +*/ +if (enter (in_b ? cg_b : cg_c, pid) || + (on_i1 && !running &&fallback_wake (fd[1 ], pid))) + goto out_fd; +if (running) { +int n = mv_state[1 ]; + +if (write (fd[1 ],"w" ,1 ) !=1 ||wait_for (1000 , mv_state[1 ] != n) || +wait_for (1000 ,task_cpu_of (pid) == fb_i1)) { +ksft_print_msg ("the deadline task does not run on CPU %d\n" , fb_i1); + goto out_fd; + } + } + at =task_cpu_of (pid); +if (on_i1 ? at != fb_i1 : on_h ? at != fb_h : at == fb_i1) { +ksft_print_msg ("the deadline task sleeps on CPU %d\n" , at); + ret = on_h ?KSFT_SKIP :KSFT_FAIL ; + goto out_fd; + } +/* With no CPU offline, the reference is total_bw with no task. */ + def_ref = no_offline ? def0 : def_off; + hk_ref = no_offline ? hk0 : hk_off; +if (mode ==FB_MASK_H ) { +snprintf (one,sizeof (one),"%d" , fb_h); + err =write_file (cg_c,"cpuset.cpus" , one); +if (err != -EBUSY ) { +ksft_print_msg ("C given CPU %d only: %s, not EBUSY\n" , fb_h, + err ?strerror (-err) :"accepted" ); + goto out_fd; + } + }else if (mode ==FB_LAST_H ) { + err =cpu_online (fb_h,0 ); +if (err != -EBUSY ) { +ksft_print_msg ("offline of CPU %d: %s, not EBUSY\n" , fb_h, + err ?strerror (-err) :"accepted" ); +cpu_up (fb_h); + goto out_fd; + } + }else if (mode ==FB_PART_H ) { +snprintf (one,sizeof (one),"%d" , fb_h); +if ((mkdir (cg_q,0755 ) && errno !=EEXIST ) || +write_file (cg_q,"cpuset.cpus" , one) || +write_file (cg_q,"cpuset.cpus.partition" ,"root" )) { +ksft_print_msg ("cpuset Q cannot be made\n" ); + goto out_fd; + } +snprintf (path,sizeof (path),"%s/cpuset.cpus.partition" , cg_q); +if (read_file (path, buf,sizeof (buf)) || !strstr (buf,"Deadline task" )) { +ksft_print_msg ("Q takes CPU %d from B: %s" , fb_h, buf); + goto out_fd; + } + }else if (cpu_online (off_cpu,0 )) { +ksft_print_msg ("offline of CPU %d with the task asleep refused\n" , off_cpu); + goto out_fd; + } +if (running) +sleep_ns (PERIOD ); +else if (fallback_wake (fd[1 ], pid)) + goto out_up; + def =total_bw_of (fb_i2); + hk =total_bw_of (fb_k); +if (def ==-1 || hk ==-1 ) + goto out_up; + w =task_cpu_of (pid); + ret =KSFT_PASS ; +if (in_cpu_list (isolated, w) ? def != def_ref + bw || hk != hk_ref : + hk != hk_ref + bw || def != def_ref) { +ksft_print_msg ("woken on CPU %d: total_bw %lld, %lld, not %lld, %lld\n" , + w, def, hk, +in_cpu_list (isolated, w) ? def_ref + bw : def_ref, +in_cpu_list (isolated, w) ? hk_ref : hk_ref + bw); + ret =KSFT_FAIL ; + }else if (in_cpu_list (isolated, w) == in_b) { +ksft_print_msg ("woken on CPU %d: out of the root domain that counted it\n" , + w); + ret =KSFT_FAIL ; + } +reap (pid); + pid =-1 ; +sleep_ns (3 *PERIOD ); + def =total_bw_of (fb_i2); + hk =total_bw_of (fb_k); +if (def != def_ref || hk != hk_ref) { +ksft_print_msg ("task gone: total_bw %lld, %lld, not %lld, %lld\n" , + def, hk, def_ref, hk_ref); + ret =KSFT_FAIL ; + } +out_up: +reap (pid); +if (!no_offline) +cpu_up (off_cpu); + pid =-1 ; +out_fd: +reap (pid); +close (fd[1 ]); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS && (fallback_totals (&def, &hk) || def != def0 || hk != hk0)) { +ksft_print_msg ("task gone, CPU %d online: total_bw %lld, %lld, not %lld, %lld\n" , + off_cpu, def, hk, def0, hk0); + ret =KSFT_FAIL ; + } +out: +if (made) { +if (rmdir (cg_q) && errno !=ENOENT ) +ksft_print_msg ("rmdir %s: %s\n" , cg_q,strerror (errno)); +if (wait_for (1000 , !rmdir (cg_c) || errno ==ENOENT ) || +wait_for (1000 , !rmdir (cg_b) || errno ==ENOENT )) +ksft_print_msg ("rmdir %s: %s\n" , cg_b,strerror (errno)); + } +if (added &&write_file (root,"cgroup.subtree_control" ,"-cpuset" )) +ksft_print_msg ("the cpuset controller stays enabled\n" ); +return ret; +} + +static int hotplug_fallback_running (void ) +{ +return fallback_case (FB_RUN_I1 ); +} + +static int hotplug_fallback (void ) +{ +return fallback_case (FB_ON_I1 ); +} + +static int hotplug_fallback_moved (void ) +{ +return fallback_case (FB_ON_K ); +} + +static int hotplug_fallback_in_mask (void ) +{ +return fallback_case (FB_ON_H ); +} + +static int mask_out_refused (void ) +{ +return fallback_case (FB_MASK_H ); +} + +static int hotplug_last_refused (void ) +{ +return fallback_case (FB_LAST_H ); +} + +static int hotplug_keeps_root_domain (void ) +{ +return fallback_case (FB_STAY_K ); +} + +static int partition_takes_refused (void ) +{ +return fallback_case (FB_PART_H ); +} + +/* +* Turn admission control off. A task pins itself to H, becomes a deadline +* task of 10% and spins. The housekeeping root domain counts it. The case +* then sets its affinity to I2 with sched_setaffinity(). Nothing reserves +* room for it: its bandwidth must follow it to the root domain of the +* isolated CPUs. Once the task is gone and admission control is on again, +* total_bw must be what it was. +*/ +static int no_admission_affinity_moves (void ) +{ +long long def0, hk0, def, hk, bw; +int ret =KSFT_FAIL , at; +char saved[64 ]; +cpu_set_t set; +pid_t pid; + +if (fallback_cpus (0 )) { +ksft_print_msg ("needs isolcpus=domain: 2 isolated, 2 housekeeping CPUs\n" ); +return KSFT_SKIP ; + } +if (fallback_totals (&def0, &hk0)) +return KSFT_SKIP ; +if (read_file ("/proc/sys/kernel/sched_rt_runtime_us" , saved,sizeof (saved)) || +write_file ("/proc/sys/kernel" ,"sched_rt_runtime_us" ,"-1" )) { +ksft_print_msg ("admission control cannot be turned off\n" ); +return KSFT_SKIP ; + } + mv_state[0 ] =0 ; + pid =fork_child ("a deadline task" ); +if (!pid) { +CPU_ZERO (&set); +CPU_SET (fb_h, &set); +if (sched_setaffinity (0 ,sizeof (set), &set) || +set_deadline (0 ,10 *NSEC_PER_MSEC ,PERIOD )) { + mv_state[0 ] =-1 ; +_exit (1 ); + } + mv_state[0 ] =1 ; +for (;;) +spin_ns (NSEC_PER_MSEC ); + } +if (pid <0 ||child_up ("no_admission_affinity_moves" ,0 )) + goto out; +sleep_ns (PERIOD ); +if (fallback_totals (&def, &hk)) + goto out; + bw = hk - hk0; +if (bw <=0 || def != def0) { +ksft_print_msg ("the deadline task adds %lld to total_bw of its root domain\n" , bw); + goto out; + } +CPU_ZERO (&set); +CPU_SET (fb_i2, &set); +if (sched_setaffinity (pid,sizeof (set), &set)) { +ksft_print_msg ("sched_setaffinity to CPU %d: %s\n" , fb_i2,strerror (errno)); + goto out; + } +sleep_ns (2 *PERIOD ); + at =task_cpu_of (pid); +if (at != fb_i2 ||fallback_totals (&def, &hk)) + goto out; + ret =KSFT_PASS ; +if (def != def0 + bw || hk != hk0) { +ksft_print_msg ("on CPU %d: total_bw %lld, %lld, not %lld, %lld\n" , + at, def, hk, def0 + bw, hk0); + ret =KSFT_FAIL ; + } +out: +reap (pid); +if (write_file ("/proc/sys/kernel" ,"sched_rt_runtime_us" , saved)) { +ksft_print_msg ("admission control cannot be turned on again\n" ); +return KSFT_FAIL ; + } +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS && (fallback_totals (&def, &hk) || def != def0 || hk != hk0)) { +ksft_print_msg ("task gone: total_bw %lld, %lld, not %lld, %lld\n" , + def, hk, def0, hk0); + ret =KSFT_FAIL ; + } +return ret; +} +/* * The cpusets of the cases below: B and Q in the root cgroup, C in B. A * case makes those it needs. cs_setup() names them and enables the cpuset
@@ -7144,6 +7695,1779 @@ static int move_back_cpu_offline(void)return ret; } +enum sp_mode {SP_HK_10 ,SP_HK_WAKING ,SP_ISO_WAKING }; + +/* +* Return a CPU other than @not: an isolated one if @side, else a +* housekeeping one. Return -1 if there is none. +*/ +static int other_cpu (int side,int not) +{ +int n = side ? nisol : ncpus, *cpus = side ? isol : online, i; + +for (i = n -1 ; i >=0 ; i--) { +if (cpus[i] != not) +return cpus[i]; + } +return -1 ; +} + +/* +* A deadline task loses its CPU to an offline. It is in the root cgroup, +* and allowed on every CPU: its mask holds CPUs of the two root domains. +* It must stay in the root domain that counts it. The steps: +* 1. A task runs on the last housekeeping CPU that can go offline. Then +* it allows every CPU and becomes a deadline task, of 10% for +* SP_HK_10, of 90% for SP_HK_WAKING. The housekeeping root domain +* counts it. +* For SP_ISO_WAKING, the task runs on the last isolated CPU that can +* go offline. Then it allows the isolated CPUs only, and becomes a +* deadline task of 90%. The root domain of the isolated CPUs counts +* it. Then it allows every CPU. +* The task of SP_HK_10 spins. The others spin 50us and sleep 50us, in +* a loop. +* 2. Wait a period. X is the CPU the task runs on. +* 3. Take X offline. +* 4. Wait two periods. The task must run on a CPU of its root domain. +* That root domain must still count it. +* The task of SP_HK_10 is throttled most of the time. Its replenishment +* timer then finds its CPU offline, and picks a CPU for it. The other +* tasks wake up while the offline goes on, and the wakeup picks a CPU +* for them. +* +* A kernel that does not prefer the root domain of the task at a wakeup +* gives it the first CPU of its mask. The waking cases fail on such a +* kernel only if that CPU is of the other kind. SP_HK_WAKING needs an +* isolated CPU first, SP_ISO_WAKING a housekeeping CPU first. +* +* The kernel records with the task the CPU where it became a deadline +* task. The waking cases need X to be that CPU: a wakeup in the offline +* looks at the root domain of that CPU. SP_HK_10 takes any housekeeping +* CPU as X. +* +* Do it in rounds, each with a new task. Stop once 3 rounds check +* something, or after 10 rounds. A round checks nothing if X cannot go +* offline, or for a waking case, if X is not the CPU recorded. +* +* The references of total_bw are taken with no task: with X offline +* after the task is gone, and with every CPU online. The messages give +* total_bw of the root domain of the isolated CPUs first, then of the +* housekeeping one. +*/ +static int spin_offline (enum sp_mode mode) +{ +uint64_t runtime = (mode ==SP_HK_10 ?10 :90 ) *NSEC_PER_MSEC ; +int iso = mode ==SP_ISO_WAKING , start =-1 , rounds =0 , round; +long long def0, hk0, def, hk, ref_def, ref_hk, bw; +int x, d, h, cpu, i, down, ret =KSFT_SKIP ; +pid_t pid; + +for (i =0 ; i < (iso ? nisol : ncpus); i++) { +if (cpu_hotpluggable (iso ? isol[i] : online[i])) + start = iso ? isol[i] : online[i]; + } +if (!nisol || (iso && nisol <2 ) || start <0 ) { +ksft_print_msg ("needs isolcpus=domain: %d isolated CPUs, one hotpluggable\n" , + iso ?2 :1 ); +return KSFT_SKIP ; + } +if (totals_of (isol[0 ], online[0 ], &def0, &hk0)) +return KSFT_SKIP ; + ret =KSFT_PASS ; +for (round =0 ; round <10 && rounds <3 && ret ==KSFT_PASS ; round++) { + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =fork_child ("a spinning deadline task" ); +if (!pid) { +cpu_set_t set; + +CPU_ZERO (&set); +CPU_SET (start, &set); +if (sched_setaffinity (0 ,sizeof (set), &set)) +_exit (1 ); +spin_ns (NSEC_PER_MSEC ); +for (i =0 ; i < (iso ? nisol :CPU_SETSIZE ); i++) +CPU_SET (iso ? isol[i] : i, &set); +if (sched_setaffinity (0 ,sizeof (set), &set) || +set_deadline (0 , runtime,PERIOD )) { + mv_state[0 ] =-1 ; +_exit (1 ); + } +for (i =0 ; i <CPU_SETSIZE ; i++) +CPU_SET (i, &set); +if (sched_setaffinity (0 ,sizeof (set), &set)) { + mv_state[0 ] =-1 ; +_exit (1 ); + } + mv_state[0 ] =1 ; +for (;;) { + mv_state[1 ]++; +if (mode ==SP_HK_10 ) +continue ; +spin_ns (50 *NSEC_PER_USEC ); +sleep_ns (50 *NSEC_PER_USEC ); + } + } + ret =KSFT_FAIL ; + down =0 ; +if (pid <0 ||child_up ("spin_offline" ,0 )) + goto next; +sleep_ns (PERIOD ); + x =task_cpu_of (pid); +if (mode ==SP_HK_10 ?in_cpu_list (isolated, x) || !cpu_hotpluggable (x) : + x != start) { + ret =KSFT_PASS ;/* check nothing */ + goto next; + } + d =other_cpu (1 , x); + h =other_cpu (0 , x); +if (totals_of (d, h, &def, &hk)) + goto next; + bw = iso ? def - def0 : hk - hk0; +if (bw <=0 || (iso ? hk != hk0 : def != def0)) { +ksft_print_msg ("on CPU %d, the deadline task adds %lld, %lld to total_bw\n" , +task_cpu_of (pid), def - def0, hk - hk0); + goto next; + } +if (task_cpu_of (pid) != x) { + ret =KSFT_PASS ;/* moved off X: check nothing */ + goto next; + } + rounds++; +if (cpu_online (x,0 )) { +ksft_print_msg ("offline of CPU %d with the task on it refused\n" , x); + goto next; + } + down =1 ; +sleep_ns (2 *PERIOD ); + cpu =task_cpu_of (pid); +if (totals_of (d, h, &def, &hk)) + goto next; +reap (pid); + pid =-1 ; +sleep_ns (3 *PERIOD ); +if (totals_of (d, h, &ref_def, &ref_hk)) + goto next; +if (in_cpu_list (isolated, cpu) != iso) { +ksft_print_msg ("round %d: CPU %d offline, the task runs on CPU %d\n" , + round, x, cpu); + goto next; + } +if (iso ? def != ref_def + bw || hk != ref_hk : + hk != ref_hk + bw || def != ref_def) { +ksft_print_msg ("round %d: on CPU %d: total_bw %lld, %lld, not %lld, %lld\n" , + round, cpu, def, hk, iso ? ref_def + bw : ref_def, + iso ? ref_hk : ref_hk + bw); + goto next; + } + ret =KSFT_PASS ; +next: +reap (pid); +if (down) +cpu_up (x); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =totals_check ("task gone" , isol[0 ], online[0 ], def0, hk0); + } +if (ret ==KSFT_PASS && !rounds) { +ksft_print_msg ("the task never ran on a CPU that can go offline\n" ); + ret =KSFT_SKIP ; + } +return ret; +} + +static int hotplug_spinner_keeps (void ) +{ +return spin_offline (SP_HK_10 ); +} + +static int hotplug_waking_keeps (void ) +{ +return spin_offline (SP_HK_WAKING ); +} + +static int hotplug_isolated_keeps (void ) +{ +return spin_offline (SP_ISO_WAKING ); +} + +/* +* A rebuild of the sched domains must keep a deadline task in the root +* domain that counts it. The task wakes up often, so that a wakeup can +* land inside a rebuild. The steps: +* 1. A task runs on H, the highest housekeeping CPU. Then it allows every +* CPU and becomes a deadline task of 90%. The housekeeping root domain +* counts it. It spins 50us and sleeps 50us, in a loop. +* 2. Wait a period. The task must run on a housekeeping CPU. +* 3. Make Q a partition root of another housekeeping CPU, and remove it. +* Each of the two steps rebuilds the sched domains. +* 4. The task must run on a housekeeping CPU. The housekeeping root +* domain must still count it, and the other one must not. +* A kernel that gets this wrong gives the task the first CPU of the root +* domain of the isolated CPUs. That root domain then counts it, with no +* admission test. This shows only if that first CPU is isolated, as with +* isolcpus=domain,0,1. The case skips otherwise. +* +* Do it in 20 rounds, each with a new task. The first rebuild of a new +* task is the one most likely to go wrong. The case is not deterministic. +* The messages give total_bw of the root domain of the isolated CPUs +* first, then of the housekeeping one. +*/ +static int rebuild_waking_keeps (void ) +{ +int h =top_cpu (1 ), q =other_cpu (0 ,top_cpu (1 )), d = nisol ? isol[0 ] :-1 ; +long long def0, hk0, def, hk; +int round, cpu, ret; +pid_t pid; + +if (d <0 || d > online[0 ] || q <0 ) { +ksft_print_msg ("needs isolcpus=domain with the first CPU isolated, as with 0,1\n" ); +return KSFT_SKIP ; + } +if (cs_setup ()) +return KSFT_SKIP ; + ret =KSFT_SKIP ; +if (totals_of (d, h, &def0, &hk0)) + goto out; + ret =KSFT_PASS ; +for (round =0 ; round <20 && ret ==KSFT_PASS ; round++) { + mv_state[0 ] =0 ; + pid =fork_child ("a waking deadline task" ); +if (!pid) { +cpu_set_t set; +int i; + +CPU_ZERO (&set); +CPU_SET (h, &set); +if (sched_setaffinity (0 ,sizeof (set), &set)) +_exit (1 ); +spin_ns (NSEC_PER_MSEC ); +for (i =0 ; i <CPU_SETSIZE ; i++) +CPU_SET (i, &set); +if (sched_setaffinity (0 ,sizeof (set), &set) || +set_deadline (0 ,90 *NSEC_PER_MSEC ,PERIOD )) { + mv_state[0 ] =-1 ; +_exit (1 ); + } + mv_state[0 ] =1 ; +for (;;) { +spin_ns (50 *NSEC_PER_USEC ); +sleep_ns (50 *NSEC_PER_USEC ); + } + } + ret =KSFT_FAIL ; +if (pid <0 ||child_up ("rebuild_waking_keeps" ,0 )) + goto next; +sleep_ns (PERIOD ); + cpu =task_cpu_of (pid); +if (totals_of (d, h, &def, &hk)) + goto next; +if (in_cpu_list (isolated, cpu) || def != def0 || hk <= hk0) { +ksft_print_msg ("round %d: on CPU %d before the rebuild: total_bw %lld, %lld\n" , + round, cpu, def, hk); + goto next; + } +if (make_partition (cs_q, q)) { +ksft_print_msg ("no partition root Q of CPU %d\n" , q); + goto next; + } +remove_partition (cs_q); + cpu =task_cpu_of (pid); +if (totals_check ("after the rebuild" , d, h, def, hk) !=KSFT_PASS ) +ksft_print_msg ("round %d: the task runs on CPU %d\n" , round, cpu); +else if (in_cpu_list (isolated, cpu)) +ksft_print_msg ("round %d: after the rebuild, the task runs on CPU %d\n" , + round, cpu); +else + ret =KSFT_PASS ; +next: +remove_partition (cs_q); +reap (pid); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =totals_check ("task gone" , d, h, def0, hk0); + } +out: +cs_cleanup (); +return ret; +} + +/* +* A user affinity can keep a task out of the root domain that counts +* it. The CPUs are those of fallback_case(). B is a cpuset of I1, I2, H +* and K. C, in B, is a cpuset of H. Neither is a partition. The steps: +* 1. A task in the root cgroup allows I1 and I2 only. It becomes a +* deadline task of 10%. The root domain of the isolated CPUs counts +* it. +* 2. It sets its affinity to I1, I2 and H. That holds the root domain +* that counts it, so it is accepted. It sleeps. +* 3. Move it asleep into C. It has H only. The housekeeping root domain +* counts it now. Wake it: it runs on H. +* 4. Take H offline. C has no CPU left, and the task would get the CPUs +* of B: I1, I2 and K. Its affinity keeps I1 and I2 of them. That holds +* no CPU of the root domain that counts it. +* The offline must fail with EBUSY. The task must still sleep on H, and +* total_bw must not change. Wake it: it must run on H. +*/ +static int hotplug_user_mask_refused (void ) +{ +int fd[2 ] = {-1 ,-1 }, err, cpu, ret =KSFT_SKIP ; +long long def0, hk0, def, hk, bw; +char buf[64 ]; +pid_t pid =-1 ; + +if (fallback_cpus (0 ) || !cpu_hotpluggable (fb_h)) { +ksft_print_msg ("needs isolcpus=domain: 2 isolated, 2 housekeeping CPUs, hotplug\n" ); +return KSFT_SKIP ; + } +if (cs_setup ()) +return KSFT_SKIP ; +snprintf (buf,sizeof (buf),"%d,%d,%d,%d" , fb_i1, fb_i2, fb_h, fb_k); +if (cs_make (cs_b, buf) ||write_file (cs_b,"cgroup.subtree_control" ,"+cpuset" )) + goto out; +snprintf (buf,sizeof (buf),"%d" , fb_h); +if (cs_make (cs_c, buf) ||fallback_totals (&def0, &hk0) ||pipe (fd)) + goto out; + ret =KSFT_FAIL ; + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =fork_child ("a deadline task" ); +if (!pid) { +cpu_set_t set; +char c; + +CPU_ZERO (&set); +CPU_SET (fb_i1, &set); +CPU_SET (fb_i2, &set); +if (sched_setaffinity (0 ,sizeof (set), &set) || +set_deadline (0 ,10 *NSEC_PER_MSEC ,PERIOD )) { + mv_state[0 ] =-1 ; +_exit (1 ); + } +CPU_SET (fb_h, &set); +if (sched_setaffinity (0 ,sizeof (set), &set)) { + mv_state[0 ] =-1 ; +_exit (1 ); + } + mv_state[0 ] =1 ; +while (read (fd[0 ], &c,1 ) ==1 ) + mv_state[1 ]++; +_exit (0 ); + } +if (pid <0 ||child_up ("hotplug_user_mask_refused" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' ) ||fallback_totals (&def, &hk)) + goto out; + bw = def - def0; +if (bw <=0 || hk != hk0) { +ksft_print_msg ("the deadline task adds %lld, %lld to total_bw\n" , bw, hk - hk0); + goto out; + } + err =enter (cs_c, pid); +if (err) { +ksft_print_msg ("a move into C refused: %s\n" ,strerror (-err)); + goto out; + } +if (fallback_wake (fd[1 ], pid) || +totals_check ("in C" , fb_i2, fb_h, def0, hk0 + bw)) + goto out; + cpu =task_cpu_of (pid); +if (cpu != fb_h) { +ksft_print_msg ("woken in C on CPU %d, not %d\n" , cpu, fb_h); + goto out; + } + err =cpu_online (fb_h,0 ); +if (err != -EBUSY ) { +ksft_print_msg ("offline of CPU %d: %s, not EBUSY\n" , fb_h, + err ?strerror (-err) :"accepted" ); +if (!err) { +if (!fallback_wake (fd[1 ], pid)) +ksft_print_msg ("woken on CPU %d\n" ,task_cpu_of (pid)); +cpu_up (fb_h); + } + goto out; + } + cpu =task_cpu_of (pid); +if (cpu != fb_h ||proc_state_of (pid) !='S' ) { +ksft_print_msg ("offline refused: the task is on CPU %d, state %c\n" , + cpu,proc_state_of (pid)); + goto out; + } +if (totals_check ("offline refused" , fb_i2, fb_h, def0, hk0 + bw) || +fallback_wake (fd[1 ], pid)) + goto out; + cpu =task_cpu_of (pid); +if (cpu != fb_h) { +ksft_print_msg ("woken on CPU %d, not %d\n" , cpu, fb_h); + goto out; + } + ret =totals_check ("woken" , fb_i2, fb_h, def0, hk0 + bw); +out: +reap (pid); +if (fd[0 ] >=0 ) { +close (fd[0 ]); +close (fd[1 ]); + } +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =totals_check ("task gone" , fb_i2, fb_h, def0, hk0); +cs_cleanup (); +return ret; +} + +/* +* cgroup v2 works out the CPUs of each cpuset from the top down, at an +* offline too. A cpuset left with no CPU takes all those of its parent. +* Its child then keeps those that its own cpuset.cpus holds. The CPUs are +* those of fallback_case(). B, in the root cgroup, has cpuset.cpus H. C, +* in B, has cpuset.cpus I1, I2 and H. Neither is a partition. C gets H. +* The steps: +* 1. A task in the root cgroup runs on H. Then it allows every CPU and +* becomes a deadline task of 10%. It sleeps. The housekeeping root +* domain counts it. +* 2. Move it asleep into C. It keeps H. +* 3. Take H offline. B has no CPU left, and gets all the CPUs of the root +* cgroup. C keeps I1 and I2 of them: no CPU of the root domain that +* counts the task. The root cgroup itself still has K. The kernel must +* work out the CPUs of C from those of B, not from those of the root +* cgroup. +* The offline must fail with EBUSY. The task must still sleep on H, and +* total_bw must not change. Wake it: it must run on H. +*/ +static int hotplug_nested_refused (void ) +{ +int fd[2 ] = {-1 ,-1 }, err, cpu, ret =KSFT_SKIP ; +long long def0, hk0, def, hk, bw; +char buf[64 ]; +pid_t pid =-1 ; + +if (fallback_cpus (0 ) || !cpu_hotpluggable (fb_h)) { +ksft_print_msg ("needs isolcpus=domain: 2 isolated, 2 housekeeping CPUs, hotplug\n" ); +return KSFT_SKIP ; + } +if (cs_setup ()) +return KSFT_SKIP ; +snprintf (buf,sizeof (buf),"%d" , fb_h); +if (cs_make (cs_b, buf) || +write_file (cs_b,"cgroup.subtree_control" ,"+cpuset" )) + goto out; +snprintf (buf,sizeof (buf),"%d,%d,%d" , fb_i1, fb_i2, fb_h); +if (cs_make (cs_c, buf) ||fallback_totals (&def0, &hk0) ||pipe (fd)) + goto out; + ret =KSFT_FAIL ; + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =task_from (fb_h,1 ,10 *NSEC_PER_MSEC ,PERIOD , fd[0 ]); +if (pid <0 ||child_up ("hotplug_nested_refused" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' ) || +fallback_totals (&def, &hk)) + goto out; + bw = hk - hk0; + err =enter (cs_c, pid); + cpu =task_cpu_of (pid); +if (bw <=0 || def != def0 || err || cpu != fb_h) { +ksft_print_msg ("a deadline task adds %lld, %lld to total_bw, in C: %s, on CPU %d\n" , + def - def0, bw,strerror (-err), cpu); + goto out; + } +if (totals_check ("in C" , fb_i2, fb_h, def0, hk0 + bw)) + goto out; + err =cpu_online (fb_h,0 ); +if (err != -EBUSY ) { +ksft_print_msg ("offline of CPU %d: %s, not EBUSY\n" , fb_h, + err ?strerror (-err) :"accepted" ); +if (!err) { +if (!fallback_wake (fd[1 ], pid)) +ksft_print_msg ("woken on CPU %d\n" , +task_cpu_of (pid)); +cpu_up (fb_h); + } + goto out; + } + cpu =task_cpu_of (pid); +if (cpu != fb_h ||proc_state_of (pid) !='S' ) { +ksft_print_msg ("offline refused: the task is on CPU %d, state %c\n" , + cpu,proc_state_of (pid)); + goto out; + } +if (totals_check ("offline refused" , fb_i2, fb_h, def0, hk0 + bw) || +fallback_wake (fd[1 ], pid)) + goto out; + cpu =task_cpu_of (pid); +if (cpu != fb_h) { +ksft_print_msg ("woken on CPU %d, not %d\n" , cpu, fb_h); + goto out; + } + ret =totals_check ("woken" , fb_i2, fb_h, def0, hk0 + bw); +out: +reap (pid); +if (fd[0 ] >=0 ) { +close (fd[0 ]); +close (fd[1 ]); + } +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =totals_check ("task gone" , fb_i2, fb_h, def0, hk0); +cs_cleanup (); +return ret; +} + +/* +* The CPUs of the two cases below. X, Y and Z are the three highest +* online CPUs: CPUs that isolcpus="domain" does not isolate. W, the +* lowest one, stays with the root cgroup. Return 0, or -1 if they are +* missing or X cannot go offline. +*/ +static int pc_x, pc_y, pc_z, pc_w; + +static int partition_cpus (void ) +{ +if (ncpus <4 || !cpu_hotpluggable (top_cpu (3 ))) { +ksft_print_msg ("needs 4 CPUs, CPU hotplug\n" ); +return -1 ; + } + pc_x =top_cpu (3 ); + pc_y =top_cpu (2 ); + pc_z =top_cpu (1 ); + pc_w = online[0 ]; +return 0 ; +} + +/* +* A partition root left with no CPU of its own takes the CPUs of a child +* partition, if it holds a task. The child partition becomes invalid. B +* is an isolated partition of X, Y and Z. C, in B, is an isolated +* partition of Y and Z. So B keeps X only. The steps: +* 1. Take X offline and back with no task, to read the total_bw of each +* root domain without X. +* 2. A task in the root cgroup runs on W. Then it allows every CPU and +* becomes a deadline task of 10%. It sleeps. The housekeeping root +* domain counts it. +* 3. Move it asleep into B. It has X only. The default root domain, which +* spans the CPUs of isolated partitions, counts it. +* 4. Take X offline. B takes Y and Z, and C becomes invalid. The task gets +* Y and Z: CPUs of the same root domain. +* The offline must be accepted. The default root domain must still count +* the task. Wake it: it must run on Y or Z. +*/ +static int hotplug_child_partition_kept (void ) +{ +int fd[2 ] = {-1 ,-1 }, err, cpu, ret =KSFT_SKIP , x_off =0 ; +long long def0, hk0, def_off, hk_off, def, hk, bw; +char buf[64 ]; +pid_t pid =-1 ; + +if (partition_cpus () ||cs_setup ()) +return KSFT_SKIP ; +snprintf (buf,sizeof (buf),"%d,%d,%d" , pc_x, pc_y, pc_z); +if (cs_make (cs_b, buf) || +write_file (cs_b,"cpuset.cpus.partition" ,"isolated" ) || +write_file (cs_b,"cgroup.subtree_control" ,"+cpuset" )) + goto out; +snprintf (buf,sizeof (buf),"%d,%d" , pc_y, pc_z); +if (cs_make (cs_c, buf) || +write_file (cs_c,"cpuset.cpus.partition" ,"isolated" ) || + !partition_has (cs_b,"isolated" ,1 ) ||partition_has (cs_b,"invalid" ,0 ) || + !partition_has (cs_c,"isolated" ,1 ) ||partition_has (cs_c,"invalid" ,0 )) + goto out; +if (totals_of (pc_y, pc_w, &def0, &hk0)) + goto out; + err =cpu_online (pc_x,0 ); +if (err) { +ksft_print_msg ("offline of CPU %d with no task: %s\n" , pc_x,strerror (-err)); + goto out; + } + err =totals_of (pc_y, pc_w, &def_off, &hk_off); +cpu_up (pc_x); +if (err ||pipe (fd)) + goto out; + ret =KSFT_FAIL ; + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =task_from (pc_w,1 ,10 *NSEC_PER_MSEC ,PERIOD , fd[0 ]); +if (pid <0 ||child_up ("hotplug_child_partition_kept" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' ) ||totals_of (pc_y, pc_w, &def, &hk)) + goto out; + bw = hk - hk0; + err =enter (cs_b, pid); +if (bw <=0 || def != def0 || err) { +ksft_print_msg ("a deadline task adds %lld, %lld to total_bw, in B: %s\n" , + def - def0, bw,strerror (-err)); + goto out; + } +if (totals_check ("in B" , pc_y, pc_w, def0 + bw, hk0) ||fallback_wake (fd[1 ], pid)) + goto out; + cpu =task_cpu_of (pid); +if (cpu != pc_x) { +ksft_print_msg ("woken in B on CPU %d, not %d\n" , cpu, pc_x); + goto out; + } + err =cpu_online (pc_x,0 ); +if (err) { +ksft_print_msg ("offline of CPU %d: %s, not accepted\n" , pc_x,strerror (-err)); + goto out; + } + x_off =1 ; +if (!partition_has (cs_c,"invalid" ,1 ) || +totals_check ("X offline" , pc_y, pc_w, def_off + bw, hk_off) || +fallback_wake (fd[1 ], pid)) + goto out; + cpu =task_cpu_of (pid); +if (cpu != pc_y && cpu != pc_z) { +ksft_print_msg ("woken on CPU %d, not %d or %d\n" , cpu, pc_y, pc_z); + goto out; + } + ret =KSFT_PASS ; +out: +reap (pid); +if (fd[0 ] >=0 ) { +close (fd[0 ]); +close (fd[1 ]); + } +if (x_off) +cpu_up (pc_x); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =totals_check ("task gone" , pc_y, pc_w, def0, hk0); +cs_cleanup (); +return ret; +} + +/* +* The same rule, with a child partition of another kind. B is a root +* partition of X and Y. C, in B, is an isolated partition of Y. So B keeps +* X only, in a root domain of its own. N, in B, is a cpuset that is not a +* partition. The steps: +* 1. A task that is not a deadline task sleeps in N. +* 2. A task in the root cgroup runs on W. Then it allows every CPU and +* becomes a deadline task of 10%. It sleeps. The housekeeping root +* domain counts it. +* 3. Move it asleep into C. The default root domain counts it. +* 4. Take X offline. B would take Y, and C would become invalid. Y would +* leave the default root domain for that of B, with the deadline task. +* That root domain did not admit it. +* The offline must fail with EBUSY. C must stay valid, and total_bw must +* not change. Wake the task: it must run on Y. Once the task is gone, the +* offline must be accepted. +*/ +static int hotplug_partition_invalid_refused (void ) +{ +int fd[2 ] = {-1 ,-1 }, err, cpu, ret =KSFT_SKIP , x_off =0 ; +long long def0, hk0, def, hk, bw; +char buf[64 ], cs_n[PATH_MAX ]; +pid_t pid =-1 , other =-1 ; + +if (partition_cpus () ||cs_setup ()) +return KSFT_SKIP ; +if (snprintf (cs_n,sizeof (cs_n),"%s/n" , cs_b) >= (int )sizeof (cs_n)) + goto out; +snprintf (buf,sizeof (buf),"%d,%d" , pc_x, pc_y); +if (cs_make (cs_b, buf) || +write_file (cs_b,"cpuset.cpus.partition" ,"root" ) || +write_file (cs_b,"cgroup.subtree_control" ,"+cpuset" )) + goto out; +snprintf (buf,sizeof (buf),"%d" , pc_y); +if (cs_make (cs_c, buf) || +write_file (cs_c,"cpuset.cpus.partition" ,"isolated" ) || + !partition_has (cs_b,"root" ,1 ) ||partition_has (cs_b,"invalid" ,0 ) || + !partition_has (cs_c,"isolated" ,1 ) ||partition_has (cs_c,"invalid" ,0 )) + goto out; +if (mkdir (cs_n,0755 ) && errno !=EEXIST ) { +ksft_print_msg ("mkdir %s: %s\n" , cs_n,strerror (errno)); + goto out; + } + other =fork_child ("a task in N" ); +if (!other) { +for (;;) +pause (); + } +if (other <0 ||enter (cs_n, other) ||totals_of (pc_y, pc_w, &def0, &hk0) || +pipe (fd)) + goto out; + ret =KSFT_FAIL ; + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =task_from (pc_w,1 ,10 *NSEC_PER_MSEC ,PERIOD , fd[0 ]); +if (pid <0 ||child_up ("hotplug_partition_invalid_refused" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' ) ||totals_of (pc_y, pc_w, &def, &hk)) + goto out; + bw = hk - hk0; + err =enter (cs_c, pid); +if (bw <=0 || def != def0 || err) { +ksft_print_msg ("a deadline task adds %lld, %lld to total_bw, in C: %s\n" , + def - def0, bw,strerror (-err)); + goto out; + } +if (totals_check ("in C" , pc_y, pc_w, def0 + bw, hk0)) + goto out; + err =cpu_online (pc_x,0 ); +if (err != -EBUSY ) { +ksft_print_msg ("offline of CPU %d: %s, not EBUSY\n" , pc_x, + err ?strerror (-err) :"accepted" ); + x_off = !err; + goto out; + } +if (partition_has (cs_c,"invalid" ,0 ) || +totals_check ("offline refused" , pc_y, pc_w, def0 + bw, hk0) || +fallback_wake (fd[1 ], pid)) + goto out; + cpu =task_cpu_of (pid); +if (cpu != pc_y) { +ksft_print_msg ("woken on CPU %d, not %d\n" , cpu, pc_y); + goto out; + } +reap (pid); + pid =-1 ; +sleep_ns (3 *PERIOD ); + err =cpu_online (pc_x,0 ); +if (err) { +ksft_print_msg ("offline of CPU %d with the task gone: %s\n" , pc_x, +strerror (-err)); + goto out; + } + x_off =1 ; + ret =KSFT_PASS ; +out: +reap (pid); +reap (other); +if (fd[0 ] >=0 ) { +close (fd[0 ]); +close (fd[1 ]); + } +if (x_off) +cpu_up (pc_x); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =totals_check ("task gone" , pc_y, pc_w, def0, hk0); +if (cs_n[0 ] &&wait_for (1000 , !rmdir (cs_n) || errno ==ENOENT )) +ksft_print_msg ("rmdir %s: %s\n" , cs_n,strerror (errno)); +cs_cleanup (); +return ret; +} + +/* +* A partition root that gets more CPUs makes a child partition valid. A +* is the highest housekeeping CPU, B the next one: +* 1. B is a root partition of A. A deadline task of 10% sleeps in B. +* 2. C, in B, becomes a root partition of A. B would have no CPU left +* for its task: C is invalid. +* 3. B gets A and B. +* C must become valid. The check runs before the rebuild of the sched +* domains: the root domain that counts the task still spans A alone. +* Its task then gets B. +*/ +static int partition_grows_child_valid (void ) +{ +int go[2 ] = {-1 ,-1 }, ack[2 ] = {-1 ,-1 }, i, err, ret =KSFT_SKIP ; +int a =top_cpu (1 ), b =top_cpu (2 ); +char buf[64 ], path[PATH_MAX +32 ]; +pid_t pid =-1 ; +cpu_set_t all; +char c; + +if (ncpus <3 ||cs_setup ()) +return KSFT_SKIP ; +snprintf (buf,sizeof (buf),"%d" , a); +if (cs_make (cs_b, buf) || +write_file (cs_b,"cpuset.cpus.partition" ,"root" ) || +write_file (cs_b,"cgroup.subtree_control" ,"+cpuset" ) || + !partition_has (cs_b,"root" ,1 ) ||partition_has (cs_b,"invalid" ,0 )) + goto out; +if (pipe (go) ||pipe (ack)) + goto out; + pid =fork_child ("a deadline task in B" ); +if (!pid) { +/* Run once in B, so that the task sleeps on a CPU of B. */ +if (read (go[0 ], &c,1 ) ==1 &&write (ack[1 ], &c,1 ) ==1 ) +for (;;) +pause (); +_exit (1 ); + } + ret =KSFT_FAIL ; +if (pid <0 ) + goto out; +CPU_ZERO (&all); +for (i =0 ; i <CPU_SETSIZE ; i++) +CPU_SET (i, &all); +if (sched_setaffinity (pid,sizeof (all), &all)) { +ksft_print_msg ("sched_setaffinity: %s\n" ,strerror (errno)); + goto out; + } + c ='x' ; +if (enter (cs_b, pid) ||write (go[1 ], &c,1 ) !=1 ||read (ack[0 ], &c,1 ) !=1 ) { +ksft_print_msg ("the task never ran in B\n" ); + goto out; + } + err =set_deadline (pid,10 *NSEC_PER_MSEC ,PERIOD ); +if (err) { +ksft_print_msg ("a deadline task refused in B: %s\n" ,strerror (-err)); + goto out; + } +snprintf (buf,sizeof (buf),"%d" , a); +if (cs_make (cs_c, buf)) + goto out; +write_file (cs_c,"cpuset.cpus.partition" ,"root" ); +if (!partition_has (cs_c,"invalid" ,1 )) { +ksft_print_msg ("C took the last CPU of B\n" ); + goto out; + } +snprintf (buf,sizeof (buf),"%d,%d" , a, b); + err =write_file (cs_b,"cpuset.cpus" , buf); +if (err) { +ksft_print_msg ("B cannot get CPUs %s: %s\n" , buf,strerror (-err)); + goto out; + } +if (!partition_has (cs_c,"root" ,1 ) ||partition_has (cs_c,"invalid" ,1 )) + goto out; +snprintf (path,sizeof (path),"%s/cpuset.cpus.effective" , cs_b); +if (read_file (path, buf,sizeof (buf))) + goto out; + buf[strcspn (buf,"\n" )] =0 ; +snprintf (path,sizeof (path),"%d" , b); +if (strcmp (buf, path)) { +ksft_print_msg ("B has CPUs %s, not %d\n" , buf, b); + goto out; + } + ret =KSFT_PASS ; +out: +reap (pid); +for (i =0 ; i <2 ; i++) { +if (go[i] >=0 ) +close (go[i]); +if (ack[i] >=0 ) +close (ack[i]); + } +sleep_ns (3 *PERIOD ); +cs_cleanup (); +return ret; +} + +/* +* Take offline the last CPU of the top cpuset. B is a root partition of +* every housekeeping CPU but X, the highest one. A deadline task of 10% +* sleeps in B. With X offline, the top cpuset would have no CPU left, so +* cpuset would make every partition invalid. B's CPUs would join a new +* root domain, with the deadline task. That root domain did not admit it. +* The offline must fail with EBUSY, and B must stay valid. Once the task +* is gone, the offline must be accepted. The case skips if the top +* cpuset keeps other CPUs, such as isolated ones. +*/ +static int hotplug_top_last_refused (void ) +{ +int i, n =0 , err, ret =KSFT_SKIP , x =top_cpu (1 ), x_off =0 ; +char buf[256 ], cpus[1024 ], path[PATH_MAX +32 ]; +int go[2 ] = {-1 ,-1 }, ack[2 ] = {-1 ,-1 }; +pid_t pid =-1 ; +cpu_set_t all; +char c; + +if (ncpus <3 ||cs_setup ()) +return KSFT_SKIP ; +for (i =0 ; i < ncpus -1 && n < (int )sizeof (cpus) -16 ; i++) + n +=snprintf (cpus + n,sizeof (cpus) - n,"%s%d" , i ?"," :"" , online[i]); +if (cs_make (cs_b, cpus) || +write_file (cs_b,"cpuset.cpus.partition" ,"root" ) || + !partition_has (cs_b,"root" ,1 ) ||partition_has (cs_b,"invalid" ,0 )) + goto out; +snprintf (path,sizeof (path),"%s/cpuset.cpus.effective" , root); +if (read_file (path, buf,sizeof (buf))) + goto out; + buf[strcspn (buf,"\n" )] =0 ; +snprintf (cpus,sizeof (cpus),"%d" , x); +if (strcmp (buf, cpus)) { +ksft_print_msg ("the top cpuset keeps CPUs %s, not only %d\n" , buf, x); + goto out; + } +if (pipe (go) ||pipe (ack)) + goto out; + pid =fork_child ("a deadline task in B" ); +if (!pid) { +/* Run once in B, so that the task sleeps on a CPU of B. */ +if (read (go[0 ], &c,1 ) ==1 &&write (ack[1 ], &c,1 ) ==1 ) +for (;;) +pause (); +_exit (1 ); + } + ret =KSFT_FAIL ; +if (pid <0 ) + goto out; +/* It inherits our affinity: let it have every CPU of B. */ +CPU_ZERO (&all); +for (i =0 ; i <CPU_SETSIZE ; i++) +CPU_SET (i, &all); +if (sched_setaffinity (pid,sizeof (all), &all)) { +ksft_print_msg ("sched_setaffinity: %s\n" ,strerror (errno)); + goto out; + } +if (enter (cs_b, pid)) + goto out; + c ='x' ; +if (write (go[1 ], &c,1 ) !=1 ||read (ack[0 ], &c,1 ) !=1 ) { +ksft_print_msg ("the task never ran in B\n" ); + goto out; + } + err =set_deadline (pid,10 *NSEC_PER_MSEC ,PERIOD ); +if (err) { +ksft_print_msg ("a deadline task refused in B: %s\n" ,strerror (-err)); + goto out; + } + err =cpu_online (x,0 ); +if (err != -EBUSY ) { +ksft_print_msg ("offline of CPU %d: %s, not EBUSY\n" , x, + err ?strerror (-err) :"accepted" ); + x_off = !err; + goto out; + } +if (partition_has (cs_b,"invalid" ,0 )) { +ksft_print_msg ("B became invalid\n" ); + goto out; + } +reap (pid); + pid =-1 ; +sleep_ns (3 *PERIOD ); + err =cpu_online (x,0 ); +if (err) { +ksft_print_msg ("offline of CPU %d with the task gone: %s\n" , x, +strerror (-err)); + goto out; + } + x_off =1 ; + ret =KSFT_PASS ; +out: +reap (pid); +for (i =0 ; i <2 ; i++) { +if (go[i] >=0 ) +close (go[i]); +if (ack[i] >=0 ) +close (ack[i]); + } +if (x_off) +cpu_up (x); +sleep_ns (3 *PERIOD ); +cs_cleanup (); +return ret; +} + +/* +* The same rule, when the offline makes invalid a partition that does not +* hold the CPU. B is a root partition of X and Y, with no task. D, in B, +* is a root partition of X. C, in B, is an isolated partition of Y, made +* after D. So B keeps no CPU of its own. The steps: +* 1. A task that is not a deadline task sleeps in D. +* 2. A task in the root cgroup runs on W. Then it allows every CPU and +* becomes a deadline task of 10%. It sleeps. The housekeeping root +* domain counts it. +* 3. Move it asleep into C. The default root domain counts it. +* 4. Take X offline. D would have no CPU left, and become invalid. Its +* task would then count in B, which would take Y from C. C would +* become invalid. Y would leave the default root domain for that of B, +* with the deadline task. That root domain did not admit it. +* The offline must fail with EBUSY. C must stay valid, and total_bw must +* not change. Once the task is gone, the offline must be accepted, and C +* must become invalid. +*/ +static int hotplug_sibling_partition_refused (void ) +{ +int fd[2 ] = {-1 ,-1 }, err, ret =KSFT_SKIP , x_off =0 ; +long long def0, hk0, def, hk, bw; +char buf[64 ], cs_d[PATH_MAX ]; +pid_t pid =-1 , other =-1 ; + + cs_d[0 ] =0 ; +if (partition_cpus () ||cs_setup ()) +return KSFT_SKIP ; +snprintf (buf,sizeof (buf),"%d,%d" , pc_x, pc_y); +if (cs_make (cs_b, buf) || +write_file (cs_b,"cpuset.cpus.partition" ,"root" ) || +write_file (cs_b,"cgroup.subtree_control" ,"+cpuset" )) + goto out; +if (snprintf (cs_d,sizeof (cs_d),"%s/d" , cs_b) >= (int )sizeof (cs_d)) { + cs_d[0 ] =0 ; + goto out; + } +snprintf (buf,sizeof (buf),"%d" , pc_x); +if (cs_make (cs_d, buf) || +write_file (cs_d,"cpuset.cpus.partition" ,"root" )) + goto out; +snprintf (buf,sizeof (buf),"%d" , pc_y); +if (cs_make (cs_c, buf) || +write_file (cs_c,"cpuset.cpus.partition" ,"isolated" ) || + !partition_has (cs_d,"root" ,1 ) ||partition_has (cs_d,"invalid" ,0 ) || + !partition_has (cs_c,"isolated" ,1 ) ||partition_has (cs_c,"invalid" ,0 )) + goto out; + other =fork_child ("a task in D" ); +if (!other) { +for (;;) +pause (); + } +if (other <0 ||enter (cs_d, other) ||totals_of (pc_y, pc_w, &def0, &hk0) || +pipe (fd)) + goto out; + ret =KSFT_FAIL ; + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =task_from (pc_w,1 ,10 *NSEC_PER_MSEC ,PERIOD , fd[0 ]); +if (pid <0 ||child_up ("hotplug_sibling_partition_refused" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' ) ||totals_of (pc_y, pc_w, &def, &hk)) + goto out; + bw = hk - hk0; + err =enter (cs_c, pid); +if (bw <=0 || def != def0 || err) { +ksft_print_msg ("a deadline task adds %lld, %lld to total_bw, in C: %s\n" , + def - def0, bw,strerror (-err)); + goto out; + } +if (totals_check ("in C" , pc_y, pc_w, def0 + bw, hk0)) + goto out; + err =cpu_online (pc_x,0 ); +if (err != -EBUSY ) { +ksft_print_msg ("offline of CPU %d: %s, not EBUSY\n" , pc_x, + err ?strerror (-err) :"accepted" ); + x_off = !err; + goto out; + } +if (partition_has (cs_c,"invalid" ,0 ) || +totals_check ("offline refused" , pc_y, pc_w, def0 + bw, hk0)) + goto out; +reap (pid); + pid =-1 ; +sleep_ns (3 *PERIOD ); + err =cpu_online (pc_x,0 ); +if (err) { +ksft_print_msg ("offline of CPU %d with the task gone: %s\n" , pc_x, +strerror (-err)); + goto out; + } + x_off =1 ; +/* The offline did make C invalid: the refusal was not too strict. */ +if (!partition_has (cs_c,"invalid" ,1 )) { +ksft_print_msg ("C stays valid with CPU %d offline\n" , pc_x); + goto out; + } + ret =KSFT_PASS ; +out: +reap (pid); +reap (other); +if (fd[0 ] >=0 ) { +close (fd[0 ]); +close (fd[1 ]); + } +if (x_off) +cpu_up (pc_x); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =totals_check ("task gone" , pc_y, pc_w, def0, hk0); +if (cs_d[0 ] &&wait_for (1000 , !rmdir (cs_d) || errno ==ENOENT )) +ksft_print_msg ("rmdir %s: %s\n" , cs_d,strerror (errno)); +cs_cleanup (); +return ret; +} + +/* +* A CPU of an isolated partition is in the default root domain, like the +* CPUs that isolcpus="domain" isolates. X and K are housekeeping CPUs: X +* the highest, K the lowest. I is an isolated CPU. Q is an isolated +* partition of X. The steps: +* 1. A task in the root cgroup runs on K. Then it allows every CPU and +* becomes a deadline task of 10%. It sleeps. The housekeeping root +* domain counts it. +* 2. Move it asleep into Q. The default root domain counts it. +* 3. Take X offline. Q has no CPU left and becomes invalid. The task +* falls back to the CPUs of the root cgroup. The isolated CPUs are +* still in the default root domain. +* The offline must be accepted. The default root domain must go on +* counting the task, and the housekeeping root domain must not. Woken, +* the task must run on an isolated CPU. +*/ +static int hotplug_isolated_partition_falls_back (void ) +{ +int fd[2 ] = {-1 ,-1 }, err, cpu, i, ret =KSFT_SKIP , x_off =0 ; +int x = ncpus >=2 ?top_cpu (1 ) :-1 , k = online[0 ], ic = nisol ? isol[0 ] :-1 ; +long long def0, hk0, def, hk, bw, srv; +uint64_t runtime, period; +char buf[64 ]; +pid_t pid =-1 ; + +if (ic <0 || x <0 || !cpu_hotpluggable (x) ||cs_setup ()) { +ksft_print_msg ("needs isolcpus=\"domain\", 2 housekeeping CPUs, CPU hotplug\n" ); +return KSFT_SKIP ; + } +snprintf (buf,sizeof (buf),"%d" , x); +if (cs_make (cs_q, buf) ||write_file (cs_q,"cpuset.cpus.partition" ,"isolated" ) || + !partition_has (cs_q,"isolated" ,1 ) ||partition_has (cs_q,"invalid" ,0 ) || +totals_of (ic, k, &def0, &hk0) ||pipe (fd)) + goto out; + ret =KSFT_FAIL ; + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =task_from (k,1 ,10 *NSEC_PER_MSEC ,PERIOD , fd[0 ]); +if (pid <0 ||child_up ("hotplug_isolated_partition_falls_back" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' ) ||totals_of (ic, k, &def, &hk)) + goto out; + bw = hk - hk0; + err =enter (cs_q, pid); +if (bw <=0 || def != def0 || err) { +ksft_print_msg ("a deadline task adds %lld, %lld to total_bw, in Q: %s\n" , + def - def0, bw,strerror (-err)); + goto out; + } +if (totals_check ("in Q" , ic, k, def0 + bw, hk0)) + goto out; +/* The fair server of X leaves the default root domain with X. */ +if (server_get (x,"runtime" , &runtime) ||server_get (x,"period" , &period) || !period) { +ksft_print_msg ("no fair server parameters for CPU %d\n" , x); + goto out; + } + srv = (long long )((runtime <<20 ) / period);/* as to_ratio() */ + err =cpu_online (x,0 ); +if (err) { +ksft_print_msg ("offline of CPU %d: %s\n" , x,strerror (-err)); + goto out; + } + x_off =1 ; +if (totals_check ("X offline" , ic, k, def0 + bw - srv, hk0) ||fallback_wake (fd[1 ], pid)) + goto out; + cpu =task_cpu_of (pid); +for (i =0 ; i < nisol && isol[i] != cpu; i++) + ; +if (i == nisol) { +ksft_print_msg ("woken on CPU %d, not an isolated CPU\n" , cpu); + goto out; + } + ret =KSFT_PASS ; +out: +reap (pid); +if (fd[0 ] >=0 ) { +close (fd[0 ]); +close (fd[1 ]); + } +if (x_off) +cpu_up (x); +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =totals_check ("task gone" , ic, k, def0, hk0); +cs_cleanup (); +return ret; +} + +/* +* A dead task runs no more, so it cannot leave its root domain. The CPUs +* are those of fallback_case(). B is a cpuset of I1, I2 and H, not a +* partition. The steps: +* 1. A task in the root cgroup runs on H. Then it allows every CPU and +* becomes a deadline task of 100ms every 1s. It sleeps. +* 2. Move it asleep into B. It keeps H. The housekeeping root domain +* counts it. +* 3. Wake it. It spins 90ms and exits. The case does not reap it. Its +* 0-lag time is about 800ms ahead. +* 4. Take H offline. Once H is offline, the dead task would have no CPU +* in the root domain that counts it. That does not matter: it never +* runs again. +* The offline must be accepted. The case then reaps the task and brings +* H back. Past the 0-lag time, total_bw must be what it was. +* +* The case depends on time: it skips if the task takes more than 500ms +* to show as a zombie. +*/ +static int hotplug_dead_accepted (void ) +{ +int fd[2 ] = {-1 ,-1 }, err, cpu, ret =KSFT_SKIP , ran =0 ; +long long def0, hk0, def, hk; +uint64_t t; +char buf[64 ]; +pid_t pid =-1 ; + +if (fallback_cpus (0 ) || !cpu_hotpluggable (fb_h)) { +ksft_print_msg ("needs isolcpus=domain: 2 isolated, 2 housekeeping CPUs, hotplug\n" ); +return KSFT_SKIP ; + } +if (cs_setup ()) +return KSFT_SKIP ; +snprintf (buf,sizeof (buf),"%d,%d,%d" , fb_i1, fb_i2, fb_h); +if (cs_make (cs_b, buf) ||fallback_totals (&def0, &hk0) ||pipe (fd)) + goto out; + ret =KSFT_FAIL ; + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =task_from (fb_h,1 ,100 *NSEC_PER_MSEC ,NSEC_PER_SEC , fd[0 ]); + ran = pid >=0 ; +if (pid <0 ||child_up ("hotplug_dead_accepted" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' )) + goto out; + err =enter (cs_b, pid); +if (err) { +ksft_print_msg ("a move into B refused: %s\n" ,strerror (-err)); + goto out; + } +if (fallback_totals (&def, &hk)) + goto out; + cpu =task_cpu_of (pid); +if (cpu != fb_h || def != def0 || hk <= hk0) { +ksft_print_msg ("in B on CPU %d: total_bw %lld, %lld\n" , cpu, def, hk); + goto out; + } +if (write (fd[1 ],"x" ,1 ) !=1 ) { +ksft_print_msg ("write to the deadline task: %s\n" ,strerror (errno)); + goto out; + } + t =now_ns (); +if (wait_for (1000 ,proc_state_of (pid) =='Z' )) + goto out; +if (now_ns () - t >500 *NSEC_PER_MSEC ) { +ksft_print_msg ("the task took over 500ms to exit\n" ); + ret =KSFT_SKIP ; + goto out; + } +sleep_ns (10 *NSEC_PER_MSEC );/* past do_task_dead() */ + err =cpu_online (fb_h,0 ); +if (err) { +ksft_print_msg ("offline of CPU %d under a dead task: %s\n" , fb_h, +strerror (-err)); + goto out; + } +reap (pid); + pid =-1 ; +cpu_up (fb_h); +sleep_ns (NSEC_PER_SEC );/* past the 0-lag time */ + ran =0 ; + ret =totals_check ("task gone" , fb_i2, fb_h, def0, hk0); +out: +reap (pid); +cpu_up (fb_h); +/* Let the next case start after the 0-lag time of the task. */ +if (ran) +sleep_ns (NSEC_PER_SEC ); +if (fd[0 ] >=0 ) { +close (fd[0 ]); +close (fd[1 ]); + } +cs_cleanup (); +return ret; +} + +/* +* A cpuset that loses all its CPUs takes them from its parent, through +* its own cpuset.cpus. The CPUs are those of fallback_case(). The steps: +* 1. B, in the root cgroup, has cpuset.cpus H. C, in B, has cpuset.cpus +* I2. I2 is not in B, so C gets H. +* 2. A task in the root cgroup runs on H. Then it allows every CPU and +* becomes a deadline task of 10%. It sleeps. The housekeeping root +* domain counts it. +* 3. Move it asleep into C. It keeps H. +* 4. Q, in the root cgroup, has cpuset.cpus H. Make it a partition root. +* B would then get the CPUs of the root cgroup, and C would get I2 +* of them. The task would have no CPU in the root domain that counts +* it. +* Q must be an invalid partition root, for the deadline task. Wake the +* task: it must run on H, and the housekeeping root domain must count it. +*/ +static int partition_takes_fallback_refused (void ) +{ +int fd[2 ] = {-1 ,-1 }, cpu, err, ret =KSFT_SKIP ; +long long def0, hk0, def, hk, bw; +char buf[64 ]; +pid_t pid =-1 ; + +if (fallback_cpus (0 )) { +ksft_print_msg ("needs isolcpus=domain: 2 isolated, 2 housekeeping CPUs\n" ); +return KSFT_SKIP ; + } +if (cs_setup ()) +return KSFT_SKIP ; +snprintf (buf,sizeof (buf),"%d" , fb_h); +if (cs_make (cs_b, buf) ||write_file (cs_b,"cgroup.subtree_control" ,"+cpuset" )) + goto out; +snprintf (buf,sizeof (buf),"%d" , fb_i2); +if (cs_make (cs_c, buf) ||fallback_totals (&def0, &hk0) ||pipe (fd)) + goto out; + ret =KSFT_FAIL ; + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =task_from (fb_h,1 ,10 *NSEC_PER_MSEC ,PERIOD , fd[0 ]); +if (pid <0 ||child_up ("partition_takes_fallback_refused" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' ) ||fallback_totals (&def, &hk)) + goto out; + bw = hk - hk0; + err =enter (cs_c, pid); + cpu =task_cpu_of (pid); +if (bw <=0 || def != def0 || err || cpu != fb_h) { +ksft_print_msg ("a deadline task adds %lld, %lld to total_bw, in C: %s, on CPU %d\n" , + def - def0, bw,strerror (-err), cpu); + goto out; + } +snprintf (buf,sizeof (buf),"%d" , fb_h); +if (cs_make (cs_q, buf)) + goto out; +write_file (cs_q,"cpuset.cpus.partition" ,"root" ); +if (!partition_has (cs_q,"Deadline task" ,1 ) || +fallback_wake (fd[1 ], pid)) + goto out; + cpu =task_cpu_of (pid); +if (cpu != fb_h) { +ksft_print_msg ("woken on CPU %d, not %d\n" , cpu, fb_h); + goto out; + } + ret =totals_check ("woken" , fb_i2, fb_h, def0, hk0 + bw); +out: +reap (pid); +if (fd[0 ] >=0 ) { +close (fd[0 ]); +close (fd[1 ]); + } +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =totals_check ("task gone" , fb_i2, fb_h, def0, hk0); +cs_cleanup (); +return ret; +} + +/* +* A mask that grows can still take a task out of its root domain. That +* happens if the user affinity of the task holds only the new CPUs. +* The CPUs: I1 is isolated by isolcpus="domain", H is the first +* housekeeping CPU. The steps: +* 1. Q is a partition root of the other housekeeping CPUs. The +* housekeeping root domain of the root cgroup then has H only. +* 2. B, in the root cgroup, is a cpuset of H and I1. A task enters B. It +* sets its affinity to I1, and runs there. It is not a deadline task. +* 3. Give B the CPU H only. The task gets H: its affinity holds no CPU of +* B. Wake it: it runs on H. +* 4. The task becomes a deadline task of 10%. Its mask holds the whole +* housekeeping root domain of the root cgroup, H. That root domain +* counts it. It sleeps. +* 5. Give B the CPUs H and I1 again. The task would then get I1 only, its +* affinity, in another root domain. +* The write of step 5 must fail with EBUSY. Wake the task: it must run on +* H, and the housekeeping root domain must count it. +*/ +static int mask_grows_refused (void ) +{ +int h = online[0 ], i1 = nisol ? isol[0 ] :-1 , fd[2 ] = {-1 ,-1 }; +int i, n =0 , err, cpu, ret =KSFT_SKIP ; +long long def0, hk0, def, hk, bw; +char buf[4096 ]; +pid_t pid =-1 ; + +if (i1 <0 ) { +ksft_print_msg ("needs CPUs isolated by isolcpus=domain\n" ); +return KSFT_SKIP ; + } +if (cs_setup ()) +return KSFT_SKIP ; +for (i =1 ; i < ncpus && n < (int )sizeof (buf); i++) + n +=snprintf (buf + n,sizeof (buf) - n,"%s%d" , i >1 ?"," :"" , online[i]); +if (n >= (int )sizeof (buf) ||make_partition_cpus (cs_q, buf)) { +ksft_print_msg ("no partition root Q of CPUs %s\n" , buf); + goto out; + } +snprintf (buf,sizeof (buf),"%d,%d" , h, i1); +if (cs_make (cs_b, buf) ||totals_of (i1, h, &def0, &hk0) ||pipe (fd)) + goto out; + ret =KSFT_FAIL ; + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =fork_child ("a task with an affinity" ); +if (!pid) { +cpu_set_t set; +char c; + +CPU_ZERO (&set); +CPU_SET (i1, &set); +if (enter (cs_b,getpid ()) ||sched_setaffinity (0 ,sizeof (set), &set)) { + mv_state[1 ] =-1 ; +_exit (1 ); + } +spin_ns (NSEC_PER_MSEC ); + mv_state[1 ] =1 ; +/* Wait for B of H only. Run there, and become a deadline task. */ +if (read (fd[0 ], &c,1 ) !=1 ) +_exit (1 ); +spin_ns (NSEC_PER_MSEC ); +if (set_deadline (0 ,10 *NSEC_PER_MSEC ,PERIOD )) { + mv_state[0 ] =-1 ; +_exit (1 ); + } + mv_state[0 ] =1 ; +while (read (fd[0 ], &c,1 ) ==1 ) + mv_state[1 ]++; +_exit (0 ); + } +if (pid <0 ||wait_for (1000 , mv_state[1 ]) || mv_state[1 ] !=1 || +wait_for (1000 ,proc_state_of (pid) =='S' )) { +ksft_print_msg ("the task cannot take CPU %d\n" , i1); + goto out; + } +snprintf (buf,sizeof (buf),"%d" , h); + err =write_file (cs_b,"cpuset.cpus" , buf); +if (err) { +ksft_print_msg ("B given CPU %d only: %s\n" , h,strerror (-err)); + goto out; + } + mv_state[1 ] =0 ; +if (write (fd[1 ],"d" ,1 ) !=1 ||child_up ("mask_grows_refused" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' ) ||totals_of (i1, h, &def, &hk)) + goto out; + bw = hk - hk0; + cpu =task_cpu_of (pid); +if (bw <=0 || def != def0 || cpu != h) { +ksft_print_msg ("a deadline task on CPU %d adds %lld, %lld to total_bw\n" , + cpu, def - def0, bw); + goto out; + } +snprintf (buf,sizeof (buf),"%d,%d" , h, i1); + err =write_file (cs_b,"cpuset.cpus" , buf); +if (err != -EBUSY ) { +ksft_print_msg ("B given CPUs %s: %s, not EBUSY\n" , buf, + err ?strerror (-err) :"accepted" ); +if (!err && !fallback_wake (fd[1 ], pid)) +ksft_print_msg ("woken on CPU %d\n" ,task_cpu_of (pid)); + goto out; + } +if (fallback_wake (fd[1 ], pid)) + goto out; + cpu =task_cpu_of (pid); +if (cpu != h) { +ksft_print_msg ("woken on CPU %d, not %d\n" , cpu, h); + goto out; + } + ret =totals_check ("woken" , i1, h, def0, hk0 + bw); +out: +reap (pid); +if (fd[0 ] >=0 ) { +close (fd[0 ]); +close (fd[1 ]); + } +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =totals_check ("task gone" , i1, h, def0, hk0); +cs_cleanup (); +return ret; +} + +/* +* One write to cpuset.cpus can make a partition root take CPUs and give +* others back. The CPUs: I1 is isolated by isolcpus="domain". H is a +* housekeeping CPU. The steps: +* 1. Q is an isolated partition root of I1. +* 2. B, in the root cgroup, has cpuset.cpus H and I1. Q holds I1, so B +* gets H. +* 3. A task in the root cgroup runs on H. Then it allows every CPU and +* becomes a deadline task of 10%. It sleeps. The housekeeping root +* domain counts it. +* 4. Move it asleep into B. It keeps H. +* 5. Write H to the cpuset.cpus of Q. Q would take H and give I1 back. B +* would then get I1 only, in another root domain. +* Q must become an invalid partition root, for the deadline task. Wake +* the task: it must run on H, and the housekeeping root domain must count +* it. +*/ +static int partition_swap_refused (void ) +{ +int h = online[ncpus -1 ], i1 = nisol ? isol[0 ] :-1 , fd[2 ] = {-1 ,-1 }; +int cpu, err, ret =KSFT_SKIP ; +long long def0, hk0, def, hk, bw; +char buf[64 ]; +pid_t pid =-1 ; + +if (i1 <0 ) { +ksft_print_msg ("needs CPUs isolated by isolcpus=domain\n" ); +return KSFT_SKIP ; + } +if (cs_setup ()) +return KSFT_SKIP ; +snprintf (buf,sizeof (buf),"%d" , i1); +if (cs_make (cs_q, buf) ||write_file (cs_q,"cpuset.cpus.partition" ,"isolated" ) || + !partition_has (cs_q,"isolated" ,1 ) ||partition_has (cs_q,"invalid" ,0 )) { +ksft_print_msg ("no isolated partition Q of CPU %d\n" , i1); + goto out; + } +snprintf (buf,sizeof (buf),"%d,%d" , h, i1); +if (cs_make (cs_b, buf) ||totals_of (i1, h, &def0, &hk0) ||pipe (fd)) + goto out; + ret =KSFT_FAIL ; + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =task_from (h,1 ,10 *NSEC_PER_MSEC ,PERIOD , fd[0 ]); +if (pid <0 ||child_up ("partition_swap_refused" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' ) ||totals_of (i1, h, &def, &hk)) + goto out; + bw = hk - hk0; + err =enter (cs_b, pid); + cpu =task_cpu_of (pid); +if (bw <=0 || def != def0 || err || cpu != h) { +ksft_print_msg ("a deadline task adds %lld, %lld to total_bw, in B: %s, on CPU %d\n" , + def - def0, bw,strerror (-err), cpu); + goto out; + } +snprintf (buf,sizeof (buf),"%d" , h); +write_file (cs_q,"cpuset.cpus" , buf); +if (!partition_has (cs_q,"Deadline task" ,1 ) ||fallback_wake (fd[1 ], pid)) + goto out; + cpu =task_cpu_of (pid); +if (cpu != h) { +ksft_print_msg ("woken on CPU %d, not %d\n" , cpu, h); + goto out; + } + ret =totals_check ("woken" , i1, h, def0, hk0 + bw); +out: +reap (pid); +if (fd[0 ] >=0 ) { +close (fd[0 ]); +close (fd[1 ]); + } +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =totals_check ("task gone" , i1, h, def0, hk0); +cs_cleanup (); +return ret; +} + +/* Return the taint flags of the kernel, or -1. */ +static long taint_flags (void ) +{ +char buf[64 ]; + +if (read_file ("/proc/sys/kernel/tainted" , buf,sizeof (buf))) +return -1 ; +return atol (buf); +} + +#define TAINT_WARN (1L << 9) + +/* +* An invalid partition root holds no CPU of its parent. A refusal must +* not give any back. The CPUs are those of fallback_case(). B is a cpuset +* of I1, I2 and H, not a partition. The steps: +* 1. A task in the root cgroup runs on H. Then it allows every CPU and +* becomes a deadline task of 10%. It sleeps. The housekeeping root +* domain counts it. +* 2. Move it asleep into B. It keeps H. +* 3. Q, in the root cgroup, has cpuset.cpus H and K. Its +* cpuset.cpus.exclusive is H. Make it a partition root. It would take H, +* the only housekeeping CPU of B. Q must be invalid, for the deadline +* task. +* 4. Write H to the cpuset.cpus of Q. The same check refuses again. +* The kernel must not warn: the taint flag W must stay clear. Q must +* still be invalid for the deadline task. The case skips if the flag +* was set before. +*/ +static int partition_refused_no_warn (void ) +{ +int fd[2 ] = {-1 ,-1 }, cpu, err, ret =KSFT_SKIP ; +long long def0, hk0, def, hk, bw; +char buf[64 ]; +pid_t pid =-1 ; + +if (fallback_cpus (0 )) { +ksft_print_msg ("needs isolcpus=domain: 2 isolated, 2 housekeeping CPUs\n" ); +return KSFT_SKIP ; + } +if (taint_flags () <0 || (taint_flags () &TAINT_WARN )) { +ksft_print_msg ("the kernel has warned already\n" ); +return KSFT_SKIP ; + } +if (cs_setup ()) +return KSFT_SKIP ; +snprintf (buf,sizeof (buf),"%d,%d,%d" , fb_i1, fb_i2, fb_h); +if (cs_make (cs_b, buf) ||fallback_totals (&def0, &hk0) ||pipe (fd)) + goto out; + ret =KSFT_FAIL ; + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =task_from (fb_h,1 ,10 *NSEC_PER_MSEC ,PERIOD , fd[0 ]); +if (pid <0 ||child_up ("partition_refused_no_warn" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' ) ||fallback_totals (&def, &hk)) + goto out; + bw = hk - hk0; + err =enter (cs_b, pid); + cpu =task_cpu_of (pid); +if (bw <=0 || def != def0 || err || cpu != fb_h) { +ksft_print_msg ("a deadline task adds %lld, %lld to total_bw, in B: %s, on CPU %d\n" , + def - def0, bw,strerror (-err), cpu); + goto out; + } +snprintf (buf,sizeof (buf),"%d,%d" , fb_h, fb_k); +if (cs_make (cs_q, buf)) + goto out; +snprintf (buf,sizeof (buf),"%d" , fb_h); +if (write_file (cs_q,"cpuset.cpus.exclusive" , buf)) { +ksft_print_msg ("Q cannot have CPU %d exclusive\n" , fb_h); + goto out; + } +write_file (cs_q,"cpuset.cpus.partition" ,"root" ); +if (!partition_has (cs_q,"Deadline task" ,1 )) + goto out; +write_file (cs_q,"cpuset.cpus" , buf); +if (taint_flags () &TAINT_WARN ) { +ksft_print_msg ("Q given CPU %d: the kernel warned\n" , fb_h); + goto out; + } +if (!partition_has (cs_q,"Deadline task" ,1 ) ||fallback_wake (fd[1 ], pid)) + goto out; + cpu =task_cpu_of (pid); +if (cpu != fb_h) { +ksft_print_msg ("woken on CPU %d, not %d\n" , cpu, fb_h); + goto out; + } + ret =totals_check ("woken" , fb_i2, fb_h, def0, hk0 + bw); +out: +reap (pid); +if (fd[0 ] >=0 ) { +close (fd[0 ]); +close (fd[1 ]); + } +sleep_ns (3 *PERIOD ); +if (ret ==KSFT_PASS ) + ret =totals_check ("task gone" , fb_i2, fb_h, def0, hk0); +cs_cleanup (); +return ret; +} + +/* +* A rebuild must count a task where its cpuset lets it run. The kernel +* records, with the bandwidth of a task, a CPU of the root domain that +* counts it. A partition root can take that CPU later. A, B and D are the +* three highest housekeeping CPUs. C is the first one. Q is a partition +* root of the other housekeeping CPUs, C among them. The steps: +* 1. Take D offline. The housekeeping root domain then has A and B only. +* 2. A task in the root cgroup sets its affinity to A and B. It becomes a +* deadline task of 10% and sleeps. Bring D back online. The task keeps +* its affinity. +* 3. Take A and B offline. No CPU of the task's mask is active. Its +* cpuset gives it D, so the rebuild counts it on D. D is recorded. +* 4. Make Q a member, then a partition root of D instead of C. The +* cpuset of the task now gives it C. Its mask is still A and B. +* Q must not count the task. The root domain of C must count it. Wake +* the task: it must run on C, and total_bw must not change. +* +* The references of total_bw are taken once the task is gone, with the +* CPUs as in step 4. +*/ +static int rebuild_record_taken (void ) +{ +int a =top_cpu (3 ), b =top_cpu (2 ), c = online[0 ], d =top_cpu (1 ); +int fd[2 ] = {-1 ,-1 }, i, n, err, cpu, ret =KSFT_SKIP ; +long long q, h, q0, h0, bw; +char buf[4096 ]; +pid_t pid =-1 ; + +if (ncpus <4 || !cpu_hotpluggable (a) || !cpu_hotpluggable (b) || + !cpu_hotpluggable (d)) { +ksft_print_msg ("needs 4 CPUs, 3 of them hotpluggable\n" ); +return KSFT_SKIP ; + } +if (cs_setup ()) +return KSFT_SKIP ; +/* Q has every housekeeping CPU but A, B and D. */ +for (i = n =0 ; i < ncpus -3 ; i++) + n +=snprintf (buf + n,sizeof (buf) - n,"%s%d" , n ?"," :"" , + online[i]); +if (make_partition_cpus (cs_q, buf)) { +ksft_print_msg ("Q cannot be a partition root of CPUs %s\n" , buf); + goto out; + } + ret =KSFT_FAIL ; + err =cpu_online (d,0 ); +if (err) { +ksft_print_msg ("offline of CPU %d: %s\n" , d,strerror (-err)); + goto out; + } + bw =total_bw_of (a); +if (bw ==-1 ) { +ksft_print_msg ("no total_bw in /sys/kernel/debug/sched/debug\n" ); + ret =KSFT_SKIP ; + goto out; + } +if (pipe (fd)) { +ksft_print_msg ("pipe: %s\n" ,strerror (errno)); + goto out; + } + mv_state[0 ] =0 ; + mv_state[1 ] =0 ; + pid =fork_child ("a deadline task" ); +if (!pid) { +cpu_set_t set; +char ch; + +CPU_ZERO (&set); +CPU_SET (a, &set); +CPU_SET (b, &set); +if (sched_setaffinity (0 ,sizeof (set), &set) || +set_deadline (0 ,10 *NSEC_PER_MSEC ,PERIOD )) { + mv_state[0 ] =-1 ; +_exit (1 ); + } + mv_state[0 ] =1 ; +while (read (fd[0 ], &ch,1 ) ==1 ) + mv_state[1 ]++; +_exit (0 ); + } +if (pid <0 ||child_up ("rebuild_record_taken" ,0 ) || +wait_for (1000 ,proc_state_of (pid) =='S' )) + goto out; + bw =total_bw_of (a) - bw; +if (bw <=0 ) { +ksft_print_msg ("the deadline task adds %lld to total_bw\n" , bw); + goto out; + } + err =cpu_online (d,1 ); +if (!err) + err =cpu_online (a,0 ); +if (!err) + err =cpu_online (b,0 ); +if (err) { +ksft_print_msg ("hotplug of CPUs %d, %d, %d: %s\n" , d, a, b, +strerror (-err)); + goto out; + } +/* +* Q has D and every CPU it had but C. One write of cpuset.cpus +* would make Q an invalid partition root: the check sees the root +* cgroup lose D, and get C back too late. +*/ +for (i =1 , n =0 ; i < ncpus -3 ; i++) + n +=snprintf (buf + n,sizeof (buf) - n,"%d," , online[i]); +snprintf (buf + n,sizeof (buf) - n,"%d" , d); + err =write_file (cs_q,"cpuset.cpus.partition" ,"member" ); +if (err ||make_partition_cpus (cs_q, buf)) { +ksft_print_msg ("Q cannot be a partition root of CPUs %s\n" , buf); +partition_has (cs_q,"root\n" ,1 ); + goto out; + } + q =total_bw_of (d); + h =total_bw_of (c); +if (fallback_wake (fd[1 ], pid)) + goto out; +/* Go on after a failure: the totals tell more. */ + cpu =task_cpu_of (pid); +if (cpu != c) +ksft_print_msg ("woken on CPU %d, not %d\n" , cpu, c); +else if (!totals_check ("woken" , d, c, q, h)) + ret =KSFT_PASS ; +reap (pid); + pid =-1 ; +sleep_ns (3 *PERIOD ); +if (totals_of (d, c, &q0, &h0)) { + ret =KSFT_FAIL ; + goto out; + } +if (q != q0 || h != h0 + bw) { +ksft_print_msg ("Q counts %lld of %lld, the root cgroup %lld\n" , + q - q0, bw, h - h0); + ret =KSFT_FAIL ; + } +out: +reap (pid); +if (fd[0 ] >=0 ) { +close (fd[0 ]); +close (fd[1 ]); + } +cpu_up (a); +cpu_up (b); +cpu_up (d); +cs_cleanup (); +return ret; +} +/* Moves between two root domains, from the main process, with P */ /*
@@ -7960,8 +10284,36 @@ static const struct hp_case { { "hotplug_refused", hotplug_refused }, { "hotplug_departed", hotplug_departed }, { "hotplug_server", hotplug_server }, + { "hotplug_server_refused", hotplug_server_refused }, + { "hotplug_fallback", hotplug_fallback }, + { "hotplug_fallback_running", hotplug_fallback_running }, + { "hotplug_fallback_moved", hotplug_fallback_moved }, + { "hotplug_fallback_in_mask", hotplug_fallback_in_mask }, + { "mask_out_refused", mask_out_refused, 1 }, + { "hotplug_last_refused", hotplug_last_refused }, + { "hotplug_keeps_root_domain", hotplug_keeps_root_domain }, + { "partition_takes_refused", partition_takes_refused, 1 }, + { "no_admission_affinity_moves", no_admission_affinity_moves, 1 }, { "affinity_vs_setattr", affinity_vs_setattr, 1 }, { "move_back_cpu_offline", move_back_cpu_offline }, + { "hotplug_spinner_keeps", hotplug_spinner_keeps }, + { "hotplug_waking_keeps", hotplug_waking_keeps }, + { "hotplug_isolated_keeps", hotplug_isolated_keeps }, + { "rebuild_waking_keeps", rebuild_waking_keeps, 1 }, + { "hotplug_user_mask_refused", hotplug_user_mask_refused }, + { "hotplug_nested_refused", hotplug_nested_refused }, + { "hotplug_dead_accepted", hotplug_dead_accepted }, + { "hotplug_child_partition_kept", hotplug_child_partition_kept }, + { "hotplug_partition_invalid_refused", hotplug_partition_invalid_refused }, + { "hotplug_sibling_partition_refused", hotplug_sibling_partition_refused }, + { "hotplug_top_last_refused", hotplug_top_last_refused }, + { "partition_grows_child_valid", partition_grows_child_valid }, + { "hotplug_isolated_partition_falls_back", hotplug_isolated_partition_falls_back }, + { "partition_takes_fallback_refused", partition_takes_fallback_refused, 1 }, + { "mask_grows_refused", mask_grows_refused, 1 }, + { "partition_swap_refused", partition_swap_refused, 1 }, + { "partition_refused_no_warn", partition_refused_no_warn, 1 }, + { "rebuild_record_taken", rebuild_record_taken }, };#define N_HP_CASES ((int)ARRAY_SIZE(hp_cases))
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v10 18/40] selftests/sched: Test the root domain of a task in deadline_bw_test
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Low] selftests/sched: missing no_hp flag for partition_grows_child_valid() - [Low] selftests/sched…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Low] selftests/sched: missing no_hp flag for partition_grows_child_valid()
- [Low] selftests/sched: inverted tell flag in partition_grows_child_valid()
- [Low] selftests/sched: missing cpu_hotpluggable() check in hotplug_top_last_refused()
--
commit 31c2701ec79ac71f9d7072a6b7c9ae3a844ce21e
Author: Mathieu Desnoyers <[email protected]>
selftests/sched: Test the root domain of a task in deadline_bw_test
This commit adds 28 test cases to deadline_bw_test covering root domain bandwidth accounting across CPU hotplug, cpuset partition changes, and isolated CPUs.
Quoted 4 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +0
-0
[ ... ]
Quoted 1 line
tools/testing/selftests/sched/deadline/deadline_bw_test.c +0
-0@@ -7144,6 +7695,1779 @@ static int move_back_cpu_offline(void)
[ ... ]
Quoted 2 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +2
-0+static int partition_grows_child_valid (void ) +{
[ ... ]
tools/testing/selftests/sched/deadline/deadline_bw_test.c static int move_back_cpu_offline(void) · line 8520
Quoted 8 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +8
-0+snprintf (buf,sizeof (buf),"%d,%d" , a, b); + err =write_file (cs_b,"cpuset.cpus" , buf); +if (err) { +ksft_print_msg ("B cannot get CPUs %s: %s\n" , buf,strerror (-err)); + goto out; + } +if (!partition_has (cs_c,"root" ,1 ) ||partition_has (cs_c,"invalid" ,1 )) + goto out;
[Severity: Low]
Should the second call pass 0 for tell: partition_has(cs_c, "invalid", 0)?
partition_has() prints cpuset.cpus.partition when the substring is absent and tell is non-zero. Passing 1 here logs a message on every passing run when "invalid" is absent, and stays silent when cs_c fails by remaining "root invalid (...)".
[ ... ]
tools/testing/selftests/sched/deadline/deadline_bw_test.c static int move_back_cpu_offline(void) · line 8564
Quoted 11 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +11
-0+static int hotplug_top_last_refused (void ) +{ +int i, n =0 , err, ret =KSFT_SKIP , x =top_cpu (1 ), x_off =0 ; +char buf[256 ], cpus[1024 ], path[PATH_MAX +32 ]; +int go[2 ] = {-1 ,-1 }, ack[2 ] = {-1 ,-1 }; +pid_t pid =-1 ; +cpu_set_t all; +char c; + +if (ncpus <3 ||cs_setup ()) +return KSFT_SKIP ;
[Severity: Low]
Should this also skip when !cpu_hotpluggable(x)?
main() sets hp_cpu if any housekeeping CPU can go offline, which does not guarantee that top_cpu(1) specifically is hotpluggable. If top_cpu(1) cannot go offline while another housekeeping CPU can, cpu_online(x, 0) later in this test fails instead of skipping.
[ ... ]
Quoted 1 line
tools/testing/selftests/sched/deadline/deadline_bw_test.c +0
-0@@ -7960,8 +10284,36 @@ static const struct hp_case {
[ ... ]
tools/testing/selftests/sched/deadline/deadline_bw_test.c static const struct hp_case { · line 10312
Quoted 5 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +5
-0+ { "hotplug_sibling_partition_refused", hotplug_sibling_partition_refused }, + { "hotplug_top_last_refused", hotplug_top_last_refused }, + { "partition_grows_child_valid", partition_grows_child_valid }, + { "hotplug_isolated_partition_falls_back", hotplug_isolated_partition_falls_back }, + { "partition_takes_fallback_refused", partition_takes_fallback_refused, 1 },
[Severity: Low]
Should the partition_grows_child_valid entry set no_hp to 1?
partition_grows_child_valid() does not take any CPU offline and the file header lists it among the ten cases in hp_cases[] that still run when no CPU can go offline, but without the trailing 1 initializer main() skips it whenever hp_cpu < 0.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
Re: [PATCH v10 18/40] selftests/sched: Test the root domain of a task in deadline_bw_test
Quoted 1 line
Should the second call pass 0 for tell:
partition_has(cs_c, "invalid", 0)?
Yes. v11 will pass 0.
Quoted 1 line
Should this also skip when !cpu_hotpluggable(x)?
Yes. v11 will skip then.
Quoted 1 line
Should the
partition_grows_child_validentry setno_hpto 1?
Yes. v11 will set it.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
PATCH v10 19/40 sched/deadline: Count only active CPUs when spreading extra_bw
In reply to Mathieu Desnoyers__dl_add() and __dl_sub() change the total_bw of a root domain. They also spread the change over the extra_bw of its CPUs, which GRUB uses for SCHED_FLAG_RECLAIM tasks. __dl_update() changes only the active CPUs, so the cpus argument must count those. Most callers pass dl_bw_cpus(), which does.
dl_task_offline_migration() and dl_add_task_root_domain() pass cpumask_weight(rd->span) instead. That also counts the inactive CPUs of the span. A span holds one while that CPU goes offline, which is when dl_task_offline_migration() runs. Each active CPU then gets a smaller share of the change than it should. A later change made with dl_bw_cpus() does not undo it exactly. extra_bw stays off until the next rebuild of the root domains resets it. SCHED_FLAG_RECLAIM tasks would reclaim a little more or less than their share meanwhile. This was found by reading.
Fix this by passing dl_bw_cpus() of the CPU whose root domain changes.
--- kernel/sched/deadline.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/sched/deadline.c +3 -3
@@ -1070,7 +1070,7 @@ static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p dl_b = &src_rq->rd->dl_bw;raw_spin_lock (&dl_b->lock);__dl_sub (dl_b, p->dl.dl_bw, -cpumask_weight (src_rq->rd->span)); +dl_bw_cpus (cpu_of (src_rq)));raw_spin_unlock (&dl_b->lock); }/*
@@ -1082,7 +1082,7 @@ static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p dl_b = &later_rq->rd->dl_bw;raw_spin_lock (&dl_b->lock);__dl_add (dl_b, p->dl.dl_bw, -cpumask_weight (later_rq->rd->span)); +dl_bw_cpus (cpu_of (later_rq)));raw_spin_unlock (&dl_b->lock);dl_record_bw_cpu (&p->dl, later_rq->cpu); p->dl.dl_bw_gen = dl_bw_gen;
@@ -4357,7 +4357,7 @@ void dl_add_task_root_domain(struct task_struct *p) dl_b = &rq->rd->dl_bw;raw_spin_lock (&dl_b->lock); -__dl_add (dl_b, p->dl.dl_bw,cpumask_weight (rq->rd->span)); +__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;
Signature · 1 line
2.47.3
PATCH v10 20/40 sched/deadline: Do not truncate a bandwidth to 32 bits in __dl_add/sub()
In reply to Mathieu Desnoyers__dl_add() and __dl_sub() spread a change of total_bw over the extra_bw of the active CPUs of the root domain. Commit daec57983670 ("sched/deadline: Reclaim bandwidth not used by dl tasks") added this, for the bandwidth of one task. That is at most 1 << 20, the bandwidth of one CPU.
But cpuset_can_attach() reserves the sum of the bandwidths of all the deadline tasks it moves, with dl_bw_alloc(). __dl_add() and __dl_sub() cast the bandwidth to s32 before the division. Past S32_MAX, more than 2048 CPUs of deadline bandwidth, the cast flips its sign. The extra_bw of each CPU then moves the wrong way. GRUB lets reclaiming tasks run more than the free bandwidth, or less. This was found by reading. It needs a machine with more than 2048 CPUs, so no case tests it.
Fix this by dividing on 64 bits, with div_u64().
The Sashiko AI review of the first series found this.
--- kernel/sched/deadline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/deadline.c +2 -2
@@ -205,7 +205,7 @@ void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus) { dl_b->total_bw -= tsk_bw;if (cpus) -__dl_update (dl_b, (s32 )tsk_bw / cpus); +__dl_update (dl_b,div_u64 (tsk_bw, cpus)); } static inline
@@ -213,7 +213,7 @@ void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus) { dl_b->total_bw += tsk_bw;if (cpus) -__dl_update (dl_b, -((s32 )tsk_bw / cpus)); +__dl_update (dl_b, -(s64 )div_u64 (tsk_bw, cpus)); } static inline bool
Signature · 1 line
2.47.3
PATCH v10 21/40 rtmutex: Continue the chain walk when a waiter's deadline donor changes
In reply to Mathieu DesnoyersA task boosted by a SCHED_DEADLINE waiter runs on the parameters of a donor, dl.pi_se. Commit 2279f540ea7d ("sched/deadline: Fix priority inheritance with multiple scheduling classes") made the donor follow the chain. rt_mutex_setprio() copies the donor of the top waiter to the owner. So the owner at the end of a chain points at the deadline task at its head. That task waits on no lock the owner holds.
The chain walk stops at a task whose waiter has the same sort keys as the task: the priority and the deadline. Then "there is no further priority adjustment necessary". Before 2279f540ea7d, this was enough. The refill read the parameters of the owner's own top waiter, which is blocked and so alive.
But a boost can change the donor of a blocked task and leave its keys as they were. The task keeps a deadline priority. It sleeps, so no refill moves its deadline. The walk then stops one lock too early.
M and O are SCHED_OTHER tasks. W and Z are SCHED_DEADLINE tasks. Z has the earlier deadline. L1, L1' and L2 are rt_mutexes:
O M W Z
locks L2 locks L1 and L1'
blocks on L1'
rt_mutex_setprio(M, W)
M->dl.pi_se = &W->dl
blocks on L2
rt_mutex_setprio(O, M)
O->dl.pi_se = &W->dl
blocks on L1
rt_mutex_setprio(M, Z)
M->dl.pi_se = &Z->dl
chain walk at M:
keys equal, stops
times out on L1'
exits
O runs on W's parameters, with a later deadline than Z's, while Z waits for it. After W exits, O's next refill would read W's freed task_struct.
A deboost has the same effect. Take a deadline task that a waiter with an earlier deadline boosts, and that blocks on a second lock. It keeps its own deadline. When that waiter leaves, rt_mutex_setprio() makes the task its own donor again. Its keys stay equal. A policy change in __setscheduler_dl_pi() can also change the donor of a blocked task with no change of keys. rt_mutex_adjust_pi() then skips the walk.
Setting up the deadline tasks needs CAP_SYS_NICE. The timeout of a FUTEX_LOCK_PI needs no privilege. The Sashiko AI review of this series found the deboost case.
Fix this by recording the donor in the waiter with the sort keys, in waiter_update_prio(). rt_waiter_up_to_date() compares both. The walk and rt_mutex_adjust_pi() use it. A new donor then takes the walk on to the next owner, and rt_mutex_setprio() gives that owner the new donor. The waiter's copy is only compared, never dereferenced. A task without a deadline priority is always its own donor, so its walks do not change.
A known limit remains. Two walks can overlap on a chain of three locks or more. The owners further down then keep the old donor until the first walk reaches them.
The case pi_chain_donor_leaves of deadline_bw_test tests the missed boost. W leaves SCHED_DEADLINE there, in place of exiting. It passes on v7.4-rc2, and without this patch. The case pi_chain_top_moves, added to deadline_bw_test later in this series, needs this patch and the next one. If one of the two is missing, another deadline task gets 30ms of 200ms in every run. With both, it passes.
--- kernel/locking/rtmutex.c | 22 ++++++++++++++++++++-- kernel/locking/rtmutex_api.c | 2 +- kernel/locking/rtmutex_common.h | 3 +++ 3 files changed, 24 insertions(+), 3 deletions(-)
kernel/locking/rtmutex.c +20 -2
@@ -369,10 +369,12 @@ static __always_inline void waiter_update_prio(struct rt_mutex_waiter *waiter, struct task_struct *task) { lockdep_assert_held(&waiter->lock->wait_lock); +lockdep_assert_held (&task->pi_lock);lockdep_assert (RB_EMPTY_NODE (&waiter->tree.entry)); waiter->tree.prio =__waiter_prio (task); waiter->tree.deadline = task->dl.deadline; + waiter->pi_se = task->dl.pi_se; }/*
@@ -433,6 +435,21 @@ static __always_inline int rt_waiter_node_equal(struct rt_waiter_node *left,return 1 ; } +/* +* Return true if @waiter is up to date with its task @task. Compare the +* sort keys and the deadline donor, which rt_mutex_setprio() copies from +* @task to the lock owner. A boost or a deboost of @task can change its +* donor and leave its keys as they were. The owner must then get the new +* donor: the old one can exit. The caller holds @task->pi_lock, which +* protects @task->dl.pi_se. @waiter->pi_se is never dereferenced. +*/ +static __always_inline bool +rt_waiter_up_to_date (struct rt_mutex_waiter *waiter,struct task_struct *task) +{ +return rt_waiter_node_equal (&waiter->tree,task_to_waiter_node (task)) && + waiter->pi_se == task->dl.pi_se; +} +static inline bool rt_mutex_steal (struct rt_mutex_waiter *waiter,struct rt_mutex_waiter *top_waiter) {
@@ -824,12 +841,13 @@ static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,/* * If the waiter priority is the same as the task priority -* then there is no further priority adjustment necessary. If +* then there is no further priority adjustment necessary. The +* task must also pass on the same deadline donor. If * deadlock detection is off, we stop the chain walk. If its * enabled we continue, but stop the requeueing in the chain * walk. */ -if (rt_waiter_node_equal (&waiter->tree,task_to_waiter_node (task))) { +if (rt_waiter_up_to_date (waiter, task)) {if (!detect_deadlock) goto out_unlock_pi;else
kernel/locking/rtmutex_api.c +1 -1
@@ -510,7 +510,7 @@ void __sched rt_mutex_adjust_pi(struct task_struct *task)raw_spin_lock_irqsave (&task->pi_lock, flags); waiter = task->pi_blocked_on; -if (!waiter ||rt_waiter_node_equal (&waiter->tree,task_to_waiter_node (task))) { +if (!waiter ||rt_waiter_up_to_date (waiter, task)) {raw_spin_unlock_irqrestore (&task->pi_lock, flags);return ; }
kernel/locking/rtmutex_common.h +3 -0
@@ -43,6 +43,8 @@ struct rt_waiter_node {* @pi_tree: node to enqueue into the mutex owner waiters tree * @task: task reference to the blocked task * @lock: Pointer to the rt_mutex on which the waiter blocks +* @pi_se: The deadline donor of @task when @tree was last updated. +* Only compared, never dereferenced. * @wake_state: Wakeup state to use (TASK_NORMAL or TASK_RTLOCK_WAIT) * @ww_ctx: WW context pointer *
@@ -54,6 +56,7 @@ struct rt_mutex_waiter {struct rt_waiter_node pi_tree;struct task_struct *task;struct rt_mutex_base *lock; +struct sched_dl_entity *pi_se;unsigned int wake_state;struct ww_acquire_ctx *ww_ctx; };
Signature · 1 line
2.47.3
PATCH v10 22/40 rtmutex: Do not stop the chain walk at an owner whose top waiter changed
In reply to Mathieu Desnoyersrt_mutex_adjust_prio_chain() stops the walk at an owner whose top pi waiter is not the top waiter of the lock just walked. That lock then does not set the owner's priority, so there is nothing to pass on.
But the step before can change the owner's top pi waiter. It requeues a waiter whose priority dropped. The top pi waiter of the owner can then be a waiter on another lock. The owner's priority, or its deadline donor, changed. The walk still stops at it, and the next owner keeps the old ones.
O and M are SCHED_OTHER tasks. W and Z are SCHED_DEADLINE tasks. W has the earlier deadline. X is a SCHED_FIFO task:
O M W Z X
locks L2 locks L1 and L1'
blocks on L1'
M->dl.pi_se = &W->dl
blocks on L2
O->dl.pi_se = &W->dl
blocks on L1
sched_setscheduler(SCHED_OTHER)
rt_mutex_adjust_pi(W)
M->dl.pi_se = &Z->dl
walk at M: L1' is not M's top
stops
blocks on L1'
walk stops at M
times out on L1'
not the top waiter: no walk
exits
runs out of runtime
refill reads O->dl.pi_se:
W's freed task_struct
If W and Z are SCHED_FIFO tasks instead, O stays boosted to W's old priority until it unlocks L2.
A second check next to it stops the walk at an owner that has no pi waiters left. A waiter that leaves can cause this. remove_waiter() then walks on from the owner, but that walk can be preempted and come late.
O, M and X are SCHED_OTHER tasks. W is a SCHED_DEADLINE task. Another task, S, changes the policy of W:
O M S W X
locks L2 locks L1
blocks on L2
blocks on L1
M, O get W's parameters
blocks on L1
sched_setscheduler(W)
walk at L1: W behind X,
M deboosted
times out on L1
not top waiter: no walk
times out on L1
M: no pi waiters walk from M:
preempted
walk at M:
no pi waiters, stops
exits
runs out of runtime
refill reads O->dl.pi_se:
W's freed task_struct
The next patch keeps the donor's task_struct alive as long as a boost points at it.
Remove both checks. The next check already stops the walk when the owner's waiter is up to date. An out of date waiter is now requeued, and the walk goes on to the next owner.
The cases pi_chain_top_moves and pi_chain_top_moves_rt, added to deadline_bw_test later in this series, test the first case. pi_chain_top_moves_rt fails without this patch and passes with it. pi_chain_top_moves also needs the previous patch. No case tests the second one: it needs a walk preempted at the right step.
The Sashiko AI review of this series found both cases.
--- kernel/locking/rtmutex.c | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-)
kernel/locking/rtmutex.c +7 -20
@@ -818,27 +818,14 @@ static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task, detect_deadlock = false;/* -* Drop out, when the task has no waiters. Note, -* top_waiter can be NULL, when we are in the deboosting -* mode! +* Do not stop at a task that has no pi waiters, or whose top pi +* waiter is not top_waiter. The previous step can have made a +* waiter on another lock the top pi waiter of the task. A waiter +* can also have left meanwhile, and its own walk can be late. The +* priority of the task changed then, and the next owner must +* follow. The test below tells whether there is something to pass +* on. */ -if (top_waiter) { -if (!task_has_pi_waiters (task)) - goto out_unlock_pi; -/* -* If deadlock detection is off, we stop here if we -* are not the top pi waiter of the task. If deadlock -* detection is enabled we continue, but stop the -* requeueing in the chain walk. -*/ -if (top_waiter !=task_top_pi_waiter (task)) { -if (!detect_deadlock) - goto out_unlock_pi; -else - requeue = false; - } - } -/* * If the waiter priority is the same as the task priority * then there is no further priority adjustment necessary. The
Signature · 1 line
2.47.3
PATCH v10 23/40 sched/deadline: Hold a reference on the deadline donor of a boosted task
In reply to Mathieu DesnoyersA task boosted by a SCHED_DEADLINE waiter runs on the parameters of a donor, dl.pi_se. Since commit 2279f540ea7d ("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.
M, O and P are SCHED_OTHER tasks. W is a SCHED_DEADLINE task:
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 reads P->dl.pi_se:
W's freed task_struct
P 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 a FUTEX_LOCK_PI needs 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. put_task_struct() frees the task through call_rcu(), so it can run under the rq lock. A late walk can still leave P on the parameters of an exited donor for a while. P now reads them from valid memory.
dl_set_donor() reads the donor of the top waiter without the waiter's locks. Another CPU can change that donor at the same time, and put the last reference on the old one. So dl_set_donor() takes its reference with tryget_task_struct(). If that fails, it reads the new donor again.
put_task_struct() frees every task through call_rcu() since v6.17:
commit 8671bad873eb ("sched: Do not call __put_task_struct() on rt if pi_blocked_on is set"). Before it, put_task_struct() frees the task at once without PREEMPT_RT. So stable trees older than v6.17 need that commit first.
__dl_clear_params() no longer resets the donor. switched_from_dl() can call it on a boosted task, before the deboost drops the reference. init_dl_entity() resets it for a new task, whose copy of the pointer holds no reference.
No case tests the use after free: it needs a walk preempted at the right step.
--- kernel/sched/core.c | 8 +++--- kernel/sched/deadline.c | 12 ++++++--- kernel/sched/sched.h | 56 +++++++++++++++++++++++++++++++++++++++++ kernel/sched/syscalls.c | 4 +-- 4 files changed, 70 insertions(+), 10 deletions(-)
kernel/sched/core.c +4 -4
@@ -7969,19 +7969,19 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)if (!dl_prio (p->normal_prio) || (pi_task &&dl_prio (pi_task->prio) &&dl_entity_preempt (&pi_task->dl, &p->dl))) { - p->dl.pi_se = pi_task->dl.pi_se; +dl_set_donor (&p->dl, pi_task); scope->flags |=ENQUEUE_REPLENISH ; }else { - p->dl.pi_se = &p->dl; +dl_clear_donor (&p->dl); } }else if (rt_prio (prio)) {if (dl_prio (oldprio)) - p->dl.pi_se = &p->dl; +dl_clear_donor (&p->dl);if (oldprio < prio) scope->flags |=ENQUEUE_HEAD ; }else {if (dl_prio (oldprio)) - p->dl.pi_se = &p->dl; +dl_clear_donor (&p->dl);if (rt_prio (oldprio)) p->rt.timeout =0 ; }
kernel/sched/deadline.c +8 -4
@@ -5080,10 +5080,6 @@ static void __dl_clear_params(struct sched_dl_entity *dl_se) dl_se->dl_defer_running =0 ; dl_se->dl_defer_armed =0 ; dl_se->dl_bw_oncpu =0 ; - -#ifdef CONFIG_RT_MUTEXES - dl_se->pi_se = dl_se; -#endif }void init_dl_entity (struct sched_dl_entity *dl_se)
@@ -5098,6 +5094,14 @@ void init_dl_entity(struct sched_dl_entity *dl_se)*/ dl_se->dl_bw =0 ;__dl_clear_params (dl_se); +#ifdef CONFIG_RT_MUTEXES +/* +* The copy points at the donor of the parent, without a reference. +* Reset it here only: __dl_clear_params() can run on a boosted entity, +* from switched_from_dl() before the deboost drops its reference. +*/ + dl_se->pi_se = dl_se; +#endif dl_se->dl_bw_cpu =-1 ; dl_se->dl_bw_resv_cpu =-1 ;INIT_LIST_HEAD (&dl_se->dl_departed_node);
kernel/sched/sched.h +56 -0
62 lines not on this page · raw message
kernel/sched/syscalls.c +2 -2
@@ -300,7 +300,7 @@ static inline void __setscheduler_dl_pi(int newprio, int policy,struct task_struct *pi_task =rt_mutex_get_top_task (p);if (pi_task) { - p->dl.pi_se = pi_task->dl.pi_se; +dl_set_donor (&p->dl, pi_task); scope->flags |=ENQUEUE_REPLENISH ; } }
@@ -310,7 +310,7 @@ static inline void __setscheduler_dl_pi(int newprio, int policy,* the class. Drop its boost then, as rt_mutex_setprio() does. */ if (!dl_prio (newprio)) - p->dl.pi_se = &p->dl; +dl_clear_donor (&p->dl); }#else /* !CONFIG_RT_MUTEXES */ static inline void __setscheduler_dl_pi(int newprio, int policy,
Signature · 1 line
2.47.3
PATCH v10 24/40 selftests/sched: Test the rtmutex chain walk in deadline_bw_test
In reply to Mathieu DesnoyersAdd pi_chain_top_moves and pi_chain_top_moves_rt to deadline_bw_test. They test the two rtmutex fixes earlier in this series. In both, a chain walk changes the top waiter of an owner to a waiter of another lock it holds. The walk must go on to the next owner. That owner must then get the new donor, or the new priority. The first case uses deadline tasks, the second RT tasks. The comment above each case gives its scenario.
On v7.4-rc2, in 8 VMs of 8 CPUs, pi_chain_top_moves_rt fails in every VM.pi_chain_top_moves passes there. It fails with the first series, until "rtmutex: Continue the chain walk when a waiter's deadline donor changes" and "rtmutex: Continue the walk when the owner's top waiter changes lock". It needs both.
With the fixes up to here, both cases pass wherever they run.
--- .../sched/deadline/deadline_bw_test.c | 293 ++++++++++++++++++ 1 file changed, 293 insertions(+)
tools/testing/selftests/sched/deadline/deadline_bw_test.c +293 -0
@@ -2522,6 +2522,297 @@ static int pi_chain_donor_leaves(void)return ret; } +/* +* The threads of top_moves(): O, M, W, Z and Y. top_tid[] holds their +* tids. top_lock is L1'. top_y_ns is the CPU time Y gets in O's window, +* or -1. +*/ +enum {TM_O ,TM_M ,TM_W ,TM_Z ,TM_Y ,TM_N }; +static _Atomicpid_t top_tid[TM_N ]; +static pthread_mutex_t top_lock; +static atomic_int top_err, top_rt, top_m_locking, top_w_locking, top_y_stop; +static _Atomiclong long top_y_ns; +static sem_t top_o_go, top_m_go; + +/* The priority /proc shows for a SCHED_FIFO task of priority @p. */ +#define FIFO_TASK_PRIO (p) (-1 - (p)) + +/* +* O: take pi_lock2 (L2), and sleep until top_o_go. With Y (@arg), spin +* 200ms and measure the CPU time Y gets meanwhile. Then unlock L2. +*/ +static void *top_o (void *arg) +{ +uint64_t t0, y0; + +if (pthread_mutex_lock (&pi_lock2)) { + top_err =1 ; +return arg; + } + top_tid[TM_O ] =gettid_ ();/* O holds L2 */ +sem_wait_sig (&top_o_go); +if (arg) { + y0 =cpu_time_of (*(pthread_t *)arg); + t0 =now_ns (); +while (now_ns () - t0 <2 *PERIOD ) + ; + top_y_ns =cpu_time_of (*(pthread_t *)arg) - y0; + } +pthread_mutex_unlock (&pi_lock2); +return NULL ; +} + +/* +* M: take pi_lock (L1) and top_lock (L1'). At top_m_go, block on L2, +* which O holds. Unlock L2, L1 and L1' once it gets L2. +*/ +static void *top_m (void *arg) +{ +if (pthread_mutex_lock (&pi_lock)) { + top_err =1 ; +return arg; + } +if (pthread_mutex_lock (&top_lock)) { +pthread_mutex_unlock (&pi_lock); + top_err =1 ; +return arg; + } + top_tid[TM_M ] =gettid_ ();/* M holds L1 and L1' */ +sem_wait_sig (&top_m_go); + top_m_locking =1 ; +if (pthread_mutex_lock (&pi_lock2)) + top_err =1 ; +else +pthread_mutex_unlock (&pi_lock2); +pthread_mutex_unlock (&pi_lock); +pthread_mutex_unlock (&top_lock); +return arg; +} + +/* +* W and Z: W blocks on L1', Z on L1. @arg is 1 for W. With top_rt, W +* becomes SCHED_FIFO 50 and Z SCHED_FIFO 20. Otherwise W becomes a +* deadline task of 2ms every 80ms and Z one of 5ms every 200ms. The main +* thread switches W to SCHED_OTHER while it waits. +*/ +static void *top_waiter (void *arg) +{ +int w = (long )arg; +struct sched_param sp = { .sched_priority = w ?50 :20 }; +uint64_t runtime = (w ?2 :5 ) *NSEC_PER_MSEC ; +pthread_mutex_t *l = w ? &top_lock : &pi_lock; + +if (top_rt ?sched_setscheduler (0 ,SCHED_FIFO , &sp) : +set_deadline (0 , runtime,40 * runtime)) { + top_err =1 ; +return arg; + } + top_tid[w ?TM_W :TM_Z ] =gettid_ (); +if (w) + top_w_locking =1 ; +if (pthread_mutex_lock (l)) + top_err =1 ; +else +pthread_mutex_unlock (l); +if (set_other (0 )) + top_err =1 ; +return arg; +} + +/* Y: become a deadline task of 30ms every 100ms, and spin. */ +static void *top_y (void *arg) +{ +if (set_deadline (0 ,30 *NSEC_PER_MSEC ,PERIOD )) { + top_err =1 ; +return arg; + } + top_tid[TM_Y ] =gettid_ (); +while (!top_y_stop) + ; +return arg; +} + +/* +* A chain walk must go on past a task whose top waiter moves to another +* lock. The priority of that task changes, and the next owner must +* follow. +* +* L1 is pi_lock, L1' is top_lock and L2 is pi_lock2. All threads run on +* the CPU of P. W and Z set their own policy when they start. With @rt, +* W is SCHED_FIFO 50 and Z SCHED_FIFO 20. Without @rt, W is a deadline +* task of 2ms every 80ms and Z one of 5ms every 200ms. W has the earlier +* deadline. O and M are SCHED_OTHER. Without @rt, Y is a deadline task +* of 30ms every 100ms. It spins all along. +* +* 1. O takes L2, and sleeps. +* 2. M takes L1 and L1', and sleeps. +* 3. W blocks on L1'. W boosts M. +* 4. M blocks on L2. M passes W on to O. +* 5. Z blocks on L1. W stays the top waiter of M. +* 6. The main thread switches W to SCHED_OTHER. The chain walk requeues +* W on L1'. Z on L1 becomes the top waiter of M, and Z boosts M. The +* walk must go on to O. Z must boost O too. +* +* With @rt, the case reads O's priority. It must be the one of Z. With +* the walk stopped at M, O keeps the priority of W. +* +* Without @rt, W sleeps and has run almost nothing. So its parameters +* are cleared at once. 250ms later, the main thread wakes O up. O +* measures the CPU time Y gets over the next 200ms, and unlocks L2. +* +* With Z as its donor, O gets 5ms every 200ms. Y runs at the start of +* each of its periods, and gets its 60ms. With W as its donor, O runs on +* cleared parameters. Each refill gives it no runtime and a deadline of +* about now. O then runs before Y until Y's deadline is past. Y runs +* only then, one period late, and gets about 30ms. The case fails if Y +* gets less than 45ms. +* +* W does not exit before O unlocks L2. It waits for L1', which M +* releases after it gets L2. +*/ +static int top_moves (int rt) +{ +int ret =KSFT_FAIL , i, have[TM_N ] = {0 }; +int boost = rt ?FIFO_TASK_PRIO (50 ) :DL_TASK_PRIO ; +pthread_mutexattr_t a; +pthread_t t[TM_N ]; +long long y_ns; +int err, o_prio =0 ; + +if (pi_init ()) +return KSFT_SKIP ; +if (pi_init_lock2 ()) { +pi_fini (); +return KSFT_SKIP ; + } + err =pthread_mutexattr_init (&a); +if (!err) { + err =pthread_mutexattr_setprotocol (&a,PTHREAD_PRIO_INHERIT ); +if (!err) + err =pthread_mutex_init (&top_lock, &a); +pthread_mutexattr_destroy (&a); + } +if (err) { +ksft_print_msg ("no third priority inheritance mutex: %s\n" , +strerror (err)); + ret =KSFT_SKIP ; + goto out_lock2; + } +if (sem_init (&top_o_go,0 ,0 ) ||sem_init (&top_m_go,0 ,0 )) { +ksft_print_msg ("sem_init: %s\n" ,strerror (errno)); + goto out_lock3; + } +for (i =0 ; i <TM_N ; i++) + top_tid[i] =0 ; + top_err =0 ; + top_rt = rt; + top_m_locking =0 ; + top_w_locking =0 ; + top_y_stop =0 ; + top_y_ns =-1 ; +if (!rt) { + have[TM_Y ] = !pthread_create (&t[TM_Y ],NULL , top_y,NULL ); +if (!have[TM_Y ] ||wait_for (1000 , top_tid[TM_Y ] || top_err)) { +ksft_print_msg ("Y never became a deadline task\n" ); + goto out; + } + } + have[TM_O ] = !pthread_create (&t[TM_O ],NULL , top_o, + rt ?NULL : &t[TM_Y ]); +if (!have[TM_O ] ||wait_for (1000 , top_tid[TM_O ] || top_err)) { +ksft_print_msg ("O never took L2\n" ); + goto out; + } + have[TM_M ] = !pthread_create (&t[TM_M ],NULL , top_m,NULL ); +if (!have[TM_M ] ||wait_for (1000 , top_tid[TM_M ] || top_err)) { +ksft_print_msg ("M never took L1 and L1'\n" ); + goto out; + } + have[TM_W ] = !pthread_create (&t[TM_W ],NULL , top_waiter, (void *)1L ); +if (!have[TM_W ] || +wait_for (1000 , (top_w_locking && +proc_state_of (top_tid[TM_W ]) =='S' ) || top_err)) { +ksft_print_msg ("W never blocked on L1'\n" ); + goto out; + } +sem_post (&top_m_go); +if (wait_for (1000 , (top_m_locking &&prio_of (top_tid[TM_O ]) == boost) || + top_err)) { +ksft_print_msg ("O was not boosted through M\n" ); + goto out; + } + have[TM_Z ] = !pthread_create (&t[TM_Z ],NULL , top_waiter, (void *)0L ); +if (!have[TM_Z ] || +wait_for (1000 , (top_tid[TM_Z ] && +proc_state_of (top_tid[TM_Z ]) =='S' ) || top_err)) { +ksft_print_msg ("Z never blocked on L1\n" ); + goto out; + } +sleep_ns (10 *NSEC_PER_MSEC );/* the chain walk of Z is done */ + err = top_err ? -EINVAL :set_other (top_tid[TM_W ]); +if (err) { +ksft_print_msg ("W cannot leave its class: %s\n" , +strerror (-err)); + goto out; + } +if (rt) { +sleep_ns (10 *NSEC_PER_MSEC ); +if (prio_of (top_tid[TM_M ]) !=FIFO_TASK_PRIO (20 )) { +ksft_print_msg ("Z does not boost M\n" ); + goto out; + } + o_prio =prio_of (top_tid[TM_O ]); + }else { +sleep_ns (250 *NSEC_PER_MSEC ); + } + ret =KSFT_PASS ; +out: +/* Let every thread end: O unlocks L2, then M unlocks L1 and L1'. */ +sem_post (&top_m_go); +sem_post (&top_o_go); +for (i =TM_O ; i <TM_Y ; i++) +if (have[i]) +join (t[i]); + top_y_stop =1 ; +if (have[TM_Y ]) +join (t[TM_Y ]); + y_ns = top_y_ns; +if (top_err) { +ksft_print_msg ("a thread of the chain failed\n" ); + ret =KSFT_FAIL ; + }else if (ret ==KSFT_PASS && rt && o_prio !=FIFO_TASK_PRIO (20 )) { +ksft_print_msg ("O runs at priority %d, not %d: W still boosts it\n" , + o_prio,FIFO_TASK_PRIO (20 )); + ret =KSFT_FAIL ; + }else if (ret ==KSFT_PASS && !rt && + y_ns <45 * (long long )NSEC_PER_MSEC ) { +ksft_print_msg ("Y ran %lld ms of 200: W is still the donor of O\n" , + y_ns / (long long )NSEC_PER_MSEC ); + ret =KSFT_FAIL ; + } +sem_destroy (&top_o_go); +sem_destroy (&top_m_go); +out_lock3: +pthread_mutex_destroy (&top_lock); +out_lock2: +pthread_mutex_destroy (&pi_lock2); +sem_destroy (&holder_go); +pi_fini (); +return ret; +} + +/* W and Z are deadline tasks. */ +static int pi_chain_top_moves (void ) +{ +return top_moves (0 ); +} + +/* W and Z are SCHED_FIFO tasks. */ +static int pi_chain_top_moves_rt (void ) +{ +return top_moves (1 ); +} +/* cpuset moves, from the main process, outside P */ /* Shared with the processes of a case: atomic in shared memory. */
@@ -10218,6 +10509,8 @@ static const struct p_case { { "return_asleep_new_params", return_asleep_new_params, MEASURED_IN_P }, { "deboost_blocked_keeps_order", deboost_blocked_keeps_order, MEASURED_IN_P }, { "pi_chain_donor_leaves", pi_chain_donor_leaves, MEASURED_IN_P }, + { "pi_chain_top_moves", pi_chain_top_moves, MEASURED_IN_P }, + { "pi_chain_top_moves_rt", pi_chain_top_moves_rt, MEASURED_IN_P }, { "move_asleep", move_asleep, FROM_OUTSIDE }, { "move_throttled", move_throttled, FROM_OUTSIDE }, { "move_non_contending", move_non_contending, FROM_OUTSIDE },
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v10 24/40] selftests/sched: Test the rtmutex chain walk in deadline_bw_test
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] selftests/sched: top_moves() fails instead of skipping without RT runtime --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests/sched: top_moves() fails instead of skipping without RT runtime
--
commit 6100a36909c549fb0079a550f7245c5c073ca05e
Author: Mathieu Desnoyers <[email protected]>
selftests/sched: Test the rtmutex chain walk in deadline_bw_test
This commit adds the pi_chain_top_moves and pi_chain_top_moves_rt test cases to deadline_bw_test to verify rtmutex priority inheritance chain walks when an owner's top waiter moves to another lock it holds.
Quoted 4 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +0
-0
[ ... ]
Quoted 8 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +8
-0+/* +* W and Z: W blocks on L1', Z on L1. @arg is 1 for W. With top_rt, W +* becomes SCHED_FIFO 50 and Z SCHED_FIFO 20. Otherwise W becomes a +* deadline task of 2ms every 80ms and Z one of 5ms every 200ms. The main +* thread switches W to SCHED_OTHER while it waits. +*/ +static void *top_waiter (void *arg) +{
tools/testing/selftests/sched/deadline/deadline_bw_test.c static int pi_chain_donor_leaves(void) · line 2609
Quoted 10 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +10
-0+int w = (long )arg; +struct sched_param sp = { .sched_priority = w ?50 :20 }; +uint64_t runtime = (w ?2 :5 ) *NSEC_PER_MSEC ; +pthread_mutex_t *l = w ? &top_lock : &pi_lock; + +if (top_rt ?sched_setscheduler (0 ,SCHED_FIFO , &sp) : +set_deadline (0 , runtime,40 * runtime)) { + top_err =1 ; +return arg; + }
[Severity: Medium]
Should top_moves() return KSFT_SKIP instead of KSFT_FAIL when sched_setscheduler(0, SCHED_FIFO, &sp) fails with EPERM?
pi_chain_top_moves_rt runs with MEASURED_IN_P inside child cgroup P. When CONFIG_RT_GROUP_SCHED is enabled and the cgroup v2 cpu controller is active in the root cgroup, child cgroups have no RT runtime, so sched_setscheduler() returns EPERM and sets top_err = 1, failing the test. Other SCHED_FIFO tests in P check for EPERM and skip:
tools/testing/selftests/sched/deadline/deadline_bw_test.c:server_client() {
...
if (rt && sched_setscheduler(0, SCHED_FIFO, &sp)) {
/* With RT group scheduling, P can have no RT runtime. */
if (errno == EPERM)
mv_state[6] = 1;
_exit(1);
}
...
}Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
Re: [PATCH v10 24/40] selftests/sched: Test the rtmutex chain walk in deadline_bw_test
Quoted 2 lines
Should
top_moves()returnKSFT_SKIPinstead ofKSFT_FAILwhensched_setscheduler(0, SCHED_FIFO, &sp)fails with EPERM?
Yes. In v11, pi_chain_top_moves_rt will skip when SCHED_FIFO is refused with EPERM in P.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
PATCH v10 25/40 sched/deadline: Reset the donor's runtime at a deboost in the class
In reply to Mathieu DesnoyersA task boosted by a deadline waiter, its donor, runs on the donor's reservation. Commit 2d3d891d3344 ("sched/deadline: Add SCHED_DEADLINE inheritance logic") uses the waiter's parameters "during the whole period it holds the pi-lock".
A boosted task that runs out of runtime is not throttled. replenish_dl_entity() refills it from the waiter's parameters: it adds the waiter's runtime, and moves the deadline by the waiter's period. From then on, the runtime and the deadline of the task are not those of its own reservation.
A task that leaves the class at its deboost does not keep them. "sched/deadline: Reset the deadline and the runtime with the parameters", of the first series, resets them for a task with no parameters. For a task with parameters, they are cleared with the parameters at its 0-lag time.
But the boosted task can be a SCHED_DEADLINE task itself, when the waiter has an earlier deadline. It stays in the class at its deboost. rt_mutex_setprio() only resets dl.pi_se. enqueue_dl_entity() keeps a deadline that is ahead. So a task that was refilled keeps the waiter's runtime and deadline after the unlock. A task that sched_setattr() makes SCHED_DEADLINE while boosted ends its boost the same way.
In one period, the task can run what the waiter left on top of its own runtime. Other deadline tasks can miss deadlines.
Fix this by recording that refill in a new flag, dl_donor_refill. If the boost ends in the class with the flag set, start a new period at the deboost. The next enqueue is too late for a task blocked on anotherrt_mutex: the chain walk copies its deadline into its waiter first.
A task asleep since the refill has its inactive timer armed from the waiter's runtime and deadline. Its 0-lag time can then be up to the waiter's period ahead, and the task keeps counting in running_bw until then. If it leaves the class meanwhile, its bandwidth stays in total_bw until then too. So the deboost cancels that timer, and works out the 0-lag time of the new period, which is now.
The task can still hold a lock that another deadline task waits on. The new deadline can be later than that waiter's. rt_mutex_setprio() then boosts the task again.
A task that was not refilled keeps its runtime and its deadline. They are those of its own reservation. A new period there would give it a full runtime at every contended unlock.
The Sashiko AI review of this series found the sched_setattr() case and the inactive timer. These cases of deadline_bw_test test this fix:
- deboost_blocked_keeps_order and deboost_keeps_own_runtime;
- deboost_in_class_resets_period, deboost_after_donor_refill and deboost_in_asleep_after_refill, added later in this series.
A flag is needed: a test of the runtime against dl_runtime misses a refill that leaves less than dl_runtime. In deboost_after_donor_refill the owner runs past its own runtime with the lock, and the refill leaves it less than dl_runtime. With such a test, the owner had less than 1ms of its 5ms after the unlock, in every run. With the flag, it gets its own 5ms.
--- include/linux/sched.h | 6 +++++ kernel/sched/core.c | 16 +++++++++--- kernel/sched/deadline.c | 57 +++++++++++++++++++++++++++++++++++++++++ kernel/sched/sched.h | 9 +++++++ kernel/sched/syscalls.c | 2 +- 5 files changed, 86 insertions(+), 4 deletions(-)
include/linux/sched.h +6 -0
@@ -683,6 +683,11 @@ struct sched_dl_entity {* @dl_overrun tells if the task asked to be informed about runtime * overruns. * +* @dl_donor_refill tells if a boosted task had its runtime refilled +* from the parameters of its donor. Its runtime and deadline are +* then not those of its own reservation. The end of the boost +* clears it. +* * @dl_server tells if this is a server entity. * * @dl_server_active tells if the dlserver is active(started).
@@ -715,6 +720,7 @@ struct sched_dl_entity { unsigned int dl_yielded : 1; unsigned int dl_non_contending : 1; unsigned int dl_overrun : 1; + unsigned int dl_donor_refill : 1; unsigned int dl_server : 1; unsigned int dl_server_active : 1; unsigned int dl_defer : 1;
kernel/sched/core.c +13 -3
@@ -7972,16 +7972,26 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)dl_set_donor (&p->dl, pi_task); scope->flags |=ENQUEUE_REPLENISH ; }else { -dl_clear_donor (&p->dl); +dl_deboost (&p->dl); +/* +* dl_deboost() can start a new period. Its +* deadline can then be after the top waiter's. +* Boost the task again then. +*/ +if (pi_task &&dl_prio (pi_task->prio) && +dl_entity_preempt (&pi_task->dl, &p->dl)) { +dl_set_donor (&p->dl, pi_task); + scope->flags |=ENQUEUE_REPLENISH ; + } } }else if (rt_prio (prio)) {if (dl_prio (oldprio)) -dl_clear_donor (&p->dl); +dl_drop_boost (&p->dl);if (oldprio < prio) scope->flags |=ENQUEUE_HEAD ; }else {if (dl_prio (oldprio)) -dl_clear_donor (&p->dl); +dl_drop_boost (&p->dl);if (rt_prio (oldprio)) p->rt.timeout =0 ; }
kernel/sched/deadline.c +57 -0
98 lines not on this page · raw message
kernel/sched/sched.h +9 -0
@@ -411,6 +411,15 @@ static inline void dl_clear_donor(struct sched_dl_entity *dl_se) {dl_replace_donor (dl_se, dl_se); } + +extern void dl_deboost (struct sched_dl_entity *dl_se); + +/* End the boost of @dl_se. The caller holds the rq lock of its task. */ +static inline void dl_drop_boost (struct sched_dl_entity *dl_se) +{ +dl_clear_donor (dl_se); + dl_se->dl_donor_refill =0 ; +}#endif extern void __getparam_dl (struct task_struct *p,struct sched_attr *attr,unsigned int flags);extern bool __checkparam_dl (const struct sched_attr *attr);
kernel/sched/syscalls.c +1 -1
@@ -310,7 +310,7 @@ static inline void __setscheduler_dl_pi(int newprio, int policy,* the class. Drop its boost then, as rt_mutex_setprio() does. */ if (!dl_prio (newprio)) -dl_clear_donor (&p->dl); +dl_drop_boost (&p->dl); }#else /* !CONFIG_RT_MUTEXES */ static inline void __setscheduler_dl_pi(int newprio, int policy,
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v10 25/40] sched/deadline: Reset the donor's runtime at a deboost in the class
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] sched/deadline: dl_deboost() starts period for sleeping constrained task --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] sched/deadline: dl_deboost() starts period for sleeping constrained task
--
commit 280104d364cfcdeb61df32b73bafd44ef91c8ae1
Author: Mathieu Desnoyers <[email protected]>
sched/deadline: Reset the donor's runtime at a deboost in the class
This commit records whether a boosted deadline entity had its runtime refilled from its donor's parameters in dl_donor_refill, and starts a new period from the entity's own reservation when deboosting within SCHED_DEADLINE if that flag is set.
Quoted 29 lines
kernel/sched/deadline.c +21
-0@@ -93,12 +93,62 @@ static inline bool is_dl_boosted(struct sched_dl_entity *dl_se) {return pi_of (dl_se) != dl_se; } + +/* +* Record that @dl_se had its runtime refilled from the parameters of its +* donor, if it is boosted. +*/ +static inline void dl_note_donor_refill (struct sched_dl_entity *dl_se) +{ +if (is_dl_boosted (dl_se)) + dl_se->dl_donor_refill =1 ; +} + +static void task_non_contending (struct sched_dl_entity *dl_se,bool dl_task); + +/* +* End the boost of @dl_se, whose task stays in the class. If the boost +* refilled its runtime from the donor's parameters, start a new period +* now. The next enqueue is too late for a task blocked on another +* rt_mutex: the chain walk copies its deadline into its waiter node +* first. Without a refill, the task keeps the runtime and the deadline +* of its own reservation. +*
kernel/sched/deadline.c static inline bool is_dl_boosted(struct sched_dl_entity *dl_se) · line 126
Quoted 10 lines
kernel/sched/deadline.c +10
-0+* The caller holds the rq lock and the pi_lock of the task, and has +* updated the rq clock. +*/ +void dl_deboost (struct sched_dl_entity *dl_se) +{ +struct rq *rq =rq_of_dl_se (dl_se); + +if (dl_se->dl_donor_refill) { + dl_se->deadline =rq_clock (rq) + dl_se->dl_deadline; + dl_se->runtime = dl_se->dl_runtime;
[Severity: Medium]
Does starting a new period in dl_deboost() while the task is asleep cause a constrained-deadline task (dl_deadline < dl_period) to lose runtime or be throttled when it later wakes up?
If a deadline waiter times out on an rt_mutex held by a sleeping constrained-deadline task with dl_donor_refill set (and not blocked on another rt_mutex), dl_deboost() sets dl_se->deadline = t_deboost + dl_deadline and dl_se->runtime = dl_runtime while the task sleeps. When the task later wakes up at t_wake:
- if t_deboost < t_wake <= t_deboost + dl_deadline, dl_entity_overflow() is true because dl_se->runtime == dl_runtime while remaining time to deadline is less than dl_deadline, so update_dl_entity() calls update_dl_revised_wakeup() and scales down dl_se->runtime for the time spent sleeping since t_deboost;
- if t_deboost + dl_deadline < t_wake < t_deboost + dl_period, enqueue_dl_entity() -> dl_check_constrained_dl() sees deadline < t_wake < dl_next_period(dl_se) and throttles the waking task until t_deboost + dl_period, even though it did not run during that period.
Later in this series, commit "sched/deadline: Check the runtime of a task whose parameters change" avoids starting a period at deboost for dl_new_params when the task is neither queued nor pi-blocked, and commit "sched/deadline: Boost an owner that cannot run on its own reservation" restores the task's saved reservation when dl_boost_held is set, but the dl_donor_refill path in dl_deboost() still starts a period while asleep.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=25
Re: [PATCH v10 25/40] sched/deadline: Reset the donor's runtime at a deboost in the class
Quoted 3 lines
Does starting a new period in
dl_deboost()while the task is asleep cause a constrained-deadline task (dl_deadline<dl_period) to lose runtime or be throttled when it later wakes up?
Yes, by reading, for a task asleep on anything but an rt_mutex. Its wakeup then trims the runtime of that period, or throttles it in its gap. The task can lose part of a period. It never gains any. Moving the period to the wakeup needs a design of its own. v11 will leave it, and list it as a known limit.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
PATCH v10 26/40 sched/deadline: Replenish a throttled task boosted while asleep
In reply to Mathieu DesnoyersA deadline task can be throttled as it blocks, with an rt_mutex held. Its replenishment timer is then armed. A deadline waiter can boost the task while it sleeps.
Commit feff2e65efd8 ("sched/deadline: Unthrottle PI boosted threads while enqueuing") lets a boost override the throttle. enqueue_task_dl() lifts the throttle of a boosted task, and dl_task_timer() ignores a boosted task. This covers a task that is enqueued while boosted. But a boost of a sleeping task does not enqueue it.
T is a SCHED_DEADLINE task that holds the rt_mutex L. W is a SCHED_DEADLINE task with an earlier deadline:
T W T's timer
uses up its runtime, sleeps
dl_throttled = 1
start_dl_timer()
pthread_mutex_timedlock(L)
rt_mutex_setprio(T, W)
T sleeps: no enqueue
dl_task_timer()
T is boosted: returns
times out
rt_mutex_setprio(T, NULL)
wakes up
enqueue_dl_entity()
dl_throttled: waits for the timer
The timer has already fired. T would never run again, not even to die of SIGKILL. Every later waiter on L would hang too. This was found by reading.
Fix this by returning early in dl_task_timer() only for a boosted task that is queued. A boosted task that sleeps then takes the existing path for a throttled task that is not queued: it is replenished.
The refill reads the parameters of the donor, from the timer. So this relies on two earlier patches. "sched/deadline: Do not replenish a boost from cleared parameters" keeps the refill from looping on cleared parameters. "rtmutex: Continue the chain walk when a waiter's deadline donor changes" keeps the donor pointer from going stale. After a deboost in the class, "sched/deadline: Reset the donor's runtime at a deboost in the class" starts a period of the task's own reservation.
The Sashiko AI review of this series found this.
--- kernel/sched/deadline.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
kernel/sched/deadline.c +6 -3
@@ -1705,10 +1705,13 @@ static enum hrtimer_restart dl_task_timer(struct hrtimer *timer) }/* -* The task might have been boosted by someone else and might be in the -* boosting/deboosting path, its not throttled. +* A boost overrides the throttle. enqueue_task_dl() lifts it when the +* boost enqueues the task, or when the task wakes up boosted. So a +* queued task needs nothing here. A boost of a sleeping task does not +* enqueue it, and it can end before the wakeup. Nothing would lift the +* throttle then. So let a sleeping task reach the !queued case below. */ -if (is_dl_boosted (dl_se)) +if (is_dl_boosted (dl_se) &&task_on_rq_queued (p)) goto unlock;/*
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v10 26/40] sched/deadline: Replenish a throttled task boosted while asleep
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] sched/deadline: missing dl_note_donor_refill() in replenish_dl_entity() - [Medium] sched/deadline…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] sched/deadline: missing dl_note_donor_refill() in replenish_dl_entity()
- [Medium] sched/deadline: dl_task_timer() refills sleeping task from donor parameters
--
commit c9c72591d4a367d03f9bada51027022e7fbe7e42
Author: Mathieu Desnoyers <[email protected]>
sched/deadline: Replenish a throttled task boosted while asleep
This commit lets dl_task_timer() replenish a throttled task that was PI boosted while sleeping instead of returning early before clearing dl_throttled.
Quoted 9 lines
kernel/sched/deadline.c +0
-1@@ -1705,10 +1705,13 @@ static enum hrtimer_restart dl_task_timer(struct hrtimer *timer) }/* -* The task might have been boosted by someone else and might be in the
kernel/sched/deadline.c static enum hrtimer_restart dl_task_timer(struct hrtimer *timer) · line 1715
Quoted 10 lines
kernel/sched/deadline.c +6
-2-* boosting/deboosting path, its not throttled. +* A boost overrides the throttle. enqueue_task_dl() lifts it when the +* boost enqueues the task, or when the task wakes up boosted. So a +* queued task needs nothing here. A boost of a sleeping task does not +* enqueue it, and it can end before the wakeup. Nothing would lift the +* throttle then. So let a sleeping task reach the !queued case below. */ -if (is_dl_boosted (dl_se) &&task_on_rq_queued (p)) +if (is_dl_boosted (dl_se) &&task_on_rq_queued (p)) goto unlock;
[Severity: Medium]
Can replenish_dl_entity() leave dl_se->dl_donor_refill unset when a sleeping SCHED_DEADLINE task's donor has its parameters cleared before dl_task_timer() fires?
If the donor leaves SCHED_DEADLINE and reaches its 0-lag time before the rt_mutex chain walk finishes deboosting the sleeping owner, replenish_dl_entity() takes the cleared-parameter branch:
kernel/sched/deadline.c:replenish_dl_entity() {
if (unlikely(!runtime || !deadline || !period)) {
WARN_ON_ONCE(!is_dl_boosted(dl_se));
if (!dl_se->dl_deadline) {
dl_se->deadline = rq_clock(rq);
dl_se->runtime = 0;
dl_note_donor_refill(dl_se);
}
if (dl_se->runtime < 0 || dl_se->dl_yielded)
dl_se->runtime = 0;
dl_se->dl_yielded = 0;
dl_se->dl_throttled = 0;
return;
}Because a SCHED_DEADLINE owner has dl_se->dl_deadline != 0, it skips dl_note_donor_refill(dl_se) while zeroing runtime and clearing dl_throttled. When dl_deboost() then runs, dl_se->dl_donor_refill is 0, so it does not start a new period of the task's own reservation, leaving the owner with runtime == 0 and a stale past deadline that prevents remaining deadline waiters from re-boosting it. Calling dl_note_donor_refill(dl_se) and updating dl_se->deadline unconditionally in the cleared-parameter branch would ensure dl_deboost() resets the owner's reservation.
[Severity: Medium]
Does calling replenish_dl_entity() from dl_task_timer() refill a sleeping task's pre-boost runtime debt using the donor's parameters?
A boosted task is never throttled with dl_timer armed while boosted, so a task reaching dl_task_timer() with is_dl_boosted(dl_se) && !task_on_rq_queued(p) was throttled on its own reservation as it went to sleep and has consumed no runtime while boosted. However, replenish_dl_entity() reads runtime and period from pi_of(dl_se) and calls dl_note_donor_refill(dl_se):
kernel/sched/deadline.c:replenish_dl_entity() {
if (dl_se->runtime <= 0)
dl_note_donor_refill(dl_se);
while (dl_se->runtime <= 0) {
dl_se->deadline += period;
dl_se->runtime += runtime;
}If the sleeping owner overran its own large runtime and is boosted by a waiter with a small dl_runtime, the loop pays back the owner's pre-boost overrun in increments of the donor's runtime and period, pushing dl_se->deadline multiple donor periods into the future so the owner runs with a far-future deadline upon wakeup instead of the donor's urgent deadline. Conversely, if the waiter times out before the owner wakes up, dl_deboost() sees dl_se->dl_donor_refill set, grants a fresh full period that erases the owner's pre-boost overrun, and cancels inactive_timer early to drop running_bw before the true 0-lag time. In addition, with later commit "sched/deadline: Boost an owner that cannot run on its own reservation", dl_boost_start() saves the throttled sleeping task's negative own_runtime and expired own_deadline with dl_boost_held = 1, and dl_deboost() restores those unreplenished values after dl_task_timer() has already cleared dl_throttled.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=26
Re: [PATCH v10 26/40] sched/deadline: Replenish a throttled task boosted while asleep
Quoted 3 lines
Can
replenish_dl_entity()leavedl_se->dl_donor_refillunset when a sleepingSCHED_DEADLINEtask's donor has its parameters cleared beforedl_task_timer()fires?
Yes, by reading. In v11, that branch will note a donor refill for any boosted entity. The deboost then starts a period of the task's own.
Quoted 2 lines
Does calling
replenish_dl_entity()fromdl_task_timer()refill a sleeping task's pre-boost runtime debt using the donor's parameters?
Yes in v10. v11 will close it with your patch 38 findings. A boost of a throttled owner will start a period of its donor at once. The deboost will put back the owner's saved values. It will throttle the owner again if its runtime is used up.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
PATCH v10 27/40 sched/deadline: Count the bandwidth a boosted task is given
In reply to Mathieu Desnoyerssched_setattr(SCHED_DEADLINE) on a sleeping task that priority inheritance boosted into the class leaves the new bandwidth out of this_bw. switched_to_dl() does not run, since the task is in the class already. sched_dl_overflow() changes this_bw only for a task of the deadline policy. When the task wakes up, __add_running_bw() warns:
WARN_ON_ONCE(dl_rq->running_bw > dl_rq->this_bw);
When the task leaves the runqueue, this_bw would stay short by that bandwidth, and skew GRUB reclaiming on that CPU.
Fix this by changing this_bw in __setparam_dl(), which every task that gains deadline parameters goes through. If no runqueue counts the bandwidth of the task, dl_change_utilization() now adds the new one. It now also runs when the bandwidth does not change. It then returns early, so that a sleeping task keeps its running_bw and its inactive timer.
A sugov kthread given a reservation of its own now has its bandwidth counted in this_bw. It reaches dl_change_utilization(), so this patch removes the WARN_ON_ONCE() for SCHED_FLAG_SUGOV there. sched_dl_overflow() returned early for an unchanged bandwidth, even if no root domain counts it. That is the case for such a kthread, and for a task that a rebuild left out. sched_dl_overflow() now admits such a bandwidth as new. Without room, that fails with EBUSY. The Sashiko AI review of this series found this early return.
A reproducer shows the warning, and this_bw unchanged by the change. With this patch, this_bw takes the new bandwidth, and nothing warns.
--- kernel/sched/deadline.c | 54 ++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 11 deletions(-)
kernel/sched/deadline.c +43 -11
@@ -633,22 +633,37 @@ void cancel_inactive_timer(struct sched_dl_entity *dl_se)cancel_dl_timer (dl_se, &dl_se->inactive_timer); } +/* +* Make the runqueue of a sleeping task count @new_bw as the bandwidth of +* the task. Do nothing for a queued task. The dequeue and the enqueue +* around the change of its parameters update this_bw. +*/ static void dl_change_utilization (struct task_struct *p,u64 new_bw) { -WARN_ON_ONCE (p->dl.flags &SCHED_FLAG_SUGOV ); +struct sched_dl_entity *dl_se = &p->dl; +struct rq *rq =task_rq (p);if (task_on_rq_queued (p))return ;/* -* The task is waking up on another CPU, and no runqueue counts its -* bandwidth. The enqueue adds it, with the new parameters that -* __setparam_dl() sets right after this. +* No runqueue counts the bandwidth of the task. Add the new one to +* this runqueue, so that add_rq_bw() adds nothing later. This holds +* for a task that enters the class asleep, for a special entity given +* a reservation of its own, and for a task waking up on another CPU: +* task_cpu() already points at the CPU it wakes on. */ -if (!p->dl.dl_bw_oncpu) +if (!dl_se->dl_bw_oncpu) { +__add_rq_bw (new_bw, &rq->dl); + dl_se->dl_bw_oncpu =1 ; +return ; + } + +/* Same bandwidth: leave running_bw and the inactive timer alone. */ +if (new_bw == dl_se->dl_bw)return ; -dl_rq_change_utilization (task_rq (p), &p->dl, new_bw); +dl_rq_change_utilization (rq, dl_se, new_bw); }static void __dl_clear_params (struct sched_dl_entity *dl_se);
@@ -4919,10 +4934,6 @@ int sched_dl_overflow(struct task_struct *p, int policy,if (attr->sched_flags &SCHED_FLAG_SUGOV )return 0 ; -/* !deadline task may carry old deadline bandwidth */ -if (new_bw == p->dl.dl_bw &&task_has_dl_policy (p)) -return 0 ; -/* * Change the bandwidth of a deadline task where it is counted: in * the root domain of the CPU recorded with it. A task moved asleep
@@ -4937,6 +4948,16 @@ int sched_dl_overflow(struct task_struct *p, int policy,*/ counted =task_has_dl_policy (p) && p->dl.dl_bw_cpu >=0 && p->dl.dl_bw_gen == dl_bw_gen; + +/* +* !deadline task may carry old deadline bandwidth. The same +* bandwidth needs no admission if it is counted. If it is not, admit +* it as new below. A special entity given a reservation of its own +* has no bandwidth counted, nor has a task that a rebuild left out. +*/ +if (new_bw == p->dl.dl_bw && counted) +return 0 ; + cpu =dl_task_admit_cpu (p); dl_b =dl_bw_of (cpu);
@@ -4966,7 +4987,6 @@ int sched_dl_overflow(struct task_struct *p, int policy,if (counted)__dl_sub (dl_b, p->dl.dl_bw, cpus);__dl_add (dl_b, new_bw, cpus); -dl_change_utilization (p, new_bw); err =0 ; }else if (!dl_policy (policy) &&task_has_dl_policy (p)) {/*
@@ -5016,8 +5036,20 @@ int sched_dl_overflow(struct task_struct *p, int policy,void __setparam_dl (struct task_struct *p,const struct sched_attr *attr) {struct sched_dl_entity *dl_se = &p->dl; +u64 period = attr->sched_period ?: attr->sched_deadline;bool held; +/* +* Change the bandwidth that the runqueue counts here, with the +* parameters. A task that priority inheritance boosted into the +* class is in it already, so switched_to_dl() does not run for it. +* +* A special entity has no bandwidth on a runqueue. Test the flags +* of the request, since those of the entity are still the old ones. +*/ +if (!(attr->sched_flags &SCHED_FLAG_SUGOV )) +dl_change_utilization (p,to_ratio (period, attr->sched_runtime)); + held =dl_se_holds_bw (dl_se); dl_se->dl_runtime = attr->sched_runtime; dl_se->dl_deadline = attr->sched_deadline;
Signature · 1 line
2.47.3
PATCH v10 28/40 sched/deadline: Start a period for a task that enters the class blocked
In reply to Mathieu DesnoyersCommit fb00aca47440 ("rtmutex: Turn the plist into an rb-tree") sorts the waiters of an rt_mutex by priority. For deadline tasks, "the one with the earliest deadline wins". A waiter takes the deadline of its task.
A task can enter the class while it waits on an rt_mutex. sched_setattr() can make it SCHED_DEADLINE. A priority inheritance boost can also put it in the class, if it owns another rt_mutex. switched_to_dl() does not set up a task that is not queued. Its wakeup does. But the chain walk reads the deadline before that wakeup, and copies it into the waiter. A task new to the class has a deadline of 0.
So the waiter goes before every other deadline waiter. It takes the rt_mutex first, whatever their deadlines. Those waiters can then miss their deadlines. It also boosts a deadline owner whose own deadline is earlier.
Fix this by starting a new period in switched_to_dl() for a task that waits on an rt_mutex, if its deadline has passed. The period uses the reservation the task runs on: its own, or the copy a boost made. This is what setup_new_dl_entity() does at an enqueue. At the wakeup, update_dl_entity() then checks that period as for any wakeup.
A new donor of a task with no parameters of its own needs the same. For a queued task, the enqueue of the boost starts a period from the new donor. For a blocked task, dl_boost_blocked() starts it at the boost.
The setattr_blocked_keeps_order and boost_blocked_keeps_order cases, added to deadline_bw_test later in this series, test this. Both fail on v7.4-rc2 and on the series without this patch. The triage of the Sashiko AI review of this series found the bug.
--- kernel/sched/core.c | 4 ++++ kernel/sched/deadline.c | 46 +++++++++++++++++++++++++++++++++++++++++ kernel/sched/sched.h | 1 + 3 files changed, 51 insertions(+)
kernel/sched/core.c +4 -0
@@ -7969,8 +7969,12 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)if (!dl_prio (p->normal_prio) || (pi_task &&dl_prio (pi_task->prio) &&dl_entity_preempt (&pi_task->dl, &p->dl))) { +struct sched_dl_entity *old_donor = p->dl.pi_se; +dl_set_donor (&p->dl, pi_task); scope->flags |=ENQUEUE_REPLENISH ; +if (!scope->queued && p->dl.pi_se != old_donor) +dl_boost_blocked (p); }else {dl_deboost (&p->dl);/*
kernel/sched/deadline.c +46 -0
70 lines not on this page · raw message
kernel/sched/sched.h +1 -0
@@ -413,6 +413,7 @@ static inline void dl_clear_donor(struct sched_dl_entity *dl_se) }extern void dl_deboost (struct sched_dl_entity *dl_se); +extern void dl_boost_blocked (struct task_struct *p);/* End the boost of @dl_se. The caller holds the rq lock of its task. */ static inline void dl_drop_boost (struct sched_dl_entity *dl_se)
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v10 28/40] sched/deadline: Start a period for a task that enters the class blocked
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] sched/deadline: dl_boost_blocked() skips departed tasks with dl_deadline --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] sched/deadline: dl_boost_blocked() skips departed tasks with dl_deadline
--
commit 83a90ad95f7f2d67e4562e7ef1ff35c08c85da9f
Author: Mathieu Desnoyers <[email protected]>
sched/deadline: Start a period for a task that enters the class blocked
This commit starts a deadline period for a task blocked on an rt_mutex when it enters the deadline class or receives a new priority-inheritance donor while having no deadline reservation of its own.
Quoted 4 lines
kernel/sched/deadline.c +0
-0
[ ... ]
Quoted 17 lines
kernel/sched/deadline.c +13
-0@@ -1184,6 +1195,30 @@ static inline void replenish_dl_new_period(struct sched_dl_entity *dl_se,trace_sched_dl_replenish_tp (dl_se,cpu_of (rq),dl_get_type (dl_se, rq)); } +#ifdef CONFIG_RT_MUTEXES +/* +* Start a period from the new donor of @p, if @p has no reservation of its +* own and waits on an rt_mutex. The enqueue does this for a queued task, in +* replenish_dl_entity(). A blocked task is not enqueued, but the chain walk +* copies its deadline into its waiter. +* +* The caller calls it only when the donor changes. A new period changes the +* deadline, so the chain walk finds the waiter out of date and goes on. In a +* ww_mutex cycle, a new period at each step would never let the walk stop. +* +* The caller holds the rq lock and the pi_lock of @p, and has updated the +* rq clock.
kernel/sched/deadline.c static inline void replenish_dl_new_period(struct sched_dl_entity *dl_se, · line 1220
Quoted 10 lines
kernel/sched/deadline.c +10
-0+*/ +void dl_boost_blocked (struct task_struct *p) +{ +struct sched_dl_entity *dl_se = &p->dl; + +if (!dl_se->dl_deadline && !dl_se->dl_throttled && +dl_task_pi_blocked (p)) +replenish_dl_new_period (dl_se,task_rq (p)); +} +#endif
[Severity: Medium]
Can dl_boost_blocked() skip starting a period for a task that left SCHED_DEADLINE before its 0-lag time?
When a deadline task switches to a non-deadline policy before its 0-lag time, switched_from_dl() arms inactive_timer and leaves dl_se->dl_deadline and dl_se->deadline non-zero until that timer fires. If the task blocks on an rt_mutex and is boosted back into dl_sched_class before dl_se->deadline passes, switched_to_dl() cancels inactive_timer (so __dl_clear_params() never clears dl_se->dl_deadline while the task remains boosted) and skips replenish_dl_new_period() because dl_se->deadline has not passed yet.
Once dl_se->deadline does pass while the task is still blocked and boosted, a subsequent boost by a new donor calls dl_boost_blocked(p), which sees dl_se->dl_deadline != 0 and returns without replenishing. The chain walk then requeues the waiter with the expired dl_se->deadline ahead of waiters with current deadlines.
Checking !dl_prio(p->normal_prio) (or whether dl_se->deadline has passed) instead of !dl_se->dl_deadline would cover tasks still carrying parameters from before they left the class.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=28
Re: [PATCH v10 28/40] sched/deadline: Start a period for a task that enters the class blocked
Quoted 2 lines
Can
dl_boost_blocked()skip starting a period for a task that leftSCHED_DEADLINEbefore its 0-lag time?
Yes, by reading. This is the waiter that keeps an expired deadline, which you found on v9. v7.4-rc2 sorts such a waiter the same way. v11 will keep it as a known limit, with the question to Juri in the cover letter.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
PATCH v10 29/40 sched/deadline: Start a period at a deboost out of the class
In reply to Mathieu DesnoyersA task that leaves SCHED_DEADLINE keeps its runtime and deadline until its 0-lag time. Commit a649f237db18 ("sched,dl: Fix sched class hopping CBS hole") does this, so that leaving the class and coming back gains nothing. A priority inheritance boost also keeps the runtime and deadline of a task with parameters of its own.
But a boosted task is not throttled. When it runs out of runtime, replenish_dl_entity() refills it from the donor's parameters. If the task stays in the class at its deboost, "sched/deadline: Reset the donor's runtime at a deboost in the class" starts a new period.
A task that leaves the class at its deboost is missed. switched_from_dl() calls task_non_contending(), which computes the 0-lag time from the runtime and deadline of the donor's refills, with the task's own dl_runtime and dl_period. The root domain then keeps the task's bandwidth in total_bw for too long, or releases it too early. Until it is released, deadline tasks that would fit are refused.
Fix this by starting a period of its own in switched_from_dl(), as dl_deboost() does. Share that code in dl_start_own_period(). The 0-lag time of the new period is now, or has passed, so the bandwidth is released at once. task_non_contending() now also releases it when the 0-lag time is exactly now, instead of arming a timer of 0ns.
A task can also block while boosted, after a refill. Its dequeue then arms the inactive timer from the same mix. If the boost ends while the task sleeps, switched_from_dl() finds that timer armed, and keeps it. Fix this too: cancel the timer there, and compute the 0-lag time of the new period instead.
The Sashiko AI review found this, on "sched/deadline: Reset the deadline and the runtime with the parameters" of the first series. The deboost_out_after_donor_refill case, added to deadline_bw_test later in this series, tests it. Without this patch, the owner's bandwidth was still in total_bw right after the unlock, in every run. The deboost_out_asleep_after_refill case tests an owner asleep when the boost ends. Without the cancel of the timer, its bandwidth was still in total_bw after the boost ended, in every run.
--- kernel/sched/deadline.c | 49 +++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 7 deletions(-)
kernel/sched/deadline.c +42 -7
@@ -83,6 +83,16 @@ static inline int on_dl_rq(struct sched_dl_entity *dl_se)return !RB_EMPTY_NODE (&dl_se->rb_node); } +/* +* Start a period of the reservation of @dl_se now. The caller holds the rq +* lock and has updated the rq clock. +*/ +static inline void dl_start_own_period (struct sched_dl_entity *dl_se) +{ + dl_se->deadline =rq_clock (rq_of_dl_se (dl_se)) + dl_se->dl_deadline; + dl_se->runtime = dl_se->dl_runtime; +} +#ifdef CONFIG_RT_MUTEXES static inline struct sched_dl_entity *pi_of (struct sched_dl_entity *dl_se) {
@@ -119,11 +129,8 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task);*/ void dl_deboost (struct sched_dl_entity *dl_se) { -struct rq *rq =rq_of_dl_se (dl_se); -if (dl_se->dl_donor_refill) { - dl_se->deadline =rq_clock (rq) + dl_se->dl_deadline; - dl_se->runtime = dl_se->dl_runtime; +dl_start_own_period (dl_se);/* * A task asleep since the refill has its inactive timer armed * from the donor's runtime and deadline. The 0-lag time of the
@@ -826,10 +833,12 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task) zerolag_time =dl_time_to_zerolag (dl_se, rq);/* -* If the "0-lag time" already passed, decrease the active -* utilization now, instead of starting a timer +* If the "0-lag time" is now or already passed, decrease the active +* utilization now, instead of starting a timer. A task that leaves +* the class with a period of its own just started has a 0-lag time +* of now. */ -if ((zerolag_time <0 ) ||hrtimer_active (&dl_se->inactive_timer)) { +if ((zerolag_time <=0 ) ||hrtimer_active (&dl_se->inactive_timer)) {if (dl_server (dl_se)) {sub_running_bw (dl_se, dl_rq); }else {
@@ -4662,6 +4671,19 @@ void dl_clear_root_domains(void)static void switched_from_dl (struct rq *rq,struct task_struct *p) { +bool own_period = p->dl.dl_runtime && p->dl.dl_donor_refill; + +/* +* The boost of a task with parameters of its own ends, and the task +* leaves the class. If the boost refilled its runtime from the +* donor's parameters, its runtime and deadline are not those of its +* reservation. task_non_contending() would mix them with its own +* dl_runtime and dl_period. Start a period of its own instead, as +* dl_deboost() does. Its 0-lag time is then now, or has passed. +*/ +if (own_period) +dl_start_own_period (&p->dl); +/* * task_non_contending() can start the "inactive timer" (if the 0-lag * time is in the future). If the task switches back to dl before
@@ -4693,6 +4715,19 @@ static void switched_from_dl(struct rq *rq, struct task_struct *p)if (p->dl.dl_non_contending)sub_running_bw (&p->dl, &rq->dl);sub_rq_bw (&p->dl, &rq->dl); +/* +* The task blocked while boosted, after a refill from the +* donor. The timer armed then mixes the donor's runtime and +* deadline with the task's own parameters. Cancel it, and +* work out the 0-lag time of the period just started below. +* If the timer handler is already running, it releases the +* bandwidth. +*/ +if (own_period && p->dl.dl_non_contending && +hrtimer_try_to_cancel (&p->dl.inactive_timer) ==1 ) { + p->dl.dl_non_contending =0 ; +put_task_struct (p); + }/* * Without an armed timer, nothing releases total_bw. Do as for * a queued task: arm the timer if the 0-lag time is ahead, or
Signature · 1 line
2.47.3
PATCH v10 30/40 sched/deadline: Start a period for a task requeued onto an rt_mutex
In reply to Mathieu DesnoyersThe waiter node of an rt_mutex holds the deadline of its task, as a sort key. task_blocks_on_rt_mutex() copies it when the task blocks. A task that blocks by itself is running, so its deadline is a current one.
FUTEX_CMP_REQUEUE_PI is different. It makes a task that sleeps in FUTEX_WAIT_REQUEUE_PI wait on the rt_mutex of a PI futex, with rt_mutex_start_proxy_lock(). That task is not running. Its deadline can be one that it would never run with:
- 0, if sched_setattr() made it SCHED_DEADLINE while it slept. switched_to_dl() leaves its deadline to its next wakeup.
- one that has passed, if it slept past it.
The waiter then sorts before the waiters with a real deadline. At the unlock, it gets the lock before waiters with earlier deadlines. Its deadline also decides the boost of the owner.
Fix this in task_blocks_on_rt_mutex(): dl_proxy_block() starts a new period for such a task, as its wakeup would. A task past its deadline and before its next period keeps its deadline: its wakeup throttles it until that period.
The Sashiko AI review of this series found this. The case requeue_pi_new_period of deadline_bw_test, added later in this series, tests the deadline of 0. It fails without this patch.
--- include/linux/sched/deadline.h | 1 + kernel/locking/rtmutex.c | 3 +++ kernel/sched/deadline.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+)
include/linux/sched/deadline.h +1 -0
@@ -62,6 +62,7 @@ extern int dl_task_bw_reserve_cpu(struct task_struct *p, const struct cpumask *new_mask);extern int dl_task_bw_reserved_cpu (struct task_struct *p);extern void dl_task_bw_unreserve (struct task_struct *p); +extern void dl_proxy_block (struct task_struct *p);extern bool dl_task_keeps_root_domain (struct task_struct *p,const struct cpumask *new_mask,const struct cpumask *pending);
kernel/locking/rtmutex.c +3 -0
@@ -1245,6 +1245,9 @@ static int __sched task_blocks_on_rt_mutex(struct rt_mutex_base *lock,return -EDEADLK ;raw_spin_lock (&task->pi_lock); +/* A proxy lock: @task sleeps, and its deadline can be stale. */ +if (task != current) +dl_proxy_block (task); waiter->task = task; waiter->lock = lock;waiter_update_prio (waiter, task);
kernel/sched/deadline.c +30 -0
@@ -1573,6 +1573,36 @@ static inline u64 dl_next_period(struct sched_dl_entity *dl_se)return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period; } +#ifdef CONFIG_RT_MUTEXES +/* +* FUTEX_CMP_REQUEUE_PI makes @p wait on an rt_mutex while @p sleeps. +* The waiter node of @p takes its deadline, before any wakeup. @p would +* not run with a deadline of 0, which it has if it entered the class +* asleep. Nor would it run with a deadline that has passed. Start a new +* period then, as its wakeup would. Leave a task past its deadline and +* before its next period: its wakeup throttles it until that period. +* +* The caller holds the pi_lock of @p. +*/ +void dl_proxy_block (struct task_struct *p) +{ +struct sched_dl_entity *dl_se = &p->dl; +struct rq_flags rf; +struct rq *rq; + +if (!dl_task (p) ||is_dl_boosted (dl_se) || !dl_se->dl_runtime) +return ; + rq =__task_rq_lock (p, &rf); +update_rq_clock (rq); +if (!task_on_rq_queued (p) && !dl_se->dl_throttled && +dl_time_before (dl_se->deadline,rq_clock (rq)) && + (dl_is_implicit (dl_se) || + !dl_time_before (rq_clock (rq),dl_next_period (dl_se)))) +replenish_dl_new_period (dl_se, rq); +__task_rq_unlock (rq, p, &rf); +} +#endif +/* * If the entity depleted all its runtime, and if we want it to sleep * while waiting for some new execution time to become available, we
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v10 30/40] sched/deadline: Start a period for a task requeued onto an rt_mutex
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] sched/deadline: dl_proxy_block() skips period start for PI-boosted tasks - [Medium]…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Medium] sched/deadline: dl_proxy_block() skips period start for PI-boosted tasks
- [Medium] locking/rtmutex: proxy lock steal uses stale deadline before dl_proxy_block()
- [Medium] sched/deadline: missing dl_entity_overflow() check in dl_proxy_block()
- [Medium] sched/deadline: dl_proxy_block() skips period start for zero constrained deadline
--
commit ccbb5e6cd761023e5e38a79f3d3be937fad4a331
Author: Mathieu Desnoyers <[email protected]>
sched/deadline: Start a period for a task requeued onto an rt_mutex
This commit adds dl_proxy_block() to start a new period for a sleeping SCHED_DEADLINE task when it is proxy-locked onto an rt_mutex, so the waiter node does not use a deadline of 0 or a deadline in the past.
Quoted 5 lines
kernel/locking/rtmutex.c +0
-0
kernel/locking/rtmutex.c static int __sched task_blocks_on_rt_mutex(struct rt_mutex_base *lock, · line 1253
Quoted 9 lines
kernel/locking/rtmutex.c +3
-0@@ -1245,6 +1245,9 @@ static int __sched task_blocks_on_rt_mutex(struct rt_mutex_base *lock,return -EDEADLK ;raw_spin_lock (&task->pi_lock); +/* A proxy lock: @task sleeps, and its deadline can be stale. */ +if (task != current) +dl_proxy_block (task); waiter->task = task; waiter->lock = lock;waiter_update_prio (waiter, task);
[Severity: Medium]
Can __rt_mutex_start_proxy_lock() steal the rt_mutex using a stale task->dl.deadline before task_blocks_on_rt_mutex() runs dl_proxy_block()?
__rt_mutex_start_proxy_lock() calls try_to_take_rt_mutex(lock, task, NULL) before calling task_blocks_on_rt_mutex(). When the target PI futex has just been unlocked and is in the handoff state with waiters, try_to_take_rt_mutex() tests rt_mutex_steal(task_to_waiter(task), rt_mutex_top_waiter(lock)), which reads task->dl.deadline before dl_proxy_block() refreshes it:
kernel/locking/rtmutex.c:try_to_take_rt_mutex() {
...
if (rt_mutex_has_waiters(lock)) {
/* Check whether the trylock can steal it. */
if (!rt_mutex_steal(task_to_waiter(task),
rt_mutex_top_waiter(lock)))
return 0;
...
}A requeued task with a deadline of 0 or a passed deadline can then steal the lock from a woken top waiter with an earlier real deadline, and because try_to_take_rt_mutex() does not call rt_mutex_adjust_prio(), the new owner remains unboosted once wakeup assigns its real later deadline.
Quoted 17 lines
kernel/sched/deadline.c +9
-0@@ -1573,6 +1573,36 @@ static inline u64 dl_next_period(struct sched_dl_entity *dl_se)return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period; } +#ifdef CONFIG_RT_MUTEXES +/* +* FUTEX_CMP_REQUEUE_PI makes @p wait on an rt_mutex while @p sleeps. +* The waiter node of @p takes its deadline, before any wakeup. @p would +* not run with a deadline of 0, which it has if it entered the class +* asleep. Nor would it run with a deadline that has passed. Start a new +* period then, as its wakeup would. Leave a task past its deadline and +* before its next period: its wakeup throttles it until that period. +*
kernel/sched/deadline.c static inline u64 dl_next_period(struct sched_dl_entity *dl_se) · line 1594
Quoted 10 lines
kernel/sched/deadline.c +10
-0+* The caller holds the pi_lock of @p. +*/ +void dl_proxy_block (struct task_struct *p) +{ +struct sched_dl_entity *dl_se = &p->dl; +struct rq_flags rf; +struct rq *rq; + +if (!dl_task (p) ||is_dl_boosted (dl_se) || !dl_se->dl_runtime) +return ;
[Severity: Medium]
Does returning early on is_dl_boosted(dl_se) or !dl_se->dl_runtime leave p->dl.deadline at 0 (or expired) when p was PI-boosted while sleeping in FUTEX_WAIT_REQUEUE_PI?
While p sleeps in futex_wait_requeue_pi(), p is not queued and p->pi_blocked_on is NULL (so dl_task_pi_blocked(p) is false). If p holds another rt_mutex and a SCHED_DEADLINE waiter blocks on it, both dl_boost_blocked() and switched_to_dl() skip replenish_dl_new_period() because dl_task_pi_blocked(p) is false, leaving p->dl.deadline at 0 or at a passed deadline. When FUTEX_CMP_REQUEUE_PI then requeues p onto a second rt_mutex, dl_proxy_block() returns early instead of starting a period from pi_of(dl_se), and waiter_update_prio() copies the 0 or expired deadline into waiter->tree.deadline.
kernel/sched/deadline.c static inline u64 dl_next_period(struct sched_dl_entity *dl_se) · line 1601
Quoted 7 lines
kernel/sched/deadline.c +7
-0+ rq =__task_rq_lock (p, &rf); +update_rq_clock (rq); +if (!task_on_rq_queued (p) && !dl_se->dl_throttled && +dl_time_before (dl_se->deadline,rq_clock (rq)) && + (dl_is_implicit (dl_se) || + !dl_time_before (rq_clock (rq),dl_next_period (dl_se)))) +replenish_dl_new_period (dl_se, rq);
[Severity: Medium]
Should an implicit-deadline task also start a new period here when dl_entity_overflow(dl_se, rq_clock(rq)) is true?
When an implicit-deadline task sleeps in FUTEX_WAIT_REQUEUE_PI past its 0-lag time (or has its parameters shrunk via sched_setattr() while sleeping) and is requeued before its old dl_se->deadline passes, dl_entity_overflow(dl_se, rq_clock(rq)) is already true. Because the task is asleep, update_dl_entity() will unconditionally see dl_entity_overflow() (or a passed deadline) at wakeup and call replenish_dl_new_period(). Without checking dl_entity_overflow() in dl_proxy_block(), waiter_update_prio() copies the old dl_se->deadline that the task would never run with, causing it to sort ahead of waiters whose deadlines are earlier than its wakeup deadline.
[Severity: Medium]
What happens if a constrained-deadline task has dl_se->deadline == 0 when rq_clock(rq) is still before dl_next_period(dl_se)?
If sched_setattr() switches a task sleeping in FUTEX_WAIT_REQUEUE_PI to SCHED_DEADLINE with dl_deadline < dl_period, dl_se->deadline is 0. Because dl_is_implicit(dl_se) is false, dl_proxy_block() evaluates dl_next_period(dl_se), which computes dl_period - dl_deadline (or dl_period after commit "sched/deadline: Keep the start of the period across a change of dl_deadline") as if a period had started at t = 0. If rq_clock(rq) < dl_next_period(dl_se), dl_proxy_block() skips replenish_dl_new_period() and leaves waiter->tree.deadline at 0. Checking !dl_se->deadline before testing dl_next_period(dl_se) would avoid this.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=30
Re: [PATCH v10 30/40] sched/deadline: Start a period for a task requeued onto an rt_mutex
Quoted 2 lines
Can
__rt_mutex_start_proxy_lock()steal thert_mutexusing a staletask->dl.deadlinebeforetask_blocks_on_rt_mutex()runsdl_proxy_block()?
Yes, by reading. v11 will call dl_proxy_block() in __rt_mutex_start_proxy_lock(), before the trylock.
Quoted 3 lines
Does returning early on
is_dl_boosted(dl_se)or !dl_se->dl_runtime leavep->dl.deadlineat 0 (or expired) when p was PI-boosted while sleeping inFUTEX_WAIT_REQUEUE_PI?
Yes. In v11, dl_proxy_block() will start a period of the donor for a boosted task whose deadline is 0 or has passed.
Quoted 2 lines
Should an implicit-deadline task also start a new period here when
dl_entity_overflow(dl_se, rq_clock(rq))is true?
Yes. In v11, dl_proxy_block() will apply the wakeup rule to a task not in its gap: update_dl_entity(). That covers the overflow.
Quoted 2 lines
What happens if a constrained-deadline task has
dl_se->deadline== 0 whenrq_clock(rq)is still beforedl_next_period(dl_se)?
Yes, it keeps the deadline of 0. That needs an rq clock still under one period after boot. v11 will start a new period for a deadline of 0 first.
No case will test these four changes.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
PATCH v10 31/40 selftests/sched: Test deadline boosts in deadline_bw_test
In reply to Mathieu DesnoyersAdd the ten cases of deadline_bw_test that test the fixes of priority inheritance boosts and deboosts, earlier in this series. They cover:
- a deboost after the donor refilled the owner's runtime, in the class and out of it, with the owner running or asleep
- a deboost of an owner that left the class asleep, or that enters the class while boosted
- a former deadline owner that sleeps with the lock
- the order of the waiters of a lock, when sched_setattr() or a boost puts a waiter in the class while it waits
The comment above each case gives its scenario.
The message of each fix names the cases that test it. deboost_released_resets_period and pi_former_owner_asleep are not named there. They fail on v7.4-rc2, and pass with the first series alone. They test deboosts, so they come here.
On v7.4-rc2, in 8 VMs of 8 CPUs, six of these cases fail in every VM:
- boost_blocked_keeps_order
- deboost_after_donor_refill
- deboost_in_class_resets_period
- deboost_released_resets_period
- pi_former_owner_asleep
- setattr_blocked_keeps_order
These four cases did not run there. Each fails without the fix named:
- deboost_out_after_donor_refill and deboost_out_asleep_after_refill:
"sched/deadline: Start a period at a deboost out of the class".
- deboost_in_asleep_after_refill: "sched/deadline: Reset the donor's runtime at a deboost in the class", which cancels the inactive timer armed from the donor's refill.
- requeue_pi_new_period: "sched/deadline: Start a period for a task requeued onto an rt_mutex".
With the fixes up to here, every case passes wherever it runs.
--- .../sched/deadline/deadline_bw_test.c | 989 +++++++++++++++++- 1 file changed, 954 insertions(+), 35 deletions(-)
tools/testing/selftests/sched/deadline/deadline_bw_test.c +954 -35
@@ -881,12 +881,31 @@ static int pi_chain_two_owners(void) }/* -* Run as the owner thread: take pi_lock, and sleep 400ms while holding -* it. Return NULL, or 1 on an error. +* Run as the owner thread. With @arg (was_dl) set, first become a +* deadline task of 30ms every 100ms, run 1ms, and sleep on owner_go. The +* main thread switches the owner to SCHED_OTHER during that sleep. Then +* take pi_lock, and sleep 400ms while holding it. Return NULL, or 1 on an +* error. */ static void *sleeping_owner (void *arg) { +int was_dl = (long )arg; + owner_tid =gettid_ (); +if (was_dl) { +if (set_deadline (0 ,30 *NSEC_PER_MSEC ,PERIOD )) { + owner_holds =-1 ; +sem_post (&owner_ready); +return (void *)1 ; + } +spin_ns (NSEC_PER_MSEC ); +/* +* Sleep. The main thread switches this thread to SCHED_OTHER +* 200ms later, past its 0-lag time. +*/ +sem_post (&owner_ready); +sem_wait_sig (&owner_go); + }if (take_pi_lock ())return (void *)1 ; owner_holds =2 ;
@@ -897,30 +916,47 @@ 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) {pthread_t o;int r, ret =KSFT_PASS ;if (pi_init ())return KSFT_SKIP ; -if (pthread_create (&o,NULL , sleeping_owner,NULL )) { +if (pthread_create (&o,NULL , sleeping_owner, (void *)(long )was_dl)) {ksft_print_msg ("the owner thread could not start\n" );return KSFT_FAIL ; } -if (wait_for (5000 , owner_holds) || owner_holds !=2 ) { +if (was_dl) { +if (sem_wait_for (&owner_ready,5000 )) { +ksft_print_msg ("the owner did not become a deadline task\n" ); + ret =KSFT_FAIL ; + }else { +sleep_ns (200 *NSEC_PER_MSEC ); +if (set_other (owner_tid)) { +ksft_print_msg ("the owner cannot leave SCHED_DEADLINE\n" ); + ret =KSFT_FAIL ; + } + } +sem_post (&owner_go); + } +if (ret ==KSFT_PASS && (wait_for (5000 , owner_holds) || owner_holds !=2 )) {ksft_print_msg ("the owner never took the lock\n" ); ret =KSFT_FAIL ; }if (ret ==KSFT_PASS )sleep_ns (20 *NSEC_PER_MSEC ); -for (r =0 ; r <5 && ret ==KSFT_PASS ; r++) { +for (r =0 ; r < rounds && ret ==KSFT_PASS ; r++) {if (wait_lock_for (20 ,1 )) { -ksft_print_msg ("round %d of 5 failed\n" , r +1 ); +ksft_print_msg ("round %d of %d failed\n" , r +1 , rounds); ret =KSFT_FAIL ; } }
@@ -928,6 +964,16 @@ static int pi_other_owner_asleep(void)return ret; } +static int pi_other_owner_asleep (void ) +{ +return pi_asleep (0 ,5 ); +} + +static int pi_former_owner_asleep (void ) +{ +return pi_asleep (1 ,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
@@ -1636,9 +1682,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) {
@@ -1646,6 +1692,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); + }if (take_pi_lock ())return (void *)1 ; owner_holds =2 ;
@@ -1656,9 +1713,18 @@ static void *deboosted_owner(void *arg)return (void *)2 ;/* never boosted */ } } +if (arg == (void *)2L ) { + err =set_deadline (0 ,10 *NSEC_PER_MSEC ,PERIOD ); +if (err) { +ksft_print_msg ("the boosted owner refused as a deadline task: %s\n" , +strerror (-err)); +drop_pi_lock (); +return (void *)1 ; + } + }spin_ns (5 *NSEC_PER_MSEC );drop_pi_lock (); - err =set_deadline (0 ,10 *NSEC_PER_MSEC ,PERIOD ); + err = arg == (void *)2L ?0 :set_deadline (0 ,10 *NSEC_PER_MSEC ,PERIOD );if (err) {ksft_print_msg ("the owner refused as a deadline task: %s\n" ,strerror (-err));return (void *)1 ;
@@ -1695,8 +1761,8 @@ static void *donor_waiter(void *arg) }/* -* The owner O starts as SCHED_OTHER. The donor D is the waiter that -* boosts it: a deadline task of 40ms every 100ms. +* Run a deboost case. The owner O starts as SCHED_OTHER. The donor D is +* the waiter that boosts it: a deadline task of 40ms every 100ms. * 1. O takes pi_lock and spins. * 2. D blocks on pi_lock. This boosts O, which then runs on D's runtime. * 3. O spins 5ms more and unlocks. The unlock deboosts O.
@@ -1705,8 +1771,16 @@ static void *donor_waiter(void *arg)* * O must get about 10ms in step 4. The case fails above 20ms: O then * still runs on the runtime that the boost left. +* +* @mode changes one step: +* - 0: nothing. +* - 1: before step 1, O runs 1ms as a deadline task of 60ms every 100ms, +* and sleeps. The main thread switches it to SCHED_OTHER 300ms later, +* past its 0-lag time. +* - 2: O becomes the deadline task of step 4 in step 3 instead, before +* the 5ms. It stays in the deadline class at the deboost. */ -static int deboost_resets_period (void ) +static int deboost_case (long mode) {int err, ret =KSFT_PASS ;void *o_ret, *w_ret;
@@ -1715,10 +1789,27 @@ static int deboost_resets_period(void)if (pi_init ())return KSFT_SKIP ; owner_first_period_ms =-1 ; -if (pthread_create (&o,NULL , deboosted_owner,NULL )) { +if (pthread_create (&o,NULL , deboosted_owner, (void *)mode)) {ksft_print_msg ("the owner thread could not start\n" );return KSFT_FAIL ; } +if (mode ==1 ) { +if (sem_wait_for (&owner_ready,5000 )) { +/* The owner may get there late. Let it end. */ +sem_post (&owner_go); +join (o); +return KSFT_FAIL ;/* the owner said why */ + } +sleep_ns (3 *PERIOD ); + err =proc_state_of (owner_tid) =='S' ?set_other (owner_tid) : -EAGAIN ; +sem_post (&owner_go); +if (err) { +ksft_print_msg ("the owner could not leave the class asleep: %s\n" , +strerror (-err)); +join (o); +return KSFT_FAIL ; + } + }if (wait_for (5000 , owner_holds) || owner_holds !=2 ) {ksft_print_msg ("the owner never took the lock\n" );join (o);
@@ -1753,6 +1844,27 @@ static int deboost_resets_period(void)return ret; } +/* Run deboost_case() with a plain SCHED_OTHER owner. */ +static int deboost_resets_period (void ) +{ +return deboost_case (0 ); +} + +/* +* Run deboost_case() with an owner that left the deadline class asleep. +* Its bandwidth was released then. +*/ +static int deboost_released_resets_period (void ) +{ +return deboost_case (1 ); +} + +/* Run deboost_case() with an owner that enters the class while boosted. */ +static int deboost_in_class_resets_period (void ) +{ +return deboost_case (2 ); +} +/* * 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.
@@ -1783,14 +1895,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 sem_t refill_donor_ready, refill_measured; +static atomic_int refill_donor_locking, refill_hold_donor;static _Atomiclong long refill_owner_left;/* -* Run as the donor D of deboost_keeps_own_runtime(). Become a deadline -* task of 2ms every 10ms and sleep until the owner holds pi_lock. Then -* block on pi_lock. Return NULL once it got the lock, or 1 on an error. +* Run as the donor D of refill_case(). Become a deadline task of 2ms +* every 10ms and sleep until the owner holds pi_lock. Then block on +* pi_lock. With refill_hold_donor, stay a deadline task after the unlock +* until refill_measured is posted. Return NULL once it got the lock, or 1 +* on an error. */ static void *refill_donor (void *arg) {
@@ -1810,16 +1924,18 @@ static void *refill_donor(void *arg) err =take_pi_lock ();if (!err)drop_pi_lock (); +if (refill_hold_donor) +sem_wait_sig (&refill_measured);if (set_other (0 ))ksft_print_msg ("the donor cannot leave SCHED_DEADLINE\n" );return (void *)(long )err; }/* -* Run as the owner O of deboost_keeps_own_runtime(). @arg is the CPU -* time, in ms, that O spins with the lock once it is boosted. Store the -* runtime O has left after the unlock in refill_owner_left. Return NULL, -* 1 on an error, or 2 if the donor did not block on the lock within 1s. +* Run as the owner O of refill_case(). @arg is the CPU time, in ms, that +* O spins with the lock once it is boosted. Store the runtime O has left +* after the unlock in refill_owner_left. Return NULL, 1 on an error, or 2 +* if the donor did not block on the lock within 1s. */ static void *refill_owner (void *arg) {
@@ -1866,18 +1982,25 @@ static void *refill_owner(void *arg)* preempts O: its deadline is * earlier * blocks on pi_lock, boosts O -* runs 1ms more +* runs @spin_ms more * unlocks: the boost ends * takes pi_lock, unlocks * reads its dl.runtime * * A boosted task that runs out of runtime is not throttled. Its runtime -* is refilled from the parameters of its donor. Here O has used about 3ms -* of its own 5ms at the unlock, and was never refilled. It must keep what -* it has left. A new period would hand it a full runtime at every -* contended unlock. The case fails if O has 4ms or more. +* is refilled from the parameters of its donor: 2ms here. +* +* - @spin_ms is 8: O runs out of its own 5ms, and is refilled from D at +* least once. At the unlock it holds what the last refill left, 2ms at +* most. That is not the runtime of its own reservation. O must start a +* new period at the unlock, with 5ms. The case fails if O has 3.5ms or +* less. +* - @spin_ms is 1: O has used about 3ms of its own 5ms at the unlock, and +* was never refilled. It must keep what it has left. A new period would +* hand it a full runtime at every contended unlock. The case fails if O +* has 4ms or more. */ -static int deboost_keeps_own_runtime (void ) +static int refill_case (long spin_ms) {void *o_ret =NULL , *d_ret =NULL ;int err, ret =KSFT_PASS ;
@@ -1904,7 +2027,7 @@ static int deboost_keeps_own_runtime(void)join (d);return KSFT_FAIL ; } -if (pthread_create (&o,NULL , refill_owner, (void *)1L )) { +if (pthread_create (&o,NULL , refill_owner, (void *)spin_ms)) {ksft_print_msg ("the owner thread could not start\n" );sem_post (&owner_go);join (d);
@@ -1931,7 +2054,11 @@ static int deboost_keeps_own_runtime(void) }else if (left <0 ) {ksft_print_msg ("the runtime of the owner cannot be read\n" ); ret =KSFT_SKIP ; - }else if (left >=4 *NSEC_PER_MSEC ) { + }else if (spin_ms ==8 && left <=3500 *NSEC_PER_USEC ) { +ksft_print_msg ("the owner has %lld us after the unlock: what the donor's refill left\n" , + left /NSEC_PER_USEC ); + ret =KSFT_FAIL ; + }else if (spin_ms ==1 && left >=4 *NSEC_PER_MSEC ) {ksft_print_msg ("the owner has %lld us after the unlock: a new runtime, with no refill from the donor\n" , left /NSEC_PER_USEC ); ret =KSFT_FAIL ;
@@ -1939,6 +2066,664 @@ static int deboost_keeps_own_runtime(void)return ret; } +/* Run refill_case() with an owner that is refilled from its donor. */ +static int deboost_after_donor_refill (void ) +{ +return refill_case (8 ); +} + +/* Run refill_case() with an owner that stays on its own runtime. */ +static int deboost_keeps_own_runtime (void ) +{ +return refill_case (1 ); +} + +/* +* Run as the owner O of deboost_out_after_donor_refill(). Store total_bw +* of O's root domain right after the unlock in refill_owner_left. Return +* NULL, 1 on an error, or 2 if the donor did not block on the lock within +* 1s. +*/ +static void *former_owner (void *arg) +{ +uint64_t t0; +int err; + + owner_tid =gettid_ (); + err =set_deadline (0 ,REFILL_OWN_RUNTIME ,PERIOD ); +if (err) { +ksft_print_msg ("the owner refused as a deadline task: %s\n" ,strerror (-err)); +return (void *)1 ; + } +spin_ns (2 *NSEC_PER_MSEC ); +if (take_pi_lock ()) +return (void *)1 ; +if (set_other (0 )) { +ksft_print_msg ("the owner cannot leave SCHED_DEADLINE\n" ); +drop_pi_lock (); +return (void *)1 ; + } + owner_holds =2 ; +sem_post (&owner_go); + t0 =now_ns (); +while (!refill_donor_locking ||proc_state_of (donor_tid) !='S' ) { +if (now_ns () - t0 >NSEC_PER_SEC ) { +drop_pi_lock (); +return (void *)2 ; + } + } +spin_ns (8 *NSEC_PER_MSEC ); +drop_pi_lock (); + refill_owner_left =total_bw_of (top_cpu (1 )); +sem_post (&refill_measured); +return NULL ; +} + +/* +* As refill_case() with @spin_ms 8, but O leaves SCHED_DEADLINE once it +* holds pi_lock. O and D share the CPU of P. +* +* owner O: 5ms every 100ms donor D: 2ms every 10ms +* +* sleeps +* starts its period, runs 2ms +* takes pi_lock +* switches to SCHED_OTHER: its 0-lag +* time is about 40ms ahead +* wakes D up +* preempts O +* blocks on pi_lock, boosts O +* runs 8ms: runs out of its own +* runtime, is refilled from D +* unlocks: leaves the class +* takes pi_lock, unlocks, waits +* reads total_bw +* +* At the unlock, O's runtime and deadline come from D's refills. O must +* start a period of its own there. Its 0-lag time is then now, and its +* bandwidth must be gone from total_bw at once. Mixed with O's own +* parameters, the runtime and deadline of the refills put the 0-lag time +* about 100ms ahead. +*/ +static int deboost_out_after_donor_refill (void ) +{ +void *o_ret =NULL , *d_ret =NULL ; +int err, ret =KSFT_PASS ; +long long base; +pthread_t o, d; + +if (total_bw_of (top_cpu (1 )) <0 ) { +ksft_print_msg ("sched_debug tells of no total_bw\n" ); +return KSFT_SKIP ; + } +if (pi_init ()) +return KSFT_SKIP ; +if (sem_init (&refill_donor_ready,0 ,0 ) ||sem_init (&refill_measured,0 ,0 )) { +ksft_print_msg ("sem_init: %s\n" ,strerror (errno)); +pi_fini (); +return KSFT_SKIP ; + } + refill_donor_locking =0 ; + refill_hold_donor =1 ; + refill_owner_left =-1 ; + donor_tid =0 ; +if (pthread_create (&d,NULL , refill_donor,NULL )) { +ksft_print_msg ("the donor thread could not start\n" ); + refill_hold_donor =0 ; +return KSFT_FAIL ; + } +if (sem_wait_for (&refill_donor_ready,5000 ) || donor_tid <=0 ) { +ksft_print_msg ("the donor is not ready\n" ); +sem_post (&owner_go); +sem_post (&refill_measured); +join (d); + refill_hold_donor =0 ; +return KSFT_FAIL ; + } + base =total_bw_of (top_cpu (1 ));/* with D, without O */ +if (pthread_create (&o,NULL , former_owner,NULL )) { +ksft_print_msg ("the owner thread could not start\n" ); +sem_post (&owner_go); +sem_post (&refill_measured); +join (d); + refill_hold_donor =0 ; +return KSFT_FAIL ; + } + err =pthread_join (o, &o_ret); +if (err) { +ksft_print_msg ("pthread_join: %s\n" ,strerror (err)); + ret =KSFT_FAIL ; + } +if (o_ret == (void *)1 ) +sem_post (&owner_go);/* the owner never woke the donor up */ +if (o_ret) +sem_post (&refill_measured); + err =pthread_join (d, &d_ret); +if (err) { +ksft_print_msg ("pthread_join: %s\n" ,strerror (err)); + ret =KSFT_FAIL ; + } + refill_hold_donor =0 ; +if (o_ret == (void *)2 ) { +ksft_print_msg ("the donor never blocked on the lock\n" ); + ret =KSFT_FAIL ; + }else if (o_ret || d_ret) { + ret =KSFT_FAIL ;/* the thread said why */ + }else if (refill_owner_left != base) { +ksft_print_msg ("total_bw %lld right after the unlock, %lld expected\n" , + (long long )refill_owner_left, base); + ret =KSFT_FAIL ; + } +return ret; +} + +static sem_t aslp_donor_ready, aslp_owner_locked, aslp_owner_wake; +static sem_t aslp_donor_out, aslp_read_done; +static atomic_int aslp_donor_tid, aslp_donor_locking; +static _Atomiclong long aslp_total_bw; + +/* +* Run as the donor D of deboost_out_asleep_after_refill(). Post +* aslp_donor_out right after the timeout, or on an error before it, and +* wait for aslp_read_done. Return NULL, or 1 on an error. +* +* The timeout starts a new period for D. Each microsecond of CPU time +* that D then uses moves its 0-lag time 1ms ahead. A read of sched_debug +* can take more than 300us, so it would move D's 0-lag time past the +* wait of measured() after the case. So the main thread reads total_bw, +* not D. +*/ +static void *asleep_donor (void *arg) +{ +struct timespec ts; +int err; + + aslp_donor_tid =gettid_ (); + err =set_deadline (0 ,NSEC_PER_MSEC ,NSEC_PER_SEC ); +sem_post (&aslp_donor_ready); +if (err) { +ksft_print_msg ("the donor refused as a deadline task: %s\n" ,strerror (-err)); +sem_post (&aslp_donor_out); +return (void *)1 ; + } +if (sem_wait_for (&aslp_owner_locked,5000 )) { +ksft_print_msg ("the owner did not take the lock\n" ); +sem_post (&aslp_donor_out); +return (void *)1 ; + } +clock_gettime (CLOCK_REALTIME , &ts); + ts.tv_nsec +=200 *1000000L ; + ts.tv_sec += ts.tv_nsec /1000000000L ; + ts.tv_nsec %=1000000000L ; + aslp_donor_locking =1 ; + err =pthread_mutex_timedlock (&pi_lock, &ts); +sem_post (&aslp_donor_out); +sem_wait_sig (&aslp_read_done); +if (!err) { +ksft_print_msg ("the donor got the lock, not a timeout\n" ); +pthread_mutex_unlock (&pi_lock); +return (void *)1 ; + } +if (err !=ETIMEDOUT ) { +ksft_print_msg ("the donor's wait: %s\n" ,strerror (err)); +return (void *)1 ; + } +return NULL ; +} + +/* +* Run as the owner O of deboost_out_asleep_after_refill(). Return NULL, +* 1 on an error, or 2 if the donor did not block on the lock within 1s. +*/ +static void *asleep_owner (void *arg) +{ +uint64_t t0; +int err; + + err =set_deadline (0 ,REFILL_OWN_RUNTIME ,PERIOD ); +if (err) { +ksft_print_msg ("the owner refused as a deadline task: %s\n" ,strerror (-err)); +return (void *)1 ; + } +spin_ns (2 *NSEC_PER_MSEC ); +if (take_pi_lock ()) +return (void *)1 ; +if (set_other (0 )) { +ksft_print_msg ("the owner cannot leave SCHED_DEADLINE\n" ); +drop_pi_lock (); +return (void *)1 ; + } +sem_post (&aslp_owner_locked); + t0 =now_ns (); +while (!aslp_donor_locking ||proc_state_of (aslp_donor_tid) !='S' ) { +if (now_ns () - t0 >NSEC_PER_SEC ) { +drop_pi_lock (); +return (void *)2 ; + } + } +spin_ns (8 *NSEC_PER_MSEC ); +while (sem_wait (&aslp_owner_wake) && errno ==EINTR ) + ; +drop_pi_lock (); +return NULL ; +} + +/* +* As deboost_out_after_donor_refill(), but O sleeps when the boost ends. +* D has a lower bandwidth than O. O and D share the CPU of P. +* +* owner O: 5ms every 100ms donor D: 1ms every 1s +* +* waits for O +* starts its period, runs 2ms +* takes pi_lock +* switches to SCHED_OTHER +* wakes D up +* blocks on pi_lock for 200ms, +* boosts O +* runs 8ms: runs out of its own +* runtime, is refilled from D +* sleeps, boosted +* times out: O leaves the class +* sleeps; the main thread reads +* total_bw +* +* O blocked with the runtime and the deadline of D's refills. Mixed with +* O's own parameters, they put its 0-lag time seconds ahead. At the end +* of the boost, O must start a period of its own, as when it runs. Its +* 0-lag time is then now, and its bandwidth must be gone from total_bw at +* once. +*/ +static int deboost_out_asleep_after_refill (void ) +{ +void *o_ret =NULL , *d_ret =NULL ; +int err, ret =KSFT_PASS ; +long long base; +pthread_t o, d; + +if (total_bw_of (top_cpu (1 )) <0 ) { +ksft_print_msg ("sched_debug tells of no total_bw\n" ); +return KSFT_SKIP ; + } +if (pi_init ()) +return KSFT_SKIP ; +if (sem_init (&aslp_donor_ready,0 ,0 ) ||sem_init (&aslp_owner_locked,0 ,0 ) || +sem_init (&aslp_owner_wake,0 ,0 ) ||sem_init (&aslp_donor_out,0 ,0 ) || +sem_init (&aslp_read_done,0 ,0 )) { +ksft_print_msg ("sem_init: %s\n" ,strerror (errno)); +pi_fini (); +return KSFT_SKIP ; + } + aslp_donor_tid =0 ; + aslp_donor_locking =0 ; + aslp_total_bw =-1 ; +if (pthread_create (&d,NULL , asleep_donor,NULL )) { +ksft_print_msg ("the donor thread could not start\n" ); +return KSFT_FAIL ; + } +if (sem_wait_for (&aslp_donor_ready,5000 )) { +ksft_print_msg ("the donor is not ready\n" ); +sem_post (&aslp_owner_locked); +join (d); +return KSFT_FAIL ; + } + base =total_bw_of (top_cpu (1 ));/* with D, without O */ +if (pthread_create (&o,NULL , asleep_owner,NULL )) { +ksft_print_msg ("the owner thread could not start\n" ); +sem_post (&aslp_owner_locked); +join (d); +return KSFT_FAIL ; + } +if (sem_wait_for (&aslp_donor_out,5000 )) { +ksft_print_msg ("the donor did not time out\n" ); + ret =KSFT_FAIL ; + }else { + aslp_total_bw =total_bw_of (top_cpu (1 )); + } +sem_post (&aslp_read_done); + err =pthread_join (d, &d_ret); +if (err) { +ksft_print_msg ("pthread_join: %s\n" ,strerror (err)); + ret =KSFT_FAIL ; + } +sem_post (&aslp_owner_wake); + err =pthread_join (o, &o_ret); +if (err) { +ksft_print_msg ("pthread_join: %s\n" ,strerror (err)); + ret =KSFT_FAIL ; + } +if (o_ret == (void *)2 ) { +ksft_print_msg ("the donor never blocked on the lock\n" ); + ret =KSFT_FAIL ; + }else if (o_ret || d_ret) { + ret =KSFT_FAIL ;/* the thread said why */ + }else if (aslp_total_bw != base) { +ksft_print_msg ("total_bw %lld right after the timeout, %lld expected\n" , + (long long )aslp_total_bw, base); + ret =KSFT_FAIL ; + } +return ret; +} + +static sem_t inas_ready, inas_locked, inas_out, inas_read, inas_wake; +static atomic_int inas_locking; +static _Atomicpid_t inas_d_tid, inas_o_tid; + +/* +* Run as the donor D of deboost_in_asleep_after_refill(). Become a +* deadline task of 2ms every 50ms. Once O holds pi_lock, wait 30ms for +* it. Then post inas_out, and wait for inas_read. Return NULL if the wait +* timed out, or 1 otherwise. +*/ +static void *inas_donor (void *arg) +{ +struct timespec ts; +int err; + + inas_d_tid =gettid_ (); + err =set_deadline (0 ,2 *NSEC_PER_MSEC ,PERIOD /2 ); +sem_post (&inas_ready); +if (err) { +ksft_print_msg ("the donor refused as a deadline task: %s\n" ,strerror (-err)); +sem_post (&inas_out); +return (void *)1 ; + } +if (sem_wait_for (&inas_locked,5000 )) { +ksft_print_msg ("the owner did not take the lock\n" ); +sem_post (&inas_out); +set_other (0 ); +return (void *)1 ; + } +clock_gettime (CLOCK_REALTIME , &ts); + ts.tv_nsec +=30 *1000000L ; + ts.tv_sec += ts.tv_nsec /1000000000L ; + ts.tv_nsec %=1000000000L ; + inas_locking =1 ; + err =pthread_mutex_timedlock (&pi_lock, &ts); +sem_post (&inas_out); +sem_wait_sig (&inas_read); +if (!err) +pthread_mutex_unlock (&pi_lock); +if (set_other (0 )) +ksft_print_msg ("the donor cannot leave SCHED_DEADLINE\n" ); +if (err !=ETIMEDOUT ) { +ksft_print_msg ("the donor: %s, not a timeout\n" , + err ?strerror (err) :"got pi_lock" ); +return (void *)1 ; + } +return NULL ; +} + +/* +* Run as the owner O of deboost_in_asleep_after_refill(). Its comment has +* the steps. Return NULL, or 1 on an error. +*/ +static void *inas_owner (void *arg) +{ +uint64_t t0; +int err; + + inas_o_tid =gettid_ (); + err =set_deadline (0 ,5 *NSEC_PER_MSEC ,PERIOD ); +if (err ||take_pi_lock ()) { +if (err) +ksft_print_msg ("the owner refused as a deadline task: %s\n" , +strerror (-err)); +sem_post (&inas_locked); +set_other (0 ); +return (void *)1 ; + } +sem_post (&inas_locked); + t0 =now_ns (); +while (!inas_locking ||proc_state_of (inas_d_tid) !='S' ) { +if (now_ns () - t0 >NSEC_PER_SEC ) { +ksft_print_msg ("the donor did not block on the lock\n" ); +drop_pi_lock (); +set_other (0 ); +return (void *)1 ; + } + } +spin_ns (8 *NSEC_PER_MSEC ); +sem_wait_sig (&inas_wake); +drop_pi_lock (); +set_other (0 ); +return NULL ; +} + +/* +* As deboost_out_asleep_after_refill(), but O keeps its deadline policy +* through the deboost, and leaves the class later. O and D share the CPU +* of P: +* 1. D becomes a deadline task of 2ms every 50ms, and waits. +* 2. O becomes a deadline task of 5ms every 100ms. It takes pi_lock. +* 3. D waits 30ms for pi_lock. Its deadline is the earlier one: it boosts +* O. +* 4. O runs 8ms. It runs out of its own 5ms, and is refilled from D. It +* sleeps, boosted. +* 5. D times out. O stays in the class, and starts a period of its own: +* its 0-lag time is now. +* 6. The main thread switches O to SCHED_OTHER, and reads total_bw. +* +* O went to sleep with the runtime and the deadline of D's refills. Mixed +* with O's own parameters, they put its 0-lag time more than 100ms +* ahead. O's bandwidth must still be gone from total_bw at step 6. +*/ +static int deboost_in_asleep_after_refill (void ) +{ +void *o_ret =NULL , *d_ret =NULL ; +long long base, bw =-1 ; +int err, ret =KSFT_FAIL ; +pthread_t o, d; + +if (total_bw_of (top_cpu (1 )) <0 ) { +ksft_print_msg ("sched_debug tells of no total_bw\n" ); +return KSFT_SKIP ; + } +if (pi_init ()) +return KSFT_SKIP ; +if (sem_init (&inas_ready,0 ,0 ) ||sem_init (&inas_locked,0 ,0 ) || +sem_init (&inas_out,0 ,0 ) ||sem_init (&inas_read,0 ,0 ) || +sem_init (&inas_wake,0 ,0 )) { +ksft_print_msg ("sem_init: %s\n" ,strerror (errno)); +pi_fini (); +return KSFT_SKIP ; + } + inas_locking =0 ; + inas_d_tid =0 ; + inas_o_tid =0 ; +if (pthread_create (&d,NULL , inas_donor,NULL )) { +ksft_print_msg ("the donor thread could not start\n" ); + goto out; + } +if (sem_wait_for (&inas_ready,5000 )) { +sem_post (&inas_locked); +sem_post (&inas_read); +join (d); + goto out; + } + base =total_bw_of (top_cpu (1 ));/* with D, without O */ +if (pthread_create (&o,NULL , inas_owner,NULL )) { +ksft_print_msg ("the owner thread could not start\n" ); +sem_post (&inas_locked); +sem_post (&inas_read); +join (d); + goto out; + } +if (sem_wait_for (&inas_out,5000 )) { +ksft_print_msg ("the donor did not time out\n" ); + }else if (wait_for (1000 ,proc_state_of (inas_o_tid) =='S' )) { +ksft_print_msg ("the owner is not asleep\n" ); + }else { + err =set_other (inas_o_tid); +if (err) +ksft_print_msg ("the owner cannot leave SCHED_DEADLINE: %s\n" , +strerror (-err)); +else + bw =total_bw_of (top_cpu (1 )); + } +sem_post (&inas_read); +pthread_join (d, &d_ret); +sem_post (&inas_wake); +pthread_join (o, &o_ret); +if (o_ret || d_ret || bw <0 ) + goto out;/* a thread said why */ +if (bw != base) { +ksft_print_msg ("total_bw %lld after O left the class asleep, %lld expected\n" , + bw, base); + goto out; + } + ret =KSFT_PASS ; +out: +sem_destroy (&inas_ready); +sem_destroy (&inas_locked); +sem_destroy (&inas_out); +sem_destroy (&inas_read); +sem_destroy (&inas_wake); +pi_fini (); +return ret; +} + +static _Atomicuint32_t rq_cond, rq_pi; +static atomic_int rq_order, rq_w_rank, rq_x_rank, rq_x_locking; +static _Atomicpid_t rq_w_tid, rq_x_tid; + +/* Unlock the PI futex rq_pi, held by the caller. */ +static void rq_pi_unlock (void ) +{ +uint32_t tid =gettid_ (); + +if (!atomic_compare_exchange_strong (&rq_pi, &tid,0 )) +syscall (SYS_futex, &rq_pi,FUTEX_UNLOCK_PI |FUTEX_PRIVATE_FLAG , +0 ,NULL ,NULL ,0 ); +} + +/* +* Run as W of requeue_pi_new_period(). Wait on rq_cond with +* FUTEX_WAIT_REQUEUE_PI, to be requeued onto rq_pi. Once W holds rq_pi, +* store its rank in rq_w_rank and unlock. Return NULL, or 1 on an error. +*/ +static void *rq_w (void *arg) +{ +long ret; + + rq_w_tid =gettid_ (); + ret =syscall (SYS_futex, &rq_cond,FUTEX_WAIT_REQUEUE_PI |FUTEX_PRIVATE_FLAG , +0 ,NULL , &rq_pi,0 ); +if (ret) { +ksft_print_msg ("FUTEX_WAIT_REQUEUE_PI: %s\n" ,strerror (errno)); +return (void *)1 ; + } + rq_w_rank = rq_order++; +rq_pi_unlock (); +if (set_other (0 )) +ksft_print_msg ("W cannot leave SCHED_DEADLINE\n" ); +return NULL ; +} + +/* +* Run as X of requeue_pi_new_period(). Become a deadline task of 5ms +* every 50ms, and take rq_pi with FUTEX_LOCK_PI. Store its rank in +* rq_x_rank and unlock. Return NULL, or 1 on an error. +*/ +static void *rq_x (void *arg) +{ +int err; + + rq_x_tid =gettid_ (); + err =set_deadline (0 ,5 *NSEC_PER_MSEC ,PERIOD /2 ); +if (err) { +ksft_print_msg ("X refused as a deadline task: %s\n" ,strerror (-err)); + rq_x_locking =-1 ; +return (void *)1 ; + } + rq_x_locking =1 ; +if (syscall (SYS_futex, &rq_pi,FUTEX_LOCK_PI |FUTEX_PRIVATE_FLAG , +0 ,NULL ,NULL ,0 )) { +ksft_print_msg ("FUTEX_LOCK_PI: %s\n" ,strerror (errno)); +set_other (0 ); +return (void *)1 ; + } + rq_x_rank = rq_order++; +rq_pi_unlock (); +if (set_other (0 )) +ksft_print_msg ("X cannot leave SCHED_DEADLINE\n" ); +return NULL ; +} + +/* +* FUTEX_CMP_REQUEUE_PI makes a sleeping task wait on a PI futex. The main +* thread holds the PI futex rq_pi: +* 1. W, a fair task, sleeps in FUTEX_WAIT_REQUEUE_PI. +* 2. The main thread makes W a deadline task of 5ms every 100ms. W sleeps, +* so it gets no deadline yet: its deadline is 0. +* 3. X, a deadline task of 5ms every 50ms, blocks on rq_pi. +* 4. The main thread requeues W onto rq_pi, then unlocks it. +* +* X has the earlier deadline. It must get rq_pi first. With W's deadline +* of 0 in its waiter node, W would get it first. +*/ +static int requeue_pi_new_period (void ) +{ +void *w_ret =NULL , *x_ret =NULL ; +int ret =KSFT_FAIL , have_x =0 , err; +pthread_t w, x; +long n; + + rq_cond =0 ; + rq_pi =gettid_ (); + rq_order =0 ; + rq_w_rank =-1 ; + rq_x_rank =-1 ; + rq_x_locking =0 ; + rq_w_tid =0 ; +if (pthread_create (&w,NULL , rq_w,NULL )) { +ksft_print_msg ("the W thread could not start\n" ); +return KSFT_FAIL ; + } +if (wait_for (1000 , rq_w_tid &&proc_state_of (rq_w_tid) =='S' )) { +ksft_print_msg ("W never slept\n" ); + goto out; + } + err =set_deadline (rq_w_tid,5 *NSEC_PER_MSEC ,PERIOD ); +if (err) { +ksft_print_msg ("W refused as a deadline task: %s\n" ,strerror (-err)); + goto out; + } + have_x = !pthread_create (&x,NULL , rq_x,NULL ); +if (!have_x ||wait_for (1000 , rq_x_locking && + (rq_x_locking <0 ||proc_state_of (rq_x_tid) =='S' )) || + rq_x_locking <0 ) { +ksft_print_msg ("X never waited on rq_pi\n" ); + goto out; + } + n =syscall (SYS_futex, &rq_cond,FUTEX_CMP_REQUEUE_PI |FUTEX_PRIVATE_FLAG , +1 , (void *)1L , &rq_pi,0 ); +if (n <0 ) { +ksft_print_msg ("FUTEX_CMP_REQUEUE_PI: %s\n" ,strerror (errno)); + goto out; + } + ret =KSFT_PASS ; +out: +rq_pi_unlock (); +if (ret !=KSFT_PASS ) { +/* Let W go if it was not requeued. */ + rq_cond =1 ; +syscall (SYS_futex, &rq_cond,FUTEX_WAKE |FUTEX_PRIVATE_FLAG , +1 ,NULL ,NULL ,0 ); + } +if (have_x &&pthread_join (x, &x_ret)) + ret =KSFT_FAIL ; +if (pthread_join (w, &w_ret)) + ret =KSFT_FAIL ; +if (ret ==KSFT_PASS && (w_ret || x_ret)) { + ret =KSFT_FAIL ;/* the thread said why */ + }else if (ret ==KSFT_PASS && rq_w_rank < rq_x_rank) { +ksft_print_msg ("W got rq_pi before X\n" ); + ret =KSFT_FAIL ; + } +return ret; +} +static sem_t ret_ready, ret_go;static _Atomicpid_t ret_tid;static atomic_int ret_first_period_ms;
@@ -2033,7 +2818,7 @@ static pthread_mutex_t pi_lock2;static sem_t holder_go;static atomic_int holder_holds, chain_waits, donor_done, x_waits, l2_takes;static atomic_int chain_rank, x_rank; -static _Atomicpid_t x_tid; +static _Atomicpid_t x_tid, fair_tid;/* The holder thread: take pi_lock2, and hold it until holder_go. */ static void *l2_holder (void *arg)
@@ -2191,6 +2976,7 @@ static int pi_init_lock2(void) x_rank =-1 ; donor_tid =0 ; x_tid =0 ; + fair_tid =0 ;return 0 ; }
@@ -2289,6 +3075,129 @@ static int deboost_blocked_keeps_order(void)return ret; } +/* +* The fair waiter: wait on pi_lock2 as a fair task. With @arg set, hold +* pi_lock meanwhile, for a donor to boost it. Otherwise the main thread +* makes it a deadline task while it waits. It leaves the class at the end. +*/ +static void *fair_waiter (void *arg) +{ +int err; + + fair_tid =gettid_ (); +if (arg &&take_pi_lock ()) +return (void *)1 ; + chain_waits =1 ; + err =take_l2_ranked (&chain_rank); +if (arg) +drop_pi_lock (); +else if (set_other (0 )) +ksft_print_msg ("a waiter cannot leave SCHED_DEADLINE\n" ); +return err ? (void *)1 :NULL ; +} + +/* +* Check the order of the waiters of pi_lock2 when one of them enters the +* class while it waits. The holder has pi_lock2: +* 1. The fair waiter waits on pi_lock2. With @boost, it holds pi_lock. +* 2. The other waiter, a deadline task, waits on pi_lock2. +* 3. The fair waiter enters the class: +* - Without @boost, the main thread makes it a deadline task of 10ms +* every 200ms. +* - With @boost, a donor of 40ms every 100ms waits on pi_lock, and +* boosts it. +* 4. The holder drops pi_lock2. +* +* The other waiter must take pi_lock2 first: it started its period +* earlier, with a shorter deadline. At step 3 the fair waiter is not +* queued, and the chain walk copies its deadline into its waiter node on +* pi_lock2. A deadline of 0 in the node would put the fair waiter first. +*/ +static int blocked_keeps_order (long boost) +{ +void *f_ret =NULL , *x_ret =NULL , *w_ret =NULL ; +int ret =KSFT_FAIL , have_f =0 , have_x =0 , have_w =0 , err; +pthread_t h, f, x, w; + +if (pi_init ()) +return KSFT_SKIP ; +if (pi_init_lock2 ()) { +pi_fini (); +return KSFT_SKIP ; + } +if (pthread_create (&h,NULL , l2_holder,NULL )) { +ksft_print_msg ("the holder thread could not start\n" ); + goto out_fini; + } +if (wait_for (5000 , holder_holds) || holder_holds !=1 ) { +ksft_print_msg ("the holder never took pi_lock2\n" ); + goto out; + } + have_f = !pthread_create (&f,NULL , fair_waiter, (void *)boost); +if (!have_f ||wait_for (1000 , chain_waits &&proc_state_of (fair_tid) =='S' )) { +ksft_print_msg ("the fair waiter never waited on pi_lock2\n" ); + goto out; + } + have_x = !pthread_create (&x,NULL , early_waiter,NULL ); +if (!have_x ||wait_for (1000 , x_waits &&proc_state_of (x_tid) =='S' )) { +ksft_print_msg ("the other waiter never waited on pi_lock2\n" ); + goto out; + } +if (boost) { + have_w = !pthread_create (&w,NULL , donor_waiter,NULL ); +if (!have_w ||wait_for (1000 , donor_tid &&proc_state_of (donor_tid) =='S' )) { +ksft_print_msg ("the donor never waited on pi_lock\n" ); + goto out; + } +if (prio_of (fair_tid) !=DL_TASK_PRIO ) { +ksft_print_msg ("the donor never boosted the fair waiter\n" ); + goto out; + } + }else { + err =set_deadline (fair_tid,10 *NSEC_PER_MSEC ,2 *PERIOD ); +if (err) { +ksft_print_msg ("the fair waiter refused as a deadline task: %s\n" , +strerror (-err)); +if (err == -EBUSY ) + ret =KSFT_SKIP ;/* no room in P */ + goto out; + } + } + ret =KSFT_PASS ; +out: +sem_post (&holder_go);/* pi_lock2 goes to its top waiter */ +if (have_w &&pthread_join (w, &w_ret)) + ret =KSFT_FAIL ; +if (have_x &&pthread_join (x, &x_ret)) + ret =KSFT_FAIL ; +if (have_f &&pthread_join (f, &f_ret)) + ret =KSFT_FAIL ; +join (h); +if (f_ret || x_ret || w_ret) { + ret =KSFT_FAIL ;/* the thread said why */ + }else if (ret ==KSFT_PASS && chain_rank < x_rank) { +ksft_print_msg ("the fair waiter took pi_lock2 before the earlier waiter\n" ); + ret =KSFT_FAIL ; + } +out_fini: +pthread_mutex_destroy (&pi_lock2); +sem_destroy (&holder_go); +pi_fini (); +return ret; +} + +/* sched_setattr() puts the fair waiter in the class. */ +static int setattr_blocked_keeps_order (void ) +{ +return blocked_keeps_order (0 ); +} + +/* A boost puts the fair waiter in the class. */ +static int boost_blocked_keeps_order (void ) +{ +return blocked_keeps_order (1 ); +} +/* * The threads of pi_chain_donor_leaves(): O, M, W, Z and Y. leave_tid[] * holds their tids. leave_y_ns is the CPU time Y gets in O's window, or
@@ -10492,6 +11401,7 @@ static const struct p_case { } p_cases[] = { { "pi_deadline_owner", pi_deadline_owner, MEASURED_IN_P }, { "pi_other_owner_asleep", pi_other_owner_asleep, MEASURED_IN_P }, + { "pi_former_owner_asleep", pi_former_owner_asleep, MEASURED_IN_P }, { "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 },
@@ -10505,9 +11415,18 @@ static const struct p_case { { "params_keep_boosted", params_keep_boosted, MEASURED_IN_P }, { "boosted_owner_policy", boosted_owner_policy, MEASURED_IN_P }, { "deboost_resets_period", deboost_resets_period, MEASURED_IN_P }, + { "deboost_released_resets_period", deboost_released_resets_period, MEASURED_IN_P }, + { "deboost_in_class_resets_period", deboost_in_class_resets_period, MEASURED_IN_P }, + { "deboost_after_donor_refill", deboost_after_donor_refill, MEASURED_IN_P }, + { "deboost_out_after_donor_refill", deboost_out_after_donor_refill, MEASURED_IN_P }, + { "deboost_out_asleep_after_refill", deboost_out_asleep_after_refill, MEASURED_IN_P }, + { "deboost_in_asleep_after_refill", deboost_in_asleep_after_refill, MEASURED_IN_P }, + { "requeue_pi_new_period", requeue_pi_new_period, MEASURED_IN_P }, { "deboost_keeps_own_runtime", deboost_keeps_own_runtime, MEASURED_IN_P }, { "return_asleep_new_params", return_asleep_new_params, MEASURED_IN_P }, { "deboost_blocked_keeps_order", deboost_blocked_keeps_order, MEASURED_IN_P }, + { "setattr_blocked_keeps_order", setattr_blocked_keeps_order, MEASURED_IN_P }, + { "boost_blocked_keeps_order", boost_blocked_keeps_order, MEASURED_IN_P }, { "pi_chain_donor_leaves", pi_chain_donor_leaves, MEASURED_IN_P }, { "pi_chain_top_moves", pi_chain_top_moves, MEASURED_IN_P }, { "pi_chain_top_moves_rt", pi_chain_top_moves_rt, MEASURED_IN_P },
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v10 31/40] selftests/sched: Test deadline boosts in deadline_bw_test
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] selftests/sched: hang in requeue_pi_new_period() error path on FUTEX_WAKE - [Low] selftests/sched…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] selftests/sched: hang in requeue_pi_new_period() error path on FUTEX_WAKE
- [Low] selftests/sched: deadlock in deboost_out_asleep_after_refill() error path
--
commit 27cfa3f823b3f1e4dfbcad5af492394f2bdb22ea
Author: Mathieu Desnoyers <[email protected]>
selftests/sched: Test deadline boosts in deadline_bw_test
This commit adds ten test cases to deadline_bw_test covering SCHED_DEADLINE priority inheritance boosts, deboosts, runtime refills, and lock waiter ordering.
Quoted 4 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +0
-0
[ ... ]
Quoted 1 line
tools/testing/selftests/sched/deadline/deadline_bw_test.c +0
-0@@ -1939,6 +2066,664 @@ static int deboost_keeps_own_runtime(void)
[ ... ]
Quoted 26 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +26
-0+static void *asleep_donor (void *arg) +{ +struct timespec ts; +int err; + + aslp_donor_tid =gettid_ (); + err =set_deadline (0 ,NSEC_PER_MSEC ,NSEC_PER_SEC ); +sem_post (&aslp_donor_ready); +if (err) { +ksft_print_msg ("the donor refused as a deadline task: %s\n" ,strerror (-err)); +sem_post (&aslp_donor_out); +return (void *)1 ; + } +if (sem_wait_for (&aslp_owner_locked,5000 )) { +ksft_print_msg ("the owner did not take the lock\n" ); +sem_post (&aslp_donor_out); +return (void *)1 ; + } +clock_gettime (CLOCK_REALTIME , &ts); + ts.tv_nsec +=200 *1000000L ; + ts.tv_sec += ts.tv_nsec /1000000000L ; + ts.tv_nsec %=1000000000L ; + aslp_donor_locking =1 ; + err =pthread_mutex_timedlock (&pi_lock, &ts); +sem_post (&aslp_donor_out); +sem_wait_sig (&aslp_read_done);
[ ... ]
Quoted 2 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +2
-0+static int deboost_out_asleep_after_refill (void ) +{
[ ... ]
Quoted 7 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +7
-0+if (pthread_create (&d,NULL , asleep_donor,NULL )) { +ksft_print_msg ("the donor thread could not start\n" ); +return KSFT_FAIL ; + } +if (sem_wait_for (&aslp_donor_ready,5000 )) { +ksft_print_msg ("the donor is not ready\n" ); +sem_post (&aslp_owner_locked);
tools/testing/selftests/sched/deadline/deadline_bw_test.c static int deboost_keeps_own_runtime(void) · line 2376
Quoted 10 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +10
-0+join (d); +return KSFT_FAIL ; + } + base =total_bw_of (top_cpu (1 ));/* with D, without O */ +if (pthread_create (&o,NULL , asleep_owner,NULL )) { +ksft_print_msg ("the owner thread could not start\n" ); +sem_post (&aslp_owner_locked); +join (d); +return KSFT_FAIL ; + }
[Severity: Low]
Will join(d) hang on these two error paths if aslp_read_done is not posted? Posting aslp_owner_locked wakes asleep_donor(), which then takes pi_lock and blocks in sem_wait_sig(&aslp_read_done).
[ ... ]
Quoted 17 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +17
-0+static void *rq_w (void *arg) +{ +long ret; + + rq_w_tid =gettid_ (); + ret =syscall (SYS_futex, &rq_cond,FUTEX_WAIT_REQUEUE_PI |FUTEX_PRIVATE_FLAG , +0 ,NULL , &rq_pi,0 ); +if (ret) { +ksft_print_msg ("FUTEX_WAIT_REQUEUE_PI: %s\n" ,strerror (errno)); +return (void *)1 ; + } + rq_w_rank = rq_order++; +rq_pi_unlock (); +if (set_other (0 )) +ksft_print_msg ("W cannot leave SCHED_DEADLINE\n" ); +return NULL ; +}
[ ... ]
Quoted 2 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +2
-0+static int requeue_pi_new_period (void ) +{
[ ... ]
tools/testing/selftests/sched/deadline/deadline_bw_test.c static int deboost_keeps_own_runtime(void) · line 2717
Quoted 12 lines
tools/testing/selftests/sched/deadline/deadline_bw_test.c +12
-0+out: +rq_pi_unlock (); +if (ret !=KSFT_PASS ) { +/* Let W go if it was not requeued. */ + rq_cond =1 ; +syscall (SYS_futex, &rq_cond,FUTEX_WAKE |FUTEX_PRIVATE_FLAG , +1 ,NULL ,NULL ,0 ); + } +if (have_x &&pthread_join (x, &x_ret)) + ret =KSFT_FAIL ; +if (pthread_join (w, &w_ret)) + ret =KSFT_FAIL ;
[Severity: Low]
Does FUTEX_WAKE wake W when it is sleeping in FUTEX_WAIT_REQUEUE_PI on rq_cond? In the kernel, futex_wake() rejects waiters that have rt_waiter set with -EINVAL without waking them:
kernel/futex/waitwake.c:futex_wake() {
...
if (this->pi_state || this->rt_waiter) {
ret = -EINVAL;
break;
}
...
}If an earlier setup step such as set_deadline() fails before W is requeued, W stays asleep on rq_cond and pthread_join(w, &w_ret) hangs.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=31
Re: [PATCH v10 31/40] selftests/sched: Test deadline boosts in deadline_bw_test
Quoted 2 lines
Will join(d) hang on these two error paths if
aslp_read_doneis not posted?
Yes. v11 will post aslp_read_done on both paths.
Quoted 2 lines
Does
FUTEX_WAKEwake W when it is sleeping inFUTEX_WAIT_REQUEUE_PIonrq_cond?
No. In v11, the error path will wake W with FUTEX_CMP_REQUEUE_PI, which gives it rq_pi. It will then set rq_cond, in case W has not started its wait.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
PATCH v10 32/40 sched/deadline: Cancel the replenishment timer with the parameters
In reply to Mathieu DesnoyersA task that leaves SCHED_DEADLINE has its parameters cleared at its 0-lag time. Its replenishment timer can still be armed then. If the task comes back and throttles before that timer fires, start_dl_timer() does not restart it, since it is queued. The replenishment would then come at the period of the reservation the task left. Too early, it would give the task runtime it has not earned. Too late, it would delay the task by up to a period.
Commit a649f237db18 ("sched,dl: Fix sched class hopping CBS hole") armed the replenishment timer when a task leaves the class. The timer then reset the task if it was still out. Commit 209a0cbda7a0 ("sched/deadline: Improve the tracking of active utilization") moved that reset to the inactive timer, at the 0-lag time. Nothing cancels the replenishment timer there.
The timer of a throttled task fires at its next period, which can come after the 0-lag time:
- A task that yielded is throttled with runtime left. Its 0-lag time comes before its deadline.
- A constrained-deadline task that overran by little reaches its 0-lag time before the end of its period.
Fix this by cancelling the timer where the parameters are cleared. This includes task_non_contending(). switched_from_dl() calls it to release the bandwidth of a task that leaves asleep past its 0-lag time. A constrained task that overran as it blocked has the timer armed there too.
The Sashiko AI review of this series found this. It has no reproducer.
--- kernel/sched/deadline.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/sched/deadline.c +2 -0
@@ -851,6 +851,7 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task)if (dl_task &&READ_ONCE (p->__state) ==TASK_DEAD )sub_rq_bw (dl_se, &rq->dl);dl_task_release_bw (p); +cancel_replenish_timer (dl_se);__dl_clear_params (dl_se); } }
@@ -2744,6 +2745,7 @@ static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer) }dl_task_release_bw (p); +cancel_replenish_timer (dl_se);__dl_clear_params (dl_se); goto unlock;
Signature · 1 line
2.47.3
PATCH v10 33/40 sched/deadline: Cap the runtime left of a constrained task at dl_runtime
In reply to Mathieu Desnoyerssched_setattr() can change the parameters of a SCHED_DEADLINE task. The task keeps its runtime and its absolute deadline: the new parameters take effect at the next period. At the next wakeup, update_dl_entity() checks the runtime left against the new parameters.
For a constrained task, that check uses the density, dl_runtime / dl_deadline. Commit 2317d5f1c349 ("sched/deadline: Use deadline instead of period when calculating overflow") made dl_entity_overflow() compare with it: "We care about if the runtime can make its deadline, not its period." Commit 3effcb4247e7 ("sched/deadline: Use the revised wakeup rule for suspending constrained dl tasks") then cut the runtime of a constrained task that overflows to density * laxity. Both bound the runtime by dl_runtime only while the deadline is at most dl_deadline ahead.
A change to a shorter dl_deadline breaks that. The old deadline can be farther ahead. The check then keeps more runtime than the new dl_runtime, or computes more. For example, take a task of 50ms every 100ms with 49ms left. Changed to 8ms in 20ms every 100ms, it would wake with 39.6ms. It would then run far more than its new runtime in this period. Admission control already counts the new, smaller bandwidth. So other deadline tasks could miss their deadlines.
An implicit task is not affected. Its check compares with its bandwidth, which bounds the runtime for any deadline.
Fix this by keeping no more than dl_runtime before the check, for a constrained task. A boosted task runs on the parameters of its donor, and is left alone. The runtime is signed and can be negative there, so compare it as signed.
The cases change_constrained_new_params and wake_constrained_new_params, added to deadline_bw_test later in this series, test this. The first changes a running task, the second a sleeping one. Both fail without this patch.
The Sashiko AI review of this series found this.
--- kernel/sched/deadline.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
kernel/sched/deadline.c +19 -0
@@ -1539,6 +1539,25 @@ static void update_dl_entity(struct sched_dl_entity *dl_se) {struct rq *rq =rq_of_dl_se (dl_se); +/* +* The checks below compare the runtime left with the density, +* dl_runtime / dl_deadline. That bounds the runtime by dl_runtime +* only while the deadline is at most dl_deadline ahead. After a +* change to a shorter dl_deadline, the deadline can be farther +* ahead, and the runtime left larger than dl_runtime. A constrained +* task would then run more than dl_runtime in a period. Keep no +* more than dl_runtime. +* +* An implicit task is checked against its bandwidth, which holds +* for any deadline. A boosted task runs on the parameters of its +* donor. Leave both alone. +* +* The runtime is signed, and can be negative here. +*/ +if (!dl_is_implicit (dl_se) && !is_dl_boosted (dl_se) && + dl_se->runtime > (s64 )dl_se->dl_runtime) + dl_se->runtime = dl_se->dl_runtime; +if (dl_time_before (dl_se->deadline,rq_clock (rq)) ||dl_entity_overflow (dl_se,rq_clock (rq))) {
Signature · 1 line
2.47.3
PATCH v10 34/40 sched/deadline: Check the runtime of a task that comes back runnable
In reply to Mathieu DesnoyersCommit a649f237db18 ("sched,dl: Fix sched class hopping CBS hole") keeps the runtime and the deadline of a task that leaves SCHED_DEADLINE. A task that comes back before its deadline goes on with them. So leaving the class and coming back gives no new runtime.
But the task can come back with new parameters. If it is asleep, its wakeup checks the runtime left against them, with dl_entity_overflow(). If it is runnable, enqueue_dl_entity() starts a new period only if the deadline has passed. The runtime left is not checked.
So a task can come back with a smaller runtime, and still run on the old one. Admission control counts only the new one. Juri Lelli found this case. On v7.4-rc2, a task of 50ms every 100ms came back through SCHED_OTHER with 10ms every 100ms. It ran 39ms of the next 100ms.
Fix this by applying the wakeup rule to a task that enters the class runnable, with update_dl_entity(). It starts a new period, or reduces the runtime of a constrained task, if the runtime left does not fit the new parameters. The task gets no more than a wakeup would give it.
The return_running_new_params case, added to deadline_bw_test later in this series, tests this. It fails on v7.4-rc2 and passes with this patch. The return_asleep_new_params case tests the wakeup, and passes on both.
--- kernel/sched/deadline.c | 8 ++++++++ 1 file changed, 8 insertions(+)
kernel/sched/deadline.c +8 -0
@@ -3014,6 +3014,14 @@ enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags)update_dl_entity (dl_se); }else if (flags &ENQUEUE_REPLENISH ) {replenish_dl_entity (dl_se); + }else if ((flags &ENQUEUE_CLASS ) && !is_dl_boosted (dl_se)) { +/* +* The task enters the class runnable. It may come back before +* its deadline, with the runtime left from older parameters. +* Check that runtime against the new parameters, as a wakeup +* does. +*/ +update_dl_entity (dl_se); }else if ((flags &ENQUEUE_MOVE ) && !is_dl_boosted (dl_se) &&dl_time_before (dl_se->deadline,rq_clock (rq_of_dl_se (dl_se)))) {
Signature · 1 line
2.47.3
PATCH v10 35/40 sched/deadline: Check the runtime of a task whose parameters change
In reply to Mathieu DesnoyersCommit 40767b0dc768 ("sched/deadline: Fix deadline parameter modification handling") lets new parameters take effect at the next period. So sched_setattr() cannot give a task a fresh runtime.
But admission control counts the new bandwidth at once. When a task shrinks its reservation, the runtime left from the old one is not checked against the new one. A comment in sched_dl_overflow() names the gap: the old bandwidth should stay counted until the 0-lag time.
So a running task can shrink its reservation, and still run on the old one. Other tasks can meanwhile be admitted into the bandwidth it gave back. On v7.4-rc2, a task of 50ms every 100ms changed to 10ms every 100ms. It ran 39ms of the next 100ms.
Fix this by applying the wakeup rule after a change of parameters, as the previous patch does for a task that enters the class. __setparam_dl() marks a change of the runtime, the deadline or the period in dl_new_params. The next enqueue then calls update_dl_entity(). It starts a new period, or reduces the runtime of a constrained task, if the runtime left does not fit. The task gets no more than a wakeup would give it. A larger reservation still takes effect at the next period.
The enqueue flags cannot tell a change of parameters: a cgroup move also uses ENQUEUE_MOVE. Without the mark, a task that waited behind others could fail the check, and get a later deadline for no reason.
A boosted task runs on the parameters of its donor. So it keeps the mark until its deboost. dl_deboost() checks it there, before rt_mutex_setprio() compares the task with its top waiter. Until then, the task has the deadline of its old parameters. So rt_mutex_setprio() deboosts a marked task first, even if its top waiter has the earlier deadline. It then compares the waiter with the deadline from the check, and boosts the task again if needed.
dl_deboost() leaves the check to the next enqueue in two cases:
- a constrained task past its deadline and before its next period: the enqueue throttles it first.
- a task asleep on anything but an rt_mutex: a period started at the deboost would run while it sleeps.
A task blocked on an rt_mutex is not enqueued before its wakeup, but its waiter node holds its deadline. So sched_setattr() checks the runtime of such a task at once, in dl_check_blocked_params(), also when the task comes back to the class. rt_mutex_adjust_pi() then requeues its waiter. prio_changed_dl() does not check it: a cgroup move or an affinity change gets there too, and does not requeue the waiter.
Until the check, the 0-lag time of a task that blocks mixes its old runtime and deadline with its new parameters. When the runtime left does not fit them, that 0-lag time has passed. The bandwidth is then released at once, as the check would do. A comment in dl_time_to_zerolag() now says so.
The change_running_new_params case, added to deadline_bw_test later in this series, tests this. change_boosted_new_params tests a task that changes its parameters while boosted. Both fail on v7.4-rc2 and pass with this patch. deboost_new_params_reboost and boost_new_params_stale test the check at the deboost. setattr_blocked_new_period tests a task blocked on an rt_mutex. Without dl_check_blocked_params(), it took the lock after a waiter with a later deadline, in every run.
--- include/linux/sched.h | 5 ++ kernel/sched/core.c | 6 +++ kernel/sched/deadline.c | 106 ++++++++++++++++++++++++++++++++++++---- kernel/sched/sched.h | 1 + kernel/sched/syscalls.c | 3 ++ 5 files changed, 112 insertions(+), 9 deletions(-)
include/linux/sched.h +5 -0
@@ -715,6 +715,10 @@ struct sched_dl_entity {* @dl_bw_oncpu tells if the this_bw of a runqueue counts the task's * @dl_bw: add_rq_bw() and sub_rq_bw() set and clear it, under that * runqueue's lock. Not for servers, which keep @dl_bw_attached. +* +* @dl_new_params tells if sched_setattr() changed the runtime, the +* deadline or the period. The next enqueue that is not boosted, or +* the deboost, checks the runtime left against them, and clears it. */ unsigned int dl_throttled : 1; unsigned int dl_yielded : 1;
@@ -729,6 +733,7 @@ struct sched_dl_entity { unsigned int dl_defer_idle : 1; unsigned int dl_bw_attached : 1; unsigned int dl_bw_oncpu : 1; + unsigned int dl_new_params : 1;/* * Bandwidth enforcement timer. Each -deadline task has its
kernel/sched/core.c +6 -0
@@ -7966,8 +7966,14 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)* running task */ if (dl_prio (prio)) { +/* +* A boosted task that changed its parameters still has +* the deadline of its old ones. Deboost it first, so +* that its top waiter is compared with its new one. +*/ if (!dl_prio (p->normal_prio) || (pi_task &&dl_prio (pi_task->prio) && + !(p->dl.dl_new_params && p->dl.pi_se != &p->dl) &&dl_entity_preempt (&pi_task->dl, &p->dl))) {struct sched_dl_entity *old_donor = p->dl.pi_se;
kernel/sched/deadline.c +97 -9
176 lines not on this page · raw message
kernel/sched/sched.h +1 -0
@@ -414,6 +414,7 @@ static inline void dl_clear_donor(struct sched_dl_entity *dl_se)extern void dl_deboost (struct sched_dl_entity *dl_se);extern void dl_boost_blocked (struct task_struct *p); +extern void dl_check_blocked_params (struct task_struct *p);/* End the boost of @dl_se. The caller holds the rq lock of its task. */ static inline void dl_drop_boost (struct sched_dl_entity *dl_se)
kernel/sched/syscalls.c +3 -0
@@ -311,6 +311,9 @@ static inline void __setscheduler_dl_pi(int newprio, int policy,*/ if (!dl_prio (newprio))dl_drop_boost (&p->dl); + +/* rt_mutex_adjust_pi() then requeues the waiter of a blocked task. */ +dl_check_blocked_params (p); }#else /* !CONFIG_RT_MUTEXES */ static inline void __setscheduler_dl_pi(int newprio, int policy,
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v10 35/40] sched/deadline: Check the runtime of a task whose parameters change
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] sched/deadline: dl_proxy_block() misses dl_new_params check on PI futex requeue - [Medium]…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] sched/deadline: dl_proxy_block() misses dl_new_params check on PI futex requeue
- [Medium] sched/deadline: missing PI boost when update_dl_entity() pushes owner deadline
- [Low] sched/deadline: __setparam_dl() skips dl_new_params on same-param re-entry
--
commit a68761d066fed61088f4cef94943c4d4d24b8d1f
Author: Mathieu Desnoyers <[email protected]>
sched/deadline: Check the runtime of a task whose parameters change
This commit tracks deadline parameter changes in dl_new_params so that the remaining runtime is checked against the new reservation at the next enqueue, at deboost, or immediately when blocked on an rt_mutex.
Quoted 4 lines
kernel/sched/deadline.c +0
-0
[ ... ]
Quoted 33 lines
kernel/sched/deadline.c +23
-3@@ -145,12 +177,41 @@ void dl_deboost(struct sched_dl_entity *dl_se) } }dl_drop_boost (dl_se); + +if (!task_on_rq_queued (p) && !dl_task_pi_blocked (p)) +return ; +if (dl_se->dl_new_params && !dl_se->dl_throttled &&dl_in_constrained_gap (dl_se)) +return ; +if (dl_se->dl_new_params) { + dl_se->dl_new_params =0 ; +if (!dl_se->dl_throttled) +update_dl_entity (dl_se); + } } -/* Return true if @p waits on an rt_mutex. The caller holds its pi_lock. */ -static inline bool dl_task_pi_blocked (struct task_struct *p) +/* +* Check the runtime of @p against the parameters that sched_setattr() just +* gave it, if @p waits on an rt_mutex. Before any wakeup, its waiter node +* holds its deadline. The caller then calls rt_mutex_adjust_pi(), which +* requeues the waiter with the result. Other changes of @p, such as a +* cgroup move, do not requeue it: leave the check to sched_setattr(). +* +* A boosted task, a throttled one, or one past its deadline and before +* its next period keeps the mark. Its deboost or its wakeup checks it. +* The caller holds the rq lock and the pi_lock of @p, and has updated the +* rq clock. +*/ +void dl_check_blocked_params (struct task_struct *p) { -return p->pi_blocked_on;
kernel/sched/deadline.c void dl_deboost(struct sched_dl_entity *dl_se) · line 215
Quoted 10 lines
kernel/sched/deadline.c +9
-0+struct sched_dl_entity *dl_se = &p->dl; + +if (!dl_task (p) ||task_on_rq_queued (p) || !dl_task_pi_blocked (p)) +return ; +if (dl_se->dl_new_params && !is_dl_boosted (dl_se) && + !dl_se->dl_throttled && !dl_in_constrained_gap (dl_se)) { + dl_se->dl_new_params =0 ; +update_dl_entity (dl_se); + } }
[Severity: Medium]
Should dl_proxy_block() also check and clear dl_se->dl_new_params?
When a task sleeps in futex(FUTEX_WAIT_REQUEUE_PI), p->pi_blocked_on is NULL, so dl_check_blocked_params() returns early and leaves dl_se->dl_new_params set. If futex(FUTEX_CMP_REQUEUE_PI) then requeues the task onto an rt_mutex before its old deadline expires, task_blocks_on_rt_mutex() calls dl_proxy_block() before copying p->dl.deadline into the waiter node:
kernel/sched/deadline.c:dl_proxy_block() {
...
if (!task_on_rq_queued(p) && !dl_se->dl_throttled &&
dl_time_before(dl_se->deadline, rq_clock(rq)) &&
(dl_is_implicit(dl_se) ||
!dl_time_before(rq_clock(rq), dl_next_period(dl_se))))
replenish_dl_new_period(dl_se, rq);
...
}Because dl_proxy_block() only checks whether dl_se->deadline has passed, a task whose reservation was shrunk while sleeping in FUTEX_WAIT_REQUEUE_PI is queued on the rt_mutex (and boosts the lock owner) with its stale earlier deadline, allowing it to acquire the lock ahead of waiters with earlier deadlines. Even when the old deadline has passed and dl_proxy_block() starts a new period, it leaves dl_se->dl_new_params set, so a later boost and deboost while blocked calls update_dl_entity() a second time.
[ ... ]
kernel/sched/deadline.c void __setparam_dl(struct task_struct *p, const struct sched_attr *attr) · line 5268
Quoted 9 lines
kernel/sched/deadline.c +5
-0@@ -5179,6 +5261,11 @@ void __setparam_dl(struct task_struct *p, const struct sched_attr *attr)u64 period = attr->sched_period ?: attr->sched_deadline;bool held; +if (attr->sched_runtime != dl_se->dl_runtime || + attr->sched_deadline != dl_se->dl_deadline || + period != dl_se->dl_period) + dl_se->dl_new_params =1 ; +
[Severity: Low]
Can __setparam_dl() leave dl_se->dl_new_params unset when a task leaves SCHED_DEADLINE before its 0-lag time and later re-enters the class with the same parameters?
When a task leaves SCHED_DEADLINE before its 0-lag time, switched_from_dl() leaves dl_runtime, dl_deadline and dl_period in dl_se until inactive_task_timer() runs __dl_clear_params(). If the task was PI-boosted back into dl_sched_class before the 0-lag time (without refilling from the donor), or if sched_setattr() races with inactive_task_timer() right after the 0-lag time while the task is blocked on an rt_mutex, calling sched_setattr() to restore SCHED_DEADLINE with identical parameters sees no change in dl_runtime, dl_deadline or dl_period and does not set dl_se->dl_new_params. switched_to_dl() then cancels inactive_timer without starting a new period if dl_se->deadline has not passed, and neither dl_check_blocked_params() nor dl_deboost() checks the remaining runtime against the new reservation.
Quoted 5 lines
kernel/sched/syscalls.c +0
-0
kernel/sched/syscalls.c static inline void __setscheduler_dl_pi(int newprio, int policy, · line 319
Quoted 9 lines
kernel/sched/syscalls.c +3
-0@@ -311,6 +311,9 @@ static inline void __setscheduler_dl_pi(int newprio, int policy,*/ if (!dl_prio (newprio))dl_drop_boost (&p->dl); + +/* rt_mutex_adjust_pi() then requeues the waiter of a blocked task. */ +dl_check_blocked_params (p); }#else /* !CONFIG_RT_MUTEXES */ static inline void __setscheduler_dl_pi(int newprio, int policy,
[Severity: Medium]
What happens if p owns an rt_mutex with a deadline waiter whose deadline was later than p's old deadline, and shrinking p's reservation pushes p->dl.deadline past that waiter's deadline?
In __setscheduler_dl_pi(), rt_mutex_get_top_task(p) is only checked when !dl_policy(policy):
kernel/sched/syscalls.c:__setscheduler_dl_pi() {
if (dl_prio(newprio) && !dl_policy(policy)) {
struct task_struct *pi_task = rt_mutex_get_top_task(p);
if (pi_task) {
dl_set_donor(&p->dl, pi_task);
scope->flags |= ENQUEUE_REPLENISH;
}
}
...
}When an unboosted deadline task p stays in SCHED_DEADLINE and shrinks its reservation, update_dl_entity() (in dl_check_blocked_params() or in enqueue_dl_entity() during sched_change_end()) can start a new period and move p->dl.deadline after its top waiter's deadline. Unlike rt_mutex_setprio(), which rechecks whether pi_task preempts p after dl_deboost() updates p->dl, __sched_setscheduler() never boosts p with its top waiter. If p is running, it will throttle when its reduced runtime expires while holding the lock instead of running on the waiter's donor parameters; if p is blocked on another rt_mutex, rt_mutex_adjust_pi() will propagate p's own unboosted deadline up the PI chain instead of the waiter's.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=35
Re: [PATCH v10 35/40] sched/deadline: Check the runtime of a task whose parameters change
Quoted 1 line
Should
dl_proxy_block()also check and cleardl_se->dl_new_params?
Yes. In v11, dl_proxy_block() will clear the mark and check the runtime left, with update_dl_entity().
Quoted 3 lines
Can
__setparam_dl()leavedl_se->dl_new_paramsunset when a task leavesSCHED_DEADLINEbefore its 0-lag time and later re-enters the class with the same parameters?
Yes, by reading. In v11, a task that enters the deadline policy will get the mark, even with the same parameters.
Quoted 3 lines
What happens if p owns an
rt_mutexwith a deadline waiter whose deadline was later than p's old deadline, and shrinking p's reservation pushesp->dl.deadlinepast that waiter's deadline?
p is not boosted. A boost never gives the owner the deadline of its waiter. It stops the throttle, and the refills use the donor's parameters. v7.4-rc2 does the same. v11 will not change this.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
PATCH v10 36/40 sched/deadline: Reschedule a running task throttled at its enqueue
In reply to Mathieu DesnoyersA task with a constrained deadline (dl_deadline < dl_period) can wake up after its deadline but before its next period.
Commit df8eac8cafce ("sched/deadline: Throttle a constrained deadline task activated after the deadline") throttles it then until that period. A refill would let it run for dl_runtime every dl_deadline, more than its reservation.
enqueue_dl_entity() makes this check at every enqueue, not only at a wakeup. These changes to a running task also dequeue and enqueue it:
- sched_setattr() and sched_setaffinity();
- a cgroup move;
- a deboost in rt_mutex_setprio().
A task can run past its deadline with runtime left, for example after tasks with earlier deadlines delayed it. The check then throttles it and leaves it off the dl_rq. But nothing reschedules: prio_changed_dl() sees the same deadline and returns.
The task keeps the CPU. update_curr_dl() skips a task that is off the dl_rq, so the tick neither charges it nor preempts it. It runs until it blocks, until a task with an earlier deadline wakes up, or until its next period. That is up to dl_period - dl_deadline of CPU time outside its reservation. The Sashiko AI review of this series found this. The constrained_gap_change_running case, added to deadline_bw_test later in this series, tests this. It fails without this patch.
Fix this by rescheduling when the check throttles the current task, as a throttle in update_curr_dl_se() does.
--- kernel/sched/deadline.c | 7 +++++++ 1 file changed, 7 insertions(+)
kernel/sched/deadline.c +7 -0
@@ -1996,6 +1996,13 @@ static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se) dl_se->dl_throttled =1 ;if (dl_se->runtime >0 ) dl_se->runtime =0 ; +/* +* A running task is now off the dl_rq: the tick no longer +* charges it or preempts it. Reschedule, as a throttle in +* update_curr_dl_se() does. +*/ +if (!dl_server (dl_se) &&task_current_donor (rq,dl_task_of (dl_se))) +resched_curr (rq); } }
Signature · 1 line
2.47.3
PATCH v10 37/40 sched/deadline: Keep the start of the period across a change of dl_deadline
In reply to Mathieu Desnoyersdl_next_period() works out the start of the next period of a deadline entity as deadline - dl_deadline + dl_period.
Commit 5ac69d37784b ("sched/deadline: Make sure the replenishment timer fires in the next period") added it. The replenishment timer of a task with a constrained deadline then fires at its next period, not at its deadline. Commit df8eac8cafce ("sched/deadline: Throttle a constrained deadline task activated after the deadline") uses it too. It throttles a constrained task that wakes between its deadline and its next period.
The formula assumes that the deadline was set with the current dl_deadline. But sched_setattr() can change dl_deadline during a period, and the deadline stays. The start of the next period then moves by the old dl_deadline minus the new one:
- A shorter dl_deadline moves it later. A task that wakes after its period ended is throttled until that later time. For an implicit task made constrained, that is up to dl_period - dl_deadline.
- A longer dl_deadline moves it earlier. A constrained task that wakes between its deadline and its real next period is not throttled. It gets a new runtime before its next period.
Fix this by recording the start of each period in period_start, where the deadline is set. dl_next_period() and dl_in_constrained_gap() use it.
The Sashiko AI review of this series found this. The case wake_after_deadline_shrunk of deadline_bw_test, added later in this series, tests the shorter dl_deadline. It fails without this patch.
--- include/linux/sched.h | 1 + kernel/sched/deadline.c | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-)
include/linux/sched.h +1 -0
@@ -661,6 +661,7 @@ struct sched_dl_entity {*/ s64 runtime;/* Remaining runtime for this instance */ u64 deadline;/* Absolute deadline for this instance */ +u64 period_start;/* Absolute start of this instance */ unsigned int flags;/* Specifying the scheduler behaviour */ /*
kernel/sched/deadline.c +19 -6
@@ -89,7 +89,8 @@ static inline int on_dl_rq(struct sched_dl_entity *dl_se)*/ static inline void dl_start_own_period (struct sched_dl_entity *dl_se) { - dl_se->deadline =rq_clock (rq_of_dl_se (dl_se)) + dl_se->dl_deadline; + dl_se->period_start =rq_clock (rq_of_dl_se (dl_se)); + dl_se->deadline = dl_se->period_start + dl_se->dl_deadline; dl_se->runtime = dl_se->dl_runtime; }
@@ -103,7 +104,7 @@ static inline bool dl_in_constrained_gap(struct sched_dl_entity *dl_se)u64 now =rq_clock (rq_of_dl_se (dl_se));return !dl_is_implicit (dl_se) &&dl_time_before (dl_se->deadline, now) && -dl_time_before (now, dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period); +dl_time_before (now, dl_se->period_start + dl_se->dl_period); }#ifdef CONFIG_RT_MUTEXES
@@ -1260,7 +1261,8 @@ static inline void replenish_dl_new_period(struct sched_dl_entity *dl_se, struct rq *rq) {/* for non-boosted task, pi_of(dl_se) == dl_se */ - dl_se->deadline =rq_clock (rq) +pi_of (dl_se)->dl_deadline; + dl_se->period_start =rq_clock (rq); + dl_se->deadline = dl_se->period_start +pi_of (dl_se)->dl_deadline; dl_se->runtime =pi_of (dl_se)->dl_runtime;dl_note_donor_refill (dl_se);
@@ -1384,6 +1386,7 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se)WARN_ON_ONCE (!is_dl_boosted (dl_se));if (!dl_se->dl_deadline) { dl_se->deadline =rq_clock (rq); + dl_se->period_start = dl_se->deadline; dl_se->runtime =0 ;dl_note_donor_refill (dl_se); }
@@ -1406,7 +1409,8 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se)*/ if (dl_se->dl_deadline ==0 || (dl_se->dl_defer_armed &&dl_entity_overflow (dl_se,rq_clock (rq)))) { - dl_se->deadline =rq_clock (rq) + deadline; + dl_se->period_start =rq_clock (rq); + dl_se->deadline = dl_se->period_start + deadline; dl_se->runtime = runtime;dl_note_donor_refill (dl_se); }
@@ -1425,6 +1429,7 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se)*/ while (dl_se->runtime <=0 ) { dl_se->deadline += period; + dl_se->period_start += period; dl_se->runtime += runtime; }
@@ -1658,9 +1663,14 @@ static void update_dl_entity(struct sched_dl_entity *dl_se) } } +/* +* Return the start of the next period of @dl_se. Do not work it out from +* the deadline and dl_deadline: sched_setattr() can change dl_deadline +* during the period. +*/ static inline u64 dl_next_period (struct sched_dl_entity *dl_se) { -return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period; +return dl_se->period_start + dl_se->dl_period; }#ifdef CONFIG_RT_MUTEXES
@@ -1728,7 +1738,7 @@ static int start_dl_timer(struct sched_dl_entity *dl_se)WARN_ON_ONCE (!dl_se->dl_throttled); act =ns_to_ktime (dl_se->deadline - dl_se->runtime); }else { -/* act = deadline - rel-deadline + period */ +/* act = the start of the next period */ act =ns_to_ktime (dl_next_period (dl_se)); }
@@ -2603,6 +2613,7 @@ int dl_server_apply_params(struct sched_dl_entity *dl_se, u64 runtime, u64 perio dl_se->runtime =0 ; dl_se->deadline =0 ; + dl_se->period_start =0 ; dl_se->dl_bw =to_ratio (dl_se->dl_period, dl_se->dl_runtime); dl_se->dl_density =to_ratio (dl_se->dl_deadline, dl_se->dl_runtime);
@@ -4848,6 +4859,7 @@ static void switched_from_dl(struct rq *rq, struct task_struct *p)*/ if (!p->dl.dl_runtime) { p->dl.deadline =0 ; + p->dl.period_start =0 ; p->dl.runtime =0 ; }
@@ -5394,6 +5406,7 @@ static void __dl_clear_params(struct sched_dl_entity *dl_se) dl_se->dl_density =0 ; dl_se->deadline =0 ; + dl_se->period_start =0 ; dl_se->runtime =0 ; dl_se->dl_throttled =0 ;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v10 37/40] sched/deadline: Keep the start of the period across a change of dl_deadline
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] sched/deadline: replenish_dl_entity() keeps old dl_deadline across periods - [Medium]…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] sched/deadline: replenish_dl_entity() keeps old dl_deadline across periods
- [Medium] sched/deadline: dl_is_implicit() bypasses constrained gap on dl_deadline growth
--
commit f729f2d93fa31589edb8491dd221ca94c6da13fe
Author: Mathieu Desnoyers <[email protected]>
sched/deadline: Keep the start of the period across a change of dl_deadline
This commit records the start of each deadline period in sched_dl_entity::period_start and uses it in dl_next_period() and dl_in_constrained_gap() so that mid-period changes to dl_deadline via sched_setattr() do not shift the computed start of the next period.
Quoted 4 lines
kernel/sched/deadline.c +0
-0
[ ... ]
kernel/sched/deadline.c static inline bool dl_in_constrained_gap(struct sched_dl_entity *dl_se) · line 108
Quoted 7 lines
kernel/sched/deadline.c +1
-1@@ -103,7 +104,7 @@ static inline bool dl_in_constrained_gap(struct sched_dl_entity *dl_se)u64 now =rq_clock (rq_of_dl_se (dl_se));return !dl_is_implicit (dl_se) &&dl_time_before (dl_se->deadline, now) && -dl_time_before (now, dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period); +dl_time_before (now, dl_se->period_start + dl_se->dl_period); }
[Severity: Medium]
Can a constrained task whose dl_deadline is increased to equal dl_period during an active period still bypass throttling in the gap between its current deadline and its next period?
When sched_setattr() sets dl_deadline == dl_period mid-period, dl_is_implicit(dl_se) becomes true while dl_se->deadline is still earlier than dl_next_period(dl_se). Because dl_in_constrained_gap(), enqueue_dl_entity(), update_dl_entity(), and dl_proxy_block() gate their constrained-deadline checks on !dl_is_implicit(dl_se), a task waking between dl_se->deadline and dl_next_period(dl_se) skips dl_check_constrained_dl() and receives a new period and full runtime early via replenish_dl_new_period():
kernel/sched/deadline.c:enqueue_dl_entity() {
...
if (!dl_se->dl_throttled && !dl_is_implicit(dl_se))
dl_check_constrained_dl(dl_se);
...
}[ ... ]
kernel/sched/deadline.c static void replenish_dl_entity(struct sched_dl_entity *dl_se) · line 1434
Quoted 7 lines
kernel/sched/deadline.c +1
-0@@ -1425,6 +1429,7 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se)*/ while (dl_se->runtime <=0 ) { dl_se->deadline += period; + dl_se->period_start += period; dl_se->runtime += runtime; }
[Severity: High]
Does advancing dl_se->deadline by period here keep the old relative deadline offset across future periods after sched_setattr() changes dl_deadline?
With start_dl_timer() now arming the replenishment timer at dl_se->period_start + dl_se->dl_period instead of shifting by the old dl_deadline, dl_se->deadline at replenishment time is still period_start + old_dl_deadline. Adding period to both dl_se->period_start and dl_se->deadline leaves dl_se->deadline atnew_period_start + old_dl_deadline instead of dl_se->period_start + deadline, so subsequent periods continue using the old relative deadline and can falsely throttle the task in dl_check_constrained_dl() or grant a second replenishment mid-period in update_dl_entity().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=37
Re: [PATCH v10 37/40] sched/deadline: Keep the start of the period across a change of dl_deadline
Quoted 3 lines
Can a constrained task whose
dl_deadlineis increased to equaldl_periodduring an active period still bypass throttling in the gap between its current deadline and its next period?
Yes. We answered "not a bug" on v9: with period_start kept, it is one. In v11, sched_setattr() will keep the end of a period in progress, and move its deadline to period_start plus the new dl_deadline. A new case, deadline_widened_asleep, will test it. In testing without the fix, the task got 10ms in its period instead of the 2ms it had left.
Quoted 3 lines
Does advancing
dl_se->deadlineby period here keep the old relative deadline offset across future periods aftersched_setattr()changesdl_deadline?
Yes. This patch introduced it: v7.4-rc2 shifted the period instead. v11 will set the deadline to period_start plus dl_deadline when the loop runs, for an entity that is not boosted. A new case, deadline_grows_running, will test it. In testing without the fix, the task ran 69ms after its wakeup.
The same change covers a shorter dl_period while the task is throttled. The refill would otherwise print "sched: DL replenish lagged too much". A new case, period_shrunk_throttled, will test it.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
PATCH v10 38/40 sched/deadline: Boost an owner that cannot run on its own reservation
In reply to Mathieu Desnoyersrt_mutex_setprio() lets a deadline waiter boost the owner of the lock if the waiter's deadline is earlier than the owner's.
Commit 2d3d891d3344 ("sched/deadline: Add SCHED_DEADLINE inheritance logic") added this test. A boosted owner is not throttled: when its runtime runs out, it is refilled from the waiter's parameters.
The test assumes that the owner runs on its own deadline. Two kinds of owners cannot:
- An owner that ran out of runtime is throttled until its next period.
- An owner with a constrained deadline, past its deadline and before its next period, is throttled at its next enqueue.
Commit df8eac8cafce ("sched/deadline: Throttle a constrained deadline task activated after the deadline") does this.
Such an owner can still have the earlier deadline. A deadline that has passed is always the earlier one. So the waiter does not boost such an owner. It waits until the owner's next period: up to dl_period - dl_deadline for a constrained owner. Before "sched/deadline: Reschedule a running task throttled at its enqueue", a running owner in that gap kept the CPU. It usually released the lock then. That patch now stops it at once, so its waiters wait.
An owner past its deadline and not in that gap gets a new period at its next enqueue. Its deadline is then now + dl_deadline, which can be after the waiter's. The waiter does not boost it either.
Fix this by comparing the waiter with the deadline that the owner would run on, in dl_waiter_boosts():
- A throttled owner, or one in that gap, is always boosted. The boost lasts until the owner's own next period.
- Another owner past its deadline is compared at now + dl_deadline.
dl_boost_start() saves the deadline, runtime and period start of an owner boosted this way, and dl_deboost() puts them back. The time the owner ran boosted was the donor's. If the owner blocked while boosted, its inactive timer was armed from the donor's runtime and deadline. dl_deboost() then works out its 0-lag time again. replenish_dl_entity() no longer warns about a boosted owner with an expired deadline. It starts a period of the donor.
The Sashiko AI review of this series found the gap case. The cases pi_owner_asleep_in_gap and pi_owner_throttled of deadline_bw_test, added later in this series, test the two kinds of owners. Both fail without this patch. pi_owner_throttled_asleep tests the 0-lag time worked out again at the deboost. It fails without this patch too.
--- include/linux/sched.h | 12 +++++ kernel/sched/core.c | 9 +++- kernel/sched/deadline.c | 104 +++++++++++++++++++++++++++++++++++----- kernel/sched/sched.h | 3 ++ 4 files changed, 113 insertions(+), 15 deletions(-)
include/linux/sched.h +12 -0
@@ -662,6 +662,10 @@ struct sched_dl_entity {s64 runtime;/* Remaining runtime for this instance */ u64 deadline;/* Absolute deadline for this instance */ u64 period_start;/* Absolute start of this instance */ +/* Saved by dl_boost_start(): see dl_boost_held */ +u64 own_deadline; +s64 own_runtime; +u64 own_period_start;unsigned int flags;/* Specifying the scheduler behaviour */ /*
@@ -720,6 +724,13 @@ struct sched_dl_entity {* @dl_new_params tells if sched_setattr() changed the runtime, the * deadline or the period. The next enqueue that is not boosted, or * the deboost, checks the runtime left against them, and clears it. +* +* @dl_boost_held tells if a waiter boosts the task because the task +* could not run on its own reservation: it was throttled, or past its +* deadline and before its next period. The boost then lasts until +* the task's own next period at least. The deboost puts back the +* deadline, runtime and period start saved in own_deadline, +* own_runtime and own_period_start. */ unsigned int dl_throttled : 1; unsigned int dl_yielded : 1;
@@ -735,6 +746,7 @@ struct sched_dl_entity { unsigned int dl_bw_attached : 1; unsigned int dl_bw_oncpu : 1; unsigned int dl_new_params : 1; + unsigned int dl_boost_held : 1;/* * Bandwidth enforcement timer. Each -deadline task has its
kernel/sched/core.c +7 -2
@@ -7974,10 +7974,13 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)if (!dl_prio (p->normal_prio) || (pi_task &&dl_prio (pi_task->prio) && !(p->dl.dl_new_params && p->dl.pi_se != &p->dl) && -dl_entity_preempt (&pi_task->dl, &p->dl))) { +dl_waiter_boosts (p, pi_task))) {struct sched_dl_entity *old_donor = p->dl.pi_se;dl_set_donor (&p->dl, pi_task); +if (dl_prio (p->normal_prio) && old_donor == &p->dl && + p->dl.pi_se != &p->dl) +dl_boost_start (&p->dl); scope->flags |=ENQUEUE_REPLENISH ;if (!scope->queued && p->dl.pi_se != old_donor)dl_boost_blocked (p);
@@ -7989,8 +7992,10 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)* Boost the task again then. */ if (pi_task &&dl_prio (pi_task->prio) && -dl_entity_preempt (&pi_task->dl, &p->dl)) { +dl_waiter_boosts (p, pi_task)) {dl_set_donor (&p->dl, pi_task); +if (p->dl.pi_se != &p->dl) +dl_boost_start (&p->dl); scope->flags |=ENQUEUE_REPLENISH ; } }
kernel/sched/deadline.c +91 -13
137 lines not on this page · raw message
kernel/sched/sched.h +3 -0
@@ -415,12 +415,15 @@ static inline void dl_clear_donor(struct sched_dl_entity *dl_se)extern void dl_deboost (struct sched_dl_entity *dl_se);extern void dl_boost_blocked (struct task_struct *p);extern void dl_check_blocked_params (struct task_struct *p); +extern bool dl_waiter_boosts (struct task_struct *p,struct task_struct *pi_task); +extern void dl_boost_start (struct sched_dl_entity *dl_se);/* End the boost of @dl_se. The caller holds the rq lock of its task. */ static inline void dl_drop_boost (struct sched_dl_entity *dl_se) {dl_clear_donor (dl_se); dl_se->dl_donor_refill =0 ; + dl_se->dl_boost_held =0 ; }#endif extern void __getparam_dl (struct task_struct *p,struct sched_attr *attr,unsigned int flags);
Signature · 1 line
2.47.3