Skip to contentSkip to conversation

kexec_file: skip checksum verification when relocations aren't needed

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

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

PATCH 1/1 kexec_file: skip checksum verification when relocations aren't needed

Checksum verification is needed
1. for crash kernels. In a crash, we can't be sure the kernel is intact.
2. if we're worried about relocating the kernel into a region used by some DMA that wasn't properly cancelled.

If we used CMA to allocate segments then
1. we're not working with a crash kernel.
2. relocations are not going to happen.

Therefore, we can safely disable checksum verification.

Instead of adding a new variable to purgatory, just skip adding regions and save the default value of SHA256 hash.

Saves ~250ms on my 4.0 GHz CPU.

---
 kernel/kexec_file.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
kernel/kexec_file.c +19 -0
index 2bfbb2d144e6..2dc8b0435fe6 100644
@@ -808,6 +808,7 @@ static int kexec_calculate_store_digests(struct kimage *image)
 	void *zero_buf;
 	struct kexec_sha_region *sha_regions;
 	struct purgatory_info *pi = &image->purgatory_info;
+	bool can_skip_checksum = true;
 
 	if (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY))
 		return 0;
@@ -822,6 +823,23 @@ static int kexec_calculate_store_digests(struct kimage *image)
 
 	sha256_init(&sctx);
 
+	/*
+	 * If all segments were loaded into contiguous memory, there will be no
+	 * relocations. In that case there is no risk of memory corruption by
+	 * uncancelled DMA and we can skip checksum calculation.
+	 */
+	for (i = 0; i < image->nr_segments; i++) {
+		if (!image->segment_cma[i]) {
+			can_skip_checksum = false;
+			break;
+		}
+	}
+
+	if (can_skip_checksum) {
+		pr_info("disabling checksum verification in purgatory\n");
+		goto skip_checksum;
+	}
+
 	for (j = i = 0; i < image->nr_segments; i++) {
 		struct kexec_segment *ksegment;
 
@@ -867,6 +885,7 @@ static int kexec_calculate_store_digests(struct kimage *image)
 		j++;
 	}
 
+skip_checksum:
 	sha256_final(&sctx, digest);
 
 	ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
Signature · 1 line
2.54.0.929.g9b7fa37559-goog
maintainer Reviewed-by on v1 1/1
#1 lore raw

Nit: The crash kernel also does not perform relocations, yet a checksum is still required. The subject should be something like:
kexec_file: skip purgatory checksum if all segments are CMA allocated

Quoted 5 lines · Michal Clapinski

Checksum verification is needed
1. for crash kernels. In a crash, we can't be sure the kernel is intact.
2. if we're worried about relocating the kernel into a region used by some DMA that wasn't properly cancelled.

Nit: Please add a little background information about CMA segments being recently added, as well as the necessity for a fast reboot due to the live update use case.

Quoted 54 lines

If we used CMA to allocate segments then
1. we're not working with a crash kernel.
2. relocations are not going to happen.

Therefore, we can safely disable checksum verification.

Instead of adding a new variable to purgatory, just skip adding regions and save the default value of SHA256 hash.

Saves ~250ms on my 4.0 GHz CPU.

---
 kernel/kexec_file.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
kernel/kexec_file.c +18 -0
index 2bfbb2d144e6..2dc8b0435fe6 100644
@@ -808,6 +808,7 @@ static int kexec_calculate_store_digests(struct kimage *image)
 	void *zero_buf;
 	struct kexec_sha_region *sha_regions;
 	struct purgatory_info *pi = &image->purgatory_info;
+	bool can_skip_checksum = true;
 
 	if (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY))
 		return 0;
@@ -822,6 +823,23 @@ static int kexec_calculate_store_digests(struct kimage *image)
 
 	sha256_init(&sctx);
 
+	/*
+	 * If all segments were loaded into contiguous memory, there will be no
+	 * relocations. In that case there is no risk of memory corruption by
+	 * uncancelled DMA and we can skip checksum calculation.
+	 */
+	for (i = 0; i < image->nr_segments; i++) {
+		if (!image->segment_cma[i]) {
+			can_skip_checksum = false;
+			break;
+		}
+	}
+
+	if (can_skip_checksum) {
+		pr_info("disabling checksum verification in purgatory\n");
+		goto skip_checksum;
+	}
+
 	for (j = i = 0; i < image->nr_segments; i++) {
 		struct kexec_segment *ksegment;
 

kernel/kexec_file.c static int kexec_calculate_store_digests(struct kimage *image) · line 889

Quoted 5 lines
kernel/kexec_file.c +1 -0
@@ -867,6 +885,7 @@ static int kexec_calculate_store_digests(struct kimage *image)
 		j++;
 	}
 
+skip_checksum:
 	sha256_final(&sctx, digest);

With the few nits:

Quoted 5 lines
kernel/kexec_file.c +0 -0
 	ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
Signature · 1 line
2.54.0.929.g9b7fa37559-goog
maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Michal Clapinski

On 06/01/26 at 09:11pm, Michal Clapinski wrote:
...snip...

Quoted 4 lines
kernel/kexec_file.c +4 -0
+	/*
+	 * If all segments were loaded into contiguous memory, there will be no
+	 * relocations. In that case there is no risk of memory corruption by
+	 * uncancelled DMA and we can skip checksum calculation.

kernel/kexec_file.c static int kexec_calculate_store_digests(struct kimage *image) · line 839

Quoted 10 lines
kernel/kexec_file.c +10 -0
+	 */
+	for (i = 0; i < image->nr_segments; i++) {
+		if (!image->segment_cma[i]) {
+			can_skip_checksum = false;
+			break;
+		}
+	}
+
+	if (can_skip_checksum) {
+		pr_info("disabling checksum verification in purgatory\n");

Use pr_debug() or kexec_dprintk() instead because this is unnecessary to note users if it's a normal action?

Except of this, the overral looks good to me.

Quoted 17 lines
kernel/kexec_file.c +4 -0
+		goto skip_checksum;
+	}
+
 	for (j = i = 0; i < image->nr_segments; i++) {
 		struct kexec_segment *ksegment;
@@ -867,6 +885,7 @@ static int kexec_calculate_store_digests(struct kimage *image)
 		j++;
 	}
 
+skip_checksum:
 	sha256_final(&sctx, digest);
 
 	ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
Signature · 1 line
2.54.0.929.g9b7fa37559-goog