Re: [PATCHv2 2/2] kernel/kexec: Fix IMA when allocation happens in CMA area

Baoquan He
2025-11-07T09:31:51Z

On 11/07/25 at 05:00pm, Pingfan Liu wrote:
> On Fri, Nov 07, 2025 at 01:25:41PM +0800, Baoquan He wrote:
> > On 11/07/25 at 01:13pm, Pingfan Liu wrote:
> > > On Fri, Nov 7, 2025 at 9:51 AM Baoquan He <bhe@redhat.com> wrote:
> > > >
> > > > On 11/06/25 at 06:01pm, Pingfan Liu wrote:
> > > > > On Thu, Nov 6, 2025 at 4:01 PM Baoquan He <bhe@redhat.com> wrote:
> > > > > >
> > > > > > On 11/06/25 at 02:59pm, Pingfan Liu wrote:
> > > > > > > When I tested kexec with the latest kernel, I ran into the following warning:
> > > > > > >
> > > > > > > [ 40.712410] ------------[ cut here ]------------
> > > > > > > [ 40.712576] WARNING: CPU: 2 PID: 1562 at kernel/kexec_core.c:1001 kimage_map_segment+0x144/0x198
> > > > > > > [...]
> > > > > > > [ 40.816047] Call trace:
> > > > > > > [ 40.818498] kimage_map_segment+0x144/0x198 (P)
> > > > > > > [ 40.823221] ima_kexec_post_load+0x58/0xc0
> > > > > > > [ 40.827246] __do_sys_kexec_file_load+0x29c/0x368
> > > > > > > [...]
> > > > > > > [ 40.855423] ---[ end trace 0000000000000000 ]---
> > > > > > >
> > > > > > > This is caused by the fact that kexec allocates the destination directly
> > > > > > > in the CMA area. In that case, the CMA kernel address should be exported
> > > > > > > directly to the IMA component, instead of using the vmalloc'd address.
> > > > > >
> > > > > > Well, you didn't update the log accordingly.
> > > > > >
> > > > >
> > > > > I am not sure what you mean. Do you mean the earlier content which I
> > > > > replied to you?
> > > >
> > > > No. In v1, you return cma directly. But in v2, you return its direct
> > > > mapping address, isnt' it?
> > > >
> > >
> > > Yes. But I think it is a fault in the code, which does not convey the
> > > expression in the commit log. Do you think I should rephrase the words
> > > "the CMA kernel address" as "the CMA kernel direct mapping address"?
> >
> > That's fine to me.
> >
> > >
> > > > >
> > > > > > Do you know why cma area can't be mapped into vmalloc?
> > > > > >
> > > > > Should not the kernel direct mapping be used?
> > > >
> > > > When image->segment_cma[i] has value, image->ima_buffer_addr also
> > > > contains the physical address of the cma area, why cma physical address
> > > > can't be mapped into vmalloc and cause the failure and call trace?
> > > >
> > >
> > > It could be done using the vmalloc approach, but it's unnecessary.
> > > IIUC, kimage_map_segment() was introduced to provide a contiguous
> > > virtual address for IMA access, since the IND_SRC pages are scattered
> > > throughout the kernel. However, in the CMA case, there is already a
> > > contiguous virtual address in the kernel direct mapping range.
> > > Normally, when we have a physical address, we simply use
> > > phys_to_virt() to get its corresponding kernel virtual address.
> >
> > OK, I understand cma area is contiguous, and no need to map into
> > vmalloc. I am wondering why in the old code mapping cma addrss into
> > vmalloc cause the warning which you said is a IMA problem.
> >
>
> It doesn't go that far. The old code doesn't map CMA into vmalloc'd
> area.
>
> void *kimage_map_segment(struct kimage *image, int idx)
> {
> ...
> for_each_kimage_entry(image, ptr, entry) {
> if (entry & IND_DESTINATION) {
> dest_page_addr = entry & PAGE_MASK;
> } else if (entry & IND_SOURCE) {
> if (dest_page_addr >= addr && dest_page_addr < eaddr) {
> src_page_addr = entry & PAGE_MASK;
> src_pages[i++] =
> virt_to_page(__va(src_page_addr));
> if (i == npages)
> break;
> dest_page_addr += PAGE_SIZE;
> }
> }
> }
>
> /* Sanity check. */
> WARN_ON(i < npages); //--> This is the warning thrown by kernel
>
> vaddr = vmap(src_pages, npages, VM_MAP, PAGE_KERNEL);
> kfree(src_pages);
>
> if (!vaddr)
> pr_err("Could not map ima buffer.\n");
>
> return vaddr;
> }
>
> When CMA is used, there is no IND_SOURCE, so we have i=0 < npages.
> Now, I see how my words ("In that case, the CMA kernel address should be
> exported directly to the IMA component, instead of using the vmalloc'd
> address.") confused you. As for "instead of using the vmalloc'd
> address", I meant to mention "vmap()" approach.

Ok, I got it. It's truly a bug because if image->segment_cma[idx] is
valid, the current kimage_map_segment() can't collect the source pages
at all since they are not marked with IND_DESTINATION|IND_SOURCE as
normal segment does. In that situation, we can take the direct mapping
address of image->segment_cma[idx] which is more efficient, instead of
collecting source pages and vmap().