Skip to contentSkip to conversation

smp: print only local CPU info when sched_clock goes backward

PATCH In review Rik van Riel 4 participants 6 messages Jul 15, 2024 – Jul 24, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 smp: print only local CPU info when sched_clock goes backward

About 40% of all csd_lock warnings observed in our fleet appear to be due to sched_clock() going backward in time (usually only a little bit), resulting in ts0 being larger than ts2.

When the local CPU is at fault, we should print out a message reflecting that, rather than trying to get the remote CPU's stack trace.

---
 kernel/smp.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/smp.c +8 -0
index f085ebcdf9e7..5656ef63ea82 100644
@@ -237,6 +237,14 @@ static bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, in
 	if (likely(ts_delta <= csd_lock_timeout_ns || csd_lock_timeout_ns == 0))
 		return false;
 
+	if (ts0 > ts2) {
+		/* Our own sched_clock went backward; don't blame another CPU. */
+		ts_delta = ts0 - ts2;
+		pr_alert("sched_clock on CPU %d went backward by %llu ns\n", raw_smp_processor_id(), ts_delta);
+		*ts1 = ts2;
+		return false;
+	}
+
 	firsttime = !*bug_id;
 	if (firsttime)
 		*bug_id = atomic_inc_return(&csd_bug_count);
Signature · 1 line
2.45.2
Tested-by on v1 1/1
#1 lore raw
Quoted 8 lines · Rik van Riel

About 40% of all csd_lock warnings observed in our fleet appear to be due to sched_clock() going backward in time (usually only a little bit), resulting in ts0 being larger than ts2.

When the local CPU is at fault, we should print out a message reflecting that, rather than trying to get the remote CPU's stack trace.

Quoted 26 lines
---
 kernel/smp.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/smp.c +8 -0
index f085ebcdf9e7..5656ef63ea82 100644
@@ -237,6 +237,14 @@ static bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, in
 	if (likely(ts_delta <= csd_lock_timeout_ns || csd_lock_timeout_ns == 0))
 		return false;
 
+	if (ts0 > ts2) {
+		/* Our own sched_clock went backward; don't blame another CPU. */
+		ts_delta = ts0 - ts2;
+		pr_alert("sched_clock on CPU %d went backward by %llu ns\n", raw_smp_processor_id(), ts_delta);
+		*ts1 = ts2;
+		return false;
+	}
+
 	firsttime = !*bug_id;
 	if (firsttime)
 		*bug_id = atomic_inc_return(&csd_bug_count);
Signature · 1 line
2.45.2
#2 lore raw
Quoted 5 lines · Paul E. McKenney
Quoted 4 lines · Rik van Riel

About 40% of all csd_lock warnings observed in our fleet appear to be due to sched_clock() going backward in time (usually only a little bit), resulting in ts0 being larger than ts2.

Quoted 7 lines
Quoted 4 lines

When the local CPU is at fault, we should print out a message reflecting that, rather than trying to get the remote CPU's stack trace.

I have included this patch as part of the CSD-lock diagnostics series which is submitted for review and planned for v6.12 [1]. I have also included it in RCU tree [2] for more testing.

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://git.kernel.org/pub/scm/linux/kernel/git/neeraj.upadhyay/linux-rcu.git/log/?h=next

- Neeraj

Quoted 26 lines
Quoted 26 lines
---
 kernel/smp.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/smp.c +8 -0
index f085ebcdf9e7..5656ef63ea82 100644
@@ -237,6 +237,14 @@ static bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, in
 	if (likely(ts_delta <= csd_lock_timeout_ns || csd_lock_timeout_ns == 0))
 		return false;
 
+	if (ts0 > ts2) {
+		/* Our own sched_clock went backward; don't blame another CPU. */
+		ts_delta = ts0 - ts2;
+		pr_alert("sched_clock on CPU %d went backward by %llu ns\n", raw_smp_processor_id(), ts_delta);
+		*ts1 = ts2;
+		return false;
+	}
+
 	firsttime = !*bug_id;
 	if (firsttime)
 		*bug_id = atomic_inc_return(&csd_bug_count);
Signature · 1 line
2.45.2
#3 lore raw
In reply to Rik van Riel
Quoted 17 lines · Rik van Riel

About 40% of all csd_lock warnings observed in our fleet appear to be due to sched_clock() going backward in time (usually only a little bit), resulting in ts0 being larger than ts2.

When the local CPU is at fault, we should print out a message reflecting that, rather than trying to get the remote CPU's stack trace.

---
 kernel/smp.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/smp.c +0 -0
index f085ebcdf9e7..5656ef63ea82 100644

kernel/smp.c static bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, in · line 246

Quoted 10 lines
kernel/smp.c +7 -0
@@ -237,6 +237,14 @@ static bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, in
 	if (likely(ts_delta <= csd_lock_timeout_ns || csd_lock_timeout_ns == 0))
 		return false;
 
