Skip to contentSkip to conversation

arm64: topology: Fix false warning in counters_read_on_cpu() for same-CPU reads

PATCH Superseded Sumit Gupta 3 participants 6 messages Jan 27 – Feb 24

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: topology: Fix false warning in counters_read_on_cpu() for same-CPU reads

The counters_read_on_cpu() function warns when called with IRQs disabled to prevent deadlock in smp_call_function_single(). However, this warning is spurious when reading counters on the current CPU since no IPI is needed for same-CPU reads.

Commit 12eb8f4fff24 ("cpufreq: CPPC: Update FIE arch_freq_scale in ticks for non-PCC regs") changed the CPPC Frequency Invariance Engine to read AMU counters directly from the scheduler tick for non-PCC register spaces (like FFH), instead of deferring to a kthread. This means counters_read_on_cpu() is now called with IRQs disabled from the tick handler, triggering the warning:

| WARNING: arch/arm64/kernel/topology.c:410 at counters_read_on_cpu
| ...
| Call trace:
|  counters_read_on_cpu+0x88/0xa8 (P)
|  cpc_read_ffh+0xdc/0x148
|  cpc_read+0x260/0x518
|  cppc_get_perf_ctrs+0xf0/0x398
|  __cppc_scale_freq_tick+0x4c/0x148 [cppc_cpufreq]
|  cppc_scale_freq_tick+0x44/0x88 [cppc_cpufreq]
|  topology_scale_freq_tick+0x34/0x58
|  sched_tick+0x58/0x300
|  update_process_times+0xcc/0x120
|  tick_nohz_handler+0xa8/0x260
|  __hrtimer_run_queues+0x154/0x360
|  hrtimer_interrupt+0xf4/0x2b0
|  arch_timer_handler_phys+0x4c/0x78
|  ....
|  CPPC Cpufreq:__cppc_scale_freq_tick: failed to read perf counters
|  ....

Fix this by calling the counter read function directly for same-CPU case, bypassing smp_call_function_single() entirely. Use get_cpu() to disable preemption as the counter read functions call this_cpu_has_cap() which requires a non-preemptible context.

Fixes 12eb8f4fff24 ("cpufreq: CPPC: Update FIE arch_freq_scale in ticks for non-PCC regs")
---
 arch/arm64/kernel/topology.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
