[PATCH] timers: Add warning if timer list is corrupted

From: yw85.kim
Date: Sun Sep 24 2023 - 23:21:07 EST


[Description]
If some drivers use timers wrong, it can cause list corruption
(LIST_POISON2) and oops.

detach_timer(struct timer_list *timer, bool clear_pending)
__hlist_del(entry);
next->pprev = pprev; // oops, next is LIST_POISON2(0x122)

However, it is difficult to find the cause through backtrace.

[1-11.7841] Unable to handle kernel NULL pointer dereference at
virtual address 00000126
[1-11.7841] pgd = 0ac52d81
[1-11.7841] [00000126] *pgd=00000000
Die cpu info :
[1-11.7841] Internal error: Oops: 805 [#1] PREEMPT SMP ARM
[1-11.7842] PC is at run_timer_softirq+0x1f0/0x684
[1-11.7842] LR is at __warn+0xb4/0xf8
...
[1-11.7842] Backtrace:
[1-11.7842] [<c00bd464>] (run_timer_softirq)
[1-11.7842] [<c000a438>] (__do_softirq+0x1ac/0x50c)
[1-11.7842] [<c00362d4>] (run_ksoftirqd+0x84/0x98)
[1-11.7842] [<c005a564>] (smpboot_thread_fn+0x140/0x2c0)
[1-11.7843] [<c00560c0>] (kthread+0x1cc/0x1d0)

Adding this warning will give us more detailed information.
In this case the hdmi_switch driver was the cause.

A problem occurred because multiple schedule delayed works
were created using same dw(struct delayed_work dw) structure.

[1-11.7835] WARNING: CPU: 1 PID: 17 at kernel/time/timer.c:830
run_timer_softirq+0x50c/0x684
[1-11.7836] timer: list corruption, entry->next is LIST_POISON2,
entry:dw+0x10/0xfffe1a3c [hdmi_switch],
fn:delayed_work_timer_fn+0x0/0x28

Signed-off-by: yw85.kim <yw85.kim@xxxxxxxxxxx>
---
kernel/time/timer.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 63a8ce7..e1e3340 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -877,6 +877,10 @@ static inline void detach_timer(struct timer_list *timer, bool clear_pending)

debug_deactivate(timer);

+ if (unlikely(entry->next == LIST_POISON2))
+ WARN_ONCE(1, "timer: list corruption, next is LIST_POISON2, entry:%pS, fn:%pS\n",
+ entry, timer->function);
+
__hlist_del(entry);
if (clear_pending)
entry->pprev = NULL;
--
2.7.4