Skip to contentSkip to conversation

x86/Dom0: Use streaming decompression for ZSTD compressed kernels

PATCH In review Rafaël Kooi 2 participants 6 messages May 10, 2023 – May 11, 2023 2 messages not in the archive Zstd

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/2 xen/decompress: Add a user pointer for book keeping in the callbacks

Before this change the callbacks either needed to be completely stateless or use global state for book keeping. In the case where book keeping is needed we can't use global state in decompress.c because the linker disallows the existence of the .data and .bss segments. This change allows for localized book keeping with the user pointer.

---
 xen/common/bunzip2.c         | 23 +++++++++++++----------
 xen/common/unlz4.c           | 15 ++++++++-------
 xen/common/unlzma.c          | 30 ++++++++++++++++++------------
 xen/common/unlzo.c           | 13 +++++++------
 xen/common/unxz.c            | 11 ++++++-----
 xen/common/unzstd.c          | 13 +++++++------
 xen/include/xen/decompress.h | 10 +++++++---
 7 files changed, 66 insertions(+), 49 deletions(-)
xen/common/bunzip2.c +13 -10
index 4466426941..a854046524 100644
@@ -1,4 +1,4 @@
-/* vi: set sw = 4 ts = 4: */
+/* vi: set sw=4 ts=4: */
 /*	Small bzip2 deflate implementation, by Rob Landley ([email protected]).
 
 	Based on bzip2 decompression code by Julian R Seward ([email protected]),
@@ -86,7 +86,7 @@ struct bunzip_data {
 	/* State for interrupting output loop */
 	int writeCopies, writePos, writeRunCountdown, writeCount, writeCurrent;
 	/* I/O tracking data (file handles, buffers, positions, etc.) */
-	int (*fill)(void*, unsigned int);
+	int (*fill)(void*, unsigned int, void*);
 	int inbufCount, inbufPos /*, outbufPos*/;
 	unsigned char *inbuf /*,*outbuf*/;
 	unsigned int inbufBitCount, inbufBits;
@@ -99,6 +99,7 @@ struct bunzip_data {
 	unsigned char selectors[32768];		/* nSelectors = 15 bits */
 	struct group_data groups[MAX_GROUPS];	/* Huffman coding tables */
 	int io_error;			/* non-zero if we have IO error */
+	void *userptr;			/* user pointer to pass to fill */
 };
 
 
