Skip to contentSkip to conversation

Clean up of gzip decompressor

PATCH Superseded Daniel P. Smith 4 participants 28 messages Apr 11, 2024 – Apr 18, 2024

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Clean up of gzip decompressor

An issue ran into by hyperlaunch was the need to use the gzip decompressor multiple times. The current implementation fails when reused due to tainting of decompressor state from a previous usage. This series seeks to colocate the gzip unit files under a single directory similar to the other decompression algorithms. To enable the refactoring of the state tracking, the code is then cleaned up in line with Xen coding style.

Daniel P. Smith (5):
gzip: colocate gunzip code files
gzip: clean up comments and fix code alignment
gzip: refactor state tracking
gzip: move crc state into consilidated gzip state
gzip: move huffman code table tracking into gzip state

 xen/common/Makefile             |   2 +-
 xen/common/gzip/Makefile        |   1 +
 xen/common/{ => gzip}/gunzip.c  |  73 ++-
 xen/common/{ => gzip}/inflate.c | 912 ++++++++++++++++----------------
 4 files changed, 504 insertions(+), 484 deletions(-)
 create mode 100644 xen/common/gzip/Makefile
 rename xen/common/{ => gzip}/gunzip.c (64%)
 rename xen/common/{ => gzip}/inflate.c (57%)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/5 gzip: colocate gunzip code files

This patch moves the gunzip code files to common/gzip. Makefiles are adjusted accordingly.

---
 xen/common/Makefile             | 2 +-
 xen/common/gzip/Makefile        | 1 +
 xen/common/{ => gzip}/gunzip.c  | 0
 xen/common/{ => gzip}/inflate.c | 0
 4 files changed, 2 insertions(+), 1 deletion(-)
 create mode 100644 xen/common/gzip/Makefile
 rename xen/common/{ => gzip}/gunzip.c (100%)
 rename xen/common/{ => gzip}/inflate.c (100%)
xen/common/Makefile +1 -1
index e5eee19a8537..d512cad5243f 100644
@@ -14,7 +14,7 @@ obj-y += event_channel.o
 obj-y += event_fifo.o
 obj-$(CONFIG_GRANT_TABLE) += grant_table.o
 obj-y += guestcopy.o
-obj-bin-y += gunzip.init.o
+obj-y += gzip/
 obj-$(CONFIG_HYPFS) += hypfs.o
 obj-$(CONFIG_IOREQ_SERVER) += ioreq.o
 obj-y += irq.o
xen/common/gzip/Makefile new file +1 -0
new file mode 100644
index 000000000000..bda73c0184ad
@@ -0,0 +1 @@
+obj-bin-y += gunzip.init.o
xen/common/gzip/gunzip.c renamed from xen/common/gunzip.c +0 -0
similarity index 100%
rename from xen/common/gunzip.c
rename to xen/common/gzip/gunzip.c
xen/common/gzip/inflate.c renamed from xen/common/inflate.c +0 -0
similarity index 100%
rename from xen/common/inflate.c
rename to xen/common/gzip/inflate.c
Signature · 1 line
2.30.2
#6 lore raw

PATCH 2/5 gzip: clean up comments and fix code alignment

In reply to Daniel P. Smith

This commit cleans up the comments and fixes the code alignment using Xen coding style. This is done to make the code more legible before refactoring.

---
 xen/common/gzip/gunzip.c  |  10 +-
 xen/common/gzip/inflate.c | 748 +++++++++++++++++++-------------------
 2 files changed, 384 insertions(+), 374 deletions(-)
xen/common/gzip/gunzip.c +9 -1
index 2c6eae167d54..1bcb007395ba 100644
@@ -63,7 +63,6 @@ static __init int fill_inbuf(void)
         return 0;
 }
 
-
 #include "inflate.c"
 
 static __init void flush_window(void)
@@ -137,3 +136,12 @@ __init int perform_gunzip(char *output, char *image, unsigned long image_len)
 
     return rc;
 }
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/common/gzip/inflate.c +375 -373

1,025 lines not on this page · raw message

Signature · 1 line
2.30.2
#10 lore raw

PATCH 3/5 gzip: refactor state tracking

In reply to Daniel P. Smith

Move the core state into struct gzip_data to allow a per decompression instance.

---
 xen/common/gzip/gunzip.c  |  50 ++++++-----
 xen/common/gzip/inflate.c | 174 +++++++++++++++++++-------------------
 2 files changed, 116 insertions(+), 108 deletions(-)
xen/common/gzip/gunzip.c +29 -21
index 1bcb007395ba..9b4891731b8b 100644
@@ -6,21 +6,29 @@
 
 #define HEAPORDER 3
 
-static unsigned char *__initdata window;
 #define memptr long
 static memptr __initdata free_mem_ptr;
 static memptr __initdata free_mem_end_ptr;
 
 #define WSIZE           0x80000000U
 
-static unsigned char *__initdata inbuf;
-static unsigned int __initdata insize;
+struct gzip_data {
+    unsigned char *window;
 
-/* Index of next byte to be processed in inbuf: */
-static unsigned int __initdata inptr;
+    unsigned char *inbuf;
+    unsigned int insize;
 
-/* Bytes in output buffer: */
-static unsigned int __initdata outcnt;
+    /* Index of next byte to be processed in inbuf: */
+    unsigned int inptr;
+
+    /* Bytes in output buffer: */
+    unsigned int outcnt;
+
+    long bytes_out;
+
+    unsigned long bb;      /* bit buffer */
+    unsigned bk;           /* bits in bit buffer */
+};
 
 #define OF(args)        args
 
@@ -30,7 +38,7 @@ typedef unsigned char   uch;
 typedef unsigned short  ush;
 typedef unsigned long   ulg;
 
-#define get_byte()      (inptr < insize ? inbuf[inptr++] : fill_inbuf())
+#define get_byte()     (gd->inptr < gd->insize ? gd->inbuf[gd->inptr++] : fill_inbuf())
 
 /* Diagnostic functions */
 #ifdef DEBUG
@@ -49,8 +57,7 @@ typedef unsigned long   ulg;
 #  define Tracecv(c, x)
 #endif
 
