Skip to contentSkip to conversation

Load compressed modules with compression-less kmod

PATCH In review Emil Velikov 2 participants 36 messages Feb 12, 2024 – Apr 30, 2024

lore
Share & tools
lore
#0 lore raw

cover Load compressed modules with compression-less kmod

While I was digging into a dkms bug, I've noticed that kmod does not fallback to the normal init_module() code path when the in-kernel decompression fails.

As I looked closer, I realised that the current module compression detection relies on kmod being build with zstd, xz, zlib ... Even in the case where we rely on the kernel module itself to decompress - aka we don't use the external libraries all together.

By building kmod without zstd and friends, we shave some space off the initrd which helps with boot time.

Majority of this series handles the latter and fixes a few related bugs along the way. The last commits is somewhat questionable commit and addresses the former issue.

Happy to hear your feedback,
Emil

---
Emil Velikov (13):
libkmod: use a dup()'d fd for zlib
libkmod: keep gzFile gzf local to load_zlib()
libkmod: remove kmod_file::{zstd,xz}_used flags
libkmod: clear file->memory if map fails
libkmod: nuke struct file_ops
libkmod: propagate {zstd,xz,zlib}_load errors
libkmod: move kmod_file_load_contents as applicable
libkmod: always detect the module compression
libkmod: swap alloca usage for a few assert_cc
libkmod: tidy-up kmod_file_open()
libkmod: move load_reg() further up
libkmod: keep KMOD_FILE_COMPRESSION_NONE/load_reg in comp_types
libkmod: always fallback to do_init_module()

 libkmod/libkmod-file.c     | 206 ++++++++++++++++++---------------------------
 libkmod/libkmod-internal.h |   2 +-
 libkmod/libkmod-module.c   |   8 +-
 3 files changed, 90 insertions(+), 126 deletions(-)

---
base-commit: b29704cd448aaa455dba4e656fc0f0d3c686df3f
change-id: 20240212-decompression-fixes-56371eeb707d

Best regards,

Signature · 1 line
Emil Velikov <[email protected]>
#1 lore raw

PATCH 1/13 libkmod: use a dup()'d fd for zlib

The gzdopen() API used, takes ownership of the fd. To make that more explicit we clear it (-1) as applicable.

Yet again, kmod has explicit API to return the fd to the user - which currently is used solely when uncompressed, so we're safe.

Regardless - simply duplicate the fd locally and use that with zlib.

---
 libkmod/libkmod-file.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
libkmod/libkmod-file.c +12 -7
index b138e7e..b97062b 100644
@@ -317,12 +317,18 @@ static int load_zlib(struct kmod_file *file)
 	int err = 0;
 	off_t did = 0, total = 0;
 	_cleanup_free_ unsigned char *p = NULL;
+	int gzfd;
 
 	errno = 0;
-	file->gzf = gzdopen(file->fd, "rb");
-	if (file->gzf == NULL)
+	gzfd = fcntl(file->fd, F_DUPFD_CLOEXEC, 3);
+	if (gzfd < 0)
+		return -errno;
+
+	file->gzf = gzdopen(gzfd, "rb"); /* takes ownership of the fd */
+	if (file->gzf == NULL) {
+		close(gzfd);
 		return -errno;
-	file->fd = -1; /* now owned by gzf due gzdopen() */
+	}
 
 	for (;;) {
 		int r;
@@ -359,7 +365,7 @@ static int load_zlib(struct kmod_file *file)
 	return 0;
 
 error:
-	gzclose(file->gzf);
+	gzclose(file->gzf); /* closes the gzfd */
 	return err;
 }
 
@@ -368,7 +374,7 @@ static void unload_zlib(struct kmod_file *file)
 	if (file->gzf == NULL)
 		return;
 	free(file->memory);
-	gzclose(file->gzf); /* closes file->fd */
+	gzclose(file->gzf);
 }
 
 static const char magic_zlib[] = {0x1f, 0x8b};
@@ -535,7 +541,6 @@ void kmod_file_unref(struct kmod_file *file)
 	if (file->memory)
 		file->ops->unload(file);
 
-	if (file->fd >= 0)
-		close(file->fd);
+	close(file->fd);
 	free(file);
 }
Signature · 1 line
2.43.0
Reviewed-by on v1 1/13
#2 lore raw

Re: [PATCH kmod 01/13] libkmod: use a dup()'d fd for zlib

Quoted 11 lines · On Mon, Feb 12, 2024 at 05:23:02PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

The gzdopen() API used, takes ownership of the fd. To make that more explicit we clear it (-1) as applicable.

Yet again, kmod has explicit API to return the fd to the user - which currently is used solely when uncompressed, so we're safe.

Regardless - simply duplicate the fd locally and use that with zlib.

Lucas De Marchi

Quoted 61 lines

---
libkmod/libkmod-file.c | 19 ++++++++++++-------

1 file changed, 12 insertions(+), 7 deletions(-)
libkmod/libkmod-file.c +9 -3
index b138e7e..b97062b 100644
@@ -317,12 +317,18 @@ static int load_zlib(struct kmod_file *file)
 	int err = 0;
 	off_t did = 0, total = 0;
 	_cleanup_free_ unsigned char *p = NULL;
+	int gzfd;
 
 	errno = 0;
-	file->gzf = gzdopen(file->fd, "rb");
-	if (file->gzf == NULL)
+	gzfd = fcntl(file->fd, F_DUPFD_CLOEXEC, 3);
+	if (gzfd < 0)
+		return -errno;
+
+	file->gzf = gzdopen(gzfd, "rb"); /* takes ownership of the fd */
+	if (file->gzf == NULL) {
+		close(gzfd);
 		return -errno;
-	file->fd = -1; /* now owned by gzf due gzdopen() */
+	}
 
 	for (;;) {
 		int r;
@@ -359,7 +365,7 @@ static int load_zlib(struct kmod_file *file)
 	return 0;
 

error:

libkmod/libkmod-file.c +1 -1
-	gzclose(file->gzf);
+	gzclose(file->gzf); /* closes the gzfd */
	return err;
}
libkmod/libkmod-file.c +2 -3
@@ -368,7 +374,7 @@ static void unload_zlib(struct kmod_file *file)
 	if (file->gzf == NULL)
 		return;
 	free(file->memory);
-	gzclose(file->gzf); /* closes file->fd */
+	gzclose(file->gzf);
 }
 
 static const char magic_zlib[] = {0x1f, 0x8b};
@@ -535,7 +541,6 @@ void kmod_file_unref(struct kmod_file *file)
 	if (file->memory)
 		file->ops->unload(file);
 
-	if (file->fd >= 0)
-		close(file->fd);
+	close(file->fd);
 	free(file);
 }
Signature · 2 lines
-- 
2.43.0
#3 lore raw

PATCH 2/13 libkmod: keep gzFile gzf local to load_zlib()

In reply to Emil Velikov

There is no need to keep the root gzFile context open for the whole duration. Once we've copied the decompressed module to file->memory we can close the handle.

---
 libkmod/libkmod-file.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
