Skip to contentSkip to conversation

kho: try to allocate contiguous memory for kexec segments

PATCH In review Michal Clapinski 2 participants 5 messages Jun 1 – Jun 2

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kho: try to allocate contiguous memory for kexec segments

This allows us to skip relocations (and maybe checksum calculation in the future).

kho_scratch is marked as MIGRATE_CMA but isn't actually given to the CMA, so it should only contain movable allocations, therefore this should always succeed.

---
 kernel/kexec_core.c                |  6 +++++-
 kernel/liveupdate/kexec_handover.c | 21 +++++++++++++++++----
 2 files changed, 22 insertions(+), 5 deletions(-)
kernel/kexec_core.c +5 -1
index dc770b9a6d05..cba3ce985aa9 100644
@@ -43,6 +43,7 @@
 #include <linux/kmsg_dump.h>
 #include <linux/dma-map-ops.h>
 #include <linux/sysfs.h>
+#include <linux/kexec_handover.h>
 
 #include <asm/page.h>
 #include <asm/sections.h>
@@ -566,7 +567,10 @@ static void kimage_free_cma(struct kimage *image)
 			continue;
 
 		arch_kexec_pre_free_pages(page_address(cma), nr_pages);
-		dma_release_from_contiguous(NULL, cma, nr_pages);
+		if (kho_is_enabled())
+			free_contig_range(page_to_pfn(cma), nr_pages);
+		else
+			dma_release_from_contiguous(NULL, cma, nr_pages);
 		image->segment_cma[i] = NULL;
 	}
 
kernel/liveupdate/kexec_handover.c +17 -4
index 4834a809985a..289fd5948fd2 100644
@@ -1770,15 +1770,28 @@ static int kho_walk_scratch(struct kexec_buf *kbuf,
 	return ret;
 }
 
+static void kho_try_alloc_contig(struct kexec_buf *kbuf)
+{
+	unsigned long start_pfn = PFN_DOWN(kbuf->mem);
+	unsigned long nr_pages = kbuf->memsz >> PAGE_SHIFT;
+
+	if (alloc_contig_range(start_pfn, start_pfn + nr_pages,
+			       ACR_FLAGS_CMA, GFP_KERNEL))
+		return;
+
+	kbuf->cma = pfn_to_page(start_pfn);
+	arch_kexec_post_alloc_pages(page_address(kbuf->cma), nr_pages, 0);
+}
+
 int kho_locate_mem_hole(struct kexec_buf *kbuf,
 			int (*func)(struct resource *, void *))
 {
-	int ret;
-
 	if (!kho_enable || kbuf->image->type == KEXEC_TYPE_CRASH)
 		return 1;
 
-	ret = kho_walk_scratch(kbuf, func);
+	if (!kho_walk_scratch(kbuf, func))
+		return -EADDRNOTAVAIL;
 
-	return ret == 1 ? 0 : -EADDRNOTAVAIL;
+	kho_try_alloc_contig(kbuf);
+	return 0;
 }
Signature · 1 line
2.54.0.929.g9b7fa37559-goog
#1 lore raw
Quoted 7 lines · Michal Clapinski

This allows us to skip relocations (and maybe checksum calculation in the future).

kho_scratch is marked as MIGRATE_CMA but isn't actually given to the CMA, so it should only contain movable allocations, therefore this should always succeed.

Now that I think about it, this is only true on the primary boot. On subsequent boots, kho scratch will contain memblock allocations forever. I should have tested it more than once.

I have no idea how probable it is that I will find enough movable/free memory in kho scratch for this to ever succeed. I'll give it more thought.

maintainer
#2 lore raw
In reply to Michal Clapinski
Quoted 2 lines · Michal Clapinski

This allows us to skip relocations (and maybe checksum calculation in the future).

I'm confused. Doesn't your patch "kexec_file: skip checksum verification when safe" [0] skip the checksum for KHO already? So this only skips the relocations part then?

And based on the discussion on that thread, relocations don't seem to take much time. So is there a real need for this patch?

[0] https://lore.kernel.org/kexec/[email protected]/

Quoted 6 lines

kho_scratch is marked as MIGRATE_CMA but isn't actually given to the CMA, so it should only contain movable allocations, therefore this should always succeed.

[...]

Signature · 2 lines
Regards,
Pratyush Yadav
#3 lore raw
Quoted 6 lines · Pratyush Yadav
Quoted 2 lines · Michal Clapinski

This allows us to skip relocations (and maybe checksum calculation in the future).

Quoted 6 lines

I'm confused. Doesn't your patch "kexec_file: skip checksum verification when safe" [0] skip the checksum for KHO already? So this only skips the relocations part then?

And based on the discussion on that thread, relocations don't seem to take much time. So is there a real need for this patch?

I sent this patch out yesterday, then realized it wasn't going to work so I abandoned this approach.
Today I sent patch [0] that skips the checksum for KHO, so this patch is no longer needed.

Quoted 13 lines

[0] https://lore.kernel.org/kexec/[email protected]/

Quoted 6 lines

kho_scratch is marked as MIGRATE_CMA but isn't actually given to the CMA, so it should only contain movable allocations, therefore this should always succeed.

[...]

--
Regards,
Pratyush Yadav

maintainer
#4 lore raw
Quoted 11 lines · Michał Cłapiński
Quoted 10 lines · Pratyush Yadav
Quoted 2 lines · Michal Clapinski

This allows us to skip relocations (and maybe checksum calculation in the future).

I'm confused. Doesn't your patch "kexec_file: skip checksum verification when safe" [0] skip the checksum for KHO already? So this only skips the relocations part then?

Quoted 7 lines
Quoted 2 lines

And based on the discussion on that thread, relocations don't seem to take much time. So is there a real need for this patch?

I sent this patch out yesterday, then realized it wasn't going to work so I abandoned this approach.
Today I sent patch [0] that skips the checksum for KHO, so this patch is no longer needed.

Makes sense, thanks for clarifying.

[...]

Signature · 2 lines
Regards,
Pratyush Yadav