Re: [PATCH] mm: fix KASAN build error due to p*d_populate_kernel()

From: Harry Yoo
Date: Thu Aug 21 2025 - 06:44:07 EST


On Thu, Aug 21, 2025 at 11:10:39AM +0100, Lorenzo Stoakes wrote:
> On Thu, Aug 21, 2025 at 06:35:42PM +0900, Harry Yoo wrote:
> > KASAN unconditionally references kasan_early_shadow_{p4d,pud}.
> > However, these global variables may not exist depending on the number of
> > page table levels. For example, if CONFIG_PGTABLE_LEVELS=3, both
> > variables do not exist. Although KASAN may refernce non-existent
> > variables, it didn't break builds because calls to {pgd,p4d}_populate()
> > are optimized away at compile time.
> >
> > However, {pgd,p4d}_populate_kernel() is defined as a function regardless
> > of the number of page table levels, so the compiler may not optimize
> > them away. In this case, the following linker error occurs:
> >
> > ld.lld: error: undefined symbol: kasan_early_shadow_p4d
> > >>> referenced by init.c:260 (/home/hyeyoo/mm-new/mm/kasan/init.c:260)
> > >>> mm/kasan/init.o:(kasan_populate_early_shadow) in archive vmlinux.a
> > >>> referenced by init.c:260 (/home/hyeyoo/mm-new/mm/kasan/init.c:260)
> > >>> mm/kasan/init.o:(kasan_populate_early_shadow) in archive vmlinux.a
> > >>> did you mean: kasan_early_shadow_pmd
> > >>> defined in: vmlinux.a(mm/kasan/init.o)
> >
> > ld.lld: error: undefined symbol: kasan_early_shadow_pud
> > >>> referenced by init.c:263 (/home/hyeyoo/mm-new/mm/kasan/init.c:263)
> > >>> mm/kasan/init.o:(kasan_populate_early_shadow) in archive vmlinux.a
> > >>> referenced by init.c:263 (/home/hyeyoo/mm-new/mm/kasan/init.c:263)
> > >>> mm/kasan/init.o:(kasan_populate_early_shadow) in archive vmlinux.a
> > >>> referenced by init.c:200 (/home/hyeyoo/mm-new/mm/kasan/init.c:200)
> > >>> mm/kasan/init.o:(zero_p4d_populate) in archive vmlinux.a
> > >>> referenced 1 more times
> >
> > Therefore, to allow calls to {pgd,p4d}_populate_kernel() to be optimized
> > out at compile time, define {pgd,p4d}_populate_kernel() as macros.
> > This way, when pgd_populate() or p4d_populate() are simply empty macros,
> > the corresponding *_populate_kernel() functions can also be optimized
> > away.
> >
> > Signed-off-by: Harry Yoo <harry.yoo@xxxxxxxxxx>
>
> This looks good, other than the nit below re: a comment, I think when we
> are doing this kind of thing it's necessary to spell out plainly why
> exactly we're doing it because it's not obvious at first glance.

Good point, will do:

/*
* {pgd,p4d}_populate_kernel() are defined as macros to allow
* compile-time optimization based on the configured page table levels.
* Without this, linking may fail because callers (e.g., KASAN) may rely
* on calls to these functions being optimized away when passing symbols
* that exist only for certain page table levels.
*/

> Anyway have checked locally and all good and LGTM code-wise so aside from
> above:
>
> Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@xxxxxxxxxx>

Thanks!

--
Cheers,
Harry / Hyeonggon