Skip to contentSkip to conversation

powerpc: fix preempt_count imbalances in perf and kexec paths

PATCH Superseded Aboorva Devarajan 2 participants 13 messages May 18 – Jun 3 PowerPC

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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.

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           | 15 ++++++++-------
 arch/powerpc/perf/core-fsl-emb.c       |  3 ++-
 arch/powerpc/platforms/powernv/setup.c |  2 +-
 3 files changed, 11 insertions(+), 9 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH 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 a11106544f33c ("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 7120ab20cbfec..02b5dd74c187a 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
reviewer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] powerpc/perf: fix preempt count underflow in fsl_emb_pmu_del

Quoted 25 lines · Aboorva Devarajan

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 a11106544f33c ("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 +0 -0
index 7120ab20cbfec..02b5dd74c187a 100644
@@ -366,9 +366,10 @@ static void fsl_emb_pmu_del(struct perf_event *event, int flags)
 

arch/powerpc/perf/core-fsl-emb.c static void fsl_emb_pmu_del(struct perf_event *event, int flags) · line 375

Quoted 10 lines
arch/powerpc/perf/core-fsl-emb.c +2 -1
  	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)

Thanks for fixing this. Looks good to me.

#3 lore raw

Re: [PATCH 1/3] powerpc/perf: fix preempt count underflow in fsl_emb_pmu_del

Quoted 49 lines · Shrikanth Hegde
Quoted 39 lines · Aboorva Devarajan

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 a11106544f33c ("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 +0 -0
index 7120ab20cbfec..02b5dd74c187a 100644
@@ -366,9 +366,10 @@ static void fsl_emb_pmu_del(struct perf_event
 *event, int flags)


   cpuhw->n_events--;

arch/powerpc/perf/core-fsl-emb.c +2 -0
+	put_cpu_var(cpu_hw_events);
+

   out:
   perf_pmu_enable(event->pmu);

arch/powerpc/perf/core-fsl-emb.c +0 -1
-	put_cpu_var(cpu_hw_events);
Quoted 4 lines

  }

  static void fsl_emb_pmu_start(struct perf_event *event, int ef_flags)

Thanks for fixing this. Looks good to me.

Quoted 1 line

Hi Shrikanth,

Thanks for the review.

Regards,
Aboorva

#4 lore raw

PATCH 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.

