From: Gabriele Monaco <gmonaco@redhat.com>
To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
Steven Rostedt <rostedt@goodmis.org>,
Gabriele Monaco <gmonaco@redhat.com>,
Masami Hiramatsu <mhiramat@kernel.org>
Cc: Nam Cao <namcao@linutronix.de>, Wen Yang <wen.yang@linux.dev>,
Juri Lelli <juri.lelli@redhat.com>
Subject: [PATCH 03/10] rv: Cache timer budget when starting HA timers
Date: Tue, 29 Sep 2026 14:49:01 +0200 [thread overview]
Message-ID: <20260929124908.177676-4-gmonaco@redhat.com> (raw)
In-Reply-To: <20260929124908.177676-1-gmonaco@redhat.com>
Commit ab2900ae252b ("rv: Simplify hybrid automata monitors's clock
variables") simplified storage but introduces the assumption that the
expires value passed to ha_check_invariant() is the same used to arm the
timer, this may not always be true.
The invariant needs to act on a constant value and that needs to be
retrievable somewhere.
Cache the armed budget to ensure invariant checks remain consistent with
the timer's duration. For the timer wheel, add an expire field to the
ha_monitor union, this allows to store ns-granularity values if needed.
For hrtimers, use hrtimer_get_expires() to retrieve the internal
absolute expiration time.
The expire field is used also for jiffy granularity clock to avoid
accessing private fields and hrtimers always use a ns base to avoid
inconsistencies with the jiffies.
Also remove the now superfluous ha_inv_to_guard().
Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>
---
include/linux/rv.h | 5 +-
include/rv/ha_monitor.h | 53 ++++++++++---------
kernel/trace/rv/monitors/nomiss/nomiss.c | 4 +-
kernel/trace/rv/monitors/stall/stall.c | 2 +-
tools/verification/rvgen/rvgen/dot2k.py | 4 +-
.../rvgen/tests/golden/ha_percpu/ha_percpu.c | 4 +-
.../rvgen/tests/golden/test_ha/test_ha.c | 4 +-
.../golden/test_ha_kunit/test_ha_kunit.c | 4 +-
8 files changed, 41 insertions(+), 39 deletions(-)
diff --git a/include/linux/rv.h b/include/linux/rv.h
index 541ba404926a..723cc62d11d8 100644
--- a/include/linux/rv.h
+++ b/include/linux/rv.h
@@ -109,7 +109,10 @@ struct ha_monitor {
u64 env_store[MAX_HA_ENV_LEN];
union {
struct hrtimer hrtimer;
- struct timer_list timer;
+ struct {
+ struct timer_list timer;
+ u64 expire;
+ };
};
};
diff --git a/include/rv/ha_monitor.h b/include/rv/ha_monitor.h
index 87cf91abdf67..332671f3ce2c 100644
--- a/include/rv/ha_monitor.h
+++ b/include/rv/ha_monitor.h
@@ -129,9 +129,10 @@ static enum hrtimer_restart ha_monitor_timer_callback(struct hrtimer *hrtimer);
* ktime_get_ns is expensive, since we usually don't require precise accounting
* of changes within the same event, cache the current time at the beginning of
* the constraint handler and use the cache for subsequent calls.
- * Monitors without ns clocks automatically skip this.
+ * Monitors without ns clocks automatically skip this, unless they use
+ * hrtimers, which require a ns base.
*/
-#ifdef HA_CLK_NS
+#if defined(HA_CLK_NS) || HA_TIMER_TYPE == HA_TIMER_HRTIMER
#define ha_get_ns() ktime_get_ns()
#else
#define ha_get_ns() 0
@@ -341,9 +342,13 @@ static inline void ha_reset_clk_ns(struct ha_monitor *ha_mon, enum envs env, u64
WRITE_ONCE(ha_mon->env_store[env], time_ns);
}
static inline bool ha_check_invariant_ns(struct ha_monitor *ha_mon, enum envs env,
- u64 time_ns, u64 expire_ns)
+ u64 time_ns)
{
- return READ_ONCE(ha_mon->env_store[env]) >= time_ns - expire_ns;
+ if (HA_TIMER_TYPE == HA_TIMER_WHEEL)
+ return time_ns - READ_ONCE(ha_mon->env_store[env]) <= ha_mon->expire;
+ if (HA_TIMER_TYPE == HA_TIMER_HRTIMER)
+ return time_ns <= ktime_to_ns(hrtimer_get_expires(&ha_mon->hrtimer));
+ return true;
}
/*
* ha_invariant_passed_ns - prepare the invariant and return the time since reset
@@ -367,9 +372,14 @@ static inline void ha_reset_clk_jiffy(struct ha_monitor *ha_mon, enum envs env)
WRITE_ONCE(ha_mon->env_store[env], get_jiffies_64());
}
static inline bool ha_check_invariant_jiffy(struct ha_monitor *ha_mon, enum envs env,
- u64 time_ns, u64 expire_jiffy)
+ u64 time_ns)
{
- return time_after64(READ_ONCE(ha_mon->env_store[env]), get_jiffies_64() - expire_jiffy);
+ if (HA_TIMER_TYPE == HA_TIMER_WHEEL)
+ return time_after64(READ_ONCE(ha_mon->env_store[env]) + ha_mon->expire,
+ get_jiffies_64());
+ if (HA_TIMER_TYPE == HA_TIMER_HRTIMER)
+ return time_ns <= ktime_to_ns(hrtimer_get_expires(&ha_mon->hrtimer));
+ return true;
}
/*
* ha_invariant_passed_jiffy - prepare the invariant and return the time since reset
@@ -381,23 +391,6 @@ static inline u64 ha_invariant_passed_jiffy(struct ha_monitor *ha_mon, enum envs
return ha_get_env(ha_mon, env, time_ns);
}
-/*
- * Retrieve the last reset time (guard representation) from the invariant
- * representation (expiration).
- * It the caller's responsibility to make sure the storage was actually in the
- * invariant representation (e.g. the current state has an invariant).
- * The provided value must be the same used when starting the invariant.
- *
- * This function's access to the storage is NOT atomic, due to the rarity when
- * this is used. If a monitor allows writes concurrent to this, likely
- * other things are broken and need rethinking the model or additional locking.
- */
-static inline void ha_inv_to_guard(struct ha_monitor *ha_mon, enum envs env,
- u64 value, u64 time_ns)
-{
- WRITE_ONCE(ha_mon->env_store[env], READ_ONCE(ha_mon->env_store[env]) - value);
-}
-
#if HA_TIMER_TYPE == HA_TIMER_WHEEL
/*
* Helper functions to handle the monitor timer.
@@ -420,22 +413,30 @@ static inline void ha_setup_timer(struct ha_monitor *ha_mon)
if (RV_MON_TYPE == RV_MON_PER_CPU)
mode |= TIMER_PINNED;
+ ha_mon->expire = ENV_INVALID_VALUE;
timer_setup(&ha_mon->timer, ha_monitor_timer_callback, mode);
}
+static inline void _ha_start_timer(struct ha_monitor *ha_mon, u64 expire,
+ u64 time_ns)
+{
+ mod_timer(&ha_mon->timer, get_jiffies_64() + expire);
+}
static inline void ha_start_timer_jiffy(struct ha_monitor *ha_mon, enum envs env,
u64 expire, u64 time_ns)
{
u64 passed = ha_invariant_passed_jiffy(ha_mon, env, time_ns);
- mod_timer(&ha_mon->timer, get_jiffies_64() + expire - passed);
+ ha_mon->expire = expire;
+ _ha_start_timer(ha_mon, expire - passed, time_ns);
}
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);
- ha_start_timer_jiffy(ha_mon, ENV_MAX_STORED,
- nsecs_to_jiffies(expire - passed + TICK_NSEC - 1), time_ns);
+ ha_mon->expire = expire;
+ _ha_start_timer(ha_mon, nsecs_to_jiffies(expire - passed + TICK_NSEC - 1),
+ time_ns);
}
/*
* ha_cancel_timer - Cancel the timer
diff --git a/kernel/trace/rv/monitors/nomiss/nomiss.c b/kernel/trace/rv/monitors/nomiss/nomiss.c
index 6e47d379f777..5e72df87b23a 100644
--- a/kernel/trace/rv/monitors/nomiss/nomiss.c
+++ b/kernel/trace/rv/monitors/nomiss/nomiss.c
@@ -57,9 +57,9 @@ static inline bool ha_verify_invariants(struct ha_monitor *ha_mon,
enum states next_state, u64 time_ns)
{
if (curr_state == ready_nomiss)
- return ha_check_invariant_ns(ha_mon, clk_nomiss, time_ns, DEADLINE_NS(ha_mon));
+ return ha_check_invariant_ns(ha_mon, clk_nomiss, time_ns);
else if (curr_state == running_nomiss)
- return ha_check_invariant_ns(ha_mon, clk_nomiss, time_ns, DEADLINE_NS(ha_mon));
+ return ha_check_invariant_ns(ha_mon, clk_nomiss, time_ns);
return true;
}
diff --git a/kernel/trace/rv/monitors/stall/stall.c b/kernel/trace/rv/monitors/stall/stall.c
index b265578f845c..3c38fb1a0159 100644
--- a/kernel/trace/rv/monitors/stall/stall.c
+++ b/kernel/trace/rv/monitors/stall/stall.c
@@ -38,7 +38,7 @@ static inline bool ha_verify_invariants(struct ha_monitor *ha_mon,
enum states next_state, u64 time_ns)
{
if (curr_state == enqueued_stall)
- return ha_check_invariant_jiffy(ha_mon, clk_stall, time_ns, threshold_jiffies);
+ return ha_check_invariant_jiffy(ha_mon, clk_stall, time_ns);
return true;
}
diff --git a/tools/verification/rvgen/rvgen/dot2k.py b/tools/verification/rvgen/rvgen/dot2k.py
index 0b4e19ad9bd5..c8109f3602a6 100644
--- a/tools/verification/rvgen/rvgen/dot2k.py
+++ b/tools/verification/rvgen/rvgen/dot2k.py
@@ -248,9 +248,7 @@ class ha2k(dot2k):
if inv.unit == "j":
clock_type = "jiffy"
- value = self.__adjust_value(inv.val, inv.unit)
-
- return f"return ha_check_invariant_{clock_type}(ha_mon, {inv.env}_{self.name}, time_ns, {value})"
+ return f"return ha_check_invariant_{clock_type}(ha_mon, {inv.env}_{self.name}, time_ns)"
def __parse_invariant(self, inv):
# by default assume the timer has ns expiration
diff --git a/tools/verification/rvgen/tests/golden/ha_percpu/ha_percpu.c b/tools/verification/rvgen/tests/golden/ha_percpu/ha_percpu.c
index 7357e182e487..5aa7e64467f5 100644
--- a/tools/verification/rvgen/tests/golden/ha_percpu/ha_percpu.c
+++ b/tools/verification/rvgen/tests/golden/ha_percpu/ha_percpu.c
@@ -87,9 +87,9 @@ static inline bool ha_verify_invariants(struct ha_monitor *ha_mon,
enum states next_state, u64 time_ns)
{
if (curr_state == S0_ha_percpu)
- return ha_check_invariant_ns(ha_mon, clk_ha_percpu, time_ns, bar_ns(ha_mon));
+ return ha_check_invariant_ns(ha_mon, clk_ha_percpu, time_ns);
else if (curr_state == S2_ha_percpu)
- return ha_check_invariant_ns(ha_mon, clk_ha_percpu, time_ns, BAR_NS(ha_mon));
+ return ha_check_invariant_ns(ha_mon, clk_ha_percpu, time_ns);
return true;
}
diff --git a/tools/verification/rvgen/tests/golden/test_ha/test_ha.c b/tools/verification/rvgen/tests/golden/test_ha/test_ha.c
index a2bd1eb93b03..3852337dc675 100644
--- a/tools/verification/rvgen/tests/golden/test_ha/test_ha.c
+++ b/tools/verification/rvgen/tests/golden/test_ha/test_ha.c
@@ -87,9 +87,9 @@ static inline bool ha_verify_invariants(struct ha_monitor *ha_mon,
enum states next_state, u64 time_ns)
{
if (curr_state == S0_test_ha)
- return ha_check_invariant_ns(ha_mon, clk_test_ha, time_ns, bar_ns(ha_mon));
+ return ha_check_invariant_ns(ha_mon, clk_test_ha, time_ns);
else if (curr_state == S2_test_ha)
- return ha_check_invariant_ns(ha_mon, clk_test_ha, time_ns, BAR_NS(ha_mon));
+ return ha_check_invariant_ns(ha_mon, clk_test_ha, time_ns);
return true;
}
diff --git a/tools/verification/rvgen/tests/golden/test_ha_kunit/test_ha_kunit.c b/tools/verification/rvgen/tests/golden/test_ha_kunit/test_ha_kunit.c
index 61446e7f32c4..1fd524f95c07 100644
--- a/tools/verification/rvgen/tests/golden/test_ha_kunit/test_ha_kunit.c
+++ b/tools/verification/rvgen/tests/golden/test_ha_kunit/test_ha_kunit.c
@@ -87,9 +87,9 @@ static inline bool ha_verify_invariants(struct ha_monitor *ha_mon,
enum states next_state, u64 time_ns)
{
if (curr_state == S0_test_ha_kunit)
- return ha_check_invariant_ns(ha_mon, clk_test_ha_kunit, time_ns, bar_ns(ha_mon));
+ return ha_check_invariant_ns(ha_mon, clk_test_ha_kunit, time_ns);
else if (curr_state == S2_test_ha_kunit)
- return ha_check_invariant_ns(ha_mon, clk_test_ha_kunit, time_ns, BAR_NS(ha_mon));
+ return ha_check_invariant_ns(ha_mon, clk_test_ha_kunit, time_ns);
return true;
}
--
2.55.0
next prev parent reply other threads:[~2026-09-29 12:49 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-29 12:48 [PATCH 00/10] rv: Add remaining deadline monitors Gabriele Monaco
2026-09-29 12:48 ` [PATCH 01/10] rv: Force environment reset action on HA monitor start Gabriele Monaco
2026-09-29 12:49 ` [PATCH 02/10] rv: Remove unused ha_monitor_env_invalid() Gabriele Monaco
2026-09-29 12:49 ` Gabriele Monaco [this message]
2026-09-29 12:49 ` [PATCH 04/10] tools/rvgen: Default to HA_TIMER_WHEEL for jiffy clocks Gabriele Monaco
2026-09-29 12:49 ` [PATCH 05/10] sched: Add task enqueue/dequeue trace points Gabriele Monaco
2026-09-29 12:49 ` [PATCH 06/10] rv: Add enqueue/dequeue to snroc monitor Gabriele Monaco
2026-09-29 12:49 ` [PATCH 07/10] rv: Add throttle deadline monitor Gabriele Monaco
2026-09-29 12:49 ` [PATCH 08/10] rv: Add dl_server specific monitors Gabriele Monaco
2026-09-29 12:49 ` [PATCH 09/10] rv: Add KUnit test for throttle monitor Gabriele Monaco
2026-09-29 12:49 ` [PATCH 10/10] selftests/verification: Lower stressor priority in rv_deadline Gabriele Monaco
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260929124908.177676-4-gmonaco@redhat.com \
--to=gmonaco@redhat.com \
--cc=juri.lelli@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mhiramat@kernel.org \
--cc=namcao@linutronix.de \
--cc=rostedt@goodmis.org \
--cc=wen.yang@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®