mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] time/sched_clock: Call enable_sched_clock_irqtime() with IRQs enabled
@ 2026-11-13 16:40 Benoît Monin
  2026-11-13 16:49 ` Herve Codina
  0 siblings, 1 reply; 2+ messages in thread
From: Benoît Monin @ 2026-11-13 16:40 UTC (permalink / raw)
  To: Thomas Gleixner, John Stultz
  Cc: Daniel Lezcano, Stephen Boyd, Paul Cercueil, Thomas Petazzoni,
	Chen-Yu Tsai, Herve Codina, linux-kernel, Benoît Monin

sched_clock_register() disables interrupts with local_irq_save() and
calls enable_sched_clock_irqtime() before restoring them.

With CONFIG_IRQ_TIME_ACCOUNTING, enable_sched_clock_irqtime() calls
static_branch_enable(). With CONFIG_JUMP_LABEL and CONFIG_HOTPLUG_CPU,
this takes cpus_read_lock(), which eventually calls might_sleep() from
percpu_down_read_internal(). So with CONFIG_DEBUG_ATOMIC_SLEEP the
following is reported:

  BUG: sleeping function called from invalid context at include/linux/percpu-rwsem.h:51
  ...
  Call Trace:
  ...
  [<a8000001008ad9c4>] __might_resched+0x3ac/0x3d0
  [<a8000001008598b0>] percpu_down_read_internal.constprop.0+0x30/0x1b8
  [<a800000100af5ddc>] static_key_enable+0x14/0x30
  [<a80000010099b600>] sched_clock_register+0x2a0/0x370
  ...

Most callers of sched_clock_register() are early init functions,
for which might_sleep() stays silent because system_state is still
SYSTEM_BOOTING. However, platform drivers such as timer-nxp-stm,
timer-ti-dm, timer-mediatek and timer-rzn1, as well as the mips-gic-timer
promotion work, call it later, where the warning is reported.

Only the sched_clock data update requires interrupts to be disabled.
Enabling IRQ time accounting only flips a static branch, so restore
interrupts before calling enable_sched_clock_irqtime().

Fixes: 2707745533d6 ("time/sched_clock: Disable interrupts in sched_clock_register()")
Reported-by: Chen-Yu Tsai <wenst@chromium.org>
Closes: https://lore.kernel.org/lkml/20261109095036.GA722431@google.com/
Signed-off-by: Benoît Monin <benoit.monin@bootlin.com>
---
Found the sleeping-in-atomic warning while working on the GIC
synchronization series [1] and testing configuration options on the
Mobileye EyeQ6H SoC, but the issue is not limited to that clock source.

Chen-Yu Tsai reported the same issue with timer-mediatek.

I also discussed the issue with my colleague Hervé, who reproduced the
BUG on a board using timer-rzn1.

[1]: https://lore.kernel.org/lkml/20260929-sync-gic-counters-v4-0-ec70c4b60434@bootlin.com/
---
 kernel/time/sched_clock.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/kernel/time/sched_clock.c b/kernel/time/sched_clock.c
index f3aaef695b8c..b4e317b8a585 100644
--- a/kernel/time/sched_clock.c
+++ b/kernel/time/sched_clock.c
@@ -238,12 +238,13 @@ void sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
 	pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
 		bits, r, r_unit, res, wrap);
 
+	/* Restore interrupts now as enable_sched_clock_irqtime() might sleep */
+	local_irq_restore(flags);
+
 	/* Enable IRQ time accounting if we have a fast enough sched_clock() */
 	if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))
 		enable_sched_clock_irqtime();
 
-	local_irq_restore(flags);
-
 	pr_debug("Registered %pS as sched_clock source\n", read);
 }
 EXPORT_SYMBOL_GPL(sched_clock_register);

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261012-sched-clock-might-sleep-48af26c8911b

Best regards,
--  
Benoît Monin, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] time/sched_clock: Call enable_sched_clock_irqtime() with IRQs enabled
  2026-11-13 16:40 [PATCH] time/sched_clock: Call enable_sched_clock_irqtime() with IRQs enabled Benoît Monin
@ 2026-11-13 16:49 ` Herve Codina
  0 siblings, 0 replies; 2+ messages in thread
From: Herve Codina @ 2026-11-13 16:49 UTC (permalink / raw)
  To: Benoît Monin
  Cc: Thomas Gleixner, John Stultz, Daniel Lezcano, Stephen Boyd,
	Paul Cercueil, Thomas Petazzoni, Chen-Yu Tsai, linux-kernel

Hi Benoît,

On Fri, 13 Nov 2026 17:40:09 +0100
Benoît Monin <benoit.monin@bootlin.com> wrote:

> sched_clock_register() disables interrupts with local_irq_save() and
> calls enable_sched_clock_irqtime() before restoring them.
> 
> With CONFIG_IRQ_TIME_ACCOUNTING, enable_sched_clock_irqtime() calls
> static_branch_enable(). With CONFIG_JUMP_LABEL and CONFIG_HOTPLUG_CPU,
> this takes cpus_read_lock(), which eventually calls might_sleep() from
> percpu_down_read_internal(). So with CONFIG_DEBUG_ATOMIC_SLEEP the
> following is reported:
> 
>   BUG: sleeping function called from invalid context at include/linux/percpu-rwsem.h:51
>   ...
>   Call Trace:
>   ...
>   [<a8000001008ad9c4>] __might_resched+0x3ac/0x3d0
>   [<a8000001008598b0>] percpu_down_read_internal.constprop.0+0x30/0x1b8
>   [<a800000100af5ddc>] static_key_enable+0x14/0x30
>   [<a80000010099b600>] sched_clock_register+0x2a0/0x370
>   ...
> 
> Most callers of sched_clock_register() are early init functions,
> for which might_sleep() stays silent because system_state is still
> SYSTEM_BOOTING. However, platform drivers such as timer-nxp-stm,
> timer-ti-dm, timer-mediatek and timer-rzn1, as well as the mips-gic-timer
> promotion work, call it later, where the warning is reported.
> 
> Only the sched_clock data update requires interrupts to be disabled.
> Enabling IRQ time accounting only flips a static branch, so restore
> interrupts before calling enable_sched_clock_irqtime().
> 
> Fixes: 2707745533d6 ("time/sched_clock: Disable interrupts in sched_clock_register()")
> Reported-by: Chen-Yu Tsai <wenst@chromium.org>
> Closes: https://lore.kernel.org/lkml/20261109095036.GA722431@google.com/
> Signed-off-by: Benoît Monin <benoit.monin@bootlin.com>
> ---
> Found the sleeping-in-atomic warning while working on the GIC
> synchronization series [1] and testing configuration options on the
> Mobileye EyeQ6H SoC, but the issue is not limited to that clock source.
> 
> Chen-Yu Tsai reported the same issue with timer-mediatek.
> 
> I also discussed the issue with my colleague Hervé, who reproduced the
> BUG on a board using timer-rzn1.
> 
> [1]: https://lore.kernel.org/lkml/20260929-sync-gic-counters-v4-0-ec70c4b60434@bootlin.com/
> ---
>  kernel/time/sched_clock.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)

And I confirm that the patch fixes the BUG reproduced on my RZ/N1 board.

Tested-by: Herve Codina <herve.codina@bootlin.com>

Best regards,
Hervé

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-13 16:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-13 16:40 [PATCH] time/sched_clock: Call enable_sched_clock_irqtime() with IRQs enabled Benoît Monin
2026-11-13 16:49 ` Herve Codina

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®