Skip to contentSkip to conversation

kexec: return -ENOEXEC from image probe functions on mismatch

PATCH Applied Mukesh Pilaniya 4 participants 9 messages Aug 13 – Nov 1

Subsystems (6)

v1 of 3 Applied 1/1 Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kexec: return -ENOEXEC from image probe functions on mismatch

Several kexec_file_load() image probe functions return -EINVAL when they do not recognize the image format. A probe function that rejects an image should return -ENOEXEC to indicate that the image is not a recognized executable format. -EINVAL implies a problem with the syscall parameters, not with image recognition.

kexec_image_probe_default() iterates through registered loaders and returns the last probe's error code to the caller. That error propagates as the kexec_file_load() return value to userspace. Returning -EINVAL from a probe when no loader matches is semantically incorrect and misleads userspace about the nature of the failure.

Return -ENOEXEC from all probe functions and their helpers when the image format is not recognized.

---
branch: next-20260812
base commit: 28d012efb4327f9c75d5e042a7c91e9a542efa98

---
 arch/arm64/kernel/kexec_image.c   | 4 ++--
 arch/loongarch/kernel/kexec_efi.c | 4 ++--
 arch/riscv/kernel/kexec_image.c   | 4 ++--
 kernel/kexec_elf.c                | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)
arch/arm64/kernel/kexec_image.c +2 -2
index b70f4df15a1a..101259874d44 100644
@@ -25,10 +25,10 @@ static int image_probe(const char *kernel_buf, unsigned long kernel_len)
 		(const struct arm64_image_header *)(kernel_buf);
 
 	if (!h || (kernel_len < sizeof(*h)))
-		return -EINVAL;
+		return -ENOEXEC;
 
 	if (memcmp(&h->magic, ARM64_IMAGE_MAGIC, sizeof(h->magic)))
-		return -EINVAL;
+		return -ENOEXEC;
 
 	return 0;
 }
arch/loongarch/kernel/kexec_efi.c +2 -2
index 5ee78ebb1546..28a1d0420ba3 100644
@@ -24,12 +24,12 @@ static int efi_kexec_probe(const char *kernel_buf, unsigned long kernel_len)
 
 	if (!h || (kernel_len < sizeof(*h))) {
 		kexec_dprintk("No LoongArch image header.\n");
-		return -EINVAL;
+		return -ENOEXEC;
 	}
 
 	if (!loongarch_header_check_dos_sig(h)) {
 		kexec_dprintk("No LoongArch PE image header.\n");
-		return -EINVAL;
+		return -ENOEXEC;
 	}
 
 	return 0;
arch/riscv/kernel/kexec_image.c +2 -2
index 51dc89259f16..963a25f5b55b 100644
@@ -20,7 +20,7 @@ static int image_probe(const char *kernel_buf, unsigned long kernel_len)
 	const struct riscv_image_header *h = (const struct riscv_image_header *)kernel_buf;
 
 	if (!h || kernel_len < sizeof(*h))
