[PATCH v2 2/4] lib: sbi: Add error check for sbi_timer_waitms_until

Xiang W wxjstz at 126.com
Tue May 27 22:53:51 PDT 2025


sbi_timer_value may always return 0 when there is no suitable device,
which may cause sbi_timer_waitms_until to fall into an infinite loop.
Add error handling to prevent this from happening

Signed-off-by: Xiang W <wxjstz at 126.com>
---
 lib/sbi/sbi_timer.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/lib/sbi/sbi_timer.c b/lib/sbi/sbi_timer.c
index 3530a005..e13d6161 100644
--- a/lib/sbi/sbi_timer.c
+++ b/lib/sbi/sbi_timer.c
@@ -79,12 +79,18 @@ void sbi_timer_delay_loop(ulong units, u64 unit_freq,
 bool sbi_timer_waitms_until(bool (*predicate)(void *), void *arg,
 			    uint64_t timeout_ms)
 {
-	uint64_t start_time = sbi_timer_value();
-	uint64_t ticks =
-		(sbi_timer_get_device()->timer_freq / 1000) *
-		timeout_ms;
+	u64 start_time, ticks;
+
+	/* Do nothing if we don't have timer device */
+	if (!timer_dev || !get_time_val) {
+		sbi_printf("%s: called without timer device\n", __func__);
+		return false;
+	}
+
+	start_time = get_time_val();
+	ticks = (timer_dev->timer_freq / 1000) * timeout_ms;
 	while(!predicate(arg))
-		if (sbi_timer_value() - start_time  >= ticks)
+		if (get_time_val() - start_time  >= ticks)
 			return false;
 	return true;
 }
-- 
2.47.2




More information about the opensbi mailing list