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.

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 <namcao@linutronix.de>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/all/20a937b0ae09ad54b5b6d86eabead7c570f1b72e.1730386209.git.namcao@linutronix.de

authored by

Nam Cao and committed by
Thomas Gleixner
8f02e356 c9bd83ab

+22
+22
include/linux/hrtimer.h
··· 327 327 return timer->base->running == timer; 328 328 } 329 329 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 + 330 352 /* Forward a hrtimer so it expires after now: */ 331 353 extern u64 332 354 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);