-		return -EINVAL;
+		return -ENOEXEC;
 
 	/* According to Documentation/arch/riscv/boot-image-header.rst,
 	 * use "magic2" field to check when version >= 0.2.
@@ -28,7 +28,7 @@ static int image_probe(const char *kernel_buf, unsigned long kernel_len)
 
 	if (h->version >= RISCV_HEADER_VERSION &&
 	    memcmp(&h->magic2, RISCV_IMAGE_MAGIC2, sizeof(h->magic2)))
-		return -EINVAL;
+		return -ENOEXEC;
 
 	return 0;
 }
kernel/kexec_elf.c +2 -2
index 3a5c25b2adc9..89a444a00693 100644
@@ -172,7 +172,7 @@ static int elf_read_ehdr(const char *buf, size_t len, struct elfhdr *ehdr)
 
 	default:
 		pr_debug("Unknown ELF class.\n");
-		return -EINVAL;
+		return -ENOEXEC;
 	}
 
 	return elf_is_ehdr_sane(ehdr, len) ? 0 : -ENOEXEC;
@@ -236,7 +236,7 @@ static int elf_read_phdr(const char *buf, size_t len,
 
 	default:
 		pr_debug("Unknown ELF class.\n");
-		return -EINVAL;
+		return -ENOEXEC;
 	}
 
 	return elf_is_phdr_sane(phdr, len) ? 0 : -ENOEXEC;

---
base-commit: 28d012efb4327f9c75d5e042a7c91e9a542efa98
change-id: 20260813-mpilaniy-ffb0cc92c313

Best regards,

Signature · 1 line
Mukesh Pilaniya <[email protected]>
Reviewed-by on v1 1/1
#1 lore raw

Hi Mukesh,

the patch looks good to me.

There is also a corresponding kexec-tools patch, which was accidentally opened as a Github PR (https://github.com/horms/kexec-tools/pull/12).
It will be sent to [email protected] shortly.

Thanks

Quoted 115 lines · On Thu, 13 Aug 2026 12:36:29 +0530 Mukesh Pilaniya <[email protected]> wrote:

Several kexec_file_load() image probe functions return -EINVAL when they do not recognize the image format. A probe function that rejects an image should return -ENOEXEC to indicate that the image is not a recognized executable format. -EINVAL implies a problem with the syscall parameters, not with image recognition.

kexec_image_probe_default() iterates through registered loaders and returns the last probe's error code to the caller. That error propagates as the kexec_file_load() return value to userspace. Returning -EINVAL from a probe when no loader matches is semantically incorrect and misleads userspace about the nature of the failure.

Return -ENOEXEC from all probe functions and their helpers when the image format is not recognized.

---
branch: next-20260812
base commit: 28d012efb4327f9c75d5e042a7c91e9a542efa98

---
 arch/arm64/kernel/kexec_image.c   | 4 ++--
 arch/loongarch/kernel/kexec_efi.c | 4 ++--
 arch/riscv/kernel/kexec_image.c   | 4 ++--
 kernel/kexec_elf.c                | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)
arch/arm64/kernel/kexec_image.c +2 -2
index b70f4df15a1a..101259874d44 100644
@@ -25,10 +25,10 @@ static int image_probe(const char *kernel_buf, unsigned long kernel_len)
 		(const struct arm64_image_header *)(kernel_buf);
 
 	if (!h || (kernel_len < sizeof(*h)))
-		return -EINVAL;
+		return -ENOEXEC;
 
 	if (memcmp(&h->magic, ARM64_IMAGE_MAGIC, sizeof(h->magic)))
-		return -EINVAL;
+		return -ENOEXEC;
 
 	return 0;
 }
arch/loongarch/kernel/kexec_efi.c +2 -2
index 5ee78ebb1546..28a1d0420ba3 100644
@@ -24,12 +24,12 @@ static int efi_kexec_probe(const char *kernel_buf, unsigned long kernel_len)
 
 	if (!h || (kernel_len < sizeof(*h))) {
 		kexec_dprintk("No LoongArch image header.\n");
-		return -EINVAL;
+		return -ENOEXEC;
 	}
 
 	if (!loongarch_header_check_dos_sig(h)) {
 		kexec_dprintk("No LoongArch PE image header.\n");
-		return -EINVAL;
+		return -ENOEXEC;
 	}
 
 	return 0;
arch/riscv/kernel/kexec_image.c +2 -2
index 51dc89259f16..963a25f5b55b 100644
@@ -20,7 +20,7 @@ static int image_probe(const char *kernel_buf, unsigned long kernel_len)
 	const struct riscv_image_header *h = (const struct riscv_image_header *)kernel_buf;
 
 	if (!h || kernel_len < sizeof(*h))
-		return -EINVAL;
+		return -ENOEXEC;
 
 	/* According to Documentation/arch/riscv/boot-image-header.rst,
 	 * use "magic2" field to check when version >= 0.2.
@@ -28,7 +28,7 @@ static int image_probe(const char *kernel_buf, unsigned long kernel_len)
 
 	if (h->version >= RISCV_HEADER_VERSION &&
 	    memcmp(&h->magic2, RISCV_IMAGE_MAGIC2, sizeof(h->magic2)))
-		return -EINVAL;
+		return -ENOEXEC;
 
 	return 0;
 }
kernel/kexec_elf.c +2 -3
index 3a5c25b2adc9..89a444a00693 100644
@@ -172,7 +172,7 @@ static int elf_read_ehdr(const char *buf, size_t len, struct elfhdr *ehdr)
 
 	default:
 		pr_debug("Unknown ELF class.\n");
-		return -EINVAL;
+		return -ENOEXEC;
 	}
 
 	return elf_is_ehdr_sane(ehdr, len) ? 0 : -ENOEXEC;
@@ -236,7 +236,7 @@ static int elf_read_phdr(const char *buf, size_t len,
 
 	default:
 		pr_debug("Unknown ELF class.\n");
-		return -EINVAL;
+		return -ENOEXEC;
 	}
 
 	return elf_is_phdr_sane(phdr, len) ? 0 : -ENOEXEC;
 
---

base-commit: 28d012efb4327f9c75d5e042a7c91e9a542efa98
change-id: 20260813-mpilaniy-ffb0cc92c313

Best regards,

Signature · 1 line
Mukesh Pilaniya <[email protected]>
#2 lore raw

Hi Philipp,

Quoted 4 lines · Philipp Rudo

Hi Mukesh,

the patch looks good to me.

Quoted 8 lines

There is also a corresponding kexec-tools patch, which was accidentally opened as a Github PR (https://github.com/horms/kexec-tools/pull/12).
It will be sent to [email protected] shortly.

Thanks

The kexec-tools patch has now been posted to the mailing list. It can be found here:
https://lore.kernel.org/all/[email protected]/

Quoted 119 lines
Quoted 115 lines · On Thu, 13 Aug 2026 12:36:29 +0530 Mukesh Pilaniya <[email protected]> wrote:

Several kexec_file_load() image probe functions return -EINVAL when they do not recognize the image format. A probe function that rejects an image should return -ENOEXEC to indicate that the image is not a recognized executable format. -EINVAL implies a problem with the syscall parameters, not with image recognition.

kexec_image_probe_default() iterates through registered loaders and returns the last probe's error code to the caller. That error propagates as the kexec_file_load() return value to userspace. Returning -EINVAL from a probe when no loader matches is semantically incorrect and misleads userspace about the nature of the failure.

Return -ENOEXEC from all probe functions and their helpers when the image format is not recognized.

---
branch: next-20260812
base commit: 28d012efb4327f9c75d5e042a7c91e9a542efa98

---
  arch/arm64/kernel/kexec_image.c   | 4 ++--
  arch/loongarch/kernel/kexec_efi.c | 4 ++--
  arch/riscv/kernel/kexec_image.c   | 4 ++--
  kernel/kexec_elf.c                | 4 ++--
  4 files changed, 8 insertions(+), 8 deletions(-)
arch/arm64/kernel/kexec_image.c +2 -2
index b70f4df15a1a..101259874d44 100644
@@ -25,10 +25,10 @@ static int image_probe(const char *kernel_buf, unsigned long kernel_len)
  		(const struct arm64_image_header *)(kernel_buf);
 
  	if (!h || (kernel_len < sizeof(*h)))
-		return -EINVAL;
+		return -ENOEXEC;
 
  	if (memcmp(&h->magic, ARM64_IMAGE_MAGIC, sizeof(h->magic)))
-		return -EINVAL;
+		return -ENOEXEC;
 
  	return 0;
  }
arch/loongarch/kernel/kexec_efi.c +2 -2
index 5ee78ebb1546..28a1d0420ba3 100644
@@ -24,12 +24,12 @@ static int efi_kexec_probe(const char *kernel_buf, unsigned long kernel_len)
 
  	if (!h || (kernel_len < sizeof(*h))) {
  		kexec_dprintk("No LoongArch image header.\n");
-		return -EINVAL;
+		return -ENOEXEC;
  	}
 
  	if (!loongarch_header_check_dos_sig(h)) {
  		kexec_dprintk("No LoongArch PE image header.\n");
-		return -EINVAL;
+		return -ENOEXEC;
  	}
 
  	return 0;
arch/riscv/kernel/kexec_image.c +2 -2
index 51dc89259f16..963a25f5b55b 100644
@@ -20,7 +20,7 @@ static int image_probe(const char *kernel_buf, unsigned long kernel_len)
  	const struct riscv_image_header *h = (const struct riscv_image_header *)kernel_buf;
 
  	if (!h || kernel_len < sizeof(*h))
-		return -EINVAL;
+		return -ENOEXEC;
 
  	/* According to Documentation/arch/riscv/boot-image-header.rst,
  	 * use "magic2" field to check when version >= 0.2.
@@ -28,7 +28,7 @@ static int image_probe(const char *kernel_buf, unsigned long kernel_len)
 
  	if (h->version >= RISCV_HEADER_VERSION &&
  	    memcmp(&h->magic2, RISCV_IMAGE_MAGIC2, sizeof(h->magic2)))
-		return -EINVAL;
+		return -ENOEXEC;
 
  	return 0;
  }
kernel/kexec_elf.c +2 -3
index 3a5c25b2adc9..89a444a00693 100644
@@ -172,7 +172,7 @@ static int elf_read_ehdr(const char *buf, size_t len, struct elfhdr *ehdr)
 
  	default:
  		pr_debug("Unknown ELF class.\n");
-		return -EINVAL;
+		return -ENOEXEC;
  	}
 
  	return elf_is_ehdr_sane(ehdr, len) ? 0 : -ENOEXEC;
@@ -236,7 +236,7 @@ static int elf_read_phdr(const char *buf, size_t len,
 
  	default:
  		pr_debug("Unknown ELF class.\n");
-		return -EINVAL;
+		return -ENOEXEC;
  	}
 
  	return elf_is_phdr_sane(phdr, len) ? 0 : -ENOEXEC;
 
---

base-commit: 28d012efb4327f9c75d5e042a7c91e9a542efa98
change-id: 20260813-mpilaniy-ffb0cc92c313

Best regards,
--
Mukesh Pilaniya <[email protected]>

maintainer
#3 lore raw
In reply to Mukesh Pilaniya
Quoted 6 lines · Mukesh Pilaniya

Several kexec_file_load() image probe functions return -EINVAL when they do not recognize the image format. A probe function that rejects an image should return -ENOEXEC to indicate that the image is not a recognized executable format. -EINVAL implies a problem with the syscall parameters, not with image recognition.

Quoted 8 lines

kexec_image_probe_default() iterates through registered loaders and returns the last probe's error code to the caller. That error propagates as the kexec_file_load() return value to userspace. Returning -EINVAL from a probe when no loader matches is semantically incorrect and misleads userspace about the nature of the failure.

Return -ENOEXEC from all probe functions and their helpers when the image format is not recognized.

Sounds fine in principle but can you please also share what the real problem you face is and how changing these return codes helps? These error codes are uAPI and while we _can_ change them as long as we don't break something, there should be a clear motivation for doing so.

[...]

Signature · 2 lines
Regards,
Pratyush Yadav
#4 lore raw

Hi Pratyush,

Quoted 17 lines · Pratyush Yadav
Quoted 6 lines · Mukesh Pilaniya

Several kexec_file_load() image probe functions return -EINVAL when they do not recognize the image format. A probe function that rejects an image should return -ENOEXEC to indicate that the image is not a recognized executable format. -EINVAL implies a problem with the syscall parameters, not with image recognition.

Quoted 8 lines

kexec_image_probe_default() iterates through registered loaders and returns the last probe's error code to the caller. That error propagates as the kexec_file_load() return value to userspace. Returning -EINVAL from a probe when no loader matches is semantically incorrect and misleads userspace about the nature of the failure.

Return -ENOEXEC from all probe functions and their helpers when the image format is not recognized.

Quoted 7 lines

Sounds fine in principle but can you please also share what the real problem you face is and how changing these return codes helps? These error codes are uAPI and while we _can_ change them as long as we don't break something, there should be a clear motivation for doing so.

[...]

While debugging a misleading error on s390x where kexec -s reported "syscall kexec_file_load not available" instead of the actual EINVAL from a kernel command line that exceeded the architecture limit, we traced the problem to the kexec-tools userspace utility treating EINVAL the same as ENOSYS and ENOEXEC -- as a signal to silently fall back to kexec_load().

kexec-tools supports two syscalls: kexec_file_load() and the older kexec_load(). With -a (the default), it tries kexec_file_load() first and falls back to kexec_load() when the syscall is not implemented (ENOSYS) or the kernel does not have a loader for the image format. With -s, it uses kexec_file_load() only with no fallback.

When the kernel returns -EINVAL it means something went wrong while loading the image, not that the syscall is missing or the image format is unrecognized. kexec-tools should not fall back to the older syscall in that case. However, some kernel probe functions currently return -EINVAL when the image header does not match, instead of returning -ENOEXEC. Keeping EINVAL in the fallback set to accommodate these probes has the side effect of also hiding genuine loading errors like an oversized command line.

kexec-tools should only fall back when kexec_file_load() is not implemented or does not have a matching loader -- not when something goes wrong during load.

The fix on the kexec-tools side is to remove EINVAL from the fallback set, but that requires the kernel to be clean first -- probe functions must return -ENOEXEC when they do not recognize an image format, not -EINVAL.

kexec-tools patch:
https://lore.kernel.org/all/[email protected]/

A review of all kexec_file_ops.probe implementations found that arm64 image_probe(), riscv image_probe(), and loongarch efi_kexec_probe() return -EINVAL where they should return -ENOEXEC. x86 bzImage64_probe() and s390 s390_elf_probe() already use -ENOEXEC correctly.

maintainer
#5 lore raw
Quoted 49 lines · Mukesh Pilaniya

Hi Pratyush,

Quoted 17 lines · Pratyush Yadav
Quoted 6 lines · Mukesh Pilaniya

Several kexec_file_load() image probe functions return -EINVAL when they do not recognize the image format. A probe function that rejects an image should return -ENOEXEC to indicate that the image is not a recognized executable format. -EINVAL implies a problem with the syscall parameters, not with image recognition.

Quoted 8 lines

kexec_image_probe_default() iterates through registered loaders and returns the last probe's error code to the caller. That error propagates as the kexec_file_load() return value to userspace. Returning -EINVAL from a probe when no loader matches is semantically incorrect and misleads userspace about the nature of the failure.

Return -ENOEXEC from all probe functions and their helpers when the image format is not recognized.

Quoted 7 lines

Sounds fine in principle but can you please also share what the real problem you face is and how changing these return codes helps? These error codes are uAPI and while we _can_ change them as long as we don't break something, there should be a clear motivation for doing so.

[...]

While debugging a misleading error on s390x where kexec -s reported "syscall kexec_file_load not available" instead of the actual EINVAL from a kernel command line that exceeded the architecture limit, we traced the problem to the kexec-tools userspace utility treating EINVAL the same as ENOSYS and ENOEXEC -- as a signal to silently fall back to kexec_load().

kexec-tools supports two syscalls: kexec_file_load() and the older kexec_load(). With -a (the default), it tries kexec_file_load() first and falls back to kexec_load() when the syscall is not implemented (ENOSYS) or the kernel does not have a loader for the image format. With -s, it uses kexec_file_load() only with no fallback.

When the kernel returns -EINVAL it means something went wrong while loading the image, not that the syscall is missing or the image format is unrecognized. kexec-tools should not fall back to the older syscall in that case. However, some kernel probe functions currently return -EINVAL when the image header does not match, instead of returning -ENOEXEC. Keeping EINVAL in the fallback set to accommodate these probes has the side effect of also hiding genuine loading errors like an oversized command line.

Quoted 7 lines

kexec-tools should only fall back when kexec_file_load() is not implemented or does not have a matching loader -- not when something goes wrong during load.

The fix on the kexec-tools side is to remove EINVAL from the fallback set, but that requires the kernel to be clean first -- probe functions must return -ENOEXEC when they do not recognize an image format, not -EINVAL.

Okay, the idea makes sense. Though I am curious if in practice there are images that kexec_file_load() can't load but kexec-tools can.

But then I went and looked at the code. The only caller of the probe functions I can see is kexec_image_probe_default(). Looking at its code:

  int kexec_image_probe_default(struct kimage *image, void *buf,
  			      unsigned long buf_len)
  {
  	const struct kexec_file_ops * const *fops;
  	int ret = -ENOEXEC;
  
  	for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
  		ret = (*fops)->probe(buf, buf_len);
  		if (!ret) {
  			image->fops = *fops;
  			return ret;
  		}
  	}
  
  	return ret;
  }

It defaults ret to -ENOEXEC, which makes sense, but then lets it be over-written by each loader. So in practice, it returns what the _last_ loader returned. _This_ makes no sense. Why should the last loader be any special?

From reading the code, I think what the author of this wanted to do is to try all loaders, and return -ENOEXEC if none succeeded. But the code of course strays from that and ends up returning the last loader's value.

So how about the below diff instead? (** only compile tested **)

--- 8< ---

kernel/kexec_file.c +4 -5
index 2bfbb2d144e6..cfb2b8cd5679 100644
@@ -67,17 +67,16 @@ int kexec_image_probe_default(struct kimage *image, void *buf,
 			      unsigned long buf_len)
 {
 	const struct kexec_file_ops * const *fops;
-	int ret = -ENOEXEC;
 
 	for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
-		ret = (*fops)->probe(buf, buf_len);
-		if (!ret) {
+		if (!(*fops)->probe(buf, buf_len)) {
 			image->fops = *fops;
-			return ret;
+			return 0;
 		}
 	}
 
-	return ret;
+	/* No loader found. */
+	return -ENOEXEC;
 }
 
 static void *kexec_image_load_default(struct kimage *image)
