hwmon: allow immediate cold-start acquisition; keep failures rate-limited

Addresses the second CodeRabbit minor finding (verified against
b6f657b4): the zero-initialized attempt timestamp was a sentinel, not
a real attempt, so a freshly registered slot could be throttled before
its first poll when the Linux tick counter was below the poll
interval. A separate 'attempted' flag now gates the retry deadline:
the first attempt always runs (cold start acquires immediately, as the
merge base did), while subsequent attempts - successes, failures and
unsupported providers alike - remain rate-limited by the configured
interval. mark_polled also marks an attempt so a Rust-side poll
completion keeps its throttling semantics. Regression test: cold
start with a small tick counter acquires on the first read with
exactly one poll, and the window still suppresses further polls.

The first CodeRabbit minor finding (transport-latency vs acquisition
age on a late-RPC timeout) is an existing upstream producer-contract
limitation, not a regression of this patch: the merge base served the
delayed value indefinitely; this patch bounds it to the 2 s served
age. Producer-side acquisition timestamps remain the documented
follow-up.
This commit is contained in:
Jo Lodewyckx
2026-12-05 03:37:28 +01:00
parent b6f657b4a9
commit 60112a59af
2 changed files with 27 additions and 7 deletions

View File

@ -94,6 +94,7 @@ struct nv_rs_hwmon_slot {
spinlock_t sample_lock;
u64 mirror_seq;
bool has_poll;
bool attempted;
unsigned long last_poll_jiffies;
unsigned long live_stamps[NV_RS_HWMON_LIVE_COUNT];
u64 pub_seq[NV_RS_HWMON_LIVE_COUNT];
@ -194,16 +195,18 @@ static void nv_rs_hwmon_maybe_refresh(struct nv_rs_hwmon_slot *slot)
poll_ms = NV_RS_HWMON_POLL_MAX_MS;
ttl = msecs_to_jiffies(poll_ms);
/* Rate-limit attempts on last_poll_jiffies alone so a failing cold
* start retries at most once per window. A failed poll does not
* count as a successful poll; unsupported providers are retried
* instead of latched, so a runtime that starts later still
* acquires. */
if (time_before(jiffies, READ_ONCE(slot->last_poll_jiffies) + ttl))
/* Rate-limit repeated attempts with the poll interval, but never
* gate the very first attempt: a freshly registered slot must
* acquire immediately (the zero-initialized attempt timestamp is
* a sentinel, not a real attempt). Failures and unsupported
* providers stay rate-limited once an attempt has happened. */
if (READ_ONCE(slot->attempted) &&
time_before(jiffies, READ_ONCE(slot->last_poll_jiffies) + ttl))
return;
mutex_lock(&slot->poll_lock);
if (time_before(jiffies, READ_ONCE(slot->last_poll_jiffies) + ttl)) {
if (READ_ONCE(slot->attempted) &&
time_before(jiffies, READ_ONCE(slot->last_poll_jiffies) + ttl)) {
mutex_unlock(&slot->poll_lock);
return;
}
@ -228,6 +231,7 @@ static void nv_rs_hwmon_maybe_refresh(struct nv_rs_hwmon_slot *slot)
* normally. */
nv_rs_rmapi_node_unlock();
WRITE_ONCE(slot->last_poll_jiffies, jiffies);
WRITE_ONCE(slot->attempted, true);
mutex_unlock(&slot->poll_lock);
return;
}
@ -271,6 +275,7 @@ static void nv_rs_hwmon_maybe_refresh(struct nv_rs_hwmon_slot *slot)
/* Rate-limit the next attempt regardless of the outcome. */
WRITE_ONCE(slot->last_poll_jiffies, jiffies);
WRITE_ONCE(slot->attempted, true);
if (rc == 0)
WRITE_ONCE(slot->has_poll, true);
mutex_unlock(&slot->poll_lock);
@ -550,6 +555,7 @@ int nv_rs_hwmon_register_dev(struct device *dev, u32 gpu_id)
/* Start with valid_mask = 0 (-ENODATA for every sensor) until live telemetry
* arrives; never publish fabricated placeholder readings (#141). */
slot->has_poll = false;
slot->attempted = false;
slot->last_poll_jiffies = 0;
memset(slot->live_stamps, 0, sizeof(slot->live_stamps));
memset(slot->pub_seq, 0, sizeof(slot->pub_seq));
@ -708,6 +714,7 @@ void nv_rs_hwmon_mark_polled(u32 gpu_id)
* exclusively from a publication that carries
* VALID_TEMP. */
WRITE_ONCE(slot->last_poll_jiffies, jiffies);
WRITE_ONCE(slot->attempted, true);
return;
}
}

View File

@ -495,6 +495,18 @@ static void test_default_interval_never_starves_a_healthy_provider(void)
nv_rs_hwmon_unregister_dev(&mock_dev);
}
static void test_cold_start_acquires_immediately(void)
{
reset_slot();
mock_jiffies = 100; /* before the poll interval elapses since boot */
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 20) == 0, "register");
CHECK(read_temp() == 40000, "first read acquires immediately, no sentinel throttle");
CHECK(mock_poll_calls == 1, "exactly one poll");
CHECK(read_temp() == 40000, "fresh reading served");
CHECK(mock_poll_calls == 1, "no second poll inside the window");
nv_rs_hwmon_unregister_dev(&mock_dev);
}
int main(void)
{
failures = 0;
@ -518,6 +530,7 @@ int main(void)
test_long_poll_interval_honors_request_and_exposes_expiry();
test_intermediate_poll_interval_returns_enodata_then_recovers();
test_default_interval_never_starves_a_healthy_provider();
test_cold_start_acquires_immediately();
printf("%s\n", failures ? "FAILED" : "ALL PASSED");
return failures ? 1 : 0;
}