+	if (ts0 > ts2) {
+		/* Our own sched_clock went backward; don't blame another CPU. */
+		ts_delta = ts0 - ts2;
+		pr_alert("sched_clock on CPU %d went backward by %llu ns\n", raw_smp_processor_id(), ts_delta);
+		*ts1 = ts2;
+		return false;
+	}

So I've seen some chatter about this on IRC and was WTF, seeing this patch I'm still WTF. What is going on with those machines?!?!

#4 lore raw
Quoted 30 lines · Peter Zijlstra
Quoted 29 lines · Rik van Riel

About 40% of all csd_lock warnings observed in our fleet appear to be due to sched_clock() going backward in time (usually only a little
bit), resulting in ts0 being larger than ts2.

When the local CPU is at fault, we should print out a message reflecting
that, rather than trying to get the remote CPU's stack trace.

---
 kernel/smp.c | 8 ++++++++

 1 file changed, 8 insertions(+)
kernel/smp.c +0 -0
index f085ebcdf9e7..5656ef63ea82 100644
@@ -237,6 +237,14 @@ static bool
 csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1,

in
  if (likely(ts_delta <= csd_lock_timeout_ns || csd_lock_timeout_ns == 0))
  return false;

kernel/smp.c +2 -0
+	if (ts0 > ts2) {
+		/* Our own sched_clock went backward; don't blame

another CPU. */

kernel/smp.c +1 -0
+		ts_delta = ts0 - ts2;
Quoted 9 lines

kernel/smp.c static bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, in · line 246

Quoted 5 lines
kernel/smp.c +1 -0
+		pr_alert("sched_clock on CPU %d went backward by

%llu ns\n", raw_smp_processor_id(), ts_delta);

kernel/smp.c +3 -0
+		*ts1 = ts2;
+		return false;
+	}

So I've seen some chatter about this on IRC and was WTF, seeing this patch I'm still WTF. What is going on with those machines?!?!

Unfortunately this particular behavior does not appear to be limited to a few bad systems. The distribution over time looks as follows:

- 1 day: N affected systems
- 3 days: 3N affected systems
- 1 week: 6N affected systems
- 2 weeks: 14N affected systems
- 30 days: 29N affected systems
- 90 days: 72N affected systems

In other words, we are not looking at a few "repeat customers", but at a large number of systems that show the backward TSC behavior a few times, and then not again for months.

N is a triple digit number, so I'm fairly confident in these numbers.

If it's any consolation this seems to happen on both Intel and AMD systems.

The only detection code we have currently is in csd_lock_wait_toolong, so there could be a lot of cases of the TSC moving backward that are falling through the cracks.

Most of the time the TSC only moves backward a few dozen, or a few hundred nanoseconds, and backward time past a few microseconds is a very long tail. I have not sliced this data by CPU model, and don't know if the distribution is different for different CPUs.

The code in question simply loads one sched_clock value into ts0, and another into ts2:

static void __csd_lock_wait(call_single_data_t *csd)

{
        int bug_id = 0;
        u64 ts0, ts1;

        ts1 = ts0 = sched_clock();
        for (;;) {
                if (csd_lock_wait_toolong(csd, ts0, &ts1, &bug_id))
                        break;
                cpu_relax();
        }

...

static bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, int *bug_id)
{
...

        ts2 = sched_clock();

I don't know if it matters for the CPU that csd_lock_wait_toolong gets inlined into __csd_lock_wait.

On one particular system replacing the rdtsc() in native_sched_clock() with rdtsc_ordered() did not help, but that system sees frequent negative time jumps of 2.5ms, and might just be broken.

The MTBF is high enough that I'm not sure whether always using rdtsc_ordered() would be warranted, and I don't know whether it would even help...

Signature · 1 line
All Rights Reversed.
#5 lore raw
In reply to Peter Zijlstra
Quoted 21 lines · Peter Zijlstra
Quoted 20 lines · Rik van Riel

About 40% of all csd_lock warnings observed in our fleet appear to be due to sched_clock() going backward in time (usually only a little bit), resulting in ts0 being larger than ts2.

When the local CPU is at fault, we should print out a message reflecting that, rather than trying to get the remote CPU's stack trace.

---
 kernel/smp.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/smp.c +0 -0
index f085ebcdf9e7..5656ef63ea82 100644
@@ -237,6 +237,14 @@ static bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, in
 	if (likely(ts_delta <= csd_lock_timeout_ns || csd_lock_timeout_ns == 0))
 		return false;
 
Quoted 10 lines

kernel/smp.c static bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, in · line 246

Quoted 7 lines
kernel/smp.c +7 -0
+	if (ts0 > ts2) {
+		/* Our own sched_clock went backward; don't blame another CPU. */
+		ts_delta = ts0 - ts2;
+		pr_alert("sched_clock on CPU %d went backward by %llu ns\n", raw_smp_processor_id(), ts_delta);
+		*ts1 = ts2;
+		return false;
+	}

So I've seen some chatter about this on IRC and was WTF, seeing this patch I'm still WTF. What is going on with those machines?!?!

I wish I knew, but for the record, I completely agree that both your acronym and interrobang apply to this case. ;-)

Thanx, Paul