In the kexec teardown path IRQs are already disabled and the CPU is pinned, so get_cpu()'s preempt_disable() side-effect is unnecessary. Replace get_cpu() with raw_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 4dbb47ddbdcc4..177da0defcb36 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 = raw_smp_processor_id();
 
 	for_each_online_cpu(i) {
 		uint8_t status;
Signature · 1 line
2.54.0
#5 lore raw

Re: [PATCH 2/3] powerpc/powernv: fix preempt count leak in pnv_kexec_wait_secondaries_down

Hi Aboorva.

Quoted 26 lines · 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.

In the kexec teardown path IRQs are already disabled and the CPU is pinned, so get_cpu()'s preempt_disable() side-effect is unnecessary. Replace get_cpu() with raw_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 +0 -0
index 4dbb47ddbdcc4..177da0defcb36 100644

arch/powerpc/platforms/powernv/setup.c static void pnv_kexec_wait_secondaries_down(void) · line 400

Quoted 6 lines
arch/powerpc/platforms/powernv/setup.c +1 -1
@@ -396,7 +396,7 @@ static void pnv_kexec_wait_secondaries_down(void)
  {
  	int my_cpu, i, notified = -1;
 
-	my_cpu = get_cpu();
+	my_cpu = raw_smp_processor_id();
 

Is it always with irq-disabled?
How about !CONFIG_SMP and in kexec_prepare_cpus. I see it disables interrupt later. (though it is a less common config)

So use smp_processor_id()?? One could compile with CONFIG_DEBUG_PREEMPT=y and see any reports.

Quoted 2 lines
arch/powerpc/platforms/powernv/setup.c +0 -0
  	for_each_online_cpu(i) {
  		uint8_t status;
#6 lore raw

Re: [PATCH 2/3] powerpc/powernv: fix preempt count leak in pnv_kexec_wait_secondaries_down

Quoted 25 lines · Shrikanth Hegde

Hi Aboorva.

Quoted 21 lines · 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.

In the kexec teardown path IRQs are already disabled and the CPU is pinned, so get_cpu()'s preempt_disable() side-effect is unnecessary. Replace get_cpu() with raw_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(-)
Quoted 16 lines
Quoted 11 lines
arch/powerpc/platforms/powernv/setup.c +0 -0
index 4dbb47ddbdcc4..177da0defcb36 100644
@@ -396,7 +396,7 @@ static void pnv_kexec_wait_secondaries_down(void)
   {
   	int my_cpu, i, notified = -1;

arch/powerpc/platforms/powernv/setup.c +1 -1
-	my_cpu = get_cpu();
+	my_cpu = raw_smp_processor_id();

Is it always with irq-disabled?
How about !CONFIG_SMP and in kexec_prepare_cpus. I see it disables interrupt later. (though it is a less common config)

IIUC, PPC_POWERNV does 'select FORCE_SMP' (-> selects SMP), so there is no !CONFIG_SMP powernv build. The !SMP kexec_prepare_cpus() variant in arch/powerpc/kexec/core_64.c, the one you spotted that calls ppc_md.kexec_cpu_down() before local_irq_disable() is therefore never compiled with powernv, so pnv_kexec_cpu_down() -> pnv_kexec_wait_secondaries_down() can't be reached through it. so, IRQs are disabled in every case that reaches this function.

Quoted 5 lines

So use smp_processor_id()?? One could compile with CONFIG_DEBUG_PREEMPT=y and see any reports.

Quoted 2 lines

   for_each_online_cpu(i) {
   uint8_t status;

sure, I'll switch to smp_processor_id() in v2 rather than raw_smp_processor_id(). It returns the cpu id without touching preempt_count (so the leak is gone), and unlike the raw variant it keeps the CONFIG_DEBUG_PREEMPT check which is a no-op here since IRQs are off, but will flag any future caller that reaches this path while preemptible instead of silently hiding it.

Thanks,
Aboorva

#7 lore raw

PATCH 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.

Lift the get_cpu()/put_cpu() pair into kexec_prepare_cpus() so it is called exactly once, and pass the CPU id to kexec_prepare_cpus_wait() as a parameter. This keeps preempt_count correctly balanced.

Fixes 1fc711f7ffb01 ("powerpc/kexec: Fix race in kexec shutdown")
---
 arch/powerpc/kexec/core_64.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
arch/powerpc/kexec/core_64.c +8 -7
index 825ab8a88f18e..9d7e5a1e6e5b8 100644
@@ -164,12 +164,11 @@ static void kexec_smp_down(void *arg)
 	/* NOTREACHED */
 }
 
-static void kexec_prepare_cpus_wait(int wait_state)
+static void kexec_prepare_cpus_wait(int wait_state, int my_cpu)
 {
-	int my_cpu, i, notified=-1;
+	int i, notified = -1;
 
 	hw_breakpoint_disable();
-	my_cpu = get_cpu();
 	/* 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
@@ -246,6 +245,8 @@ static void wake_offline_cpus(void)
 
 static void kexec_prepare_cpus(void)
 {
+	int my_cpu;
+
 	wake_offline_cpus();
 	smp_call_function(kexec_smp_down, NULL, /* wait */0);
 	local_irq_disable();
@@ -254,7 +255,8 @@ static void kexec_prepare_cpus(void)
 	mb(); /* make sure IRQs are disabled before we say they are */
 	get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;
 
-	kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF);
+	my_cpu = get_cpu();
+	kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF, my_cpu);
 	/* we are sure every CPU has IRQs off at this point */
 	kexec_all_irq_disabled = 1;
 
@@ -262,13 +264,12 @@ static void kexec_prepare_cpus(void)
 	 * Before removing MMU mappings make sure all CPUs have entered real
 	 * mode:
 	 */
-	kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE);
+	kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE, my_cpu);
+	put_cpu();
 
 	/* 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
#8 lore raw

Re: [PATCH 3/3] powerpc/kexec: fix double get_cpu() imbalance in kexec_prepare_cpus

Hi Aboorva.

Quoted 52 lines · 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.

Lift the get_cpu()/put_cpu() pair into kexec_prepare_cpus() so it is called exactly once, and pass the CPU id to kexec_prepare_cpus_wait() as a parameter. This keeps preempt_count correctly balanced.

Fixes 1fc711f7ffb01 ("powerpc/kexec: Fix race in kexec shutdown")
---
  arch/powerpc/kexec/core_64.c | 15 ++++++++-------
  1 file changed, 8 insertions(+), 7 deletions(-)
arch/powerpc/kexec/core_64.c +4 -3
index 825ab8a88f18e..9d7e5a1e6e5b8 100644
@@ -164,12 +164,11 @@ static void kexec_smp_down(void *arg)
  	/* NOTREACHED */
  }
 
-static void kexec_prepare_cpus_wait(int wait_state)
+static void kexec_prepare_cpus_wait(int wait_state, int my_cpu)
  {
-	int my_cpu, i, notified=-1;
+	int i, notified = -1;
 
  	hw_breakpoint_disable();
-	my_cpu = get_cpu();
  	/* 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
@@ -246,6 +245,8 @@ static void wake_offline_cpus(void)
 
  static void kexec_prepare_cpus(void)
  {
+	int my_cpu;
+
  	wake_offline_cpus();
  	smp_call_function(kexec_smp_down, NULL, /* wait */0);
  	local_irq_disable();

