Skip to contentSkip to conversation

tracing: Fix context switch counter truncation

PATCH In review Usama Arif 3 participants 3 messages Jul 17 – Jul 20

Subsystems (2)
lore
Share & tools
lore

Usama Arif

author
#0 lore raw

PATCH 1/1 tracing: Fix context switch counter truncation

trace_user_fault_read() samples nr_context_switches_cpu() before enabling preemption and retries the user copy if the counter changes. The helper returns unsigned long long because rq->nr_switches is u64, but the saved value is unsigned int.

Once a CPU has performed 2^32 context switches, assigning the counter to cnt discards its upper bits. The comparison after the copy promotes cnt back to unsigned long long, but the lost bits remain zero, so it reports a change even when the task was never scheduled out. Every retry then fails the same way until the 100-try guard warns and the user copy is abandoned.

This affects long-running systems and workloads with high context-switch rates. A CPU switching 1,000 times per second takes about 50 days.

Store the sampled count in unsigned long long so the full value is preserved.

Fixes 64cf7d058a00 ("tracing: Have trace_marker use per-cpu data to read user space")
---
 kernel/trace/trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace.c +1 -1
index 1146b83b711a..412a8daf2162 100644
@@ -6188,7 +6188,7 @@ char *trace_user_fault_read(struct trace_user_buf_info *tinfo,
 {
 	int cpu = smp_processor_id();
 	char *buffer = per_cpu_ptr(tinfo->tbuf, cpu)->buf;
-	unsigned int cnt;
+	unsigned long long cnt;
 	int trys = 0;
 	int ret;
 
Signature · 1 line
2.53.0-Meta
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · On Fri, 17 Jul 2026 10:32:52 -0700 Usama Arif <[email protected]> wrote:

trace_user_fault_read() samples nr_context_switches_cpu() before enabling preemption and retries the user copy if the counter changes. The helper returns unsigned long long because rq->nr_switches is u64, but the saved value is unsigned int.

Once a CPU has performed 2^32 context switches, assigning the counter to cnt discards its upper bits. The comparison after the copy promotes cnt back to unsigned long long, but the lost bits remain zero, so it reports a change even when the task was never scheduled out. Every retry then fails the same way until the 100-try guard warns and the user copy is abandoned.

Quoted 5 lines

This affects long-running systems and workloads with high context-switch rates. A CPU switching 1,000 times per second takes about 50 days.

Store the sampled count in unsigned long long so the full value is preserved.

Looks good to me.

Thanks!

Quoted 25 lines
Fixes 64cf7d058a00 ("tracing: Have trace_marker use per-cpu data to read user space")
---
 kernel/trace/trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace.c +1 -1
index 1146b83b711a..412a8daf2162 100644
@@ -6188,7 +6188,7 @@ char *trace_user_fault_read(struct trace_user_buf_info *tinfo,
 {
 	int cpu = smp_processor_id();
 	char *buffer = per_cpu_ptr(tinfo->tbuf, cpu)->buf;
-	unsigned int cnt;
+	unsigned long long cnt;
 	int trys = 0;
 	int ret;
 
Signature · 1 line
2.53.0-Meta
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Reviewed-by on v1 1/1
#2 lore raw
In reply to Usama Arif
Quoted 2 lines · Usama Arif

This affects long-running systems and workloads with high context-switch rates. A CPU switching 1,000 times per second takes about 50 days.

nr_context_switches_cpu() returns "unsigned long long"
and rq->nr_switches is u64, so the unsigned int cnt truncation is real

Quoted 4 lines
Fixes 64cf7d058a00 ("tracing: Have trace_marker use per-cpu data to read user space")

Thanks for the investigation and fix,
--breno