fix(hwmon): invalidate stale telemetry after acquisition failures (#567)

* hwmon: WIP temperature freshness correction with host-fixture tests

WIP contribution addressing #561 (temperature readings stay valid
after acquisition failures). Not ready for merge; submitted for
contributor review.

- Failed acquisitions invalidate the live channels they cover
  (provider bits on failure are untrusted); a failed poll no longer
  extends the freshness window and does not count as a successful
  poll; retries are rate-limited and unsupported providers are
  retried rather than latched.
- Per-channel acquisition stamps bound the served age (2 s cap) and
  per-channel publication sequences provide same-tick ordering
  between polls and mirror publications; reads take validity, stamp
  and value in one critical section.
- Mirror snapshots are incremental: they never invalidate channels
  they do not carry.
- Static limits (temp_max/temp_crit/power_cap) supplied by successful
  polls are applied and preserved across failures.

Regression coverage: host fixture tests/hwmon-mock/ compiles the real
hwmon_adapter.c against a kernel API mock - 71/71 assertions (17
registration + 54 behavioral). Kernel build on 7.0.0-44-generic
headers: module gate 18/18.

Feedback incorporated from dcr000 on #561 (sample timestamps,
failed-poll semantics; the GP100 self-poll provider is intentionally
left out of this patch). Independent review: Codex (2f67d67e) - GO
for these two corrections, with the legacy-provider compatibility
limitation (ENODEV from the GA10x-only provider can invalidate recent
legacy-mirror data) and physical qualification remaining outstanding.

Prepared with AI assistance; operator-reviewed.

* hwmon: retain bounded mirror data when no active acquisition is possible

Addresses CodeRabbit's finding on the draft PR and the legacy-provider
compatibility gap recorded by Codex (2f67d67e): an -ENODEV poll (no
GA10x/GSP owner - mirror-only GPUs such as GP100, or a runtime that
has not started yet) no longer invalidates recent mirror publications.
Such polls are not acquired-channel errors: the mirror samples are
retained and expire by their own per-channel age bound, the provider
is retried every window and is never latched, and a runtime that
appears later acquires normally.

Real acquisition failures (non-ENODEV) keep the immediate
per-channel invalidation, and all freshness, ordering, static-limit
and incremental-mirror semantics are unchanged. Fixture updated:
ENODEV retains recent mirror data within its age bound, expiry still
applies, retries happen per window, and recovery works once the
provider appears (72/72 PASS: 17 registration + 55 behavioral).

* hwmon: align accepted poll intervals with the served-age bound

Addresses the remaining trivial CodeRabbit finding: with poll
intervals above roughly 500 ms, a healthy active provider's reading
expired under the 2 s served-age cap before the next poll could run,
producing artificial -ENODATA periods (merge-base kept the value).
The freshness limit itself is unchanged; instead the accepted maximum
poll interval is now aligned with it: NV_RS_HWMON_POLL_MAX_MS is 500
(the largest interval whose uncapped active age formula, 2*poll+1000,
stays within the 2 s bound), larger configured values are clamped
rather than refused so polling keeps working, and the clamp is
documented in the module parameter description. A static_assert ties
the interval and age constants together at compile time. Regression
tests: oversized intervals are clamped and stay fresh; the default
interval never starves a healthy provider (no re-poll inside the
rate-limit window, exactly one poll in the next). 83/83 PASS.

* hwmon: revert the polling clamp; document long-interval -ENODATA behavior

Per independent review (cdaf3ee0), revert the v7 polling clamp: the
established minimum-interval contract is preserved and hwmon_poll_ms
is again honored up to 60000 ms. The 2 s served-age bound is
unchanged and is never extended to bridge a long polling interval:
with hwmon_poll_ms above the age bound, cached samples legitimately
expire and reads return -ENODATA between eligible acquisitions. This
is now documented in the served-age comment and in the module
parameter description.

Configuration regression tests updated: long intervals honor the
requested minimum (no clamp, no re-poll inside the window) and expose
-ENODATA once the cache expires, then refresh at the next eligible
poll; intermediate intervals (1000 ms) return -ENODATA after expiry
and recover on the next eligible read; the default interval keeps a
healthy provider fresh without extra polls. 92/92 PASS.

* 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.

* hwmon: partial-poll retention, separate loop index, C89 declarations, stale-bit clear

One minimal revision addressing the latest CodeRabbit minor finding and
dcr000's review items at 60112a59:

- Partially available telemetry: a successful poll that did not
  acquire a channel no longer erases a still-fresh cached value for
  that channel (a partial batch must not invalidate an unrelated
  fresh mirror reading); the cached value is cleared only once it is
  already past its served age. Failed acquisitions still invalidate
  immediately.
- Separate loop index in nv_rs_hwmon_update_sample so the channel
  stamp loop no longer reuses the enclosing slot index.
- The sysfs show helpers declare their locals at function top,
  keeping kernels before 6.5 (-Wdeclaration-after-statement)
  buildable.
- Timestamp expiry and wraparound: the first stale read clears the
  channel's valid bit (publication-sequence guarded), so a jiffies
  wraparound cannot revive an ancient reading; a new publication
  restores it. Regression test added with a simulated 32-bit wrap.

Configurable freshness limits remain a separate design decision for
the maintainers (not changed here); the GP100 self-poll provider
stays with dcr000. Fixture 103/103 PASS; kernel-object gate 18/18.

* hwmon: conservative partial-poll semantics, in-lock expiry, wrap-safe age, strict-C89 helpers

Addresses Codex 36419308's four findings:

1. Partial-poll retention reverted: with the existing provider API an
   absent channel cannot be told apart from a failed acquisition, so
   successful polls conservatively invalidate channels they did not
   acquire (v9 error semantics restored); per-channel acquisition/
   error status from the provider remains the documented follow-up.
2. Read-side expiry evaluation and the stale-bit clear now happen
   inside the same sample_lock critical section as the snapshot, so a
   concurrently published sample (poll or mirror) is never discarded.
3. Age comparison uses the wrap-safe unsigned jiffies difference;
   on every supported target (x86_64, arm64) stamps are 64-bit and
   cannot compare young again. Fixture covers observed expiry, a huge
   dormant offset and recovery.
4. The five sysfs helpers now declare locals before any statement;
   verified with -Werror=declaration-after-statement on the guest.

Fixture 102/102 PASS (22 registration + 80 behavioral), also passing
under the strict declaration check; kernel-object gate 18/18.
This commit is contained in:
Jo
2026-12-05 15:30:40 +01:00
committed by GitHub
parent f3862b6300
commit cc6c5bae65
17 changed files with 962 additions and 57 deletions

View File