Signature · 2 lines
Regards,
Pratyush Yadav
#6 lore raw

Hi Pratyush,

Quoted 82 lines · Pratyush Yadav
Quoted 49 lines · Mukesh Pilaniya

Hi Pratyush,

Quoted 17 lines · Pratyush Yadav
Quoted 6 lines · Mukesh Pilaniya

Several kexec_file_load() image probe functions return -EINVAL when they do not recognize the image format. A probe function that rejects an image should return -ENOEXEC to indicate that the image is not a recognized executable format. -EINVAL implies a problem with the syscall parameters, not with image recognition.

Quoted 8 lines

kexec_image_probe_default() iterates through registered loaders and returns the last probe's error code to the caller. That error propagates as the kexec_file_load() return value to userspace. Returning -EINVAL from a probe when no loader matches is semantically incorrect and misleads userspace about the nature of the failure.

Return -ENOEXEC from all probe functions and their helpers when the image format is not recognized.

Quoted 7 lines

Sounds fine in principle but can you please also share what the real problem you face is and how changing these return codes helps? These error codes are uAPI and while we _can_ change them as long as we don't break something, there should be a clear motivation for doing so.

[...]

While debugging a misleading error on s390x where kexec -s reported "syscall kexec_file_load not available" instead of the actual EINVAL from a kernel command line that exceeded the architecture limit, we traced the problem to the kexec-tools userspace utility treating EINVAL the same as ENOSYS and ENOEXEC -- as a signal to silently fall back to kexec_load().