-static long __initdata bytes_out;
-static void flush_window(void);
+static void flush_window(struct gzip_data *gd);
 
 static __init void error(const char *x)
 {
@@ -65,7 +72,7 @@ static __init int fill_inbuf(void)
 
 #include "inflate.c"
 
-static __init void flush_window(void)
+static __init void flush_window(struct gzip_data *gd)
 {
     /*
      * The window is equal to the output buffer therefore only need to
@@ -75,16 +82,16 @@ static __init void flush_window(void)
     unsigned int n;
     unsigned char *in, ch;
 
-    in = window;
-    for ( n = 0; n < outcnt; n++ )
+    in = gd->window;
+    for ( n = 0; n < gd->outcnt; n++ )
     {
         ch = *in++;
         c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
     }
     crc = c;
 
-    bytes_out += (unsigned long)outcnt;
-    outcnt = 0;
+    gd->bytes_out += (unsigned long)gd->outcnt;
+    gd->outcnt = 0;
 }
 
 __init int gzip_check(char *image, unsigned long image_len)
@@ -102,12 +109,13 @@ __init int gzip_check(char *image, unsigned long image_len)
 
 __init int perform_gunzip(char *output, char *image, unsigned long image_len)
 {
+    struct gzip_data gd;
     int rc;
 
     if ( !gzip_check(image, image_len) )
         return 1;
 
-    window = (unsigned char *)output;
+    gd.window = (unsigned char *)output;
 
     free_mem_ptr = (unsigned long)alloc_xenheap_pages(HEAPORDER, 0);
     if ( !free_mem_ptr )
@@ -116,14 +124,14 @@ __init int perform_gunzip(char *output, char *image, unsigned long image_len)
     free_mem_end_ptr = free_mem_ptr + (PAGE_SIZE << HEAPORDER);
     init_allocator();
 
-    inbuf = (unsigned char *)image;
-    insize = image_len;
-    inptr = 0;
-    bytes_out = 0;
+    gd.inbuf = (unsigned char *)image;
+    gd.insize = image_len;
+    gd.inptr = 0;
+    gd.bytes_out = 0;
 
     makecrc();
 
-    if ( gunzip() < 0 )
+    if ( gunzip(&gd) < 0 )
     {
         rc = -EINVAL;
     }
xen/common/gzip/inflate.c +87 -87

468 lines not on this page · raw message

Signature · 1 line
2.30.2
#22 lore raw

PATCH 4/5 gzip: move crc state into consilidated gzip state

In reply to Daniel P. Smith
---
 xen/common/gzip/gunzip.c  | 11 +++++++----
 xen/common/gzip/inflate.c | 12 +++++-------
 2 files changed, 12 insertions(+), 11 deletions(-)
xen/common/gzip/gunzip.c +7 -4
index 9b4891731b8b..a1b516b925c9 100644
@@ -28,6 +28,9 @@ struct gzip_data {
 
     unsigned long bb;      /* bit buffer */
     unsigned bk;           /* bits in bit buffer */
+
+    unsigned long crc_32_tab[256];
+    unsigned long crc;
 };
 
 #define OF(args)        args
@@ -78,7 +81,7 @@ static __init void flush_window(struct gzip_data *gd)
      * The window is equal to the output buffer therefore only need to
      * compute the crc.
      */
-    unsigned long c = crc;
+    unsigned long c = gd->crc;
     unsigned int n;
     unsigned char *in, ch;
 
@@ -86,9 +89,9 @@ static __init void flush_window(struct gzip_data *gd)
     for ( n = 0; n < gd->outcnt; n++ )
     {
         ch = *in++;
-        c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
+        c = gd->crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
     }
-    crc = c;
+    gd->crc = c;
 
     gd->bytes_out += (unsigned long)gd->outcnt;
     gd->outcnt = 0;
@@ -129,7 +132,7 @@ __init int perform_gunzip(char *output, char *image, unsigned long image_len)
     gd.inptr = 0;
     gd.bytes_out = 0;
 
-    makecrc();
+    makecrc(&gd);
 
     if ( gunzip(&gd) < 0 )
     {
xen/common/gzip/inflate.c +5 -7
index c8dd35962abb..6c8c7452a31f 100644
@@ -1125,16 +1125,14 @@ static int __init inflate(struct gzip_data *gd)
  *
  **********************************************************************/
 
-static ulg __initdata crc_32_tab[256];
-static ulg __initdata crc;  /* initialized in makecrc() so it'll reside in bss */
-#define CRC_VALUE (crc ^ 0xffffffffUL)
+#define CRC_VALUE (gd->crc ^ 0xffffffffUL)
 
 /*
  * Code to compute the CRC-32 table. Borrowed from
  * gzip-1.0.3/makecrc.c.
  */
 
-static void __init makecrc(void)
+static void __init makecrc(struct gzip_data *gd)
 {
 /* Not copyrighted 1990 Mark Adler */
 
@@ -1151,7 +1149,7 @@ static void __init makecrc(void)
     for (i = 0; i < sizeof(p)/sizeof(int); i++)
         e |= 1L << (31 - p[i]);
 
-    crc_32_tab[0] = 0;
+    gd->crc_32_tab[0] = 0;
 
     for (i = 1; i < 256; i++)
     {
@@ -1162,11 +1160,11 @@ static void __init makecrc(void)
             if (k & 1)
                 c ^= e;
         }
-        crc_32_tab[i] = c;
+        gd->crc_32_tab[i] = c;
     }
 
     /* this is initialized here so this code could reside in ROM */
-    crc = (ulg)0xffffffffUL; /* shift register contents */
+    gd->crc = (ulg)0xffffffffUL; /* shift register contents */
 }
 
 /* gzip flag byte */
Signature · 1 line
2.30.2
#25 lore raw

PATCH 5/5 gzip: move huffman code table tracking into gzip state

In reply to Daniel P. Smith
---
 xen/common/gzip/gunzip.c  |  2 ++
 xen/common/gzip/inflate.c | 26 ++++++++++++--------------
 2 files changed, 14 insertions(+), 14 deletions(-)
xen/common/gzip/gunzip.c +2 -0
index a1b516b925c9..79a641263597 100644
@@ -31,6 +31,8 @@ struct gzip_data {
 
     unsigned long crc_32_tab[256];
     unsigned long crc;
+
+    unsigned hufts;        /* track memory usage */
 };
 
 #define OF(args)        args
xen/common/gzip/inflate.c +12 -14
index 6c8c7452a31f..53ee1d8ce1e3 100644
@@ -140,7 +140,7 @@ struct huft {
 };
 
 /* Function prototypes */
-static int huft_build OF((unsigned *, unsigned, unsigned,
+static int huft_build OF((struct gzip_data *, unsigned *, unsigned, unsigned,
                           const ush *, const ush *, struct huft **, int *));
 static int huft_free OF((struct huft *));
 static int inflate_codes OF((struct gzip_data *, struct huft *, struct huft *, int, int));
@@ -311,8 +311,6 @@ static const int dbits = 6;          /* bits in base distance lookup table */
 #define BMAX 16         /* maximum bit length of any code (16 for explode) */
 #define N_MAX 288       /* maximum number of codes in any set */
 
-static unsigned __initdata hufts;      /* track memory usage */
-
 /*
  * Given a list of code lengths and a maximum table size, make a set of
  * tables to decode that set of codes.  Return zero on success, one if
@@ -329,8 +327,8 @@ static unsigned __initdata hufts;      /* track memory usage */
  * @param m Maximum lookup bits, returns actual
  */
 static int __init huft_build(
-    unsigned *b, unsigned n, unsigned s, const ush *d, const ush *e,
-    struct huft **t, int *m)
+    struct gzip_data *gd, unsigned *b, unsigned n, unsigned s, const ush *d,
+    const ush *e, struct huft **t, int *m)
 {
     unsigned a;                   /* counter for codes of length k */
     unsigned f;                   /* i repeats in table every f entries */
@@ -492,7 +490,7 @@ static int __init huft_build(
                     goto out;
                 }
                 DEBG1("4 ");
-                hufts += z + 1;         /* track memory usage */
+                gd->hufts += z + 1;         /* track memory usage */
                 *t = q + 1;             /* link to list for huft_free() */
                 *(t = &(q->v.t)) = (struct huft *)NULL;
                 u[h] = ++q;             /* table starts after link */
@@ -787,7 +785,7 @@ static int noinline __init inflate_fixed(struct gzip_data *gd)
     for (; i < 288; i++)          /* make a complete, but wrong code set */
         l[i] = 8;
     bl = 7;
-    if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
+    if ((i = huft_build(gd, l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
         free(l);
         return i;
     }
@@ -796,7 +794,7 @@ static int noinline __init inflate_fixed(struct gzip_data *gd)
     for (i = 0; i < 30; i++)      /* make an incomplete code set */
         l[i] = 5;
     bd = 5;
-    if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
+    if ((i = huft_build(gd, l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
     {
         huft_free(tl);
         free(l);
@@ -894,7 +892,7 @@ static int noinline __init inflate_dynamic(struct gzip_data *gd)
 
     /* build decoding table for trees--single level, 7 bit lookup */
     bl = 7;
-    if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
+    if ((i = huft_build(gd, ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
     {
         if (i == 1)
             huft_free(tl);
@@ -971,7 +969,7 @@ static int noinline __init inflate_dynamic(struct gzip_data *gd)
 
     /* build the decoding tables for literal/length and distance codes */
     bl = lbits;
-    if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
+    if ((i = huft_build(gd, ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
     {
         DEBG("dyn5b ");
         if (i == 1) {
@@ -983,7 +981,7 @@ static int noinline __init inflate_dynamic(struct gzip_data *gd)
     }
     DEBG("dyn5c ");
     bd = dbits;
-    if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
+    if ((i = huft_build(gd, ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
     {
         DEBG("dyn5d ");
         if (i == 1) {
@@ -1090,15 +1088,15 @@ static int __init inflate(struct gzip_data *gd)
     /* decompress until the last block */
     h = 0;
     do {
-        hufts = 0;
+        gd->hufts = 0;
 #ifdef ARCH_HAS_DECOMP_WDOG
         arch_decomp_wdog();
 #endif
         r = inflate_block(gd, &e);
         if (r)
             return r;
-        if (hufts > h)
-            h = hufts;
+        if (gd->hufts > h)
+            h = gd->hufts;
     } while (!e);
 
     /* Undo too much lookahead. The next read will be byte aligned so we
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH 1/5] gzip: colocate gunzip code files

In reply to Daniel P. Smith
Quoted 9 lines · Daniel P. Smith

This patch moves the gunzip code files to common/gzip. Makefiles are adjusted accordingly.

---

Quoted 8 lines
xen/common/Makefile             | 2 +-
xen/common/gzip/Makefile        | 1 +
xen/common/{ => gzip}/gunzip.c  | 0
xen/common/{ => gzip}/inflate.c | 0
4 files changed, 2 insertions(+), 1 deletion(-)

create mode 100644 xen/common/gzip/Makefile rename xen/common/{ => gzip}/gunzip.c (100%) rename xen/common/{ => gzip}/inflate.c (100%)

For inflate.c you will need to update also docs/misra/exclude-list.json

#3 lore raw

Re: [PATCH 1/5] gzip: colocate gunzip code files

Quoted 15 lines · Luca Fancellu
Quoted 7 lines · Daniel P. Smith

This patch moves the gunzip code files to common/gzip. Makefiles are adjusted accordingly.

---

Quoted 8 lines
xen/common/Makefile             | 2 +-
xen/common/gzip/Makefile        | 1 +
xen/common/{ => gzip}/gunzip.c  | 0
xen/common/{ => gzip}/inflate.c | 0
4 files changed, 2 insertions(+), 1 deletion(-)

create mode 100644 xen/common/gzip/Makefile rename xen/common/{ => gzip}/gunzip.c (100%) rename xen/common/{ => gzip}/inflate.c (100%)

Quoted 1 line

For inflate.c you will need to update also docs/misra/exclude-list.json

Something like this?

docs/misra/exclude-list.json +0 -0
index 36bad9e54f7d..095636415897 100644
@@ -118,7 +118,7 @@

             "comment": "Imported from Linux, ignore for now" },
         {
-            "rel_path": "common/inflate.c",
+            "rel_path": "common/gzip/inflate.c", "comment": "Imported from Linux, ignore for now" },
         {

I can fold on commit.

~Andrew

#7 lore raw

Re: [PATCH 2/5] gzip: clean up comments and fix code alignment

In reply to Daniel P. Smith
Quoted 4 lines · Daniel P. Smith

This commit cleans up the comments and fixes the code alignment using Xen coding style. This is done to make the code more legible before refactoring.

I've found two more minor adjustments:

xen/common/gzip/inflate.c +0 -0
index feb6d51008aa..9205189d4618 100644
@@ -375,7 +375,7 @@ static int __init huft_build(

     memzero(stk->c, sizeof(stk->c));
     p = b;  i = n;
     do {
-        Tracecv(*p, (stderr, (n-i >= ' ' && n-i <= '~' ? "%c %d\n" :
"0x%x %d\n"),
+        Tracecv(*p, (stderr, (n-i >= ' ' && n-i <= '~' ? "%c %d\n" :
"0x%x %d\n"),
                      n-i, *p));
         c[*p]++;                    /* assume all entries <= BMAX */ p++;                      /* Can't combine with above line (Solaris bug) */
@@ -563,7 +563,8 @@ static int __init huft_build(
     return ret;
 }

-/* Free the malloc'ed tables built by huft_build(), which makes a linked +/*
+ * Free the malloc'ed tables built by huft_build(), which makes a linked
  * list of the tables it made, with the links in a dummy first entry of
  * each table.
  *

I can fold on commit.  (First hunk is trailing whitespace, which doesn't show up so well on email).

However, there are some more major adjustments wanted too.

The reason why the code indention is so messed up is because it has been auto-formatted, but with some NEXTBYTE()/NEEDBITS()/DUMPBITS() missing semi-colons.  This throws off subsequent formatting, including some of the indentation changes you've made.

Fixing the semicolons is a far more messy diff, but a much better end result.

However, the PKZIP_BUG_WORKAROUND ifdefary hiding braces still throws things off, this time in the opposite direction.  NOMEMCPY also gets in the way.

I'd be tempted to suggest breaking out a patch earlier dropping these two, and then doing the semicolon fixes in this one along with the other work.

Thoughts?

~Andrew

#11 lore raw

Re: [PATCH 3/5] gzip: refactor state tracking

In reply to Daniel P. Smith

xen/common/gzip/gunzip.c __init int gzip_check(char *image, unsigned long image_len) · line 113

Quoted 10 lines · Daniel P. Smith
xen/common/gzip/gunzip.c +1 -0
index 1bcb007395ba..9b4891731b8b 100644
@@ -102,12 +109,13 @@ __init int gzip_check(char *image, unsigned long image_len)
 
 __init int perform_gunzip(char *output, char *image, unsigned long image_len)
 {
+    struct gzip_data gd;
     int rc;

By the end of this series,

Reading symbols from xen-syms...
(gdb) p sizeof(struct gzip_data)
$1 = 2120

x86 has an 8k stack and this takes 1/4 of it.  Other bits of state are dynamically allocated, even in inflate.c, so I'd highly recommend doing the same for this.

Also, could I nitpick the name and request:

struct gzip_state *s;

?

~Andrew

#23 lore raw

Re: [PATCH 4/5] gzip: move crc state into consilidated gzip state

In reply to Daniel P. Smith
Quoted 36 lines · Daniel P. Smith
xen/common/gzip/inflate.c +3 -5
index c8dd35962abb..6c8c7452a31f 100644
@@ -1125,16 +1125,14 @@ static int __init inflate(struct gzip_data *gd)
  *
  **********************************************************************/
 
-static ulg __initdata crc_32_tab[256];
-static ulg __initdata crc;  /* initialized in makecrc() so it'll reside in bss */
-#define CRC_VALUE (crc ^ 0xffffffffUL)
+#define CRC_VALUE (gd->crc ^ 0xffffffffUL)
 
 /*
  * Code to compute the CRC-32 table. Borrowed from
  * gzip-1.0.3/makecrc.c.
  */
 
-static void __init makecrc(void)
+static void __init makecrc(struct gzip_data *gd)
 {
 /* Not copyrighted 1990 Mark Adler */
 
@@ -1151,7 +1149,7 @@ static void __init makecrc(void)
     for (i = 0; i < sizeof(p)/sizeof(int); i++)
         e |= 1L << (31 - p[i]);
 
-    crc_32_tab[0] = 0;
+    gd->crc_32_tab[0] = 0;
 
     for (i = 1; i < 256; i++)
     {
@@ -1162,11 +1160,11 @@ static void __init makecrc(void)
             if (k & 1)
                 c ^= e;
         }

xen/common/gzip/inflate.c static void __init makecrc(void) · line 1170

Quoted 10 lines
xen/common/gzip/inflate.c +2 -2
-        crc_32_tab[i] = c;
+        gd->crc_32_tab[i] = c;
     }
 
     /* this is initialized here so this code could reside in ROM */
-    crc = (ulg)0xffffffffUL; /* shift register contents */
+    gd->crc = (ulg)0xffffffffUL; /* shift register contents */
 }
 
 /* gzip flag byte */

I can't see any way that a non-32bit value ever gets stored, because 'e' doesn't ever have bit 32 set in it.  I have a sneaking suspicion that this is code written in the 32bit days, where sizeof(long) was still 4.

This change, if correct, halves the size of gzip_state.

~Andrew

#4 lore raw

Re: [PATCH 1/5] gzip: colocate gunzip code files

In reply to Andrew Cooper
Quoted 13 lines
Quoted 5 lines
Quoted 4 lines

create mode 100644 xen/common/gzip/Makefile
rename xen/common/{ => gzip}/gunzip.c (100%)
rename xen/common/{ => gzip}/inflate.c (100%)

For inflate.c you will need to update also docs/misra/exclude-list.json

Something like this?

docs/misra/exclude-list.json +0 -0
index 36bad9e54f7d..095636415897 100644

docs/misra/exclude-list.json line 124

Quoted 8 lines
docs/misra/exclude-list.json +1 -1
@@ -118,7 +118,7 @@
             "comment": "Imported from Linux, ignore for now"
         },
         {
-            "rel_path": "common/inflate.c",
+            "rel_path": "common/gzip/inflate.c",
             "comment": "Imported from Linux, ignore for now"
         },
         {

Yes indeed

#26 lore raw

Re: [PATCH 5/5] gzip: move huffman code table tracking into gzip state

In reply to Daniel P. Smith
Quoted 115 lines · Daniel P. Smith
---
 xen/common/gzip/gunzip.c  |  2 ++
 xen/common/gzip/inflate.c | 26 ++++++++++++--------------
 2 files changed, 14 insertions(+), 14 deletions(-)
xen/common/gzip/gunzip.c +2 -0
index a1b516b925c9..79a641263597 100644
@@ -31,6 +31,8 @@ struct gzip_data {
 
     unsigned long crc_32_tab[256];
     unsigned long crc;
+
+    unsigned hufts;        /* track memory usage */
 };
 
 #define OF(args)        args
xen/common/gzip/inflate.c +10 -12
index 6c8c7452a31f..53ee1d8ce1e3 100644
@@ -140,7 +140,7 @@ struct huft {
 };
 
 /* Function prototypes */
-static int huft_build OF((unsigned *, unsigned, unsigned,
+static int huft_build OF((struct gzip_data *, unsigned *, unsigned, unsigned,
                           const ush *, const ush *, struct huft **, int *));
 static int huft_free OF((struct huft *));
 static int inflate_codes OF((struct gzip_data *, struct huft *, struct huft *, int, int));
@@ -311,8 +311,6 @@ static const int dbits = 6;          /* bits in base distance lookup table */
 #define BMAX 16         /* maximum bit length of any code (16 for explode) */
 #define N_MAX 288       /* maximum number of codes in any set */
 
-static unsigned __initdata hufts;      /* track memory usage */
-
 /*
  * Given a list of code lengths and a maximum table size, make a set of
  * tables to decode that set of codes.  Return zero on success, one if
@@ -329,8 +327,8 @@ static unsigned __initdata hufts;      /* track memory usage */
  * @param m Maximum lookup bits, returns actual
  */
 static int __init huft_build(
-    unsigned *b, unsigned n, unsigned s, const ush *d, const ush *e,
-    struct huft **t, int *m)
+    struct gzip_data *gd, unsigned *b, unsigned n, unsigned s, const ush *d,
+    const ush *e, struct huft **t, int *m)
 {
     unsigned a;                   /* counter for codes of length k */
     unsigned f;                   /* i repeats in table every f entries */
@@ -492,7 +490,7 @@ static int __init huft_build(
                     goto out;
                 }
                 DEBG1("4 ");
-                hufts += z + 1;         /* track memory usage */
+                gd->hufts += z + 1;         /* track memory usage */
                 *t = q + 1;             /* link to list for huft_free() */
                 *(t = &(q->v.t)) = (struct huft *)NULL;
                 u[h] = ++q;             /* table starts after link */
@@ -787,7 +785,7 @@ static int noinline __init inflate_fixed(struct gzip_data *gd)
     for (; i < 288; i++)          /* make a complete, but wrong code set */
         l[i] = 8;
     bl = 7;
-    if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
+    if ((i = huft_build(gd, l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
         free(l);
         return i;
     }
@@ -796,7 +794,7 @@ static int noinline __init inflate_fixed(struct gzip_data *gd)
     for (i = 0; i < 30; i++)      /* make an incomplete code set */
         l[i] = 5;
     bd = 5;
-    if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
+    if ((i = huft_build(gd, l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
     {
         huft_free(tl);
         free(l);
@@ -894,7 +892,7 @@ static int noinline __init inflate_dynamic(struct gzip_data *gd)
 
     /* build decoding table for trees--single level, 7 bit lookup */
     bl = 7;
-    if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
+    if ((i = huft_build(gd, ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
     {
         if (i == 1)
             huft_free(tl);
@@ -971,7 +969,7 @@ static int noinline __init inflate_dynamic(struct gzip_data *gd)
 
     /* build the decoding tables for literal/length and distance codes */
     bl = lbits;
-    if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
+    if ((i = huft_build(gd, ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
     {
         DEBG("dyn5b ");
         if (i == 1) {
@@ -983,7 +981,7 @@ static int noinline __init inflate_dynamic(struct gzip_data *gd)
     }
     DEBG("dyn5c ");
     bd = dbits;
-    if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
+    if ((i = huft_build(gd, ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
     {
         DEBG("dyn5d ");
         if (i == 1) {
@@ -1090,15 +1088,15 @@ static int __init inflate(struct gzip_data *gd)
     /* decompress until the last block */
     h = 0;
     do {
-        hufts = 0;
+        gd->hufts = 0;
 #ifdef ARCH_HAS_DECOMP_WDOG
         arch_decomp_wdog();
 #endif

xen/common/gzip/inflate.c static int __init inflate(struct gzip_data *gd) · line 1102

Quoted 10 lines
xen/common/gzip/inflate.c +2 -2
         r = inflate_block(gd, &e);
         if (r)
             return r;
-        if (hufts > h)
-            h = hufts;
+        if (gd->hufts > h)
+            h = gd->hufts;
     } while (!e);
 
     /* Undo too much lookahead. The next read will be byte aligned so we

AFAICT, hothing in inflate() reads h.  So hufts is a write-only variable?

Can't we just delete it, rather than plumb it through into the state block?  It would certainly shrink this patch somewhat.

~Andrew

#5 lore raw

Re: [PATCH 1/5] gzip: colocate gunzip code files

In reply to Luca Fancellu
Quoted 8 lines · Luca Fancellu
Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

create mode 100644 xen/common/gzip/Makefile
rename xen/common/{ => gzip}/gunzip.c (100%)
rename xen/common/{ => gzip}/inflate.c (100%)

For inflate.c you will need to update also docs/misra/exclude-list.json

Something like this?

Quoted 15 lines

docs/misra/exclude-list.json line 124

Quoted 13 lines
docs/misra/exclude-list.json +1 -1
index 36bad9e54f7d..095636415897 100644
@@ -118,7 +118,7 @@
              "comment": "Imported from Linux, ignore for now"
          },
          {
-            "rel_path": "common/inflate.c",
+            "rel_path": "common/gzip/inflate.c",
              "comment": "Imported from Linux, ignore for now"
          },
          {

Yes indeed

Ack, will add.

#8 lore raw

Re: [PATCH 2/5] gzip: clean up comments and fix code alignment

In reply to Andrew Cooper
Quoted 36 lines · Andrew Cooper
Quoted 4 lines · Daniel P. Smith

This commit cleans up the comments and fixes the code alignment using Xen coding style. This is done to make the code more legible before refactoring.

I've found two more minor adjustments:

xen/common/gzip/inflate.c +1 -1
index feb6d51008aa..9205189d4618 100644
@@ -375,7 +375,7 @@ static int __init huft_build(
      memzero(stk->c, sizeof(stk->c));
      p = b;  i = n;
      do {
-        Tracecv(*p, (stderr, (n-i >= ' ' && n-i <= '~' ? "%c %d\n" :
 "0x%x %d\n"),
+        Tracecv(*p, (stderr, (n-i >= ' ' && n-i <= '~' ? "%c %d\n" :
 "0x%x %d\n"),
                       n-i, *p));

         c[*p]++;                    /* assume all entries <= BMAX */ p++;                      /* Can't combine with above line
(Solaris bug) */

xen/common/gzip/inflate.c +2 -1
@@ -563,7 +563,8 @@ static int __init huft_build(
      return ret;
  }
 
-/* Free the malloc'ed tables built by huft_build(), which makes a linked
+/*
+ * Free the malloc'ed tables built by huft_build(), which makes a linked
   * list of the tables it made, with the links in a dummy first entry of
   * each table.
   *
Quoted 2 lines

I can fold on commit.  (First hunk is trailing whitespace, which doesn't show up so well on email).

Hmm, I tried to catch all the trailing space. Since I already have to respin for the MISRA exclude list, I can double-check if there are any other trailing whitespaces I missed.

Quoted 1 line

However, there are some more major adjustments wanted too.

That's fine, I attempted at tugging on some of the ugliness and kept finding myself in a mess. If there are subtle improvements that can be made without doing the complete rewrite I think this really deserves, would be glad to incorporate them.

Quoted 7 lines

The reason why the code indention is so messed up is because it has been auto-formatted, but with some NEXTBYTE()/NEEDBITS()/DUMPBITS() missing semi-colons.  This throws off subsequent formatting, including some of the indentation changes you've made.

Fixing the semicolons is a far more messy diff, but a much better end result.

I looked and didn't see any missing semicolons for NEXTBYTE, but you are correct, I think almost every invocation of NEEDBITS and DUMPBITS are missing semicolons.

Quoted 7 lines

However, the PKZIP_BUG_WORKAROUND ifdefary hiding braces still throws things off, this time in the opposite direction.  NOMEMCPY also gets in the way.

I'd be tempted to suggest breaking out a patch earlier dropping these two, and then doing the semicolon fixes in this one along with the other work.

Upon checking, I did not see any way to set PKZIP_BUG_WORKAROUND and NOMEMCPY, so yes I think we can safely drop them.

Quoted 1 line

Thoughts?

I think it is all doable, the only question is would you prefer to see the PKZIP_BUG_WORKAROUND and NOMEMCPY drop happen before relocating the files or after relocation?

v/r,
dps

#12 lore raw

Re: [PATCH 3/5] gzip: refactor state tracking

In reply to Andrew Cooper
Quoted 14 lines · Andrew Cooper

xen/common/gzip/gunzip.c __init int gzip_check(char *image, unsigned long image_len) · line 113

Quoted 10 lines · Daniel P. Smith
xen/common/gzip/gunzip.c +1 -0
index 1bcb007395ba..9b4891731b8b 100644
@@ -102,12 +109,13 @@ __init int gzip_check(char *image, unsigned long image_len)
 
  __init int perform_gunzip(char *output, char *image, unsigned long image_len)
  {
+    struct gzip_data gd;
      int rc;

By the end of this series,

Quoted 7 lines

Reading symbols from xen-syms...
(gdb) p sizeof(struct gzip_data)
$1 = 2120

x86 has an 8k stack and this takes 1/4 of it.  Other bits of state are dynamically allocated, even in inflate.c, so I'd highly recommend doing the same for this.

I take it you are mainly talking about crc_32_tab? Yes, I can switch that to being dynamically allocated.

Quoted 3 lines

Also, could I nitpick the name and request:

struct gzip_state *s;

I have no attachment to names, so yes, I can switch the name.

v/r,
dps

#13 lore raw

Re: [PATCH 3/5] gzip: refactor state tracking

Quoted 19 lines · Daniel P. Smith
Quoted 18 lines · Andrew Cooper

xen/common/gzip/gunzip.c __init int gzip_check(char *image, unsigned · line 113

Quoted 10 lines · Daniel P. Smith
xen/common/gzip/gunzip.c +1 -0
index 1bcb007395ba..9b4891731b8b 100644
@@ -102,12 +109,13 @@ __init int gzip_check(char *image, unsigned 
 long image_len)
   __init int perform_gunzip(char *output, char *image, unsigned long image_len)
   {
+    struct gzip_data gd;
       int rc;

By the end of this series,

Reading symbols from xen-syms...
(gdb) p sizeof(struct gzip_data)
$1 = 2120

Quoted 5 lines
Quoted 3 lines

x86 has an 8k stack and this takes 1/4 of it.  Other bits of state are dynamically allocated, even in inflate.c, so I'd highly recommend doing the same for this.

I take it you are mainly talking about crc_32_tab? Yes, I can switch that to being dynamically allocated.

Never mind, reading your comment on patch4 made me realize you wanted the instance of struct dynamically allocated. Though the answer is still, yes, we can dynamically allocate it.

v/r,
dps

#24 lore raw

Re: [PATCH 4/5] gzip: move crc state into consilidated gzip state

In reply to Andrew Cooper
Quoted 46 lines · Andrew Cooper
Quoted 45 lines · Daniel P. Smith
xen/common/gzip/inflate.c +5 -7
index c8dd35962abb..6c8c7452a31f 100644
@@ -1125,16 +1125,14 @@ static int __init inflate(struct gzip_data *gd)
   *
   **********************************************************************/
 
-static ulg __initdata crc_32_tab[256];
-static ulg __initdata crc;  /* initialized in makecrc() so it'll reside in bss */
-#define CRC_VALUE (crc ^ 0xffffffffUL)
+#define CRC_VALUE (gd->crc ^ 0xffffffffUL)
 
  /*
   * Code to compute the CRC-32 table. Borrowed from
   * gzip-1.0.3/makecrc.c.
   */
 
-static void __init makecrc(void)
+static void __init makecrc(struct gzip_data *gd)
  {
  /* Not copyrighted 1990 Mark Adler */
 
@@ -1151,7 +1149,7 @@ static void __init makecrc(void)
      for (i = 0; i < sizeof(p)/sizeof(int); i++)
          e |= 1L << (31 - p[i]);
 
-    crc_32_tab[0] = 0;
+    gd->crc_32_tab[0] = 0;
 
      for (i = 1; i < 256; i++)
      {
@@ -1162,11 +1160,11 @@ static void __init makecrc(void)
              if (k & 1)
                  c ^= e;
          }
-        crc_32_tab[i] = c;
+        gd->crc_32_tab[i] = c;
      }
 
      /* this is initialized here so this code could reside in ROM */
-    crc = (ulg)0xffffffffUL; /* shift register contents */
+    gd->crc = (ulg)0xffffffffUL; /* shift register contents */
  }
 
Quoted 5 lines

xen/common/gzip/inflate.c static void __init makecrc(void) · line 1170

Quoted 1 line
xen/common/gzip/inflate.c +0 -0
  /* gzip flag byte */

I can't see any way that a non-32bit value ever gets stored, because 'e' doesn't ever have bit 32 set in it.  I have a sneaking suspicion that this is code written in the 32bit days, where sizeof(long) was still 4.

Yes, I can switch crc and crc_32_tab to uint32_t.

Quoted 1 line

This change, if correct, halves the size of gzip_state.

Ack.

v/r,
dps

#27 lore raw

Re: [PATCH 5/5] gzip: move huffman code table tracking into gzip state

In reply to Andrew Cooper
Quoted 125 lines · Andrew Cooper
Quoted 124 lines · Daniel P. Smith
---
  xen/common/gzip/gunzip.c  |  2 ++
  xen/common/gzip/inflate.c | 26 ++++++++++++--------------
  2 files changed, 14 insertions(+), 14 deletions(-)
xen/common/gzip/gunzip.c +2 -0
index a1b516b925c9..79a641263597 100644
@@ -31,6 +31,8 @@ struct gzip_data {
 
      unsigned long crc_32_tab[256];
      unsigned long crc;
+
+    unsigned hufts;        /* track memory usage */
  };
 
  #define OF(args)        args
xen/common/gzip/inflate.c +12 -14
index 6c8c7452a31f..53ee1d8ce1e3 100644
@@ -140,7 +140,7 @@ struct huft {
  };
 
  /* Function prototypes */
-static int huft_build OF((unsigned *, unsigned, unsigned,
+static int huft_build OF((struct gzip_data *, unsigned *, unsigned, unsigned,
                            const ush *, const ush *, struct huft **, int *));
  static int huft_free OF((struct huft *));
  static int inflate_codes OF((struct gzip_data *, struct huft *, struct huft *, int, int));
@@ -311,8 +311,6 @@ static const int dbits = 6;          /* bits in base distance lookup table */
  #define BMAX 16         /* maximum bit length of any code (16 for explode) */
  #define N_MAX 288       /* maximum number of codes in any set */
 
-static unsigned __initdata hufts;      /* track memory usage */
-
  /*
   * Given a list of code lengths and a maximum table size, make a set of
   * tables to decode that set of codes.  Return zero on success, one if
@@ -329,8 +327,8 @@ static unsigned __initdata hufts;      /* track memory usage */
   * @param m Maximum lookup bits, returns actual
   */
  static int __init huft_build(
-    unsigned *b, unsigned n, unsigned s, const ush *d, const ush *e,
-    struct huft **t, int *m)
+    struct gzip_data *gd, unsigned *b, unsigned n, unsigned s, const ush *d,
+    const ush *e, struct huft **t, int *m)
  {
      unsigned a;                   /* counter for codes of length k */
      unsigned f;                   /* i repeats in table every f entries */
@@ -492,7 +490,7 @@ static int __init huft_build(
                      goto out;
                  }
                  DEBG1("4 ");
-                hufts += z + 1;         /* track memory usage */
+                gd->hufts += z + 1;         /* track memory usage */
                  *t = q + 1;             /* link to list for huft_free() */
                  *(t = &(q->v.t)) = (struct huft *)NULL;
                  u[h] = ++q;             /* table starts after link */
@@ -787,7 +785,7 @@ static int noinline __init inflate_fixed(struct gzip_data *gd)
      for (; i < 288; i++)          /* make a complete, but wrong code set */
          l[i] = 8;
      bl = 7;
-    if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
+    if ((i = huft_build(gd, l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
          free(l);
          return i;
      }
@@ -796,7 +794,7 @@ static int noinline __init inflate_fixed(struct gzip_data *gd)
      for (i = 0; i < 30; i++)      /* make an incomplete code set */
          l[i] = 5;
      bd = 5;
-    if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
+    if ((i = huft_build(gd, l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
      {
          huft_free(tl);
          free(l);
@@ -894,7 +892,7 @@ static int noinline __init inflate_dynamic(struct gzip_data *gd)
 
      /* build decoding table for trees--single level, 7 bit lookup */
      bl = 7;
-    if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
+    if ((i = huft_build(gd, ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
      {
          if (i == 1)
              huft_free(tl);
@@ -971,7 +969,7 @@ static int noinline __init inflate_dynamic(struct gzip_data *gd)
 
      /* build the decoding tables for literal/length and distance codes */
      bl = lbits;
-    if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
+    if ((i = huft_build(gd, ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
      {
          DEBG("dyn5b ");
          if (i == 1) {
@@ -983,7 +981,7 @@ static int noinline __init inflate_dynamic(struct gzip_data *gd)
      }
      DEBG("dyn5c ");
      bd = dbits;
-    if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
+    if ((i = huft_build(gd, ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
      {
          DEBG("dyn5d ");
          if (i == 1) {
@@ -1090,15 +1088,15 @@ static int __init inflate(struct gzip_data *gd)
      /* decompress until the last block */
      h = 0;
      do {
-        hufts = 0;
+        gd->hufts = 0;
  #ifdef ARCH_HAS_DECOMP_WDOG
          arch_decomp_wdog();
  #endif
          r = inflate_block(gd, &e);
          if (r)
              return r;
-        if (hufts > h)
-            h = hufts;
+        if (gd->hufts > h)
+            h = gd->hufts;
      } while (!e);
 
Quoted 4 lines

xen/common/gzip/inflate.c static int __init inflate(struct gzip_data *gd) · line 1102

Quoted 1 line
xen/common/gzip/inflate.c +0 -0
      /* Undo too much lookahead. The next read will be byte aligned so we

AFAICT, hothing in inflate() reads h.  So hufts is a write-only variable?

Good question, let me study it to make sure.

Quoted 2 lines

Can't we just delete it, rather than plumb it through into the state block?  It would certainly shrink this patch somewhat.

Yes, if I can't find any reads of gd->hufts, directly or indirectly intermediate variables like h above, then yes, I can try dropping it and testing to see if anything breaks.

v/r,
dps

#9 lore raw

Re: [PATCH 2/5] gzip: clean up comments and fix code alignment

In reply to Daniel P. Smith
Quoted 7 lines · Daniel P. Smith
Quoted 1 line · Andrew Cooper

Thoughts?

I think it is all doable, the only question is would you prefer to see the PKZIP_BUG_WORKAROUND and NOMEMCPY drop happen before relocating the files or after relocation?

I'd suggest dropping these two in a prep patch ahead of this main cleanup.

~Andrew

#14 lore raw

Re: [PATCH 3/5] gzip: refactor state tracking

In reply to Daniel P. Smith
Quoted 25 lines · Daniel P. Smith
Quoted 24 lines · Daniel P. Smith
Quoted 22 lines · Andrew Cooper
Quoted 11 lines · Daniel P. Smith
xen/common/gzip/gunzip.c +0 -0
index 1bcb007395ba..9b4891731b8b 100644
@@ -102,12 +109,13 @@ __init int gzip_check(char *image, unsigned
 long image_len)

  __init int perform_gunzip(char *output, char *image, unsigned long image_len)
  {
+    struct gzip_data gd;
      int rc;

By the end of this series,

Reading symbols from xen-syms...
(gdb) p sizeof(struct gzip_data)
$1 = 2120

x86 has an 8k stack and this takes 1/4 of it.  Other bits of state are dynamically allocated, even in inflate.c, so I'd highly recommend doing the same for this.

Quoted 6 lines
Quoted 2 lines

I take it you are mainly talking about crc_32_tab? Yes, I can switch that to being dynamically allocated.

Never mind, reading your comment on patch4 made me realize you wanted the instance of struct dynamically allocated. Though the answer is still, yes, we can dynamically allocate it.

I wrote this before realising that crc_32_tag could be shrunk.

If it's only1k on the stack, then that's a whole lot less bad, and is perhaps ok.  I guess it depends on the stack size of the other architectures.

Still - I expect dynamically allocating would be a safer course of action.  Internal blocks are dynamically allocated already, so this is "just" one more.

~Andrew

#15 lore raw

Re: [PATCH 3/5] gzip: refactor state tracking

Quoted 35 lines · Andrew Cooper
Quoted 25 lines · Daniel P. Smith
Quoted 24 lines · Daniel P. Smith
Quoted 22 lines · Andrew Cooper

xen/common/gzip/gunzip.c __init int gzip_check(char *image, unsigned · line 114

Quoted 11 lines · Daniel P. Smith
xen/common/gzip/gunzip.c +1 -0
index 1bcb007395ba..9b4891731b8b 100644
@@ -102,12 +109,13 @@ __init int gzip_check(char *image, unsigned
 long image_len)
   __init int perform_gunzip(char *output, char *image, unsigned
 long image_len)
   {
+    struct gzip_data gd;
       int rc;

By the end of this series,

Reading symbols from xen-syms...
(gdb) p sizeof(struct gzip_data)
$1 = 2120

x86 has an 8k stack and this takes 1/4 of it.  Other bits of state are dynamically allocated, even in inflate.c, so I'd highly recommend doing the same for this.

Quoted 6 lines
Quoted 2 lines

I take it you are mainly talking about crc_32_tab? Yes, I can switch that to being dynamically allocated.

Never mind, reading your comment on patch4 made me realize you wanted the instance of struct dynamically allocated. Though the answer is still, yes, we can dynamically allocate it.

I wrote this before realising that crc_32_tag could be shrunk.

Quoted 7 lines

If it's only1k on the stack, then that's a whole lot less bad, and is perhaps ok.  I guess it depends on the stack size of the other architectures.

Still - I expect dynamically allocating would be a safer course of action.  Internal blocks are dynamically allocated already, so this is "just" one more.

Another course of action that could be considered is making a unit file global instance of the struct, and then memset() it to zero instead of allocating and freeing from heap. The global instance should be able to be made init_data and dropped after init was complete.

I am good with either way, just let me know which would be preferred and I will adjust appropriately.

v/r,
dps

#16 lore raw

Re: [PATCH 3/5] gzip: refactor state tracking

Quoted 46 lines · Daniel P. Smith
Quoted 37 lines · Andrew Cooper
Quoted 27 lines · Daniel P. Smith
Quoted 26 lines · Daniel P. Smith
Quoted 24 lines · Andrew Cooper
Quoted 11 lines · Daniel P. Smith
xen/common/gzip/gunzip.c +0 -0
index 1bcb007395ba..9b4891731b8b 100644
@@ -102,12 +109,13 @@ __init int gzip_check(char *image, unsigned
 long image_len)

   __init int perform_gunzip(char *output, char *image, unsigned long image_len)
   {
+    struct gzip_data gd;
       int rc;

By the end of this series,

Reading symbols from xen-syms...
(gdb) p sizeof(struct gzip_data)
$1 = 2120

x86 has an 8k stack and this takes 1/4 of it.  Other bits of state are
dynamically allocated, even in inflate.c, so I'd highly recommend doing
the same for this.

Quoted 6 lines
Quoted 2 lines

I take it you are mainly talking about crc_32_tab? Yes, I can switch that to being dynamically allocated.

Never mind, reading your comment on patch4 made me realize you wanted the instance of struct dynamically allocated. Though the answer is still, yes, we can dynamically allocate it.

I wrote this before realising that crc_32_tag could be shrunk.

Quoted 7 lines

If it's only1k on the stack, then that's a whole lot less bad, and is perhaps ok.  I guess it depends on the stack size of the other architectures.

Still - I expect dynamically allocating would be a safer course of action.  Internal blocks are dynamically allocated already, so this is "just" one more.

Quoted 7 lines

Another course of action that could be considered is making a unit file global instance of the struct, and then memset() it to zero instead of allocating and freeing from heap. The global instance should be able to be made init_data and dropped after init was complete.

I am good with either way, just let me know which would be preferred and I will adjust appropriately.

Other things inside gzunip() are dynamically allocated.  I'd keep this consistent with the others.

~Andrew

#17 lore raw

Re: [PATCH 3/5] gzip: refactor state tracking

Quoted 52 lines · Andrew Cooper
Quoted 51 lines · Daniel P. Smith
Quoted 37 lines · Andrew Cooper
Quoted 27 lines · Daniel P. Smith
Quoted 26 lines · Daniel P. Smith
Quoted 24 lines · Andrew Cooper

xen/common/gzip/gunzip.c __init int gzip_check(char *image, unsigned · line 114

Quoted 11 lines · Daniel P. Smith
xen/common/gzip/gunzip.c +1 -0
index 1bcb007395ba..9b4891731b8b 100644
@@ -102,12 +109,13 @@ __init int gzip_check(char *image, unsigned
 long image_len)
    __init int perform_gunzip(char *output, char *image, unsigned
 long image_len)
    {
+    struct gzip_data gd;
        int rc;

By the end of this series,

Reading symbols from xen-syms...
(gdb) p sizeof(struct gzip_data)
$1 = 2120

x86 has an 8k stack and this takes 1/4 of it.  Other bits of state are
dynamically allocated, even in inflate.c, so I'd highly recommend doing
the same for this.

Quoted 6 lines
Quoted 2 lines

I take it you are mainly talking about crc_32_tab? Yes, I can switch that to being dynamically allocated.

Never mind, reading your comment on patch4 made me realize you wanted the instance of struct dynamically allocated. Though the answer is still, yes, we can dynamically allocate it.

I wrote this before realising that crc_32_tag could be shrunk.

Quoted 7 lines

If it's only1k on the stack, then that's a whole lot less bad, and is perhaps ok.  I guess it depends on the stack size of the other architectures.

Still - I expect dynamically allocating would be a safer course of action.  Internal blocks are dynamically allocated already, so this is "just" one more.

Another course of action that could be considered is making a unit file global instance of the struct, and then memset() it to zero instead of allocating and freeing from heap. The global instance should be able to be made init_data and dropped after init was complete.

Quoted 5 lines
Quoted 2 lines

I am good with either way, just let me know which would be preferred and I will adjust appropriately.

Other things inside gzunip() are dynamically allocated.  I'd keep this consistent with the others.

Ack.

v/r,
dps

#18 lore raw

Re: [PATCH 3/5] gzip: refactor state tracking

In reply to Andrew Cooper
Quoted 18 lines · Andrew Cooper

xen/common/gzip/gunzip.c __init int gzip_check(char *image, unsigned long image_len) · line 113

Quoted 10 lines · Daniel P. Smith
xen/common/gzip/gunzip.c +1 -0
index 1bcb007395ba..9b4891731b8b 100644
@@ -102,12 +109,13 @@ __init int gzip_check(char *image, unsigned long image_len)
 
 __init int perform_gunzip(char *output, char *image, unsigned long image_len)
 {
+    struct gzip_data gd;
     int rc;

By the end of this series,

Reading symbols from xen-syms...
(gdb) p sizeof(struct gzip_data)
$1 = 2120

Quoted 8 lines

x86 has an 8k stack and this takes 1/4 of it.  Other bits of state are dynamically allocated, even in inflate.c, so I'd highly recommend doing the same for this.

Also, could I nitpick the name and request:

struct gzip_state *s;

Except: Why "gzip" when it's un-zipping state?

Jan

#19 lore raw

Re: [PATCH 3/5] gzip: refactor state tracking

Quoted 24 lines · Jan Beulich
Quoted 23 lines · Andrew Cooper

xen/common/gzip/gunzip.c __init int gzip_check(char *image, unsigned long image_len) · line 113

Quoted 10 lines · Daniel P. Smith
xen/common/gzip/gunzip.c +1 -0
index 1bcb007395ba..9b4891731b8b 100644
@@ -102,12 +109,13 @@ __init int gzip_check(char *image, unsigned long image_len)
 
  __init int perform_gunzip(char *output, char *image, unsigned long image_len)
  {
+    struct gzip_data gd;
      int rc;

By the end of this series,

Reading symbols from xen-syms...
(gdb) p sizeof(struct gzip_data)
$1 = 2120

x86 has an 8k stack and this takes 1/4 of it.  Other bits of state are dynamically allocated, even in inflate.c, so I'd highly recommend doing the same for this.

Quoted 5 lines
Quoted 3 lines

Also, could I nitpick the name and request:

struct gzip_state *s;

Except: Why "gzip" when it's un-zipping state?

Gzip is the name of the algo/suite for which the code is moved under, and in typical fashion its structures are named after the feature they belong. Still, I went and looked at the other algos. I found two that have state tracking and yes, they do use the operation for the struct name and not the algo/feature under which they reside. If you want this yak shaved, I have no vested interest one way or another, I just need the decompressor to be re-entrant.

v/r,
dps

#20 lore raw

Re: [PATCH 3/5] gzip: refactor state tracking

Quoted 31 lines · Daniel P. Smith
Quoted 24 lines · Jan Beulich
Quoted 23 lines · Andrew Cooper

xen/common/gzip/gunzip.c __init int gzip_check(char *image, unsigned long image_len) · line 113

Quoted 10 lines · Daniel P. Smith
xen/common/gzip/gunzip.c +1 -0
index 1bcb007395ba..9b4891731b8b 100644
@@ -102,12 +109,13 @@ __init int gzip_check(char *image, unsigned long image_len)
 
  __init int perform_gunzip(char *output, char *image, unsigned long image_len)
  {
+    struct gzip_data gd;
      int rc;

By the end of this series,

Reading symbols from xen-syms...
(gdb) p sizeof(struct gzip_data)
$1 = 2120

x86 has an 8k stack and this takes 1/4 of it.  Other bits of state are dynamically allocated, even in inflate.c, so I'd highly recommend doing the same for this.

Quoted 5 lines
Quoted 3 lines

Also, could I nitpick the name and request:

struct gzip_state *s;

Except: Why "gzip" when it's un-zipping state?

Quoted 7 lines

Gzip is the name of the algo/suite for which the code is moved under, and in typical fashion its structures are named after the feature they belong. Still, I went and looked at the other algos. I found two that have state tracking and yes, they do use the operation for the struct name and not the algo/feature under which they reside. If you want this yak shaved, I have no vested interest one way or another, I just need the decompressor to be re-entrant.

Well. Generally speaking compressor and decompressor may need different state to track. As we have seen with tmem, there may be reasons why a compressor may also be needed in Xen. Hence unless it is known for sure that either no need will ever appear for gzip, or gzip's compression and decompression states are identical, I'd prefer the struct name to reflect the specific purpose.

Jan

#21 lore raw

Re: [PATCH 3/5] gzip: refactor state tracking

Quoted 40 lines · Jan Beulich
Quoted 31 lines · Daniel P. Smith
Quoted 24 lines · Jan Beulich
Quoted 23 lines · Andrew Cooper

xen/common/gzip/gunzip.c __init int gzip_check(char *image, unsigned long image_len) · line 113

Quoted 10 lines · Daniel P. Smith
xen/common/gzip/gunzip.c +1 -0
index 1bcb007395ba..9b4891731b8b 100644
@@ -102,12 +109,13 @@ __init int gzip_check(char *image, unsigned long image_len)
 
   __init int perform_gunzip(char *output, char *image, unsigned long image_len)
   {
+    struct gzip_data gd;
       int rc;

By the end of this series,

Reading symbols from xen-syms...
(gdb) p sizeof(struct gzip_data)
$1 = 2120

x86 has an 8k stack and this takes 1/4 of it.  Other bits of state are dynamically allocated, even in inflate.c, so I'd highly recommend doing the same for this.

Quoted 5 lines
Quoted 3 lines

Also, could I nitpick the name and request:

struct gzip_state *s;

Except: Why "gzip" when it's un-zipping state?

Quoted 7 lines

Gzip is the name of the algo/suite for which the code is moved under, and in typical fashion its structures are named after the feature they belong. Still, I went and looked at the other algos. I found two that have state tracking and yes, they do use the operation for the struct name and not the algo/feature under which they reside. If you want this yak shaved, I have no vested interest one way or another, I just need the decompressor to be re-entrant.

Quoted 6 lines

Well. Generally speaking compressor and decompressor may need different state to track. As we have seen with tmem, there may be reasons why a compressor may also be needed in Xen. Hence unless it is known for sure that either no need will ever appear for gzip, or gzip's compression and decompression states are identical, I'd prefer the struct name to reflect the specific purpose.

Ack