Skip to content

Commit 804ef31

Browse files
committed
execmem: add support for cache of large ROX pages
Using large pages to map text areas reduces iTLB pressure and improves performance. Extend execmem_alloc() with an ability to use huge pages with ROX permissions as a cache for smaller allocations. To populate the cache, a writable large page is allocated from vmalloc with VM_ALLOW_HUGE_VMAP, filled with invalid instructions and then remapped as ROX. The direct map alias of that large page is exculded from the direct map. Portions of that large page are handed out to execmem_alloc() callers without any changes to the permissions. When the memory is freed with execmem_free() it is invalidated again so that it won't contain stale instructions. An architecture has to implement execmem_fill_trapping_insns() callback and select ARCH_HAS_EXECMEM_ROX configuration option to be able to use the ROX cache. The cache is enabled on per-range basis when an architecture sets EXECMEM_ROX_CACHE flag in definition of an execmem_range. Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
1 parent e7e87f0 commit 804ef31

5 files changed

Lines changed: 345 additions & 8 deletions

File tree

arch/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,14 @@ config ARCH_WANTS_EXECMEM_LATE
10101010
enough entropy for module space randomization, for instance
10111011
arm64.
10121012

1013+
config ARCH_HAS_EXECMEM_ROX
1014+
bool
1015+
depends on MMU && !HIGHMEM
1016+
help
1017+
For architectures that support allocations of executable memory
1018+
with read-only execute permissions. Architecture must implement
1019+
execmem_fill_trapping_insns() callback to enable this.
1020+
10131021
config HAVE_IRQ_EXIT_ON_IRQ_STACK
10141022
bool
10151023
help

include/linux/execmem.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ enum execmem_range_flags {
5353
EXECMEM_ROX_CACHE = (1 << 1),
5454
};
5555

56+
#ifdef CONFIG_ARCH_HAS_EXECMEM_ROX
57+
/**
58+
* execmem_fill_trapping_insns - set memory to contain instructions that
59+
* will trap
60+
* @ptr: pointer to memory to fill
61+
* @size: size of the range to fill
62+
* @writable: is the memory poited by @ptr is writable or ROX
63+
*
64+
* A hook for architecures to fill execmem ranges with invalid instructions.
65+
* Architectures that use EXECMEM_ROX_CACHE must implement this.
66+
*/
67+
void execmem_fill_trapping_insns(void *ptr, size_t size, bool writable);
68+
#endif
69+
5670
/**
5771
* struct execmem_range - definition of an address space suitable for code and
5872
* related data allocations

0 commit comments

Comments
 (0)