linux/arch/loongarch
Tiezhu Yang db47a9bc78 LoongArch: Rework KASAN initialization for PTW-enabled systems
commit 5ec5ac4ca2 upstream.

kasan_init_generic() indicates that kasan is fully initialized, so it
should be put at end of kasan_init().

Otherwise bringing up the primary CPU failed when CONFIG_KASAN is set
on PTW-enabled systems, here are the call chains:

    kernel_entry()
      start_kernel()
        setup_arch()
          kasan_init()
            kasan_init_generic()

The reason is PTW-enabled systems have speculative accesses which means
memory accesses to the shadow memory after kasan_init() may be executed
by hardware before. However, accessing shadow memory is safe only after
kasan fully initialized because kasan_init() uses a temporary PGD table
until we have populated all levels of shadow page tables and writen the
PGD register. Moving kasan_init_generic() later can defer the occasion
of kasan_enabled(), so as to avoid speculative accesses on shadow pages.

After moving kasan_init_generic() to the end, kasan_init() can no longer
call kasan_mem_to_shadow() for shadow address conversion because it will
always return kasan_early_shadow_page. On the other hand, we should keep
the current logic of kasan_mem_to_shadow() for both the early and final
stage because there may be instrumentation before kasan_init().

To solve this, we factor out a new mem_to_shadow() function from current
kasan_mem_to_shadow() for the shadow address conversion in kasan_init().

Cc: stable@vger.kernel.org
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
History 2026-02-19 16:33:24 +01:00
..
boot LoongArch: dts: loongson-2k1000: Fix i2c-gpio node names 2026-01-17 10:56:53 +08:00
configs LoongArch changes for v6.19 2025-12-13 05:44:03 +12:00
crypto loongarch/crc32: expose CRC32 functions through lib 2024-12-01 17:23:01 -08:00
include LoongArch: Complete CPUCFG registers definition 2025-12-31 15:19:10 +08:00
kernel LoongArch: Fix PMU counter allocation for mixed-type event groups 2026-01-17 10:56:43 +08:00
kvm LoongArch: KVM: Fix kvm_device leak in kvm_pch_pic_destroy() 2026-01-17 10:57:03 +08:00
lib LoongArch: Adjust misc routines for 32BIT/64BIT 2025-12-08 18:09:17 +08:00
mm LoongArch: Rework KASAN initialization for PTW-enabled systems 2026-02-19 16:33:24 +01:00
net LoongArch: BPF: Enhance the bpf_arch_text_poke() function 2025-12-31 15:19:21 +08:00
pci LoongArch: Add new PCI ID for pci_fixup_vgadev() 2025-12-06 10:39:49 +08:00
power LoongArch: Adjust misc routines for 32BIT/64BIT 2025-12-08 18:09:17 +08:00
vdso LoongArch changes for v6.19 2025-12-13 05:44:03 +12:00
Kbuild LoongArch: Migrate to the generic rule for built-in DTB 2025-01-25 18:51:33 +08:00
Kconfig LoongArch: Add atomic operations for 32BIT/64BIT 2025-12-06 10:40:32 +08:00
Kconfig.debug LoongArch: Only allow OBJTOOL & ORC unwinder if toolchain supports -mthin-add-sub 2024-06-21 10:18:40 +08:00
Makefile LoongArch: Adjust default config files for 32BIT/64BIT 2025-12-08 18:09:17 +08:00