Skip to contentSkip to conversation

locking/csd_lock: fix csd_lock_wait_toolong() error warning

PATCH In review Zhu Qiyu 2 participants 4 messages Aug 23, 2024 – Sep 6, 2024

lore
Share & tools
lore

Zhu Qiyu

author
#0 lore raw

PATCH 1/1 locking/csd_lock: fix csd_lock_wait_toolong() error warning

From: "Zhu Qiyu" <[email protected]>

In AC cycle stress tests, it was found that the csd_lock_wait_toolong() did not reach the 5s threshold, but printed the warning message, the probability of occurrence is about 0.58%-0.87%.

This is due to the out-of-order execution of the CPU, which causes ts2 to be used before it is initialized. The original value of ts2 on the stack is random, which may cause ts_delta = ts2 - *ts1 to be greater than 5s, thus triggering the printing.

The solution is to add a memory barrier after reading the ts2, which can effectively avoid this issue.

---
 kernel/smp.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/smp.c +2 -0
index f25e20617b7e..e58678d424a4 100644
@@ -247,6 +247,8 @@ static bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, in
 	}
 
 	ts2 = sched_clock();
+	/* Avoid ts2 being used before it is initialized */
+	mb();
 	/* How long since we last checked for a stuck CSD lock.*/
 	ts_delta = ts2 - *ts1;
 	if (likely(ts_delta <= csd_lock_timeout_ns * (*nmessages + 1) *
Signature · 1 line
2.34.1
#1 lore raw
Quoted 11 lines · Zhu Qiyu

From: "Zhu Qiyu" <[email protected]>

In AC cycle stress tests, it was found that the csd_lock_wait_toolong() did not reach the 5s threshold, but printed the warning message, the probability of occurrence is about 0.58%-0.87%.

This is due to the out-of-order execution of the CPU, which causes ts2 to be used before it is initialized. The original value of ts2 on the stack is random, which may cause ts_delta = ts2 - *ts1 to be greater than 5s, thus triggering the printing.

Quoted 4 lines

The solution is to add a memory barrier after reading the ts2, which can effectively avoid this issue.

Good catch! But does the following commit help?

7ba82f5d831b ("locking/csd-lock: Switch from sched_clock() to ktime_get_mono_fast_ns()")

This is in the "dev" branch of the -rcu tree:

git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu.git

This commit switches from sched_clock() to ktime_get_mono_fast_ns() in csd_lock_wait_toolong() and __csd_lock_wait(). Because this code runs with preemption disabled, both calls to ktime_get_mono_fast_ns() will be from the same CPU. Furthermore, neither call will ever be from an NMI handler. Therefore, the times returned from those two call sites should never go backwards.

Please see the end of this email for the corresponding patch.

Thanx, Paul

Quoted 9 lines
---
 kernel/smp.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/smp.c +0 -0
index f25e20617b7e..e58678d424a4 100644

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

Quoted 8 lines
kernel/smp.c +2 -0
@@ -247,6 +247,8 @@ static bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, in
 	}
 
 	ts2 = sched_clock();
+	/* Avoid ts2 being used before it is initialized */
+	mb();
 	/* How long since we last checked for a stuck CSD lock.*/
 	ts_delta = ts2 - *ts1;
 	if (likely(ts_delta <= csd_lock_timeout_ns * (*nmessages + 1) *

------------------------------------------------------------------------

commit 7ba82f5d831bcddfa22b6b782c5e254c5580bc07

Author: Paul E. McKenney <[email protected]>
Date:   Mon Aug 5 11:44:43 2024 -0700

locking/csd-lock: Switch from sched_clock() to ktime_get_mono_fast_ns()

Currently, the CONFIG_CSD_LOCK_WAIT_DEBUG code uses sched_clock() to check for excessive CSD-lock wait times. This works, but does not guarantee monotonic timestamps. Therefore, switch from sched_clock() to ktime_get_mono_fast_ns(), which does guarantee monotonic timestamps, at least in the absence of calls from NMI handlers, which are not involved in this code path.

Cc Neeraj Upadhyay <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Leonardo Bras <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc "Peter Zijlstra (Intel)" <[email protected]>
kernel/smp.c +2 -2
index f25e20617b7eb..27dc31a146a35 100644
@@ -246,7 +246,7 @@ static bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, in
 		return true;
 	}
 
-	ts2 = sched_clock();
+	ts2 = ktime_get_mono_fast_ns();
 	/* How long since we last checked for a stuck CSD lock.*/
 	ts_delta = ts2 - *ts1;
 	if (likely(ts_delta <= csd_lock_timeout_ns * (*nmessages + 1) *
@@ -321,7 +321,7 @@ static void __csd_lock_wait(call_single_data_t *csd)
 	int bug_id = 0;
 	u64 ts0, ts1;
 
-	ts1 = ts0 = sched_clock();
+	ts1 = ts0 = ktime_get_mono_fast_ns();
 	for (;;) {
 		if (csd_lock_wait_toolong(csd, ts0, &ts1, &bug_id, &nmessages))
 			break;
author
#2 lore raw

I planned to wait for the test results before reply. After discussing with neeraj, I believe that there will be a positive experimental result after applying this patch, and if there are any experimental results, I will update further. For now, I agree with this patch.

Zhu Qiyu

#3 lore raw
Quoted 1 line · Zhu Qiyu

I planned to wait for the test results before reply. After discussing with neeraj, I believe that there will be a positive experimental result after applying this patch, and if there are any experimental results, I will update further. For now, I agree with this patch.

Thank you, Zhu Qiyu!

Thanx, Paul