From nobody Fri Dec 19 14:03:01 2025 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 132672FB093 for ; Fri, 10 Oct 2025 15:03:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760108620; cv=none; b=fgI5pKPOghy9Q+nnY6JXg811xisDwuDO1ybJ6lKnchEGOBmUD/PflkpYmQwYKv6ez0qWX34mPi5OaYjc8ZugPVC8ByOlvESYZ8nfyHj6VRm7pjqenfUzodIrX/gTb7QuzhI8SiqdH2x4Znk75ygZtqiQ/vR/OaV+HbxyQCYAWYI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760108620; c=relaxed/simple; bh=FfeWAFL01yrxGNZ9KQqmQL8U/+v/91/EffrzfYdxZZA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qDwT9hGaSpjOFhFjYtqE6R1+fOLKGzzg3dzYpVKdIjLqUuZ+Itp7DPLpagXHoaOXwFOCo3p5JKy0YnVZyWG11gW8q3ho0CKm5c4tAbRPPhmD46AHivzaOf6WLmZ5EcGMGyc/MFhEwBCkVUSg6pc9TeiLkevfc/Vi78+ePHd4aqw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PwMKTOWj; arc=none smtp.client-ip=209.85.210.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PwMKTOWj" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-782e93932ffso2066210b3a.3 for ; Fri, 10 Oct 2025 08:03:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1760108618; x=1760713418; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=j0bNeek1LW81/KG2/Q/uicdT1bV3tR+ITFKGtccSqSg=; b=PwMKTOWjlKiSl8aZ4Tlq2h2zXpGg1JGTXK+2+563ZeXzobtQwUekS+fZ6BRV/ryzIE ppH+oHxz7/46gEn1NADzkUBktoRhQyDNMICbXNroThOcPE4sL76b63ns+fsaueVqhGm9 F0iSIW0DYN/LatDho76W58TtqfbzaN2AOpRZCACsHNH43sr/GY2OQb91KukM4QxBP9+k LiVa29UQdMiYwcsvGbX2pCvOPZfq3PGRDwyjswKcjYEIuq3IQlos3Pza52wMqN6B71vK BkbNaqQvpW257zoB0e0Dgubf/14DC4z+uzsGYx5+yrIv3HxVvAUPVdmkzgKxkXQPtKIY 8GNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760108618; x=1760713418; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=j0bNeek1LW81/KG2/Q/uicdT1bV3tR+ITFKGtccSqSg=; b=JQOONGyDM3oV73vbIbtRxRKumc+cZPy5wI5Mpw6QzuLlQqRgu5vuQvo9Nntcaj5Z/5 5aPnJ5HMXhvUWnrTWW5qiZ2ZenEFudXbAPpn4CLEUBH9mv/J9R7xXP1qIdfJYHR8i5+m NMN1HVf7WtBwXQmAA2Kb97Sq4JSR98vHuMvDAmoPp8ltNrtHJ4m6YVkHnYMHJr+Sghu9 8jVAh/73UQqxOsSs4yTsuzgS4UziJp5VYlu3oPglNmY0ay4+VkvwCj/oR7SD2syKeN8G m6O0adhZtDgceS2zGjJ9s5oTka4AImgWyTijjjpOwE+dy4KrRU7onLLxDW9ZJ/vjrAnn Z70Q== X-Forwarded-Encrypted: i=1; AJvYcCVjQtJQyibI4Ppl4Pg3sZXo6N9hqlG9gWLL2EHJRB9lRCpb8GRcARqUcFZt40n+yc9lrWFL/zlIoUQwdGE=@vger.kernel.org X-Gm-Message-State: AOJu0Yxninh9LtjHWxjM/33iVn7hWwzmFhSD1bUpvQS870FfuQLgmuM5 zXIn7W28LfwDM9p/d17zPBntw3Rp5QRlFSgwXKH22CE5xCN5ZBetIu3b X-Gm-Gg: ASbGncu2Jyj4qGN5FM/K1ysILZ9iiVDabBkW2fgL6nZ6OS3wAJCwJVX7zZOzLlnJPsG EI0ge9ygIOfFBiTaPWy1g+mKvPQNHyq/Jmi+bdlq1HlcyFpBFo/OeJG/807ncn9E7HWr1m9ONMs scRqGHJG14vnhR5x3rN08oBn3wA/lJWMJ8jzn80ZfYoZ0z+BpZzrb6xx++ONGxDTXJOf0aFhq0S MDkar9WSmkjzo9Co1IxJ2Nwx9jt9FfQSQm7jKpaLY6m7bRAJ/GPrfmjKh6xbSKuXgYQlNWYafpf 2pKDP6iO6BboKuyBJd3g074lBuXhI5LJmBXdbKY0aKue/bmC+bVFOSbpwmTclHnJNHLkhjwz2/T +cGjREjWvTn8nVn+S2SnYkCYlZT7m8291T7ieuTvuwPeW75pHD+N3IfOt4KRvyqjuPbAC1bU7d8 HDiL70ZCAPP/Iwhg== X-Google-Smtp-Source: AGHT+IESNC8RkQTBaynIxPs6OR43ASsf0mjK4WexzZ/IzmWVAMQfi+tV6noR5/B9CBVHAqdLTCbbEg== X-Received: by 2002:a05:6a00:3c84:b0:781:1e80:f0c6 with SMTP id d2e1a72fcca58-79387440a4dmr15774554b3a.17.1760108617993; Fri, 10 Oct 2025 08:03:37 -0700 (PDT) Received: from name2965-Precision-7820-Tower.. ([121.185.186.233]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7992b639cbcsm3266359b3a.18.2025.10.10.08.03.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Oct 2025 08:03:37 -0700 (PDT) From: Jeongjun Park To: stable@vger.kernel.org Cc: gregkh@linuxfoundation.org, tglx@linutronix.de, Julia.Lawall@inria.fr, akpm@linux-foundation.org, anna-maria@linutronix.de, arnd@arndb.de, linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org, linux@roeck-us.net, luiz.dentz@gmail.com, marcel@holtmann.org, maz@kernel.org, peterz@infradead.org, rostedt@goodmis.org, sboyd@kernel.org, viresh.kumar@linaro.org, Jacob Keller , Jeongjun Park Subject: [PATCH 6.1.y 08/12] timers: Split [try_to_]del_timer[_sync]() to prepare for shutdown mode Date: Sat, 11 Oct 2025 00:02:48 +0900 Message-Id: <20251010150252.1115788-9-aha310510@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251010150252.1115788-1-aha310510@gmail.com> References: <20251010150252.1115788-1-aha310510@gmail.com> 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 Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner [ Upstream commit 8553b5f2774a66b1f293b7d783934210afb8f23c ] Tearing down timers which have circular dependencies to other functionality, e.g. workqueues, where the timer can schedule work and work can arm timers, is not trivial. In those cases it is desired to shutdown the timer in a way which prevents rearming of the timer. The mechanism to do so is to set timer->function to NULL and use this as an indicator for the timer arming functions to ignore the (re)arm request. Split the inner workings of try_do_del_timer_sync(), del_timer_sync() and del_timer() into helper functions to prepare for implementing the shutdown functionality. No functional change. Co-developed-by: Steven Rostedt Signed-off-by: Steven Rostedt Signed-off-by: Thomas Gleixner Tested-by: Guenter Roeck Reviewed-by: Jacob Keller Reviewed-by: Anna-Maria Behnsen Link: https://lore.kernel.org/all/20220407161745.7d6754b3@gandalf.local.home Link: https://lore.kernel.org/all/20221110064101.429013735@goodmis.org Link: https://lore.kernel.org/r/20221123201625.195147423@linutronix.de Signed-off-by: Jeongjun Park --- kernel/time/timer.c | 143 ++++++++++++++++++++++++++++---------------- 1 file changed, 92 insertions(+), 51 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index 3b6624cd9507..0b76a2ab42e3 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1298,20 +1298,14 @@ void add_timer_on(struct timer_list *timer, int cpu) EXPORT_SYMBOL_GPL(add_timer_on); =20 /** - * timer_delete - Deactivate a timer + * __timer_delete - Internal function: Deactivate a timer * @timer: The timer to be deactivated * - * The function only deactivates a pending timer, but contrary to - * timer_delete_sync() it does not take into account whether the timer's - * callback function is concurrently executed on a different CPU or not. - * It neither prevents rearming of the timer. If @timer can be rearmed - * concurrently then the return value of this function is meaningless. - * * Return: * * %0 - The timer was not pending * * %1 - The timer was pending and deactivated */ -int timer_delete(struct timer_list *timer) +static int __timer_delete(struct timer_list *timer) { struct timer_base *base; unsigned long flags; @@ -1327,25 +1321,37 @@ int timer_delete(struct timer_list *timer) =20 return ret; } -EXPORT_SYMBOL(timer_delete); =20 /** - * try_to_del_timer_sync - Try to deactivate a timer - * @timer: Timer to deactivate + * timer_delete - Deactivate a timer + * @timer: The timer to be deactivated * - * This function tries to deactivate a timer. On success the timer is not - * queued and the timer callback function is not running on any CPU. + * The function only deactivates a pending timer, but contrary to + * timer_delete_sync() it does not take into account whether the timer's + * callback function is concurrently executed on a different CPU or not. + * It neither prevents rearming of the timer. If @timer can be rearmed + * concurrently then the return value of this function is meaningless. * - * This function does not guarantee that the timer cannot be rearmed right - * after dropping the base lock. That needs to be prevented by the calling - * code if necessary. + * Return: + * * %0 - The timer was not pending + * * %1 - The timer was pending and deactivated + */ +int timer_delete(struct timer_list *timer) +{ + return __timer_delete(timer); +} +EXPORT_SYMBOL(timer_delete); + +/** + * __try_to_del_timer_sync - Internal function: Try to deactivate a timer + * @timer: Timer to deactivate * * Return: * * %0 - The timer was not pending * * %1 - The timer was pending and deactivated * * %-1 - The timer callback function is running on a different CPU */ -int try_to_del_timer_sync(struct timer_list *timer) +static int __try_to_del_timer_sync(struct timer_list *timer) { struct timer_base *base; unsigned long flags; @@ -1362,6 +1368,27 @@ int try_to_del_timer_sync(struct timer_list *timer) =20 return ret; } + +/** + * try_to_del_timer_sync - Try to deactivate a timer + * @timer: Timer to deactivate + * + * This function tries to deactivate a timer. On success the timer is not + * queued and the timer callback function is not running on any CPU. + * + * This function does not guarantee that the timer cannot be rearmed right + * after dropping the base lock. That needs to be prevented by the calling + * code if necessary. + * + * Return: + * * %0 - The timer was not pending + * * %1 - The timer was pending and deactivated + * * %-1 - The timer callback function is running on a different CPU + */ +int try_to_del_timer_sync(struct timer_list *timer) +{ + return __try_to_del_timer_sync(timer); +} EXPORT_SYMBOL(try_to_del_timer_sync); =20 #ifdef CONFIG_PREEMPT_RT @@ -1438,45 +1465,15 @@ static inline void del_timer_wait_running(struct ti= mer_list *timer) { } #endif =20 /** - * timer_delete_sync - Deactivate a timer and wait for the handler to fini= sh. + * __timer_delete_sync - Internal function: Deactivate a timer and wait + * for the handler to finish. * @timer: The timer to be deactivated * - * Synchronization rules: Callers must prevent restarting of the timer, - * otherwise this function is meaningless. It must not be called from - * interrupt contexts unless the timer is an irqsafe one. The caller must - * not hold locks which would prevent completion of the timer's callback - * function. The timer's handler must not call add_timer_on(). Upon exit - * the timer is not queued and the handler is not running on any CPU. - * - * For !irqsafe timers, the caller must not hold locks that are held in - * interrupt context. Even if the lock has nothing to do with the timer in - * question. Here's why:: - * - * CPU0 CPU1 - * ---- ---- - * - * call_timer_fn(); - * base->running_timer =3D mytimer; - * spin_lock_irq(somelock); - * - * spin_lock(somelock); - * timer_delete_sync(mytimer); - * while (base->running_timer =3D=3D mytimer); - * - * Now timer_delete_sync() will never return and never release somelock. - * The interrupt on the other CPU is waiting to grab somelock but it has - * interrupted the softirq that CPU0 is waiting to finish. - * - * This function cannot guarantee that the timer is not rearmed again by - * some concurrent or preempting code, right after it dropped the base - * lock. If there is the possibility of a concurrent rearm then the return - * value of the function is meaningless. - * * Return: * * %0 - The timer was not pending * * %1 - The timer was pending and deactivated */ -int timer_delete_sync(struct timer_list *timer) +static int __timer_delete_sync(struct timer_list *timer) { int ret; =20 @@ -1506,7 +1503,7 @@ int timer_delete_sync(struct timer_list *timer) lockdep_assert_preemption_enabled(); =20 do { - ret =3D try_to_del_timer_sync(timer); + ret =3D __try_to_del_timer_sync(timer); =20 if (unlikely(ret < 0)) { del_timer_wait_running(timer); @@ -1516,6 +1513,50 @@ int timer_delete_sync(struct timer_list *timer) =20 return ret; } + +/** + * timer_delete_sync - Deactivate a timer and wait for the handler to fini= sh. + * @timer: The timer to be deactivated + * + * Synchronization rules: Callers must prevent restarting of the timer, + * otherwise this function is meaningless. It must not be called from + * interrupt contexts unless the timer is an irqsafe one. The caller must + * not hold locks which would prevent completion of the timer's callback + * function. The timer's handler must not call add_timer_on(). Upon exit + * the timer is not queued and the handler is not running on any CPU. + * + * For !irqsafe timers, the caller must not hold locks that are held in + * interrupt context. Even if the lock has nothing to do with the timer in + * question. Here's why:: + * + * CPU0 CPU1 + * ---- ---- + * + * call_timer_fn(); + * base->running_timer =3D mytimer; + * spin_lock_irq(somelock); + * + * spin_lock(somelock); + * timer_delete_sync(mytimer); + * while (base->running_timer =3D=3D mytimer); + * + * Now timer_delete_sync() will never return and never release somelock. + * The interrupt on the other CPU is waiting to grab somelock but it has + * interrupted the softirq that CPU0 is waiting to finish. + * + * This function cannot guarantee that the timer is not rearmed again by + * some concurrent or preempting code, right after it dropped the base + * lock. If there is the possibility of a concurrent rearm then the return + * value of the function is meaningless. + * + * Return: + * * %0 - The timer was not pending + * * %1 - The timer was pending and deactivated + */ +int timer_delete_sync(struct timer_list *timer) +{ + return __timer_delete_sync(timer); +} EXPORT_SYMBOL(timer_delete_sync); =20 static void call_timer_fn(struct timer_list *timer, --