Kexec Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Pratyush Yadav <pratyush@kernel.org>
To: Sourabh Jain <sourabhjain@linux.ibm.com>
Cc: sashiko-reviews@lists.linux.dev,
	 Aditya Gupta <adityag@linux.ibm.com>,
	Baoquan He <baoquan.he@linux.dev>,
	 Madhavan Srinivasan <maddy@linux.ibm.com>,
	 Pratyush Yadav <pratyush@kernel.org>,
	kexec@lists.infradead.org,
	 "Christophe Leroy (CS GROUP)" <chleroy@kernel.org>,
	 Mahesh Salgaonkar <mahesh@linux.ibm.com>,
	 Michael Ellerman <mpe@ellerman.id.au>,
	 Hari Bathini <hbathini@linux.ibm.com>,
	Mike Rapoport <rppt@kernel.org>,
	 linux-kernel@vger.kernel.org,
	 Pasha Tatashin <pasha.tatashin@soleen.com>,
	 linuxppc-dev@lists.ozlabs.org,
	Andrew Morton <akpm@linux-foundation.org>,
	 Alexander Graf <graf@amazon.com>,
	 Shrikanth Hegde <sshegde@linux.ibm.com>,
	 Shivang Upadhyay <shivangu@linux.ibm.com>,
	 Nicholas Piggin <npiggin@gmail.com>,
	"Ritesh Harjani (IBM)" <ritesh.list@gmail.com>
