FAILED: patch "[PATCH] kho: warn and fail on metadata or preserved memory in scratch" failed to apply to 6.17-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 6.17-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.17.y
git checkout FETCH_HEAD
git cherry-pick -x e38f65d317df1fd2dcafe614d9c537475ecf9992
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2025112117-pursuant-varmint-6bbc@gregkh' --subject-prefix 'PATCH 6.17.y' HEAD^..
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From e38f65d317df1fd2dcafe614d9c537475ecf9992 Mon Sep 17 00:00:00 2001
From: Pasha Tatashin <[email protected]>
Date: Mon, 20 Oct 2025 20:08:50 -0400
Subject: [PATCH] kho: warn and fail on metadata or preserved memory in scratch
area
Patch series "KHO: kfence + KHO memory corruption fix", v3.
This series fixes a memory corruption bug in KHO that occurs when KFENCE is enabled.
The root cause is that KHO metadata, allocated via kzalloc(), can be randomly serviced by kfence_alloc(). When a kernel boots via KHO, the early memblock allocator is restricted to a "scratch area". This forces the KFENCE pool to be allocated within this scratch area, creating a conflict. If KHO metadata is subsequently placed in this pool, it gets corrupted during the next kexec operation.
Google is using KHO and have had obscure crashes due to this memory corruption, with stacks all over the place. I would prefer this fix to be properly backported to stable so we can also automatically consume it once we switch to the upstream KHO.
Patch 1/3 introduces a debug-only feature (CONFIG_KEXEC_HANDOVER_DEBUG) that adds checks to detect and fail any operation that attempts to place KHO metadata or preserved memory within the scratch area. This serves as a validation and diagnostic tool to confirm the problem without affecting production builds.
Patch 2/3 Increases bitmap to PAGE_SIZE, so buddy allocator can be used.
Patch 3/3 Provides the fix by modifying KHO to allocate its metadata directly from the buddy allocator instead of slab. This bypasses the KFENCE interception entirely.
This patch (of 3):
It is invalid for KHO metadata or preserved memory regions to be located within the KHO scratch area, as this area is overwritten when the next kernel is loaded, and used early in boot by the next kernel. This can lead to memory corruption.
Add checks to kho_preserve_* and KHO's internal metadata allocators (xa_load_or_alloc, new_chunk) to verify that the physical address of the memory does not overlap with any defined scratch region. If an overlap is detected, the operation will fail and a WARN_ON is triggered. To avoid performance overhead in production kernels, these checks are enabled only when CONFIG_KEXEC_HANDOVER_DEBUG is selected.
kernel/Kconfig.kexec +9 -0
@@ -109,6 +109,15 @@ config KEXEC_HANDOVER to keep data or state alive across the kexec. For this to work, both source and target kernels need to have this option enabled. +config KEXEC_HANDOVER_DEBUG + bool "Enable Kexec Handover debug checks" + depends on KEXEC_HANDOVER + help + This option enables extra sanity checks for the Kexec Handover + subsystem. Since, KHO performance is crucial in live update + scenarios and the extra code might be adding overhead it is + only optionally enabled. + config CRASH_DUMP bool "kernel crash dumps" default ARCH_DEFAULT_CRASH_DUMP
kernel/Makefile +1 -0
@@ -83,6 +83,7 @@ obj-$(CONFIG_KEXEC) += kexec.o obj-$(CONFIG_KEXEC_FILE) += kexec_file.o obj-$(CONFIG_KEXEC_ELF) += kexec_elf.o obj-$(CONFIG_KEXEC_HANDOVER) += kexec_handover.o +obj-$(CONFIG_KEXEC_HANDOVER_DEBUG) += kexec_handover_debug.o obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o obj-$(CONFIG_COMPAT) += compat.o obj-$(CONFIG_CGROUPS) += cgroup/
kernel/kexec_handover.c +38 -19
140 lines not on this page · raw message
kernel/kexec_handover_debug.c new file +25 -0
@@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* kexec_handover_debug.c - kexec handover optional debug functionality +* Copyright (C) 2025 Google LLC, Pasha Tatashin <[email protected]> +*/ + +#define pr_fmt (fmt) "KHO: " fmt + +#include "kexec_handover_internal.h" + +bool kho_scratch_overlap (phys_addr_t phys,size_t size) +{ +phys_addr_t scratch_start, scratch_end; +unsigned int i; + +for (i =0 ; i < kho_scratch_cnt; i++) { + scratch_start = kho_scratch[i].addr; + scratch_end = kho_scratch[i].addr + kho_scratch[i].size; + +if (phys < scratch_end && (phys + size) > scratch_start) +return true; + } + +return false; +}
kernel/kexec_handover_internal.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef LINUX_KEXEC_HANDOVER_INTERNAL_H +#define LINUX_KEXEC_HANDOVER_INTERNAL_H + +#include <linux/kexec_handover.h> +#include <linux/types.h> + +extern struct kho_scratch *kho_scratch; +extern unsigned int kho_scratch_cnt; + +#ifdef CONFIG_KEXEC_HANDOVER_DEBUG +bool kho_scratch_overlap (phys_addr_t phys,size_t size); +#else +static inline bool kho_scratch_overlap (phys_addr_t phys,size_t size) +{ +return false; +} +#endif /* CONFIG_KEXEC_HANDOVER_DEBUG */ + +#endif /* LINUX_KEXEC_HANDOVER_INTERNAL_H */
Sasha Levin
maintainerPATCH 1/2 kho: check if kho is finalized in __kho_preserve_order()
From: "Mike Rapoport (Microsoft)" <[email protected]>
[ Upstream commit 469661d0d3a55a7ba1e7cb847c26baf78cace086 ]
Patch series "kho: add support for preserving vmalloc allocations", v5.
Following the discussion about preservation of memfd with LUO [1] these patches add support for preserving vmalloc allocations.
Any KHO uses case presumes that there's a data structure that lists physical addresses of preserved folios (and potentially some additional metadata). Allowing vmalloc preservations with KHO allows scalable preservation of such data structures.
For instance, instead of allocating array describing preserved folios in the fdt, memfd preservation can use vmalloc:
preserved_folios = vmalloc_array(nr_folios, sizeof(*preserved_folios));
memfd_luo_preserve_folios(preserved_folios, folios, nr_folios);
kho_preserve_vmalloc(preserved_folios, &folios_info);This patch (of 4):
Instead of checking if kho is finalized in each caller of __kho_preserve_order(), do it in the core function itself.
Stable-dep-of: e38f65d317df ("kho: warn and fail on metadata or preserved memory in scratch area")
--- kernel/kexec_handover.c | 55 +++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 29 deletions(-)
kernel/kexec_handover.c +26 -29
@@ -91,6 +91,29 @@ struct kho_serialization {struct khoser_mem_chunk *preserved_mem_map; }; +struct kho_out { +struct blocking_notifier_head chain_head; + +struct dentry *dir; + +struct mutex lock;/* protects KHO FDT finalization */ + +struct kho_serialization ser; +bool finalized; +}; + +static struct kho_out kho_out = { + .chain_head =BLOCKING_NOTIFIER_INIT (kho_out.chain_head), + .lock =__MUTEX_INITIALIZER (kho_out.lock), + .ser = { + .fdt_list =LIST_HEAD_INIT (kho_out.ser.fdt_list), + .track = { + .orders =XARRAY_INIT (kho_out.ser.track.orders,0 ), + }, + }, + .finalized = false, +}; +static void *xa_load_or_alloc (struct xarray *xa,unsigned long index,size_t sz) {void *elm, *res;
@@ -149,6 +172,9 @@ static int __kho_preserve_order(struct kho_mem_track *track, unsigned long pfn,might_sleep (); +if (kho_out.finalized) +return -EBUSY ; + physxa =xa_load (&track->orders, order);if (!physxa) {int err;
@@ -631,29 +657,6 @@ int kho_add_subtree(struct kho_serialization *ser, const char *name, void *fdt) }EXPORT_SYMBOL_GPL (kho_add_subtree); -struct kho_out { -struct blocking_notifier_head chain_head; - -struct dentry *dir; - -struct mutex lock;/* protects KHO FDT finalization */ - -struct kho_serialization ser; -bool finalized; -}; - -static struct kho_out kho_out = { - .chain_head =BLOCKING_NOTIFIER_INIT (kho_out.chain_head), - .lock =__MUTEX_INITIALIZER (kho_out.lock), - .ser = { - .fdt_list =LIST_HEAD_INIT (kho_out.ser.fdt_list), - .track = { - .orders =XARRAY_INIT (kho_out.ser.track.orders,0 ), - }, - }, - .finalized = false, -}; -int register_kho_notifier (struct notifier_block *nb) {return blocking_notifier_chain_register (&kho_out.chain_head, nb);
@@ -681,9 +684,6 @@ int kho_preserve_folio(struct folio *folio)const unsigned int order =folio_order (folio);struct kho_mem_track *track = &kho_out.ser.track; -if (kho_out.finalized) -return -EBUSY ; -return __kho_preserve_order (track, pfn, order); }EXPORT_SYMBOL_GPL (kho_preserve_folio);
@@ -707,9 +707,6 @@ int kho_preserve_phys(phys_addr_t phys, size_t size)int err =0 ;struct kho_mem_track *track = &kho_out.ser.track; -if (kho_out.finalized) -return -EBUSY ; -if (!PAGE_ALIGNED (phys) || !PAGE_ALIGNED (size))return -EINVAL ;
Signature · 1 line
2.51.0
Sasha Levin
maintainerPATCH 2/2 kho: warn and fail on metadata or preserved memory in scratch area
From: Pasha Tatashin <[email protected]>
[ Upstream commit e38f65d317df1fd2dcafe614d9c537475ecf9992 ]
Patch series "KHO: kfence + KHO memory corruption fix", v3.
This series fixes a memory corruption bug in KHO that occurs when KFENCE is enabled.
The root cause is that KHO metadata, allocated via kzalloc(), can be randomly serviced by kfence_alloc(). When a kernel boots via KHO, the early memblock allocator is restricted to a "scratch area". This forces the KFENCE pool to be allocated within this scratch area, creating a conflict. If KHO metadata is subsequently placed in this pool, it gets corrupted during the next kexec operation.
Google is using KHO and have had obscure crashes due to this memory corruption, with stacks all over the place. I would prefer this fix to be properly backported to stable so we can also automatically consume it once we switch to the upstream KHO.
Patch 1/3 introduces a debug-only feature (CONFIG_KEXEC_HANDOVER_DEBUG) that adds checks to detect and fail any operation that attempts to place KHO metadata or preserved memory within the scratch area. This serves as a validation and diagnostic tool to confirm the problem without affecting production builds.
Patch 2/3 Increases bitmap to PAGE_SIZE, so buddy allocator can be used.
Patch 3/3 Provides the fix by modifying KHO to allocate its metadata directly from the buddy allocator instead of slab. This bypasses the KFENCE interception entirely.
This patch (of 3):
It is invalid for KHO metadata or preserved memory regions to be located within the KHO scratch area, as this area is overwritten when the next kernel is loaded, and used early in boot by the next kernel. This can lead to memory corruption.
Add checks to kho_preserve_* and KHO's internal metadata allocators (xa_load_or_alloc, new_chunk) to verify that the physical address of the memory does not overlap with any defined scratch region. If an overlap is detected, the operation will fail and a WARN_ON is triggered. To avoid performance overhead in production kernels, these checks are enabled only when CONFIG_KEXEC_HANDOVER_DEBUG is selected.
--- kernel/Kconfig.kexec | 9 +++++ kernel/Makefile | 1 + kernel/kexec_handover.c | 57 +++++++++++++++++++++----------- kernel/kexec_handover_debug.c | 25 ++++++++++++++ kernel/kexec_handover_internal.h | 20 +++++++++++ 5 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 kernel/kexec_handover_debug.c create mode 100644 kernel/kexec_handover_internal.h
kernel/Kconfig.kexec +9 -0
@@ -109,6 +109,15 @@ config KEXEC_HANDOVER to keep data or state alive across the kexec. For this to work, both source and target kernels need to have this option enabled. +config KEXEC_HANDOVER_DEBUG + bool "Enable Kexec Handover debug checks" + depends on KEXEC_HANDOVER + help + This option enables extra sanity checks for the Kexec Handover + subsystem. Since, KHO performance is crucial in live update + scenarios and the extra code might be adding overhead it is + only optionally enabled. + config CRASH_DUMP bool "kernel crash dumps" default ARCH_DEFAULT_CRASH_DUMP
kernel/Makefile +1 -0
@@ -82,6 +82,7 @@ obj-$(CONFIG_KEXEC) += kexec.o obj-$(CONFIG_KEXEC_FILE) += kexec_file.o obj-$(CONFIG_KEXEC_ELF) += kexec_elf.o obj-$(CONFIG_KEXEC_HANDOVER) += kexec_handover.o +obj-$(CONFIG_KEXEC_HANDOVER_DEBUG) += kexec_handover_debug.o obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o obj-$(CONFIG_COMPAT) += compat.o obj-$(CONFIG_CGROUPS) += cgroup/
kernel/kexec_handover.c +38 -19
140 lines not on this page · raw message
kernel/kexec_handover_debug.c new file +25 -0
@@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* kexec_handover_debug.c - kexec handover optional debug functionality +* Copyright (C) 2025 Google LLC, Pasha Tatashin <[email protected]> +*/ + +#define pr_fmt (fmt) "KHO: " fmt + +#include "kexec_handover_internal.h" + +bool kho_scratch_overlap (phys_addr_t phys,size_t size) +{ +phys_addr_t scratch_start, scratch_end; +unsigned int i; + +for (i =0 ; i < kho_scratch_cnt; i++) { + scratch_start = kho_scratch[i].addr; + scratch_end = kho_scratch[i].addr + kho_scratch[i].size; + +if (phys < scratch_end && (phys + size) > scratch_start) +return true; + } + +return false; +}
kernel/kexec_handover_internal.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef LINUX_KEXEC_HANDOVER_INTERNAL_H +#define LINUX_KEXEC_HANDOVER_INTERNAL_H + +#include <linux/kexec_handover.h> +#include <linux/types.h> + +extern struct kho_scratch *kho_scratch; +extern unsigned int kho_scratch_cnt; + +#ifdef CONFIG_KEXEC_HANDOVER_DEBUG +bool kho_scratch_overlap (phys_addr_t phys,size_t size); +#else +static inline bool kho_scratch_overlap (phys_addr_t phys,size_t size) +{ +return false; +} +#endif /* CONFIG_KEXEC_HANDOVER_DEBUG */ + +#endif /* LINUX_KEXEC_HANDOVER_INTERNAL_H */
Signature · 1 line
2.51.0