Optimize jump label implementation on ARM64
Subsystems (2)
cover Optimize jump label implementation on ARM64
From: Jiang Liu <[email protected]>
This patchset tries to optimize arch specfic jump label implementation for ARM64 by dynamic kernel text patching.
To enable this feature, your toolchain must support "asm goto" extension and "%c" constraint extesion. Current GCC for AARCH64 doesn't support "%c", so you need a GCC patch similiar to this:
http://gcc.gnu.org/viewcvs/gcc/trunk/gcc/config/arm/arm.c?view=patch&r1=175293&r2=175565&pathrev=175565
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48637
It has been tested on ARM Fast mode, but not on real hardware yet.
Any comments are welcomed!
Jiang Liu (7):
arm64: introduce basic aarch64 instruction decoding helpers
arm64: introduce interfaces to hotpatch kernel and module code
arm64: move encode_insn_immediate() from module.c to insn.c
arm64: introduce aarch64_insn_gen_{nop|branch_imm}() helper functions arm64, jump label: detect %c support for ARM64
arm64, jump label: optimize jump label implementation
jump_label: use defined macros instead of hard-coding for better
readability
arch/arm64/Kconfig | 1 + arch/arm64/include/asm/insn.h | 75 ++++++++++++ arch/arm64/include/asm/jump_label.h | 52 ++++++++ arch/arm64/kernel/Makefile | 3 +- arch/arm64/kernel/insn.c | 238 ++++++++++++++++++++++++++++++++++++ arch/arm64/kernel/jump_label.c | 60 +++++++++ arch/arm64/kernel/module.c | 151 +++++------------------ include/linux/jump_label.h | 3 +- scripts/gcc-goto.sh | 2 +- 9 files changed, 463 insertions(+), 122 deletions(-) create mode 100644 arch/arm64/include/asm/insn.h create mode 100644 arch/arm64/include/asm/jump_label.h create mode 100644 arch/arm64/kernel/insn.c create mode 100644 arch/arm64/kernel/jump_label.c
Signature · 1 line
1.8.1.2
PATCH 1/7 arm64: introduce basic aarch64 instruction decoding helpers
From: Jiang Liu <[email protected]>
Introduce basic aarch64 instruction decoding helper aarch64_get_insn_class() and aarch64_insn_hotpatch_safe().
--- arch/arm64/include/asm/insn.h | 53 ++++++++++++++++++++++++++++++++ arch/arm64/kernel/Makefile | 2 +- arch/arm64/kernel/insn.c | 71 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/include/asm/insn.h create mode 100644 arch/arm64/kernel/insn.c
arch/arm64/include/asm/insn.h new file +53 -0
@@ -0,0 +1,53 @@ +/* +* Copyright (C) 2013 Huawei Ltd. +* Author: Jiang Liu <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ +#ifndef _ASM_ARM64_INSN_H +#define _ASM_ARM64_INSN_H +#include <linux/types.h> + +enum aarch64_insn_class { +AARCH64_INSN_CLS_UNKNOWN ,/* UNALLOCATED */ +AARCH64_INSN_CLS_DP_IMM ,/* Data processing - immediate */ +AARCH64_INSN_CLS_DP_REG ,/* Data processing - register */ +AARCH64_INSN_CLS_DP_FPSIMD ,/* Data processing - SIMD and FP */ +AARCH64_INSN_CLS_LDST ,/* Loads and stores */ +AARCH64_INSN_CLS_BR_SYS ,/* Branch, exception generation and +* system instructions */ +}; + +#define __AARCH64_INSN_FUNCS (abbr, mask, val) \ +static __always_inline bool aarch64_insn_is_##abbr(u32 code) \ +{ return (code & (mask)) == (val); } \ +static __always_inline u32 aarch64_insn_get_##abbr##_mask(void) \ +{ return (mask); } \ +static __always_inline u32 aarch64_insn_get_##abbr##_value(void) \ +{ return (val); } + +__AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000) +__AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000) +__AARCH64_INSN_FUNCS(svc, 0xFFE0001F, 0xD4000001) +__AARCH64_INSN_FUNCS(hvc, 0xFFE0001F, 0xD4000002) +__AARCH64_INSN_FUNCS(smc, 0xFFE0001F, 0xD4000003) +__AARCH64_INSN_FUNCS(brk, 0xFFE0001F, 0xD4200000) +__AARCH64_INSN_FUNCS(nop, 0xFFFFFFFF, 0xD503201F) + +#undef __AARCH64_INSN_FUNCS + +enum aarch64_insn_class aarch64_get_insn_class (u32 insn); + +bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn); + +#endif /* _ASM_ARM64_INSN_H */
arch/arm64/kernel/Makefile +1 -1
@@ -9,7 +9,7 @@ AFLAGS_head.o := -DTEXT_OFFSET=$(TEXT_OFFSET) arm64-obj-y := cputable.o debug-monitors.o entry.o irq.o fpsimd.o \ entry-fpsimd.o process.o ptrace.o setup.o signal.o \ sys.o stacktrace.o time.o traps.o io.o vdso.o \ - hyp-stub.o psci.o + hyp-stub.o psci.o insn.o arm64-obj-$(CONFIG_COMPAT) += sys32.o kuser32.o signal32.o \ sys_compat.o
arch/arm64/kernel/insn.c new file +71 -0
71 lines not on this page · raw message
Signature · 1 line
1.8.1.2
PATCH 2/7 arm64: introduce interfaces to hotpatch kernel and module code
In reply to Jiang LiuFrom: Jiang Liu <[email protected]>
Introduce aarch64_insn_patch_text() and __aarch64_insn_patch_text() to patch kernel and module code.
Function aarch64_insn_patch_text() is a heavy version which may use stop_machine() to serialize all online CPUs, and function __aarch64_insn_patch_text() is light version without explicitly serialization.
--- arch/arm64/include/asm/insn.h | 2 ++ arch/arm64/kernel/insn.c | 64 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+)
arch/arm64/include/asm/insn.h +2 -0
@@ -49,5 +49,7 @@ __AARCH64_INSN_FUNCS(nop, 0xFFFFFFFF, 0xD503201F)enum aarch64_insn_class aarch64_get_insn_class (u32 insn);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn); +int aarch64_insn_patch_text (void *addr,u32 *insns,int cnt); +int __aarch64_insn_patch_text (void *addr,u32 *insns,int cnt);#endif /* _ASM_ARM64_INSN_H */
arch/arm64/kernel/insn.c +64 -0
73 lines not on this page · raw message
Signature · 1 line
1.8.1.2
PATCH 3/7 arm64: move encode_insn_immediate() from module.c to insn.c
In reply to Jiang LiuFrom: Jiang Liu <[email protected]>
Function encode_insn_immediate() will be used by other instruction manipulate related functions, so move it into insn.c and rename it as aarch64_insn_encode_immediate().
--- arch/arm64/include/asm/insn.h | 14 ++++ arch/arm64/kernel/insn.c | 77 +++++++++++++++++++++ arch/arm64/kernel/module.c | 151 +++++++++--------------------------------- 3 files changed, 123 insertions(+), 119 deletions(-)
arch/arm64/include/asm/insn.h +14 -0
@@ -28,6 +28,18 @@ enum aarch64_insn_class {* system instructions */ }; +enum aarch64_insn_imm_type { +AARCH64_INSN_IMM_MOVNZ , +AARCH64_INSN_IMM_MOVK , +AARCH64_INSN_IMM_ADR , +AARCH64_INSN_IMM_26 , +AARCH64_INSN_IMM_19 , +AARCH64_INSN_IMM_16 , +AARCH64_INSN_IMM_14 , +AARCH64_INSN_IMM_12 , +AARCH64_INSN_IMM_9 , +}; +#define __AARCH64_INSN_FUNCS (abbr, mask, val) \ static __always_inline bool aarch64_insn_is_##abbr(u32 code) \ { return (code & (mask)) == (val); } \
@@ -47,6 +59,8 @@ __AARCH64_INSN_FUNCS(nop, 0xFFFFFFFF, 0xD503201F)#undef __AARCH64_INSN_FUNCSenum aarch64_insn_class aarch64_get_insn_class (u32 insn); +u32 aarch64_insn_encode_immediate (enum aarch64_insn_imm_type type, +u32 insn,u64 imm);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn);int aarch64_insn_patch_text (void *addr,u32 *insns,int cnt);
arch/arm64/kernel/insn.c +77 -0
80 lines not on this page · raw message
arch/arm64/kernel/module.c +32 -119
289 lines not on this page · raw message
Signature · 1 line
1.8.1.2
PATCH 4/7 arm64: introduce aarch64_insn_gen_{nop|branch_imm}() helper functions
In reply to Jiang LiuFrom: Jiang Liu <[email protected]>
Introduce aarch64_insn_gen_{nop|branch_imm}() helper functions, which will be used to implement jump label on ARM64.
--- arch/arm64/include/asm/insn.h | 6 ++++++ arch/arm64/kernel/insn.c | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+)
arch/arm64/include/asm/insn.h +6 -0
@@ -61,6 +61,12 @@ __AARCH64_INSN_FUNCS(nop, 0xFFFFFFFF, 0xD503201F)enum aarch64_insn_class aarch64_get_insn_class (u32 insn);u32 aarch64_insn_encode_immediate (enum aarch64_insn_imm_type type,u32 insn,u64 imm); +u32 aarch64_insn_gen_branch_imm (unsigned long pc,unsigned long addr, +bool link); +static __always_inline u32 aarch64_insn_gen_nop(void) +{ +return aarch64_insn_get_nop_value (); +}bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn);int aarch64_insn_patch_text (void *addr,u32 *insns,int cnt);
arch/arm64/kernel/insn.c +26 -0
@@ -14,6 +14,7 @@* You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include <linux/bitops.h> #include <linux/kernel.h> #include <linux/stop_machine.h> #include <asm/cacheflush.h>
@@ -210,3 +211,28 @@ u32 aarch64_insn_encode_immediate(enum aarch64_insn_imm_type type,return insn; } + +u32 aarch64_insn_gen_branch_imm (unsigned long pc,unsigned long addr,bool link) +{ +u32 insn; +long offset; + +/* +* PC: A 64-bit Program Counter holding the address of the current +* instruction. +*/ +BUG_ON ((pc &0x3 ) || (addr &0x3 )); + offset = ((long )addr - (long )pc) >>2 ; +/* B/BR support [-128M, 128M) offset */ +if (offset >=BIT (25 ) ||abs (offset) >BIT (25 )) { +WARN_ON_ONCE (1 ); +return 0 ; + } + +if (link) + insn =aarch64_insn_get_bl_value (); +else + insn =aarch64_insn_get_b_value (); + +return aarch64_insn_encode_immediate (AARCH64_INSN_IMM_26 , insn, offset); +}
Signature · 1 line
1.8.1.2
PATCH 5/7 arm64, jump label: detect %c support for ARM64
In reply to Jiang LiuFrom: Jiang Liu <[email protected]>
As commit a9468f30b5eac6 "ARM: 7333/2: jump label: detect %c support for ARM", this patch detects the same thing for ARM64 because some ARM64 GCC versions have the same issue.
Some versions of ARM64 GCC which do support asm goto, do not support the %c specifier. Since we need the %c to support jump labels on ARM64, detect that too in the asm goto detection script to avoid build errors with these versions.
--- scripts/gcc-goto.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/gcc-goto.sh +1 -1
@@ -5,7 +5,7 @@ cat << "END" | $@ -x c - -c -o /dev/null >/dev/null 2>&1 && echo "y" int main(void) { -#ifdef __arm__ +#if defined(__arm__) || defined(__aarch64__) /* * Not related to asm goto, but used by jump label * and broken on some ARM GCC versions (see GCC Bug 48637).
Signature · 1 line
1.8.1.2
PATCH 6/7 arm64, jump label: optimize jump label implementation
In reply to Jiang LiuFrom: Jiang Liu <[email protected]>
Optimize jump label implementation for ARM64 by dynamically patching kernel text.
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/jump_label.h | 52 ++++++++++++++++++++++++++++++++ arch/arm64/kernel/Makefile | 1 + arch/arm64/kernel/jump_label.c | 60 +++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 arch/arm64/include/asm/jump_label.h create mode 100644 arch/arm64/kernel/jump_label.c
arch/arm64/Kconfig +1 -0
@@ -17,6 +17,7 @@ config ARM64 select GENERIC_SMP_IDLE_THREAD select GENERIC_TIME_VSYSCALL select HARDIRQS_SW_RESEND + select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_TRACEHOOK select HAVE_DEBUG_BUGVERBOSE select HAVE_DEBUG_KMEMLEAK
arch/arm64/include/asm/jump_label.h new file +52 -0
52 lines not on this page · raw message
arch/arm64/kernel/Makefile +1 -0
@@ -18,6 +18,7 @@ arm64-obj-$(CONFIG_SMP) += smp.o smp_spin_table.o smp_psci.o arm64-obj-$(CONFIG_HW_PERF_EVENTS) += perf_event.o arm64-obj-$(CONFIG_HAVE_HW_BREAKPOINT)+= hw_breakpoint.o arm64-obj-$(CONFIG_EARLY_PRINTK) += early_printk.o +arm64-obj-$(CONFIG_JUMP_LABEL) += jump_label.o obj-y += $(arm64-obj-y) vdso/ obj-m += $(arm64-obj-m)
arch/arm64/kernel/jump_label.c new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
1.8.1.2
PATCH 7/7 jump_label: use defined macros instead of hard-coding for better readability
In reply to Jiang LiuFrom: Jiang Liu <[email protected]>
Use macro JUMP_LABEL_TRUE_BRANCH instead of hard-coding for better readability.
--- include/linux/jump_label.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/jump_label.h +2 -1
@@ -116,7 +116,8 @@ extern void static_key_slow_dec(struct static_key *key);extern void jump_label_apply_nops (struct module *mod);#define STATIC_KEY_INIT_TRUE ((struct static_key) \ - { .enabled = ATOMIC_INIT(1), .entries = (void *)1 }) + { .enabled = ATOMIC_INIT(1), \ + .entries = (void *)JUMP_LABEL_TRUE_BRANCH })#define STATIC_KEY_INIT_FALSE ((struct static_key) \ { .enabled = ATOMIC_INIT(0), .entries = (void *)0 })
Signature · 1 line
1.8.1.2
Re: [PATCH v1 1/7] arm64: introduce basic aarch64 instruction decoding helpers
In reply to Jiang LiuHi Jiang,
On 09/25/2013 06:44 AM, Jiang Liu wrote:
[...]
Quoted 1 line
arch/arm64/kernel/insn.c +0
-0
[...]
Quoted 8 lines
arch/arm64/kernel/insn.c +8
-0+static int aarch64_insn_cls[] = { +AARCH64_INSN_CLS_UNKNOWN ,/* 0 0 X X */ +AARCH64_INSN_CLS_UNKNOWN ,/* 0 0 X X */ +AARCH64_INSN_CLS_UNKNOWN ,/* 0 0 X X */ +AARCH64_INSN_CLS_UNKNOWN ,/* 0 0 X X */ +AARCH64_INSN_CLS_LDST ,/* X 1 X 0 */ +AARCH64_INSN_CLS_DP_REG ,/* X 1 0 1 */ +AARCH64_INSN_CLS_LDST ,/* X 1 X 0 */
arch/arm64/kernel/insn.c line 37
Quoted 10 lines
arch/arm64/kernel/insn.c +10
-0+AARCH64_INSN_CLS_DP_FPSIMD ,/* 0 1 1 1 */ +AARCH64_INSN_CLS_DP_IMM ,/* 1 0 0 X */ +AARCH64_INSN_CLS_DP_IMM ,/* 1 0 0 X */ +AARCH64_INSN_CLS_BR_SYS ,/* 1 0 1 X */ +AARCH64_INSN_CLS_BR_SYS ,/* 1 0 1 X */ +AARCH64_INSN_CLS_LDST ,/* X 1 X 0 */ +AARCH64_INSN_CLS_DP_REG ,/* X 1 0 1 */ +AARCH64_INSN_CLS_LDST ,/* X 1 X 0 */ + AARCH64_INSN_CLS_DP_FPSIMD, /* 1 1 1 1 */ +};
As I read this, I was initially puzzled as to why there are duplicate entries in the list. Since the data structure doesn't really support don't-cares, perhaps it would be clearer to use 1's and 0's instead of X's.
[...]
Thanks,
Christopher
Signature · 3 lines
Employee of Qualcomm Innovation Center, Inc. Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by the Linux Foundation.
Steven Rostedt
reviewerRe: [PATCH v1 2/7] arm64: introduce interfaces to hotpatch kernel and module code
In reply to Jiang LiuQuoted 22 lines · On Wed, 25 Sep 2013 18:44:22 +0800 Jiang Liu <[email protected]> wrote:
arch/arm64/kernel/insn.c +7
-0@@ -15,6 +15,8 @@* along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <linux/kernel.h> +#include <linux/stop_machine.h> +#include <asm/cacheflush.h> #include <asm/insn.h> static int aarch64_insn_cls[] = {@@ -69,3 +71,65 @@ bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn)return __aarch64_insn_hotpatch_safe (old_insn) &&__aarch64_insn_hotpatch_safe (new_insn); } + +struct aarch64_insn_patch { +void *text_addr; +u32 *new_insns; +int insn_cnt;
arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn) · line 88
Quoted 10 lines
arch/arm64/kernel/insn.c +10
-0+}; + +int __kprobes __aarch64_insn_patch_text (void *addr,u32 *insns,int cnt) +{ +int i; +u32 *tp = addr; + +/* instructions must be word aligned */ +if (cnt <=0 || ((uintptr_t )addr &0x3 )) +return -EINVAL ;
On aarch64, are instructions always word aligned? If not, it should be safe for stop machine to modify non word aligned instructions, but this patch looks like it doesn't allow stop_machine() to do so.
-- Steve
Quoted 47 lines
arch/arm64/kernel/insn.c +47
-0+ +for (i =0 ; i < cnt; i++) + tp[i] = insns[i]; + +flush_icache_range ((uintptr_t )tp, (uintptr_t )tp + cnt *sizeof (u32)); + +return 0 ; +} + +static int __kprobes aarch64_insn_patch_text_cb (void *arg) +{ +struct aarch64_insn_patch *pp = arg; + +return __aarch64_insn_patch_text (pp->text_addr, pp->new_insns, + pp->insn_cnt); +} + +int __kprobes aarch64_insn_patch_text (void *addr,u32 *insns,int cnt) +{ +int ret; +bool safe = false; + +/* instructions must be word aligned */ +if (cnt <=0 || ((uintptr_t )addr &0x3 )) +return -EINVAL ; + +if (cnt ==1 ) + safe =aarch64_insn_hotpatch_safe (*(u32 *)addr, insns[0 ]); + +if (safe) { + ret =__aarch64_insn_patch_text (addr, insns, cnt); + }else { +struct aarch64_insn_patch patch = { + .text_addr = addr, + .new_insns = insns, + .insn_cnt = cnt, + }; + +/* +* Execute __aarch64_insn_patch_text() on every online CPU, +* which ensure serialization among all online CPUs. +*/ + ret =stop_machine (aarch64_insn_patch_text_cb, &patch,NULL ); + } + +return ret; +}
Re: [PATCH v1 2/7] arm64: introduce interfaces to hotpatch kernel and module code
In reply to Jiang LiuQuoted 9 lines · Jiang Liu
From: Jiang Liu <[email protected]>
Introduce
aarch64_insn_patch_text()and__aarch64_insn_patch_text()to patch kernel and module code.Function
aarch64_insn_patch_text()is a heavy version which may usestop_machine()to serialize all online CPUs, and function__aarch64_insn_patch_text()is light version without explicitly serialization.
Hi Jiang,
I have written kprobes support for aarch64, and need both the functionality (lightweight and stop_machine() versions).
I would like to rebase these API in kprobes, however slight changes would require in case of stop_machine version, which I explained below.
[Though kprobes cannot share Instruction encode support of jump labels as, decoding & simulation quite different for kprobes/uprobes and based around single stepping]
Quoted 50 lines
Signed-off-by Jiang Liu <[email protected]>Cc Jiang Liu <[email protected]>--- arch/arm64/include/asm/insn.h | 2 ++ arch/arm64/kernel/insn.c | 64 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+)arch/arm64/include/asm/insn.h +2
-0@@ -49,5 +49,7 @@ __AARCH64_INSN_FUNCS(nop, 0xFFFFFFFF, 0xD503201F)enum aarch64_insn_class aarch64_get_insn_class (u32 insn);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn); +int aarch64_insn_patch_text (void *addr,u32 *insns,int cnt); +int __aarch64_insn_patch_text (void *addr,u32 *insns,int cnt);#endif /* _ASM_ARM64_INSN_H */ arch/arm64/kernel/insn.c +15
-0@@ -15,6 +15,8 @@* along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <linux/kernel.h> +#include <linux/stop_machine.h> +#include <asm/cacheflush.h> #include <asm/insn.h> static int aarch64_insn_cls[] = {@@ -69,3 +71,65 @@ bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn)return __aarch64_insn_hotpatch_safe (old_insn) &&__aarch64_insn_hotpatch_safe (new_insn); } + +struct aarch64_insn_patch { +void *text_addr; +u32 *new_insns; +int insn_cnt; +}; + +int __kprobes __aarch64_insn_patch_text (void *addr,u32 *insns,int cnt) +{ +int i; +u32 *tp = addr; + +/* instructions must be word aligned */
arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn) · line 96
Quoted 10 lines
arch/arm64/kernel/insn.c +10
-0+if (cnt <=0 || ((uintptr_t )addr &0x3 )) +return -EINVAL ; + +for (i =0 ; i < cnt; i++) + tp[i] = insns[i]; + +flush_icache_range ((uintptr_t )tp, (uintptr_t )tp + cnt *sizeof (u32)); + +return 0 ; +}
Looks fine, but do you need to check for CPU big endian mode here? (I think swab32() needed if EL1 is in big-endian mode)
Quoted 14 lines
arch/arm64/kernel/insn.c +14
-0+ +static int __kprobes aarch64_insn_patch_text_cb (void *arg) +{ +struct aarch64_insn_patch *pp = arg; + +return __aarch64_insn_patch_text (pp->text_addr, pp->new_insns, + pp->insn_cnt); +} + +int __kprobes aarch64_insn_patch_text (void *addr,u32 *insns,int cnt) +{ +int ret; +bool safe = false; +
arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn) · line 120
Quoted 10 lines
arch/arm64/kernel/insn.c +10
-0+/* instructions must be word aligned */ +if (cnt <=0 || ((uintptr_t )addr &0x3 )) +return -EINVAL ; + +if (cnt ==1 ) + safe =aarch64_insn_hotpatch_safe (*(u32 *)addr, insns[0 ]); + +if (safe) { + ret =__aarch64_insn_patch_text (addr, insns, cnt); + }else {
Can you move the code below this into separate API that just apply patch with stop_machine? And then a wrapper function for jump label specific handling that checks for aarch64_insn_hotpatch_safe() ? Also, it will be good to move the patching code out of insn.c to patch.c (refer to arch/arm/kernel/patch.c).
Please refer to attached file (my current implementation) to make sense of exactly what kprobes would need (ignore the big-endian part for now). I think splitting the code should be straight-forward and we can avoid two different implementations. Please let me know if this can be done, I will rebase my patches above your next version.
Thanks,
Sandeepa
Quoted 18 lines
arch/arm64/kernel/insn.c +15
-1+struct aarch64_insn_patch patch = { + .text_addr = addr, + .new_insns = insns, + .insn_cnt = cnt, + }; + +/* +* Execute __aarch64_insn_patch_text() on every online CPU, +* which ensure serialization among all online CPUs. +*/ + ret =stop_machine (aarch64_insn_patch_text_cb, &patch,NULL ); + } + +return ret; +} --1.8.1.2
Quoted 5 lines
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch_text.c
Type: text/x-csrc
Size: 1969 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20130925/8ae687b6/attachment-0001.bin>
Re: [PATCH v1 2/7] arm64: introduce interfaces to hotpatch kernel and module code
In reply to Steven RostedtQuoted 30 lines · Steven Rostedt
Quoted 28 lines · On Wed, 25 Sep 2013 18:44:22 +0800 Jiang Liu <[email protected]> wrote:
arch/arm64/kernel/insn.c +13
-0@@ -15,6 +15,8 @@* along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <linux/kernel.h> +#include <linux/stop_machine.h> +#include <asm/cacheflush.h> #include <asm/insn.h> static int aarch64_insn_cls[] = {@@ -69,3 +71,65 @@ bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn)return __aarch64_insn_hotpatch_safe (old_insn) &&__aarch64_insn_hotpatch_safe (new_insn); } + +struct aarch64_insn_patch { +void *text_addr; +u32 *new_insns; +int insn_cnt; +}; + +int __kprobes __aarch64_insn_patch_text (void *addr,u32 *insns,int cnt) +{ +int i; +u32 *tp = addr;
Quoted 8 lines
arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn) · line 88
Quoted 4 lines
arch/arm64/kernel/insn.c +4
-0+ +/* instructions must be word aligned */ +if (cnt <=0 || ((uintptr_t )addr &0x3 )) +return -EINVAL ;On aarch64, are instructions always word aligned? If not, it should be safe for stop machine to modify non word aligned instructions, but this patch looks like it doesn't allow
stop_machine()to do so.
Steve,
Yes, aarch64 instructions must be word-aligned, else instruction fetch would generate Misaligned PC fault.
Thanks,
Sandeepa
Quoted 56 lines
-- Steve
Quoted 37 lines
arch/arm64/kernel/insn.c +37
-0+ +for (i =0 ; i < cnt; i++) + tp[i] = insns[i]; + +flush_icache_range ((uintptr_t )tp, (uintptr_t )tp + cnt *sizeof (u32)); + +return 0 ; +} + +static int __kprobes aarch64_insn_patch_text_cb (void *arg) +{ +struct aarch64_insn_patch *pp = arg; + +return __aarch64_insn_patch_text (pp->text_addr, pp->new_insns, + pp->insn_cnt); +} + +int __kprobes aarch64_insn_patch_text (void *addr,u32 *insns,int cnt) +{ +int ret; +bool safe = false; + +/* instructions must be word aligned */ +if (cnt <=0 || ((uintptr_t )addr &0x3 )) +return -EINVAL ; + +if (cnt ==1 ) + safe =aarch64_insn_hotpatch_safe (*(u32 *)addr, insns[0 ]); + +if (safe) { + ret =__aarch64_insn_patch_text (addr, insns, cnt); + }else { +struct aarch64_insn_patch patch = { + .text_addr = addr, + .new_insns = insns, + .insn_cnt = cnt, + };arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn) · line 135
Quoted 10 lines
arch/arm64/kernel/insn.c +10
-0+ +/* +* Execute __aarch64_insn_patch_text() on every online CPU, +* which ensure serialization among all online CPUs. +*/ + ret =stop_machine (aarch64_insn_patch_text_cb, &patch,NULL ); + } + +return ret; +}--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Steven Rostedt
reviewerRe: [PATCH v1 7/7] jump_label: use defined macros instead of hard-coding for better readability
In reply to Jiang LiuQuoted 16 lines · On Wed, 25 Sep 2013 18:44:27 +0800 Jiang Liu <[email protected]> wrote:
From: Jiang Liu <[email protected]>
Use macro
JUMP_LABEL_TRUE_BRANCHinstead of hard-coding for better readability.Signed-off-by Jiang Liu <[email protected]>Cc Jiang Liu <[email protected]>--- include/linux/jump_label.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)include/linux/jump_label.h +0
-0
include/linux/jump_label.h extern void static_key_slow_dec(struct static_key *key); · line 123
Quoted 9 lines
include/linux/jump_label.h +2
-1@@ -116,7 +116,8 @@ extern void static_key_slow_dec(struct static_key *key);extern void jump_label_apply_nops (struct module *mod);#define STATIC_KEY_INIT_TRUE ((struct static_key) \ - { .enabled = ATOMIC_INIT(1), .entries = (void *)1 }) + { .enabled = ATOMIC_INIT(1), \ + .entries = (void *)JUMP_LABEL_TRUE_BRANCH })#define STATIC_KEY_INIT_FALSE ((struct static_key) \ { .enabled = ATOMIC_INIT(0), .entries = (void *)0 })
Probably should add a comment that there is no "JUMP_LABEL_FALSE_BRANCH" or maybe we should add one?
-- Steve
Steven Rostedt
reviewerRe: [PATCH v1 2/7] arm64: introduce interfaces to hotpatch kernel and module code
In reply to Sandeepa PrabhuQuoted 8 lines · On Wed, 25 Sep 2013 20:12:17 +0530 Sandeepa Prabhu…
Quoted 3 lines
On aarch64, are instructions always word aligned? If not, it should be safe for stop machine to modify non word aligned instructions, but this patch looks like it doesn't allow
stop_machine()to do so.Steve,
Yes, aarch64 instructions must be word-aligned, else instruction fetch would generate Misaligned PC fault.
Thanks for clarifying, as IIUC, there's ARM architectures that allow for 2 and 4 byte instructions.
-- Steve
Re: [PATCH v1 7/7] jump_label: use defined macros instead of hard-coding for better readability
In reply to Steven RostedtQuoted 21 lines · Steven Rostedt
Quoted 18 lines · On Wed, 25 Sep 2013 18:44:27 +0800 Jiang Liu <[email protected]> wrote:
From: Jiang Liu <[email protected]>
Use macro
JUMP_LABEL_TRUE_BRANCHinstead of hard-coding for better readability.Signed-off-by Jiang Liu <[email protected]>Cc Jiang Liu <[email protected]>--- include/linux/jump_label.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)include/linux/jump_label.h +0
-0@@ -116,7 +116,8 @@ extern void static_key_slow_dec(struct static_key *key);extern void jump_label_apply_nops (struct module *mod);
Quoted 10 lines
include/linux/jump_label.h extern void static_key_slow_dec(struct static_key *key); · line 123
Quoted 7 lines
include/linux/jump_label.h +2
-1#define STATIC_KEY_INIT_TRUE ((struct static_key) \ - { .enabled = ATOMIC_INIT(1), .entries = (void *)1 }) + { .enabled = ATOMIC_INIT(1), \ + .entries = (void *)JUMP_LABEL_TRUE_BRANCH })#define STATIC_KEY_INIT_FALSE ((struct static_key) \ { .enabled = ATOMIC_INIT(0), .entries = (void *)0 })Probably should add a comment that there is no "
JUMP_LABEL_FALSE_BRANCH" or maybe we should add one?
Hi Steven,
How about
JUMP_LABEL_TYPE_MASK/JUMP_LABEL_TRUE_BRANCH/JUMP_LABEL_FALSE_BRANCH tuple? That should have the best readability.
Thanks!
Quoted 4 lines
-- Steve
Re: [PATCH v1 1/7] arm64: introduce basic aarch64 instruction decoding helpers
In reply to Christopher CovingtonQuoted 24 lines · Christopher Covington
Hi Jiang,
On 09/25/2013 06:44 AM, Jiang Liu wrote:
[...]
Quoted 1 line
arch/arm64/kernel/insn.c +0
-0[...]
Quoted 14 lines
arch/arm64/kernel/insn.c +14
-0+static int aarch64_insn_cls[] = { +AARCH64_INSN_CLS_UNKNOWN ,/* 0 0 X X */ +AARCH64_INSN_CLS_UNKNOWN ,/* 0 0 X X */ +AARCH64_INSN_CLS_UNKNOWN ,/* 0 0 X X */ +AARCH64_INSN_CLS_UNKNOWN ,/* 0 0 X X */ +AARCH64_INSN_CLS_LDST ,/* X 1 X 0 */ +AARCH64_INSN_CLS_DP_REG ,/* X 1 0 1 */ +AARCH64_INSN_CLS_LDST ,/* X 1 X 0 */ +AARCH64_INSN_CLS_DP_FPSIMD ,/* 0 1 1 1 */ +AARCH64_INSN_CLS_DP_IMM ,/* 1 0 0 X */ +AARCH64_INSN_CLS_DP_IMM ,/* 1 0 0 X */ +AARCH64_INSN_CLS_BR_SYS ,/* 1 0 1 X */ +AARCH64_INSN_CLS_BR_SYS ,/* 1 0 1 X */ +AARCH64_INSN_CLS_LDST ,/* X 1 X 0 */
Quoted 8 lines
arch/arm64/kernel/insn.c line 37
Quoted 4 lines
arch/arm64/kernel/insn.c +4
-0+AARCH64_INSN_CLS_DP_REG ,/* X 1 0 1 */ +AARCH64_INSN_CLS_LDST ,/* X 1 X 0 */ + AARCH64_INSN_CLS_DP_FPSIMD, /* 1 1 1 1 */ +};As I read this, I was initially puzzled as to why there are duplicate entries in the list. Since the data structure doesn't really support don't-cares, perhaps it would be clearer to use 1's and 0's instead of X's.
Thanks for review, Chris!
I will try to improve the comments in next version.
Quoted 6 lines
[...]
Thanks,
Christopher
Re: [PATCH v1 2/7] arm64: introduce interfaces to hotpatch kernel and module code
In reply to Sandeepa PrabhuHi Sandeepa,
Great to know that you are working on kprobe for ARM64.
I have done some investigation, found it's an huge work for me then gave up:(
I will try refine the implementation based on your requirement. Thanks!
Quoted 146 lines · Sandeepa Prabhu
Quoted 9 lines · Jiang Liu
From: Jiang Liu <[email protected]>
Introduce
aarch64_insn_patch_text()and__aarch64_insn_patch_text()to patch kernel and module code.Function
aarch64_insn_patch_text()is a heavy version which may usestop_machine()to serialize all online CPUs, and function__aarch64_insn_patch_text()is light version without explicitly serialization.Hi Jiang,
I have written kprobes support for aarch64, and need both the functionality (lightweight and
stop_machine()versions).
I would like to rebase these API in kprobes, however slight changes would require in case ofstop_machineversion, which I explained below.
[Though kprobes cannot share Instruction encode support of jump labels as, decoding & simulation quite different for kprobes/uprobes and based around single stepping]Quoted 50 lines
Signed-off-by Jiang Liu <[email protected]>Cc Jiang Liu <[email protected]>--- arch/arm64/include/asm/insn.h | 2 ++ arch/arm64/kernel/insn.c | 64 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+)arch/arm64/include/asm/insn.h +2
-0@@ -49,5 +49,7 @@ __AARCH64_INSN_FUNCS(nop, 0xFFFFFFFF, 0xD503201F)enum aarch64_insn_class aarch64_get_insn_class (u32 insn);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn); +int aarch64_insn_patch_text (void *addr,u32 *insns,int cnt); +int __aarch64_insn_patch_text (void *addr,u32 *insns,int cnt);#endif /* _ASM_ARM64_INSN_H */ arch/arm64/kernel/insn.c +15
-0@@ -15,6 +15,8 @@* along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <linux/kernel.h> +#include <linux/stop_machine.h> +#include <asm/cacheflush.h> #include <asm/insn.h> static int aarch64_insn_cls[] = {@@ -69,3 +71,65 @@ bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn)return __aarch64_insn_hotpatch_safe (old_insn) &&__aarch64_insn_hotpatch_safe (new_insn); } + +struct aarch64_insn_patch { +void *text_addr; +u32 *new_insns; +int insn_cnt; +}; + +int __kprobes __aarch64_insn_patch_text (void *addr,u32 *insns,int cnt) +{ +int i; +u32 *tp = addr; + +/* instructions must be word aligned */ arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn) · line 96
Quoted 10 lines
arch/arm64/kernel/insn.c +10
-0+if (cnt <=0 || ((uintptr_t )addr &0x3 )) +return -EINVAL ; + +for (i =0 ; i < cnt; i++) + tp[i] = insns[i]; + +flush_icache_range ((uintptr_t )tp, (uintptr_t )tp + cnt *sizeof (u32)); + +return 0 ; +}Looks fine, but do you need to check for CPU big endian mode here? (I think
swab32()needed if EL1 is in big-endian mode)Quoted 14 lines
arch/arm64/kernel/insn.c +14
-0+ +static int __kprobes aarch64_insn_patch_text_cb (void *arg) +{ +struct aarch64_insn_patch *pp = arg; + +return __aarch64_insn_patch_text (pp->text_addr, pp->new_insns, + pp->insn_cnt); +} + +int __kprobes aarch64_insn_patch_text (void *addr,u32 *insns,int cnt) +{ +int ret; +bool safe = false; +arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn) · line 120
Quoted 10 lines
arch/arm64/kernel/insn.c +10
-0+/* instructions must be word aligned */ +if (cnt <=0 || ((uintptr_t )addr &0x3 )) +return -EINVAL ; + +if (cnt ==1 ) + safe =aarch64_insn_hotpatch_safe (*(u32 *)addr, insns[0 ]); + +if (safe) { + ret =__aarch64_insn_patch_text (addr, insns, cnt); + }else {Can you move the code below this into separate API that just apply patch with
stop_machine? And then a wrapper function for jump label specific handling that checks foraarch64_insn_hotpatch_safe()? Also, it will be good to move the patching code out ofinsn.ctopatch.c(refer toarch/arm/kernel/patch.c).Please refer to attached file (my current implementation) to make sense of exactly what kprobes would need (ignore the big-endian part for now). I think splitting the code should be straight-forward and we can avoid two different implementations. Please let me know if this can be done, I will rebase my patches above your next version.
Thanks,
SandeepaQuoted 23 lines
arch/arm64/kernel/insn.c +15
-1+struct aarch64_insn_patch patch = { + .text_addr = addr, + .new_insns = insns, + .insn_cnt = cnt, + }; + +/* +* Execute __aarch64_insn_patch_text() on every online CPU, +* which ensure serialization among all online CPUs. +*/ + ret =stop_machine (aarch64_insn_patch_text_cb, &patch,NULL ); + } + +return ret; +} --1.8.1.2
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Steven Rostedt
reviewerRe: [PATCH v1 7/7] jump_label: use defined macros instead of hard-coding for better readability
In reply to Jiang Liu[ Added Jason to Cc. ]
Quoted 32 lines · On Wed, 25 Sep 2013 23:47:19 +0800 Jiang Liu <[email protected]> wrote:
Quoted 21 lines · Steven Rostedt
Quoted 18 lines · On Wed, 25 Sep 2013 18:44:27 +0800 Jiang Liu <[email protected]> wrote:
From: Jiang Liu <[email protected]>
Use macro
JUMP_LABEL_TRUE_BRANCHinstead of hard-coding for better readability.Signed-off-by Jiang Liu <[email protected]>Cc Jiang Liu <[email protected]>--- include/linux/jump_label.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)include/linux/jump_label.h +0
-0@@ -116,7 +116,8 @@ extern void static_key_slow_dec(struct static_key *key);extern void jump_label_apply_nops (struct module *mod);Quoted 10 lines
include/linux/jump_label.h extern void static_key_slow_dec(struct static_key *key); · line 123
Quoted 7 lines
include/linux/jump_label.h +2
-1#define STATIC_KEY_INIT_TRUE ((struct static_key) \ - { .enabled = ATOMIC_INIT(1), .entries = (void *)1 }) + { .enabled = ATOMIC_INIT(1), \ + .entries = (void *)JUMP_LABEL_TRUE_BRANCH })#define STATIC_KEY_INIT_FALSE ((struct static_key) \ { .enabled = ATOMIC_INIT(0), .entries = (void *)0 })Probably should add a comment that there is no "
JUMP_LABEL_FALSE_BRANCH" or maybe we should add one?
Quoted 6 lines
Hi Steven,
How about
JUMP_LABEL_TYPE_MASK/JUMP_LABEL_TRUE_BRANCH/JUMP_LABEL_FALSE_BRANCH tuple? That should have the best readability.
Thanks!
You mean a mask of ~1, and TRUE be 1 and FALSE be 0.
That may work.
-- Steve
Re: [PATCH v1 2/7] arm64: introduce interfaces to hotpatch kernel and module code
In reply to Steven RostedtQuoted 7 lines · Steven Rostedt
Quoted 3 lines · On Wed, 25 Sep 2013 20:12:17 +0530 Sandeepa Prabhu…
Quoted 3 lines
On aarch64, are instructions always word aligned? If not, it should be safe for stop machine to modify non word aligned instructions, but this patch looks like it doesn't allow
stop_machine()to do so.
Quoted 8 lines
Quoted 5 lines
Steve,
Yes, aarch64 instructions must be word-aligned, else instruction fetch would generate Misaligned PC fault.
Thanks for clarifying, as IIUC, there's ARM architectures that allow for 2 and 4 byte instructions.
Yes, ARM 32-bit mode would support both 32-bit and 16-bit alignment based on ARM or Thumb mode, whereas
AArch64 (in arch/arm64/) is always 32-bit instructions and PC need to be aligned to 32-bit address.
Thanks,
Sandeepa
Quoted 2 lines
-- Steve
Re: [PATCH v1 2/7] arm64: introduce interfaces to hotpatch kernel and module code
In reply to Jiang LiuQuoted 6 lines · Jiang Liu
Hi Sandeepa,
Great to know that you are working on kprobe for ARM64.
I have done some investigation, found it's an huge work for me then gave up:(
I will try refine the implementation based on your requirement. Thanks!
Hi Jiang,
Thanks. please CC me when you post next version of this patch, then I can rebase my code and verify it.
Thanks,
Sandeepa
Quoted 149 lines · Sandeepa Prabhu
Quoted 146 lines · Jiang Liu
Quoted 9 lines
From: Jiang Liu <[email protected]>
Introduce
aarch64_insn_patch_text()and__aarch64_insn_patch_text()to patch kernel and module code.Function
aarch64_insn_patch_text()is a heavy version which may usestop_machine()to serialize all online CPUs, and function__aarch64_insn_patch_text()is light version without explicitly serialization.Hi Jiang,
I have written kprobes support for aarch64, and need both the functionality (lightweight and
stop_machine()versions).
I would like to rebase these API in kprobes, however slight changes would require in case ofstop_machineversion, which I explained below.
[Though kprobes cannot share Instruction encode support of jump labels as, decoding & simulation quite different for kprobes/uprobes and based around single stepping]Quoted 50 lines
Signed-off-by Jiang Liu <[email protected]>Cc Jiang Liu <[email protected]>--- arch/arm64/include/asm/insn.h | 2 ++ arch/arm64/kernel/insn.c | 64 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+)arch/arm64/include/asm/insn.h +2
-0@@ -49,5 +49,7 @@ __AARCH64_INSN_FUNCS(nop, 0xFFFFFFFF, 0xD503201F)enum aarch64_insn_class aarch64_get_insn_class (u32 insn);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn); +int aarch64_insn_patch_text (void *addr,u32 *insns,int cnt); +int __aarch64_insn_patch_text (void *addr,u32 *insns,int cnt);#endif /* _ASM_ARM64_INSN_H */ arch/arm64/kernel/insn.c +15
-0@@ -15,6 +15,8 @@* along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <linux/kernel.h> +#include <linux/stop_machine.h> +#include <asm/cacheflush.h> #include <asm/insn.h> static int aarch64_insn_cls[] = {@@ -69,3 +71,65 @@ bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn)return __aarch64_insn_hotpatch_safe (old_insn) &&__aarch64_insn_hotpatch_safe (new_insn); } + +struct aarch64_insn_patch { +void *text_addr; +u32 *new_insns; +int insn_cnt; +}; + +int __kprobes __aarch64_insn_patch_text (void *addr,u32 *insns,int cnt) +{ +int i; +u32 *tp = addr; + +/* instructions must be word aligned */ arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn) · line 96
Quoted 10 lines
arch/arm64/kernel/insn.c +10
-0+if (cnt <=0 || ((uintptr_t )addr &0x3 )) +return -EINVAL ; + +for (i =0 ; i < cnt; i++) + tp[i] = insns[i]; + +flush_icache_range ((uintptr_t )tp, (uintptr_t )tp + cnt *sizeof (u32)); + +return 0 ; +}Looks fine, but do you need to check for CPU big endian mode here? (I think
swab32()needed if EL1 is in big-endian mode)Quoted 14 lines
arch/arm64/kernel/insn.c +14
-0+ +static int __kprobes aarch64_insn_patch_text_cb (void *arg) +{ +struct aarch64_insn_patch *pp = arg; + +return __aarch64_insn_patch_text (pp->text_addr, pp->new_insns, + pp->insn_cnt); +} + +int __kprobes aarch64_insn_patch_text (void *addr,u32 *insns,int cnt) +{ +int ret; +bool safe = false; +arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn) · line 120
Quoted 10 lines
arch/arm64/kernel/insn.c +10
-0+/* instructions must be word aligned */ +if (cnt <=0 || ((uintptr_t )addr &0x3 )) +return -EINVAL ; + +if (cnt ==1 ) + safe =aarch64_insn_hotpatch_safe (*(u32 *)addr, insns[0 ]); + +if (safe) { + ret =__aarch64_insn_patch_text (addr, insns, cnt); + }else {Can you move the code below this into separate API that just apply patch with
stop_machine? And then a wrapper function for jump label specific handling that checks foraarch64_insn_hotpatch_safe()? Also, it will be good to move the patching code out ofinsn.ctopatch.c(refer toarch/arm/kernel/patch.c).Please refer to attached file (my current implementation) to make sense of exactly what kprobes would need (ignore the big-endian part for now). I think splitting the code should be straight-forward and we can avoid two different implementations. Please let me know if this can be done, I will rebase my patches above your next version.
Thanks,
SandeepaQuoted 23 lines
arch/arm64/kernel/insn.c +15
-1+struct aarch64_insn_patch patch = { + .text_addr = addr, + .new_insns = insns, + .insn_cnt = cnt, + }; + +/* +* Execute __aarch64_insn_patch_text() on every online CPU, +* which ensure serialization among all online CPUs. +*/ + ret =stop_machine (aarch64_insn_patch_text_cb, &patch,NULL ); + } + +return ret; +} --1.8.1.2
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH v1 2/7] arm64: introduce interfaces to hotpatch kernel and module code
In reply to Sandeepa PrabhuQuoted 75 lines · Sandeepa Prabhu
Quoted 9 lines · Jiang Liu
From: Jiang Liu <[email protected]>
Introduce
aarch64_insn_patch_text()and__aarch64_insn_patch_text()to patch kernel and module code.Function
aarch64_insn_patch_text()is a heavy version which may usestop_machine()to serialize all online CPUs, and function__aarch64_insn_patch_text()is light version without explicitly serialization.Hi Jiang,
I have written kprobes support for aarch64, and need both the functionality (lightweight and
stop_machine()versions).
I would like to rebase these API in kprobes, however slight changes would require in case ofstop_machineversion, which I explained below.
[Though kprobes cannot share Instruction encode support of jump labels as, decoding & simulation quite different for kprobes/uprobes and based around single stepping]Quoted 54 lines
Signed-off-by Jiang Liu <[email protected]>Cc Jiang Liu <[email protected]>--- arch/arm64/include/asm/insn.h | 2 ++ arch/arm64/kernel/insn.c | 64 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+)arch/arm64/include/asm/insn.h +2
-0@@ -49,5 +49,7 @@ __AARCH64_INSN_FUNCS(nop, 0xFFFFFFFF, 0xD503201F)enum aarch64_insn_class aarch64_get_insn_class (u32 insn);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn); +int aarch64_insn_patch_text (void *addr,u32 *insns,int cnt); +int __aarch64_insn_patch_text (void *addr,u32 *insns,int cnt);#endif /* _ASM_ARM64_INSN_H */ arch/arm64/kernel/insn.c +19
-0@@ -15,6 +15,8 @@* along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <linux/kernel.h> +#include <linux/stop_machine.h> +#include <asm/cacheflush.h> #include <asm/insn.h> static int aarch64_insn_cls[] = {@@ -69,3 +71,65 @@ bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn)return __aarch64_insn_hotpatch_safe (old_insn) &&__aarch64_insn_hotpatch_safe (new_insn); } + +struct aarch64_insn_patch { +void *text_addr; +u32 *new_insns; +int insn_cnt; +}; + +int __kprobes __aarch64_insn_patch_text (void *addr,u32 *insns,int cnt) +{ +int i; +u32 *tp = addr; + +/* instructions must be word aligned */ +if (cnt <=0 || ((uintptr_t )addr &0x3 )) +return -EINVAL ; + +for (i =0 ; i < cnt; i++)
Quoted 8 lines
arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn) · line 96
Quoted 6 lines
arch/arm64/kernel/insn.c +6
-0+ tp[i] = insns[i]; + +flush_icache_range ((uintptr_t )tp, (uintptr_t )tp + cnt *sizeof (u32)); + +return 0 ; +}Looks fine, but do you need to check for CPU big endian mode here? (I think
swab32()needed if EL1 is in big-endian mode)
Hi Sandeepa,
Thanks for reminder, we do need to take care of data access endian
issue here, will fix it in next version.
Quoted 32 lines
Quoted 15 lines
arch/arm64/kernel/insn.c +14
-0+ +static int __kprobes aarch64_insn_patch_text_cb (void *arg) +{ +struct aarch64_insn_patch *pp = arg; + +return __aarch64_insn_patch_text (pp->text_addr, pp->new_insns, + pp->insn_cnt); +} + +int __kprobes aarch64_insn_patch_text (void *addr,u32 *insns,int cnt) +{ +int ret; +bool safe = false; +arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn) · line 120
Quoted 10 lines
arch/arm64/kernel/insn.c +10
-0+/* instructions must be word aligned */ +if (cnt <=0 || ((uintptr_t )addr &0x3 )) +return -EINVAL ; + +if (cnt ==1 ) + safe =aarch64_insn_hotpatch_safe (*(u32 *)addr, insns[0 ]); + +if (safe) { + ret =__aarch64_insn_patch_text (addr, insns, cnt); + }else {Can you move the code below this into separate API that just apply patch with
stop_machine? And then a wrapper function for jump label specific handling that checks foraarch64_insn_hotpatch_safe()? Also, it will be good to move the patching code out ofinsn.ctopatch.c(refer toarch/arm/kernel/patch.c).
Quoted 5 lines
Please refer to attached file (my current implementation) to make sense of exactly what kprobes would need (ignore the big-endian part for now). I think splitting the code should be straight-forward and we can avoid two different implementations. Please let me know if this can be done, I will rebase my patches above your next version.
After reading the attached file, I feel current implementation of aarch64_insn_patch_text() should satisfy kprobe's requirements too.
The extra optimization of aarch64_insn_hotpatch_safe() should work for kprobe too. For example, if we install a kprobe on a function call instruction(BL), we need to replace "BL offset1" with "BRK" or "BL offset2". In such cases, we could directly patch the code without stop_machine(). Is there any special requirement that we must use stop_machine() for kprobe here?
Thanks!
Gerry
Quoted 26 lines
Thanks,
SandeepaQuoted 23 lines
arch/arm64/kernel/insn.c +15
-1+struct aarch64_insn_patch patch = { + .text_addr = addr, + .new_insns = insns, + .insn_cnt = cnt, + }; + +/* +* Execute __aarch64_insn_patch_text() on every online CPU, +* which ensure serialization among all online CPUs. +*/ + ret =stop_machine (aarch64_insn_patch_text_cb, &patch,NULL ); + } + +return ret; +} --1.8.1.2
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH v1 2/7] arm64: introduce interfaces to hotpatch kernel and module code
Quoted 129 lines · Jiang Liu
Quoted 75 lines · Sandeepa Prabhu
Quoted 9 lines · Jiang Liu
From: Jiang Liu <[email protected]>
Introduce
aarch64_insn_patch_text()and__aarch64_insn_patch_text()to patch kernel and module code.Function
aarch64_insn_patch_text()is a heavy version which may usestop_machine()to serialize all online CPUs, and function__aarch64_insn_patch_text()is light version without explicitly serialization.Hi Jiang,
I have written kprobes support for aarch64, and need both the functionality (lightweight and
stop_machine()versions).
I would like to rebase these API in kprobes, however slight changes would require in case ofstop_machineversion, which I explained below.
[Though kprobes cannot share Instruction encode support of jump labels as, decoding & simulation quite different for kprobes/uprobes and based around single stepping]Quoted 54 lines
Signed-off-by Jiang Liu <[email protected]>Cc Jiang Liu <[email protected]>--- arch/arm64/include/asm/insn.h | 2 ++ arch/arm64/kernel/insn.c | 64 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+)arch/arm64/include/asm/insn.h +2
-0@@ -49,5 +49,7 @@ __AARCH64_INSN_FUNCS(nop, 0xFFFFFFFF, 0xD503201F)enum aarch64_insn_class aarch64_get_insn_class (u32 insn);bool aarch64_insn_hotpatch_safe (u32 old_insn,u32 new_insn); +int aarch64_insn_patch_text (void *addr,u32 *insns,int cnt); +int __aarch64_insn_patch_text (void *addr,u32 *insns,int cnt);#endif /* _ASM_ARM64_INSN_H */ arch/arm64/kernel/insn.c +19
-0@@ -15,6 +15,8 @@* along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <linux/kernel.h> +#include <linux/stop_machine.h> +#include <asm/cacheflush.h> #include <asm/insn.h> static int aarch64_insn_cls[] = {@@ -69,3 +71,65 @@ bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn)return __aarch64_insn_hotpatch_safe (old_insn) &&__aarch64_insn_hotpatch_safe (new_insn); } + +struct aarch64_insn_patch { +void *text_addr; +u32 *new_insns; +int insn_cnt; +}; + +int __kprobes __aarch64_insn_patch_text (void *addr,u32 *insns,int cnt) +{ +int i; +u32 *tp = addr; + +/* instructions must be word aligned */ +if (cnt <=0 || ((uintptr_t )addr &0x3 )) +return -EINVAL ; + +for (i =0 ; i < cnt; i++)Quoted 8 lines
arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn) · line 96
Quoted 6 lines
arch/arm64/kernel/insn.c +6
-0+ tp[i] = insns[i]; + +flush_icache_range ((uintptr_t )tp, (uintptr_t )tp + cnt *sizeof (u32)); + +return 0 ; +}Looks fine, but do you need to check for CPU big endian mode here? (I think
swab32()needed if EL1 is in big-endian mode)Hi Sandeepa,
Thanks for reminder, we do need to take care of data access endian
issue here, will fix it in next version.Quoted 32 lines
Quoted 15 lines
arch/arm64/kernel/insn.c +14
-0+ +static int __kprobes aarch64_insn_patch_text_cb (void *arg) +{ +struct aarch64_insn_patch *pp = arg; + +return __aarch64_insn_patch_text (pp->text_addr, pp->new_insns, + pp->insn_cnt); +} + +int __kprobes aarch64_insn_patch_text (void *addr,u32 *insns,int cnt) +{ +int ret; +bool safe = false; +arch/arm64/kernel/insn.c bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn) · line 120
Quoted 10 lines
arch/arm64/kernel/insn.c +10
-0+/* instructions must be word aligned */ +if (cnt <=0 || ((uintptr_t )addr &0x3 )) +return -EINVAL ; + +if (cnt ==1 ) + safe =aarch64_insn_hotpatch_safe (*(u32 *)addr, insns[0 ]); + +if (safe) { + ret =__aarch64_insn_patch_text (addr, insns, cnt); + }else {Can you move the code below this into separate API that just apply patch with
stop_machine? And then a wrapper function for jump label specific handling that checks foraarch64_insn_hotpatch_safe()? Also, it will be good to move the patching code out ofinsn.ctopatch.c(refer toarch/arm/kernel/patch.c).Quoted 5 lines
Please refer to attached file (my current implementation) to make sense of exactly what kprobes would need (ignore the big-endian part for now). I think splitting the code should be straight-forward and we can avoid two different implementations. Please let me know if this can be done, I will rebase my patches above your next version.
After reading the attached file, I feel current implementation of
aarch64_insn_patch_text()should satisfy kprobe's requirements too.
Quoted 6 lines
The extra optimization of
aarch64_insn_hotpatch_safe()should work for kprobe too. For example, if we install a kprobe on a function call instruction(BL), we need to replace "BL offset1" with "BRK" or "BL offset2". In such cases, we could directly patch the code withoutstop_machine(). Is there any special requirement that we must usestop_machine()for kprobe here?
Kprobes will have it's own checks before placing a break-point so additional check is not required. Though it might work, that's not the point, it does not need extra check while kprobes logic would have already validated before calling patching API.
And stop_machine is needed to apply code patch in sync with all cpus, kprobes on most other platforms use stop_machine to update code section.
Thanks,
Sandeepa
Quoted 31 lines
Thanks!
GerryQuoted 26 lines
Thanks,
SandeepaQuoted 23 lines
arch/arm64/kernel/insn.c +15
-1+struct aarch64_insn_patch patch = { + .text_addr = addr, + .new_insns = insns, + .insn_cnt = cnt, + }; + +/* +* Execute __aarch64_insn_patch_text() on every online CPU, +* which ensure serialization among all online CPUs. +*/ + ret =stop_machine (aarch64_insn_patch_text_cb, &patch,NULL ); + } + +return ret; +} --1.8.1.2
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/