From bcf587a987573057968a11b941729d70799947f4 Mon Sep 17 00:00:00 2001 From: Nickolai Zeldovich Date: Sun, 11 Oct 2026 18:24:12 -0400 Subject: [PATCH] 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: b7fa8a246cba ("lib: sbi_timer: Add support for timer events") Signed-off-by: Nickolai Zeldovich Assisted-By: Claude Fable 5.1 Reviewe-by: Anup Patel Link: https://lore.kernel.org/r/20261011222412.769530-1-nickolai@csail.mit.edu Signed-off-by: Anup Patel --- lib/sbi/sbi_timer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sbi/sbi_timer.c b/lib/sbi/sbi_timer.c index 84030310..8b0fe02a 100644 --- a/lib/sbi/sbi_timer.c +++ b/lib/sbi/sbi_timer.c @@ -205,7 +205,7 @@ static void __sbi_timer_event_start(struct timer_state *tstate, ev->hart_index = current_hartindex(); ev->time_stamp = next_event; if (next_ev) - sbi_list_add(&ev->head, &next_ev->head); + sbi_list_add_tail(&ev->head, &next_ev->head); else sbi_list_add_tail(&ev->head, &tstate->event_list); }