[Devel] [PATCH hci-8.1 3/4] sched: account sleep and block time of a task group entity

Vasileios Almpanis vasileios.almpanis at virtuozzo.com
Wed Oct 14 14:16:26 MSK 2026


CT idle and iowait in cpu.proc.stat come from the schedstats of the
group entity: cpu_cgroup_update_stat() reads iowait_sum and
sum_sleep_runtime. But only tasks get sleep_start/block_start, so for
a group both stay 0 and the CT shows 0 idle and 0 iowait.
fixup_vcpustat_delta() then gives all idle time of the CT's /proc/stat
to IDLE, so iowait there is always 0 too.

vz7 dequeue_sleeper() had a branch for group entities that was lost
when porting to newer kernels. Restore it in update_stats_dequeue_fair():
when a group entity is dequeued on sleep, start a block period if its
cfs_rq has iowait sleepers and a sleep period otherwise. The existing
enqueue path closes it.

Like on vz7, skip throttled groups, they are not idle, just waiting
for bandwidth. If a group has nothing to run when it is unthrottled,
tg_unthrottle_up() doesn't enqueue it, so start its period there.

https://virtuozzo.atlassian.net/browse/VSTOR-147823
Fixes: 095994c57b84 ("ve/sched/stat: Introduce functions to calculate vcpustat data")
Signed-off-by: Vasileios Almpanis <vasileios.almpanis at virtuozzo.com>

Feature: sched: emulate virtual cpus for Containers
---
 kernel/sched/fair.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index df296edb828d..2df5a9b47480 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1332,6 +1332,21 @@ update_stats_enqueue_fair(struct cfs_rq *cfs_rq, struct sched_entity *se, int fl
 		update_stats_enqueue_sleeper_fair(cfs_rq, se);
 }
 
+static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
+
+/* The group goes idle, blocked if it has iowait sleepers */
+static inline void
+update_stats_group_sleep_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
+{
+	struct sched_statistics *stats;
+
+	stats = __schedstats_from_se(se);
+	if (group_cfs_rq(se)->nr_iowait)
+		__schedstat_set(stats->block_start, rq_clock(rq_of(cfs_rq)));
+	else
+		__schedstat_set(stats->sleep_start, rq_clock(rq_of(cfs_rq)));
+}
+
 static inline void
 update_stats_dequeue_fair(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 {
@@ -1358,6 +1373,9 @@ update_stats_dequeue_fair(struct cfs_rq *cfs_rq, struct sched_entity *se, int fl
 		if (state & TASK_UNINTERRUPTIBLE)
 			__schedstat_set(tsk->stats.block_start,
 				      rq_clock(rq_of(cfs_rq)));
+	} else if ((flags & DEQUEUE_SLEEP) &&
+		   !throttled_hierarchy(group_cfs_rq(se))) {
+		update_stats_group_sleep_start(cfs_rq, se);
 	}
 }
 
@@ -3825,8 +3843,6 @@ static void reweight_task_fair(struct rq *rq, struct task_struct *p,
 	load->inv_weight = lw->inv_weight;
 }
 
-static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
-
 #ifdef CONFIG_FAIR_GROUP_SCHED
 /*
  * All this does is approximate the hierarchical proportion which includes that
@@ -5904,6 +5920,7 @@ static int tg_unthrottle_up(struct task_group *tg, void *data)
 {
 	struct rq *rq = data;
 	struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
+	struct sched_entity *se = tg->se[cpu_of(rq)];
 	struct task_struct *p, *tmp;
 
 	if (--cfs_rq->throttle_count)
@@ -5933,6 +5950,15 @@ static int tg_unthrottle_up(struct task_group *tg, void *data)
 		enqueue_task_fair(rq_of(cfs_rq), p, ENQUEUE_WAKEUP);
 	}
 
+	/*
+	 * The group may have gone idle while throttled, when no sleep or
+	 * block period is started for it. Start it now if nothing runs.
+	 */
+	if (schedstat_enabled() && se && !se->on_rq && !cfs_rq->nr_queued &&
+	    !schedstat_val(__schedstats_from_se(se)->sleep_start) &&
+	    !schedstat_val(__schedstats_from_se(se)->block_start))
+		update_stats_group_sleep_start(cfs_rq_of(se), se);
+
 	/* Add cfs_rq with load or one or more already running entities to the list */
 	if (!cfs_rq_is_decayed(cfs_rq))
 		list_add_leaf_cfs_rq(cfs_rq);

-- 
2.43.0



More information about the Devel mailing list