linux/arch/loongarch/mm
Tiezhu Yang eaa9fcf579 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:31:34 +01:00
..
cache.c LoongArch: Set correct protection_map[] for VM_NONE/VM_SHARED 2026-02-11 13:41:47 +01:00
extable.c LoongArch: BPF: Add BPF exception tables 2022-12-14 08:36:11 +08:00
fault.c LoongArch: Try VMA lock-based page fault handling first 2025-10-02 22:39:19 +08:00
hugetlbpage.c LoongArch: Fix panic caused by NULL-PMD in huge_pte_offset() 2025-05-30 21:45:57 +08:00
init.c LoongArch: Consolidate max_pfn & max_low_pfn calculation 2025-11-10 08:37:06 +08:00
ioremap.c LoongArch: Consolidate early_ioremap()/ioremap_prot() 2025-11-10 08:37:06 +08:00
kasan_init.c LoongArch: Rework KASAN initialization for PTW-enabled systems 2026-02-19 16:31:34 +01:00
maccess.c LoongArch: Add memory management 2022-06-03 20:09:28 +08:00
Makefile LoongArch: Add ARCH_HAS_SET_MEMORY support 2024-09-24 15:32:20 +08:00
mmap.c LoongArch: Set hugetlb mmap base address aligned with pmd size 2025-03-08 13:51:32 +08:00
page.S LoongArch: Replace #include <asm/export.h> with #include <linux/export.h> 2023-08-25 23:40:26 +08:00
pageattr.c mm/pagewalk: split walk_page_range_novma() into kernel/user parts 2025-07-09 22:42:05 -07:00
pgtable.c arch: remove mk_pmd() 2025-05-11 17:48:04 -07:00
tlb.c LoongArch: Fix sleeping in atomic context for PREEMPT_RT 2024-11-26 18:06:04 +08:00
tlbex.S LoongArch: Give a chance to build with !CONFIG_SMP 2024-05-14 12:24:18 +08:00