Skip to contentSkip to conversation

xen/arm: Couple of bug fixes when decompressing kernels

PATCH Superseded Julien Grall 2 participants 10 messages Apr 2, 2021 – Apr 7, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover xen/arm: Couple of bug fixes when decompressing kernels

From: Julien Grall <[email protected]>

Hi all,

The main goal of this series is to address the bug report [1]. It is not possible

While testing the series, I also noticed that it is not possible to re-use the same compressed kernel for multiple domains as the module will be overwritten during the first decompression.

I am still a bit undecided how to fix it. We can either create a new module for the uncompressed kernel and link with the compressed kernel. Or we could decompress everytime.

One inconvenience to kernel to only decompress once is we have to keep it until all the domains have booted. This may means a lot of memory to be wasted just for keeping the uncompressed kernel (one my setup, it takes ~3 times more space).

Any opinions on how to approach it?

Cheers,

[1] https://lists.xenproject.org/archives/html/xen-users/2021-02/msg00027.html

Julien Grall (2):
xen/arm: kernel: Propagate the error if we fail to decompress the
kernel
xen/gunzip: Allow perform_gunzip() to be called multiple times

 xen/arch/arm/kernel.c | 8 +++++++-
 xen/common/gunzip.c   | 5 +++++
 xen/common/inflate.c  | 6 ++++++
 3 files changed, 18 insertions(+), 1 deletion(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/2 xen/arm: kernel: Propagate the error if we fail to decompress the kernel

From: Julien Grall <[email protected]>

Currently, we are ignoring any error from perform_gunzip() and replacing the compressed kernel with the "uncompressed" kernel.

If there is a gzip failure, then it means that the output buffer may contain garbagge. So it can result to various sort of behavior that may be difficult to root cause.

In case of failure, free the output buffer and propagate the error. We also need to adjust the return check for kernel_compress() as perform_gunzip() may return a positive value.

Take the opportunity to adjust the code style for the check.

---
 xen/arch/arm/kernel.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
xen/arch/arm/kernel.c +7 -1
index ab78689ed2a6..f6b60ab77355 100644
@@ -292,6 +292,12 @@ static __init int kernel_decompress(struct bootmodule *mod)
     iounmap(input);
     vunmap(output);
 
+    if ( rc )
+    {
+        free_domheap_pages(pages);
+        return rc;
+    }
+
     mod->start = page_to_maddr(pages);
     mod->size = output_size;
 
@@ -503,7 +509,7 @@ int __init kernel_probe(struct kernel_info *info,
 
     /* if it is a gzip'ed image, 32bit or 64bit, uncompress it */
     rc = kernel_decompress(mod);
-    if (rc < 0 && rc != -EINVAL)
+    if ( rc && rc != -EINVAL )
         return rc;
 
 #ifdef CONFIG_ARM_64
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH 1/2] xen/arm: kernel: Propagate the error if we fail to decompress the kernel

Quoted 25 lines · Julien Grall

From: Julien Grall <[email protected]>

Currently, we are ignoring any error from perform_gunzip() and replacing the compressed kernel with the "uncompressed" kernel.

If there is a gzip failure, then it means that the output buffer may contain garbagge. So it can result to various sort of behavior that may be difficult to root cause.

In case of failure, free the output buffer and propagate the error. We also need to adjust the return check for kernel_compress() as perform_gunzip() may return a positive value.

Take the opportunity to adjust the code style for the check.

---
  xen/arch/arm/kernel.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
xen/arch/arm/kernel.c +0 -0
index ab78689ed2a6..f6b60ab77355 100644

xen/arch/arm/kernel.c static __init int kernel_decompress(struct bootmodule *mod) · line 297

Quoted 6 lines
xen/arch/arm/kernel.c +3 -0
@@ -292,6 +292,12 @@ static __init int kernel_decompress(struct bootmodule *mod)
      iounmap(input);
      vunmap(output);
 
+    if ( rc )
+    {
+        free_domheap_pages(pages);

This breaks the build. I am not sure how I managed to test it before... I will send a v2.

Cheers,

Signature · 1 line
Julien Grall
#3 lore raw

PATCH 2/2 xen/gunzip: Allow perform_gunzip() to be called multiple times

In reply to Julien Grall

From: Julien Grall <[email protected]>

Currently perform_gunzip() can only be called once because the the internal state (e.g allocate) is not fully re-initialized.

This works fine if you are only booting dom0. But this will break when booting multiple using the dom0less that uses compressed kernel images.

This can be resolved by re-initializing bytes_out, malloc_ptr, malloc_count every time perform_gunzip() is called.

Note the latter is only re-initialized for hardening purpose as there is no guarantee that every malloc() are followed by free() (It should in theory!).

Take the opportunity to check the return of alloc_heap_pages() to return an error rather than dereferencing a NULL pointer later on failure.

---
 xen/common/gunzip.c  | 5 +++++
 xen/common/inflate.c | 6 ++++++
 2 files changed, 11 insertions(+)
xen/common/gunzip.c +5 -0
index db4efcd34b77..425d64e904d8 100644
@@ -114,11 +114,16 @@ __init int perform_gunzip(char *output, char *image, unsigned long image_len)
     window = (unsigned char *)output;
 
     free_mem_ptr = (unsigned long)alloc_xenheap_pages(HEAPORDER, 0);
+    if ( !free_mem_ptr )
+        return -ENOMEM;
+
     free_mem_end_ptr = free_mem_ptr + (PAGE_SIZE << HEAPORDER);
+    init_allocator();
 
     inbuf = (unsigned char *)image;
     insize = image_len;
     inptr = 0;
+    bytes_out = 0;
 
     makecrc();
 
xen/common/inflate.c +6 -0
index f99c985d6135..d8c28a3e9593 100644
@@ -238,6 +238,12 @@ STATIC const ush mask_bits[] = {
 static unsigned long INITDATA malloc_ptr;
 static int INITDATA malloc_count;
 
+static void init_allocator(void)
+{
+    malloc_ptr = free_mem_ptr;
+    malloc_count = 0;
+}
+
 static void *INIT malloc(int size)
 {
     void *p;
Signature · 1 line
2.17.1
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] xen/gunzip: Allow perform_gunzip() to be called multiple times

Quoted 15 lines · Julien Grall

From: Julien Grall <[email protected]>

Currently perform_gunzip() can only be called once because the the internal state (e.g allocate) is not fully re-initialized.

This works fine if you are only booting dom0. But this will break when booting multiple using the dom0less that uses compressed kernel images.

This can be resolved by re-initializing bytes_out, malloc_ptr, malloc_count every time perform_gunzip() is called.

Note the latter is only re-initialized for hardening purpose as there is no guarantee that every malloc() are followed by free() (It should in theory!).

Quoted 5 lines

Take the opportunity to check the return of alloc_heap_pages() to return an error rather than dereferencing a NULL pointer later on failure.

#5 lore raw

Re: [PATCH 2/2] xen/gunzip: Allow perform_gunzip() to be called multiple times

In reply to Julien Grall
Quoted 5 lines · Julien Grall
xen/common/inflate.c +0 -0
@@ -238,6 +238,12 @@ STATIC const ush mask_bits[] = {
 static unsigned long INITDATA malloc_ptr;
 static int INITDATA malloc_count;

xen/common/inflate.c STATIC const ush mask_bits[] = { · line 249

Quoted 10 lines
xen/common/inflate.c +6 -0
 
+static void init_allocator(void)
+{
+    malloc_ptr = free_mem_ptr;
+    malloc_count = 0;
+}
+
 static void *INIT malloc(int size)
 {
     void *p;

I'm sorry for noticing this only now, but I'm afraid this may break the build in certain environments / configurations. Iirc clang is relatively likely to not inline functions in debug builds even when they're used just once. Yet when the new function doesn't end up getting inlined, it needs INIT added or else the cmd_obj_init_o checking would find a non-empty .text section. (If there's no actual build breakage anywhere, I can also address this in my to-be-re-based "gunzip: drop INIT{,DATA} and STATIC", which is intended to go in as soon as the tree is fully open again.)

Jan

#6 lore raw

Re: [PATCH 2/2] xen/gunzip: Allow perform_gunzip() to be called multiple times

Hi Jan,

Quoted 7 lines · Jan Beulich
Quoted 6 lines · Julien Grall
xen/common/inflate.c +0 -0
@@ -238,6 +238,12 @@ STATIC const ush mask_bits[] = {
  static unsigned long INITDATA malloc_ptr;
  static int INITDATA malloc_count;
 
Quoted 12 lines

xen/common/inflate.c STATIC const ush mask_bits[] = { · line 249

Quoted 9 lines
xen/common/inflate.c +6 -0
+static void init_allocator(void)
+{
+    malloc_ptr = free_mem_ptr;
+    malloc_count = 0;
+}
+
  static void *INIT malloc(int size)
  {
      void *p;

I'm sorry for noticing this only now, but I'm afraid this may break the build in certain environments / configurations.

You actually mentioned it on the original thread that reported the bug. But I forgot to add INIT. Sorry for that :(.

Quoted 8 lines

Iirc clang is
relatively likely to not inline functions in debug builds even when they're used just once. Yet when the new function doesn't end up getting inlined, it needs INIT added or else the cmd_obj_init_o checking would find a non-empty .text section. (If there's no actual build breakage anywhere, I can also address this in my to-be-re-based "gunzip: drop INIT{,DATA} and STATIC", which is intended to go in as soon as the tree is fully open again.)

The pipeline actually reported some failure. But I initially didn't notice them. I think we want to fix it because your rework, so I will send a patch.

Sorry for the breakage.

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw
In reply to Julien Grall
Quoted 6 lines · Julien Grall

From: Julien Grall <[email protected]>

Hi all,

The main goal of this series is to address the bug report [1]. It is not possible

...?

Quoted 8 lines

While testing the series, I also noticed that it is not possible to re-use the same compressed kernel for multiple domains as the module will be overwritten during the first decompression.

I am still a bit undecided how to fix it. We can either create a new module for the uncompressed kernel and link with the compressed kernel. Or we could decompress everytime.

Quoted 6 lines

One inconvenience to kernel to only decompress once is we have to keep it until all the domains have booted. This may means a lot of memory to be wasted just for keeping the uncompressed kernel (one my setup, it takes ~3 times more space).

Any opinions on how to approach it?

Well, it's not "until all the domains have booted", but until all the domains have had their kernel image placed in the designated piece of memory. So while for the time being multiple decompression may indeed be a reasonable approach, longer term one could populate all the domains' memory in two steps - first just the kernel space for all of them, then load the kernel(s), then populate the rest of the memory.

Jan

#8 lore raw

Hi Jan,

Quoted 9 lines · Jan Beulich
Quoted 6 lines · Julien Grall

From: Julien Grall <[email protected]>

Hi all,

The main goal of this series is to address the bug report [1]. It is not possible

...?

Urgh, I forgot to add the rest of the sentence :/. I was meant to say that it is not possible to decompress multiple kernels (doesn't need to be the same) today.

Quoted 14 lines
Quoted 14 lines

While testing the series, I also noticed that it is not possible to re-use the same compressed kernel for multiple domains as the module will be overwritten during the first decompression.

I am still a bit undecided how to fix it. We can either create a new module for the uncompressed kernel and link with the compressed kernel. Or we could decompress everytime.

One inconvenience to kernel to only decompress once is we have to keep it until all the domains have booted. This may means a lot of memory to be wasted just for keeping the uncompressed kernel (one my setup, it takes ~3 times more space).

Quoted 8 lines
Quoted 1 line

Any opinions on how to approach it?

Well, it's not "until all the domains have booted", but until all the domains have had their kernel image placed in the designated piece of memory. So while for the time being multiple decompression may indeed be a reasonable approach, longer term one could populate all the domains' memory in two steps - first just the kernel space for all of them, then load the kernel(s), then populate the rest of the memory.

Right, I am not sure this brings us to a better position. We would want to use superpage mapping for the guest memory as much as possible for performance reason.

So we may end up to populate the full memory of each guests. Therefore, I am not sure the split is that worth it.

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw
In reply to Julien Grall

Hi,

Quoted 22 lines · Julien Grall

From: Julien Grall <[email protected]>

Hi all,

The main goal of this series is to address the bug report [1]. It is not possible

While testing the series, I also noticed that it is not possible to re-use the same compressed kernel for multiple domains as the module will be overwritten during the first decompression.

I am still a bit undecided how to fix it. We can either create a new module for the uncompressed kernel and link with the compressed kernel. Or we could decompress everytime.

One inconvenience to kernel to only decompress once is we have to keep it until all the domains have booted. This may means a lot of memory to be wasted just for keeping the uncompressed kernel (one my setup, it takes ~3 times more space).

Any opinions on how to approach it?

Quoted 8 lines

Cheers,

[1] https://lists.xenproject.org/archives/html/xen-users/2021-02/msg00027.html

Julien Grall (2):
xen/arm: kernel: Propagate the error if we fail to decompress the
kernel
xen/gunzip: Allow perform_gunzip() to be called multiple times

I have committed the second patch as it is independent. The first patch still need some reviews.

Cheers,

Signature · 1 line
Julien Grall