[PATCH v1 1/2] hrtimer: Add hrtimer_forward_safe_from_callback()

From: FUJITA Tomonori

Date: Sun Nov 01 2026 - 18:35:41 EST


From: FUJITA Tomonori <fujita.tomonori@xxxxxxxxx>

A hrtimer can be started while its callback runs. hrtimer_forward()
writes the expiry without the base lock, so the caller has to serialize
it against a concurrent hrtimer_start(). Otherwise the write can change
the sort key of a queued timer.

Add a variant which does the check and the update with the base lock
held. If the timer is already queued, the expiry is left alone and the
expiry set by hrtimer_start() wins.

The first user is the Rust hrtimer abstraction. Safe Rust code can
start a timer while its callback runs, so forward() in the callback
can only be a safe API if the core handles a concurrent start.

Suggested-by: Thomas Gleixner <tglx@xxxxxxxxxx>
Link: https://lore.kernel.org/all/877bk3ixp5.ffs@fw13/
Signed-off-by: FUJITA Tomonori <fujita.tomonori@xxxxxxxxx>
---
include/linux/hrtimer.h | 4 ++++
kernel/time/hrtimer.c | 30 ++++++++++++++++++++++++++++++
2 files changed, 34 insertions(+)

diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h
index acfc5f93d967..f3c8542a2dbd 100644
--- a/include/linux/hrtimer.h
+++ b/include/linux/hrtimer.h
@@ -317,6 +317,10 @@ void hrtimer_update_function(struct hrtimer *timer,
extern u64
hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);

+extern u64
+hrtimer_forward_safe_from_callback(struct hrtimer *timer, ktime_t now,
+ ktime_t interval);
+
/**
* hrtimer_forward_now() - forward the timer expiry so it expires after now
* @timer: hrtimer to forward
diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index a9dc2d53aa92..9869827585af 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -1121,6 +1121,36 @@ u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
}
EXPORT_SYMBOL_GPL(hrtimer_forward);

+/**
+ * hrtimer_forward_safe_from_callback() - forward the timer expiry under the base lock
+ * @timer: hrtimer to forward
+ * @now: forward past this time
+ * @interval: the interval to forward
+ *
+ * Variant of hrtimer_forward() which is safe against a concurrent
+ * hrtimer_start(). The expiry is updated with the base lock held. If the
+ * timer is already queued, the expiry is not changed.
+ *
+ * Context: Only from the callback function of @timer.
+ *
+ * Return: The number of overruns, or 0 if the expiry was not changed.
+ */
+u64 hrtimer_forward_safe_from_callback(struct hrtimer *timer, ktime_t now,
+ ktime_t interval)
+{
+ unsigned long flags;
+ u64 orun = 0;
+
+ lock_hrtimer_base(timer, &flags);
+
+ if (!timer->is_queued)
+ orun = hrtimer_forward(timer, now, interval);
+
+ unlock_hrtimer_base(timer, &flags);
+ return orun;
+}
+EXPORT_SYMBOL_GPL(hrtimer_forward_safe_from_callback);
+
/*
* enqueue_hrtimer - internal function to (re)start a timer
*
--
2.43.0