x86/module: use large ROX pages for text allocations
Subsystems (41)
Mike Rapoport
maintainercover x86/module: use large ROX pages for text allocations
From: "Mike Rapoport (IBM)" <[email protected]>
Hi,
These patches add support for using large ROX pages for allocations of executable memory on x86.
They address Andy's comments [1] about having executable mappings for code that was not completely formed.
The approach taken is to allocate ROX memory along with writable but not executable memory and use the writable copy to perform relocations and alternatives patching. After the module text gets into its final shape, the contents of the writable memory is copied into the actual ROX location using text poking.
The allocations of the ROX memory use vmalloc(VMAP_ALLOW_HUGE_MAP) to allocate PMD aligned memory, fill that memory with invalid instructions and in the end remap it as ROX. Portions of these large pages 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.
The module memory allocation, x86 code dealing with relocations and alternatives patching takes into account the existence of the two copies, the writable memory and the ROX memory at the actual allocated virtual address.
This is an early RFC, it is not well tested and there is a lot of room for improvement. For example, the locking of execmem_cache can be made more fine grained, freeing of PMD-sized pages from execmem_cache can be implemented with a shrinker, the large pages can be removed from the direct map when they are added to the cache and restored there when they are free from the cache.
Still, I'd like to hear feedback on the approach in general before moving forward with polishing the details.
The series applies on top of v4 of "jit/text allocator" [2] and also available at git:
https://git.kernel.org/pub/scm/linux/kernel/git/rppt/linux.git/log/?h=execmem/v4%2bx86-rox
[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/linux-mm/[email protected]
Mike Rapoport (IBM) (6):
asm-generic: introduce text-patching.h
mm: vmalloc: don't account for number of nodes for HUGE_VMAP allocations
module: prepare to handle ROX allocations for text x86/module: perpare module loading for ROX allocations of text
execmem: add support for cache of large ROX pages x86/module: enable ROX caches for module text
Song Liu (1):
ftrace: Add swap_func to ftrace_process_locs()
arch/alpha/include/asm/Kbuild | 1 +
arch/arc/include/asm/Kbuild | 1 +
.../include/asm/{patch.h => text-patching.h} | 0
arch/arm/kernel/ftrace.c | 2 +-
arch/arm/kernel/jump_label.c | 2 +-
arch/arm/kernel/kgdb.c | 2 +-
arch/arm/kernel/patch.c | 2 +-
arch/arm/probes/kprobes/core.c | 2 +-
arch/arm/probes/kprobes/opt-arm.c | 2 +-
.../asm/{patching.h => text-patching.h} | 0
arch/arm64/kernel/ftrace.c | 2 +-
arch/arm64/kernel/jump_label.c | 2 +-
arch/arm64/kernel/kgdb.c | 2 +-
arch/arm64/kernel/patching.c | 2 +-
arch/arm64/kernel/probes/kprobes.c | 2 +-
arch/arm64/kernel/traps.c | 2 +-
arch/arm64/net/bpf_jit_comp.c | 2 +-
arch/csky/include/asm/Kbuild | 1 +
arch/hexagon/include/asm/Kbuild | 1 +
arch/loongarch/include/asm/Kbuild | 1 +
arch/m68k/include/asm/Kbuild | 1 +
arch/microblaze/include/asm/Kbuild | 1 +
arch/mips/include/asm/Kbuild | 1 +
arch/nios2/include/asm/Kbuild | 1 +
arch/openrisc/include/asm/Kbuild | 1 +
.../include/asm/{patch.h => text-patching.h} | 0
arch/parisc/kernel/ftrace.c | 2 +-
arch/parisc/kernel/jump_label.c | 2 +-
arch/parisc/kernel/kgdb.c | 2 +-
arch/parisc/kernel/kprobes.c | 2 +-
arch/parisc/kernel/patch.c | 2 +-
arch/powerpc/include/asm/kprobes.h | 2 +-
.../asm/{code-patching.h => text-patching.h} | 0
arch/powerpc/kernel/crash_dump.c | 2 +-
arch/powerpc/kernel/epapr_paravirt.c | 2 +-
arch/powerpc/kernel/jump_label.c | 2 +-
arch/powerpc/kernel/kgdb.c | 2 +-
arch/powerpc/kernel/kprobes.c | 2 +-
arch/powerpc/kernel/module_32.c | 2 +-
arch/powerpc/kernel/module_64.c | 2 +-
arch/powerpc/kernel/optprobes.c | 2 +-
arch/powerpc/kernel/process.c | 2 +-
arch/powerpc/kernel/security.c | 2 +-
arch/powerpc/kernel/setup_32.c | 2 +-
arch/powerpc/kernel/setup_64.c | 2 +-
arch/powerpc/kernel/static_call.c | 2 +-
arch/powerpc/kernel/trace/ftrace.c | 2 +-
arch/powerpc/kernel/trace/ftrace_64_pg.c | 2 +-
arch/powerpc/lib/code-patching.c | 2 +-
arch/powerpc/lib/feature-fixups.c | 2 +-
arch/powerpc/lib/test-code-patching.c | 2 +-
arch/powerpc/lib/test_emulate_step.c | 2 +-
arch/powerpc/mm/book3s32/mmu.c | 2 +-
arch/powerpc/mm/book3s64/hash_utils.c | 2 +-
arch/powerpc/mm/book3s64/slb.c | 2 +-
arch/powerpc/mm/kasan/init_32.c | 2 +-
arch/powerpc/mm/mem.c | 2 +-
arch/powerpc/mm/nohash/44x.c | 2 +-
arch/powerpc/mm/nohash/book3e_pgtable.c | 2 +-
arch/powerpc/mm/nohash/tlb.c | 2 +-
arch/powerpc/net/bpf_jit_comp.c | 2 +-
arch/powerpc/perf/8xx-pmu.c | 2 +-
arch/powerpc/perf/core-book3s.c | 2 +-
arch/powerpc/platforms/85xx/smp.c | 2 +-
arch/powerpc/platforms/86xx/mpc86xx_smp.c | 2 +-
arch/powerpc/platforms/cell/smp.c | 2 +-
arch/powerpc/platforms/powermac/smp.c | 2 +-
arch/powerpc/platforms/powernv/idle.c | 2 +-
arch/powerpc/platforms/powernv/smp.c | 2 +-
arch/powerpc/platforms/pseries/smp.c | 2 +-
arch/powerpc/xmon/xmon.c | 2 +-
arch/riscv/errata/andes/errata.c | 2 +-
arch/riscv/errata/sifive/errata.c | 2 +-
arch/riscv/errata/thead/errata.c | 2 +-
.../include/asm/{patch.h => text-patching.h} | 0
arch/riscv/include/asm/uprobes.h | 2 +-
arch/riscv/kernel/alternative.c | 2 +-
arch/riscv/kernel/cpufeature.c | 3 +-
arch/riscv/kernel/ftrace.c | 2 +-
arch/riscv/kernel/jump_label.c | 2 +-
arch/riscv/kernel/patch.c | 2 +-
arch/riscv/kernel/probes/kprobes.c | 2 +-
arch/riscv/net/bpf_jit_comp64.c | 2 +-
arch/riscv/net/bpf_jit_core.c | 2 +-
arch/sh/include/asm/Kbuild | 1 +
arch/sparc/include/asm/Kbuild | 1 +
arch/um/kernel/um_arch.c | 16 +-
arch/x86/entry/vdso/vma.c | 3 +-
arch/x86/include/asm/alternative.h | 14 +-
arch/x86/include/asm/text-patching.h | 1 +
arch/x86/kernel/alternative.c | 152 ++++++----
arch/x86/kernel/ftrace.c | 41 ++-
arch/x86/kernel/module.c | 17 +-
arch/x86/mm/init.c | 29 +-
arch/xtensa/include/asm/Kbuild | 1 +
include/asm-generic/text-patching.h | 5 +
include/linux/execmem.h | 25 ++
include/linux/ftrace.h | 2 +
include/linux/module.h | 11 +
include/linux/text-patching.h | 15 +
kernel/module/main.c | 70 ++++-
kernel/module/strict_rwx.c | 3 +
kernel/trace/ftrace.c | 13 +-
mm/execmem.c | 278 +++++++++++++++++-
mm/vmalloc.c | 9 +-
105 files changed, 663 insertions(+), 193 deletions(-)
rename arch/arm/include/asm/{patch.h => text-patching.h} (100%)
rename arch/arm64/include/asm/{patching.h => text-patching.h} (100%)
rename arch/parisc/include/asm/{patch.h => text-patching.h} (100%)
rename arch/powerpc/include/asm/{code-patching.h => text-patching.h} (100%)
rename arch/riscv/include/asm/{patch.h => text-patching.h} (100%)
create mode 100644 include/asm-generic/text-patching.h
create mode 100644 include/linux/text-patching.hSignature · 1 line
2.43.0
Mike Rapoport
maintainerRFC PATCH 1/7 asm-generic: introduce text-patching.h
From: "Mike Rapoport (IBM)" <[email protected]>
Several architectures support text patching, but they name the header files that declare patching functions differently.
Make all such headers consistently named text-patching.h and add an empty header in asm-generic for architectures that do not support text patching.
---
arch/alpha/include/asm/Kbuild | 1 +
arch/arc/include/asm/Kbuild | 1 +
arch/arm/include/asm/{patch.h => text-patching.h} | 0
arch/arm/kernel/ftrace.c | 2 +-
arch/arm/kernel/jump_label.c | 2 +-
arch/arm/kernel/kgdb.c | 2 +-
arch/arm/kernel/patch.c | 2 +-
arch/arm/probes/kprobes/core.c | 2 +-
arch/arm/probes/kprobes/opt-arm.c | 2 +-
.../include/asm/{patching.h => text-patching.h} | 0
arch/arm64/kernel/ftrace.c | 2 +-
arch/arm64/kernel/jump_label.c | 2 +-
arch/arm64/kernel/kgdb.c | 2 +-
arch/arm64/kernel/patching.c | 2 +-
arch/arm64/kernel/probes/kprobes.c | 2 +-
arch/arm64/kernel/traps.c | 2 +-
arch/arm64/net/bpf_jit_comp.c | 2 +-
arch/csky/include/asm/Kbuild | 1 +
arch/hexagon/include/asm/Kbuild | 1 +
arch/loongarch/include/asm/Kbuild | 1 +
arch/m68k/include/asm/Kbuild | 1 +
arch/microblaze/include/asm/Kbuild | 1 +
arch/mips/include/asm/Kbuild | 1 +
arch/nios2/include/asm/Kbuild | 1 +
arch/openrisc/include/asm/Kbuild | 1 +
.../include/asm/{patch.h => text-patching.h} | 0
arch/parisc/kernel/ftrace.c | 2 +-
arch/parisc/kernel/jump_label.c | 2 +-
arch/parisc/kernel/kgdb.c | 2 +-
arch/parisc/kernel/kprobes.c | 2 +-
arch/parisc/kernel/patch.c | 2 +-
arch/powerpc/include/asm/kprobes.h | 2 +-
.../asm/{code-patching.h => text-patching.h} | 0
arch/powerpc/kernel/crash_dump.c | 2 +-
arch/powerpc/kernel/epapr_paravirt.c | 2 +-
arch/powerpc/kernel/jump_label.c | 2 +-
arch/powerpc/kernel/kgdb.c | 2 +-
arch/powerpc/kernel/kprobes.c | 2 +-
arch/powerpc/kernel/module_32.c | 2 +-
arch/powerpc/kernel/module_64.c | 2 +-
arch/powerpc/kernel/optprobes.c | 2 +-
arch/powerpc/kernel/process.c | 2 +-
arch/powerpc/kernel/security.c | 2 +-
arch/powerpc/kernel/setup_32.c | 2 +-
arch/powerpc/kernel/setup_64.c | 2 +-
arch/powerpc/kernel/static_call.c | 2 +-
arch/powerpc/kernel/trace/ftrace.c | 2 +-
arch/powerpc/kernel/trace/ftrace_64_pg.c | 2 +-
arch/powerpc/lib/code-patching.c | 2 +-
arch/powerpc/lib/feature-fixups.c | 2 +-
arch/powerpc/lib/test-code-patching.c | 2 +-
arch/powerpc/lib/test_emulate_step.c | 2 +-
arch/powerpc/mm/book3s32/mmu.c | 2 +-
arch/powerpc/mm/book3s64/hash_utils.c | 2 +-
arch/powerpc/mm/book3s64/slb.c | 2 +-
arch/powerpc/mm/kasan/init_32.c | 2 +-
arch/powerpc/mm/mem.c | 2 +-
arch/powerpc/mm/nohash/44x.c | 2 +-
arch/powerpc/mm/nohash/book3e_pgtable.c | 2 +-
arch/powerpc/mm/nohash/tlb.c | 2 +-
arch/powerpc/net/bpf_jit_comp.c | 2 +-
arch/powerpc/perf/8xx-pmu.c | 2 +-
arch/powerpc/perf/core-book3s.c | 2 +-
arch/powerpc/platforms/85xx/smp.c | 2 +-
arch/powerpc/platforms/86xx/mpc86xx_smp.c | 2 +-
arch/powerpc/platforms/cell/smp.c | 2 +-
arch/powerpc/platforms/powermac/smp.c | 2 +-
arch/powerpc/platforms/powernv/idle.c | 2 +-
arch/powerpc/platforms/powernv/smp.c | 2 +-
arch/powerpc/platforms/pseries/smp.c | 2 +-
arch/powerpc/xmon/xmon.c | 2 +-
arch/riscv/errata/andes/errata.c | 2 +-
arch/riscv/errata/sifive/errata.c | 2 +-
arch/riscv/errata/thead/errata.c | 2 +-
.../include/asm/{patch.h => text-patching.h} | 0
arch/riscv/include/asm/uprobes.h | 2 +-
arch/riscv/kernel/alternative.c | 2 +-
arch/riscv/kernel/cpufeature.c | 3 ++-
arch/riscv/kernel/ftrace.c | 2 +-
arch/riscv/kernel/jump_label.c | 2 +-
arch/riscv/kernel/patch.c | 2 +-
arch/riscv/kernel/probes/kprobes.c | 2 +-
arch/riscv/net/bpf_jit_comp64.c | 2 +-
arch/riscv/net/bpf_jit_core.c | 2 +-
arch/sh/include/asm/Kbuild | 1 +
arch/sparc/include/asm/Kbuild | 1 +
arch/um/kernel/um_arch.c | 5 +++++
arch/x86/include/asm/text-patching.h | 1 +
arch/xtensa/include/asm/Kbuild | 1 +
include/asm-generic/text-patching.h | 5 +++++
include/linux/text-patching.h | 15 +++++++++++++++
91 files changed, 109 insertions(+), 69 deletions(-)
rename arch/arm/include/asm/{patch.h => text-patching.h} (100%)
rename arch/arm64/include/asm/{patching.h => text-patching.h} (100%)
rename arch/parisc/include/asm/{patch.h => text-patching.h} (100%)
rename arch/powerpc/include/asm/{code-patching.h => text-patching.h} (100%)
rename arch/riscv/include/asm/{patch.h => text-patching.h} (100%)
create mode 100644 include/asm-generic/text-patching.h
create mode 100644 include/linux/text-patching.harch/alpha/include/asm/Kbuild +1 -0
@@ -5,3 +5,4 @@ generic-y += agp.h generic-y += asm-offsets.h generic-y += kvm_para.h generic-y += mcs_spinlock.h +generic-y += text-patching.h
arch/arc/include/asm/Kbuild +1 -0
@@ -4,3 +4,4 @@ generic-y += kvm_para.h generic-y += mcs_spinlock.h generic-y += parport.h generic-y += user.h +generic-y += text-patching.h
arch/arm/include/asm/text-patching.h renamed from arch/arm/include/asm/patch.h +0 -0
arch/arm/kernel/ftrace.c +1 -1
@@ -23,7 +23,7 @@#include <asm/insn.h> #include <asm/set_memory.h> #include <asm/stacktrace.h> -#include <asm/patch.h> +#include <asm/text-patching.h> /* * The compiler emitted profiling hook consists of
arch/arm/kernel/jump_label.c +1 -1
@@ -1,7 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/kernel.h> #include <linux/jump_label.h> -#include <asm/patch.h> +#include <asm/text-patching.h> #include <asm/insn.h> static void __arch_jump_label_transform(struct jump_entry *entry,
arch/arm/kernel/kgdb.c +1 -1
@@ -15,7 +15,7 @@#include <linux/kgdb.h> #include <linux/uaccess.h> -#include <asm/patch.h> +#include <asm/text-patching.h> #include <asm/traps.h> struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
arch/arm/kernel/patch.c +1 -1
@@ -9,7 +9,7 @@#include <asm/fixmap.h> #include <asm/smp_plat.h> #include <asm/opcodes.h> -#include <asm/patch.h> +#include <asm/text-patching.h> struct patch {void *addr;
arch/arm/probes/kprobes/core.c +1 -1
@@ -25,7 +25,7 @@#include <asm/cacheflush.h> #include <linux/percpu.h> #include <linux/bug.h> -#include <asm/patch.h> +#include <asm/text-patching.h> #include <asm/sections.h> #include "../decode-arm.h"
arch/arm/probes/kprobes/opt-arm.c +1 -1
@@ -14,7 +14,7 @@/* for arm_gen_branch */ #include <asm/insn.h> /* for patch_text */ -#include <asm/patch.h> +#include <asm/text-patching.h> #include "core.h"
arch/arm64/include/asm/text-patching.h renamed from arch/arm64/include/asm/patching.h +0 -0
arch/arm64/kernel/ftrace.c +1 -1
@@ -15,7 +15,7 @@#include <asm/debug-monitors.h> #include <asm/ftrace.h> #include <asm/insn.h> -#include <asm/patching.h> +#include <asm/text-patching.h> #ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS struct fregs_offset {
arch/arm64/kernel/jump_label.c +1 -1
@@ -8,7 +8,7 @@#include <linux/kernel.h> #include <linux/jump_label.h> #include <asm/insn.h> -#include <asm/patching.h> +#include <asm/text-patching.h> void arch_jump_label_transform (struct jump_entry *entry,enum jump_label_type type)
arch/arm64/kernel/kgdb.c +1 -1
@@ -17,7 +17,7 @@#include <asm/debug-monitors.h> #include <asm/insn.h> -#include <asm/patching.h> +#include <asm/text-patching.h> #include <asm/traps.h> struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM ] = {
arch/arm64/kernel/patching.c +1 -1
@@ -10,7 +10,7 @@#include <asm/fixmap.h> #include <asm/insn.h> #include <asm/kprobes.h> -#include <asm/patching.h> +#include <asm/text-patching.h> #include <asm/sections.h> static DEFINE_RAW_SPINLOCK (patch_lock);
arch/arm64/kernel/probes/kprobes.c +1 -1
@@ -27,7 +27,7 @@#include <asm/debug-monitors.h> #include <asm/insn.h> #include <asm/irq.h> -#include <asm/patching.h> +#include <asm/text-patching.h> #include <asm/ptrace.h> #include <asm/sections.h> #include <asm/system_misc.h>
arch/arm64/kernel/traps.c +1 -1
@@ -41,7 +41,7 @@#include <asm/extable.h> #include <asm/insn.h> #include <asm/kprobes.h> -#include <asm/patching.h> +#include <asm/text-patching.h> #include <asm/traps.h> #include <asm/smp.h> #include <asm/stack_pointer.h>
arch/arm64/net/bpf_jit_comp.c +1 -1
@@ -19,7 +19,7 @@#include <asm/cacheflush.h> #include <asm/debug-monitors.h> #include <asm/insn.h> -#include <asm/patching.h> +#include <asm/text-patching.h> #include <asm/set_memory.h> #include "bpf_jit.h"
arch/csky/include/asm/Kbuild +1 -0
@@ -10,3 +10,4 @@ generic-y += qspinlock.h generic-y += parport.h generic-y += user.h generic-y += vmlinux.lds.h +generic-y += text-patching.h
arch/hexagon/include/asm/Kbuild +1 -0
@@ -3,3 +3,4 @@ generic-y += extable.h generic-y += iomap.h generic-y += kvm_para.h generic-y += mcs_spinlock.h +generic-y += text-patching.h
arch/loongarch/include/asm/Kbuild +1 -0
@@ -27,3 +27,4 @@ generic-y += param.h generic-y += posix_types.h generic-y += resource.h generic-y += kvm_para.h +generic-y += text-patching.h
arch/m68k/include/asm/Kbuild +1 -0
@@ -4,3 +4,4 @@ generic-y += extable.h generic-y += kvm_para.h generic-y += mcs_spinlock.h generic-y += spinlock.h +generic-y += text-patching.h
arch/microblaze/include/asm/Kbuild +1 -0
@@ -8,3 +8,4 @@ generic-y += parport.h generic-y += syscalls.h generic-y += tlb.h generic-y += user.h +generic-y += text-patching.h
arch/mips/include/asm/Kbuild +1 -0
@@ -13,3 +13,4 @@ generic-y += parport.h generic-y += qrwlock.h generic-y += qspinlock.h generic-y += user.h +generic-y += text-patching.h
arch/nios2/include/asm/Kbuild +1 -0
@@ -5,3 +5,4 @@ generic-y += kvm_para.h generic-y += mcs_spinlock.h generic-y += spinlock.h generic-y += user.h +generic-y += text-patching.h
arch/openrisc/include/asm/Kbuild +1 -0
@@ -7,3 +7,4 @@ generic-y += spinlock.h generic-y += qrwlock_types.h generic-y += qrwlock.h generic-y += user.h +generic-y += text-patching.h
arch/parisc/include/asm/text-patching.h renamed from arch/parisc/include/asm/patch.h +0 -0
arch/parisc/kernel/ftrace.c +1 -1
@@ -20,7 +20,7 @@#include <asm/assembly.h> #include <asm/sections.h> #include <asm/ftrace.h> -#include <asm/patch.h> +#include <asm/text-patching.h> #define __hot __section(".text.hot")
arch/parisc/kernel/jump_label.c +1 -1
@@ -8,7 +8,7 @@#include <linux/jump_label.h> #include <linux/bug.h> #include <asm/alternative.h> -#include <asm/patch.h> +#include <asm/text-patching.h> static inline int reassemble_17 (int as17) {
arch/parisc/kernel/kgdb.c +1 -1
@@ -16,7 +16,7 @@#include <asm/ptrace.h> #include <asm/traps.h> #include <asm/processor.h> -#include <asm/patch.h> +#include <asm/text-patching.h> #include <asm/cacheflush.h> const struct kgdb_arch arch_kgdb_ops = {
arch/parisc/kernel/kprobes.c +1 -1
@@ -12,7 +12,7 @@#include <linux/kprobes.h> #include <linux/slab.h> #include <asm/cacheflush.h> -#include <asm/patch.h> +#include <asm/text-patching.h> DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
arch/parisc/kernel/patch.c +1 -1
@@ -13,7 +13,7 @@#include <asm/cacheflush.h> #include <asm/fixmap.h> -#include <asm/patch.h> +#include <asm/text-patching.h> struct patch {void *addr;
arch/powerpc/include/asm/kprobes.h +1 -1
@@ -21,7 +21,7 @@#include <linux/percpu.h> #include <linux/module.h> #include <asm/probes.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #ifdef CONFIG_KPROBES #define __ARCH_WANT_KPROBES_INSN_SLOT
arch/powerpc/include/asm/text-patching.h renamed from arch/powerpc/include/asm/code-patching.h +0 -0
arch/powerpc/kernel/crash_dump.c +1 -1
@@ -13,7 +13,7 @@#include <linux/io.h> #include <linux/memblock.h> #include <linux/of.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/kdump.h> #include <asm/firmware.h> #include <linux/uio.h>
arch/powerpc/kernel/epapr_paravirt.c +1 -1
@@ -9,7 +9,7 @@#include <linux/of_fdt.h> #include <asm/epapr_hcalls.h> #include <asm/cacheflush.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/machdep.h> #include <asm/inst.h>
arch/powerpc/kernel/jump_label.c +1 -1
@@ -5,7 +5,7 @@#include <linux/kernel.h> #include <linux/jump_label.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/inst.h> void arch_jump_label_transform(struct jump_entry *entry,
arch/powerpc/kernel/kgdb.c +1 -1
@@ -21,7 +21,7 @@#include <asm/processor.h> #include <asm/machdep.h> #include <asm/debug.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <linux/slab.h> #include <asm/inst.h>
arch/powerpc/kernel/kprobes.c +1 -1
@@ -21,7 +21,7 @@#include <linux/slab.h> #include <linux/set_memory.h> #include <linux/execmem.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/cacheflush.h> #include <asm/sstep.h> #include <asm/sections.h>
arch/powerpc/kernel/module_32.c +1 -1
@@ -18,7 +18,7 @@#include <linux/bug.h> #include <linux/sort.h> #include <asm/setup.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> /* Count how many different relocations (different symbol, different addend) */
arch/powerpc/kernel/module_64.c +1 -1
@@ -17,7 +17,7 @@#include <linux/kernel.h> #include <asm/module.h> #include <asm/firmware.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <linux/sort.h> #include <asm/setup.h> #include <asm/sections.h>
arch/powerpc/kernel/optprobes.c +1 -1
@@ -13,7 +13,7 @@#include <asm/kprobes.h> #include <asm/ptrace.h> #include <asm/cacheflush.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/sstep.h> #include <asm/ppc-opcode.h> #include <asm/inst.h>
arch/powerpc/kernel/process.c +1 -1
@@ -54,7 +54,7 @@#include <asm/firmware.h> #include <asm/hw_irq.h> #endif -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/exec.h> #include <asm/livepatch.h> #include <asm/cpu_has_feature.h>
arch/powerpc/kernel/security.c +1 -1
@@ -14,7 +14,7 @@#include <linux/debugfs.h> #include <asm/asm-prototypes.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/security_features.h> #include <asm/sections.h> #include <asm/setup.h>
arch/powerpc/kernel/setup_32.c +1 -1
@@ -40,7 +40,7 @@#include <asm/time.h> #include <asm/serial.h> #include <asm/udbg.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/cpu_has_feature.h> #include <asm/asm-prototypes.h> #include <asm/kdump.h>
arch/powerpc/kernel/setup_64.c +1 -1
@@ -60,7 +60,7 @@#include <asm/xmon.h> #include <asm/udbg.h> #include <asm/kexec.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/ftrace.h> #include <asm/opal.h> #include <asm/cputhreads.h>
arch/powerpc/kernel/static_call.c +1 -1
@@ -2,7 +2,7 @@#include <linux/memory.h> #include <linux/static_call.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> void arch_static_call_transform (void *site,void *tramp,void *func,bool tail) {
arch/powerpc/kernel/trace/ftrace.c +1 -1
@@ -23,7 +23,7 @@#include <linux/list.h> #include <asm/cacheflush.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/ftrace.h> #include <asm/syscall.h> #include <asm/inst.h>
arch/powerpc/kernel/trace/ftrace_64_pg.c +1 -1
@@ -23,7 +23,7 @@#include <linux/list.h> #include <asm/cacheflush.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/ftrace.h> #include <asm/syscall.h> #include <asm/inst.h>
arch/powerpc/lib/code-patching.c +1 -1
@@ -17,7 +17,7 @@#include <asm/tlb.h> #include <asm/tlbflush.h> #include <asm/page.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/inst.h> static int __patch_instruction (u32 *exec_addr,ppc_inst_t instr,u32 *patch_addr)
arch/powerpc/lib/feature-fixups.c +1 -1
@@ -16,7 +16,7 @@#include <linux/sched/mm.h> #include <linux/stop_machine.h> #include <asm/cputable.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/interrupt.h> #include <asm/page.h> #include <asm/sections.h>
arch/powerpc/lib/test-code-patching.c +1 -1
@@ -6,7 +6,7 @@#include <linux/vmalloc.h> #include <linux/init.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> static int __init instr_is_branch_to_addr (const u32 *instr,unsigned long addr) {
arch/powerpc/lib/test_emulate_step.c +1 -1
@@ -11,7 +11,7 @@#include <asm/cpu_has_feature.h> #include <asm/sstep.h> #include <asm/ppc-opcode.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/inst.h> #define MAX_SUBTESTS 16
arch/powerpc/mm/book3s32/mmu.c +1 -1
@@ -25,7 +25,7 @@#include <asm/mmu.h> #include <asm/machdep.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/sections.h> #include <mm/mmu_decl.h>
arch/powerpc/mm/book3s64/hash_utils.c +1 -1
@@ -57,7 +57,7 @@#include <asm/sections.h> #include <asm/copro.h> #include <asm/udbg.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/fadump.h> #include <asm/firmware.h> #include <asm/tm.h>
arch/powerpc/mm/book3s64/slb.c +1 -1
@@ -24,7 +24,7 @@#include <linux/pgtable.h> #include <asm/udbg.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include "internal.h"
arch/powerpc/mm/kasan/init_32.c +1 -1
@@ -7,7 +7,7 @@#include <linux/memblock.h> #include <linux/sched/task.h> #include <asm/pgalloc.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <mm/mmu_decl.h> static pgprot_t __init kasan_prot_ro (void )
arch/powerpc/mm/mem.c +1 -1
@@ -25,7 +25,7 @@#include <asm/svm.h> #include <asm/mmzone.h> #include <asm/ftrace.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/setup.h> #include <asm/fixmap.h>
arch/powerpc/mm/nohash/44x.c +1 -1
@@ -24,7 +24,7 @@#include <asm/mmu.h> #include <asm/page.h> #include <asm/cacheflush.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/smp.h> #include <mm/mmu_decl.h>
arch/powerpc/mm/nohash/book3e_pgtable.c +1 -1
@@ -10,7 +10,7 @@#include <asm/pgalloc.h> #include <asm/tlb.h> #include <asm/dma.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <mm/mmu_decl.h>
arch/powerpc/mm/nohash/tlb.c +1 -1
@@ -37,7 +37,7 @@#include <asm/pgalloc.h> #include <asm/tlbflush.h> #include <asm/tlb.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/cputhreads.h> #include <asm/hugetlb.h> #include <asm/paca.h>
arch/powerpc/net/bpf_jit_comp.c +1 -1
@@ -18,7 +18,7 @@#include <linux/bpf.h> #include <asm/kprobes.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include "bpf_jit.h"
arch/powerpc/perf/8xx-pmu.c +1 -1
@@ -14,7 +14,7 @@#include <asm/machdep.h> #include <asm/firmware.h> #include <asm/ptrace.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/inst.h> #define PERF_8xx_ID_CPU_CYCLES 1
arch/powerpc/perf/core-book3s.c +1 -1
@@ -16,7 +16,7 @@#include <asm/machdep.h> #include <asm/firmware.h> #include <asm/ptrace.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/hw_irq.h> #include <asm/interrupt.h>
arch/powerpc/platforms/85xx/smp.c +1 -1
@@ -23,7 +23,7 @@#include <asm/mpic.h> #include <asm/cacheflush.h> #include <asm/dbell.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/cputhreads.h> #include <asm/fsl_pm.h>
arch/powerpc/platforms/86xx/mpc86xx_smp.c +1 -1
@@ -12,7 +12,7 @@#include <linux/delay.h> #include <linux/pgtable.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/page.h> #include <asm/pci-bridge.h> #include <asm/mpic.h>
arch/powerpc/platforms/cell/smp.c +1 -1
@@ -35,7 +35,7 @@#include <asm/firmware.h> #include <asm/rtas.h> #include <asm/cputhreads.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include "interrupt.h" #include <asm/udbg.h>
arch/powerpc/platforms/powermac/smp.c +1 -1
@@ -35,7 +35,7 @@#include <asm/ptrace.h> #include <linux/atomic.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/irq.h> #include <asm/page.h> #include <asm/sections.h>
arch/powerpc/platforms/powernv/idle.c +1 -1
@@ -18,7 +18,7 @@#include <asm/opal.h> #include <asm/cputhreads.h> #include <asm/cpuidle.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/smp.h> #include <asm/runlatch.h> #include <asm/dbell.h>
arch/powerpc/platforms/powernv/smp.c +1 -1
@@ -28,7 +28,7 @@#include <asm/xive.h> #include <asm/opal.h> #include <asm/runlatch.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/dbell.h> #include <asm/kvm_ppc.h> #include <asm/ppc-opcode.h>
arch/powerpc/platforms/pseries/smp.c +1 -1
@@ -39,7 +39,7 @@#include <asm/xive.h> #include <asm/dbell.h> #include <asm/plpar_wrappers.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/svm.h> #include <asm/kvm_guest.h>
arch/powerpc/xmon/xmon.c +1 -1
@@ -50,7 +50,7 @@#include <asm/xive.h> #include <asm/opal.h> #include <asm/firmware.h> -#include <asm/code-patching.h> +#include <asm/text-patching.h> #include <asm/sections.h> #include <asm/inst.h> #include <asm/interrupt.h>
arch/riscv/errata/andes/errata.c +1 -1
@@ -13,7 +13,7 @@#include <asm/alternative.h> #include <asm/cacheflush.h> #include <asm/errata_list.h> -#include <asm/patch.h> +#include <asm/text-patching.h> #include <asm/processor.h> #include <asm/sbi.h> #include <asm/vendorid_list.h>
arch/riscv/errata/sifive/errata.c +1 -1
@@ -8,7 +8,7 @@#include <linux/module.h> #include <linux/string.h> #include <linux/bug.h> -#include <asm/patch.h> +#include <asm/text-patching.h> #include <asm/alternative.h> #include <asm/vendorid_list.h> #include <asm/errata_list.h>
arch/riscv/errata/thead/errata.c +1 -1
@@ -16,7 +16,7 @@#include <asm/errata_list.h> #include <asm/hwprobe.h> #include <asm/io.h> -#include <asm/patch.h> +#include <asm/text-patching.h> #include <asm/vendorid_list.h> static bool errata_probe_pbmt(unsigned int stage,
arch/riscv/include/asm/text-patching.h renamed from arch/riscv/include/asm/patch.h +0 -0
arch/riscv/include/asm/uprobes.h +1 -1
@@ -4,7 +4,7 @@#define _ASM_RISCV_UPROBES_H#include <asm/probes.h> -#include <asm/patch.h> +#include <asm/text-patching.h> #include <asm/bug.h> #define MAX_UINSN_BYTES 8
arch/riscv/kernel/alternative.c +1 -1
@@ -18,7 +18,7 @@#include <asm/sbi.h> #include <asm/csr.h> #include <asm/insn.h> -#include <asm/patch.h> +#include <asm/text-patching.h> struct cpu_manufacturer_info_t {unsigned long vendor_id;
arch/riscv/kernel/cpufeature.c +2 -1
@@ -20,7 +20,8 @@#include <asm/cacheflush.h> #include <asm/cpufeature.h> #include <asm/hwcap.h> -#include <asm/patch.h> +#include <asm/text-patching.h> +#include <asm/hwprobe.h> #include <asm/processor.h> #include <asm/sbi.h> #include <asm/vector.h>
arch/riscv/kernel/ftrace.c +1 -1
@@ -9,7 +9,7 @@#include <linux/uaccess.h> #include <linux/memory.h> #include <asm/cacheflush.h> -#include <asm/patch.h> +#include <asm/text-patching.h> #ifdef CONFIG_DYNAMIC_FTRACE void ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex)
arch/riscv/kernel/jump_label.c +1 -1
@@ -9,7 +9,7 @@#include <linux/memory.h> #include <linux/mutex.h> #include <asm/bug.h> -#include <asm/patch.h> +#include <asm/text-patching.h> #define RISCV_INSN_NOP 0x00000013U#define RISCV_INSN_JAL 0x0000006fU
arch/riscv/kernel/patch.c +1 -1
@@ -13,7 +13,7 @@#include <asm/cacheflush.h> #include <asm/fixmap.h> #include <asm/ftrace.h> -#include <asm/patch.h> +#include <asm/text-patching.h> #include <asm/sections.h> struct patch_insn {
arch/riscv/kernel/probes/kprobes.c +1 -1
@@ -11,7 +11,7 @@#include <asm/sections.h> #include <asm/cacheflush.h> #include <asm/bug.h> -#include <asm/patch.h> +#include <asm/text-patching.h> #include "decode-insn.h"
arch/riscv/net/bpf_jit_comp64.c +1 -1
@@ -10,7 +10,7 @@#include <linux/filter.h> #include <linux/memory.h> #include <linux/stop_machine.h> -#include <asm/patch.h> +#include <asm/text-patching.h> #include <asm/cfi.h> #include "bpf_jit.h"
arch/riscv/net/bpf_jit_core.c +1 -1
@@ -9,7 +9,7 @@#include <linux/bpf.h> #include <linux/filter.h> #include <linux/memory.h> -#include <asm/patch.h> +#include <asm/text-patching.h> #include <asm/cfi.h> #include "bpf_jit.h"
arch/sh/include/asm/Kbuild +1 -0
@@ -3,3 +3,4 @@ generated-y += syscall_table.h generic-y += kvm_para.h generic-y += mcs_spinlock.h generic-y += parport.h +generic-y += text-patching.h
arch/sparc/include/asm/Kbuild +1 -0
@@ -4,3 +4,4 @@ generated-y += syscall_table_64.h generic-y += agp.h generic-y += kvm_para.h generic-y += mcs_spinlock.h +generic-y += text-patching.h
arch/um/kernel/um_arch.c +5 -0
@@ -470,6 +470,11 @@ void *text_poke(void *addr, const void *opcode, size_t len)return memcpy (addr, opcode, len); } +void *text_poke_copy (void *addr,const void *opcode,size_t len) +{ +return text_poke (addr, opcode, len); +} +void text_poke_sync (void ) { }
arch/x86/include/asm/text-patching.h +1 -0
@@ -35,6 +35,7 @@ extern void *text_poke(void *addr, const void *opcode, size_t len);extern void text_poke_sync (void );extern void *text_poke_kgdb (void *addr,const void *opcode,size_t len);extern void *text_poke_copy (void *addr,const void *opcode,size_t len); +#define text_poke_copy text_poke_copyextern void *text_poke_copy_locked (void *addr,const void *opcode,size_t len,bool core_ok);extern void *text_poke_set (void *addr,int c,size_t len);extern int poke_int3_handler (struct pt_regs *regs);
arch/xtensa/include/asm/Kbuild +1 -0
@@ -8,3 +8,4 @@ generic-y += parport.h generic-y += qrwlock.h generic-y += qspinlock.h generic-y += user.h +generic-y += text-patching.h
include/asm-generic/text-patching.h new file +5 -0
@@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_GENERIC_TEXT_PATCHING_H +#define _ASM_GENERIC_TEXT_PATCHING_H + +#endif /* _ASM_GENERIC_TEXT_PATCHING_H */
include/linux/text-patching.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_TEXT_PATCHING_H +#define _LINUX_TEXT_PATCHING_H + +#include <asm/text-patching.h> + +#ifndef text_poke_copy +static inline void *text_poke_copy (void *dst,const void *src,size_t len) +{ +return memcpy (dst, src, len); +} +#define text_poke_copy text_poke_copy +#endif + +#endif /* _LINUX_TEXT_PATCHING_H */
Signature · 1 line
2.43.0
Mike Rapoport
maintainerRFC PATCH 2/7 mm: vmalloc: don't account for number of nodes for HUGE_VMAP allocations
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
vmalloc allocations with VM_ALLOW_HUGE_VMAP that do not explictly specify node ID will use huge pages only if size_per_node is larger than PMD_SIZE.
Still the actual allocated memory is not distributed between nodes and there is no advantage in such approach.
On the contrary, BPF allocates PMD_SIZE * num_possible_nodes() for each new bpf_prog_pack, while it could do with PMD_SIZE'ed packs.
Don't account for number of nodes for VM_ALLOW_HUGE_VMAP with NUMA_NO_NODE and use huge pages whenever the requested allocation size is larger than PMD_SIZE.
--- mm/vmalloc.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
mm/vmalloc.c +2 -7
@@ -3737,8 +3737,6 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align, }if (vmap_allow_huge && (vm_flags &VM_ALLOW_HUGE_VMAP )) { -unsigned long size_per_node; -/* * Try huge pages. Only try for PAGE_KERNEL allocations, * others like modules don't yet expect huge pages in
@@ -3746,13 +3744,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,* supporting them. */ - size_per_node = size; -if (node ==NUMA_NO_NODE ) - size_per_node /=num_online_nodes (); -if (arch_vmap_pmd_supported (prot) && size_per_node >=PMD_SIZE ) +if (arch_vmap_pmd_supported (prot) && size >=PMD_SIZE ) shift =PMD_SHIFT ;else - shift =arch_vmap_pte_supported_shift (size_per_node); + shift =arch_vmap_pte_supported_shift (size); align =max (real_align,1UL << shift); size =ALIGN (real_size,1UL << shift);
Signature · 1 line
2.43.0
Mike Rapoport
maintainerRFC PATCH 3/7 module: prepare to handle ROX allocations for text
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
In order to support ROX allocations for module text, it is necessary to handle modifications to the code, such as relocations and alternatives patching, without write access to that memory.
One option is to use text patching, but this would make module loading extremely slow and will expose executable code that is not finally formed.
A better way is to have memory allocated with ROX permissions contain invalid instructions and keep a writable, but not executable copy of the module text. The relocations and alternative patches would be done on the writable copy using the addresses of the ROX memory.
Once the module is completely ready, the updated text will be copied to ROX memory using text patching in one go and the writable copy will be freed.
Add support for that to module initialization code and provide necessary interfaces in execmem.
--- include/linux/execmem.h | 23 +++++++++++++ include/linux/module.h | 11 ++++++ kernel/module/main.c | 70 ++++++++++++++++++++++++++++++++++---- kernel/module/strict_rwx.c | 3 ++ mm/execmem.c | 11 ++++++ 5 files changed, 111 insertions(+), 7 deletions(-)
include/linux/execmem.h +23 -0
@@ -46,9 +46,11 @@ enum execmem_type {/** * enum execmem_range_flags - options for executable memory allocations * @EXECMEM_KASAN_SHADOW: allocate kasan shadow +* @EXECMEM_READ_ONLY: allocated memory should be mapped as read only */ enum execmem_range_flags {EXECMEM_KASAN_SHADOW = (1 <<0 ), +EXECMEM_ROX_CACHE = (1 <<1 ), };/**
@@ -123,6 +125,27 @@ void *execmem_alloc(enum execmem_type type, size_t size);*/ void execmem_free (void *ptr); +/** +* execmem_update_copy - copy an update to executable memory +* @dst: destination address to update +* @src: source address containing the data +* @size: how many bytes of memory shold be copied +* +* Copy @size bytes from @src to @dst using text poking if the memory at +* @dst is read-only. +* +* Return: a pointer to @dst or NULL on error +*/ +void *execmem_update_copy (void *dst,const void *src,size_t size); + +/** +* execmem_is_rox - check if execmem is read-only +* @type - the execmem type to check +* +* Return: %true if the @type is read-only, %false if it's writable +*/ +bool execmem_is_rox (enum execmem_type type); +#ifdef CONFIG_ARCH_WANTS_EXECMEM_EARLY void execmem_early_init (void );#else
include/linux/module.h +11 -0
@@ -361,6 +361,8 @@ enum mod_mem_type {struct module_memory {void *base; +void *rw_copy; +bool is_rox;unsigned int size;#ifdef CONFIG_MODULES_TREE_LOOKUP
@@ -368,6 +370,15 @@ struct module_memory {#endif }; +#ifdef CONFIG_MODULES +unsigned long module_writable_offset (struct module *mod,void *loc); +#else +static inline unsigned long module_writable_offset (struct module *mod,void *loc) +{ +return 0 ; +} +#endif +#ifdef CONFIG_MODULES_TREE_LOOKUP /* Only touch one cacheline for common rbtree-for-core-layout case. */ #define __module_memory_align ____cacheline_aligned
kernel/module/main.c +63 -7
127 lines not on this page · raw message
kernel/module/strict_rwx.c +3 -0
@@ -34,6 +34,9 @@ int module_enable_text_rox(const struct module *mod) for_class_mod_mem_type(type, text) {int ret; +if (mod->mem[type].is_rox) +continue ; +if (IS_ENABLED (CONFIG_STRICT_MODULE_RWX )) ret =module_set_memory (mod, type, set_memory_rox);else
mm/execmem.c +11 -0
@@ -4,6 +4,7 @@#include <linux/vmalloc.h> #include <linux/execmem.h> #include <linux/moduleloader.h> +#include <linux/text-patching.h> static struct execmem_info *execmem_info__ro_after_init ;static struct execmem_info default_execmem_info__ro_after_init ;
@@ -63,6 +64,16 @@ void execmem_free(void *ptr)vfree (ptr); } +void *execmem_update_copy (void *dst,const void *src,size_t size) +{ +return text_poke_copy (dst, src, size); +} + +bool execmem_is_rox (enum execmem_type type) +{ +return !!(execmem_info->ranges[type].flags &EXECMEM_ROX_CACHE ); +} +static bool execmem_validate (struct execmem_info *info) {struct execmem_range *r = &info->ranges[EXECMEM_DEFAULT ];
Signature · 1 line
2.43.0
Mike Rapoport
maintainerRFC PATCH 4/7 ftrace: Add swap_func to ftrace_process_locs()
In reply to Mike RapoportFrom: Song Liu <[email protected]>
ftrace_process_locs sorts module mcount, which is inside RO memory. Add a ftrace_swap_func so that archs can use RO-memory-poke function to do the sorting.
--- include/linux/ftrace.h | 2 ++ kernel/trace/ftrace.c | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-)
include/linux/ftrace.h +2 -0
@@ -1172,4 +1172,6 @@ unsigned long arch_syscall_addr(int nr);#endif /* CONFIG_FTRACE_SYSCALLS */ +void ftrace_swap_func (void *a,void *b,int n); +#endif /* _LINUX_FTRACE_H */
kernel/trace/ftrace.c +12 -1
@@ -6480,6 +6480,17 @@ static void test_is_sorted(unsigned long *start, unsigned long count) }#endif +void __weak ftrace_swap_func (void *a,void *b,int n) +{ +unsigned long t; + +WARN_ON_ONCE (n !=sizeof (t)); + + t = *((unsigned long *)a); + *(unsigned long *)a = *(unsigned long *)b; + *(unsigned long *)b = t; +} +static int ftrace_process_locs (struct module *mod,unsigned long *start,unsigned long *end)
@@ -6507,7 +6518,7 @@ static int ftrace_process_locs(struct module *mod,*/ if (!IS_ENABLED (CONFIG_BUILDTIME_MCOUNT_SORT ) || mod) {sort (start, count,sizeof (*start), - ftrace_cmp_ips,NULL ); + ftrace_cmp_ips, ftrace_swap_func); }else {test_is_sorted (start, count); }
Signature · 1 line
2.43.0
Mike Rapoport
maintainerRFC PATCH 5/7 x86/module: perpare module loading for ROX allocations of text
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
When module text memory will be allocated with ROX permissions, the memory at the actual address where the module will live will contain invalid instructions and there will be a writable copy that contains the actual module code.
Update relocations and alternatives patching to deal with it.
--- arch/um/kernel/um_arch.c | 11 ++- arch/x86/entry/vdso/vma.c | 3 +- arch/x86/include/asm/alternative.h | 14 +-- arch/x86/kernel/alternative.c | 152 +++++++++++++++++------------ arch/x86/kernel/ftrace.c | 41 +++++--- arch/x86/kernel/module.c | 17 ++-- 6 files changed, 140 insertions(+), 98 deletions(-)
arch/um/kernel/um_arch.c +6 -5
@@ -437,24 +437,25 @@ void __init arch_cpu_finalize_init(void)os_check_bugs (); } -void apply_seal_endbr (s32 *start,s32 *end) +void apply_seal_endbr (s32 *start,s32 *end,struct module *mod) { } -void apply_retpolines (s32 *start,s32 *end) +void apply_retpolines (s32 *start,s32 *end,struct module *mod) { } -void apply_returns (s32 *start,s32 *end) +void apply_returns (s32 *start,s32 *end,struct module *mod) { }void apply_fineibt (s32 *start_retpoline,s32 *end_retpoline, -s32 *start_cfi,s32 *end_cfi) +s32 *start_cfi,s32 *end_cfi,struct module *mod) { } -void apply_alternatives (struct alt_instr *start,struct alt_instr *end) +void apply_alternatives (struct alt_instr *start,struct alt_instr *end, +struct module *mod) { }
arch/x86/entry/vdso/vma.c +2 -1
@@ -51,7 +51,8 @@ int __init init_vdso_image(const struct vdso_image *image)apply_alternatives ((struct alt_instr *)(image->data + image->alt), (struct alt_instr *)(image->data + image->alt + - image->alt_len)); + image->alt_len), +NULL );return 0 ; }
arch/x86/include/asm/alternative.h +7 -7
@@ -96,16 +96,16 @@ extern struct alt_instr __alt_instructions[], __alt_instructions_end[];* instructions were patched in already: */ extern int alternatives_patched; +struct module ;extern void alternative_instructions (void ); -extern void apply_alternatives (struct alt_instr *start,struct alt_instr *end); -extern void apply_retpolines (s32 *start,s32 *end); -extern void apply_returns (s32 *start,s32 *end); -extern void apply_seal_endbr (s32 *start,s32 *end); +extern void apply_alternatives (struct alt_instr *start,struct alt_instr *end, +struct module *mod); +extern void apply_retpolines (s32 *start,s32 *end,struct module *mod); +extern void apply_returns (s32 *start,s32 *end,struct module *mod); +extern void apply_seal_endbr (s32 *start,s32 *end,struct module *mod);extern void apply_fineibt (s32 *start_retpoline,s32 *end_retpoine, -s32 *start_cfi,s32 *end_cfi); - -struct module ; +s32 *start_cfi,s32 *end_cfi,struct module *mod);struct callthunk_sites {s32 *call_start, *call_end;
arch/x86/kernel/alternative.c +88 -64
481 lines not on this page · raw message
arch/x86/kernel/ftrace.c +27 -14
94 lines not on this page · raw message
arch/x86/kernel/module.c +10 -7
@@ -146,18 +146,21 @@ static int __write_relocate_add(Elf64_Shdr *sechdrs, }if (apply) { -if (memcmp (loc, &zero, size)) { +void *wr_loc = loc +module_writable_offset (me, loc); + +if (memcmp (wr_loc, &zero, size)) {pr_err ("x86/modules: Invalid relocation target, existing value is nonzero for type %d, loc %p, val %Lx\n" , (int )ELF64_R_TYPE (rel[i].r_info), loc, val);return -ENOEXEC ; } -write (loc, &val, size); +write (wr_loc, &val, size); }else {if (memcmp (loc, &val, size)) {pr_warn ("x86/modules: Invalid relocation target, existing value does not match expected value for type %d, loc %p, val %Lx\n" , (int )ELF64_R_TYPE (rel[i].r_info), loc, val);return -ENOEXEC ; } +/* FIXME: needs care for ROX module allocations */ write (loc, &zero, size); } }
@@ -265,20 +268,20 @@ int module_finalize(const Elf_Ehdr *hdr, csize = cfi->sh_size; } -apply_fineibt (rseg, rseg + rsize, cseg, cseg + csize); +apply_fineibt (rseg, rseg + rsize, cseg, cseg + csize, me); }if (retpolines) {void *rseg = (void *)retpolines->sh_addr; -apply_retpolines (rseg, rseg + retpolines->sh_size); +apply_retpolines (rseg, rseg + retpolines->sh_size, me); }if (returns) {void *rseg = (void *)returns->sh_addr; -apply_returns (rseg, rseg + returns->sh_size); +apply_returns (rseg, rseg + returns->sh_size, me); }if (alt) {/* patch .altinstructions */ void *aseg = (void *)alt->sh_addr; -apply_alternatives (aseg, aseg + alt->sh_size); +apply_alternatives (aseg, aseg + alt->sh_size, me); }if (calls || alt) {struct callthunk_sites cs = {};
@@ -297,7 +300,7 @@ int module_finalize(const Elf_Ehdr *hdr, }if (ibt_endbr) {void *iseg = (void *)ibt_endbr->sh_addr; -apply_seal_endbr (iseg, iseg + ibt_endbr->sh_size); +apply_seal_endbr (iseg, iseg + ibt_endbr->sh_size, me); }if (locks) {void *lseg = (void *)locks->sh_addr;
Signature · 1 line
2.43.0
Mike Rapoport
maintainerRFC PATCH 6/7 execmem: add support for cache of large ROX pages
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
Using large pages to map text areas reduces iTLB pressure and improves performance.
Extend execmem_alloc() with an ability to use PMD_SIZE'ed 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.
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.
The cache is enabled when an architecture sets EXECMEM_ROX_CACHE flag in definition of an execmem_range.
--- include/linux/execmem.h | 2 + mm/execmem.c | 267 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 262 insertions(+), 7 deletions(-)
include/linux/execmem.h +2 -0
@@ -77,12 +77,14 @@ struct execmem_range {/** * struct execmem_info - architecture parameters for code allocations +* @invalidate: set memory to contain invalid instructions * @ranges: array of parameter sets defining architecture specific * parameters for executable memory allocations. The ranges that are not * explicitly initialized by an architecture use parameters defined for * @EXECMEM_DEFAULT. */ struct execmem_info { +void (*invalidate)(void *ptr,size_t size,bool writable);struct execmem_range ranges[EXECMEM_TYPE_MAX ]; };
mm/execmem.c +260 -7
303 lines not on this page · raw message
Signature · 1 line
2.43.0
Mike Rapoport
maintainerRFC PATCH 7/7 x86/module: enable ROX caches for module text
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
Enable execmem's cache of PMD_SIZE'ed pages mapped as ROX for module text allocations.
--- arch/x86/mm/init.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-)
arch/x86/mm/init.c +25 -4
@@ -1102,9 +1102,23 @@ unsigned long arch_max_swapfile_size(void)#endif #ifdef CONFIG_EXECMEM +static void execmem_invalidate(void *ptr, size_t size, bool writeable) +{ + /* fill memory with INT3 instructions */ + if (writeable) + memset(ptr, 0xcc, size); + else + text_poke_set(ptr, 0xcc, size); +} + static struct execmem_info execmem_info __ro_after_init = { + .invalidate = execmem_invalidate, .ranges = { - [EXECMEM_DEFAULT] = { + [EXECMEM_MODULE_TEXT] = { + .flags = EXECMEM_KASAN_SHADOW | EXECMEM_ROX_CACHE, + .alignment = MODULE_ALIGN, + }, + [EXECMEM_KPROBES...EXECMEM_MODULE_DATA] = { .flags = EXECMEM_KASAN_SHADOW, .alignment = MODULE_ALIGN, },
@@ -1119,9 +1133,16 @@ struct execmem_info __init *execmem_arch_setup(void) offset =get_random_u32_inclusive (1 ,1024 ) *PAGE_SIZE ; start =MODULES_VADDR + offset; - execmem_info.ranges[EXECMEM_DEFAULT ].start = start; - execmem_info.ranges[EXECMEM_DEFAULT ].end =MODULES_END ; - execmem_info.ranges[EXECMEM_DEFAULT ].pgprot =PAGE_KERNEL ; + +for (int i =EXECMEM_MODULE_TEXT ; i <EXECMEM_TYPE_MAX ; i++) { +struct execmem_range *r = &execmem_info.ranges[i]; + + r->start = start; + r->end =MODULES_END ; + r->pgprot =PAGE_KERNEL ; + } + + execmem_info.ranges[EXECMEM_MODULE_TEXT ].pgprot =PAGE_KERNEL_ROX ;return &execmem_info; }
Signature · 1 line
2.43.0
Re: [RFC PATCH 2/7] mm: vmalloc: don't account for number of nodes for HUGE_VMAP allocations
In reply to Mike RapoportLe 11/04/2024 à 18:05, Mike Rapoport a écrit :
Quoted 10 lines
From: "Mike Rapoport (IBM)" <[email protected]>
vmalloc allocations with
VM_ALLOW_HUGE_VMAPthat do not explictly specify node ID will use huge pages only ifsize_per_nodeis larger thanPMD_SIZE.
Still the actual allocated memory is not distributed between nodes and there is no advantage in such approach.
On the contrary, BPF allocatesPMD_SIZE*num_possible_nodes()for each newbpf_prog_pack, while it could do with PMD_SIZE'ed packs.
Quoted 3 lines
Don't account for number of nodes for
VM_ALLOW_HUGE_VMAPwithNUMA_NO_NODEand use huge pages whenever the requested allocation size is larger thanPMD_SIZE.
Patch looks ok but message is confusing. We also use huge pages at PTE size, for instance 512k pages or 16k pages on powerpc 8xx, while PMD_SIZE is 4M.
Christophe
Quoted 35 lines
Signed-off-by Mike Rapoport (IBM) <[email protected]>--- mm/vmalloc.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)mm/vmalloc.c +2
-7@@ -3737,8 +3737,6 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align, }if (vmap_allow_huge && (vm_flags &VM_ALLOW_HUGE_VMAP )) { -unsigned long size_per_node; -/* * Try huge pages. Only try for PAGE_KERNEL allocations, * others like modules don't yet expect huge pages in @@ -3746,13 +3744,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,* supporting them. */ - size_per_node = size; -if (node ==NUMA_NO_NODE ) - size_per_node /=num_online_nodes (); -if (arch_vmap_pmd_supported (prot) && size_per_node >=PMD_SIZE ) +if (arch_vmap_pmd_supported (prot) && size >=PMD_SIZE ) shift =PMD_SHIFT ;else - shift =arch_vmap_pte_supported_shift (size_per_node); + shift =arch_vmap_pte_supported_shift (size); align =max (real_align,1UL << shift); size =ALIGN (real_size,1UL << shift);
Re: [RFC PATCH 5/7] x86/module: perpare module loading for ROX allocations of text
In reply to Mike Rapoportarch/x86/kernel/alternative.c static int patch_retpoline(void *addr, struct insn *insn, u8 *bytes) · line 742
Quoted 3 lines · Mike Rapoport
arch/x86/kernel/alternative.c +1
-0for (s = start; s < end; s++) {void *addr = (void *)s + *s; +void *wr_addr = addr +module_writable_offset (mod, addr);
So instead of repeating this pattern in a dozen of places, why not use a simpler method:
void *wr_addr = module_writable_address(mod, addr);
or so, since we have to pass 'addr' to the module code anyway.
The text patching code is pretty complex already.
Thanks,
Ingo
Mike Rapoport
maintainerRe: [RFC PATCH 2/7] mm: vmalloc: don't account for number of nodes for HUGE_VMAP allocations
In reply to Christophe LeroyQuoted 13 lines · Christophe Leroy
Le 11/04/2024 à 18:05, Mike Rapoport a écrit :
Quoted 10 lines
From: "Mike Rapoport (IBM)" <[email protected]>
vmalloc allocations with
VM_ALLOW_HUGE_VMAPthat do not explictly specify node ID will use huge pages only ifsize_per_nodeis larger thanPMD_SIZE.
Still the actual allocated memory is not distributed between nodes and there is no advantage in such approach.
On the contrary, BPF allocatesPMD_SIZE*num_possible_nodes()for each newbpf_prog_pack, while it could do with PMD_SIZE'ed packs.
Quoted 7 lines
Quoted 3 lines
Don't account for number of nodes for
VM_ALLOW_HUGE_VMAPwithNUMA_NO_NODEand use huge pages whenever the requested allocation size is larger thanPMD_SIZE.Patch looks ok but message is confusing. We also use huge pages at PTE size, for instance 512k pages or 16k pages on powerpc 8xx, while
PMD_SIZEis 4M.
Ok, I'll rephrase.
Quoted 37 lines
Christophe
Quoted 35 lines
Signed-off-by Mike Rapoport (IBM) <[email protected]>--- mm/vmalloc.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)mm/vmalloc.c +2
-7@@ -3737,8 +3737,6 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align, }if (vmap_allow_huge && (vm_flags &VM_ALLOW_HUGE_VMAP )) { -unsigned long size_per_node; -/* * Try huge pages. Only try for PAGE_KERNEL allocations, * others like modules don't yet expect huge pages in @@ -3746,13 +3744,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,* supporting them. */ - size_per_node = size; -if (node ==NUMA_NO_NODE ) - size_per_node /=num_online_nodes (); -if (arch_vmap_pmd_supported (prot) && size_per_node >=PMD_SIZE ) +if (arch_vmap_pmd_supported (prot) && size >=PMD_SIZE ) shift =PMD_SHIFT ;else - shift =arch_vmap_pte_supported_shift (size_per_node); + shift =arch_vmap_pte_supported_shift (size); align =max (real_align,1UL << shift); size =ALIGN (real_size,1UL << shift);
Signature · 2 lines
Sincerely yours, Mike.
Mike Rapoport
maintainerRe: [RFC PATCH 5/7] x86/module: perpare module loading for ROX allocations of text
In reply to Ingo MolnarQuoted 10 lines · Ingo Molnar
arch/x86/kernel/alternative.c static int patch_retpoline(void *addr, struct insn *insn, u8 *bytes) · line 742
Quoted 3 lines · Mike Rapoport
arch/x86/kernel/alternative.c +1
-0for (s = start; s < end; s++) {void *addr = (void *)s + *s; +void *wr_addr = addr +module_writable_offset (mod, addr);So instead of repeating this pattern in a dozen of places, why not use a simpler method:
Quoted 3 lines
void *wr_addr = module_writable_address(mod, addr);or so, since we have to pass 'addr' to the module code anyway.
Agree.
Quoted 5 lines
The text patching code is pretty complex already.
Thanks,
Ingo
Signature · 2 lines
Sincerely yours, Mike.
Peter Zijlstra
maintainerRe: [RFC PATCH 5/7] x86/module: perpare module loading for ROX allocations of text
In reply to Mike RapoportQuoted 12 lines · Mike Rapoport
arch/x86/kernel/alternative.c +1
-1@@ -504,17 +513,17 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,* patch if feature is *NOT* present. */ if (!boot_cpu_has (a->cpuid) == !(a->flags &ALT_FLAG_NOT )) { -optimize_nops_inplace (instr, a->instrlen); +optimize_nops_inplace (wr_instr, a->instrlen);continue ;
arch/x86/kernel/alternative.c void __init_or_module noinline apply_alternatives(struct alt_instr *start, · line 524
Quoted 10 lines
arch/x86/kernel/alternative.c +3
-3} -DPRINTK (ALT ,"feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x" , +DPRINTK (ALT ,"feat: %d*32+%d, old: (%px (%px) len: %d), repl: (%px (%px), len: %d) flags: 0x%x" , a->cpuid >>5 , a->cpuid &0x1f , - instr, instr, a->instrlen, - replacement, a->replacementlen, a->flags); + instr, wr_instr, a->instrlen, + replacement, wr_replacement, a->replacementlen, a->flags);
I think this, and
arch/x86/kernel/alternative.c void __init_or_module noinline apply_alternatives(struct alt_instr *start, · line 540
Quoted 12 lines
arch/x86/kernel/alternative.c +2
-2-memcpy (insn_buff, replacement, a->replacementlen); +memcpy (insn_buff, wr_replacement, a->replacementlen); insn_buff_sz = a->replacementlen;if (a->flags &ALT_FLAG_DIRECT_CALL ) {@@ -528,11 +537,11 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,apply_relocation (insn_buff, a->instrlen, instr, replacement, a->replacementlen); -DUMP_BYTES (ALT , instr, a->instrlen,"%px: old_insn: " , instr); +DUMP_BYTES (ALT , wr_instr, a->instrlen,"%px: old_insn: " , instr);
this, want to remain as is.
arch/x86/kernel/alternative.c void __init_or_module noinline apply_alternatives(struct alt_instr *start, · line 547
Quoted 8 lines
arch/x86/kernel/alternative.c +1
-1DUMP_BYTES (ALT , replacement, a->replacementlen,"%px: rpl_insn: " , replacement);DUMP_BYTES (ALT , insn_buff, insn_buff_sz,"%px: final_insn: " , instr); -text_poke_early (instr, insn_buff, insn_buff_sz); +text_poke_early (wr_instr, insn_buff, insn_buff_sz); }kasan_enable_current ();
The rationale being that we then print an address that can be correlated to the kernel image (provided one either kills kaslr or adjusts for it).
Peter Zijlstra
maintainerRe: [RFC PATCH 6/7] execmem: add support for cache of large ROX pages
In reply to Mike Rapoportmm/execmem.c line 66
Quoted 11 lines · Mike Rapoport
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.mm/execmem.c +7
-0+static void execmem_invalidate (void *ptr,size_t size,bool writable) +{ +if (execmem_info->invalidate) + execmem_info->invalidate (ptr, size, writable); +else +memset (ptr,0 , size); +}
+static void execmem_invalidate(void *ptr, size_t size, bool writeable) +{
+ /* fill memory with INT3 instructions */
+ if (writeable)
+ memset(ptr, 0xcc, size);
+ else
+ text_poke_set(ptr, 0xcc, size);
+}
Thing is, 0xcc (aka INT3_INSN_OPCODE) is not an invalid instruction. It raises #BP not #UD.
Mike Rapoport
maintainerRe: [RFC PATCH 6/7] execmem: add support for cache of large ROX pages
Quoted 23 lines · Peter Zijlstra
Quoted 11 lines · Mike Rapoport
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.mm/execmem.c +7
-0+static void execmem_invalidate (void *ptr,size_t size,bool writable) +{ +if (execmem_info->invalidate) + execmem_info->invalidate (ptr, size, writable); +else +memset (ptr,0 , size); +}(quoted diff) +8
-0+static void execmem_invalidate(void *ptr, size_t size, bool writeable) +{ + /* fill memory with INT3 instructions */ + if (writeable) + memset(ptr, 0xcc, size); + else + text_poke_set(ptr, 0xcc, size); +}
Quoted 2 lines
Thing is,
0xcc(akaINT3_INSN_OPCODE) is not an invalid instruction. It raises #BP not #UD.
Do you mean that _invalidate is a poor name choice or that it's necessary to use an instruction that raises #UD?
Signature · 2 lines
Sincerely yours, Mike.
Mike Rapoport
maintainerRe: [RFC PATCH 5/7] x86/module: perpare module loading for ROX allocations of text
In reply to Peter ZijlstraQuoted 15 lines · Peter Zijlstra
Quoted 14 lines · Mike Rapoport
arch/x86/kernel/alternative.c +1
-1@@ -504,17 +513,17 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,* patch if feature is *NOT* present. */ if (!boot_cpu_has (a->cpuid) == !(a->flags &ALT_FLAG_NOT )) { -optimize_nops_inplace (instr, a->instrlen); +optimize_nops_inplace (wr_instr, a->instrlen);continue ; }
Quoted 10 lines
arch/x86/kernel/alternative.c void __init_or_module noinline apply_alternatives(struct alt_instr *start, · line 524
Quoted 8 lines
arch/x86/kernel/alternative.c +3
-3-DPRINTK (ALT ,"feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x" , +DPRINTK (ALT ,"feat: %d*32+%d, old: (%px (%px) len: %d), repl: (%px (%px), len: %d) flags: 0x%x" , a->cpuid >>5 , a->cpuid &0x1f , - instr, instr, a->instrlen, - replacement, a->replacementlen, a->flags); + instr, wr_instr, a->instrlen, + replacement, wr_replacement, a->replacementlen, a->flags);I think this, and
I've found printing both address handy when I debugged it, but no strong feelings here.
Quoted 8 lines
Quoted 8 lines
arch/x86/kernel/alternative.c +1
-1-memcpy (insn_buff, replacement, a->replacementlen); +memcpy (insn_buff, wr_replacement, a->replacementlen); insn_buff_sz = a->replacementlen;if (a->flags &ALT_FLAG_DIRECT_CALL ) {@@ -528,11 +537,11 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
Quoted 6 lines
arch/x86/kernel/alternative.c void __init_or_module noinline apply_alternatives(struct alt_instr *start, · line 540
Quoted 4 lines
arch/x86/kernel/alternative.c +1
-1apply_relocation (insn_buff, a->instrlen, instr, replacement, a->replacementlen); -DUMP_BYTES (ALT , instr, a->instrlen,"%px: old_insn: " , instr); +DUMP_BYTES (ALT , wr_instr, a->instrlen,"%px: old_insn: " , instr);this, want to remain as is.
here wr_instr is the buffer to dump:
DUMP_BYTES(type, buf, len, fmt, args...)
rather than an address, which remained 'instr'.
Quoted 11 lines
arch/x86/kernel/alternative.c void __init_or_module noinline apply_alternatives(struct alt_instr *start, · line 547
Quoted 8 lines
arch/x86/kernel/alternative.c +1
-1DUMP_BYTES (ALT , replacement, a->replacementlen,"%px: rpl_insn: " , replacement);DUMP_BYTES (ALT , insn_buff, insn_buff_sz,"%px: final_insn: " , instr); -text_poke_early (instr, insn_buff, insn_buff_sz); +text_poke_early (wr_instr, insn_buff, insn_buff_sz); }kasan_enable_current ();The rationale being that we then print an address that can be correlated to the kernel image (provided one either kills kaslr or adjusts for it).
Signature · 2 lines
Sincerely yours, Mike.
Peter Zijlstra
maintainerRe: [RFC PATCH 6/7] execmem: add support for cache of large ROX pages
In reply to Mike RapoportQuoted 24 lines · Mike Rapoport
Quoted 23 lines · Peter Zijlstra
Quoted 11 lines · Mike Rapoport
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.mm/execmem.c +7
-0+static void execmem_invalidate (void *ptr,size_t size,bool writable) +{ +if (execmem_info->invalidate) + execmem_info->invalidate (ptr, size, writable); +else +memset (ptr,0 , size); +}(quoted diff) +8
-0+static void execmem_invalidate(void *ptr, size_t size, bool writeable) +{ + /* fill memory with INT3 instructions */ + if (writeable) + memset(ptr, 0xcc, size); + else + text_poke_set(ptr, 0xcc, size); +}
Quoted 5 lines
Quoted 2 lines
Thing is,
0xcc(akaINT3_INSN_OPCODE) is not an invalid instruction. It raises #BP not #UD.Do you mean that
_invalidateis a poor name choice or that it's necessary to use an instruction that raises #UD?
Poor naming, mostly. #BP handler will still scream bloody murder if the site is otherwise unclaimed.
It just isn't an invalid instruction.
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [RFC PATCH 3/7] module: prepare to handle ROX allocations for text
In reply to Mike RapoportQuoted 18 lines · Mike Rapoport
kernel/module/main.c +9
-1@@ -2440,7 +2479,24 @@ static int post_relocation(struct module *mod, const struct load_info *info)add_kallsyms (mod, info);/* Arch-specific module finalizing. */ -return module_finalize (info->hdr, info->sechdrs, mod); + ret =module_finalize (info->hdr, info->sechdrs, mod); +if (ret) +return ret; + + for_each_mod_mem_type(type) { + struct module_memory *mem = &mod->mem[type]; + + if (mem->is_rox) { + if (!execmem_update_copy(mem->base, mem->rw_copy,
kernel/module/main.c static int post_relocation(struct module *mod, const struct load_info *info) · line 2500
Quoted 10 lines
kernel/module/main.c +9
-0+ mem->size)) +return -ENOMEM ; + +vfree (mem->rw_copy); + mem->rw_copy =NULL ; + } + } + +return 0 ; }
I might be missing something, but it seems a bit racy.
IIUC, module_finalize() calls alternatives_smp_module_add(). At this point, since you don’t hold the text_mutex, some might do text_poke(), e.g., by enabling/disabling static-key, and the update would be overwritten. No?
Mike Rapoport
maintainerRe: [RFC PATCH 3/7] module: prepare to handle ROX allocations for text
Quoted 31 lines · Nadav Amit
Quoted 18 lines · Mike Rapoport
kernel/module/main.c +9
-1@@ -2440,7 +2479,24 @@ static int post_relocation(struct module *mod, const struct load_info *info)add_kallsyms (mod, info);/* Arch-specific module finalizing. */ -return module_finalize (info->hdr, info->sechdrs, mod); + ret =module_finalize (info->hdr, info->sechdrs, mod); +if (ret) +return ret; + + for_each_mod_mem_type(type) { + struct module_memory *mem = &mod->mem[type]; + + if (mem->is_rox) { + if (!execmem_update_copy(mem->base, mem->rw_copy,kernel/module/main.c static int post_relocation(struct module *mod, const struct load_info *info) · line 2500
Quoted 10 lines
kernel/module/main.c +9
-0+ mem->size)) +return -ENOMEM ; + +vfree (mem->rw_copy); + mem->rw_copy =NULL ; + } + } + +return 0 ; }I might be missing something, but it seems a bit racy.
Quoted 4 lines
IIUC,
module_finalize()callsalternatives_smp_module_add(). At this point, since you don’t hold thetext_mutex, some might dotext_poke(), e.g., by enabling/disabling static-key, and the update would be overwritten. No?
Right :(
Even worse, for UP case alternatives_smp_unlock() will "patch" still empty area.
So I'm thinking about calling alternatives_smp_module_add() from an additional callback after the execmem_update_copy().
Does it make sense to you?
Signature · 2 lines
Sincerely yours, Mike.
Mike Rapoport
maintainerRe: [RFC PATCH 6/7] execmem: add support for cache of large ROX pages
In reply to Peter ZijlstraQuoted 31 lines · Peter Zijlstra
Quoted 24 lines · Mike Rapoport
Quoted 23 lines · Peter Zijlstra
Quoted 11 lines · Mike Rapoport
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.mm/execmem.c +7
-0+static void execmem_invalidate (void *ptr,size_t size,bool writable) +{ +if (execmem_info->invalidate) + execmem_info->invalidate (ptr, size, writable); +else +memset (ptr,0 , size); +}(quoted diff) +8
-0+static void execmem_invalidate(void *ptr, size_t size, bool writeable) +{ + /* fill memory with INT3 instructions */ + if (writeable) + memset(ptr, 0xcc, size); + else + text_poke_set(ptr, 0xcc, size); +}Quoted 5 lines
Quoted 2 lines
Thing is,
0xcc(akaINT3_INSN_OPCODE) is not an invalid instruction. It raises #BP not #UD.Do you mean that
_invalidateis a poor name choice or that it's necessary to use an instruction that raises #UD?
Quoted 4 lines
Poor naming, mostly. #BP handler will still scream bloody murder if the site is otherwise unclaimed.
It just isn't an invalid instruction.
Well, execmem_fill_with_insns_screaming_bloody_murder seems too long, how about execmem_fill_trapping_insns?
Signature · 2 lines
Sincerely yours, Mike.
Re: [RFC PATCH 3/7] module: [
In reply to Mike RapoportQuoted 15 lines · Mike Rapoport
Quoted 9 lines · Nadav Amit
I might be missing something, but it seems a bit racy.
IIUC,
module_finalize()callsalternatives_smp_module_add(). At this point, since you don’t hold thetext_mutex, some might dotext_poke(), e.g., by enabling/disabling static-key, and the update would be overwritten. No?
Quoted 8 lines
Right :(
Even worse, for UP casealternatives_smp_unlock()will "patch" still empty area.So I'm thinking about calling
alternatives_smp_module_add()from an additional callback after theexecmem_update_copy().Does it make sense to you?
Going over the code again - I might have just been wrong: I confused the alternatives and the jump-label mechanisms (as they do share a lot of code and characteristics).
The jump-labels are updated when prepare_coming_module() is called, which happens after post_relocation() [which means they would be updated using text_poke() “inefficiently” but should be safe].
The “alternatives” appear only to use text_poke() (in contrast for text_poke_early()) from very specific few flows, e.g., common_cpu_up() -> alternatives_enable_smp().
Are those flows pose a problem after boot?
Anyhow, sorry for the noise.
Mike Rapoport
maintainerRe: [RFC PATCH 3/7] module: [
Quoted 31 lines · Nadav Amit
Quoted 14 lines · Mike Rapoport
Quoted 9 lines · Nadav Amit
I might be missing something, but it seems a bit racy.
IIUC,
module_finalize()callsalternatives_smp_module_add(). At this point, since you don’t hold thetext_mutex, some might dotext_poke(), e.g., by enabling/disabling static-key, and the update would be overwritten. No?Quoted 8 lines
Right :(
Even worse, for UP casealternatives_smp_unlock()will "patch" still empty area.So I'm thinking about calling
alternatives_smp_module_add()from an additional callback after theexecmem_update_copy().Does it make sense to you?
Going over the code again - I might have just been wrong: I confused the alternatives and the jump-label mechanisms (as they do share a lot of code and characteristics).
The jump-labels are updated when
prepare_coming_module()is called, which happens afterpost_relocation()[which means they would be updated usingtext_poke()“inefficiently” but should be safe].
Quoted 5 lines
The “alternatives” appear only to use
text_poke()(in contrast fortext_poke_early()) from very specific few flows, e.g.,common_cpu_up()->alternatives_enable_smp().Are those flows pose a problem after boot?
Yes, common_cpu_up is called on CPU hotplug, so it's possible to have a race between alternatives_smp_module_add() and common_cpu_up() -> alternatives_enable_smp().
And in UP case alternatives_smp_module_add() will call alternatives_smp_unlock() that will patch module text before it is updated.
Quoted 1 line
Anyhow, sorry for the noise.
On the contrary, I would have missed it.
Signature · 2 lines
Sincerely yours, Mike.