libkmod/libkmod-file.c +7 -11
index b97062b..9a014ea 100644
@@ -53,9 +53,6 @@ struct kmod_file {
 #endif
 #ifdef ENABLE_XZ
 	bool xz_used;
-#endif
-#ifdef ENABLE_ZLIB
-	gzFile gzf;
 #endif
 	int fd;
 	enum kmod_file_compression_type compression;
@@ -317,6 +314,7 @@ static int load_zlib(struct kmod_file *file)
 	int err = 0;
 	off_t did = 0, total = 0;
 	_cleanup_free_ unsigned char *p = NULL;
+	gzFile gzf;
 	int gzfd;
 
 	errno = 0;
@@ -324,8 +322,8 @@ static int load_zlib(struct kmod_file *file)
 	if (gzfd < 0)
 		return -errno;
 
-	file->gzf = gzdopen(gzfd, "rb"); /* takes ownership of the fd */
-	if (file->gzf == NULL) {
+	gzf = gzdopen(gzfd, "rb"); /* takes ownership of the fd */
+	if (gzf == NULL) {
 		close(gzfd);
 		return -errno;
 	}
@@ -343,12 +341,12 @@ static int load_zlib(struct kmod_file *file)
 			p = tmp;
 		}
 
-		r = gzread(file->gzf, p + did, total - did);
+		r = gzread(gzf, p + did, total - did);
 		if (r == 0)
 			break;
 		else if (r < 0) {
 			int gzerr;
-			const char *gz_errmsg = gzerror(file->gzf, &gzerr);
+			const char *gz_errmsg = gzerror(gzf, &gzerr);
 
 			ERR(file->ctx, "gzip: %s\n", gz_errmsg);
 
@@ -362,19 +360,17 @@ static int load_zlib(struct kmod_file *file)
 	file->memory = p;
 	file->size = did;
 	p = NULL;
+	gzclose(gzf);
 	return 0;
 
 error:
-	gzclose(file->gzf); /* closes the gzfd */
+	gzclose(gzf); /* closes the gzfd */
 	return err;
 }
 
 static void unload_zlib(struct kmod_file *file)
 {
-	if (file->gzf == NULL)
-		return;
 	free(file->memory);
-	gzclose(file->gzf);
 }
 
 static const char magic_zlib[] = {0x1f, 0x8b};
Signature · 1 line
2.43.0
Reviewed-by on v1 2/13
#4 lore raw

Re: [PATCH kmod 02/13] libkmod: keep gzFile gzf local to load_zlib()

Quoted 7 lines · On Mon, Feb 12, 2024 at 05:23:03PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

There is no need to keep the root gzFile context open for the whole duration. Once we've copied the decompressed module to file->memory we can close the handle.

thanks
Lucas De Marchi

Quoted 78 lines

---
libkmod/libkmod-file.c | 18 +++++++-----------

1 file changed, 7 insertions(+), 11 deletions(-)
libkmod/libkmod-file.c +6 -7
index b97062b..9a014ea 100644
@@ -53,9 +53,6 @@ struct kmod_file {
 #endif
 #ifdef ENABLE_XZ
 	bool xz_used;
-#endif
-#ifdef ENABLE_ZLIB
-	gzFile gzf;
 #endif
 	int fd;
 	enum kmod_file_compression_type compression;
@@ -317,6 +314,7 @@ static int load_zlib(struct kmod_file *file)
 	int err = 0;
 	off_t did = 0, total = 0;
 	_cleanup_free_ unsigned char *p = NULL;
+	gzFile gzf;
 	int gzfd;
 
 	errno = 0;
@@ -324,8 +322,8 @@ static int load_zlib(struct kmod_file *file)
 	if (gzfd < 0)
 		return -errno;
 
-	file->gzf = gzdopen(gzfd, "rb"); /* takes ownership of the fd */
-	if (file->gzf == NULL) {
+	gzf = gzdopen(gzfd, "rb"); /* takes ownership of the fd */
+	if (gzf == NULL) {
 		close(gzfd);
 		return -errno;
 	}
@@ -343,12 +341,12 @@ static int load_zlib(struct kmod_file *file)
 			p = tmp;
 		}
 
-		r = gzread(file->gzf, p + did, total - did);
+		r = gzread(gzf, p + did, total - did);
 		if (r == 0)
 			break;
 		else if (r < 0) {
 			int gzerr;
-			const char *gz_errmsg = gzerror(file->gzf, &gzerr);
+			const char *gz_errmsg = gzerror(gzf, &gzerr);
 
 			ERR(file->ctx, "gzip: %s\n", gz_errmsg);
 
@@ -362,19 +360,17 @@ static int load_zlib(struct kmod_file *file)
 	file->memory = p;
 	file->size = did;
 	p = NULL;
+	gzclose(gzf);
 	return 0;
 

error:

libkmod/libkmod-file.c +1 -1
-	gzclose(file->gzf); /* closes the gzfd */
+	gzclose(gzf); /* closes the gzfd */
	return err;
}

static void unload_zlib(struct kmod_file *file)
{

libkmod/libkmod-file.c +0 -2
-	if (file->gzf == NULL)
-		return;
	free(file->memory);
libkmod/libkmod-file.c +0 -1
-	gzclose(file->gzf);

}

static const char magic_zlib[] = {0x1f, 0x8b};

Signature · 1 line
2.43.0
#5 lore raw

PATCH 3/13 libkmod: remove kmod_file::{zstd,xz}_used flags

In reply to Emil Velikov

These are used to protect a free(file->memory), within their respective unload functions. Where the sole caller of the unload function already does a NULL check prior.

Even so, free(NULL) is guaranteed to be safe by the standard.

---
 libkmod/libkmod-file.c | 12 ------------
 1 file changed, 12 deletions(-)
libkmod/libkmod-file.c +0 -12
index 9a014ea..abd4723 100644
@@ -48,12 +48,6 @@ struct file_ops {
 };
 
 struct kmod_file {
-#ifdef ENABLE_ZSTD
-	bool zstd_used;
-#endif
-#ifdef ENABLE_XZ
-	bool xz_used;
-#endif
 	int fd;
 	enum kmod_file_compression_type compression;
 	off_t size;
@@ -176,7 +170,6 @@ static int load_zstd(struct kmod_file *file)
 
 	ZSTD_freeDStream(dstr);
 	free((void *)zst_inb.src);
-	file->zstd_used = true;
 	file->memory = zst_outb.dst;
 	file->size = zst_outb.pos;
 	return 0;
@@ -190,8 +183,6 @@ out:
 
 static void unload_zstd(struct kmod_file *file)
 {
-	if (!file->zstd_used)
-		return;
 	free(file->memory);
 }
 
@@ -269,7 +260,6 @@ static int xz_uncompress(lzma_stream *strm, struct kmod_file *file)
 			goto out;
 		}
 	}
-	file->xz_used = true;
 	file->memory = p;
 	file->size = total;
 	return 0;
@@ -299,8 +289,6 @@ static int load_xz(struct kmod_file *file)
 
 static void unload_xz(struct kmod_file *file)
 {
-	if (!file->xz_used)
-		return;
 	free(file->memory);
 }
 
Signature · 1 line
2.43.0
Reviewed-by on v1 3/13
#6 lore raw

Re: [PATCH kmod 03/13] libkmod: remove kmod_file::{zstd,xz}_used flags

Quoted 9 lines · On Mon, Feb 12, 2024 at 05:23:04PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

These are used to protect a free(file->memory), within their respective unload functions. Where the sole caller of the unload function already does a NULL check prior.

Even so, free(NULL) is guaranteed to be safe by the standard.

thanks
Lucas De Marchi

Quoted 59 lines

---
libkmod/libkmod-file.c | 12 ------------

1 file changed, 12 deletions(-)
libkmod/libkmod-file.c +0 -12
index 9a014ea..abd4723 100644
@@ -48,12 +48,6 @@ struct file_ops {
 };
 
 struct kmod_file {
-#ifdef ENABLE_ZSTD
-	bool zstd_used;
-#endif
-#ifdef ENABLE_XZ
-	bool xz_used;
-#endif
 	int fd;
 	enum kmod_file_compression_type compression;
 	off_t size;
@@ -176,7 +170,6 @@ static int load_zstd(struct kmod_file *file)
 
 	ZSTD_freeDStream(dstr);
 	free((void *)zst_inb.src);
-	file->zstd_used = true;
 	file->memory = zst_outb.dst;
 	file->size = zst_outb.pos;
 	return 0;
@@ -190,8 +183,6 @@ out:
 
 static void unload_zstd(struct kmod_file *file)
 {
-	if (!file->zstd_used)
-		return;
 	free(file->memory);
 }
 
@@ -269,7 +260,6 @@ static int xz_uncompress(lzma_stream *strm, struct kmod_file *file)
 			goto out;
 		}
 	}
-	file->xz_used = true;
 	file->memory = p;
 	file->size = total;
 	return 0;
@@ -299,8 +289,6 @@ static int load_xz(struct kmod_file *file)
 
 static void unload_xz(struct kmod_file *file)
 {
-	if (!file->xz_used)
-		return;
 	free(file->memory);
 }
 
Signature · 2 lines
-- 
2.43.0
#7 lore raw

PATCH 4/13 libkmod: clear file->memory if map fails

In reply to Emil Velikov

On mmap failure file->memory is set to -1, which we'll happily pass down to munmap later on.

More importantly, since we do a NULL check in kmod_file_load_contents() we will exit the function without (re)attempting the load again.

Since we ignore the return code for the load function(s), one can end up calling kmod_elf_get_memory() and feed that -1 into init_module.

---
 libkmod/libkmod-file.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libkmod/libkmod-file.c +3 -1
index abd4723..b408aed 100644
@@ -392,8 +392,10 @@ static int load_reg(struct kmod_file *file)
 	file->size = st.st_size;
 	file->memory = mmap(NULL, file->size, PROT_READ, MAP_PRIVATE,
 			    file->fd, 0);
-	if (file->memory == MAP_FAILED)
+	if (file->memory == MAP_FAILED) {
+		file->memory = NULL;
 		return -errno;
+	}
 
 	return 0;
 }
Signature · 1 line
2.43.0
Reviewed-by on v1 4/13
#8 lore raw

Re: [PATCH kmod 04/13] libkmod: clear file->memory if map fails

Quoted 5 lines · On Mon, Feb 12, 2024 at 05:23:05PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

On mmap failure file->memory is set to -1, which we'll happily pass down to munmap later on.

Quoted 7 lines

More importantly, since we do a NULL check in kmod_file_load_contents() we will exit the function without (re)attempting the load again.

Since we ignore the return code for the load function(s), one can end up calling kmod_elf_get_memory() and feed that -1 into init_module.

Lucas De Marchi

Quoted 24 lines

---
libkmod/libkmod-file.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
libkmod/libkmod-file.c +3 -1
index abd4723..b408aed 100644
@@ -392,8 +392,10 @@ static int load_reg(struct kmod_file *file)
 	file->size = st.st_size;
 	file->memory = mmap(NULL, file->size, PROT_READ, MAP_PRIVATE,
 			    file->fd, 0);
-	if (file->memory == MAP_FAILED)
+	if (file->memory == MAP_FAILED) {
+		file->memory = NULL;
 		return -errno;
+	}
 
 	return 0;
 }
Signature · 2 lines
-- 
2.43.0
#9 lore raw

PATCH 5/13 libkmod: nuke struct file_ops

In reply to Emil Velikov

With the previous commits, we removed the need for a distinct unload callback.

So nuke the struct all together and only use/keep the load one around.

---
 libkmod/libkmod-file.c | 62 +++++++++++++++-----------------------------------
 1 file changed, 18 insertions(+), 44 deletions(-)
libkmod/libkmod-file.c +18 -44
index b408aed..8a0336f 100644
@@ -41,18 +41,12 @@
 #include "libkmod.h"
 #include "libkmod-internal.h"
 
-struct kmod_file;
-struct file_ops {
-	int (*load)(struct kmod_file *file);
-	void (*unload)(struct kmod_file *file);
-};
-
 struct kmod_file {
 	int fd;
 	enum kmod_file_compression_type compression;
 	off_t size;
 	void *memory;
-	const struct file_ops *ops;
+	int (*load)(struct kmod_file *file);
 	const struct kmod_ctx *ctx;
 	struct kmod_elf *elf;
 };
@@ -181,11 +175,6 @@ out:
 	return ret;
 }
 
-static void unload_zstd(struct kmod_file *file)
-{
-	free(file->memory);
-}
-
 static const char magic_zstd[] = {0x28, 0xB5, 0x2F, 0xFD};
 #endif
 
@@ -287,11 +276,6 @@ static int load_xz(struct kmod_file *file)
 	return ret;
 }
 
-static void unload_xz(struct kmod_file *file)
-{
-	free(file->memory);
-}
-
 static const char magic_xz[] = {0xfd, '7', 'z', 'X', 'Z', 0};
 #endif
 
@@ -356,11 +340,6 @@ error:
 	return err;
 }
 
-static void unload_zlib(struct kmod_file *file)
-{
-	free(file->memory);
-}
-
 static const char magic_zlib[] = {0x1f, 0x8b};
 #endif
 
