From nobody Tue Dec 2 01:04:41 2025 Received: from canpmsgout04.his.huawei.com (canpmsgout04.his.huawei.com [113.46.200.219]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9A702F25F5 for ; Sat, 22 Nov 2025 09:43:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.219 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763804624; cv=none; b=mipxL1zp2xfySdRjHXl1MqKJ+lnwbP+v9YyxVwOyPv2NiDooVXhGK47cn0iOBGFWQX+tnWLj1WXWilG+N1Uh1iv1IcGNFzRBwiEsTuLGEHZkA+OuV09kHHBvh037pj0o/v8cWd/46cdL/fSmb+MUvkBy0NLncPRLsZcmVQzO7SY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763804624; c=relaxed/simple; bh=sRklcfd2cwSqm4yo0TLTRnEeMaXDG8S+daWZj2ZEil0=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=YdoUHjrHf9GnWC+ZwcBHhzHb1Z6ZirivdJd7fbUMqQSajaHn3uWXZvz5LBqGhijMkC0jnxhDajGfN0T4MRKANhTitHYzbOwN4qTshC4CFUiX4kTVN7RLKuw/sCs1Q24MYz+b8mBR8hO7wyChB5W4l+WLlGJXQ43+Q2kUmLpODfM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=A+twi9qI; arc=none smtp.client-ip=113.46.200.219 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="A+twi9qI" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=qi3MHSoXvZ9hCK904hyhHXwV3EVj23YHh81zyqyRR6s=; b=A+twi9qI6p5JQ0pK8i99ABtePs5JCxVHJHSAa/6BOjoEmAg8jIfNdpD0RWaQBNIyRmo2Pwiry AX69wzszWR/L/7QB4VGrHIfDCUUIL1f5hIGK+JmYmdJSf+xQRpdsg3yECEiwvgAbY8FONX5QjKn KvJMueAP56u8SBxgcixcktE= Received: from mail.maildlp.com (unknown [172.19.163.252]) by canpmsgout04.his.huawei.com (SkyGuard) with ESMTPS id 4dD6YX2BLDz1prLk; Sat, 22 Nov 2025 17:41:52 +0800 (CST) Received: from dggpemf100018.china.huawei.com (unknown [7.185.36.183]) by mail.maildlp.com (Postfix) with ESMTPS id C75EF180B54; Sat, 22 Nov 2025 17:43:38 +0800 (CST) Received: from huawei.com (10.67.174.51) by dggpemf100018.china.huawei.com (7.185.36.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Sat, 22 Nov 2025 17:43:38 +0800 From: Yipeng Zou To: , , , CC: Subject: [PATCH] timers: Fix NULL function pointer race in timer_shutdown_sync Date: Sat, 22 Nov 2025 09:39:42 +0000 Message-ID: <20251122093942.301559-1-zouyipeng@huawei.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: kwepems200002.china.huawei.com (7.221.188.68) To dggpemf100018.china.huawei.com (7.185.36.183) Content-Type: text/plain; charset="utf-8" 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 lock_timer_base expire_timers base->running_timer =3D 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 !=3D timer) ret =3D detach_if_pending(timer, base, true); if (shutdown) timer->function =3D NULL; unlock_timer_base [call_timer_fn exit] lock_timer_base base->running_timer =3D NULL unlock_timer_base ... // Now timer is pending while its function set to NULL. // next timer trigger expire_timers WARN_ON_ONCE(!fn) // hit ... lock_timer_base // Now timer will detach if (base->running_timer !=3D timer) ret =3D detach_if_pending(timer, base, true); if (shutdown) timer->function =3D 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 --- 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_lis= t *timer, bool shutdown) =20 base =3D lock_timer_base(timer, &flags); =20 - if (base->running_timer !=3D timer) + if (base->running_timer !=3D timer) { ret =3D detach_if_pending(timer, base, true); - if (shutdown) - timer->function =3D NULL; + if (shutdown) + timer->function =3D NULL; + } =20 raw_spin_unlock_irqrestore(&base->lock, flags); =20 --=20 2.34.1