Skip to contentSkip to conversation

module: add in-kernel support for decompressing

PATCH Superseded Dmitry Torokhov 2 participants 12 messages Dec 10, 2021 – Jan 11, 2022

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 module: add in-kernel support for decompressing

Current scheme of having userspace decompress kernel modules before loading them into the kernel runs afoul of LoadPin security policy, as it loses link between the source of kernel module on the disk and binary blob that is being loaded into the kernel. To solve this issue let's implement decompression in kernel, so that we can pass a file descriptor of compressed module file into finit_module() which will keep LoadPin happy.

To let userspace know what compression/decompression scheme kernel supports it will create /sys/module/compression attribute. kmod can read this attribute and decide if it can pass compressed file to finit_module(). New MODULE_INIT_COMPRESSED_DATA flag indicates that the kernel should attempt to decompress the data read from file descriptor prior to trying load the module.

To simplify things kernel will only implement single decompression method matching compression method selected when generating modules. This patch implements gzip and xz; more can be added later,

---

v3:
- fix missing Kconfig dependency

v2:
- changed flag MODULE_INIT_COMPRESSED_DATA -> MODULE_INIT_COMPRESSED_FLAG per Kees' request

 include/uapi/linux/module.h |   1 +
 init/Kconfig                |  13 ++
 kernel/Makefile             |   1 +
 kernel/module-internal.h    |  18 +++
 kernel/module.c             |  35 +++--
 kernel/module_decompress.c  | 271 ++++++++++++++++++++++++++++++++++++
 6 files changed, 328 insertions(+), 11 deletions(-)
include/uapi/linux/module.h +1 -0
index 50d98ec5e866..03a33ffffcba 100644
@@ -5,5 +5,6 @@
 /* Flags for sys_finit_module: */
 #define MODULE_INIT_IGNORE_MODVERSIONS	1
 #define MODULE_INIT_IGNORE_VERMAGIC	2
+#define MODULE_INIT_COMPRESSED_FILE	4
 
 #endif /* _UAPI_LINUX_MODULE_H */
init/Kconfig +13 -0
index cd23faa163d1..d90774ff7610 100644
@@ -2305,6 +2305,19 @@ config MODULE_COMPRESS_ZSTD
 
 endchoice
 
+config MODULE_DECOMPRESS
+	bool "Support in-kernel module decompression"
+	depends on MODULE_COMPRESS_GZIP || MODULE_COMPRESS_XZ
+	select ZLIB_INFLATE if MODULE_COMPRESS_GZIP
+	select XZ_DEC if MODULE_COMPRESS_XZ
+	help
+
+	  Support for decompressing kernel modules by the kernel itself
+	  instead of relying on userspace to perform this task. Useful when
+	  load pinning security policy is enabled.
+
+	  If unsure, say N.
+
 config MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
 	bool "Allow loading of modules with missing namespace imports"
 	help
kernel/Makefile +1 -0
index 186c49582f45..56f4ee97f328 100644
@@ -67,6 +67,7 @@ obj-y += up.o
 endif
 obj-$(CONFIG_UID16) += uid16.o
 obj-$(CONFIG_MODULES) += module.o
+obj-$(CONFIG_MODULE_DECOMPRESS) += module_decompress.o
 obj-$(CONFIG_MODULE_SIG) += module_signing.o
 obj-$(CONFIG_MODULE_SIG_FORMAT) += module_signature.o
 obj-$(CONFIG_KALLSYMS) += kallsyms.o
kernel/module-internal.h +18 -0
index 33783abc377b..3c1143d2c8c7 100644
@@ -22,6 +22,11 @@ struct load_info {
 	bool sig_ok;
 #ifdef CONFIG_KALLSYMS
 	unsigned long mod_kallsyms_init_off;
+#endif
+#ifdef CONFIG_MODULE_DECOMPRESS
+	struct page **pages;
+	unsigned int max_pages;
+	unsigned int used_pages;
 #endif
 	struct {
 		unsigned int sym, str, mod, vers, info, pcpu;
@@ -29,3 +34,16 @@ struct load_info {
 };
 
 extern int mod_verify_sig(const void *mod, struct load_info *info);
+
+#ifdef CONFIG_MODULE_DECOMPRESS
+int module_decompress(struct load_info *info, const void *buf, size_t size);
+void module_decompress_cleanup(struct load_info *info);
+#else
+int module_decompress(struct load_info *info, const void *buf, size_t size)
+{
+	return -EOPNOTSUPP;
+}
+void module_decompress_cleanup(struct load_info *info)
+{
+}
+#endif
kernel/module.c +24 -11
index 84a9141a5e15..a7c5490b41e9 100644
@@ -3174,9 +3174,12 @@ static int copy_module_from_user(const void __user *umod, unsigned long len,
 	return err;
 }
 
-static void free_copy(struct load_info *info)
+static void free_copy(struct load_info *info, int flags)
 {
-	vfree(info->hdr);
+	if (flags & MODULE_INIT_COMPRESSED_FILE)
+		module_decompress_cleanup(info);
+	else
+		vfree(info->hdr);
 }
 
 static int rewrite_section_headers(struct load_info *info, int flags)
@@ -4125,7 +4128,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
 	}
 
 	/* Get rid of temporary copy. */
-	free_copy(info);
+	free_copy(info, flags);
 
 	/* Done! */
 	trace_module_load(mod);
@@ -4174,7 +4177,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
 
 	module_deallocate(mod, info);
  free_copy:
-	free_copy(info);
+	free_copy(info, flags);
 	return err;
 }
 
