kernel/kexec: Change the prototype of kimage_map_segment()
Subsystems (7)
PATCH 1/2 kernel/kexec: Change the prototype of kimage_map_segment()
The kexec segment index will be required to extract the corresponding information for that segment in kimage_map_segment(). Additionally, kexec_segment already holds the kexec relocation destination address and size. Therefore, the prototype of kimage_map_segment() can be changed.
To: [email protected]
To: [email protected]
--- include/linux/kexec.h | 4 ++-- kernel/kexec_core.c | 9 ++++++--- security/integrity/ima/ima_kexec.c | 4 +--- 3 files changed, 9 insertions(+), 8 deletions(-)
include/linux/kexec.h +2 -2
@@ -530,7 +530,7 @@ extern bool kexec_file_dbg_print; #define kexec_dprintk(fmt, arg...) \ do { if (kexec_file_dbg_print) pr_info(fmt, ##arg); } while (0) -extern void *kimage_map_segment (struct kimage *image,unsigned long addr,unsigned long size); +extern void *kimage_map_segment (struct kimage *image,int idx);extern void kimage_unmap_segment (void *buffer);#else /* !CONFIG_KEXEC_CORE */ struct pt_regs ;
@@ -540,7 +540,7 @@ static inline void __crash_kexec(struct pt_regs *regs) { }static inline void crash_kexec (struct pt_regs *regs) { }static inline int kexec_should_crash (struct task_struct *p) {return 0 ; }static inline int kexec_crash_loaded (void ) {return 0 ; } -static inline void *kimage_map_segment (struct kimage *image,unsigned long addr,unsigned long size) +static inline void *kimage_map_segment (struct kimage *image,int idx) {return NULL ; }static inline void kimage_unmap_segment (void *buffer) { }#define kexec_in_progress false
kernel/kexec_core.c +6 -3
@@ -960,17 +960,20 @@ int kimage_load_segment(struct kimage *image, int idx)return result; } -void *kimage_map_segment (struct kimage *image, -unsigned long addr,unsigned long size) +void *kimage_map_segment (struct kimage *image,int idx) { +unsigned long addr, size, eaddr;unsigned long src_page_addr, dest_page_addr =0 ; -unsigned long eaddr = addr + size;kimage_entry_t *ptr, entry;struct page **src_pages;unsigned int npages;void *vaddr =NULL ;int i; + addr = image->segment[idx].mem; + size = image->segment[idx].memsz; + eaddr = addr + size; +/* * Collect the source pages and map them in a contiguous VA range. */
security/integrity/ima/ima_kexec.c +1 -3
@@ -250,9 +250,7 @@ void ima_kexec_post_load(struct kimage *image)if (!image->ima_buffer_addr)return ; - ima_kexec_buffer =kimage_map_segment (image, - image->ima_buffer_addr, - image->ima_buffer_size); + ima_kexec_buffer =kimage_map_segment (image, image->ima_segment_index);if (!ima_kexec_buffer) {pr_err ("Could not map measurements buffer.\n" );return ;
Signature · 1 line
2.49.0
PATCH 2/2 kernel/kexec: Fix IMA when allocation happens in CMA area
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.
--- kernel/kexec_core.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/kexec_core.c +6 -1
@@ -967,6 +967,7 @@ void *kimage_map_segment(struct kimage *image, int idx)kimage_entry_t *ptr, entry;struct page **src_pages;unsigned int npages; +struct page *cma;void *vaddr =NULL ;int i;
@@ -974,6 +975,9 @@ void *kimage_map_segment(struct kimage *image, int idx) size = image->segment[idx].memsz; eaddr = addr + size; + cma = image->segment_cma[idx]; +if (cma) +return cma;/* * Collect the source pages and map them in a contiguous VA range. */
@@ -1014,7 +1018,8 @@ void *kimage_map_segment(struct kimage *image, int idx)void kimage_unmap_segment (void *segment_buffer) { -vunmap (segment_buffer); +if (is_vmalloc_addr (segment_buffer)) +vunmap (segment_buffer); }struct kexec_load_limit {
Signature · 1 line
2.49.0
Andrew Morton
maintainerRe: [PATCH 2/2] kernel/kexec: Fix IMA when allocation happens in CMA area
Quoted 12 lines · On Wed, 5 Nov 2025 21:09:22 +0800 Pingfan Liu <[email protected]> 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 ]---
Quoted 3 lines
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.
This is something we should backport into tearlier kernels.
Quoted 7 lines
Signed-off-by Pingfan Liu <[email protected]>Cc Andrew Morton <[email protected]>Cc Baoquan He <[email protected]>Cc Alexander Graf <[email protected]>Cc Steven Chen <[email protected]>
So I'm thinking we should add
yes?
Re: [PATCH 2/2] kernel/kexec: Fix IMA when allocation happens in CMA area
Quoted 29 lines · Andrew Morton
Quoted 12 lines · On Wed, 5 Nov 2025 21:09:22 +0800 Pingfan Liu <[email protected]> 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 ]---Quoted 3 lines
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.
This is something we should backport into tearlier kernels.
Quoted 7 lines
Signed-off-by Pingfan Liu <[email protected]>Cc Andrew Morton <[email protected]>Cc Baoquan He <[email protected]>Cc Alexander Graf <[email protected]>Cc Steven Chen <[email protected]>
Quoted 7 lines
So I'm thinking we should add
Fixes 0091d9241ea2 ("kexec: define functions to map and unmap segments")Cc <[email protected]>yes?
Yes, it should be. Thanks for your help!
Best Regards,
Pingfan
Re: [PATCH 2/2] kernel/kexec: Fix IMA when allocation happens in CMA area
In reply to Andrew MortonHi Andrew,
Thanks for your help, but on second thought, I think the Fixes commit is wrong.
Quoted 29 lines · Andrew Morton
Quoted 12 lines · On Wed, 5 Nov 2025 21:09:22 +0800 Pingfan Liu <[email protected]> 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 ]---Quoted 3 lines
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.
This is something we should backport into tearlier kernels.
Quoted 7 lines
Signed-off-by Pingfan Liu <[email protected]>Cc Andrew Morton <[email protected]>Cc Baoquan He <[email protected]>Cc Alexander Graf <[email protected]>Cc Steven Chen <[email protected]>
Quoted 3 lines
So I'm thinking we should add
Fixes 0091d9241ea2 ("kexec: define functions to map and unmap segments")
Should be:
Because 07d24902977e came after 0091d9241ea2 and introduced this issue.
Thanks,
Pingfan
Quoted 4 lines
Cc <[email protected]>yes?
Andrew Morton
maintainerRe: [PATCH 2/2] kernel/kexec: Fix IMA when allocation happens in CMA area
Quoted 14 lines · On Thu, 6 Nov 2025 10:57:33 +0800 Pingfan Liu <[email protected]> wrote:
Quoted 14 lines
Quoted 3 lines
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.
This is something we should backport into tearlier kernels.
Quoted 7 lines
Signed-off-by Pingfan Liu <[email protected]>Cc Andrew Morton <[email protected]>Cc Baoquan He <[email protected]>Cc Alexander Graf <[email protected]>Cc Steven Chen <[email protected]>
Quoted 7 lines
Quoted 3 lines
So I'm thinking we should add
Fixes 0091d9241ea2 ("kexec: define functions to map and unmap segments")Should be:
Fixes 07d24902977e ("kexec: enable CMA based contiguous allocation")Because
07d24902977ecame after0091d9241ea2and introduced this issue.
Thanks, I updated the mm.git copy of this patch.
Re: [PATCH 2/2] kernel/kexec: Fix IMA when allocation happens in CMA area
In reply to Pingfan LiuHi Pingfan,
Quoted 40 lines · Pingfan Liu
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.
Signed-off-by Pingfan Liu <[email protected]>Cc Andrew Morton <[email protected]>Cc Baoquan He <[email protected]>Cc Alexander Graf <[email protected]>Cc Steven Chen <[email protected]>--- kernel/kexec_core.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)kernel/kexec_core.c +1
-0@@ -967,6 +967,7 @@ void *kimage_map_segment(struct kimage *image, int idx)kimage_entry_t *ptr, entry;struct page **src_pages;unsigned int npages; +struct page *cma;void *vaddr =NULL ;int i;
kernel/kexec_core.c void *kimage_map_segment(struct kimage *image, int idx) · line 978
Quoted 4 lines
kernel/kexec_core.c +1
-0@@ -974,6 +975,9 @@ void *kimage_map_segment(struct kimage *image, int idx) size = image->segment[idx].memsz; eaddr = addr + size; + cma = image->segment_cma[idx];
Thanks for your fix. But I totally can't get what you are doing. The idx passed into kimage_map_segment() could index image->segment[], and can index image->segment_cma[], could you reconsider and make the code more reasonable?
Quoted 18 lines
kernel/kexec_core.c +4
-1+if (cma) +return cma;/* * Collect the source pages and map them in a contiguous VA range. */ @@ -1014,7 +1018,8 @@ void *kimage_map_segment(struct kimage *image, int idx)void kimage_unmap_segment (void *segment_buffer) { -vunmap (segment_buffer); +if (is_vmalloc_addr (segment_buffer)) +vunmap (segment_buffer); }struct kexec_load_limit {Signature · 1 line
2.49.0
Re: [PATCH 2/2] kernel/kexec: Fix IMA when allocation happens in CMA area
Hi Baoquan,
Thanks for your review. Please see the comment below.
Quoted 47 lines · Baoquan He
Hi Pingfan,
Quoted 43 lines · Pingfan Liu
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.
Signed-off-by Pingfan Liu <[email protected]>Cc Andrew Morton <[email protected]>Cc Baoquan He <[email protected]>Cc Alexander Graf <[email protected]>Cc Steven Chen <[email protected]>--- kernel/kexec_core.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)kernel/kexec_core.c +1
-0@@ -967,6 +967,7 @@ void *kimage_map_segment(struct kimage *image, int idx)kimage_entry_t *ptr, entry;struct page **src_pages;unsigned int npages; +struct page *cma;void *vaddr =NULL ;int i;@@ -974,6 +975,9 @@ void *kimage_map_segment(struct kimage *image, int idx) size = image->segment[idx].memsz; eaddr = addr + size;
Quoted 7 lines
kernel/kexec_core.c void *kimage_map_segment(struct kimage *image, int idx) · line 978
Quoted 1 line
kernel/kexec_core.c +1
-0+ cma = image->segment_cma[idx];Thanks for your fix. But I totally can't get what you are doing. The idx passed into
kimage_map_segment()could index image->segment[], and can index image->segment_cma[], could you reconsider and make the code more reasonable?
Since idx can index both image->segment[] and segment_cma[], the behavior differs based on whether segment_cma[idx] is NULL:
- If segment_cma[idx] is not NULL, it points directly to the final target location, eliminating the need for data copying that traditional kexec relocation requires.
- If segment_cma[idx] is NULL, the segment relies on the traditional kexec relocation code to copy its data.
Thanks,
Pingfan
Quoted 19 lines
Quoted 18 lines
kernel/kexec_core.c +4
-2+if (cma) +return cma;/* * Collect the source pages and map them in a contiguous VA range. */ @@ -1014,7 +1018,8 @@ void *kimage_map_segment(struct kimage *image, int idx)void kimage_unmap_segment (void *segment_buffer) { -vunmap (segment_buffer); +if (is_vmalloc_addr (segment_buffer)) +vunmap (segment_buffer); }struct kexec_load_limit { --2.49.0
Re: [PATCH 2/2] kernel/kexec: Fix IMA when allocation happens in CMA area
Quoted 60 lines · Pingfan Liu
Hi Baoquan,
Thanks for your review. Please see the comment below.
Quoted 47 lines · Baoquan He
Hi Pingfan,
Quoted 43 lines · Pingfan Liu
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.
Signed-off-by Pingfan Liu <[email protected]>Cc Andrew Morton <[email protected]>Cc Baoquan He <[email protected]>Cc Alexander Graf <[email protected]>Cc Steven Chen <[email protected]>--- kernel/kexec_core.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)kernel/kexec_core.c +1
-0@@ -967,6 +967,7 @@ void *kimage_map_segment(struct kimage *image, int idx)kimage_entry_t *ptr, entry;struct page **src_pages;unsigned int npages; +struct page *cma;void *vaddr =NULL ;int i;@@ -974,6 +975,9 @@ void *kimage_map_segment(struct kimage *image, int idx) size = image->segment[idx].memsz; eaddr = addr + size;Quoted 7 lines
kernel/kexec_core.c void *kimage_map_segment(struct kimage *image, int idx) · line 978
Quoted 1 line
kernel/kexec_core.c +1
-0+ cma = image->segment_cma[idx];Thanks for your fix. But I totally can't get what you are doing. The idx passed into
kimage_map_segment()could index image->segment[], and can index image->segment_cma[], could you reconsider and make the code more reasonable?
Quoted 8 lines
Since idx can index both image->segment[] and segment_cma[], the behavior differs based on whether segment_cma[idx] is NULL:
- If segment_cma[idx] is not NULL, it points directly to the final target location, eliminating the need for data copying that traditional kexec relocation requires.
- If segment_cma[idx] is NULL, the segment relies on the traditional kexec relocation code to copy its data.
I see, thanks. While image->segment_cma[idx] records the struct page of the relevant cma area, but not virtual address. Is it OK for IMA later to update? ima_kexec_buffer is supposed to be a virtual address, wondering how IMA behaved in this case.
Re: [PATCH 2/2] kernel/kexec: Fix IMA when allocation happens in CMA area
Quoted 65 lines · Baoquan He
Quoted 63 lines · Pingfan Liu
Hi Baoquan,
Thanks for your review. Please see the comment below.
Quoted 47 lines · Baoquan He
Hi Pingfan,
Quoted 43 lines · Pingfan Liu
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.
Signed-off-by Pingfan Liu <[email protected]>Cc Andrew Morton <[email protected]>Cc Baoquan He <[email protected]>Cc Alexander Graf <[email protected]>Cc Steven Chen <[email protected]>--- kernel/kexec_core.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)kernel/kexec_core.c +1
-0@@ -967,6 +967,7 @@ void *kimage_map_segment(struct kimage *image, int idx)kimage_entry_t *ptr, entry;struct page **src_pages;unsigned int npages; +struct page *cma;void *vaddr =NULL ;int i;@@ -974,6 +975,9 @@ void *kimage_map_segment(struct kimage *image, int idx) size = image->segment[idx].memsz; eaddr = addr + size;Quoted 7 lines
kernel/kexec_core.c void *kimage_map_segment(struct kimage *image, int idx) · line 978
Quoted 1 line
kernel/kexec_core.c +1
-0+ cma = image->segment_cma[idx];Thanks for your fix. But I totally can't get what you are doing. The idx passed into
kimage_map_segment()could index image->segment[], and can index image->segment_cma[], could you reconsider and make the code more reasonable?Since idx can index both image->segment[] and segment_cma[], the behavior differs based on whether segment_cma[idx] is NULL:
Quoted 8 lines
Quoted 5 lines
- If segment_cma[idx] is not NULL, it points directly to the final target location, eliminating the need for data copying that traditional kexec relocation requires.
- If segment_cma[idx] is NULL, the segment relies on the traditional kexec relocation code to copy its data.I see, thanks. While image->segment_cma[idx] records the struct page of the relevant cma area, but not virtual address. Is it OK for IMA later
Oops. It requires page_address(page) to convert the address. I will send out V2 to fix it.
Thanks,
Pingfan