From 1ba332872b5fbb50d83abef1f8f451a28b5119fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20L=C3=B3pez?= Date: Wed, 9 Sep 2026 18:23:17 +0200 Subject: [PATCH] lib: htif: add TSA annotations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Anup Patel Link: https://lore.kernel.org/r/20260909162322.29778-10-carlos.lopezr4096@gmail.com Signed-off-by: Anup Patel --- lib/utils/sys/htif.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/utils/sys/htif.c b/lib/utils/sys/htif.c index b4e63212..1360613c 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 uint64_t *htif_fromhost = NULL; -static uint64_t *htif_tohost = NULL; -static bool htif_custom = false; - -static int htif_console_buf; static spinlock_t htif_lock = SPIN_LOCK_INITIALIZER; -static inline uint64_t __read_tohost(void) +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 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 GUARDED_BY(&htif_lock); + +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);