@@ -4201,7 +4204,8 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
 SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
 {
 	struct load_info info = { };
-	void *hdr = NULL;
+	void *buf = NULL;
+	int len;
 	int err;
 
 	err = may_init_module();
@@ -4211,15 +4215,24 @@ SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
 	pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
 
 	if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
-		      |MODULE_INIT_IGNORE_VERMAGIC))
+		      |MODULE_INIT_IGNORE_VERMAGIC
+		      |MODULE_INIT_COMPRESSED_FILE))
 		return -EINVAL;
 
-	err = kernel_read_file_from_fd(fd, 0, &hdr, INT_MAX, NULL,
+	len = kernel_read_file_from_fd(fd, 0, &buf, INT_MAX, NULL,
 				       READING_MODULE);
-	if (err < 0)
-		return err;
-	info.hdr = hdr;
-	info.len = err;
+	if (len < 0)
+		return len;
+
+	if (flags & MODULE_INIT_COMPRESSED_FILE) {
+		err = module_decompress(&info, buf, len);
+		vfree(buf); /* compressed data is no longer needed */
+		if (err)
+			return err;
+	} else {
+		info.hdr = buf;
+		info.len = len;
+	}
 
 	return load_module(&info, uargs, flags);
 }
kernel/module_decompress.c new file +271 -0

271 lines not on this page · raw message

Signature · 5 lines
2.34.1.173.g76aa8bc2d0-goog


-- 
Dmitry
#1 lore raw
Quoted 20 lines · Dmitry Torokhov
kernel/module_decompress.c new file +20 -0
+static ssize_t module_xz_decompress(struct load_info *info,
+				    const void *buf, size_t size)
+{
+	static const u8 signature[] = { 0xfd, '7', 'z', 'X', 'Z', 0 };
+	struct xz_dec *xz_dec;
+	struct xz_buf xz_buf;
+	enum xz_ret xz_ret;
+	size_t new_size = 0;
+	ssize_t retval;
+
+	if (size < sizeof(signature) ||
+	    memcmp(buf, signature, sizeof(signature))) {
+		pr_err("not an xz compressed module\n");
+		return -EINVAL;
+	}
+
+	xz_dec = xz_dec_init(XZ_DYNALLOC, (u32)-1);
+	if (!xz_dec)
+		return -ENOMEM;
+

kernel/module_decompress.c line 177

Quoted 10 lines
kernel/module_decompress.c new file +10 -0
+	xz_buf.in_size = size;
+	xz_buf.in = buf;
+	xz_buf.in_pos = 0;
+
+	do {
+		struct page *page = module_get_next_page(info);
+		if (!page) {
+			retval = -ENOMEM;
+			goto out;
+		}

This looks very similar to fw_decompress_xz_pages() on drivers/base/firmware_loader/main.c

Is there any sharing possible with decompression between the two?

Luis

#2 lore raw
Quoted 35 lines · Luis Chamberlain
Quoted 20 lines · Dmitry Torokhov
kernel/module_decompress.c new file +20 -0
+static ssize_t module_xz_decompress(struct load_info *info,
+				    const void *buf, size_t size)
+{
+	static const u8 signature[] = { 0xfd, '7', 'z', 'X', 'Z', 0 };
+	struct xz_dec *xz_dec;
+	struct xz_buf xz_buf;
+	enum xz_ret xz_ret;
+	size_t new_size = 0;
+	ssize_t retval;
+
+	if (size < sizeof(signature) ||
+	    memcmp(buf, signature, sizeof(signature))) {
+		pr_err("not an xz compressed module\n");
+		return -EINVAL;
+	}
+
+	xz_dec = xz_dec_init(XZ_DYNALLOC, (u32)-1);
+	if (!xz_dec)
+		return -ENOMEM;
+

kernel/module_decompress.c line 177

Quoted 10 lines
kernel/module_decompress.c new file +10 -0
+	xz_buf.in_size = size;
+	xz_buf.in = buf;
+	xz_buf.in_pos = 0;
+
+	do {
+		struct page *page = module_get_next_page(info);
+		if (!page) {
+			retval = -ENOMEM;
+			goto out;
+		}

This looks very similar to fw_decompress_xz_pages() on drivers/base/firmware_loader/main.c

Quoted 1 line

Is there any sharing possible with decompression between the two?

Ideally we'd get proper crypto API allowing this (page by page) decompression and then we can switch to it in both places and stop calling into particular algorithms directly. Unfortunately this is not something I can sign up for at this time.

As it is I am not sure if it worth to try to split out XZ code and share it between module and firmware loader.

Thanks.

Signature · 1 line
Dmitry
#3 lore raw
Quoted 38 lines · Dmitry Torokhov
Quoted 35 lines · Luis Chamberlain
Quoted 20 lines · Dmitry Torokhov
kernel/module_decompress.c new file +20 -0
+static ssize_t module_xz_decompress(struct load_info *info,
+				    const void *buf, size_t size)
+{
+	static const u8 signature[] = { 0xfd, '7', 'z', 'X', 'Z', 0 };
+	struct xz_dec *xz_dec;
+	struct xz_buf xz_buf;
+	enum xz_ret xz_ret;
+	size_t new_size = 0;
+	ssize_t retval;
+
+	if (size < sizeof(signature) ||
+	    memcmp(buf, signature, sizeof(signature))) {
+		pr_err("not an xz compressed module\n");
+		return -EINVAL;
+	}
+
+	xz_dec = xz_dec_init(XZ_DYNALLOC, (u32)-1);
+	if (!xz_dec)
+		return -ENOMEM;
+

kernel/module_decompress.c line 177

Quoted 10 lines
kernel/module_decompress.c new file +10 -0
+	xz_buf.in_size = size;
+	xz_buf.in = buf;
+	xz_buf.in_pos = 0;
+
+	do {
+		struct page *page = module_get_next_page(info);
+		if (!page) {
+			retval = -ENOMEM;
+			goto out;
+		}

This looks very similar to fw_decompress_xz_pages() on drivers/base/firmware_loader/main.c

Quoted 1 line

Is there any sharing possible with decompression between the two?

Quoted 7 lines

Ideally we'd get proper crypto API allowing this (page by page) decompression and then we can switch to it in both places and stop calling into particular algorithms directly. Unfortunately this is not something I can sign up for at this time.

As it is I am not sure if it worth to try to split out XZ code and share it between module and firmware loader.

Got it, makes sense.

What type of tests have you implemented for this ? kmod has quite a bit of tests. Can you add some for this?

Luis

#4 lore raw
Quoted 44 lines · Luis Chamberlain
Quoted 43 lines · Dmitry Torokhov
Quoted 35 lines · Luis Chamberlain
Quoted 20 lines · Dmitry Torokhov
kernel/module_decompress.c new file +20 -0
+static ssize_t module_xz_decompress(struct load_info *info,
+				    const void *buf, size_t size)
+{
+	static const u8 signature[] = { 0xfd, '7', 'z', 'X', 'Z', 0 };
+	struct xz_dec *xz_dec;
+	struct xz_buf xz_buf;
+	enum xz_ret xz_ret;
+	size_t new_size = 0;
+	ssize_t retval;
+
+	if (size < sizeof(signature) ||
+	    memcmp(buf, signature, sizeof(signature))) {
+		pr_err("not an xz compressed module\n");
+		return -EINVAL;
+	}
+
+	xz_dec = xz_dec_init(XZ_DYNALLOC, (u32)-1);
+	if (!xz_dec)
+		return -ENOMEM;
+

kernel/module_decompress.c line 177

Quoted 10 lines
kernel/module_decompress.c new file +10 -0
+	xz_buf.in_size = size;
+	xz_buf.in = buf;
+	xz_buf.in_pos = 0;
+
+	do {
+		struct page *page = module_get_next_page(info);
+		if (!page) {
+			retval = -ENOMEM;
+			goto out;
+		}

This looks very similar to fw_decompress_xz_pages() on drivers/base/firmware_loader/main.c

Quoted 1 line

Is there any sharing possible with decompression between the two?

Ideally we'd get proper crypto API allowing this (page by page) decompression and then we can switch to it in both places and stop calling into particular algorithms directly. Unfortunately this is not something I can sign up for at this time.

Quoted 7 lines
Quoted 2 lines

As it is I am not sure if it worth to try to split out XZ code and share it between module and firmware loader.

Got it, makes sense.

What type of tests have you implemented for this ? kmod has quite a bit of tests. Can you add some for this?

OK, will do.

Thanks.

Signature · 1 line
Dmitry
#5 lore raw
In reply to Dmitry Torokhov

init/Kconfig config MODULE_COMPRESS_ZSTD · line 2312

Quoted 13 lines · Dmitry Torokhov
init/Kconfig +5 -0
index cd23faa163d1..d90774ff7610 100644
@@ -2305,6 +2305,19 @@ config MODULE_COMPRESS_ZSTD
 
 endchoice
 
+config MODULE_DECOMPRESS
+	bool "Support in-kernel module decompression"
+	depends on MODULE_COMPRESS_GZIP || MODULE_COMPRESS_XZ
+	select ZLIB_INFLATE if MODULE_COMPRESS_GZIP
+	select XZ_DEC if MODULE_COMPRESS_XZ

What if MODULE_COMPRESS_GZIP and MODULE_COMPRESS_XZ are enabled? These are not mutually exclusive.

init/Kconfig config MODULE_COMPRESS_ZSTD · line 2317

Quoted 5 lines
init/Kconfig +5 -0
+	help
+
+	  Support for decompressing kernel modules by the kernel itself
+	  instead of relying on userspace to perform this task. Useful when
+	  load pinning security policy is enabled.

Shouldn't kernel decompression be faster too? If so, what's the point of doing it in userspace?

Quoted 52 lines
kernel/module_decompress.c new file +46 -0
new file mode 100644
index 000000000000..590ca00aa098
@@ -0,0 +1,271 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright 2021 Google LLC.
+ */
+
+#include <linux/init.h>
+#include <linux/highmem.h>
+#include <linux/kobject.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/sysfs.h>
+#include <linux/vmalloc.h>
+
+#include "module-internal.h"
+
+static int module_extend_max_pages(struct load_info *info, unsigned int extent)
+{
+	struct page **new_pages;
+
+	new_pages = kvmalloc_array(info->max_pages + extent,
+				   sizeof(info->pages), GFP_KERNEL);
+	if (!new_pages)
+		return -ENOMEM;
+
+	memcpy(new_pages, info->pages, info->max_pages * sizeof(info->pages));
+	kvfree(info->pages);
+	info->pages = new_pages;
+	info->max_pages += extent;
+
+	return 0;
+}
+
+static struct page *module_get_next_page(struct load_info *info)
+{
+	struct page *page;
+	int error;
+
+	if (info->max_pages == info->used_pages) {
+		error = module_extend_max_pages(info, info->used_pages);
+		if (error)
+			return ERR_PTR(error);
+	}
+
+	page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
+	if (!page)

kernel/module_decompress.c line 56

Quoted 10 lines
kernel/module_decompress.c new file +10 -0
+		return ERR_PTR(-ENOMEM);
+
+	info->pages[info->used_pages++] = page;
+	return page;
+}
+
+#ifdef CONFIG_MODULE_COMPRESS_GZIP
+#include <linux/zlib.h>
+#define MODULE_COMPRESSION	gzip
+#define MODULE_DECOMPRESS_FN	module_gzip_decompress

So gzip is assumed if your kernel has both gzip and xz. That seems odd.

<-- snip -->

kernel/module_decompress.c line 200

Quoted 5 lines
kernel/module_decompress.c new file +5 -0
+#elif CONFIG_MODULE_COMPRESS_XZ
+#include <linux/xz.h>
+#define MODULE_COMPRESSION	xz
+#define MODULE_DECOMPRESS_FN	module_xz_decompress
+#else

<-- snip -->

kernel/module_decompress.c line 201

Quoted 1 line
kernel/module_decompress.c new file +1 -0
+#error "Unexpected configuration for CONFIG_MODULE_DECOMPRESS"

Using "depends on" logic on the kconfig symbol would resolve this and make this not needed.

Why can't we just inspect the module and determine? Or, why not just add a new kconfig symbol under MODULE_DECOMPRESS which lets you specify the MODULE_COMPRESSION_TYPE. This way this is explicit.

kernel/module_decompress.c line 271

Quoted 1 line
kernel/module_decompress.c new file +1 -0
+module_init(module_decompress_sysfs_init);

This seems odd, altough it works, can you use late_initcall instead()?

Luis

#6 lore raw
Quoted 9 lines · Luis Chamberlain
Quoted 8 lines · Dmitry Torokhov
init/Kconfig +0 -0
index cd23faa163d1..d90774ff7610 100644
@@ -2305,6 +2305,19 @@ config MODULE_COMPRESS_ZSTD
 
 endchoice
 
Quoted 8 lines

init/Kconfig config MODULE_COMPRESS_ZSTD · line 2312

Quoted 5 lines
init/Kconfig +5 -0
+config MODULE_DECOMPRESS
+	bool "Support in-kernel module decompression"
+	depends on MODULE_COMPRESS_GZIP || MODULE_COMPRESS_XZ
+	select ZLIB_INFLATE if MODULE_COMPRESS_GZIP
+	select XZ_DEC if MODULE_COMPRESS_XZ

What if MODULE_COMPRESS_GZIP and MODULE_COMPRESS_XZ are enabled? These are not mutually exclusive.

They are mutually exclusive, the kernel uses the same (one) compression method for all kernel modules that it generates (i.e we do not compress drivers/usb/... with gzip while drivers/net/... with xz).

The idea here is to allow the kernel consume the same format that was used when generating modules. Supporting multiple formats at once is overkill IMO.

Quoted 9 lines

init/Kconfig config MODULE_COMPRESS_ZSTD · line 2317

Quoted 6 lines
init/Kconfig +5 -0
+	help
+
+	  Support for decompressing kernel modules by the kernel itself
+	  instead of relying on userspace to perform this task. Useful when
+	  load pinning security policy is enabled.

Shouldn't kernel decompression be faster too? If so, what's the point of doing it in userspace?

Make the kernel smaller? Have more flexibility with exotic compression formats?

Quoted 57 lines
Quoted 57 lines
kernel/module_decompress.c new file +50 -0
new file mode 100644
index 000000000000..590ca00aa098
@@ -0,0 +1,271 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright 2021 Google LLC.
+ */
+
+#include <linux/init.h>
+#include <linux/highmem.h>
+#include <linux/kobject.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/sysfs.h>
+#include <linux/vmalloc.h>
+
+#include "module-internal.h"
+
+static int module_extend_max_pages(struct load_info *info, unsigned int extent)
+{
+	struct page **new_pages;
+
+	new_pages = kvmalloc_array(info->max_pages + extent,
+				   sizeof(info->pages), GFP_KERNEL);
+	if (!new_pages)
+		return -ENOMEM;
+
+	memcpy(new_pages, info->pages, info->max_pages * sizeof(info->pages));
+	kvfree(info->pages);
+	info->pages = new_pages;
+	info->max_pages += extent;
+
+	return 0;
+}
+
+static struct page *module_get_next_page(struct load_info *info)
+{
+	struct page *page;
+	int error;
+
+	if (info->max_pages == info->used_pages) {
+		error = module_extend_max_pages(info, info->used_pages);
+		if (error)
+			return ERR_PTR(error);
+	}
+
+	page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
+	if (!page)
+		return ERR_PTR(-ENOMEM);
+
+	info->pages[info->used_pages++] = page;
+	return page;
Quoted 8 lines

kernel/module_decompress.c line 56

Quoted 6 lines
kernel/module_decompress.c new file +6 -0
+}
+
+#ifdef CONFIG_MODULE_COMPRESS_GZIP
+#include <linux/zlib.h>
+#define MODULE_COMPRESSION	gzip
+#define MODULE_DECOMPRESS_FN	module_gzip_decompress

So gzip is assumed if your kernel has both gzip and xz. That seems odd.

No, gzuo is used when CONFIG_MODULE_COMPRESS_GZIP is enabled. That means that CONFIG_MODULE_COMPRESS_XZ is not enabled.

Quoted 11 lines

<-- snip -->

kernel/module_decompress.c line 200

Quoted 5 lines
kernel/module_decompress.c new file +5 -0
+#elif CONFIG_MODULE_COMPRESS_XZ
+#include <linux/xz.h>
+#define MODULE_COMPRESSION	xz
+#define MODULE_DECOMPRESS_FN	module_xz_decompress
+#else

<-- snip -->

Quoted 8 lines

kernel/module_decompress.c line 201

Quoted 1 line
kernel/module_decompress.c new file +1 -0
+#error "Unexpected configuration for CONFIG_MODULE_DECOMPRESS"

Using "depends on" logic on the kconfig symbol would resolve this and make this not needed.

Why can't we just inspect the module and determine? Or, why not just add a new kconfig symbol under MODULE_DECOMPRESS which lets you specify the MODULE_COMPRESSION_TYPE. This way this is explicit.

It is a possibility, I just wanted to decompression scheme match compression one so there is less potential for misconfiguration.

Quoted 4 lines

kernel/module_decompress.c line 271

Quoted 2 lines
kernel/module_decompress.c new file +1 -0
+module_init(module_decompress_sysfs_init);

This seems odd, altough it works, can you use late_initcall instead()?

Yes, I can change this.

Thanks.

Signature · 1 line
Dmitry
#7 lore raw
Quoted 19 lines · Dmitry Torokhov
Quoted 9 lines · Luis Chamberlain
Quoted 8 lines · Dmitry Torokhov
init/Kconfig +0 -0
index cd23faa163d1..d90774ff7610 100644
@@ -2305,6 +2305,19 @@ config MODULE_COMPRESS_ZSTD
 
 endchoice
 
Quoted 8 lines

init/Kconfig config MODULE_COMPRESS_ZSTD · line 2312

Quoted 5 lines
init/Kconfig +5 -0
+config MODULE_DECOMPRESS
+	bool "Support in-kernel module decompression"
+	depends on MODULE_COMPRESS_GZIP || MODULE_COMPRESS_XZ
+	select ZLIB_INFLATE if MODULE_COMPRESS_GZIP
+	select XZ_DEC if MODULE_COMPRESS_XZ

What if MODULE_COMPRESS_GZIP and MODULE_COMPRESS_XZ are enabled? These are not mutually exclusive.

Quoted 3 lines

They are mutually exclusive, the kernel uses the same (one) compression method for all kernel modules that it generates (i.e we do not compress drivers/usb/... with gzip while drivers/net/... with xz).

Ah yes I failed to see the choice/prompt for it.

Quoted 3 lines

The idea here is to allow the kernel consume the same format that was used when generating modules. Supporting multiple formats at once is overkill IMO.

Indeed.

Quoted 10 lines
Quoted 8 lines

init/Kconfig config MODULE_COMPRESS_ZSTD · line 2317

Quoted 5 lines
init/Kconfig +5 -0
+	help
+
+	  Support for decompressing kernel modules by the kernel itself
+	  instead of relying on userspace to perform this task. Useful when
+	  load pinning security policy is enabled.

Shouldn't kernel decompression be faster too? If so, what's the point of doing it in userspace?

Make the kernel smaller?

Yes this I buy.

Quoted 2 lines

Have more flexibility with exotic compression
formats?

I just have a hunch that doing module decompression in the kernel will speed things quite a bit... any chance you can provide some before and after systemd-analyze ?

Luis

#8 lore raw
Quoted 43 lines · Luis Chamberlain
Quoted 19 lines · Dmitry Torokhov
Quoted 9 lines · Luis Chamberlain
Quoted 8 lines · Dmitry Torokhov
init/Kconfig +0 -0
index cd23faa163d1..d90774ff7610 100644
@@ -2305,6 +2305,19 @@ config MODULE_COMPRESS_ZSTD
 
 endchoice
 
Quoted 8 lines

init/Kconfig config MODULE_COMPRESS_ZSTD · line 2312

Quoted 5 lines
init/Kconfig +5 -0
+config MODULE_DECOMPRESS
+	bool "Support in-kernel module decompression"
+	depends on MODULE_COMPRESS_GZIP || MODULE_COMPRESS_XZ
+	select ZLIB_INFLATE if MODULE_COMPRESS_GZIP
+	select XZ_DEC if MODULE_COMPRESS_XZ

What if MODULE_COMPRESS_GZIP and MODULE_COMPRESS_XZ are enabled? These are not mutually exclusive.

Quoted 3 lines

They are mutually exclusive, the kernel uses the same (one) compression method for all kernel modules that it generates (i.e we do not compress drivers/usb/... with gzip while drivers/net/... with xz).

Ah yes I failed to see the choice/prompt for it.

Quoted 3 lines

The idea here is to allow the kernel consume the same format that was used when generating modules. Supporting multiple formats at once is overkill IMO.

Indeed.

Quoted 10 lines
Quoted 8 lines

init/Kconfig config MODULE_COMPRESS_ZSTD · line 2317

Quoted 5 lines
init/Kconfig +5 -0
+	help
+
+	  Support for decompressing kernel modules by the kernel itself
+	  instead of relying on userspace to perform this task. Useful when
+	  load pinning security policy is enabled.

Shouldn't kernel decompression be faster too? If so, what's the point of doing it in userspace?

Make the kernel smaller?

Quoted 8 lines

Yes this I buy.

Quoted 2 lines

Have more flexibility with exotic compression
formats?

I just have a hunch that doing module decompression in the kernel will speed things quite a bit... any chance you can provide some before and after systemd-analyze ?

If you insist I can try running it, but it should be slower unless your memory controller is so slow that reading file from disk and dealing with page by page decompression is quicker than copying already decompressed data from userspace. We still reading and uncompressing file in kmod (to make sure the format is valid) and we can uncompress using large buffers (we are not concerned with using unswappable kernel memory).

Maybe in the future when we have streaming and accelerated in-kernel decompression API we could optimize for that in kmod and see some savings on very large modules.

Thanks.

Signature · 1 line
Dmitry
#9 lore raw
Quoted 46 lines · Dmitry Torokhov
Quoted 45 lines · Luis Chamberlain
Quoted 19 lines · Dmitry Torokhov
Quoted 9 lines · Luis Chamberlain
Quoted 8 lines · Dmitry Torokhov
init/Kconfig +0 -0
index cd23faa163d1..d90774ff7610 100644
@@ -2305,6 +2305,19 @@ config MODULE_COMPRESS_ZSTD
 
 endchoice
 
Quoted 8 lines

init/Kconfig config MODULE_COMPRESS_ZSTD · line 2312

Quoted 5 lines
init/Kconfig +5 -0
+config MODULE_DECOMPRESS
+	bool "Support in-kernel module decompression"
+	depends on MODULE_COMPRESS_GZIP || MODULE_COMPRESS_XZ
+	select ZLIB_INFLATE if MODULE_COMPRESS_GZIP
+	select XZ_DEC if MODULE_COMPRESS_XZ

What if MODULE_COMPRESS_GZIP and MODULE_COMPRESS_XZ are enabled? These are not mutually exclusive.

Quoted 3 lines

They are mutually exclusive, the kernel uses the same (one) compression method for all kernel modules that it generates (i.e we do not compress drivers/usb/... with gzip while drivers/net/... with xz).

Ah yes I failed to see the choice/prompt for it.

Quoted 3 lines

The idea here is to allow the kernel consume the same format that was used when generating modules. Supporting multiple formats at once is overkill IMO.

Indeed.

Quoted 10 lines
Quoted 8 lines

init/Kconfig config MODULE_COMPRESS_ZSTD · line 2317

Quoted 5 lines
init/Kconfig +5 -0
+	help
+
+	  Support for decompressing kernel modules by the kernel itself
+	  instead of relying on userspace to perform this task. Useful when
+	  load pinning security policy is enabled.

Shouldn't kernel decompression be faster too? If so, what's the point of doing it in userspace?

Make the kernel smaller?

Yes this I buy.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Have more flexibility with exotic compression
formats?

I just have a hunch that doing module decompression in the kernel will speed things quite a bit... any chance you can provide some before and after systemd-analyze ?

If you insist I can try running it,

If you can run the test, yes it would be appreciated.

Quoted 4 lines

but it should be slower unless your
memory controller is so slow that reading file from disk and dealing with page by page decompression is quicker than copying already decompressed data from userspace.

With userspace decompression I'd imagine we also have more context switches.

Quoted 2 lines

We still reading and uncompressing
file in kmod (to make sure the format is valid)

I don't understand, that seems wasteful.

Quoted 7 lines

and we can uncompress
using large buffers (we are not concerned with using unswappable kernel memory).

Maybe in the future when we have streaming and accelerated in-kernel decompression API we could optimize for that in kmod and see some savings on very large modules.

That would be very nice.

Luis

#10 lore raw

Hi Luis,

Happy New Year!

Quoted 50 lines · Luis Chamberlain
Quoted 49 lines · Dmitry Torokhov
Quoted 48 lines · Luis Chamberlain
Quoted 19 lines · Dmitry Torokhov
Quoted 9 lines · Luis Chamberlain
Quoted 8 lines · Dmitry Torokhov
init/Kconfig +0 -0
index cd23faa163d1..d90774ff7610 100644
@@ -2305,6 +2305,19 @@ config MODULE_COMPRESS_ZSTD
 
 endchoice
 
Quoted 8 lines

init/Kconfig config MODULE_COMPRESS_ZSTD · line 2312

Quoted 5 lines
init/Kconfig +5 -0
+config MODULE_DECOMPRESS
+	bool "Support in-kernel module decompression"
+	depends on MODULE_COMPRESS_GZIP || MODULE_COMPRESS_XZ
+	select ZLIB_INFLATE if MODULE_COMPRESS_GZIP
+	select XZ_DEC if MODULE_COMPRESS_XZ

What if MODULE_COMPRESS_GZIP and MODULE_COMPRESS_XZ are enabled? These are not mutually exclusive.

Quoted 3 lines

They are mutually exclusive, the kernel uses the same (one) compression method for all kernel modules that it generates (i.e we do not compress drivers/usb/... with gzip while drivers/net/... with xz).

Ah yes I failed to see the choice/prompt for it.

Quoted 3 lines

The idea here is to allow the kernel consume the same format that was used when generating modules. Supporting multiple formats at once is overkill IMO.

Indeed.

Quoted 10 lines
Quoted 8 lines

init/Kconfig config MODULE_COMPRESS_ZSTD · line 2317

Quoted 5 lines
init/Kconfig +5 -0
+	help
+
+	  Support for decompressing kernel modules by the kernel itself
+	  instead of relying on userspace to perform this task. Useful when
+	  load pinning security policy is enabled.

Shouldn't kernel decompression be faster too? If so, what's the point of doing it in userspace?

Make the kernel smaller?

Yes this I buy.

Quoted 2 lines

Have more flexibility with exotic compression
formats?

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

I just have a hunch that doing module decompression in the kernel will speed things quite a bit... any chance you can provide some before and after systemd-analyze ?

If you insist I can try running it,

If you can run the test, yes it would be appreciated.

OK, so I finally got around to doing it and the differences are pretty much noise, as I expected:

5.16.0-rc7: Startup finished in 5.022s (firmware) + 6.106s (loader) + 1.370s (kernel) + 5.685s (initrd) + 10.842s (userspace) = 29.026s
5.16.0-rc7-patched: Startup finished in 4.958s (firmware) + 6.701s (loader) + 1.382s (kernel) + 5.278s (initrd) + 10.822s (userspace) = 29.145s 5.16.0-rc7-patched: Startup finished in 4.953s (firmware) + 5.912s (loader) + 1.385s (kernel) + 5.327s (initrd) + 10.457s (userspace) = 28.036s

Also see attached.

Quoted 7 lines
Quoted 5 lines

but it should be slower unless your
memory controller is so slow that reading file from disk and dealing with page by page decompression is quicker than copying already decompressed data from userspace.

With userspace decompression I'd imagine we also have more context switches.

We have plenty of rescheduling points when we allocate new pages, so I expect we'll be getting preempted while in kernel as well if needed.

Quoted 5 lines
Quoted 3 lines

We still reading and uncompressing
file in kmod (to make sure the format is valid)

I don't understand, that seems wasteful.

This way we can make sure we are not feeding kernel garbage and abort early. Yes, we could just check signature and hope that the data is good (and if it is not the kernel will reject it) but this is not a hot path at all and amount of data we decompress is relatively small, so I do not think trying to optimize this makes much sense (as shown by the numbers above).

Quoted 10 lines
Quoted 8 lines

and we can uncompress
using large buffers (we are not concerned with using unswappable kernel memory).

Maybe in the future when we have streaming and accelerated in-kernel decompression API we could optimize for that in kmod and see some savings on very large modules.

That would be very nice.

Again, practical benefit of doing this is pretty much close to 0 in this particular case.

Thanks.

Signature · 1 line
Dmitry
#11 lore raw
Quoted 8 lines · Dmitry Torokhov

OK, so I finally got around to doing it and the differences are pretty much noise, as I expected:

5.16.0-rc7: Startup finished in 5.022s (firmware) + 6.106s (loader) + 1.370s (kernel) + 5.685s (initrd) + 10.842s (userspace) = 29.026s
5.16.0-rc7-patched: Startup finished in 4.958s (firmware) + 6.701s (loader) + 1.382s (kernel) + 5.278s (initrd) + 10.822s (userspace) = 29.145s 5.16.0-rc7-patched: Startup finished in 4.953s (firmware) + 5.912s (loader) + 1.385s (kernel) + 5.327s (initrd) + 10.457s (userspace) = 28.036s

Also see attached.

If kmod didn't do the decompression I suspect things might be slightly different, but I agree that given the different with kernel compression now being done, removing userespace compression might just be noise as well.

Quoted 11 lines
Quoted 4 lines
Quoted 2 lines

We still reading and uncompressing
file in kmod (to make sure the format is valid)

I don't understand, that seems wasteful.

This way we can make sure we are not feeding kernel garbage and abort early. Yes, we could just check signature and hope that the data is good (and if it is not the kernel will reject it) but this is not a hot path at all and amount of data we decompress is relatively small, so I do not think trying to optimize this makes much sense (as shown by the numbers above).

Sure. And if an LSM is used, one would assume the LSM does its own finit module checks.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

and we can uncompress
using large buffers (we are not concerned with using unswappable kernel memory).

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Maybe in the future when we have streaming and accelerated in-kernel decompression API we could optimize for that in kmod and see some savings on very large modules.

That would be very nice.

Again, practical benefit of doing this is pretty much close to 0 in this particular case.

Based on what is observed so far, I agree.

Luis