@@ -368,18 +347,18 @@ static const struct comp_type {
 	size_t magic_size;
 	enum kmod_file_compression_type compression;
 	const char *magic_bytes;
-	const struct file_ops ops;
+	int (*load)(struct kmod_file *file);
 } comp_types[] = {
 #ifdef ENABLE_ZSTD
-	{sizeof(magic_zstd),	KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, {load_zstd, unload_zstd}},
+	{sizeof(magic_zstd),	KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, load_zstd},
 #endif
 #ifdef ENABLE_XZ
-	{sizeof(magic_xz),	KMOD_FILE_COMPRESSION_XZ, magic_xz, {load_xz, unload_xz}},
+	{sizeof(magic_xz),	KMOD_FILE_COMPRESSION_XZ, magic_xz, load_xz},
 #endif
 #ifdef ENABLE_ZLIB
-	{sizeof(magic_zlib),	KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, {load_zlib, unload_zlib}},
+	{sizeof(magic_zlib),	KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, load_zlib},
 #endif
-	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, {NULL, NULL}}
+	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, NULL}
 };
 
 static int load_reg(struct kmod_file *file)
@@ -400,15 +379,6 @@ static int load_reg(struct kmod_file *file)
 	return 0;
 }
 
-static void unload_reg(struct kmod_file *file)
-{
-	munmap(file->memory, file->size);
-}
-
-static const struct file_ops reg_ops = {
-	load_reg, unload_reg
-};
-
 struct kmod_elf *kmod_file_get_elf(struct kmod_file *file)
 {
 	if (file->elf)
@@ -436,7 +406,7 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 		goto error;
 	}
 
-	for (itr = comp_types; itr->ops.load != NULL; itr++) {
+	for (itr = comp_types; itr->load != NULL; itr++) {
 		if (magic_size_max < itr->magic_size)
 			magic_size_max = itr->magic_size;
 	}
@@ -459,17 +429,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 			goto error;
 		}
 
-		for (itr = comp_types; itr->ops.load != NULL; itr++) {
+		for (itr = comp_types; itr->load != NULL; itr++) {
 			if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
-				file->ops = &itr->ops;
+				file->load = itr->load;
 				file->compression = itr->compression;
 				break;
 			}
 		}
 	}
 
-	if (file->ops == NULL) {
-		file->ops = &reg_ops;
+	if (file->load == NULL) {
+		file->load = load_reg;
 		file->compression = KMOD_FILE_COMPRESSION_NONE;
 	}
 
@@ -496,7 +466,7 @@ void kmod_file_load_contents(struct kmod_file *file)
 		return;
 
 	/*  The load functions already log possible errors. */
-	file->ops->load(file);
+	file->load(file);
 }
 
 void *kmod_file_get_contents(const struct kmod_file *file)
@@ -524,8 +494,12 @@ void kmod_file_unref(struct kmod_file *file)
 	if (file->elf)
 		kmod_elf_unref(file->elf);
 
-	if (file->memory)
-		file->ops->unload(file);
+	if (file->compression == KMOD_FILE_COMPRESSION_NONE) {
+		if (file->memory)
+			munmap(file->memory, file->size);
+	} else {
+		free(file->memory);
+	}
 
 	close(file->fd);
 	free(file);
Signature · 1 line
2.43.0
Reviewed-by on v1 5/13
#10 lore raw

Re: [PATCH kmod 05/13] libkmod: nuke struct file_ops

Quoted 9 lines · On Mon, Feb 12, 2024 at 05:23:06PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

With the previous commits, we removed the need for a distinct unload callback.

So nuke the struct all together and only use/keep the load one around.

---

thanks
Lucas De Marchi

Quoted 162 lines

libkmod/libkmod-file.c | 62 +++++++++++++++-----------------------------------

1 file changed, 18 insertions(+), 44 deletions(-)
libkmod/libkmod-file.c +18 -44
index b408aed..8a0336f 100644
@@ -41,18 +41,12 @@
 #include "libkmod.h"
 #include "libkmod-internal.h"
 
-struct kmod_file;
-struct file_ops {
-	int (*load)(struct kmod_file *file);
-	void (*unload)(struct kmod_file *file);
-};
-
 struct kmod_file {
 	int fd;
 	enum kmod_file_compression_type compression;
 	off_t size;
 	void *memory;
-	const struct file_ops *ops;
+	int (*load)(struct kmod_file *file);
 	const struct kmod_ctx *ctx;
 	struct kmod_elf *elf;
 };
@@ -181,11 +175,6 @@ out:
 	return ret;
 }
 
-static void unload_zstd(struct kmod_file *file)
-{
-	free(file->memory);
-}
-
 static const char magic_zstd[] = {0x28, 0xB5, 0x2F, 0xFD};
 #endif
 
@@ -287,11 +276,6 @@ static int load_xz(struct kmod_file *file)
 	return ret;
 }
 
-static void unload_xz(struct kmod_file *file)
-{
-	free(file->memory);
-}
-
 static const char magic_xz[] = {0xfd, '7', 'z', 'X', 'Z', 0};
 #endif
 
@@ -356,11 +340,6 @@ error:
 	return err;
 }
 
-static void unload_zlib(struct kmod_file *file)
-{
-	free(file->memory);
-}
-
 static const char magic_zlib[] = {0x1f, 0x8b};
 #endif
 
@@ -368,18 +347,18 @@ static const struct comp_type {
 	size_t magic_size;
 	enum kmod_file_compression_type compression;
 	const char *magic_bytes;
-	const struct file_ops ops;
+	int (*load)(struct kmod_file *file);
 } comp_types[] = {
 #ifdef ENABLE_ZSTD
-	{sizeof(magic_zstd),	KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, {load_zstd, unload_zstd}},
+	{sizeof(magic_zstd),	KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, load_zstd},
 #endif
 #ifdef ENABLE_XZ
-	{sizeof(magic_xz),	KMOD_FILE_COMPRESSION_XZ, magic_xz, {load_xz, unload_xz}},
+	{sizeof(magic_xz),	KMOD_FILE_COMPRESSION_XZ, magic_xz, load_xz},
 #endif
 #ifdef ENABLE_ZLIB
-	{sizeof(magic_zlib),	KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, {load_zlib, unload_zlib}},
+	{sizeof(magic_zlib),	KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, load_zlib},
 #endif
-	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, {NULL, NULL}}
+	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, NULL}
 };
 
 static int load_reg(struct kmod_file *file)
@@ -400,15 +379,6 @@ static int load_reg(struct kmod_file *file)
 	return 0;
 }
 
-static void unload_reg(struct kmod_file *file)
-{
-	munmap(file->memory, file->size);
-}
-
-static const struct file_ops reg_ops = {
-	load_reg, unload_reg
-};
-
 struct kmod_elf *kmod_file_get_elf(struct kmod_file *file)
 {
 	if (file->elf)
@@ -436,7 +406,7 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 		goto error;
 	}
 
-	for (itr = comp_types; itr->ops.load != NULL; itr++) {
+	for (itr = comp_types; itr->load != NULL; itr++) {
 		if (magic_size_max < itr->magic_size)
 			magic_size_max = itr->magic_size;
 	}
@@ -459,17 +429,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 			goto error;
 		}
 
-		for (itr = comp_types; itr->ops.load != NULL; itr++) {
+		for (itr = comp_types; itr->load != NULL; itr++) {
 			if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
-				file->ops = &itr->ops;
+				file->load = itr->load;
 				file->compression = itr->compression;
 				break;
 			}
 		}
 	}
 
-	if (file->ops == NULL) {
-		file->ops = &reg_ops;
+	if (file->load == NULL) {
+		file->load = load_reg;
 		file->compression = KMOD_FILE_COMPRESSION_NONE;
 	}
 
@@ -496,7 +466,7 @@ void kmod_file_load_contents(struct kmod_file *file)
 		return;
 
 	/*  The load functions already log possible errors. */
-	file->ops->load(file);
+	file->load(file);
 }
 
 void *kmod_file_get_contents(const struct kmod_file *file)
@@ -524,8 +494,12 @@ void kmod_file_unref(struct kmod_file *file)
 	if (file->elf)
 		kmod_elf_unref(file->elf);
 
-	if (file->memory)
-		file->ops->unload(file);
+	if (file->compression == KMOD_FILE_COMPRESSION_NONE) {
+		if (file->memory)
+			munmap(file->memory, file->size);
+	} else {
+		free(file->memory);
+	}
 
 	close(file->fd);
 	free(file);
Signature · 2 lines
-- 
2.43.0
#11 lore raw

PATCH 6/13 libkmod: propagate {zstd,xz,zlib}_load errors

In reply to Emil Velikov

Propagate any errors during decompression further up the call stack. Without this we could easily pass NULL as mem to init_module(2).

---
 libkmod/libkmod-file.c     | 15 +++++++++++----
 libkmod/libkmod-internal.h |  2 +-
 libkmod/libkmod-module.c   |  4 +++-
 3 files changed, 15 insertions(+), 6 deletions(-)
libkmod/libkmod-file.c +11 -4
index 8a0336f..3a79464 100644
@@ -381,10 +381,17 @@ static int load_reg(struct kmod_file *file)
 
 struct kmod_elf *kmod_file_get_elf(struct kmod_file *file)
 {
+	int err;
+
 	if (file->elf)
 		return file->elf;
 
-	kmod_file_load_contents(file);
+	err = kmod_file_load_contents(file);
+	if (err) {
+		errno = err;
+		return NULL;
+	}
+
 	file->elf = kmod_elf_new(file->memory, file->size);
 	return file->elf;
 }
@@ -460,13 +467,13 @@ error:
 /*
  *  Callers should just check file->memory got updated
  */
-void kmod_file_load_contents(struct kmod_file *file)
+int kmod_file_load_contents(struct kmod_file *file)
 {
 	if (file->memory)
-		return;
+		return 0;
 
 	/*  The load functions already log possible errors. */
-	file->load(file);
+	return file->load(file);
 }
 
 void *kmod_file_get_contents(const struct kmod_file *file)
libkmod/libkmod-internal.h +1 -1
index 26a7e28..3bc6e11 100644
@@ -160,7 +160,7 @@ bool kmod_module_is_builtin(struct kmod_module *mod) __attribute__((nonnull(1)))
 /* libkmod-file.c */
 struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, const char *filename) _must_check_ __attribute__((nonnull(1,2)));
 struct kmod_elf *kmod_file_get_elf(struct kmod_file *file) __attribute__((nonnull(1)));
