[PATCH RFC v2 34/54] LoongArch: stop pretending LoongArch supports set_direct_map APIs
Huacai Chen
chenhuacai at kernel.org
Thu Dec 3 22:44:16 PST 2026
Hi, Mike,
On Thu, Nov 26, 2026 at 7:19 PM Mike Rapoport (Microsoft)
<rppt at kernel.org> wrote:
>
> LoongArch selects ARCH_HAS_SET_DIRECT_MAP and implements set_direct_map*()
> APIs to allow coexistence of hibernation with KFENCE, see
> commit f04de6d8f252 ("LoongArch: Add ARCH_HAS_SET_DIRECT_MAP support").
>
> However, LoongArch does not have page tables for its direct map to begin
> with. It maps the physical memory with a hardware window, so there is no
> way to change the permissions of an individual page there.
>
> Protection of KFENCE pages is handled outside the direct map: they are
> mapped in the dedicated address space area and their virtual address is
> explicitly set with set_page_address() to keep page_address() working for
> those pages.
>
> Selecting ARCH_HAS_SET_DIRECT_MAP enables secretmem on LoongArch, although
> without the actual ability to modify the direct map, secretmem becomes a
> limited memfd and does not provide the security it promises.
>
> After the rework of the temporary mappings in the hibernation code [1],
> there is no need for LoongArch to pretend it supports direct map
> operations.
>
> Remove the set_direct_map*() implementation and stop enabling
> ARCH_HAS_SET_DIRECT_MAP.
I want to keep it.
Yes, for now LoongArch does not have page tables for its direct map.
The major reason is PALEN and VALEN are both 48, which means the
virtual address space cannot cover all physical memory in its direct
map and have a vmalloc space at the same time (unless enable HIGHMEM
for 64bit, but that is a disaster).
Things will change, new generations of our processor will support VA52
or VA56, then we can move the direct map to the page table mapped
area.
Current set_direct_map_* API needn't change, in future the direct map
will be above vm_map_base.
Huacai
>
> Removing support for ARCH_HAS_SET_DIRECT_MAP effectively disables
> secretmem, and memfd_secret() returns -ENOSYS on LoongArch.
>
> [1] https://lore.kernel.org/all/20260917-hibernation-v1-4-7f7dfae3dbe0@kernel.org
>
> Signed-off-by: Mike Rapoport (Microsoft) <rppt at kernel.org>
> ---
> arch/loongarch/Kconfig | 1 -
> arch/loongarch/include/asm/set_memory.h | 4 ---
> arch/loongarch/mm/pageattr.c | 60 ---------------------------------
> 3 files changed, 65 deletions(-)
>
> diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
> index b5ed4881d3c0..bad699e2847f 100644
> --- a/arch/loongarch/Kconfig
> +++ b/arch/loongarch/Kconfig
> @@ -27,7 +27,6 @@ config LOONGARCH
> select ARCH_HAS_PREEMPT_LAZY
> select ARCH_HAS_PTE_SPECIAL if 64BIT
> select ARCH_HAS_SET_MEMORY
> - select ARCH_HAS_SET_DIRECT_MAP
> select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
> select ARCH_HAS_UBSAN
> select ARCH_HAS_VDSO_ARCH_DATA
> diff --git a/arch/loongarch/include/asm/set_memory.h b/arch/loongarch/include/asm/set_memory.h
> index 4bb01172fbc2..64c7f942e8ec 100644
> --- a/arch/loongarch/include/asm/set_memory.h
> +++ b/arch/loongarch/include/asm/set_memory.h
> @@ -14,8 +14,4 @@ int set_memory_nx(unsigned long addr, int numpages);
> int set_memory_ro(unsigned long addr, int numpages);
> int set_memory_rw(unsigned long addr, int numpages);
>
> -bool kernel_page_present(struct page *page);
> -int set_direct_map_default_noflush(struct page *page, unsigned int nr);
> -int set_direct_map_invalid_noflush(struct page *page, unsigned int nr);
> -
> #endif /* _ASM_LOONGARCH_SET_MEMORY_H */
> diff --git a/arch/loongarch/mm/pageattr.c b/arch/loongarch/mm/pageattr.c
> index a728db40f9e0..7283059d6e1f 100644
> --- a/arch/loongarch/mm/pageattr.c
> +++ b/arch/loongarch/mm/pageattr.c
> @@ -156,63 +156,3 @@ int set_memory_rw(unsigned long addr, int numpages)
>
> return __set_memory(addr, numpages, __pgprot(_PAGE_WRITE | _PAGE_DIRTY), __pgprot(0));
> }
> -
> -bool kernel_page_present(struct page *page)
> -{
> - pgd_t *pgd;
> - p4d_t *p4d;
> - pud_t *pud;
> - pmd_t *pmd;
> - pte_t *pte;
> - unsigned long addr = (unsigned long)page_address(page);
> -
> - if (addr < vm_map_base)
> - return true;
> -
> - pgd = pgd_offset_k(addr);
> - if (pgd_none(pgdp_get(pgd)))
> - return false;
> - if (pgd_leaf(pgdp_get(pgd)))
> - return true;
> -
> - p4d = p4d_offset(pgd, addr);
> - if (p4d_none(p4dp_get(p4d)))
> - return false;
> - if (p4d_leaf(p4dp_get(p4d)))
> - return true;
> -
> - pud = pud_offset(p4d, addr);
> - if (pud_none(pudp_get(pud)))
> - return false;
> - if (pud_leaf(pudp_get(pud)))
> - return true;
> -
> - pmd = pmd_offset(pud, addr);
> - if (pmd_none(pmdp_get(pmd)))
> - return false;
> - if (pmd_leaf(pmdp_get(pmd)))
> - return true;
> -
> - pte = pte_offset_kernel(pmd, addr);
> - return pte_present(ptep_get(pte));
> -}
> -
> -int set_direct_map_default_noflush(struct page *page, unsigned int nr)
> -{
> - unsigned long addr = (unsigned long)page_address(page);
> -
> - if (addr < vm_map_base)
> - return 0;
> -
> - return __set_memory(addr, nr, PAGE_KERNEL, __pgprot(0));
> -}
> -
> -int set_direct_map_invalid_noflush(struct page *page, unsigned int nr)
> -{
> - unsigned long addr = (unsigned long)page_address(page);
> -
> - if (addr < vm_map_base)
> - return 0;
> -
> - return __set_memory(addr, nr, __pgprot(0), __pgprot(_PAGE_PRESENT | _PAGE_VALID));
> -}
>
> --
> 2.53.0
>
More information about the linux-arm-kernel
mailing list