[PATCH 08/18] dbtr: Improve error handling for trigger enable, disable, uninstall

Nicholas Piggin
2026-03-13T05:20:49Z

When updating multiple triggers, error checking should be performed on
all triggers before changing any. Check all triggers for mapping to HW
in these handlers.

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

diff --git a/lib/sbi/sbi_dbtr.c b/lib/sbi/sbi_dbtr.c
index 3925170f..a2c45061 100644
--- a/lib/sbi/sbi_dbtr.c
+++ b/lib/sbi/sbi_dbtr.c
@@ -672,7 +672,7 @@ int sbi_dbtr_uninstall_trig(unsigned long trig_idx_base,
unsigned long trig_idx_mask)
{
unsigned long trig_mask = trig_idx_mask << trig_idx_base;
- unsigned long idx = trig_idx_base;
+ unsigned long idx;
struct sbi_dbtr_trigger *trig;
struct sbi_dbtr_hart_triggers_state *hs;

@@ -685,13 +685,17 @@ int sbi_dbtr_uninstall_trig(unsigned long trig_idx_base,
trig_idx_base >= hs->total_trigs)
return SBI_ERR_INVALID_PARAM;

+ idx = trig_idx_base;
for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
trig = INDEX_TO_TRIGGER(idx);
if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED)))
return SBI_ERR_INVALID_PARAM;
+ }

+ idx = trig_idx_base;
+ for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
+ trig = INDEX_TO_TRIGGER(idx);
dbtr_trigger_clear(trig);
-
sbi_free_trigger(trig);
}

@@ -702,7 +706,7 @@ int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
unsigned long trig_idx_mask)
{
unsigned long trig_mask = trig_idx_mask << trig_idx_base;
- unsigned long idx = trig_idx_base;
+ unsigned long idx;
struct sbi_dbtr_trigger *trig;
struct sbi_dbtr_hart_triggers_state *hs;

@@ -715,6 +719,14 @@ int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
trig_idx_base >= hs->total_trigs)
return SBI_ERR_INVALID_PARAM;

+ idx = trig_idx_base;
+ for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
+ trig = INDEX_TO_TRIGGER(idx);
+ if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED)))
+ return SBI_ERR_INVALID_PARAM;
+ }
+
+ idx = trig_idx_base;
for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
trig = INDEX_TO_TRIGGER(idx);
sbi_dprintf("%s: enable trigger %lu\n", __func__, idx);
@@ -810,7 +822,7 @@ int sbi_dbtr_disable_trig(unsigned long trig_idx_base,
unsigned long trig_idx_mask)
{
unsigned long trig_mask = trig_idx_mask << trig_idx_base;
- unsigned long idx = trig_idx_base;
+ unsigned long idx;
struct sbi_dbtr_trigger *trig;
struct sbi_dbtr_hart_triggers_state *hs;

@@ -823,6 +835,14 @@ int sbi_dbtr_disable_trig(unsigned long trig_idx_base,
trig_idx_base >= hs->total_trigs)
return SBI_ERR_INVALID_PARAM;

+ idx = trig_idx_base;
+ for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
+ trig = INDEX_TO_TRIGGER(idx);
+ if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED)))
+ return SBI_ERR_INVALID_PARAM;
+ }
+
+ idx = trig_idx_base;
for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
trig = INDEX_TO_TRIGGER(idx);
dbtr_trigger_disable(trig);