-void kmod_file_load_contents(struct kmod_file *file) __attribute__((nonnull(1)));
+int kmod_file_load_contents(struct kmod_file *file) __attribute__((nonnull(1)));
 void *kmod_file_get_contents(const struct kmod_file *file) _must_check_ __attribute__((nonnull(1)));
 off_t kmod_file_get_size(const struct kmod_file *file) _must_check_ __attribute__((nonnull(1)));
 enum kmod_file_compression_type kmod_file_get_compression(const struct kmod_file *file) _must_check_ __attribute__((nonnull(1)));
libkmod/libkmod-module.c +3 -1
index 585da41..1e43482 100644
@@ -903,7 +903,9 @@ static int do_init_module(struct kmod_module *mod, unsigned int flags,
 	off_t size;
 	int err;
 
-	kmod_file_load_contents(mod->file);
+	err = kmod_file_load_contents(mod->file);
+	if (err)
+		return err;
 
 	if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
 		elf = kmod_file_get_elf(mod->file);
Signature · 1 line
2.43.0
Reviewed-by on v1 6/13
#12 lore raw

Re: [PATCH kmod 06/13] libkmod: propagate {zstd,xz,zlib}_load errors

Quoted 6 lines · On Mon, Feb 12, 2024 at 05:23:07PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

Propagate any errors during decompression further up the call stack. Without this we could easily pass NULL as mem to init_module(2).

thanks
Lucas De Marchi

Quoted 78 lines

---

libkmod/libkmod-file.c     | 15 +++++++++++----
libkmod/libkmod-internal.h |  2 +-
libkmod/libkmod-module.c   |  4 +++-
3 files changed, 15 insertions(+), 6 deletions(-)
libkmod/libkmod-file.c +11 -4
index 8a0336f..3a79464 100644
@@ -381,10 +381,17 @@ static int load_reg(struct kmod_file *file)
 
 struct kmod_elf *kmod_file_get_elf(struct kmod_file *file)
 {
+	int err;
+
 	if (file->elf)
 		return file->elf;
 
-	kmod_file_load_contents(file);
+	err = kmod_file_load_contents(file);
+	if (err) {
+		errno = err;
+		return NULL;
+	}
+
 	file->elf = kmod_elf_new(file->memory, file->size);
 	return file->elf;
 }
@@ -460,13 +467,13 @@ error:
 /*
 *  Callers should just check file->memory got updated
 */
-void kmod_file_load_contents(struct kmod_file *file)
+int kmod_file_load_contents(struct kmod_file *file)
 {
 	if (file->memory)
-		return;
+		return 0;
 
 	/*  The load functions already log possible errors. */
-	file->load(file);
+	return file->load(file);
 }
 
 void *kmod_file_get_contents(const struct kmod_file *file)
libkmod/libkmod-internal.h +1 -1
index 26a7e28..3bc6e11 100644
@@ -160,7 +160,7 @@ bool kmod_module_is_builtin(struct kmod_module *mod) __attribute__((nonnull(1)))
 /* libkmod-file.c */
 struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, const char *filename) _must_check_ __attribute__((nonnull(1,2)));
 struct kmod_elf *kmod_file_get_elf(struct kmod_file *file) __attribute__((nonnull(1)));
-void kmod_file_load_contents(struct kmod_file *file) __attribute__((nonnull(1)));
+int kmod_file_load_contents(struct kmod_file *file) __attribute__((nonnull(1)));
 void *kmod_file_get_contents(const struct kmod_file *file) _must_check_ __attribute__((nonnull(1)));
 off_t kmod_file_get_size(const struct kmod_file *file) _must_check_ __attribute__((nonnull(1)));
 enum kmod_file_compression_type kmod_file_get_compression(const struct kmod_file *file) _must_check_ __attribute__((nonnull(1)));
libkmod/libkmod-module.c +3 -1
index 585da41..1e43482 100644
@@ -903,7 +903,9 @@ static int do_init_module(struct kmod_module *mod, unsigned int flags,
 	off_t size;
 	int err;
 
-	kmod_file_load_contents(mod->file);
+	err = kmod_file_load_contents(mod->file);
+	if (err)
+		return err;
 
 	if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
 		elf = kmod_file_get_elf(mod->file);
Signature · 2 lines
-- 
2.43.0
#13 lore raw

PATCH 7/13 libkmod: move kmod_file_load_contents as applicable

In reply to Emil Velikov

When dealing with an elf, we don't know or care about loading the file. The kmod_elf subsystem/API will deal with the required parts itself.

Which in this case, already calls kmod_file_load_contents() as applicable.

---
 libkmod/libkmod-module.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
libkmod/libkmod-module.c +4 -4
index 1e43482..d309948 100644
@@ -903,10 +903,6 @@ static int do_init_module(struct kmod_module *mod, unsigned int flags,
 	off_t size;
 	int err;
 
-	err = kmod_file_load_contents(mod->file);
-	if (err)
-		return err;
-
 	if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
 		elf = kmod_file_get_elf(mod->file);
 		if (elf == NULL) {
@@ -928,6 +924,10 @@ static int do_init_module(struct kmod_module *mod, unsigned int flags,
 
 		mem = kmod_elf_get_memory(elf);
 	} else {
+		err = kmod_file_load_contents(mod->file);
+		if (err)
+			return err;
+
 		mem = kmod_file_get_contents(mod->file);
 	}
 	size = kmod_file_get_size(mod->file);
Signature · 1 line
2.43.0
Reviewed-by on v1 7/13
#14 lore raw

Re: [PATCH kmod 07/13] libkmod: move kmod_file_load_contents as applicable

Quoted 9 lines · On Mon, Feb 12, 2024 at 05:23:08PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

When dealing with an elf, we don't know or care about loading the file. The kmod_elf subsystem/API will deal with the required parts itself.

Which in this case, already calls kmod_file_load_contents() as applicable.

thanks
Lucas De Marchi

Quoted 34 lines

---
libkmod/libkmod-module.c | 8 ++++----

1 file changed, 4 insertions(+), 4 deletions(-)
libkmod/libkmod-module.c +4 -4
index 1e43482..d309948 100644
@@ -903,10 +903,6 @@ static int do_init_module(struct kmod_module *mod, unsigned int flags,
 	off_t size;
 	int err;
 
-	err = kmod_file_load_contents(mod->file);
-	if (err)
-		return err;
-
 	if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
 		elf = kmod_file_get_elf(mod->file);
 		if (elf == NULL) {
@@ -928,6 +924,10 @@ static int do_init_module(struct kmod_module *mod, unsigned int flags,
 
 		mem = kmod_elf_get_memory(elf);
 	} else {
+		err = kmod_file_load_contents(mod->file);
+		if (err)
+			return err;
+
 		mem = kmod_file_get_contents(mod->file);
 	}
 	size = kmod_file_get_size(mod->file);
Signature · 2 lines
-- 
2.43.0
#15 lore raw

PATCH 8/13 libkmod: always detect the module compression

In reply to Emil Velikov

Currently, when built w/o given compression we'll incorrectly report a "compression_none".

As we reach do_finit_module(), we'll naively assume that the kernel can handle the compressed module, yet omit the MODULE_INIT_COMPRESSED_FILE flag.

As result the kernel will barf at us, do_finit_module will fail with non -ENOSYS and we won't end in the do_init_module codepath (which will also fail).

In other words: with this change, you can build kmod without zstd, xz and zlib support and the kernel will load the modules, assuming it supports the format \o/

---
 libkmod/libkmod-file.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
libkmod/libkmod-file.c +18 -9
index 3a79464..b69f1ef 100644
@@ -174,9 +174,14 @@ out:
 	free((void *)zst_outb.dst);
 	return ret;
 }
+#else
+static int load_zstd(struct kmod_file *file)
+{
+	return -ENOSYS;
+}
+#endif
 
 static const char magic_zstd[] = {0x28, 0xB5, 0x2F, 0xFD};
-#endif
 
 #ifdef ENABLE_XZ
 static void xz_uncompress_belch(struct kmod_file *file, lzma_ret ret)
@@ -275,9 +280,14 @@ static int load_xz(struct kmod_file *file)
 	lzma_end(&strm);
 	return ret;
 }
+#else
+static int load_xz(struct kmod_file *file)
+{
+	return -ENOSYS;
+}
+#endif
 
 static const char magic_xz[] = {0xfd, '7', 'z', 'X', 'Z', 0};
-#endif
 
 #ifdef ENABLE_ZLIB
 #define READ_STEP (4 * 1024 * 1024)
@@ -339,9 +349,14 @@ error:
 	gzclose(gzf); /* closes the gzfd */
 	return err;
 }
+#else
+static int load_zlib(struct kmod_file *file)
+{
+	return -ENOSYS;
+}
+#endif
 
 static const char magic_zlib[] = {0x1f, 0x8b};
-#endif
 
 static const struct comp_type {
 	size_t magic_size;
@@ -349,15 +364,9 @@ static const struct comp_type {
 	const char *magic_bytes;
 	int (*load)(struct kmod_file *file);
 } comp_types[] = {
-#ifdef ENABLE_ZSTD
 	{sizeof(magic_zstd),	KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, load_zstd},
-#endif
-#ifdef ENABLE_XZ
 	{sizeof(magic_xz),	KMOD_FILE_COMPRESSION_XZ, magic_xz, load_xz},
-#endif
-#ifdef ENABLE_ZLIB
 	{sizeof(magic_zlib),	KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, load_zlib},
-#endif
 	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, NULL}
 };
 
Signature · 1 line
2.43.0
#16 lore raw

Re: [PATCH kmod 08/13] libkmod: always detect the module compression

Quoted 10 lines · On Mon, 12 Feb 2024 at 17:23, Emil Velikov via B4 Relay…

From: Emil Velikov <[email protected]>

Currently, when built w/o given compression we'll incorrectly report a "compression_none".

As we reach do_finit_module(), we'll naively assume that the kernel can handle the compressed module, yet omit the MODULE_INIT_COMPRESSED_FILE flag.

Quoted 8 lines

As result the kernel will barf at us, do_finit_module will fail with non -ENOSYS and we won't end in the do_init_module codepath (which will also fail).

In other words: with this change, you can build kmod without zstd, xz and zlib support and the kernel will load the modules, assuming it supports the format \o/

Important part to note here is that the above is only valid for insmod and modprobe.

Tools such as depmod and modinfo, still depend on kmod being built with e.g. zstd, in order for .ko.zstd modules to be considered valid entries.
I'm not 100% sure about modinfo, at a glance those tools require decompression support in order to parse the elfs and extract the required module information.

Regardless of this caveat I do see value in this series:
- it notably simplifies the codebase
- You can use different kmod on the build vs target system.

Namely: Yocto recipe with full blown kmod/depmod generates the mutable modules.xxx files, yet the target system uses compression-less kmod for insmod/modprobe/rmmod/lsmod.

HTH
Emil

Reviewed-by on v1 8/13
#17 lore raw

Re: [PATCH kmod 08/13] libkmod: always detect the module compression

In reply to Emil Velikov
Quoted 13 lines · On Mon, Feb 12, 2024 at 05:23:09PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

Currently, when built w/o given compression we'll incorrectly report a "compression_none".

As we reach do_finit_module(), we'll naively assume that the kernel can handle the compressed module, yet omit the MODULE_INIT_COMPRESSED_FILE flag.

As result the kernel will barf at us, do_finit_module will fail with non -ENOSYS and we won't end in the do_init_module codepath (which will also fail).

Quoted 5 lines

In other words: with this change, you can build kmod without zstd, xz and zlib support and the kernel will load the modules, assuming it supports the format \o/

nice.

Lucas De Marchi

Quoted 76 lines

---
libkmod/libkmod-file.c | 27 ++++++++++++++++++---------

1 file changed, 18 insertions(+), 9 deletions(-)
libkmod/libkmod-file.c +18 -9
index 3a79464..b69f1ef 100644
@@ -174,9 +174,14 @@ out:
 	free((void *)zst_outb.dst);
 	return ret;
 }
+#else
+static int load_zstd(struct kmod_file *file)
+{
+	return -ENOSYS;
+}
+#endif
 
 static const char magic_zstd[] = {0x28, 0xB5, 0x2F, 0xFD};
-#endif
 
 #ifdef ENABLE_XZ
 static void xz_uncompress_belch(struct kmod_file *file, lzma_ret ret)
@@ -275,9 +280,14 @@ static int load_xz(struct kmod_file *file)
 	lzma_end(&strm);
 	return ret;
 }
