* [PATCH 1/3] UKI: Split out the routine to create temporary fd
2024-11-05 3:57 [PATCH 0/3] Enable UKI image load on x86_64 Pingfan Liu
@ 2024-11-05 3:57 ` Pingfan Liu
2024-11-05 3:57 ` [PATCH 2/3] kexec: Create a temporary file to hold .linux section in uki_probe() Pingfan Liu
` (2 subsequent siblings)
3 siblings, 0 replies; 7+ messages in thread
From: Pingfan Liu @ 2024-11-05 3:57 UTC (permalink / raw)
To: kexec; +Cc: Pingfan Liu, Simon Horman, Philipp Rudo
The process of creating temporary fd for initrd can be reused. Hence
splitting it out for later use.
Signed-off-by: Pingfan Liu <piliu@redhat.com>
Cc: Simon Horman <horms@kernel.org>
To: kexec@lists.infradead.org
---
kexec/kexec-uki.c | 53 +++++++++++++++++++++++++++--------------------
1 file changed, 30 insertions(+), 23 deletions(-)
diff --git a/kexec/kexec-uki.c b/kexec/kexec-uki.c
index 210ebb6..8a7d349 100644
--- a/kexec/kexec-uki.c
+++ b/kexec/kexec-uki.c
@@ -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++;
}
--
2.41.0
_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 2/3] kexec: Create a temporary file to hold .linux section in uki_probe()
2024-11-05 3:57 [PATCH 0/3] Enable UKI image load on x86_64 Pingfan Liu
2024-11-05 3:57 ` [PATCH 1/3] UKI: Split out the routine to create temporary fd Pingfan Liu
@ 2024-11-05 3:57 ` Pingfan Liu
2024-11-05 3:57 ` [PATCH 3/3] x86_64: Support UKI image format Pingfan Liu
2024-11-22 2:12 ` [PATCH 0/3] Enable UKI image load on x86_64 Dave Young
3 siblings, 0 replies; 7+ messages in thread
From: Pingfan Liu @ 2024-11-05 3:57 UTC (permalink / raw)
To: kexec; +Cc: Pingfan Liu, Simon Horman, Philipp Rudo
**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.
Signed-off-by: Pingfan Liu <piliu@redhat.com>
Cc: Simon Horman <horms@kernel.org>
To: kexec@lists.infradead.org
---
kexec/arch/arm64/kexec-vmlinuz-arm64.c | 2 ++
kexec/kexec-uki.c | 14 ++++++++++++++
2 files changed, 16 insertions(+)
diff --git a/kexec/arch/arm64/kexec-vmlinuz-arm64.c b/kexec/arch/arm64/kexec-vmlinuz-arm64.c
index e291a34..e558296 100644
--- a/kexec/arch/arm64/kexec-vmlinuz-arm64.c
+++ b/kexec/arch/arm64/kexec-vmlinuz-arm64.c
@@ -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) {
diff --git a/kexec/kexec-uki.c b/kexec/kexec-uki.c
index 8a7d349..235f5a1 100644
--- a/kexec/kexec-uki.c
+++ b/kexec/kexec-uki.c
@@ -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);
}
--
2.41.0
_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH 3/3] x86_64: Support UKI image format
2024-11-05 3:57 [PATCH 0/3] Enable UKI image load on x86_64 Pingfan Liu
2024-11-05 3:57 ` [PATCH 1/3] UKI: Split out the routine to create temporary fd Pingfan Liu
2024-11-05 3:57 ` [PATCH 2/3] kexec: Create a temporary file to hold .linux section in uki_probe() Pingfan Liu
@ 2024-11-05 3:57 ` Pingfan Liu
2024-11-22 2:12 ` [PATCH 0/3] Enable UKI image load on x86_64 Dave Young
3 siblings, 0 replies; 7+ messages in thread
From: Pingfan Liu @ 2024-11-05 3:57 UTC (permalink / raw)
To: kexec; +Cc: Pingfan Liu, Simon Horman, Philipp Rudo
Signed-off-by: Pingfan Liu <piliu@redhat.com>
Cc: Simon Horman <horms@kernel.org>
To: kexec@lists.infradead.org
---
kexec/arch/x86_64/kexec-x86_64.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/kexec/arch/x86_64/kexec-x86_64.c b/kexec/arch/x86_64/kexec-x86_64.c
index 7b2453f..f7f59f6 100644
--- a/kexec/arch/x86_64/kexec-x86_64.c
+++ b/kexec/arch/x86_64/kexec-x86_64.c
@@ -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]);
--
2.41.0
_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 0/3] Enable UKI image load on x86_64
2024-11-05 3:57 [PATCH 0/3] Enable UKI image load on x86_64 Pingfan Liu
` (2 preceding siblings ...)
2024-11-05 3:57 ` [PATCH 3/3] x86_64: Support UKI image format Pingfan Liu
@ 2024-11-22 2:12 ` Dave Young
2024-12-02 13:06 ` Simon Horman
3 siblings, 1 reply; 7+ messages in thread
From: Dave Young @ 2024-11-22 2:12 UTC (permalink / raw)
To: Pingfan Liu; +Cc: kexec, Simon Horman, Philipp Rudo
Hi Pingfan,
On Tue, 5 Nov 2024 at 11:57, Pingfan Liu <piliu@redhat.com> wrote:
>
> 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 <horms@kernel.org>
> Cc: Philipp Rudo <prudo@redhat.com>
> To: kexec@lists.infradead.org
>
> 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.
Acked-by: Dave Young <dyoung@redhat.com>
>
> 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
> kexec@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/kexec
>
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH 0/3] Enable UKI image load on x86_64
2024-11-22 2:12 ` [PATCH 0/3] Enable UKI image load on x86_64 Dave Young
@ 2024-12-02 13:06 ` Simon Horman
2024-12-02 13:34 ` Simon Horman
0 siblings, 1 reply; 7+ messages in thread
From: Simon Horman @ 2024-12-02 13:06 UTC (permalink / raw)
To: Dave Young; +Cc: Pingfan Liu, kexec, Philipp Rudo
On Fri, Nov 22, 2024 at 10:12:35AM +0800, Dave Young wrote:
> Hi Pingfan,
>
> On Tue, 5 Nov 2024 at 11:57, Pingfan Liu <piliu@redhat.com> wrote:
> >
> > 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 <horms@kernel.org>
> > Cc: Philipp Rudo <prudo@redhat.com>
> > To: kexec@lists.infradead.org
> >
> > 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.
>
> Acked-by: Dave Young <dyoung@redhat.com>
Thanks,
I will plan to apply this after v2.0.30 has been released.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 0/3] Enable UKI image load on x86_64
2024-12-02 13:06 ` Simon Horman
@ 2024-12-02 13:34 ` Simon Horman
0 siblings, 0 replies; 7+ messages in thread
From: Simon Horman @ 2024-12-02 13:34 UTC (permalink / raw)
To: Dave Young; +Cc: Pingfan Liu, kexec, Philipp Rudo
On Mon, Dec 02, 2024 at 01:06:23PM +0000, Simon Horman wrote:
> On Fri, Nov 22, 2024 at 10:12:35AM +0800, Dave Young wrote:
> > Hi Pingfan,
> >
> > On Tue, 5 Nov 2024 at 11:57, Pingfan Liu <piliu@redhat.com> wrote:
> > >
> > > 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 <horms@kernel.org>
> > > Cc: Philipp Rudo <prudo@redhat.com>
> > > To: kexec@lists.infradead.org
> > >
> > > 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.
> >
> > Acked-by: Dave Young <dyoung@redhat.com>
>
> Thanks,
>
> I will plan to apply this after v2.0.30 has been released.
Thanks everyone, applied.
^ permalink raw reply [flat|nested] 7+ messages in thread