Skip to contentSkip to conversation

sched/core: initialize resched_latency in sched_tick()

PATCH In review Joseph Salisbury 4 participants 5 messages Mar 17 – Mar 18 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/core: initialize resched_latency in sched_tick()

sched_tick() only assigns resched_latency when the LATENCY_WARN feature is enabled, but tests the variable later in a separate feature check.

That leaves the code dependent on both checks observing identical state. Initializing resched_latency to zero makes the later test safe even if the feature state changes between the two checks.

This does not change behavior when LATENCY_WARN is enabled. It only avoids using an uninitialized local value.

Fixes c006fac556e4 ("sched: Warn on long periods of pending need_resched")
Assisted-by Codex:GPT-5
---
 kernel/sched/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/core.c +1 -1
index 08a06162dd9a..15916186e4ff 100644
@@ -5548,7 +5548,7 @@ void sched_tick(void)
 	struct task_struct *donor;
 	struct rq_flags rf;
 	unsigned long hw_pressure;
-	u64 resched_latency;
+	u64 resched_latency = 0;
 
 	if (housekeeping_cpu(cpu, HK_TYPE_KERNEL_NOISE))
 		arch_scale_freq_tick();
Signature · 1 line
2.47.3
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 23 lines · On Tue, 17 Mar 2026 15:58:07 -0400 Joseph Salisbury…

sched_tick() only assigns resched_latency when the LATENCY_WARN feature is enabled, but tests the variable later in a separate feature check.

That leaves the code dependent on both checks observing identical state. Initializing resched_latency to zero makes the later test safe even if the feature state changes between the two checks.

This does not change behavior when LATENCY_WARN is enabled. It only avoids using an uninitialized local value.

Fixes c006fac556e4 ("sched: Warn on long periods of pending need_resched")
Assisted-by Codex:GPT-5
---
 kernel/sched/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/core.c +0 -0
index 08a06162dd9a..15916186e4ff 100644

kernel/sched/core.c void sched_tick(void) · line 5554

Quoted 8 lines
kernel/sched/core.c +1 -1
@@ -5548,7 +5548,7 @@ void sched_tick(void)
 	struct task_struct *donor;
 	struct rq_flags rf;
 	unsigned long hw_pressure;
-	u64 resched_latency;
+	u64 resched_latency = 0;
 
 	if (housekeeping_cpu(cpu, HK_TYPE_KERNEL_NOISE))
 		arch_scale_freq_tick();

The code has:

	if (sched_feat(LATENCY_WARN))
		resched_latency = cpu_resched_latency(rq);
	calc_global_load_tick(rq);
	sched_core_tick(rq);
	scx_tick(rq);

	rq_unlock(rq, &rf);

	if (sched_feat(LATENCY_WARN) && resched_latency)
		resched_latency_warn(cpu, resched_latency);

I guess if LATENCY_WARN gets enabled between the first check and the second, then the resched_latency could be uninitialized.

Heck, probably could even get rid of the second sched_feat(LATENCY_WARN), and make it simply:

if (resched_latency)

-- Steve

#2 lore raw

Hi.

Quoted 18 lines

The code has:

	if (sched_feat(LATENCY_WARN))
		resched_latency = cpu_resched_latency(rq);
	calc_global_load_tick(rq);
	sched_core_tick(rq);
	scx_tick(rq);

	rq_unlock(rq, &rf);

	if (sched_feat(LATENCY_WARN) && resched_latency)
		resched_latency_warn(cpu, resched_latency);

I guess if LATENCY_WARN gets enabled between the first check and the second, then the resched_latency could be uninitialized.

Quoted 8 lines

Heck, probably could even get rid of the second sched_feat(LATENCY_WARN), and make it simply:

if (resched_latency)

-- Steve

Whats the relevance of resched_latency warnings in the age of preempt kernels being the default for majority of the archs.

In case of full/lazy preemption, at the end of sched_tick itself we would call schedule if need_resched is set provided it was preemptible.

It kind of makes sense for non preemptive kernels. But we have preemptoff and preemptirqoff tracers which one could use. (although may need a kernel build)

What bugs is it catching these days?

#3 lore raw
Quoted 10 lines · On Wed, 18 Mar 2026 12:00:58 +0530 Shrikanth Hegde <[email protected]>…

Whats the relevance of resched_latency warnings in the age of preempt kernels being the default for majority of the archs.

In case of full/lazy preemption, at the end of sched_tick itself we would call schedule if need_resched is set provided it was preemptible.

It kind of makes sense for non preemptive kernels. But we have preemptoff and preemptirqoff tracers which one could use. (although may need a kernel build)

What bugs is it catching these days?

Good point. I've never used it. I guess the question is, does anyone?

-- Steve

maintainer
#4 lore raw
Quoted 9 lines · Steven Rostedt
Quoted 6 lines · On Wed, 18 Mar 2026 12:00:58 +0530 Shrikanth Hegde <[email protected]>…

Whats the relevance of resched_latency warnings in the age of preempt kernels being the default for majority of the archs.

In case of full/lazy preemption, at the end of sched_tick itself we would call schedule if need_resched is set provided it was preemptible.

Quoted 6 lines
Quoted 4 lines

It kind of makes sense for non preemptive kernels. But we have preemptoff and preemptirqoff tracers which one could use. (although may need a kernel build)

What bugs is it catching these days?

Good point. I've never used it. I guess the question is, does anyone?

Google added that, Ben are you guys still needing that?