Re: [PATCH v2 1/2] LoongArch: kfence: avoid use CONFIG_KFENCE_NUM_OBJECTS

From: Enze Li

Date: Mon Dec 22 2025 - 04:37:28 EST


On 12/22/25 5:16 PM, yuanlinyu wrote:
>> From: Enze Li <lienze@xxxxxxxxxx>
>> Sent: Saturday, December 20, 2025 1:44 PM
>> To: Huacai Chen <chenhuacai@xxxxxxxxxx>; yuanlinyu <yuanlinyu@xxxxxxxxx>
>> Cc: Alexander Potapenko <glider@xxxxxxxxxx>; Marco Elver
>> <elver@xxxxxxxxxx>; Dmitry Vyukov <dvyukov@xxxxxxxxxx>; Andrew Morton
>> <akpm@xxxxxxxxxxxxxxxxxxxx>; WANG Xuerui <kernel@xxxxxxxxxx>;
>> kasan-dev@xxxxxxxxxxxxxxxx; linux-mm@xxxxxxxxx; loongarch@xxxxxxxxxxxxxxx;
>> linux-kernel@xxxxxxxxxxxxxxx; enze.li@xxxxxxx
>> Subject: Re: [PATCH v2 1/2] LoongArch: kfence: avoid use
>> CONFIG_KFENCE_NUM_OBJECTS
>>
>> On 2025/12/19 10:13, Huacai Chen wrote:
>>> Hi, Enze,
>>>
>>> On Thu, Dec 18, 2025 at 2:39 PM yuan linyu <yuanlinyu@xxxxxxxxx> wrote:
>>>>
>>>> use common kfence macro KFENCE_POOL_SIZE for KFENCE_AREA_SIZE
>>>> definition
>>>>
>>>> Signed-off-by: yuan linyu <yuanlinyu@xxxxxxxxx>
>>>> ---
>>>> arch/loongarch/include/asm/pgtable.h | 3 ++-
>>>> 1 file changed, 2 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/arch/loongarch/include/asm/pgtable.h
>>>> b/arch/loongarch/include/asm/pgtable.h
>>>> index f41a648a3d9e..e9966c9f844f 100644
>>>> --- a/arch/loongarch/include/asm/pgtable.h
>>>> +++ b/arch/loongarch/include/asm/pgtable.h
>>>> @@ -10,6 +10,7 @@
>>>> #define _ASM_PGTABLE_H
>>>>
>>>> #include <linux/compiler.h>
>>>> +#include <linux/kfence.h>
>>>> #include <asm/addrspace.h>
>>>> #include <asm/asm.h>
>>>> #include <asm/page.h>
>>>> @@ -96,7 +97,7 @@ extern unsigned long empty_zero_page[PAGE_SIZE /
>> sizeof(unsigned long)];
>>>> #define MODULES_END (MODULES_VADDR + SZ_256M)
>>>>
>>>> #ifdef CONFIG_KFENCE
>>>> -#define KFENCE_AREA_SIZE (((CONFIG_KFENCE_NUM_OBJECTS + 1)
>> * 2 + 2) * PAGE_SIZE)
>>>> +#define KFENCE_AREA_SIZE (KFENCE_POOL_SIZE + (2 *
>> PAGE_SIZE))
>>> Can you remember why you didn't use KFENCE_POOL_SIZE at the first place?
>>
>> I don't recall the exact reason off the top of my head, but I believe it was due to
>> complex dependency issues with the header files where KFENCE_POOL_SIZE is
>> defined. To avoid those complications, we likely opted to use
>> KFENCE_NUM_OBJECTS directly.
>>
>> I checked out the code at commit
>> (6ad3df56bb199134800933df2afcd7df3b03ef33 "LoongArch: Add KFENCE
>> (Kernel
>> Electric-Fence) support") and encountered the following errors when compiling
>> with this patch applied.
>>
>> 8<------------------------------------------------------
>> CC arch/loongarch/kernel/asm-offsets.s
>> In file included from ./arch/loongarch/include/asm/pgtable.h:13,
>> from ./include/linux/pgtable.h:6,
>> from ./include/linux/mm.h:29,
>> from arch/loongarch/kernel/asm-offsets.c:9:
>> ./include/linux/kfence.h:93:35: warning: 'struct kmem_cache' declared inside
>> parameter list will n ot be visible outside of this definition or declaration
>> 93 | void kfence_shutdown_cache(struct kmem_cache *s);
>> | ^~~~~~~~~~
>> ./include/linux/kfence.h:99:29: warning: 'struct kmem_cache' declared inside
>> parameter list will n ot be visible outside of this definition or declaration
>> 99 | void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags);
>> | ^~~~~~~~~~
>> ./include/linux/kfence.h:117:50: warning: 'struct kmem_cache' declared inside
>> parameter list will not be visible outside of this definition or declaration
>> 117 | static __always_inline void *kfence_alloc(struct kmem_cache *s, size_t
>> size, gfp_t flags)
>> |
>> ^~~~~~~~~~
>> ./include/linux/kfence.h: In function 'kfence_alloc':
>> ./include/linux/kfence.h:128:31: error: passing argument 1 of '__kfence_alloc'
>> from incompatible p ointer type [-Wincompatible-pointer-types]
>> 128 | return __kfence_alloc(s, size, flags);
>> | ^
>> | |
>> | struct kmem_cache *
>> ./include/linux/kfence.h:99:41: note: expected 'struct kmem_cache *' but
>> argument is of type 'stru ct kmem_cache *'
>> 99 | void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags);
>> | ~~~~~~~~~~~~~~~~~~~^
>> ------------------------------------------------------>8
>>
>> Similarly, after applying this patch to the latest code
>> (dd9b004b7ff3289fb7bae35130c0a5c0537266af "Merge tag 'trace-v6.19-rc1'")
>> from the master branch of the Linux repository and enabling KFENCE, I
>> encountered the following compilation errors.
>>
>> 8<------------------------------------------------------
>> CC arch/loongarch/kernel/asm-offsets.s
>> In file included from ./arch/loongarch/include/asm/pgtable.h:13,
>> from ./include/linux/pgtable.h:6,
>> from ./include/linux/mm.h:31,
>> from arch/loongarch/kernel/asm-offsets.c:11:
>> ./include/linux/kfence.h:97:35: warning: 'struct kmem_cache' declared inside
>> parameter list will n ot be visible outside of this definition or declaration
>> 97 | void kfence_shutdown_cache(struct kmem_cache *s);
>> | ^~~~~~~~~~
>> ./include/linux/kfence.h:103:29: warning: 'struct kmem_cache' declared inside
>> parameter list will not be visible outside of this definition or declaration
>> 103 | void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags);
>> | ^~~~~~~~~~
>> ./include/linux/kfence.h:121:50: warning: 'struct kmem_cache' declared inside
>> parameter list will not be visible outside of this definition or declaration
>> 121 | static __always_inline void *kfence_alloc(struct kmem_cache *s, size_t
>> size, gfp_t flags)
>> |
>> ^~~~~~~~~~
>> ./include/linux/kfence.h: In function 'kfence_alloc':
>> ./include/linux/kfence.h:132:31: error: passing argument 1 of '__kfence_alloc'
>> from incompatible p ointer type [-Wincompatible-pointer-types]
>> 132 | return __kfence_alloc(s, size, flags);
>> | ^
>> | |
>> | struct kmem_cache *
>> ./include/linux/kfence.h:103:41: note: expected 'struct kmem_cache *'
>> but argument is of type 'str
>> uct kmem_cache *'
>> 103 | void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags);
>> | ~~~~~~~~~~~~~~~~~~~^
>> ------------------------------------------------------>8
>>
>> So, this patch currently runs into compilation issues. linyu probably didn't have
>> KFENCE enabled when compiling locally, which is why this error was missed.
>> You can enable it as follows:
>>
>> Kernel hacking
>> Memory Debugging
>> [*] KFENCE: low-overhead sampling-based memory safety
>
> Hi Enze,
>
> Sorry only test on arm64.
>
> Could you help fix the compile issue and provide a correct change ?
>
> Or I need sometime to resolve the issue.
>

Thanks for pointing out this issue. I've taken a look at the
compilation problem you mentioned. Based on my current understanding,
the header dependencies are quite complex, and I couldn't find a
straightforward fix without potentially affecting other parts of the
codebase.

Given the risk of introducing broader compilation errors, I think it
might be safer to hold off on using the KFENCE_POOL_SIZE macro for now,
unless there's a clear and safe path forward that I might have missed.
I'm happy to discuss this further if you have any insights or suggestions.

Thanks,
Enze