+#else
+static int load_xz(struct kmod_file *file)
+{
+	return -ENOSYS;
+}
+#endif
 
 static const char magic_xz[] = {0xfd, '7', 'z', 'X', 'Z', 0};
-#endif
 
 #ifdef ENABLE_ZLIB
 #define READ_STEP (4 * 1024 * 1024)
@@ -339,9 +349,14 @@ error:
 	gzclose(gzf); /* closes the gzfd */
 	return err;
 }
+#else
+static int load_zlib(struct kmod_file *file)
+{
+	return -ENOSYS;
+}
+#endif
 
 static const char magic_zlib[] = {0x1f, 0x8b};
-#endif
 
 static const struct comp_type {
 	size_t magic_size;
@@ -349,15 +364,9 @@ static const struct comp_type {
 	const char *magic_bytes;
 	int (*load)(struct kmod_file *file);
 } comp_types[] = {
-#ifdef ENABLE_ZSTD
 	{sizeof(magic_zstd),	KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, load_zstd},
-#endif
-#ifdef ENABLE_XZ
 	{sizeof(magic_xz),	KMOD_FILE_COMPRESSION_XZ, magic_xz, load_xz},
-#endif
-#ifdef ENABLE_ZLIB
 	{sizeof(magic_zlib),	KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, load_zlib},
-#endif
 	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, NULL}
 };
 
Signature · 2 lines
-- 
2.43.0
#18 lore raw

PATCH 9/13 libkmod: swap alloca usage for a few assert_cc

In reply to Emil Velikov

Since all the compression magic is always available now, we don't need to loop at runtime nor use alloca - latter of which comes with a handful of caveats.

Simply throw in a few assert_cc(), which will trigger at build-time.

---
 libkmod/libkmod-file.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)
libkmod/libkmod-file.c +8 -14
index b69f1ef..5b88d6c 100644
@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 {
 	struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
 	const struct comp_type *itr;
-	size_t magic_size_max = 0;
 	int err = 0;
 
 	if (file == NULL)
@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 		goto error;
 	}
 
