[PATCH v1 0/7] Setting the scene to convert the timers into modules

Daniel Lezcano posted 7 patches 6 months, 2 weeks ago
drivers/clocksource/scx200_hrt.c     | 1 +
drivers/clocksource/timer-cs5535.c   | 1 +
drivers/clocksource/timer-nxp-stm.c  | 2 ++
drivers/clocksource/timer-stm32-lp.c | 1 +
drivers/clocksource/timer-sun5i.c    | 2 ++
drivers/clocksource/timer-tegra186.c | 3 +++
kernel/time/sched_clock.c            | 4 ++--
7 files changed, 12 insertions(+), 2 deletions(-)
[PATCH v1 0/7] Setting the scene to convert the timers into modules
Posted by Daniel Lezcano 6 months, 2 weeks ago
The timer drivers are all compiled-in. The initial pre-requisite is to
have them available as soon as possible in the boot process. While
this statement made sense a long time ago, the platforms have today
multiple timers for different purposes along with architected timers
which are initialized very early. For example, a timer can be used as
a backup timer when the local timers are belonging to a power domain
which is shutted down, or used a watchdog timer when the counter are
shared, or also as a pulse width modulation counter. Another use case
is the platform user may want to switch to a timer different from the
architected timers because they have interesting characteristics in
the context of a dedicated platform (eg. automotive).

In some existing drivers, there is already the code to load and unload
a timer driver even if the Kconfig does not allow that. It means, the
need is there but partially upstream.

There were multiple attempts to configure the timer drivers into
modules but it faced the fact that we were unsure if it is correctly
supported by the time framework.

After investigating deeper in the core code it appears we have
everything set for the modularization of the timer drivers.

 - When a clocksource is registered with a better rating, the current
   clocksource is swapped with the new one. The userspace allows to
   change the current clocksource via sysfs

 - A clocksource can be unregistered

 - When a clockevent is registered with a better rating, it becomes
   the active one

 - A clockevent can not be unregistered

A timer driver can be loaded later because of all the supported
above. However unloading is unsupported because a clockevent can not
be unregistered and that will lead to a crash.

But if the timer driver has the module owner set, the core framework
will handle the refcount correctly and will prevent to unload the
module if a clockevent is registered. All the refcounting is working
in different use cases.

 - A clocksource is the current clocksource, the refcount is held

 - A current clocksource is switched to another one, the refcount is
   released

 - A broadcast timer is registered, the refcount is held

 - A local timer is registered, the refcount is held

Consequently, it is possible to unload a module which is only used as
a clocksource. As soon as a clockevent is registered, the refcount is
held and can not be released thus preventing the module to be
unloaded.

That mechanism ensure it is safe to convert the different timer
drivers into modules.

This series adds the module owner in the different driver which are
initialized with the module_platform_driver() function and export the
symbols for the sched_clock_register() function.

Cc: Jim Cromie <jim.cromie@gmail.com>
Cc: Daniel Lezcano <daniel.lezcano@linaro.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>
Cc: Alexandre Torgue <alexandre.torgue@foss.st.com>
Cc: Chen-Yu Tsai <wens@csie.org>
Cc: Jernej Skrabec <jernej.skrabec@gmail.com>
Cc: Samuel Holland <samuel@sholland.org>
Cc: Thierry Reding <thierry.reding@gmail.com>
Cc: Jonathan Hunter <jonathanh@nvidia.com>
Cc: "Peter Zijlstra (Intel)" <peterz@infradead.org>
Cc: Marco Elver <elver@google.com>
Cc: Nam Cao <namcao@linutronix.de>
Cc: linux-kernel@vger.kernel.org
Cc: linux-stm32@st-md-mailman.stormreply.com
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-sunxi@lists.linux.dev
Cc: linux-tegra@vger.kernel.org
Cc: John Stulz <jstultz@google.com>
Cc: Will McVicker <willmcvicker@google.com>
Cc: Peter Griffin <peter.griffin@linaro.org>
Cc: Saravan Kanna <saravanak@google.com>


