sched/psi: clamp negative cpu_clock() skew
Subsystems (3)
PATCH 1/1 sched/psi: clamp negative cpu_clock() skew
When working with timestamps, psi is careful to only compare timestamps from cpu_clock(cpu) with the same cpu argument. However, on systems with a stable clock, that cpu argument is ignored. This means that despite psi's best efforts, there will be some cross-CPU cpu_clock() timestamp comparisons.
The cross-CPU skew in cpu_clock() timestamps is generally orders of magnitude smaller than psi measurement intervals, so the cyclic times counters naturally absorb the jitter. However, if get_recent_times() executes immediately after a task on another CPU enters an active stall state, then negative skew can cause the u32 active state duration calculation to underflow. If that state had not been active since the previous execution of get_recent_times(), then times and times_prev will be equal and the very large underflow value will be passed out to collect_percpu_times(). There, it will be zero extended and folded into the u64 total accumulator as a very large delta of about 4 seconds scaled by that CPU's share of nonidle time.
That large jump can lead to spurious wakeups for the PSI_POLL aggregator or incorrect values for the PSI_AVGS aggregator. Spurious triggers or nonsense psi values (e.g. full>some, psi values >100%) can cause userspace to take unnecessary corrective action, such as a userspace OOM daemon killing processes to relieve (non-existent) memory pressure.
When running browser workloads on an Intel N100 with a sustained psi.mem.some of 2-3%, this underflow is observed once every couple of hours.
Clamping elapsed time to >= 0 prevents this jump from happening. It does result in the total accumulator being slightly elevated compared to what it should actually be, but that error is bounded by the skew and is in practice always smaller than what is added today. Note that expanding times to u64 to prevent the u32->u64 conversion from causing issues would result in userspace-facing total counters no longer being monotonic, which could break consumers that derive rates via successive total values.
Clock skew can cause issues in two more places. First, negative clock skew in record_times() can propagate through the times accumulator to the delta calculation against times_prev in get_recent_times() and cause it to underflow. Second, the elapsed time values calculated by successive executions of get_recent_times() can appear to go backwards if the first execution has positive skew and the second has negative skew. This can cause the delta calculation to underflow, since elapsed is propagated in the times_prev value. However, both of these require that the growth in stall time between iterations be non-zero but smaller than the inter-CPU skew. Neither issue has been observed in practice and they are not addressed here.
--- kernel/sched/psi.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
kernel/sched/psi.c +18 -2
@@ -305,8 +305,24 @@ static void get_recent_times(struct psi_group *group, int cpu,* (u32) and our reported pressure close to what's * actually happening. */ -if (state_mask & (1 << s)) - times[s] += now - state_start; +if (state_mask & (1 << s)) { +s64 elapsed = now - state_start; + +/* +* When sched_clock_stable(), cpu_clock(cpu) ignores +* the cpu argument, so we can end up doing cross-CPU +* comparisons. A negative clock skew can result in +* underflow to a very large u32. +* +* While the u32 cyclic accumulators in groupc could +* handle a very large u32 from underflow, folding it +* into the u64 totals in collect_percpu_times() +* would result in huge jumps. Clamp to avoid that. +*/ +if (elapsed <0 ) + elapsed =0 ; + times[s] += elapsed; + } delta = times[s] - groupc->times_prev[aggregator][s]; groupc->times_prev[aggregator][s] = times[s];
base-commit: 1fb28c664a19df8d45a6afa04d28d102b04ea680
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Peter Zijlstra
maintainerQuoted 70 lines · David Stevens
When working with timestamps, psi is careful to only compare timestamps from
cpu_clock(cpu)with the same cpu argument. However, on systems with a stable clock, that cpu argument is ignored. This means that despite psi's best efforts, there will be some cross-CPUcpu_clock()timestamp comparisons.The cross-CPU skew in
cpu_clock()timestamps is generally orders of magnitude smaller than psi measurement intervals, so the cyclic times counters naturally absorb the jitter. However, ifget_recent_times()executes immediately after a task on another CPU enters an active stall state, then negative skew can cause the u32 active state duration calculation to underflow. If that state had not been active since the previous execution ofget_recent_times(), then times andtimes_prevwill be equal and the very large underflow value will be passed out tocollect_percpu_times(). There, it will be zero extended and folded into the u64 total accumulator as a very large delta of about 4 seconds scaled by that CPU's share of nonidle time.That large jump can lead to spurious wakeups for the
PSI_POLLaggregator or incorrect values for thePSI_AVGSaggregator. Spurious triggers or nonsense psi values (e.g. full>some, psi values >100%) can cause userspace to take unnecessary corrective action, such as a userspace OOM daemon killing processes to relieve (non-existent) memory pressure.When running browser workloads on an Intel N100 with a sustained psi.mem.some of 2-3%, this underflow is observed once every couple of hours.
Clamping elapsed time to >= 0 prevents this jump from happening. It does result in the total accumulator being slightly elevated compared to what it should actually be, but that error is bounded by the skew and is in practice always smaller than what is added today. Note that expanding times to u64 to prevent the
u32->u64conversion from causing issues would result in userspace-facing total counters no longer being monotonic, which could break consumers that derive rates via successive total values.Clock skew can cause issues in two more places. First, negative clock skew in
record_times()can propagate through the times accumulator to the delta calculation againsttimes_previnget_recent_times()and cause it to underflow. Second, the elapsed time values calculated by successive executions ofget_recent_times()can appear to go backwards if the first execution has positive skew and the second has negative skew. This can cause the delta calculation to underflow, since elapsed is propagated in thetimes_prevvalue. However, both of these require that the growth in stall time between iterations be non-zero but smaller than the inter-CPU skew. Neither issue has been observed in practice and they are not addressed here.Fixes eb414681d5a0 ("psi: pressure stall information for CPU, memory, and IO")Signed-off-by David Stevens <[email protected]>--- kernel/sched/psi.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)kernel/sched/psi.c +4
-2@@ -305,8 +305,24 @@ static void get_recent_times(struct psi_group *group, int cpu,* (u32) and our reported pressure close to what's * actually happening. */ -if (state_mask & (1 << s)) - times[s] += now - state_start; +if (state_mask & (1 << s)) { +s64 elapsed = now - state_start; + +/*
kernel/sched/psi.c static void get_recent_times(struct psi_group *group, int cpu, · line 321
Quoted 10 lines
kernel/sched/psi.c +10
-0+* When sched_clock_stable(), cpu_clock(cpu) ignores +* the cpu argument, so we can end up doing cross-CPU +* comparisons. A negative clock skew can result in +* underflow to a very large u32. +* +* While the u32 cyclic accumulators in groupc could +* handle a very large u32 from underflow, folding it +* into the u64 totals in collect_percpu_times() +* would result in huge jumps. Clamp to avoid that. +*/
I am confused. If sched_clock_stable() you are 1) running x86 and 2) that promises CPU A and CPU B doing RDTSC cannot observe non monotonic movement.
If you are somehow able to see non monotonic movement, then 1) your hardware is fucked, and 2) you should not have sched_clock_stable().
What kind of machine are you seeing this on?
Quoted 12 lines
kernel/sched/psi.c +4
-0+if (elapsed <0 ) + elapsed =0 ; + times[s] += elapsed; + } delta = times[s] - groupc->times_prev[aggregator][s]; groupc->times_prev[aggregator][s] = times[s];base-commit:
1fb28c664a19df8d45a6afa04d28d102b04ea680Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Peter Zijlstra
maintainerQuoted 3 lines · Peter Zijlstra
Quoted 3 lines
When running browser workloads on an Intel N100 with a sustained psi.mem.some of 2-3%, this underflow is observed once every couple of hours.
Reading is hard. This is sad, that's a relatively modern chip :-(
Johannes Weiner
maintainerQuoted 7 lines · Peter Zijlstra
Quoted 3 lines · Peter Zijlstra
Quoted 3 lines
When running browser workloads on an Intel N100 with a sustained psi.mem.some of 2-3%, this underflow is observed once every couple of hours.
Reading is hard. This is sad, that's a relatively modern chip :-(
I'm still struggling, but I'm under the weather so bear with me.
That's a cpu_clock() implementation issue, right? "The value for cpu_clock(i) is monotonic for constant i". Presumably violating that would be a problem beyond psi?
Peter Zijlstra
maintainerQuoted 7 lines · Johannes Weiner
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines · Peter Zijlstra
Quoted 3 lines
When running browser workloads on an Intel N100 with a sustained psi.mem.some of 2-3%, this underflow is observed once every couple of hours.
Quoted 7 lines
Quoted 1 line
Reading is hard. This is sad, that's a relatively modern chip :-(
I'm still struggling, but I'm under the weather so bear with me.
That's a
cpu_clock()implementation issue, right? "The value forcpu_clock(i)is monotonic for constant i". Presumably violating that would be a problem beyond psi?
Yeah, although most everybody has a filter on top for some reason or another.
Mostly it could happen that when the watchdog would detect the TSC wasn't stable after all, you'd be up a creek if you didn't also have this filter.
But I'm very sad to see modern chips have shit TSC, that wasn't supposed to happen.
Quoted 20 lines · Peter Zijlstra
Quoted 7 lines · Johannes Weiner
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines · Peter Zijlstra
Quoted 3 lines
When running browser workloads on an Intel N100 with a sustained psi.mem.some of 2-3%, this underflow is observed once every couple of hours.
Quoted 7 lines
Quoted 1 line
Reading is hard. This is sad, that's a relatively modern chip :-(
I'm still struggling, but I'm under the weather so bear with me.
That's a
cpu_clock()implementation issue, right? "The value forcpu_clock(i)is monotonic for constant i". Presumably violating that would be a problem beyond psi?Yeah, although most everybody has a filter on top for some reason or another.
Quoted 6 lines
Mostly it could happen that when the watchdog would detect the TSC wasn't stable after all, you'd be up a creek if you didn't also have this filter.
But I'm very sad to see modern chips have shit TSC, that wasn't supposed to happen.
I ran artificial memory pressure tests on the other two devices I have at hand - one with an Intel i5-1245U and one with an Intel Ultra 5
325. Both devices also observed negative elapsed durations. The artificial workloads were much heavier and actually produced negative elapsed durations every couple of minutes. So this doesn't look like a platform-specific issue.
For reference, the negative elapsed durations I saw averaged a little under 100ns. The largest duration I saw was 371ns.
If I'm following the code correctly, cpu_clock() on x86 ends up calling rdtsc() via sched_clock(). The rdtsc() function is documented to produce non-monotonic results when compared across CPUs, since the rdtsc instruction can read the tsc out of order relative to other instructions. If that results in get_recent_times() reading the tsc before it reads the psi state, then I think that would explain the negative skew. I added a rbm() immediately before the call to cpu_clock() in get_recent_times() to match the behavior of rdtsc_ordered(), and with that I didn't see any negative elapsed durations during overnight testing. This does suggest that calling through to sched_clock() might not provide strong enough guarantees on x86 to satisfy the cpu_clock() contract.
-David