arch/powerpc/kexec/core_64.c static void kexec_prepare_cpus(void) · line 258

Quoted 5 lines
arch/powerpc/kexec/core_64.c +1 -1
@@ -254,7 +255,8 @@ static void kexec_prepare_cpus(void)
  	mb(); /* make sure IRQs are disabled before we say they are */
  	get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;
 
-	kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF);
+	my_cpu = get_cpu();

raw_smp_processor_id() is better here. All it needs is get current cpu? caller does irq_disable above and that renders call for get_cpu un-necessary.

Quoted 20 lines
arch/powerpc/kexec/core_64.c +3 -3
+	kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF, my_cpu);
  	/* we are sure every CPU has IRQs off at this point */
  	kexec_all_irq_disabled = 1;
 
@@ -262,13 +264,12 @@ static void kexec_prepare_cpus(void)
  	 * Before removing MMU mappings make sure all CPUs have entered real
  	 * mode:
  	 */
-	kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE);
+	kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE, my_cpu);
+	put_cpu();
 
  	/* 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 */
#9 lore raw

Re: [PATCH 3/3] powerpc/kexec: fix double get_cpu() imbalance in kexec_prepare_cpus

Hi Shrikanth,

Quoted 55 lines · Shrikanth Hegde

Hi Aboorva.

Quoted 52 lines · 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.

Lift the get_cpu()/put_cpu() pair into kexec_prepare_cpus() so it is called exactly once, and pass the CPU id to kexec_prepare_cpus_wait() as a parameter. This keeps preempt_count correctly balanced.

Fixes 1fc711f7ffb01 ("powerpc/kexec: Fix race in kexec shutdown")

---
  arch/powerpc/kexec/core_64.c | 15 ++++++++-------

  1 file changed, 8 insertions(+), 7 deletions(-)
arch/powerpc/kexec/core_64.c +0 -0
index 825ab8a88f18e..9d7e5a1e6e5b8 100644
@@ -164,12 +164,11 @@ static void kexec_smp_down(void *arg)
   	/* NOTREACHED */
   }

arch/powerpc/kexec/core_64.c +1 -1
-static void kexec_prepare_cpus_wait(int wait_state)
+static void kexec_prepare_cpus_wait(int wait_state, int my_cpu)

  {

arch/powerpc/kexec/core_64.c +1 -1
-	int my_cpu, i, notified=-1;
+	int i, notified = -1;


   hw_breakpoint_disable();

arch/powerpc/kexec/core_64.c +0 -1
-	my_cpu = get_cpu();

   /* 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

arch/powerpc/kexec/core_64.c +0 -0
@@ -246,6 +245,8 @@ static void wake_offline_cpus(void)


  static void kexec_prepare_cpus(void)
  {

arch/powerpc/kexec/core_64.c +2 -0
+	int my_cpu;
+

   wake_offline_cpus(); smp_call_function(kexec_smp_down, NULL, /* wait */0); local_irq_disable();

arch/powerpc/kexec/core_64.c +0 -0
@@ -254,7 +255,8 @@ static void kexec_prepare_cpus(void)
Quoted 8 lines

arch/powerpc/kexec/core_64.c static void kexec_prepare_cpus(void) · line 258

Quoted 5 lines

   mb(); /* make sure IRQs are disabled before we say they are */ get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;

arch/powerpc/kexec/core_64.c +1 -1
-	kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF);
+	my_cpu = get_cpu();

raw_smp_processor_id() is better here. All it needs is get current cpu? caller does irq_disable above and that renders call for get_cpu un-necessary.

Agreed, get_cpu() is not needed here. kexec_prepare_cpus() already does local_irq_disable()/hard_irq_disable() before calling kexec_prepare_cpus_wait(), so we only need the current cpu id.

I will go ahead with smp_processor_id() rather than raw_smp_processor_id() to stay consistent with Patch 2 and to keep the CONFIG_DEBUG_PREEMPT check.

Quoted 9 lines
Quoted 9 lines

arch/powerpc/kexec/core_64.c +2 -1
@@ -262,13 +264,12 @@ static void kexec_prepare_cpus(void)
   	 * Before removing MMU mappings make sure all CPUs have entered real
   	 * mode:
   	 */
-	kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE);
+	kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE, my_cpu);
+	put_cpu();

Quoted 8 lines
Quoted 8 lines

   /* after we tell the others to go down */
   if (ppc_md.kexec_cpu_down)
   ppc_md.kexec_cpu_down(0, 0);

arch/powerpc/kexec/core_64.c +0 -2
-
-	put_cpu();

  }

  #else /* ! SMP */

Regards,
Aboorva

#10 lore raw