Daniel Lezcano (7):
  clocksource/drivers/scx200: Add module owner
  clocksource/drivers/stm32-lp: Add module owner
  clocksource/drivers/sun5i: Add module owner
  clocksource/drivers/tegra186: Add module owner
  clocksource/drivers/stm: Add module owner
  clocksource/drivers/cs5535: Add module owner
  time: Export symbol for sched_clock register function

 drivers/clocksource/scx200_hrt.c     | 1 +
 drivers/clocksource/timer-cs5535.c   | 1 +
 drivers/clocksource/timer-nxp-stm.c  | 2 ++
 drivers/clocksource/timer-stm32-lp.c | 1 +
 drivers/clocksource/timer-sun5i.c    | 2 ++
 drivers/clocksource/timer-tegra186.c | 3 +++
 kernel/time/sched_clock.c            | 4 ++--
 7 files changed, 12 insertions(+), 2 deletions(-)

-- 
2.43.0
Re: [PATCH v1 0/7] Setting the scene to convert the timers into modules
Posted by Daniel Lezcano 4 months ago
Hi Thomas,

was this series dropped ?

I'm not able to find it in v6.17-rc1 but it is in the master branch of 
the tip tree (v6.16-rc7)


On 02/06/2025 17:18, Daniel Lezcano wrote:
> The timer drivers are all compiled-in. The initial pre-requisite is to
> have them available as soon as possible in the boot process. While
> this statement made sense a long time ago, the platforms have today
> multiple timers for different purposes along with architected timers
> which are initialized very early. For example, a timer can be used as
> a backup timer when the local timers are belonging to a power domain
> which is shutted down, or used a watchdog timer when the counter are
> shared, or also as a pulse width modulation counter. Another use case
> is the platform user may want to switch to a timer different from the
> architected timers because they have interesting characteristics in
> the context of a dedicated platform (eg. automotive).
> 
> In some existing drivers, there is already the code to load and unload
> a timer driver even if the Kconfig does not allow that. It means, the
> need is there but partially upstream.
> 
> There were multiple attempts to configure the timer drivers into
> modules but it faced the fact that we were unsure if it is correctly
> supported by the time framework.
> 
> After investigating deeper in the core code it appears we have
> everything set for the modularization of the timer drivers.
> 
>   - When a clocksource is registered with a better rating, the current
>     clocksource is swapped with the new one. The userspace allows to
>     change the current clocksource via sysfs
> 
>   - A clocksource can be unregistered
> 
>   - When a clockevent is registered with a better rating, it becomes
>     the active one
> 
>   - A clockevent can not be unregistered
> 
> A timer driver can be loaded later because of all the supported
> above. However unloading is unsupported because a clockevent can not
> be unregistered and that will lead to a crash.
> 
> But if the timer driver has the module owner set, the core framework
> will handle the refcount correctly and will prevent to unload the
> module if a clockevent is registered. All the refcounting is working
> in different use cases.
> 
>   - A clocksource is the current clocksource, the refcount is held
> 
>   - A current clocksource is switched to another one, the refcount is
>     released
> 
>   - A broadcast timer is registered, the refcount is held
> 
>   - A local timer is registered, the refcount is held
> 
> Consequently, it is possible to unload a module which is only used as
> a clocksource. As soon as a clockevent is registered, the refcount is
> held and can not be released thus preventing the module to be
> unloaded.
> 
> That mechanism ensure it is safe to convert the different timer
> drivers into modules.
> 
> This series adds the module owner in the different driver which are
> initialized with the module_platform_driver() function and export the
> symbols for the sched_clock_register() function.
> 
> Cc: Jim Cromie <jim.cromie@gmail.com>
> Cc: Daniel Lezcano <daniel.lezcano@linaro.org>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>
> Cc: Alexandre Torgue <alexandre.torgue@foss.st.com>
> Cc: Chen-Yu Tsai <wens@csie.org>
> Cc: Jernej Skrabec <jernej.skrabec@gmail.com>
> Cc: Samuel Holland <samuel@sholland.org>
> Cc: Thierry Reding <thierry.reding@gmail.com>
> Cc: Jonathan Hunter <jonathanh@nvidia.com>
> Cc: "Peter Zijlstra (Intel)" <peterz@infradead.org>
> Cc: Marco Elver <elver@google.com>
> Cc: Nam Cao <namcao@linutronix.de>
> Cc: linux-kernel@vger.kernel.org
> Cc: linux-stm32@st-md-mailman.stormreply.com
> Cc: linux-arm-kernel@lists.infradead.org
> Cc: linux-sunxi@lists.linux.dev
> Cc: linux-tegra@vger.kernel.org
> Cc: John Stulz <jstultz@google.com>
> Cc: Will McVicker <willmcvicker@google.com>
> Cc: Peter Griffin <peter.griffin@linaro.org>
> Cc: Saravan Kanna <saravanak@google.com>
> 
> 
> Daniel Lezcano (7):
>    clocksource/drivers/scx200: Add module owner
>    clocksource/drivers/stm32-lp: Add module owner
>    clocksource/drivers/sun5i: Add module owner
>    clocksource/drivers/tegra186: Add module owner
>    clocksource/drivers/stm: Add module owner
>    clocksource/drivers/cs5535: Add module owner
>    time: Export symbol for sched_clock register function
> 
>   drivers/clocksource/scx200_hrt.c     | 1 +
>   drivers/clocksource/timer-cs5535.c   | 1 +
>   drivers/clocksource/timer-nxp-stm.c  | 2 ++
>   drivers/clocksource/timer-stm32-lp.c | 1 +
>   drivers/clocksource/timer-sun5i.c    | 2 ++
>   drivers/clocksource/timer-tegra186.c | 3 +++
>   kernel/time/sched_clock.c            | 4 ++--
>   7 files changed, 12 insertions(+), 2 deletions(-)
> 


