Commit Graph
2 Commits
Author SHA1 Message Date
Nickolai Zeldovich f6abdaf25d lib: sbi_pmu: Do not return uninitialized stack data on FW read errors
sbi_ecall_pmu_handler() passes the uninitialized local temp to
sbi_pmu_ctr_fw_read() and copies it into out->value unconditionally.
sbi_pmu_ctr_fw_read() only writes *cval on success, so every error
return of SBI_EXT_PMU_COUNTER_FW_READ (e.g. a hardware or inactive
counter index) hands an M-mode stack word to the supervisor in a1.
SBI_EXT_PMU_COUNTER_FW_READ_HI does the same with the upper half of
temp on RV32.

Initialize temp to zero so that the error paths return zero in
sbiret.value; the success paths are unchanged.

Fixes: 1664d0efce ("lib: sbi_pmu: Replace sbi_pmu_ctr_read() with sbi_pmu_ctr_fw_read()")
Fixes: 51951d9e9a ("lib: sbi_pmu: Implement sbi_pmu_counter_fw_read_hi")
Signed-off-by: Nickolai Zeldovich <nickolai@csail.mit.edu>
Assisted-By: Claude Fable 5.1
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261011222115.769450-1-nickolai@csail.mit.edu
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-11-04 11:34:46 +05:30
Nickolai Zeldovich bcf587a987 lib: sbi_timer: Insert timer event before the later event
The per-HART event list is kept sorted by expiry time: the head is the
event programmed into the timer device by __sbi_timer_update_device(),
and sbi_timer_process() stops at the first unexpired entry.

__sbi_timer_event_start() finds the first event that expires after the
new one, but then inserts the new event with sbi_list_add(), i.e. after
that event instead of before it.  Starting an event at time 50 while an
event at time 1000 is pending yields the list [1000, 50]: the device is
programmed for 1000 and the earlier event fires 950 ticks late.

Today each HART only ever has its S-mode timer event on the list, so
the misordering needs a second timer event user to show up.  Once there
is one, on HARTs without Sstc an S-mode timer programmed while a later
M-mode event is pending is delivered only when that later event
expires.

Use sbi_list_add_tail() so the new event goes in front of next_ev.

Fixes: b7fa8a246c ("lib: sbi_timer: Add support for timer events")
Signed-off-by: Nickolai Zeldovich <nickolai@csail.mit.edu>
Assisted-By: Claude Fable 5.1
Reviewe-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20261011222412.769530-1-nickolai@csail.mit.edu
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-10-25 15:05:29 +05:30