mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] sched/fair: factor out common sched_entity stats/task lookup
@ 2025-12-17  7:00 Zhan Xusheng
  2025-12-18  8:14 ` Peter Zijlstra
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Zhan Xusheng @ 2025-12-17  7:00 UTC (permalink / raw)
  To: peterz
  Cc: vincent.guittot, dietmar.eggemann, linux-sched, linux-kernel,
	Zhan Xusheng

The fair scheduler has several update_stats_*_fair() helpers which
open-code the same boilerplate to retrieve sched_statistics and the
associated task (if any) from a sched_entity.

Factor this common logic into a small static inline helper to reduce
duplication and improve readability, without changing behaviour or
control flow.

No functional change intended.

Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
---
 kernel/sched/fair.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index da46c3164537..b4a9319a5753 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1243,6 +1243,15 @@ static void update_curr_fair(struct rq *rq)
 	update_curr(cfs_rq_of(&rq->donor->se));
 }
 
+static inline void
+get_se_stats_and_task(struct sched_entity *se,
+		struct sched_statistics **stats,
+		struct task_struct **p)
+{
+	*stats = __schedstats_from_se(se);
+	*p = entity_is_task(se) ? task_of(se) : NULL;
+}
+
 static inline void
 update_stats_wait_start_fair(struct cfs_rq *cfs_rq, struct sched_entity *se)
 {
@@ -1252,10 +1261,7 @@ update_stats_wait_start_fair(struct cfs_rq *cfs_rq, struct sched_entity *se)
 	if (!schedstat_enabled())
 		return;
 
-	stats = __schedstats_from_se(se);
-
-	if (entity_is_task(se))
-		p = task_of(se);
+	get_se_stats_and_task(se, &stats, &p);
 
 	__update_stats_wait_start(rq_of(cfs_rq), p, stats);
 }
@@ -1269,7 +1275,7 @@ update_stats_wait_end_fair(struct cfs_rq *cfs_rq, struct sched_entity *se)
 	if (!schedstat_enabled())
 		return;
 
-	stats = __schedstats_from_se(se);
+	get_se_stats_and_task(se, &stats, &p);
 
 	/*
 	 * When the sched_schedstat changes from 0 to 1, some sched se
@@ -1280,9 +1286,6 @@ update_stats_wait_end_fair(struct cfs_rq *cfs_rq, struct sched_entity *se)
 	if (unlikely(!schedstat_val(stats->wait_start)))
 		return;
 
-	if (entity_is_task(se))
-		p = task_of(se);
-
 	__update_stats_wait_end(rq_of(cfs_rq), p, stats);
 }
 
@@ -1295,10 +1298,7 @@ update_stats_enqueue_sleeper_fair(struct cfs_rq *cfs_rq, struct sched_entity *se
 	if (!schedstat_enabled())
 		return;
 
-	stats = __schedstats_from_se(se);
-
-	if (entity_is_task(se))
-		tsk = task_of(se);
+	get_se_stats_and_task(se, &stats, &p);
 
 	__update_stats_enqueue_sleeper(rq_of(cfs_rq), tsk, stats);
 }
-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: [PATCH] sched/fair: factor out common sched_entity stats/task lookup
@ 2025-12-17 18:50 Shubhang Kaushik Prasanna Kumar
  0 siblings, 0 replies; 8+ messages in thread
From: Shubhang Kaushik Prasanna Kumar @ 2025-12-17 18:50 UTC (permalink / raw)
  To: zhanxusheng1024
  Cc: dietmar.eggemann, linux-kernel, linux-sched, peterz,
	vincent.guittot, zhanxusheng

Hi Zhan,

The cleanup looks nice, but these helpers are sitting in some very critical hot paths. `update_stats_wait_end_fair` and `update_stats_enqueue_sleeper_fair` are hit on practically every task wakeup and context switch. 

Even though it's not a functional refactoring, have you verified it with any benchmarks? I'm concerned that introducing the helper and passing around addresses of local variables (&stats, &p) might make it harder for the compiler to optimize compared to the original version.

I'm actually looking at a similar improvement in the PELT area, so I'd be curious to know if you've seen any measurable change in overhead.. To help the compiler, it might be better to have the helper return the stats pointer directly rather than passing &stats. This avoids the "double indirection" and makes it easier for the compiler to keep everything in registers. Also, for these hot paths, using `__always_inline` would be safer than just `inline` to avoid possible overhead.

Thanks,
Shubhang Kaushik

^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: [PATCH] sched/fair: factor out common sched_entity stats/task lookup
@ 2025-12-17 18:50 Shubhang Kaushik Prasanna Kumar
  0 siblings, 0 replies; 8+ messages in thread
From: Shubhang Kaushik Prasanna Kumar @ 2025-12-17 18:50 UTC (permalink / raw)
  To: zhanxusheng1024
  Cc: dietmar.eggemann, linux-kernel, linux-sched, peterz,
	vincent.guittot, zhanxusheng

Hi Zhan,

The cleanup looks nice, but these helpers are sitting in some very critical hot paths. `update_stats_wait_end_fair` and `update_stats_enqueue_sleeper_fair` are hit on practically every task wakeup and context switch. 

Even though it's not a functional refactoring, have you verified it with any benchmarks? I'm concerned that introducing the helper and passing around addresses of local variables (&stats, &p) might make it harder for the compiler to optimize compared to the original version.

I'm actually looking at a similar improvement in the PELT area, so I'd be curious to know if you've seen any measurable change in overhead.. To help the compiler, it might be better to have the helper return the stats pointer directly rather than passing &stats. This avoids the "double indirection" and makes it easier for the compiler to keep everything in registers. Also, for these hot paths, using `__always_inline` would be safer than just `inline` to avoid possible overhead.

Thanks,
Shubhang Kaushik

^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: [PATCH] sched/fair: factor out common sched_entity stats/task lookup
@ 2025-12-17 19:22 Shubhang Kaushik Prasanna Kumar
  2025-12-17 19:22 ` Shubhang Kaushik Prasanna Kumar
  0 siblings, 1 reply; 8+ messages in thread
