From eaac726ea11e4f1a2a36353e137fc8d0d0c0a298 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20L=C3=B3pez?= Date: Wed, 9 Sep 2026 18:23:13 +0200 Subject: [PATCH] lib: sbi_heap: 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 heap allocator. This includes indicating which fields are protected by a spinlock and annotating functions that must be called under a spinlock. Exclude sbi_heap_init_new(), as the spinlock cannot be acquired before it is initialized. Signed-off-by: Carlos López Reviewed-by: Anup Patel Link: https://lore.kernel.org/r/20260909162322.29778-6-carlos.lopezr4096@gmail.com Signed-off-by: Anup Patel --- lib/sbi/sbi_heap.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/sbi/sbi_heap.c b/lib/sbi/sbi_heap.c index 1de6dc1e..cda124a4 100644 --- a/lib/sbi/sbi_heap.c +++ b/lib/sbi/sbi_heap.c @@ -28,18 +28,18 @@ struct heap_node { struct sbi_heap_control { spinlock_t lock; - unsigned long base; - unsigned long size; - unsigned long resv; - struct sbi_dlist free_node_list; - struct sbi_dlist free_space_list; - struct sbi_dlist used_space_list; - struct heap_node init_free_space_node; + unsigned long base GUARDED_BY(&lock); + unsigned long size GUARDED_BY(&lock); + unsigned long resv GUARDED_BY(&lock); + struct sbi_dlist free_node_list GUARDED_BY(&lock); + struct sbi_dlist free_space_list GUARDED_BY(&lock); + struct sbi_dlist used_space_list GUARDED_BY(&lock); + struct heap_node init_free_space_node GUARDED_BY(&lock); }; struct sbi_heap_control global_hpctrl; -static bool alloc_nodes(struct sbi_heap_control *hpctrl) +static bool alloc_nodes(struct sbi_heap_control *hpctrl) MUST_HOLD(&hpctrl->lock) { size_t size = HEAP_NODE_BATCH_SIZE * sizeof(struct heap_node); struct heap_node *n, *new = NULL; @@ -69,8 +69,9 @@ static bool alloc_nodes(struct sbi_heap_control *hpctrl) return true; } -static void *alloc_with_align(struct sbi_heap_control *hpctrl, - size_t align, size_t size) +static void *alloc_with_align(struct sbi_heap_control *hpctrl, size_t align, + size_t size) + MUST_NOT_HOLD(&hpctrl->lock) { void *ret = NULL; struct heap_node *n, *np; @@ -230,17 +231,19 @@ unsigned long sbi_heap_free_space_from(struct sbi_heap_control *hpctrl) } unsigned long sbi_heap_used_space_from(struct sbi_heap_control *hpctrl) + NO_THREAD_SAFETY_ANALYSIS { return hpctrl->size - hpctrl->resv - sbi_heap_free_space(); } unsigned long sbi_heap_reserved_space_from(struct sbi_heap_control *hpctrl) + NO_THREAD_SAFETY_ANALYSIS { return hpctrl->resv; } int sbi_heap_init_new(struct sbi_heap_control *hpctrl, unsigned long base, - unsigned long size) + unsigned long size) NO_THREAD_SAFETY_ANALYSIS { struct heap_node *n;