arch/arm64/kernel/topology.c +19 -2
index 539b38935182..57b71f403007 100644
@@ -401,12 +401,29 @@ static inline
 int counters_read_on_cpu(int cpu, smp_call_func_t func, u64 *val)
 {
 	/*
-	 * Abort call on counterless CPU or when interrupts are
-	 * disabled - can lead to deadlock in smp sync call.
+	 * Abort call on counterless CPU.
 	 */
 	if (!cpu_has_amu_feat(cpu))
 		return -EOPNOTSUPP;
 
+	/*
+	 * For same-CPU reads, call the function directly since no IPI
+	 * is needed and this is safe even with IRQs disabled.
+	 * Use get_cpu() to disable preemption as the counter read
+	 * functions call this_cpu_has_cap() which requires a
+	 * non-preemptible context.
+	 */
+	if (cpu == get_cpu()) {
+		func(val);
+		put_cpu();
+		return 0;
+	}
+	put_cpu();
+
+	/*
+	 * Reading from a remote CPU requires IRQs enabled to avoid
+	 * deadlock in smp_call_function_single().
+	 */
 	if (WARN_ON_ONCE(irqs_disabled()))
 		return -EPERM;
 
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw

Hi Sumit,

Quoted 32 lines · Sumit Gupta

The counters_read_on_cpu() function warns when called with IRQs disabled to prevent deadlock in smp_call_function_single(). However, this warning is spurious when reading counters on the current CPU since no IPI is needed for same-CPU reads.

Commit 12eb8f4fff24 ("cpufreq: CPPC: Update FIE arch_freq_scale in ticks for non-PCC regs") changed the CPPC Frequency Invariance Engine to read AMU counters directly from the scheduler tick for non-PCC register spaces (like FFH), instead of deferring to a kthread. This means counters_read_on_cpu() is now called with IRQs disabled from the tick handler, triggering the warning:

| WARNING: arch/arm64/kernel/topology.c:410 at counters_read_on_cpu
| ...
| Call trace:
|  counters_read_on_cpu+0x88/0xa8 (P)
|  cpc_read_ffh+0xdc/0x148
|  cpc_read+0x260/0x518
|  cppc_get_perf_ctrs+0xf0/0x398
|  __cppc_scale_freq_tick+0x4c/0x148 [cppc_cpufreq]
|  cppc_scale_freq_tick+0x44/0x88 [cppc_cpufreq]
|  topology_scale_freq_tick+0x34/0x58
|  sched_tick+0x58/0x300
|  update_process_times+0xcc/0x120
|  tick_nohz_handler+0xa8/0x260
|  __hrtimer_run_queues+0x154/0x360
|  hrtimer_interrupt+0xf4/0x2b0
|  arch_timer_handler_phys+0x4c/0x78
|  ....
|  CPPC Cpufreq:__cppc_scale_freq_tick: failed to read perf counters
|  ....
Quoted 7 lines

Fix this by calling the counter read function directly for same-CPU case, bypassing smp_call_function_single() entirely. Use get_cpu() to disable preemption as the counter read functions call this_cpu_has_cap() which requires a non-preemptible context.

Fixes 12eb8f4fff24 ("cpufreq: CPPC: Update FIE arch_freq_scale in ticks for non-PCC regs")

Looks fine for me except for the minor comment wrapping.

Thanks for spotting this.
I may have missed the warning log in the FFH test.

This happens during the short window in cpufreq_policy_online() between driver->init() and the CREATE_POLICY notifier that gets AMU FIE ready. After that, CPPC FIE will be stopped.

Ideally this can be merged together with Viresh's PR since the CPPC FIE changes are there.
https://lore.kernel.org/all/j4qdid7iqmng4gzb5ozefemjkep3wx2b5z2yki5tnqc3vzvzf4@kvrnarvdod5p/

Jie

Quoted 16 lines
---
 arch/arm64/kernel/topology.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
arch/arm64/kernel/topology.c +1 -2
index 539b38935182..57b71f403007 100644
@@ -401,12 +401,29 @@ static inline
 int counters_read_on_cpu(int cpu, smp_call_func_t func, u64 *val)
 {
 	/*
-	 * Abort call on counterless CPU or when interrupts are
-	 * disabled - can lead to deadlock in smp sync call.
+	 * Abort call on counterless CPU.
 	 */

arch/arm64/kernel/topology.c static inline · line 415

Quoted 10 lines
arch/arm64/kernel/topology.c +7 -0
 	if (!cpu_has_amu_feat(cpu))
 		return -EOPNOTSUPP;
 
+	/*
+	 * For same-CPU reads, call the function directly since no IPI
+	 * is needed and this is safe even with IRQs disabled.
+	 * Use get_cpu() to disable preemption as the counter read
+	 * functions call this_cpu_has_cap() which requires a
+	 * non-preemptible context.
+	 */

Wrap at 80 chars?

Quoted 14 lines
arch/arm64/kernel/topology.c +11 -0
+	if (cpu == get_cpu()) {
+		func(val);
+		put_cpu();
+		return 0;
+	}
+	put_cpu();
+
+	/*
+	 * Reading from a remote CPU requires IRQs enabled to avoid
+	 * deadlock in smp_call_function_single().
+	 */
 	if (WARN_ON_ONCE(irqs_disabled()))
 		return -EPERM;
 
maintainer
#2 lore raw
Quoted 48 lines · Jie Zhan
Quoted 32 lines · Sumit Gupta

The counters_read_on_cpu() function warns when called with IRQs disabled to prevent deadlock in smp_call_function_single(). However, this warning is spurious when reading counters on the current CPU since no IPI is needed for same-CPU reads.

Commit 12eb8f4fff24 ("cpufreq: CPPC: Update FIE arch_freq_scale in ticks for non-PCC regs") changed the CPPC Frequency Invariance Engine to read AMU counters directly from the scheduler tick for non-PCC register spaces (like FFH), instead of deferring to a kthread. This means counters_read_on_cpu() is now called with IRQs disabled from the tick handler, triggering the warning:

| WARNING: arch/arm64/kernel/topology.c:410 at counters_read_on_cpu
| ...
| Call trace:
|  counters_read_on_cpu+0x88/0xa8 (P)
|  cpc_read_ffh+0xdc/0x148
|  cpc_read+0x260/0x518
|  cppc_get_perf_ctrs+0xf0/0x398
|  __cppc_scale_freq_tick+0x4c/0x148 [cppc_cpufreq]
|  cppc_scale_freq_tick+0x44/0x88 [cppc_cpufreq]
|  topology_scale_freq_tick+0x34/0x58
|  sched_tick+0x58/0x300
|  update_process_times+0xcc/0x120
|  tick_nohz_handler+0xa8/0x260
|  __hrtimer_run_queues+0x154/0x360
|  hrtimer_interrupt+0xf4/0x2b0
|  arch_timer_handler_phys+0x4c/0x78
|  ....
|  CPPC Cpufreq:__cppc_scale_freq_tick: failed to read perf counters
|  ....
Quoted 7 lines

Fix this by calling the counter read function directly for same-CPU case, bypassing smp_call_function_single() entirely. Use get_cpu() to disable preemption as the counter read functions call this_cpu_has_cap() which requires a non-preemptible context.

Fixes 12eb8f4fff24 ("cpufreq: CPPC: Update FIE arch_freq_scale in ticks for non-PCC regs")

Looks fine for me except for the minor comment wrapping.

Thanks for spotting this.
I may have missed the warning log in the FFH test.

Quoted 7 lines

This happens during the short window in cpufreq_policy_online() between driver->init() and the CREATE_POLICY notifier that gets AMU FIE ready. After that, CPPC FIE will be stopped.

Ideally this can be merged together with Viresh's PR since the CPPC FIE changes are there.
https://lore.kernel.org/all/j4qdid7iqmng4gzb5ozefemjkep3wx2b5z2yki5tnqc3vzvzf4@kvrnarvdod5p/

Right, looks like this should go via Rafael but if it doesn't make the merge window then I can pick it up at -rc1 (please remind me :)

Will

maintainer
#3 lore raw
Quoted 53 lines · Will Deacon
Quoted 52 lines · Jie Zhan
Quoted 32 lines · Sumit Gupta

The counters_read_on_cpu() function warns when called with IRQs disabled to prevent deadlock in smp_call_function_single(). However, this warning is spurious when reading counters on the current CPU since no IPI is needed for same-CPU reads.

Commit 12eb8f4fff24 ("cpufreq: CPPC: Update FIE arch_freq_scale in ticks for non-PCC regs") changed the CPPC Frequency Invariance Engine to read AMU counters directly from the scheduler tick for non-PCC register spaces (like FFH), instead of deferring to a kthread. This means counters_read_on_cpu() is now called with IRQs disabled from the tick handler, triggering the warning:

| WARNING: arch/arm64/kernel/topology.c:410 at counters_read_on_cpu
| ...
| Call trace:
|  counters_read_on_cpu+0x88/0xa8 (P)
|  cpc_read_ffh+0xdc/0x148
|  cpc_read+0x260/0x518
|  cppc_get_perf_ctrs+0xf0/0x398
|  __cppc_scale_freq_tick+0x4c/0x148 [cppc_cpufreq]
|  cppc_scale_freq_tick+0x44/0x88 [cppc_cpufreq]
|  topology_scale_freq_tick+0x34/0x58
|  sched_tick+0x58/0x300
|  update_process_times+0xcc/0x120
|  tick_nohz_handler+0xa8/0x260
|  __hrtimer_run_queues+0x154/0x360
|  hrtimer_interrupt+0xf4/0x2b0
|  arch_timer_handler_phys+0x4c/0x78
|  ....
|  CPPC Cpufreq:__cppc_scale_freq_tick: failed to read perf counters
|  ....
Quoted 7 lines

Fix this by calling the counter read function directly for same-CPU case, bypassing smp_call_function_single() entirely. Use get_cpu() to disable preemption as the counter read functions call this_cpu_has_cap() which requires a non-preemptible context.

Fixes 12eb8f4fff24 ("cpufreq: CPPC: Update FIE arch_freq_scale in ticks for non-PCC regs")

Looks fine for me except for the minor comment wrapping.

Thanks for spotting this.
I may have missed the warning log in the FFH test.

This happens during the short window in cpufreq_policy_online() between driver->init() and the CREATE_POLICY notifier that gets AMU FIE ready. After that, CPPC FIE will be stopped.

Quoted 6 lines
Quoted 3 lines

Ideally this can be merged together with Viresh's PR since the CPPC FIE changes are there.
https://lore.kernel.org/all/j4qdid7iqmng4gzb5ozefemjkep3wx2b5z2yki5tnqc3vzvzf4@kvrnarvdod5p/

Right, looks like this should go via Rafael but if it doesn't make the merge window then I can pick it up at -rc1 (please remind me :)

Looks like this fix is still needed. Please can you post a new version, based on -rc1, so that I can pick it up?

You'll also need to fix the SHA in the commit message and the Fixes: tag, as 12eb8f4fff24 doesn't match the upstream change.

Will

#4 lore raw
Quoted 61 lines · Will Deacon
Quoted 53 lines · Will Deacon
Quoted 52 lines · Jie Zhan
Quoted 32 lines · Sumit Gupta

The counters_read_on_cpu() function warns when called with IRQs disabled to prevent deadlock in smp_call_function_single(). However, this warning is spurious when reading counters on the current CPU since no IPI is needed for same-CPU reads.

Commit 12eb8f4fff24 ("cpufreq: CPPC: Update FIE arch_freq_scale in ticks for non-PCC regs") changed the CPPC Frequency Invariance Engine to read AMU counters directly from the scheduler tick for non-PCC register spaces (like FFH), instead of deferring to a kthread. This means counters_read_on_cpu() is now called with IRQs disabled from the tick handler, triggering the warning:

| WARNING: arch/arm64/kernel/topology.c:410 at counters_read_on_cpu
| ...
| Call trace:
|  counters_read_on_cpu+0x88/0xa8 (P)
|  cpc_read_ffh+0xdc/0x148
|  cpc_read+0x260/0x518
|  cppc_get_perf_ctrs+0xf0/0x398
|  __cppc_scale_freq_tick+0x4c/0x148 [cppc_cpufreq]
|  cppc_scale_freq_tick+0x44/0x88 [cppc_cpufreq]
|  topology_scale_freq_tick+0x34/0x58
|  sched_tick+0x58/0x300
|  update_process_times+0xcc/0x120
|  tick_nohz_handler+0xa8/0x260
|  __hrtimer_run_queues+0x154/0x360
|  hrtimer_interrupt+0xf4/0x2b0
|  arch_timer_handler_phys+0x4c/0x78
|  ....
|  CPPC Cpufreq:__cppc_scale_freq_tick: failed to read perf counters
|  ....
Quoted 7 lines

Fix this by calling the counter read function directly for same-CPU case, bypassing smp_call_function_single() entirely. Use get_cpu() to disable preemption as the counter read functions call this_cpu_has_cap() which requires a non-preemptible context.

Fixes 12eb8f4fff24 ("cpufreq: CPPC: Update FIE arch_freq_scale in ticks for non-PCC regs")

Looks fine for me except for the minor comment wrapping.

Thanks for spotting this.
I may have missed the warning log in the FFH test.

This happens during the short window in cpufreq_policy_online() between driver->init() and the CREATE_POLICY notifier that gets AMU FIE ready. After that, CPPC FIE will be stopped.

Quoted 6 lines
Quoted 3 lines

Ideally this can be merged together with Viresh's PR since the CPPC FIE changes are there.
https://lore.kernel.org/all/j4qdid7iqmng4gzb5ozefemjkep3wx2b5z2yki5tnqc3vzvzf4@kvrnarvdod5p/

Right, looks like this should go via Rafael but if it doesn't make the merge window then I can pick it up at -rc1 (please remind me :)

Quoted 8 lines

Looks like this fix is still needed. Please can you post a new version, based on -rc1, so that I can pick it up?

You'll also need to fix the SHA in the commit message and the Fixes: tag, as 12eb8f4fff24 doesn't match the upstream change.

Will

I believe this goes to Sumit because he's the author.

Sumit, can you update this patch?

Jie

#5 lore raw
Quoted 68 lines · Jie Zhan

External email: Use caution opening links or attachments

Quoted 64 lines · Will Deacon
Quoted 52 lines · Will Deacon
Quoted 51 lines · Jie Zhan
Quoted 32 lines · Sumit Gupta

The counters_read_on_cpu() function warns when called with IRQs disabled to prevent deadlock in smp_call_function_single(). However, this warning is spurious when reading counters on the current CPU since no IPI is needed for same-CPU reads.

Commit 12eb8f4fff24 ("cpufreq: CPPC: Update FIE arch_freq_scale in ticks for non-PCC regs") changed the CPPC Frequency Invariance Engine to read AMU counters directly from the scheduler tick for non-PCC register spaces (like FFH), instead of deferring to a kthread. This means counters_read_on_cpu() is now called with IRQs disabled from the tick handler, triggering the warning:

| WARNING: arch/arm64/kernel/topology.c:410 at counters_read_on_cpu
| ...
| Call trace:
|  counters_read_on_cpu+0x88/0xa8 (P)
|  cpc_read_ffh+0xdc/0x148
|  cpc_read+0x260/0x518
|  cppc_get_perf_ctrs+0xf0/0x398
|  __cppc_scale_freq_tick+0x4c/0x148 [cppc_cpufreq]
|  cppc_scale_freq_tick+0x44/0x88 [cppc_cpufreq]
|  topology_scale_freq_tick+0x34/0x58
|  sched_tick+0x58/0x300
|  update_process_times+0xcc/0x120
|  tick_nohz_handler+0xa8/0x260
|  __hrtimer_run_queues+0x154/0x360
|  hrtimer_interrupt+0xf4/0x2b0
|  arch_timer_handler_phys+0x4c/0x78
|  ....
|  CPPC Cpufreq:__cppc_scale_freq_tick: failed to read perf counters
|  ....
Quoted 7 lines

Fix this by calling the counter read function directly for same-CPU case, bypassing smp_call_function_single() entirely. Use get_cpu() to disable preemption as the counter read functions call this_cpu_has_cap() which requires a non-preemptible context.

Fixes 12eb8f4fff24 ("cpufreq: CPPC: Update FIE arch_freq_scale in ticks for non-PCC regs")

Looks fine for me except for the minor comment wrapping.

Thanks for spotting this.
I may have missed the warning log in the FFH test.

This happens during the short window in cpufreq_policy_online() between driver->init() and the CREATE_POLICY notifier that gets AMU FIE ready. After that, CPPC FIE will be stopped.

Quoted 5 lines
Quoted 3 lines

Ideally this can be merged together with Viresh's PR since the CPPC FIE changes are there.
https://lore.kernel.org/all/j4qdid7iqmng4gzb5ozefemjkep3wx2b5z2yki5tnqc3vzvzf4@kvrnarvdod5p/

Right, looks like this should go via Rafael but if it doesn't make the merge window then I can pick it up at -rc1 (please remind me :)

Looks like this fix is still needed. Please can you post a new version, based on -rc1, so that I can pick it up?

You'll also need to fix the SHA in the commit message and the Fixes: tag, as 12eb8f4fff24 doesn't match the upstream change.

Quoted 7 lines
Quoted 2 lines

Will

I believe this goes to Sumit because he's the author.

Sumit, can you update this patch?

Jie

Sent v2[1] with updated Fixes: tag and re-based on v7.0-rc1.

[1] https://lore.kernel.org/lkml/[email protected]/

Thank you,
Sumit Gupta