@@ -117,7 +118,7 @@ static unsigned int __init get_bits(struct bunzip_data *bd, char bits_wanted)
 		if (bd->inbufPos == bd->inbufCount) {
 			if (bd->io_error)
 				return 0;
-			bd->inbufCount = bd->fill(bd->inbuf, BZIP2_IOBUF_SIZE);
+			bd->inbufCount = bd->fill(bd->inbuf, BZIP2_IOBUF_SIZE, bd->userptr);
 			if (bd->inbufCount <= 0) {
 				bd->io_error = RETVAL_UNEXPECTED_INPUT_EOF;
 				return 0;
@@ -612,7 +613,7 @@ decode_next_byte:
 	goto decode_next_byte;
 }
 
-static int __init cf_check nofill(void *buf, unsigned int len)
+static int __init cf_check nofill(void *buf, unsigned int len, void *userptr)
 {
 	return -1;
 }
@@ -621,7 +622,7 @@ static int __init cf_check nofill(void *buf, unsigned int len)
    a complete bunzip file (len bytes long).  If in_fd!=-1, inbuf and len are
    ignored, and data is read from file handle into temporary buffer. */
 static int __init start_bunzip(struct bunzip_data **bdp, void *inbuf, int len,
-			       int (*fill)(void*, unsigned int))
+			       int (*fill)(void*, unsigned int, void*), void *userptr)
 {
 	struct bunzip_data *bd;
 	unsigned int i, j, c;
@@ -644,6 +645,7 @@ static int __init start_bunzip(struct bunzip_data **bdp, void *inbuf, int len,
 		bd->fill = fill;
 	else
 		bd->fill = nofill;
+	bd->userptr = userptr;
 
 	/* Init the CRC32 table (big endian) */
 	for (i = 0; i < 256; i++) {
@@ -671,10 +673,11 @@ static int __init start_bunzip(struct bunzip_data **bdp, void *inbuf, int len,
 /* Example usage: decompress src_fd to dst_fd.  (Stops at end of bzip2 data,
    not end of file.) */
 int __init bunzip2(unsigned char *buf, unsigned int len,
-		   int(*fill)(void*, unsigned int),
-		   int(*flush)(void*, unsigned int),
+		   int(*fill)(void*, unsigned int, void*),
+		   int(*flush)(void*, unsigned int, void*),
 		   unsigned char *outbuf, unsigned int *pos,
-		   void(*error)(const char *x))
+		   void(*error)(const char *x),
+		   void *userptr)
 {
 	struct bunzip_data *bd;
 	int i = -1;
@@ -696,7 +699,7 @@ int __init bunzip2(unsigned char *buf, unsigned int len,
 		i = RETVAL_OUT_OF_MEMORY;
 		goto exit_0;
 	}
-	i = start_bunzip(&bd, inbuf, len, fill);
+	i = start_bunzip(&bd, inbuf, len, fill, userptr);
 	if (!i) {
 		for (;;) {
 			i = read_bunzip(bd, outbuf, BZIP2_IOBUF_SIZE);
@@ -705,7 +708,7 @@ int __init bunzip2(unsigned char *buf, unsigned int len,
 			if (!flush)
 				outbuf += i;
 			else
-				if (i != flush(outbuf, i)) {
+				if (i != flush(outbuf, i, userptr)) {
 					i = RETVAL_UNEXPECTED_OUTPUT_EOF;
 					break;
 				}
xen/common/unlz4.c +8 -7
index 2096b98f36..00c179732e 100644
@@ -23,10 +23,11 @@
 #define ARCHIVE_MAGICNUMBER 0x184C2102
 
 int __init unlz4(unsigned char *input, unsigned int in_len,
-		 int (*fill)(void *, unsigned int),
-		 int (*flush)(void *, unsigned int),
+		 int (*fill)(void *, unsigned int, void*),
+		 int (*flush)(void *, unsigned int, void*),
 		 unsigned char *output, unsigned int *posp,
-		 void (*error)(const char *x))
+		 void (*error)(const char *x),
+		 void *userptr)
 {
 	int ret = -1;
 	size_t chunksize = 0;
@@ -75,7 +76,7 @@ int __init unlz4(unsigned char *input, unsigned int in_len,
 		*posp = 0;
 
 	if (fill)
-		fill(inp, 4);
+		fill(inp, 4, userptr);
 
 	chunksize = get_unaligned_le32(inp);
 	if (chunksize == ARCHIVE_MAGICNUMBER) {
@@ -92,7 +93,7 @@ int __init unlz4(unsigned char *input, unsigned int in_len,
 	for (;;) {
 
 		if (fill)
-			fill(inp, 4);
+			fill(inp, 4, userptr);
 
 		chunksize = get_unaligned_le32(inp);
 		if (chunksize == ARCHIVE_MAGICNUMBER) {
@@ -113,7 +114,7 @@ int __init unlz4(unsigned char *input, unsigned int in_len,
 				error("chunk length is longer than allocated");
 				goto exit_2;
 			}
-			fill(inp, chunksize);
+			fill(inp, chunksize, userptr);
 		}
 #if defined(__XEN__) || defined(__MINIOS__)
 		if (out_len >= uncomp_chunksize) {
@@ -133,7 +134,7 @@ int __init unlz4(unsigned char *input, unsigned int in_len,
 		}
 
 		ret = -1;
-		if (flush && flush(outp, dest_len) != dest_len)
+		if (flush && flush(outp, dest_len, userptr) != dest_len)
 			goto exit_2;
 		if (output)
 			outp += dest_len;
xen/common/unlzma.c +18 -12
index 6cd99023ad..d5dbc44881 100644
@@ -59,7 +59,7 @@ static long long __init read_int(unsigned char *ptr, int size)
 #define LZMA_IOBUF_SIZE	0x10000
 
 struct rc {
-	int (*fill)(void*, unsigned int);
+	int (*fill)(void*, unsigned int, void*);
 	uint8_t *ptr;
 	uint8_t *buffer;
 	uint8_t *buffer_end;
@@ -68,6 +68,7 @@ struct rc {
 	uint32_t range;
 	uint32_t bound;
 	void (*error)(const char *);
+	void *userptr;
 };
 
 
@@ -76,7 +77,7 @@ struct rc {
 #define RC_MODEL_TOTAL_BITS 11
 
 
-static int __init cf_check nofill(void *buffer, unsigned int len)
+static int __init cf_check nofill(void *buffer, unsigned int len, void *userptr)
 {
 	return -1;
 }
@@ -84,7 +85,7 @@ static int __init cf_check nofill(void *buffer, unsigned int len)
 /* Called twice: once at startup and once in rc_normalize() */
 static void __init rc_read(struct rc *rc)
 {
-	rc->buffer_size = rc->fill((char *)rc->buffer, LZMA_IOBUF_SIZE);
+	rc->buffer_size = rc->fill((char *)rc->buffer, LZMA_IOBUF_SIZE, rc->userptr);
 	if (rc->buffer_size <= 0)
 		rc->error("unexpected EOF");
 	rc->ptr = rc->buffer;
@@ -93,8 +94,9 @@ static void __init rc_read(struct rc *rc)
 
 /* Called once */
 static inline void __init rc_init(struct rc *rc,
-				  int (*fill)(void*, unsigned int),
-				  unsigned char *buffer, int buffer_size)
+				  int (*fill)(void*, unsigned int, void*),
+				  unsigned char *buffer, int buffer_size,
+				  void *userptr)
 {
 	if (fill)
 		rc->fill = fill;
@@ -104,6 +106,7 @@ static inline void __init rc_init(struct rc *rc,
 	rc->buffer_size = buffer_size;
 	rc->buffer_end = rc->buffer + rc->buffer_size;
 	rc->ptr = rc->buffer;
+	rc->userptr = userptr;
 
 	rc->code = 0;
 	rc->range = 0xFFFFFFFF;
@@ -274,8 +277,9 @@ struct writer {
 	size_t buffer_pos;
 	int bufsize;
 	size_t global_pos;
-	int(*flush)(void*, unsigned int);
+	int(*flush)(void*, unsigned int, void*);
 	struct lzma_header *header;
+	void *userptr;
 };
 
 struct cstate {
@@ -313,7 +317,7 @@ static inline int __init write_byte(struct writer *wr, uint8_t byte)
 	if (wr->flush && wr->buffer_pos == wr->header->dict_size) {
 		wr->buffer_pos = 0;
 		wr->global_pos += wr->header->dict_size;
-		if (wr->flush((char *)wr->buffer, wr->header->dict_size)
+		if (wr->flush((char *)wr->buffer, wr->header->dict_size, wr->userptr)
 				!= wr->header->dict_size)
 			return -1;
 	}
@@ -529,10 +533,11 @@ static inline int __init process_bit1(struct writer *wr, struct rc *rc,
 
 
 int __init unlzma(unsigned char *buf, unsigned int in_len,
-		  int(*fill)(void*, unsigned int),
-		  int(*flush)(void*, unsigned int),
+		  int(*fill)(void*, unsigned int, void*),
+		  int(*flush)(void*, unsigned int, void*),
 		  unsigned char *output, unsigned int *posp,
-		  void(*error)(const char *x))
+		  void(*error)(const char *x),
+		  void *userptr)
 {
 	struct lzma_header header;
 	int lc, pb, lp;
@@ -566,8 +571,9 @@ int __init unlzma(unsigned char *buf, unsigned int in_len,
 	wr.global_pos = 0;
 	wr.previous_byte = 0;
 	wr.buffer_pos = 0;
+	wr.userptr = userptr;
 
-	rc_init(&rc, fill, inbuf, in_len);
+	rc_init(&rc, fill, inbuf, in_len, userptr);
 
 	for (i = 0; i < sizeof(header); i++) {
 		if (rc.ptr >= rc.buffer_end)
@@ -644,7 +650,7 @@ int __init unlzma(unsigned char *buf, unsigned int in_len,
 
 	if (posp)
 		*posp = rc.ptr-rc.buffer;
-	if (!wr.flush || wr.flush(wr.buffer, wr.buffer_pos) == wr.buffer_pos)
+	if (!wr.flush || wr.flush(wr.buffer, wr.buffer_pos, wr.userptr) == wr.buffer_pos)
 		ret = 0;
 exit_3:
 	large_free(p);
xen/common/unlzo.c +7 -6
index 74056778eb..8908790425 100644
@@ -115,10 +115,11 @@ static int __init parse_header(u8 *input, int *skip, int in_len)
 }
 
 int __init unlzo(unsigned char *input, unsigned int in_len,
-		 int (*fill) (void *, unsigned int),
-		 int (*flush) (void *, unsigned int),
+		 int (*fill) (void *, unsigned int, void*),
+		 int (*flush) (void *, unsigned int, void*),
 		 unsigned char *output, unsigned int *posp,
-		 void (*error) (const char *x))
+		 void (*error) (const char *x),
+		 void *userptr)
 {
 	u8 r = 0;
 	int skip = 0;
@@ -161,7 +162,7 @@ int __init unlzo(unsigned char *input, unsigned int in_len,
 		*posp = 0;
 
 	if (fill)
-		fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
+		fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE), userptr);
 
 	if (!parse_header(input, &skip, in_len)) {
 		error("invalid header");
@@ -227,7 +228,7 @@ int __init unlzo(unsigned char *input, unsigned int in_len,
 			}
 		}
 
-		if (flush && flush(out_buf, dst_len) != dst_len)
+		if (flush && flush(out_buf, dst_len, userptr) != dst_len)
 			goto exit_2;
 		if (output)
 			out_buf += dst_len;
@@ -235,7 +236,7 @@ int __init unlzo(unsigned char *input, unsigned int in_len,
 			*posp += src_len + 12;
 		if (fill) {
 			in_buf = in_buf_save;
-			fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
+			fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE), userptr);
 		} else {
 			in_buf += src_len;
 			in_len -= src_len;
xen/common/unxz.c +6 -5
index 17aead0adf..6f005170d3 100644
@@ -158,10 +158,11 @@
  * fill() and flush() won't be used.
  */
 int __init unxz(unsigned char *in, unsigned int in_size,
-		int (*fill)(void *dest, unsigned int size),
-		int (*flush)(void *src, unsigned int size),
+		int (*fill)(void *dest, unsigned int size, void *userptr),
+		int (*flush)(void *src, unsigned int size, void *userptr),
 		unsigned char *out, unsigned int *in_used,
-		void (*error)(const char *x))
+		void (*error)(const char *x),
+		void *userptr)
 {
 	struct xz_buf b;
 	struct xz_dec *s;
@@ -213,7 +214,7 @@ int __init unxz(unsigned char *in, unsigned int in_size,
 
 				b.in_pos = 0;
 
-				in_size = fill(in, XZ_IOBUF_SIZE);
+				in_size = fill(in, XZ_IOBUF_SIZE, userptr);
 				if ((int) in_size < 0) {
 					/*
 					 * This isn't an optimal error code
@@ -236,7 +237,7 @@ int __init unxz(unsigned char *in, unsigned int in_size,
 				 * returned by xz_dec_run(), but probably
 				 * it's not too bad.
 				 */
-				if (flush(b.out, b.out_pos) != (int)b.out_pos)
+				if (flush(b.out, b.out_pos, userptr) != (int)b.out_pos)
 					ret = XZ_BUF_ERROR;
 
 				b.out_pos = 0;
xen/common/unzstd.c +7 -6
index 47073dd3e3..09e8fdef04 100644
@@ -143,10 +143,11 @@ out:
 }
 
 int __init unzstd(unsigned char *in_buf, unsigned int in_len,
-		  int (*fill)(void*, unsigned int),
-		  int (*flush)(void*, unsigned int),
+		  int (*fill)(void*, unsigned int, void*),
+		  int (*flush)(void*, unsigned int, void*),
 		  unsigned char *out_buf, unsigned int *in_pos,
-		  void (*error)(const char *x))
+		  void (*error)(const char *x),
+		  void *userptr)
 {
 	ZSTD_inBuffer in;
 	ZSTD_outBuffer out;
@@ -190,7 +191,7 @@ int __init unzstd(unsigned char *in_buf, unsigned int in_len,
 	}
 	/* Read the first chunk, since we need to decode the frame header. */
 	if (fill != NULL)
-		in_len = fill(in_buf, ZSTD_IOBUF_SIZE);
+		in_len = fill(in_buf, ZSTD_IOBUF_SIZE, userptr);
 	if ((int)in_len < 0) {
 		error("ZSTD-compressed data is truncated");
 		err = -1;
@@ -267,7 +268,7 @@ int __init unzstd(unsigned char *in_buf, unsigned int in_len,
 		if (in.pos == in.size) {
 			if (in_pos != NULL)
 				*in_pos += in.pos;
-			in_len = fill ? fill(in_buf, ZSTD_IOBUF_SIZE) : -1;
+			in_len = fill ? fill(in_buf, ZSTD_IOBUF_SIZE, userptr) : -1;
 			if ((int)in_len < 0) {
 				error("ZSTD-compressed data is truncated");
 				err = -1;
@@ -283,7 +284,7 @@ int __init unzstd(unsigned char *in_buf, unsigned int in_len,
 			goto out;
 		/* Flush all of the data produced if using flush(). */
 		if (flush != NULL && out.pos > 0) {
-			if (out.pos != flush(out.dst, out.pos)) {
+			if (out.pos != flush(out.dst, out.pos, userptr)) {
 				error("Failed to flush()");
 				err = -1;
 				goto out;
xen/include/xen/decompress.h +7 -3
index f5bc17f2b6..804dbca963 100644
@@ -2,10 +2,11 @@
 #define __XEN_GENERIC_H
 
 typedef int decompress_fn(unsigned char *inbuf, unsigned int len,
-                          int (*fill)(void*, unsigned int),
-                          int (*flush)(void*, unsigned int),
+                          int (*fill)(void*, unsigned int, void*),
+                          int (*flush)(void*, unsigned int, void*),
                           unsigned char *outbuf, unsigned int *posp,
-                          void (*error)(const char *x));
+                          void (*error)(const char *x),
+                          void *userptr);
 
 /* inbuf   - input buffer
  * len     - len of pre-read data in inbuf
@@ -15,6 +16,7 @@ typedef int decompress_fn(unsigned char *inbuf, unsigned int len,
  * posp    - if non-null, input position (number of bytes read) will be
  *           returned here
  * error   - error reporting function
+ * userptr - user pointer to pass to flush and fill
  *
  * If len != 0, inbuf should contain all the necessary input data, and fill
  * should be NULL
@@ -29,6 +31,8 @@ typedef int decompress_fn(unsigned char *inbuf, unsigned int len,
  * decompressor (outbuf = NULL), and the flush function will be called to
  * flush the output buffer at the appropriate time (decompressor and stream
  * dependent).
+ *
+ * Flush and fill's third argument is a user pointer for book keeping purposes.
  */
 
 decompress_fn bunzip2, unxz, unlzma, unlzo, unlz4, unzstd;
Signature · 1 line
2.40.0
#2 lore raw

PATCH 2/2 x86/Dom0: Use streaming decompression for ZSTD compressed kernels

In reply to a message not in the archive

On Arch Linux kernel decompression will fail when Xen has been unified with the kernel and initramfs as a single binary. This change works for both streaming and non-streaming ZSTD content.

---
 xen/common/decompress.c | 37 +++++++++++++++++++++++++++++++------
 1 file changed, 31 insertions(+), 6 deletions(-)
xen/common/decompress.c +31 -6
index 989336983f..cde754ffb1 100644
@@ -3,11 +3,26 @@
 #include <xen/string.h>
 #include <xen/decompress.h>
 
+typedef struct _ZSTD_state
+{
+    void *write_buf;
+    unsigned int write_pos;
+} ZSTD_state;
+
 static void __init cf_check error(const char *msg)
 {
     printk("%s\n", msg);
 }
 
+static int __init cf_check ZSTD_flush(void *buf, unsigned int pos,
+                                      void *userptr)
+{
+    ZSTD_state *state = (ZSTD_state*)userptr;
+    memcpy(state->write_buf + state->write_pos, buf, pos);
+    state->write_pos += pos;
+    return pos;
+}
+
 int __init decompress(void *inbuf, unsigned int len, void *outbuf)
 {
 #if 0 /* Not needed here yet. */
@@ -17,22 +32,32 @@ int __init decompress(void *inbuf, unsigned int len, void *outbuf)
 #endif
 
     if ( len >= 3 && !memcmp(inbuf, "\x42\x5a\x68", 3) )
-        return bunzip2(inbuf, len, NULL, NULL, outbuf, NULL, error);
+        return bunzip2(inbuf, len, NULL, NULL, outbuf, NULL, error, NULL);
 
     if ( len >= 6 && !memcmp(inbuf, "\3757zXZ", 6) )
-        return unxz(inbuf, len, NULL, NULL, outbuf, NULL, error);
+        return unxz(inbuf, len, NULL, NULL, outbuf, NULL, error, NULL);
 
     if ( len >= 2 && !memcmp(inbuf, "\135\000", 2) )
-        return unlzma(inbuf, len, NULL, NULL, outbuf, NULL, error);
+        return unlzma(inbuf, len, NULL, NULL, outbuf, NULL, error, NULL);
 
     if ( len >= 5 && !memcmp(inbuf, "\x89LZO", 5) )
-        return unlzo(inbuf, len, NULL, NULL, outbuf, NULL, error);
+        return unlzo(inbuf, len, NULL, NULL, outbuf, NULL, error, NULL);
 
     if ( len >= 2 && !memcmp(inbuf, "\x02\x21", 2) )
-	return unlz4(inbuf, len, NULL, NULL, outbuf, NULL, error);
+        return unlz4(inbuf, len, NULL, NULL, outbuf, NULL, error, NULL);
 
     if ( len >= 4 && !memcmp(inbuf, "\x28\xb5\x2f\xfd", 4) )
-	return unzstd(inbuf, len, NULL, NULL, outbuf, NULL, error);
+    {
+        // NOTE (Rafaël): On Arch Linux the kernel is compressed in a way
+        // that requires streaming ZSTD decompression. Otherwise decompression
+        // will fail when using a unified EFI binary. Somehow decompression
+        // works when not using a unified EFI binary, I suspect this is the
+        // kernel self decompressing. Or there is a code path that I am not
+        // aware of that takes care of the use case properly.
+
+        ZSTD_state state = (ZSTD_state){ outbuf, 0 };
+        return unzstd(inbuf, len, NULL, ZSTD_flush, NULL, NULL, error, &state);
+    }
 
     return 1;
 }
Signature · 1 line
2.40.0
#3 lore raw
Quoted 3 lines · Rafaël Kooi

On Arch Linux kernel decompression will fail when Xen has been unified with the kernel and initramfs as a single binary. This change works for both streaming and non-streaming ZSTD content.

This could do with better explaining what "unified" means, and how streaming decompression actually makes a difference.

Quoted 15 lines
xen/common/decompress.c +6 -0
@@ -3,11 +3,26 @@
 #include <xen/string.h>
 #include <xen/decompress.h>
 
+typedef struct _ZSTD_state
+{
+    void *write_buf;
+    unsigned int write_pos;
+} ZSTD_state;
+
 static void __init cf_check error(const char *msg)
 {
     printk("%s\n", msg);

xen/common/decompress.c line 24

Quoted 10 lines
xen/common/decompress.c +8 -0
 }
 
+static int __init cf_check ZSTD_flush(void *buf, unsigned int pos,
+                                      void *userptr)
+{
+    ZSTD_state *state = (ZSTD_state*)userptr;
+    memcpy(state->write_buf + state->write_pos, buf, pos);
+    state->write_pos += pos;
+    return pos;
+}

This doesn't really belong here, but will (I expect) go away anyway once you drop the earlier patch.

Quoted 12 lines
xen/common/decompress.c +2 -2
@@ -17,22 +32,32 @@ int __init decompress(void *inbuf, unsigned int len, void *outbuf)
 #endif
 
     if ( len >= 3 && !memcmp(inbuf, "\x42\x5a\x68", 3) )
-        return bunzip2(inbuf, len, NULL, NULL, outbuf, NULL, error);
+        return bunzip2(inbuf, len, NULL, NULL, outbuf, NULL, error, NULL);
 
     if ( len >= 6 && !memcmp(inbuf, "\3757zXZ", 6) )
-        return unxz(inbuf, len, NULL, NULL, outbuf, NULL, error);
+        return unxz(inbuf, len, NULL, NULL, outbuf, NULL, error, NULL);
 
     if ( len >= 2 && !memcmp(inbuf, "\135\000", 2) )

xen/common/decompress.c int __init decompress(void *inbuf, unsigned int len, void *outbuf) · line 47

Quoted 10 lines
xen/common/decompress.c +3 -3
-        return unlzma(inbuf, len, NULL, NULL, outbuf, NULL, error);
+        return unlzma(inbuf, len, NULL, NULL, outbuf, NULL, error, NULL);
 
     if ( len >= 5 && !memcmp(inbuf, "\x89LZO", 5) )
-        return unlzo(inbuf, len, NULL, NULL, outbuf, NULL, error);
+        return unlzo(inbuf, len, NULL, NULL, outbuf, NULL, error, NULL);
 
     if ( len >= 2 && !memcmp(inbuf, "\x02\x21", 2) )
-	return unlz4(inbuf, len, NULL, NULL, outbuf, NULL, error);
+        return unlz4(inbuf, len, NULL, NULL, outbuf, NULL, error, NULL);

This also looks wrong here - if the earlier patch was to be kept, I expect all these adjustments would have to move there. Otherwise build would be broken when just the 1st patch is in place.

xen/common/decompress.c int __init decompress(void *inbuf, unsigned int len, void *outbuf) · line 56

Quoted 9 lines
xen/common/decompress.c +7 -1
     if ( len >= 4 && !memcmp(inbuf, "\x28\xb5\x2f\xfd", 4) )
-	return unzstd(inbuf, len, NULL, NULL, outbuf, NULL, error);
+    {
+        // NOTE (Rafaël): On Arch Linux the kernel is compressed in a way
+        // that requires streaming ZSTD decompression. Otherwise decompression
+        // will fail when using a unified EFI binary. Somehow decompression
+        // works when not using a unified EFI binary, I suspect this is the
+        // kernel self decompressing. Or there is a code path that I am not
+        // aware of that takes care of the use case properly.

Along the lines of what I've said for the description, this wants to avoid terms like "somehow" if at all possible.

We also don't normally put our names in such comments.

Finally please see ./CODING_STYLE for how we expect comments to be formatted.

Jan

A message that is not in the archive (sent off-list or not yet received): DU0P192MB1700F7BB44DC06D67D7AE345E3779@DU0P192MB1700.EURP192.PROD.OUTLOOK.COM

#5 lore raw

First of all - please don't drop Cc-s when replying. I'm restoring xen-devel@ here at least.

Quoted 11 lines · Rafaël Kooi
Quoted 7 lines · Rafaël Kooi
Quoted 3 lines

On Arch Linux kernel decompression will fail when Xen has been unified with the kernel and initramfs as a single binary. This change works for both streaming and non-streaming ZSTD content.

This could do with better explaining what "unified" means, and how streaming decompression actually makes a difference.

I don't mind explaining it further, but with the efi documentation for it existing on xenbits, should I just refer to that?

You may of course refer to existing documentation. Iirc that doesn't cover any compression aspects, though.

Quoted 30 lines
Quoted 17 lines
Quoted 17 lines
xen/common/decompress.c +6 -0
@@ -3,11 +3,26 @@
  #include <xen/string.h>
  #include <xen/decompress.h>
 
+typedef struct _ZSTD_state
+{
+    void *write_buf;
+    unsigned int write_pos;
+} ZSTD_state;
+
  static void __init cf_check error(const char *msg)
  {
      printk("%s\n", msg);
  }
 
Quoted 12 lines

xen/common/decompress.c line 24

Quoted 8 lines
xen/common/decompress.c +8 -0
+static int __init cf_check ZSTD_flush(void *buf, unsigned int pos,
+                                      void *userptr)
+{
+    ZSTD_state *state = (ZSTD_state*)userptr;
+    memcpy(state->write_buf + state->write_pos, buf, pos);
+    state->write_pos += pos;
+    return pos;
+}

This doesn't really belong here, but will (I expect) go away anyway once you drop the earlier patch.

Quoted 3 lines

The ZSTD_flush will have to stay, as that is how the decompressor will start streaming decompression. The difference will be that the book keeping will be "global" (to the translation unit).

But this bookkeeping should be entirely in zstd code (i.e. presumably unzstd.c).

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
xen/common/decompress.c +2 -1
      if ( len >= 4 && !memcmp(inbuf, "\x28\xb5\x2f\xfd", 4) )
-	return unzstd(inbuf, len, NULL, NULL, outbuf, NULL, error);
+    {
+        // NOTE (Rafaël): On Arch Linux the kernel is compressed in a way
Quoted 8 lines

xen/common/decompress.c int __init decompress(void *inbuf, unsigned int len, void *outbuf) · line 56

Quoted 5 lines
xen/common/decompress.c +5 -0
+        // that requires streaming ZSTD decompression. Otherwise decompression
+        // will fail when using a unified EFI binary. Somehow decompression
+        // works when not using a unified EFI binary, I suspect this is the
+        // kernel self decompressing. Or there is a code path that I am not
+        // aware of that takes care of the use case properly.

Along the lines of what I've said for the description, this wants to avoid terms like "somehow" if at all possible.

Quoted 4 lines

I've used the term "somehow" because I don't know why decompression works when Xen loads the kernel from the EFI file system. I assume the kernel still gets unpacked by Xen, right? Or does the kernel unpack itself?

The handling of Dom0 kernel decompression ought to be entirely independent of EFI vs legacy. Unless I'm wrong with that (mis-remembering), you mentioning EFI is potentially misleading. And yes, at least on x86 the kernel is decompressed by Xen (by peeking into the supplied bzImage). The difference between a plain bzImage and a "unified EFI binary" is what you will want to outline in the description (and at least mention in the comment). What I'm wondering is whether there simply is an issue with size determination when the kernel is taken from the .kernel section.

Quoted 2 lines

When I present the v2 of this patch, do I add you as a reviewer? Or will that be done by the merger?

I'm afraid I don't understand the question. You will continue to Cc respective maintainers, which will include me. In case you refer to a
Reviewed-by: tag - you can only add such tags once they were offered to you by the respective person. For this specific one it doesn't mean "an earlier version of this was looked at by <person>" but "this is deemed okay by <person>".

Jan

#6 lore raw

On 10/05/2023 11:48, Jan Beulich wrote:> First of all - please don't drop Cc-s when replying. I'm restoring

Quoted 2 lines

xen-devel@ here at least.

Apologies, I didn't notice that replying dropped the Cc-s. Should I send the emails again with the proper Cc-s?

Quoted 10 lines · Rafaël Kooi
Quoted 9 lines · Rafaël Kooi
Quoted 7 lines
Quoted 3 lines

On Arch Linux kernel decompression will fail when Xen has been unified with the kernel and initramfs as a single binary. This change works for both streaming and non-streaming ZSTD content.

This could do with better explaining what "unified" means, and how streaming decompression actually makes a difference.

Quoted 6 lines
Quoted 2 lines

I don't mind explaining it further, but with the efi documentation for it existing on xenbits, should I just refer to that?

You may of course refer to existing documentation. Iirc that doesn't cover any compression aspects, though.

Right, I'll think about what ends up being the clearest explanation.

Quoted 30 lines
Quoted 30 lines
Quoted 29 lines
Quoted 15 lines
xen/common/decompress.c +6 -0
@@ -3,11 +3,26 @@
   #include <xen/string.h>
   #include <xen/decompress.h>
 
+typedef struct _ZSTD_state
+{
+    void *write_buf;
+    unsigned int write_pos;
+} ZSTD_state;
+
   static void __init cf_check error(const char *msg)
   {
       printk("%s\n", msg);

xen/common/decompress.c line 24

Quoted 10 lines
xen/common/decompress.c +8 -0
   }
 
+static int __init cf_check ZSTD_flush(void *buf, unsigned int pos,
+                                      void *userptr)
+{
+    ZSTD_state *state = (ZSTD_state*)userptr;
+    memcpy(state->write_buf + state->write_pos, buf, pos);
+    state->write_pos += pos;
+    return pos;
+}

This doesn't really belong here, but will (I expect) go away anyway once you drop the earlier patch.

Quoted 7 lines
Quoted 3 lines

The ZSTD_flush will have to stay, as that is how the decompressor will start streaming decompression. The difference will be that the book keeping will be "global" (to the translation unit).

But this bookkeeping should be entirely in zstd code (i.e. presumably unzstd.c).

The implementation of the decompression functions seem to indicate otherwise. Referring to unzstd.c:unzstd, the function will take the streaming decompression path if either fill or flush have been supplied. I cross checked with unlzma.c and unxz.c, and that seems to have similar behavior in regards to flushing the output data. The flush function is passed a buffer to a chunk of decompressed data with pos being the size of the chunk. For the sake of consistency I don't think it's a good idea to deviate from this behavior in just unzstd.c.

I could wrap the decompression code in another file and function, but in my opinion it should stay here and be renamed to something generic like stream_flush or chunk_flush.

Quoted 18 lines
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
xen/common/decompress.c +2 -1
       if ( len >= 4 && !memcmp(inbuf, "\x28\xb5\x2f\xfd", 4) )
-	return unzstd(inbuf, len, NULL, NULL, outbuf, NULL, error);
+    {
+        // NOTE (Rafaël): On Arch Linux the kernel is compressed in a way
Quoted 8 lines

xen/common/decompress.c int __init decompress(void *inbuf, unsigned int len, void *outbuf) · line 56

Quoted 5 lines
xen/common/decompress.c +5 -0
+        // that requires streaming ZSTD decompression. Otherwise decompression
+        // will fail when using a unified EFI binary. Somehow decompression
+        // works when not using a unified EFI binary, I suspect this is the
+        // kernel self decompressing. Or there is a code path that I am not
+        // aware of that takes care of the use case properly.

Along the lines of what I've said for the description, this wants to avoid terms like "somehow" if at all possible.

Quoted 4 lines

I've used the term "somehow" because I don't know why decompression works when Xen loads the kernel from the EFI file system. I assume the kernel still gets unpacked by Xen, right? Or does the kernel unpack itself?

Quoted 9 lines

The handling of Dom0 kernel decompression ought to be entirely independent of EFI vs legacy. Unless I'm wrong with that (mis-remembering), you mentioning EFI is potentially misleading. And yes, at least on x86 the kernel is decompressed by Xen (by peeking into the supplied bzImage). The difference between a plain bzImage and a "unified EFI binary" is what you will want to outline in the description (and at least mention in the comment). What I'm wondering is whether there simply is an issue with size determination when the kernel is taken from the .kernel section.

Assuming you are talking about size determination of the compressed bzImage, I notice a discrepancy in the size of the ZSTD stream and the reported size by the vmlinuz-* header upon further investigation. When using the streaming decompression code I made it output how many bytes it reads from the extracted-but-still-compressed bzImage. The code reads 12,327,560 bytes, but the size of the compressed bzImage in the header is 12,327,564 bytes. In xen/arch/x86/bzImage.c decompress is called with orig_image_len, when the function output_length calculates the end address and removes 4 bytes from that address. If I remove the last 4 bytes from the compressed bzImage then unzstd will unpack it with unzstd bzImage.zst -o bzImage, otherwise it will complain with zstd: /*stdin*\: unknown header. With this new information I think the correct solution is to try calling decompress a second time with with orig_image_len - 4 if it fails.

Quoted 11 lines
Quoted 2 lines

When I present the v2 of this patch, do I add you as a reviewer? Or will that be done by the merger?

I'm afraid I don't understand the question. You will continue to Cc respective maintainers, which will include me. In case you refer to a
Reviewed-by: tag - you can only add such tags once they were offered to you by the respective person. For this specific one it doesn't mean "an earlier version of this was looked at by <person>" but "this is deemed okay by <person>".

Jan

I meant the "Reviewed-by:" tag indeed. Thanks again.

Rafaël

#7 lore raw
Quoted 41 lines · Rafaël Kooi
Quoted 32 lines · Jan Beulich
Quoted 31 lines · Rafaël Kooi
Quoted 29 lines · Rafaël Kooi
Quoted 15 lines
xen/common/decompress.c +6 -0
@@ -3,11 +3,26 @@
   #include <xen/string.h>
   #include <xen/decompress.h>
 
+typedef struct _ZSTD_state
+{
+    void *write_buf;
+    unsigned int write_pos;
+} ZSTD_state;
+
   static void __init cf_check error(const char *msg)
   {
       printk("%s\n", msg);

xen/common/decompress.c line 24

Quoted 10 lines
xen/common/decompress.c +8 -0
   }
 
+static int __init cf_check ZSTD_flush(void *buf, unsigned int pos,
+                                      void *userptr)
+{
+    ZSTD_state *state = (ZSTD_state*)userptr;
+    memcpy(state->write_buf + state->write_pos, buf, pos);
+    state->write_pos += pos;
+    return pos;
+}

This doesn't really belong here, but will (I expect) go away anyway once you drop the earlier patch.

Quoted 7 lines
Quoted 3 lines

The ZSTD_flush will have to stay, as that is how the decompressor will start streaming decompression. The difference will be that the book keeping will be "global" (to the translation unit).

But this bookkeeping should be entirely in zstd code (i.e. presumably unzstd.c).

Quoted 8 lines

The implementation of the decompression functions seem to indicate otherwise. Referring to unzstd.c:unzstd, the function will take the streaming decompression path if either fill or flush have been supplied. I cross checked with unlzma.c and unxz.c, and that seems to have similar behavior in regards to flushing the output data. The flush function is passed a buffer to a chunk of decompressed data with pos being the size of the chunk. For the sake of consistency I don't think it's a good idea to deviate from this behavior in just unzstd.c.

Well, so far we don't use any flush functions, do we? The question could therefore also be put differently: In how far is the flush function you introduce zstd-specific? If it isn't be other than the fact that it is only passed to unzstd(), perhaps it shouldn't be named as if zstd-specific?

Quoted 28 lines
Quoted 18 lines
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
xen/common/decompress.c +2 -1
       if ( len >= 4 && !memcmp(inbuf, "\x28\xb5\x2f\xfd", 4) )
-	return unzstd(inbuf, len, NULL, NULL, outbuf, NULL, error);
+    {
+        // NOTE (Rafaël): On Arch Linux the kernel is compressed in a way
Quoted 8 lines

xen/common/decompress.c int __init decompress(void *inbuf, unsigned int len, void *outbuf) · line 56

Quoted 5 lines
xen/common/decompress.c +5 -0
+        // that requires streaming ZSTD decompression. Otherwise decompression
+        // will fail when using a unified EFI binary. Somehow decompression
+        // works when not using a unified EFI binary, I suspect this is the
+        // kernel self decompressing. Or there is a code path that I am not
+        // aware of that takes care of the use case properly.

Along the lines of what I've said for the description, this wants to avoid terms like "somehow" if at all possible.

Quoted 4 lines

I've used the term "somehow" because I don't know why decompression works when Xen loads the kernel from the EFI file system. I assume the kernel still gets unpacked by Xen, right? Or does the kernel unpack itself?

Quoted 9 lines

The handling of Dom0 kernel decompression ought to be entirely independent of EFI vs legacy. Unless I'm wrong with that (mis-remembering), you mentioning EFI is potentially misleading. And yes, at least on x86 the kernel is decompressed by Xen (by peeking into the supplied bzImage). The difference between a plain bzImage and a "unified EFI binary" is what you will want to outline in the description (and at least mention in the comment). What I'm wondering is whether there simply is an issue with size determination when the kernel is taken from the .kernel section.

Quoted 14 lines

Assuming you are talking about size determination of the compressed bzImage, I notice a discrepancy in the size of the ZSTD stream and the reported size by the vmlinuz-* header upon further investigation. When using the streaming decompression code I made it output how many bytes it reads from the extracted-but-still-compressed bzImage. The code reads 12,327,560 bytes, but the size of the compressed bzImage in the header is 12,327,564 bytes. In xen/arch/x86/bzImage.c decompress is called with orig_image_len, when the function output_length calculates the end address and removes 4 bytes from that address. If I remove the last 4 bytes from the compressed bzImage then unzstd will unpack it with unzstd bzImage.zst -o bzImage, otherwise it will complain with zstd: /*stdin*\: unknown header. With this new information I think the correct solution is to try calling decompress a second time with with orig_image_len - 4 if it fails.

That's not very likely to be an appropriate solution. If the sizes diverge by 4, that difference needs explaining. Once understood, it'll hopefully become clear under what conditions (if any) to adjust the length right away, without any need to retry.

Jan