[PATCH 04/14] lib: sbi_heap: add TSA annotations

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


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 <carlos.lopezr4096 at gmail.com>
---
 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 1de6dc1e87a8..cda124a4eebd 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;
 
-- 
2.51.0




More information about the opensbi mailing list