kprobes cleanups
cover kprobes cleanups
This patchset is a series of small fixups on kprobes.
Namhyung Kim (5):
kprobes: remove redundant address check
kprobes: verify jprobe entry point
kprobes: make functions static
kprobes: remove __dummy_buf
kprobes: add sparse context annotations
arch/x86/kernel/kprobes.c | 11 +++-------- kernel/kprobes.c | 22 ++++++++++++++-------- 2 files changed, 17 insertions(+), 16 deletions(-)
--
1.7.2.2
PATCH 1/5 kprobes: remove redundant address check
remove call to kernel_text_address() in register_jprobes() because it is called right after in register_kprobe().
--- kernel/kprobes.c | 13 +++++-------- 1 files changed, 5 insertions(+), 8 deletions(-)
kernel/kprobes.c +5 -8
@@ -1343,14 +1343,11 @@ int __kprobes register_jprobes(struct jprobe **jps, int num) jp = jps[i]; addr =arch_deref_entry_point (jp->entry); -if (!kernel_text_address (addr)) - ret = -EINVAL ; -else { -/* Todo: Verify probepoint is a function entry point */ - jp->kp.pre_handler = setjmp_pre_handler; - jp->kp.break_handler = longjmp_break_handler; - ret =register_kprobe (&jp->kp); - } +/* Todo: Verify probepoint is a function entry point */ + jp->kp.pre_handler = setjmp_pre_handler; + jp->kp.break_handler = longjmp_break_handler; + ret =register_kprobe (&jp->kp); +if (ret <0 ) {if (i >0 )unregister_jprobes (jps, i);
Signature · 1 line
1.7.2.2
PATCH 2/5 kprobes: verify jprobe entry point
In reply to Namhyung Kimverify jprobe's entry point is a function entry point using kallsyms' offset value.
--- kernel/kprobes.c | 14 +++++++++----- 1 files changed, 9 insertions(+), 5 deletions(-)
kernel/kprobes.c +9 -5
@@ -1339,14 +1339,18 @@ int __kprobes register_jprobes(struct jprobe **jps, int num)if (num <=0 )return -EINVAL ;for (i =0 ; i < num; i++) { -unsigned long addr; +unsigned long addr, offset; jp = jps[i]; addr =arch_deref_entry_point (jp->entry); -/* Todo: Verify probepoint is a function entry point */ - jp->kp.pre_handler = setjmp_pre_handler; - jp->kp.break_handler = longjmp_break_handler; - ret =register_kprobe (&jp->kp); +/* Verify probepoint is a function entry point */ +if (kallsyms_lookup_size_offset (addr,NULL , &offset) && + offset ==0 ) { + jp->kp.pre_handler = setjmp_pre_handler; + jp->kp.break_handler = longjmp_break_handler; + ret =register_kprobe (&jp->kp); + }else + ret = -EINVAL ;if (ret <0 ) {if (i >0 )
Signature · 1 line
1.7.2.2
PATCH 3/5 kprobes: make functions static
In reply to Namhyung Kimmake following (internal) functions static to make sparse happier :-)
* get_optimized_kprobe: only called from static functions
* kretprobe_table_unlock: _lock function is static
* kprobes_optinsn_template_holder: never called but holding asm code
--- arch/x86/kernel/kprobes.c | 2 +- kernel/kprobes.c | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-)
arch/x86/kernel/kprobes.c +1 -1
@@ -1129,7 +1129,7 @@ static void __kprobes synthesize_set_arg1(kprobe_opcode_t *addr, *(unsigned long *)addr = val; } -void __kprobes kprobes_optinsn_template_holder (void ) +static void __used __kprobes kprobes_optinsn_template_holder (void ) { asmvolatile ( ".global optprobe_template_entry\n"
kernel/kprobes.c +3 -2
@@ -399,7 +399,7 @@ static inline int kprobe_optready(struct kprobe *p)* Return an optimized kprobe whose optimizing code replaces * instructions including addr (exclude breakpoint). */ -struct kprobe *__kprobes get_optimized_kprobe (unsigned long addr) +static struct kprobe *__kprobes get_optimized_kprobe (unsigned long addr) {int i;struct kprobe *p =NULL ;
@@ -857,7 +857,8 @@ void __kprobes kretprobe_hash_unlock(struct task_struct *tsk,spin_unlock_irqrestore (hlist_lock, *flags); } -void __kprobes kretprobe_table_unlock (unsigned long hash,unsigned long *flags) +static void __kprobes kretprobe_table_unlock (unsigned long hash, +unsigned long *flags) {spinlock_t *hlist_lock =kretprobe_table_lock_ptr (hash);spin_unlock_irqrestore (hlist_lock, *flags);
Signature · 1 line
1.7.2.2
PATCH 4/5 kprobes: remove __dummy_buf
In reply to Namhyung Kimremove __dummy_buf which is needed for kallsyms_lookup only. use kallsysm_lookup_size_offset instead.
--- arch/x86/kernel/kprobes.c | 9 ++------- 1 files changed, 2 insertions(+), 7 deletions(-)
arch/x86/kernel/kprobes.c +2 -7
@@ -230,9 +230,6 @@ static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)return 0 ; } -/* Dummy buffers for kallsyms_lookup */ -static char __dummy_buf[KSYM_NAME_LEN ]; -/* Check if paddr is at an instruction boundary */ static int __kprobes can_probe (unsigned long paddr) {
@@ -241,7 +238,7 @@ static int __kprobes can_probe(unsigned long paddr)struct insn insn;kprobe_opcode_t buf[MAX_INSN_SIZE ]; -if (!kallsyms_lookup (paddr,NULL , &offset,NULL , __dummy_buf)) +if (!kallsyms_lookup_size_offset (paddr,NULL , &offset))return 0 ;/* Decode instructions */
@@ -1269,11 +1266,9 @@ static int __kprobes can_optimize(unsigned long paddr)unsigned long addr, size =0 , offset =0 ;struct insn insn;kprobe_opcode_t buf[MAX_INSN_SIZE ]; -/* Dummy buffers for lookup_symbol_attrs */ -static char __dummy_buf[KSYM_NAME_LEN ];/* Lookup symbol including addr */ -if (!kallsyms_lookup (paddr, &size, &offset,NULL , __dummy_buf)) +if (!kallsyms_lookup_size_offset (paddr, &size, &offset))return 0 ;/* Check there is enough space for a relative jump. */
Signature · 1 line
1.7.2.2
PATCH 5/5 kprobes: add sparse context annotations
In reply to Namhyung Kimthis removes following warnings when build with C=1
warning: context imbalance in 'kretprobe_hash_lock' - wrong count at exit
warning: context imbalance in 'kretprobe_table_lock' - wrong count at exit
warning: context imbalance in 'kretprobe_hash_unlock' - unexpected unlock
warning: context imbalance in 'kretprobe_table_unlock' - unexpected unlock
--- kernel/kprobes.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
kernel/kprobes.c +4 -0
@@ -831,6 +831,7 @@ void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,void __kprobes kretprobe_hash_lock (struct task_struct *tsk,struct hlist_head **head,unsigned long *flags) +__acquires (hlist_lock) {unsigned long hash =hash_ptr (tsk,KPROBE_HASH_BITS );spinlock_t *hlist_lock;
@@ -842,6 +843,7 @@ void __kprobes kretprobe_hash_lock(struct task_struct *tsk,static void __kprobes kretprobe_table_lock (unsigned long hash,unsigned long *flags) +__acquires (hlist_lock) {spinlock_t *hlist_lock =kretprobe_table_lock_ptr (hash);spin_lock_irqsave (hlist_lock, *flags);
@@ -849,6 +851,7 @@ static void __kprobes kretprobe_table_lock(unsigned long hash,void __kprobes kretprobe_hash_unlock (struct task_struct *tsk,unsigned long *flags) +__releases (hlist_lock) {unsigned long hash =hash_ptr (tsk,KPROBE_HASH_BITS );spinlock_t *hlist_lock;
@@ -859,6 +862,7 @@ void __kprobes kretprobe_hash_unlock(struct task_struct *tsk,static void __kprobes kretprobe_table_unlock (unsigned long hash,unsigned long *flags) +__releases (hlist_lock) {spinlock_t *hlist_lock =kretprobe_table_lock_ptr (hash);spin_unlock_irqrestore (hlist_lock, *flags);
Signature · 1 line
1.7.2.2