mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yipeng Zou <zouyipeng@huawei.com>
To: <anna-maria@linutronix.de>, <frederic@kernel.org>,
	<tglx@linutronix.de>, <linux-kernel@vger.kernel.org>
Cc: <zouyipeng@huawei.com>
Subject: [PATCH] timers: Fix NULL function pointer race in timer_shutdown_sync
Date: Sat, 22 Nov 2025 09:39:42 +0000	[thread overview]
Message-ID: <20251122093942.301559-1-zouyipeng@huawei.com> (raw)

There is a race condition between timer_shutdown_sync() and timer
expiration that can lead to hitting a WARN_ON in expire_timers().

The issue occurs when timer_shutdown_sync() clears the timer function
to NULL while the timer is still running on another CPU. The race
scenario looks like this:

CPU0					CPU1
					<SOFTIRQ>
					lock_timer_base
					expire_timers
					base->running_timer = timer;
					unlock_timer_base
					[call_timer_fn enter]
					mod_timer
					...
timer_shutdown_sync
lock_timer_base
// For now, will not detach the timer but only clear its function to NULL
if (base->running_timer != timer)
	ret = detach_if_pending(timer, base, true);
if (shutdown)
	timer->function = NULL;
unlock_timer_base
					[call_timer_fn exit]
					lock_timer_base
					base->running_timer = NULL
					unlock_timer_base
					...
					// Now timer is pending while its function set to NULL.
					// next timer trigger
					<SOFTIRQ>
					expire_timers
					WARN_ON_ONCE(!fn) // hit
					...
lock_timer_base
// Now timer will detach
if (base->running_timer != timer)
	ret = detach_if_pending(timer, base, true);
if (shutdown)
	timer->function = NULL;
unlock_timer_base

The problem is that timer_shutdown_sync() clears the timer function
regardless of whether the timer is currently running. This can leave
a pending timer with a NULL function pointer, which triggers the
WARN_ON_ONCE(!fn) check in expire_timers().

Fix this by only clearing the timer function when we actually detach
the timer. If the timer is running, we leave the function pointer
intact, which is safe because the timer will be properly detached
when it finishes running.

Signed-off-by: Yipeng Zou <zouyipeng@huawei.com>
---
 kernel/time/timer.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 282a8e5c05f8..1f2364126894 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -1458,10 +1458,11 @@ static int __try_to_del_timer_sync(struct timer_list *timer, bool shutdown)
 
 	base = lock_timer_base(timer, &flags);
 
-	if (base->running_timer != timer)
+	if (base->running_timer != timer) {
 		ret = detach_if_pending(timer, base, true);
-	if (shutdown)
-		timer->function = NULL;
+		if (shutdown)
+			timer->function = NULL;
+	}
 
 	raw_spin_unlock_irqrestore(&base->lock, flags);
 
-- 
2.34.1


             reply	other threads:[~2025-11-22  9:43 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-22  9:39 Yipeng Zou [this message]
2025-11-22 21:57 ` [tip: timers/urgent] timers: Fix NULL function pointer race in timer_shutdown_sync() tip-bot2 for Yipeng Zou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251122093942.301559-1-zouyipeng@huawei.com \
    --to=zouyipeng@huawei.com \
    --cc=anna-maria@linutronix.de \
    --cc=frederic@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®