kfence: fix pool allocation failure handling
Subsystems (3)
v2 cover kfence: fix pool allocation failure handling
From: Jackie Liu <[email protected]>
Fix the cleanup address and page reuse window in failed late KFENCE initialization. Also let RISC-V boot if the early pool reservation fails.
Patch 2 depends on patch 1. Patch 3 can be applied independently.
Changes in v2:
- Add Marco Elver's Reviewed-by to patches 1 and 2; their code is unchanged.
- Drop the unused asm/kfence.h include from patch 3. Sashiko reported that its unconditional inclusion breaks RV32 and NOMMU builds.
- Credit Sashiko and kernel test robot with Reported-by and Closes tags in patch 3.
v1: https://lore.kernel.org/all/[email protected]/
Reports:
https://sashiko.dev/#/patchset/[email protected]?part=3
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/oe-kbuild-all/[email protected]/
Validation on AArch64 Linux with Clang 17.0.6 and GNU ld 2.40:
- RV32 and NOMMU: reproduced v1 failures; v2 init.o builds successfully.
- RV64 defconfig with CONFIG_KFENCE=y: init.o, mm/kfence/core.o, and the full Image build pass without warnings.
The existing NOMMU warning in include/asm-generic/io.h is identical to that from the unpatched base. No boot or failure-injection tests were run. All three patches pass checkpatch.pl --strict and git diff --check.
OpenAI Codex assisted with review, adaptation, wording, and validation.
Jackie Liu (3):
kfence: fix pool cleanup on metadata allocation failure
kfence: clear the pool pointer before freeing pages
riscv: keep booting if KFENCE pool reservation fails
arch/riscv/include/asm/kfence.h | 4 +++- arch/riscv/mm/init.c | 18 ++++++++++++++---- mm/kfence/core.c | 6 ++++-- 3 files changed, 21 insertions(+), 7 deletions(-)
base-commit: 7be4bc52fd9e0e55dc9a2e0232b89427d2c4b614
Signature · 1 line
2.56.0
PATCH v2 1/3 kfence: fix pool cleanup on metadata allocation failure
From: Jackie Liu <[email protected]>
kfence_init_late() saves __kfence_pool in addr before allocating the pool, so addr is still NULL if metadata allocation fails. The error path then tries to free pages using that address.
Set addr after pool allocation so cleanup frees the allocated pool.
--- mm/kfence/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/kfence/core.c +2 -1
@@ -999,7 +999,7 @@ static int kfence_init_late(void) {const unsigned long nr_pages_pool =KFENCE_POOL_SIZE /PAGE_SIZE ;const unsigned long nr_pages_meta =KFENCE_METADATA_SIZE /PAGE_SIZE ; -unsigned long addr = (unsigned long )__kfence_pool; +unsigned long addr;unsigned long free_size =KFENCE_POOL_SIZE ;int err = -ENOMEM ;
@@ -1032,6 +1032,7 @@ static int kfence_init_late(void) GFP_KERNEL | __GFP_SKIP_KASAN);#endif + addr = (unsigned long )__kfence_pool;if (!kfence_metadata_init) goto free_pool;
Signature · 1 line
2.56.0
PATCH v2 2/3 kfence: clear the pool pointer before freeing pages
In reply to Jackie LiuFrom: Jackie Liu <[email protected]>
Late initialization frees a failed pool before clearing __kfence_pool. Slab allocations can reuse those pages in between and be mistaken for KFENCE objects by is_kfence_address(), even though KFENCE is not enabled.
Clear __kfence_pool before freeing the pages, and use the saved address for cleanup.
--- mm/kfence/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/kfence/core.c +2 -1
@@ -1052,16 +1052,17 @@ static int kfence_init_late(void)free_contig_range (page_to_pfn (virt_to_page ((void *)kfence_metadata_init)), nr_pages_meta); free_pool: +WRITE_ONCE (__kfence_pool,NULL );free_contig_range (page_to_pfn (virt_to_page ((void *)addr)), free_size /PAGE_SIZE );#else free_pages_exact ((void *)kfence_metadata_init,KFENCE_METADATA_SIZE ); free_pool: +WRITE_ONCE (__kfence_pool,NULL );free_pages_exact ((void *)addr, free_size);#endif kfence_metadata_init =NULL ; - __kfence_pool =NULL ;return err; }
Signature · 1 line
2.56.0
PATCH v2 3/3 riscv: keep booting if KFENCE pool reservation fails
In reply to Jackie LiuFrom: Jackie Liu <[email protected]>
A failed KFENCE pool reservation hits BUG_ON() and stops boot. Continue building the linear map and disable KFENCE if the reservation fails.
Reject later pool initialization as well, since a replacement pool may have huge-page mappings that kfence_protect_page() cannot handle.
--- arch/riscv/include/asm/kfence.h | 4 +++- arch/riscv/mm/init.c | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-)
arch/riscv/include/asm/kfence.h +3 -1
@@ -9,9 +9,11 @@#include <asm/cacheflush.h> #include <asm/pgtable.h> +extern bool kfence_disabled; +static inline bool arch_kfence_init_pool (void ) { -return true; +return !kfence_disabled; }static inline bool kfence_protect_page (unsigned long addr,bool protect)
arch/riscv/mm/init.c +14 -4
@@ -44,6 +44,10 @@ DECLARE_BITMAP(new_valid_map_cpus, NR_CPUS);struct kernel_mapping kernel_map__ro_after_init ;EXPORT_SYMBOL (kernel_map); +#ifdef CONFIG_KFENCE +bool kfence_disabled__ro_after_init ; +#endif +#ifdef CONFIG_64BIT u64 satp_mode__ro_after_init =SATP_MODE_57 ;#else
@@ -1229,10 +1233,13 @@ static void __init create_linear_mapping_page_table(void)* for this region. */ kfence_pool =memblock_phys_alloc (KFENCE_POOL_SIZE ,PAGE_SIZE ); -BUG_ON (!kfence_pool); - -memblock_mark_nomap (kfence_pool,KFENCE_POOL_SIZE ); - __kfence_pool =__va (kfence_pool); +if (kfence_pool) { +memblock_mark_nomap (kfence_pool,KFENCE_POOL_SIZE ); + __kfence_pool =__va (kfence_pool); + }else { +pr_err ("failed to allocate kfence pool\n" ); + kfence_disabled = true; + }#endif /* Map all memory banks in the linear mapping */
@@ -1253,6 +1260,9 @@ static void __init create_linear_mapping_page_table(void)#endif #ifdef CONFIG_KFENCE +if (!kfence_pool) +return ; +create_linear_mapping_range (kfence_pool, kfence_pool +KFENCE_POOL_SIZE ,PAGE_SIZE ,NULL );memblock_clear_nomap (kfence_pool,KFENCE_POOL_SIZE );
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v2 3/3] riscv: keep booting if KFENCE pool reservation fails
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] riscv: kfence: incomplete replacement pool rejection and fallback memblock leak --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] riscv: kfence: incomplete replacement pool rejection and fallback memblock leak --
commit 3fb224ed3ecf516e32ff76314492cf99262a5a4f
Author: Jackie Liu <[email protected]>
riscv: keep booting if KFENCE pool reservation fails
This commit avoids hitting BUG_ON() during linear mapping setup when the early KFENCE pool reservation fails by tracking the failure in kfence_disabled and returning !kfence_disabled from arch_kfence_init_pool().
Quoted 37 lines
arch/riscv/include/asm/kfence.h +3
-1@@ -9,9 +9,11 @@#include <asm/cacheflush.h> #include <asm/pgtable.h> +extern bool kfence_disabled; +static inline bool arch_kfence_init_pool (void ) { -return true; +return !kfence_disabled; }static inline bool kfence_protect_page (unsigned long addr,bool protect)arch/riscv/mm/init.c +4
-1@@ -44,6 +44,10 @@ DECLARE_BITMAP(new_valid_map_cpus, NR_CPUS);struct kernel_mapping kernel_map__ro_after_init ;EXPORT_SYMBOL (kernel_map); +#ifdef CONFIG_KFENCE +bool kfence_disabled__ro_after_init ; +#endif +#ifdef CONFIG_64BIT u64 satp_mode__ro_after_init =SATP_MODE_57 ;#else @@ -1229,10 +1233,13 @@ static void __init create_linear_mapping_page_table(void)* for this region. */ kfence_pool =memblock_phys_alloc (KFENCE_POOL_SIZE ,PAGE_SIZE ); -BUG_ON (!kfence_pool);
arch/riscv/mm/init.c static void __init create_linear_mapping_page_table(void) · line 1242
Quoted 10 lines
arch/riscv/mm/init.c +7
-3- -memblock_mark_nomap (kfence_pool,KFENCE_POOL_SIZE ); - __kfence_pool =__va (kfence_pool); +if (kfence_pool) { +memblock_mark_nomap (kfence_pool,KFENCE_POOL_SIZE ); + __kfence_pool =__va (kfence_pool); + }else { +pr_err ("failed to allocate kfence pool\n" ); + kfence_disabled = true; + }
[Severity: High]
Does setting kfence_disabled without clearing kfence_sample_interval still allow a fallback memblock allocation and a late hugepage-backed replacement pool?
In mm/kfence/core.c:kfence_alloc_pool_and_metadata():
if (!kfence_sample_interval)
return;
/*
* If the pool has already been initialized by arch, there is no need to
* re-allocate the memory pool.
*/
if (!__kfence_pool)
__kfence_pool = memblock_alloc(KFENCE_POOL_SIZE, PAGE_SIZE);
...
kfence_metadata_init = memblock_alloc(KFENCE_METADATA_SIZE, PAGE_SIZE);
if (!kfence_metadata_init) {
pr_err("failed to allocate metadata\n");
memblock_free(__kfence_pool, KFENCE_POOL_SIZE);
__kfence_pool = NULL;
}There look to be two issues with relying solely on kfence_disabled here:
First, when memblock_phys_alloc() fails in create_linear_mapping_page_table(), __kfence_pool remains NULL while kfence_sample_interval is still non-zero. Later in mm_core_init(), kfence_alloc_pool_and_metadata() will allocate a fallback __kfence_pool and kfence_metadata_init from memblock. When kfence_init() -> kfence_init_pool_early() subsequently runs and sees arch_kfence_init_pool() return false, it calls memblock_free() on both allocations after kmem_cache_init() has already set slab_is_available(). When CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled, memblock_free() -> memblock_phys_free() -> __free_reserved_area() triggers a warning because deferred struct page initialization has not yet run, leaking both __kfence_pool and kfence_metadata_init.
Second, if memblock_phys_alloc() succeeds in create_linear_mapping_page_table() (leaving kfence_disabled false), but the subsequent kfence_metadata_init allocation in kfence_alloc_pool_and_metadata() fails, the early PTE-mapped __kfence_pool is freed and reset to NULL while kfence_disabled remains false. If KFENCE is later enabled at runtime via sample_interval, kfence_enable_late() -> kfence_init_late() will allocate a replacement pool from the linear map (which may be backed by PMD/PUD/P4D/PGD hugepages), arch_kfence_init_pool() will return true, and kfence_protect_page() -> virt_to_kpte() will walk the page tables assuming PTE mappings, which can corrupt memory or fault.
Would setting kfence_sample_interval = 0 on allocation failure in create_linear_mapping_page_table() and checking whether the address falls within the early PTE-mapped pool range (or disabling late initialization in arch_kfence_init_pool()) avoid both paths?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Andrew Morton
maintainerQuoted 4 lines · On Thu, 29 Oct 2026 10:57:40 +0800 Jackie Liu <[email protected]> wrote:
Fix the cleanup address and page reuse window in failed late KFENCE initialization. Also let RISC-V boot if the early pool reservation fails.
Patch 2 depends on patch 1. Patch 3 can be applied independently.
Thanks.
We're all horridly busy with bugfixes at present and the first thing recipients think when presented with yet another is "why should I look at this".
So it really helps to start out with a succinct and punchy sentence which explains that thing.
LLM suggested to me
: KFENCE initialization failures can cause kernel crashes or memory : corruption, while a RISC-V failure can prevent the system from booting. : These fixes make the failure paths safe and allow systems to continue : running with KFENCE disabled.
How does that sound?
2026年10月30日 09:26, "Andrew Morton" <[email protected] mailto:[email protected]?to=%22Andrew%20Morton%22%20%3Cakpm%40linux-foundation.org%3E > 写到:
Quoted 18 lines · On Thu, 29 Oct 2026 10:57:40 +0800 Jackie Liu <[email protected]> wrote:
Quoted 7 lines
Fix the cleanup address and page reuse window in failed late KFENCE
initialization. Also let RISC-V boot if the early pool reservation fails.Patch 2 depends on patch 1. Patch 3 can be applied independently.
Thanks.
We're all horridly busy with bugfixes at present and the first thing recipients think when presented with yet another is "why should I look at this".
So it really helps to start out with a succinct and punchy sentence which explains that thing.
Quoted 8 lines
LLM suggested to me
: KFENCE initialization failures can cause kernel crashes or memory : corruption, while a RISC-V failure can prevent the system from booting. : These fixes make the failure paths safe and allow systems to continue : running with KFENCE disabled.
How does that sound?
Hi Andrew,
Thanks for the suggestion. That wording sounds good to me. It makes the impact of these bugs much clearer, and I'll keep this in mind when writing future patch descriptions.
Also, Sashiko has reported an issue with patch 3/3.
Could you please apply only the first two patches and drop patch 3/3 for now? I'll address the reported issue and resend the third patch separately once it's fixed.
Thanks,
Jackie
Quoted 1 line
Andrew Morton
maintainerQuoted 2 lines · On Fri, 30 Oct 2026 05:55:21 +0000 "Jackie Liu" <[email protected]> wrote:
Also, Sashiko has reported an issue with patch 3/3.
thanks, I missed that.
Quoted 3 lines
Could you please apply only the first two patches and drop patch 3/3 for now? I'll address the reported issue and resend the third patch separately once it's fixed.
Sure.