evl/sched: Add sched_out handler to sched_class
PATCH 1/2 evl/sched: Add sched_out handler to sched_class
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the sched_pick may not be invoked when a higher-weighted class is providing the next thread.
--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)
include/evl/sched.h +2 -0
@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);
kernel/evl/sched/core.c +5 -0
@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;
@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false; + prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code
Signature · 1 line
2.47.3
PATCH 2/2 evl/sched/quota: Correct budget tracking for preempted threads
From: Jan Kiszka <[email protected]>
This was so far not possible due to a missing sched_class callback, causing threads being credited for the preemption time of threads from higher-weighted classes.
--- kernel/evl/sched/quota.c | 56 +++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 15 deletions(-)
kernel/evl/sched/quota.c +41 -15
@@ -187,11 +187,28 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *raw_spin_unlock (&rq->lock); } +static void charge_usage (struct evl_quota_group *tg,ktime_t now) +{ +ktime_t elapsed; + + elapsed =ktime_sub (now, tg->run_start); +if (elapsed < tg->run_budget) + tg->run_budget =ktime_sub (tg->run_budget, elapsed); +else + tg->run_budget =0 ; +} +static void quota_limit_handler (struct evl_timer *timer)/* oob stage stalled */ { +struct evl_quota_group *tg;struct evl_rq *rq; rq = container_of(timer, struct evl_rq, quota.limit_timer); + + tg = rq->curr->quota; +if (tg) +charge_usage (tg,evl_ktime_monotonic ()); +/* * Force a rescheduling on the return path of the current * interrupt, so that the budget is re-evaluated for the
@@ -253,6 +270,8 @@ static bool quota_setparam(struct evl_thread *thread,/* Dequeued earlier by our caller. */ list_del (&thread->quota_next); thread->quota->nr_threads--; + }else if (thread ==evl_current ()) { + tg->run_start =evl_ktime_monotonic (); } thread->quota = tg;list_add (&thread->quota_next, &tg->members);
@@ -400,21 +419,11 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)struct evl_thread *next, *curr = rq->curr;struct evl_sched_quota *qs = &rq->quota;struct evl_quota_group *otg, *tg; -ktime_t now, elapsed; +ktime_t now; now =evl_ktime_monotonic (); otg = curr->quota; -if (otg ==NULL ) - goto pick; -/* -* Charge the time consumed by the outgoing thread to the -* group it belongs to. -*/ - elapsed =ktime_sub (now, otg->run_start); -if (elapsed < otg->run_budget) - otg->run_budget =ktime_sub (otg->run_budget, elapsed); -else - otg->run_budget =0 ; + pick: next =evl_get_schedq (&rq->fifo.runnable);if (next ==NULL ) {
@@ -430,8 +439,6 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)if (tg ==NULL )return next; - tg->run_start = now; -/* * Don't consider budget if kicked, we have to allow this * thread to run until it eventually switches to in-band
@@ -448,9 +455,12 @@ static struct evl_thread *quota_pick(struct evl_rq *rq) goto pick; } -if (otg == tg &&evl_timer_is_running (&qs->limit_timer)) +if (otg != tg) { + tg->run_start = now; + }else if (evl_timer_is_running (&qs->limit_timer)) {/* Same group, leave the running timer untouched. */ goto out; + }/* Arm limit timer for the new running group. */ evl_start_timer(&qs->limit_timer,
@@ -462,6 +472,21 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)return next; } +static void quota_out (struct evl_thread *thread,struct evl_thread *next) +{ +struct evl_quota_group *otg = thread->quota; +struct evl_quota_group *ntg = next->quota; +ktime_t now; + +if (otg && otg != ntg) { +if (ntg) + now = ntg->run_start; +else + now =evl_ktime_monotonic (); +charge_usage (otg, now); + } +} +static void quota_migrate (struct evl_thread *thread,struct evl_rq *rq) {union evl_sched_param param;
@@ -786,6 +811,7 @@ struct evl_sched_class evl_sched_quota = { .sched_dequeue = quota_dequeue, .sched_requeue = quota_requeue, .sched_pick = quota_pick, + .sched_out = quota_out, .sched_migrate = quota_migrate, .sched_chkparam = quota_chkparam, .sched_setparam = quota_setparam,
Signature · 1 line
2.47.3
Re: [PATCH 2/2] evl/sched/quota: Correct budget tracking for preempted threads
Quoted 101 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This was so far not possible due to a missing
sched_classcallback, causing threads being credited for the preemption time of threads from higher-weighted classes.Signed-off-by Jan Kiszka <[email protected]>--- kernel/evl/sched/quota.c | 56 +++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 15 deletions(-)kernel/evl/sched/quota.c +25
-15@@ -187,11 +187,28 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *raw_spin_unlock (&rq->lock); } +static void charge_usage (struct evl_quota_group *tg,ktime_t now) +{ +ktime_t elapsed; + + elapsed =ktime_sub (now, tg->run_start); +if (elapsed < tg->run_budget) + tg->run_budget =ktime_sub (tg->run_budget, elapsed); +else + tg->run_budget =0 ; +} +static void quota_limit_handler (struct evl_timer *timer)/* oob stage stalled */ { +struct evl_quota_group *tg;struct evl_rq *rq; rq = container_of(timer, struct evl_rq, quota.limit_timer); + + tg = rq->curr->quota; +if (tg) +charge_usage (tg,evl_ktime_monotonic ()); +/* * Force a rescheduling on the return path of the current * interrupt, so that the budget is re-evaluated for the @@ -253,6 +270,8 @@ static bool quota_setparam(struct evl_thread *thread,/* Dequeued earlier by our caller. */ list_del (&thread->quota_next); thread->quota->nr_threads--; + }else if (thread ==evl_current ()) { + tg->run_start =evl_ktime_monotonic (); } thread->quota = tg;list_add (&thread->quota_next, &tg->members);@@ -400,21 +419,11 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)struct evl_thread *next, *curr = rq->curr;struct evl_sched_quota *qs = &rq->quota;struct evl_quota_group *otg, *tg; -ktime_t now, elapsed; +ktime_t now; now =evl_ktime_monotonic (); otg = curr->quota; -if (otg ==NULL ) - goto pick; -/* -* Charge the time consumed by the outgoing thread to the -* group it belongs to. -*/ - elapsed =ktime_sub (now, otg->run_start); -if (elapsed < otg->run_budget) - otg->run_budget =ktime_sub (otg->run_budget, elapsed); -else - otg->run_budget =0 ; + pick: next =evl_get_schedq (&rq->fifo.runnable);if (next ==NULL ) {@@ -430,8 +439,6 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)if (tg ==NULL )return next; - tg->run_start = now; -/* * Don't consider budget if kicked, we have to allow this * thread to run until it eventually switches to in-band @@ -448,9 +455,12 @@ static struct evl_thread *quota_pick(struct evl_rq *rq) goto pick; } -if (otg == tg &&evl_timer_is_running (&qs->limit_timer)) +if (otg != tg) { + tg->run_start = now; + }else if (evl_timer_is_running (&qs->limit_timer)) {/* Same group, leave the running timer untouched. */ goto out; + }/* Arm limit timer for the new running group. */ evl_start_timer(&qs->limit_timer,
kernel/evl/sched/quota.c static struct evl_thread *quota_pick(struct evl_rq *rq) · line 481
Quoted 10 lines
kernel/evl/sched/quota.c +7
-0@@ -462,6 +472,21 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)return next; } +static void quota_out (struct evl_thread *thread,struct evl_thread *next) +{ +struct evl_quota_group *otg = thread->quota; +struct evl_quota_group *ntg = next->quota; +ktime_t now; + +if (otg && otg != ntg) {
otg can't be NULL by construction since quota_out() is called for thread->sched_class.
Signature · 1 line
Philippe.
Quoted 39 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 10 lines
kernel/evl/sched/core.c +4
-0@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false; + prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code
I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
Signature · 1 line
Philippe.
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;
Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in __evl_schedule() is merely cosmetic, no functional change).
commit f49dbd63c1389a6b99508ef0da852545b102d1cc (HEAD -> wip/fix-quota-sched)
Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200
evl: sched/quota: fix budget tracking on preemption
Upon preemption of a SCHED_QUOTA thread by a SCHED_FIFO one, the runtime budget of the former is inaccurately tracked. This is due to the fifo->pick() handler returning a valid thread, which prevents the quota->pick() handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.
To fix this, we introduce a new sched_out() handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue with SCHED_FIFO.
include/evl/sched.h +1 -0
@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);
include/evl/sched/quota.h +1 -0
@@ -40,6 +40,7 @@ struct evl_quota_group {struct evl_sched_quota {ktime_t period; +struct evl_sched_queue runnable;struct evl_timer refill_timer;struct evl_timer limit_timer;struct list_head groups;
kernel/evl/sched/core.c +13 -7
@@ -773,7 +773,8 @@ static inline void set_next_running(struct evl_rq *rq,evl_stop_timer (&rq->rrbtimer); } -static struct evl_thread *__pick_next_thread (struct evl_rq *rq) +static __always_inline struct evl_thread * +__pick_next_thread (struct evl_rq *rq) {struct evl_sched_class *sched_class;struct evl_thread *curr = rq->curr;
@@ -821,8 +822,14 @@ static struct evl_thread *__pick_next_thread(struct evl_rq *rq)/* rq->curr->lock + rq->lock held, hard irqs off. */ static struct evl_thread *pick_next_thread (struct evl_rq *rq) { -struct evl_thread *next =__pick_next_thread (rq); +struct evl_thread *next, *prev = rq->curr; +struct evl_sched_class *prev_class = prev->sched_class; +if (prev_class->sched_out) + prev_class->sched_out (prev); + + next =__pick_next_thread (rq); +trace_evl_pick_thread (next);set_next_running (rq, next);return next;
@@ -972,21 +979,20 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */return ; } + prev = curr; next =pick_next_thread (this_rq); -trace_evl_pick_thread (next); -if (next == curr) { -if (unlikely (next->state &EVL_T_ROOT )) { +if (next == prev) { +if (unlikely (prev->state &EVL_T_ROOT )) {if (this_rq->local_flags &RQ_TPROXY )evl_notify_proxy_tick (this_rq);if (this_rq->local_flags &RQ_TDEFER )evl_program_local_tick (&evl_mono_clock); }raw_spin_unlock (&this_rq->lock); -raw_spin_unlock_irqrestore (&curr->lock, flags); +raw_spin_unlock_irqrestore (&prev->lock, flags);return ; } - prev = curr; this_rq->curr = next; leaving_inband = false;
kernel/evl/sched/quota.c +99 -104
383 lines not on this page · raw message
Signature · 1 line
Philippe.
Quoted 562 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO.Signed-off-by Philippe Gerum <[email protected]>include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);include/evl/sched/quota.h +1
-0@@ -40,6 +40,7 @@ struct evl_quota_group {struct evl_sched_quota {ktime_t period; +struct evl_sched_queue runnable;struct evl_timer refill_timer;struct evl_timer limit_timer;struct list_head groups;kernel/evl/sched/core.c +13
-7@@ -773,7 +773,8 @@ static inline void set_next_running(struct evl_rq *rq,evl_stop_timer (&rq->rrbtimer); } -static struct evl_thread *__pick_next_thread (struct evl_rq *rq) +static __always_inline struct evl_thread * +__pick_next_thread (struct evl_rq *rq) {struct evl_sched_class *sched_class;struct evl_thread *curr = rq->curr;@@ -821,8 +822,14 @@ static struct evl_thread *__pick_next_thread(struct evl_rq *rq)/* rq->curr->lock + rq->lock held, hard irqs off. */ static struct evl_thread *pick_next_thread (struct evl_rq *rq) { -struct evl_thread *next =__pick_next_thread (rq); +struct evl_thread *next, *prev = rq->curr; +struct evl_sched_class *prev_class = prev->sched_class; +if (prev_class->sched_out) + prev_class->sched_out (prev); + + next =__pick_next_thread (rq); +trace_evl_pick_thread (next);set_next_running (rq, next);return next;@@ -972,21 +979,20 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */return ; } + prev = curr; next =pick_next_thread (this_rq); -trace_evl_pick_thread (next); -if (next == curr) { -if (unlikely (next->state &EVL_T_ROOT )) { +if (next == prev) { +if (unlikely (prev->state &EVL_T_ROOT )) {if (this_rq->local_flags &RQ_TPROXY )evl_notify_proxy_tick (this_rq);if (this_rq->local_flags &RQ_TDEFER )evl_program_local_tick (&evl_mono_clock); }raw_spin_unlock (&this_rq->lock); -raw_spin_unlock_irqrestore (&curr->lock, flags); +raw_spin_unlock_irqrestore (&prev->lock, flags);return ; } - prev = curr; this_rq->curr = next; leaving_inband = false;kernel/evl/sched/quota.c +98
-104@@ -12,45 +12,35 @@#include <uapi/evl/sched-abi.h> /* -* With this policy, each per-CPU runqueue maintains a list of active -* thread groups for the sched_fifo class. -* -* Each time a thread is picked from the runqueue, we check whether we -* still have budget for running it, looking at the group it belongs -* to. If so, a timer is armed to elapse when that group has no more -* budget, would the incoming thread run unpreempted until then -* (i.e. evl_quota->limit_timer). +* Each time a thread is picked from the ->runnable queue, we check +* whether the group it belongs to still has runtime budget. If so, a +* timer is armed to fire when that group has no more budget, would +* the incoming thread run unpreempted until then +* (i.e. quota->limit_timer). * * Otherwise, if no budget remains in the group for running the * candidate thread, we move the latter to a local expiry queue * maintained by the group. This process is done on the fly as we pull -* from the runqueue. +* from the ->runnable queue. * -* Updating the remaining budget is done each time the EVL core asks -* for replacing the current thread with the next runnable one, -* i.e. evl_quota_pick(). There we charge the elapsed run time of the -* outgoing thread to the relevant group, and conversely, we check -* whether the incoming thread has budget. +* Updating the remaining budget is done each time the EVL core +* schedules out a thread undergoing the quota scheduling policy, * -* Finally, a per-CPU timer (evl_quota->refill_timer) periodically -* ticks in the background, in accordance to the defined quota -* interval. Thread group budgets get replenished by its handler in -* accordance to their respective share, pushing all expired threads -* back to the run queue in the same move. +* Finally, a per-CPU timer (quota->refill_timer) periodically ticks +* in the background, in accordance to the defined quota interval, +* replenishing per-group budgets, pushing all expired threads back to +* the quota ->runqueue too. * -* NOTE: since the core logic enforcing the budget entirely happens in -* evl_quota_pick(), applying a budget change can be done as simply as -* forcing the rescheduling procedure to be invoked asap. As a result -* of this, the EVL core will ask for the next thread to run, which -* means calling evl_quota_pick() eventually. +* NOTE: forcing a call to the rescheduling procedure is enoiugh to +* apply a budget change. * -* CAUTION: evl_quota_group->nr_active does count both the threads -* from that group linked to the sched_fifo runqueue, _and_ the -* threads moved to the local expiry queue. As a matter of fact, the -* expired threads - those for which we consumed all the per-group -* budget - are still seen as runnable (i.e. not blocked/suspended) by -* the EVL core. This only means that the SCHED_QUOTA policy won't -* pick them until the corresponding budget is replenished. +* CAUTION: quota_group->nr_active does count both the threads from +* that group linked to the runnable queue, _and_ the threads moved to +* the local expiry queue. As a matter of fact, the expired threads - +* those for which we consumed all the per-group budget - are still +* seen as runnable (i.e. not blocked/suspended) by the EVL core. This +* only means that the SCHED_QUOTA policy won't pick them until the +* corresponding budget is replenished. */ #define MAX_QUOTA_GROUPS 1024@@ -61,15 +51,19 @@ static DECLARE_BITMAP(group_map, MAX_QUOTA_GROUPS);static LIST_HEAD (group_list); -static inline bool thread_on_quota (struct evl_thread *thread, -struct evl_quota_group *tg) +static inline bool current_on_quota (struct evl_quota_group *tg) { -/* -* Check whether @thread is running on some CPU, and belongs -* to quota group @tg. -*/ -return thread->quota == tg && - !(thread->state & (EVL_T_READY |EVL_THREAD_BLOCK_MASK )); +struct evl_rq *rq = tg->rq; +struct evl_thread *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; + +if (curr->quota != tg) +return false; + +if (curr->state & (EVL_T_READY |EVL_T_KICKED |EVL_THREAD_BLOCK_MASK )) +return false; + +return evl_timer_is_running (&qs->limit_timer); }static inline bool group_is_active (struct evl_quota_group *tg)@@ -82,7 +76,7 @@ static inline bool group_is_active(struct evl_quota_group *tg)* runqueue, in which case tg->nr_active already accounted for * it. */ -return thread_on_quota (tg->rq->curr, tg); +return current_on_quota (tg); } static inline void replenish_budget(struct evl_sched_quota *qs,@@ -134,10 +128,10 @@ static inline void replenish_budget(struct evl_sched_quota *qs, }else if (tg->run_credit) { credit =ktime_sub (tg->quota_peak, budget);/* Consume the accumulated credit. */ -if (tg->run_credit >= credit) +if (tg->run_credit >= credit) { tg->run_credit =ktime_sub (tg->run_credit, credit); -else { + }else { credit = tg->run_credit; tg->run_credit =0 ; }@@ -150,8 +144,8 @@ static inline void replenish_budget(struct evl_sched_quota *qs,static void quota_refill_handler (struct evl_timer *timer)/* oob stage stalled */ { -struct evl_quota_group *tg;struct evl_thread *thread, *tmp; +struct evl_quota_group *tg;struct evl_sched_quota *qs;struct evl_rq *rq;@@ -167,7 +161,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *if (tg->run_budget ==0 ||list_empty (&tg->expired))continue ;/* -* For each group living on this CPU, move all expired +* For each group pinned on this CPU, move all expired * threads back to the runqueue. Since those threads * were moved out of the runqueue as we were * considering them for execution, we push them back @@ -178,7 +172,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }@@ -195,7 +189,7 @@ static void quota_limit_handler(struct evl_timer *timer) /* oob stage stalled *//* * Force a rescheduling on the return path of the current * interrupt, so that the budget is re-evaluated for the -* current group in evl_quota_pick(). +* current group in quota_pick(). */ raw_spin_lock (&rq->lock);evl_set_self_resched (rq);@@ -221,6 +215,7 @@ static void quota_init(struct evl_rq *rq) {struct evl_sched_quota *qs = &rq->quota; +evl_init_schedq (&qs->runnable); qs->period = quota_period;INIT_LIST_HEAD (&qs->groups);@@ -337,8 +332,8 @@ static void quota_forget(struct evl_thread *thread)static void quota_kick (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;/* * Allow a kicked thread to be elected for running until it @@ -347,7 +342,7 @@ static void quota_kick(struct evl_thread *thread)*/ if (tg->run_budget ==0 && !list_empty (&thread->quota_expired)) {list_del_init (&thread->quota_expired); -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); } }@@ -358,79 +353,82 @@ static inline int thread_is_runnable(struct evl_thread *thread)static void quota_enqueue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add_tail (&thread->quota_expired, &tg->expired);else -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); tg->nr_active++; }static void quota_dequeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!list_empty (&thread->quota_expired))list_del_init (&thread->quota_expired);else -evl_del_schedq (&rq->fifo.runnable, thread); +evl_del_schedq (&qs->runnable, thread); tg->nr_active--; }static void quota_requeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add (&thread->quota_expired, &tg->expired);else -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); tg->nr_active++; } -static struct evl_thread *quota_pick (struct evl_rq *rq) +static void quota_out (struct evl_thread *thread) { -struct evl_thread *next, *curr = rq->curr; -struct evl_sched_quota *qs = &rq->quota; -struct evl_quota_group *otg, *tg; -ktime_t now, elapsed; +struct evl_sched_quota *qs = &thread->rq->quota; +struct evl_quota_group *tg = thread->quota; +ktime_t now, consumed; + +/* Timer off means that we are not tracking quota. */ +if (!evl_timer_is_running (&qs->limit_timer)) +return ; - now =evl_ktime_monotonic (); - otg = curr->quota; -if (otg ==NULL ) - goto pick;/* * Charge the time consumed by the outgoing thread to the * group it belongs to. */ - elapsed =ktime_sub (now, otg->run_start); -if (elapsed < otg->run_budget) - otg->run_budget =ktime_sub (otg->run_budget, elapsed); -else - otg->run_budget =0 ; + now =evl_ktime_monotonic (); + consumed =ktime_sub (now, tg->run_start); +if (consumed < tg->run_budget) { + tg->run_budget =ktime_sub (tg->run_budget, consumed); + }else { + tg->run_budget =0 ; +evl_stop_timer (&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick (struct evl_rq *rq) +{ +struct evl_thread *next, *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; +struct evl_quota_group *tg; + pick: - next =evl_get_schedq (&rq->fifo.runnable); + next =evl_get_schedq (&qs->runnable);if (next ==NULL ) {evl_stop_timer (&qs->limit_timer);return NULL ; } -/* -* As we basically piggyback on the SCHED_FIFO runqueue, make -* sure to detect non-quota threads. -*/ tg = next->quota; -if (tg ==NULL ) -return next; - - tg->run_start = now; + tg->nr_active--;/* * Don't consider budget if kicked, we have to allow this @@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)*/ if (next->info &EVL_T_KICKED ) {evl_stop_timer (&qs->limit_timer); - goto out; +return next; } +/* +* __pick_next_thread() might have requeued the current +* thread which is still leading the pack. +*/ +if (curr == next) +return next; +if (ktime_to_ns (tg->run_budget) ==0 ) { -/* Flush expired group members as we go. */ +/* Park expired group members as we go. */ list_add_tail (&next->quota_expired, &tg->expired); goto pick; } -if (otg == tg &&evl_timer_is_running (&qs->limit_timer)) -/* Same group, leave the running timer untouched. */ - goto out; - -/* Arm limit timer for the new running group. */ -evl_start_timer (&qs->limit_timer, -ktime_add (now, tg->run_budget), -EVL_INFINITE ); -out: - tg->nr_active--; +/* Arm new limit timer on change of running group. */ +if (curr->quota != tg || !evl_timer_is_running (&qs->limit_timer)) { + tg->run_start =evl_ktime_monotonic (); +evl_start_timer (&qs->limit_timer, +ktime_add (tg->run_start, tg->run_budget), +EVL_INFINITE ); + }return next; }@@ -542,7 +544,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* Unregister the group before we drop rq->lock. As a result, * it won't accept threads anymore while we are busy moving * the current members to the fifo class, and concurrent -* evl_quota_remove requests would receive -EINVAL. +* quota_remove requests would receive -EINVAL. */ __clear_bit (tg->tgid, group_map);list_del (&tg->next);@@ -556,7 +558,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* hold rq->lock on entry, we do a trylock dance to prevent an * ABBA issue. No livelock is possible since we unregistered * that group already, so &tg->members can only be depleted -* (by this loop specifically). +* (by this loop exclusively). */ while (!list_empty (&tg->members)) {@@ -583,10 +585,10 @@ static void quota_set_limit(struct evl_quota_group *tg, int *quota_sum_r) {struct evl_rq *rq = tg->rq; -struct evl_thread *thread, *tmp, *curr = rq->curr; +struct evl_thread *thread, *tmp;struct evl_sched_quota *qs = &rq->quota; -ktime_t now, elapsed, consumed;ktime_t old_quota = tg->quota; +ktime_t consumed;u64 n;assert_hard_lock (&rq->lock);@@ -615,16 +617,8 @@ static void quota_set_limit(struct evl_quota_group *tg, tg->quota_percent = quota_percent; tg->quota_peak_percent = quota_peak_percent; -if (thread_on_quota (curr, tg)) { - now =evl_ktime_monotonic (); - - elapsed = now - tg->run_start; -if (elapsed < tg->run_budget) - tg->run_budget -= elapsed; -else - tg->run_budget =0 ; - - tg->run_start = now; +if (current_on_quota (tg)) { +quota_out (rq->curr);evl_stop_timer (&qs->limit_timer); }@@ -646,7 +640,7 @@ static void quota_set_limit(struct evl_quota_group *tg,list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }
kernel/evl/sched/quota.c struct evl_sched_class evl_sched_quota = { · line 786
Quoted 7 lines
kernel/evl/sched/quota.c +1
-0@@ -786,6 +780,7 @@ struct evl_sched_class evl_sched_quota = { .sched_dequeue = quota_dequeue, .sched_requeue = quota_requeue, .sched_pick = quota_pick, + .sched_out = quota_out, .sched_migrate = quota_migrate, .sched_chkparam = quota_chkparam, .sched_setparam = quota_setparam,
Could you summarize the key difference to my patch 2? Which preemption or accounting cases did you additionally identify?
Jan
Signature · 2 lines
Siemens AG, Foundational Technologies Linux Expert Center
Quoted 87 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO.Signed-off-by Philippe Gerum <[email protected]>include/evl/sched.h +0
-0
include/evl/sched.h struct evl_sched_class { · line 123
Quoted 4 lines
include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);
The out handler also needs the target thread in order to identify if there is a tg change or not.
Jan
Signature · 2 lines
Siemens AG, Foundational Technologies Linux Expert Center
Quoted 83 lines · Jan Kiszka
Quoted 82 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO.Signed-off-by Philippe Gerum <[email protected]>
Quoted 13 lines
include/evl/sched.h struct evl_sched_class { · line 123
Quoted 9 lines
include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);The out handler also needs the target thread in order to identify if there is a tg change or not.
Your implementation needs this because the sequence is pick() -> out(), mine is out() -> pick(), with the latter using the budget updated by the former to figure out what to do next.
Signature · 1 line
Philippe.
Quoted 99 lines · Philippe Gerum
Quoted 83 lines · Jan Kiszka
Quoted 82 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO.Signed-off-by Philippe Gerum <[email protected]>Quoted 13 lines
include/evl/sched.h struct evl_sched_class { · line 123
Quoted 9 lines
include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);The out handler also needs the target thread in order to identify if there is a tg change or not.
Quoted 3 lines
Your implementation needs this because the sequence is
pick()->out(), mine isout()->pick(), with the latter using the budget updated by the former to figure out what to do next.
But that will call out even on old == new - does not sound right.
Jan
Signature · 2 lines
Siemens AG, Foundational Technologies Linux Expert Center
Quoted 100 lines · Jan Kiszka
Quoted 99 lines · Philippe Gerum
Quoted 96 lines · Jan Kiszka
Quoted 87 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO.Signed-off-by Philippe Gerum <[email protected]>include/evl/sched.h +0
-0include/evl/sched.h struct evl_sched_class { · line 123
Quoted 4 lines
include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);The out handler also needs the target thread in order to identify if there is a tg change or not.
Quoted 6 lines
Quoted 3 lines
Your implementation needs this because the sequence is
pick()->out(), mine isout()->pick(), with the latter using the budget updated by the former to figure out what to do next.But that will call out even on old == new - does not sound right.
Call to determine that next ends up being prev, then filtered out by quota_pick() as a consequence. This is strictly identical performance-wise.
Signature · 1 line
Philippe.
Quoted 58 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
Quoted 4 lines
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).
Please avoid that for the final patch. I already had troubles telling substantial from cosmetical changes apart in your lazy-blocking commit (>50% unrelated changes in there).
Quoted 12 lines
commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.
Quoted 5 lines
To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO.
...and why we need a separate queue now? Please provide reasoning for changes.
Quoted 334 lines
Signed-off-by Philippe Gerum <[email protected]>include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);include/evl/sched/quota.h +1
-0@@ -40,6 +40,7 @@ struct evl_quota_group {struct evl_sched_quota {ktime_t period; +struct evl_sched_queue runnable;struct evl_timer refill_timer;struct evl_timer limit_timer;struct list_head groups;kernel/evl/sched/core.c +13
-7@@ -773,7 +773,8 @@ static inline void set_next_running(struct evl_rq *rq,evl_stop_timer (&rq->rrbtimer); } -static struct evl_thread *__pick_next_thread (struct evl_rq *rq) +static __always_inline struct evl_thread * +__pick_next_thread (struct evl_rq *rq) {struct evl_sched_class *sched_class;struct evl_thread *curr = rq->curr;@@ -821,8 +822,14 @@ static struct evl_thread *__pick_next_thread(struct evl_rq *rq)/* rq->curr->lock + rq->lock held, hard irqs off. */ static struct evl_thread *pick_next_thread (struct evl_rq *rq) { -struct evl_thread *next =__pick_next_thread (rq); +struct evl_thread *next, *prev = rq->curr; +struct evl_sched_class *prev_class = prev->sched_class; +if (prev_class->sched_out) + prev_class->sched_out (prev); + + next =__pick_next_thread (rq); +trace_evl_pick_thread (next);set_next_running (rq, next);return next;@@ -972,21 +979,20 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */return ; } + prev = curr; next =pick_next_thread (this_rq); -trace_evl_pick_thread (next); -if (next == curr) { -if (unlikely (next->state &EVL_T_ROOT )) { +if (next == prev) { +if (unlikely (prev->state &EVL_T_ROOT )) {if (this_rq->local_flags &RQ_TPROXY )evl_notify_proxy_tick (this_rq);if (this_rq->local_flags &RQ_TDEFER )evl_program_local_tick (&evl_mono_clock); }raw_spin_unlock (&this_rq->lock); -raw_spin_unlock_irqrestore (&curr->lock, flags); +raw_spin_unlock_irqrestore (&prev->lock, flags);return ; } - prev = curr; this_rq->curr = next; leaving_inband = false;kernel/evl/sched/quota.c +57
-63@@ -12,45 +12,35 @@#include <uapi/evl/sched-abi.h> /* -* With this policy, each per-CPU runqueue maintains a list of active -* thread groups for the sched_fifo class. -* -* Each time a thread is picked from the runqueue, we check whether we -* still have budget for running it, looking at the group it belongs -* to. If so, a timer is armed to elapse when that group has no more -* budget, would the incoming thread run unpreempted until then -* (i.e. evl_quota->limit_timer). +* Each time a thread is picked from the ->runnable queue, we check +* whether the group it belongs to still has runtime budget. If so, a +* timer is armed to fire when that group has no more budget, would +* the incoming thread run unpreempted until then +* (i.e. quota->limit_timer). * * Otherwise, if no budget remains in the group for running the * candidate thread, we move the latter to a local expiry queue * maintained by the group. This process is done on the fly as we pull -* from the runqueue. +* from the ->runnable queue. * -* Updating the remaining budget is done each time the EVL core asks -* for replacing the current thread with the next runnable one, -* i.e. evl_quota_pick(). There we charge the elapsed run time of the -* outgoing thread to the relevant group, and conversely, we check -* whether the incoming thread has budget. +* Updating the remaining budget is done each time the EVL core +* schedules out a thread undergoing the quota scheduling policy, * -* Finally, a per-CPU timer (evl_quota->refill_timer) periodically -* ticks in the background, in accordance to the defined quota -* interval. Thread group budgets get replenished by its handler in -* accordance to their respective share, pushing all expired threads -* back to the run queue in the same move. +* Finally, a per-CPU timer (quota->refill_timer) periodically ticks +* in the background, in accordance to the defined quota interval, +* replenishing per-group budgets, pushing all expired threads back to +* the quota ->runqueue too. * -* NOTE: since the core logic enforcing the budget entirely happens in -* evl_quota_pick(), applying a budget change can be done as simply as -* forcing the rescheduling procedure to be invoked asap. As a result -* of this, the EVL core will ask for the next thread to run, which -* means calling evl_quota_pick() eventually. +* NOTE: forcing a call to the rescheduling procedure is enoiugh to +* apply a budget change. * -* CAUTION: evl_quota_group->nr_active does count both the threads -* from that group linked to the sched_fifo runqueue, _and_ the -* threads moved to the local expiry queue. As a matter of fact, the -* expired threads - those for which we consumed all the per-group -* budget - are still seen as runnable (i.e. not blocked/suspended) by -* the EVL core. This only means that the SCHED_QUOTA policy won't -* pick them until the corresponding budget is replenished. +* CAUTION: quota_group->nr_active does count both the threads from +* that group linked to the runnable queue, _and_ the threads moved to +* the local expiry queue. As a matter of fact, the expired threads - +* those for which we consumed all the per-group budget - are still +* seen as runnable (i.e. not blocked/suspended) by the EVL core. This +* only means that the SCHED_QUOTA policy won't pick them until the +* corresponding budget is replenished. */ #define MAX_QUOTA_GROUPS 1024@@ -61,15 +51,19 @@ static DECLARE_BITMAP(group_map, MAX_QUOTA_GROUPS);static LIST_HEAD (group_list); -static inline bool thread_on_quota (struct evl_thread *thread, -struct evl_quota_group *tg) +static inline bool current_on_quota (struct evl_quota_group *tg) { -/* -* Check whether @thread is running on some CPU, and belongs -* to quota group @tg. -*/ -return thread->quota == tg && - !(thread->state & (EVL_T_READY |EVL_THREAD_BLOCK_MASK )); +struct evl_rq *rq = tg->rq; +struct evl_thread *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; + +if (curr->quota != tg) +return false; + +if (curr->state & (EVL_T_READY |EVL_T_KICKED |EVL_THREAD_BLOCK_MASK )) +return false; + +return evl_timer_is_running (&qs->limit_timer); }static inline bool group_is_active (struct evl_quota_group *tg)@@ -82,7 +76,7 @@ static inline bool group_is_active(struct evl_quota_group *tg)* runqueue, in which case tg->nr_active already accounted for * it. */ -return thread_on_quota (tg->rq->curr, tg); +return current_on_quota (tg); } static inline void replenish_budget(struct evl_sched_quota *qs,@@ -134,10 +128,10 @@ static inline void replenish_budget(struct evl_sched_quota *qs, }else if (tg->run_credit) { credit =ktime_sub (tg->quota_peak, budget);/* Consume the accumulated credit. */ -if (tg->run_credit >= credit) +if (tg->run_credit >= credit) { tg->run_credit =ktime_sub (tg->run_credit, credit); -else { + }else { credit = tg->run_credit; tg->run_credit =0 ; }@@ -150,8 +144,8 @@ static inline void replenish_budget(struct evl_sched_quota *qs,static void quota_refill_handler (struct evl_timer *timer)/* oob stage stalled */ { -struct evl_quota_group *tg;struct evl_thread *thread, *tmp; +struct evl_quota_group *tg;struct evl_sched_quota *qs;struct evl_rq *rq;@@ -167,7 +161,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *if (tg->run_budget ==0 ||list_empty (&tg->expired))continue ;/* -* For each group living on this CPU, move all expired +* For each group pinned on this CPU, move all expired * threads back to the runqueue. Since those threads * were moved out of the runqueue as we were * considering them for execution, we push them back @@ -178,7 +172,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }@@ -195,7 +189,7 @@ static void quota_limit_handler(struct evl_timer *timer) /* oob stage stalled *//* * Force a rescheduling on the return path of the current * interrupt, so that the budget is re-evaluated for the -* current group in evl_quota_pick(). +* current group in quota_pick(). */ raw_spin_lock (&rq->lock);evl_set_self_resched (rq);@@ -221,6 +215,7 @@ static void quota_init(struct evl_rq *rq) {struct evl_sched_quota *qs = &rq->quota; +evl_init_schedq (&qs->runnable); qs->period = quota_period;INIT_LIST_HEAD (&qs->groups);@@ -337,8 +332,8 @@ static void quota_forget(struct evl_thread *thread)static void quota_kick (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;/* * Allow a kicked thread to be elected for running until it @@ -347,7 +342,7 @@ static void quota_kick(struct evl_thread *thread)*/ if (tg->run_budget ==0 && !list_empty (&thread->quota_expired)) {list_del_init (&thread->quota_expired); -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); } }@@ -358,79 +353,82 @@ static inline int thread_is_runnable(struct evl_thread *thread)static void quota_enqueue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add_tail (&thread->quota_expired, &tg->expired);else -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); tg->nr_active++; }static void quota_dequeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!list_empty (&thread->quota_expired))list_del_init (&thread->quota_expired);else -evl_del_schedq (&rq->fifo.runnable, thread); +evl_del_schedq (&qs->runnable, thread); tg->nr_active--; }static void quota_requeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add (&thread->quota_expired, &tg->expired);else -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); tg->nr_active++; } -static struct evl_thread *quota_pick (struct evl_rq *rq) +static void quota_out (struct evl_thread *thread) { -struct evl_thread *next, *curr = rq->curr; -struct evl_sched_quota *qs = &rq->quota; -struct evl_quota_group *otg, *tg; -ktime_t now, elapsed; +struct evl_sched_quota *qs = &thread->rq->quota; +struct evl_quota_group *tg = thread->quota; +ktime_t now, consumed; + +/* Timer off means that we are not tracking quota. */ +if (!evl_timer_is_running (&qs->limit_timer)) +return ; - now =evl_ktime_monotonic (); - otg = curr->quota; -if (otg ==NULL ) - goto pick;
kernel/evl/sched/quota.c static inline int thread_is_runnable(struct evl_thread *thread) · line 407
Quoted 10 lines
kernel/evl/sched/quota.c +1
-5/* * Charge the time consumed by the outgoing thread to the * group it belongs to. */ - elapsed =ktime_sub (now, otg->run_start); -if (elapsed < otg->run_budget) - otg->run_budget =ktime_sub (otg->run_budget, elapsed); -else - otg->run_budget =0 ; + now =evl_ktime_monotonic ();
Thing brins some, though minor, glitch when doing the run_start update for the next group already in pick. That's why I'm trying to reuse that stamp when available.
Quoted 139 lines
kernel/evl/sched/quota.c +40
-36+ consumed =ktime_sub (now, tg->run_start); +if (consumed < tg->run_budget) { + tg->run_budget =ktime_sub (tg->run_budget, consumed); + }else { + tg->run_budget =0 ; +evl_stop_timer (&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick (struct evl_rq *rq) +{ +struct evl_thread *next, *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; +struct evl_quota_group *tg; + pick: - next =evl_get_schedq (&rq->fifo.runnable); + next =evl_get_schedq (&qs->runnable);if (next ==NULL ) {evl_stop_timer (&qs->limit_timer);return NULL ; } -/* -* As we basically piggyback on the SCHED_FIFO runqueue, make -* sure to detect non-quota threads. -*/ tg = next->quota; -if (tg ==NULL ) -return next; - - tg->run_start = now; + tg->nr_active--;/* * Don't consider budget if kicked, we have to allow this @@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)*/ if (next->info &EVL_T_KICKED ) {evl_stop_timer (&qs->limit_timer); - goto out; +return next; } +/* +* __pick_next_thread() might have requeued the current +* thread which is still leading the pack. +*/ +if (curr == next) +return next; +if (ktime_to_ns (tg->run_budget) ==0 ) { -/* Flush expired group members as we go. */ +/* Park expired group members as we go. */ list_add_tail (&next->quota_expired, &tg->expired); goto pick; } -if (otg == tg &&evl_timer_is_running (&qs->limit_timer)) -/* Same group, leave the running timer untouched. */ - goto out; - -/* Arm limit timer for the new running group. */ -evl_start_timer (&qs->limit_timer, -ktime_add (now, tg->run_budget), -EVL_INFINITE ); -out: - tg->nr_active--; +/* Arm new limit timer on change of running group. */ +if (curr->quota != tg || !evl_timer_is_running (&qs->limit_timer)) { + tg->run_start =evl_ktime_monotonic (); +evl_start_timer (&qs->limit_timer, +ktime_add (tg->run_start, tg->run_budget), +EVL_INFINITE ); + }return next; }@@ -542,7 +544,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* Unregister the group before we drop rq->lock. As a result, * it won't accept threads anymore while we are busy moving * the current members to the fifo class, and concurrent -* evl_quota_remove requests would receive -EINVAL. +* quota_remove requests would receive -EINVAL. */ __clear_bit (tg->tgid, group_map);list_del (&tg->next);@@ -556,7 +558,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* hold rq->lock on entry, we do a trylock dance to prevent an * ABBA issue. No livelock is possible since we unregistered * that group already, so &tg->members can only be depleted -* (by this loop specifically). +* (by this loop exclusively). */ while (!list_empty (&tg->members)) {@@ -583,10 +585,10 @@ static void quota_set_limit(struct evl_quota_group *tg, int *quota_sum_r) {struct evl_rq *rq = tg->rq; -struct evl_thread *thread, *tmp, *curr = rq->curr; +struct evl_thread *thread, *tmp;struct evl_sched_quota *qs = &rq->quota; -ktime_t now, elapsed, consumed;ktime_t old_quota = tg->quota; +ktime_t consumed;u64 n;assert_hard_lock (&rq->lock);@@ -615,16 +617,8 @@ static void quota_set_limit(struct evl_quota_group *tg, tg->quota_percent = quota_percent; tg->quota_peak_percent = quota_peak_percent; -if (thread_on_quota (curr, tg)) { - now =evl_ktime_monotonic (); - - elapsed = now - tg->run_start; -if (elapsed < tg->run_budget) - tg->run_budget -= elapsed; -else - tg->run_budget =0 ; - - tg->run_start = now; +if (current_on_quota (tg)) { +quota_out (rq->curr);evl_stop_timer (&qs->limit_timer); }@@ -646,7 +640,7 @@ static void quota_set_limit(struct evl_quota_group *tg,list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }
kernel/evl/sched/quota.c struct evl_sched_class evl_sched_quota = { · line 786
Quoted 7 lines
kernel/evl/sched/quota.c +1
-0@@ -786,6 +780,7 @@ struct evl_sched_class evl_sched_quota = { .sched_dequeue = quota_dequeue, .sched_requeue = quota_requeue, .sched_pick = quota_pick, + .sched_out = quota_out, .sched_migrate = quota_migrate, .sched_chkparam = quota_chkparam, .sched_setparam = quota_setparam,
You also seem to miss the case I discovered only yesterday as well: When starting the current thread on a quota via setparam, we also need to set its run_start stamp at that point.
Please have a closer look at both my test case (libevl) as well as my patch 2. Currently, I don't see remaining issues with both, but I'm all ears to learn what I missed.
Jan
Signature · 2 lines
Siemens AG, Foundational Technologies Linux Expert Center
Quoted 575 lines · Jan Kiszka
Quoted 58 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
Quoted 4 lines
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).Please avoid that for the final patch. I already had troubles telling substantial from cosmetical changes apart in your lazy-blocking commit (>50% unrelated changes in there).
Quoted 12 lines
commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.Quoted 5 lines
To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO....and why we need a separate queue now? Please provide reasoning for changes.
Quoted 334 lines
Signed-off-by Philippe Gerum <[email protected]>include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);include/evl/sched/quota.h +1
-0@@ -40,6 +40,7 @@ struct evl_quota_group {struct evl_sched_quota {ktime_t period; +struct evl_sched_queue runnable;struct evl_timer refill_timer;struct evl_timer limit_timer;struct list_head groups;kernel/evl/sched/core.c +13
-7@@ -773,7 +773,8 @@ static inline void set_next_running(struct evl_rq *rq,evl_stop_timer (&rq->rrbtimer); } -static struct evl_thread *__pick_next_thread (struct evl_rq *rq) +static __always_inline struct evl_thread * +__pick_next_thread (struct evl_rq *rq) {struct evl_sched_class *sched_class;struct evl_thread *curr = rq->curr;@@ -821,8 +822,14 @@ static struct evl_thread *__pick_next_thread(struct evl_rq *rq)/* rq->curr->lock + rq->lock held, hard irqs off. */ static struct evl_thread *pick_next_thread (struct evl_rq *rq) { -struct evl_thread *next =__pick_next_thread (rq); +struct evl_thread *next, *prev = rq->curr; +struct evl_sched_class *prev_class = prev->sched_class; +if (prev_class->sched_out) + prev_class->sched_out (prev); + + next =__pick_next_thread (rq); +trace_evl_pick_thread (next);set_next_running (rq, next);return next;@@ -972,21 +979,20 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */return ; } + prev = curr; next =pick_next_thread (this_rq); -trace_evl_pick_thread (next); -if (next == curr) { -if (unlikely (next->state &EVL_T_ROOT )) { +if (next == prev) { +if (unlikely (prev->state &EVL_T_ROOT )) {if (this_rq->local_flags &RQ_TPROXY )evl_notify_proxy_tick (this_rq);if (this_rq->local_flags &RQ_TDEFER )evl_program_local_tick (&evl_mono_clock); }raw_spin_unlock (&this_rq->lock); -raw_spin_unlock_irqrestore (&curr->lock, flags); +raw_spin_unlock_irqrestore (&prev->lock, flags);return ; } - prev = curr; this_rq->curr = next; leaving_inband = false;kernel/evl/sched/quota.c +57
-63@@ -12,45 +12,35 @@#include <uapi/evl/sched-abi.h> /* -* With this policy, each per-CPU runqueue maintains a list of active -* thread groups for the sched_fifo class. -* -* Each time a thread is picked from the runqueue, we check whether we -* still have budget for running it, looking at the group it belongs -* to. If so, a timer is armed to elapse when that group has no more -* budget, would the incoming thread run unpreempted until then -* (i.e. evl_quota->limit_timer). +* Each time a thread is picked from the ->runnable queue, we check +* whether the group it belongs to still has runtime budget. If so, a +* timer is armed to fire when that group has no more budget, would +* the incoming thread run unpreempted until then +* (i.e. quota->limit_timer). * * Otherwise, if no budget remains in the group for running the * candidate thread, we move the latter to a local expiry queue * maintained by the group. This process is done on the fly as we pull -* from the runqueue. +* from the ->runnable queue. * -* Updating the remaining budget is done each time the EVL core asks -* for replacing the current thread with the next runnable one, -* i.e. evl_quota_pick(). There we charge the elapsed run time of the -* outgoing thread to the relevant group, and conversely, we check -* whether the incoming thread has budget. +* Updating the remaining budget is done each time the EVL core +* schedules out a thread undergoing the quota scheduling policy, * -* Finally, a per-CPU timer (evl_quota->refill_timer) periodically -* ticks in the background, in accordance to the defined quota -* interval. Thread group budgets get replenished by its handler in -* accordance to their respective share, pushing all expired threads -* back to the run queue in the same move. +* Finally, a per-CPU timer (quota->refill_timer) periodically ticks +* in the background, in accordance to the defined quota interval, +* replenishing per-group budgets, pushing all expired threads back to +* the quota ->runqueue too. * -* NOTE: since the core logic enforcing the budget entirely happens in -* evl_quota_pick(), applying a budget change can be done as simply as -* forcing the rescheduling procedure to be invoked asap. As a result -* of this, the EVL core will ask for the next thread to run, which -* means calling evl_quota_pick() eventually. +* NOTE: forcing a call to the rescheduling procedure is enoiugh to +* apply a budget change. * -* CAUTION: evl_quota_group->nr_active does count both the threads -* from that group linked to the sched_fifo runqueue, _and_ the -* threads moved to the local expiry queue. As a matter of fact, the -* expired threads - those for which we consumed all the per-group -* budget - are still seen as runnable (i.e. not blocked/suspended) by -* the EVL core. This only means that the SCHED_QUOTA policy won't -* pick them until the corresponding budget is replenished. +* CAUTION: quota_group->nr_active does count both the threads from +* that group linked to the runnable queue, _and_ the threads moved to +* the local expiry queue. As a matter of fact, the expired threads - +* those for which we consumed all the per-group budget - are still +* seen as runnable (i.e. not blocked/suspended) by the EVL core. This +* only means that the SCHED_QUOTA policy won't pick them until the +* corresponding budget is replenished. */ #define MAX_QUOTA_GROUPS 1024@@ -61,15 +51,19 @@ static DECLARE_BITMAP(group_map, MAX_QUOTA_GROUPS);static LIST_HEAD (group_list); -static inline bool thread_on_quota (struct evl_thread *thread, -struct evl_quota_group *tg) +static inline bool current_on_quota (struct evl_quota_group *tg) { -/* -* Check whether @thread is running on some CPU, and belongs -* to quota group @tg. -*/ -return thread->quota == tg && - !(thread->state & (EVL_T_READY |EVL_THREAD_BLOCK_MASK )); +struct evl_rq *rq = tg->rq; +struct evl_thread *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; + +if (curr->quota != tg) +return false; + +if (curr->state & (EVL_T_READY |EVL_T_KICKED |EVL_THREAD_BLOCK_MASK )) +return false; + +return evl_timer_is_running (&qs->limit_timer); }static inline bool group_is_active (struct evl_quota_group *tg)@@ -82,7 +76,7 @@ static inline bool group_is_active(struct evl_quota_group *tg)* runqueue, in which case tg->nr_active already accounted for * it. */ -return thread_on_quota (tg->rq->curr, tg); +return current_on_quota (tg); } static inline void replenish_budget(struct evl_sched_quota *qs,@@ -134,10 +128,10 @@ static inline void replenish_budget(struct evl_sched_quota *qs, }else if (tg->run_credit) { credit =ktime_sub (tg->quota_peak, budget);/* Consume the accumulated credit. */ -if (tg->run_credit >= credit) +if (tg->run_credit >= credit) { tg->run_credit =ktime_sub (tg->run_credit, credit); -else { + }else { credit = tg->run_credit; tg->run_credit =0 ; }@@ -150,8 +144,8 @@ static inline void replenish_budget(struct evl_sched_quota *qs,static void quota_refill_handler (struct evl_timer *timer)/* oob stage stalled */ { -struct evl_quota_group *tg;struct evl_thread *thread, *tmp; +struct evl_quota_group *tg;struct evl_sched_quota *qs;struct evl_rq *rq;@@ -167,7 +161,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *if (tg->run_budget ==0 ||list_empty (&tg->expired))continue ;/* -* For each group living on this CPU, move all expired +* For each group pinned on this CPU, move all expired * threads back to the runqueue. Since those threads * were moved out of the runqueue as we were * considering them for execution, we push them back @@ -178,7 +172,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }@@ -195,7 +189,7 @@ static void quota_limit_handler(struct evl_timer *timer) /* oob stage stalled *//* * Force a rescheduling on the return path of the current * interrupt, so that the budget is re-evaluated for the -* current group in evl_quota_pick(). +* current group in quota_pick(). */ raw_spin_lock (&rq->lock);evl_set_self_resched (rq);@@ -221,6 +215,7 @@ static void quota_init(struct evl_rq *rq) {struct evl_sched_quota *qs = &rq->quota; +evl_init_schedq (&qs->runnable); qs->period = quota_period;INIT_LIST_HEAD (&qs->groups);@@ -337,8 +332,8 @@ static void quota_forget(struct evl_thread *thread)static void quota_kick (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;/* * Allow a kicked thread to be elected for running until it @@ -347,7 +342,7 @@ static void quota_kick(struct evl_thread *thread)*/ if (tg->run_budget ==0 && !list_empty (&thread->quota_expired)) {list_del_init (&thread->quota_expired); -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); } }@@ -358,79 +353,82 @@ static inline int thread_is_runnable(struct evl_thread *thread)static void quota_enqueue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add_tail (&thread->quota_expired, &tg->expired);else -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); tg->nr_active++; }static void quota_dequeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!list_empty (&thread->quota_expired))list_del_init (&thread->quota_expired);else -evl_del_schedq (&rq->fifo.runnable, thread); +evl_del_schedq (&qs->runnable, thread); tg->nr_active--; }static void quota_requeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add (&thread->quota_expired, &tg->expired);else -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); tg->nr_active++; } -static struct evl_thread *quota_pick (struct evl_rq *rq) +static void quota_out (struct evl_thread *thread) { -struct evl_thread *next, *curr = rq->curr; -struct evl_sched_quota *qs = &rq->quota; -struct evl_quota_group *otg, *tg; -ktime_t now, elapsed; +struct evl_sched_quota *qs = &thread->rq->quota; +struct evl_quota_group *tg = thread->quota; +ktime_t now, consumed; + +/* Timer off means that we are not tracking quota. */ +if (!evl_timer_is_running (&qs->limit_timer)) +return ; - now =evl_ktime_monotonic (); - otg = curr->quota; -if (otg ==NULL ) - goto pick;kernel/evl/sched/quota.c static inline int thread_is_runnable(struct evl_thread *thread) · line 407
Quoted 10 lines
kernel/evl/sched/quota.c +1
-5/* * Charge the time consumed by the outgoing thread to the * group it belongs to. */ - elapsed =ktime_sub (now, otg->run_start); -if (elapsed < otg->run_budget) - otg->run_budget =ktime_sub (otg->run_budget, elapsed); -else - otg->run_budget =0 ; + now =evl_ktime_monotonic ();Thing brins some, though minor, glitch when doing the
run_startupdate for the next group already in pick. That's why I'm trying to reuse that stamp when available.Quoted 137 lines
kernel/evl/sched/quota.c +40
-36+ consumed =ktime_sub (now, tg->run_start); +if (consumed < tg->run_budget) { + tg->run_budget =ktime_sub (tg->run_budget, consumed); + }else { + tg->run_budget =0 ; +evl_stop_timer (&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick (struct evl_rq *rq) +{ +struct evl_thread *next, *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; +struct evl_quota_group *tg; + pick: - next =evl_get_schedq (&rq->fifo.runnable); + next =evl_get_schedq (&qs->runnable);if (next ==NULL ) {evl_stop_timer (&qs->limit_timer);return NULL ; } -/* -* As we basically piggyback on the SCHED_FIFO runqueue, make -* sure to detect non-quota threads. -*/ tg = next->quota; -if (tg ==NULL ) -return next; - - tg->run_start = now; + tg->nr_active--;/* * Don't consider budget if kicked, we have to allow this @@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)*/ if (next->info &EVL_T_KICKED ) {evl_stop_timer (&qs->limit_timer); - goto out; +return next; } +/* +* __pick_next_thread() might have requeued the current +* thread which is still leading the pack. +*/ +if (curr == next) +return next; +if (ktime_to_ns (tg->run_budget) ==0 ) { -/* Flush expired group members as we go. */ +/* Park expired group members as we go. */ list_add_tail (&next->quota_expired, &tg->expired); goto pick; } -if (otg == tg &&evl_timer_is_running (&qs->limit_timer)) -/* Same group, leave the running timer untouched. */ - goto out; - -/* Arm limit timer for the new running group. */ -evl_start_timer (&qs->limit_timer, -ktime_add (now, tg->run_budget), -EVL_INFINITE ); -out: - tg->nr_active--; +/* Arm new limit timer on change of running group. */ +if (curr->quota != tg || !evl_timer_is_running (&qs->limit_timer)) { + tg->run_start =evl_ktime_monotonic (); +evl_start_timer (&qs->limit_timer, +ktime_add (tg->run_start, tg->run_budget), +EVL_INFINITE ); + }return next; }@@ -542,7 +544,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* Unregister the group before we drop rq->lock. As a result, * it won't accept threads anymore while we are busy moving * the current members to the fifo class, and concurrent -* evl_quota_remove requests would receive -EINVAL. +* quota_remove requests would receive -EINVAL. */ __clear_bit (tg->tgid, group_map);list_del (&tg->next);@@ -556,7 +558,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* hold rq->lock on entry, we do a trylock dance to prevent an * ABBA issue. No livelock is possible since we unregistered * that group already, so &tg->members can only be depleted -* (by this loop specifically). +* (by this loop exclusively). */ while (!list_empty (&tg->members)) {@@ -583,10 +585,10 @@ static void quota_set_limit(struct evl_quota_group *tg, int *quota_sum_r) {struct evl_rq *rq = tg->rq; -struct evl_thread *thread, *tmp, *curr = rq->curr; +struct evl_thread *thread, *tmp;struct evl_sched_quota *qs = &rq->quota; -ktime_t now, elapsed, consumed;ktime_t old_quota = tg->quota; +ktime_t consumed;u64 n;assert_hard_lock (&rq->lock);@@ -615,16 +617,8 @@ static void quota_set_limit(struct evl_quota_group *tg, tg->quota_percent = quota_percent; tg->quota_peak_percent = quota_peak_percent; -if (thread_on_quota (curr, tg)) { - now =evl_ktime_monotonic (); - - elapsed = now - tg->run_start; -if (elapsed < tg->run_budget) - tg->run_budget -= elapsed; -else - tg->run_budget =0 ; - - tg->run_start = now; +if (current_on_quota (tg)) { +quota_out (rq->curr);evl_stop_timer (&qs->limit_timer); }@@ -646,7 +640,7 @@ static void quota_set_limit(struct evl_quota_group *tg,list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }
Quoted 13 lines
kernel/evl/sched/quota.c struct evl_sched_class evl_sched_quota = { · line 786
Quoted 9 lines
kernel/evl/sched/quota.c +1
-0@@ -786,6 +780,7 @@ struct evl_sched_class evl_sched_quota = { .sched_dequeue = quota_dequeue, .sched_requeue = quota_requeue, .sched_pick = quota_pick, + .sched_out = quota_out, .sched_migrate = quota_migrate, .sched_chkparam = quota_chkparam, .sched_setparam = quota_setparam,You also seem to miss the case I discovered only yesterday as well: When starting the current thread on a quota via setparam, we also need to set its
run_startstamp at that point.
run_start should be meaningful only when a limit timer runs.
Quoted 5 lines
Please have a closer look at both my test case (libevl) as well as my patch 2. Currently, I don't see remaining issues with both, but I'm all ears to learn what I missed.
Signature · 1 line
Philippe.
Quoted 591 lines · Philippe Gerum
Quoted 575 lines · Jan Kiszka
Quoted 58 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
Quoted 4 lines
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).Please avoid that for the final patch. I already had troubles telling substantial from cosmetical changes apart in your lazy-blocking commit (>50% unrelated changes in there).
Quoted 12 lines
commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.Quoted 5 lines
To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO....and why we need a separate queue now? Please provide reasoning for changes.
Quoted 334 lines
Signed-off-by Philippe Gerum <[email protected]>include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);include/evl/sched/quota.h +1
-0@@ -40,6 +40,7 @@ struct evl_quota_group {struct evl_sched_quota {ktime_t period; +struct evl_sched_queue runnable;struct evl_timer refill_timer;struct evl_timer limit_timer;struct list_head groups;kernel/evl/sched/core.c +13
-7@@ -773,7 +773,8 @@ static inline void set_next_running(struct evl_rq *rq,evl_stop_timer (&rq->rrbtimer); } -static struct evl_thread *__pick_next_thread (struct evl_rq *rq) +static __always_inline struct evl_thread * +__pick_next_thread (struct evl_rq *rq) {struct evl_sched_class *sched_class;struct evl_thread *curr = rq->curr;@@ -821,8 +822,14 @@ static struct evl_thread *__pick_next_thread(struct evl_rq *rq)/* rq->curr->lock + rq->lock held, hard irqs off. */ static struct evl_thread *pick_next_thread (struct evl_rq *rq) { -struct evl_thread *next =__pick_next_thread (rq); +struct evl_thread *next, *prev = rq->curr; +struct evl_sched_class *prev_class = prev->sched_class; +if (prev_class->sched_out) + prev_class->sched_out (prev); + + next =__pick_next_thread (rq); +trace_evl_pick_thread (next);set_next_running (rq, next);return next;@@ -972,21 +979,20 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */return ; } + prev = curr; next =pick_next_thread (this_rq); -trace_evl_pick_thread (next); -if (next == curr) { -if (unlikely (next->state &EVL_T_ROOT )) { +if (next == prev) { +if (unlikely (prev->state &EVL_T_ROOT )) {if (this_rq->local_flags &RQ_TPROXY )evl_notify_proxy_tick (this_rq);if (this_rq->local_flags &RQ_TDEFER )evl_program_local_tick (&evl_mono_clock); }raw_spin_unlock (&this_rq->lock); -raw_spin_unlock_irqrestore (&curr->lock, flags); +raw_spin_unlock_irqrestore (&prev->lock, flags);return ; } - prev = curr; this_rq->curr = next; leaving_inband = false;kernel/evl/sched/quota.c +57
-63@@ -12,45 +12,35 @@#include <uapi/evl/sched-abi.h> /* -* With this policy, each per-CPU runqueue maintains a list of active -* thread groups for the sched_fifo class. -* -* Each time a thread is picked from the runqueue, we check whether we -* still have budget for running it, looking at the group it belongs -* to. If so, a timer is armed to elapse when that group has no more -* budget, would the incoming thread run unpreempted until then -* (i.e. evl_quota->limit_timer). +* Each time a thread is picked from the ->runnable queue, we check +* whether the group it belongs to still has runtime budget. If so, a +* timer is armed to fire when that group has no more budget, would +* the incoming thread run unpreempted until then +* (i.e. quota->limit_timer). * * Otherwise, if no budget remains in the group for running the * candidate thread, we move the latter to a local expiry queue * maintained by the group. This process is done on the fly as we pull -* from the runqueue. +* from the ->runnable queue. * -* Updating the remaining budget is done each time the EVL core asks -* for replacing the current thread with the next runnable one, -* i.e. evl_quota_pick(). There we charge the elapsed run time of the -* outgoing thread to the relevant group, and conversely, we check -* whether the incoming thread has budget. +* Updating the remaining budget is done each time the EVL core +* schedules out a thread undergoing the quota scheduling policy, * -* Finally, a per-CPU timer (evl_quota->refill_timer) periodically -* ticks in the background, in accordance to the defined quota -* interval. Thread group budgets get replenished by its handler in -* accordance to their respective share, pushing all expired threads -* back to the run queue in the same move. +* Finally, a per-CPU timer (quota->refill_timer) periodically ticks +* in the background, in accordance to the defined quota interval, +* replenishing per-group budgets, pushing all expired threads back to +* the quota ->runqueue too. * -* NOTE: since the core logic enforcing the budget entirely happens in -* evl_quota_pick(), applying a budget change can be done as simply as -* forcing the rescheduling procedure to be invoked asap. As a result -* of this, the EVL core will ask for the next thread to run, which -* means calling evl_quota_pick() eventually. +* NOTE: forcing a call to the rescheduling procedure is enoiugh to +* apply a budget change. * -* CAUTION: evl_quota_group->nr_active does count both the threads -* from that group linked to the sched_fifo runqueue, _and_ the -* threads moved to the local expiry queue. As a matter of fact, the -* expired threads - those for which we consumed all the per-group -* budget - are still seen as runnable (i.e. not blocked/suspended) by -* the EVL core. This only means that the SCHED_QUOTA policy won't -* pick them until the corresponding budget is replenished. +* CAUTION: quota_group->nr_active does count both the threads from +* that group linked to the runnable queue, _and_ the threads moved to +* the local expiry queue. As a matter of fact, the expired threads - +* those for which we consumed all the per-group budget - are still +* seen as runnable (i.e. not blocked/suspended) by the EVL core. This +* only means that the SCHED_QUOTA policy won't pick them until the +* corresponding budget is replenished. */ #define MAX_QUOTA_GROUPS 1024@@ -61,15 +51,19 @@ static DECLARE_BITMAP(group_map, MAX_QUOTA_GROUPS);static LIST_HEAD (group_list); -static inline bool thread_on_quota (struct evl_thread *thread, -struct evl_quota_group *tg) +static inline bool current_on_quota (struct evl_quota_group *tg) { -/* -* Check whether @thread is running on some CPU, and belongs -* to quota group @tg. -*/ -return thread->quota == tg && - !(thread->state & (EVL_T_READY |EVL_THREAD_BLOCK_MASK )); +struct evl_rq *rq = tg->rq; +struct evl_thread *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; + +if (curr->quota != tg) +return false; + +if (curr->state & (EVL_T_READY |EVL_T_KICKED |EVL_THREAD_BLOCK_MASK )) +return false; + +return evl_timer_is_running (&qs->limit_timer); }static inline bool group_is_active (struct evl_quota_group *tg)@@ -82,7 +76,7 @@ static inline bool group_is_active(struct evl_quota_group *tg)* runqueue, in which case tg->nr_active already accounted for * it. */ -return thread_on_quota (tg->rq->curr, tg); +return current_on_quota (tg); } static inline void replenish_budget(struct evl_sched_quota *qs,@@ -134,10 +128,10 @@ static inline void replenish_budget(struct evl_sched_quota *qs, }else if (tg->run_credit) { credit =ktime_sub (tg->quota_peak, budget);/* Consume the accumulated credit. */ -if (tg->run_credit >= credit) +if (tg->run_credit >= credit) { tg->run_credit =ktime_sub (tg->run_credit, credit); -else { + }else { credit = tg->run_credit; tg->run_credit =0 ; }@@ -150,8 +144,8 @@ static inline void replenish_budget(struct evl_sched_quota *qs,static void quota_refill_handler (struct evl_timer *timer)/* oob stage stalled */ { -struct evl_quota_group *tg;struct evl_thread *thread, *tmp; +struct evl_quota_group *tg;struct evl_sched_quota *qs;struct evl_rq *rq;@@ -167,7 +161,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *if (tg->run_budget ==0 ||list_empty (&tg->expired))continue ;/* -* For each group living on this CPU, move all expired +* For each group pinned on this CPU, move all expired * threads back to the runqueue. Since those threads * were moved out of the runqueue as we were * considering them for execution, we push them back @@ -178,7 +172,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }@@ -195,7 +189,7 @@ static void quota_limit_handler(struct evl_timer *timer) /* oob stage stalled *//* * Force a rescheduling on the return path of the current * interrupt, so that the budget is re-evaluated for the -* current group in evl_quota_pick(). +* current group in quota_pick(). */ raw_spin_lock (&rq->lock);evl_set_self_resched (rq);@@ -221,6 +215,7 @@ static void quota_init(struct evl_rq *rq) {struct evl_sched_quota *qs = &rq->quota; +evl_init_schedq (&qs->runnable); qs->period = quota_period;INIT_LIST_HEAD (&qs->groups);@@ -337,8 +332,8 @@ static void quota_forget(struct evl_thread *thread)static void quota_kick (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;/* * Allow a kicked thread to be elected for running until it @@ -347,7 +342,7 @@ static void quota_kick(struct evl_thread *thread)*/ if (tg->run_budget ==0 && !list_empty (&thread->quota_expired)) {list_del_init (&thread->quota_expired); -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); } }@@ -358,79 +353,82 @@ static inline int thread_is_runnable(struct evl_thread *thread)static void quota_enqueue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add_tail (&thread->quota_expired, &tg->expired);else -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); tg->nr_active++; }static void quota_dequeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!list_empty (&thread->quota_expired))list_del_init (&thread->quota_expired);else -evl_del_schedq (&rq->fifo.runnable, thread); +evl_del_schedq (&qs->runnable, thread); tg->nr_active--; }static void quota_requeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add (&thread->quota_expired, &tg->expired);else -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); tg->nr_active++; } -static struct evl_thread *quota_pick (struct evl_rq *rq) +static void quota_out (struct evl_thread *thread) { -struct evl_thread *next, *curr = rq->curr; -struct evl_sched_quota *qs = &rq->quota; -struct evl_quota_group *otg, *tg; -ktime_t now, elapsed; +struct evl_sched_quota *qs = &thread->rq->quota; +struct evl_quota_group *tg = thread->quota; +ktime_t now, consumed; + +/* Timer off means that we are not tracking quota. */ +if (!evl_timer_is_running (&qs->limit_timer)) +return ; - now =evl_ktime_monotonic (); - otg = curr->quota; -if (otg ==NULL ) - goto pick;kernel/evl/sched/quota.c static inline int thread_is_runnable(struct evl_thread *thread) · line 407
Quoted 10 lines
kernel/evl/sched/quota.c +1
-5/* * Charge the time consumed by the outgoing thread to the * group it belongs to. */ - elapsed =ktime_sub (now, otg->run_start); -if (elapsed < otg->run_budget) - otg->run_budget =ktime_sub (otg->run_budget, elapsed); -else - otg->run_budget =0 ; + now =evl_ktime_monotonic ();Thing brins some, though minor, glitch when doing the
run_startupdate for the next group already in pick. That's why I'm trying to reuse that stamp when available.Quoted 137 lines
kernel/evl/sched/quota.c +40
-36+ consumed =ktime_sub (now, tg->run_start); +if (consumed < tg->run_budget) { + tg->run_budget =ktime_sub (tg->run_budget, consumed); + }else { + tg->run_budget =0 ; +evl_stop_timer (&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick (struct evl_rq *rq) +{ +struct evl_thread *next, *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; +struct evl_quota_group *tg; + pick: - next =evl_get_schedq (&rq->fifo.runnable); + next =evl_get_schedq (&qs->runnable);if (next ==NULL ) {evl_stop_timer (&qs->limit_timer);return NULL ; } -/* -* As we basically piggyback on the SCHED_FIFO runqueue, make -* sure to detect non-quota threads. -*/ tg = next->quota; -if (tg ==NULL ) -return next; - - tg->run_start = now; + tg->nr_active--;/* * Don't consider budget if kicked, we have to allow this @@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)*/ if (next->info &EVL_T_KICKED ) {evl_stop_timer (&qs->limit_timer); - goto out; +return next; } +/* +* __pick_next_thread() might have requeued the current +* thread which is still leading the pack. +*/ +if (curr == next) +return next; +if (ktime_to_ns (tg->run_budget) ==0 ) { -/* Flush expired group members as we go. */ +/* Park expired group members as we go. */ list_add_tail (&next->quota_expired, &tg->expired); goto pick; } -if (otg == tg &&evl_timer_is_running (&qs->limit_timer)) -/* Same group, leave the running timer untouched. */ - goto out; - -/* Arm limit timer for the new running group. */ -evl_start_timer (&qs->limit_timer, -ktime_add (now, tg->run_budget), -EVL_INFINITE ); -out: - tg->nr_active--; +/* Arm new limit timer on change of running group. */ +if (curr->quota != tg || !evl_timer_is_running (&qs->limit_timer)) { + tg->run_start =evl_ktime_monotonic (); +evl_start_timer (&qs->limit_timer, +ktime_add (tg->run_start, tg->run_budget), +EVL_INFINITE ); + }return next; }@@ -542,7 +544,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* Unregister the group before we drop rq->lock. As a result, * it won't accept threads anymore while we are busy moving * the current members to the fifo class, and concurrent -* evl_quota_remove requests would receive -EINVAL. +* quota_remove requests would receive -EINVAL. */ __clear_bit (tg->tgid, group_map);list_del (&tg->next);@@ -556,7 +558,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* hold rq->lock on entry, we do a trylock dance to prevent an * ABBA issue. No livelock is possible since we unregistered * that group already, so &tg->members can only be depleted -* (by this loop specifically). +* (by this loop exclusively). */ while (!list_empty (&tg->members)) {@@ -583,10 +585,10 @@ static void quota_set_limit(struct evl_quota_group *tg, int *quota_sum_r) {struct evl_rq *rq = tg->rq; -struct evl_thread *thread, *tmp, *curr = rq->curr; +struct evl_thread *thread, *tmp;struct evl_sched_quota *qs = &rq->quota; -ktime_t now, elapsed, consumed;ktime_t old_quota = tg->quota; +ktime_t consumed;u64 n;assert_hard_lock (&rq->lock);@@ -615,16 +617,8 @@ static void quota_set_limit(struct evl_quota_group *tg, tg->quota_percent = quota_percent; tg->quota_peak_percent = quota_peak_percent; -if (thread_on_quota (curr, tg)) { - now =evl_ktime_monotonic (); - - elapsed = now - tg->run_start; -if (elapsed < tg->run_budget) - tg->run_budget -= elapsed; -else - tg->run_budget =0 ; - - tg->run_start = now; +if (current_on_quota (tg)) { +quota_out (rq->curr);evl_stop_timer (&qs->limit_timer); }@@ -646,7 +640,7 @@ static void quota_set_limit(struct evl_quota_group *tg,list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }Quoted 13 lines
kernel/evl/sched/quota.c struct evl_sched_class evl_sched_quota = { · line 786
Quoted 9 lines
kernel/evl/sched/quota.c +1
-0@@ -786,6 +780,7 @@ struct evl_sched_class evl_sched_quota = { .sched_dequeue = quota_dequeue, .sched_requeue = quota_requeue, .sched_pick = quota_pick, + .sched_out = quota_out, .sched_migrate = quota_migrate, .sched_chkparam = quota_chkparam, .sched_setparam = quota_setparam,You also seem to miss the case I discovered only yesterday as well: When starting the current thread on a quota via setparam, we also need to set its
run_startstamp at that point.
Quoted 2 lines
run_startshould be meaningful only when a limit timer runs.
Right, but are you sure it will be started in the case I described...?
Jan
Quoted 6 lines
Quoted 5 lines
Please have a closer look at both my test case (libevl) as well as my patch 2. Currently, I don't see remaining issues with both, but I'm all ears to learn what I missed.
Signature · 2 lines
Siemens AG, Foundational Technologies Linux Expert Center
Quoted 592 lines · Jan Kiszka
Quoted 591 lines · Philippe Gerum
Quoted 588 lines · Jan Kiszka
Quoted 58 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
Quoted 4 lines
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).Please avoid that for the final patch. I already had troubles telling substantial from cosmetical changes apart in your lazy-blocking commit (>50% unrelated changes in there).
Quoted 12 lines
commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.Quoted 5 lines
To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO....and why we need a separate queue now? Please provide reasoning for changes.
Quoted 334 lines
Signed-off-by Philippe Gerum <[email protected]>include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);include/evl/sched/quota.h +1
-0@@ -40,6 +40,7 @@ struct evl_quota_group {struct evl_sched_quota {ktime_t period; +struct evl_sched_queue runnable;struct evl_timer refill_timer;struct evl_timer limit_timer;struct list_head groups;kernel/evl/sched/core.c +13
-7@@ -773,7 +773,8 @@ static inline void set_next_running(struct evl_rq *rq,evl_stop_timer (&rq->rrbtimer); } -static struct evl_thread *__pick_next_thread (struct evl_rq *rq) +static __always_inline struct evl_thread * +__pick_next_thread (struct evl_rq *rq) {struct evl_sched_class *sched_class;struct evl_thread *curr = rq->curr;@@ -821,8 +822,14 @@ static struct evl_thread *__pick_next_thread(struct evl_rq *rq)/* rq->curr->lock + rq->lock held, hard irqs off. */ static struct evl_thread *pick_next_thread (struct evl_rq *rq) { -struct evl_thread *next =__pick_next_thread (rq); +struct evl_thread *next, *prev = rq->curr; +struct evl_sched_class *prev_class = prev->sched_class; +if (prev_class->sched_out) + prev_class->sched_out (prev); + + next =__pick_next_thread (rq); +trace_evl_pick_thread (next);set_next_running (rq, next);return next;@@ -972,21 +979,20 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */return ; } + prev = curr; next =pick_next_thread (this_rq); -trace_evl_pick_thread (next); -if (next == curr) { -if (unlikely (next->state &EVL_T_ROOT )) { +if (next == prev) { +if (unlikely (prev->state &EVL_T_ROOT )) {if (this_rq->local_flags &RQ_TPROXY )evl_notify_proxy_tick (this_rq);if (this_rq->local_flags &RQ_TDEFER )evl_program_local_tick (&evl_mono_clock); }raw_spin_unlock (&this_rq->lock); -raw_spin_unlock_irqrestore (&curr->lock, flags); +raw_spin_unlock_irqrestore (&prev->lock, flags);return ; } - prev = curr; this_rq->curr = next; leaving_inband = false;kernel/evl/sched/quota.c +57
-63@@ -12,45 +12,35 @@#include <uapi/evl/sched-abi.h> /* -* With this policy, each per-CPU runqueue maintains a list of active -* thread groups for the sched_fifo class. -* -* Each time a thread is picked from the runqueue, we check whether we -* still have budget for running it, looking at the group it belongs -* to. If so, a timer is armed to elapse when that group has no more -* budget, would the incoming thread run unpreempted until then -* (i.e. evl_quota->limit_timer). +* Each time a thread is picked from the ->runnable queue, we check +* whether the group it belongs to still has runtime budget. If so, a +* timer is armed to fire when that group has no more budget, would +* the incoming thread run unpreempted until then +* (i.e. quota->limit_timer). * * Otherwise, if no budget remains in the group for running the * candidate thread, we move the latter to a local expiry queue * maintained by the group. This process is done on the fly as we pull -* from the runqueue. +* from the ->runnable queue. * -* Updating the remaining budget is done each time the EVL core asks -* for replacing the current thread with the next runnable one, -* i.e. evl_quota_pick(). There we charge the elapsed run time of the -* outgoing thread to the relevant group, and conversely, we check -* whether the incoming thread has budget. +* Updating the remaining budget is done each time the EVL core +* schedules out a thread undergoing the quota scheduling policy, * -* Finally, a per-CPU timer (evl_quota->refill_timer) periodically -* ticks in the background, in accordance to the defined quota -* interval. Thread group budgets get replenished by its handler in -* accordance to their respective share, pushing all expired threads -* back to the run queue in the same move. +* Finally, a per-CPU timer (quota->refill_timer) periodically ticks +* in the background, in accordance to the defined quota interval, +* replenishing per-group budgets, pushing all expired threads back to +* the quota ->runqueue too. * -* NOTE: since the core logic enforcing the budget entirely happens in -* evl_quota_pick(), applying a budget change can be done as simply as -* forcing the rescheduling procedure to be invoked asap. As a result -* of this, the EVL core will ask for the next thread to run, which -* means calling evl_quota_pick() eventually. +* NOTE: forcing a call to the rescheduling procedure is enoiugh to +* apply a budget change. * -* CAUTION: evl_quota_group->nr_active does count both the threads -* from that group linked to the sched_fifo runqueue, _and_ the -* threads moved to the local expiry queue. As a matter of fact, the -* expired threads - those for which we consumed all the per-group -* budget - are still seen as runnable (i.e. not blocked/suspended) by -* the EVL core. This only means that the SCHED_QUOTA policy won't -* pick them until the corresponding budget is replenished. +* CAUTION: quota_group->nr_active does count both the threads from +* that group linked to the runnable queue, _and_ the threads moved to +* the local expiry queue. As a matter of fact, the expired threads - +* those for which we consumed all the per-group budget - are still +* seen as runnable (i.e. not blocked/suspended) by the EVL core. This +* only means that the SCHED_QUOTA policy won't pick them until the +* corresponding budget is replenished. */ #define MAX_QUOTA_GROUPS 1024@@ -61,15 +51,19 @@ static DECLARE_BITMAP(group_map, MAX_QUOTA_GROUPS);static LIST_HEAD (group_list); -static inline bool thread_on_quota (struct evl_thread *thread, -struct evl_quota_group *tg) +static inline bool current_on_quota (struct evl_quota_group *tg) { -/* -* Check whether @thread is running on some CPU, and belongs -* to quota group @tg. -*/ -return thread->quota == tg && - !(thread->state & (EVL_T_READY |EVL_THREAD_BLOCK_MASK )); +struct evl_rq *rq = tg->rq; +struct evl_thread *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; + +if (curr->quota != tg) +return false; + +if (curr->state & (EVL_T_READY |EVL_T_KICKED |EVL_THREAD_BLOCK_MASK )) +return false; + +return evl_timer_is_running (&qs->limit_timer); }static inline bool group_is_active (struct evl_quota_group *tg)@@ -82,7 +76,7 @@ static inline bool group_is_active(struct evl_quota_group *tg)* runqueue, in which case tg->nr_active already accounted for * it. */ -return thread_on_quota (tg->rq->curr, tg); +return current_on_quota (tg); } static inline void replenish_budget(struct evl_sched_quota *qs,@@ -134,10 +128,10 @@ static inline void replenish_budget(struct evl_sched_quota *qs, }else if (tg->run_credit) { credit =ktime_sub (tg->quota_peak, budget);/* Consume the accumulated credit. */ -if (tg->run_credit >= credit) +if (tg->run_credit >= credit) { tg->run_credit =ktime_sub (tg->run_credit, credit); -else { + }else { credit = tg->run_credit; tg->run_credit =0 ; }@@ -150,8 +144,8 @@ static inline void replenish_budget(struct evl_sched_quota *qs,static void quota_refill_handler (struct evl_timer *timer)/* oob stage stalled */ { -struct evl_quota_group *tg;struct evl_thread *thread, *tmp; +struct evl_quota_group *tg;struct evl_sched_quota *qs;struct evl_rq *rq;@@ -167,7 +161,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *if (tg->run_budget ==0 ||list_empty (&tg->expired))continue ;/* -* For each group living on this CPU, move all expired +* For each group pinned on this CPU, move all expired * threads back to the runqueue. Since those threads * were moved out of the runqueue as we were * considering them for execution, we push them back @@ -178,7 +172,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }@@ -195,7 +189,7 @@ static void quota_limit_handler(struct evl_timer *timer) /* oob stage stalled *//* * Force a rescheduling on the return path of the current * interrupt, so that the budget is re-evaluated for the -* current group in evl_quota_pick(). +* current group in quota_pick(). */ raw_spin_lock (&rq->lock);evl_set_self_resched (rq);@@ -221,6 +215,7 @@ static void quota_init(struct evl_rq *rq) {struct evl_sched_quota *qs = &rq->quota; +evl_init_schedq (&qs->runnable); qs->period = quota_period;INIT_LIST_HEAD (&qs->groups);@@ -337,8 +332,8 @@ static void quota_forget(struct evl_thread *thread)static void quota_kick (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;/* * Allow a kicked thread to be elected for running until it @@ -347,7 +342,7 @@ static void quota_kick(struct evl_thread *thread)*/ if (tg->run_budget ==0 && !list_empty (&thread->quota_expired)) {list_del_init (&thread->quota_expired); -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); } }@@ -358,79 +353,82 @@ static inline int thread_is_runnable(struct evl_thread *thread)static void quota_enqueue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add_tail (&thread->quota_expired, &tg->expired);else -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); tg->nr_active++; }static void quota_dequeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!list_empty (&thread->quota_expired))list_del_init (&thread->quota_expired);else -evl_del_schedq (&rq->fifo.runnable, thread); +evl_del_schedq (&qs->runnable, thread); tg->nr_active--; }static void quota_requeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add (&thread->quota_expired, &tg->expired);else -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); tg->nr_active++; } -static struct evl_thread *quota_pick (struct evl_rq *rq) +static void quota_out (struct evl_thread *thread) { -struct evl_thread *next, *curr = rq->curr; -struct evl_sched_quota *qs = &rq->quota; -struct evl_quota_group *otg, *tg; -ktime_t now, elapsed; +struct evl_sched_quota *qs = &thread->rq->quota; +struct evl_quota_group *tg = thread->quota; +ktime_t now, consumed; + +/* Timer off means that we are not tracking quota. */ +if (!evl_timer_is_running (&qs->limit_timer)) +return ; - now =evl_ktime_monotonic (); - otg = curr->quota; -if (otg ==NULL ) - goto pick;kernel/evl/sched/quota.c static inline int thread_is_runnable(struct evl_thread *thread) · line 407
Quoted 10 lines
kernel/evl/sched/quota.c +1
-5/* * Charge the time consumed by the outgoing thread to the * group it belongs to. */ - elapsed =ktime_sub (now, otg->run_start); -if (elapsed < otg->run_budget) - otg->run_budget =ktime_sub (otg->run_budget, elapsed); -else - otg->run_budget =0 ; + now =evl_ktime_monotonic ();Thing brins some, though minor, glitch when doing the
run_startupdate for the next group already in pick. That's why I'm trying to reuse that stamp when available.Quoted 139 lines
kernel/evl/sched/quota.c +40
-36+ consumed =ktime_sub (now, tg->run_start); +if (consumed < tg->run_budget) { + tg->run_budget =ktime_sub (tg->run_budget, consumed); + }else { + tg->run_budget =0 ; +evl_stop_timer (&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick (struct evl_rq *rq) +{ +struct evl_thread *next, *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; +struct evl_quota_group *tg; + pick: - next =evl_get_schedq (&rq->fifo.runnable); + next =evl_get_schedq (&qs->runnable);if (next ==NULL ) {evl_stop_timer (&qs->limit_timer);return NULL ; } -/* -* As we basically piggyback on the SCHED_FIFO runqueue, make -* sure to detect non-quota threads. -*/ tg = next->quota; -if (tg ==NULL ) -return next; - - tg->run_start = now; + tg->nr_active--;/* * Don't consider budget if kicked, we have to allow this @@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)*/ if (next->info &EVL_T_KICKED ) {evl_stop_timer (&qs->limit_timer); - goto out; +return next; } +/* +* __pick_next_thread() might have requeued the current +* thread which is still leading the pack. +*/ +if (curr == next) +return next; +if (ktime_to_ns (tg->run_budget) ==0 ) { -/* Flush expired group members as we go. */ +/* Park expired group members as we go. */ list_add_tail (&next->quota_expired, &tg->expired); goto pick; } -if (otg == tg &&evl_timer_is_running (&qs->limit_timer)) -/* Same group, leave the running timer untouched. */ - goto out; - -/* Arm limit timer for the new running group. */ -evl_start_timer (&qs->limit_timer, -ktime_add (now, tg->run_budget), -EVL_INFINITE ); -out: - tg->nr_active--; +/* Arm new limit timer on change of running group. */ +if (curr->quota != tg || !evl_timer_is_running (&qs->limit_timer)) { + tg->run_start =evl_ktime_monotonic (); +evl_start_timer (&qs->limit_timer, +ktime_add (tg->run_start, tg->run_budget), +EVL_INFINITE ); + }return next; }@@ -542,7 +544,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* Unregister the group before we drop rq->lock. As a result, * it won't accept threads anymore while we are busy moving * the current members to the fifo class, and concurrent -* evl_quota_remove requests would receive -EINVAL. +* quota_remove requests would receive -EINVAL. */ __clear_bit (tg->tgid, group_map);list_del (&tg->next);@@ -556,7 +558,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* hold rq->lock on entry, we do a trylock dance to prevent an * ABBA issue. No livelock is possible since we unregistered * that group already, so &tg->members can only be depleted -* (by this loop specifically). +* (by this loop exclusively). */ while (!list_empty (&tg->members)) {@@ -583,10 +585,10 @@ static void quota_set_limit(struct evl_quota_group *tg, int *quota_sum_r) {struct evl_rq *rq = tg->rq; -struct evl_thread *thread, *tmp, *curr = rq->curr; +struct evl_thread *thread, *tmp;struct evl_sched_quota *qs = &rq->quota; -ktime_t now, elapsed, consumed;ktime_t old_quota = tg->quota; +ktime_t consumed;u64 n;assert_hard_lock (&rq->lock);@@ -615,16 +617,8 @@ static void quota_set_limit(struct evl_quota_group *tg, tg->quota_percent = quota_percent; tg->quota_peak_percent = quota_peak_percent; -if (thread_on_quota (curr, tg)) { - now =evl_ktime_monotonic (); - - elapsed = now - tg->run_start; -if (elapsed < tg->run_budget) - tg->run_budget -= elapsed; -else - tg->run_budget =0 ; - - tg->run_start = now; +if (current_on_quota (tg)) { +quota_out (rq->curr);evl_stop_timer (&qs->limit_timer); }@@ -646,7 +640,7 @@ static void quota_set_limit(struct evl_quota_group *tg,list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }kernel/evl/sched/quota.c struct evl_sched_class evl_sched_quota = { · line 786
Quoted 7 lines
kernel/evl/sched/quota.c +1
-0@@ -786,6 +780,7 @@ struct evl_sched_class evl_sched_quota = { .sched_dequeue = quota_dequeue, .sched_requeue = quota_requeue, .sched_pick = quota_pick, + .sched_out = quota_out, .sched_migrate = quota_migrate, .sched_chkparam = quota_chkparam, .sched_setparam = quota_setparam,You also seem to miss the case I discovered only yesterday as well: When starting the current thread on a quota via setparam, we also need to set its
run_startstamp at that point.
Quoted 5 lines
Quoted 2 lines
run_startshould be meaningful only when a limit timer runs.Right, but are you sure it will be started in the case I described...?
To be started, it must be picked, so yes.
Signature · 1 line
Philippe.
Quoted 594 lines · Philippe Gerum
Quoted 592 lines · Jan Kiszka
Quoted 591 lines · Philippe Gerum
Quoted 588 lines · Jan Kiszka
Quoted 58 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
Quoted 4 lines
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).Please avoid that for the final patch. I already had troubles telling substantial from cosmetical changes apart in your lazy-blocking commit (>50% unrelated changes in there).
Quoted 12 lines
commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.Quoted 5 lines
To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO....and why we need a separate queue now? Please provide reasoning for changes.
Quoted 334 lines
Signed-off-by Philippe Gerum <[email protected]>include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);include/evl/sched/quota.h +1
-0@@ -40,6 +40,7 @@ struct evl_quota_group {struct evl_sched_quota {ktime_t period; +struct evl_sched_queue runnable;struct evl_timer refill_timer;struct evl_timer limit_timer;struct list_head groups;kernel/evl/sched/core.c +13
-7@@ -773,7 +773,8 @@ static inline void set_next_running(struct evl_rq *rq,evl_stop_timer (&rq->rrbtimer); } -static struct evl_thread *__pick_next_thread (struct evl_rq *rq) +static __always_inline struct evl_thread * +__pick_next_thread (struct evl_rq *rq) {struct evl_sched_class *sched_class;struct evl_thread *curr = rq->curr;@@ -821,8 +822,14 @@ static struct evl_thread *__pick_next_thread(struct evl_rq *rq)/* rq->curr->lock + rq->lock held, hard irqs off. */ static struct evl_thread *pick_next_thread (struct evl_rq *rq) { -struct evl_thread *next =__pick_next_thread (rq); +struct evl_thread *next, *prev = rq->curr; +struct evl_sched_class *prev_class = prev->sched_class; +if (prev_class->sched_out) + prev_class->sched_out (prev); + + next =__pick_next_thread (rq); +trace_evl_pick_thread (next);set_next_running (rq, next);return next;@@ -972,21 +979,20 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */return ; } + prev = curr; next =pick_next_thread (this_rq); -trace_evl_pick_thread (next); -if (next == curr) { -if (unlikely (next->state &EVL_T_ROOT )) { +if (next == prev) { +if (unlikely (prev->state &EVL_T_ROOT )) {if (this_rq->local_flags &RQ_TPROXY )evl_notify_proxy_tick (this_rq);if (this_rq->local_flags &RQ_TDEFER )evl_program_local_tick (&evl_mono_clock); }raw_spin_unlock (&this_rq->lock); -raw_spin_unlock_irqrestore (&curr->lock, flags); +raw_spin_unlock_irqrestore (&prev->lock, flags);return ; } - prev = curr; this_rq->curr = next; leaving_inband = false;kernel/evl/sched/quota.c +57
-63@@ -12,45 +12,35 @@#include <uapi/evl/sched-abi.h> /* -* With this policy, each per-CPU runqueue maintains a list of active -* thread groups for the sched_fifo class. -* -* Each time a thread is picked from the runqueue, we check whether we -* still have budget for running it, looking at the group it belongs -* to. If so, a timer is armed to elapse when that group has no more -* budget, would the incoming thread run unpreempted until then -* (i.e. evl_quota->limit_timer). +* Each time a thread is picked from the ->runnable queue, we check +* whether the group it belongs to still has runtime budget. If so, a +* timer is armed to fire when that group has no more budget, would +* the incoming thread run unpreempted until then +* (i.e. quota->limit_timer). * * Otherwise, if no budget remains in the group for running the * candidate thread, we move the latter to a local expiry queue * maintained by the group. This process is done on the fly as we pull -* from the runqueue. +* from the ->runnable queue. * -* Updating the remaining budget is done each time the EVL core asks -* for replacing the current thread with the next runnable one, -* i.e. evl_quota_pick(). There we charge the elapsed run time of the -* outgoing thread to the relevant group, and conversely, we check -* whether the incoming thread has budget. +* Updating the remaining budget is done each time the EVL core +* schedules out a thread undergoing the quota scheduling policy, * -* Finally, a per-CPU timer (evl_quota->refill_timer) periodically -* ticks in the background, in accordance to the defined quota -* interval. Thread group budgets get replenished by its handler in -* accordance to their respective share, pushing all expired threads -* back to the run queue in the same move. +* Finally, a per-CPU timer (quota->refill_timer) periodically ticks +* in the background, in accordance to the defined quota interval, +* replenishing per-group budgets, pushing all expired threads back to +* the quota ->runqueue too. * -* NOTE: since the core logic enforcing the budget entirely happens in -* evl_quota_pick(), applying a budget change can be done as simply as -* forcing the rescheduling procedure to be invoked asap. As a result -* of this, the EVL core will ask for the next thread to run, which -* means calling evl_quota_pick() eventually. +* NOTE: forcing a call to the rescheduling procedure is enoiugh to +* apply a budget change. * -* CAUTION: evl_quota_group->nr_active does count both the threads -* from that group linked to the sched_fifo runqueue, _and_ the -* threads moved to the local expiry queue. As a matter of fact, the -* expired threads - those for which we consumed all the per-group -* budget - are still seen as runnable (i.e. not blocked/suspended) by -* the EVL core. This only means that the SCHED_QUOTA policy won't -* pick them until the corresponding budget is replenished. +* CAUTION: quota_group->nr_active does count both the threads from +* that group linked to the runnable queue, _and_ the threads moved to +* the local expiry queue. As a matter of fact, the expired threads - +* those for which we consumed all the per-group budget - are still +* seen as runnable (i.e. not blocked/suspended) by the EVL core. This +* only means that the SCHED_QUOTA policy won't pick them until the +* corresponding budget is replenished. */ #define MAX_QUOTA_GROUPS 1024@@ -61,15 +51,19 @@ static DECLARE_BITMAP(group_map, MAX_QUOTA_GROUPS);static LIST_HEAD (group_list); -static inline bool thread_on_quota (struct evl_thread *thread, -struct evl_quota_group *tg) +static inline bool current_on_quota (struct evl_quota_group *tg) { -/* -* Check whether @thread is running on some CPU, and belongs -* to quota group @tg. -*/ -return thread->quota == tg && - !(thread->state & (EVL_T_READY |EVL_THREAD_BLOCK_MASK )); +struct evl_rq *rq = tg->rq; +struct evl_thread *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; + +if (curr->quota != tg) +return false; + +if (curr->state & (EVL_T_READY |EVL_T_KICKED |EVL_THREAD_BLOCK_MASK )) +return false; + +return evl_timer_is_running (&qs->limit_timer); }static inline bool group_is_active (struct evl_quota_group *tg)@@ -82,7 +76,7 @@ static inline bool group_is_active(struct evl_quota_group *tg)* runqueue, in which case tg->nr_active already accounted for * it. */ -return thread_on_quota (tg->rq->curr, tg); +return current_on_quota (tg); } static inline void replenish_budget(struct evl_sched_quota *qs,@@ -134,10 +128,10 @@ static inline void replenish_budget(struct evl_sched_quota *qs, }else if (tg->run_credit) { credit =ktime_sub (tg->quota_peak, budget);/* Consume the accumulated credit. */ -if (tg->run_credit >= credit) +if (tg->run_credit >= credit) { tg->run_credit =ktime_sub (tg->run_credit, credit); -else { + }else { credit = tg->run_credit; tg->run_credit =0 ; }@@ -150,8 +144,8 @@ static inline void replenish_budget(struct evl_sched_quota *qs,static void quota_refill_handler (struct evl_timer *timer)/* oob stage stalled */ { -struct evl_quota_group *tg;struct evl_thread *thread, *tmp; +struct evl_quota_group *tg;struct evl_sched_quota *qs;struct evl_rq *rq;@@ -167,7 +161,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *if (tg->run_budget ==0 ||list_empty (&tg->expired))continue ;/* -* For each group living on this CPU, move all expired +* For each group pinned on this CPU, move all expired * threads back to the runqueue. Since those threads * were moved out of the runqueue as we were * considering them for execution, we push them back @@ -178,7 +172,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }@@ -195,7 +189,7 @@ static void quota_limit_handler(struct evl_timer *timer) /* oob stage stalled *//* * Force a rescheduling on the return path of the current * interrupt, so that the budget is re-evaluated for the -* current group in evl_quota_pick(). +* current group in quota_pick(). */ raw_spin_lock (&rq->lock);evl_set_self_resched (rq);@@ -221,6 +215,7 @@ static void quota_init(struct evl_rq *rq) {struct evl_sched_quota *qs = &rq->quota; +evl_init_schedq (&qs->runnable); qs->period = quota_period;INIT_LIST_HEAD (&qs->groups);@@ -337,8 +332,8 @@ static void quota_forget(struct evl_thread *thread)static void quota_kick (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;/* * Allow a kicked thread to be elected for running until it @@ -347,7 +342,7 @@ static void quota_kick(struct evl_thread *thread)*/ if (tg->run_budget ==0 && !list_empty (&thread->quota_expired)) {list_del_init (&thread->quota_expired); -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); } }@@ -358,79 +353,82 @@ static inline int thread_is_runnable(struct evl_thread *thread)static void quota_enqueue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add_tail (&thread->quota_expired, &tg->expired);else -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); tg->nr_active++; }static void quota_dequeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!list_empty (&thread->quota_expired))list_del_init (&thread->quota_expired);else -evl_del_schedq (&rq->fifo.runnable, thread); +evl_del_schedq (&qs->runnable, thread); tg->nr_active--; }static void quota_requeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add (&thread->quota_expired, &tg->expired);else -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); tg->nr_active++; } -static struct evl_thread *quota_pick (struct evl_rq *rq) +static void quota_out (struct evl_thread *thread) { -struct evl_thread *next, *curr = rq->curr; -struct evl_sched_quota *qs = &rq->quota; -struct evl_quota_group *otg, *tg; -ktime_t now, elapsed; +struct evl_sched_quota *qs = &thread->rq->quota; +struct evl_quota_group *tg = thread->quota; +ktime_t now, consumed; + +/* Timer off means that we are not tracking quota. */ +if (!evl_timer_is_running (&qs->limit_timer)) +return ; - now =evl_ktime_monotonic (); - otg = curr->quota; -if (otg ==NULL ) - goto pick;kernel/evl/sched/quota.c static inline int thread_is_runnable(struct evl_thread *thread) · line 407
Quoted 10 lines
kernel/evl/sched/quota.c +1
-5/* * Charge the time consumed by the outgoing thread to the * group it belongs to. */ - elapsed =ktime_sub (now, otg->run_start); -if (elapsed < otg->run_budget) - otg->run_budget =ktime_sub (otg->run_budget, elapsed); -else - otg->run_budget =0 ; + now =evl_ktime_monotonic ();Thing brins some, though minor, glitch when doing the
run_startupdate for the next group already in pick. That's why I'm trying to reuse that stamp when available.Quoted 139 lines
kernel/evl/sched/quota.c +40
-36+ consumed =ktime_sub (now, tg->run_start); +if (consumed < tg->run_budget) { + tg->run_budget =ktime_sub (tg->run_budget, consumed); + }else { + tg->run_budget =0 ; +evl_stop_timer (&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick (struct evl_rq *rq) +{ +struct evl_thread *next, *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; +struct evl_quota_group *tg; + pick: - next =evl_get_schedq (&rq->fifo.runnable); + next =evl_get_schedq (&qs->runnable);if (next ==NULL ) {evl_stop_timer (&qs->limit_timer);return NULL ; } -/* -* As we basically piggyback on the SCHED_FIFO runqueue, make -* sure to detect non-quota threads. -*/ tg = next->quota; -if (tg ==NULL ) -return next; - - tg->run_start = now; + tg->nr_active--;/* * Don't consider budget if kicked, we have to allow this @@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)*/ if (next->info &EVL_T_KICKED ) {evl_stop_timer (&qs->limit_timer); - goto out; +return next; } +/* +* __pick_next_thread() might have requeued the current +* thread which is still leading the pack. +*/ +if (curr == next) +return next; +if (ktime_to_ns (tg->run_budget) ==0 ) { -/* Flush expired group members as we go. */ +/* Park expired group members as we go. */ list_add_tail (&next->quota_expired, &tg->expired); goto pick; } -if (otg == tg &&evl_timer_is_running (&qs->limit_timer)) -/* Same group, leave the running timer untouched. */ - goto out; - -/* Arm limit timer for the new running group. */ -evl_start_timer (&qs->limit_timer, -ktime_add (now, tg->run_budget), -EVL_INFINITE ); -out: - tg->nr_active--; +/* Arm new limit timer on change of running group. */ +if (curr->quota != tg || !evl_timer_is_running (&qs->limit_timer)) { + tg->run_start =evl_ktime_monotonic (); +evl_start_timer (&qs->limit_timer, +ktime_add (tg->run_start, tg->run_budget), +EVL_INFINITE ); + }return next; }@@ -542,7 +544,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* Unregister the group before we drop rq->lock. As a result, * it won't accept threads anymore while we are busy moving * the current members to the fifo class, and concurrent -* evl_quota_remove requests would receive -EINVAL. +* quota_remove requests would receive -EINVAL. */ __clear_bit (tg->tgid, group_map);list_del (&tg->next);@@ -556,7 +558,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* hold rq->lock on entry, we do a trylock dance to prevent an * ABBA issue. No livelock is possible since we unregistered * that group already, so &tg->members can only be depleted -* (by this loop specifically). +* (by this loop exclusively). */ while (!list_empty (&tg->members)) {@@ -583,10 +585,10 @@ static void quota_set_limit(struct evl_quota_group *tg, int *quota_sum_r) {struct evl_rq *rq = tg->rq; -struct evl_thread *thread, *tmp, *curr = rq->curr; +struct evl_thread *thread, *tmp;struct evl_sched_quota *qs = &rq->quota; -ktime_t now, elapsed, consumed;ktime_t old_quota = tg->quota; +ktime_t consumed;u64 n;assert_hard_lock (&rq->lock);@@ -615,16 +617,8 @@ static void quota_set_limit(struct evl_quota_group *tg, tg->quota_percent = quota_percent; tg->quota_peak_percent = quota_peak_percent; -if (thread_on_quota (curr, tg)) { - now =evl_ktime_monotonic (); - - elapsed = now - tg->run_start; -if (elapsed < tg->run_budget) - tg->run_budget -= elapsed; -else - tg->run_budget =0 ; - - tg->run_start = now; +if (current_on_quota (tg)) { +quota_out (rq->curr);evl_stop_timer (&qs->limit_timer); }@@ -646,7 +640,7 @@ static void quota_set_limit(struct evl_quota_group *tg,list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }kernel/evl/sched/quota.c struct evl_sched_class evl_sched_quota = { · line 786
Quoted 7 lines
kernel/evl/sched/quota.c +1
-0@@ -786,6 +780,7 @@ struct evl_sched_class evl_sched_quota = { .sched_dequeue = quota_dequeue, .sched_requeue = quota_requeue, .sched_pick = quota_pick, + .sched_out = quota_out, .sched_migrate = quota_migrate, .sched_chkparam = quota_chkparam, .sched_setparam = quota_setparam,You also seem to miss the case I discovered only yesterday as well: When starting the current thread on a quota via setparam, we also need to set its
run_startstamp at that point.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
run_startshould be meaningful only when a limit timer runs.Right, but are you sure it will be started in the case I described...?
To be started, it must be picked, so yes.
The way the original implementation dealt with ->run_start was a mess (proudly brought to you by /me). The basic idea for fixing this is to acknowledge that we have no reason to have a limit timer running unless we have a group to charge with the budget consumed by the current thread. Changing the code to reflect that basic point makes it much clearer I believe.
Signature · 1 line
Philippe.
Quoted 594 lines · Philippe Gerum
Quoted 592 lines · Jan Kiszka
Quoted 591 lines · Philippe Gerum
Quoted 588 lines · Jan Kiszka
Quoted 58 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
Quoted 4 lines
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).Please avoid that for the final patch. I already had troubles telling substantial from cosmetical changes apart in your lazy-blocking commit (>50% unrelated changes in there).
Quoted 12 lines
commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.Quoted 5 lines
To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO....and why we need a separate queue now? Please provide reasoning for changes.
Quoted 334 lines
Signed-off-by Philippe Gerum <[email protected]>include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);include/evl/sched/quota.h +1
-0@@ -40,6 +40,7 @@ struct evl_quota_group {struct evl_sched_quota {ktime_t period; +struct evl_sched_queue runnable;struct evl_timer refill_timer;struct evl_timer limit_timer;struct list_head groups;kernel/evl/sched/core.c +13
-7@@ -773,7 +773,8 @@ static inline void set_next_running(struct evl_rq *rq,evl_stop_timer (&rq->rrbtimer); } -static struct evl_thread *__pick_next_thread (struct evl_rq *rq) +static __always_inline struct evl_thread * +__pick_next_thread (struct evl_rq *rq) {struct evl_sched_class *sched_class;struct evl_thread *curr = rq->curr;@@ -821,8 +822,14 @@ static struct evl_thread *__pick_next_thread(struct evl_rq *rq)/* rq->curr->lock + rq->lock held, hard irqs off. */ static struct evl_thread *pick_next_thread (struct evl_rq *rq) { -struct evl_thread *next =__pick_next_thread (rq); +struct evl_thread *next, *prev = rq->curr; +struct evl_sched_class *prev_class = prev->sched_class; +if (prev_class->sched_out) + prev_class->sched_out (prev); + + next =__pick_next_thread (rq); +trace_evl_pick_thread (next);set_next_running (rq, next);return next;@@ -972,21 +979,20 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */return ; } + prev = curr; next =pick_next_thread (this_rq); -trace_evl_pick_thread (next); -if (next == curr) { -if (unlikely (next->state &EVL_T_ROOT )) { +if (next == prev) { +if (unlikely (prev->state &EVL_T_ROOT )) {if (this_rq->local_flags &RQ_TPROXY )evl_notify_proxy_tick (this_rq);if (this_rq->local_flags &RQ_TDEFER )evl_program_local_tick (&evl_mono_clock); }raw_spin_unlock (&this_rq->lock); -raw_spin_unlock_irqrestore (&curr->lock, flags); +raw_spin_unlock_irqrestore (&prev->lock, flags);return ; } - prev = curr; this_rq->curr = next; leaving_inband = false;kernel/evl/sched/quota.c +57
-63@@ -12,45 +12,35 @@#include <uapi/evl/sched-abi.h> /* -* With this policy, each per-CPU runqueue maintains a list of active -* thread groups for the sched_fifo class. -* -* Each time a thread is picked from the runqueue, we check whether we -* still have budget for running it, looking at the group it belongs -* to. If so, a timer is armed to elapse when that group has no more -* budget, would the incoming thread run unpreempted until then -* (i.e. evl_quota->limit_timer). +* Each time a thread is picked from the ->runnable queue, we check +* whether the group it belongs to still has runtime budget. If so, a +* timer is armed to fire when that group has no more budget, would +* the incoming thread run unpreempted until then +* (i.e. quota->limit_timer). * * Otherwise, if no budget remains in the group for running the * candidate thread, we move the latter to a local expiry queue * maintained by the group. This process is done on the fly as we pull -* from the runqueue. +* from the ->runnable queue. * -* Updating the remaining budget is done each time the EVL core asks -* for replacing the current thread with the next runnable one, -* i.e. evl_quota_pick(). There we charge the elapsed run time of the -* outgoing thread to the relevant group, and conversely, we check -* whether the incoming thread has budget. +* Updating the remaining budget is done each time the EVL core +* schedules out a thread undergoing the quota scheduling policy, * -* Finally, a per-CPU timer (evl_quota->refill_timer) periodically -* ticks in the background, in accordance to the defined quota -* interval. Thread group budgets get replenished by its handler in -* accordance to their respective share, pushing all expired threads -* back to the run queue in the same move. +* Finally, a per-CPU timer (quota->refill_timer) periodically ticks +* in the background, in accordance to the defined quota interval, +* replenishing per-group budgets, pushing all expired threads back to +* the quota ->runqueue too. * -* NOTE: since the core logic enforcing the budget entirely happens in -* evl_quota_pick(), applying a budget change can be done as simply as -* forcing the rescheduling procedure to be invoked asap. As a result -* of this, the EVL core will ask for the next thread to run, which -* means calling evl_quota_pick() eventually. +* NOTE: forcing a call to the rescheduling procedure is enoiugh to +* apply a budget change. * -* CAUTION: evl_quota_group->nr_active does count both the threads -* from that group linked to the sched_fifo runqueue, _and_ the -* threads moved to the local expiry queue. As a matter of fact, the -* expired threads - those for which we consumed all the per-group -* budget - are still seen as runnable (i.e. not blocked/suspended) by -* the EVL core. This only means that the SCHED_QUOTA policy won't -* pick them until the corresponding budget is replenished. +* CAUTION: quota_group->nr_active does count both the threads from +* that group linked to the runnable queue, _and_ the threads moved to +* the local expiry queue. As a matter of fact, the expired threads - +* those for which we consumed all the per-group budget - are still +* seen as runnable (i.e. not blocked/suspended) by the EVL core. This +* only means that the SCHED_QUOTA policy won't pick them until the +* corresponding budget is replenished. */ #define MAX_QUOTA_GROUPS 1024@@ -61,15 +51,19 @@ static DECLARE_BITMAP(group_map, MAX_QUOTA_GROUPS);static LIST_HEAD (group_list); -static inline bool thread_on_quota (struct evl_thread *thread, -struct evl_quota_group *tg) +static inline bool current_on_quota (struct evl_quota_group *tg) { -/* -* Check whether @thread is running on some CPU, and belongs -* to quota group @tg. -*/ -return thread->quota == tg && - !(thread->state & (EVL_T_READY |EVL_THREAD_BLOCK_MASK )); +struct evl_rq *rq = tg->rq; +struct evl_thread *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; + +if (curr->quota != tg) +return false; + +if (curr->state & (EVL_T_READY |EVL_T_KICKED |EVL_THREAD_BLOCK_MASK )) +return false; + +return evl_timer_is_running (&qs->limit_timer); }static inline bool group_is_active (struct evl_quota_group *tg)@@ -82,7 +76,7 @@ static inline bool group_is_active(struct evl_quota_group *tg)* runqueue, in which case tg->nr_active already accounted for * it. */ -return thread_on_quota (tg->rq->curr, tg); +return current_on_quota (tg); } static inline void replenish_budget(struct evl_sched_quota *qs,@@ -134,10 +128,10 @@ static inline void replenish_budget(struct evl_sched_quota *qs, }else if (tg->run_credit) { credit =ktime_sub (tg->quota_peak, budget);/* Consume the accumulated credit. */ -if (tg->run_credit >= credit) +if (tg->run_credit >= credit) { tg->run_credit =ktime_sub (tg->run_credit, credit); -else { + }else { credit = tg->run_credit; tg->run_credit =0 ; }@@ -150,8 +144,8 @@ static inline void replenish_budget(struct evl_sched_quota *qs,static void quota_refill_handler (struct evl_timer *timer)/* oob stage stalled */ { -struct evl_quota_group *tg;struct evl_thread *thread, *tmp; +struct evl_quota_group *tg;struct evl_sched_quota *qs;struct evl_rq *rq;@@ -167,7 +161,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *if (tg->run_budget ==0 ||list_empty (&tg->expired))continue ;/* -* For each group living on this CPU, move all expired +* For each group pinned on this CPU, move all expired * threads back to the runqueue. Since those threads * were moved out of the runqueue as we were * considering them for execution, we push them back @@ -178,7 +172,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }@@ -195,7 +189,7 @@ static void quota_limit_handler(struct evl_timer *timer) /* oob stage stalled *//* * Force a rescheduling on the return path of the current * interrupt, so that the budget is re-evaluated for the -* current group in evl_quota_pick(). +* current group in quota_pick(). */ raw_spin_lock (&rq->lock);evl_set_self_resched (rq);@@ -221,6 +215,7 @@ static void quota_init(struct evl_rq *rq) {struct evl_sched_quota *qs = &rq->quota; +evl_init_schedq (&qs->runnable); qs->period = quota_period;INIT_LIST_HEAD (&qs->groups);@@ -337,8 +332,8 @@ static void quota_forget(struct evl_thread *thread)static void quota_kick (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;/* * Allow a kicked thread to be elected for running until it @@ -347,7 +342,7 @@ static void quota_kick(struct evl_thread *thread)*/ if (tg->run_budget ==0 && !list_empty (&thread->quota_expired)) {list_del_init (&thread->quota_expired); -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); } }@@ -358,79 +353,82 @@ static inline int thread_is_runnable(struct evl_thread *thread)static void quota_enqueue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add_tail (&thread->quota_expired, &tg->expired);else -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); tg->nr_active++; }static void quota_dequeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!list_empty (&thread->quota_expired))list_del_init (&thread->quota_expired);else -evl_del_schedq (&rq->fifo.runnable, thread); +evl_del_schedq (&qs->runnable, thread); tg->nr_active--; }static void quota_requeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add (&thread->quota_expired, &tg->expired);else -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); tg->nr_active++; } -static struct evl_thread *quota_pick (struct evl_rq *rq) +static void quota_out (struct evl_thread *thread) { -struct evl_thread *next, *curr = rq->curr; -struct evl_sched_quota *qs = &rq->quota; -struct evl_quota_group *otg, *tg; -ktime_t now, elapsed; +struct evl_sched_quota *qs = &thread->rq->quota; +struct evl_quota_group *tg = thread->quota; +ktime_t now, consumed; + +/* Timer off means that we are not tracking quota. */ +if (!evl_timer_is_running (&qs->limit_timer)) +return ; - now =evl_ktime_monotonic (); - otg = curr->quota; -if (otg ==NULL ) - goto pick;kernel/evl/sched/quota.c static inline int thread_is_runnable(struct evl_thread *thread) · line 407
Quoted 10 lines
kernel/evl/sched/quota.c +1
-5/* * Charge the time consumed by the outgoing thread to the * group it belongs to. */ - elapsed =ktime_sub (now, otg->run_start); -if (elapsed < otg->run_budget) - otg->run_budget =ktime_sub (otg->run_budget, elapsed); -else - otg->run_budget =0 ; + now =evl_ktime_monotonic ();Thing brins some, though minor, glitch when doing the
run_startupdate for the next group already in pick. That's why I'm trying to reuse that stamp when available.Quoted 139 lines
kernel/evl/sched/quota.c +40
-36+ consumed =ktime_sub (now, tg->run_start); +if (consumed < tg->run_budget) { + tg->run_budget =ktime_sub (tg->run_budget, consumed); + }else { + tg->run_budget =0 ; +evl_stop_timer (&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick (struct evl_rq *rq) +{ +struct evl_thread *next, *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; +struct evl_quota_group *tg; + pick: - next =evl_get_schedq (&rq->fifo.runnable); + next =evl_get_schedq (&qs->runnable);if (next ==NULL ) {evl_stop_timer (&qs->limit_timer);return NULL ; } -/* -* As we basically piggyback on the SCHED_FIFO runqueue, make -* sure to detect non-quota threads. -*/ tg = next->quota; -if (tg ==NULL ) -return next; - - tg->run_start = now; + tg->nr_active--;/* * Don't consider budget if kicked, we have to allow this @@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)*/ if (next->info &EVL_T_KICKED ) {evl_stop_timer (&qs->limit_timer); - goto out; +return next; } +/* +* __pick_next_thread() might have requeued the current +* thread which is still leading the pack. +*/ +if (curr == next) +return next; +if (ktime_to_ns (tg->run_budget) ==0 ) { -/* Flush expired group members as we go. */ +/* Park expired group members as we go. */ list_add_tail (&next->quota_expired, &tg->expired); goto pick; } -if (otg == tg &&evl_timer_is_running (&qs->limit_timer)) -/* Same group, leave the running timer untouched. */ - goto out; - -/* Arm limit timer for the new running group. */ -evl_start_timer (&qs->limit_timer, -ktime_add (now, tg->run_budget), -EVL_INFINITE ); -out: - tg->nr_active--; +/* Arm new limit timer on change of running group. */ +if (curr->quota != tg || !evl_timer_is_running (&qs->limit_timer)) { + tg->run_start =evl_ktime_monotonic (); +evl_start_timer (&qs->limit_timer, +ktime_add (tg->run_start, tg->run_budget), +EVL_INFINITE ); + }return next; }@@ -542,7 +544,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* Unregister the group before we drop rq->lock. As a result, * it won't accept threads anymore while we are busy moving * the current members to the fifo class, and concurrent -* evl_quota_remove requests would receive -EINVAL. +* quota_remove requests would receive -EINVAL. */ __clear_bit (tg->tgid, group_map);list_del (&tg->next);@@ -556,7 +558,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* hold rq->lock on entry, we do a trylock dance to prevent an * ABBA issue. No livelock is possible since we unregistered * that group already, so &tg->members can only be depleted -* (by this loop specifically). +* (by this loop exclusively). */ while (!list_empty (&tg->members)) {@@ -583,10 +585,10 @@ static void quota_set_limit(struct evl_quota_group *tg, int *quota_sum_r) {struct evl_rq *rq = tg->rq; -struct evl_thread *thread, *tmp, *curr = rq->curr; +struct evl_thread *thread, *tmp;struct evl_sched_quota *qs = &rq->quota; -ktime_t now, elapsed, consumed;ktime_t old_quota = tg->quota; +ktime_t consumed;u64 n;assert_hard_lock (&rq->lock);@@ -615,16 +617,8 @@ static void quota_set_limit(struct evl_quota_group *tg, tg->quota_percent = quota_percent; tg->quota_peak_percent = quota_peak_percent; -if (thread_on_quota (curr, tg)) { - now =evl_ktime_monotonic (); - - elapsed = now - tg->run_start; -if (elapsed < tg->run_budget) - tg->run_budget -= elapsed; -else - tg->run_budget =0 ; - - tg->run_start = now; +if (current_on_quota (tg)) { +quota_out (rq->curr);evl_stop_timer (&qs->limit_timer); }@@ -646,7 +640,7 @@ static void quota_set_limit(struct evl_quota_group *tg,list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }kernel/evl/sched/quota.c struct evl_sched_class evl_sched_quota = { · line 786
Quoted 7 lines
kernel/evl/sched/quota.c +1
-0@@ -786,6 +780,7 @@ struct evl_sched_class evl_sched_quota = { .sched_dequeue = quota_dequeue, .sched_requeue = quota_requeue, .sched_pick = quota_pick, + .sched_out = quota_out, .sched_migrate = quota_migrate, .sched_chkparam = quota_chkparam, .sched_setparam = quota_setparam,You also seem to miss the case I discovered only yesterday as well: When starting the current thread on a quota via setparam, we also need to set its
run_startstamp at that point.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
run_startshould be meaningful only when a limit timer runs.Right, but are you sure it will be started in the case I described...?
To be started, it must be picked, so yes.
But prev == next then - nothing new to pick, no?
But let me apply your patch and stop only looking at the diff.
Jan
Signature · 2 lines
Siemens AG, Foundational Technologies Linux Expert Center
Quoted 601 lines · Jan Kiszka
Quoted 600 lines · Philippe Gerum
Quoted 597 lines · Jan Kiszka
Quoted 591 lines · Philippe Gerum
Quoted 575 lines · Jan Kiszka
Quoted 58 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
Quoted 4 lines
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).Please avoid that for the final patch. I already had troubles telling substantial from cosmetical changes apart in your lazy-blocking commit (>50% unrelated changes in there).
Quoted 12 lines
commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.Quoted 5 lines
To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO....and why we need a separate queue now? Please provide reasoning for changes.
Quoted 334 lines
Signed-off-by Philippe Gerum <[email protected]>include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);include/evl/sched/quota.h +1
-0@@ -40,6 +40,7 @@ struct evl_quota_group {struct evl_sched_quota {ktime_t period; +struct evl_sched_queue runnable;struct evl_timer refill_timer;struct evl_timer limit_timer;struct list_head groups;kernel/evl/sched/core.c +13
-7@@ -773,7 +773,8 @@ static inline void set_next_running(struct evl_rq *rq,evl_stop_timer (&rq->rrbtimer); } -static struct evl_thread *__pick_next_thread (struct evl_rq *rq) +static __always_inline struct evl_thread * +__pick_next_thread (struct evl_rq *rq) {struct evl_sched_class *sched_class;struct evl_thread *curr = rq->curr;@@ -821,8 +822,14 @@ static struct evl_thread *__pick_next_thread(struct evl_rq *rq)/* rq->curr->lock + rq->lock held, hard irqs off. */ static struct evl_thread *pick_next_thread (struct evl_rq *rq) { -struct evl_thread *next =__pick_next_thread (rq); +struct evl_thread *next, *prev = rq->curr; +struct evl_sched_class *prev_class = prev->sched_class; +if (prev_class->sched_out) + prev_class->sched_out (prev); + + next =__pick_next_thread (rq); +trace_evl_pick_thread (next);set_next_running (rq, next);return next;@@ -972,21 +979,20 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */return ; } + prev = curr; next =pick_next_thread (this_rq); -trace_evl_pick_thread (next); -if (next == curr) { -if (unlikely (next->state &EVL_T_ROOT )) { +if (next == prev) { +if (unlikely (prev->state &EVL_T_ROOT )) {if (this_rq->local_flags &RQ_TPROXY )evl_notify_proxy_tick (this_rq);if (this_rq->local_flags &RQ_TDEFER )evl_program_local_tick (&evl_mono_clock); }raw_spin_unlock (&this_rq->lock); -raw_spin_unlock_irqrestore (&curr->lock, flags); +raw_spin_unlock_irqrestore (&prev->lock, flags);return ; } - prev = curr; this_rq->curr = next; leaving_inband = false;kernel/evl/sched/quota.c +57
-63@@ -12,45 +12,35 @@#include <uapi/evl/sched-abi.h> /* -* With this policy, each per-CPU runqueue maintains a list of active -* thread groups for the sched_fifo class. -* -* Each time a thread is picked from the runqueue, we check whether we -* still have budget for running it, looking at the group it belongs -* to. If so, a timer is armed to elapse when that group has no more -* budget, would the incoming thread run unpreempted until then -* (i.e. evl_quota->limit_timer). +* Each time a thread is picked from the ->runnable queue, we check +* whether the group it belongs to still has runtime budget. If so, a +* timer is armed to fire when that group has no more budget, would +* the incoming thread run unpreempted until then +* (i.e. quota->limit_timer). * * Otherwise, if no budget remains in the group for running the * candidate thread, we move the latter to a local expiry queue * maintained by the group. This process is done on the fly as we pull -* from the runqueue. +* from the ->runnable queue. * -* Updating the remaining budget is done each time the EVL core asks -* for replacing the current thread with the next runnable one, -* i.e. evl_quota_pick(). There we charge the elapsed run time of the -* outgoing thread to the relevant group, and conversely, we check -* whether the incoming thread has budget. +* Updating the remaining budget is done each time the EVL core +* schedules out a thread undergoing the quota scheduling policy, * -* Finally, a per-CPU timer (evl_quota->refill_timer) periodically -* ticks in the background, in accordance to the defined quota -* interval. Thread group budgets get replenished by its handler in -* accordance to their respective share, pushing all expired threads -* back to the run queue in the same move. +* Finally, a per-CPU timer (quota->refill_timer) periodically ticks +* in the background, in accordance to the defined quota interval, +* replenishing per-group budgets, pushing all expired threads back to +* the quota ->runqueue too. * -* NOTE: since the core logic enforcing the budget entirely happens in -* evl_quota_pick(), applying a budget change can be done as simply as -* forcing the rescheduling procedure to be invoked asap. As a result -* of this, the EVL core will ask for the next thread to run, which -* means calling evl_quota_pick() eventually. +* NOTE: forcing a call to the rescheduling procedure is enoiugh to +* apply a budget change. * -* CAUTION: evl_quota_group->nr_active does count both the threads -* from that group linked to the sched_fifo runqueue, _and_ the -* threads moved to the local expiry queue. As a matter of fact, the -* expired threads - those for which we consumed all the per-group -* budget - are still seen as runnable (i.e. not blocked/suspended) by -* the EVL core. This only means that the SCHED_QUOTA policy won't -* pick them until the corresponding budget is replenished. +* CAUTION: quota_group->nr_active does count both the threads from +* that group linked to the runnable queue, _and_ the threads moved to +* the local expiry queue. As a matter of fact, the expired threads - +* those for which we consumed all the per-group budget - are still +* seen as runnable (i.e. not blocked/suspended) by the EVL core. This +* only means that the SCHED_QUOTA policy won't pick them until the +* corresponding budget is replenished. */ #define MAX_QUOTA_GROUPS 1024@@ -61,15 +51,19 @@ static DECLARE_BITMAP(group_map, MAX_QUOTA_GROUPS);static LIST_HEAD (group_list); -static inline bool thread_on_quota (struct evl_thread *thread, -struct evl_quota_group *tg) +static inline bool current_on_quota (struct evl_quota_group *tg) { -/* -* Check whether @thread is running on some CPU, and belongs -* to quota group @tg. -*/ -return thread->quota == tg && - !(thread->state & (EVL_T_READY |EVL_THREAD_BLOCK_MASK )); +struct evl_rq *rq = tg->rq; +struct evl_thread *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; + +if (curr->quota != tg) +return false; + +if (curr->state & (EVL_T_READY |EVL_T_KICKED |EVL_THREAD_BLOCK_MASK )) +return false; + +return evl_timer_is_running (&qs->limit_timer); }static inline bool group_is_active (struct evl_quota_group *tg)@@ -82,7 +76,7 @@ static inline bool group_is_active(struct evl_quota_group *tg)* runqueue, in which case tg->nr_active already accounted for * it. */ -return thread_on_quota (tg->rq->curr, tg); +return current_on_quota (tg); } static inline void replenish_budget(struct evl_sched_quota *qs,@@ -134,10 +128,10 @@ static inline void replenish_budget(struct evl_sched_quota *qs, }else if (tg->run_credit) { credit =ktime_sub (tg->quota_peak, budget);/* Consume the accumulated credit. */ -if (tg->run_credit >= credit) +if (tg->run_credit >= credit) { tg->run_credit =ktime_sub (tg->run_credit, credit); -else { + }else { credit = tg->run_credit; tg->run_credit =0 ; }@@ -150,8 +144,8 @@ static inline void replenish_budget(struct evl_sched_quota *qs,static void quota_refill_handler (struct evl_timer *timer)/* oob stage stalled */ { -struct evl_quota_group *tg;struct evl_thread *thread, *tmp; +struct evl_quota_group *tg;struct evl_sched_quota *qs;struct evl_rq *rq;@@ -167,7 +161,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *if (tg->run_budget ==0 ||list_empty (&tg->expired))continue ;/* -* For each group living on this CPU, move all expired +* For each group pinned on this CPU, move all expired * threads back to the runqueue. Since those threads * were moved out of the runqueue as we were * considering them for execution, we push them back @@ -178,7 +172,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }@@ -195,7 +189,7 @@ static void quota_limit_handler(struct evl_timer *timer) /* oob stage stalled *//* * Force a rescheduling on the return path of the current * interrupt, so that the budget is re-evaluated for the -* current group in evl_quota_pick(). +* current group in quota_pick(). */ raw_spin_lock (&rq->lock);evl_set_self_resched (rq);@@ -221,6 +215,7 @@ static void quota_init(struct evl_rq *rq) {struct evl_sched_quota *qs = &rq->quota; +evl_init_schedq (&qs->runnable); qs->period = quota_period;INIT_LIST_HEAD (&qs->groups);@@ -337,8 +332,8 @@ static void quota_forget(struct evl_thread *thread)static void quota_kick (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;/* * Allow a kicked thread to be elected for running until it @@ -347,7 +342,7 @@ static void quota_kick(struct evl_thread *thread)*/ if (tg->run_budget ==0 && !list_empty (&thread->quota_expired)) {list_del_init (&thread->quota_expired); -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); } }@@ -358,79 +353,82 @@ static inline int thread_is_runnable(struct evl_thread *thread)static void quota_enqueue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add_tail (&thread->quota_expired, &tg->expired);else -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); tg->nr_active++; }static void quota_dequeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!list_empty (&thread->quota_expired))list_del_init (&thread->quota_expired);else -evl_del_schedq (&rq->fifo.runnable, thread); +evl_del_schedq (&qs->runnable, thread); tg->nr_active--; }static void quota_requeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add (&thread->quota_expired, &tg->expired);else -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); tg->nr_active++; } -static struct evl_thread *quota_pick (struct evl_rq *rq) +static void quota_out (struct evl_thread *thread) { -struct evl_thread *next, *curr = rq->curr; -struct evl_sched_quota *qs = &rq->quota; -struct evl_quota_group *otg, *tg; -ktime_t now, elapsed; +struct evl_sched_quota *qs = &thread->rq->quota; +struct evl_quota_group *tg = thread->quota; +ktime_t now, consumed; + +/* Timer off means that we are not tracking quota. */ +if (!evl_timer_is_running (&qs->limit_timer)) +return ; - now =evl_ktime_monotonic (); - otg = curr->quota; -if (otg ==NULL ) - goto pick;kernel/evl/sched/quota.c static inline int thread_is_runnable(struct evl_thread *thread) · line 407
Quoted 10 lines
kernel/evl/sched/quota.c +1
-5/* * Charge the time consumed by the outgoing thread to the * group it belongs to. */ - elapsed =ktime_sub (now, otg->run_start); -if (elapsed < otg->run_budget) - otg->run_budget =ktime_sub (otg->run_budget, elapsed); -else - otg->run_budget =0 ; + now =evl_ktime_monotonic ();Thing brins some, though minor, glitch when doing the
run_startupdate for the next group already in pick. That's why I'm trying to reuse that stamp when available.Quoted 137 lines
kernel/evl/sched/quota.c +40
-36+ consumed =ktime_sub (now, tg->run_start); +if (consumed < tg->run_budget) { + tg->run_budget =ktime_sub (tg->run_budget, consumed); + }else { + tg->run_budget =0 ; +evl_stop_timer (&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick (struct evl_rq *rq) +{ +struct evl_thread *next, *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; +struct evl_quota_group *tg; + pick: - next =evl_get_schedq (&rq->fifo.runnable); + next =evl_get_schedq (&qs->runnable);if (next ==NULL ) {evl_stop_timer (&qs->limit_timer);return NULL ; } -/* -* As we basically piggyback on the SCHED_FIFO runqueue, make -* sure to detect non-quota threads. -*/ tg = next->quota; -if (tg ==NULL ) -return next; - - tg->run_start = now; + tg->nr_active--;/* * Don't consider budget if kicked, we have to allow this @@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)*/ if (next->info &EVL_T_KICKED ) {evl_stop_timer (&qs->limit_timer); - goto out; +return next; } +/* +* __pick_next_thread() might have requeued the current +* thread which is still leading the pack. +*/ +if (curr == next) +return next; +if (ktime_to_ns (tg->run_budget) ==0 ) { -/* Flush expired group members as we go. */ +/* Park expired group members as we go. */ list_add_tail (&next->quota_expired, &tg->expired); goto pick; } -if (otg == tg &&evl_timer_is_running (&qs->limit_timer)) -/* Same group, leave the running timer untouched. */ - goto out; - -/* Arm limit timer for the new running group. */ -evl_start_timer (&qs->limit_timer, -ktime_add (now, tg->run_budget), -EVL_INFINITE ); -out: - tg->nr_active--; +/* Arm new limit timer on change of running group. */ +if (curr->quota != tg || !evl_timer_is_running (&qs->limit_timer)) { + tg->run_start =evl_ktime_monotonic (); +evl_start_timer (&qs->limit_timer, +ktime_add (tg->run_start, tg->run_budget), +EVL_INFINITE ); + }return next; }@@ -542,7 +544,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* Unregister the group before we drop rq->lock. As a result, * it won't accept threads anymore while we are busy moving * the current members to the fifo class, and concurrent -* evl_quota_remove requests would receive -EINVAL. +* quota_remove requests would receive -EINVAL. */ __clear_bit (tg->tgid, group_map);list_del (&tg->next);@@ -556,7 +558,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* hold rq->lock on entry, we do a trylock dance to prevent an * ABBA issue. No livelock is possible since we unregistered * that group already, so &tg->members can only be depleted -* (by this loop specifically). +* (by this loop exclusively). */ while (!list_empty (&tg->members)) {@@ -583,10 +585,10 @@ static void quota_set_limit(struct evl_quota_group *tg, int *quota_sum_r) {struct evl_rq *rq = tg->rq; -struct evl_thread *thread, *tmp, *curr = rq->curr; +struct evl_thread *thread, *tmp;struct evl_sched_quota *qs = &rq->quota; -ktime_t now, elapsed, consumed;ktime_t old_quota = tg->quota; +ktime_t consumed;u64 n;assert_hard_lock (&rq->lock);@@ -615,16 +617,8 @@ static void quota_set_limit(struct evl_quota_group *tg, tg->quota_percent = quota_percent; tg->quota_peak_percent = quota_peak_percent; -if (thread_on_quota (curr, tg)) { - now =evl_ktime_monotonic (); - - elapsed = now - tg->run_start; -if (elapsed < tg->run_budget) - tg->run_budget -= elapsed; -else - tg->run_budget =0 ; - - tg->run_start = now; +if (current_on_quota (tg)) { +quota_out (rq->curr);evl_stop_timer (&qs->limit_timer); }@@ -646,7 +640,7 @@ static void quota_set_limit(struct evl_quota_group *tg,list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }Quoted 13 lines
kernel/evl/sched/quota.c struct evl_sched_class evl_sched_quota = { · line 786
Quoted 9 lines
kernel/evl/sched/quota.c +1
-0@@ -786,6 +780,7 @@ struct evl_sched_class evl_sched_quota = { .sched_dequeue = quota_dequeue, .sched_requeue = quota_requeue, .sched_pick = quota_pick, + .sched_out = quota_out, .sched_migrate = quota_migrate, .sched_chkparam = quota_chkparam, .sched_setparam = quota_setparam,You also seem to miss the case I discovered only yesterday as well: When starting the current thread on a quota via setparam, we also need to set its
run_startstamp at that point.Quoted 2 lines
run_startshould be meaningful only when a limit timer runs.Right, but are you sure it will be started in the case I described...?
Quoted 7 lines
Quoted 2 lines
To be started, it must be picked, so yes.
But prev == next then - nothing new to pick, no?
But let me apply your patch and stop only looking at the diff.
Beware: not tested yet.
Signature · 1 line
Philippe.
Quoted 598 lines · Jan Kiszka
Quoted 597 lines · Philippe Gerum
Quoted 595 lines · Jan Kiszka
Quoted 593 lines · Philippe Gerum
Quoted 575 lines · Jan Kiszka
Quoted 58 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
Quoted 4 lines
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).Please avoid that for the final patch. I already had troubles telling substantial from cosmetical changes apart in your lazy-blocking commit (>50% unrelated changes in there).
Quoted 12 lines
commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.Quoted 5 lines
To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO....and why we need a separate queue now? Please provide reasoning for changes.
Quoted 334 lines
Signed-off-by Philippe Gerum <[email protected]>include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);include/evl/sched/quota.h +1
-0@@ -40,6 +40,7 @@ struct evl_quota_group {struct evl_sched_quota {ktime_t period; +struct evl_sched_queue runnable;struct evl_timer refill_timer;struct evl_timer limit_timer;struct list_head groups;kernel/evl/sched/core.c +13
-7@@ -773,7 +773,8 @@ static inline void set_next_running(struct evl_rq *rq,evl_stop_timer (&rq->rrbtimer); } -static struct evl_thread *__pick_next_thread (struct evl_rq *rq) +static __always_inline struct evl_thread * +__pick_next_thread (struct evl_rq *rq) {struct evl_sched_class *sched_class;struct evl_thread *curr = rq->curr;@@ -821,8 +822,14 @@ static struct evl_thread *__pick_next_thread(struct evl_rq *rq)/* rq->curr->lock + rq->lock held, hard irqs off. */ static struct evl_thread *pick_next_thread (struct evl_rq *rq) { -struct evl_thread *next =__pick_next_thread (rq); +struct evl_thread *next, *prev = rq->curr; +struct evl_sched_class *prev_class = prev->sched_class; +if (prev_class->sched_out) + prev_class->sched_out (prev); + + next =__pick_next_thread (rq); +trace_evl_pick_thread (next);set_next_running (rq, next);return next;@@ -972,21 +979,20 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */return ; } + prev = curr; next =pick_next_thread (this_rq); -trace_evl_pick_thread (next); -if (next == curr) { -if (unlikely (next->state &EVL_T_ROOT )) { +if (next == prev) { +if (unlikely (prev->state &EVL_T_ROOT )) {if (this_rq->local_flags &RQ_TPROXY )evl_notify_proxy_tick (this_rq);if (this_rq->local_flags &RQ_TDEFER )evl_program_local_tick (&evl_mono_clock); }raw_spin_unlock (&this_rq->lock); -raw_spin_unlock_irqrestore (&curr->lock, flags); +raw_spin_unlock_irqrestore (&prev->lock, flags);return ; } - prev = curr; this_rq->curr = next; leaving_inband = false;kernel/evl/sched/quota.c +57
-63@@ -12,45 +12,35 @@#include <uapi/evl/sched-abi.h> /* -* With this policy, each per-CPU runqueue maintains a list of active -* thread groups for the sched_fifo class. -* -* Each time a thread is picked from the runqueue, we check whether we -* still have budget for running it, looking at the group it belongs -* to. If so, a timer is armed to elapse when that group has no more -* budget, would the incoming thread run unpreempted until then -* (i.e. evl_quota->limit_timer). +* Each time a thread is picked from the ->runnable queue, we check +* whether the group it belongs to still has runtime budget. If so, a +* timer is armed to fire when that group has no more budget, would +* the incoming thread run unpreempted until then +* (i.e. quota->limit_timer). * * Otherwise, if no budget remains in the group for running the * candidate thread, we move the latter to a local expiry queue * maintained by the group. This process is done on the fly as we pull -* from the runqueue. +* from the ->runnable queue. * -* Updating the remaining budget is done each time the EVL core asks -* for replacing the current thread with the next runnable one, -* i.e. evl_quota_pick(). There we charge the elapsed run time of the -* outgoing thread to the relevant group, and conversely, we check -* whether the incoming thread has budget. +* Updating the remaining budget is done each time the EVL core +* schedules out a thread undergoing the quota scheduling policy, * -* Finally, a per-CPU timer (evl_quota->refill_timer) periodically -* ticks in the background, in accordance to the defined quota -* interval. Thread group budgets get replenished by its handler in -* accordance to their respective share, pushing all expired threads -* back to the run queue in the same move. +* Finally, a per-CPU timer (quota->refill_timer) periodically ticks +* in the background, in accordance to the defined quota interval, +* replenishing per-group budgets, pushing all expired threads back to +* the quota ->runqueue too. * -* NOTE: since the core logic enforcing the budget entirely happens in -* evl_quota_pick(), applying a budget change can be done as simply as -* forcing the rescheduling procedure to be invoked asap. As a result -* of this, the EVL core will ask for the next thread to run, which -* means calling evl_quota_pick() eventually. +* NOTE: forcing a call to the rescheduling procedure is enoiugh to +* apply a budget change. * -* CAUTION: evl_quota_group->nr_active does count both the threads -* from that group linked to the sched_fifo runqueue, _and_ the -* threads moved to the local expiry queue. As a matter of fact, the -* expired threads - those for which we consumed all the per-group -* budget - are still seen as runnable (i.e. not blocked/suspended) by -* the EVL core. This only means that the SCHED_QUOTA policy won't -* pick them until the corresponding budget is replenished. +* CAUTION: quota_group->nr_active does count both the threads from +* that group linked to the runnable queue, _and_ the threads moved to +* the local expiry queue. As a matter of fact, the expired threads - +* those for which we consumed all the per-group budget - are still +* seen as runnable (i.e. not blocked/suspended) by the EVL core. This +* only means that the SCHED_QUOTA policy won't pick them until the +* corresponding budget is replenished. */ #define MAX_QUOTA_GROUPS 1024@@ -61,15 +51,19 @@ static DECLARE_BITMAP(group_map, MAX_QUOTA_GROUPS);static LIST_HEAD (group_list); -static inline bool thread_on_quota (struct evl_thread *thread, -struct evl_quota_group *tg) +static inline bool current_on_quota (struct evl_quota_group *tg) { -/* -* Check whether @thread is running on some CPU, and belongs -* to quota group @tg. -*/ -return thread->quota == tg && - !(thread->state & (EVL_T_READY |EVL_THREAD_BLOCK_MASK )); +struct evl_rq *rq = tg->rq; +struct evl_thread *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; + +if (curr->quota != tg) +return false; + +if (curr->state & (EVL_T_READY |EVL_T_KICKED |EVL_THREAD_BLOCK_MASK )) +return false; + +return evl_timer_is_running (&qs->limit_timer); }static inline bool group_is_active (struct evl_quota_group *tg)@@ -82,7 +76,7 @@ static inline bool group_is_active(struct evl_quota_group *tg)* runqueue, in which case tg->nr_active already accounted for * it. */ -return thread_on_quota (tg->rq->curr, tg); +return current_on_quota (tg); } static inline void replenish_budget(struct evl_sched_quota *qs,@@ -134,10 +128,10 @@ static inline void replenish_budget(struct evl_sched_quota *qs, }else if (tg->run_credit) { credit =ktime_sub (tg->quota_peak, budget);/* Consume the accumulated credit. */ -if (tg->run_credit >= credit) +if (tg->run_credit >= credit) { tg->run_credit =ktime_sub (tg->run_credit, credit); -else { + }else { credit = tg->run_credit; tg->run_credit =0 ; }@@ -150,8 +144,8 @@ static inline void replenish_budget(struct evl_sched_quota *qs,static void quota_refill_handler (struct evl_timer *timer)/* oob stage stalled */ { -struct evl_quota_group *tg;struct evl_thread *thread, *tmp; +struct evl_quota_group *tg;struct evl_sched_quota *qs;struct evl_rq *rq;@@ -167,7 +161,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *if (tg->run_budget ==0 ||list_empty (&tg->expired))continue ;/* -* For each group living on this CPU, move all expired +* For each group pinned on this CPU, move all expired * threads back to the runqueue. Since those threads * were moved out of the runqueue as we were * considering them for execution, we push them back @@ -178,7 +172,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }@@ -195,7 +189,7 @@ static void quota_limit_handler(struct evl_timer *timer) /* oob stage stalled *//* * Force a rescheduling on the return path of the current * interrupt, so that the budget is re-evaluated for the -* current group in evl_quota_pick(). +* current group in quota_pick(). */ raw_spin_lock (&rq->lock);evl_set_self_resched (rq);@@ -221,6 +215,7 @@ static void quota_init(struct evl_rq *rq) {struct evl_sched_quota *qs = &rq->quota; +evl_init_schedq (&qs->runnable); qs->period = quota_period;INIT_LIST_HEAD (&qs->groups);@@ -337,8 +332,8 @@ static void quota_forget(struct evl_thread *thread)static void quota_kick (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;/* * Allow a kicked thread to be elected for running until it @@ -347,7 +342,7 @@ static void quota_kick(struct evl_thread *thread)*/ if (tg->run_budget ==0 && !list_empty (&thread->quota_expired)) {list_del_init (&thread->quota_expired); -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); } }@@ -358,79 +353,82 @@ static inline int thread_is_runnable(struct evl_thread *thread)static void quota_enqueue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add_tail (&thread->quota_expired, &tg->expired);else -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); tg->nr_active++; }static void quota_dequeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!list_empty (&thread->quota_expired))list_del_init (&thread->quota_expired);else -evl_del_schedq (&rq->fifo.runnable, thread); +evl_del_schedq (&qs->runnable, thread); tg->nr_active--; }static void quota_requeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add (&thread->quota_expired, &tg->expired);else -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); tg->nr_active++; } -static struct evl_thread *quota_pick (struct evl_rq *rq) +static void quota_out (struct evl_thread *thread) { -struct evl_thread *next, *curr = rq->curr; -struct evl_sched_quota *qs = &rq->quota; -struct evl_quota_group *otg, *tg; -ktime_t now, elapsed; +struct evl_sched_quota *qs = &thread->rq->quota; +struct evl_quota_group *tg = thread->quota; +ktime_t now, consumed; + +/* Timer off means that we are not tracking quota. */ +if (!evl_timer_is_running (&qs->limit_timer)) +return ; - now =evl_ktime_monotonic (); - otg = curr->quota; -if (otg ==NULL ) - goto pick;kernel/evl/sched/quota.c static inline int thread_is_runnable(struct evl_thread *thread) · line 407
Quoted 10 lines
kernel/evl/sched/quota.c +1
-5/* * Charge the time consumed by the outgoing thread to the * group it belongs to. */ - elapsed =ktime_sub (now, otg->run_start); -if (elapsed < otg->run_budget) - otg->run_budget =ktime_sub (otg->run_budget, elapsed); -else - otg->run_budget =0 ; + now =evl_ktime_monotonic ();Thing brins some, though minor, glitch when doing the
run_startupdate for the next group already in pick. That's why I'm trying to reuse that stamp when available.Quoted 137 lines
kernel/evl/sched/quota.c +40
-36+ consumed =ktime_sub (now, tg->run_start); +if (consumed < tg->run_budget) { + tg->run_budget =ktime_sub (tg->run_budget, consumed); + }else { + tg->run_budget =0 ; +evl_stop_timer (&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick (struct evl_rq *rq) +{ +struct evl_thread *next, *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; +struct evl_quota_group *tg; + pick: - next =evl_get_schedq (&rq->fifo.runnable); + next =evl_get_schedq (&qs->runnable);if (next ==NULL ) {evl_stop_timer (&qs->limit_timer);return NULL ; } -/* -* As we basically piggyback on the SCHED_FIFO runqueue, make -* sure to detect non-quota threads. -*/ tg = next->quota; -if (tg ==NULL ) -return next; - - tg->run_start = now; + tg->nr_active--;/* * Don't consider budget if kicked, we have to allow this @@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)*/ if (next->info &EVL_T_KICKED ) {evl_stop_timer (&qs->limit_timer); - goto out; +return next; } +/* +* __pick_next_thread() might have requeued the current +* thread which is still leading the pack. +*/ +if (curr == next) +return next; +if (ktime_to_ns (tg->run_budget) ==0 ) { -/* Flush expired group members as we go. */ +/* Park expired group members as we go. */ list_add_tail (&next->quota_expired, &tg->expired); goto pick; } -if (otg == tg &&evl_timer_is_running (&qs->limit_timer)) -/* Same group, leave the running timer untouched. */ - goto out; - -/* Arm limit timer for the new running group. */ -evl_start_timer (&qs->limit_timer, -ktime_add (now, tg->run_budget), -EVL_INFINITE ); -out: - tg->nr_active--; +/* Arm new limit timer on change of running group. */ +if (curr->quota != tg || !evl_timer_is_running (&qs->limit_timer)) { + tg->run_start =evl_ktime_monotonic (); +evl_start_timer (&qs->limit_timer, +ktime_add (tg->run_start, tg->run_budget), +EVL_INFINITE ); + }return next; }@@ -542,7 +544,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* Unregister the group before we drop rq->lock. As a result, * it won't accept threads anymore while we are busy moving * the current members to the fifo class, and concurrent -* evl_quota_remove requests would receive -EINVAL. +* quota_remove requests would receive -EINVAL. */ __clear_bit (tg->tgid, group_map);list_del (&tg->next);@@ -556,7 +558,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* hold rq->lock on entry, we do a trylock dance to prevent an * ABBA issue. No livelock is possible since we unregistered * that group already, so &tg->members can only be depleted -* (by this loop specifically). +* (by this loop exclusively). */ while (!list_empty (&tg->members)) {@@ -583,10 +585,10 @@ static void quota_set_limit(struct evl_quota_group *tg, int *quota_sum_r) {struct evl_rq *rq = tg->rq; -struct evl_thread *thread, *tmp, *curr = rq->curr; +struct evl_thread *thread, *tmp;struct evl_sched_quota *qs = &rq->quota; -ktime_t now, elapsed, consumed;ktime_t old_quota = tg->quota; +ktime_t consumed;u64 n;assert_hard_lock (&rq->lock);@@ -615,16 +617,8 @@ static void quota_set_limit(struct evl_quota_group *tg, tg->quota_percent = quota_percent; tg->quota_peak_percent = quota_peak_percent; -if (thread_on_quota (curr, tg)) { - now =evl_ktime_monotonic (); - - elapsed = now - tg->run_start; -if (elapsed < tg->run_budget) - tg->run_budget -= elapsed; -else - tg->run_budget =0 ; - - tg->run_start = now; +if (current_on_quota (tg)) { +quota_out (rq->curr);evl_stop_timer (&qs->limit_timer); }@@ -646,7 +640,7 @@ static void quota_set_limit(struct evl_quota_group *tg,list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }Quoted 13 lines
kernel/evl/sched/quota.c struct evl_sched_class evl_sched_quota = { · line 786
Quoted 9 lines
kernel/evl/sched/quota.c +1
-0@@ -786,6 +780,7 @@ struct evl_sched_class evl_sched_quota = { .sched_dequeue = quota_dequeue, .sched_requeue = quota_requeue, .sched_pick = quota_pick, + .sched_out = quota_out, .sched_migrate = quota_migrate, .sched_chkparam = quota_chkparam, .sched_setparam = quota_setparam,You also seem to miss the case I discovered only yesterday as well: When starting the current thread on a quota via setparam, we also need to set its
run_startstamp at that point.
run_startshould be meaningful only when a limit timer runs.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Right, but are you sure it will be started in the case I described...?
To be started, it must be picked, so yes.
But prev == next then - nothing new to pick, no?
Yes, with the current group properly being charged for the consumed time since sched_out() is called first and unconditionally. Whether next == curr can only be determined by sched_pick() anyway.
Signature · 1 line
Philippe.
Quoted 589 lines · Jan Kiszka
Quoted 58 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
Quoted 4 lines
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).Please avoid that for the final patch. I already had troubles telling substantial from cosmetical changes apart in your lazy-blocking commit (>50% unrelated changes in there).
Quoted 12 lines
commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.Quoted 5 lines
To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO....and why we need a separate queue now? Please provide reasoning for changes.
Quoted 334 lines
Signed-off-by Philippe Gerum <[email protected]>include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);include/evl/sched/quota.h +1
-0@@ -40,6 +40,7 @@ struct evl_quota_group {struct evl_sched_quota {ktime_t period; +struct evl_sched_queue runnable;struct evl_timer refill_timer;struct evl_timer limit_timer;struct list_head groups;kernel/evl/sched/core.c +13
-7@@ -773,7 +773,8 @@ static inline void set_next_running(struct evl_rq *rq,evl_stop_timer (&rq->rrbtimer); } -static struct evl_thread *__pick_next_thread (struct evl_rq *rq) +static __always_inline struct evl_thread * +__pick_next_thread (struct evl_rq *rq) {struct evl_sched_class *sched_class;struct evl_thread *curr = rq->curr;@@ -821,8 +822,14 @@ static struct evl_thread *__pick_next_thread(struct evl_rq *rq)/* rq->curr->lock + rq->lock held, hard irqs off. */ static struct evl_thread *pick_next_thread (struct evl_rq *rq) { -struct evl_thread *next =__pick_next_thread (rq); +struct evl_thread *next, *prev = rq->curr; +struct evl_sched_class *prev_class = prev->sched_class; +if (prev_class->sched_out) + prev_class->sched_out (prev); + + next =__pick_next_thread (rq); +trace_evl_pick_thread (next);set_next_running (rq, next);return next;@@ -972,21 +979,20 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */return ; } + prev = curr; next =pick_next_thread (this_rq); -trace_evl_pick_thread (next); -if (next == curr) { -if (unlikely (next->state &EVL_T_ROOT )) { +if (next == prev) { +if (unlikely (prev->state &EVL_T_ROOT )) {if (this_rq->local_flags &RQ_TPROXY )evl_notify_proxy_tick (this_rq);if (this_rq->local_flags &RQ_TDEFER )evl_program_local_tick (&evl_mono_clock); }raw_spin_unlock (&this_rq->lock); -raw_spin_unlock_irqrestore (&curr->lock, flags); +raw_spin_unlock_irqrestore (&prev->lock, flags);return ; } - prev = curr; this_rq->curr = next; leaving_inband = false;kernel/evl/sched/quota.c +57
-63@@ -12,45 +12,35 @@#include <uapi/evl/sched-abi.h> /* -* With this policy, each per-CPU runqueue maintains a list of active -* thread groups for the sched_fifo class. -* -* Each time a thread is picked from the runqueue, we check whether we -* still have budget for running it, looking at the group it belongs -* to. If so, a timer is armed to elapse when that group has no more -* budget, would the incoming thread run unpreempted until then -* (i.e. evl_quota->limit_timer). +* Each time a thread is picked from the ->runnable queue, we check +* whether the group it belongs to still has runtime budget. If so, a +* timer is armed to fire when that group has no more budget, would +* the incoming thread run unpreempted until then +* (i.e. quota->limit_timer). * * Otherwise, if no budget remains in the group for running the * candidate thread, we move the latter to a local expiry queue * maintained by the group. This process is done on the fly as we pull -* from the runqueue. +* from the ->runnable queue. * -* Updating the remaining budget is done each time the EVL core asks -* for replacing the current thread with the next runnable one, -* i.e. evl_quota_pick(). There we charge the elapsed run time of the -* outgoing thread to the relevant group, and conversely, we check -* whether the incoming thread has budget. +* Updating the remaining budget is done each time the EVL core +* schedules out a thread undergoing the quota scheduling policy, * -* Finally, a per-CPU timer (evl_quota->refill_timer) periodically -* ticks in the background, in accordance to the defined quota -* interval. Thread group budgets get replenished by its handler in -* accordance to their respective share, pushing all expired threads -* back to the run queue in the same move. +* Finally, a per-CPU timer (quota->refill_timer) periodically ticks +* in the background, in accordance to the defined quota interval, +* replenishing per-group budgets, pushing all expired threads back to +* the quota ->runqueue too. * -* NOTE: since the core logic enforcing the budget entirely happens in -* evl_quota_pick(), applying a budget change can be done as simply as -* forcing the rescheduling procedure to be invoked asap. As a result -* of this, the EVL core will ask for the next thread to run, which -* means calling evl_quota_pick() eventually. +* NOTE: forcing a call to the rescheduling procedure is enoiugh to +* apply a budget change. * -* CAUTION: evl_quota_group->nr_active does count both the threads -* from that group linked to the sched_fifo runqueue, _and_ the -* threads moved to the local expiry queue. As a matter of fact, the -* expired threads - those for which we consumed all the per-group -* budget - are still seen as runnable (i.e. not blocked/suspended) by -* the EVL core. This only means that the SCHED_QUOTA policy won't -* pick them until the corresponding budget is replenished. +* CAUTION: quota_group->nr_active does count both the threads from +* that group linked to the runnable queue, _and_ the threads moved to +* the local expiry queue. As a matter of fact, the expired threads - +* those for which we consumed all the per-group budget - are still +* seen as runnable (i.e. not blocked/suspended) by the EVL core. This +* only means that the SCHED_QUOTA policy won't pick them until the +* corresponding budget is replenished. */ #define MAX_QUOTA_GROUPS 1024@@ -61,15 +51,19 @@ static DECLARE_BITMAP(group_map, MAX_QUOTA_GROUPS);static LIST_HEAD (group_list); -static inline bool thread_on_quota (struct evl_thread *thread, -struct evl_quota_group *tg) +static inline bool current_on_quota (struct evl_quota_group *tg) { -/* -* Check whether @thread is running on some CPU, and belongs -* to quota group @tg. -*/ -return thread->quota == tg && - !(thread->state & (EVL_T_READY |EVL_THREAD_BLOCK_MASK )); +struct evl_rq *rq = tg->rq; +struct evl_thread *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; + +if (curr->quota != tg) +return false; + +if (curr->state & (EVL_T_READY |EVL_T_KICKED |EVL_THREAD_BLOCK_MASK )) +return false; + +return evl_timer_is_running (&qs->limit_timer); }static inline bool group_is_active (struct evl_quota_group *tg)@@ -82,7 +76,7 @@ static inline bool group_is_active(struct evl_quota_group *tg)* runqueue, in which case tg->nr_active already accounted for * it. */ -return thread_on_quota (tg->rq->curr, tg); +return current_on_quota (tg); } static inline void replenish_budget(struct evl_sched_quota *qs,@@ -134,10 +128,10 @@ static inline void replenish_budget(struct evl_sched_quota *qs, }else if (tg->run_credit) { credit =ktime_sub (tg->quota_peak, budget);/* Consume the accumulated credit. */ -if (tg->run_credit >= credit) +if (tg->run_credit >= credit) { tg->run_credit =ktime_sub (tg->run_credit, credit); -else { + }else { credit = tg->run_credit; tg->run_credit =0 ; }@@ -150,8 +144,8 @@ static inline void replenish_budget(struct evl_sched_quota *qs,static void quota_refill_handler (struct evl_timer *timer)/* oob stage stalled */ { -struct evl_quota_group *tg;struct evl_thread *thread, *tmp; +struct evl_quota_group *tg;struct evl_sched_quota *qs;struct evl_rq *rq;@@ -167,7 +161,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *if (tg->run_budget ==0 ||list_empty (&tg->expired))continue ;/* -* For each group living on this CPU, move all expired +* For each group pinned on this CPU, move all expired * threads back to the runqueue. Since those threads * were moved out of the runqueue as we were * considering them for execution, we push them back @@ -178,7 +172,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }@@ -195,7 +189,7 @@ static void quota_limit_handler(struct evl_timer *timer) /* oob stage stalled *//* * Force a rescheduling on the return path of the current * interrupt, so that the budget is re-evaluated for the -* current group in evl_quota_pick(). +* current group in quota_pick(). */ raw_spin_lock (&rq->lock);evl_set_self_resched (rq);@@ -221,6 +215,7 @@ static void quota_init(struct evl_rq *rq) {struct evl_sched_quota *qs = &rq->quota; +evl_init_schedq (&qs->runnable); qs->period = quota_period;INIT_LIST_HEAD (&qs->groups);@@ -337,8 +332,8 @@ static void quota_forget(struct evl_thread *thread)static void quota_kick (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;/* * Allow a kicked thread to be elected for running until it @@ -347,7 +342,7 @@ static void quota_kick(struct evl_thread *thread)*/ if (tg->run_budget ==0 && !list_empty (&thread->quota_expired)) {list_del_init (&thread->quota_expired); -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); } }@@ -358,79 +353,82 @@ static inline int thread_is_runnable(struct evl_thread *thread)static void quota_enqueue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add_tail (&thread->quota_expired, &tg->expired);else -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); tg->nr_active++; }static void quota_dequeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!list_empty (&thread->quota_expired))list_del_init (&thread->quota_expired);else -evl_del_schedq (&rq->fifo.runnable, thread); +evl_del_schedq (&qs->runnable, thread); tg->nr_active--; }static void quota_requeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add (&thread->quota_expired, &tg->expired);else -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); tg->nr_active++; } -static struct evl_thread *quota_pick (struct evl_rq *rq) +static void quota_out (struct evl_thread *thread) { -struct evl_thread *next, *curr = rq->curr; -struct evl_sched_quota *qs = &rq->quota; -struct evl_quota_group *otg, *tg; -ktime_t now, elapsed; +struct evl_sched_quota *qs = &thread->rq->quota; +struct evl_quota_group *tg = thread->quota; +ktime_t now, consumed; + +/* Timer off means that we are not tracking quota. */ +if (!evl_timer_is_running (&qs->limit_timer)) +return ; - now =evl_ktime_monotonic (); - otg = curr->quota; -if (otg ==NULL ) - goto pick;kernel/evl/sched/quota.c static inline int thread_is_runnable(struct evl_thread *thread) · line 407
Quoted 10 lines
kernel/evl/sched/quota.c +1
-5/* * Charge the time consumed by the outgoing thread to the * group it belongs to. */ - elapsed =ktime_sub (now, otg->run_start); -if (elapsed < otg->run_budget) - otg->run_budget =ktime_sub (otg->run_budget, elapsed); -else - otg->run_budget =0 ; + now =evl_ktime_monotonic ();Thing brins some, though minor, glitch when doing the
run_startupdate for the next group already in pick. That's why I'm trying to reuse that stamp when available.Quoted 139 lines
kernel/evl/sched/quota.c +40
-36+ consumed =ktime_sub (now, tg->run_start); +if (consumed < tg->run_budget) { + tg->run_budget =ktime_sub (tg->run_budget, consumed); + }else { + tg->run_budget =0 ; +evl_stop_timer (&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick (struct evl_rq *rq) +{ +struct evl_thread *next, *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; +struct evl_quota_group *tg; + pick: - next =evl_get_schedq (&rq->fifo.runnable); + next =evl_get_schedq (&qs->runnable);if (next ==NULL ) {evl_stop_timer (&qs->limit_timer);return NULL ; } -/* -* As we basically piggyback on the SCHED_FIFO runqueue, make -* sure to detect non-quota threads. -*/ tg = next->quota; -if (tg ==NULL ) -return next; - - tg->run_start = now; + tg->nr_active--;/* * Don't consider budget if kicked, we have to allow this @@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)*/ if (next->info &EVL_T_KICKED ) {evl_stop_timer (&qs->limit_timer); - goto out; +return next; } +/* +* __pick_next_thread() might have requeued the current +* thread which is still leading the pack. +*/ +if (curr == next) +return next; +if (ktime_to_ns (tg->run_budget) ==0 ) { -/* Flush expired group members as we go. */ +/* Park expired group members as we go. */ list_add_tail (&next->quota_expired, &tg->expired); goto pick; } -if (otg == tg &&evl_timer_is_running (&qs->limit_timer)) -/* Same group, leave the running timer untouched. */ - goto out; - -/* Arm limit timer for the new running group. */ -evl_start_timer (&qs->limit_timer, -ktime_add (now, tg->run_budget), -EVL_INFINITE ); -out: - tg->nr_active--; +/* Arm new limit timer on change of running group. */ +if (curr->quota != tg || !evl_timer_is_running (&qs->limit_timer)) { + tg->run_start =evl_ktime_monotonic (); +evl_start_timer (&qs->limit_timer, +ktime_add (tg->run_start, tg->run_budget), +EVL_INFINITE ); + }return next; }@@ -542,7 +544,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* Unregister the group before we drop rq->lock. As a result, * it won't accept threads anymore while we are busy moving * the current members to the fifo class, and concurrent -* evl_quota_remove requests would receive -EINVAL. +* quota_remove requests would receive -EINVAL. */ __clear_bit (tg->tgid, group_map);list_del (&tg->next);@@ -556,7 +558,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* hold rq->lock on entry, we do a trylock dance to prevent an * ABBA issue. No livelock is possible since we unregistered * that group already, so &tg->members can only be depleted -* (by this loop specifically). +* (by this loop exclusively). */ while (!list_empty (&tg->members)) {@@ -583,10 +585,10 @@ static void quota_set_limit(struct evl_quota_group *tg, int *quota_sum_r) {struct evl_rq *rq = tg->rq; -struct evl_thread *thread, *tmp, *curr = rq->curr; +struct evl_thread *thread, *tmp;struct evl_sched_quota *qs = &rq->quota; -ktime_t now, elapsed, consumed;ktime_t old_quota = tg->quota; +ktime_t consumed;u64 n;assert_hard_lock (&rq->lock);@@ -615,16 +617,8 @@ static void quota_set_limit(struct evl_quota_group *tg, tg->quota_percent = quota_percent; tg->quota_peak_percent = quota_peak_percent; -if (thread_on_quota (curr, tg)) { - now =evl_ktime_monotonic (); - - elapsed = now - tg->run_start; -if (elapsed < tg->run_budget) - tg->run_budget -= elapsed; -else - tg->run_budget =0 ; - - tg->run_start = now; +if (current_on_quota (tg)) { +quota_out (rq->curr);evl_stop_timer (&qs->limit_timer); }@@ -646,7 +640,7 @@ static void quota_set_limit(struct evl_quota_group *tg,list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }kernel/evl/sched/quota.c struct evl_sched_class evl_sched_quota = { · line 786
Quoted 7 lines
kernel/evl/sched/quota.c +1
-0@@ -786,6 +780,7 @@ struct evl_sched_class evl_sched_quota = { .sched_dequeue = quota_dequeue, .sched_requeue = quota_requeue, .sched_pick = quota_pick, + .sched_out = quota_out, .sched_migrate = quota_migrate, .sched_chkparam = quota_chkparam, .sched_setparam = quota_setparam,You also seem to miss the case I discovered only yesterday as well: When starting the current thread on a quota via setparam, we also need to set its
run_startstamp at that point.
Quoted 4 lines
Please have a closer look at both my test case (libevl) as well as my patch 2. Currently, I don't see remaining issues with both, but I'm all ears to learn what I missed.
AFAIU, the logic of your changes is as follows:
1. provide a sched_out() handler which charges the previous/current group with the budget consumed by the outgoing thread, dropping such accounting from sched_pick() in the same move. As a consequence, charge the consumed budget in quota_limit_handler() too since you can't expect sched_pick() to do so (i.e. turning on the resched bit on curr->rq in quota_limit_handler() would not help in this case).
2. invoke sched_out() after sched_pick(), unless prev == next.
Since sched_pick() checks next->quota->run_budget to make sure that next may run, what if curr and next belong to the same quota group given that curr->quota->run_budget won't be updated by sched_out() until sched_pick() has returned?
Signature · 1 line
Philippe.
Quoted 596 lines · Philippe Gerum
Quoted 589 lines · Jan Kiszka
Quoted 58 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
Quoted 4 lines
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).Please avoid that for the final patch. I already had troubles telling substantial from cosmetical changes apart in your lazy-blocking commit (>50% unrelated changes in there).
Quoted 12 lines
commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.Quoted 5 lines
To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO....and why we need a separate queue now? Please provide reasoning for changes.
Quoted 334 lines
Signed-off-by Philippe Gerum <[email protected]>include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);include/evl/sched/quota.h +1
-0@@ -40,6 +40,7 @@ struct evl_quota_group {struct evl_sched_quota {ktime_t period; +struct evl_sched_queue runnable;struct evl_timer refill_timer;struct evl_timer limit_timer;struct list_head groups;kernel/evl/sched/core.c +13
-7@@ -773,7 +773,8 @@ static inline void set_next_running(struct evl_rq *rq,evl_stop_timer (&rq->rrbtimer); } -static struct evl_thread *__pick_next_thread (struct evl_rq *rq) +static __always_inline struct evl_thread * +__pick_next_thread (struct evl_rq *rq) {struct evl_sched_class *sched_class;struct evl_thread *curr = rq->curr;@@ -821,8 +822,14 @@ static struct evl_thread *__pick_next_thread(struct evl_rq *rq)/* rq->curr->lock + rq->lock held, hard irqs off. */ static struct evl_thread *pick_next_thread (struct evl_rq *rq) { -struct evl_thread *next =__pick_next_thread (rq); +struct evl_thread *next, *prev = rq->curr; +struct evl_sched_class *prev_class = prev->sched_class; +if (prev_class->sched_out) + prev_class->sched_out (prev); + + next =__pick_next_thread (rq); +trace_evl_pick_thread (next);set_next_running (rq, next);return next;@@ -972,21 +979,20 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */return ; } + prev = curr; next =pick_next_thread (this_rq); -trace_evl_pick_thread (next); -if (next == curr) { -if (unlikely (next->state &EVL_T_ROOT )) { +if (next == prev) { +if (unlikely (prev->state &EVL_T_ROOT )) {if (this_rq->local_flags &RQ_TPROXY )evl_notify_proxy_tick (this_rq);if (this_rq->local_flags &RQ_TDEFER )evl_program_local_tick (&evl_mono_clock); }raw_spin_unlock (&this_rq->lock); -raw_spin_unlock_irqrestore (&curr->lock, flags); +raw_spin_unlock_irqrestore (&prev->lock, flags);return ; } - prev = curr; this_rq->curr = next; leaving_inband = false;kernel/evl/sched/quota.c +57
-63@@ -12,45 +12,35 @@#include <uapi/evl/sched-abi.h> /* -* With this policy, each per-CPU runqueue maintains a list of active -* thread groups for the sched_fifo class. -* -* Each time a thread is picked from the runqueue, we check whether we -* still have budget for running it, looking at the group it belongs -* to. If so, a timer is armed to elapse when that group has no more -* budget, would the incoming thread run unpreempted until then -* (i.e. evl_quota->limit_timer). +* Each time a thread is picked from the ->runnable queue, we check +* whether the group it belongs to still has runtime budget. If so, a +* timer is armed to fire when that group has no more budget, would +* the incoming thread run unpreempted until then +* (i.e. quota->limit_timer). * * Otherwise, if no budget remains in the group for running the * candidate thread, we move the latter to a local expiry queue * maintained by the group. This process is done on the fly as we pull -* from the runqueue. +* from the ->runnable queue. * -* Updating the remaining budget is done each time the EVL core asks -* for replacing the current thread with the next runnable one, -* i.e. evl_quota_pick(). There we charge the elapsed run time of the -* outgoing thread to the relevant group, and conversely, we check -* whether the incoming thread has budget. +* Updating the remaining budget is done each time the EVL core +* schedules out a thread undergoing the quota scheduling policy, * -* Finally, a per-CPU timer (evl_quota->refill_timer) periodically -* ticks in the background, in accordance to the defined quota -* interval. Thread group budgets get replenished by its handler in -* accordance to their respective share, pushing all expired threads -* back to the run queue in the same move. +* Finally, a per-CPU timer (quota->refill_timer) periodically ticks +* in the background, in accordance to the defined quota interval, +* replenishing per-group budgets, pushing all expired threads back to +* the quota ->runqueue too. * -* NOTE: since the core logic enforcing the budget entirely happens in -* evl_quota_pick(), applying a budget change can be done as simply as -* forcing the rescheduling procedure to be invoked asap. As a result -* of this, the EVL core will ask for the next thread to run, which -* means calling evl_quota_pick() eventually. +* NOTE: forcing a call to the rescheduling procedure is enoiugh to +* apply a budget change. * -* CAUTION: evl_quota_group->nr_active does count both the threads -* from that group linked to the sched_fifo runqueue, _and_ the -* threads moved to the local expiry queue. As a matter of fact, the -* expired threads - those for which we consumed all the per-group -* budget - are still seen as runnable (i.e. not blocked/suspended) by -* the EVL core. This only means that the SCHED_QUOTA policy won't -* pick them until the corresponding budget is replenished. +* CAUTION: quota_group->nr_active does count both the threads from +* that group linked to the runnable queue, _and_ the threads moved to +* the local expiry queue. As a matter of fact, the expired threads - +* those for which we consumed all the per-group budget - are still +* seen as runnable (i.e. not blocked/suspended) by the EVL core. This +* only means that the SCHED_QUOTA policy won't pick them until the +* corresponding budget is replenished. */ #define MAX_QUOTA_GROUPS 1024@@ -61,15 +51,19 @@ static DECLARE_BITMAP(group_map, MAX_QUOTA_GROUPS);static LIST_HEAD (group_list); -static inline bool thread_on_quota (struct evl_thread *thread, -struct evl_quota_group *tg) +static inline bool current_on_quota (struct evl_quota_group *tg) { -/* -* Check whether @thread is running on some CPU, and belongs -* to quota group @tg. -*/ -return thread->quota == tg && - !(thread->state & (EVL_T_READY |EVL_THREAD_BLOCK_MASK )); +struct evl_rq *rq = tg->rq; +struct evl_thread *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; + +if (curr->quota != tg) +return false; + +if (curr->state & (EVL_T_READY |EVL_T_KICKED |EVL_THREAD_BLOCK_MASK )) +return false; + +return evl_timer_is_running (&qs->limit_timer); }static inline bool group_is_active (struct evl_quota_group *tg)@@ -82,7 +76,7 @@ static inline bool group_is_active(struct evl_quota_group *tg)* runqueue, in which case tg->nr_active already accounted for * it. */ -return thread_on_quota (tg->rq->curr, tg); +return current_on_quota (tg); } static inline void replenish_budget(struct evl_sched_quota *qs,@@ -134,10 +128,10 @@ static inline void replenish_budget(struct evl_sched_quota *qs, }else if (tg->run_credit) { credit =ktime_sub (tg->quota_peak, budget);/* Consume the accumulated credit. */ -if (tg->run_credit >= credit) +if (tg->run_credit >= credit) { tg->run_credit =ktime_sub (tg->run_credit, credit); -else { + }else { credit = tg->run_credit; tg->run_credit =0 ; }@@ -150,8 +144,8 @@ static inline void replenish_budget(struct evl_sched_quota *qs,static void quota_refill_handler (struct evl_timer *timer)/* oob stage stalled */ { -struct evl_quota_group *tg;struct evl_thread *thread, *tmp; +struct evl_quota_group *tg;struct evl_sched_quota *qs;struct evl_rq *rq;@@ -167,7 +161,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *if (tg->run_budget ==0 ||list_empty (&tg->expired))continue ;/* -* For each group living on this CPU, move all expired +* For each group pinned on this CPU, move all expired * threads back to the runqueue. Since those threads * were moved out of the runqueue as we were * considering them for execution, we push them back @@ -178,7 +172,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }@@ -195,7 +189,7 @@ static void quota_limit_handler(struct evl_timer *timer) /* oob stage stalled *//* * Force a rescheduling on the return path of the current * interrupt, so that the budget is re-evaluated for the -* current group in evl_quota_pick(). +* current group in quota_pick(). */ raw_spin_lock (&rq->lock);evl_set_self_resched (rq);@@ -221,6 +215,7 @@ static void quota_init(struct evl_rq *rq) {struct evl_sched_quota *qs = &rq->quota; +evl_init_schedq (&qs->runnable); qs->period = quota_period;INIT_LIST_HEAD (&qs->groups);@@ -337,8 +332,8 @@ static void quota_forget(struct evl_thread *thread)static void quota_kick (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;/* * Allow a kicked thread to be elected for running until it @@ -347,7 +342,7 @@ static void quota_kick(struct evl_thread *thread)*/ if (tg->run_budget ==0 && !list_empty (&thread->quota_expired)) {list_del_init (&thread->quota_expired); -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); } }@@ -358,79 +353,82 @@ static inline int thread_is_runnable(struct evl_thread *thread)static void quota_enqueue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add_tail (&thread->quota_expired, &tg->expired);else -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); tg->nr_active++; }static void quota_dequeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!list_empty (&thread->quota_expired))list_del_init (&thread->quota_expired);else -evl_del_schedq (&rq->fifo.runnable, thread); +evl_del_schedq (&qs->runnable, thread); tg->nr_active--; }static void quota_requeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add (&thread->quota_expired, &tg->expired);else -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); tg->nr_active++; } -static struct evl_thread *quota_pick (struct evl_rq *rq) +static void quota_out (struct evl_thread *thread) { -struct evl_thread *next, *curr = rq->curr; -struct evl_sched_quota *qs = &rq->quota; -struct evl_quota_group *otg, *tg; -ktime_t now, elapsed; +struct evl_sched_quota *qs = &thread->rq->quota; +struct evl_quota_group *tg = thread->quota; +ktime_t now, consumed; + +/* Timer off means that we are not tracking quota. */ +if (!evl_timer_is_running (&qs->limit_timer)) +return ; - now =evl_ktime_monotonic (); - otg = curr->quota; -if (otg ==NULL ) - goto pick;kernel/evl/sched/quota.c static inline int thread_is_runnable(struct evl_thread *thread) · line 407
Quoted 10 lines
kernel/evl/sched/quota.c +1
-5/* * Charge the time consumed by the outgoing thread to the * group it belongs to. */ - elapsed =ktime_sub (now, otg->run_start); -if (elapsed < otg->run_budget) - otg->run_budget =ktime_sub (otg->run_budget, elapsed); -else - otg->run_budget =0 ; + now =evl_ktime_monotonic ();Thing brins some, though minor, glitch when doing the
run_startupdate for the next group already in pick. That's why I'm trying to reuse that stamp when available.Quoted 139 lines
kernel/evl/sched/quota.c +40
-36+ consumed =ktime_sub (now, tg->run_start); +if (consumed < tg->run_budget) { + tg->run_budget =ktime_sub (tg->run_budget, consumed); + }else { + tg->run_budget =0 ; +evl_stop_timer (&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick (struct evl_rq *rq) +{ +struct evl_thread *next, *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; +struct evl_quota_group *tg; + pick: - next =evl_get_schedq (&rq->fifo.runnable); + next =evl_get_schedq (&qs->runnable);if (next ==NULL ) {evl_stop_timer (&qs->limit_timer);return NULL ; } -/* -* As we basically piggyback on the SCHED_FIFO runqueue, make -* sure to detect non-quota threads. -*/ tg = next->quota; -if (tg ==NULL ) -return next; - - tg->run_start = now; + tg->nr_active--;/* * Don't consider budget if kicked, we have to allow this @@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)*/ if (next->info &EVL_T_KICKED ) {evl_stop_timer (&qs->limit_timer); - goto out; +return next; } +/* +* __pick_next_thread() might have requeued the current +* thread which is still leading the pack. +*/ +if (curr == next) +return next; +if (ktime_to_ns (tg->run_budget) ==0 ) { -/* Flush expired group members as we go. */ +/* Park expired group members as we go. */ list_add_tail (&next->quota_expired, &tg->expired); goto pick; } -if (otg == tg &&evl_timer_is_running (&qs->limit_timer)) -/* Same group, leave the running timer untouched. */ - goto out; - -/* Arm limit timer for the new running group. */ -evl_start_timer (&qs->limit_timer, -ktime_add (now, tg->run_budget), -EVL_INFINITE ); -out: - tg->nr_active--; +/* Arm new limit timer on change of running group. */ +if (curr->quota != tg || !evl_timer_is_running (&qs->limit_timer)) { + tg->run_start =evl_ktime_monotonic (); +evl_start_timer (&qs->limit_timer, +ktime_add (tg->run_start, tg->run_budget), +EVL_INFINITE ); + }return next; }@@ -542,7 +544,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* Unregister the group before we drop rq->lock. As a result, * it won't accept threads anymore while we are busy moving * the current members to the fifo class, and concurrent -* evl_quota_remove requests would receive -EINVAL. +* quota_remove requests would receive -EINVAL. */ __clear_bit (tg->tgid, group_map);list_del (&tg->next);@@ -556,7 +558,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* hold rq->lock on entry, we do a trylock dance to prevent an * ABBA issue. No livelock is possible since we unregistered * that group already, so &tg->members can only be depleted -* (by this loop specifically). +* (by this loop exclusively). */ while (!list_empty (&tg->members)) {@@ -583,10 +585,10 @@ static void quota_set_limit(struct evl_quota_group *tg, int *quota_sum_r) {struct evl_rq *rq = tg->rq; -struct evl_thread *thread, *tmp, *curr = rq->curr; +struct evl_thread *thread, *tmp;struct evl_sched_quota *qs = &rq->quota; -ktime_t now, elapsed, consumed;ktime_t old_quota = tg->quota; +ktime_t consumed;u64 n;assert_hard_lock (&rq->lock);@@ -615,16 +617,8 @@ static void quota_set_limit(struct evl_quota_group *tg, tg->quota_percent = quota_percent; tg->quota_peak_percent = quota_peak_percent; -if (thread_on_quota (curr, tg)) { - now =evl_ktime_monotonic (); - - elapsed = now - tg->run_start; -if (elapsed < tg->run_budget) - tg->run_budget -= elapsed; -else - tg->run_budget =0 ; - - tg->run_start = now; +if (current_on_quota (tg)) { +quota_out (rq->curr);evl_stop_timer (&qs->limit_timer); }@@ -646,7 +640,7 @@ static void quota_set_limit(struct evl_quota_group *tg,list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }kernel/evl/sched/quota.c struct evl_sched_class evl_sched_quota = { · line 786
Quoted 7 lines
kernel/evl/sched/quota.c +1
-0@@ -786,6 +780,7 @@ struct evl_sched_class evl_sched_quota = { .sched_dequeue = quota_dequeue, .sched_requeue = quota_requeue, .sched_pick = quota_pick, + .sched_out = quota_out, .sched_migrate = quota_migrate, .sched_chkparam = quota_chkparam, .sched_setparam = quota_setparam,You also seem to miss the case I discovered only yesterday as well: When starting the current thread on a quota via setparam, we also need to set its
run_startstamp at that point.Quoted 4 lines
Please have a closer look at both my test case (libevl) as well as my patch 2. Currently, I don't see remaining issues with both, but I'm all ears to learn what I missed.
Quoted 8 lines
AFAIU, the logic of your changes is as follows:
1. provide a
sched_out()handler which charges the previous/current group with the budget consumed by the outgoing thread, dropping such accounting fromsched_pick()in the same move. As a consequence, charge the consumed budget inquota_limit_handler()too since you can't expectsched_pick()to do so (i.e. turning on the resched bit oncurr->rqinquota_limit_handler()would not help in this case).
Yep.
Quoted 7 lines
2. invoke
sched_out()aftersched_pick(), unless prev == next.Since
sched_pick()checksnext->quota->run_budgetto make sure that next may run, what if curr and next belong to the same quota group given thatcurr->quota->run_budgetwon't be updated bysched_out()untilsched_pick()has returned?
Nothing is changed in case final next->quota == curr->quota. It's indeed true that the run_budget == 0 check is not going to trigger anymore - can be removed, the quota-timer will take care anyway.
Jan
Signature · 2 lines
Siemens AG, Foundational Technologies Linux Expert Center
Quoted 611 lines · Jan Kiszka
Quoted 596 lines · Philippe Gerum
Quoted 589 lines · Jan Kiszka
Quoted 58 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
Quoted 4 lines
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).Please avoid that for the final patch. I already had troubles telling substantial from cosmetical changes apart in your lazy-blocking commit (>50% unrelated changes in there).
Quoted 12 lines
commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.Quoted 5 lines
To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO....and why we need a separate queue now? Please provide reasoning for changes.
Quoted 334 lines
Signed-off-by Philippe Gerum <[email protected]>include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);include/evl/sched/quota.h +1
-0@@ -40,6 +40,7 @@ struct evl_quota_group {struct evl_sched_quota {ktime_t period; +struct evl_sched_queue runnable;struct evl_timer refill_timer;struct evl_timer limit_timer;struct list_head groups;kernel/evl/sched/core.c +13
-7@@ -773,7 +773,8 @@ static inline void set_next_running(struct evl_rq *rq,evl_stop_timer (&rq->rrbtimer); } -static struct evl_thread *__pick_next_thread (struct evl_rq *rq) +static __always_inline struct evl_thread * +__pick_next_thread (struct evl_rq *rq) {struct evl_sched_class *sched_class;struct evl_thread *curr = rq->curr;@@ -821,8 +822,14 @@ static struct evl_thread *__pick_next_thread(struct evl_rq *rq)/* rq->curr->lock + rq->lock held, hard irqs off. */ static struct evl_thread *pick_next_thread (struct evl_rq *rq) { -struct evl_thread *next =__pick_next_thread (rq); +struct evl_thread *next, *prev = rq->curr; +struct evl_sched_class *prev_class = prev->sched_class; +if (prev_class->sched_out) + prev_class->sched_out (prev); + + next =__pick_next_thread (rq); +trace_evl_pick_thread (next);set_next_running (rq, next);return next;@@ -972,21 +979,20 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */return ; } + prev = curr; next =pick_next_thread (this_rq); -trace_evl_pick_thread (next); -if (next == curr) { -if (unlikely (next->state &EVL_T_ROOT )) { +if (next == prev) { +if (unlikely (prev->state &EVL_T_ROOT )) {if (this_rq->local_flags &RQ_TPROXY )evl_notify_proxy_tick (this_rq);if (this_rq->local_flags &RQ_TDEFER )evl_program_local_tick (&evl_mono_clock); }raw_spin_unlock (&this_rq->lock); -raw_spin_unlock_irqrestore (&curr->lock, flags); +raw_spin_unlock_irqrestore (&prev->lock, flags);return ; } - prev = curr; this_rq->curr = next; leaving_inband = false;kernel/evl/sched/quota.c +57
-63@@ -12,45 +12,35 @@#include <uapi/evl/sched-abi.h> /* -* With this policy, each per-CPU runqueue maintains a list of active -* thread groups for the sched_fifo class. -* -* Each time a thread is picked from the runqueue, we check whether we -* still have budget for running it, looking at the group it belongs -* to. If so, a timer is armed to elapse when that group has no more -* budget, would the incoming thread run unpreempted until then -* (i.e. evl_quota->limit_timer). +* Each time a thread is picked from the ->runnable queue, we check +* whether the group it belongs to still has runtime budget. If so, a +* timer is armed to fire when that group has no more budget, would +* the incoming thread run unpreempted until then +* (i.e. quota->limit_timer). * * Otherwise, if no budget remains in the group for running the * candidate thread, we move the latter to a local expiry queue * maintained by the group. This process is done on the fly as we pull -* from the runqueue. +* from the ->runnable queue. * -* Updating the remaining budget is done each time the EVL core asks -* for replacing the current thread with the next runnable one, -* i.e. evl_quota_pick(). There we charge the elapsed run time of the -* outgoing thread to the relevant group, and conversely, we check -* whether the incoming thread has budget. +* Updating the remaining budget is done each time the EVL core +* schedules out a thread undergoing the quota scheduling policy, * -* Finally, a per-CPU timer (evl_quota->refill_timer) periodically -* ticks in the background, in accordance to the defined quota -* interval. Thread group budgets get replenished by its handler in -* accordance to their respective share, pushing all expired threads -* back to the run queue in the same move. +* Finally, a per-CPU timer (quota->refill_timer) periodically ticks +* in the background, in accordance to the defined quota interval, +* replenishing per-group budgets, pushing all expired threads back to +* the quota ->runqueue too. * -* NOTE: since the core logic enforcing the budget entirely happens in -* evl_quota_pick(), applying a budget change can be done as simply as -* forcing the rescheduling procedure to be invoked asap. As a result -* of this, the EVL core will ask for the next thread to run, which -* means calling evl_quota_pick() eventually. +* NOTE: forcing a call to the rescheduling procedure is enoiugh to +* apply a budget change. * -* CAUTION: evl_quota_group->nr_active does count both the threads -* from that group linked to the sched_fifo runqueue, _and_ the -* threads moved to the local expiry queue. As a matter of fact, the -* expired threads - those for which we consumed all the per-group -* budget - are still seen as runnable (i.e. not blocked/suspended) by -* the EVL core. This only means that the SCHED_QUOTA policy won't -* pick them until the corresponding budget is replenished. +* CAUTION: quota_group->nr_active does count both the threads from +* that group linked to the runnable queue, _and_ the threads moved to +* the local expiry queue. As a matter of fact, the expired threads - +* those for which we consumed all the per-group budget - are still +* seen as runnable (i.e. not blocked/suspended) by the EVL core. This +* only means that the SCHED_QUOTA policy won't pick them until the +* corresponding budget is replenished. */ #define MAX_QUOTA_GROUPS 1024@@ -61,15 +51,19 @@ static DECLARE_BITMAP(group_map, MAX_QUOTA_GROUPS);static LIST_HEAD (group_list); -static inline bool thread_on_quota (struct evl_thread *thread, -struct evl_quota_group *tg) +static inline bool current_on_quota (struct evl_quota_group *tg) { -/* -* Check whether @thread is running on some CPU, and belongs -* to quota group @tg. -*/ -return thread->quota == tg && - !(thread->state & (EVL_T_READY |EVL_THREAD_BLOCK_MASK )); +struct evl_rq *rq = tg->rq; +struct evl_thread *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; + +if (curr->quota != tg) +return false; + +if (curr->state & (EVL_T_READY |EVL_T_KICKED |EVL_THREAD_BLOCK_MASK )) +return false; + +return evl_timer_is_running (&qs->limit_timer); }static inline bool group_is_active (struct evl_quota_group *tg)@@ -82,7 +76,7 @@ static inline bool group_is_active(struct evl_quota_group *tg)* runqueue, in which case tg->nr_active already accounted for * it. */ -return thread_on_quota (tg->rq->curr, tg); +return current_on_quota (tg); } static inline void replenish_budget(struct evl_sched_quota *qs,@@ -134,10 +128,10 @@ static inline void replenish_budget(struct evl_sched_quota *qs, }else if (tg->run_credit) { credit =ktime_sub (tg->quota_peak, budget);/* Consume the accumulated credit. */ -if (tg->run_credit >= credit) +if (tg->run_credit >= credit) { tg->run_credit =ktime_sub (tg->run_credit, credit); -else { + }else { credit = tg->run_credit; tg->run_credit =0 ; }@@ -150,8 +144,8 @@ static inline void replenish_budget(struct evl_sched_quota *qs,static void quota_refill_handler (struct evl_timer *timer)/* oob stage stalled */ { -struct evl_quota_group *tg;struct evl_thread *thread, *tmp; +struct evl_quota_group *tg;struct evl_sched_quota *qs;struct evl_rq *rq;@@ -167,7 +161,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *if (tg->run_budget ==0 ||list_empty (&tg->expired))continue ;/* -* For each group living on this CPU, move all expired +* For each group pinned on this CPU, move all expired * threads back to the runqueue. Since those threads * were moved out of the runqueue as we were * considering them for execution, we push them back @@ -178,7 +172,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }@@ -195,7 +189,7 @@ static void quota_limit_handler(struct evl_timer *timer) /* oob stage stalled *//* * Force a rescheduling on the return path of the current * interrupt, so that the budget is re-evaluated for the -* current group in evl_quota_pick(). +* current group in quota_pick(). */ raw_spin_lock (&rq->lock);evl_set_self_resched (rq);@@ -221,6 +215,7 @@ static void quota_init(struct evl_rq *rq) {struct evl_sched_quota *qs = &rq->quota; +evl_init_schedq (&qs->runnable); qs->period = quota_period;INIT_LIST_HEAD (&qs->groups);@@ -337,8 +332,8 @@ static void quota_forget(struct evl_thread *thread)static void quota_kick (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;/* * Allow a kicked thread to be elected for running until it @@ -347,7 +342,7 @@ static void quota_kick(struct evl_thread *thread)*/ if (tg->run_budget ==0 && !list_empty (&thread->quota_expired)) {list_del_init (&thread->quota_expired); -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); } }@@ -358,79 +353,82 @@ static inline int thread_is_runnable(struct evl_thread *thread)static void quota_enqueue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add_tail (&thread->quota_expired, &tg->expired);else -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); tg->nr_active++; }static void quota_dequeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!list_empty (&thread->quota_expired))list_del_init (&thread->quota_expired);else -evl_del_schedq (&rq->fifo.runnable, thread); +evl_del_schedq (&qs->runnable, thread); tg->nr_active--; }static void quota_requeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add (&thread->quota_expired, &tg->expired);else -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); tg->nr_active++; } -static struct evl_thread *quota_pick (struct evl_rq *rq) +static void quota_out (struct evl_thread *thread) { -struct evl_thread *next, *curr = rq->curr; -struct evl_sched_quota *qs = &rq->quota; -struct evl_quota_group *otg, *tg; -ktime_t now, elapsed; +struct evl_sched_quota *qs = &thread->rq->quota; +struct evl_quota_group *tg = thread->quota; +ktime_t now, consumed; + +/* Timer off means that we are not tracking quota. */ +if (!evl_timer_is_running (&qs->limit_timer)) +return ; - now =evl_ktime_monotonic (); - otg = curr->quota; -if (otg ==NULL ) - goto pick;kernel/evl/sched/quota.c static inline int thread_is_runnable(struct evl_thread *thread) · line 407
Quoted 10 lines
kernel/evl/sched/quota.c +1
-5/* * Charge the time consumed by the outgoing thread to the * group it belongs to. */ - elapsed =ktime_sub (now, otg->run_start); -if (elapsed < otg->run_budget) - otg->run_budget =ktime_sub (otg->run_budget, elapsed); -else - otg->run_budget =0 ; + now =evl_ktime_monotonic ();Thing brins some, though minor, glitch when doing the
run_startupdate for the next group already in pick. That's why I'm trying to reuse that stamp when available.Quoted 139 lines
kernel/evl/sched/quota.c +40
-36+ consumed =ktime_sub (now, tg->run_start); +if (consumed < tg->run_budget) { + tg->run_budget =ktime_sub (tg->run_budget, consumed); + }else { + tg->run_budget =0 ; +evl_stop_timer (&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick (struct evl_rq *rq) +{ +struct evl_thread *next, *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; +struct evl_quota_group *tg; + pick: - next =evl_get_schedq (&rq->fifo.runnable); + next =evl_get_schedq (&qs->runnable);if (next ==NULL ) {evl_stop_timer (&qs->limit_timer);return NULL ; } -/* -* As we basically piggyback on the SCHED_FIFO runqueue, make -* sure to detect non-quota threads. -*/ tg = next->quota; -if (tg ==NULL ) -return next; - - tg->run_start = now; + tg->nr_active--;/* * Don't consider budget if kicked, we have to allow this @@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)*/ if (next->info &EVL_T_KICKED ) {evl_stop_timer (&qs->limit_timer); - goto out; +return next; } +/* +* __pick_next_thread() might have requeued the current +* thread which is still leading the pack. +*/ +if (curr == next) +return next; +if (ktime_to_ns (tg->run_budget) ==0 ) { -/* Flush expired group members as we go. */ +/* Park expired group members as we go. */ list_add_tail (&next->quota_expired, &tg->expired); goto pick; } -if (otg == tg &&evl_timer_is_running (&qs->limit_timer)) -/* Same group, leave the running timer untouched. */ - goto out; - -/* Arm limit timer for the new running group. */ -evl_start_timer (&qs->limit_timer, -ktime_add (now, tg->run_budget), -EVL_INFINITE ); -out: - tg->nr_active--; +/* Arm new limit timer on change of running group. */ +if (curr->quota != tg || !evl_timer_is_running (&qs->limit_timer)) { + tg->run_start =evl_ktime_monotonic (); +evl_start_timer (&qs->limit_timer, +ktime_add (tg->run_start, tg->run_budget), +EVL_INFINITE ); + }return next; }@@ -542,7 +544,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* Unregister the group before we drop rq->lock. As a result, * it won't accept threads anymore while we are busy moving * the current members to the fifo class, and concurrent -* evl_quota_remove requests would receive -EINVAL. +* quota_remove requests would receive -EINVAL. */ __clear_bit (tg->tgid, group_map);list_del (&tg->next);@@ -556,7 +558,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* hold rq->lock on entry, we do a trylock dance to prevent an * ABBA issue. No livelock is possible since we unregistered * that group already, so &tg->members can only be depleted -* (by this loop specifically). +* (by this loop exclusively). */ while (!list_empty (&tg->members)) {@@ -583,10 +585,10 @@ static void quota_set_limit(struct evl_quota_group *tg, int *quota_sum_r) {struct evl_rq *rq = tg->rq; -struct evl_thread *thread, *tmp, *curr = rq->curr; +struct evl_thread *thread, *tmp;struct evl_sched_quota *qs = &rq->quota; -ktime_t now, elapsed, consumed;ktime_t old_quota = tg->quota; +ktime_t consumed;u64 n;assert_hard_lock (&rq->lock);@@ -615,16 +617,8 @@ static void quota_set_limit(struct evl_quota_group *tg, tg->quota_percent = quota_percent; tg->quota_peak_percent = quota_peak_percent; -if (thread_on_quota (curr, tg)) { - now =evl_ktime_monotonic (); - - elapsed = now - tg->run_start; -if (elapsed < tg->run_budget) - tg->run_budget -= elapsed; -else - tg->run_budget =0 ; - - tg->run_start = now; +if (current_on_quota (tg)) { +quota_out (rq->curr);evl_stop_timer (&qs->limit_timer); }@@ -646,7 +640,7 @@ static void quota_set_limit(struct evl_quota_group *tg,list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }kernel/evl/sched/quota.c struct evl_sched_class evl_sched_quota = { · line 786
Quoted 7 lines
kernel/evl/sched/quota.c +1
-0@@ -786,6 +780,7 @@ struct evl_sched_class evl_sched_quota = { .sched_dequeue = quota_dequeue, .sched_requeue = quota_requeue, .sched_pick = quota_pick, + .sched_out = quota_out, .sched_migrate = quota_migrate, .sched_chkparam = quota_chkparam, .sched_setparam = quota_setparam,You also seem to miss the case I discovered only yesterday as well: When starting the current thread on a quota via setparam, we also need to set its
run_startstamp at that point.Quoted 4 lines
Please have a closer look at both my test case (libevl) as well as my patch 2. Currently, I don't see remaining issues with both, but I'm all ears to learn what I missed.
Quoted 8 lines
AFAIU, the logic of your changes is as follows:
1. provide a
sched_out()handler which charges the previous/current group with the budget consumed by the outgoing thread, dropping such accounting fromsched_pick()in the same move. As a consequence, charge the consumed budget inquota_limit_handler()too since you can't expectsched_pick()to do so (i.e. turning on the resched bit oncurr->rqinquota_limit_handler()would not help in this case).Yep.
Quoted 3 lines
2. invoke
sched_out()aftersched_pick(), unless prev == next.
Quoted 9 lines
Quoted 4 lines
Since
sched_pick()checksnext->quota->run_budgetto make sure that next may run, what if curr and next belong to the same quota group given thatcurr->quota->run_budgetwon't be updated bysched_out()untilsched_pick()has returned?Nothing is changed in case final
next->quota==curr->quota. It's indeed true that therun_budget== 0 check is not going to trigger anymore - can be removed, the quota-timer will take care anyway.
For this to work, every place stopping the timer would have to call charge_usage() then.
Signature · 1 line
Philippe.
Quoted 416 lines · Philippe Gerum
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This shall be invoked before a thread switch, providing both the current and the next thread as arguments. Some scheduling classes may need it to correctly handle their state as the
sched_pickmay not be invoked when a higher-weighted class is providing the next thread.Signed-off-by Jan Kiszka <[email protected]>--- include/evl/sched.h | 2 ++ kernel/evl/sched/core.c | 5 +++++ 2 files changed, 7 insertions(+)include/evl/sched.h +2
-0@@ -120,6 +120,8 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread, +struct evl_thread *next);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);kernel/evl/sched/core.c +1
-0@@ -910,6 +910,7 @@ static __always_inline bool test_resched(struct evl_rq *this_rq)*/ void __evl_schedule (void )/* oob or/and hard irqs off (CPU migration-safe) */ { +struct evl_sched_class *prev_schedclass;struct evl_rq *this_rq =this_evl_rq ();struct evl_thread *prev, *next, *curr;bool leaving_inband, inband_tail;@@ -990,6 +991,10 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ this_rq->curr = next; leaving_inband = false;Quoted 11 lines
kernel/evl/sched/core.c void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */ · line 1000
Quoted 7 lines
kernel/evl/sched/core.c +4
-0+ prev_schedclass = prev->sched_class; +if (prev_schedclass->sched_out) + prev_schedclass->sched_out (prev, next); +/* * Careful: we _must_ have updated this_rq->curr before * performing the rest of the context switch code I've been working on this lately too. It turns out that we need more than this, although this is definitely part of the solution. I'll follow up on this issue.
This is still wip, I'm sharing this early to discuss details for reconciling both proposals (the hunk in
__evl_schedule()is merely cosmetic, no functional change).commit
f49dbd63c1389a6b99508ef0da852545b102d1cc(HEAD -> wip/fix-quota-sched)Author: Philippe Gerum <[email protected]> Date: Sun Jun 14 12:08:59 2026 +0200evl: sched/quota: fix budget tracking on preemption
Upon preemption of a
SCHED_QUOTAthread by aSCHED_FIFOone, the runtime budget of the former is inaccurately tracked. This is due to thefifo->pick()handler returning a valid thread, which prevents thequota->pick()handler from being called. As a result, the last runtime period of the outgoing thread is not accounted for.To fix this, we introduce a new
sched_out()handler which is called for the outgoing thread, which the quota policy uses to update the remaining budget of preempted threads appropriately. In addition, the implementation no longer shares the runnable thread queue withSCHED_FIFO.Signed-off-by Philippe Gerum <[email protected]>include/evl/sched.h +1
-0@@ -120,6 +120,7 @@ struct evl_sched_class {void (*sched_dequeue)(struct evl_thread *thread);void (*sched_requeue)(struct evl_thread *thread);struct evl_thread *(*sched_pick)(struct evl_rq *rq); +void (*sched_out)(struct evl_thread *thread);void (*sched_yield)(struct evl_thread *thread);void (*sched_migrate)(struct evl_thread *thread,struct evl_rq *rq);include/evl/sched/quota.h +1
-0@@ -40,6 +40,7 @@ struct evl_quota_group {struct evl_sched_quota {ktime_t period; +struct evl_sched_queue runnable;struct evl_timer refill_timer;struct evl_timer limit_timer;struct list_head groups;kernel/evl/sched/core.c +13
-7@@ -773,7 +773,8 @@ static inline void set_next_running(struct evl_rq *rq,evl_stop_timer (&rq->rrbtimer); } -static struct evl_thread *__pick_next_thread (struct evl_rq *rq) +static __always_inline struct evl_thread * +__pick_next_thread (struct evl_rq *rq) {struct evl_sched_class *sched_class;struct evl_thread *curr = rq->curr;@@ -821,8 +822,14 @@ static struct evl_thread *__pick_next_thread(struct evl_rq *rq)/* rq->curr->lock + rq->lock held, hard irqs off. */ static struct evl_thread *pick_next_thread (struct evl_rq *rq) { -struct evl_thread *next =__pick_next_thread (rq); +struct evl_thread *next, *prev = rq->curr; +struct evl_sched_class *prev_class = prev->sched_class; +if (prev_class->sched_out) + prev_class->sched_out (prev); + + next =__pick_next_thread (rq); +trace_evl_pick_thread (next);set_next_running (rq, next);return next;@@ -972,21 +979,20 @@ void __evl_schedule(void) /* oob or/and hard irqs off (CPU migration-safe) */return ; } + prev = curr; next =pick_next_thread (this_rq); -trace_evl_pick_thread (next); -if (next == curr) { -if (unlikely (next->state &EVL_T_ROOT )) { +if (next == prev) { +if (unlikely (prev->state &EVL_T_ROOT )) {if (this_rq->local_flags &RQ_TPROXY )evl_notify_proxy_tick (this_rq);if (this_rq->local_flags &RQ_TDEFER )evl_program_local_tick (&evl_mono_clock); }raw_spin_unlock (&this_rq->lock); -raw_spin_unlock_irqrestore (&curr->lock, flags); +raw_spin_unlock_irqrestore (&prev->lock, flags);return ; } - prev = curr; this_rq->curr = next; leaving_inband = false;kernel/evl/sched/quota.c +57
-63@@ -12,45 +12,35 @@#include <uapi/evl/sched-abi.h> /* -* With this policy, each per-CPU runqueue maintains a list of active -* thread groups for the sched_fifo class. -* -* Each time a thread is picked from the runqueue, we check whether we -* still have budget for running it, looking at the group it belongs -* to. If so, a timer is armed to elapse when that group has no more -* budget, would the incoming thread run unpreempted until then -* (i.e. evl_quota->limit_timer). +* Each time a thread is picked from the ->runnable queue, we check +* whether the group it belongs to still has runtime budget. If so, a +* timer is armed to fire when that group has no more budget, would +* the incoming thread run unpreempted until then +* (i.e. quota->limit_timer). * * Otherwise, if no budget remains in the group for running the * candidate thread, we move the latter to a local expiry queue * maintained by the group. This process is done on the fly as we pull -* from the runqueue. +* from the ->runnable queue. * -* Updating the remaining budget is done each time the EVL core asks -* for replacing the current thread with the next runnable one, -* i.e. evl_quota_pick(). There we charge the elapsed run time of the -* outgoing thread to the relevant group, and conversely, we check -* whether the incoming thread has budget. +* Updating the remaining budget is done each time the EVL core +* schedules out a thread undergoing the quota scheduling policy, * -* Finally, a per-CPU timer (evl_quota->refill_timer) periodically -* ticks in the background, in accordance to the defined quota -* interval. Thread group budgets get replenished by its handler in -* accordance to their respective share, pushing all expired threads -* back to the run queue in the same move. +* Finally, a per-CPU timer (quota->refill_timer) periodically ticks +* in the background, in accordance to the defined quota interval, +* replenishing per-group budgets, pushing all expired threads back to +* the quota ->runqueue too. * -* NOTE: since the core logic enforcing the budget entirely happens in -* evl_quota_pick(), applying a budget change can be done as simply as -* forcing the rescheduling procedure to be invoked asap. As a result -* of this, the EVL core will ask for the next thread to run, which -* means calling evl_quota_pick() eventually. +* NOTE: forcing a call to the rescheduling procedure is enoiugh to +* apply a budget change. * -* CAUTION: evl_quota_group->nr_active does count both the threads -* from that group linked to the sched_fifo runqueue, _and_ the -* threads moved to the local expiry queue. As a matter of fact, the -* expired threads - those for which we consumed all the per-group -* budget - are still seen as runnable (i.e. not blocked/suspended) by -* the EVL core. This only means that the SCHED_QUOTA policy won't -* pick them until the corresponding budget is replenished. +* CAUTION: quota_group->nr_active does count both the threads from +* that group linked to the runnable queue, _and_ the threads moved to +* the local expiry queue. As a matter of fact, the expired threads - +* those for which we consumed all the per-group budget - are still +* seen as runnable (i.e. not blocked/suspended) by the EVL core. This +* only means that the SCHED_QUOTA policy won't pick them until the +* corresponding budget is replenished. */ #define MAX_QUOTA_GROUPS 1024@@ -61,15 +51,19 @@ static DECLARE_BITMAP(group_map, MAX_QUOTA_GROUPS);static LIST_HEAD (group_list); -static inline bool thread_on_quota (struct evl_thread *thread, -struct evl_quota_group *tg) +static inline bool current_on_quota (struct evl_quota_group *tg) { -/* -* Check whether @thread is running on some CPU, and belongs -* to quota group @tg. -*/ -return thread->quota == tg && - !(thread->state & (EVL_T_READY |EVL_THREAD_BLOCK_MASK )); +struct evl_rq *rq = tg->rq; +struct evl_thread *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; + +if (curr->quota != tg) +return false; + +if (curr->state & (EVL_T_READY |EVL_T_KICKED |EVL_THREAD_BLOCK_MASK )) +return false; + +return evl_timer_is_running (&qs->limit_timer); }static inline bool group_is_active (struct evl_quota_group *tg)@@ -82,7 +76,7 @@ static inline bool group_is_active(struct evl_quota_group *tg)* runqueue, in which case tg->nr_active already accounted for * it. */ -return thread_on_quota (tg->rq->curr, tg); +return current_on_quota (tg); } static inline void replenish_budget(struct evl_sched_quota *qs,@@ -134,10 +128,10 @@ static inline void replenish_budget(struct evl_sched_quota *qs, }else if (tg->run_credit) { credit =ktime_sub (tg->quota_peak, budget);/* Consume the accumulated credit. */ -if (tg->run_credit >= credit) +if (tg->run_credit >= credit) { tg->run_credit =ktime_sub (tg->run_credit, credit); -else { + }else { credit = tg->run_credit; tg->run_credit =0 ; }@@ -150,8 +144,8 @@ static inline void replenish_budget(struct evl_sched_quota *qs,static void quota_refill_handler (struct evl_timer *timer)/* oob stage stalled */ { -struct evl_quota_group *tg;struct evl_thread *thread, *tmp; +struct evl_quota_group *tg;struct evl_sched_quota *qs;struct evl_rq *rq;@@ -167,7 +161,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *if (tg->run_budget ==0 ||list_empty (&tg->expired))continue ;/* -* For each group living on this CPU, move all expired +* For each group pinned on this CPU, move all expired * threads back to the runqueue. Since those threads * were moved out of the runqueue as we were * considering them for execution, we push them back @@ -178,7 +172,7 @@ static void quota_refill_handler(struct evl_timer *timer) /* oob stage stalled *list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }@@ -195,7 +189,7 @@ static void quota_limit_handler(struct evl_timer *timer) /* oob stage stalled *//* * Force a rescheduling on the return path of the current * interrupt, so that the budget is re-evaluated for the -* current group in evl_quota_pick(). +* current group in quota_pick(). */ raw_spin_lock (&rq->lock);evl_set_self_resched (rq);@@ -221,6 +215,7 @@ static void quota_init(struct evl_rq *rq) {struct evl_sched_quota *qs = &rq->quota; +evl_init_schedq (&qs->runnable); qs->period = quota_period;INIT_LIST_HEAD (&qs->groups);@@ -337,8 +332,8 @@ static void quota_forget(struct evl_thread *thread)static void quota_kick (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;/* * Allow a kicked thread to be elected for running until it @@ -347,7 +342,7 @@ static void quota_kick(struct evl_thread *thread)*/ if (tg->run_budget ==0 && !list_empty (&thread->quota_expired)) {list_del_init (&thread->quota_expired); -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); } }@@ -358,79 +353,82 @@ static inline int thread_is_runnable(struct evl_thread *thread)static void quota_enqueue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add_tail (&thread->quota_expired, &tg->expired);else -evl_add_schedq_tail (&rq->fifo.runnable, thread); +evl_add_schedq_tail (&qs->runnable, thread); tg->nr_active++; }static void quota_dequeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!list_empty (&thread->quota_expired))list_del_init (&thread->quota_expired);else -evl_del_schedq (&rq->fifo.runnable, thread); +evl_del_schedq (&qs->runnable, thread); tg->nr_active--; }static void quota_requeue (struct evl_thread *thread) { +struct evl_sched_quota *qs = &thread->rq->quota;struct evl_quota_group *tg = thread->quota; -struct evl_rq *rq = thread->rq;if (!thread_is_runnable (thread))list_add (&thread->quota_expired, &tg->expired);else -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); tg->nr_active++; } -static struct evl_thread *quota_pick (struct evl_rq *rq) +static void quota_out (struct evl_thread *thread) { -struct evl_thread *next, *curr = rq->curr; -struct evl_sched_quota *qs = &rq->quota; -struct evl_quota_group *otg, *tg; -ktime_t now, elapsed; +struct evl_sched_quota *qs = &thread->rq->quota; +struct evl_quota_group *tg = thread->quota; +ktime_t now, consumed; + +/* Timer off means that we are not tracking quota. */ +if (!evl_timer_is_running (&qs->limit_timer)) +return ; - now =evl_ktime_monotonic (); - otg = curr->quota; -if (otg ==NULL ) - goto pick;/* * Charge the time consumed by the outgoing thread to the * group it belongs to.
kernel/evl/sched/quota.c static inline int thread_is_runnable(struct evl_thread *thread) · line 410
Quoted 10 lines
kernel/evl/sched/quota.c +4
-5*/ - elapsed =ktime_sub (now, otg->run_start); -if (elapsed < otg->run_budget) - otg->run_budget =ktime_sub (otg->run_budget, elapsed); -else - otg->run_budget =0 ; + now =evl_ktime_monotonic (); + consumed =ktime_sub (now, tg->run_start); +if (consumed < tg->run_budget) { + tg->run_budget =ktime_sub (tg->run_budget, consumed);
You also need to update tg->run_start. Otherwise, you will overcharge the next time this group is checked here.
Quoted 62 lines
kernel/evl/sched/quota.c +23
-18+ }else { + tg->run_budget =0 ; +evl_stop_timer (&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick (struct evl_rq *rq) +{ +struct evl_thread *next, *curr = rq->curr; +struct evl_sched_quota *qs = &rq->quota; +struct evl_quota_group *tg; + pick: - next =evl_get_schedq (&rq->fifo.runnable); + next =evl_get_schedq (&qs->runnable);if (next ==NULL ) {evl_stop_timer (&qs->limit_timer);return NULL ; } -/* -* As we basically piggyback on the SCHED_FIFO runqueue, make -* sure to detect non-quota threads. -*/ tg = next->quota; -if (tg ==NULL ) -return next; - - tg->run_start = now; + tg->nr_active--;/* * Don't consider budget if kicked, we have to allow this @@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)*/ if (next->info &EVL_T_KICKED ) {evl_stop_timer (&qs->limit_timer); - goto out; +return next; } +/* +* __pick_next_thread() might have requeued the current +* thread which is still leading the pack. +*/ +if (curr == next) +return next; +if (ktime_to_ns (tg->run_budget) ==0 ) { -/* Flush expired group members as we go. */ +/* Park expired group members as we go. */ list_add_tail (&next->quota_expired, &tg->expired); goto pick; } -if (otg == tg &&evl_timer_is_running (&qs->limit_timer)) -/* Same group, leave the running timer untouched. */ - goto out; - -/* Arm limit timer for the new running group. */ - evl_start_timer(&qs->limit_timer, - ktime_add(now, tg->run_budget),
kernel/evl/sched/quota.c static struct evl_thread *quota_pick(struct evl_rq *rq) · line 462
Quoted 10 lines
kernel/evl/sched/quota.c +7
-3- EVL_INFINITE); -out: - tg->nr_active--; +/* Arm new limit timer on change of running group. */ +if (curr->quota != tg || !evl_timer_is_running (&qs->limit_timer)) { + tg->run_start =evl_ktime_monotonic (); +evl_start_timer (&qs->limit_timer, +ktime_add (tg->run_start, tg->run_budget), +EVL_INFINITE ); + }
I still don't fully get how the limit timer should be started on setparam over the current thread. Will curr be NULL in that case, thus curr != next? Is that the reason to go for a separate runqueue?
Quoted 64 lines
kernel/evl/sched/quota.c +7
-15return next; }@@ -542,7 +544,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* Unregister the group before we drop rq->lock. As a result, * it won't accept threads anymore while we are busy moving * the current members to the fifo class, and concurrent -* evl_quota_remove requests would receive -EINVAL. +* quota_remove requests would receive -EINVAL. */ __clear_bit (tg->tgid, group_map);list_del (&tg->next);@@ -556,7 +558,7 @@ static int quota_destroy_group(struct evl_quota_group *tg,* hold rq->lock on entry, we do a trylock dance to prevent an * ABBA issue. No livelock is possible since we unregistered * that group already, so &tg->members can only be depleted -* (by this loop specifically). +* (by this loop exclusively). */ while (!list_empty (&tg->members)) {@@ -583,10 +585,10 @@ static void quota_set_limit(struct evl_quota_group *tg, int *quota_sum_r) {struct evl_rq *rq = tg->rq; -struct evl_thread *thread, *tmp, *curr = rq->curr; +struct evl_thread *thread, *tmp;struct evl_sched_quota *qs = &rq->quota; -ktime_t now, elapsed, consumed;ktime_t old_quota = tg->quota; +ktime_t consumed;u64 n;assert_hard_lock (&rq->lock);@@ -615,16 +617,8 @@ static void quota_set_limit(struct evl_quota_group *tg, tg->quota_percent = quota_percent; tg->quota_peak_percent = quota_peak_percent; -if (thread_on_quota (curr, tg)) { - now =evl_ktime_monotonic (); - - elapsed = now - tg->run_start; -if (elapsed < tg->run_budget) - tg->run_budget -= elapsed; -else - tg->run_budget =0 ; - - tg->run_start = now; +if (current_on_quota (tg)) { +quota_out (rq->curr);evl_stop_timer (&qs->limit_timer); }@@ -646,7 +640,7 @@ static void quota_set_limit(struct evl_quota_group *tg,list_for_each_entry_safe_reverse (thread, tmp, &tg->expired, quota_expired) {list_del_init (&thread->quota_expired); -evl_add_schedq (&rq->fifo.runnable, thread); +evl_add_schedq (&qs->runnable, thread); } }
kernel/evl/sched/quota.c struct evl_sched_class evl_sched_quota = { · line 786
Quoted 7 lines
kernel/evl/sched/quota.c +1
-0@@ -786,6 +780,7 @@ struct evl_sched_class evl_sched_quota = { .sched_dequeue = quota_dequeue, .sched_requeue = quota_requeue, .sched_pick = quota_pick, + .sched_out = quota_out, .sched_migrate = quota_migrate, .sched_chkparam = quota_chkparam, .sched_setparam = quota_setparam,
Looks ok otherwise, tests pass as well.
Jan
Signature · 2 lines
Siemens AG, Foundational Technologies Linux Expert Center
Quoted 7 lines · Jan Kiszka
Quoted 2 lines · Philippe Gerum
Philippe Gerum <[email protected]> writes:
You also need to update
tg->run_start. Otherwise, you will overcharge the next time this group is checked here.
Yes, this needs fixing.
Quoted 68 lines
Quoted 68 lines
(quoted diff) +26
-21+ } else { + tg->run_budget = 0; + evl_stop_timer(&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick(struct evl_rq *rq) +{ + struct evl_thread *next, *curr = rq->curr; + struct evl_sched_quota *qs = &rq->quota; + struct evl_quota_group *tg; + pick: - next = evl_get_schedq(&rq->fifo.runnable); + next = evl_get_schedq(&qs->runnable); if (next == NULL) { evl_stop_timer(&qs->limit_timer); return NULL; } - /* - * As we basically piggyback on the SCHED_FIFO runqueue, make - * sure to detect non-quota threads. - */ tg = next->quota; - if (tg == NULL) - return next; - - tg->run_start = now; + tg->nr_active--; /* * Don't consider budget if kicked, we have to allow this@@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq) */ if (next->info & EVL_T_KICKED) { evl_stop_timer(&qs->limit_timer); - goto out; + return next; } + /* + * __pick_next_thread() might have requeued the current + * thread which is still leading the pack. + */ + if (curr == next) + return next; + if (ktime_to_ns(tg->run_budget) == 0) { - /* Flush expired group members as we go. */ + /* Park expired group members as we go. */ list_add_tail(&next->quota_expired, &tg->expired); goto pick; } - if (otg == tg && evl_timer_is_running(&qs->limit_timer)) - /* Same group, leave the running timer untouched. */ - goto out; - - /* Arm limit timer for the new running group. */ - evl_start_timer(&qs->limit_timer, - ktime_add(now, tg->run_budget), - EVL_INFINITE); -out: - tg->nr_active--; + /* Arm new limit timer on change of running group. */ + if (curr->quota != tg || !evl_timer_is_running(&qs->limit_timer)) { + tg->run_start = evl_ktime_monotonic();
Quoted 9 lines
Review static struct evl_thread *quota_pick(struct evl_rq *rq) · line 462
Quoted 4 lines
(quoted diff) +4
-0+ evl_start_timer(&qs->limit_timer, + ktime_add(tg->run_start, tg->run_budget), + EVL_INFINITE); + }I still don't fully get how the limit timer should be started on setparam over the current thread. Will curr be NULL in that case, thus curr != next? Is that the reason to go for a separate runqueue?
Nope, that one is only to make things clearer, since there is no upside in sharing the runqueues (this was inherited from SCHED_SPORADIC for no obvious reason with hindsight). At some point, I noticed that it would even have introduced a bug in a previous tentative implementation using sched_class as a discriminator between threads, to figure out whether we should call the sched_out() handler. So, the simpler, the better:
distinct classes, distinct runqueues.
Regarding the limit timer issue, sched_pick() should not shortcut on curr == next. IOW, the rest of the code should run even in this case. By turning on the resched bit (which the quota_setparam() caller does), we would end up in sched_pick() with either of those outcomes out of quota_setparam():
- 'current' switched group, in which case we would arm a limit timer in sched_pick() because curr->quota != next->quota (provided the shortcut on curr == next is gone).
- 'current' stays in the same group, so the limit timer still runs unless the budget was already fully consumed.
So I would revise my latest patch as follows:
kernel/evl/sched/quota.c +2 -8
@@ -407,6 +407,7 @@ static void quota_out(struct evl_thread *thread) now =evl_ktime_monotonic (); consumed =ktime_sub (now, tg->run_start);if (consumed < tg->run_budget) { + tg->run_start = now; tg->run_budget =ktime_sub (tg->run_budget, consumed); }else { tg->run_budget =0 ;
@@ -440,20 +441,13 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)return next; } -/* -* __pick_next_thread() might have requeued the current -* thread which is still leading the pack. -*/ -if (curr == next) -return next; -if (ktime_to_ns (tg->run_budget) ==0 ) {/* Park expired group members as we go. */ list_add_tail (&next->quota_expired, &tg->expired); goto pick; } -/* Arm new limit timer on change of running group. */ +/* Arm new limit timer if need be. */ if (curr->quota != tg || !evl_timer_is_running (&qs->limit_timer)) { tg->run_start =evl_ktime_monotonic (); evl_start_timer(&qs->limit_timer,
Quoted 3 lines
Looks ok otherwise, tests pass as well.
Uh? Ok. That was absolutely unintentional at this stage!
Signature · 1 line
Philippe.
Quoted 104 lines · Philippe Gerum
Quoted 7 lines · Jan Kiszka
Quoted 2 lines · Philippe Gerum
Philippe Gerum <[email protected]> writes:
You also need to update
tg->run_start. Otherwise, you will overcharge the next time this group is checked here.Yes, this needs fixing.
Quoted 68 lines
Quoted 68 lines
(quoted diff) +26
-21+ } else { + tg->run_budget = 0; + evl_stop_timer(&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick(struct evl_rq *rq) +{ + struct evl_thread *next, *curr = rq->curr; + struct evl_sched_quota *qs = &rq->quota; + struct evl_quota_group *tg; + pick: - next = evl_get_schedq(&rq->fifo.runnable); + next = evl_get_schedq(&qs->runnable); if (next == NULL) { evl_stop_timer(&qs->limit_timer); return NULL; } - /* - * As we basically piggyback on the SCHED_FIFO runqueue, make - * sure to detect non-quota threads. - */ tg = next->quota; - if (tg == NULL) - return next; - - tg->run_start = now; + tg->nr_active--; /* * Don't consider budget if kicked, we have to allow this@@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq) */ if (next->info & EVL_T_KICKED) { evl_stop_timer(&qs->limit_timer); - goto out; + return next; } + /* + * __pick_next_thread() might have requeued the current + * thread which is still leading the pack. + */ + if (curr == next) + return next; + if (ktime_to_ns(tg->run_budget) == 0) { - /* Flush expired group members as we go. */ + /* Park expired group members as we go. */ list_add_tail(&next->quota_expired, &tg->expired); goto pick; } - if (otg == tg && evl_timer_is_running(&qs->limit_timer)) - /* Same group, leave the running timer untouched. */ - goto out; - - /* Arm limit timer for the new running group. */ - evl_start_timer(&qs->limit_timer, - ktime_add(now, tg->run_budget), - EVL_INFINITE); -out: - tg->nr_active--; + /* Arm new limit timer on change of running group. */ + if (curr->quota != tg || !evl_timer_is_running(&qs->limit_timer)) { + tg->run_start = evl_ktime_monotonic();Quoted 9 lines
Review static struct evl_thread *quota_pick(struct evl_rq *rq) · line 462
Quoted 4 lines
(quoted diff) +4
-0+ evl_start_timer(&qs->limit_timer, + ktime_add(tg->run_start, tg->run_budget), + EVL_INFINITE); + }I still don't fully get how the limit timer should be started on setparam over the current thread. Will curr be NULL in that case, thus curr != next? Is that the reason to go for a separate runqueue?
Nope, that one is only to make things clearer, since there is no upside in sharing the runqueues (this was inherited from
SCHED_SPORADICfor no obvious reason with hindsight). At some point, I noticed that it would even have introduced a bug in a previous tentative implementation usingsched_classas a discriminator between threads, to figure out whether we should call thesched_out()handler. So, the simpler, the better:
distinct classes, distinct runqueues.Regarding the limit timer issue,
sched_pick()should not shortcut on curr == next. IOW, the rest of the code should run even in this case. By turning on the resched bit (which thequota_setparam()caller does), we would end up insched_pick()with either of those outcomes out ofquota_setparam():
Quoted 7 lines
- 'current' switched group, in which case we would arm a limit timer in
sched_pick()becausecurr->quota!=next->quota(provided the shortcut on curr == next is gone).- 'current' stays in the same group, so the limit timer still runs unless the budget was already fully consumed.
Makes sense, now.
Quoted 38 lines
So I would revise my latest patch as follows:
kernel/evl/sched/quota.c +2
-8@@ -407,6 +407,7 @@ static void quota_out(struct evl_thread *thread) now =evl_ktime_monotonic (); consumed =ktime_sub (now, tg->run_start);if (consumed < tg->run_budget) { + tg->run_start = now; tg->run_budget =ktime_sub (tg->run_budget, consumed); }else { tg->run_budget =0 ;@@ -440,20 +441,13 @@ static struct evl_thread *quota_pick(struct evl_rq *rq)return next; } -/* -* __pick_next_thread() might have requeued the current -* thread which is still leading the pack. -*/ -if (curr == next) -return next; -if (ktime_to_ns (tg->run_budget) ==0 ) {/* Park expired group members as we go. */ list_add_tail (&next->quota_expired, &tg->expired); goto pick; } -/* Arm new limit timer on change of running group. */ +/* Arm new limit timer if need be. */ if (curr->quota != tg || !evl_timer_is_running (&qs->limit_timer)) { tg->run_start =evl_ktime_monotonic (); evl_start_timer(&qs->limit_timer,Quoted 2 lines
Quoted 5 lines
Quoted 2 lines
Looks ok otherwise, tests pass as well.
Uh? Ok. That was absolutely unintentional at this stage!
Tests aren't perfect either: sched-quota was "tested" before, and my current proposal apparently missed the corner-cases we just discussed above.
Jan
Signature · 2 lines
Siemens AG, Foundational Technologies Linux Expert Center
Quoted 90 lines · Philippe Gerum
Quoted 7 lines · Jan Kiszka
Quoted 2 lines · Philippe Gerum
Philippe Gerum <[email protected]> writes:
You also need to update
tg->run_start. Otherwise, you will overcharge the next time this group is checked here.Yes, this needs fixing.
Quoted 68 lines
Quoted 68 lines
(quoted diff) +26
-21+ } else { + tg->run_budget = 0; + evl_stop_timer(&qs->limit_timer); + } +} + +static struct evl_thread *quota_pick(struct evl_rq *rq) +{ + struct evl_thread *next, *curr = rq->curr; + struct evl_sched_quota *qs = &rq->quota; + struct evl_quota_group *tg; + pick: - next = evl_get_schedq(&rq->fifo.runnable); + next = evl_get_schedq(&qs->runnable); if (next == NULL) { evl_stop_timer(&qs->limit_timer); return NULL; } - /* - * As we basically piggyback on the SCHED_FIFO runqueue, make - * sure to detect non-quota threads. - */ tg = next->quota; - if (tg == NULL) - return next; - - tg->run_start = now; + tg->nr_active--; /* * Don't consider budget if kicked, we have to allow this@@ -439,25 +437,29 @@ static struct evl_thread *quota_pick(struct evl_rq *rq) */ if (next->info & EVL_T_KICKED) { evl_stop_timer(&qs->limit_timer); - goto out; + return next; } + /* + * __pick_next_thread() might have requeued the current + * thread which is still leading the pack. + */ + if (curr == next) + return next; + if (ktime_to_ns(tg->run_budget) == 0) { - /* Flush expired group members as we go. */ + /* Park expired group members as we go. */ list_add_tail(&next->quota_expired, &tg->expired); goto pick; } - if (otg == tg && evl_timer_is_running(&qs->limit_timer)) - /* Same group, leave the running timer untouched. */ - goto out; - - /* Arm limit timer for the new running group. */ - evl_start_timer(&qs->limit_timer, - ktime_add(now, tg->run_budget), - EVL_INFINITE); -out: - tg->nr_active--; + /* Arm new limit timer on change of running group. */ + if (curr->quota != tg || !evl_timer_is_running(&qs->limit_timer)) { + tg->run_start = evl_ktime_monotonic();Quoted 9 lines
Review static struct evl_thread *quota_pick(struct evl_rq *rq) · line 462
Quoted 4 lines
(quoted diff) +4
-0+ evl_start_timer(&qs->limit_timer, + ktime_add(tg->run_start, tg->run_budget), + EVL_INFINITE); + }I still don't fully get how the limit timer should be started on setparam over the current thread. Will curr be NULL in that case, thus curr != next? Is that the reason to go for a separate runqueue?
Quoted 3 lines
Nope, that one is only to make things clearer, since there is no upside in sharing the runqueues (this was inherited from
SCHED_SPORADICfor no obvious reason with hindsight).
There was an obvious reason for the quota thread to piggyback off the fifo runqueue though, i.e. the ability for quota threads to compete for CPU with fifo threads. Moving the former to their own runqueue, with SCHED_QUOTA having a lower scheduler weight than SCHED_FIFO, the latter would always preempt, regardless of the fixed priority.
e.g. we would have the following sequence with the current implementation (provided T2 still has budget):
[SCHED_FIFO] T1(fifo:3) => T2(quota:2) => T3(fifo:1)
and that one with the split runqueues:
[SCHED_FIFO] T1(fifo:3) => => T3(fifo:1)
|
v[SCHED_QUOTA] T2(quota:2)
Which is way different from the current implementation.
Quoted 6 lines
At some point, I noticed that it would
even have introduced a bug in a previous tentative implementation usingsched_classas a discriminator between threads, to figure out whether we should call thesched_out()handler. So, the simpler, the better:
distinct classes, distinct runqueues.
I'm going to revert to piggyback mode for v3 since this is how it is supposed to work.
Signature · 1 line
Philippe.