Hierarchical Constant Bandwidth Server
Subsystems (4)
v6 cover Hierarchical Constant Bandwidth Server
Hello,
This is the v6 for Hierarchical Constant Bandwidth Server, aiming at replacing the current RT_GROUP_SCHED mechanism with something more robust and theoretically sound. The patchset has been presented at OSPM25 and OSPM26
(https://retis.sssup.it/ospm-summit/), and a summary of its inner workings can be found at https://lwn.net/Articles/1021332/ . You can find the previous versions of this patchset at the bottom of the page, in particular version 1 which talks in more detail what this patchset is all about and how it is implemented.
This v6 version works on the comments by the reviewers and introduces the following meaningful changes:
- Update to kernel version 7.1.
- Refactorings and general cleanups.
- Removal of substantial duplicated code.
- Express more locking constraints in code.
- New cpu.rt.max interface.
- Refactoring of migration code to reduce code duplication.
The new migration code now reuses the existing push/pull and similar functions and specializes where needed, substantially reducing the footprint of group migration code from previous versions.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - New cgroup-v2 interface:
After extensive discussions with the kernel's maintainers, we have built a new interface to support HCBS scheduling. Since this will be a cgroup-v2 only feature (the fate of cgroup-v1 old RT_GROUP_SCHED has yet to be decided), it was possible to drop the original v1 interface entirely and create a completely new one that is similar to those that are already existing.
Every cgroup has now two new files:
- cpu.rt.max (similar to the cpu.max file)
- cpu.rt.internal (read-only, not available in the root cgroup, it may be removed if deemed unnecessary, see later for details)
In this new interface, HCBS cgroups may either be set to use deadline servers, and thus reserving a specified amount of bandwidth, very similarly to the previous system, or can delegate their FIFO/RR tasks' scheduling to the nearest ancestor that it is configured (default on group creation). If the nearest configured ancestor is the root cgroup, tasks will be effectively run on the root runqueue even if their cgroup is not the root task group.
This means that subtrees are allowed to retain the original non-RT_GROUP_SCHED behaviour, scheduling on root, while the feature is nonetheless active. In the meantime other subtrees may use HCBS, and the whole hierarchy can coexist without issues.
This behaviour is specified in the cpu.rt.max file, which accepts the string "<runtime | 'max'> <period>". A zero runtime disables FIFO/RR scheduling for tasks in that group, a non-zero runtime creates a reservation and uses HCBS, a runtime of 'max' instead tells the scheduler to use the nearest configured ancestor for the FIFO/RR task scheduling.
The admission test now does not only check the immediate children of a cgroup for schedulability (recall that a group's bandwidth must be always greater than or equal to its children total bandwidth), but it has to check its whole
subtree: if a child delegates its tasks to its parents (runtime = 'max'), then this child's own children (the grandchildrens) are effectively viewed as immediate children that compete for the same bandwidth of their grandparent, and so on down the hierarchy.
To support both threaded and domain cgroups, the original test that allowed only to run tasks in leaf cgroups has been removed: this is already enforced for domain cgroups by existing code, while this must not be the case for threaded cgroups.
Since groups in the middle of the hierarchy can now also run tasks, their dl_servers must be configured properly: a parent cgroup dl_servers can only use their assigned bandwidth minus the total of their children. The cpu.rt.internal file reads exactly what is this "remainder" bandwidth. Since dl_servers must have a runtime and period values assigned, the period is taken from the user configured cpu.rt.max file and the runtime is computed from the remainder bw. This runtime and the period are the values shown by cpu.rt.internal.
Supporting both threaded and domain cgroups also dropped all the extra code related to active and 'live' cgroups as mentioned in previous RFCs.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Summary of the patches:
1-2) Commits already included in sched/tip (not yet in mainline). 3-8) Preparation patches, so that the RT classes' code can be used both
for normal and cgroup scheduling.
9-19) Implementation of HCBS, no migration.
The old RT_GROUP_SCHED code is removed.
16) Remove support for cgroup-v1.
17) Implement cgroup-v2 cpu.rt.max interface.
20-24) Add support for tasks migration.
25) Documentation for HCBS.
Updates from v5:
- Rebase to latest master.
- General rebasing/cleanup.
- More locking contraints expressed in code.
- New cpu.rt.max interface.
- Refactoring of migration code.
Updates from v4:
- Rebase to latest tip/master.
- General rebasing/cleanup.
- Update default sysctl_sched_rt_runtime to 1s, same as the period.
- Fix non-deferred deadline server replenishment logic.
- Add missing RCU read sections.
- Account HCBS servers along with their tasks when the servers are active.
- Release bandwidth resources early in unregister_rt_sched_group.
- Drop server_try_pull_task as it is now redundant.
- Remove dl_server_stop call in dequeue_task_rt.
- Update to reuse __checkparam_dl for deadline servers.
Updates from v3:
- Rebase to latest tip/master.
- General rebasing/cleanup.
- Add Documentation.
- Define **live** and **active** groups.
- Introduce server_try_pull_task in place of the removed server_has_task.
- Introduce RELEASE_LOCK helper macro for guard-based locking.
- Update inc/dec_dl_tasks to account for served runqueues regardless of the server type.
- Fix computing of new bandwidth values in dl_init_tg.
- Fix check in dl_check_tg to use capacity scaling.
- Fix wakeup_preempt_rt to check if curr is a DEADLINE task.
Updates from v2:
- Rebase to latest tip/master.
- Remove fair-servers' bw reclaiming.
- Fix a check which prevented execution of wakeup_preempt code.
- Fix a priority check in group_pull_rt_task between tasks of different groups.
- Rework allocation/deallocation code for rt-cgroups.
- Update signatures for some group related migration functions.
- Add documentation for wakeup_preempt preemption rules.
Updates from v1:
- Rebase to latest tip/master.
- Add migration code.
- Split big patches for more readability.
- Refactor code to use guarded locks where applicable.
- Remove unnecessary patches from v1 which have been addressed differently by mainline updates.
- Remove unnecessary checks and general code cleanup.
Notes:
Patches 1-2 have already been merged in sched/tip, but not yet merged in master.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Testing v6:
The patchset has been tested with a suite of tests tailored to stress all the implemented functionalities.
The tests are available at https://github.com/Yurand2000/HCBS-Test-Suite . Refer to the README of the repository for more details.
Follow these steps to test HCBS v6:
- Get the HCBS patch up and running. Any kernel/disto should work effortlessly.
- Get, compile and _install_ the tests.
- Run the go_rt.sh script to set the frequency of the CPUs to a fixed value and disable hyperthreading and power saving features.
- Run the run_tests.sh full script, to run the whole test suite.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Future Work:
We think the current patchset is stable enough. Our current test suite demonstrates, on our limited hardware, that the kernel does not throw warnings and that it is actually possible to guarantee time reservations and isolation among tenants.
Comments on the new cpu.rt.max interface are to be expected, but hopefully with this new ideas we have solved some of the issues mentioned in the past, such as not being able to use the cpu controller because standard FIFO/RR tasks had to be migrated to the root cgroup first. For the future it needs to be investigated how to integrate this interface with the cpuset controller and with the multiCPU feature which was presented at OSPM26.
Additional future work:
- unprivileged FIFO/RR in cgroups.
- capacity aware bandwidth reservation.
- hotplug/hotunplug management.
Have a nice day,
Yuri
v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/
v5: https://lore.kernel.org/all/[email protected]/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Yuri Andriaccio (14):
sched/deadline: Fix replenishment logic for non-deferred servers sched/rt: Update default bandwidth for real-time tasks to ONE sched/rt: Disable RT_GROUP_SCHED
sched/rt: Remove unnecessary runqueue pointer in struct rt_rq sched/rt: Add {alloc/unregister/free}_rt_sched_group sched/rt: Implement dl-server operations for rt-cgroups. sched/rt: Update task event callbacks for HCBS scheduling sched/rt: Remove support for cgroups-v1
sched/rt: Update task's RT runqueue when switching scheduling class sched/rt: Add HCBS migration code to related functions sched/rt: Hook HCBS migration functions
sched/rt: Try pull task on empty server pick. sched/core: Execute enqueued balance callbacks after
migrate_disable_switch
Documentation: Update documentation for real-time cgroups
luca abeni (11):
sched/deadline: Do not access dl_se->rq directly sched/deadline: Distinguish between dl_rq and my_q sched/rt: Pass an rt_rq instead of an rq where needed sched/rt: Move functions from rt.c to sched.h
sched/rt: Introduce HCBS specific structs in task_group sched/core: Initialize HCBS specific structures. sched/deadline: Add dl_init_tg
sched/deadline: Account rt-cgroups bandwidth in deadline tasks
schedulability tests.
sched/rt: Update rt-cgroup schedulability checks sched/rt: Remove old RT_GROUP_SCHED data structures sched/core: Execute enqueued balance callbacks when changing allowed
CPUs
Documentation/scheduler/sched-rt-group.rst | 470 ++++- include/linux/rcupdate.h | 1 + include/linux/sched.h | 12 +- kernel/sched/autogroup.c | 4 +- kernel/sched/core.c | 143 +- kernel/sched/deadline.c | 221 ++- kernel/sched/debug.c | 6 - kernel/sched/ext.c | 4 +- kernel/sched/fair.c | 4 +- kernel/sched/rt.c | 2046 +++++++++----------- kernel/sched/sched.h | 214 +- kernel/sched/syscalls.c | 11 +- 12 files changed, 1761 insertions(+), 1375 deletions(-)
base-commit: e43ffb69e0438cddd72aaa30898b4dc446f664f8 --
2.54.0
RFC PATCH v6 1/25 sched/deadline: Fix replenishment logic for non-deferred servers
Enqueue and replenish non-deferred deadline servers when their runtime is exhausted and the replenishment timer could not be started because it is too close to the wake-up instant.
---
Already merged in sched/tip:
https://git.kernel.org/tip/eecd5e117cfa63a353f4c69fdcea5d9b14af698e
--- kernel/sched/deadline.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/sched/deadline.c +6 -2
@@ -1515,8 +1515,12 @@ static void update_curr_dl_se(struct rq *rq, struct sched_dl_entity *dl_se, s64if (unlikely (is_dl_boosted (dl_se) || !start_dl_timer (dl_se))) {if (dl_server (dl_se)) { -replenish_dl_new_period (dl_se, rq); -start_dl_timer (dl_se); +if (dl_se->dl_defer) { +replenish_dl_new_period (dl_se, rq); +start_dl_timer (dl_se); + }else { +enqueue_dl_entity (dl_se,ENQUEUE_REPLENISH ); + } }else {enqueue_task_dl (rq,dl_task_of (dl_se),ENQUEUE_REPLENISH ); }
--
2.54.0
RFC PATCH v6 2/25 sched/rt: Update default bandwidth for real-time tasks to ONE
In reply to Yuri AndriaccioSet the default total bandwidth for SCHED_DEADLINE tasks and servers to ONE.
FIFO/RR tasks are already throttled by fair-servers and ext-servers, and the sysctl_sched_rt_runtime parameter now only defines the total bw that is allowed to deadline entities.
---
Already merged in sched/tip:
https://git.kernel.org/tip/c2e390197ad1360db6686a8c89abaafaf83adf72
--- kernel/sched/rt.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/rt.c +2 -2
@@ -19,9 +19,9 @@ int sysctl_sched_rt_period = 1000000;/* * part of the period that we allow rt tasks to run in us. -* default: 0.95s +* default: 1s */ -int sysctl_sched_rt_runtime =950000 ; +int sysctl_sched_rt_runtime =1000000 ;#ifdef CONFIG_SYSCTL static int sysctl_sched_rr_timeslice = (MSEC_PER_SEC *RR_TIMESLICE ) /HZ ;
--
2.54.0
RFC PATCH v6 3/25 sched/deadline: Do not access dl_se->rq directly
In reply to Yuri AndriaccioFrom: luca abeni <[email protected]>
Make deadline.c code access the runqueue of a scheduling entity saved in the sched_dl_entity data structure. This allows future patches to save different runqueues in sched_dl_entity other than the global runqueues.
Move dl_server_apply_params call in sched_init_dl_servers as the rq_of_dl_se function will return the correct deadline entity only if the dl_server flag is set.
Add a WARN_ON on the return value of dl_server_apply_params in sched_init_dl_servers as this function may fail if the kernel is not configured correctly.
--- kernel/sched/deadline.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
kernel/sched/deadline.c +17 -17
@@ -869,7 +869,7 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se)* and arm the defer timer. */ if (dl_se->dl_defer && !dl_se->dl_defer_running && -dl_time_before (rq_clock (dl_se->rq), dl_se->deadline - dl_se->runtime)) { +dl_time_before (rq_clock (rq), dl_se->deadline - dl_se->runtime)) {if (!is_dl_boosted (dl_se)) {/*
@@ -1170,11 +1170,11 @@ static enum hrtimer_restart dl_server_timer(struct hrtimer *timer, struct sched_* of time. The dl_server_min_res serves as a limit to avoid * forwarding the timer for a too small amount of time. */ -if (dl_time_before (rq_clock (dl_se->rq), +if (dl_time_before (rq_clock (rq), (dl_se->deadline - dl_se->runtime - dl_server_min_res))) {/* reset the defer timer */ - fw = dl_se->deadline -rq_clock (dl_se->rq) - dl_se->runtime; + fw = dl_se->deadline -rq_clock (rq) - dl_se->runtime;hrtimer_forward_now (timer,ns_to_ktime (fw));return HRTIMER_RESTART ;
@@ -1185,7 +1185,7 @@ static enum hrtimer_restart dl_server_timer(struct hrtimer *timer, struct sched_enqueue_dl_entity (dl_se,ENQUEUE_REPLENISH ); -if (!dl_task (dl_se->rq->curr) ||dl_entity_preempt (dl_se, &dl_se->rq->curr->dl)) +if (!dl_task (rq->curr) ||dl_entity_preempt (dl_se, &rq->curr->dl))resched_curr (rq);__push_dl_task (rq, rf);
@@ -1481,7 +1481,7 @@ static void update_curr_dl_se(struct rq *rq, struct sched_dl_entity *dl_se, s64hrtimer_try_to_cancel (&dl_se->dl_timer); -replenish_dl_new_period (dl_se, dl_se->rq); +replenish_dl_new_period (dl_se, rq);if (idle) dl_se->dl_defer_idle =1 ;
@@ -1578,14 +1578,14 @@ static void update_curr_dl_se(struct rq *rq, struct sched_dl_entity *dl_se, s64void dl_server_update_idle (struct sched_dl_entity *dl_se,s64 delta_exec) {if (dl_se->dl_server_active && dl_se->dl_runtime && dl_se->dl_defer) -update_curr_dl_se (dl_se->rq, dl_se, delta_exec); +update_curr_dl_se (rq_of_dl_se (dl_se), dl_se, delta_exec); }void dl_server_update (struct sched_dl_entity *dl_se,s64 delta_exec) {/* 0 runtime = fair server disabled */ if (dl_se->dl_server_active && dl_se->dl_runtime) -update_curr_dl_se (dl_se->rq, dl_se, delta_exec); +update_curr_dl_se (rq_of_dl_se (dl_se), dl_se, delta_exec); }/*
@@ -1794,7 +1794,7 @@ void dl_server_update(struct sched_dl_entity *dl_se, s64 delta_exec)*/ void dl_server_start (struct sched_dl_entity *dl_se) { -struct rq *rq = dl_se->rq; +struct rq *rq; dl_se->dl_defer_idle =0 ;if (!dl_server (dl_se) || dl_se->dl_server_active || !dl_se->dl_runtime)
@@ -1803,16 +1803,16 @@ void dl_server_start(struct sched_dl_entity *dl_se)/* * Update the current task to 'now'. */ + rq =rq_of_dl_se (dl_se); rq->donor->sched_class->update_curr (rq); -if (WARN_ON_ONCE (!cpu_online (cpu_of (rq))))return ;trace_sched_dl_server_start_tp (dl_se,cpu_of (rq),dl_get_type (dl_se, rq)); dl_se->dl_server_active =1 ;enqueue_dl_entity (dl_se,ENQUEUE_WAKEUP ); -if (!dl_task (dl_se->rq->curr) ||dl_entity_preempt (dl_se, &rq->curr->dl)) -resched_curr (dl_se->rq); +if (!dl_task (rq->curr) ||dl_entity_preempt (dl_se, &rq->curr->dl)) +resched_curr (rq); }void dl_server_stop (struct sched_dl_entity *dl_se)
@@ -1856,9 +1856,9 @@ void sched_init_dl_servers(void)WARN_ON (dl_server (dl_se)); -dl_server_apply_params (dl_se, runtime, period,1 ); - dl_se->dl_server =1 ; +WARN_ON (dl_server_apply_params (dl_se, runtime, period,1 )); + dl_se->dl_defer =1 ;setup_new_dl_entity (dl_se);
@@ -1867,9 +1867,9 @@ void sched_init_dl_servers(void)WARN_ON (dl_server (dl_se)); -dl_server_apply_params (dl_se, runtime, period,1 ); - dl_se->dl_server =1 ; +WARN_ON (dl_server_apply_params (dl_se, runtime, period,1 )); + dl_se->dl_defer =1 ;setup_new_dl_entity (dl_se);#endif
@@ -1895,7 +1895,7 @@ int dl_server_apply_params(struct sched_dl_entity *dl_se, u64 runtime, u64 perio {u64 old_bw = init ?0 :to_ratio (dl_se->dl_period, dl_se->dl_runtime);u64 new_bw =to_ratio (period, runtime); -struct rq *rq = dl_se->rq; +struct rq *rq =rq_of_dl_se (dl_se);int cpu =cpu_of (rq);struct dl_bw *dl_b;unsigned long cap;
@@ -1971,7 +1971,7 @@ static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer) p =dl_task_of (dl_se); rq =task_rq_lock (p, &rf); }else { - rq = dl_se->rq; + rq =rq_of_dl_se (dl_se);rq_lock (rq, &rf); }
Signature · 1 line
2.54.0
RFC PATCH v6 4/25 sched/deadline: Distinguish between dl_rq and my_q
In reply to Yuri AndriaccioFrom: luca abeni <[email protected]>
Split the single runqueue pointer in sched_dl_entity into two separate pointers, following the existing pattern used by sched_rt_entity:
- dl_rq: Points to the deadline runqueue where this entity is queued (global runqueue).
- my_q: Points to the runqueue that this entity serves (for servers).
This distinction is currently redundant for the fair_server and ext_servers (both point to the same CPU's structures), but is essential for future RT cgroup support where deadline servers will be queued on the global dl_rq while serving tasks from cgroup-specific runqueues.
Update rq_of_dl_se() to use container_of_const() to recover the global rq from dl_rq, and update fair.c and ext.c to explicitly use my_q (local rq) when accessing the served runqueue.
Update dl_server_init() to take a dl_rq pointer (use to retrieve the global runqueue where the dl_server is scheduled) and a rq pointer (for the local runqueue served by the server).
--- include/linux/sched.h | 6 ++++-- kernel/sched/deadline.c | 17 ++++++++++++----- kernel/sched/ext.c | 4 ++-- kernel/sched/fair.c | 4 ++-- kernel/sched/sched.h | 3 ++- 5 files changed, 22 insertions(+), 12 deletions(-)
include/linux/sched.h +4 -2
@@ -733,9 +733,11 @@ struct sched_dl_entity {* Bits for DL-server functionality. Also see the comment near * dl_server_update(). * -* @rq the runqueue this server is for +* @dl_rq the runqueue on which this entity is (to be) queued +* @my_q the runqueue "owned" by this entity */ -struct rq *rq; +struct dl_rq *dl_rq; +struct rq *my_q;dl_server_pick_f server_pick_task;#ifdef CONFIG_RT_MUTEXES
kernel/sched/deadline.c +12 -5
@@ -65,10 +65,12 @@ static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)static inline struct rq *rq_of_dl_se (struct sched_dl_entity *dl_se) { -struct rq *rq = dl_se->rq; +struct rq *rq;if (!dl_server (dl_se)) rq =task_rq (dl_task_of (dl_se)); +else + rq = container_of_const(dl_se->dl_rq, struct rq, dl);return rq; }
@@ -1817,11 +1819,14 @@ void dl_server_start(struct sched_dl_entity *dl_se)void dl_server_stop (struct sched_dl_entity *dl_se) { +struct rq *rq; +if (!dl_server (dl_se) || !dl_server_active (dl_se))return ; -trace_sched_dl_server_stop_tp (dl_se,cpu_of (dl_se->rq), -dl_get_type (dl_se, dl_se->rq)); + rq =rq_of_dl_se (dl_se); +trace_sched_dl_server_stop_tp (dl_se,cpu_of (rq), +dl_get_type (dl_se, rq));dequeue_dl_entity (dl_se,DEQUEUE_SLEEP );hrtimer_try_to_cancel (&dl_se->dl_timer); dl_se->dl_defer_armed =0 ;
@@ -1830,10 +1835,12 @@ void dl_server_stop(struct sched_dl_entity *dl_se) dl_se->dl_server_active =0 ; } -void dl_server_init (struct sched_dl_entity *dl_se,struct rq *rq, +void dl_server_init (struct sched_dl_entity *dl_se,struct dl_rq *dl_rq, +struct rq *served_rq,dl_server_pick_f pick_task) { - dl_se->rq = rq; + dl_se->dl_rq = dl_rq; + dl_se->my_q = served_rq; dl_se->server_pick_task = pick_task; }
kernel/sched/ext.c +2 -2
@@ -3252,7 +3252,7 @@ ext_server_pick_task(struct sched_dl_entity *dl_se, struct rq_flags *rf)if (!scx_enabled ())return NULL ; -return do_pick_task_scx (dl_se->rq, rf, true); +return do_pick_task_scx (dl_se->my_q, rf, true); }/*
@@ -3264,7 +3264,7 @@ void ext_server_init(struct rq *rq)init_dl_entity (dl_se); -dl_server_init (dl_se, rq, ext_server_pick_task); +dl_server_init (dl_se, &rq->dl, rq, ext_server_pick_task); }#ifdef CONFIG_SCHED_CORE
kernel/sched/fair.c +2 -2
@@ -9315,7 +9315,7 @@ pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rfstatic struct task_struct *fair_server_pick_task (struct sched_dl_entity *dl_se,struct rq_flags *rf) { -return pick_task_fair (dl_se->rq, rf); +return pick_task_fair (dl_se->my_q, rf); }void fair_server_init (struct rq *rq)
@@ -9324,7 +9324,7 @@ void fair_server_init(struct rq *rq)init_dl_entity (dl_se); -dl_server_init (dl_se, rq, fair_server_pick_task); +dl_server_init (dl_se, &rq->dl, rq, fair_server_pick_task); }/*
kernel/sched/sched.h +2 -1
@@ -412,7 +412,8 @@ extern void dl_server_update_idle(struct sched_dl_entity *dl_se, s64 delta_exec)extern void dl_server_update (struct sched_dl_entity *dl_se,s64 delta_exec);extern void dl_server_start (struct sched_dl_entity *dl_se);extern void dl_server_stop (struct sched_dl_entity *dl_se); -extern void dl_server_init (struct sched_dl_entity *dl_se,struct rq *rq, +extern void dl_server_init (struct sched_dl_entity *dl_se,struct dl_rq *dl_rq, +struct rq *served_rq,dl_server_pick_f pick_task);extern void sched_init_dl_servers (void );
--
2.54.0
RFC PATCH v6 5/25 sched/rt: Pass an rt_rq instead of an rq where needed
In reply to Yuri AndriaccioFrom: luca abeni <[email protected]>
Make rt.c code access the runqueue through the rt_rq data structure rather than passing an rq pointer directly. This allows future patches to define rt_rq data structures which do not refer only to the global runqueue, but also to local cgroup runqueues (as rt_rq will not be always equal to &rq->rt).
--- kernel/sched/rt.c | 169 +++++++++++++++++++++++++++------------------- 1 file changed, 100 insertions(+), 69 deletions(-)
kernel/sched/rt.c +100 -69
@@ -370,9 +370,9 @@ static inline void rt_clear_overload(struct rq *rq)cpumask_clear_cpu (rq->cpu, rq->rd->rto_mask); } -static inline int has_pushable_tasks (struct rq *rq) +static inline int has_pushable_tasks (struct rt_rq *rt_rq) { -return !plist_head_empty (&rq->rt.pushable_tasks); +return !plist_head_empty (&rt_rq->pushable_tasks); } static DEFINE_PER_CPU(struct balance_callback, rt_push_head);
@@ -381,50 +381,66 @@ static DEFINE_PER_CPU(struct balance_callback, rt_pull_head);static void push_rt_tasks (struct rq *);static void pull_rt_task (struct rq *); -static inline void rt_queue_push_tasks (struct rq *rq) +static inline void rt_queue_push_tasks (struct rt_rq *rt_rq) { -if (!has_pushable_tasks (rq)) + struct rq *rq = container_of_const(rt_rq, struct rq, rt); + +if (!has_pushable_tasks (rt_rq))return ;queue_balance_callback (rq, &per_cpu (rt_push_head, rq->cpu), push_rt_tasks); } -static inline void rt_queue_pull_task (struct rq *rq) +static inline void rt_queue_pull_task (struct rt_rq *rt_rq) { + struct rq *rq = container_of_const(rt_rq, struct rq, rt); +queue_balance_callback (rq, &per_cpu (rt_pull_head, rq->cpu), pull_rt_task); } -static void enqueue_pushable_task (struct rq *rq,struct task_struct *p) +static void push_rt_rq_tasks (struct rt_rq *rt_rq); + +static void push_rt_tasks (struct rq *global_rq) { +push_rt_rq_tasks (&global_rq->rt); +} + +static void pull_rt_rq_task (struct rt_rq *this_rt_rq); + +static void pull_rt_task (struct rq *global_rq) { +pull_rt_rq_task (&global_rq->rt); +} + +static void enqueue_pushable_task (struct rt_rq *rt_rq,struct task_struct *p) { -plist_del (&p->pushable_tasks, &rq->rt.pushable_tasks); +plist_del (&p->pushable_tasks, &rt_rq->pushable_tasks);plist_node_init (&p->pushable_tasks, p->prio); -plist_add (&p->pushable_tasks, &rq->rt.pushable_tasks); +plist_add (&p->pushable_tasks, &rt_rq->pushable_tasks);/* Update the highest prio pushable task */ -if (p->prio < rq->rt.highest_prio.next) - rq->rt.highest_prio.next = p->prio; +if (p->prio < rt_rq->highest_prio.next) + rt_rq->highest_prio.next = p->prio; -if (!rq->rt.overloaded) { -rt_set_overload (rq); - rq->rt.overloaded =1 ; +if (!rt_rq->overloaded) { +rt_set_overload (rq_of_rt_rq (rt_rq)); + rt_rq->overloaded =1 ; } } -static void dequeue_pushable_task (struct rq *rq,struct task_struct *p) +static void dequeue_pushable_task (struct rt_rq *rt_rq,struct task_struct *p) { -plist_del (&p->pushable_tasks, &rq->rt.pushable_tasks); +plist_del (&p->pushable_tasks, &rt_rq->pushable_tasks);/* Update the new highest prio pushable task */ -if (has_pushable_tasks (rq)) { - p =plist_first_entry (&rq->rt.pushable_tasks, +if (has_pushable_tasks (rt_rq)) { + p =plist_first_entry (&rt_rq->pushable_tasks, struct task_struct, pushable_tasks); - rq->rt.highest_prio.next = p->prio; + rt_rq->highest_prio.next = p->prio; }else { - rq->rt.highest_prio.next =MAX_RT_PRIO -1 ; + rt_rq->highest_prio.next =MAX_RT_PRIO -1 ; -if (rq->rt.overloaded) { -rt_clear_overload (rq); - rq->rt.overloaded =0 ; +if (rt_rq->overloaded) { +rt_clear_overload (rq_of_rt_rq (rt_rq)); + rt_rq->overloaded =0 ; } } }
@@ -1436,6 +1452,7 @@ static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags) { struct sched_rt_entity *rt_se = &p->rt; +struct rt_rq *rt_rq =rt_rq_of_se (rt_se);if (flags &ENQUEUE_WAKEUP ) rt_se->timeout =0 ;
@@ -1449,17 +1466,18 @@ enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)return ;if (!task_current (rq, p) && p->nr_cpus_allowed >1 ) -enqueue_pushable_task (rq, p); +enqueue_pushable_task (rt_rq, p); }static bool dequeue_task_rt (struct rq *rq,struct task_struct *p,int flags) {struct sched_rt_entity *rt_se = &p->rt; +struct rt_rq *rt_rq =rt_rq_of_se (rt_se);update_curr_rt (rq);dequeue_rt_entity (rt_se, flags); -dequeue_pushable_task (rq, p); +dequeue_pushable_task (rt_rq, p);return true; }
@@ -1498,7 +1516,7 @@ static void yield_task_rt(struct rq *rq)requeue_task_rt (rq, rq->donor,0 ); } -static int find_lowest_rq (struct task_struct *task); +static int find_lowest_rt_rq (struct task_struct *task);static int select_task_rq_rt (struct task_struct *p,int cpu,int flags)
@@ -1548,7 +1566,7 @@ select_task_rq_rt(struct task_struct *p, int cpu, int flags) (curr->nr_cpus_allowed <2 || donor->prio <= p->prio);if (test || !rt_task_fits_capacity (p, cpu)) { -int target =find_lowest_rq (p); +int target =find_lowest_rt_rq (p);/* * Bail out if we were forcing a migration to find a better
@@ -1606,7 +1624,7 @@ static int balance_rt(struct rq *rq, struct task_struct *p, struct rq_flags *rf)* not yet started the picking loop. */ rq_unpin_lock (rq, rf); -pull_rt_task (rq); +pull_rt_rq_task (&rq->rt);rq_repin_lock (rq, rf); }
@@ -1650,14 +1668,14 @@ static void wakeup_preempt_rt(struct rq *rq, struct task_struct *p, int flags)static inline void set_next_task_rt (struct rq *rq,struct task_struct *p,bool first) {struct sched_rt_entity *rt_se = &p->rt; -struct rt_rq *rt_rq = &rq->rt; +struct rt_rq *rt_rq =rt_rq_of_se (&p->rt); p->se.exec_start =rq_clock_task (rq);if (on_rt_rq (&p->rt))update_stats_wait_end_rt (rt_rq, rt_se);/* The running task is never eligible for pushing */ -dequeue_pushable_task (rq, p); +dequeue_pushable_task (rt_rq, p);if (!first)return ;
@@ -1670,7 +1688,7 @@ static inline void set_next_task_rt(struct rq *rq, struct task_struct *p, bool fif (rq->donor->sched_class != &rt_sched_class)update_rt_rq_load_avg (rq_clock_pelt (rq), rq,0 ); -rt_queue_push_tasks (rq); +rt_queue_push_tasks (rt_rq); }static struct sched_rt_entity *pick_next_rt_entity (struct rt_rq *rt_rq)
@@ -1721,7 +1739,7 @@ static struct task_struct *pick_task_rt(struct rq *rq, struct rq_flags *rf)static void put_prev_task_rt (struct rq *rq,struct task_struct *p,struct task_struct *next) {struct sched_rt_entity *rt_se = &p->rt; -struct rt_rq *rt_rq = &rq->rt; +struct rt_rq *rt_rq =rt_rq_of_se (&p->rt);if (on_rt_rq (&p->rt))update_stats_wait_start_rt (rt_rq, rt_se);
@@ -1737,7 +1755,7 @@ static void put_prev_task_rt(struct rq *rq, struct task_struct *p, struct task_s* if it is still active */ if (on_rt_rq (&p->rt) && p->nr_cpus_allowed >1 ) -enqueue_pushable_task (rq, p); +enqueue_pushable_task (rt_rq, p); }/* Only try algorithms three times */
@@ -1747,16 +1765,16 @@ static void put_prev_task_rt(struct rq *rq, struct task_struct *p, struct task_s* Return the highest pushable rq's task, which is suitable to be executed * on the CPU, NULL otherwise */ -static struct task_struct *pick_highest_pushable_task (struct rq *rq,int cpu) +static struct task_struct *pick_highest_pushable_task (struct rt_rq *rt_rq,int cpu) { -struct plist_head *head = &rq->rt.pushable_tasks; +struct plist_head *head = &rt_rq->pushable_tasks;struct task_struct *p; -if (!has_pushable_tasks (rq)) +if (!has_pushable_tasks (rt_rq))return NULL ;plist_for_each_entry (p, head, pushable_tasks) { -if (task_is_pushable (rq, p, cpu)) +if (task_is_pushable (rq_of_rt_rq (rt_rq), p, cpu))return p; }
@@ -1765,7 +1783,7 @@ static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu) static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask); -static int find_lowest_rq (struct task_struct *task) +static int find_lowest_rt_rq (struct task_struct *task) {struct sched_domain *sd;struct cpumask *lowest_mask =this_cpu_cpumask_var_ptr (local_cpu_mask);
@@ -1856,12 +1874,13 @@ static int find_lowest_rq(struct task_struct *task)return -1 ; } -static struct task_struct *pick_next_pushable_task (struct rq *rq) +static struct task_struct *pick_next_pushable_task (struct rt_rq *rt_rq) { -struct plist_head *head = &rq->rt.pushable_tasks; +struct rq *rq =rq_of_rt_rq (rt_rq); +struct plist_head *head = &rt_rq->pushable_tasks;struct task_struct *i, *p =NULL ; -if (!has_pushable_tasks (rq)) +if (!has_pushable_tasks (rt_rq))return NULL ;plist_for_each_entry (i, head, pushable_tasks) {
@@ -1887,14 +1906,15 @@ static struct task_struct *pick_next_pushable_task(struct rq *rq) }/* Will lock the rq it finds */ -static struct rq *find_lock_lowest_rq (struct task_struct *task,struct rq *rq) +static struct rt_rq *find_lock_lowest_rt_rq (struct task_struct *task,struct rt_rq *rt_rq) { +struct rq *rq =rq_of_rt_rq (rt_rq);struct rq *lowest_rq =NULL ;int tries;int cpu;for (tries =0 ; tries <RT_MAX_TRIES ; tries++) { - cpu =find_lowest_rq (task); + cpu =find_lowest_rt_rq (task);if ((cpu ==-1 ) || (cpu == rq->cpu))break ;
@@ -1925,7 +1945,7 @@ static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)*/ if (unlikely (is_migration_disabled (task) || !cpumask_test_cpu (lowest_rq->cpu, &task->cpus_mask) || - task !=pick_next_pushable_task (rq))) { + task !=pick_next_pushable_task (rt_rq))) {double_unlock_balance (rq, lowest_rq); lowest_rq =NULL ;
@@ -1942,7 +1962,13 @@ static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq) lowest_rq =NULL ; } -return lowest_rq; +return &lowest_rq->rt; +} + +static struct rq *find_lock_lowest_rq (struct task_struct *task,struct rq *rq) { +struct rt_rq *rt_rq =find_lock_lowest_rt_rq (task, &rq->rt); + +return rq_of_rt_rq (rt_rq); }/*
@@ -1950,16 +1976,17 @@ static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)* running task can migrate over to a CPU that is running a task * of lesser priority. */ -static int push_rt_task (struct rq *rq,bool pull) +static int push_rt_rq_task (struct rt_rq *rt_rq,bool pull) {struct task_struct *next_task; -struct rq *lowest_rq; +struct rq *lowest_rq, *rq =rq_of_rt_rq (rt_rq); +struct rt_rq *lowest_rt_rq;int ret =0 ; -if (!rq->rt.overloaded) +if (!rt_rq->overloaded)return 0 ; - next_task =pick_next_pushable_task (rq); + next_task =pick_next_pushable_task (rt_rq);if (!next_task)return 0 ;
@@ -1982,7 +2009,7 @@ static int push_rt_task(struct rq *rq, bool pull)return 0 ;/* -* Invoking find_lowest_rq() on anything but an RT task doesn't +* Invoking find_lowest_rt_rq() on anything but an RT task doesn't * make sense. Per the above priority check, curr has to * be of higher priority than next_task, so no need to * reschedule when bailing out.
@@ -1993,7 +2020,7 @@ static int push_rt_task(struct rq *rq, bool pull)if (rq->donor->sched_class != &rt_sched_class)return 0 ; - cpu =find_lowest_rq (rq->curr); + cpu =find_lowest_rt_rq (rq->curr);if (cpu ==-1 || cpu == rq->cpu)return 0 ;
@@ -2022,19 +2049,19 @@ static int push_rt_task(struct rq *rq, bool pull)/* We might release rq lock */ get_task_struct (next_task); -/* find_lock_lowest_rq locks the rq if found */ - lowest_rq =find_lock_lowest_rq (next_task, rq); -if (!lowest_rq) { +/* find_lock_lowest_rt_rq locks the rq if found */ + lowest_rt_rq =find_lock_lowest_rt_rq (next_task, rt_rq); +if (!lowest_rt_rq) {struct task_struct *task;/* -* find_lock_lowest_rq releases rq->lock +* find_lock_lowest_rt_rq releases rq->lock * so it is possible that next_task has migrated. * * We need to make sure that the task is still on the same * run-queue and is also still the next task eligible for * pushing. */ - task =pick_next_pushable_task (rq); + task =pick_next_pushable_task (rt_rq);if (task == next_task) {/* * The task hasn't migrated, and is still the next
@@ -2057,6 +2084,7 @@ static int push_rt_task(struct rq *rq, bool pull) goto retry; } + lowest_rq =rq_of_rt_rq (lowest_rt_rq);move_queued_task_locked (rq, lowest_rq, next_task);resched_curr (lowest_rq); ret =1 ;
@@ -2068,10 +2096,10 @@ static int push_rt_task(struct rq *rq, bool pull)return ret; } -static void push_rt_tasks (struct rq *rq) +static void push_rt_rq_tasks (struct rt_rq *rt_rq) { -/* push_rt_task will return true if it moved an RT */ -while (push_rt_task (rq, false)) +/* push_rt_rq_task will return true if it moved an RT */ +while (push_rt_rq_task (rt_rq, false)) ; }
@@ -2227,9 +2255,9 @@ void rto_push_irq_work_func(struct irq_work *work)* We do not need to grab the lock to check for has_pushable_tasks. * When it gets updated, a check is made if a push is possible. */ -if (has_pushable_tasks (rq)) { +if (has_pushable_tasks (&rq->rt)) {raw_spin_rq_lock (rq); -while (push_rt_task (rq, true)) +while (push_rt_rq_task (&rq->rt, true)) ;raw_spin_rq_unlock (rq); }
@@ -2251,11 +2279,13 @@ void rto_push_irq_work_func(struct irq_work *work) }#endif /* HAVE_RT_PUSH_IPI */ -static void pull_rt_task (struct rq *this_rq) +static void pull_rt_rq_task (struct rt_rq *this_rt_rq) { +struct rq *this_rq =rq_of_rt_rq (this_rt_rq);int this_cpu = this_rq->cpu, cpu;bool resched = false;struct task_struct *p, *push_task; +struct rt_rq *src_rt_rq;struct rq *src_rq;int rt_overload_count =rt_overloaded (this_rq);
@@ -2285,6 +2315,7 @@ static void pull_rt_task(struct rq *this_rq)continue ; src_rq =cpu_rq (cpu); + src_rt_rq = &src_rq->rt;/* * Don't bother taking the src_rq->lock if the next highest
@@ -2293,8 +2324,8 @@ static void pull_rt_task(struct rq *this_rq)* logically higher, the src_rq will push this task away. * And if its going logically lower, we do not care */ -if (src_rq->rt.highest_prio.next >= - this_rq->rt.highest_prio.curr) +if (src_rt_rq->highest_prio.next >= + this_rt_rq->highest_prio.curr)continue ;/*
@@ -2309,13 +2340,13 @@ static void pull_rt_task(struct rq *this_rq)* We can pull only a task, which is pushable * on its rq, and no others. */ - p =pick_highest_pushable_task (src_rq, this_cpu); + p =pick_highest_pushable_task (src_rt_rq, this_cpu);/* * Do we have an RT task that preempts * the to-be-scheduled task? */ -if (p && (p->prio < this_rq->rt.highest_prio.curr)) { +if (p && (p->prio < this_rt_rq->highest_prio.curr)) {WARN_ON (p == src_rq->curr);WARN_ON (!task_on_rq_queued (p));
@@ -2374,7 +2405,7 @@ static void task_woken_rt(struct rq *rq, struct task_struct *p) rq->donor->prio <= p->prio);if (need_to_push) -push_rt_tasks (rq); +push_rt_rq_tasks (rt_rq_of_se (&p->rt)); }/* Assumes rq->lock is held */
@@ -2415,7 +2446,7 @@ static void switched_from_rt(struct rq *rq, struct task_struct *p)if (!task_on_rq_queued (p) || rq->rt.rt_nr_running)return ; -rt_queue_pull_task (rq); +rt_queue_pull_task (rt_rq_of_se (&p->rt)); }void __init init_sched_rt_class (void )
@@ -2451,7 +2482,7 @@ static void switched_to_rt(struct rq *rq, struct task_struct *p)*/ if (task_on_rq_queued (p)) {if (p->nr_cpus_allowed >1 && rq->rt.overloaded) -rt_queue_push_tasks (rq); +rt_queue_push_tasks (rt_rq_of_se (&p->rt));if (p->prio < rq->donor->prio &&cpu_online (cpu_of (rq)))resched_curr (rq); }
@@ -2476,7 +2507,7 @@ prio_changed_rt(struct rq *rq, struct task_struct *p, u64 oldprio)* may need to pull tasks to this runqueue. */ if (oldprio < p->prio) -rt_queue_pull_task (rq); +rt_queue_pull_task (rt_rq_of_se (&p->rt));/* * If there's a higher priority task waiting to run
--
2.54.0
RFC PATCH v6 6/25 sched/rt: Move functions from rt.c to sched.h
In reply to Yuri AndriaccioFrom: luca abeni <[email protected]>
Make the following functions/macros be non-static and move them in sched.h, so that they can be also used in other source files:
- rt_entity_is_task()
- rt_task_of()
- rq_of_rt_rq()
- rt_rq_of_se()
- rq_of_rt_se()
There are no functional changes, apart from the use of container_of_const() instead of container_of() where applicable. This is needed by future patches.
--- kernel/sched/rt.c | 56 ------------------------------------------ kernel/sched/sched.h | 58 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 56 deletions(-)
kernel/sched/rt.c +0 -56
@@ -166,36 +166,6 @@ static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)hrtimer_cancel (&rt_b->rt_period_timer); } -#define rt_entity_is_task (rt_se) (!(rt_se)->my_q) - -static inline struct task_struct *rt_task_of (struct sched_rt_entity *rt_se) -{ -WARN_ON_ONCE (!rt_entity_is_task (rt_se)); - - return container_of(rt_se, struct task_struct, rt); -} - -static inline struct rq *rq_of_rt_rq (struct rt_rq *rt_rq) -{ -/* Cannot fold with non-CONFIG_RT_GROUP_SCHED version, layout */ -WARN_ON (!rt_group_sched_enabled () && rt_rq->tg != &root_task_group); -return rt_rq->rq; -} - -static inline struct rt_rq *rt_rq_of_se (struct sched_rt_entity *rt_se) -{ -WARN_ON (!rt_group_sched_enabled () && rt_se->rt_rq->tg != &root_task_group); -return rt_se->rt_rq; -} - -static inline struct rq *rq_of_rt_se (struct sched_rt_entity *rt_se) -{ -struct rt_rq *rt_rq = rt_se->rt_rq; - -WARN_ON (!rt_group_sched_enabled () && rt_rq->tg != &root_task_group); -return rt_rq->rq; -} -void unregister_rt_sched_group (struct task_group *tg) {if (!rt_group_sched_enabled ())
@@ -294,32 +264,6 @@ int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)#else /* !CONFIG_RT_GROUP_SCHED: */ -#define rt_entity_is_task (rt_se) (1) - -static inline struct task_struct *rt_task_of (struct sched_rt_entity *rt_se) -{ - return container_of(rt_se, struct task_struct, rt); -} - -static inline struct rq *rq_of_rt_rq (struct rt_rq *rt_rq) -{ - return container_of(rt_rq, struct rq, rt); -} - -static inline struct rq *rq_of_rt_se (struct sched_rt_entity *rt_se) -{ -struct task_struct *p =rt_task_of (rt_se); - -return task_rq (p); -} - -static inline struct rt_rq *rt_rq_of_se (struct sched_rt_entity *rt_se) -{ -struct rq *rq =rq_of_rt_se (rt_se); - -return &rq->rt; -} -void unregister_rt_sched_group (struct task_group *tg) { }void free_rt_sched_group (struct task_group *tg) { }
kernel/sched/sched.h +58 -0
64 lines not on this page · raw message
Signature · 1 line
2.54.0
RFC PATCH v6 7/25 sched/rt: Disable RT_GROUP_SCHED
In reply to Yuri AndriaccioDisable the old RT_GROUP_SCHED scheduler. Note that this does not completely remove all the RT_GROUP_SCHED functionality, just unhooks it and removes most of the relevant functions. Some of the RT_GROUP_SCHED functions are kept because they will be adapted for the HCBS scheduling.
Most notably:
- Disable the initialization of the rt_bandwidth for group scheduling.
- Unhook any functionality for RT_GROUP_SCHED in normal rt.c code, leaving only non-group functionality.
- Remove group related field initialization in init_rt_rq().
- Remove all the unhooked (and so unused) functions from RT_GROUP_SCHED.
- Remove all allocation/deallocation code for rt-groups, always returning failure on allocation.
- Update inc/dec_rt_tasks active tasks' counters, as rt scheduling entities now only represent a single task, and not a group of tasks anymore.
- Remove unused rq_of_rt_se function.
--- kernel/sched/core.c | 6 - kernel/sched/deadline.c | 34 -- kernel/sched/debug.c | 6 - kernel/sched/rt.c | 852 ++-------------------------------------- kernel/sched/sched.h | 32 +- kernel/sched/syscalls.c | 13 - 6 files changed, 28 insertions(+), 915 deletions(-)
kernel/sched/core.c +0 -6
@@ -8922,11 +8922,6 @@ void __init sched_init(void)init_defrootdomain (); -#ifdef CONFIG_RT_GROUP_SCHED -init_rt_bandwidth (&root_task_group.rt_bandwidth, -global_rt_period (),global_rt_runtime ()); -#endif /* CONFIG_RT_GROUP_SCHED */ -#ifdef CONFIG_CGROUP_SCHED task_group_cache =KMEM_CACHE (task_group,0 );
@@ -8978,7 +8973,6 @@ void __init sched_init(void)* starts working after scheduler_running, which is not the case * yet. */ - rq->rt.rt_runtime =global_rt_runtime ();init_tg_rt_entry (&root_task_group, &rq->rt,NULL , i,NULL );#endif rq->next_class = &idle_sched_class;
kernel/sched/deadline.c +0 -34
@@ -1533,40 +1533,6 @@ static void update_curr_dl_se(struct rq *rq, struct sched_dl_entity *dl_se, s64 }else {trace_sched_dl_update_tp (dl_se,cpu_of (rq),dl_get_type (dl_se, rq)); } - -/* -* The dl_server does not account for real-time workload because it -* is running fair work. -*/ -if (dl_se->dl_server) -return ; - -#ifdef CONFIG_RT_GROUP_SCHED -/* -* Because -- for now -- we share the rt bandwidth, we need to -* account our runtime there too, otherwise actual rt tasks -* would be able to exceed the shared quota. -* -* Account to the root rt group for now. -* -* The solution we're working towards is having the RT groups scheduled -* using deadline servers -- however there's a few nasties to figure -* out before that can happen. -*/ -if (rt_bandwidth_enabled ()) { -struct rt_rq *rt_rq = &rq->rt; - -raw_spin_lock (&rt_rq->rt_runtime_lock); -/* -* We'll let actual RT tasks worry about the overflow here, we -* have our own CBS to keep us inline; only account when RT -* bandwidth is relevant. -*/ -if (sched_rt_bandwidth_account (rt_rq)) - rt_rq->rt_time += delta_exec; -raw_spin_unlock (&rt_rq->rt_runtime_lock); - } -#endif /* CONFIG_RT_GROUP_SCHED */ }/*
kernel/sched/debug.c +0 -6
@@ -1009,12 +1009,6 @@ void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq)PU (rt_nr_running); -#ifdef CONFIG_RT_GROUP_SCHED -P (rt_throttled); -PN (rt_time); -PN (rt_runtime); -#endif -#undef PN#undef PU#undef P
kernel/sched/rt.c +24 -828
1,050 lines not on this page · raw message
kernel/sched/sched.h +4 -28
@@ -827,7 +827,7 @@ struct scx_rq {static inline int rt_bandwidth_enabled (void ) { -return sysctl_sched_rt_runtime >=0 ; +return 0 ; }/* RT IPI pull logic requires IRQ_WORK */
@@ -867,7 +867,7 @@ struct rt_rq {static inline bool rt_rq_is_runnable (struct rt_rq *rt_rq) { -return rt_rq->rt_queued && rt_rq->rt_nr_running; +return rt_rq->rt_nr_running; }/* Deadline class' related fields in a runqueue */
@@ -2794,7 +2794,7 @@ static inline bool sched_dl_runnable(struct rq *rq)static inline bool sched_rt_runnable (struct rq *rq) { -return rq->rt.rt_queued >0 ; +return rq->rt.rt_nr_running >0 ; }static inline bool sched_fair_runnable (struct rq *rq)
@@ -2906,9 +2906,6 @@ extern void resched_curr(struct rq *rq);extern void resched_curr_lazy (struct rq *rq);extern void resched_cpu (int cpu); -extern void init_rt_bandwidth (struct rt_bandwidth *rt_b,u64 period,u64 runtime); -extern bool sched_rt_bandwidth_account (struct rt_rq *rt_rq); -extern void init_dl_entity (struct sched_dl_entity *dl_se);extern void init_cfs_throttle_work (struct task_struct *p);
@@ -3333,12 +3330,8 @@ extern void set_rq_offline(struct rq *rq);extern bool sched_smp_initialized;#ifdef CONFIG_RT_GROUP_SCHED -#define rt_entity_is_task (rt_se) (!(rt_se)->my_q) -static inline struct task_struct *rt_task_of (struct sched_rt_entity *rt_se) { -WARN_ON_ONCE (!rt_entity_is_task (rt_se)); - return container_of_const(rt_se, struct task_struct, rt); }
@@ -3354,17 +3347,7 @@ static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)WARN_ON (!rt_group_sched_enabled () && rt_se->rt_rq->tg != &root_task_group);return rt_se->rt_rq; } - -static inline struct rq *rq_of_rt_se (struct sched_rt_entity *rt_se) -{ -struct rt_rq *rt_rq = rt_se->rt_rq; - -WARN_ON (!rt_group_sched_enabled () && rt_rq->tg != &root_task_group); -return rt_rq->rq; -}#else -#define rt_entity_is_task (rt_se) (1) -static inline struct task_struct *rt_task_of (struct sched_rt_entity *rt_se) { return container_of_const(rt_se, struct task_struct, rt);
@@ -3375,16 +3358,9 @@ static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) return container_of_const(rt_rq, struct rq, rt); } -static inline struct rq *rq_of_rt_se (struct sched_rt_entity *rt_se) -{ -struct task_struct *p =rt_task_of (rt_se); - -return task_rq (p); -} -static inline struct rt_rq *rt_rq_of_se (struct sched_rt_entity *rt_se) { -struct rq *rq =rq_of_rt_se (rt_se); +struct rq *rq =task_rq (rt_task_of (rt_se));return &rq->rt; }
kernel/sched/syscalls.c +0 -13
@@ -606,19 +606,6 @@ int __sched_setscheduler(struct task_struct *p, change:if (user) { -#ifdef CONFIG_RT_GROUP_SCHED -/* -* Do not allow real-time tasks into groups that have no runtime -* assigned. -*/ -if (rt_group_sched_enabled () && -rt_bandwidth_enabled () &&rt_policy (policy) && -task_group (p)->rt_bandwidth.rt_runtime ==0 && - !task_group_is_autogroup (task_group (p))) { - retval = -EPERM ; - goto unlock; - } -#endif /* CONFIG_RT_GROUP_SCHED */ if (dl_bandwidth_enabled () &&dl_policy (policy) && !(attr->sched_flags &SCHED_FLAG_SUGOV )) {cpumask_t *span = rq->rd->span;
Signature · 1 line
2.54.0
RFC PATCH v6 8/25 sched/rt: Remove unnecessary runqueue pointer in struct rt_rq
In reply to Yuri AndriaccioRemove the rq field in struct rt_rq.
The rq field now is just caching the pointer to the global runqueue of the given rt_rq, so it is unnecessary as the global runqueue can be retrieved in other ways.
Introduce global_rq_of_rt_rq to retrieve the global runqueue which serves a rt_rq's dl_server.
Rework rq_of_rt_rq to retrieve the runqueue a rt_rq is serving.
--- kernel/sched/rt.c | 21 +++++++++------------ kernel/sched/sched.h | 16 ++++++++++++---- 2 files changed, 21 insertions(+), 16 deletions(-)
kernel/sched/rt.c +9 -12
@@ -101,10 +101,7 @@ void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int cpu, struct sched_rt_entity *parent) { - struct rq *rq = cpu_rq(cpu); - rt_rq->highest_prio.curr = MAX_RT_PRIO-1; - rt_rq->rq = rq; rt_rq->tg = tg; tg->rt_rq[cpu] = rt_rq;
@@ -184,7 +181,7 @@ static void pull_rt_task(struct rq *);static inline void rt_queue_push_tasks (struct rt_rq *rt_rq) { - struct rq *rq = container_of_const(rt_rq, struct rq, rt); +struct rq *rq =global_rq_of_rt_rq (rt_rq);if (!has_pushable_tasks (rt_rq))return ;
@@ -194,7 +191,7 @@ static inline void rt_queue_push_tasks(struct rt_rq *rt_rq)static inline void rt_queue_pull_task (struct rt_rq *rt_rq) { - struct rq *rq = container_of_const(rt_rq, struct rq, rt); +struct rq *rq =global_rq_of_rt_rq (rt_rq);queue_balance_callback (rq, &per_cpu (rt_pull_head, rq->cpu), pull_rt_task); }
@@ -222,7 +219,7 @@ static void enqueue_pushable_task(struct rt_rq *rt_rq, struct task_struct *p) rt_rq->highest_prio.next = p->prio;if (!rt_rq->overloaded) { -rt_set_overload (rq_of_rt_rq (rt_rq)); +rt_set_overload (global_rq_of_rt_rq (rt_rq)); rt_rq->overloaded =1 ; } }
@@ -240,7 +237,7 @@ static void dequeue_pushable_task(struct rt_rq *rt_rq, struct task_struct *p) rt_rq->highest_prio.next =MAX_RT_PRIO -1 ;if (rt_rq->overloaded) { -rt_clear_overload (rq_of_rt_rq (rt_rq)); +rt_clear_overload (global_rq_of_rt_rq (rt_rq)); rt_rq->overloaded =0 ; } }
@@ -495,7 +492,7 @@ update_stats_wait_start_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)if (!stats)return ; -__update_stats_wait_start (rq_of_rt_rq (rt_rq), p, stats); +__update_stats_wait_start (global_rq_of_rt_rq (rt_rq), p, stats); } static inline void
@@ -512,7 +509,7 @@ update_stats_enqueue_sleeper_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_if (!stats)return ; -__update_stats_enqueue_sleeper (rq_of_rt_rq (rt_rq), p, stats); +__update_stats_enqueue_sleeper (global_rq_of_rt_rq (rt_rq), p, stats); } static inline void
@@ -540,7 +537,7 @@ update_stats_wait_end_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)if (!stats)return ; -__update_stats_wait_end (rq_of_rt_rq (rt_rq), p, stats); +__update_stats_wait_end (global_rq_of_rt_rq (rt_rq), p, stats); } static inline void
@@ -564,11 +561,11 @@ update_stats_dequeue_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, state =READ_ONCE (p->__state);if (state &TASK_INTERRUPTIBLE )__schedstat_set (p->stats.sleep_start, -rq_clock (rq_of_rt_rq (rt_rq))); +rq_clock (global_rq_of_rt_rq (rt_rq)));if (state &TASK_UNINTERRUPTIBLE )__schedstat_set (p->stats.block_start, -rq_clock (rq_of_rt_rq (rt_rq))); +rq_clock (global_rq_of_rt_rq (rt_rq))); } }
kernel/sched/sched.h +12 -4
@@ -857,8 +857,6 @@ struct rt_rq {raw_spinlock_t rt_runtime_lock;unsigned int rt_nr_boosted; - -struct rq *rq;/* this is always top-level rq, cache? */ #endif #ifdef CONFIG_CGROUP_SCHED struct task_group *tg;/* this tg has "this" rt_rq on given CPU for runnable entities */
@@ -3337,9 +3335,14 @@ static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)static inline struct rq *rq_of_rt_rq (struct rt_rq *rt_rq) { -/* Cannot fold with non-CONFIG_RT_GROUP_SCHED version, layout */ WARN_ON (!rt_group_sched_enabled () && rt_rq->tg != &root_task_group); -return rt_rq->rq; + return container_of_const(rt_rq, struct rq, rt); +} + +static inline struct rq *global_rq_of_rt_rq (struct rt_rq *rt_rq) +{ +/* Cannot fold with non-CONFIG_RT_GROUP_SCHED version, layout */ +return cpu_rq (rq_of_rt_rq (rt_rq)->cpu); }static inline struct rt_rq *rt_rq_of_se (struct sched_rt_entity *rt_se)
@@ -3358,6 +3361,11 @@ static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) return container_of_const(rt_rq, struct rq, rt); } +static inline struct rq *global_rq_of_rt_rq (struct rt_rq *rt_rq) +{ + return container_of_const(rt_rq, struct rq, rt); +} +static inline struct rt_rq *rt_rq_of_se (struct sched_rt_entity *rt_se) {struct rq *rq =task_rq (rt_task_of (rt_se));
--
2.54.0
RFC PATCH v6 9/25 sched/rt: Introduce HCBS specific structs in task_group
In reply to Yuri AndriaccioFrom: luca abeni <[email protected]>
Add an array of sched_dl_entity objects in task_group.
Create the dl_bandwidth struct and add a field for it in task_group.
Add a rq pointer field in struct rt_rq.
---
For each CPU on the host system, the task_group manages a sched_dl_entity and a rt_rq object, which in turn keeps a pointer to its locally managed runqueue. The sched_dl_entity object manages the deadline server which will be scheduled for execution on the CPU, while the rt_rq object is instead used to reference the local runqueue's specific data and entities and it is used when an actual task must be scheduled when the CPU is given to the dl_server.
The dl_bandwidth object keeps track of the currently allocated bandwidth for the cgroup and the currently active context. RT-cgroups can either run tasks themselves or can delegate the scheduling of their tasks to their parent, the active_context field keeps track of which cgroup is serving the tasks.
--- kernel/sched/sched.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
kernel/sched/sched.h +22 -0
@@ -322,6 +322,15 @@ struct rt_bandwidth {unsigned int rt_period_active; }; +struct dl_bandwidth { +raw_spinlock_t dl_runtime_lock; +u64 dl_runtime; +u64 dl_internal_runtime; +u64 dl_period; +struct task_group *active_context; +}; + +static inline int dl_bandwidth_enabled (void ) {return sysctl_sched_rt_runtime >=0 ;
@@ -495,10 +504,17 @@ struct task_group {#endif /* CONFIG_FAIR_GROUP_SCHED */ #ifdef CONFIG_RT_GROUP_SCHED +/* +* Each task group manages a different scheduling entity per CPU, i.e. a +* different deadline server, and a runqueue per CPU. All the dl-servers +* share the same dl_bandwidth object. +*/ struct sched_rt_entity **rt_se; +struct sched_dl_entity **dl_se;struct rt_rq **rt_rq;struct rt_bandwidth rt_bandwidth; +struct dl_bandwidth dl_bandwidth;#endif struct scx_task_group scx;
@@ -861,6 +877,12 @@ struct rt_rq {#ifdef CONFIG_CGROUP_SCHED struct task_group *tg;/* this tg has "this" rt_rq on given CPU for runnable entities */ #endif + +/* +* The cgroup's served runqueue if the rt_rq entity belongs to a cgroup, +* otherwise the top-level global runqueue. +*/ +struct rq *rq; };static inline bool rt_rq_is_runnable (struct rt_rq *rt_rq)
--
2.54.0
RFC PATCH v6 10/25 sched/core: Initialize HCBS specific structures.
In reply to Yuri AndriaccioFrom: luca abeni <[email protected]>
Update autogroups' creation/destruction to use the new data structures.
Initialize the default bandwidth for rt-cgroups (sched_init).
Initialize rt-scheduler's specific data structures for the root control group (sched_init).
Remove init_tg_rt_entry in favour of manual setup of the necessary data structures in sched_init.
Add utility functions to check (and get) if a rt_rq entity is connected to a rt-cgroup.
Add read/write accessors for dl_bandwidth.
Add dl_bw_lock_of_tg macro to reference the a task group dl_bandwidth's spinlock.
--- kernel/sched/autogroup.c | 4 ++-- kernel/sched/core.c | 11 ++++++++-- kernel/sched/deadline.c | 11 ++++++++++ kernel/sched/rt.c | 45 ++++++++++++++++++++++++++++------------ kernel/sched/sched.h | 38 ++++++++++++++++++++++++++++++--- 5 files changed, 89 insertions(+), 20 deletions(-)
kernel/sched/autogroup.c +2 -2
@@ -52,7 +52,7 @@ static inline void autogroup_destroy(struct kref *kref)#ifdef CONFIG_RT_GROUP_SCHED /* We've redirected RT tasks to the root task group... */ - ag->tg->rt_se =NULL ; + ag->tg->dl_se =NULL ; ag->tg->rt_rq =NULL ;#endif sched_release_group (ag->tg);
@@ -109,7 +109,7 @@ static inline struct autogroup *autogroup_create(void)* the policy change to proceed. */ free_rt_sched_group (tg); - tg->rt_se = root_task_group.rt_se; + tg->dl_se = root_task_group.dl_se; tg->rt_rq = root_task_group.rt_rq;#endif /* CONFIG_RT_GROUP_SCHED */ tg->autogroup = ag;
kernel/sched/core.c +9 -2
@@ -8911,7 +8911,7 @@ void __init sched_init(void)scx_tg_init (&root_task_group);#endif /* CONFIG_EXT_GROUP_SCHED */ #ifdef CONFIG_RT_GROUP_SCHED - root_task_group.rt_se = (struct sched_rt_entity **)ptr; + root_task_group.dl_se = (struct sched_dl_entity **)ptr; ptr += nr_cpu_ids *sizeof (void **); root_task_group.rt_rq = (struct rt_rq **)ptr;
@@ -8922,6 +8922,11 @@ void __init sched_init(void)init_defrootdomain (); +#ifdef CONFIG_RT_GROUP_SCHED +init_dl_bandwidth (&root_task_group.dl_bandwidth, +global_rt_period (),0 , &root_task_group); +#endif /* CONFIG_RT_GROUP_SCHED */ +#ifdef CONFIG_CGROUP_SCHED task_group_cache =KMEM_CACHE (task_group,0 );
@@ -8973,7 +8978,9 @@ void __init sched_init(void)* starts working after scheduler_running, which is not the case * yet. */ -init_tg_rt_entry (&root_task_group, &rq->rt,NULL , i,NULL ); + rq->rt.tg = &root_task_group; + root_task_group.rt_rq[i] = &rq->rt; + root_task_group.dl_se[i] =NULL ;#endif rq->next_class = &idle_sched_class;
kernel/sched/deadline.c +11 -0
@@ -508,6 +508,17 @@ static inline int is_leftmost(struct sched_dl_entity *dl_se, struct dl_rq *dl_rqstatic void init_dl_rq_bw_ratio (struct dl_rq *dl_rq); +void init_dl_bandwidth (struct dl_bandwidth *dl_b,u64 period,u64 runtime, +struct task_group *active_context) +{ +raw_spin_lock_init (&dl_b->dl_runtime_lock); + dl_b->dl_period = period; + dl_b->dl_runtime = runtime; + dl_b->dl_internal_runtime =0 ; + dl_b->active_context = active_context; +} + +void init_dl_bw (struct dl_bw *dl_b) {raw_spin_lock_init (&dl_b->lock);
kernel/sched/rt.c +32 -13
66 lines not on this page · raw message
kernel/sched/sched.h +35 -3
@@ -606,9 +606,6 @@ extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);extern void unthrottle_cfs_rq (struct cfs_rq *cfs_rq);extern bool cfs_task_bw_constrained (struct task_struct *p); -extern void init_tg_rt_entry (struct task_group *tg,struct rt_rq *rt_rq, -struct sched_rt_entity *rt_se,int cpu, -struct sched_rt_entity *parent);extern int sched_group_set_rt_runtime (struct task_group *tg,long rt_runtime_us);extern int sched_group_set_rt_period (struct task_group *tg,u64 rt_period_us);extern long sched_group_rt_runtime (struct task_group *tg);
@@ -2926,6 +2923,8 @@ extern void resched_curr(struct rq *rq);extern void resched_curr_lazy (struct rq *rq);extern void resched_cpu (int cpu); +extern void init_dl_bandwidth (struct dl_bandwidth *dl_b,u64 period,u64 runtime, +struct task_group *active_context);extern void init_dl_entity (struct sched_dl_entity *dl_se);extern void init_cfs_throttle_work (struct task_struct *p);
@@ -3349,6 +3348,9 @@ extern void set_rq_offline(struct rq *rq);extern bool sched_smp_initialized; +extern const struct dl_bandwidth *dl_bandwidth_read (struct task_group *tg); +extern struct dl_bandwidth *dl_bandwidth_write (struct task_group *tg); +#ifdef CONFIG_RT_GROUP_SCHED static inline struct task_struct *rt_task_of (struct sched_rt_entity *rt_se) {
@@ -3372,6 +3374,24 @@ static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)WARN_ON (!rt_group_sched_enabled () && rt_se->rt_rq->tg != &root_task_group); return rt_se->rt_rq; } + +static inline int is_dl_group (struct rt_rq *rt_rq) +{ +return rt_rq->tg != &root_task_group; +} + +/* +* Return the scheduling entity of this group of tasks. +*/ +static inline struct sched_dl_entity *dl_group_of (struct rt_rq *rt_rq) +{ +if (WARN_ON_ONCE (!is_dl_group (rt_rq))) +return NULL ; + +return rt_rq->tg->dl_se[rq_of_rt_rq (rt_rq)->cpu]; +} + +#define dl_bw_lock_of_tg (tg) (&(tg)->dl_bandwidth.dl_runtime_lock)#else static inline struct task_struct *rt_task_of (struct sched_rt_entity *rt_se) {
@@ -3394,6 +3414,18 @@ static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)return &rq->rt; } + +static inline int is_dl_group (struct rt_rq *rt_rq) +{ +return 0 ; +} + +static inline struct sched_dl_entity *dl_group_of (struct rt_rq *rt_rq) +{ +return NULL ; +} + +#define dl_bw_lock_of_tg (tg) ((raw_spinlock_t*)NULL)#endif DEFINE_LOCK_GUARD_2(double_rq_lock, struct rq,
Signature · 1 line
2.54.0
RFC PATCH v6 11/25 sched/deadline: Add dl_init_tg
In reply to Yuri AndriaccioFrom: luca abeni <[email protected]>
Add dl_init_tg to initialize and/or update a rt-cgroup dl_server and to also account the allocated bandwidth. This function is currently unhooked and will be later used to allocate bandwidth to rt-cgroups.
Add lock guard for raw_spin_rq_lock_irq for cleaner code.
--- kernel/sched/deadline.c | 31 +++++++++++++++++++++++++++++++ kernel/sched/sched.h | 5 +++++ 2 files changed, 36 insertions(+)
kernel/sched/deadline.c +31 -0
@@ -335,6 +335,37 @@ void cancel_inactive_timer(struct sched_dl_entity *dl_se)cancel_dl_timer (dl_se, &dl_se->inactive_timer); } +#ifdef CONFIG_RT_GROUP_SCHED +void dl_init_tg (struct sched_dl_entity *dl_se,u64 rt_runtime,u64 rt_period) +{ + struct rq *rq = container_of_const(dl_se->dl_rq, struct rq, dl); +int is_active; +u64 new_bw; + +guard (raw_spin_rq_lock_irq)(rq); + is_active = dl_se->my_q->rt.rt_nr_running >0 ; + +update_rq_clock (rq); +dl_server_stop (dl_se); + + new_bw =to_ratio (rt_period, rt_runtime); +dl_rq_change_utilization (rq, dl_se, new_bw); + + dl_se->dl_runtime = rt_runtime; + dl_se->dl_deadline = rt_period; + dl_se->dl_period = rt_period; + + dl_se->runtime =0 ; + dl_se->deadline =0 ; + + dl_se->dl_bw = new_bw; + dl_se->dl_density = new_bw; + +if (is_active) +dl_server_start (dl_se); +} +#endif +static void dl_change_utilization (struct task_struct *p,u64 new_bw) {WARN_ON_ONCE (p->dl.flags &SCHED_FLAG_SUGOV );
kernel/sched/sched.h +5 -0
@@ -425,6 +425,7 @@ extern void dl_server_init(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq,struct rq *served_rq, dl_server_pick_f pick_task);extern void sched_init_dl_servers (void ); +extern void dl_init_tg (struct sched_dl_entity *dl_se,u64 rt_runtime,u64 rt_period);extern void fair_server_init (struct rq *rq);extern void ext_server_init (struct rq *rq);
@@ -2044,6 +2045,10 @@ static inline struct rq *_this_rq_lock_irq(struct rq_flags *rf) __acquires_retreturn rq; } +DEFINE_LOCK_GUARD_1(raw_spin_rq_lock_irq, struct rq, +raw_spin_rq_lock_irq (_T->lock), + raw_spin_rq_unlock_irq(_T->lock)) +#ifdef CONFIG_NUMA enum numa_topology_type {
Signature · 1 line
2.54.0
RFC PATCH v6 12/25 sched/rt: Add {alloc/unregister/free}_rt_sched_group
In reply to Yuri AndriaccioAdd allocation and deallocation code for rt-cgroups.
Declare dl_server specific functions (only skeleton, but no implementation yet), needed by the deadline servers to be called when trying to schedule.
Initialize a cgroup's active context to that of its parent.
--- kernel/sched/rt.c | 156 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 154 insertions(+), 2 deletions(-)
kernel/sched/rt.c +154 -2
@@ -120,24 +120,176 @@ struct dl_bandwidth *dl_bandwidth_write(struct task_group *tg)void unregister_rt_sched_group (struct task_group *tg) { +int i; + +if (!rt_group_sched_enabled ()) +return ; + +if (!tg->dl_se || !tg->rt_rq) +return ; +for_each_possible_cpu (i) { +if (!tg->dl_se[i] || !tg->rt_rq[i]) +continue ; + +if (tg->dl_se[i]->dl_runtime) +dl_init_tg (tg->dl_se[i],0 , tg->dl_se[i]->dl_period); + } }void free_rt_sched_group (struct task_group *tg) { +int i; +unsigned long flags; +if (!rt_group_sched_enabled ())return ; + +if (!tg->dl_se || !tg->rt_rq) +return ; + +for_each_possible_cpu (i) { +if (!tg->dl_se[i] || !tg->rt_rq[i]) +continue ; + +/* +* Shutdown the dl_server and free it +* +* Since the dl timer is going to be cancelled, +* we risk to never decrease the running bw... +* Fix this issue by changing the group runtime +* to 0 immediately before freeing it. +*/ +if (tg->dl_se[i]->dl_runtime) +dl_init_tg (tg->dl_se[i],0 , tg->dl_se[i]->dl_period); + +raw_spin_rq_lock_irqsave (cpu_rq (i), flags); +hrtimer_cancel (&tg->dl_se[i]->dl_timer); +raw_spin_rq_unlock_irqrestore (cpu_rq (i), flags); +kfree (tg->dl_se[i]); + +/* Free the local per-cpu runqueue */ +kfree (rq_of_rt_rq (tg->rt_rq[i])); + } + +kfree (tg->rt_rq); +kfree (tg->dl_se); } +static inline void __rt_rq_free (struct rt_rq **rt_rq) +{ +int i; + +for_each_possible_cpu (i) { +kfree (rq_of_rt_rq (rt_rq[i])); + } + +kfree (rt_rq); +} + +DEFINE_FREE(rt_rq_free, struct rt_rq **, if (_T) __rt_rq_free(_T)) + +static inline void __dl_se_free (struct sched_dl_entity **dl_se) +{ +int i; + +for_each_possible_cpu (i) { +kfree (dl_se[i]); + } + +kfree (dl_se); +} + +DEFINE_FREE(dl_se_free, struct sched_dl_entity **, if (_T) __dl_se_free(_T)) + +static int __alloc_rt_sched_group_data (struct task_group *tg) { +/* Instantiate automatic cleanup in event of kalloc fail */ + struct rt_rq **tg_rt_rq __free(rt_rq_free) = NULL; + struct sched_dl_entity **tg_dl_se __free(dl_se_free) = NULL; + struct sched_dl_entity *dl_se __free(kfree) = NULL; + struct rq *s_rq __free(kfree) = NULL; +int i; + + tg_rt_rq =kcalloc (nr_cpu_ids,sizeof (struct rt_rq *),GFP_KERNEL ); +if (!tg_rt_rq) +return 0 ; + + tg_dl_se =kcalloc (nr_cpu_ids, +sizeof (struct sched_dl_entity *),GFP_KERNEL ); +if (!tg_dl_se) +return 0 ; + +for_each_possible_cpu (i) { + s_rq =kzalloc_node (sizeof (struct rq ), +GFP_KERNEL ,cpu_to_node (i)); +if (!s_rq) +return 0 ; + + dl_se =kzalloc_node (sizeof (struct sched_dl_entity ), +GFP_KERNEL ,cpu_to_node (i)); +if (!dl_se) +return 0 ; + + tg_rt_rq[i] = &no_free_ptr (s_rq)->rt; + tg_dl_se[i] =no_free_ptr (dl_se); + } + + tg->rt_rq =no_free_ptr (tg_rt_rq); + tg->dl_se =no_free_ptr (tg_dl_se); + +return 1 ; +} + +static struct task_struct *rt_server_pick (struct sched_dl_entity *dl_se,struct rq_flags *rf); +int alloc_rt_sched_group (struct task_group *tg,struct task_group *parent) { +struct sched_dl_entity *dl_se; +struct rq *s_rq; +int i; +if (!rt_group_sched_enabled ())return 1 ; +/* Allocate all necessary resources beforehand */ +if (!__alloc_rt_sched_group_data (tg)) +return 0 ; + +/* Initialize the allocated resources now. */ + scoped_guard(raw_spinlock_irq, dl_bw_lock_of_tg(parent)) { +init_dl_bandwidth (&tg->dl_bandwidth,0 ,RUNTIME_INF , +dl_bandwidth_read (parent)->active_context); + } + +for_each_possible_cpu (i) { + s_rq =rq_of_rt_rq (tg->rt_rq[i]); + dl_se = tg->dl_se[i]; + +init_rt_rq (&s_rq->rt); + s_rq->cpu = i; + s_rq->rt.tg = tg; + +init_dl_entity (dl_se); + dl_se->dl_runtime =0 ; + dl_se->dl_deadline =0 ; + dl_se->dl_period =0 ; + dl_se->runtime =0 ; + dl_se->deadline =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); + dl_se->dl_server =1 ; +dl_server_init (dl_se, &cpu_rq (i)->dl, s_rq, rt_server_pick); + } +return 1 ; } -#else /* !CONFIG_RT_GROUP_SCHED: */ +static struct task_struct *rt_server_pick (struct sched_dl_entity *dl_se,struct rq_flags *rf) +{ +return NULL ; +} + +#else /* !CONFIG_RT_GROUP_SCHED */ void unregister_rt_sched_group (struct task_group *tg) { }
@@ -147,7 +299,7 @@ int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) {return 1 ; } -#endif /* !CONFIG_RT_GROUP_SCHED */ +#endif /* CONFIG_RT_GROUP_SCHED */ static inline bool need_pull_rt_task (struct rq *rq,struct task_struct *prev) {
--
2.54.0
RFC PATCH v6 13/25 sched/deadline: Account rt-cgroups bandwidth in deadline tasks schedulability tests.
In reply to Yuri AndriaccioFrom: luca abeni <[email protected]>
Account the rt-cgroups hierarchy's reserved bandwidth in the schedulability test of deadline entities. This mechanism allows to completely reserve portion of the rt-bandwidth to rt-cgroups even if they do not use all of it.
Account for the rt-cgroups' reserved bandwidth also when changing the total dedicated bandwidth for real time tasks.
--- kernel/sched/deadline.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
kernel/sched/deadline.c +17 -3
@@ -205,11 +205,22 @@ void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus)__dl_update (dl_b, -((s32 )tsk_bw / cpus)); } +static inline u64 get_dl_groups_bw (void ) +{ +#ifdef CONFIG_RT_GROUP_SCHED +return to_ratio (root_task_group.dl_bandwidth.dl_period, + root_task_group.dl_bandwidth.dl_runtime); +#else +return 0 ; +#endif +} +static inline bool __dl_overflow (struct dl_bw *dl_b,unsigned long cap,u64 old_bw,u64 new_bw) {return dl_b->bw !=-1 && -cap_scale (dl_b->bw, cap) < dl_b->total_bw - old_bw + new_bw; +cap_scale (dl_b->bw, cap) < dl_b->total_bw - old_bw + new_bw + +cap_scale (get_dl_groups_bw (), cap); } static inline
@@ -3490,8 +3501,9 @@ int sched_dl_global_validate(void)u64 period =global_rt_period ();u64 new_bw =to_ratio (period, runtime);u64 cookie = ++dl_cookie; +u64 dl_groups_root =get_dl_groups_bw ();struct dl_bw *dl_b; -int cpu, cpus, ret =0 ; +int cpu, cap, cpus, ret =0 ;unsigned long flags;/*
@@ -3506,10 +3518,12 @@ int sched_dl_global_validate(void) goto next; dl_b =dl_bw_of (cpu); + cap =dl_bw_capacity (cpu); cpus =dl_bw_cpus (cpu);raw_spin_lock_irqsave (&dl_b->lock, flags); -if (new_bw * cpus < dl_b->total_bw) +if (new_bw * cpus < dl_b->total_bw + +cap_scale (dl_groups_root, cap)) ret = -EBUSY ;raw_spin_unlock_irqrestore (&dl_b->lock, flags);
--
2.54.0
RFC PATCH v6 14/25 sched/rt: Implement dl-server operations for rt-cgroups.
In reply to Yuri AndriaccioImplement rt_server_pick, the callback that deadline servers use to pick a task to schedule.
rt_server_pick(): pick the next runnable rt task and tell the scheduler that it is going to be scheduled next.
Let enqueue_task_rt function start the attached deadline server when the first task is enqueued on a specific rq/server.
The server is not symmetrically stopped in dequeue_task_rt as it is stopped when server_pick_task returns NULL (see deadline.c).
Change update_curr_rt to perform a deadline server update if the updated task is served by non-root group.
Update {enqueue/dequeue}_pushable_task and rt_{set/clear}_overload to only set the CPU-wise overload flag only if the root runqueues are overloaded, but not for HCBS runqueues.
Update inc/dec_dl_tasks to account the number of active tasks in the local runqueue for rt-cgroups servers, as their local runqueue is different from the global runqueue, and thus when a rt-group server is activated/deactivated, the number of served tasks' must be added/removed. This uses nr_running to be compatible with future dl-server interfaces. Account also the deadline server so that it is picked for shutdown when its runqueue is empty (future patches will try to pull tasks before stopping).
Update inc/dec_rt_prio_smp to change a rq's cpupri only if the rt_rq is the global runqueue, since cgroups are scheduled via their dl-server priority.
Update inc/dec_rt_tasks to account for waking/sleeping tasks on the global runqueue, when the task runs on the root cgroup, or its local dl server is active. The accounting is not done when servers are throttled, as they will add/sub the number of tasks running when they get enqueued/dequeued. For rt cgroups, account for the number of active tasks in the nr_running field of the local runqueue (add/sub_nr_running), as this number is used when a dl server is enqueued/dequeued.
Update set_task_rq to record the rt_rq of the cgroup's active_context, tracking where to schedule the given task.
Update set_task_rq to record the dl_rq, tracking which deadline server manages a task.
Update set_task_rq to not use the parent field anymore, as it is unused by this patchset's code. Remove the unused parent field from sched_rt_entity.
--- include/linux/sched.h | 1 - kernel/sched/deadline.c | 8 +++++ kernel/sched/rt.c | 70 ++++++++++++++++++++++++++++++++++++----- kernel/sched/sched.h | 11 +++++-- 4 files changed, 79 insertions(+), 11 deletions(-)
include/linux/sched.h +0 -1
@@ -630,7 +630,6 @@ struct sched_rt_entity {struct sched_rt_entity *back;#ifdef CONFIG_RT_GROUP_SCHED -struct sched_rt_entity *parent;/* rq on which this entity is (to be) queued: */ struct rt_rq *rt_rq;/* rq "owned" by this entity/group: */
kernel/sched/deadline.c +8 -0
@@ -2096,6 +2096,10 @@ void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)if (!dl_server (dl_se))add_nr_running (rq_of_dl_rq (dl_rq),1 ); +else if (rq_of_dl_se (dl_se) != dl_se->my_q) { +WARN_ON (dl_se->my_q->rt.rt_nr_running != dl_se->my_q->nr_running); +add_nr_running (rq_of_dl_rq (dl_rq), dl_se->my_q->nr_running +1 ); + }inc_dl_deadline (dl_rq, deadline); }
@@ -2108,6 +2112,10 @@ void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)if (!dl_server (dl_se))sub_nr_running (rq_of_dl_rq (dl_rq),1 ); +else if (rq_of_dl_se (dl_se) != dl_se->my_q) { +WARN_ON (dl_se->my_q->rt.rt_nr_running != dl_se->my_q->nr_running); +sub_nr_running (rq_of_dl_rq (dl_rq), dl_se->my_q->nr_running -1 ); + }dec_dl_deadline (dl_rq, dl_se->deadline); }
kernel/sched/rt.c +62 -8
156 lines not on this page · raw message
kernel/sched/sched.h +9 -2
@@ -2310,10 +2310,11 @@ static inline void set_task_rq(struct task_struct *p, unsigned int cpu)* root_task_group's rt_rq than switching in rt_rq_of_se() * Clobbers tg(!) */ +guard (raw_spinlock_irqsave)(&tg->dl_bandwidth.dl_runtime_lock);if (!rt_group_sched_enabled ()) tg = &root_task_group; - p->rt.rt_rq = tg->rt_rq[cpu]; - p->rt.parent = tg->rt_se[cpu]; + p->rt.rt_rq = tg->dl_bandwidth.active_context->rt_rq[cpu]; + p->dl.dl_rq = &cpu_rq (cpu)->dl;#endif /* CONFIG_RT_GROUP_SCHED */ }
@@ -2976,6 +2977,9 @@ static inline void add_nr_running(struct rq *rq, unsigned count)unsigned prev_nr = rq->nr_running; rq->nr_running = prev_nr + count; +if (rq !=cpu_rq (rq->cpu)) +return ; +if (trace_sched_update_nr_running_tp_enabled ()) {call_trace_sched_update_nr_running (rq, count); }
@@ -2989,6 +2993,9 @@ static inline void add_nr_running(struct rq *rq, unsigned count)static inline void sub_nr_running (struct rq *rq,unsigned count) { rq->nr_running -= count; +if (rq !=cpu_rq (rq->cpu)) +return ; +if (trace_sched_update_nr_running_tp_enabled ()) {call_trace_sched_update_nr_running (rq, -count); }
--
2.54.0
RFC PATCH v6 15/25 sched/rt: Update task event callbacks for HCBS scheduling
In reply to Yuri AndriaccioUpdate wakeup_preempt_rt, switched_{from/to}_rt and prio_changed_rt with rt-cgroup's specific preemption rules:
- In wakeup_preempt_rt(), whenever a task wakes up, it must be checked if it is served by a deadline server or it lives on the global runqueue. Preemption rules (as documented in the function), change based on the current task's donor and woken task runqueue:
- If both tasks are FIFO/RR tasks on the global runqueue, or the same cgroup, run as normal.
- If woken is inside a cgroup, but donor is a FIFO task on the global runqueue, always preempt. If donor is a DEADLINE task, check if the dl server preempts donor.
- If both tasks are FIFO/RR tasks in served but different groups, check whether the woken server preempts the donor server.
- In prio_changed_rt(), if the task is not running, only run preemption checks if the running task resides on the same task group of the task that changed priority.
Update sched_rt_can_attach() to check if a task can be attached to a given cgroup. For now the check only consists in checking if the group has non-zero bandwidth. Remove the tsk argument from sched_rt_can_attach, as it is unused.
Change cpu_cgroup_can_attach() to check if the attachee is a FIFO/RR task before attaching it to a cgroup.
Update __sched_setscheduler() to perform checks when trying to switch to FIFO/RR for a task inside a cgroup, as the group needs to have runtime allocated.
Update task_is_throttled_rt() for SCHED_CORE, returning the is_throttled value of the server if present, while global rt-tasks are never throttled.
Update migration functions to ignore cgroups migration, to be implemented in later patches.
--- kernel/sched/core.c | 2 +- kernel/sched/rt.c | 98 ++++++++++++++++++++++++++++++++++++++--- kernel/sched/sched.h | 2 +- kernel/sched/syscalls.c | 12 +++++ 4 files changed, 105 insertions(+), 9 deletions(-)
kernel/sched/core.c +1 -1
@@ -9545,7 +9545,7 @@ static int cpu_cgroup_can_attach(struct cgroup_taskset *tset) goto scx_check;cgroup_taskset_for_each (task, css, tset) { -if (!sched_rt_can_attach (css_tg (css), task)) +if (rt_task (task) && !sched_rt_can_attach (css_tg (css)))return -EINVAL ; } scx_check:
kernel/sched/rt.c +91 -7
189 lines not on this page · raw message
kernel/sched/sched.h +1 -1
@@ -611,7 +611,7 @@ extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)extern int sched_group_set_rt_period (struct task_group *tg,u64 rt_period_us);extern long sched_group_rt_runtime (struct task_group *tg);extern long sched_group_rt_period (struct task_group *tg); -extern int sched_rt_can_attach (struct task_group *tg,struct task_struct *tsk); +extern int sched_rt_can_attach (struct task_group *tg);extern struct task_group *sched_create_group (struct task_group *parent); extern void sched_online_group(struct task_group *tg,
kernel/sched/syscalls.c +12 -0
@@ -606,6 +606,18 @@ int __sched_setscheduler(struct task_struct *p, change:if (user) { +/* +* Do not allow real-time tasks into groups that have no runtime +* assigned. +*/ +if (rt_group_sched_enabled () && +dl_bandwidth_enabled () &&rt_policy (policy) && + !sched_rt_can_attach (task_group (p)) && + !task_group_is_autogroup (task_group (p))) { + retval = -EPERM ; + goto unlock; + } +if (dl_bandwidth_enabled () &&dl_policy (policy) && !(attr->sched_flags &SCHED_FLAG_SUGOV )) {cpumask_t *span = rq->rd->span;
--
2.54.0
RFC PATCH v6 16/25 sched/rt: Remove support for cgroups-v1
In reply to Yuri AndriaccioDisable control files for cgroups-v1.
Remove cpu_rt_group_init function and functions related to the cgroup-v1 control files 'rt_runtime_us' and 'rt_period_us'.
--- kernel/sched/core.c | 50 -------------------------------------------- kernel/sched/rt.c | 49 +------------------------------------------ kernel/sched/sched.h | 4 ---- 3 files changed, 1 insertion(+), 102 deletions(-)
kernel/sched/core.c +0 -50
@@ -10150,32 +10150,6 @@ static int cpu_burst_write_u64(struct cgroup_subsys_state *css, }#endif /* CONFIG_GROUP_SCHED_BANDWIDTH */ -#ifdef CONFIG_RT_GROUP_SCHED -static int cpu_rt_runtime_write (struct cgroup_subsys_state *css, -struct cftype *cft,s64 val) -{ -return sched_group_set_rt_runtime (css_tg (css), val); -} - -static s64 cpu_rt_runtime_read (struct cgroup_subsys_state *css, -struct cftype *cft) -{ -return sched_group_rt_runtime (css_tg (css)); -} - -static int cpu_rt_period_write_uint (struct cgroup_subsys_state *css, -struct cftype *cftype,u64 rt_period_us) -{ -return sched_group_set_rt_period (css_tg (css), rt_period_us); -} - -static u64 cpu_rt_period_read_uint (struct cgroup_subsys_state *css, -struct cftype *cft) -{ -return sched_group_rt_period (css_tg (css)); -} -#endif /* CONFIG_RT_GROUP_SCHED */ -#ifdef CONFIG_GROUP_SCHED_WEIGHT static s64 cpu_idle_read_s64 (struct cgroup_subsys_state *css,struct cftype *cft)
@@ -10253,20 +10227,6 @@ static struct cftype cpu_legacy_files[] = { };#ifdef CONFIG_RT_GROUP_SCHED -static struct cftype rt_group_files[] = { - { - .name ="rt_runtime_us" , - .read_s64 = cpu_rt_runtime_read, - .write_s64 = cpu_rt_runtime_write, - }, - { - .name ="rt_period_us" , - .read_u64 = cpu_rt_period_read_uint, - .write_u64 = cpu_rt_period_write_uint, - }, - { }/* Terminate */ -}; -# ifdef CONFIG_RT_GROUP_SCHED_DEFAULT_DISABLED DEFINE_STATIC_KEY_FALSE (rt_group_sched);# else
@@ -10289,16 +10249,6 @@ static int __init setup_rt_group_sched(char *str)return 1 ; }__setup ("rt_group_sched=" , setup_rt_group_sched); - -static int __init cpu_rt_group_init (void ) -{ -if (!rt_group_sched_enabled ()) -return 0 ; - -WARN_ON (cgroup_add_legacy_cftypes (&cpu_cgrp_subsys, rt_group_files)); -return 0 ; -} -subsys_initcall (cpu_rt_group_init);#endif /* CONFIG_RT_GROUP_SCHED */ static int cpu_extra_stat_show(struct seq_file *sf,
kernel/sched/rt.c +1 -48
58 lines not on this page · raw message
kernel/sched/sched.h +0 -4
@@ -607,10 +607,6 @@ extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);extern void unthrottle_cfs_rq (struct cfs_rq *cfs_rq);extern bool cfs_task_bw_constrained (struct task_struct *p); -extern int sched_group_set_rt_runtime (struct task_group *tg,long rt_runtime_us); -extern int sched_group_set_rt_period (struct task_group *tg,u64 rt_period_us); -extern long sched_group_rt_runtime (struct task_group *tg); -extern long sched_group_rt_period (struct task_group *tg);extern int sched_rt_can_attach (struct task_group *tg);extern struct task_group *sched_create_group (struct task_group *parent);
Signature · 1 line
2.54.0
RFC PATCH v6 17/25 sched/rt: Update rt-cgroup schedulability checks
In reply to Yuri AndriaccioFrom: luca abeni <[email protected]>
Introduce cgroup-v2 control files:
- cpu.rt.max:
Get/set the bandwidth of the given cgroup, or inherith from parent.
- cpu.rt.internal:
Get the actual remaning bandwidth for the group, removing the bw of the group's children.
Introduce a number of functions to update the cgroup settings across the whole hierarchy:
- tg_subtree_has_rt_tasks()
Checks if the active context rooted at tg is running rt workload.
Child groups which do not share the same active context are ignored.
- tg_compute_children_bw()
Computes the total bandwidth of the active context rooted at tg minux the root of the context itself.
- tg_rt_schedulable()
Runs admission tests for the current cgroup tree and the given bandwidth update.
- tg_update_active_context()
Updates the active context of a given subtree with a new one.
- tg_rt_bandwidth() / tg_rt_internal_bandwidth()
Read the max (internal) bandwidth set to the cgroup.
- tg_set_rt_bandwidth()
Set the bandwidth of the group.
Update sched_rt_can_attach to run only tasks in the root cgroup or HCBS cgroups which have non-zero runtime.
Update and reuse __checkparam_dl to check for numerical issues regarding the dl_server's parameters.
Add from_ratio function to convert from period and bw to runtime, inverse of the to_ratio function.
Add dl_check_tg(), which performs an admission control test similar to __dl_overflow, but this time we are updating the cgroup's total bandwidth rather than scheduling a new DEADLINE task or updating a non-cgroup deadline server.
Add rcu_sched lock guard for rcu_read_{lock/unlock}_sched. Add sched_domains lock guard for sched_domains_mutex_{lock/unlock}. Add lock/unlock methods for sched_rt_handler_mutex and its lock guard.
Add asserts for held sched_domains_mutex and sched_rt_handler_mutex.
--- include/linux/rcupdate.h | 1 + include/linux/sched.h | 2 + kernel/sched/core.c | 55 ++++++ kernel/sched/deadline.c | 60 ++++-- kernel/sched/rt.c | 393 +++++++++++++++++++++++++++++++-------- kernel/sched/sched.h | 18 +- kernel/sched/syscalls.c | 2 +- 7 files changed, 445 insertions(+), 86 deletions(-)
include/linux/rcupdate.h +1 -0
@@ -1179,6 +1179,7 @@ extern int rcu_expedited;extern int rcu_normal; DEFINE_LOCK_GUARD_0(rcu, rcu_read_lock(), rcu_read_unlock()) +DEFINE_LOCK_GUARD_0(rcu_sched, rcu_read_lock_sched(), rcu_read_unlock_sched()) DECLARE_LOCK_GUARD_0_ATTRS(rcu, __acquires_shared(RCU), __releases_shared(RCU))#endif /* __LINUX_RCUPDATE_H */
include/linux/sched.h +2 -0
@@ -2522,4 +2522,6 @@ extern void migrate_enable(void); DEFINE_LOCK_GUARD_0(migrate, migrate_disable(), migrate_enable()) +DEFINE_LOCK_GUARD_0(sched_domains, sched_domains_mutex_lock(), sched_domains_mutex_unlock()) +#endif
kernel/sched/core.c +55 -0
73 lines not on this page · raw message
kernel/sched/deadline.c +49 -11
111 lines not on this page · raw message
kernel/sched/rt.c +320 -73
494 lines not on this page · raw message
kernel/sched/sched.h +17 -1
@@ -366,7 +366,7 @@ extern void sched_dl_do_global(void);extern int sched_dl_overflow (struct task_struct *p,int policy,const struct sched_attr *attr);extern void __setparam_dl (struct task_struct *p,const struct sched_attr *attr);extern void __getparam_dl (struct task_struct *p,struct sched_attr *attr,unsigned int flags); -extern bool __checkparam_dl (const struct sched_attr *attr); +extern bool __checkparam_dl (const struct sched_attr *attr,bool allow_zero_runtime);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);
@@ -425,6 +425,7 @@ extern void dl_server_init(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq,struct rq *served_rq, dl_server_pick_f pick_task);extern void sched_init_dl_servers (void ); +extern int dl_check_tg (unsigned long total);extern void dl_init_tg (struct sched_dl_entity *dl_se,u64 rt_runtime,u64 rt_period);extern void fair_server_init (struct rq *rq);
@@ -607,6 +608,12 @@ extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);extern void unthrottle_cfs_rq (struct cfs_rq *cfs_rq);extern bool cfs_task_bw_constrained (struct task_struct *p); +extern int tg_rt_bandwidth (struct task_group *tg, +long *rt_period_us,long *rt_runtime_us); +extern int tg_rt_internal_bandwidth (struct task_group *tg, +long *rt_period_us,long *rt_runtime_us); +extern int tg_set_rt_bandwidth (struct task_group *tg, +u64 rt_period_us,u64 rt_runtime_us);extern int sched_rt_can_attach (struct task_group *tg);extern struct task_group *sched_create_group (struct task_group *parent);
@@ -2045,6 +2052,14 @@ DEFINE_LOCK_GUARD_1(raw_spin_rq_lock_irq, struct rq, raw_spin_rq_lock_irq(_T->lock), raw_spin_rq_unlock_irq(_T->lock)) +extern struct mutex sched_rt_handler_mutex; +extern void sched_rt_handler_mutex_lock (void ); +extern void sched_rt_handler_mutex_unlock (void ); + +DEFINE_LOCK_GUARD_0 (sched_rt_handler, +sched_rt_handler_mutex_lock (), + sched_rt_handler_mutex_unlock()) +#ifdef CONFIG_NUMA enum numa_topology_type {
@@ -2938,6 +2953,7 @@ extern void init_cfs_throttle_work(struct task_struct *p);#define MAX_BW ((1ULL << MAX_BW_BITS) - 1)extern u64 to_ratio (u64 period,u64 runtime); +extern u64 from_ratio (u64 period,u64 bw);extern void init_entity_runnable_average (struct sched_entity *se);extern void post_init_entity_util_avg (struct task_struct *p);
kernel/sched/syscalls.c +1 -1
@@ -528,7 +528,7 @@ int __sched_setscheduler(struct task_struct *p,*/ if (attr->sched_priority >MAX_RT_PRIO -1 )return -EINVAL ; -if ((dl_policy (policy) && !__checkparam_dl (attr)) || +if ((dl_policy (policy) && !__checkparam_dl (attr, false)) || (rt_policy (policy) != (attr->sched_priority !=0 )))return -EINVAL ;
Signature · 1 line
2.54.0
RFC PATCH v6 18/25 sched/rt: Update task's RT runqueue when switching scheduling class
In reply to Yuri Andriaccio--- kernel/sched/rt.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
kernel/sched/rt.c +20 -0
@@ -1897,6 +1897,25 @@ void __init init_sched_rt_class(void) } } +#ifdef CONFIG_RT_GROUP_SCHED +static void switching_to_rt (struct rq *rq,struct task_struct *p) +{ +struct task_group *tg = p->sched_task_group; +int cpu = rq->cpu; + +if (tg == &root_task_group) +return ; + +guard (raw_spinlock_irqsave)(dl_bw_lock_of_tg (tg)); +if (!rt_group_sched_enabled ()) + tg = &root_task_group; + + p->rt.rt_rq =dl_bandwidth_read (tg)->active_context->rt_rq[cpu]; +} +#else +static void switching_to_rt (struct rq *rq,struct task_struct *p) {} +#endif +/* * When switching a task to RT, we may overload the runqueue * with RT tasks. In this case we try to push them off to
@@ -2095,6 +2114,7 @@ DEFINE_SCHED_CLASS(rt) = { .get_rr_interval = get_rr_interval_rt, + .switching_to = switching_to_rt, .switched_to = switched_to_rt, .prio_changed = prio_changed_rt,
--
2.54.0
RFC PATCH v6 19/25 sched/rt: Remove old RT_GROUP_SCHED data structures
In reply to Yuri AndriaccioFrom: luca abeni <[email protected]>
Completely remove the old RT_GROUP_SCHED's functions and data structures:
- Remove the fields back and my_q from sched_rt_entity.
- Remove the rt_bandwidth data structure.
- Remove the field rt_bandwidth from task_group.
- Remove the rt_bandwidth_enabled function.
- Remove the fields rt_queued, rt_throttled, rt_time, rt_runtime, rt_runtime_lock and rt_nr_boosted from rt_rq.
All of the removed fields and data are similarly represented in previously added fields in rq, rt_rq, dl_bandwidth and in the dl server themselves.
--- include/linux/sched.h | 3 --- kernel/sched/sched.h | 33 --------------------------------- 2 files changed, 36 deletions(-)
include/linux/sched.h +0 -3
@@ -628,12 +628,9 @@ struct sched_rt_entity {unsigned short on_rq;unsigned short on_list; -struct sched_rt_entity *back;#ifdef CONFIG_RT_GROUP_SCHED /* rq on which this entity is (to be) queued: */ struct rt_rq *rt_rq; -/* rq "owned" by this entity/group: */ -struct rt_rq *my_q;#endif } __randomize_layout;
kernel/sched/sched.h +0 -33
@@ -313,15 +313,6 @@ struct rt_prio_array {struct list_head queue[MAX_RT_PRIO ]; }; -struct rt_bandwidth { -/* nests inside the rq lock: */ -raw_spinlock_t rt_runtime_lock; -ktime_t rt_period; -u64 rt_runtime; -struct hrtimer rt_period_timer; -unsigned int rt_period_active; -}; -struct dl_bandwidth {raw_spinlock_t dl_runtime_lock;u64 dl_runtime;
@@ -343,12 +334,6 @@ static inline int dl_bandwidth_enabled(void)* - cache the fraction of bandwidth that is currently allocated in * each root domain; * -* This is all done in the data structure below. It is similar to the -* one used for RT-throttling (rt_bandwidth), with the main difference -* that, since here we are only interested in admission control, we -* do not decrease any runtime while the group "executes", neither we -* need a timer to replenish it. -* * With respect to SMP, bandwidth is given on a per root domain basis, * meaning that: * - bw (< 100%) is the deadline bandwidth of each CPU;
@@ -511,11 +496,9 @@ struct task_group {* different deadline server, and a runqueue per CPU. All the dl-servers * share the same dl_bandwidth object. */ -struct sched_rt_entity **rt_se;struct sched_dl_entity **dl_se;struct rt_rq **rt_rq; -struct rt_bandwidth rt_bandwidth;struct dl_bandwidth dl_bandwidth;#endif
@@ -842,11 +825,6 @@ struct scx_rq { };#endif /* CONFIG_SCHED_CLASS_EXT */ -static inline int rt_bandwidth_enabled (void ) -{ -return 0 ; -} -/* RT IPI pull logic requires IRQ_WORK */ #if defined(CONFIG_IRQ_WORK ) && defined(CONFIG_SMP )# define HAVE_RT_PUSH_IPI
@@ -864,17 +842,6 @@ struct rt_rq {bool overloaded;struct plist_head pushable_tasks; -int rt_queued; - -#ifdef CONFIG_RT_GROUP_SCHED -int rt_throttled; -u64 rt_time;/* consumed RT time, goes up in update_curr_rt */ -u64 rt_runtime;/* allotted RT time, "slice" from rt_bandwidth, RT sharing/balancing */ -/* Nests inside the rq lock: */ -raw_spinlock_t rt_runtime_lock; - -unsigned int rt_nr_boosted; -#endif #ifdef CONFIG_CGROUP_SCHED struct task_group *tg;/* this tg has "this" rt_rq on given CPU for runnable entities */ #endif
--
2.54.0
RFC PATCH v6 20/25 sched/rt: Add HCBS migration code to related functions
In reply to Yuri AndriaccioUpdate rt_queue_{push/pull}_task{s} to differentiate between cgroup and global runqueue balancing.
Introduce new balance callbacks for cgroup migration.
Add rq_to_push_from and rq_to_push_to fields for cgroup related migration.
Balance callbacks are only called on the root runqueues, thus it is necessary to store which non-root runqueues need to be balanced.
Update migration functions to specialize for cgroup migration:
- find_lowest_rt_rq():
Scan all the cpus to get the cgroup specific lowest_mask.
- find_lock_lowest_rt_rq():
Use appropriate rt_rqs to differentiate the cgroup being checked. Prevent migration for throttled cgroups.
- push_rt_rq_task():
Allow pushing away for migration disabled tasks only if the tasks belong to the same cgroup.
- pull_rt_rq_task():
Use appropriate rt_rqs and push away for migration disabled only if the task to pull and curr are in the same runqueue.
Add tg_of_se to get the task group a scheduling entity is assigned to.
This is different from the active context of the group.
Add new macros for field access and non-CONFIG_RT_GROUP_SCHED code.
--- kernel/sched/rt.c | 263 ++++++++++++++++++++++++++++++++----------- kernel/sched/sched.h | 26 +++++ 2 files changed, 221 insertions(+), 68 deletions(-)
kernel/sched/rt.c +195 -68
@@ -364,31 +364,61 @@ static inline int has_pushable_tasks(struct rt_rq *rt_rq) static DEFINE_PER_CPU(struct balance_callback, rt_push_head); static DEFINE_PER_CPU(struct balance_callback, rt_pull_head); +static DEFINE_PER_CPU(struct balance_callback, rt_group_push_head); +static DEFINE_PER_CPU(struct balance_callback, rt_group_pull_head);static void push_rt_tasks (struct rq *);static void pull_rt_task (struct rq *); +static void push_group_rt_tasks (struct rq *); +static void pull_group_rt_task (struct rq *);static inline void rt_queue_push_tasks (struct rt_rq *rt_rq) { -struct rq *rq =global_rq_of_rt_rq (rt_rq); - -if (is_dl_group (rt_rq)) -return ; +struct rq *rq =rq_of_rt_rq (rt_rq); +struct rq *global_rq =global_rq_of_rt_rq (rt_rq);if (!has_pushable_tasks (rt_rq))return ; -queue_balance_callback (rq, &per_cpu (rt_push_head, rq->cpu), push_rt_tasks); +if (!rt_group_sched_enabled () || !is_dl_group (rt_rq)) { + +queue_balance_callback (global_rq, + &per_cpu (rt_push_head, rq->cpu), + push_rt_tasks); + }else { + +if (rq_to_push_from (global_rq)) +return ; + +rq_to_push_from (global_rq) = rq; +queue_balance_callback (global_rq, + &per_cpu (rt_group_push_head, global_rq->cpu), + push_group_rt_tasks); + } }static inline void rt_queue_pull_task (struct rt_rq *rt_rq) { -struct rq *rq =global_rq_of_rt_rq (rt_rq); +struct rq *rq =rq_of_rt_rq (rt_rq); +struct rq *global_rq =global_rq_of_rt_rq (rt_rq); +struct sched_dl_entity *dl_se; -if (is_dl_group (rt_rq)) -return ; +if (!rt_group_sched_enabled () || !is_dl_group (rt_rq)) { -queue_balance_callback (rq, &per_cpu (rt_pull_head, rq->cpu), pull_rt_task); +queue_balance_callback (global_rq, + &per_cpu (rt_pull_head, rq->cpu), + pull_rt_task); + }else { + + dl_se =dl_group_of (rt_rq); +if (dl_se->dl_throttled ||rq_to_pull_to (global_rq)) +return ; + +rq_to_pull_to (global_rq) = rq; +queue_balance_callback (global_rq, + &per_cpu (rt_group_pull_head, global_rq->cpu), + pull_group_rt_task); + } }static void push_rt_rq_tasks (struct rt_rq *rt_rq);
@@ -403,6 +433,27 @@ static void pull_rt_task(struct rq *global_rq) {pull_rt_rq_task (&global_rq->rt); } +static void push_group_rt_tasks (struct rq *global_rq) +{ +struct rq *rq =rq_to_push_from (global_rq); +struct rt_rq *rt_rq = &rq->rt; + +if (rt_rq->rt_nr_running <=1 && !dl_group_of (rt_rq)->dl_throttled) +return ; + +push_rt_rq_tasks (rt_rq); +rq_to_push_from (global_rq) =NULL ; +} + +static void pull_group_rt_task (struct rq *global_rq) +{ +struct rq *rq =rq_to_pull_to (global_rq); +struct rt_rq *rt_rq = &rq->rt; + +pull_rt_rq_task (rt_rq); +rq_to_pull_to (global_rq) =NULL ; +} +static void enqueue_pushable_task (struct rt_rq *rt_rq,struct task_struct *p) {plist_del (&p->pushable_tasks, &rt_rq->pushable_tasks);
@@ -1220,35 +1271,71 @@ static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);static int find_lowest_rt_rq (struct task_struct *task) {struct sched_domain *sd; -struct cpumask *lowest_mask =this_cpu_cpumask_var_ptr (local_cpu_mask); -int this_cpu =smp_processor_id (); -int cpu =task_cpu (task); -int ret; - -/* Make sure the mask is initialized first */ -if (unlikely (!lowest_mask)) -return -1 ; +struct cpumask mask, *lowest_mask; +struct sched_dl_entity *dl_se; +struct rt_rq *rt_rq, *task_rt_rq =rt_rq_of_se (&task->rt); +int cpu, this_cpu =smp_processor_id (); +int ret, prio, lowest_prio;if (task->nr_cpus_allowed ==1 )return -1 ;/* No other targets possible */ -/* -* If we're on asym system ensure we consider the different capacities -* of the CPUs when searching for the lowest_mask. -*/ -if (sched_asym_cpucap_active ()) { +if (!rt_group_sched_enabled () || !is_dl_group (task_rt_rq)) { + + lowest_mask =this_cpu_cpumask_var_ptr (local_cpu_mask); - ret =cpupri_find_fitness (&task_rq (task)->rd->cpupri, - task, lowest_mask, - rt_task_fits_capacity); +/* Make sure the mask is initialized first */ +if (unlikely (!lowest_mask)) +return -1 ; + +/* +* If we're on asym system ensure we consider the different +* capacities of the CPUs when searching for the lowest_mask. +*/ +if (sched_asym_cpucap_active ()) { + + ret =cpupri_find_fitness (&task_rq (task)->rd->cpupri, + task, lowest_mask, + rt_task_fits_capacity); + }else { + + ret =cpupri_find (&task_rq (task)->rd->cpupri, + task, lowest_mask); + } + +if (!ret) +return -1 ;/* No targets found */ }else { - ret =cpupri_find (&task_rq (task)->rd->cpupri, - task, lowest_mask); + lowest_prio = task->prio -1 ; + lowest_mask = &mask; +cpumask_clear (lowest_mask); +for_each_cpu_and (cpu, cpu_online_mask, task->cpus_ptr) { + dl_se =dl_se_of_tg (task_rt_rq->tg, cpu); + rt_rq = &dl_se->my_q->rt; + prio = rt_rq->highest_prio.curr; + +/* +* If we're on asym system ensure we consider the +* different capacities of the CPUs when searching for +* the lowest_mask. +*/ +if (dl_se->dl_throttled || !rt_task_fits_capacity (task, cpu)) +continue ; + +if (prio >= lowest_prio) { +if (prio > lowest_prio) { +cpumask_clear (lowest_mask); + lowest_prio = prio; + } + +cpumask_set_cpu (cpu, lowest_mask); + } + } } -if (!ret) -return -1 ;/* No targets found */ +if (cpumask_empty (lowest_mask)) +return -1 ;/* * At this point we have built a mask of CPUs representing the
@@ -1258,6 +1345,7 @@ static int find_lowest_rt_rq(struct task_struct *task)* We prioritize the last CPU that the task executed on since * it is most likely cache-hot in that location. */ + cpu =task_cpu (task);if (cpumask_test_cpu (cpu, lowest_mask))return cpu;
@@ -1268,30 +1356,27 @@ static int find_lowest_rt_rq(struct task_struct *task)if (!cpumask_test_cpu (this_cpu, lowest_mask)) this_cpu =-1 ;/* Skip this_cpu opt if not among lowest */ -rcu_read_lock (); -for_each_domain (cpu, sd) { -if (sd->flags &SD_WAKE_AFFINE ) { +scoped_guard (rcu) { +for_each_domain (cpu, sd) {int best_cpu; +if (!(sd->flags &SD_WAKE_AFFINE )) +continue ; +/* * "this_cpu" is cheaper to preempt than a * remote processor. */ if (this_cpu !=-1 && -cpumask_test_cpu (this_cpu,sched_domain_span (sd))) { -rcu_read_unlock (); +cpumask_test_cpu (this_cpu,sched_domain_span (sd)))return this_cpu; - } best_cpu =cpumask_any_and_distribute (lowest_mask,sched_domain_span (sd)); -if (best_cpu < nr_cpu_ids) { -rcu_read_unlock (); +if (best_cpu < nr_cpu_ids)return best_cpu; - } } } -rcu_read_unlock ();/* * And finally, if there were no matches within the domains
@@ -1342,27 +1427,35 @@ static struct task_struct *pick_next_pushable_task(struct rt_rq *rt_rq)/* Will lock the rq it finds */ static struct rt_rq *find_lock_lowest_rt_rq (struct task_struct *task,struct rt_rq *rt_rq) { -struct rq *rq =rq_of_rt_rq (rt_rq); -struct rq *lowest_rq =NULL ; -int tries; -int cpu; +struct rq *lowest_rq, *rq =global_rq_of_rt_rq (rt_rq); +struct rt_rq *lowest_rt_rq; +struct sched_dl_entity *lowest_dl_se; +int tries, cpu; +bool dl_group; + + dl_group =rt_group_sched_enabled () &&is_dl_group (rt_rq);for (tries =0 ; tries <RT_MAX_TRIES ; tries++) { cpu =find_lowest_rt_rq (task);if ((cpu ==-1 ) || (cpu == rq->cpu)) -break ; +return NULL ; lowest_rq =cpu_rq (cpu); +if (dl_group) { + lowest_dl_se =dl_se_of_tg (rt_rq->tg, cpu); + lowest_rt_rq = &lowest_dl_se->my_q->rt; + }else { + lowest_rt_rq = &lowest_rq->rt; + } -if (lowest_rq->rt.highest_prio.curr <= task->prio) { +if (lowest_rt_rq->highest_prio.curr <= task->prio) {/* * Target rq has tasks of equal or higher priority, * retrying does not release any lock and is unlikely * to yield a different result. */ - lowest_rq =NULL ; -break ; +return NULL ; }/* if the prio of this runqueue changed, try again */
@@ -1378,25 +1471,24 @@ static struct rt_rq *find_lock_lowest_rt_rq(struct task_struct *task, struct rt_* check the task migration disable flag here too. */ if (unlikely (is_migration_disabled (task) || + (dl_group && lowest_dl_se->dl_throttled) || !cpumask_test_cpu (lowest_rq->cpu, &task->cpus_mask) || task !=pick_next_pushable_task (rt_rq))) {double_unlock_balance (rq, lowest_rq); - lowest_rq =NULL ; -break ; +return NULL ; } }/* If this rq is still suitable use it. */ -if (lowest_rq->rt.highest_prio.curr > task->prio) -break ; +if (lowest_rt_rq->highest_prio.curr > task->prio) +return lowest_rt_rq;/* try again */ double_unlock_balance (rq, lowest_rq); - lowest_rq =NULL ; } -return &lowest_rq->rt; +return NULL ; }static struct rq *find_lock_lowest_rq (struct task_struct *task,struct rq *rq) {
@@ -1413,12 +1505,10 @@ static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq) {static int push_rt_rq_task (struct rt_rq *rt_rq,bool pull) {struct task_struct *next_task; -struct rq *lowest_rq, *rq =rq_of_rt_rq (rt_rq); +struct rq *lowest_rq, *rq =global_rq_of_rt_rq (rt_rq);struct rt_rq *lowest_rt_rq;int ret =0 ; - -if (is_dl_group (rt_rq)) -return 0 ; +bool dl_group;if (!rt_rq->overloaded)return 0 ;
@@ -1433,7 +1523,8 @@ static int push_rt_rq_task(struct rt_rq *rt_rq, bool pull)* higher priority than current. If that's the case * just reschedule current. */ -if (unlikely (next_task->prio < rq->donor->prio)) { + dl_group =rt_group_sched_enabled () &&is_dl_group (rt_rq); +if (!dl_group &&unlikely (next_task->prio < rq->donor->prio)) {resched_curr (rq);return 0 ; }
@@ -1445,6 +1536,13 @@ static int push_rt_rq_task(struct rt_rq *rt_rq, bool pull)if (!pull || rq->push_busy)return 0 ; +/* +* If the current task does not belong to the same task group +* we cannot push it away. +*/ +if (dl_group && rq->donor->sched_task_group != rt_rq->tg) +return 0 ; +/* * Invoking find_lowest_rt_rq() on anything but an RT task doesn't * make sense. Per the above priority check, curr has to
@@ -1521,7 +1619,7 @@ static int push_rt_rq_task(struct rt_rq *rt_rq, bool pull) goto retry; } - lowest_rq =rq_of_rt_rq (lowest_rt_rq); + lowest_rq =global_rq_of_rt_rq (lowest_rt_rq);move_queued_task_locked (rq, lowest_rq, next_task);resched_curr (lowest_rq); ret =1 ;
@@ -1718,16 +1816,22 @@ void rto_push_irq_work_func(struct irq_work *work)static void pull_rt_rq_task (struct rt_rq *this_rt_rq) { -struct rq *this_rq =rq_of_rt_rq (this_rt_rq); +struct rq * this_rq =global_rq_of_rt_rq (this_rt_rq);int this_cpu = this_rq->cpu, cpu;bool resched = false; -struct task_struct *p, *push_task; +struct task_struct *p, *push_task =NULL ; +struct sched_dl_entity *src_dl_se;struct rt_rq *src_rt_rq;struct rq *src_rq; -int rt_overload_count =rt_overloaded (this_rq); +int rt_overload_count; +const struct cpumask *cpu_mask; +bool dl_group; -if (is_dl_group (&this_rq->rt)) -return ; + dl_group =rt_group_sched_enabled () &&is_dl_group (this_rt_rq); +if (dl_group) + goto group_sched; + + rt_overload_count =rt_overloaded (this_rq);if (likely (!rt_overload_count))return ;
@@ -1750,12 +1854,26 @@ static void pull_rt_rq_task(struct rt_rq *this_rt_rq) }#endif -for_each_cpu (cpu, this_rq->rd->rto_mask) { +group_sched: +if (!dl_group) + cpu_mask = this_rq->rd->rto_mask; +else + cpu_mask = cpu_online_mask; + +for_each_cpu (cpu, cpu_mask) {if (this_cpu == cpu)continue ; src_rq =cpu_rq (cpu); - src_rt_rq = &src_rq->rt; +if (!dl_group) { + src_rt_rq = &src_rq->rt; + }else { + src_dl_se =dl_se_of_tg (this_rt_rq->tg, cpu); + src_rt_rq = &src_dl_se->my_q->rt; + +if (src_rt_rq->rt_nr_running <=1 && !src_dl_se->dl_throttled) +continue ; + }/* * Don't bother taking the src_rq->lock if the next highest
@@ -1796,12 +1914,21 @@ static void pull_rt_rq_task(struct rt_rq *this_rt_rq)* This is just that p is waking up and hasn't * had a chance to schedule. We only pull * p if it is lower in priority than the -* current task on the run queue +* current task on the run queue and p is +* in the same runqueue as donor. */ -if (p->prio < src_rq->donor->prio) +if (tg_of_se (&src_rq->donor->rt) == this_rt_rq->tg && + p->prio < src_rq->donor->prio) goto skip;if (is_migration_disabled (p)) { +/* +* If the current task does not belong to the +* same task group we cannot push it away. +*/ +if (tg_of_se (&src_rq->donor->rt) != this_rt_rq->tg) + goto skip; + push_task =get_push_task (src_rq); }else {move_queued_task_locked (src_rq, this_rq, p);
kernel/sched/sched.h +26 -0
@@ -1341,6 +1341,16 @@ struct rq {struct list_head cfsb_csd_list;#endif +#ifdef CONFIG_RT_GROUP_SCHED +/* +* Balance callbacks operate only on global runqueues. +* These pointers allow referencing cgroup specific runqueues +* for balancing operations. +*/ +struct rq *rq_to_push_from; +struct rq *rq_to_pull_to; +#endif +atomic_t nr_iowait; } __no_randomize_layout;
@@ -3366,6 +3376,11 @@ static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)return rt_se->rt_rq; } +static inline struct task_group *tg_of_se (struct sched_rt_entity *rt_se) +{ +return rt_rq_of_se (rt_se)->tg; +} +static inline int is_dl_group (struct rt_rq *rt_rq) {return rt_rq->tg != &root_task_group;
@@ -3382,6 +3397,9 @@ static inline struct sched_dl_entity *dl_group_of(struct rt_rq *rt_rq) return rt_rq->tg->dl_se[rq_of_rt_rq(rt_rq)->cpu]; } +#define rq_to_push_from (rq) ((rq)->rq_to_push_from) +#define rq_to_pull_to (rq) ((rq)->rq_to_pull_to) +#define dl_se_of_tg (tg, cpu) ((tg)->dl_se[(cpu)])#define dl_bw_lock_of_tg (tg) (&(tg)->dl_bandwidth.dl_runtime_lock)#else static inline struct task_struct *rt_task_of (struct sched_rt_entity *rt_se)
@@ -3406,6 +3424,11 @@ static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)return &rq->rt; } +static inline struct task_group *tg_of_se (struct sched_rt_entity *rt_se) +{ +return &root_task_group; +} +static inline int is_dl_group (struct rt_rq *rt_rq) {return 0 ;
@@ -3416,6 +3439,9 @@ static inline struct sched_dl_entity *dl_group_of(struct rt_rq *rt_rq) return NULL; } +#define rq_to_push_from (rq) (rq) +#define rq_to_pull_to (rq) (rq) +#define dl_se_of_tg (tg, cpu) ((struct sched_dl_entity*)NULL)#define dl_bw_lock_of_tg (tg) ((raw_spinlock_t*)NULL)#endif
--
2.54.0
RFC PATCH v6 21/25 sched/rt: Hook HCBS migration functions
In reply to Yuri AndriaccioHook rt-cgroup migration functions:
- select_task_rt_rq
Always return the cpu where the task is scheduled.
- balance_rt
- put_prev_task_rt
If a server is throttled, put_prev_task_rt is invoked and a push is necessary so that the task can keep running on another server if possible.
- switched_to_rt
Keep track of the deadline server that is assigned to the task switching to FIFO/RR priority.
--- kernel/sched/rt.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-)
kernel/sched/rt.c +31 -7
@@ -976,6 +976,10 @@ select_task_rq_rt(struct task_struct *p, int cpu, int flags)struct rq *rq;bool test; +/* Just return the task_cpu for processes inside task groups */ +if (is_dl_group (rt_rq_of_se (&p->rt))) + goto out; +/* For anything but wake ups, just return the task_cpu */ if (!(flags & (WF_TTWU |WF_FORK ))) goto out;
@@ -1065,21 +1069,25 @@ static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)resched_curr (rq); } -static int balance_rt (struct rq *rq,struct task_struct *p,struct rq_flags *rf) +static int balance_rt (struct rq *global_rq,struct task_struct *p,struct rq_flags *rf) { -if (!on_rt_rq (&p->rt) &&need_pull_rt_task (rq, p)) { +struct rt_rq *rt_rq =rt_rq_of_se (&p->rt); + +if (!on_rt_rq (&p->rt) &&need_pull_rt_task (rq_of_rt_rq (rt_rq), p)) {/* * This is OK, because current is on_cpu, which avoids it being * picked for load-balance and preemption/IRQs are still * disabled avoiding further scheduler activity on it and we've * not yet started the picking loop. */ -rq_unpin_lock (rq, rf); -pull_rt_rq_task (&rq->rt); -rq_repin_lock (rq, rf); +rq_unpin_lock (global_rq, rf); +pull_rt_rq_task (rt_rq); +rq_repin_lock (global_rq, rf); } -return sched_stop_runnable (rq) ||sched_dl_runnable (rq) ||sched_rt_runnable (rq); +return sched_stop_runnable (global_rq) || +sched_dl_runnable (global_rq) || +sched_rt_runnable (global_rq); }/*
@@ -1241,6 +1249,13 @@ static void put_prev_task_rt(struct rq *rq, struct task_struct *p, struct task_s*/ if (on_rt_rq (&p->rt) && p->nr_cpus_allowed >1 )enqueue_pushable_task (rt_rq, p); + +if (is_dl_group (rt_rq)) { +struct sched_dl_entity *dl_se =dl_group_of (rt_rq); + +if (dl_se->dl_throttled) +rt_queue_push_tasks (rt_rq); + } }/* Only try algorithms three times */
@@ -2050,12 +2065,21 @@ static void switching_to_rt(struct rq *rq, struct task_struct *p) {}*/ static void switched_to_rt (struct rq *rq,struct task_struct *p) { +struct rt_rq *rt_rq =rt_rq_of_se (&p->rt); +/* * If we are running, update the avg_rt tracking, as the running time * will now on be accounted into the latter. */ if (task_current (rq, p)) {update_rt_rq_load_avg (rq_clock_pelt (rq), rq,0 ); + +if (is_dl_group (rt_rq)) { +struct sched_dl_entity *dl_se =dl_group_of (rt_rq); + + p->dl_server = dl_se; + } +return ; }
@@ -2066,7 +2090,7 @@ static void switched_to_rt(struct rq *rq, struct task_struct *p)*/ if (task_on_rq_queued (p)) {if (p->nr_cpus_allowed >1 && rq->rt.overloaded) -rt_queue_push_tasks (rt_rq_of_se (&p->rt)); +rt_queue_push_tasks (rt_rq);if (p->prio < rq->donor->prio &&cpu_online (cpu_of (rq)))resched_curr (rq);
Signature · 1 line
2.54.0
RFC PATCH v6 22/25 sched/core: Execute enqueued balance callbacks when changing allowed CPUs
In reply to Yuri AndriaccioFrom: luca abeni <[email protected]>
Execute balancing callbacks when setting the affinity of a task, since the HCBS scheduler may request balancing of throttled dl_servers to fully utilize the server's bandwidth.
--- kernel/sched/core.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
kernel/sched/core.c +13 -0
@@ -2933,6 +2933,7 @@ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flagif (cpumask_test_cpu (task_cpu (p), &p->cpus_mask) || (task_current_donor (rq, p) && !task_current (rq, p))) {struct task_struct *push_task =NULL ; +struct balance_callback *head;if ((flags &SCA_MIGRATE_ENABLE ) && (p->migration_flags &MDF_PUSH ) && !rq->push_busy) {
@@ -2951,11 +2952,13 @@ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flag }preempt_disable (); + head =splice_balance_callbacks (rq);task_rq_unlock (rq, p, rf);if (push_task) {stop_one_cpu_nowait (rq->cpu, push_cpu_stop, p, &rq->push_work); } +balance_callbacks (rq, head);preempt_enable ();if (complete)
@@ -3010,6 +3013,8 @@ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flag }if (task_on_cpu (rq, p) ||READ_ONCE (p->__state) ==TASK_WAKING ) { +struct balance_callback *head; +/* * MIGRATE_ENABLE gets here because 'p == current', but for * anything else we cannot do is_migration_disabled(), punt
@@ -3023,16 +3028,19 @@ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flag p->migration_flags &= ~MDF_PUSH ;preempt_disable (); + head =splice_balance_callbacks (rq);task_rq_unlock (rq, p, rf);if (!stop_pending) {stop_one_cpu_nowait (cpu_of (rq), migration_cpu_stop, &pending->arg, &pending->stop_work); } +balance_callbacks (rq, head);preempt_enable ();if (flags &SCA_MIGRATE_ENABLE )return 0 ; }else { +struct balance_callback *head;if (!is_migration_disabled (p)) {if (task_on_rq_queued (p))
@@ -3043,7 +3051,12 @@ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flag complete = true; } } + +preempt_disable (); + head =splice_balance_callbacks (rq);task_rq_unlock (rq, p, rf); +balance_callbacks (rq, head); +preempt_enable ();if (complete)complete_all (&pending->done);
--
2.54.0
RFC PATCH v6 23/25 sched/rt: Try pull task on empty server pick.
In reply to Yuri AndriaccioTry to pull task on a server with an empty runqueue before returning NULL (and thus shutting down).
---
When all the servers of a cgroup are throttled, work is pending, and any one of the servers is replenished, it may happen that the runqueue is empty and thus the replenished server is immediately shut down.
The server may try to pull a task so that the cgroup could consume its allocated runtime as soon as it is replenished.
--- kernel/sched/rt.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
kernel/sched/rt.c +10 -2
@@ -285,14 +285,22 @@ int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) }static struct sched_rt_entity *pick_next_rt_entity (struct rt_rq *rt_rq); +static void pull_rt_task (struct rq *);static struct task_struct *rt_server_pick (struct sched_dl_entity *dl_se,struct rq_flags *rf) {struct rt_rq *rt_rq = &dl_se->my_q->rt; +struct rq *global_rq =global_rq_of_rt_rq (rt_rq);struct task_struct *p; -if (!sched_rt_runnable (dl_se->my_q)) -return NULL ; +if (!sched_rt_runnable (dl_se->my_q)) { +rq_unpin_lock (global_rq, rf); +pull_rt_task (rq_of_rt_rq (rt_rq)); +rq_repin_lock (global_rq, rf); + +if (!sched_rt_runnable (dl_se->my_q)) +return NULL ; + } p =rt_task_of (pick_next_rt_entity (rt_rq));
Signature · 1 line
2.54.0
RFC PATCH v6 24/25 sched/core: Execute enqueued balance callbacks after migrate_disable_switch
In reply to Yuri AndriaccioExecute balance callbacks after migrate_disable_switch.
Balancing may be requested on the __schedule path, in migrate_disable_switch, when the running task is throttled and then pushed away from its runqueue.
--- kernel/sched/core.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
kernel/sched/core.c +10 -2
@@ -2410,6 +2410,9 @@ do_set_cpus_allowed(struct task_struct *p, struct affinity_context *ctx);static void migrate_disable_switch (struct rq *rq,struct task_struct *p) { +struct rq_flags rf; +struct balance_callback *head; +struct affinity_context ac = { .new_mask =cpumask_of (rq->cpu), .flags =SCA_MIGRATE_DISABLE ,
@@ -2421,8 +2424,13 @@ static void migrate_disable_switch(struct rq *rq, struct task_struct *p)if (p->cpus_ptr != &p->cpus_mask)return ; - scoped_guard (task_rq_lock, p) -do_set_cpus_allowed (p, &ac); + rq =task_rq_lock (p, &rf); + +do_set_cpus_allowed (p, &ac); + + head =splice_balance_callbacks (rq); +task_rq_unlock (rq, p, &rf); +balance_callbacks (rq, head); }void ___migrate_enable (void )
--
2.54.0
RFC PATCH v6 25/25 Documentation: Update documentation for real-time cgroups
In reply to Yuri AndriaccioUpdate the RT_GROUP_SCHED specific documentation. Give a brief theoretical background for Hierarchical Constant Bandwidth Server (HCBS). Document how the HCBS is implemented in the kernel and how the RT_GROUP_SCHED behaves now compared to the version which this patchset replaces.
--- Documentation/scheduler/sched-rt-group.rst | 470 +++++++++++++++++---- 1 file changed, 393 insertions(+), 77 deletions(-)
Documentation/scheduler/sched-rt-group.rst +393 -77
@@ -53,9 +53,12 @@ CPU time is divided by means of specifying how much time can be spent running in a given period. We allocate this "run time" for each real-time group which the other real-time groups will not be permitted to use. -Any time not allocated to a real-time group will be used to run normal priority -tasks (SCHED_OTHER). Any allocated run time not used will also be picked up by -SCHED_OTHER. +Each real-time group runs at the same priority as SCHED_DEADLINE, thus they +share and contend the SCHED_DEADLINE allowed bandwidth. Any time not allocated +to a real-time group (and SCHED_DEADLINE tasks) will be used to run both +SCHED_FIFO/SCHED_RR, normal priority tasks (SCHED_OTHER), and SCHED_EXT tasks, +following the usual priorities. Any allocated run time not used will also be +picked up by the other scheduling classes, in the same order as before. Let's consider an example: a frame fixed real-time renderer must deliver 25 frames a second, which yields a period of 0.04s per frame. Now say it will also
@@ -73,10 +76,6 @@ The remaining CPU time will be used for user input and other tasks. Because real-time tasks have explicitly allocated the CPU time they need to perform their tasks, buffer underruns in the graphics or audio can be eliminated. -NOTE: the above example is not fully implemented yet. We still -lack an EDF scheduler to make non-uniform periods usable. - - 2. The Interface ================
@@ -86,105 +85,422 @@ lack an EDF scheduler to make non-uniform periods usable. The system wide settings are configured under the /proc virtual file system: -/proc/sys/kernel/sched_rt_period_us: +``/proc/sys/kernel/sched_rt_period_us``: The scheduling period that is equivalent to 100% CPU bandwidth. -/proc/sys/kernel/sched_rt_runtime_us: - A global limit on how much time real-time scheduling may use. This is always - less or equal to the period_us, as it denotes the time allocated from the - period_us for the real-time tasks. Without CONFIG_RT_GROUP_SCHED enabled, - this only serves for admission control of deadline tasks. With - CONFIG_RT_GROUP_SCHED=y it also signifies the total bandwidth available to - all real-time groups. +``/proc/sys/kernel/sched_rt_runtime_us``: + A global limit on how much time real-time scheduling may use (SCHED_DEADLINE + tasks + real-time groups). This is always less or equal to the period_us, as + it denotes the time allocated from the period_us for the real-time tasks. + Without **CONFIG_RT_GROUP_SCHED** enabled, this only serves for admission + control of deadline tasks. With **CONFIG_RT_GROUP_SCHED=y** it also signifies + the total bandwidth available to both real-time groups and deadline tasks. * Time is specified in us because the interface is s32. This gives an operating range from 1us to about 35 minutes. - * sched_rt_period_us takes values from 1 to INT_MAX. - * sched_rt_runtime_us takes values from -1 to sched_rt_period_us. - * A run time of -1 specifies runtime == period, ie. no limit. - * sched_rt_runtime_us/sched_rt_period_us > 0.05 inorder to preserve - bandwidth for fair dl_server. For accurate value check average of - runtime/period in /sys/kernel/debug/sched/fair_server/cpuX/ + * ``sched_rt_period_us`` takes values from 1 to INT_MAX. + * ``sched_rt_runtime_us`` takes values from -1 to ``sched_rt_period_us``. + * A run time of -1 specifies runtime == period, i.e., no limit, but also + disables admission tests for SCHED_DEADLINE. + +The default value for both ``sched_rt_period_us`` and ``sched_rt_runtime_us`` is +1000000 (or 1s), while fair-servers and ext-servers have a default runtime of +50ms and default period of 1s, giving a minimum of 0.05s to be used by +SCHED_FIFO/SCHED_RR and non-RT tasks (SCHED_OTHER, SCHED_EXT), while 0.95s are +the maximum to be used by SCHED_DEADLINE, and rt-cgroups if enabled. + +2.2 Cgroup settings +------------------- + +Enabling **CONFIG_RT_GROUP_SCHED** lets you explicitly allocate real CPU +bandwidth to task groups. + + .. warning:: + Real Time Cgroups are only available for cgroups-v2. + .. + +This uses the cgroup virtual file system and the CPU controller for cgroups. +Enabling the controller for the hierarchy creates two files: + +* ``<cgroup>/cpu.rt.max``, which specifies the runtime and period of the group. + The file also accepts a runtime of 'max', specifying that its tasks must be + scheduled using the nearest configured ancestor (or the root cgroup if it is + the nearest non-max ancestor). +* ``<cgroup>/cpu.rt.internal``, read-only, returns the runtime and period + actually allocated to the group, excluding that of its children. + + .. tip:: + For more information on working with control groups, you should read + *Documentation/admin-guide/cgroup-v2.rst*. + .. + +By default the root cgroup has the same period of +``/proc/sys/kernel/sched_rt_period_us``, which is 1s, and a runtime of zero, so +that rt-cgroup is *soft-disabled* by default, and all the runtime is available +for SCHED_DEADLINE tasks only. New groups instead get a period of zero and +runtime of 'max' (essentially delegating their tasks' scheduling to the nearest +configured ancestor). + +3. Theoretical Background +========================= + + + .. BIG FAT WARNING ****************************************************** + + .. warning:: + + This section contains a (not-thorough) summary on deadline/hierarchical + scheduling theory, and how it applies to real-time control groups. + The reader can "safely" skip to Section 4 if only interested in seeing + how the scheduling policy can be used. Anyway, we strongly recommend + to come back here and continue reading (once the urge for testing is + satisfied :P) to be sure of fully understanding all technical details. + + .. ************************************************************************ + +The real-time cgroup scheduler is based upon the **Hierarchical Constant +Bandwidth Server** (HCBS) [1] *Compositional Scheduling Framework* (CSF). A +**CSF** is a framework where global (system-level) timing properties can be +established by composing independently (specified and) analyzed local +(component-level) timing properties [5]. + +For HCBS (related to the Linux kernel), the compositional framework consists of +two parts: + +* The *scheduling components*, which are the basic units of the scheduling. In + the kernel these are the single cgroups along with the tasks that must be run + inside. + +* The *scheduling resources*, which are the CPUs of the machine. + +HCBS is a *hierarchical scheduling framework*, where the scheduling components +form a hierarchy and resources are allocated from parent components to its child +components in the hierarchy. + +The Chapter is organized as follows: **Section 3.1** gives basic real-time +theory definitions that are used throughout the whole section. **Section 3.2** +talks about the HCBS framework, giving a general idea on how this is structured. +**Section 3.3** introduces the MPR model, one of the many models which may be +used for the analysis of the scheduling components and the computation of the +minimum required scheduling resources for a given component. **Section 3.4** +shows the schedulability test for MPR on the HCBS framework. **Section 3.5** +shows how to convert a MPR interface to a HCBS compatible resource reservation +for a component. Finally, **Section 3.6** lists other interesting models which +could be used for the component analysis in HCBS. + +3.1 Basic Definitions +--------------------- +*We borrow the same definitions given in the* ``sched_deadline`` *document, which +are very briefly summarized here, and new ones, needed by the following content, +are added.* + +A typical real-time task is composed of a repetition of computation phases (task +instances, or jobs) which are activated on a periodic or sporadic fashion. For +our purposes, real-time tasks are characterized by three parameters: + +* Worst Case Execution Time (WCET): the maximum execution time among all jobs. +* Relative Deadline (D): the maximum time each job must be completed, relative + to the release time of the job. +* Inter-Arrival Period (P): the exact/minimum (for periodic/sporadic tasks) time + between each consecutive job. + +3.2 Hierarchical Constant Bandwidth Server (HCBS) [1] +----------------------------------------------------- + +As mentioned, HCBS is a *hierarchical scheduling framework*: + +* The framework hierarchy follows the same hierarchy of cgroups. Cgroups may + have two roles, either bandwidth reservation for children cgroups, or they may + be *live*, i.e. run tasks (but not both). The root cgroup, for the kernel's + implementation of HCBS, acts only as bandwidth reservation (but as written in + this document it has also different uses outside of the hierarchical + framework). +* The cgroup tree is internally flattened, for ease of scheduling, to a + two-level hierarchy, since only the *live* groups are of interest and all the + necessary information for their scheduling lies in their interface (there is + no need for the reservation components). +* The hierarchical framework, now on two levels, consists then of a first level + of cgroups, and a second level of tasks that are run inside these groups. +* The scheduling of components is performed using global Earliest Deadline First + (gEDF), SCHED_DEADLINE in the kernel, following the bandwidth reservation of + each group. +* Whenever a component is scheduled, a local scheduler picks which of the tasks + of the cgroup to run. The scheduling policy is global Fixed Priority (gFP), + SCHED_FIFO/SCHED_RR in the kernel. -2.2 Default behaviour ---------------------- +3.3 Multiprocessor Periodic Resource (MPR) model +------------------------------------------------ + +A Multiprocessor Periodic Resource (MPR) model [2] **u = <Pi, Theta, m'>** +specifies that an identical, unit-capacity multiprocessor platform collectively +provides **Theta** units of resource every **Pi** time units, where the +**Theta** time units are supplied with concurrency at most **m'**. + +This theoretical model is one of the many models that can abstract the +interface of our real-time cgroups: let **m'** be the number of CPUs of the +machine, let **Theta** be **m' * <cgroup>/cpu.rt_runtime_us** and **Pi** be +**<cgroup>/cpu.rt_period_us**. + +Let's introduce the concept of Supply Bound Function (SBF). A SBF is a function +which outputs a lower bound for the processor supply provided in a given time +interval, given a resource supply model. For a completely dedicated CPU, the SBF +function is simply the identity function, as it will always provide **t** units +of computation for an interval of length **t**. The situation gets slightly more +complicated for the MPR model or any of the other model listed in section 3.6. + +The **SBF(t)** for a MPR model **u = <Pi, Theta, m'>** is:: + + | 0 if t' < 0 + | + SBF_u(t) = | floor(t' / PI) * Theta + | + max(0, m' * x - (m' * Pi - Theta) if t' >= 0 and 1 <= x <= y + | + | floor(t' / PI) * Theta + | + max(0, m' * x - (m' * Pi - Theta) else + | - (m' - beta) + +where:: + + alpha = floor(Theta / m') + beta = Theta - m' * alpha + t' = t - (Pi - ceil(Theta / m')) + x = t' - (Pi * floor(t' / Pi)) + y = Pi - floor(Theta / m') + +Briefly, this function models that the server's bandwidth is given as late as +possible, so describing the worst case possible for the supplied bandwidth. + +3.4 Schedulability for MPR on global Fixed-Priority +--------------------------------------------------- + +Let's introduce the concept of Demand Bound Function (DBF). A DBF is a function +that, given a taskset, a scheduling algorithm and an interval of time, outputs +the worst resource demand for that interval of time. + +It is easy to see that, given a DBF and a SBF, we can deem a component/taskset +schedulable if, for every time interval t >= 0, it is possible to demonstrate +that: + + DBF(t) <= SBF(t) + +We have the Supply Bound Function for our given MPR model, so we are missing the +Demand Bound Function for a given taskset that is being scheduled using global +Fixed Priority. + +3.4.1 Schedulability Analysis for global Fixed Priority +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Bertogna, Cirinei and Lipari [6] have derived a schedulability test for global +Fixed Priority (gFP) on multi-processor platforms. In this test (called +*BCL_gFP* test) we can consider all the CPUs to be dedicated to the scheduling. + + A taskset **Tau** is schedulable with gFP on a multiprocessor platform + composed of **m'** identical processors if for each task **tau_k in Tau**: + + Sum(for i < k)( min(W_i(D_k), D_k - C_k + 1) ) < m' * (D_k - C_k + 1) + + where **W_i(t)** is the workload of task **tau_i** over a time interval **t**: + + W_i(t) = N_i(t) * C_i + min(C_i, t + D_i - C_i - N_i(t) * P_i) -The default values for sched_rt_period_us (1000000 or 1s) and -sched_rt_runtime_us (950000 or 0.95s). This gives 0.05s to be used by -SCHED_OTHER (non-RT tasks). These defaults were chosen so that a run-away -real-time tasks will not lock up the machine but leave a little time to recover -it. By setting runtime to -1 you'd get the old behaviour back. + and **N_i(t)** is the number of activations of task **tau_i** that complete in + a time interval **t**: -By default all bandwidth is assigned to the root group and new groups get the -period from /proc/sys/kernel/sched_rt_period_us and a run time of 0. If you -want to assign bandwidth to another group, reduce the root group's bandwidth -and assign some or all of the difference to another group. + N_i(t) = floor( (t + D_i - C_i) / P_i ) -Real-time group scheduling means you have to assign a portion of total CPU -bandwidth to the group before it will accept real-time tasks. Therefore you will -not be able to run real-time tasks as any user other than root until you have -done that, even if the user has the rights to run processes with real-time -priority! + while the **min** term is the contribution of the carried-out job in the + interval **t**, i.e. that job that does not completely fit in the interval + **t**, but starts inside the interval after all the jobs that complete. + +3.4.2 From BCL_gFP to the Demand Bound Function +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +We can then derive the DBF from this test: + + DBF_gFP(tau_k) = Sum(for i < k)( min(W_i(D_k), D_k - C_k + 1) ) + m' * (C_k - 1) + +Briefly, the first sum component, the same in the BCL_gFP test, describes the +maximum interference that higher priority task give to the analysed task. The +workload is upperbounded by ``(D_k - C_K + 1)`` because we are only interested +in the interference in the slack time, while for the ``C_k`` time we are +requiring that all the CPUs are fully available, as the single job needs `C_k` +(non overlapping) time units to run. + +The demand bound function from Bertogna et al. is only defined on a single time +(i.e. the deadline of the task in analysis) instead of all possible times as +this is the minimum argument to demonstrate schedulability on global Fixed +Priority. + +3.4.3 Putting it all togheter +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +A component **C**, on **m'** processors, running a taskset **Tau = { tau_1 = +(C_1, D_1, P_1), ..., tau_n = (C_n, D_n, P_n) }** of **n** sporadic tasks, is +schedulable under gFP using an MPR model **u = <Pi, Theta, m'>**, if for all +tasks **tau_k in Tau**: + + DBF_gFP(tau_k) <= SBF_u(D_K) + +3.5 From MPR to deadline servers +-------------------------------- + +Since there exist no algorithm to schedule MPR interfaces, a tecnique was +developed to transform MPR interfaces into periodic tasks, so that a +number of periodic servers which respect the tasks requirements can be used for +the scheduling of the MPR interface and associated tasks. + +Let **u = <Pi, Theta, m>** be a MPR interface, let **a = Theta - m * floor(Theta +/ m)**, let **k = floor(a)**. Define a transformation from **u** to a periodic +taskset **Tau_u = { tau_1 = (C_1, D_1, P_1), ..., tau_m' = (C_m', D_m', P_m') +}**, where: + + **tau_1 = ... = tau_k = (floor(Theta / m') + 1, Pi, Pi)** + + **tau_k+1 = (floor(Theta / m') + a - k * floor(a/k), Pi, Pi)** + + **tau_k+2 = ... = tau_m' = (floor(Theta / m'), Pi, Pi)** + +This periodic taskset of servers **Tau_u** can be scheduled on any number of +processors with concurrency at most **m'**. + +For real-time control groups, it is possible to just consider a slightly more +demanding taskset **Tau_u'**, where each task **tau_i** is defined as follows: + + **tau_i = (ceil(Theta / m'), Pi, Pi)** + +3.6 Other models +---------------- + +There exist many other theoretical models in literature which are used to +describe a hierarchical scheduling framework on multi-core architectures. +Notable examples are the Multi Supply Function (MSF) abstraction [3], the +Parallel Supply Function (PSF) abstraction [4] and the Bounded Delay +Multipartition (BDM) [7]. + +3.7 References +-------------- + 1 - L. Abeni, A. Balsini, and T. Cucinotta, “Container-based real-time + scheduling in the Linux kernel,” SIGBED Rev., vol. 16, no. 3, pp. 33-38, + Nov. 2019, doi: 10.1145/3373400.3373405. + 2 - A. Easwaran, I. Shin, and I. Lee, “Optimal virtual cluster-based + multiprocessor scheduling,” Real-Time Syst, vol. 43, no. 1, pp. 25-59, + Sept. 2009, doi: 10.1007/s11241-009-9073-x. + 3 - E. Bini, G. Buttazzo, and M. Bertogna, “The Multi Supply Function + Abstraction for Multiprocessors,” in 2009 15th IEEE International + Conference on Embedded and Real-Time Computing Systems and Applications, + Aug. 2009, pp. 294-302. doi: 10.1109/RTCSA.2009.39. + 4 - E. Bini, B. Marko, and S. K. Baruah, “The Parallel Supply Function + Abstraction for a Virtual Multiprocessor,” in Scheduling, S. Albers, S. K. + Baruah, R. H. Möhring, and K. Pruhs, Eds., in Dagstuhl Seminar Proceedings + (DagSemProc), vol. 10071. Dagstuhl, Germany: Schloss Dagstuhl - + Leibniz-Zentrum für Informatik, 2010, pp. 1-14. doi: + 10.4230/DagSemProc.10071.14. + 5 - I. Shin and I. Lee, “Compositional real-time scheduling framework,” in + 25th IEEE International Real-Time Systems Symposium, Dec. 2004, pp. 57-67. + doi: 10.1109/REAL.2004.15. + 6 - M. Bertogna, M. Cirinei, and G. Lipari, “Schedulability Analysis of Global + Scheduling Algorithms on Multiprocessor Platforms,” IEEE Transactions on + Parallel and Distributed Systems, vol. 20, no. 4, pp. 553-566, Apr. 2009, + doi: 10.1109/TPDS.2008.129. + 7 - G. Lipari and E. Bini, “A Framework for Hierarchical Scheduling on + Multiprocessors: From Application Requirements to Run-Time Allocation,” in + 2010 31st IEEE Real-Time Systems Symposium, Nov. 2010, pp. 249-258. doi: + 10.1109/RTSS.2010.12. + + +4. Using Real-Time cgroups +========================== + +4.1 CGroup Setup +---------------- +The following is a brief guide to the use of Real-Time Control Groups. -2.3 Basis for grouping tasks ----------------------------- +Of course, real-time control groups require mounting of the cgroup file system. +We have decided to only support cgroups v2, so make sure you mount the v2 +controller for the cgroup hierarchy. -Enabling CONFIG_RT_GROUP_SCHED lets you explicitly allocate real -CPU bandwidth to task groups. +Additionally the real-time cgroups require the CPU controller for the cgroups to +be enabled:: -This uses the cgroup virtual file system and "<cgroup>/cpu.rt_runtime_us" -to control the CPU time reserved for each control group. + # Assume the cgroup file system is mounted at /sys/fs/cgroup + > echo "+cpu" > /sys/fs/cgroup/cgroup.subtree_control -For more information on working with control groups, you should read -Documentation/admin-guide/cgroup-v1/cgroups.rst as well. +The CPU controller can only be mounted if there is no SCHED_FIFO/SCHED_RR task +scheduled in any cgroup other than the root control group. -Group settings are checked against the following limits in order to keep the -configuration schedulable: +The root control group has no bandwidth allocated by default, so make sure to +allocate some bandwidth so that it can be used by the other cgroups. More on +that in the following section... - \Sum_{i} runtime_{i} / global_period <= global_runtime / global_period +4.2 Bandwidth Allocation for groups +----------------------------------- -For now, this can be simplified to just the following (but see Future plans): +Allocating bandwidth to a cgroup is a fundamental step to run real-time +workload. The cgroup filesystem exposes two files: - \Sum_{i} runtime_{i} <= global_runtime +* ``<cgroup>/cpu.rt.max``: which specifies the cgroups' runtime and period in + microseconds. +* ``<cgroup>/cpu.rt.internal``: read-only, get the cgroups' actualy runtime and + period in microseconds, without its children's bandwidth. +By definition, the specified runtime must be always less than or equal to the +period. Additionally, an admission test checks if the bandwidth invariant is +respected (i.e. sum of children's bandwidth <= parent's bandwidth). -3. Future plans -=============== +The root control group files instead control and reserve the SCHED_DEADLINE +bandwidth allocated to real-time cgroups, since real-time groups compete and +share the same bandwidth allocated to SCHED_DEADLINE tasks. -There is work in progress to make the scheduling period for each group -("<cgroup>/cpu.rt_period_us") configurable as well. +4.3 Running real-time tasks in groups +------------------------------------- -The constraint on the period is that a subgroup must have a smaller or -equal period to its parent. But realistically its not very useful _yet_ -as its prone to starvation without deadline scheduling. +To run tasks in real-time groups it is just necessary to change a tasks +scheduling policy to SCHED_FIFO/SCHED_RR and migrate it into the group. If the +group is not allowed to run real-time tasks because of incorrect configuration, +either migrating a SCHED_FIFO/SCHED_RR task into the group or changing +scheduling policy to a task already inside the group will fail:: -Consider two sibling groups A and B; both have 50% bandwidth, but A's -period is twice the length of B's. + # assume there is a task of PID 42 running + # change its scheduling policy to SCHED_FIFO, priority 99 + > chrt -f -p 99 42 -* group A: period=100000us, runtime=50000us + # migrate the task to a cgroup + > echo 42 > /sys/fs/cgroup/<my-cgroup>/cgroup.procs - - this runs for 0.05s once every 0.1s +4.4 Special case: the root control group +---------------------------------------- -* group B: period= 50000us, runtime=25000us +The root cgroup is special, compared to the other cgroups, as its tasks are not +managed by the HCBS algorithm, rather they just use the original +SCHED_FIFO/SCHED_RR policies (as if CONFIG_RT_GROUP_SCHED was disabled). As +mentioned, its bandwidth files are just used to control how much of the +SCHED_DEADLINE bandwidth is allocated to cgroups. - - this runs for 0.025s twice every 0.1s (or once every 0.05 sec). +Any non-root cgroup configured as 'max' that has the root cgroup as its nearest +non-max ancestor will run its tasks in the root runqueue. -This means that currently a while (1) loop in A will run for the full period of -B and can starve B's tasks (assuming they are of lower priority) for a whole -period. +4.5 Guarantees and Special Behaviours +------------------------------------- -The next project will be SCHED_EDF (Earliest Deadline First scheduling) to bring -full deadline scheduling to the linux kernel. Deadline scheduling the above -groups and treating end of the period as a deadline will ensure that they both -get their allocated time. +Real-time cgroups are run at the same priority level of SCHED_DEADLINE tasks. +Since this is the highest priority scheduling policy, and since the Constant +Bandwidth Server (CBS) enforces that the specified bandwidth requirements for +both groups and tasks cannot be overrun, real-time groups have the same +guarantees that SCHED_DEADLINE tasks have, i.e. they will be necessarily +supplied by the amount of bandwidth requested (whenever the admission tests +pass). -Implementing SCHED_EDF might take a while to complete. Priority Inheritance is -the biggest challenge as the current linux PI infrastructure is geared towards -the limited static priority levels 0-99. With deadline scheduling you need to -do deadline inheritance (since priority is inversely proportional to the -deadline delta (deadline - now)). +This means that, since SCHED_FIFO/SCHED_RR tasks (scheduled in the root control +group) are not subject to bandwidth controls, they are run at a lower priority +than the cgroups' counterparts. Nonetheless, a minimum amount of bandwidth, if +reserved, will always be available to run SCHED_FIFO/SCHED_RR workloads in the +root cgroup, while they will be able to use more runtime if any of the +SCHED_DEADLINE tasks or servers use less than their specified amount of +bandwidth. SCHED_OTHER tasks are instead scheduled as normal, at lower priority +than real-time workloads. -This means the whole PI machinery will have to be reworked - and that is one of -the most complex pieces of code we have. +The aforementioned behaviour differs from the preceding RT_GROUP_SCHED +implementation, but this is necessary to give actual guarantees to the amount of +bandwidth given to rt-cgroups.
--
2.54.0
Juri Lelli
maintainerHi Yuri,
Thanks for sending this out.
Quoted 75 lines · Yuri Andriaccio
Hello,
This is the v6 for Hierarchical Constant Bandwidth Server, aiming at replacing the current
RT_GROUP_SCHEDmechanism with something more robust and theoretically sound. The patchset has been presented at OSPM25 and OSPM26
(https://retis.sssup.it/ospm-summit/), and a summary of its inner workings can be found at https://lwn.net/Articles/1021332/ . You can find the previous versions of this patchset at the bottom of the page, in particular version 1 which talks in more detail what this patchset is all about and how it is implemented.This v6 version works on the comments by the reviewers and introduces the following meaningful changes:
- Update to kernel version 7.1.
- Refactorings and general cleanups.
- Removal of substantial duplicated code.
- Express more locking constraints in code.
- New cpu.rt.max interface.
- Refactoring of migration code to reduce code duplication.
The new migration code now reuses the existing push/pull and similar functions and specializes where needed, substantially reducing the footprint of group migration code from previous versions.- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - New cgroup-v2 interface:
After extensive discussions with the kernel's maintainers, we have built a new interface to support HCBS scheduling. Since this will be a cgroup-v2 only feature (the fate of cgroup-v1 oldRT_GROUP_SCHEDhas yet to be decided), it was possible to drop the original v1 interface entirely and create a completely new one that is similar to those that are already existing.Every cgroup has now two new files:
- cpu.rt.max (similar to the cpu.max file)
- cpu.rt.internal (read-only, not available in the root cgroup, it may be removed if deemed unnecessary, see later for details)In this new interface, HCBS cgroups may either be set to use deadline servers, and thus reserving a specified amount of bandwidth, very similarly to the previous system, or can delegate their FIFO/RR tasks' scheduling to the nearest ancestor that it is configured (default on group creation). If the nearest configured ancestor is the root cgroup, tasks will be effectively run on the root runqueue even if their cgroup is not the root task group.
This means that subtrees are allowed to retain the original non-RT_GROUP_SCHED behaviour, scheduling on root, while the feature is nonetheless active. In the meantime other subtrees may use HCBS, and the whole hierarchy can coexist without issues.
This behaviour is specified in the cpu.rt.max file, which accepts the string "<runtime | 'max'> <period>". A zero runtime disables FIFO/RR scheduling for tasks in that group, a non-zero runtime creates a reservation and uses HCBS, a runtime of 'max' instead tells the scheduler to use the nearest configured ancestor for the FIFO/RR task scheduling.
The admission test now does not only check the immediate children of a cgroup for schedulability (recall that a group's bandwidth must be always greater than or equal to its children total bandwidth), but it has to check its whole
subtree: if a child delegates its tasks to its parents (runtime = 'max'), then this child's own children (the grandchildrens) are effectively viewed as immediate children that compete for the same bandwidth of their grandparent, and so on down the hierarchy.To support both threaded and domain cgroups, the original test that allowed only to run tasks in leaf cgroups has been removed: this is already enforced for domain cgroups by existing code, while this must not be the case for threaded cgroups.
Since groups in the middle of the hierarchy can now also run tasks, their
dl_serversmust be configured properly: a parent cgroupdl_serverscan only use their assigned bandwidth minus the total of their children. The cpu.rt.internal file reads exactly what is this "remainder" bandwidth. Sincedl_serversmust have a runtime and period values assigned, the period is taken from the user configured cpu.rt.max file and the runtime is computed from the remainder bw. This runtime and the period are the values shown by cpu.rt.internal.
Quoted 3 lines
Supporting both threaded and domain cgroups also dropped all the extra code related to active and 'live' cgroups as mentioned in previous RFCs.
I started playing with the new interface and ended up with the following
bash-5.3# cat cpu.rt.max (root)
10000 100000
bash-5.3# cat g1/cpu.rt.max
10000 100000
bash-5.3# cat g1/cpu.rt.internal
9999 100000
which looks odd to me, as nothing is running on g1 yet and no children groups either. Maybe a rounding error of some kind?
Thanks,
Juri
Hi Juri,
Thanks for looking into this.
Quoted 11 lines
I started playing with the new interface and ended up with the following
bash-5.3# cat cpu.rt.max (root)
10000 100000
bash-5.3# cat g1/cpu.rt.max
10000 100000
bash-5.3# cat g1/cpu.rt.internal
9999 100000which looks odd to me, as nothing is running on g1 yet and no children groups either. Maybe a rounding error of some kind?
You are right. I should have mentioned that it is just a rounding error that occurs when converting from a bandwidth value to a runtime value. This happens because the tg_rt_internal_bandwidth() function truncates the value when transforming the runtime from nanoseconds to micros. Rounding could be used here to report a more accurate value.
This same issue is probably found in the from_ratio() function, which has a similar truncation issue when converting from bandwidth to runtime, but since it is working in the nanoseconds range it might not be that big of a problem. The value from from_ratio() is used for the setup of the dl_servers even when the children bw is zero, so maybe it is possible to add a special case?
Anyways, as it is right now, the cpu.rt.internal may have only a +1/-1us error in reporting the actual used values, while the error for the runtime value used internally to setup the dl_servers is in the range of tens of nanoseconds.
Thanks,
Yuri
Juri Lelli
maintainerQuoted 29 lines · Yuri Andriaccio
Hi Juri,
Thanks for looking into this.
Quoted 11 lines
I started playing with the new interface and ended up with the following
bash-5.3# cat cpu.rt.max (root)
10000 100000
bash-5.3# cat g1/cpu.rt.max
10000 100000
bash-5.3# cat g1/cpu.rt.internal
9999 100000which looks odd to me, as nothing is running on g1 yet and no children groups either. Maybe a rounding error of some kind?
You are right. I should have mentioned that it is just a rounding error that occurs when converting from a bandwidth value to a runtime value. This happens because the
tg_rt_internal_bandwidth()function truncates the value when transforming the runtime from nanoseconds to micros. Rounding could be used here to report a more accurate value.This same issue is probably found in the
from_ratio()function, which has a similar truncation issue when converting from bandwidth to runtime, but since it is working in the nanoseconds range it might not be that big of a problem. The value fromfrom_ratio()is used for the setup of thedl_serverseven when the children bw is zero, so maybe it is possible to add a special case?
Quoted 4 lines
Anyways, as it is right now, the cpu.rt.internal may have only a +1/-1us error in reporting the actual used values, while the error for the runtime value used internally to setup the
dl_serversis in the range of tens of nanoseconds.
Not a huge problem per se, but it will raise some eyebrows (and generate questions) if we leave things as is, I fear.
I wonder if, instead of converting to bandwidth ratios and back (losing precision in both directions), we can compute children's runtime sum directly in nanoseconds. For children with different periods, we can maybe normalize (128-bit intermediate?). Parent's internal runtime is then a simple exact
subtraction: parent_runtime - children_runtime_sum. This should reduce precision loss from double conversions. Also, as you suggest as well, apply rounding when displaying to user.
Juri Lelli
maintainerRe: [RFC PATCH v6 12/25] sched/rt: Add {alloc/unregister/free}_rt_sched_group
In reply to Yuri AndriaccioHello,
Quoted 16 lines · Yuri Andriaccio
Add allocation and deallocation code for rt-cgroups.
Declare
dl_serverspecific functions (only skeleton, but no implementation yet), needed by the deadline servers to be called when trying to schedule.Initialize a cgroup's active context to that of its parent.
Co-developed-by Alessio Balsini <[email protected]>Signed-off-by Alessio Balsini <[email protected]>Co-developed-by Andrea Parri <[email protected]>Signed-off-by Andrea Parri <[email protected]>Co-developed-by luca abeni <[email protected]>Signed-off-by luca abeni <[email protected]>Signed-off-by Yuri Andriaccio <[email protected]>---
...
Quoted 19 lines
void
free_rt_sched_group(struct task_group *tg)
{kernel/sched/rt.c +15
-0+int i; +unsigned long flags; +if (!rt_group_sched_enabled ())return ; + +if (!tg->dl_se || !tg->rt_rq) +return ; + +for_each_possible_cpu (i) { +if (!tg->dl_se[i] || !tg->rt_rq[i]) +continue ; + +/* +* Shutdown the dl_server and free it +* +* Since the dl timer is going to be cancelled,
kernel/sched/rt.c struct dl_bandwidth *dl_bandwidth_write(struct task_group *tg) · line 168
Quoted 10 lines
kernel/sched/rt.c +10
-0+* we risk to never decrease the running bw... +* Fix this issue by changing the group runtime +* to 0 immediately before freeing it. +*/ +if (tg->dl_se[i]->dl_runtime) +dl_init_tg (tg->dl_se[i],0 , tg->dl_se[i]->dl_period); + +raw_spin_rq_lock_irqsave (cpu_rq (i), flags); +hrtimer_cancel (&tg->dl_se[i]->dl_timer); +raw_spin_rq_unlock_irqrestore (cpu_rq (i), flags);
Why do we need to grab rq lock here? I actually fear this can deadlock with the timer callback.
Quoted 6 lines
kernel/sched/rt.c +6
-0+kfree (tg->dl_se[i]); + +/* Free the local per-cpu runqueue */ +kfree (rq_of_rt_rq (tg->rt_rq[i])); + } +
kernel/sched/rt.c struct dl_bandwidth *dl_bandwidth_write(struct task_group *tg) · line 184
Quoted 10 lines
kernel/sched/rt.c +8
-0+kfree (tg->rt_rq); +kfree (tg->dl_se); } +static inline void __rt_rq_free (struct rt_rq **rt_rq) +{ +int i; + +for_each_possible_cpu (i) { +kfree (rq_of_rt_rq (rt_rq[i]));
^^
Can this result in NULL pointer deref if __alloc_rt_sched_group_data() fails for some reason midway in the CPU loop?
Quoted 47 lines
kernel/sched/rt.c +47
-0+ } + +kfree (rt_rq); +} + +DEFINE_FREE(rt_rq_free, struct rt_rq **, if (_T) __rt_rq_free(_T)) + +static inline void __dl_se_free (struct sched_dl_entity **dl_se) +{ +int i; + +for_each_possible_cpu (i) { +kfree (dl_se[i]); + } + +kfree (dl_se); +} + +DEFINE_FREE(dl_se_free, struct sched_dl_entity **, if (_T) __dl_se_free(_T)) + +static int __alloc_rt_sched_group_data (struct task_group *tg) { +/* Instantiate automatic cleanup in event of kalloc fail */ + struct rt_rq **tg_rt_rq __free(rt_rq_free) = NULL; + struct sched_dl_entity **tg_dl_se __free(dl_se_free) = NULL; + struct sched_dl_entity *dl_se __free(kfree) = NULL; + struct rq *s_rq __free(kfree) = NULL; +int i; + + tg_rt_rq =kcalloc (nr_cpu_ids,sizeof (struct rt_rq *),GFP_KERNEL ); +if (!tg_rt_rq) +return 0 ; + + tg_dl_se =kcalloc (nr_cpu_ids, +sizeof (struct sched_dl_entity *),GFP_KERNEL ); +if (!tg_dl_se) +return 0 ; + +for_each_possible_cpu (i) { + s_rq =kzalloc_node (sizeof (struct rq ), +GFP_KERNEL ,cpu_to_node (i)); +if (!s_rq) +return 0 ; + + dl_se =kzalloc_node (sizeof (struct sched_dl_entity ), +GFP_KERNEL ,cpu_to_node (i)); +if (!dl_se) +return 0 ;
kernel/sched/rt.c struct dl_bandwidth *dl_bandwidth_write(struct task_group *tg) · line 241
Quoted 10 lines
kernel/sched/rt.c +10
-0+ + tg_rt_rq[i] = &no_free_ptr (s_rq)->rt; + tg_dl_se[i] =no_free_ptr (dl_se); + } + + tg->rt_rq =no_free_ptr (tg_rt_rq); + tg->dl_se =no_free_ptr (tg_dl_se); + +return 1 ; +}
...
Thanks,
Juri
Tejun Heo
maintainerHello,
Looks great. Two things:
1. cpu.rt.internal doesn't follow the naming convention. The file is the cgroup's own budget (cpu.rt.max minus its children), so cpu.rt.max.effective.local fits better: .effective like cpuset.cpus.effective, .local like memory.events.local.
2. root's cpu.rt.max: sched_rt_runtime_us already caps total DL/RT bandwidth and rt-cgroups admit against the same pool, so what does reserving the cgroup share separately at root add? It's also a writable control on root, which we otherwise keep off root.
Thanks.
--
tejun
Hi,
On Mon, 15 Jun 2026 10:38:54 -1000
Tejun Heo <[email protected]> wrote:
[...]
Quoted 4 lines
2. root's cpu.rt.max:
sched_rt_runtime_usalready caps total DL/RT bandwidth and rt-cgroups admit against the same pool, so what does reserving the cgroup share separately at root add? It's also a
writable control on root, which we otherwise keep off root.
Well, /proc/sys/kernel/sched_rt_{runtime,period}_us control the total fraction of CPU time that can be reserved for dl and rt tasks; here, we need a way to reserve a part of this fraction for rt cgropus only. We need this because "regular" dl entities (the ones serving dl tasks) are "global" (can migrate between different CPU cores), whereas dl entities serving rt cgroups are pinned to their own CPU cores.
Combining the admission tests for these two different kinds of dl entities is not simple, so we reserve a fixed fraction of CPU time for rt cgroups, and the remaining part of sched_rt_runtime_us/sched_rt_period_us is left to dl tasks.
If there is a better interface we can use to achieve this goal, we are happy to switch to it.
Thanks,
Luca
Juri Lelli
maintainerHi Yuri,
Quoted 11 lines · Yuri Andriaccio
Hello,
This is the v6 for Hierarchical Constant Bandwidth Server, aiming at replacing the current
RT_GROUP_SCHEDmechanism with something more robust and theoretically sound. The patchset has been presented at OSPM25 and OSPM26
(https://retis.sssup.it/ospm-summit/), and a summary of its inner workings can be found at https://lwn.net/Articles/1021332/ . You can find the previous versions of this patchset at the bottom of the page, in particular version 1 which talks in more detail what this patchset is all about and how it is implemented.
Quoted 11 lines
This v6 version works on the comments by the reviewers and introduces the following meaningful changes:
- Update to kernel version 7.1.
- Refactorings and general cleanups.
- Removal of substantial duplicated code.
- Express more locking constraints in code.
- New cpu.rt.max interface.
- Refactoring of migration code to reduce code duplication.
The new migration code now reuses the existing push/pull and similar functions and specializes where needed, substantially reducing the footprint of group migration code from previous versions.
I've been working on a simple demo and benchmark suite for HCBS to explore real-world like use cases and characterize the feature's behavior. Different angle wrt your unit test suite (I believe).
The demo is available at:
https://github.com/jlelli/hcbs-demo
The demo models three scenarios — Industrial PLC Convergence, Robotics Compute Platform, and Precision Motion Control — each with multiple cooperating SCHED_FIFO tasks at different priorities sharing a cgroup, which is the key differentiator vs. plain SCHED_DEADLINE. An aggressor subsystem overloads the system while HCBS contains it to its budget.
A side-by-side compare mode runs baseline and HCBS simultaneously on separate cpuset partitions, with a live terminal dashboard showing the contrast in real time.
Key findings from testing on an Intel Xeon Gold 6433N (4 isolated CPUs via cpuset partition):
- At 10ms task periods, HCBS provides perfect temporal isolation: zero victim deadline misses across all scenarios, while aggressors are correctly throttled to their budget.
- At 1ms task periods, the dl-server period is the critical tuning parameter, less the bandwidth. A 10ms dl-server with 60% bandwidth caused ~10% miss rates because the worst-case throttle gap (4ms) spanned multiple 1ms deadlines. Switching to a 2ms dl-server period at just 30% bandwidth eliminated all misses.
- A simple Rule of thumb might be to set the dl-server period to at most 2x the shortest task period in the cgroup (e.g., 2ms dl-server for 1ms tasks, 10ms for 10ms tasks). Would you (and Luca?) agree or would you suggest something different?
- dl-server overhead itself appears negligible: a parameter sweep confirmed zero misses for a single task at all bandwidth/period combinations tested.
- The current v6 has been quite stable throughout my testing — no warnings, no crashes, and the bandwidth isolation works as expected across all the scenarios and workload combinations I've tried.
The demo supports three workload backends:
- rt-app (default): synthetic periodic tasks with configurable periods
- RT-Bench TACLeBench (--rtbench): real algorithms (susan, dijkstra, FFT, mpeg2, etc.) matching each scenario's computation profile
- stress-ng (--stress): mixed CPU/IO/cache/memory aggressors for realistic interference patterns
Recorded demos (asciinema):
Industrial: https://asciinema.org/a/5e4BMdWxS7hmm4hI
Robotics: https://asciinema.org/a/Msj48XnJGgcCev7M
Precision: https://asciinema.org/a/56WC3bu7yrcQe9nz
Thanks for the great work on this patchset. Happy to hear if this demo is of any interest, discuss any of the findings and/or understand if/how this can be further expanded.
Best,
Juri
Hi Juri,
very interesting demo, thanks!
On Tue, 23 Jun 2026 12:15:08 +0200
Juri Lelli <[email protected]> wrote:
[...]
Quoted 10 lines
- At 1ms task periods, the dl-server period is the critical tuning parameter, less the bandwidth. A 10ms dl-server with 60% bandwidth caused ~10% miss rates because the worst-case throttle gap (4ms) spanned multiple 1ms deadlines. Switching to a 2ms dl-server period at just 30% bandwidth eliminated all misses.
- A simple Rule of thumb might be to set the dl-server period to at most 2x the shortest task period in the cgroup (e.g., 2ms dl-server for 1ms tasks, 10ms for 10ms tasks). Would you (and Luca?) agree or would you suggest something different?
With one single RT task in the cgroup (or with multiple synchronized RT tasks having the same period), I agree... Technically, the cgroup period P should be such that P - Q = T - WCET (where "Q" is the cgroup's runtime and "T" is the period of the task), but to see missed deadlines you need a relevant competing deadline (or HCBS) workload.
So, yes, I agree with your findings above.
If we consider multi-core analysis, or multiple RT threads with different, non synchronized, periods, then analysis tool by Yuri (leveraging CSF analysis from real-time literature) is needed... But that is pretty pessimistic. The rule you suggest above is a better starting point in practical situations.
Luca
Juri Lelli
maintainerHi Luca,
Quoted 18 lines · luca abeni
Hi Juri,
very interesting demo, thanks!
On Tue, 23 Jun 2026 12:15:08 +0200
Juri Lelli <[email protected]> wrote:
[...]Quoted 10 lines
- At 1ms task periods, the dl-server period is the critical tuning parameter, less the bandwidth. A 10ms dl-server with 60% bandwidth caused ~10% miss rates because the worst-case throttle gap (4ms) spanned multiple 1ms deadlines. Switching to a 2ms dl-server period at just 30% bandwidth eliminated all misses.
- A simple Rule of thumb might be to set the dl-server period to at most 2x the shortest task period in the cgroup (e.g., 2ms dl-server for 1ms tasks, 10ms for 10ms tasks). Would you (and Luca?) agree or would you suggest something different?
Quoted 7 lines
With one single RT task in the cgroup (or with multiple synchronized RT tasks having the same period), I agree... Technically, the cgroup period P should be such that P - Q = T - WCET (where "Q" is the cgroup's runtime and "T" is the period of the task), but to see missed deadlines you need a relevant competing deadline (or HCBS) workload.
So, yes, I agree with your findings above.
Great, thanks a lot for taking a look!
Quoted 5 lines
If we consider multi-core analysis, or multiple RT threads with different, non synchronized, periods, then analysis tool by Yuri (leveraging CSF analysis from real-time literature) is needed... But that is pretty pessimistic. The rule you suggest above is a better starting point in practical situations.
Indeed. I actually wonder if it would make sense to "extract" that tool from the test suite and package it somehow so that it's easier for end users to design their interfaces.
Best,
Juri