@ -30,6 +30,39 @@
#define NV_RS_HWMON_MAX_GPUS 32
#define NV_RS_HWMON_POLL_MAX_MS 60000u
/* Served-age bounds. Both are below the 2.5 s acquisition window the
* owner's thermal controller accepts (its stale threshold), so a
* successful hwmon read can satisfy the controller's freshness
* contract. The bounds are never extended to bridge a long polling
* interval: a configured hwmon_poll_ms above the active bound
* legitimately returns -ENODATA between eligible acquisitions rather
* than serving stale data. */
#define NV_RS_HWMON_MIRROR_MAX_AGE_MS 2000u
#define NV_RS_HWMON_ACTIVE_MAX_AGE_MS 2000u
#define NV_RS_HWMON_LIVE_MASK \
(NV_RS_HWMON_VALID_TEMP | NV_RS_HWMON_VALID_POWER | \
NV_RS_HWMON_VALID_VOLT | NV_RS_HWMON_VALID_GPC_CLK | \
NV_RS_HWMON_VALID_MEM_CLK | NV_RS_HWMON_VALID_GPU_UTIL | \
NV_RS_HWMON_VALID_MEM_UTIL | NV_RS_HWMON_VALID_PSTATE)
enum nv_rs_hwmon_live_channel {
NV_RS_HWMON_LIVE_TEMP = 0,
NV_RS_HWMON_LIVE_POWER,
NV_RS_HWMON_LIVE_VOLT,
NV_RS_HWMON_LIVE_GPC_CLK,
NV_RS_HWMON_LIVE_MEM_CLK,
NV_RS_HWMON_LIVE_GPU_UTIL,
NV_RS_HWMON_LIVE_MEM_UTIL,
NV_RS_HWMON_LIVE_PSTATE,
NV_RS_HWMON_LIVE_COUNT,
};
static const u32 nv_rs_hwmon_live_bits[NV_RS_HWMON_LIVE_COUNT] = {
NV_RS_HWMON_VALID_TEMP, NV_RS_HWMON_VALID_POWER,
NV_RS_HWMON_VALID_VOLT, NV_RS_HWMON_VALID_GPC_CLK,
NV_RS_HWMON_VALID_MEM_CLK, NV_RS_HWMON_VALID_GPU_UTIL,
NV_RS_HWMON_VALID_MEM_UTIL, NV_RS_HWMON_VALID_PSTATE,
};
static_assert(sizeof(struct nv_rs_hwmon_sample) == 56,
"nv_rs_hwmon_sample ABI mismatch with Rust HwmonSample");
@ -46,7 +79,7 @@ static_assert(sizeof(struct nv_rs_hwmon_sample) == 56,
static unsigned int hwmon_poll_ms = 500;
module_param(hwmon_poll_ms, uint, 0644);
MODULE_PARM_DESC(hwmon_poll_ms,
"Minimum interval (ms) between active GSP-RM hwmon polls on sysfs read (default 500, 0 = passive mirror only)");
"Minimum interval (ms) between active GSP-RM hwmon polls on sysfs read (default 500, 0 = passive mirror only; intervals above the 2 s served-age bound can return -ENODATA between polls once cached samples expire - freshness is never extended to bridge the gap)");
extern void nv_rs_rmapi_node_lock(void);
extern void nv_rs_rmapi_node_unlock(void);
@ -61,7 +94,10 @@ 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];
atomic_t valid_mask;
atomic_t temp_mc;
atomic_t temp_max_mc;
@ -118,15 +154,43 @@ static void nv_rs_hwmon_apply_sample(struct nv_rs_hwmon_slot *slot,
atomic_or((int)m, &slot->valid_mask);
}
static unsigned long nv_rs_hwmon_live_ttl_jiffies(unsigned int poll_ms)
{
unsigned long active_ms;
if (poll_ms == 0)
return msecs_to_jiffies(NV_RS_HWMON_MIRROR_MAX_AGE_MS);
if (poll_ms > NV_RS_HWMON_POLL_MAX_MS)
poll_ms = NV_RS_HWMON_POLL_MAX_MS;
active_ms = 2u * poll_ms + 1000u;
if (active_ms > NV_RS_HWMON_ACTIVE_MAX_AGE_MS)
active_ms = NV_RS_HWMON_ACTIVE_MAX_AGE_MS;
return msecs_to_jiffies(active_ms);
}
/* A live reading is only served while the last publication that
* carried its channel is younger than the bounded served-age window.
* The unsigned jiffies difference is wrap-safe over the full counter
* width; on every supported target (x86_64, arm64) that is 64 bits
* of jiffies, so an ancient stamp can never compare young. */
static bool nv_rs_hwmon_age_fresh(const struct nv_rs_hwmon_slot *slot,
unsigned long stamp)
{
unsigned long ttl = nv_rs_hwmon_live_ttl_jiffies(READ_ONCE(hwmon_poll_ms));
return (unsigned long)(jiffies - stamp) < ttl;
}
static void nv_rs_hwmon_maybe_refresh(struct nv_rs_hwmon_slot *slot)
{
unsigned int poll_ms = READ_ONCE(hwmon_poll_ms);
unsigned long ttl;
unsigned long flags;
struct nv_rs_hwmon_sample sample;
u64 seq_before;
u64 seq_before[NV_RS_HWMON_LIVE_COUNT];
u32 gpu_id;
int rc;
int i;
if (!slot || poll_ms == 0)
return;
@ -134,12 +198,17 @@ 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);
if (READ_ONCE(slot->has_poll) &&
/* 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 (READ_ONCE(slot->has_poll) &&
if (READ_ONCE(slot->attempted) &&
time_before(jiffies, READ_ONCE(slot->last_poll_jiffies) + ttl)) {
mutex_unlock(&slot->poll_lock);
return;
@ -150,19 +219,72 @@ static void nv_rs_hwmon_maybe_refresh(struct nv_rs_hwmon_slot *slot)
/* Lock order: poll_lock -> rmapi_node_lock -> sample_lock. */
nv_rs_rmapi_node_lock();
spin_lock_irqsave(&slot->sample_lock, flags);
seq_before = slot->mirror_seq;
for (i = 0; i < NV_RS_HWMON_LIVE_COUNT; i++)
seq_before[i] = slot->pub_seq[i];
spin_unlock_irqrestore(&slot->sample_lock, flags);
rc = nv_rs_ga10x_hwmon_poll(gpu_id, &sample);
if (rc == 0) {
spin_lock_irqsave(&slot->sample_lock, flags);
if (slot->mirror_seq == seq_before)
nv_rs_hwmon_apply_sample(slot, &sample);
spin_unlock_irqrestore(&slot->sample_lock, flags);
if (rc == -ENODEV) {
/* No active acquisition attempt was possible: no GA10x/GSP
* owner exists, either because this GPU is mirror-only (e.g.
* GP100) or because the runtime has not started yet. This is
* not an acquired-channel error: recent mirror publications
* are retained and expire by their own per-channel age
* bound. The provider is retried on the next window and is
* never latched, so a runtime that appears later acquires
* normally. */
nv_rs_rmapi_node_unlock();
WRITE_ONCE(slot->last_poll_jiffies, jiffies);
WRITE_ONCE(slot->attempted, true);
mutex_unlock(&slot->poll_lock);
return;
}
/* The attempted acquisition is authoritative per channel, with
* publication ordering by per-channel publication sequence: any
* publication that touched a channel while the poll was in flight
* (including in the same jiffy as the poll start) wins for that
* channel. A failed result invalidates every live channel it
* covers. A successful result applies the channels it carries and
* conservatively invalidates the channels it did not acquire:
* with the current provider API an absent channel cannot be told
* apart from a failed acquisition, so error semantics are
* preserved until the provider supplies per-channel
* acquisition/error status. Provider sample contents are never
* consulted on failure. Static limit attributes are preserved
* across failures and are applied from successful poll results
* as before. */
spin_lock_irqsave(&slot->sample_lock, flags);
for (i = 0; i < NV_RS_HWMON_LIVE_COUNT; i++) {
u32 bit = nv_rs_hwmon_live_bits[i];
if (slot->pub_seq[i] != seq_before[i])
continue;
if (rc == 0 && (sample.valid_mask & bit)) {
struct nv_rs_hwmon_sample s = sample;
s.valid_mask = bit;
nv_rs_hwmon_apply_sample(slot, &s);
slot->live_stamps[i] = jiffies;
} else {
atomic_andnot(bit, &slot->valid_mask);
}
}
if (rc == 0) {
struct nv_rs_hwmon_sample s = sample;
s.valid_mask &= (NV_RS_HWMON_VALID_TEMP_MAX |
NV_RS_HWMON_VALID_TEMP_CRIT |
NV_RS_HWMON_VALID_POWER_CAP);
nv_rs_hwmon_apply_sample(slot, &s);
}
spin_unlock_irqrestore(&slot->sample_lock, flags);
nv_rs_rmapi_node_unlock();
/* Rate-limit the next attempt regardless of the outcome. */
WRITE_ONCE(slot->last_poll_jiffies, jiffies);
WRITE_ONCE(slot->has_poll, true);
WRITE_ONCE(slot->attempted, true);
if (rc == 0)
WRITE_ONCE(slot->has_poll, true);
mutex_unlock(&slot->poll_lock);
}
@ -192,6 +314,68 @@ static umode_t nv_rs_hwmon_is_visible(const void *drvdata,
}
}
/* Coherent live-channel read: validity bit, acquisition stamp and
* value are taken in one critical section so a publication can never
* pair an old value with a new stamp. The age is then evaluated
* against the channel's own stamp. */
static int nv_rs_hwmon_read_live(struct nv_rs_hwmon_slot *slot, int idx,
long *val)
{
u32 bit = nv_rs_hwmon_live_bits[idx];
unsigned long stamp;
unsigned long flags;
u32 mask;
long value = 0;
nv_rs_hwmon_maybe_refresh(slot);
spin_lock_irqsave(&slot->sample_lock, flags);
mask = (u32)atomic_read(&slot->valid_mask);
stamp = slot->live_stamps[idx];
switch (idx) {
case NV_RS_HWMON_LIVE_TEMP:
value = atomic_read(&slot->temp_mc);
break;
case NV_RS_HWMON_LIVE_POWER:
value = (long)atomic64_read(&slot->power_uw);
break;
case NV_RS_HWMON_LIVE_VOLT:
value = atomic_read(&slot->volt_mv);
break;
case NV_RS_HWMON_LIVE_GPC_CLK:
value = atomic_read(&slot->gpc_mhz);
break;
case NV_RS_HWMON_LIVE_MEM_CLK:
value = atomic_read(&slot->mem_mhz);
break;
case NV_RS_HWMON_LIVE_GPU_UTIL:
value = atomic_read(&slot->gpu_util_pct);
break;
case NV_RS_HWMON_LIVE_MEM_UTIL:
value = atomic_read(&slot->mem_util_pct);
break;
case NV_RS_HWMON_LIVE_PSTATE:
value = atomic_read(&slot->pstate);
break;
}
if (!(mask & bit) || !nv_rs_hwmon_age_fresh(slot, stamp)) {
/* A stale channel stays invalid: clear its valid bit so a
* jiffies wraparound cannot revive an ancient reading. The
* clear happens inside the same critical section that
* observed the stale age, so a concurrently published sample
* (poll or mirror) is never discarded. */
if (mask & bit)
atomic_andnot(bit, &slot->valid_mask);
spin_unlock_irqrestore(&slot->sample_lock, flags);
return -ENODATA;
}
spin_unlock_irqrestore(&slot->sample_lock, flags);
if (bit == NV_RS_HWMON_VALID_POWER && value <= 0)
return -ENODATA;
*val = value;
return 0;
}
static int nv_rs_hwmon_read(struct device *dev,
enum hwmon_sensor_types type,
u32 attr,
@ -203,17 +387,13 @@ static int nv_rs_hwmon_read(struct device *dev,
if (!slot || channel != 0)
return -EINVAL;
nv_rs_hwmon_maybe_refresh(slot);
mask = (u32)atomic_read(&slot->valid_mask);
switch (type) {
case hwmon_temp:
if (attr == hwmon_temp_input) {
if (!(mask & NV_RS_HWMON_VALID_TEMP))
return -ENODATA;
*val = atomic_read(&slot->temp_mc);
return 0;
}
if (attr == hwmon_temp_input)
return nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_TEMP, val);
nv_rs_hwmon_maybe_refresh(slot);
mask = (u32)atomic_read(&slot->valid_mask);
if (attr == hwmon_temp_max) {
if (!(mask & NV_RS_HWMON_VALID_TEMP_MAX))
return -ENODATA;
@ -228,17 +408,10 @@ static int nv_rs_hwmon_read(struct device *dev,
}
return -EOPNOTSUPP;
case hwmon_power:
if (attr == hwmon_power_average) {
s64 uw;
if (!(mask & NV_RS_HWMON_VALID_POWER))
return -ENODATA;
uw = atomic64_read(&slot->power_uw);
if (uw <= 0)
return -ENODATA;
*val = (long)uw;
return 0;
}
if (attr == hwmon_power_average)
return nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_POWER, val);
nv_rs_hwmon_maybe_refresh(slot);
mask = (u32)atomic_read(&slot->valid_mask);
if (attr == hwmon_power_cap) {
s64 cap;
@ -252,12 +425,8 @@ static int nv_rs_hwmon_read(struct device *dev,
}
return -EOPNOTSUPP;
case hwmon_in:
if (attr == hwmon_in_input) {
if (!(mask & NV_RS_HWMON_VALID_VOLT))
return -ENODATA;
*val = atomic_read(&slot->volt_mv);
return 0;
}
if (attr == hwmon_in_input)
return nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_VOLT, val);
return -EOPNOTSUPP;
default:
return -EOPNOTSUPP;
@ -286,13 +455,16 @@ static ssize_t gpc_clock_mhz_show(struct device *dev,
char *buf)
{
struct nv_rs_hwmon_slot *slot = dev_get_drvdata(dev);
long v;
int rc;
(void)attr;
if (!slot)
return -EINVAL;
nv_rs_hwmon_maybe_refresh(slot);
if (!((u32)atomic_read(&slot->valid_mask) & NV_RS_HWMON_VALID_GPC_CLK))
return -ENODATA;
return sysfs_emit(buf, "%d\n", atomic_read(&slot->gpc_mhz));
rc = nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_GPC_CLK, &v);
if (rc)
return rc;
return sysfs_emit(buf, "%d\n", (int)v);
}
static DEVICE_ATTR_RO(gpc_clock_mhz);
@ -301,13 +473,16 @@ static ssize_t mem_clock_mhz_show(struct device *dev,
char *buf)
{
struct nv_rs_hwmon_slot *slot = dev_get_drvdata(dev);
long v;
int rc;
(void)attr;
if (!slot)
return -EINVAL;
nv_rs_hwmon_maybe_refresh(slot);
if (!((u32)atomic_read(&slot->valid_mask) & NV_RS_HWMON_VALID_MEM_CLK))
return -ENODATA;
return sysfs_emit(buf, "%d\n", atomic_read(&slot->mem_mhz));
rc = nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_MEM_CLK, &v);
if (rc)
return rc;
return sysfs_emit(buf, "%d\n", (int)v);
}
static DEVICE_ATTR_RO(mem_clock_mhz);
@ -316,13 +491,16 @@ static ssize_t gpu_util_pct_show(struct device *dev,
char *buf)
{
struct nv_rs_hwmon_slot *slot = dev_get_drvdata(dev);
long v;
int rc;
(void)attr;
if (!slot)
return -EINVAL;
nv_rs_hwmon_maybe_refresh(slot);
if (!((u32)atomic_read(&slot->valid_mask) & NV_RS_HWMON_VALID_GPU_UTIL))
return -ENODATA;
return sysfs_emit(buf, "%d\n", atomic_read(&slot->gpu_util_pct));
rc = nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_GPU_UTIL, &v);
if (rc)
return rc;
return sysfs_emit(buf, "%d\n", (int)v);
}
static DEVICE_ATTR_RO(gpu_util_pct);
@ -331,13 +509,16 @@ static ssize_t mem_util_pct_show(struct device *dev,
char *buf)
{
struct nv_rs_hwmon_slot *slot = dev_get_drvdata(dev);
long v;
int rc;
(void)attr;
if (!slot)
return -EINVAL;
nv_rs_hwmon_maybe_refresh(slot);
if (!((u32)atomic_read(&slot->valid_mask) & NV_RS_HWMON_VALID_MEM_UTIL))
return -ENODATA;
return sysfs_emit(buf, "%d\n", atomic_read(&slot->mem_util_pct));
rc = nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_MEM_UTIL, &v);
if (rc)
return rc;
return sysfs_emit(buf, "%d\n", (int)v);
}
static DEVICE_ATTR_RO(mem_util_pct);
@ -346,13 +527,16 @@ static ssize_t pstate_show(struct device *dev,
char *buf)
{
struct nv_rs_hwmon_slot *slot = dev_get_drvdata(dev);
long v;
int rc;
(void)attr;
if (!slot)
return -EINVAL;
nv_rs_hwmon_maybe_refresh(slot);
if (!((u32)atomic_read(&slot->valid_mask) & NV_RS_HWMON_VALID_PSTATE))
return -ENODATA;
return sysfs_emit(buf, "P%d\n", atomic_read(&slot->pstate));
rc = nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_PSTATE, &v);
if (rc)
return rc;
return sysfs_emit(buf, "P%d\n", (int)v);
}
static DEVICE_ATTR_RO(pstate);
@ -397,7 +581,10 @@ 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));
atomic_set(&slot->valid_mask, 0);
atomic_set(&slot->temp_mc, 0);
atomic_set(&slot->temp_max_mc, 0);
@ -510,6 +697,7 @@ void nv_rs_hwmon_update(u32 gpu_id,
void nv_rs_hwmon_update_sample(u32 gpu_id, const struct nv_rs_hwmon_sample *sample)
{
int i;
int c;
if (!sample || !sample->valid_mask)
return;
@ -520,6 +708,16 @@ void nv_rs_hwmon_update_sample(u32 gpu_id, const struct nv_rs_hwmon_sample *samp
if (READ_ONCE(slot->gpu_id) == gpu_id) {
unsigned long flags;
spin_lock_irqsave(&slot->sample_lock, flags);
/* Mirror snapshots are incremental: they set the
* channels they carry and stamp those channels only;
* they never invalidate channels they do not carry,
* so an unrelated field update cannot age out a fresh
* temperature. */
for (c = 0; c < NV_RS_HWMON_LIVE_COUNT; c++)
if (sample->valid_mask & nv_rs_hwmon_live_bits[c]) {
slot->live_stamps[c] = jiffies;
slot->pub_seq[c]++;
}
slot->mirror_seq++;
nv_rs_hwmon_apply_sample(slot, sample);
spin_unlock_irqrestore(&slot->sample_lock, flags);
@ -538,8 +736,12 @@ void nv_rs_hwmon_mark_polled(u32 gpu_id)
if (READ_ONCE(slot->in_use)) {
smp_rmb();
if (READ_ONCE(slot->gpu_id) == gpu_id) {
/* Rate-limit only: a poll completion does not claim a
* fresh temperature. Temperature validity and age come
* exclusively from a publication that carries
* VALID_TEMP. */
WRITE_ONCE(slot->last_poll_jiffies, jiffies);
WRITE_ONCE(slot->has_poll, true);
WRITE_ONCE(slot->attempted, true);
return;
}
}

560
tests/hwmon-mock/fixture.c Normal file
View File

@ -0,0 +1,560 @@
/* Host-fixture behavioral tests for kernel-open/nvidia/hwmon_adapter.c.
* Exercises the freshness semantics of the corrected implementation
* without a GPU or a loaded module: successful equal temperatures, first
* and repeated acquisition errors, partial samples without temperature,
* recovery, passive-mirror expiry, cold retry throttling, a concurrent
* mirror publication racing a failed poll, and preservation of static
* limit attributes.
*
* Build (host): cc -std=gnu11 -Itests/hwmon-mock tests/hwmon-mock/fixture.c -o hwmon-fixture
* Run: ./hwmon-fixture
*/
#include <stdio.h>
#include "linux/linux_mock.h"
#include "../../kernel-open/nvidia/hwmon_adapter.c"
static struct hwmon_device_mock mock_hwmon;
static struct nv_rs_hwmon_sample mock_sample;
static int mock_poll_rc;
static int mock_poll_calls;
static int mock_poll_publishes; /* fresh mirror temperature inside the poll */
static int mock_poll_publishes_limits; /* limits-only mirror inside the poll */
static int mock_poll_publishes_power; /* power-only mirror inside the poll */
static int mock_fail_with_data; /* failed poll still fills the sample */
void nv_rs_rmapi_node_lock(void) {}
void nv_rs_rmapi_node_unlock(void) {}
int nv_rs_ga10x_hwmon_poll(u32 gpu_id, struct nv_rs_hwmon_sample *out)
{
mock_poll_calls++;
/* No artificial clock advance: publications made from inside the
* poll happen in the same jiffy as the poll start, exercising the
* per-channel publication-sequence ordering. */
if (mock_poll_publishes) {
struct nv_rs_hwmon_sample m;
memset(&m, 0, sizeof(m));
m.temp_mc = 42000;
m.valid_mask = NV_RS_HWMON_VALID_TEMP;
nv_rs_hwmon_update_sample(gpu_id, &m);
}
if (mock_poll_publishes_limits) {
struct nv_rs_hwmon_sample m;
memset(&m, 0, sizeof(m));
m.power_cap_uw = 250000000u;
m.valid_mask = NV_RS_HWMON_VALID_POWER_CAP;
nv_rs_hwmon_update_sample(gpu_id, &m);
}
if (mock_poll_publishes_power) {
struct nv_rs_hwmon_sample m;
memset(&m, 0, sizeof(m));
m.power_uw = 28000000u;
m.valid_mask = NV_RS_HWMON_VALID_POWER;
nv_rs_hwmon_update_sample(gpu_id, &m);
}
/* A failed acquisition carries no trustworthy sample data; the
* fail-with-data flag models a broken provider that does. */
if (mock_poll_rc == 0 || mock_fail_with_data)
*out = mock_sample;
return mock_poll_rc;
}
struct device *hwmon_device_register_with_info(struct device *dev, const char *name,
const void *drvdata, const struct hwmon_chip_info *chip,
const struct attribute_group *const *groups)
{
(void)name; (void)chip; (void)groups;
mock_hwmon.registered = 1;
dev_set_drvdata(dev, (void *)drvdata);
return (struct device *)&mock_hwmon;
}
void hwmon_device_unregister(struct device *hwmon)
{
(void)hwmon;
mock_hwmon.registered = 0;
}
static struct device mock_dev;
static long read_temp(void)
{
long val = 0;
int rc = nv_rs_hwmon_read(&mock_dev, hwmon_temp, hwmon_temp_input, 0, &val);
return rc ? rc : val;
}
static long read_power(void)
{
long val = 0;
int rc = nv_rs_hwmon_read(&mock_dev, hwmon_power, hwmon_power_average, 0, &val);
return rc ? rc : val;
}
static long read_temp_max(void)
{
long val = 0;
int rc = nv_rs_hwmon_read(&mock_dev, hwmon_temp, hwmon_temp_max, 0, &val);
return rc ? rc : val;
}
static long read_temp_crit(void)
{
long val = 0;
int rc = nv_rs_hwmon_read(&mock_dev, hwmon_temp, hwmon_temp_crit, 0, &val);
return rc ? rc : val;
}
static long read_power_cap(void)
{
long val = 0;
int rc = nv_rs_hwmon_read(&mock_dev, hwmon_power, hwmon_power_cap, 0, &val);
return rc ? rc : val;
}
static void sample_temp(int mc)
{
memset(&mock_sample, 0, sizeof(mock_sample));
mock_sample.temp_mc = mc;
mock_sample.valid_mask = NV_RS_HWMON_VALID_TEMP;
}
static void sample_power_only(void)
{
memset(&mock_sample, 0, sizeof(mock_sample));
mock_sample.power_uw = 28000000u;
mock_sample.valid_mask = NV_RS_HWMON_VALID_POWER;
}
static void reset_slot(void)
{
memset(&mock_dev, 0, sizeof(mock_dev));
mock_jiffies = 1000;
mock_poll_rc = 0;
mock_poll_calls = 0;
mock_poll_publishes = 0;
mock_poll_publishes_limits = 0;
mock_poll_publishes_power = 0;
mock_fail_with_data = 0;
mock_set_hwmon_poll_ms(500);
sample_temp(40000);
}
static int failures;
#define CHECK(cond, what) \
do { \
if (cond) { \
printf("PASS %s\n", what); \
} else { \
printf("FAIL %s\n", what); \
failures++; \
} \
} while (0)
static void test_successful_equal_temperatures(void)
{
reset_slot();
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 1) == 0, "register");
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "first successful acquisition");
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "equal consecutive temperature stays valid");
nv_rs_hwmon_unregister_dev(&mock_dev);
}
static void test_first_read_error_and_recovery(void)
{
reset_slot();
mock_poll_rc = -5;
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 2) == 0, "register");
mock_jiffies += 1000;
CHECK(read_temp() == -ENODATA, "first failed poll reports -ENODATA immediately");
mock_poll_rc = 0;
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "recovery after a failed poll");
nv_rs_hwmon_unregister_dev(&mock_dev);
}
static void test_repeated_read_errors(void)
{
reset_slot();
mock_poll_rc = -5;
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 3) == 0, "register");
mock_jiffies += 1000;
CHECK(read_temp() == -ENODATA, "first error");
mock_jiffies += 1000;
CHECK(read_temp() == -ENODATA, "repeated error");
mock_jiffies += 1000;
CHECK(read_power() == -ENODATA, "other live channels invalidated too");
nv_rs_hwmon_unregister_dev(&mock_dev);
}
static void test_partial_sample_without_temperature(void)
{
reset_slot();
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 4) == 0, "register");
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "baseline temperature");
mock_jiffies += 1000;
sample_power_only();
CHECK(read_power() == 28000000, "power from partial sample");
CHECK(read_temp() == -ENODATA, "missing temperature invalidates the channel");
mock_jiffies += 1000;
sample_temp(41000);
CHECK(read_temp() == 41000, "temperature recovers");
nv_rs_hwmon_unregister_dev(&mock_dev);
}
static void test_passive_mirror_expiry(void)
{
struct nv_rs_hwmon_sample m;
reset_slot();
mock_set_hwmon_poll_ms(0);
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 5) == 0, "register");
memset(&m, 0, sizeof(m));
m.temp_mc = 40000;
m.valid_mask = NV_RS_HWMON_VALID_TEMP;
nv_rs_hwmon_update_sample(5, &m);
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "mirror reading within the maximum age");
mock_jiffies += 6000;
CHECK(read_temp() == -ENODATA, "mirror reading expires past the maximum age");
nv_rs_hwmon_unregister_dev(&mock_dev);
mock_set_hwmon_poll_ms(500);
}
static void test_cold_retry_throttling(void)
{
reset_slot();
mock_poll_rc = -5;
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 6) == 0, "register");
mock_jiffies += 1000;
CHECK(read_temp() == -ENODATA, "cold first failure");
CHECK(mock_poll_calls == 1, "one attempt so far");
CHECK(read_temp() == -ENODATA, "immediate retry is throttled");
CHECK(mock_poll_calls == 1, "no new attempt inside the rate-limit window");
mock_jiffies += 1000;
CHECK(read_temp() == -ENODATA, "retry after the window");
CHECK(mock_poll_calls == 2, "attempt count advanced exactly once");
nv_rs_hwmon_unregister_dev(&mock_dev);
}
static void test_concurrent_mirror_beats_failed_poll(void)
{
reset_slot();
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 7) == 0, "register");
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "baseline");
mock_jiffies += 1000;
mock_poll_publishes = 1;
mock_poll_rc = -5;
CHECK(read_temp() == 42000, "fresher mirror sample survives the failed poll");
mock_poll_publishes = 0;
nv_rs_hwmon_unregister_dev(&mock_dev);
}
static void test_static_limits_survive_failures(void)
{
reset_slot();
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 8) == 0, "register");
nv_rs_hwmon_set_limits(8, 250000, 90, 100);
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "baseline temperature");
mock_poll_rc = -5;
mock_jiffies += 1000;
CHECK(read_temp() == -ENODATA, "live temperature invalidated");
CHECK(read_temp_max() == 90000, "static temp_max preserved");
CHECK(read_temp_crit() == 100000, "static temp_crit preserved");
CHECK(read_power_cap() == 250000000, "static power_cap preserved");
nv_rs_hwmon_unregister_dev(&mock_dev);
}
static void test_limits_only_mirror_does_not_suppress_invalidation(void)
{
reset_slot();
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 9) == 0, "register");
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "baseline");
mock_jiffies += 1000;
mock_poll_publishes_limits = 1;
mock_poll_rc = -5;
CHECK(read_temp() == -ENODATA, "limits-only publication cannot suppress invalidation");
CHECK(read_power_cap() == 250000000, "the limits publication itself landed");
mock_poll_publishes_limits = 0;
nv_rs_hwmon_unregister_dev(&mock_dev);
}
static void test_temperature_age_is_independent_of_other_telemetry(void)
{
struct nv_rs_hwmon_sample m;
reset_slot();
mock_set_hwmon_poll_ms(0);
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 10) == 0, "register");
memset(&m, 0, sizeof(m));
m.temp_mc = 40000;
m.valid_mask = NV_RS_HWMON_VALID_TEMP;
nv_rs_hwmon_update_sample(10, &m);
mock_jiffies += 1000;
memset(&m, 0, sizeof(m));
m.power_cap_uw = 250000000u;
m.valid_mask = NV_RS_HWMON_VALID_POWER_CAP;
nv_rs_hwmon_update_sample(10, &m);
CHECK(read_temp() == 40000, "limits-only publication leaves temperature readable");
mock_jiffies += 1600;
CHECK(read_temp() == -ENODATA, "temperature expired by its own age, bit still set");
CHECK(read_power_cap() == 250000000, "limits publication unaffected");
nv_rs_hwmon_unregister_dev(&mock_dev);
mock_set_hwmon_poll_ms(500);
}
static void test_unsupported_polling_keeps_bounded_mirror_and_recovers(void)
{
struct nv_rs_hwmon_sample m;
reset_slot();
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 11) == 0, "register");
memset(&m, 0, sizeof(m));
m.temp_mc = 40000;
m.valid_mask = NV_RS_HWMON_VALID_TEMP;
nv_rs_hwmon_update_sample(11, &m);
mock_poll_rc = -ENODEV;
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "recent mirror sample retained on ENODEV");
CHECK(mock_poll_calls == 1, "one unsupported attempt");
mock_jiffies += 500;
CHECK(read_temp() == 40000, "mirror sample retained within its age bound");
CHECK(mock_poll_calls == 2, "unsupported provider is retried, not latched");
mock_jiffies += 1000;
CHECK(read_temp() == -ENODATA, "mirror expiry still applies without fresh publications");
mock_poll_rc = 0;
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "recovery once the provider appears");
CHECK(mock_poll_calls == 4, "active poll resumed");
nv_rs_hwmon_unregister_dev(&mock_dev);
}
static void test_mark_polled_never_claims_temperature_freshness(void)
{
struct nv_rs_hwmon_sample m;
reset_slot();
mock_set_hwmon_poll_ms(0);
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 12) == 0, "register");
memset(&m, 0, sizeof(m));
m.temp_mc = 40000;
m.valid_mask = NV_RS_HWMON_VALID_TEMP;
nv_rs_hwmon_update_sample(12, &m);
mock_jiffies += 300;
nv_rs_hwmon_mark_polled(12);
mock_jiffies += 2000;
CHECK(read_temp() == -ENODATA, "mark_polled does not refresh temperature age");
nv_rs_hwmon_unregister_dev(&mock_dev);
mock_set_hwmon_poll_ms(500);
}
static void test_failed_poll_ignores_untrusted_sample_bits(void)
{
reset_slot();
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 13) == 0, "register");
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "baseline");
mock_jiffies += 1000;
mock_poll_rc = -5;
mock_fail_with_data = 1;
CHECK(read_temp() == -ENODATA, "untrusted bits on failure cannot keep temperature alive");
nv_rs_hwmon_unregister_dev(&mock_dev);
}
static void test_mirror_is_incremental(void)
{
struct nv_rs_hwmon_sample m;
reset_slot();
mock_set_hwmon_poll_ms(0);
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 14) == 0, "register");
memset(&m, 0, sizeof(m));
m.temp_mc = 40000;
m.valid_mask = NV_RS_HWMON_VALID_TEMP;
nv_rs_hwmon_update_sample(14, &m);
mock_jiffies += 500;
memset(&m, 0, sizeof(m));
m.power_uw = 28000000u;
m.valid_mask = NV_RS_HWMON_VALID_POWER;
nv_rs_hwmon_update_sample(14, &m);
CHECK(read_temp() == 40000, "unrelated mirror update keeps temperature valid");
CHECK(read_power() == 28000000, "the incremental power update landed");
nv_rs_hwmon_unregister_dev(&mock_dev);
mock_set_hwmon_poll_ms(500);
}
static void test_non_temp_mirror_does_not_block_poll_temperature(void)
{
reset_slot();
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 15) == 0, "register");
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "baseline");
mock_jiffies += 1000;
mock_poll_publishes_power = 1;
CHECK(read_temp() == 40000, "power-only mirror cannot suppress the polled temperature");
mock_poll_publishes_power = 0;
nv_rs_hwmon_unregister_dev(&mock_dev);
}
static void test_poll_stale_temp_does_not_overwrite_newer_mirror_temp(void)
{
reset_slot();
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 16) == 0, "register");
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "baseline");
mock_jiffies += 1000;
mock_poll_publishes = 1; /* same-jiffy mirror temp 42000 during the poll */
sample_temp(41000); /* the poll result itself carries 41000 */
CHECK(read_temp() == 42000, "same-jiffy mirror temperature wins over the poll result");
mock_poll_publishes = 0;
nv_rs_hwmon_unregister_dev(&mock_dev);
}
static void test_static_limits_arrive_via_successful_polls(void)
{
reset_slot();
memset(&mock_sample, 0, sizeof(mock_sample));
mock_sample.temp_mc = 40000;
mock_sample.temp_max_mc = 90000;
mock_sample.temp_crit_mc = 100000;
mock_sample.power_cap_uw = 250000000u;
mock_sample.valid_mask = NV_RS_HWMON_VALID_TEMP |
NV_RS_HWMON_VALID_TEMP_MAX |
NV_RS_HWMON_VALID_TEMP_CRIT |
NV_RS_HWMON_VALID_POWER_CAP;
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 17) == 0, "register");
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "live temperature from the poll");
CHECK(read_temp_max() == 90000, "temp_max published by the poll provider");
CHECK(read_temp_crit() == 100000, "temp_crit published by the poll provider");
CHECK(read_power_cap() == 250000000, "power_cap published by the poll provider");
mock_poll_rc = -5;
mock_jiffies += 1000;
CHECK(read_temp() == -ENODATA, "failed poll invalidates live data");
CHECK(read_temp_max() == 90000, "static limits survive the failed poll");
CHECK(read_power_cap() == 250000000, "power_cap survives the failed poll");
nv_rs_hwmon_unregister_dev(&mock_dev);
}
static void test_long_poll_interval_honors_request_and_exposes_expiry(void)
{
reset_slot();
mock_set_hwmon_poll_ms(60000);
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 18) == 0, "register");
mock_jiffies += 61000;
CHECK(read_temp() == 40000, "baseline acquisition after the cold-start window");
CHECK(mock_poll_calls == 1, "one poll");
mock_jiffies += 3000;
CHECK(read_temp() == -ENODATA, "expired cache reports -ENODATA inside a long window");
CHECK(mock_poll_calls == 1, "requested minimum interval is honored, not clamped");
mock_jiffies += 58000;
CHECK(read_temp() == 40000, "next eligible poll refreshes");
CHECK(mock_poll_calls == 2, "poll advanced after the configured window");
nv_rs_hwmon_unregister_dev(&mock_dev);
mock_set_hwmon_poll_ms(500);
}
static void test_intermediate_poll_interval_returns_enodata_then_recovers(void)
{
reset_slot();
mock_set_hwmon_poll_ms(1000);
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 19) == 0, "register");
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "acquisition");
mock_jiffies += 500;
CHECK(read_temp() == 40000, "fresh inside the interval and the age bound");
CHECK(mock_poll_calls == 1, "no poll inside the requested window");
mock_jiffies += 2000;
CHECK(read_temp() == 40000, "expiry triggers a poll at the next eligible read");
CHECK(mock_poll_calls == 2, "poll advanced exactly once");
nv_rs_hwmon_unregister_dev(&mock_dev);
mock_set_hwmon_poll_ms(500);
}
static void test_default_interval_never_starves_a_healthy_provider(void)
{
reset_slot();
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 19) == 0, "register");
mock_jiffies += 1000;
CHECK(read_temp() == 40000, "first acquisition");
CHECK(mock_poll_calls == 1, "one poll so far");
mock_jiffies += 400;
CHECK(read_temp() == 40000, "reading inside the poll window stays valid");
CHECK(mock_poll_calls == 1, "no poll inside the rate-limit window");
mock_jiffies += 200;
CHECK(read_temp() == 40000, "next window re-polls and stays fresh");
CHECK(mock_poll_calls == 2, "poll advanced exactly once");
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);
}
static void test_stale_and_dormant_channels_never_serve_beyond_the_bound(void)
{
struct nv_rs_hwmon_sample m;
reset_slot();
mock_set_hwmon_poll_ms(0);
CHECK(nv_rs_hwmon_register_dev(&mock_dev, 20) == 0, "register");
memset(&m, 0, sizeof(m));
m.temp_mc = 40000;
m.valid_mask = NV_RS_HWMON_VALID_TEMP;
nv_rs_hwmon_update_sample(20, &m);
mock_jiffies += 100;
CHECK(read_temp() == 40000, "fresh reading");
mock_jiffies += 4000;
CHECK(read_temp() == -ENODATA, "observed expiry invalidates the channel");
mock_jiffies += 9223372036854775808UL; /* a huge dormant offset */
CHECK(read_temp() == -ENODATA, "cleared channel cannot revive after a dormant period");
nv_rs_hwmon_update_sample(20, &m);
CHECK(read_temp() == 40000, "a new publication restores the channel");
nv_rs_hwmon_unregister_dev(&mock_dev);
mock_set_hwmon_poll_ms(500);
}
int main(void)
{
failures = 0;
test_successful_equal_temperatures();
test_first_read_error_and_recovery();
test_repeated_read_errors();
test_partial_sample_without_temperature();
test_passive_mirror_expiry();
test_cold_retry_throttling();
test_concurrent_mirror_beats_failed_poll();
test_static_limits_survive_failures();
test_limits_only_mirror_does_not_suppress_invalidation();
test_temperature_age_is_independent_of_other_telemetry();
test_unsupported_polling_keeps_bounded_mirror_and_recovers();
test_mark_polled_never_claims_temperature_freshness();
test_failed_poll_ignores_untrusted_sample_bits();
test_mirror_is_incremental();
test_non_temp_mirror_does_not_block_poll_temperature();
test_poll_stale_temp_does_not_overwrite_newer_mirror_temp();
test_static_limits_arrive_via_successful_polls();
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();
test_stale_and_dormant_channels_never_serve_beyond_the_bound();
printf("%s\n", failures ? "FAILED" : "ALL PASSED");
return failures ? 1 : 0;
}