From: Shubhang Kaushik Prasanna Kumar @ 2025-12-17 19:22 UTC (permalink / raw)
  To: zhanxusheng1024
  Cc: dietmar.eggemann, linux-kernel, linux-sched, peterz,
	vincent.guittot, zhanxusheng

Hi Zhan,

The cleanup looks nice, but these helpers are sitting in some very critical hot paths. `update_stats_wait_end_fair` and `update_stats_enqueue_sleeper_fair` are hit on practically every task wakeup and context switch. 

Even though it's not a functional refactoring, have you verified it with any benchmarks? I'm concerned that introducing the helper and passing around addresses of local variables (&stats, &p) might make it harder for the compiler to optimize compared to the original version.

I'm actually looking at a similar improvement in the PELT area, so I'd be curious to know if you've seen any measurable change in overhead.. To help the compiler, it might be better to have the helper return the stats pointer directly rather than passing &stats. This avoids the double indirection and makes it easier for the compiler to keep everything in registers. Also, given how hot these paths are, using `__always_inline` would be safer than just `inline` to reduce function call overhead.

Thanks,
Shubhang Kaushik

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-12-21 20:55 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-17  7:00 [PATCH] sched/fair: factor out common sched_entity stats/task lookup Zhan Xusheng
2025-12-18  8:14 ` Peter Zijlstra
2025-12-21 18:52 ` kernel test robot
2025-12-21 20:54 ` kernel test robot
2025-12-17 18:50 Shubhang Kaushik Prasanna Kumar
2025-12-17 18:50 Shubhang Kaushik Prasanna Kumar
2025-12-17 19:22 Shubhang Kaushik Prasanna Kumar
2025-12-17 19:22 ` Shubhang Kaushik Prasanna Kumar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®