From: Ard Biesheuvel <ardb@kernel.org>
The arm64 module loader has to deal with a couple of corner cases that
may occur when .init.text is placed out of direct branch range of .text:
- ordinary direct branches from .init.text into .text may require the
use of a PLT entry (i.e., a trampoline aka veneer), which means not
only that additional PLT entries need to be allocated for
cross-section calls, but also that .init.text needs its own PLT
reservation, as the one in .text will be out of range as well;
- dynamic patching of the ftrace handler into .init.text code needs its
own dedicated trampoline as the one in .text may be too far away.
- recent compilers may omit BTI veneers for static functions that never
have their address taken, and so additional veneers will need to be
added to .text in case cross-section direct branches from .init.text
require a PLT entry (and therefore a landing pad at the target end).
This is unfortunate, because it is actually somewhat unusual for .text
and .init.text to be so far away from each other: only when allocating
either of them (but not both) exhausts the 'near' (PLT-less) module
region, the other will be allocated from the spillover region, which is
not in direct branching range, and therefore requires PLT entries for
cross-section calls.
This series addresses this wart by allocating both of them as a single
chunk, and freeing the .init.text part along with the other init
sections at the appropriate time. This ensures that the two regions will
never require veneers for cross-section calls, allowing the arm64 module
loader to be simplified.
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Luis Chamberlain <mcgrof@kernel.org>
Cc: Petr Pavlu <petr.pavlu@suse.com>
Cc: Daniel Gomez <da.gomez@kernel.org>
Cc: Sami Tolvanen <samitolvanen@google.com>
Cc: Aaron Tomlin <atomlin@atomlin.com>
Cc: "Adrian Barnaś" <redacted>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Kevin Brodsky <redacted>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-trace-kernel@vger.kernel.org
Cc: linux-mm@kvack.org
Cc: linux-modules@vger.kernel.org
Ard Biesheuvel (9):
mm: execmem: Add API to split an existing execmem cache allocation
mm: execmem: Allow huge vmappings to be avoided for execmem caches
module: Place MOD_TEXT before MOD_INIT_TEXT in enumeration
module: Allocate MOD_INIT_TEXT from the MOD_TEXT ROX allocation
arm64: mm: Permit permissions changes on huge vmappings
arm64: Enable the execmem ROX cache for module text
arm64: ftrace: Revert "fix unreachable PLT for ftrace_caller ..."
arm64: module: Combine init and core PLT entries again
arm64: ftrace: Simplify PLT handling
arch/arm64/Kconfig | 1 +
arch/arm64/include/asm/module.h | 2 -
arch/arm64/include/asm/module.lds.h | 3 -
arch/arm64/kernel/ftrace.c | 13 +---
arch/arm64/kernel/module-plts.c | 63 +++++---------------
arch/arm64/kernel/module.c | 20 +------
arch/arm64/mm/init.c | 19 +++++-
arch/arm64/mm/pageattr.c | 13 +++-
include/linux/execmem.h | 11 ++++
include/linux/module.h | 4 +-
kernel/module/main.c | 22 +++++--
mm/execmem.c | 47 ++++++++++++++-
12 files changed, 124 insertions(+), 94 deletions(-)
--
2.55.0.860.g4b6b3295ed-goog
From: Ard Biesheuvel <ardb@kernel.org>
In order to permit the module loader to allocate MOD_TEXT and
MOD_INIT_TEXT from the same chunk of memory, add an API function to
execmem that splits an existing execmem cache allocation in two.
This will be used on arm64 to avoid .text and .init.text being placed
far away from each other.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
include/linux/execmem.h | 9 ++++
mm/execmem.c | 44 ++++++++++++++++++++
2 files changed, 53 insertions(+)
From: Ard Biesheuvel <ardb@kernel.org>
When execmem ROX caches are used for module text and inittext, place
them adjacently in memory, by allocating space for both initially, and
splitting off the space for MOD_INIT_TEXT as needed.
This avoids the corner case on arm64, where .init.text being placed far
from .text results in a lot of complexity wrt indirect branches and PLTs
that we'd prefer to avoid.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
kernel/module/main.c | 22 ++++++++++++++++----
1 file changed, 18 insertions(+), 4 deletions(-)
From: Ard Biesheuvel <ardb@kernel.org>
Only recent arm64 systems are guaranteed to be able to manipulate the
permissions on live kernel mappings that may use huge mappings. Older
ones can only do so on live mappings that are mapped down to pages.
In order to make execmem caches work on arm64 despite this distinction,
allow huge vmappings to be omitted when allocating the caches.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
include/linux/execmem.h | 2 ++
mm/execmem.c | 3 ++-
2 files changed, 4 insertions(+), 1 deletion(-)
From: Ard Biesheuvel <ardb@kernel.org>
Reorder enum mod_mem_type so that MOD_TEXT appears right before
MOD_INIT_TEXT. This will result in MOD_INIT_TEXT being allocated right
after MOD_TEXT when the allocation logic iterates over the memory types
in enum declaration order.
In a subsequent patch, this will be taken advantage of, by allocating
.text and .init.text together, and freeing .init.text by truncating the
allocation. Doing so without this reordering would likely result in more
fragmentation, as the truncated .text allocation would be followed by
.rodata and .data/.bss of the same module.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
include/linux/module.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
From: Ard Biesheuvel <ardb@kernel.org>
Allow permission changes on huge vmappings in cases where no splitting
is needed (i.e., the region is aligned sufficiently), or when the system
has support for splitting live mappings.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
arch/arm64/mm/pageattr.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
@@ -169,8 +169,6 @@ static int change_memory_common(unsigned long addr, int numpages,*weareoperatingondoesnotresultinsuchsplitting.**Let'srestrictourselvestomappingscreatedbyvmalloc(orvmap).-*DisallowVM_ALLOW_HUGE_VMAPmappingstoguaranteethatonlypage-*mappingsareupdatedandsplittingisneverneeded.**Socheckwhetherthe[addr,addr+size)intervalisentirely*coveredbypreciselyoneVMareathathastheVM_ALLOCflagset.
@@ -179,7 +177,16 @@ static int change_memory_common(unsigned long addr, int numpages,if(!area||((unsignedlong)kasan_reset_tag((void*)end)>(unsignedlong)kasan_reset_tag(area->addr)+area->size)||-((area->flags&(VM_ALLOC|VM_ALLOW_HUGE_VMAP))!=VM_ALLOC))+!(area->flags&VM_ALLOC))+return-EINVAL;++/*+*DisallowVM_ALLOW_HUGE_VMAPmappingsunlesstheregionisPMD+*aligned,orsplittinglivehugemappingsissupported.+*/+if((area->flags&VM_ALLOW_HUGE_VMAP)&&+((start%PMD_SIZE)||(size%PMD_SIZE))&&+WARN_ON_ONCE(!system_supports_bbml2_noabort()))return-EINVAL;if(!numpages)
From: Ard Biesheuvel <ardb@kernel.org>
It is no longer possible for .init.text to end up being placed out of
direct branching range of the .text section of the same module, so the
PLT array of core and init code can be combined again.
It also means there is no longer a need to allocate PLT entries for
cross-section calls within the same module, and so the upper bound
for the number of needed entries can be lowered again as well.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
arch/arm64/include/asm/module.h | 1 -
arch/arm64/include/asm/module.lds.h | 1 -
arch/arm64/kernel/module-plts.c | 35 +++++---------------
3 files changed, 9 insertions(+), 28 deletions(-)
From: Ard Biesheuvel <ardb@kernel.org>
Wire up the existing support for the execmem ROX cache on arm64, so that
it will be used for module .text and .init.text regions. This will be
relied upon by a subsequent patch in order to allocate those regions
from a single chunk of memory.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
arch/arm64/Kconfig | 1 +
arch/arm64/mm/init.c | 19 ++++++++++++++++++-
2 files changed, 19 insertions(+), 1 deletion(-)
From: Ard Biesheuvel <ardb@kernel.org>
This reverts commit a7ed7b9d0ebb038db9963d574da0311cab0b666a, which is
no longer needed now that the corner case where .init.text is placed out
of direct branching range from .text of the same module can no longer
occur.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
arch/arm64/include/asm/module.h | 1 -
arch/arm64/include/asm/module.lds.h | 1 -
arch/arm64/kernel/ftrace.c | 13 +++----------
arch/arm64/kernel/module-plts.c | 12 +-----------
arch/arm64/kernel/module.c | 11 -----------
5 files changed, 4 insertions(+), 34 deletions(-)
From: Ard Biesheuvel <ardb@kernel.org>
Instead of emitting the ftrace trampoline PLT into a separate ELF
section, allocate them at the start of the ordinary PLT array, so
that no special sections are needed.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
arch/arm64/include/asm/module.lds.h | 1 -
arch/arm64/kernel/module-plts.c | 18 +++++-------------
arch/arm64/kernel/module.c | 9 +--------
3 files changed, 6 insertions(+), 22 deletions(-)
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The reordering of `enum mod_mem_type` breaks the `lx-lsmod` GDB script command, causing it to report a core module size of 0 for all loaded modules.
--
--- Patch [3]: [RFC PATCH 3/9] module: Place MOD_TEXT before MOD_INIT_TEXT in enumeration ---
commit 5a5649e85e950b908f9830e5862b49ac415dfa18
Author: Ard Biesheuvel [off-list ref]
module: Place MOD_TEXT before MOD_INIT_TEXT in enumeration
This commit reorders enum mod_mem_type so that MOD_TEXT appears right
before MOD_INIT_TEXT. This ensures MOD_INIT_TEXT is allocated immediately
after MOD_TEXT, reducing fragmentation when truncating allocations in
subsequent patches.
[Severity: Medium]
Does this reordering break the lx-lsmod GDB script command?
In scripts/gdb/linux/modules.py:LxLsmod.invoke(), the total size of a
module's core sections is computed by iterating over the memory regions:
for i in range(constants.LX_MOD_TEXT, constants.LX_MOD_RO_AFTER_INIT + 1):
total_size += module['mem'][i]['size']
Because MOD_TEXT now evaluates to 3 and MOD_RO_AFTER_INIT evaluates to 2
with this change, the Python iteration evaluates to range(3, 3). This
generates an empty sequence and the total_size remains 0 for all loaded
modules.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [High] Omitting VM_ALLOW_HUGE_VMAP breaks the ROX cache shrinker by stripping PMD_SIZE alignment, causing a permanent memory leak of cached module memory.
Pre-existing issues:
- [High] Fallback allocations in execmem_cache_populate_alloc() leak memory due to failing the PMD_SIZE alignment checks in the cache shrinker.
- [High] Ignored return value of execmem_restore_rox() allows memory to be silently returned to the ROX cache with RW permissions.
--
--- Patch [2]: [RFC PATCH 2/9] mm: execmem: Allow huge vmappings to be avoided for execmem caches ---
commit 3d4b59f7ca74d71807be337626018d273686c686
Author: Ard Biesheuvel [off-list ref]
mm: execmem: Allow huge vmappings to be avoided for execmem caches
Only recent arm64 systems are guaranteed to be able to manipulate the
permissions on live kernel mappings that may use huge mappings. Older
ones can only do so on live mappings that are mapped down to pages.
In order to make execmem caches work on arm64 despite this distinction,
allow huge vmappings to be omitted when allocating the caches.
[Severity: High]
Does dropping VM_ALLOW_HUGE_VMAP here cause a permanent memory leak in the
ROX cache?
When VM_ALLOW_HUGE_VMAP is omitted, the allocation may fall back to the
standard alignment (typically PAGE_SIZE) instead of being PMD_SIZE aligned.
However, the shrinker in execmem_cache_clean() strictly requires PMD_SIZE
alignment to free the memory:
mm/execmem.c:execmem_cache_clean() {
...
mas_for_each(&mas, area, ULONG_MAX) {
size_t size = mas_range_len(&mas);
if (IS_ALIGNED(size, PMD_SIZE) &&
IS_ALIGNED(mas.index, PMD_SIZE)) {
struct vm_struct *vm = find_vm_area(area);
...
}
If the starting address (mas.index) of the chunk allocated without
VM_ALLOW_HUGE_VMAP is not PMD_SIZE aligned, the shrinker will skip it,
never returning it to the vmalloc subsystem.
struct mutex *mutex = &execmem_cache.mutex;
struct vm_struct *vm;
size_t alloc_size;
int err = -ENOMEM;
void *p;
alloc_size = round_up(size, PMD_SIZE);
p = execmem_vmalloc(range, alloc_size, PAGE_KERNEL, vm_flags);
if (!p) {
alloc_size = size;
p = execmem_vmalloc(range, alloc_size, PAGE_KERNEL, vm_flags);
[Severity: High]
This is a pre-existing issue, but does this exact-size fallback allocation
leak memory for the same reason?
If the PMD_SIZE rounded allocation fails under pressure, the fallback
allocates the exact requested size. Since this fallback size is not
guaranteed to be PMD_SIZE aligned, it will also permanently fail the
IS_ALIGNED(size, PMD_SIZE) check in execmem_cache_clean(), leaking the
vmalloc space.
[ ... ]
[Severity: High]
This isn't a bug introduced by this patch, but is it safe to ignore the
return value of execmem_restore_rox() here?
If restoring the ROX permissions fails (for example, if set_memory_rox() fails
internally), the chunk is still added back to the free_areas tree. Subsequent
allocations could then receive memory that is still writable, silently breaking
the W^X security guarantee.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260822135323.795946-11-ardb+git@google.com?part=2
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Live splitting of huge vmalloc mappings silently fails and triggers a kernel warning.
- [High] Permission changes on small, PTE-mapped vmalloc areas are incorrectly rejected.
- [High] Improper use of `WARN_ON_ONCE` for a hardware capability check causes unwarranted kernel log spam and potential panic.
- [Low] Implicit declaration of function `system_supports_bbml2_noabort()` breaks the build.
--
commit e67f32e911a059a6f372cf2818088d9fde53ee7f
Author: Ard Biesheuvel [off-list ref]
arm64: mm: Permit permissions changes on huge vmappings
This commit allows permission changes on huge vmappings without splitting
when the region is PMD-aligned or when the system supports live splitting.
@@ -179,7 +177,16 @@ static int change_memory_common(unsigned long addr, int numpages, if (!area || ((unsigned long)kasan_reset_tag((void *)end) > (unsigned long)kasan_reset_tag(area->addr) + area->size) ||- ((area->flags & (VM_ALLOC | VM_ALLOW_HUGE_VMAP)) != VM_ALLOC))+ !(area->flags & VM_ALLOC)) return -EINVAL;+ /*+ * Disallow VM_ALLOW_HUGE_VMAP mappings unless the region is PMD+ * aligned, or splitting live huge mappings is supported.+ */+ if ((area->flags & VM_ALLOW_HUGE_VMAP) &&+ ((start % PMD_SIZE) || (size % PMD_SIZE)) &&+ WARN_ON_ONCE(!system_supports_bbml2_noabort()))
[Severity: Low]
Does the function system_supports_bbml2_noabort() exist in the codebase? It
appears to be missing a definition or declaration, which might cause the
kernel to fail to compile due to an implicit function declaration.
[Severity: High]
Is WARN_ON_ONCE() the right choice for a hardware capability check here?
If an unaligned permission change is requested on an older system lacking
BBML2 support, this will trigger a warning stack trace. If panic_on_warn
is enabled, an unprivileged user loading a BPF program could trigger a
kernel panic. Should this condition fail silently instead?
[Severity: High]
Does checking area->flags & VM_ALLOW_HUGE_VMAP incorrectly reject small,
PTE-mapped vmalloc areas?
Allocators like execmem use VM_ALLOW_HUGE_VMAP as an allocation hint. If
an allocation is smaller than PMD_SIZE, it falls back to PTE mappings but
retains the VM_ALLOW_HUGE_VMAP flag in area->flags.
When change_memory_common() processes this on older hardware without
BBML2, it sees the flag alongside an unaligned size, and rejects the
request with -EINVAL, even though the page table is already PTE-mapped
and requires no splitting.
Should we check the actual page table granularity, such as inspecting
area->page_order, instead of just the allocation flag?
[Severity: High]
If this condition passes because system_supports_bbml2_noabort() is true,
will the PMD actually be split later?
change_memory_common() delegates to __change_memory_common() which
calls update_range_prot() and eventually split_kernel_leaf_mapping().
Looking at split_kernel_leaf_mapping() in arch/arm64/mm/mmu.c:
if (!linear_map_requires_bbml3 || is_kfence_address((void *)start))
return 0;
if (!system_supports_bbml3()) {
...
return 0;
}
This evaluates system_supports_bbml3() and linear_map_requires_bbml3,
ignoring both the BBML2 capability and the vmalloc context. If those are
false (for example, if rodata is off), it returns 0 without splitting the
PMD.
The subsequent lockless page table walk would then encounter an unsplit PMD
spanning a larger range than requested, resulting in a warning and a
failure.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The check `s->st_shndx != SHN_UNDEF` incorrectly skips PLT counting for special symbols like `SHN_LIVEPATCH` and `SHN_ABS`, breaking livepatching.
- [High] Combining core and init PLTs breaks module loading if `execmem_split` fails and falls back to an independent memory allocation.
--
--- Patch [8]: [RFC PATCH 8/9] arm64: module: Combine init and core PLT entries again ---
commit 9e8ee18408a9e3b59ec81f693240a4dd9c7669a3
Author: Ard Biesheuvel [off-list ref]
arm64: module: Combine init and core PLT entries again
This patch consolidates the core and init PLT sections into a single array,
operating on the assumption that .init.text and .text are now always placed
within direct branching range of each other. It also eliminates the need
to allocate PLT entries for intra-module cross-section calls.
@@ -169,17 +167,17 @@ static unsigned int count_plts(Elf64_Sym *syms, Elf64_Rela *rela, int num, case R_AARCH64_CALL26: /* * We only have to consider branch targets that resolve- * to symbols that are defined in a different section.+ * to symbols that are not defined in the same module. * This is not simply a heuristic, it is a fundamental * limitation, since there is no guaranteed way to emit * PLT entries sufficiently close to the branch if the * section size exceeds the range of a branch * instruction. So ignore relocations against defined- * symbols if they live in the same section as the+ * symbols if they live in the same module as the * relocation target. */
[Severity: High]
Does this safely account for memory pressure during module loading?
While this patch expects .init.text and .text to be placed within direct
branching range, kernel/module/main.c:module_memory_alloc() attempts to
enforce this by using execmem_split().
If execmem_split() fails, such as a mas_store_gfp() failure under memory
pressure, module_memory_alloc() falls back to an independent
execmem_alloc_rw() allocation. Because MODULES_VSIZE on ARM64 is 2GB,
couldn't this fallback place the init section more than 128MB away from
the core section?
If that happens, intra-module calls would exceed the architectural range.
Since this patch removes the PLT entries for them, wouldn't the module
load fail with a WARN_ON in module_emit_plt_entry() and abort the load?
s = syms + ELF64_R_SYM(rela[i].r_info);
- if (s->st_shndx == dstidx)
+ if (s->st_shndx != SHN_UNDEF)
break;
[Severity: High]
Will this skip PLT allocations for special symbols like SHN_LIVEPATCH and
SHN_ABS?
When a livepatch module is loaded, its symbols are marked with SHN_LIVEPATCH
(0xff20) by objtool. Since SHN_LIVEPATCH is not SHN_UNDEF, this check evaluates
to true, breaking the loop and skipping PLT allocation for these symbols.
Later, when klp_apply_section_relocs() attempts to apply the .klp.rela.*
relocations against vmlinux, out-of-range branches will need a PLT entry.
Since none were allocated, won't module_emit_plt_entry() hit the WARN_ON
for exceeding pltsec->plt_max_entries and abort the load with -ENOEXEC?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260822135323.795946-11-ardb+git@google.com?part=8
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] `execmem_fill_trapping_insns()` writes trapping instructions using `memset32()` with a host-endian constant, producing incorrect/unallocated instructions on big-endian ARM64 kernels.
- [High] `execmem_fill_trapping_insns()` fails to synchronize D/I caches to the Point of Unification (PoU), potentially causing execution of random uninitialized garbage.
- [Low] The function `system_supports_bbml2_noabort()` is called but is completely undefined in the kernel tree.
--
--- Patch [6]: [RFC PATCH 6/9] arm64: Enable the execmem ROX cache for module text ---
commit 9f2855ae0653215694678b01c1ed088b2dce033c
Author: Ard Biesheuvel [off-list ref]
arm64: Enable the execmem ROX cache for module text
This commit wires up execmem ROX cache support on arm64 for module text and
init.text regions. This allows these regions to be allocated from a single
chunk of memory in subsequent changes.
[Severity: Low]
Does system_supports_bbml2_noabort() exist in the kernel tree? It appears
this might cause a compilation error due to an undefined reference.
[Severity: High]
Does this correctly handle big-endian ARM64 kernels?
Since AARCH64_BREAK_FAULT is a host-endian constant and instruction fetches
are strictly little-endian, it looks like this might write an incorrect byte
pattern and execute unallocated instructions instead of the intended trap if
cpu_to_le32() isn't used.
[Severity: High]
Does this missing cache synchronization allow the CPU to fetch stale
instructions?
Writing instructions as data requires cleaning the D-cache and invalidating
the I-cache to the Point of Unification (PoU) (e.g., using
flush_icache_range()). Otherwise, the CPU might bypass the newly written trap
instructions and fetch garbage from physical memory or the I-cache.
From: Adrian Barnaś <hidden> Date: 2026-08-23 16:47:02
On Sat, Aug 22, 2026 at 03:53:28PM +0200, Ard Biesheuvel wrote:
From: Ard Biesheuvel <ardb@kernel.org>
Wire up the existing support for the execmem ROX cache on arm64, so that
it will be used for module .text and .init.text regions. This will be
relied upon by a subsequent patch in order to allocate those regions
from a single chunk of memory.
From: Adrian Barnaś <hidden> Date: 2026-08-23 16:53:08
Hi Ard
On Sat, Aug 22, 2026 at 03:53:27PM +0200, Ard Biesheuvel wrote:
quoted hunk
From: Ard Biesheuvel <ardb@kernel.org>
Allow permission changes on huge vmappings in cases where no splitting
is needed (i.e., the region is aligned sufficiently), or when the system
has support for splitting live mappings.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
arch/arm64/mm/pageattr.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
@@ -169,8 +169,6 @@ static int change_memory_common(unsigned long addr, int numpages,
* we are operating on does not result in such splitting.
*
* Let's restrict ourselves to mappings created by vmalloc (or vmap).
- * Disallow VM_ALLOW_HUGE_VMAP mappings to guarantee that only page
- * mappings are updated and splitting is never needed.
*
* So check whether the [addr, addr + size) interval is entirely
* covered by precisely one VM area that has the VM_ALLOC flag set.
@@ -179,7 +177,16 @@ static int change_memory_common(unsigned long addr, int numpages,
if (!area ||
((unsigned long)kasan_reset_tag((void *)end) >
(unsigned long)kasan_reset_tag(area->addr) + area->size) ||
- ((area->flags & (VM_ALLOC | VM_ALLOW_HUGE_VMAP)) != VM_ALLOC))
+ !(area->flags & VM_ALLOC))
+ return -EINVAL;
+
+ /*
+ * Disallow VM_ALLOW_HUGE_VMAP mappings unless the region is PMD
+ * aligned, or splitting live huge mappings is supported.
+ */
+ if ((area->flags & VM_ALLOW_HUGE_VMAP) &&
+ ((start % PMD_SIZE) || (size % PMD_SIZE)) &&
If I understand the intention here correctly, I don't think it is valid. Even
if it is PMD-sized and PMD-aligned, it would still cause a split because
the loop below is not using page order, but performs attribute changes
page by page.
Best regards,
Adrian
From: Petr Pavlu <petr.pavlu@suse.com> Date: 2026-08-28 13:07:41
On 8/22/26 3:53 PM, Ard Biesheuvel wrote:
From: Ard Biesheuvel <ardb@kernel.org>
The arm64 module loader has to deal with a couple of corner cases that
may occur when .init.text is placed out of direct branch range of .text:
- ordinary direct branches from .init.text into .text may require the
use of a PLT entry (i.e., a trampoline aka veneer), which means not
only that additional PLT entries need to be allocated for
cross-section calls, but also that .init.text needs its own PLT
reservation, as the one in .text will be out of range as well;
- dynamic patching of the ftrace handler into .init.text code needs its
own dedicated trampoline as the one in .text may be too far away.
- recent compilers may omit BTI veneers for static functions that never
have their address taken, and so additional veneers will need to be
added to .text in case cross-section direct branches from .init.text
require a PLT entry (and therefore a landing pad at the target end).
This is unfortunate, because it is actually somewhat unusual for .text
and .init.text to be so far away from each other: only when allocating
either of them (but not both) exhausts the 'near' (PLT-less) module
region, the other will be allocated from the spillover region, which is
not in direct branching range, and therefore requires PLT entries for
cross-section calls.
This series addresses this wart by allocating both of them as a single
chunk, and freeing the .init.text part along with the other init
sections at the appropriate time. This ensures that the two regions will
never require veneers for cross-section calls, allowing the arm64 module
loader to be simplified.
It looks like this should also be useful for ppc64, which currently
merges .init.text and .text because keeping them separate would require
stubs between the two, and consequently .init.text is never released in
modules on this architecture.
--
Thanks,
Petr
From: Ard Biesheuvel <ardb@kernel.org>
The arm64 module loader has to deal with a couple of corner cases that
may occur when .init.text is placed out of direct branch range of .text:
- ordinary direct branches from .init.text into .text may require the
use of a PLT entry (i.e., a trampoline aka veneer), which means not
only that additional PLT entries need to be allocated for
cross-section calls, but also that .init.text needs its own PLT
reservation, as the one in .text will be out of range as well;
- dynamic patching of the ftrace handler into .init.text code needs its
own dedicated trampoline as the one in .text may be too far away.
- recent compilers may omit BTI veneers for static functions that never
have their address taken, and so additional veneers will need to be
added to .text in case cross-section direct branches from .init.text
require a PLT entry (and therefore a landing pad at the target end).
This is unfortunate, because it is actually somewhat unusual for .text
and .init.text to be so far away from each other: only when allocating
either of them (but not both) exhausts the 'near' (PLT-less) module
region, the other will be allocated from the spillover region, which is
not in direct branching range, and therefore requires PLT entries for
cross-section calls.
This series addresses this wart by allocating both of them as a single
chunk, and freeing the .init.text part along with the other init
sections at the appropriate time. This ensures that the two regions will
never require veneers for cross-section calls, allowing the arm64 module
loader to be simplified.
It looks like this should also be useful for ppc64, which currently
merges .init.text and .text because keeping them separate would require
stubs between the two, and consequently .init.text is never released in
modules on this architecture.
Thanks for the data point - are those stubs needed when there is some
distance between the placements of .text and .init.text?
From: Bradley Morgan <hidden> Date: 2026-08-28 14:11:56
On 22 August 2026 14:53:26 BST, Ard Biesheuvel [off-list ref] wrote:
From: Ard Biesheuvel <ardb@kernel.org>
When execmem ROX caches are used for module text and inittext, place
them adjacently in memory, by allocating space for both initially, and
splitting off the space for MOD_INIT_TEXT as needed.
This avoids the corner case on arm64, where .init.text being placed far
from .text results in a lot of complexity wrt indirect branches and PLTs
that we'd prefer to avoid.
I can't see anything wrong, thanks for the patch
Reviewed-by: Bradley Morgan <redacted> # kernel/
else
execmem_type = EXECMEM_MODULE_TEXT;
- ptr = execmem_alloc_rw(execmem_type, size);
+ bool is_rox = execmem_is_rox(execmem_type);
+ if (is_rox) {
+ /*
+ * Special case for MOD_TEXT / MOD_INIT_TEXT: allocate the
+ * latter by splitting off required space from the former
+ * so that they are always placed close together.
+ */
+ if (type == MOD_TEXT)
+ size += PAGE_ALIGN(mod->mem[MOD_INIT_TEXT].size);
+ else if (type == MOD_INIT_TEXT)
+ ptr = execmem_split(mod->mem[MOD_TEXT].base, size);
+ }
+
+ if (!ptr)
+ ptr = execmem_alloc_rw(execmem_type, size);
if (!ptr)
return -ENOMEM;
- mod->mem[type].is_rox = execmem_is_rox(execmem_type);
+ mod->mem[type].is_rox = is_rox;
/*
* The pointer to these blocks of memory are stored on the module
* *do* eventually get freed, but let's just keep things simple
* and avoid *any* false positives.
*/
- if (!mod->mem[type].is_rox)
+ if (!is_rox)
kmemleak_not_leak(ptr);
memset(ptr, 0, size);
From: Petr Pavlu <petr.pavlu@suse.com> Date: 2026-08-31 13:30:50
On 8/28/26 3:45 PM, Ard Biesheuvel wrote:
On Fri, 28 Aug 2026, at 15:07, Petr Pavlu wrote:
quoted
On 8/22/26 3:53 PM, Ard Biesheuvel wrote:
quoted
From: Ard Biesheuvel <ardb@kernel.org>
The arm64 module loader has to deal with a couple of corner cases that
may occur when .init.text is placed out of direct branch range of .text:
- ordinary direct branches from .init.text into .text may require the
use of a PLT entry (i.e., a trampoline aka veneer), which means not
only that additional PLT entries need to be allocated for
cross-section calls, but also that .init.text needs its own PLT
reservation, as the one in .text will be out of range as well;
- dynamic patching of the ftrace handler into .init.text code needs its
own dedicated trampoline as the one in .text may be too far away.
- recent compilers may omit BTI veneers for static functions that never
have their address taken, and so additional veneers will need to be
added to .text in case cross-section direct branches from .init.text
require a PLT entry (and therefore a landing pad at the target end).
This is unfortunate, because it is actually somewhat unusual for .text
and .init.text to be so far away from each other: only when allocating
either of them (but not both) exhausts the 'near' (PLT-less) module
region, the other will be allocated from the spillover region, which is
not in direct branching range, and therefore requires PLT entries for
cross-section calls.
This series addresses this wart by allocating both of them as a single
chunk, and freeing the .init.text part along with the other init
sections at the appropriate time. This ensures that the two regions will
never require veneers for cross-section calls, allowing the arm64 module
loader to be simplified.
It looks like this should also be useful for ppc64, which currently
merges .init.text and .text because keeping them separate would require
stubs between the two, and consequently .init.text is never released in
modules on this architecture.
Thanks for the data point - are those stubs needed when there is some
distance between the placements of .text and .init.text?
Yes, my understanding is that these stubs are primarily needed because
the BL instruction on ppc64 can only reach a range of +-32 MB.
Another aspect on ppc64 is the use of the Table of Contents (TOC). In
theory, when splitting .text and .init.text, one would also want
separate TOCs, along with stubs to support switching between them.
However, I don't think this is particularly feasible. As far as I can
see, the ABI and GCC don't allow separate TOCs within a single
relocatable object file. In practice, it shouldn't be a large problem to
keep a single TOC, even if some data related only to .init.text remains
present after the module is loaded. Being able to free .init.text is the
important part. On newer Power10 with PCREL, the TOC is not used, so
this issue goes away.
--
Cheers,
Petr
From: Mike Rapoport <rppt@kernel.org> Date: 2026-09-02 17:23:46
Hi Ard,
On Sat, Aug 22, 2026 at 03:53:22PM +0200, Ard Biesheuvel wrote:
From: Ard Biesheuvel <ardb@kernel.org>
The arm64 module loader has to deal with a couple of corner cases that
may occur when .init.text is placed out of direct branch range of .text:
- ordinary direct branches from .init.text into .text may require the
use of a PLT entry (i.e., a trampoline aka veneer), which means not
only that additional PLT entries need to be allocated for
cross-section calls, but also that .init.text needs its own PLT
reservation, as the one in .text will be out of range as well;
- dynamic patching of the ftrace handler into .init.text code needs its
own dedicated trampoline as the one in .text may be too far away.
- recent compilers may omit BTI veneers for static functions that never
have their address taken, and so additional veneers will need to be
added to .text in case cross-section direct branches from .init.text
require a PLT entry (and therefore a landing pad at the target end).
This is unfortunate, because it is actually somewhat unusual for .text
and .init.text to be so far away from each other: only when allocating
either of them (but not both) exhausts the 'near' (PLT-less) module
region, the other will be allocated from the spillover region, which is
not in direct branching range, and therefore requires PLT entries for
cross-section calls.
This series addresses this wart by allocating both of them as a single
chunk, and freeing the .init.text part along with the other init
sections at the appropriate time. This ensures that the two regions will
never require veneers for cross-section calls, allowing the arm64 module
loader to be simplified.
Ard Biesheuvel (9):
mm: execmem: Add API to split an existing execmem cache allocation
mm: execmem: Allow huge vmappings to be avoided for execmem caches
module: Place MOD_TEXT before MOD_INIT_TEXT in enumeration
module: Allocate MOD_INIT_TEXT from the MOD_TEXT ROX allocation
arm64: mm: Permit permissions changes on huge vmappings
arm64: Enable the execmem ROX cache for module text
arm64: ftrace: Revert "fix unreachable PLT for ftrace_caller ..."
arm64: module: Combine init and core PLT entries again
arm64: ftrace: Simplify PLT handling
I can't say I like the idea of tying the single chunk allocation of
modules .text and .init.text to the ROX cache.
The goal of the cache is to have executable code mapped at higher page
table levels. Letting it use base-page mappings completely dismisses it.
I'm not against having execmem_split() or something along these lines, but
it should work without the ROX cache as well.
Another question I had is did you consider splitting the area on free
rather than on alloc?
In this case it could be execmem_shrink() that immediately frees the
.init.text part.