Skip to contentSkip to conversation

Enable UKI image load on x86_64

PATCH In review Pingfan Liu 3 participants 7 messages Nov 5, 2024 – Dec 2, 2024

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

cover Enable UKI image load on x86_64

The correct kernel fd instead of UKI image fd should be passed to the syscall. On arm64, it is not a problem, but on x86_64, it is.(see commit log in 2/3)

This series aims to address the above problem.

Cc: Simon Horman <[email protected]>
Cc: Philipp Rudo <[email protected]>
To: [email protected]

Pingfan Liu (3):
UKI: Split out the routine to create temporary fd
kexec: Create a temporary file to hold .linux section in uki_probe()
x86_64: Support UKI image format

 kexec/arch/arm64/kexec-vmlinuz-arm64.c |  2 +
 kexec/arch/x86_64/kexec-x86_64.c       |  1 +
 kexec/kexec-uki.c                      | 67 +++++++++++++++++---------
 3 files changed, 47 insertions(+), 23 deletions(-)
Signature · 7 lines
2.41.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#1 lore raw

PATCH 1/3 UKI: Split out the routine to create temporary fd

The process of creating temporary fd for initrd can be reused. Hence splitting it out for later use.

To: [email protected]

---
 kexec/kexec-uki.c | 53 +++++++++++++++++++++++++++--------------------
 1 file changed, 30 insertions(+), 23 deletions(-)
kexec/kexec-uki.c +30 -23
index 210ebb6..8a7d349 100644
@@ -35,6 +35,34 @@ static int get_pehdr_offset(const char *buf)
 	return pe_hdr_offset;
 }
 
+static int create_tmpfd(const char *template, char *buf, int buf_sz, int *tmpfd)
+{
+	char *fname;
+	int fd = -1;
+
+	if (!(fname = strdup(template))) {
+		dbgprintf("%s: Can't duplicate strings\n", __func__);
+		return -1;
+	}
+
+	if ((fd = mkstemp(fname)) < 0) {
+		dbgprintf("%s: Can't open file %s\n", __func__,	fname);
+		return -1;
+	}
+
+	if (write(fd, buf, buf_sz) != buf_sz) {
+		dbgprintf("%s: Can't write the compressed file %s\n",
+				__func__, fname);
+		close(fd);
+		return -1;
+	} else {
+		*tmpfd = open(fname, O_RDONLY);
+		close(fd);
+	}
+
+	return 0;
+}
+
 int uki_image_probe(const char *file_buf, off_t buf_sz)
 {
 	struct pe_hdr *pe_hdr;
@@ -42,8 +70,6 @@ int uki_image_probe(const char *file_buf, off_t buf_sz)
 	struct section_header *sect_hdr;
 	int pe_hdr_offset, section_nr, linux_sz = -1;
 	char *pe_part_buf, *linux_src;
-	char *initrd_fname = NULL;
-	int initrd_fd = -1;
 
 	pe_hdr_offset = get_pehdr_offset(file_buf);
 	pe_part_buf = (char *)file_buf + pe_hdr_offset;
@@ -63,28 +89,9 @@ int uki_image_probe(const char *file_buf, off_t buf_sz)
 			linux_sz = sect_hdr->raw_data_size;
 
 		} else if (!strcmp(sect_hdr->name, UKI_INITRD_SECTION)) {
-			if (!(initrd_fname = strdup(FILENAME_UKI_INITRD))) {
-				dbgprintf("%s: Can't duplicate strings\n", __func__);
-				goto next;
-			}
-
-			if ((initrd_fd = mkstemp(initrd_fname)) < 0) {
-				dbgprintf("%s: Can't open file %s\n", __func__,	initrd_fname);
-				goto next;
-			}
-
-			if (write(initrd_fd, (char *)file_buf + sect_hdr->data_addr,
-					sect_hdr->raw_data_size) != sect_hdr->raw_data_size) {
-				dbgprintf("%s: Can't write the compressed file %s\n",
-						__func__, initrd_fname);
-				close(initrd_fd);
-				goto next;
-			} else {
-				implicit_initrd_fd = open(initrd_fname, O_RDONLY);
-				close(initrd_fd);
-			}
+			create_tmpfd(FILENAME_UKI_INITRD, (char *)file_buf + sect_hdr->data_addr,
+					sect_hdr->raw_data_size, &implicit_initrd_fd);
 		}