-- 
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
Re: [PATCH v1 0/7] Setting the scene to convert the timers into modules
Posted by William McVicker 6 months, 2 weeks ago
On 06/02/2025, Daniel Lezcano wrote:
> The timer drivers are all compiled-in. The initial pre-requisite is to
> have them available as soon as possible in the boot process. While
> this statement made sense a long time ago, the platforms have today
> multiple timers for different purposes along with architected timers
> which are initialized very early. For example, a timer can be used as
> a backup timer when the local timers are belonging to a power domain
> which is shutted down, or used a watchdog timer when the counter are
> shared, or also as a pulse width modulation counter. Another use case
> is the platform user may want to switch to a timer different from the
> architected timers because they have interesting characteristics in
> the context of a dedicated platform (eg. automotive).
> 
> In some existing drivers, there is already the code to load and unload
> a timer driver even if the Kconfig does not allow that. It means, the
> need is there but partially upstream.
> 
> There were multiple attempts to configure the timer drivers into
> modules but it faced the fact that we were unsure if it is correctly
> supported by the time framework.
> 
> After investigating deeper in the core code it appears we have
> everything set for the modularization of the timer drivers.
> 
>  - When a clocksource is registered with a better rating, the current
>    clocksource is swapped with the new one. The userspace allows to
>    change the current clocksource via sysfs
> 
>  - A clocksource can be unregistered
> 
>  - When a clockevent is registered with a better rating, it becomes
>    the active one
> 
>  - A clockevent can not be unregistered
> 
> A timer driver can be loaded later because of all the supported
> above. However unloading is unsupported because a clockevent can not
> be unregistered and that will lead to a crash.
> 
> But if the timer driver has the module owner set, the core framework
> will handle the refcount correctly and will prevent to unload the
> module if a clockevent is registered. All the refcounting is working
> in different use cases.
> 
>  - A clocksource is the current clocksource, the refcount is held
> 
>  - A current clocksource is switched to another one, the refcount is
>    released
> 
>  - A broadcast timer is registered, the refcount is held
> 
>  - A local timer is registered, the refcount is held
> 
> Consequently, it is possible to unload a module which is only used as
> a clocksource. As soon as a clockevent is registered, the refcount is
> held and can not be released thus preventing the module to be
> unloaded.
> 
> That mechanism ensure it is safe to convert the different timer
> drivers into modules.
> 
> This series adds the module owner in the different driver which are
> initialized with the module_platform_driver() function and export the
> symbols for the sched_clock_register() function.
> 

Thanks Daniel for taking the time to dig into this deeper to help identify how
we can safely convert the timer drivers to modules! The series LGTM. I'll go
ahead and address the review comments on my MCT series and rebase it on top of
your patch series.

Thanks,
Will

<cut>