Subject: Re: [PATCH v1 1/2] kho: check scratch vs CMA alignment at runtime
Date: Mon, 12 Oct 2026 16:29:43 +0200	[thread overview]
Message-ID: <2vxzjynnj8ko.fsf@kernel.org> (raw)
In-Reply-To: <0b918888-502d-4388-ab54-2c7ae9f4ccbb@linux.ibm.com> (Sourabh Jain's message of "Thu, 1 Oct 2026 09:00:42 +0530")

On Thu, Oct 01 2026, Sourabh Jain wrote:

> On 28/09/26 14:11, sashiko-bot@kernel.org wrote:
>> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
>> - [High] kho: bypassed scratch alignment check during resume causes memory corruption
>> --
>>
>> commit cbd7cb7f008e704b389d8dfe3306e1e83e49e8a8
>> Author: Sourabh Jain <sourabhjain@linux.ibm.com>
>>
>> kho: check scratch vs CMA alignment at runtime
>>
>> KHO requires the scratch alignment to be at least CMA_MIN_ALIGNMENT_BYTES,
>> which was previously enforced with a static_assert(). This commit replaces
>> the static_assert() with a runtime check in kho_reserve_scratch() to allow
>> KHO on configurations where the minimum alignment is not a constant.
>>
>>> diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c
>>> index 7c4d86daf86d5..dc76dd6b74da7 100644
>>> --- a/kernel/liveupdate/kexec_handover.c
>>> +++ b/kernel/liveupdate/kexec_handover.c
>> [ ... ]
>>
>>> @@ -836,6 +828,21 @@ static void __init kho_reserve_scratch(void)
>>>   	if (!kho_enable)
>>>   		return;
>>>   +	/*
>>> +	 * This is the minimal alignment required by deferred struct page init.
>>> +	 * deferred_init_memmap_chunk frees memory to the buddy allocator, which looks
>>> +	 * at the neighboring pages (up to MAX_PAGE_ORDER) to merge them.
>>> +	 * If KHO scratch is not aligned to that value, buddy can access uninitialized
>>> +	 * struct pages, which can cause a crash.
>>> +	 */
>>> +	if (WARN_ON_ONCE(SCRATCH_ALIGNMENT_BYTES < CMA_MIN_ALIGNMENT_BYTES)) {
>>> +		pr_err("scratch alignment %lu < CMA minimum %lu, disabling\n",
>>> +		       (unsigned long)SCRATCH_ALIGNMENT_BYTES,
>>> +		       (unsigned long)CMA_MIN_ALIGNMENT_BYTES);
>>> +		kho_enable = false;
>>> +		return;
>>> +	}
>>> +
>> [Severity: High]
>> Does this new runtime check get bypassed during a KHO resume?
>>
>> During a KHO resume, kho_memory_init() skips kho_reserve_scratch():
>>
>> kernel/liveupdate/kexec_handover.c:kho_memory_init() {
>> 	if (kho_in.scratch_phys)
>> 		kho_mem_retrieve();
>> 	else
>> 		kho_reserve_scratch();
>> }
>>
>> If a predecessor kernel passes a scratch region with a smaller alignment,
>> and the successor kernel has a larger pageblock_order, bypassing this check
>> means kho_init() will iterate over the provided scratch memory using the
>> successor kernel's larger pageblock_nr_pages:
>>
>> kernel/liveupdate/kexec_handover.c:kho_init() {
>> 		for (pfn = base_pfn; pfn < base_pfn + count;
>> 		     pfn += pageblock_nr_pages)
>> 			init_cma_reserved_pageblock(pfn_to_page(pfn));
>> }
>> Can this lead to init_cma_reserved_pageblock() overwriting state for pages
>> outside the actual scratch region bounds, or cause a crash in the buddy
>> allocator if __free_pages() is called on an unaligned PFN?
>
> Yes, if the kexeced kernel has a higher pageblock_order than the
> kernel that initiated the kexec with KHO, this can cause problems when
> the code above hands scratch-memory pages back to the buddy allocator
> in the kexeced kernel.
>
> Would it make sense to handle this by keeping only the unaligned pages
> reserved and handing the remaining pages back to the buddy?
>
> Something like this:
>
> diff --git a/kernel/liveupdate/kexec_handover.c
> b/kernel/liveupdate/kexec_handover.c
> index 2e3a36054851..dea6e7790972 100644
> --- a/kernel/liveupdate/kexec_handover.c
> +++ b/kernel/liveupdate/kexec_handover.c
> @@ -1928,7 +1928,8 @@ static __init int kho_init(void)
>
>         for (int i = 0; i < kho_scratch_cnt; i++) {
>                 unsigned long base_pfn = PHYS_PFN(kho_scratch[i].addr);
> -               unsigned long count = kho_scratch[i].size >> PAGE_SHIFT;
> +              unsigned long count = ALIGN_DOWN(kho_scratch[i].size >>
> PAGE_SHIFT,
> + pageblock_nr_pages);
>                 unsigned long pfn;

This code only runs on cold boot. So it will never run for kexec-ed
kernel. See the if (fdt) check above. It is very subtle and easy to
miss.

For kexeced kernels, the migrate type is set via
kho_scratch_migratetype() (see its callers). The logic it uses is that
if any page in the whole page block overlaps with scratch, mark it as
MIGRATE_CMA. So I think it already works with variable pageblock sizes.

>

Also, off-topic nit: your email client seems to like sending
"non-breaking space" (0xC2 0xA0 in UTF-8). For example, for indentation
in the diff above, it uses non-breaking space, and makes the diff hard
to apply. Perhaps tinker with the settings and see if you can avoid it?

-- 
Regards,
Pratyush Yadav


  reply	other threads:[~2026-10-12 14:29 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-28  8:32 [PATCH v1 0/2] powerpc: enable Kexec HandOver (KHO) Sourabh Jain
2026-09-28  8:32 ` [PATCH v1 1/2] kho: check scratch vs CMA alignment at runtime Sourabh Jain
2026-09-28  8:41   ` sashiko-bot
2026-10-01  3:30     ` Sourabh Jain
2026-10-12 14:29       ` Pratyush Yadav [this message]
2026-10-03  7:59   ` Mike Rapoport
2026-10-05  9:12     ` Sourabh Jain
2026-10-12 14:30   ` Pratyush Yadav
2026-09-28  8:32 ` [PATCH v1 2/2] powerpc: add support for Kexec HandOver (KHO) Sourabh Jain
2026-09-28  8:43   ` sashiko-bot
2026-10-12 14:43     ` Pratyush Yadav

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=2vxzjynnj8ko.fsf@kernel.org \
    --to=pratyush@kernel.org \
    --cc=adityag@linux.ibm.com \
    --cc=akpm@linux-foundation.org \
    --cc=baoquan.he@linux.dev \
    --cc=chleroy@kernel.org \
    --cc=graf@amazon.com \
    --cc=hbathini@linux.ibm.com \
    --cc=kexec@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=mahesh@linux.ibm.com \
    --cc=mpe@ellerman.id.au \
    --cc=npiggin@gmail.com \
    --cc=pasha.tatashin@soleen.com \
    --cc=ritesh.list@gmail.com \
    --cc=rppt@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shivangu@linux.ibm.com \
    --cc=sourabhjain@linux.ibm.com \
    --cc=sshegde@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