kexec-tools supports two syscalls: kexec_file_load() and the older kexec_load(). With -a (the default), it tries kexec_file_load() first and falls back to kexec_load() when the syscall is not implemented (ENOSYS) or the kernel does not have a loader for the image format. With -s, it uses kexec_file_load() only with no fallback.

When the kernel returns -EINVAL it means something went wrong while loading the image, not that the syscall is missing or the image format is unrecognized. kexec-tools should not fall back to the older syscall in that case. However, some kernel probe functions currently return -EINVAL when the image header does not match, instead of returning -ENOEXEC. Keeping EINVAL in the fallback set to accommodate these probes has the side effect of also hiding genuine loading errors like an oversized command line.

Quoted 7 lines

kexec-tools should only fall back when kexec_file_load() is not implemented or does not have a matching loader -- not when something goes wrong during load.

The fix on the kexec-tools side is to remove EINVAL from the fallback set, but that requires the kernel to be clean first -- probe functions must return -ENOEXEC when they do not recognize an image format, not -EINVAL.

Okay, the idea makes sense. Though I am curious if in practice there are images that kexec_file_load() can't load but kexec-tools can.

But then I went and looked at the code. The only caller of the probe functions I can see is kexec_image_probe_default(). Looking at its code:

  int kexec_image_probe_default(struct kimage *image, void *buf,
  			      unsigned long buf_len)
  {
  	const struct kexec_file_ops * const *fops;
  	int ret = -ENOEXEC;
  
  	for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
  		ret = (*fops)->probe(buf, buf_len);
  		if (!ret) {
  			image->fops = *fops;
  			return ret;
  		}
  	}
  
  	return ret;
  }
