Skip to contentSkip to conversation

kernel/kexec: Change the prototype of kimage_map_segment()

PATCH Superseded Pingfan Liu 3 participants 10 messages Nov 5, 2025 – Nov 7, 2025

Subsystems (7)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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.

Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Mimi Zohar <[email protected]>
Cc Roberto Sassu <[email protected]>
Cc Alexander Graf <[email protected]>
Cc Steven Chen <[email protected]>

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
index ff7e231b0485..8a22bc9b8c6c 100644
@@ -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
index fa00b239c5d9..9a1966207041 100644
@@ -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
index 7362f68f2d8b..5beb69edd12f 100644
@@ -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
#1 lore raw

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.

Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Alexander Graf <[email protected]>
Cc Steven Chen <[email protected]>

To: [email protected]

---
 kernel/kexec_core.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/kexec_core.c +6 -1
index 9a1966207041..abe40286a02c 100644
@@ -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
maintainer
#2 lore raw

Re: [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
Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Alexander Graf <[email protected]>
Cc Steven Chen <[email protected]>

To: [email protected]

So I'm thinking we should add

Fixes 0091d9241ea2 ("kexec: define functions to map and unmap segments")

yes?

#3 lore raw

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
Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Alexander Graf <[email protected]>
Cc Steven Chen <[email protected]>

To: [email protected]

Quoted 7 lines

So I'm thinking we should add

Fixes 0091d9241ea2 ("kexec: define functions to map and unmap segments")

yes?

Yes, it should be. Thanks for your help!

Best Regards,

Pingfan

#4 lore raw

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

In reply to Andrew Morton

Hi 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
Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Alexander Graf <[email protected]>
Cc Steven Chen <[email protected]>

To: [email protected]

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 07d24902977e came after 0091d9241ea2 and introduced this issue.

Thanks,

Pingfan

Quoted 4 lines

yes?

maintainer
#5 lore raw

Re: [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
Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Alexander Graf <[email protected]>
Cc Steven Chen <[email protected]>

To: [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 07d24902977e came after 0091d9241ea2 and introduced this issue.

Thanks, I updated the mm.git copy of this patch.

#6 lore raw

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

In reply to Pingfan Liu

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

Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Alexander Graf <[email protected]>
Cc Steven Chen <[email protected]>

To: [email protected]

---
 kernel/kexec_core.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/kexec_core.c +1 -0
index 9a1966207041..abe40286a02c 100644
@@ -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
#7 lore raw

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.

Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Alexander Graf <[email protected]>
Cc Steven Chen <[email protected]>

To: [email protected]

---
 kernel/kexec_core.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/kexec_core.c +1 -0
index 9a1966207041..abe40286a02c 100644
@@ -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

#8 lore raw

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.

Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Alexander Graf <[email protected]>
Cc Steven Chen <[email protected]>

To: [email protected]

---
 kernel/kexec_core.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/kexec_core.c +1 -0
index 9a1966207041..abe40286a02c 100644
@@ -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.

#9 lore raw

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.

Cc Andrew Morton <[email protected]>
Cc Baoquan He <[email protected]>
Cc Alexander Graf <[email protected]>
Cc Steven Chen <[email protected]>

To: [email protected]

---
 kernel/kexec_core.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/kexec_core.c +1 -0
index 9a1966207041..abe40286a02c 100644
@@ -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