[PATCH 08/14] lib: htif: add TSA annotations

Carlos López carlos.lopezr4096 at gmail.com
Wed Jul 29 09:35:35 PDT 2026


Add Thread Safety Analysis (TSA) annotations for the HTIF driver
implementation.

This requires annotating the tohost/fromhost and htif_console_buf
globals to indicate that they are protected by htif_lock.
htif_system_reset() can be excluded since it is only called under
special circumstances from sbi_system_reset(), when all other harts
have been stopped.

Signed-off-by: Carlos López <carlos.lopezr4096 at gmail.com>
---
 lib/utils/sys/htif.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/lib/utils/sys/htif.c b/lib/utils/sys/htif.c
index b4e63212477a..1360613cd5aa 100644
--- a/lib/utils/sys/htif.c
+++ b/lib/utils/sys/htif.c
@@ -46,22 +46,25 @@
 
 #define PK_SYS_write 64
 
-volatile uint64_t tohost __attribute__((section(".htif")));
-volatile uint64_t fromhost __attribute__((section(".htif")));
+static spinlock_t htif_lock = SPIN_LOCK_INITIALIZER;
+
+volatile uint64_t tohost
+	__attribute__((section(".htif"))) GUARDED_BY(&htif_lock);
+volatile uint64_t fromhost
+	__attribute__((section(".htif"))) GUARDED_BY(&htif_lock);
 
-static uint64_t *htif_fromhost = NULL;
-static uint64_t *htif_tohost = NULL;
+static uint64_t *htif_fromhost PT_GUARDED_BY(&htif_lock) = NULL;
+static uint64_t *htif_tohost PT_GUARDED_BY(&htif_lock)	 = NULL;
 static bool htif_custom = false;
 
-static int htif_console_buf;
-static spinlock_t htif_lock = SPIN_LOCK_INITIALIZER;
+static int htif_console_buf GUARDED_BY(&htif_lock);
 
-static inline uint64_t __read_tohost(void)
+static inline uint64_t __read_tohost(void) MUST_HOLD(&htif_lock)
 {
 	return (htif_custom) ? *htif_tohost : tohost;
 }
 
-static inline void __write_tohost(uint64_t val)
+static inline void __write_tohost(uint64_t val) MUST_HOLD(&htif_lock)
 {
 	if (htif_custom)
 		*htif_tohost = val;
@@ -69,12 +72,12 @@ static inline void __write_tohost(uint64_t val)
 		tohost = val;
 }
 
-static inline uint64_t __read_fromhost(void)
+static inline uint64_t __read_fromhost(void) MUST_HOLD(&htif_lock)
 {
 	return (htif_custom) ? *htif_fromhost : fromhost;
 }
 
-static inline void __write_fromhost(uint64_t val)
+static inline void __write_fromhost(uint64_t val) MUST_HOLD(&htif_lock)
 {
 	if (htif_custom)
 		*htif_fromhost = val;
@@ -82,7 +85,7 @@ static inline void __write_fromhost(uint64_t val)
 		fromhost = val;
 }
 
-static void __check_fromhost()
+static void __check_fromhost() MUST_HOLD(&htif_lock)
 {
 	uint64_t fh = __read_fromhost();
 	if (!fh)
@@ -104,6 +107,7 @@ static void __check_fromhost()
 }
 
 static void __set_tohost(uint64_t dev, uint64_t cmd, uint64_t data)
+	MUST_HOLD(&htif_lock)
 {
 	while (__read_tohost())
 		__check_fromhost();
@@ -217,7 +221,7 @@ static int htif_system_reset_check(u32 type, u32 reason)
 	return 1;
 }
 
-static void htif_system_reset(u32 type, u32 reason)
+static void htif_system_reset(u32 type, u32 reason) NO_THREAD_SAFETY_ANALYSIS
 {
 	while (1) {
 		__write_fromhost(0);
-- 
2.51.0




More information about the opensbi mailing list