Fix-Point commented on PR #22027:
URL: https://github.com/apache/nuttx/pull/22027#issuecomment-7001758974
This patch should not be merged. It moves the compensation from the
timer-arming side to the expiry-check side and breaks the invariant that a
timer must not fire before its deadline.
**1. What we compensate is the interrupt latency**
Total interrupt execution time:
$$t_{irqexec} = \underbrace{t_{hyp} + t_{ctxsave} +
t_{dispatch}}_{\text{interrupt latency } t_{irqlatency}} +
\underbrace{t_{gettime} + t_{process}}_{\text{timer ISR handling}}$$
Execution order: in a virtualized setup the hypervisor intercepts first
($t_{hyp}$), then context save ($t_{ctxsave}$), then interrupt dispatch into
the timer ISR ($t_{dispatch}$); inside the ISR the current time is read
($t_{gettime}$), then the callback runs ($t_{process}$). All terms are
durations.
Callback time instant:
$$T_{callback} = T + (t_{irqlatency} - t_{adj})$$
where $T_{callback}$ is the point in time at which the callback runs, $T$ is
the deadline, $t_{adj}$ is the adjustment (the hardware fires at $T -
t_{adj}$), and $t_{irqlatency}$ is the interrupt delay from trigger to
callback. `CONFIG_TIMER_ADJUST_NSEC` compensates exactly this $t_{irqlatency}$.
In a virtualized environment the hypervisor hop makes this delay significantly
larger, which is precisely why the compensation is needed.
**2. The patch violates the deadline invariant**
The invariant $T_{callback} \ge T$ is equivalent to $t_{irqlatency} \ge
t_{adj}$. The master gate `expired <= now` enforces exactly this: the callback
never runs before the deadline $T$. Even if the adjustment is set above the
actual interrupt latency, the old behavior only produces a few extra interrupts
— a performance degradation, not a correctness issue; the timer never fires
early.
The patch changes the gate to `expired <= now + ADJ`, which is always true.
Whenever the adjustment exceeds the BCET of the interrupt latency
($t_{irqlatency} < t_{adj}$), the callback necessarily runs early by $t_{adj} -
t_{irqlatency}$, i.e. before its deadline. Early timer expiry is a serious
problem in real-time systems: `nanosleep`/`usleep` and semaphore timeouts can
return before the requested time, and any drop-dead watchdog fires too early.
So the gates must stay at `expired <= now`. Please reject this change.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]