View File

@ -0,0 +1 @@
#include "linux_mock.h"

View File

@ -0,0 +1 @@
#include "linux_mock.h"

View File

@ -0,0 +1 @@
#include "linux_mock.h"

View File

@ -0,0 +1 @@
#include "linux_mock.h"

View File

@ -0,0 +1 @@
#include "linux_mock.h"

View File

@ -0,0 +1 @@
#include "linux_mock.h"

View File

@ -0,0 +1 @@
#include "linux_mock.h"

View File

@ -0,0 +1,129 @@
#ifndef HWMOON_LINUX_MOCK_H
#define HWMOON_LINUX_MOCK_H
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int32_t s32;
typedef int64_t s64;
typedef unsigned short umode_t;
#define READ_ONCE(x) (x)
#define WRITE_ONCE(x, v) ((x) = (v))
#define smp_rmb() do { } while (0)
#define smp_wmb() do { } while (0)
#define CONFIG_HWMON 1
#define IS_ENABLED(config) 1
#define HZ 1000
static unsigned long mock_jiffies;
#define jiffies mock_jiffies
#define msecs_to_jiffies(ms) ((unsigned long)(ms) * HZ / 1000)
#define time_before(a, b) ((long)(b) - (long)(a) > 0)
#define time_after(a, b) time_before(b, a)
typedef struct { int counter; } atomic_t;
typedef struct { long long counter; } atomic64_t;
#define atomic_read(v) ((v)->counter)
#define atomic_set(v, i) ((v)->counter = (i))
#define atomic_or(i, v) ((v)->counter |= (i))
#define atomic_andnot(i, v) ((v)->counter &= ~(i))
#define atomic64_read(v) ((v)->counter)
#define atomic64_set(v, i) ((v)->counter = (i))
struct mutex { int locked; };
#define DEFINE_MUTEX(name) struct mutex name = { .locked = 0 }
#define mutex_init(m) do { (m)->locked = 0; } while (0)
#define mutex_lock(m) do { assert(!(m)->locked); (m)->locked = 1; } while (0)
#define mutex_unlock(m) do { assert((m)->locked); (m)->locked = 0; } while (0)
typedef struct { int locked; } spinlock_t;
#define spin_lock_init(l) do { (l)->locked = 0; } while (0)
#define spin_lock_irqsave(l, f) do { assert(!(l)->locked); (l)->locked = 1; (void)(f); } while (0)
#define spin_unlock_irqrestore(l, f) do { assert((l)->locked); (l)->locked = 0; (void)(f); } while (0)
#define module_param(name, type, perm) \
static void mock_set_##name(unsigned int v) { name = v; }
#define MODULE_PARM_DESC(name, desc)
struct device { void *driver_data; };
#define dev_get_drvdata(d) ((d)->driver_data)
#define dev_set_drvdata(d, p) ((d)->driver_data = (p))
struct attribute { const char *name; unsigned short mode; };
struct attribute_group { const char *name; struct attribute **attrs; };
struct device_attribute {
struct attribute attr;
ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf);
};
#define __stringify(x) #x
#define __ATTR_RO(_name) { .attr = { .name = __stringify(_name), .mode = 0444 }, .show = _name##_show }
#define DEVICE_ATTR_RO(_name) struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
#define ATTRIBUTE_GROUPS(_name) \
static const struct attribute_group _name##_group = { .attrs = _name##_attrs }; \
static const struct attribute_group *const _name##_groups[] = { &_name##_group, NULL }
#define sysfs_emit(buf, fmt, ...) snprintf(buf, 4096, fmt, __VA_ARGS__)
enum hwmon_sensor_types {
hwmon_temp, hwmon_in, hwmon_curr, hwmon_power, hwmon_energy,
hwmon_humidity, hwmon_fan, hwmon_pwm, hwmon_intrusion, hwmon_max,
};
#define hwmon_temp_input 1
#define hwmon_temp_max 2
#define hwmon_temp_crit 3
#define hwmon_power_average 1
#define hwmon_power_cap 2
#define hwmon_in_input 1
#define BIT(x) (1UL << (x))
#define HWMON_T_INPUT BIT(0)
#define HWMON_T_MAX BIT(1)
#define HWMON_T_CRIT BIT(2)
#define HWMON_P_AVERAGE BIT(0)
#define HWMON_P_CAP BIT(1)
#define HWMON_I_INPUT BIT(0)
struct hwmon_channel_info { enum hwmon_sensor_types type; const u32 *config; };
#define HWMON_CHANNEL_INFO(stype, ...) \
&(struct hwmon_channel_info){ .type = hwmon_##stype, .config = (u32[]){ __VA_ARGS__ } }
struct hwmon_ops {
umode_t (*is_visible)(const void *drvdata, enum hwmon_sensor_types type, u32 attr, int channel);
int (*read)(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val);
};
struct hwmon_chip_info { const struct hwmon_ops *ops; const struct hwmon_channel_info *const *info; };
struct hwmon_device_mock { int registered; };
struct device *hwmon_device_register_with_info(struct device *dev, const char *name,
const void *drvdata, const struct hwmon_chip_info *chip,
const struct attribute_group *const *groups);
void hwmon_device_unregister(struct device *hwmon);
#define IS_ERR(p) ((long)(p) < 0 && (long)(p) > -4095)
#define PTR_ERR(p) ((int)(long)(p))
struct pci_bus { unsigned char number; };
struct pci_dev { struct pci_bus *bus; unsigned int devfn; struct device dev; };
static inline int pci_domain_nr(struct pci_bus *bus) { (void)bus; return 0; }
#define PCI_SLOT(devfn) (((devfn) >> 3) & 0x1f)
#define min_t(type, a, b) ((type)(a) < (type)(b) ? (type)(a) : (type)(b))
/* errno values used by hwmon_adapter.c (kernel headers provide these
* transitively; the mock supplies them directly). */
#define EINVAL 22
#define ENODATA 61
#define ENOSPC 28
#define EEXIST 17
#define EOPNOTSUPP 95
#define ENODEV 19
#endif

View File

@ -0,0 +1 @@
#include "linux_mock.h"

View File

@ -0,0 +1 @@
#include "linux_mock.h"

View File

@ -0,0 +1 @@
#include "linux_mock.h"

View File

@ -0,0 +1 @@
#include "linux_mock.h"

View File

@ -0,0 +1 @@
#include "linux_mock.h"

View File

@ -0,0 +1 @@
#include "linux_mock.h"

View File

@ -0,0 +1 @@
#include "linux_mock.h"