-next:
 		sect_hdr++;
 	}
 
Signature · 7 lines
2.41.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#2 lore raw

PATCH 2/3 kexec: Create a temporary file to hold .linux section in uki_probe()

In reply to Pingfan Liu

**Problem**
In the arm64 case, a temporary file for kernel is created in pez_arm64_probe() and passed down to info.kernel_fd later.

However in the x86_64 case, nobody passes a temporary fd to info.kernel_fd, so finally the fd of the UKI image is passed to syscall. That is not the expected behavior.

**Situation**
The current framework of kexec-tools passes kernel fd to syscall. Meanwhile info.kernel_fd presents each probe with an opportunity to overwhelm the existing kernel fd.

**Solution**
In the UKI probe, a temporary file is created to hold the .linux section. Later, if no other probe methods set info.kernel_fd, it will be set in the UKI load method. This way, the correct fd can be passed to the syscall.

To: [email protected]

---
 kexec/arch/arm64/kexec-vmlinuz-arm64.c |  2 ++
 kexec/kexec-uki.c                      | 14 ++++++++++++++
 2 files changed, 16 insertions(+)
kexec/arch/arm64/kexec-vmlinuz-arm64.c +2 -0
index e291a34..e558296 100644
@@ -104,6 +104,8 @@ int pez_arm64_load(int argc, char **argv, const char *buf, off_t len,
 		off_t nread;
 		int fd;
 
+		if (info->kernel_fd > 0)
+			close(info->kernel_fd);
 		info->kernel_fd = kernel_fd;
 		fd = dup(kernel_fd);
 		if (fd < 0) {
kexec/kexec-uki.c +14 -0
index 8a7d349..235f5a1 100644
@@ -15,8 +15,10 @@
 #define UKI_DTB_SECTION ".dtb"
 
 #define FILENAME_UKI_INITRD          "/tmp/InitrdXXXXXX"
+#define FILENAME_UKI_KERNEL          "/tmp/KernelXXXXXX"
 
 static int embeded_linux_format_index = -1;
+static int kernel_fd = -1;
 
 /*
  * Return -1 if not PE, else offset of the PE header
@@ -98,6 +100,15 @@ int uki_image_probe(const char *file_buf, off_t buf_sz)
 	if (linux_sz == -1) {
 		printf("ERR: can not find .linux section\n");
 		return -1;
+	} else {
+		int res;
+
+		res = create_tmpfd(FILENAME_UKI_KERNEL, linux_src, linux_sz,
+					&kernel_fd);
+		if (res < 0) {
+			printf("ERR: can not create tmp file to hold .linux section\n");
+			return -1;
+		}
 	}
 	/*
 	 * After stripping the UKI coat, the real kernel format can be handled now.
@@ -120,6 +131,9 @@ int uki_image_probe(const char *file_buf, off_t buf_sz)
 int uki_image_load(int argc, char **argv, const char *buf, off_t len,
 			struct kexec_info *info)
 {
+	/* In probe() chain, if nobody sets info->kernel_fd, set it now */
+	if (info->kernel_fd == -1)
+		info->kernel_fd = kernel_fd;
 	return file_type[embeded_linux_format_index].load(argc, argv, buf, len, info);
 }
 
Signature · 7 lines
2.41.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#3 lore raw

PATCH 3/3 x86_64: Support UKI image format

In reply to Pingfan Liu

To: [email protected]

---
 kexec/arch/x86_64/kexec-x86_64.c | 1 +
 1 file changed, 1 insertion(+)
kexec/arch/x86_64/kexec-x86_64.c +1 -0
index 7b2453f..f7f59f6 100644
@@ -43,6 +43,7 @@ struct file_type file_type[] = {
 	{ "bzImage", bzImage_probe, bzImage_load, bzImage_usage },
 	{ "beoboot-x86", beoboot_probe, beoboot_load, beoboot_usage },
 	{ "nbi-x86", nbi_probe, nbi_load, nbi_usage },
+	{"uki", uki_image_probe, uki_image_load, uki_image_usage},
 };
 int file_types = sizeof(file_type)/sizeof(file_type[0]);
 
Signature · 7 lines
2.41.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Acked-by on the v1 series
#4 lore raw
In reply to Pingfan Liu

Hi Pingfan,

Quoted 7 lines · Pingfan Liu

The correct kernel fd instead of UKI image fd should be passed to the syscall. On arm64, it is not a problem, but on x86_64, it is.(see commit log in 2/3)

This series aims to address the above problem.

Quoted 8 lines

Cc: Simon Horman <[email protected]>
Cc: Philipp Rudo <[email protected]>
To: [email protected]

Pingfan Liu (3):
UKI: Split out the routine to create temporary fd
kexec: Create a temporary file to hold .linux section in uki_probe()
x86_64: Support UKI image format

After Doing a test based on this series, I noticed the uki load will
not use the uki internal .cmdline for kernel command line parameters, it requires people to specify cmdline with kexec. I think this is helpful for kdump to use. But for general use it would be better to do something like:

by default use the UKI internal cmdline
If one specify --command-line or --reuse-cmdline then just use the user provided cmdline and ignore the UKI internal cmdline.

Anyway this can be improved in the future as an appending patch, the functionality works for me.

Quoted 15 lines
 kexec/arch/arm64/kexec-vmlinuz-arm64.c |  2 +
 kexec/arch/x86_64/kexec-x86_64.c       |  1 +
 kexec/kexec-uki.c                      | 67 +++++++++++++++++---------
 3 files changed, 47 insertions(+), 23 deletions(-)

--
2.41.0

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#5 lore raw
Quoted 25 lines · Dave Young

Hi Pingfan,

Quoted 7 lines · Pingfan Liu

The correct kernel fd instead of UKI image fd should be passed to the syscall. On arm64, it is not a problem, but on x86_64, it is.(see commit log in 2/3)

This series aims to address the above problem.

Quoted 8 lines

Cc: Simon Horman <[email protected]>
Cc: Philipp Rudo <[email protected]>
To: [email protected]

Pingfan Liu (3):
UKI: Split out the routine to create temporary fd
kexec: Create a temporary file to hold .linux section in uki_probe()
x86_64: Support UKI image format

After Doing a test based on this series, I noticed the uki load will
not use the uki internal .cmdline for kernel command line parameters, it requires people to specify cmdline with kexec. I think this is helpful for kdump to use. But for general use it would be better to do something like:

Quoted 8 lines

by default use the UKI internal cmdline
If one specify --command-line or --reuse-cmdline then just use the user provided cmdline and ignore the UKI internal cmdline.

Anyway this can be improved in the future as an appending patch, the functionality works for me.

Thanks,

I will plan to apply this after v2.0.30 has been released.

#6 lore raw
Quoted 30 lines · Simon Horman
Quoted 29 lines · Dave Young

Hi Pingfan,

Quoted 7 lines · Pingfan Liu

The correct kernel fd instead of UKI image fd should be passed to the syscall. On arm64, it is not a problem, but on x86_64, it is.(see commit log in 2/3)

This series aims to address the above problem.

Quoted 8 lines

Cc: Simon Horman <[email protected]>
Cc: Philipp Rudo <[email protected]>
To: [email protected]

Pingfan Liu (3):
UKI: Split out the routine to create temporary fd
kexec: Create a temporary file to hold .linux section in uki_probe()
x86_64: Support UKI image format

After Doing a test based on this series, I noticed the uki load will
not use the uki internal .cmdline for kernel command line parameters, it requires people to specify cmdline with kexec. I think this is helpful for kdump to use. But for general use it would be better to do something like:

by default use the UKI internal cmdline
If one specify --command-line or --reuse-cmdline then just use the user provided cmdline and ignore the UKI internal cmdline.

Quoted 8 lines
Quoted 4 lines

Anyway this can be improved in the future as an appending patch, the functionality works for me.

Thanks,

I will plan to apply this after v2.0.30 has been released.

Thanks everyone, applied.