[PATCH v8 02/14] rv: Fix ha_monitor timer and invariant underflow
From: wen . yang
Date: Thu Nov 05 2026 - 12:53:15 EST
From: Wen Yang <wen.yang@xxxxxxxxx>
ha_start_timer_ns() and ha_start_timer_jiffy() compute the remaining
budget as expire - passed. Once the elapsed time passed exceeds the
budget expire (e.g. a state transition after the invariant should have
fired, before the timer callback ran), the unsigned subtraction
underflows to a huge value and the timer is scheduled far into the
future instead of expiring immediately, so the monitor stops enforcing
the budget.
ha_check_invariant_ns() computes store >= time_ns - expire_ns, which
underflows when the system uptime is still below expire_ns (a monitor
armed early in boot with a large budget), reporting a false violation.
Clamp the timer remainder to 0 (fire immediately) when passed >= expire,
and rewrite the invariant check as store + expire_ns >= time_ns so
neither side can underflow. Both timer flavours (jiffy and hrtimer) are
fixed.
Fixes: f5587d1b6ec9 ("rv: Add Hybrid Automata monitor type")
Fixes: ab2900ae252b ("rv: Simplify hybrid automata monitors's clock variables")
Reported-by: sashiko-bot <sashiko-bot@xxxxxxxxxx>
Reviewed-by: Gabriele Monaco <gmonaco@xxxxxxxxxx>
Signed-off-by: Wen Yang <wen.yang@xxxxxxxxx>
---
include/rv/ha_monitor.h | 29 ++++++++++++++++++++++++-----
1 file changed, 24 insertions(+), 5 deletions(-)
diff --git a/include/rv/ha_monitor.h b/include/rv/ha_monitor.h
index 93ac99ff7f1d..ebb7f91e3447 100644
--- a/include/rv/ha_monitor.h
+++ b/include/rv/ha_monitor.h
@@ -351,7 +351,11 @@ static inline void ha_reset_clk_ns(struct ha_monitor *ha_mon, enum envs env, u64
static inline bool ha_check_invariant_ns(struct ha_monitor *ha_mon, enum envs env,
u64 time_ns, u64 expire_ns)
{
- return READ_ONCE(ha_mon->env_store[env]) >= time_ns - expire_ns;
+ /*
+ * Compare as store + expire >= time to avoid underflow when the
+ * system uptime is still below expire_ns (early boot, large budget).
+ */
+ return READ_ONCE(ha_mon->env_store[env]) + expire_ns >= time_ns;
}
/*
* ha_invariant_passed_ns - prepare the invariant and return the time since reset
@@ -410,6 +414,17 @@ static inline void ha_inv_to_guard(struct ha_monitor *ha_mon, enum envs env,
WRITE_ONCE(ha_mon->env_store[env], READ_ONCE(ha_mon->env_store[env]) - value);
}
+/*
+ * ha_time_left - time left before expire, 0 once it already passed
+ *
+ * A plain expire - passed underflows and would arm the timer far in the
+ * future. Unit-agnostic: callers pass ns or jiffies consistently.
+ */
+static inline u64 ha_time_left(u64 passed, u64 expire)
+{
+ return passed < expire ? expire - passed : 0;
+}
+
#if HA_TIMER_TYPE == HA_TIMER_WHEEL
/*
* Helper functions to handle the monitor timer.
@@ -439,15 +454,17 @@ static inline void ha_start_timer_jiffy(struct ha_monitor *ha_mon, enum envs env
{
u64 passed = ha_invariant_passed_jiffy(ha_mon, env, time_ns);
- mod_timer(&ha_mon->timer, get_jiffies_64() + expire - passed);
+ mod_timer(&ha_mon->timer,
+ get_jiffies_64() + ha_time_left(passed, expire));
}
static inline void ha_start_timer_ns(struct ha_monitor *ha_mon, enum envs env,
u64 expire, u64 time_ns)
{
u64 passed = ha_invariant_passed_ns(ha_mon, env, time_ns);
+ u64 delay = ha_time_left(passed, expire);
ha_start_timer_jiffy(ha_mon, ENV_MAX_STORED,
- nsecs_to_jiffies(expire - passed + TICK_NSEC - 1), time_ns);
+ nsecs_to_jiffies(delay + TICK_NSEC - 1), time_ns);
}
/*
* ha_cancel_timer - Cancel the timer
@@ -494,7 +511,8 @@ static inline void ha_start_timer_ns(struct ha_monitor *ha_mon, enum envs env,
if (RV_MON_TYPE == RV_MON_PER_CPU)
mode |= HRTIMER_MODE_PINNED;
- hrtimer_start(&ha_mon->hrtimer, ns_to_ktime(expire - passed), mode);
+ hrtimer_start(&ha_mon->hrtimer,
+ ns_to_ktime(ha_time_left(passed, expire)), mode);
}
static inline void ha_start_timer_jiffy(struct ha_monitor *ha_mon, enum envs env,
u64 expire, u64 time_ns)
@@ -502,7 +520,8 @@ static inline void ha_start_timer_jiffy(struct ha_monitor *ha_mon, enum envs env
u64 passed = ha_invariant_passed_jiffy(ha_mon, env, time_ns);
ha_start_timer_ns(ha_mon, ENV_MAX_STORED,
- jiffies_to_nsecs(expire - passed), time_ns);
+ jiffies_to_nsecs(ha_time_left(passed, expire)),
+ time_ns);
}
/*
* ha_cancel_timer - Cancel the timer
--
2.25.1