Skip to content

Commit 908a1d7

Browse files
covanamKAGA-KOKO
authored andcommitted
hrtimers: Introduce hrtimer_setup() to replace hrtimer_init()
To initialize hrtimer, hrtimer_init() needs to be called and also hrtimer::function must be set. This is error-prone and awkward to use. Introduce hrtimer_setup() which does both of these things, so that users of hrtimer can be simplified. The new setup function also has a sanity check for the provided function pointer. If NULL, a warning is emitted and a dummy callback installed. hrtimer_init() will be removed as soon as all of its users have been converted to the new function. Signed-off-by: Nam Cao <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/all/5057c1ddbfd4b92033cd93d37fe38e6b069d5ba6.1730386209.git.namcao@linutronix.de
1 parent c95d365 commit 908a1d7

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

include/linux/hrtimer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ static inline void hrtimer_cancel_wait_running(struct hrtimer *timer)
228228
/* Initialize timers: */
229229
extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
230230
enum hrtimer_mode mode);
231+
extern void hrtimer_setup(struct hrtimer *timer, enum hrtimer_restart (*function)(struct hrtimer *),
232+
clockid_t clock_id, enum hrtimer_mode mode);
231233
extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
232234
enum hrtimer_mode mode);
233235
extern void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,

kernel/time/hrtimer.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,11 @@ static inline int hrtimer_clockid_to_base(clockid_t clock_id)
15351535
return HRTIMER_BASE_MONOTONIC;
15361536
}
15371537

1538+
static enum hrtimer_restart hrtimer_dummy_timeout(struct hrtimer *unused)
1539+
{
1540+
return HRTIMER_NORESTART;
1541+
}
1542+
15381543
static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
15391544
enum hrtimer_mode mode)
15401545
{
@@ -1571,6 +1576,18 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
15711576
timerqueue_init(&timer->node);
15721577
}
15731578

1579+
static void __hrtimer_setup(struct hrtimer *timer,
1580+
enum hrtimer_restart (*function)(struct hrtimer *),
1581+
clockid_t clock_id, enum hrtimer_mode mode)
1582+
{
1583+
__hrtimer_init(timer, clock_id, mode);
1584+
1585+
if (WARN_ON_ONCE(!function))
1586+
timer->function = hrtimer_dummy_timeout;
1587+
else
1588+
timer->function = function;
1589+
}
1590+
15741591
/**
15751592
* hrtimer_init - initialize a timer to the given clock
15761593
* @timer: the timer to be initialized
@@ -1591,6 +1608,27 @@ void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
15911608
}
15921609
EXPORT_SYMBOL_GPL(hrtimer_init);
15931610

1611+
/**
1612+
* hrtimer_setup - initialize a timer to the given clock
1613+
* @timer: the timer to be initialized
1614+
* @function: the callback function
1615+
* @clock_id: the clock to be used
1616+
* @mode: The modes which are relevant for initialization:
1617+
* HRTIMER_MODE_ABS, HRTIMER_MODE_REL, HRTIMER_MODE_ABS_SOFT,
1618+
* HRTIMER_MODE_REL_SOFT
1619+
*
1620+
* The PINNED variants of the above can be handed in,
1621+
* but the PINNED bit is ignored as pinning happens
1622+
* when the hrtimer is started
1623+
*/
1624+
void hrtimer_setup(struct hrtimer *timer, enum hrtimer_restart (*function)(struct hrtimer *),
1625+
clockid_t clock_id, enum hrtimer_mode mode)
1626+
{
1627+
debug_init(timer, clock_id, mode);
1628+
__hrtimer_setup(timer, function, clock_id, mode);
1629+
}
1630+
EXPORT_SYMBOL_GPL(hrtimer_setup);
1631+
15941632
/**
15951633
* hrtimer_init_on_stack - initialize a timer in stack memory
15961634
* @timer: The timer to be initialized

0 commit comments

Comments
 (0)