kprobes: Misc bugfixes
Subsystems (2)
cover kprobes: Misc bugfixes
Hi,
Here are some bugfixes and cleanup for the kprobe architecture dependent implementation. 2 of 3 are found by Jianhua Liu on arm64 optprobe code. I found that the similar issue on ppc64 and arm32 implementation so fixed those. And last one was reported by kernel test bot.
Thank you,
---
Masami Hiramatsu (3):
ARM: kprobes: Count MAX_OPTINSN_SIZE in kprobe_opcode_t powerpc/kprobes: Fix alloc_optinsn_page() to use all area of optinsn_slot
ia64: kprobes: Cleanup unused 'template' local variable
arch/arm/include/asm/kprobes.h | 3 +-- arch/ia64/kernel/kprobes.c | 3 --- arch/powerpc/include/asm/kprobes.h | 6 ++++++ arch/powerpc/kernel/optprobes.c | 25 +++++++++++++++++++------ arch/powerpc/kernel/optprobes_head.S | 5 ++--- 5 files changed, 28 insertions(+), 14 deletions(-)
--
Masami Hiramatsu (Linaro) <[email protected]>
PATCH 1/3 ARM: kprobes: Count MAX_OPTINSN_SIZE in kprobe_opcode_t
Count the MAX_OPTINSN_SIZE in 'kprobe_opcode_t' instead of in byte. On arm, the sizeof(kprobe_opcode_t) is u32, thus the current code consumes 4 times larger memory for trampoline buffer.
--- arch/arm/include/asm/kprobes.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm/include/asm/kprobes.h +1 -2
@@ -56,8 +56,7 @@ extern __visible kprobe_opcode_t optprobe_template_restore_end[];#define MAX_OPTIMIZED_LENGTH 4#define MAX_OPTINSN_SIZE \ - ((unsigned long)optprobe_template_end - \ - (unsigned long)optprobe_template_entry) + (unsigned long)(optprobe_template_end - optprobe_template_entry)#define RELATIVEJUMP_SIZE 4struct arch_optimized_insn {
PATCH 2/3 powerpc/kprobes: Fix alloc_optinsn_page() to use all area of optinsn_slot
In reply to Masami HiramatsuWhen the ppc64 uses 4K page size, most part of the optinsn_slot is not used because alloc_optinsn_page() is expected to return only one page-size memory.
To use the remaining memories, make insn_page_in_use as array to manage page-sized slots and return corresponding memory address in the optinsn_slot.
--- arch/powerpc/include/asm/kprobes.h | 6 ++++++ arch/powerpc/kernel/optprobes.c | 25 +++++++++++++++++++------ arch/powerpc/kernel/optprobes_head.S | 5 ++--- 3 files changed, 27 insertions(+), 9 deletions(-)
arch/powerpc/include/asm/kprobes.h +6 -0
@@ -4,6 +4,8 @@#include <asm-generic/kprobes.h> +#ifndef __ASSEMBLY__ +#ifdef __KERNEL__/* * Kernel Probes (KProbes)
@@ -94,4 +96,8 @@ static inline int kprobe_handler(struct pt_regs *regs) { return 0; }static inline int kprobe_post_handler (struct pt_regs *regs) {return 0 ; }#endif /* CONFIG_KPROBES */ #endif /* __KERNEL__ */ +#endif /* __ASSEMBLY__ */ + +#define KPROBE_OPTINSN_SLOT_SIZE 65536 +#endif /* _ASM_POWERPC_KPROBES_H */
arch/powerpc/kernel/optprobes.c +19 -6
@@ -25,19 +25,32 @@#define TMPL_INSN_IDX (optprobe_template_insn - optprobe_template_entry)#define TMPL_END_IDX (optprobe_template_end - optprobe_template_entry) -static bool insn_page_in_use; +#define OPTINSN_SLOT_PAGES (KPROBE_OPTINSN_SLOT_SIZE / PAGE_SIZE) + +static bool insn_page_in_use[OPTINSN_SLOT_PAGES ];void *alloc_optinsn_page (void ) { -if (insn_page_in_use) -return NULL ; - insn_page_in_use = true; -return &optinsn_slot; +int i; + +for (i =0 ; i <OPTINSN_SLOT_PAGES ; i++) { +if (!insn_page_in_use[i]) { + insn_page_in_use[i] = true; +return (void *)((unsigned long )&optinsn_slot +PAGE_SIZE * i); + } + } +return NULL ; }void free_optinsn_page (void *page) { - insn_page_in_use = false; +unsigned long idx = (unsigned long )page - (unsigned long )&optinsn_slot; + +WARN_ON_ONCE (idx & (PAGE_SIZE -1 )); + idx >>=PAGE_SHIFT ; +if (WARN_ON_ONCE (idx >=OPTINSN_SLOT_PAGES )) +return ; + insn_page_in_use[idx] = false; }/*
arch/powerpc/kernel/optprobes_head.S +2 -3
@@ -8,6 +8,7 @@ #include <asm/ppc_asm.h> #include <asm/ptrace.h> #include <asm/asm-offsets.h> +#include <asm/kprobes.h> #ifdef CONFIG_PPC64 #define SAVE_30GPRS(base) SAVE_10GPRS(2,base); SAVE_10GPRS(12,base); SAVE_10GPRS(22,base)
@@ -19,8 +20,6 @@ #define TEMPLATE_FOR_IMM_LOAD_INSNS nop; nop; nop #endif -#define OPT_SLOT_SIZE 65536 - .balign 4 /*
@@ -30,7 +29,7 @@ */ .global optinsn_slot optinsn_slot: - .space OPT_SLOT_SIZE + .space KPROBE_OPTINSN_SLOT_SIZE /* * Optprobe template:
PATCH 3/3 ia64: kprobes: Cleanup unused 'template' local variable
In reply to Masami HiramatsuRemove unused 'template' local variable. This seems to be introduced accidentally by a cleanup patch.
--- arch/ia64/kernel/kprobes.c | 3 --- 1 file changed, 3 deletions(-)
arch/ia64/kernel/kprobes.c +0 -3
@@ -322,9 +322,6 @@ static void __kprobes get_kprobe_inst(bundle_t *bundle, uint slot, unsigned long *kprobe_inst, uint *major_opcode) { unsigned long kprobe_inst_p0, kprobe_inst_p1; -unsigned int template; - - template = bundle->quad0.template;switch (slot) {case 0 :