[PATCH 13/18] dbtr: Rework install and update error handling

Nicholas Piggin
2026-03-13T05:21:08Z

Move the unmap error path to an error handling label rather
than duplicate it several times.

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
lib/sbi/sbi_dbtr.c | 54 +++++++++++++++++++++++++---------------------
1 file changed, 30 insertions(+), 24 deletions(-)

diff --git a/lib/sbi/sbi_dbtr.c b/lib/sbi/sbi_dbtr.c
index cfcd728f..3aea486e 100644
--- a/lib/sbi/sbi_dbtr.c
+++ b/lib/sbi/sbi_dbtr.c
@@ -622,6 +622,7 @@ int sbi_dbtr_install_trig(unsigned long smode,
struct sbi_dbtr_id_msg *xmit;
struct sbi_dbtr_trigger *trig;
struct sbi_dbtr_hart_triggers_state *hs = NULL;
+ int err;

hs = dbtr_thishart_state_ptr();
if (!hs)
@@ -646,24 +647,21 @@ int sbi_dbtr_install_trig(unsigned long smode,

if (!dbtr_trigger_supported(TDATA1_GET_TYPE(tdata1), tdata1)) {
*out = _idx;
- sbi_hart_protection_unmap_range((unsigned long)shmem_base,
- trig_count * sizeof(*entry));
- return SBI_ERR_NOT_SUPPORTED;
+ err = SBI_ERR_NOT_SUPPORTED;
+ goto out_unmap;
}

if (!dbtr_trigger_valid(TDATA1_GET_TYPE(tdata1), tdata1)) {
*out = _idx;
- sbi_hart_protection_unmap_range((unsigned long)shmem_base,
- trig_count * sizeof(*entry));
- return SBI_ERR_FAILED;
+ err = SBI_ERR_FAILED;
+ goto out_unmap;
}
}

if (hs->available_trigs < trig_count) {
*out = hs->available_trigs;
- sbi_hart_protection_unmap_range((unsigned long)shmem_base,
- trig_count * sizeof(*entry));
- return SBI_ERR_FAILED;
+ err = SBI_ERR_FAILED;
+ goto out_unmap;
}

/* Install triggers */
@@ -687,6 +685,12 @@ int sbi_dbtr_install_trig(unsigned long smode,
trig_count * sizeof(*entry));

return SBI_SUCCESS;
+
+out_unmap:
+ sbi_hart_protection_unmap_range((unsigned long)shmem_base,
+ trig_count * sizeof(*entry));
+
+ return err;
}

int sbi_dbtr_uninstall_trig(unsigned long trig_idx_base,
@@ -771,6 +775,7 @@ int sbi_dbtr_update_trig(unsigned long smode,
struct sbi_dbtr_data_msg *recv;
void *shmem_base = NULL;
struct sbi_dbtr_hart_triggers_state *hs = NULL;
+ int err;

hs = dbtr_thishart_state_ptr();
if (!hs)
@@ -793,17 +798,15 @@ int sbi_dbtr_update_trig(unsigned long smode,
trig_idx = entry->id.idx;
if (trig_idx >= hs->total_trigs) {
*out = _idx;
- sbi_hart_protection_unmap_range((unsigned long)shmem_base,
- trig_count * sizeof(*entry));
- return SBI_ERR_INVALID_PARAM;
+ err = SBI_ERR_INVALID_PARAM;
+ goto out_unmap;
}

trig = INDEX_TO_TRIGGER(trig_idx);
if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED))) {
*out = _idx;
- sbi_hart_protection_unmap_range((unsigned long)shmem_base,
- trig_count * sizeof(*entry));
- return SBI_ERR_FAILED;
+ err = SBI_ERR_FAILED;
+ goto out_unmap;
}

recv = (struct sbi_dbtr_data_msg *)(&entry->data);
@@ -812,23 +815,20 @@ int sbi_dbtr_update_trig(unsigned long smode,

if (!dbtr_trigger_supported(type, tdata1)) {
*out = _idx;
- sbi_hart_protection_unmap_range((unsigned long)shmem_base,
- trig_count * sizeof(*entry));
- return SBI_ERR_NOT_SUPPORTED;
+ err = SBI_ERR_NOT_SUPPORTED;
+ goto out_unmap;
}

if (!dbtr_trigger_valid(type, tdata1)) {
*out = _idx;
- sbi_hart_protection_unmap_range((unsigned long)shmem_base,
- trig_count * sizeof(*entry));
- return SBI_ERR_INVALID_PARAM;
+ err = SBI_ERR_INVALID_PARAM;
+ goto out_unmap;
}

if (type != TDATA1_GET_TYPE(trig->tdata1)) {
*out = _idx;
- sbi_hart_protection_unmap_range((unsigned long)shmem_base,
- trig_count * sizeof(*entry));
- return SBI_ERR_INVALID_PARAM;
+ err = SBI_ERR_INVALID_PARAM;
+ goto out_unmap;
}
}

@@ -843,6 +843,12 @@ int sbi_dbtr_update_trig(unsigned long smode,
trig_count * sizeof(*entry));

return SBI_SUCCESS;
+
+out_unmap:
+ sbi_hart_protection_unmap_range((unsigned long)shmem_base,
+ trig_count * sizeof(*entry));
+
+ return err;
}

int sbi_dbtr_disable_trig(unsigned long trig_idx_base,