* [PATCH v2 0/2] hrtimer: Add cleanup class for on-stack timers @ 2026-10-16 1:09 Hui Su 2026-10-16 1:09 ` [PATCH v2 1/2] " Hui Su 2026-10-16 1:09 ` [PATCH v2 2/2] sched/idle: Use hrtimer cleanup class in play_idle_precise() Hui Su 0 siblings, 2 replies; 5+ messages in thread From: Hui Su @ 2026-10-16 1:09 UTC (permalink / raw) To: anna-maria, frederic, tglx, mingo, peterz, juri.lelli, vincent.guittot Cc: dietmar.eggemann, rostedt, bsegall, mgorman, vschneid, kprateek.nayak, rafael.j.wysocki, jacob.jun.pan, linux-kernel, Hui Su Add an opt-in cleanup class that pairs hrtimer_setup_on_stack() with destroy_hrtimer_on_stack(), and convert play_idle_precise() to use it. The class provides debug-object teardown, not timer cancellation. The converted caller limits the cleanup object's lifetime to the block covering timer start and the idle wait, which ends only after the pinned hard hrtimer callback has completed. Testing: - x86_64 QEMU with CONFIG_DEBUG_OBJECTS_TIMERS=y: baseline setup=301, play_idle_precise return=301, free=0; the patched kernel recorded setup=301, play_idle_precise return=301, free=301 for the same timer address, with no ODEBUG warning. - x86_64 GCC 15.2.0: the series built bzImage/modules, and kernel/sched/idle.o also built with CONFIG_DEBUG_OBJECTS_TIMERS=n. Changes in v2: - Add an hrtimer_on_stack cleanup class following Peter's suggestion to use __cleanup() for the setup/destroy pairing. - Document the hrtimer_setup_on_stack() lifetime requirements. - Convert play_idle_precise() using CLASS() in an explicit block and explain the callback ordering. v1: https://lore.kernel.org/lkml/20261015025029.3356060-1-sh_def@163.com/ Hui Su (2): hrtimer: Add cleanup class for on-stack timers sched/idle: Use hrtimer cleanup class in play_idle_precise() include/linux/hrtimer.h | 24 ++++++++++++++++++++++++ kernel/sched/idle.c | 16 ++++++++++------ kernel/time/hrtimer.c | 8 ++++++-- 3 files changed, 40 insertions(+), 8 deletions(-) base-commit: 0fcb05b72344f40806cbe810de8872f1698bf053 -- 2.55.0 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 1/2] hrtimer: Add cleanup class for on-stack timers 2026-10-16 1:09 [PATCH v2 0/2] hrtimer: Add cleanup class for on-stack timers Hui Su @ 2026-10-16 1:09 ` Hui Su 2026-10-25 21:47 ` [tip: timers/urgent] " tip-bot2 for Hui Su 2026-10-16 1:09 ` [PATCH v2 2/2] sched/idle: Use hrtimer cleanup class in play_idle_precise() Hui Su 1 sibling, 1 reply; 5+ messages in thread From: Hui Su @ 2026-10-16 1:09 UTC (permalink / raw) To: anna-maria, frederic, tglx, mingo, peterz, juri.lelli, vincent.guittot Cc: dietmar.eggemann, rostedt, bsegall, mgorman, vschneid, kprateek.nayak, rafael.j.wysocki, jacob.jun.pan, linux-kernel, Hui Su 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> --- include/linux/hrtimer.h | 24 ++++++++++++++++++++++++ kernel/time/hrtimer.c | 8 ++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h index 29072d89e5cb..1877beb81d5c 100644 --- a/include/linux/hrtimer.h +++ b/include/linux/hrtimer.h @@ -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); diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c index cbf1693c86b3..12e136d5691f 100644 --- a/kernel/time/hrtimer.c +++ b/kernel/time/hrtimer.c @@ -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 *), -- 2.55.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* [tip: timers/urgent] hrtimer: Add cleanup class for on-stack timers 2026-10-16 1:09 ` [PATCH v2 1/2] " Hui Su @ 2026-10-25 21:47 ` tip-bot2 for Hui Su 0 siblings, 0 replies; 5+ messages in thread From: tip-bot2 for Hui Su @ 2026-10-25 21:47 UTC (permalink / raw) To: linux-tip-commits; +Cc: Hui Su, Thomas Gleixner, x86, linux-kernel The following commit has been merged into the timers/urgent branch of tip: Commit-ID: 064bcd117e8a1a266bb92b840f63862999b7cb87 Gitweb: https://git.kernel.org/tip/064bcd117e8a1a266bb92b840f63862999b7cb87 Author: Hui Su <sh_def@163.com> AuthorDate: Fri, 16 Oct 2026 10:09:43 +09:00 Committer: Thomas Gleixner <tglx@kernel.org> CommitterDate: Sun, 25 Oct 2026 22:43:19 +01:00 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 --- include/linux/hrtimer.h | 24 ++++++++++++++++++++++++ kernel/time/hrtimer.c | 8 ++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h index 29072d8..1877beb 100644 --- a/include/linux/hrtimer.h +++ b/include/linux/hrtimer.h @@ -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); diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c index cbf1693..12e136d 100644 --- a/kernel/time/hrtimer.c +++ b/kernel/time/hrtimer.c @@ -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 *), ^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] sched/idle: Use hrtimer cleanup class in play_idle_precise() 2026-10-16 1:09 [PATCH v2 0/2] hrtimer: Add cleanup class for on-stack timers Hui Su 2026-10-16 1:09 ` [PATCH v2 1/2] " Hui Su @ 2026-10-16 1:09 ` Hui Su 2026-10-25 21:47 ` [tip: timers/urgent] " tip-bot2 for Hui Su 1 sibling, 1 reply; 5+ messages in thread From: Hui Su @ 2026-10-16 1:09 UTC (permalink / raw) To: anna-maria, frederic, tglx, mingo, peterz, juri.lelli, vincent.guittot Cc: dietmar.eggemann, rostedt, bsegall, mgorman, vschneid, kprateek.nayak, rafael.j.wysocki, jacob.jun.pan, linux-kernel, Hui Su Pair on-stack setup with destruction using the hrtimer_on_stack cleanup class in play_idle_precise(). Keep the cleanup object's scope around timer start and the idle wait. Start the timer in pinned hard mode, so its callback runs as a hardirq on the current CPU. The interrupted task observes it.done only after the callback returns. Since the callback returns HRTIMER_NORESTART, the timer is not requeued before the cleanup object tears down its on-stack debug object. Fixes: c1de45ca831a ("sched/idle: Add support for tasks that inject idle") Signed-off-by: Hui Su <sh_def@163.com> --- kernel/sched/idle.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c index eb73b65ce6c4..2880e01368b8 100644 --- a/kernel/sched/idle.c +++ b/kernel/sched/idle.c @@ -429,13 +429,17 @@ void play_idle_precise(u64 duration_ns, u64 latency_ns) cpuidle_use_deepest_state(latency_ns); it.done = 0; - hrtimer_setup_on_stack(&it.timer, idle_inject_timer_fn, CLOCK_MONOTONIC, - HRTIMER_MODE_REL_HARD); - hrtimer_start(&it.timer, ns_to_ktime(duration_ns), - HRTIMER_MODE_REL_PINNED_HARD); + { + CLASS(hrtimer_on_stack, timer)(&it.timer, idle_inject_timer_fn, + CLOCK_MONOTONIC, + HRTIMER_MODE_REL_HARD); - while (!READ_ONCE(it.done)) - do_idle(); + hrtimer_start(timer, ns_to_ktime(duration_ns), + HRTIMER_MODE_REL_PINNED_HARD); + + while (!READ_ONCE(it.done)) + do_idle(); + } cpuidle_use_deepest_state(0); current->flags &= ~PF_IDLE; -- 2.55.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* [tip: timers/urgent] sched/idle: Use hrtimer cleanup class in play_idle_precise() 2026-10-16 1:09 ` [PATCH v2 2/2] sched/idle: Use hrtimer cleanup class in play_idle_precise() Hui Su @ 2026-10-25 21:47 ` tip-bot2 for Hui Su 0 siblings, 0 replies; 5+ messages in thread From: tip-bot2 for Hui Su @ 2026-10-25 21:47 UTC (permalink / raw) To: linux-tip-commits; +Cc: Hui Su, Thomas Gleixner, x86, linux-kernel The following commit has been merged into the timers/urgent branch of tip: Commit-ID: bd091346971c88fccd44afd2547d28366f824572 Gitweb: https://git.kernel.org/tip/bd091346971c88fccd44afd2547d28366f824572 Author: Hui Su <sh_def@163.com> AuthorDate: Fri, 16 Oct 2026 10:09:44 +09:00 Committer: Thomas Gleixner <tglx@kernel.org> CommitterDate: Sun, 25 Oct 2026 22:43:19 +01:00 sched/idle: Use hrtimer cleanup class in play_idle_precise() Pair on-stack setup with destruction using the hrtimer_on_stack cleanup class in play_idle_precise(). Keep the cleanup object's scope around timer start and the idle wait. Start the timer in pinned hard mode, so its callback runs as a hardirq on the current CPU. The interrupted task observes it.done only after the callback returns. Since the callback returns HRTIMER_NORESTART, the timer is not requeued before the cleanup object tears down its on-stack debug object. Fixes: c1de45ca831a ("sched/idle: Add support for tasks that inject idle") Signed-off-by: Hui Su <sh_def@163.com> Signed-off-by: Thomas Gleixner <tglx@kernel.org> Link: https://patch.msgid.link/eccd97b2003a16291817974798cbe98b25b582a0.1792083743.git.sh_def@163.com --- kernel/sched/idle.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c index eb73b65..2880e01 100644 --- a/kernel/sched/idle.c +++ b/kernel/sched/idle.c @@ -429,13 +429,17 @@ void play_idle_precise(u64 duration_ns, u64 latency_ns) cpuidle_use_deepest_state(latency_ns); it.done = 0; - hrtimer_setup_on_stack(&it.timer, idle_inject_timer_fn, CLOCK_MONOTONIC, - HRTIMER_MODE_REL_HARD); - hrtimer_start(&it.timer, ns_to_ktime(duration_ns), - HRTIMER_MODE_REL_PINNED_HARD); + { + CLASS(hrtimer_on_stack, timer)(&it.timer, idle_inject_timer_fn, + CLOCK_MONOTONIC, + HRTIMER_MODE_REL_HARD); - while (!READ_ONCE(it.done)) - do_idle(); + hrtimer_start(timer, ns_to_ktime(duration_ns), + HRTIMER_MODE_REL_PINNED_HARD); + + while (!READ_ONCE(it.done)) + do_idle(); + } cpuidle_use_deepest_state(0); current->flags &= ~PF_IDLE; ^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-10-25 21:47 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-10-16 1:09 [PATCH v2 0/2] hrtimer: Add cleanup class for on-stack timers Hui Su 2026-10-16 1:09 ` [PATCH v2 1/2] " Hui Su 2026-10-25 21:47 ` [tip: timers/urgent] " tip-bot2 for Hui Su 2026-10-16 1:09 ` [PATCH v2 2/2] sched/idle: Use hrtimer cleanup class in play_idle_precise() Hui Su 2026-10-25 21:47 ` [tip: timers/urgent] " tip-bot2 for Hui Su
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox