From: Shrikanth Hegde <sshegde@linux.ibm.com>
To: Aboorva Devarajan <aboorvad@linux.ibm.com>,
Madhavan Srinivasan <maddy@linux.ibm.com>,
linuxppc-dev@lists.ozlabs.org
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>,
Christophe Leroy <chleroy@kernel.org>,
linux-kernel@vger.kernel.org,
Sourabh Jain <sourabhjain@linux.ibm.com>,
Ritesh Harjani <ritesh.list@gmail.com>
Subject: Re: [PATCH 3/3] powerpc/kexec: fix double get_cpu() imbalance in kexec_prepare_cpus
Date: Mon, 18 May 2026 11:32:38 +0530 [thread overview]
Message-ID: <45af9f95-da07-45b6-babe-dbae0f7eb318@linux.ibm.com> (raw)
In-Reply-To: <20260518050855.1147242-4-aboorvad@linux.ibm.com>
Hi Aboorva.
On 5/18/26 10:38 AM, Aboorva Devarajan wrote:
> 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")
> Signed-off-by: Aboorva Devarajan <aboorvad@linux.ibm.com>
> ---
> arch/powerpc/kexec/core_64.c | 15 ++++++++-------
> 1 file changed, 8 insertions(+), 7 deletions(-)
>
> diff --git a/arch/powerpc/kexec/core_64.c b/arch/powerpc/kexec/core_64.c
> index 825ab8a88f18e..9d7e5a1e6e5b8 100644
> --- a/arch/powerpc/kexec/core_64.c
> +++ b/arch/powerpc/kexec/core_64.c
> @@ -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();
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.
> + 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 */
next prev parent reply other threads:[~2026-05-18 6:02 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-18 5:08 [PATCH 0/3] powerpc: fix preempt_count imbalances in perf and kexec paths Aboorva Devarajan
2026-05-18 5:08 ` [PATCH 1/3] powerpc/perf: fix preempt count underflow in fsl_emb_pmu_del Aboorva Devarajan
2026-05-18 6:13 ` Shrikanth Hegde
2026-06-03 5:59 ` Aboorva Devarajan
2026-05-18 5:08 ` [PATCH 2/3] powerpc/powernv: fix preempt count leak in pnv_kexec_wait_secondaries_down Aboorva Devarajan
2026-05-18 7:56 ` Shrikanth Hegde
2026-06-03 6:08 ` Aboorva Devarajan
2026-05-18 5:08 ` [PATCH 3/3] powerpc/kexec: fix double get_cpu() imbalance in kexec_prepare_cpus Aboorva Devarajan
2026-05-18 6:02 ` Shrikanth Hegde [this message]
2026-06-03 6:14 ` Aboorva Devarajan
2026-06-03 6:16 ` Shrikanth Hegde
2026-05-18 8:08 ` [PATCH 0/3] powerpc: fix preempt_count imbalances in perf and kexec paths Shrikanth Hegde
2026-06-03 6:16 ` Aboorva Devarajan
-- strict thread matches above, loose matches on Subject: below --
2026-06-03 6:27 [PATCH v2 " Aboorva Devarajan
2026-06-03 6:27 ` [PATCH 3/3] powerpc/kexec: fix double get_cpu() imbalance in kexec_prepare_cpus Aboorva Devarajan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=45af9f95-da07-45b6-babe-dbae0f7eb318@linux.ibm.com \
--to=sshegde@linux.ibm.com \
--cc=aboorvad@linux.ibm.com \
--cc=atrajeev@linux.vnet.ibm.com \
--cc=chleroy@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=maddy@linux.ibm.com \
--cc=ritesh.list@gmail.com \
--cc=sourabhjain@linux.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox