Skip to contentSkip to conversation

kho: fixes for kho_init() failures

PATCH Applied Chris Down 3 participants 5 messages Nov 28 – Nov 29

Subsystems (3)
lore
Share & tools
lore

Chris Down

author
#0 lore raw

v2 cover kho: fixes for kho_init() failures

When kho_init() fails on a KHO boot, its error path frees the bootmem areas that were inherited from the previous kernel, even though the running kernel was loaded into them and is still using them. The new kernel then oopses in kho_init() and panics.

The easiest way to get there turned out to be debugfs=off. With CONFIG_KEXEC_HANDOVER_DEBUGFS=y, kho_init() fails when it can't create its debugfs entries, so a KHO kexec into a kernel with debugfs disabled is enough to hit the panic. That's a bug in its own right, because it means KHO and LUO can't be used at all on a kernel that has debugfs disabled, although nothing in KHO needs debugfs any more.

This series stops the error path from freeing bootmem areas that it didn't reserve, and stops kho_init() from failing when debugfs is disabled.

v2:

- Use is_kho_boot() instead of checking fdt (Mike)
- Say who reserves the bootmem areas on each kind of boot (Mike)
- Describe how to trigger it and add the oops (Mike)
- Add a patch so that kho_init() doesn't fail when debugfs is disabled
- Rebase onto liveupdate/fixes

v1: https://lore.kernel.org/r/[email protected]

Chris Down (2):
kho: don't release incoming bootmem areas when kho_init() fails
kho: don't fail kho_init() when debugfs is disabled

 kernel/liveupdate/kexec_handover.c          | 19 ++++++++-----------
 kernel/liveupdate/kexec_handover_debugfs.c  | 19 +++++++++++--------
 kernel/liveupdate/kexec_handover_internal.h |  8 ++++----
 3 files changed, 23 insertions(+), 23 deletions(-)

base-commit: fa1c0f103fd68558613d3df76e222463c6e4fc37

Signature · 1 line
2.56.1
author
#1 lore raw

PATCH v2 1/2 kho: don't release incoming bootmem areas when kho_init() fails

On a non-KHO boot, kho_reserve_bootmem() reserves the bootmem areas in memblock, and they stay reserved until kho_init() releases them as CMA.

On a KHO boot, the bootmem areas are inherited from the previous kernel. kexec loads the new kernel into them. kho_populate() adds them to memblock as free memory, because early allocations are made from them. Whatever is still free afterwards goes to the buddy allocator like any other memory.

Unfortunately however, if kho_init() fails, err_free_bootmem currently frees every area with free_reserved_area() on both kinds of boot. On a KHO boot this frees pages that are still in use, the kernel image among them, and frees buddy pages a second time.

With CONFIG_KEXEC_HANDOVER_DEBUGFS=y, kho_init() fails if debugfs is disabled, so a KHO kexec into a kernel with debugfs=off added to its command line is enough to hit this. In a VM, the new kernel then oopses in kho_init() and panics.

    BUG: kernel NULL pointer dereference, address: 0000000000000008
    RIP: 0010:free_pcppages_bulk+0x10f/0x280
    Call Trace:
     free_frozen_page_commit+0x1fa/0x430
     __free_frozen_pages+0x205/0x600
     __free_reserved_area+0x7b/0x120
     free_reserved_area+0x89/0xe0
     kho_init+0x301/0x3e0
     do_one_initcall+0x89/0x260
     kernel_init_freeable+0x1e0/0x250
     kernel_init+0x1f/0x140
    Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000009

To fix this, let's only free the areas when this isn't a KHO boot, because that's when kho_reserve_bootmem() reserved them.

With this applied, kho_init() still fails after the same kexec, but the kernel boots and the bootmem areas are left alone. A non-KHO boot with debugfs=off still frees the areas it reserved.

Fixes c609c144b0e8 ("kexec: add KHO parsing support")
---
 kernel/liveupdate/kexec_handover.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
kernel/liveupdate/kexec_handover.c +6 -4
index 42c6609a1766..663dd22e7172 100644
@@ -1899,11 +1899,13 @@ err_free_kho_radix_tree:
 	kho_radix_destroy_tree(tree);
 err_free_bootmem:
 	kho_out.fdt = NULL;
-	for (int i = 0; i < kho_bootmem_cnt; i++) {
-		void *start = __va(kho_bootmem[i].addr);
-		void *end = start + kho_bootmem[i].size;
+	if (!is_kho_boot()) {
+		for (int i = 0; i < kho_bootmem_cnt; i++) {
+			void *start = __va(kho_bootmem[i].addr);
+			void *end = start + kho_bootmem[i].size;
 
-		free_reserved_area(start, end, -1, "");
+			free_reserved_area(start, end, -1, "");
+		}
 	}
 	kho_enable = false;
 	return err;
Signature · 1 line
2.56.1
author
#2 lore raw

PATCH v2 2/2 kho: don't fail kho_init() when debugfs is disabled

In reply to Chris Down

kho_init() fails if kho_debugfs_init() or kho_out_debugfs_init() can't create their debugfs entries, and a failed kho_init() turns KHO off. That made sense while KHO could only be finalised through debugfs, but commit 03d3963464a4 ("kho: make debugfs interface optional") made the debugfs interface optional, and these days KHO doesn't need any of its files to work.

This is a problem because debugfs can be disabled at boot with debugfs=off, or by default with CONFIG_DEBUG_FS_ALLOW_NONE, in which case every attempt to create a debugfs entry returns -EPERM. Given that CONFIG_KEXEC_HANDOVER_DEBUGFS defaults to y when KHO is built in, KHO and LUO can't be used at all on such a kernel. With kho=on liveupdate=on debugfs=off, /dev/liveupdate never appears and a kexec hands nothing over. kho_init() doesn't log why, so the only sign of it is the WARN_ON_ONCE() that LUO then trips in kho_radix_add_key().

So, let's carry on without the debugfs entries when they can't be created, as kho_in_debugfs_init() already does for the incoming FDT.

Two things then run without KHO's debugfs directories for the first time. kho_debugfs_blob_add() would fail and trip the WARN_ON_ONCE() in its callers, so let's make it do nothing when it has no directory to add the blob to. kho_in_debugfs_init() would log an error on every KHO boot, so let's make it return early when the kho directory doesn't exist.

With this applied, test_kho and luo_kexec_simple both get their state back across a KHO kexec between two kernels booted with debugfs=off.

Fixes 03d3963464a4 ("kho: make debugfs interface optional")
---
 kernel/liveupdate/kexec_handover.c          |  9 ++-------
 kernel/liveupdate/kexec_handover_debugfs.c  | 19 +++++++++++--------
 kernel/liveupdate/kexec_handover_internal.h |  8 ++++----
 3 files changed, 17 insertions(+), 19 deletions(-)
kernel/liveupdate/kexec_handover.c +2 -7
index 663dd22e7172..4cee37ad9426 100644
@@ -1847,13 +1847,8 @@ static __init int kho_init(void)
 		goto err_free_kho_radix_tree;
 	}
 
-	err = kho_debugfs_init();
-	if (err)
-		goto err_free_fdt;
-
-	err = kho_out_debugfs_init(&kho_out.dbg);
-	if (err)
-		goto err_free_fdt;
+	kho_debugfs_init();
+	kho_out_debugfs_init(&kho_out.dbg);
 
 	err = kho_out_fdt_setup();
 	if (err)
kernel/liveupdate/kexec_handover_debugfs.c +11 -8
index 3a91c47ad7a6..cedfc955391a 100644
@@ -59,6 +59,10 @@ int kho_debugfs_blob_add(struct kho_debugfs *dbg, const char *name,
 	else
 		dir = dbg->sub_fdt_dir;
 
+	/* The directories are NULL if they could not be created. */
+	if (!dir)
+		return 0;
+
 	return __kho_debugfs_blob_add(&dbg->fdt_list, dir, name, blob, size);
 }
 
@@ -101,6 +105,9 @@ __init void kho_in_debugfs_init(struct kho_debugfs *dbg, const void *fdt)
 
 	INIT_LIST_HEAD(&dbg->fdt_list);
 
+	if (IS_ERR(debugfs_root))
+		return;
+
 	dir = debugfs_create_dir("in", debugfs_root);
 	if (IS_ERR(dir)) {
 		err = PTR_ERR(dir);
@@ -129,7 +136,7 @@ err_out:
 	}
 }
 
-__init int kho_out_debugfs_init(struct kho_debugfs *dbg)
+__init void kho_out_debugfs_init(struct kho_debugfs *dbg)
 {
 	struct dentry *dir, *f, *sub_fdt_dir;
 
@@ -137,7 +144,7 @@ __init int kho_out_debugfs_init(struct kho_debugfs *dbg)
 
 	dir = debugfs_create_dir("out", debugfs_root);
 	if (IS_ERR(dir))
-		return -ENOMEM;
+		return;
 
 	sub_fdt_dir = debugfs_create_dir("sub_fdts", dir);
 	if (IS_ERR(sub_fdt_dir))
@@ -155,17 +162,13 @@ __init int kho_out_debugfs_init(struct kho_debugfs *dbg)
 
 	dbg->dir = dir;
 	dbg->sub_fdt_dir = sub_fdt_dir;
-	return 0;
+	return;
 
 err_rmdir:
 	debugfs_remove_recursive(dir);
-	return -ENOENT;
 }
 
-__init int kho_debugfs_init(void)
+__init void kho_debugfs_init(void)
 {
 	debugfs_root = debugfs_create_dir("kho", NULL);
-	if (IS_ERR(debugfs_root))
-		return -ENOENT;
-	return 0;
 }
kernel/liveupdate/kexec_handover_internal.h +4 -4
index e9685e1ef6d7..424467a98da2 100644
@@ -23,17 +23,17 @@ extern struct kho_bootmem *kho_bootmem;
 extern unsigned int kho_bootmem_cnt;
 
 #ifdef CONFIG_KEXEC_HANDOVER_DEBUGFS
-int kho_debugfs_init(void);
+void kho_debugfs_init(void);
 void kho_in_debugfs_init(struct kho_debugfs *dbg, const void *fdt);
-int kho_out_debugfs_init(struct kho_debugfs *dbg);
+void kho_out_debugfs_init(struct kho_debugfs *dbg);
 int kho_debugfs_blob_add(struct kho_debugfs *dbg, const char *name,
 			 const void *blob, size_t size, bool root);
 void kho_debugfs_blob_remove(struct kho_debugfs *dbg, void *blob);
 #else
-static inline int kho_debugfs_init(void) { return 0; }
+static inline void kho_debugfs_init(void) { }
 static inline void kho_in_debugfs_init(struct kho_debugfs *dbg,
 				       const void *fdt) { }
-static inline int kho_out_debugfs_init(struct kho_debugfs *dbg) { return 0; }
+static inline void kho_out_debugfs_init(struct kho_debugfs *dbg) { }
 static inline int kho_debugfs_blob_add(struct kho_debugfs *dbg,
 				       const char *name, const void *blob,
 				       size_t size, bool root) { return 0; }
Signature · 1 line
2.56.1
maintainer Applied
#4 lore raw
In reply to Chris Down

From: Mike Rapoport (Microsoft) <[email protected]>

Quoted 5 lines · Chris Down

When kho_init() fails on a KHO boot, its error path frees the bootmem areas that were inherited from the previous kernel, even though the running kernel was loaded into them and is still using them. The new kernel then oopses in kho_init() and panics.

Quoted 8 lines

The easiest way to get there turned out to be debugfs=off. With CONFIG_KEXEC_HANDOVER_DEBUGFS=y, kho_init() fails when it can't create its debugfs entries, so a KHO kexec into a kernel with debugfs disabled is enough to hit the panic. That's a bug in its own right, because it means KHO and LUO can't be used at all on a kernel that has debugfs disabled, although nothing in KHO needs debugfs any more.

[...]

Applied to next branch of liveupdate/linux.git tree, thanks!

[1/2] kho: don't release incoming bootmem areas when kho_init() fails

      commit: 0e88c65680e2d0afd0abc557d138aed52ed43593
[2/2] kho: don't fail kho_init() when debugfs is disabled
      commit: 5b8a3f8fc1e81bf5ae4c77e8b644a5e6883466a0

tree: https://git.kernel.org/pub/scm/linux/kernel/git/liveupdate/linux
branch: next

--
Sincerely yours,
Mike.