mirror-linux/kernel/time
Yipeng Zou 20739af073 timers: Fix NULL function pointer race in timer_shutdown_sync()
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 actually detaching the
timer. If the timer is running, leave the function pointer intact, which is
safe because the timer will be properly detached when it finishes running.

Fixes: 0cc04e8045 ("timers: Add shutdown mechanism to the internal functions")
Signed-off-by: Yipeng Zou <zouyipeng@huawei.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20251122093942.301559-1-zouyipeng@huawei.com
2025-11-22 22:55:26 +01:00
..
Kconfig time: Introduce auxiliary POSIX clocks 2025-06-19 14:28:22 +02:00
Makefile time: Build generic update_vsyscall() only with generic time vDSO 2025-09-04 11:23:50 +02:00
alarmtimer.c time: Fix spelling mistakes in comments 2025-09-21 10:02:02 +02:00
clockevents.c tick: Do not set device to detached state in tick_shutdown() 2025-09-09 13:39:00 +02:00
clocksource-wdtest.c clocksource/wdtest: Print time values for short udelay(1) 2025-01-15 19:49:13 +01:00
clocksource.c time: Fix spelling mistakes in comments 2025-09-21 10:02:02 +02:00
hrtimer.c Updates for the time(rs) core subsystem: 2025-09-30 16:09:27 -07:00
itimer.c timers/itimer: Avoid direct access to hrtimer clockbase 2025-09-09 12:27:17 +02:00
jiffies.c time/jiffies: Change register_refined_jiffies() to void __init 2025-04-30 09:06:23 +02:00
namespace.c namespace-6.18-rc1 2025-09-29 11:20:29 -07:00
ntp.c ntp: Use ktime_get_ntp_seconds() 2025-06-19 14:28:24 +02:00
ntp_internal.h ntp: Rename __do_adjtimex() to ntp_adjtimex() 2025-06-19 14:28:23 +02:00
posix-clock.c Networking changes for 6.15. 2025-03-26 21:48:21 -07:00
posix-cpu-timers.c posix-cpu-timers: fix race between handle_posix_cpu_timers() and posix_cpu_timer_del() 2025-06-13 10:55:49 -07:00
posix-stubs.c
posix-timers.c posix-timers: Plug potential memory leak in do_timer_create() 2025-11-14 16:58:31 +01:00
posix-timers.h timekeeping: Add minimal posix-timers support for auxiliary clocks 2025-06-27 20:13:12 +02:00
sched_clock.c time/sched_clock: Export symbol for sched_clock register function 2025-09-23 10:52:31 +02:00
sleep_timeout.c treewide, timers: Rename from_timer() to timer_container_of() 2025-06-08 09:07:37 +02:00
test_udelay.c time: Add MODULE_DESCRIPTION() to time test modules 2024-06-03 11:18:50 +02:00
tick-broadcast-hrtimer.c time: Switch to hrtimer_setup() 2025-02-18 10:32:33 +01:00
tick-broadcast.c tick/broadcast: Add kernel-doc for function parameters 2025-01-15 19:49:14 +01:00
tick-common.c tick: Do not set device to detached state in tick_shutdown() 2025-09-09 13:39:00 +02:00
tick-internal.h tick: Do not set device to detached state in tick_shutdown() 2025-09-09 13:39:00 +02:00
tick-legacy.c
tick-oneshot.c
tick-sched.c tick/sched: Fix bogus condition in report_idle_softirq() 2025-11-19 19:30:45 +01:00
tick-sched.h tick/sched: Fix struct tick_sched doc warnings 2024-04-01 10:36:35 +02:00
time.c time: export timespec64_add_safe() symbol 2025-09-03 16:51:08 -07:00
time_test.c time: Add MODULE_DESCRIPTION() to time test modules 2024-06-03 11:18:50 +02:00
timeconst.bc
timeconv.c
timecounter.c time/timecounter: Fix the lie that struct cyclecounter is const 2025-07-01 15:38:25 +02:00
timekeeping.c timekeeping: Fix resource leak in tk_aux_sysfs_init() error paths 2025-11-20 16:40:48 +01:00
timekeeping.h
timekeeping_debug.c timekeeping: Add percpu counter for tracking floor swap events 2024-10-10 10:20:46 +02:00
timekeeping_internal.h timekeeping: Provide ktime_get_ntp_seconds() 2025-06-19 14:28:23 +02:00
timer.c timers: Fix NULL function pointer race in timer_shutdown_sync() 2025-11-22 22:55:26 +01:00
timer_list.c hrtimer: Remove hrtimer_clock_base:: Get_time 2025-09-09 12:27:18 +02:00
timer_migration.c timers/migration: Clean up the loop in tmigr_quick_check() 2025-06-12 21:03:45 +02:00
timer_migration.h timer/migration: Fix kernel-doc warnings for union tmigr_state 2025-01-15 19:49:14 +01:00
vsyscall.c vdso/vsyscall: Avoid slow division loop in auxiliary clock update 2025-09-03 11:55:11 +02:00