[PATCH] locking/csd_lock: fix csd_lock_wait_toolong() error warning

From: Zhu Qiyu
Date: Fri Aug 23 2024 - 04:01:37 EST


From: "Zhu Qiyu" <qiyuzhu2@xxxxxxx>

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.

Signed-off-by: Zhu Qiyu <qiyuzhu2@xxxxxxx>
---
kernel/smp.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/kernel/smp.c b/kernel/smp.c
index f25e20617b7e..e58678d424a4 100644
--- a/kernel/smp.c
+++ b/kernel/smp.c
@@ -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) *
--
2.34.1