clocksource: add devm_clocksource_register_*() helpers
PATCH 1/1 clocksource: add devm_clocksource_register_*() helpers
Introduce device-managed helpers for clocksource registration.
The clocksource framework currently provides __clocksource_register_scale() along with convenience wrappers for Hz and kHz registration. However, drivers must handle error paths and cleanup manually, typically by pairing registration with an explicit clocksource_unregister() call.
Add a devm-based variant, __devm_clocksource_register_scale(), along with devm_clocksource_register_hz() and devm_clocksource_register_khz() helpers.
These helpers register the clocksource and attach a devres action to automatically unregister it on driver detach or probe failure.
This simplifies driver code by:
* removing explicit cleanup paths
* ensuring correct teardown ordering
* aligning with the devm-based resource management model widely used across the kernel
While drivers can open-code devm_add_action_or_reset(), providing a dedicated helper avoids duplication, reduces boilerplate, and ensures consistent usage across drivers, following patterns used in other subsystems.
This is also particularly useful for drivers built as modules, where device-managed resource handling avoids manual cleanup in remove paths and ensures correct teardown on module unload.
This helper is self-contained and can be adopted progressively by drivers.
No functional change.
--- include/linux/clocksource.h | 15 +++++++++++++++ kernel/time/clocksource.c | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+)
include/linux/clocksource.h +15 -0
@@ -236,6 +236,9 @@ clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec);*/ extern int __clocksource_register_scale (struct clocksource *cs,u32 scale,u32 freq); +extern int +__devm_clocksource_register_scale (struct device *dev,struct clocksource *cs, +u32 scale,u32 freq);extern void __clocksource_update_freq_scale (struct clocksource *cs,u32 scale,u32 freq);
@@ -258,6 +261,18 @@ static inline int clocksource_register_khz(struct clocksource *cs, u32 khz)return __clocksource_register_scale (cs,1000 , khz); } +static inline int devm_clocksource_register_hz (struct device *dev, +struct clocksource *cs,u32 hz) +{ +return __devm_clocksource_register_scale (dev, cs,1 , hz); +} + +static inline int devm_clocksource_register_khz (struct device *dev, +struct clocksource *cs,u32 khz) +{ +return __devm_clocksource_register_scale (dev, cs,1000 , khz); +} +static inline void __clocksource_update_freq_hz (struct clocksource *cs,u32 hz) {__clocksource_update_freq_scale (cs,1 , hz);
kernel/time/clocksource.c +20 -0
@@ -1338,6 +1338,26 @@ int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq) }EXPORT_SYMBOL_GPL (__clocksource_register_scale); +static void __devm_clocksource_unregister (void *data) +{ +struct clocksource *cs = data; + +clocksource_unregister (cs); +} + +int __devm_clocksource_register_scale (struct device *dev,struct clocksource *cs, +u32 scale,u32 freq) +{ +int ret; + + ret =__clocksource_register_scale (cs, scale, freq); +if (ret) +return ret; + +return devm_add_action_or_reset (dev, __devm_clocksource_unregister, cs); +} +EXPORT_SYMBOL_GPL (__devm_clocksource_register_scale); +/* * Unbind clocksource @cs. Called with clocksource_mutex held */
Signature · 1 line
2.43.0
[tip: timers/clocksource] clocksource: Add devm_clocksource_register_*() helpers
The following commit has been merged into the timers/clocksource branch of tip:
The following commit has been merged into the timers/clocksource branch of tip:
Commit-ID: 3eb4923e68511741f3eb3fab55ed1e8ded9e4da8 Gitweb: https://git.kernel.org/tip/3eb4923e68511741f3eb3fab55ed1e8ded9e4da8 Author: Daniel Lezcano <[email protected]> AuthorDate: Wed, 06 May 2026 17:38:31 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Mon, 18 May 2026 11:02:51 +02:00
clocksource: Add devm_clocksource_register_*() helpers
Introduce device-managed helpers for clocksource registration.
The clocksource framework currently provides __clocksource_register_scale() along with convenience wrappers for Hz and kHz registration. However, drivers must handle error paths and cleanup manually, typically by pairing registration with an explicit clocksource_unregister() call.
Add a devm-based variant, __devm_clocksource_register_scale(), along with devm_clocksource_register_hz() and devm_clocksource_register_khz() helpers.
These helpers register the clocksource and attach a devres action to automatically unregister it on driver detach or probe failure.
This simplifies driver code by:
* removing explicit cleanup paths
* ensuring correct teardown ordering
* aligning with the devm-based resource management model widely used across the kernel
While drivers can open-code devm_add_action_or_reset(), providing a dedicated helper avoids duplication, reduces boilerplate, and ensures consistent usage across drivers, following patterns used in other subsystems.
This is also particularly useful for drivers built as modules, where device-managed resource handling avoids manual cleanup in remove paths and ensures correct teardown on module unload.
This helper is self-contained and can be adopted progressively by drivers.
No functional change.
--- include/linux/clocksource.h | 15 +++++++++++++++ kernel/time/clocksource.c | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+)
include/linux/clocksource.h +15 -0
@@ -236,6 +236,9 @@ clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec);*/ extern int __clocksource_register_scale (struct clocksource *cs,u32 scale,u32 freq); +extern int +__devm_clocksource_register_scale (struct device *dev,struct clocksource *cs, +u32 scale,u32 freq);extern void __clocksource_update_freq_scale (struct clocksource *cs,u32 scale,u32 freq);
@@ -258,6 +261,18 @@ static inline int clocksource_register_khz(struct clocksource *cs, u32 khz)return __clocksource_register_scale (cs,1000 , khz); } +static inline int devm_clocksource_register_hz (struct device *dev, +struct clocksource *cs,u32 hz) +{ +return __devm_clocksource_register_scale (dev, cs,1 , hz); +} + +static inline int devm_clocksource_register_khz (struct device *dev, +struct clocksource *cs,u32 khz) +{ +return __devm_clocksource_register_scale (dev, cs,1000 , khz); +} +static inline void __clocksource_update_freq_hz (struct clocksource *cs,u32 hz) {__clocksource_update_freq_scale (cs,1 , hz);
kernel/time/clocksource.c +20 -0
@@ -1338,6 +1338,26 @@ int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq) }EXPORT_SYMBOL_GPL (__clocksource_register_scale); +static void __devm_clocksource_unregister (void *data) +{ +struct clocksource *cs = data; + +clocksource_unregister (cs); +} + +int __devm_clocksource_register_scale (struct device *dev,struct clocksource *cs, +u32 scale,u32 freq) +{ +int ret; + + ret =__clocksource_register_scale (cs, scale, freq); +if (ret) +return ret; + +return devm_add_action_or_reset (dev, __devm_clocksource_unregister, cs); +} +EXPORT_SYMBOL_GPL (__devm_clocksource_register_scale); +/* * Unbind clocksource @cs. Called with clocksource_mutex held */