Skip to content

Commit 8f02e35

Browse files
covanamKAGA-KOKO
authored andcommitted
hrtimers: Introduce hrtimer_update_function()
Some users of hrtimer need to change the callback function after the initial setup. They write to hrtimer::function directly. That's not safe under all circumstances as the write is lockless and a concurrent timer expiry might end up using the wrong function pointer. Introduce hrtimer_update_function(), which also performs runtime checks whether it is safe to modify the callback. This allows to make hrtimer::function private once all users are converted. Signed-off-by: Nam Cao <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/all/20a937b0ae09ad54b5b6d86eabead7c570f1b72e.1730386209.git.namcao@linutronix.de
1 parent c9bd83a commit 8f02e35

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

include/linux/hrtimer.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,28 @@ static inline int hrtimer_callback_running(struct hrtimer *timer)
327327
return timer->base->running == timer;
328328
}
329329

330+
/**
331+
* hrtimer_update_function - Update the timer's callback function
332+
* @timer: Timer to update
333+
* @function: New callback function
334+
*
335+
* Only safe to call if the timer is not enqueued. Can be called in the callback function if the
336+
* timer is not enqueued at the same time (see the comments above HRTIMER_STATE_ENQUEUED).
337+
*/
338+
static inline void hrtimer_update_function(struct hrtimer *timer,
339+
enum hrtimer_restart (*function)(struct hrtimer *))
340+
{
341+
guard(raw_spinlock_irqsave)(&timer->base->cpu_base->lock);
342+
343+
if (WARN_ON_ONCE(hrtimer_is_queued(timer)))
344+
return;
345+
346+
if (WARN_ON_ONCE(!function))
347+
return;
348+
349+
timer->function = function;
350+
}
351+
330352
/* Forward a hrtimer so it expires after now: */
331353
extern u64
332354
hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);

0 commit comments

Comments
 (0)