hrtimer: Add cleanup class for on-stack timers

hrtimer_setup_on_stack() requires callers to destroy the timer before its
backing stack storage goes out of scope, but the setup interface does not
encode the required teardown pairing.

Add an hrtimer_on_stack cleanup class that pairs on-stack setup with
destroy_hrtimer_on_stack(). Document the existing setup API's lifetime
requirement.

The cleanup class provides debug-object teardown, not timer
cancellation. Callers must ensure that no hrtimer operation can still
access the timer before the scope ends; in particular, the timer must
no longer be queued and any running callback must have completed.

Signed-off-by: Hui Su <sh_def@163.com>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Link: https://patch.msgid.link/5f48c32490d19f51035e4209174f9cfe44472c71.1792083743.git.sh_def@163.com
This commit is contained in:
Hui Su 2026-10-16 10:09:43 +09:00 committed by Thomas Gleixner
parent 6541fbc942
commit 064bcd117e
2 changed files with 30 additions and 2 deletions

View File

@ -12,6 +12,7 @@
#ifndef _LINUX_HRTIMER_H
#define _LINUX_HRTIMER_H
#include <linux/cleanup.h>
#include <linux/hrtimer_rearm.h>
#include <linux/hrtimer_types.h>
#include <linux/init.h>
@ -201,6 +202,29 @@ extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
#endif
/*
* The cleanup class pairs on-stack setup with debug-object teardown. Callers
* using it must not call destroy_hrtimer_on_stack() manually.
*
* This class provides debug-object teardown, not timer cancellation. Callers
* must ensure that no hrtimer operation can still access the timer before
* leaving the scope. In particular, the timer must no longer be queued and
* any running callback must have completed.
*
* Callers using hrtimer_setup_on_stack() without this class retain
* responsibility for destroying the timer before its backing storage leaves
* scope.
*/
DEFINE_CLASS(hrtimer_on_stack, struct hrtimer *,
destroy_hrtimer_on_stack(_T),
({
hrtimer_setup_on_stack(timer, function, clock_id, mode);
timer;
}),
struct hrtimer *timer,
enum hrtimer_restart (*function)(struct hrtimer *),
clockid_t clock_id, enum hrtimer_mode mode)
/* Basic timer operations: */
extern void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
u64 range_ns, const enum hrtimer_mode mode);

View File

@ -1975,8 +1975,12 @@ EXPORT_SYMBOL_GPL(hrtimer_setup);
* @clock_id: The clock to be used
* @mode: The timer mode
*
* Similar to hrtimer_setup(), except that this one must be used if struct hrtimer is in stack
* memory.
* Similar to hrtimer_setup(), except that this one must be used if struct
* hrtimer is in stack memory.
*
* Callers that do not use the hrtimer_on_stack cleanup class must call
* destroy_hrtimer_on_stack() before the stack storage goes out of scope. The
* cleanup class pairs setup and destruction automatically.
*/
void hrtimer_setup_on_stack(struct hrtimer *timer,
enum hrtimer_restart (*function)(struct hrtimer *),