Commit 6d47c23b authored by Mike Rapoport's avatar Mike Rapoport Committed by Linus Torvalds

set_memory: allow querying whether set_direct_map_*() is actually enabled

On arm64, set_direct_map_*() functions may return 0 without actually
changing the linear map.  This behaviour can be controlled using kernel
parameters, so we need a way to determine at runtime whether calls to
set_direct_map_invalid_noflush() and set_direct_map_default_noflush() have
any effect.

Extend set_memory API with can_set_direct_map() function that allows
checking if calling set_direct_map_*() will actually change the page
table, replace several occurrences of open coded checks in arm64 with the
new function and provide a generic stub for architectures that always
modify page tables upon calls to set_direct_map APIs.

[arnd@arndb.de: arm64: kfence: fix header inclusion ]

Link: https://lkml.kernel.org/r/20210518072034.31572-4-rppt@kernel.orgSigned-off-by: default avatarMike Rapoport <rppt@linux.ibm.com>
Reviewed-by: default avatarCatalin Marinas <catalin.marinas@arm.com>
Reviewed-by: default avatarDavid Hildenbrand <david@redhat.com>
Acked-by: default avatarJames Bottomley <James.Bottomley@HansenPartnership.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Christopher Lameter <cl@linux.com>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Elena Reshetova <elena.reshetova@intel.com>
Cc: Hagen Paul Pfeifer <hagen@jauu.net>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Bottomley <jejb@linux.ibm.com>
Cc: "Kirill A. Shutemov" <kirill@shutemov.name>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Palmer Dabbelt <palmerdabbelt@google.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Rick Edgecombe <rick.p.edgecombe@intel.com>
Cc: Roman Gushchin <guro@fb.com>
Cc: Shakeel Butt <shakeelb@google.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Tycho Andersen <tycho@tycho.ws>
Cc: Will Deacon <will@kernel.org>
Cc: kernel test robot <lkp@intel.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 10cc3278
...@@ -3,7 +3,6 @@ generic-y += early_ioremap.h ...@@ -3,7 +3,6 @@ generic-y += early_ioremap.h
generic-y += mcs_spinlock.h generic-y += mcs_spinlock.h
generic-y += qrwlock.h generic-y += qrwlock.h
generic-y += qspinlock.h generic-y += qspinlock.h
generic-y += set_memory.h
generic-y += user.h generic-y += user.h
generated-y += cpucaps.h generated-y += cpucaps.h
...@@ -144,12 +144,6 @@ static __always_inline void icache_inval_all_pou(void) ...@@ -144,12 +144,6 @@ static __always_inline void icache_inval_all_pou(void)
dsb(ish); dsb(ish);
} }
int set_memory_valid(unsigned long addr, int numpages, int enable);
int set_direct_map_invalid_noflush(struct page *page);
int set_direct_map_default_noflush(struct page *page);
bool kernel_page_present(struct page *page);
#include <asm-generic/cacheflush.h> #include <asm-generic/cacheflush.h>
#endif /* __ASM_CACHEFLUSH_H */ #endif /* __ASM_CACHEFLUSH_H */
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#ifndef __ASM_KFENCE_H #ifndef __ASM_KFENCE_H
#define __ASM_KFENCE_H #define __ASM_KFENCE_H
#include <asm/cacheflush.h> #include <asm/set_memory.h>
static inline bool arch_kfence_init_pool(void) { return true; } static inline bool arch_kfence_init_pool(void) { return true; }
......
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _ASM_ARM64_SET_MEMORY_H
#define _ASM_ARM64_SET_MEMORY_H
#include <asm-generic/set_memory.h>
bool can_set_direct_map(void);
#define can_set_direct_map can_set_direct_map
int set_memory_valid(unsigned long addr, int numpages, int enable);
int set_direct_map_invalid_noflush(struct page *page);
int set_direct_map_default_noflush(struct page *page);
bool kernel_page_present(struct page *page);
#endif /* _ASM_ARM64_SET_MEMORY_H */
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/kexec.h> #include <linux/kexec.h>
#include <linux/page-flags.h> #include <linux/page-flags.h>
#include <linux/set_memory.h>
#include <linux/smp.h> #include <linux/smp.h>
#include <asm/cacheflush.h> #include <asm/cacheflush.h>
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <linux/io.h> #include <linux/io.h>
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/vmalloc.h> #include <linux/vmalloc.h>
#include <linux/set_memory.h>
#include <asm/barrier.h> #include <asm/barrier.h>
#include <asm/cputype.h> #include <asm/cputype.h>
...@@ -515,8 +516,7 @@ static void __init map_mem(pgd_t *pgdp) ...@@ -515,8 +516,7 @@ static void __init map_mem(pgd_t *pgdp)
*/ */
BUILD_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end)); BUILD_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end));
if (rodata_full || crash_mem_map || debug_pagealloc_enabled() || if (can_set_direct_map() || crash_mem_map || IS_ENABLED(CONFIG_KFENCE))
IS_ENABLED(CONFIG_KFENCE))
flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
/* /*
...@@ -1489,8 +1489,7 @@ int arch_add_memory(int nid, u64 start, u64 size, ...@@ -1489,8 +1489,7 @@ int arch_add_memory(int nid, u64 start, u64 size,
* KFENCE requires linear map to be mapped at page granularity, so that * KFENCE requires linear map to be mapped at page granularity, so that
* it is possible to protect/unprotect single pages in the KFENCE pool. * it is possible to protect/unprotect single pages in the KFENCE pool.
*/ */
if (rodata_full || debug_pagealloc_enabled() || if (can_set_direct_map() || IS_ENABLED(CONFIG_KFENCE))
IS_ENABLED(CONFIG_KFENCE))
flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
__create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start), __create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start),
......
...@@ -19,6 +19,11 @@ struct page_change_data { ...@@ -19,6 +19,11 @@ struct page_change_data {
bool rodata_full __ro_after_init = IS_ENABLED(CONFIG_RODATA_FULL_DEFAULT_ENABLED); bool rodata_full __ro_after_init = IS_ENABLED(CONFIG_RODATA_FULL_DEFAULT_ENABLED);
bool can_set_direct_map(void)
{
return rodata_full || debug_pagealloc_enabled();
}
static int change_page_range(pte_t *ptep, unsigned long addr, void *data) static int change_page_range(pte_t *ptep, unsigned long addr, void *data)
{ {
struct page_change_data *cdata = data; struct page_change_data *cdata = data;
...@@ -155,7 +160,7 @@ int set_direct_map_invalid_noflush(struct page *page) ...@@ -155,7 +160,7 @@ int set_direct_map_invalid_noflush(struct page *page)
.clear_mask = __pgprot(PTE_VALID), .clear_mask = __pgprot(PTE_VALID),
}; };
if (!debug_pagealloc_enabled() && !rodata_full) if (!can_set_direct_map())
return 0; return 0;
return apply_to_page_range(&init_mm, return apply_to_page_range(&init_mm,
...@@ -170,7 +175,7 @@ int set_direct_map_default_noflush(struct page *page) ...@@ -170,7 +175,7 @@ int set_direct_map_default_noflush(struct page *page)
.clear_mask = __pgprot(PTE_RDONLY), .clear_mask = __pgprot(PTE_RDONLY),
}; };
if (!debug_pagealloc_enabled() && !rodata_full) if (!can_set_direct_map())
return 0; return 0;
return apply_to_page_range(&init_mm, return apply_to_page_range(&init_mm,
...@@ -181,7 +186,7 @@ int set_direct_map_default_noflush(struct page *page) ...@@ -181,7 +186,7 @@ int set_direct_map_default_noflush(struct page *page)
#ifdef CONFIG_DEBUG_PAGEALLOC #ifdef CONFIG_DEBUG_PAGEALLOC
void __kernel_map_pages(struct page *page, int numpages, int enable) void __kernel_map_pages(struct page *page, int numpages, int enable)
{ {
if (!debug_pagealloc_enabled() && !rodata_full) if (!can_set_direct_map())
return; return;
set_memory_valid((unsigned long)page_address(page), numpages, enable); set_memory_valid((unsigned long)page_address(page), numpages, enable);
...@@ -206,7 +211,7 @@ bool kernel_page_present(struct page *page) ...@@ -206,7 +211,7 @@ bool kernel_page_present(struct page *page)
pte_t *ptep; pte_t *ptep;
unsigned long addr = (unsigned long)page_address(page); unsigned long addr = (unsigned long)page_address(page);
if (!debug_pagealloc_enabled() && !rodata_full) if (!can_set_direct_map())
return true; return true;
pgdp = pgd_offset_k(addr); pgdp = pgd_offset_k(addr);
......
...@@ -28,7 +28,19 @@ static inline bool kernel_page_present(struct page *page) ...@@ -28,7 +28,19 @@ static inline bool kernel_page_present(struct page *page)
{ {
return true; return true;
} }
#else /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */
/*
* Some architectures, e.g. ARM64 can disable direct map modifications at
* boot time. Let them overrive this query.
*/
#ifndef can_set_direct_map
static inline bool can_set_direct_map(void)
{
return true;
}
#define can_set_direct_map can_set_direct_map
#endif #endif
#endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */
#ifndef set_mce_nospec #ifndef set_mce_nospec
static inline int set_mce_nospec(unsigned long pfn, bool unmap) static inline int set_mce_nospec(unsigned long pfn, bool unmap)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment