Skip to contentSkip to conversation

powerpc: fix preempt_count imbalances in perf and kexec paths

PATCH Superseded Aboorva Devarajan 1 participant 4 messages Jun 3 PowerPC

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover powerpc: fix preempt_count imbalances in perf and kexec paths

Hi all,

This patch series fixes some minor preempt_count bookkeeping issues in arch/powerpc/ found during a preemption leak audit prompted by the lazy/full preemption model changes. These are get_cpu/put_cpu and get_cpu_var/put_cpu_var pairing errors that leave preempt_count incorrectly elevated or underflowed.

v1: https://lore.kernel.org/all/[email protected]/

v1 -> v2:
- Patch 1 (fsl_emb_pmu_del): no functional change; picked up Shrikanth's Reviewed-by.
- Patch 2 (pnv_kexec_wait_secondaries_down): per Shrikanth's review, use smp_processor_id() instead of raw_smp_processor_id(). The kexec/ crash teardown path runs with IRQs disabled so it is safe.
- Patch 3 (kexec_prepare_cpus): per Shrikanth's review, dropped the approach of passing the cpu id through kexec_prepare_cpus_wait() as a parameter. kexec_prepare_cpus() already runs local_irq_disable()/ hard_irq_disable() before the wait, so get_cpu()/put_cpu() is
unnecessary: just read the cpu id in kexec_prepare_cpus_wait() and drop the trailing put_cpu(). This keeps the function signature unchanged. smp_processor_id() is used here too, consistent with patch 2.

Testing:
- Patch 1: compile-tested only (ppc64e_defconfig + CONFIG_FSL_EMB_PERFMON); no e500/fsl_emb hardware to runtime-test so far.
- Patches 2 and 3: kexec boot-tested on PowerNV (powernv_defconfig) and pseries (pseries_defconfig).

Please let me know your comments.

Thanks,
Aboorva

Aboorva Devarajan (3):
powerpc/perf: fix preempt count underflow in fsl_emb_pmu_del powerpc/powernv: fix preempt count leak in
pnv_kexec_wait_secondaries_down
powerpc/kexec: fix double get_cpu() imbalance in kexec_prepare_cpus

 arch/powerpc/kexec/core_64.c           | 4 +---
 arch/powerpc/perf/core-fsl-emb.c       | 3 ++-
 arch/powerpc/platforms/powernv/setup.c | 2 +-
 3 files changed, 4 insertions(+), 5 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH v2 1/3 powerpc/perf: fix preempt count underflow in fsl_emb_pmu_del

fsl_emb_pmu_del() unconditionally calls put_cpu_var(cpu_hw_events) at the 'out:' label, but only calls the matching get_cpu_var() after the 'i < 0' early-return check. When event->hw.idx is negative the function jumps to 'out:' without having taken get_cpu_var(), and the trailing put_cpu_var() then issues an unmatched preempt_enable(), underflowing preempt_count.

On a CONFIG_PREEMPT=y kernel preempt_count would underflow and eventually present as a 'scheduling while atomic' BUG.

Move put_cpu_var() to pair with get_cpu_var() so the percpu access is correctly bracketed and the 'out:' label only handles perf_pmu_enable.

Fixes a11106544f33 ("powerpc/perf: e500 support")
---
 arch/powerpc/perf/core-fsl-emb.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/powerpc/perf/core-fsl-emb.c +2 -1
index 7120ab20cbfe..02b5dd74c187 100644
@@ -366,9 +366,10 @@ static void fsl_emb_pmu_del(struct perf_event *event, int flags)
 
 	cpuhw->n_events--;
 
+	put_cpu_var(cpu_hw_events);
+
  out:
 	perf_pmu_enable(event->pmu);
-	put_cpu_var(cpu_hw_events);
 }
 
 static void fsl_emb_pmu_start(struct perf_event *event, int ef_flags)
Signature · 1 line
2.54.0
#2 lore raw

PATCH v2 2/3 powerpc/powernv: fix preempt count leak in pnv_kexec_wait_secondaries_down

In reply to Aboorva Devarajan

pnv_kexec_wait_secondaries_down() calls get_cpu() to obtain the current CPU id but never calls the matching put_cpu(), leaking one preempt_disable() nesting level on every invocation.

In practice the imbalance does not trigger a visible splat because the kexec teardown path is a one-way trip: IRQs are already disabled, no schedule() occurs after the leak, and default_machine_kexec() overwrites preempt_count with HARDIRQ_OFFSET before jumping into kexec_sequence() which never returns. However the bookkeeping is still wrong.

The function only needs the current CPU id, and this path runs with the CPU pinned and IRQs disabled, so the preempt_disable() side-effect of get_cpu() is unnecessary. Replace it with smp_processor_id(), which returns the CPU id without touching preempt_count.

Fixes 298b34d7d578 ("powerpc/powernv: Fix kexec races going back to OPAL")
---
 arch/powerpc/platforms/powernv/setup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/platforms/powernv/setup.c +1 -1
index 4dbb47ddbdcc..73193264cbe7 100644
@@ -396,7 +396,7 @@ static void pnv_kexec_wait_secondaries_down(void)
 {
 	int my_cpu, i, notified = -1;
 
-	my_cpu = get_cpu();
+	my_cpu = smp_processor_id();
 
 	for_each_online_cpu(i) {
 		uint8_t status;
Signature · 1 line
2.54.0
#3 lore raw

PATCH v2 3/3 powerpc/kexec: fix double get_cpu() imbalance in kexec_prepare_cpus

In reply to Aboorva Devarajan

kexec_prepare_cpus_wait() calls get_cpu() internally to obtain the current CPU id. kexec_prepare_cpus() calls kexec_prepare_cpus_wait() twice -- once for KEXEC_STATE_IRQS_OFF and once for KEXEC_STATE_REAL_MODE -- but only issues a single put_cpu() at the end, leaving preempt_count elevated by one extra nesting level.

In practice the imbalance does not trigger a 'scheduling while atomic' splat because the kexec path is a one-way trip: IRQs are already disabled, no schedule() occurs after the leak, and default_machine_kexec() overwrites preempt_count with HARDIRQ_OFFSET before jumping into kexec_sequence() which never returns. However the bookkeeping is still wrong.

kexec_prepare_cpus() calls local_irq_disable()/hard_irq_disable() before invoking kexec_prepare_cpus_wait(), so the CPU is already pinned and the get_cpu()/put_cpu() preempt_disable() bracketing is unnecessary. Only the current CPU id is needed, so replace get_cpu() with smp_processor_id() and drop the now-unneeded put_cpu().

Fixes 1fc711f7ffb0 ("powerpc/kexec: Fix race in kexec shutdown")
---
 arch/powerpc/kexec/core_64.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
arch/powerpc/kexec/core_64.c +1 -3
index 825ab8a88f18..d7bdc362d497 100644
@@ -169,7 +169,7 @@ static void kexec_prepare_cpus_wait(int wait_state)
 	int my_cpu, i, notified=-1;
 
 	hw_breakpoint_disable();
-	my_cpu = get_cpu();
+	my_cpu = smp_processor_id();
 	/* Make sure each CPU has at least made it to the state we need.
 	 *
 	 * FIXME: There is a (slim) chance of a problem if not all of the CPUs
@@ -267,8 +267,6 @@ static void kexec_prepare_cpus(void)
 	/* after we tell the others to go down */
 	if (ppc_md.kexec_cpu_down)
 		ppc_md.kexec_cpu_down(0, 0);
-
-	put_cpu();
 }
 
 #else /* ! SMP */
Signature · 1 line
2.54.0