Quoted 4 lines

It defaults ret to -ENOEXEC, which makes sense, but then lets it be over-written by each loader. So in practice, it returns what the _last_ loader returned. _This_ makes no sense. Why should the last loader be any special?

Right, the last loader overwriting ret, the commit message describes this as well. It makes no sense for the return value to depend on whichever loader happens to be last in the array.

Quoted 5 lines

From reading the code, I think what the author of this wanted to do is to try all loaders, and return -ENOEXEC if none succeeded. But the code of course strays from that and ends up returning the last loader's value.IMHO the function should only try the next loader when a probe returns

-ENOEXEC, not for any other error like -ENOMEM, which is a real failure and should be propagated immediately, not silently discarded.

Quoted 18 lines

So how about the below diff instead? (** only compile tested **)

--- 8< ---

kernel/kexec_file.c +1 -3
index 2bfbb2d144e6..cfb2b8cd5679 100644
@@ -67,17 +67,16 @@ int kexec_image_probe_default(struct kimage *image, void *buf,
 			      unsigned long buf_len)
 {
 	const struct kexec_file_ops * const *fops;
-	int ret = -ENOEXEC;
 
 	for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
-		ret = (*fops)->probe(buf, buf_len);
-		if (!ret) {
+		if (!(*fops)->probe(buf, buf_len)) {

kernel/kexec_file.c int kexec_image_probe_default(struct kimage *image, void *buf, · line 80

Quoted 10 lines
kernel/kexec_file.c +3 -2
 			image->fops = *fops;
-			return ret;
+			return 0;
 		}
 	}
 
-	return ret;
+	/* No loader found. */
+	return -ENOEXEC;
 }

Nice catch, Pratyush but this discards all non-zero return values from probe functions, which means real errors get swallowed. For example, kexec_elf_probe() can return -ENOMEM when kzalloc() fails in elf_read_phdrs(). With this diff, that -ENOMEM becomes -ENOEXEC, which tells userspace "no loader found" when the actual problem was a memory allocation failure.

kernel/kexec_file.c int kexec_image_probe_default(struct kimage *image, void *buf, · line 82

Quoted 3 lines
kernel/kexec_file.c +0 -0
 static void *kexec_image_load_default(struct kimage *image)
 

How about the following instead? It keeps the probe return value and distinguishes -ENOEXEC (format not recognized, try next loader) from any other error (real failure, propagate immediately):


kernel/kexec_file.c +0 -0
index 59fb9d71e9d8..ceb5c97cedac 100644
@@ -68,17 +68,19 @@ int kexec_image_probe_default(struct kimage *image,

void *buf,
unsigned long buf_len)
{
const struct kexec_file_ops * const *fops;
- int ret = -ENOEXEC;
+ int ret;

for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
ret = (*fops)->probe(buf, buf_len);
- if (!ret) {
+ if (ret == 0) {
image->fops = *fops;
- return ret;
+ return 0;
}
+ if (ret != -ENOEXEC)
+ return ret;
}

- return ret;
+ return -ENOEXEC;
}

static void *kexec_image_load_default(struct kimage *image)

This requires probe functions to return -ENOEXEC from fixes in patch 1. I will add this as a separate patch on top in v2.

Does this looks good to you ?

--
Regards,
Mukesh Pilaniya

maintainer
#7 lore raw
Quoted 4 lines · Mukesh Pilaniya

Hi Pratyush,

Quoted 1 line · Pratyush Yadav

On Fri, Aug 14 2026, Mukesh Pilaniya wrote:

[...]

Quoted 24 lines
Quoted 24 lines

So how about the below diff instead? (** only compile tested **)

--- 8< ---

kernel/kexec_file.c +2 -4
index 2bfbb2d144e6..cfb2b8cd5679 100644
@@ -67,17 +67,16 @@ int kexec_image_probe_default(struct kimage *image, void *buf,
 			      unsigned long buf_len)
 {
 	const struct kexec_file_ops * const *fops;
-	int ret = -ENOEXEC;
 
 	for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
-		ret = (*fops)->probe(buf, buf_len);
-		if (!ret) {
+		if (!(*fops)->probe(buf, buf_len)) {
 			image->fops = *fops;
-			return ret;
+			return 0;
 		}
 	}
 
Quoted 10 lines

kernel/kexec_file.c int kexec_image_probe_default(struct kimage *image, void *buf, · line 80

Quoted 4 lines
kernel/kexec_file.c +2 -1
-	return ret;
+	/* No loader found. */
+	return -ENOEXEC;
 }

Nice catch, Pratyush but this discards all non-zero return values from probe functions, which means real errors get swallowed. For example, kexec_elf_probe() can return -ENOMEM when kzalloc() fails in elf_read_phdrs(). With this diff, that -ENOMEM becomes -ENOEXEC, which tells userspace "no loader found" when the actual problem was a memory allocation failure.

Right, good point.

Quoted 39 lines

kernel/kexec_file.c int kexec_image_probe_default(struct kimage *image, void *buf, · line 82

Quoted 4 lines
kernel/kexec_file.c +0 -0
 static void *kexec_image_load_default(struct kimage *image)
 

How about the following instead? It keeps the probe return value and distinguishes -ENOEXEC (format not recognized, try next loader) from any other error (real failure, propagate immediately):


kernel/kexec_file.c +6 -4
index 59fb9d71e9d8..ceb5c97cedac 100644
@@ -68,17 +68,19 @@ int kexec_image_probe_default(struct kimage *image,
 void *buf,
 			      unsigned long buf_len)
 {
 	const struct kexec_file_ops * const *fops;
-	int ret = -ENOEXEC;
+	int ret;
 
 	for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
 		ret = (*fops)->probe(buf, buf_len);
-		if (!ret) {
+		if (ret == 0) {
 			image->fops = *fops;
-			return ret;
+			return 0;
 		}
+		if (ret != -ENOEXEC)
+			return ret;
 	}
 
-	return ret;
+	return -ENOEXEC;
 }
 

static void *kexec_image_load_default(struct kimage *image)

Quoted 4 lines

This requires probe functions to return -ENOEXEC from fixes in patch 1. I will add this as a separate patch on top in v2.

Does this looks good to you ?

LGTM. The only thing I'd change is to perhaps move the declaration of ret inside the loop so it can never be used outside it, since ret is only useful in the loop in this function.

Signature · 2 lines
Regards,
Pratyush Yadav
#8 lore raw
In reply to Mukesh Pilaniya
This patch was applied to riscv/linux.git (fixes) by Mike Rapoport (Microsoft) <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (fixes)
by Mike Rapoport (Microsoft) <[email protected]>:

Quoted 6 lines · On Thu, 13 Aug 2026 12:36:29 +0530 you wrote:

Several kexec_file_load() image probe functions return -EINVAL when they do not recognize the image format. A probe function that rejects an image should return -ENOEXEC to indicate that the image is not a recognized executable format. -EINVAL implies a problem with the syscall parameters, not with image recognition.

Quoted 7 lines

kexec_image_probe_default() iterates through registered loaders and returns the last probe's error code to the caller. That error propagates as the kexec_file_load() return value to userspace. Returning -EINVAL from a probe when no loader matches is semantically incorrect and misleads userspace about the nature of the failure.

[...]

Here is the summary with links:
- kexec: return -ENOEXEC from image probe functions on mismatch

    https://git.kernel.org/riscv/c/2792b4b15f14

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html