-	for (itr = comp_types; itr->load != NULL; itr++) {
-		if (magic_size_max < itr->magic_size)
-			magic_size_max = itr->magic_size;
-	}
-
-	if (magic_size_max > 0) {
-		char *buf = alloca(magic_size_max + 1);
+	{
+		char buf[7];
 		ssize_t sz;
 
-		if (buf == NULL) {
-			err = -errno;
-			goto error;
-		}
-		sz = read_str_safe(file->fd, buf, magic_size_max + 1);
+		assert_cc(sizeof(magic_zstd) < sizeof(buf));
+		assert_cc(sizeof(magic_xz) < sizeof(buf));
+		assert_cc(sizeof(magic_zlib) < sizeof(buf));
+
+		sz = read_str_safe(file->fd, buf, sizeof(buf));
 		lseek(file->fd, 0, SEEK_SET);
-		if (sz != (ssize_t)magic_size_max) {
+		if (sz != (sizeof(buf) - 1)) {
 			if (sz < 0)
 				err = sz;
 			else
Signature · 1 line
2.43.0
Reviewed-by on v1 9/13
#19 lore raw

Re: [PATCH kmod 09/13] libkmod: swap alloca usage for a few assert_cc

Quoted 7 lines · On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

Since all the compression magic is always available now, we don't need to loop at runtime nor use alloca - latter of which comes with a handful of caveats.

Simply throw in a few assert_cc(), which will trigger at build-time.

given the small size, we actually never needed it and could have added the asserts.

But this gets rid of more, with the loop going away, and I'm happy with it.

thanks
Lucas De Marchi

#20 lore raw

Re: [PATCH kmod 09/13] libkmod: swap alloca usage for a few assert_cc

In reply to Emil Velikov
Quoted 38 lines · On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

Since all the compression magic is always available now, we don't need to loop at runtime nor use alloca - latter of which comes with a handful of caveats.

Simply throw in a few assert_cc(), which will trigger at build-time.

---
libkmod/libkmod-file.c | 22 ++++++++--------------

1 file changed, 8 insertions(+), 14 deletions(-)
libkmod/libkmod-file.c +2 -8
index b69f1ef..5b88d6c 100644
@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 {
 	struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
 	const struct comp_type *itr;
-	size_t magic_size_max = 0;
 	int err = 0;
 
 	if (file == NULL)
@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 		goto error;
 	}
 
-	for (itr = comp_types; itr->load != NULL; itr++) {
-		if (magic_size_max < itr->magic_size)
-			magic_size_max = itr->magic_size;
-	}
-
-	if (magic_size_max > 0) {
-		char *buf = alloca(magic_size_max + 1);
+	{
+		char buf[7];

libkmod/libkmod-file.c struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, · line 430

Quoted 10 lines
libkmod/libkmod-file.c +3 -5
 		ssize_t sz;
 
-		if (buf == NULL) {
-			err = -errno;
-			goto error;
-		}
-		sz = read_str_safe(file->fd, buf, magic_size_max + 1);
+		assert_cc(sizeof(magic_zstd) < sizeof(buf));
+		assert_cc(sizeof(magic_xz) < sizeof(buf));
+		assert_cc(sizeof(magic_zlib) < sizeof(buf));

../libkmod/libkmod-file.c: In function 'kmod_file_open':
../shared/macro.h:25:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]

   25 |         _Static_assert((expr), #expr)
      |         ^~~~~~~~~~~~~~
../libkmod/libkmod-file.c:424:9: note: in expansion of macro 'assert_cc'
  424 |         assert_cc(sizeof(magic_zstd) < sizeof(buf));
      |         ^~~~~~~~~

So I'd go with this on top of this patch... I can squash it as needed when applying:

|diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c
|index f162a10..8baf12d 100644
|--- a/libkmod/libkmod-file.c
|+++ b/libkmod/libkmod-file.c
|@@ -408,10 +408,15 @@ struct kmod_elf *kmod_file_get_elf(struct kmod_file *file)
| struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
| 						const char *filename)
| {
|-	struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
|+	struct kmod_file *file;
| 	char buf[7];
| 	ssize_t sz;
| |+	assert_cc(sizeof(magic_zstd) < sizeof(buf));
|+	assert_cc(sizeof(magic_xz) < sizeof(buf));
|+	assert_cc(sizeof(magic_zlib) < sizeof(buf));
|+
|+	file = calloc(1, sizeof(struct kmod_file));
| 	if (file == NULL)
| 		return NULL;
| |@@ -421,10 +426,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
| 		return NULL;
| 	}
| |-	assert_cc(sizeof(magic_zstd) < sizeof(buf));
|-	assert_cc(sizeof(magic_xz) < sizeof(buf));
|-	assert_cc(sizeof(magic_zlib) < sizeof(buf));
|-
| 	sz = read_str_safe(file->fd, buf, sizeof(buf));
| 	lseek(file->fd, 0, SEEK_SET);
| 	if (sz != (sizeof(buf) - 1)) {

Lucas De Marchi

#21 lore raw

Re: [PATCH kmod 09/13] libkmod: swap alloca usage for a few assert_cc

Quoted 42 lines · Lucas De Marchi
Quoted 40 lines · On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

Since all the compression magic is always available now, we don't need to loop at runtime nor use alloca - latter of which comes with a handful of caveats.

Simply throw in a few assert_cc(), which will trigger at build-time.

---
libkmod/libkmod-file.c | 22 ++++++++--------------

1 file changed, 8 insertions(+), 14 deletions(-)
libkmod/libkmod-file.c +2 -8
index b69f1ef..5b88d6c 100644
@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 {
      struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
      const struct comp_type *itr;
-      size_t magic_size_max = 0;
      int err = 0;
 
      if (file == NULL)
@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
              goto error;
      }
 
-      for (itr = comp_types; itr->load != NULL; itr++) {
-              if (magic_size_max < itr->magic_size)
-                      magic_size_max = itr->magic_size;
-      }
-
-      if (magic_size_max > 0) {
-              char *buf = alloca(magic_size_max + 1);
+      {
+              char buf[7];
              ssize_t sz;
 
Quoted 11 lines

libkmod/libkmod-file.c struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, · line 430

Quoted 8 lines
libkmod/libkmod-file.c +3 -5
-              if (buf == NULL) {
-                      err = -errno;
-                      goto error;
-              }
-              sz = read_str_safe(file->fd, buf, magic_size_max + 1);
+              assert_cc(sizeof(magic_zstd) < sizeof(buf));
+              assert_cc(sizeof(magic_xz) < sizeof(buf));
+              assert_cc(sizeof(magic_zlib) < sizeof(buf));

../libkmod/libkmod-file.c: In function 'kmod_file_open':
../shared/macro.h:25:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]

Is there a particular use-case for explicitly forcing C90?

The configure.ac contains AC_PROG_CC_C99, which seems reasonable IMHO. Plus the autogen.sh goes a step further with -std=gnu11

Thanks
Emil

#22 lore raw

Re: [PATCH kmod 09/13] libkmod: swap alloca usage for a few assert_cc

Quoted 55 lines · Emil Velikov
Quoted 42 lines · Lucas De Marchi
Quoted 40 lines · On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

Since all the compression magic is always available now, we don't need to loop at runtime nor use alloca - latter of which comes with a handful of caveats.

Simply throw in a few assert_cc(), which will trigger at build-time.

---
libkmod/libkmod-file.c | 22 ++++++++--------------

1 file changed, 8 insertions(+), 14 deletions(-)
libkmod/libkmod-file.c +2 -8
index b69f1ef..5b88d6c 100644
@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 {
      struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
      const struct comp_type *itr;
-      size_t magic_size_max = 0;
      int err = 0;
 
      if (file == NULL)
@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
              goto error;
      }
 
-      for (itr = comp_types; itr->load != NULL; itr++) {
-              if (magic_size_max < itr->magic_size)
-                      magic_size_max = itr->magic_size;
-      }
-
-      if (magic_size_max > 0) {
-              char *buf = alloca(magic_size_max + 1);
+      {
+              char buf[7];
              ssize_t sz;
 
Quoted 11 lines

libkmod/libkmod-file.c struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, · line 430

Quoted 8 lines
libkmod/libkmod-file.c +3 -5
-              if (buf == NULL) {
-                      err = -errno;
-                      goto error;
-              }
-              sz = read_str_safe(file->fd, buf, magic_size_max + 1);
+              assert_cc(sizeof(magic_zstd) < sizeof(buf));
+              assert_cc(sizeof(magic_xz) < sizeof(buf));
+              assert_cc(sizeof(magic_zlib) < sizeof(buf));

../libkmod/libkmod-file.c: In function 'kmod_file_open':
../shared/macro.h:25:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]

Quoted 1 line

Is there a particular use-case for explicitly forcing C90?

not forcing C90, but forcing -Wdeclaration-after-statement as per flag passed in configure.ac. I think the message given by gcc is misleading here.

Lucas De Marchi

Quoted 6 lines

The configure.ac contains AC_PROG_CC_C99, which seems reasonable IMHO. Plus the autogen.sh goes a step further with -std=gnu11

Thanks
Emil

#23 lore raw

Re: [PATCH kmod 09/13] libkmod: swap alloca usage for a few assert_cc

Quoted 57 lines · Lucas De Marchi
Quoted 55 lines · Emil Velikov
Quoted 53 lines · Lucas De Marchi
Quoted 38 lines · On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

Since all the compression magic is always available now, we don't need to loop at runtime nor use alloca - latter of which comes with a handful of caveats.

Simply throw in a few assert_cc(), which will trigger at build-time.

---
libkmod/libkmod-file.c | 22 ++++++++--------------

1 file changed, 8 insertions(+), 14 deletions(-)
libkmod/libkmod-file.c +2 -8
index b69f1ef..5b88d6c 100644
@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 {
      struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
      const struct comp_type *itr;
-      size_t magic_size_max = 0;
      int err = 0;
 
      if (file == NULL)
@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
              goto error;
      }
 
-      for (itr = comp_types; itr->load != NULL; itr++) {
-              if (magic_size_max < itr->magic_size)
-                      magic_size_max = itr->magic_size;
-      }
-
-      if (magic_size_max > 0) {
-              char *buf = alloca(magic_size_max + 1);
+      {
+              char buf[7];

libkmod/libkmod-file.c struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, · line 430

Quoted 10 lines
libkmod/libkmod-file.c +3 -5
              ssize_t sz;
 
-              if (buf == NULL) {
-                      err = -errno;
-                      goto error;
-              }
-              sz = read_str_safe(file->fd, buf, magic_size_max + 1);
+              assert_cc(sizeof(magic_zstd) < sizeof(buf));
+              assert_cc(sizeof(magic_xz) < sizeof(buf));
+              assert_cc(sizeof(magic_zlib) < sizeof(buf));

../libkmod/libkmod-file.c: In function 'kmod_file_open':
../shared/macro.h:25:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]

Quoted 6 lines
Quoted 1 line

Is there a particular use-case for explicitly forcing C90?

not forcing C90, but forcing -Wdeclaration-after-statement as per flag passed in configure.ac. I think the message given by gcc is misleading here.

Indeed thanks. Seems like I should add `export CFLAGS+=-Werror" to my dev box.

-Emil

#24 lore raw

Re: [PATCH kmod 09/13] libkmod: swap alloca usage for a few assert_cc

Quoted 58 lines · Emil Velikov
Quoted 57 lines · Lucas De Marchi
Quoted 55 lines · Emil Velikov
Quoted 53 lines · Lucas De Marchi
Quoted 38 lines · On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

Since all the compression magic is always available now, we don't need to loop at runtime nor use alloca - latter of which comes with a handful of caveats.

Simply throw in a few assert_cc(), which will trigger at build-time.

---
libkmod/libkmod-file.c | 22 ++++++++--------------

1 file changed, 8 insertions(+), 14 deletions(-)
libkmod/libkmod-file.c +2 -8
index b69f1ef..5b88d6c 100644
@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 {
      struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
      const struct comp_type *itr;
-      size_t magic_size_max = 0;
      int err = 0;
 
      if (file == NULL)
@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
              goto error;
      }
 
-      for (itr = comp_types; itr->load != NULL; itr++) {
-              if (magic_size_max < itr->magic_size)
-                      magic_size_max = itr->magic_size;
-      }
-
-      if (magic_size_max > 0) {
-              char *buf = alloca(magic_size_max + 1);
+      {
+              char buf[7];

libkmod/libkmod-file.c struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, · line 430

Quoted 10 lines
libkmod/libkmod-file.c +3 -5
              ssize_t sz;
 
-              if (buf == NULL) {
-                      err = -errno;
-                      goto error;
-              }
-              sz = read_str_safe(file->fd, buf, magic_size_max + 1);
+              assert_cc(sizeof(magic_zstd) < sizeof(buf));
+              assert_cc(sizeof(magic_xz) < sizeof(buf));
+              assert_cc(sizeof(magic_zlib) < sizeof(buf));

../libkmod/libkmod-file.c: In function 'kmod_file_open':
../shared/macro.h:25:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Is there a particular use-case for explicitly forcing C90?

not forcing C90, but forcing -Wdeclaration-after-statement as per flag passed in configure.ac. I think the message given by gcc is misleading here.

Indeed thanks. Seems like I should add `export CFLAGS+=-Werror" to my dev box.

so, are you ok that I just squash this?

Lucas De Marchi

Quoted 2 lines

-Emil

#25 lore raw

Re: [PATCH kmod 09/13] libkmod: swap alloca usage for a few assert_cc

Quoted 62 lines · Lucas De Marchi
Quoted 60 lines · Emil Velikov
Quoted 59 lines · Lucas De Marchi
Quoted 56 lines · Emil Velikov
Quoted 42 lines · Lucas De Marchi
Quoted 40 lines · On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

Since all the compression magic is always available now, we don't need to loop at runtime nor use alloca - latter of which comes with a handful of caveats.

Simply throw in a few assert_cc(), which will trigger at build-time.

---
libkmod/libkmod-file.c | 22 ++++++++--------------

1 file changed, 8 insertions(+), 14 deletions(-)
libkmod/libkmod-file.c +2 -8
index b69f1ef..5b88d6c 100644
@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 {
      struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
      const struct comp_type *itr;
-      size_t magic_size_max = 0;
      int err = 0;
 
      if (file == NULL)
@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
              goto error;
      }
 
-      for (itr = comp_types; itr->load != NULL; itr++) {
-              if (magic_size_max < itr->magic_size)
-                      magic_size_max = itr->magic_size;
-      }
-
-      if (magic_size_max > 0) {
-              char *buf = alloca(magic_size_max + 1);
+      {
+              char buf[7];
              ssize_t sz;
 
Quoted 11 lines

libkmod/libkmod-file.c struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, · line 430

Quoted 8 lines
libkmod/libkmod-file.c +3 -5
-              if (buf == NULL) {
-                      err = -errno;
-                      goto error;
-              }
-              sz = read_str_safe(file->fd, buf, magic_size_max + 1);
+              assert_cc(sizeof(magic_zstd) < sizeof(buf));
+              assert_cc(sizeof(magic_xz) < sizeof(buf));
+              assert_cc(sizeof(magic_zlib) < sizeof(buf));

../libkmod/libkmod-file.c: In function 'kmod_file_open':
../shared/macro.h:25:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]

Is there a particular use-case for explicitly forcing C90?

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

not forcing C90, but forcing -Wdeclaration-after-statement as per flag passed in configure.ac. I think the message given by gcc is misleading here.

Indeed thanks. Seems like I should add `export CFLAGS+=-Werror" to my dev box.

so, are you ok that I just squash this?

Yes, of course.

-Emil

#26 lore raw

Re: [PATCH kmod 09/13] libkmod: swap alloca usage for a few assert_cc

Quoted 68 lines · Emil Velikov
Quoted 67 lines · Lucas De Marchi
Quoted 65 lines · Emil Velikov
Quoted 63 lines · Lucas De Marchi
Quoted 55 lines · Emil Velikov
Quoted 42 lines · Lucas De Marchi
Quoted 40 lines · On Mon, Feb 12, 2024 at 05:23:10PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

Since all the compression magic is always available now, we don't need to loop at runtime nor use alloca - latter of which comes with a handful of caveats.

Simply throw in a few assert_cc(), which will trigger at build-time.

---
libkmod/libkmod-file.c | 22 ++++++++--------------

1 file changed, 8 insertions(+), 14 deletions(-)
libkmod/libkmod-file.c +2 -8
index b69f1ef..5b88d6c 100644
@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 {
      struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
      const struct comp_type *itr;
-      size_t magic_size_max = 0;
      int err = 0;
 
      if (file == NULL)
@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
              goto error;
      }
 
-      for (itr = comp_types; itr->load != NULL; itr++) {
-              if (magic_size_max < itr->magic_size)
-                      magic_size_max = itr->magic_size;
-      }
-
-      if (magic_size_max > 0) {
-              char *buf = alloca(magic_size_max + 1);
+      {
+              char buf[7];
              ssize_t sz;
 
Quoted 11 lines

libkmod/libkmod-file.c struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, · line 430

Quoted 8 lines
libkmod/libkmod-file.c +3 -5
-              if (buf == NULL) {
-                      err = -errno;
-                      goto error;
-              }
-              sz = read_str_safe(file->fd, buf, magic_size_max + 1);
+              assert_cc(sizeof(magic_zstd) < sizeof(buf));
+              assert_cc(sizeof(magic_xz) < sizeof(buf));
+              assert_cc(sizeof(magic_zlib) < sizeof(buf));

../libkmod/libkmod-file.c: In function 'kmod_file_open':
../shared/macro.h:25:9: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]

Quoted 1 line

Is there a particular use-case for explicitly forcing C90?

not forcing C90, but forcing -Wdeclaration-after-statement as per flag passed in configure.ac. I think the message given by gcc is misleading here.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Indeed thanks. Seems like I should add `export CFLAGS+=-Werror" to my dev box.

so, are you ok that I just squash this?

Yes, of course.

thanks, pushed everything except the last patch.

Lucas De Marchi

Quoted 2 lines

-Emil

#27 lore raw

PATCH 10/13 libkmod: tidy-up kmod_file_open()

In reply to Emil Velikov

This commit cleans up the indentation and the error path of the function. It bears no functional changes.

---
 libkmod/libkmod-file.c | 60 +++++++++++++++++++++-----------------------------
 1 file changed, 25 insertions(+), 35 deletions(-)
libkmod/libkmod-file.c +25 -35
index 5b88d6c..c4893fd 100644
@@ -410,41 +410,40 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 {
 	struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
 	const struct comp_type *itr;
-	int err = 0;
+	char buf[7];
+	ssize_t sz;
 
 	if (file == NULL)
 		return NULL;
 
 	file->fd = open(filename, O_RDONLY|O_CLOEXEC);
 	if (file->fd < 0) {
-		err = -errno;
-		goto error;
+		free(file);
+		return NULL;
 	}
 
-	{
-		char buf[7];
-		ssize_t sz;
-
-		assert_cc(sizeof(magic_zstd) < sizeof(buf));
-		assert_cc(sizeof(magic_xz) < sizeof(buf));
-		assert_cc(sizeof(magic_zlib) < sizeof(buf));
-
-		sz = read_str_safe(file->fd, buf, sizeof(buf));
-		lseek(file->fd, 0, SEEK_SET);
-		if (sz != (sizeof(buf) - 1)) {
-			if (sz < 0)
-				err = sz;
-			else
-				err = -EINVAL;
-			goto error;
-		}
+	assert_cc(sizeof(magic_zstd) < sizeof(buf));
+	assert_cc(sizeof(magic_xz) < sizeof(buf));
+	assert_cc(sizeof(magic_zlib) < sizeof(buf));
 
-		for (itr = comp_types; itr->load != NULL; itr++) {
-			if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
-				file->load = itr->load;
-				file->compression = itr->compression;
-				break;
-			}
+	sz = read_str_safe(file->fd, buf, sizeof(buf));
+	lseek(file->fd, 0, SEEK_SET);
+	if (sz != (sizeof(buf) - 1)) {
+		if (sz < 0)
+			errno = -sz;
+		else
+			errno = EINVAL;
+
+		close(file->fd);
+		free(file);
+		return NULL;
+	}
+
+	for (itr = comp_types; itr->load != NULL; itr++) {
+		if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
+			file->load = itr->load;
+			file->compression = itr->compression;
+			break;
 		}
 	}
 
@@ -455,15 +454,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 
 	file->ctx = ctx;
 
-error:
-	if (err < 0) {
-		if (file->fd >= 0)
-			close(file->fd);
-		free(file);
-		errno = -err;
-		return NULL;
-	}
-
 	return file;
 }
 
Signature · 1 line
2.43.0
Reviewed-by on v1 10/13
#28 lore raw

Re: [PATCH kmod 10/13] libkmod: tidy-up kmod_file_open()

Quoted 6 lines · On Mon, Feb 12, 2024 at 05:23:11PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

This commit cleans up the indentation and the error path of the function. It bears no functional changes.

thanks
Lucas De Marchi

Quoted 95 lines

---
libkmod/libkmod-file.c | 60 +++++++++++++++++++++-----------------------------

1 file changed, 25 insertions(+), 35 deletions(-)
libkmod/libkmod-file.c +25 -35
index 5b88d6c..c4893fd 100644
@@ -410,41 +410,40 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 {
 	struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
 	const struct comp_type *itr;
-	int err = 0;
+	char buf[7];
+	ssize_t sz;
 
 	if (file == NULL)
 		return NULL;
 
 	file->fd = open(filename, O_RDONLY|O_CLOEXEC);
 	if (file->fd < 0) {
-		err = -errno;
-		goto error;
+		free(file);
+		return NULL;
 	}
 
-	{
-		char buf[7];
-		ssize_t sz;
-
-		assert_cc(sizeof(magic_zstd) < sizeof(buf));
-		assert_cc(sizeof(magic_xz) < sizeof(buf));
-		assert_cc(sizeof(magic_zlib) < sizeof(buf));
-
-		sz = read_str_safe(file->fd, buf, sizeof(buf));
-		lseek(file->fd, 0, SEEK_SET);
-		if (sz != (sizeof(buf) - 1)) {
-			if (sz < 0)
-				err = sz;
-			else
-				err = -EINVAL;
-			goto error;
-		}
+	assert_cc(sizeof(magic_zstd) < sizeof(buf));
+	assert_cc(sizeof(magic_xz) < sizeof(buf));
+	assert_cc(sizeof(magic_zlib) < sizeof(buf));
 
-		for (itr = comp_types; itr->load != NULL; itr++) {
-			if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
-				file->load = itr->load;
-				file->compression = itr->compression;
-				break;
-			}
+	sz = read_str_safe(file->fd, buf, sizeof(buf));
+	lseek(file->fd, 0, SEEK_SET);
+	if (sz != (sizeof(buf) - 1)) {
+		if (sz < 0)
+			errno = -sz;
+		else
+			errno = EINVAL;
+
+		close(file->fd);
+		free(file);
+		return NULL;
+	}
+
+	for (itr = comp_types; itr->load != NULL; itr++) {
+		if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
+			file->load = itr->load;
+			file->compression = itr->compression;
+			break;
 		}
 	}
 
@@ -455,15 +454,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 
 	file->ctx = ctx;
 
-error:
-	if (err < 0) {
-		if (file->fd >= 0)
-			close(file->fd);
-		free(file);
-		errno = -err;
-		return NULL;
-	}
-
 	return file;
 }
 
Signature · 2 lines
-- 
2.43.0
#29 lore raw

PATCH 11/13 libkmod: move load_reg() further up

In reply to Emil Velikov

We're about to reference it in comp_types with next commit.

---
 libkmod/libkmod-file.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
libkmod/libkmod-file.c +12 -12
index c4893fd..db775a6 100644
@@ -358,18 +358,6 @@ static int load_zlib(struct kmod_file *file)
 
 static const char magic_zlib[] = {0x1f, 0x8b};
 
-static const struct comp_type {
-	size_t magic_size;
-	enum kmod_file_compression_type compression;
-	const char *magic_bytes;
-	int (*load)(struct kmod_file *file);
-} comp_types[] = {
-	{sizeof(magic_zstd),	KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, load_zstd},
-	{sizeof(magic_xz),	KMOD_FILE_COMPRESSION_XZ, magic_xz, load_xz},
-	{sizeof(magic_zlib),	KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, load_zlib},
-	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, NULL}
-};
-
 static int load_reg(struct kmod_file *file)
 {
 	struct stat st;
@@ -388,6 +376,18 @@ static int load_reg(struct kmod_file *file)
 	return 0;
 }
 
+static const struct comp_type {
+	size_t magic_size;
+	enum kmod_file_compression_type compression;
+	const char *magic_bytes;
+	int (*load)(struct kmod_file *file);
+} comp_types[] = {
+	{sizeof(magic_zstd),	KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, load_zstd},
+	{sizeof(magic_xz),	KMOD_FILE_COMPRESSION_XZ, magic_xz, load_xz},
+	{sizeof(magic_zlib),	KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, load_zlib},
+	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, NULL}
+};
+
 struct kmod_elf *kmod_file_get_elf(struct kmod_file *file)
 {
 	int err;
Signature · 1 line
2.43.0
Reviewed-by on v1 11/13
#30 lore raw

Re: [PATCH kmod 11/13] libkmod: move load_reg() further up

Quoted 5 lines · On Mon, Feb 12, 2024 at 05:23:12PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

We're about to reference it in comp_types with next commit.

thanks
Lucas De Marchi

Quoted 50 lines

---
libkmod/libkmod-file.c | 24 ++++++++++++------------

1 file changed, 12 insertions(+), 12 deletions(-)
libkmod/libkmod-file.c +12 -12
index c4893fd..db775a6 100644
@@ -358,18 +358,6 @@ static int load_zlib(struct kmod_file *file)
 
 static const char magic_zlib[] = {0x1f, 0x8b};
 
-static const struct comp_type {
-	size_t magic_size;
-	enum kmod_file_compression_type compression;
-	const char *magic_bytes;
-	int (*load)(struct kmod_file *file);
-} comp_types[] = {
-	{sizeof(magic_zstd),	KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, load_zstd},
-	{sizeof(magic_xz),	KMOD_FILE_COMPRESSION_XZ, magic_xz, load_xz},
-	{sizeof(magic_zlib),	KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, load_zlib},
-	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, NULL}
-};
-
 static int load_reg(struct kmod_file *file)
 {
 	struct stat st;
@@ -388,6 +376,18 @@ static int load_reg(struct kmod_file *file)
 	return 0;
 }
 
+static const struct comp_type {
+	size_t magic_size;
+	enum kmod_file_compression_type compression;
+	const char *magic_bytes;
+	int (*load)(struct kmod_file *file);
+} comp_types[] = {
+	{sizeof(magic_zstd),	KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, load_zstd},
+	{sizeof(magic_xz),	KMOD_FILE_COMPRESSION_XZ, magic_xz, load_xz},
+	{sizeof(magic_zlib),	KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, load_zlib},
+	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, NULL}
+};
+
 struct kmod_elf *kmod_file_get_elf(struct kmod_file *file)
 {
 	int err;
Signature · 2 lines
-- 
2.43.0
#31 lore raw

PATCH 12/13 libkmod: keep KMOD_FILE_COMPRESSION_NONE/load_reg in comp_types

In reply to Emil Velikov

It's cleaner to handle all compression types and load functions in the same style.

---
 libkmod/libkmod-file.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)
libkmod/libkmod-file.c +8 -11
index db775a6..f162a10 100644
@@ -385,7 +385,7 @@ static const struct comp_type {
 	{sizeof(magic_zstd),	KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, load_zstd},
 	{sizeof(magic_xz),	KMOD_FILE_COMPRESSION_XZ, magic_xz, load_xz},
 	{sizeof(magic_zlib),	KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, load_zlib},
-	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, NULL}
+	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, load_reg}
 };
 
 struct kmod_elf *kmod_file_get_elf(struct kmod_file *file)
@@ -409,7 +409,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 						const char *filename)
 {
 	struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
-	const struct comp_type *itr;
 	char buf[7];
 	ssize_t sz;
 
@@ -439,19 +438,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 		return NULL;
 	}
 
-	for (itr = comp_types; itr->load != NULL; itr++) {
-		if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
-			file->load = itr->load;
-			file->compression = itr->compression;
+	for (unsigned int i = 0; i < ARRAY_SIZE(comp_types); i++) {
+		const struct comp_type *itr = &comp_types[i];
+
+		file->load = itr->load;
+		file->compression = itr->compression;
+		if (itr->magic_size &&
+		    memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
 			break;
 		}
 	}
 
-	if (file->load == NULL) {
-		file->load = load_reg;
-		file->compression = KMOD_FILE_COMPRESSION_NONE;
-	}
-
 	file->ctx = ctx;
 
 	return file;
Signature · 1 line
2.43.0
Reviewed-by on v1 12/13
#32 lore raw

Re: [PATCH kmod 12/13] libkmod: keep KMOD_FILE_COMPRESSION_NONE/load_reg in comp_types

Quoted 6 lines · On Mon, Feb 12, 2024 at 05:23:13PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

It's cleaner to handle all compression types and load functions in the same style.

thanks
Lucas De Marchi

Quoted 56 lines

---
libkmod/libkmod-file.c | 19 ++++++++-----------

1 file changed, 8 insertions(+), 11 deletions(-)
libkmod/libkmod-file.c +8 -11
index db775a6..f162a10 100644
@@ -385,7 +385,7 @@ static const struct comp_type {
 	{sizeof(magic_zstd),	KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, load_zstd},
 	{sizeof(magic_xz),	KMOD_FILE_COMPRESSION_XZ, magic_xz, load_xz},
 	{sizeof(magic_zlib),	KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, load_zlib},
-	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, NULL}
+	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, load_reg}
 };
 
 struct kmod_elf *kmod_file_get_elf(struct kmod_file *file)
@@ -409,7 +409,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 						const char *filename)
 {
 	struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
-	const struct comp_type *itr;
 	char buf[7];
 	ssize_t sz;
 
@@ -439,19 +438,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 		return NULL;
 	}
 
-	for (itr = comp_types; itr->load != NULL; itr++) {
-		if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
-			file->load = itr->load;
-			file->compression = itr->compression;
+	for (unsigned int i = 0; i < ARRAY_SIZE(comp_types); i++) {
+		const struct comp_type *itr = &comp_types[i];
+
+		file->load = itr->load;
+		file->compression = itr->compression;
+		if (itr->magic_size &&
+		    memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
 			break;
 		}
 	}
 
-	if (file->load == NULL) {
-		file->load = load_reg;
-		file->compression = KMOD_FILE_COMPRESSION_NONE;
-	}
-
 	file->ctx = ctx;
 
 	return file;
Signature · 2 lines
-- 
2.43.0
#33 lore raw

PATCH 13/13 libkmod: always fallback to do_init_module()

In reply to Emil Velikov

Since the direct loading was introduced a few bugs became obvious that the compression args used were off - both in-kernel and dkms.

While both of those are fixed already, not all of those have reached all users. For example: for dkms I'm aiming to do a release just as kmod has theirs (to align /lib/modules <> /usr/lib/modules support).

Although I am wondering if we can indiscriminatingly callback to the old do_init_module() in all the cases. This means that we'll catch any in-kernel decompression issues - invalid args, ENOMEM, other.... Although for others (wrong magic, perm, etc) we will end up doing the exact kernel work twice.

Overall the trade-off seems worth it, so flip this.

---
 libkmod/libkmod-module.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libkmod/libkmod-module.c +1 -1
index d309948..2c0d46d 100644
@@ -980,7 +980,7 @@ KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
 	}
 
 	err = do_finit_module(mod, flags, args);
-	if (err == -ENOSYS)
+	if (err)
 		err = do_init_module(mod, flags, args);
 
 	if (err < 0)
Signature · 1 line
2.43.0
#34 lore raw

Re: [PATCH kmod 13/13] libkmod: always fallback to do_init_module()

Quoted 9 lines · On Mon, Feb 12, 2024 at 05:23:14PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

Since the direct loading was introduced a few bugs became obvious that the compression args used were off - both in-kernel and dkms.

While both of those are fixed already, not all of those have reached all users. For example: for dkms I'm aiming to do a release just as kmod has theirs (to align /lib/modules <> /usr/lib/modules support).

Quoted 5 lines

Although I am wondering if we can indiscriminatingly callback to the old do_init_module() in all the cases. This means that we'll catch any in-kernel decompression issues - invalid args, ENOMEM, other.... Although for others (wrong magic, perm, etc) we will end up doing the exact kernel work twice.

I'm not sure I like to repeat this for any error. Example: if we get an EAGAIN we go and try again? When thinking about the recent in-kernel optimizations to stop loading the same file again over and over, just doing it again seems like a wrong approach.

Quoted 2 lines

Overall the trade-off seems worth it, so flip this.

I'm not sure. I will keep this out for now and apply the rest. +Luis too.

Lucas De Marchi

Quoted 23 lines

---
libkmod/libkmod-module.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
libkmod/libkmod-module.c +1 -1
index d309948..2c0d46d 100644
@@ -980,7 +980,7 @@ KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
 	}
 
 	err = do_finit_module(mod, flags, args);
-	if (err == -ENOSYS)
+	if (err)
 		err = do_init_module(mod, flags, args);
 
 	if (err < 0)
Signature · 2 lines
-- 
2.43.0
#35 lore raw

Re: [PATCH kmod 13/13] libkmod: always fallback to do_init_module()

Quoted 17 lines · Lucas De Marchi
Quoted 9 lines · On Mon, Feb 12, 2024 at 05:23:14PM GMT, Emil Velikov via B4 Relay wrote:

From: Emil Velikov <[email protected]>

Since the direct loading was introduced a few bugs became obvious that the compression args used were off - both in-kernel and dkms.

While both of those are fixed already, not all of those have reached all users. For example: for dkms I'm aiming to do a release just as kmod has theirs (to align /lib/modules <> /usr/lib/modules support).

Quoted 5 lines

Although I am wondering if we can indiscriminatingly callback to the old do_init_module() in all the cases. This means that we'll catch any in-kernel decompression issues - invalid args, ENOMEM, other.... Although for others (wrong magic, perm, etc) we will end up doing the exact kernel work twice.

Quoted 5 lines

I'm not sure I like to repeat this for any error. Example: if we get an EAGAIN we go and try again? When thinking about the recent in-kernel optimizations to stop loading the same file again over and over, just doing it again seems like a wrong approach.

To be honest I did notice the optimisation work in the kernel, but wasn't able to quantify its benefits on my end. ¯\_(ツ)_/¯

The most wide-spread issue (incompatible compression arguments in kernel and dkms) has been fixed and new releases are available. And other triggers do not seem that common/likely. So deferring/dropping this patch makes sense.

Quoted 7 lines
Quoted 2 lines

Overall the trade-off seems worth it, so flip this.

I'm not sure. I will keep this out for now and apply the rest. +Luis too.

Ack, thanks o/
-Emil

Quoted 27 lines

Lucas De Marchi

Quoted 23 lines

---
libkmod/libkmod-module.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
libkmod/libkmod-module.c +1 -2
index d309948..2c0d46d 100644
@@ -980,7 +980,7 @@ KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
      }
 
      err = do_finit_module(mod, flags, args);
-      if (err == -ENOSYS)
+      if (err)
              err = do_init_module(mod, flags, args);
 
      if (err < 0)
 
--

2.43.0