Re: [PATCH 3/3] powerpc/kexec: fix double get_cpu() imbalance in kexec_prepare_cpus

Quoted 67 lines · Aboorva Devarajan

Hi Shrikanth,

Quoted 58 lines · Shrikanth Hegde

Hi Aboorva.

Quoted 55 lines · 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.

Lift the get_cpu()/put_cpu() pair into kexec_prepare_cpus() so it is called exactly once, and pass the CPU id to kexec_prepare_cpus_wait() as a parameter. This keeps preempt_count correctly balanced.

Fixes 1fc711f7ffb01 ("powerpc/kexec: Fix race in kexec shutdown")
---
   arch/powerpc/kexec/core_64.c | 15 ++++++++-------
   1 file changed, 8 insertions(+), 7 deletions(-)
arch/powerpc/kexec/core_64.c +4 -3
index 825ab8a88f18e..9d7e5a1e6e5b8 100644
@@ -164,12 +164,11 @@ static void kexec_smp_down(void *arg)
   	/* NOTREACHED */
   }
 
-static void kexec_prepare_cpus_wait(int wait_state)
+static void kexec_prepare_cpus_wait(int wait_state, int my_cpu)
   {
-	int my_cpu, i, notified=-1;
+	int i, notified = -1;
 
   	hw_breakpoint_disable();
-	my_cpu = get_cpu();
   	/* 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
@@ -246,6 +245,8 @@ static void wake_offline_cpus(void)
 
   static void kexec_prepare_cpus(void)
   {
+	int my_cpu;
+
   	wake_offline_cpus();
   	smp_call_function(kexec_smp_down, NULL, /* wait */0);
   	local_irq_disable();
@@ -254,7 +255,8 @@ static void kexec_prepare_cpus(void)
   	mb(); /* make sure IRQs are disabled before we say they are */
   	get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;
 
Quoted 5 lines

arch/powerpc/kexec/core_64.c static void kexec_prepare_cpus(void) · line 258

Quoted 2 lines
arch/powerpc/kexec/core_64.c +1 -1
-	kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF);
+	my_cpu = get_cpu();

raw_smp_processor_id() is better here. All it needs is get current cpu? caller does irq_disable above and that renders call for get_cpu un-necessary.

Quoted 7 lines

Agreed, get_cpu() is not needed here. kexec_prepare_cpus() already does local_irq_disable()/hard_irq_disable() before calling kexec_prepare_cpus_wait(), so we only need the current cpu id.

I will go ahead with smp_processor_id() rather than raw_smp_processor_id() to stay consistent with Patch 2 and to keep the CONFIG_DEBUG_PREEMPT check.

If the irq's are disabled then use raw_smp_processor_id() in both the places. For patch2, just put a comment saying irq's are disabled when its get there.

Quoted 21 lines
Quoted 10 lines
Quoted 10 lines
arch/powerpc/kexec/core_64.c +2 -1
@@ -262,13 +264,12 @@ static void kexec_prepare_cpus(void)
   	 * Before removing MMU mappings make sure all CPUs have entered real
   	 * mode:
   	 */
-	kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE);
+	kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE, my_cpu);
+	put_cpu();
 
Quoted 8 lines

arch/powerpc/kexec/core_64.c static void kexec_prepare_cpus(void) · line 275

Quoted 8 lines
arch/powerpc/kexec/core_64.c +0 -2
   	/* 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 */

Regards,
Aboorva

#11 lore raw
In reply to Aboorva Devarajan

Hi Aboorva,

Quoted 8 lines · Aboorva Devarajan

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.

Thanks for fixing some of these.

while we do this, Can you fix these mismatch in preempt disable/enable in below files as well.

1. kernel/kprobes.c - kprobe_handler - Does disable, but doesn't enable in some return paths. A definite leak.

2. Maybe platforms/pseries/lpar.c and platforms/powernv/opal-tracepoints.c. In __trace_hcall_entry/exit. It maybe a very corner case,
I don't see a big concern there. But it may be remotely possible. Need to evaluate whether it should be fixed or not.

#12 lore raw
Quoted 17 lines · Shrikanth Hegde

Hi Aboorva,

Quoted 8 lines · Aboorva Devarajan

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.

Thanks for fixing some of these.

while we do this, Can you fix these mismatch in preempt disable/enable in below files as well.

Quoted 7 lines

1. kernel/kprobes.c - kprobe_handler - Does disable, but doesn't enable in some return paths. A definite leak.

2. Maybe platforms/pseries/lpar.c and platforms/powernv/opal-tracepoints.c. In __trace_hcall_entry/exit.  It maybe a very corner case, I don't see a big concern there. But it may be remotely possible. Need to evaluate whether it should be fixed or not.

Thanks for the pointers. I'll go through these and get back.

Regards,
Aboorva