Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

timers: Provide timer_shutdown[_sync]()

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.

Expose new interfaces for this: timer_shutdown_sync() and timer_shutdown().

timer_shutdown_sync() has the same functionality as timer_delete_sync()
plus the NULL-ification of the timer function.

timer_shutdown() has the same functionality as timer_delete() plus the
NULL-ification of the timer function.

In both cases the rearming of the timer is prevented by silently discarding
rearm attempts due to timer->function being NULL.

Co-developed-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Reviewed-by: Jacob Keller <jacob.e.keller@intel.com>
Reviewed-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
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.314230270@linutronix.de

+68
+2
include/linux/timer.h
··· 184 184 extern int try_to_del_timer_sync(struct timer_list *timer); 185 185 extern int timer_delete_sync(struct timer_list *timer); 186 186 extern int timer_delete(struct timer_list *timer); 187 + extern int timer_shutdown_sync(struct timer_list *timer); 188 + extern int timer_shutdown(struct timer_list *timer); 187 189 188 190 /** 189 191 * del_timer_sync - Delete a pending timer and wait for a running callback
+66
kernel/time/timer.c
··· 1363 1363 EXPORT_SYMBOL(timer_delete); 1364 1364 1365 1365 /** 1366 + * timer_shutdown - Deactivate a timer and prevent rearming 1367 + * @timer: The timer to be deactivated 1368 + * 1369 + * The function does not wait for an eventually running timer callback on a 1370 + * different CPU but it prevents rearming of the timer. Any attempt to arm 1371 + * @timer after this function returns will be silently ignored. 1372 + * 1373 + * This function is useful for teardown code and should only be used when 1374 + * timer_shutdown_sync() cannot be invoked due to locking or context constraints. 1375 + * 1376 + * Return: 1377 + * * %0 - The timer was not pending 1378 + * * %1 - The timer was pending 1379 + */ 1380 + int timer_shutdown(struct timer_list *timer) 1381 + { 1382 + return __timer_delete(timer, true); 1383 + } 1384 + EXPORT_SYMBOL_GPL(timer_shutdown); 1385 + 1386 + /** 1366 1387 * __try_to_del_timer_sync - Internal function: Try to deactivate a timer 1367 1388 * @timer: Timer to deactivate 1368 1389 * @shutdown: If true, this indicates that the timer is about to be ··· 1616 1595 * lock. If there is the possibility of a concurrent rearm then the return 1617 1596 * value of the function is meaningless. 1618 1597 * 1598 + * If such a guarantee is needed, e.g. for teardown situations then use 1599 + * timer_shutdown_sync() instead. 1600 + * 1619 1601 * Return: 1620 1602 * * %0 - The timer was not pending 1621 1603 * * %1 - The timer was pending and deactivated ··· 1628 1604 return __timer_delete_sync(timer, false); 1629 1605 } 1630 1606 EXPORT_SYMBOL(timer_delete_sync); 1607 + 1608 + /** 1609 + * timer_shutdown_sync - Shutdown a timer and prevent rearming 1610 + * @timer: The timer to be shutdown 1611 + * 1612 + * When the function returns it is guaranteed that: 1613 + * - @timer is not queued 1614 + * - The callback function of @timer is not running 1615 + * - @timer cannot be enqueued again. Any attempt to rearm 1616 + * @timer is silently ignored. 1617 + * 1618 + * See timer_delete_sync() for synchronization rules. 1619 + * 1620 + * This function is useful for final teardown of an infrastructure where 1621 + * the timer is subject to a circular dependency problem. 1622 + * 1623 + * A common pattern for this is a timer and a workqueue where the timer can 1624 + * schedule work and work can arm the timer. On shutdown the workqueue must 1625 + * be destroyed and the timer must be prevented from rearming. Unless the 1626 + * code has conditionals like 'if (mything->in_shutdown)' to prevent that 1627 + * there is no way to get this correct with timer_delete_sync(). 1628 + * 1629 + * timer_shutdown_sync() is solving the problem. The correct ordering of 1630 + * calls in this case is: 1631 + * 1632 + * timer_shutdown_sync(&mything->timer); 1633 + * workqueue_destroy(&mything->workqueue); 1634 + * 1635 + * After this 'mything' can be safely freed. 1636 + * 1637 + * This obviously implies that the timer is not required to be functional 1638 + * for the rest of the shutdown operation. 1639 + * 1640 + * Return: 1641 + * * %0 - The timer was not pending 1642 + * * %1 - The timer was pending 1643 + */ 1644 + int timer_shutdown_sync(struct timer_list *timer) 1645 + { 1646 + return __timer_delete_sync(timer, true); 1647 + } 1648 + EXPORT_SYMBOL_GPL(timer_shutdown_sync); 1631 1649 1632 1650 static void call_timer_fn(struct timer_list *timer, 1633 1651 void (*fn)(struct timer_list *),