kprobes: treewide: Clean up kprobe code
Subsystems (11)
Masami Hiramatsu
maintainercover kprobes: treewide: Clean up kprobe code
Hi Ingo,
Here is a series of patches to cleanup the kprobes code. I tried to fix error message issues, comments, checkpatch.pl issues and so on. Some of those are what you suggested and pointed in the x86 stacktrace series.
https://lore.kernel.org/bpf/[email protected]/
I decided to split this seires from the x86 stacktrace fix series because there still be some on going discussions on that thread. The stacktrace fix series will be rebased on this series (and I've almost done, except for some discussion items).
Since this is a cleanup series, this does not change any functionality (of course some error messages, warnings, lock assertions are chaneged) but the coding styles are improved. I will continue this cleanup activities for each architecture port, and rethink the internal coding design and interfaces too.
Thank you,
---
Masami Hiramatsu (6):
kprobes: treewide: Cleanup the error messages for kprobes
kprobes: Fix coding style issues
kprobes: Use IS_ENABLED() instead of kprobes_built_in()
kprobes: Add assertions for required lock
kprobes: treewide: Use 'kprobe_opcode_t *' for the code address in get_optimized_kprobe()
kprobes: Use bool type for functions which returns boolean value
arch/arm/probes/kprobes/core.c | 4 arch/arm/probes/kprobes/opt-arm.c | 7 - arch/arm64/kernel/probes/kprobes.c | 5 - arch/csky/kernel/probes/kprobes.c | 10 + arch/mips/kernel/kprobes.c | 11 + arch/powerpc/kernel/optprobes.c | 6 - arch/riscv/kernel/probes/kprobes.c | 11 + arch/s390/kernel/kprobes.c | 4 arch/x86/kernel/kprobes/opt.c | 6 - include/linux/kprobes.h | 64 +++---- kernel/kprobes.c | 315 +++++++++++++++++++----------------- 11 files changed, 226 insertions(+), 217 deletions(-)
--
Masami Hiramatsu (Linaro) <[email protected]>
Masami Hiramatsu
maintainerPATCH 1/6 kprobes: treewide: Cleanup the error messages for kprobes
This clean up the error/notification messages in kprobes related code. Basically this defines 'pr_fmt()' macros for each files and update the messages which describes
- what happened,
- what is the kernel going to do or not do,
- is the kernel fine,
- what can the user do about it.
Also, if the message is not needed (e.g. the function returns unique error code, or other error message is already shown.) remove it, and replace the message with WARN_*() macros if suitable.
--- arch/arm/probes/kprobes/core.c | 4 +++- arch/arm64/kernel/probes/kprobes.c | 5 ++++- arch/csky/kernel/probes/kprobes.c | 10 +++++----- arch/mips/kernel/kprobes.c | 11 +++++------ arch/riscv/kernel/probes/kprobes.c | 11 +++++------ arch/s390/kernel/kprobes.c | 4 +++- kernel/kprobes.c | 36 ++++++++++++++++-------------------- 7 files changed, 41 insertions(+), 40 deletions(-)
arch/arm/probes/kprobes/core.c +3 -1
@@ -11,6 +11,8 @@* Copyright (C) 2007 Marvell Ltd. */ +#define pr_fmt (fmt) "kprobes: " fmt +#include <linux/kernel.h> #include <linux/kprobes.h> #include <linux/module.h>
@@ -278,7 +280,7 @@ void __kprobes kprobe_handler(struct pt_regs *regs)break ;case KPROBE_REENTER :/* A nested probe was hit in FIQ, it is a BUG */ -pr_warn ("Unrecoverable kprobe detected.\n" ); +pr_warn ("Failed to recover from reentered kprobes.\n" );dump_kprobe (p); fallthrough;default :
arch/arm64/kernel/probes/kprobes.c +4 -1
@@ -7,6 +7,9 @@* Copyright (C) 2013 Linaro Limited. * Author: Sandeepa Prabhu <[email protected]> */ + +#define pr_fmt (fmt) "kprobes: " fmt +#include <linux/extable.h> #include <linux/kasan.h> #include <linux/kernel.h>
@@ -218,7 +221,7 @@ static int __kprobes reenter_kprobe(struct kprobe *p,break ;case KPROBE_HIT_SS :case KPROBE_REENTER : -pr_warn ("Unrecoverable kprobe detected.\n" ); +pr_warn ("Failed to recover from reentered kprobes.\n" );dump_kprobe (p);BUG ();break ;
arch/csky/kernel/probes/kprobes.c +5 -5
@@ -1,5 +1,7 @@// SPDX-License-Identifier: GPL-2.0+ +#define pr_fmt (fmt) "kprobes: " fmt +#include <linux/kprobes.h> #include <linux/extable.h> #include <linux/slab.h>
@@ -77,10 +79,8 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p) {unsigned long probe_addr = (unsigned long )p->addr; -if (probe_addr &0x1 ) { -pr_warn ("Address not aligned.\n" ); -return -EINVAL ; - } +if (probe_addr &0x1 ) +return -EILSEQ ;/* copy instruction */ p->opcode =le32_to_cpu (*p->addr);
@@ -225,7 +225,7 @@ static int __kprobes reenter_kprobe(struct kprobe *p,break ;case KPROBE_HIT_SS :case KPROBE_REENTER : -pr_warn ("Unrecoverable kprobe detected.\n" ); +pr_warn ("Failed to recover from reentered kprobes.\n" );dump_kprobe (p);BUG ();break ;
arch/mips/kernel/kprobes.c +5 -6
@@ -11,6 +11,8 @@* Copyright (C) IBM Corporation, 2002, 2004 */ +#define pr_fmt (fmt) "kprobes: " fmt +#include <linux/kprobes.h> #include <linux/preempt.h> #include <linux/uaccess.h>
@@ -80,8 +82,7 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p) insn = p->addr[0 ];if (insn_has_ll_or_sc (insn)) { -pr_notice ("Kprobes for ll and sc instructions are not" -"supported\n" ); +pr_notice ("Kprobes for ll and sc instructions are not supported\n" ); ret = -EINVAL ; goto out; }
@@ -219,7 +220,7 @@ static int evaluate_branch_instruction(struct kprobe *p, struct pt_regs *regs,return 0 ; unaligned: -pr_notice ("%s: unaligned epc - sending SIGBUS.\n" , current->comm); +pr_notice ("Failed to emulate branch instruction because of unaligned epc - sending SIGBUS to %s.\n" , current->comm);force_sig (SIGBUS );return -EFAULT ;
@@ -238,10 +239,8 @@ static void prepare_singlestep(struct kprobe *p, struct pt_regs *regs, regs->cp0_epc = (unsigned long )p->addr; else if (insn_has_delayslot(p->opcode)) { ret =evaluate_branch_instruction (p, regs, kcb); -if (ret <0 ) { -pr_notice ("Kprobes: Error in evaluating branch\n" ); +if (ret <0 )return ; - } } regs->cp0_epc = (unsigned long )&p->ainsn.insn[0 ]; }
arch/riscv/kernel/probes/kprobes.c +5 -6
@@ -1,5 +1,7 @@// SPDX-License-Identifier: GPL-2.0+ +#define pr_fmt (fmt) "kprobes: " fmt +#include <linux/kprobes.h> #include <linux/extable.h> #include <linux/slab.h>
@@ -50,11 +52,8 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p) {unsigned long probe_addr = (unsigned long )p->addr; -if (probe_addr &0x1 ) { -pr_warn ("Address not aligned.\n" ); - -return -EINVAL ; - } +if (probe_addr &0x1 ) +return -EILSEQ ;/* copy instruction */ p->opcode = *p->addr;
@@ -208,7 +207,7 @@ static int __kprobes reenter_kprobe(struct kprobe *p,break ;case KPROBE_HIT_SS :case KPROBE_REENTER : -pr_warn ("Unrecoverable kprobe detected.\n" ); +pr_warn ("Failed to recover from reentered kprobes.\n" );dump_kprobe (p);BUG ();break ;
arch/s390/kernel/kprobes.c +3 -1
@@ -7,6 +7,8 @@* s390 port, used ppc64 as template. Mike Grundy <[email protected]> */ +#define pr_fmt (fmt) "kprobes: " fmt +#include <linux/moduleloader.h> #include <linux/kprobes.h> #include <linux/ptrace.h>
@@ -264,7 +266,7 @@ static void kprobe_reenter_check(struct kprobe_ctlblk *kcb, struct kprobe *p)* is a BUG. The code path resides in the .kprobes.text * section and is executed with interrupts disabled. */ -pr_err ("Invalid kprobe detected.\n" ); +pr_err ("Failed to recover from reentered kprobes.\n" );dump_kprobe (p);BUG (); }
kernel/kprobes.c +16 -20
@@ -18,6 +18,9 @@* <[email protected]> and Prasanna S Panchamukhi * <[email protected]> added function-return probes. */ + +#define pr_fmt (fmt) "kprobes: " fmt +#include <linux/kprobes.h> #include <linux/hash.h> #include <linux/init.h>
@@ -892,7 +895,7 @@ static void optimize_all_kprobes(void)optimize_kprobe (p); }cpus_read_unlock (); -printk (KERN_INFO "Kprobes globally optimized\n" ); +pr_info ("kprobe jump-optimization is enabled. All kprobes are optimized if possible.\n" ); out:mutex_unlock (&kprobe_mutex); }
@@ -925,7 +928,7 @@ static void unoptimize_all_kprobes(void)/* Wait for unoptimizing completion */ wait_for_kprobe_optimizer (); -printk (KERN_INFO "Kprobes globally unoptimized\n" ); +pr_info ("kprobe jump-optimization is disabled. All kprobes are based on software breakpoint.\n" ); }static DEFINE_MUTEX (kprobe_sysctl_mutex);
@@ -1003,7 +1006,7 @@ static int reuse_unused_kprobe(struct kprobe *ap)* unregistered. * Thus there should be no chance to reuse unused kprobe. */ -printk (KERN_ERR "Error: There should be no unused kprobe here.\n" ); +WARN_ON_ONCE (1 );return -EINVAL ; }
@@ -1049,18 +1052,13 @@ static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,int ret =0 ; ret =ftrace_set_filter_ip (ops, (unsigned long )p->addr,0 ,0 ); -if (ret) { -pr_debug ("Failed to arm kprobe-ftrace at %pS (%d)\n" , - p->addr, ret); +if (WARN_ONCE (ret <0 ,"Failed to arm kprobe-ftrace at %pS (error %d)\n" , p->addr, ret))return ret; - }if (*cnt =0 ) { ret =register_ftrace_function (ops); -if (ret) { -pr_debug ("Failed to init kprobe-ftrace (%d)\n" , ret); +if (WARN (ret <0 ,"Failed to register kprobe-ftrace (error %d)\n" , ret)) goto err_ftrace; - } } (*cnt)++;
@@ -1092,14 +1090,14 @@ static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,if (*cnt =1 ) { ret =unregister_ftrace_function (ops); -if (WARN (ret <0 ,"Failed to unregister kprobe-ftrace (%d)\n" , ret)) +if (WARN (ret <0 ,"Failed to unregister kprobe-ftrace (error %d)\n" , ret))return ret; } (*cnt)--; ret =ftrace_set_filter_ip (ops, (unsigned long )p->addr,1 ,0 ); -WARN_ONCE (ret <0 ,"Failed to disarm kprobe-ftrace at %pS (%d)\n" , +WARN_ONCE (ret <0 ,"Failed to disarm kprobe-ftrace at %pS (error %d)\n" , p->addr, ret);return ret; }
@@ -1885,7 +1883,7 @@ unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs, node = node->next; } -pr_err ("Oops! Kretprobe fails to find correct return address.\n" ); +pr_err ("kretprobe: Return address not found, not execute handler. Maybe there is a bug in the kernel.\n" );BUG_ON (1 ); found:
@@ -2214,8 +2212,7 @@ EXPORT_SYMBOL_GPL(enable_kprobe);/* Caller must NOT call this in usual path. This is only for critical case */ void dump_kprobe (struct kprobe *kp) { -pr_err ("Dumping kprobe:\n" ); -pr_err ("Name: %s\nOffset: %x\nAddress: %pS\n" , +pr_err ("Dump kprobe:\n.symbol_name = %s, .offset = %x, .addr = %pS\n" , kp->symbol_name, kp->offset, kp->addr); }NOKPROBE_SYMBOL (dump_kprobe);
@@ -2478,8 +2475,7 @@ static int __init init_kprobes(void) err =populate_kprobe_blacklist (__start_kprobe_blacklist, __stop_kprobe_blacklist);if (err) { -pr_err ("kprobes: failed to populate blacklist: %d\n" , err); -pr_err ("Please take care of using kprobes.\n" ); +pr_err ("Failed to populate blacklist (error %d), kprobes not restricted, be careful using them!\n" , err); }if (kretprobe_blacklist_size) {
@@ -2488,7 +2484,7 @@ static int __init init_kprobes(void) kretprobe_blacklist[i].addr kprobe_lookup_name(kretprobe_blacklist[i].name, 0);if (!kretprobe_blacklist[i].addr) -printk ("kretprobe: lookup failed: %s\n" , +pr_err ("Failed to lookup symbol '%s' for kretprobe blacklist. Maybe the target function is removed or renamed.\n" , kretprobe_blacklist[i].name); } }
@@ -2692,7 +2688,7 @@ static int arm_all_kprobes(void) }if (errors) -pr_warn ("Kprobes globally enabled, but failed to arm %d out of %d probes\n" , +pr_warn ("Kprobes globally enabled, but failed to enable %d out of %d probes. Please check which kprobes are kept disabled via debugfs.\n" , errors, total);else pr_info ("Kprobes globally enabled\n" );
@@ -2735,7 +2731,7 @@ static int disarm_all_kprobes(void) }if (errors) -pr_warn ("Kprobes globally disabled, but failed to disarm %d out of %d probes\n" , +pr_warn ("Kprobes globally disabled, but failed to disable %d out of %d probes. Please check which kprobes are kept enabled via debugfs.\n" , errors, total);else pr_info ("Kprobes globally disabled\n" );
Masami Hiramatsu
maintainerPATCH 2/6 kprobes: Fix coding style issues
In reply to Masami HiramatsuFix coding style issues reported by checkpatch.pl and update comments to quote variable names and add "()" to function name.
One TODO comment in __disarm_kprobe() is removed because it has been done by following commit.
--- include/linux/kprobes.h | 40 +++++--- kernel/kprobes.c | 238 ++++++++++++++++++++++++----------------------- 2 files changed, 146 insertions(+), 132 deletions(-)
include/linux/kprobes.h +23 -17
@@ -3,7 +3,6 @@#define _LINUX_KPROBES_H/* * Kernel Probes (KProbes) -* include/linux/kprobes.h * * Copyright (C) IBM Corporation, 2002, 2004 *
@@ -39,7 +38,7 @@#define KPROBE_REENTER 0x00000004#define KPROBE_HIT_SSDONE 0x00000008 -#else /* CONFIG_KPROBES */ +#else /* !CONFIG_KPROBES */ #include <asm-generic/kprobes.h> typedef int kprobe_opcode_t ; struct arch_specific_insn {
@@ -228,7 +227,7 @@ static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance return READ_ONCE(ri->rph->rp); } -#else /* CONFIG_KRETPROBES */ +#else /* !CONFIG_KRETPROBES */ static inline void arch_prepare_kretprobe (struct kretprobe *rp,struct pt_regs *regs) {
@@ -239,11 +238,15 @@ static inline int arch_trampoline_kprobe(struct kprobe *p) }#endif /* CONFIG_KRETPROBES */ +/* Markers of '_kprobe_blacklist' section */ +extern unsigned long __start_kprobe_blacklist[]; +extern unsigned long __stop_kprobe_blacklist[]; +extern struct kretprobe_blackpoint kretprobe_blacklist[];#ifdef CONFIG_KPROBES_SANITY_TEST extern int init_test_probes (void ); -#else +#else /* !CONFIG_KPROBES_SANITY_TEST */ static inline int init_test_probes (void ) {return 0 ;
@@ -303,7 +306,7 @@ static inline bool is_kprobe_##__name##_slot(unsigned long addr) \#define KPROBE_OPTINSN_PAGE_SYM "kprobe_optinsn_page"int kprobe_cache_get_kallsym (struct kprobe_insn_cache *c,unsigned int *symnum,unsigned long *value,char *type,char *sym); -#else /* __ARCH_WANT_KPROBES_INSN_SLOT */ +#else /* !__ARCH_WANT_KPROBES_INSN_SLOT */ #define DEFINE_INSN_CACHE_OPS (__name) \ static inline bool is_kprobe_##__name##_slot(unsigned long addr) \ { \
@@ -345,16 +348,17 @@ extern int sysctl_kprobes_optimization;extern int proc_kprobes_optimization_handler (struct ctl_table *table,int write,void *buffer,size_t *length,loff_t *ppos); -#endif +#endif /* CONFIG_SYSCTL */ extern void wait_for_kprobe_optimizer (void ); -#else +#else /* !CONFIG_OPTPROBES */ static inline void wait_for_kprobe_optimizer (void ) { }#endif /* CONFIG_OPTPROBES */ +#ifdef CONFIG_KPROBES_ON_FTRACE extern void kprobe_ftrace_handler (unsigned long ip,unsigned long parent_ip,struct ftrace_ops *ops,struct ftrace_regs *fregs);extern int arch_prepare_kprobe_ftrace (struct kprobe *p); -#endif +#endif /* CONFIG_KPROBES_ON_FTRACE */ int arch_check_ftrace_location (struct kprobe *p);
@@ -364,7 +368,7 @@ struct kprobe *get_kprobe(void *addr);/* kprobe_running() will just return the current_kprobe on this CPU */ static inline struct kprobe *kprobe_running (void ) { -return (__this_cpu_read (current_kprobe)); +return __this_cpu_read (current_kprobe); }static inline void reset_current_kprobe (void )
@@ -428,11 +432,11 @@ static inline struct kprobe *kprobe_running(void) }static inline int register_kprobe (struct kprobe *p) { -return -ENOSYS ; +return -EOPNOTSUPP ; }static inline int register_kprobes (struct kprobe **kps,int num) { -return -ENOSYS ; +return -EOPNOTSUPP ; }static inline void unregister_kprobe (struct kprobe *p) {
@@ -442,11 +446,11 @@ static inline void unregister_kprobes(struct kprobe **kps, int num) }static inline int register_kretprobe (struct kretprobe *rp) { -return -ENOSYS ; +return -EOPNOTSUPP ; }static inline int register_kretprobes (struct kretprobe **rps,int num) { -return -ENOSYS ; +return -EOPNOTSUPP ; }static inline void unregister_kretprobe (struct kretprobe *rp) {
@@ -462,11 +466,11 @@ static inline void kprobe_free_init_mem(void) }static inline int disable_kprobe (struct kprobe *kp) { -return -ENOSYS ; +return -EOPNOTSUPP ; }static inline int enable_kprobe (struct kprobe *kp) { -return -ENOSYS ; +return -EOPNOTSUPP ; }static inline bool within_kprobe_blacklist (unsigned long addr)
@@ -479,6 +483,7 @@ static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, return -ERANGE; }#endif /* CONFIG_KPROBES */ +static inline int disable_kretprobe (struct kretprobe *rp) {return disable_kprobe (&rp->kp);
@@ -493,13 +498,14 @@ static inline bool is_kprobe_insn_slot(unsigned long addr) {return false; } -#endif +#endif /* !CONFIG_KPROBES */ +#ifndef CONFIG_OPTPROBES static inline bool is_kprobe_optinsn_slot (unsigned long addr) {return false; } -#endif +#endif /* !CONFIG_OPTPROBES */ /* Returns true if kprobes handled the fault */ static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
kernel/kprobes.c +123 -115
738 lines not on this page · raw message
Masami Hiramatsu
maintainerPATCH 3/6 kprobes: Use IS_ENABLED() instead of kprobes_built_in()
In reply to Masami HiramatsuUse IS_ENABLED(CONFIG_KPROBES) instead of kprobes_built_in(). This inline function is introduced only for avoiding #ifdef. But since now we have IS_ENABLED(), it is no longer needed.
--- include/linux/kprobes.h | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-)
include/linux/kprobes.h +1 -13
@@ -180,14 +180,6 @@ struct kprobe_blacklist_entry { DECLARE_PER_CPU(struct kprobe *, current_kprobe); DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); -/* -* For #ifdef avoidance: -*/ -static inline int kprobes_built_in (void ) -{ -return 1 ; -} -extern void kprobe_busy_begin (void );extern void kprobe_busy_end (void );
@@ -414,10 +406,6 @@ int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value, char *type, char *sym);#else /* !CONFIG_KPROBES: */ -static inline int kprobes_built_in (void ) -{ -return 0 ; -}static inline int kprobe_fault_handler (struct pt_regs *regs,int trapnr) {return 0 ;
@@ -511,7 +499,7 @@ static inline bool is_kprobe_optinsn_slot(unsigned long addr) static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,unsigned int trap) { -if (!kprobes_built_in ()) +if (!IS_ENABLED (CONFIG_KPROBES ))return false;if (user_mode (regs))return false;
Masami Hiramatsu
maintainerPATCH 4/6 kprobes: Add assertions for required lock
In reply to Masami HiramatsuAdd assertions for required locks instead of comment it so that the lockdep can inspect locks automatically.
--- kernel/kprobes.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
kernel/kprobes.c +12 -7
@@ -959,11 +959,13 @@ int proc_kprobes_optimization_handler(struct ctl_table *table, int write, }#endif /* CONFIG_SYSCTL */ -/* Put a breakpoint for a probe. Must be called with 'text_mutex' locked. */ +/* Put a breakpoint for a probe. */ static void __arm_kprobe (struct kprobe *p) {struct kprobe *_p; +lockdep_assert_held (&text_mutex); +/* Find the overlapping optimized kprobes. */ _p =get_optimized_kprobe ((unsigned long )p->addr);if (unlikely (_p))
@@ -974,11 +976,13 @@ static void __arm_kprobe(struct kprobe *p)optimize_kprobe (p);/* Try to optimize (add kprobe to a list) */ } -/* Remove the breakpoint of a probe. Must be called with 'text_mutex' locked. */ +/* Remove the breakpoint of a probe. */ static void __disarm_kprobe (struct kprobe *p,bool reopt) {struct kprobe *_p; +lockdep_assert_held (&text_mutex); +/* Try to unoptimize */ unoptimize_kprobe (p, kprobes_all_disarmed);
@@ -1056,12 +1060,13 @@ static int prepare_kprobe(struct kprobe *p)return arch_prepare_kprobe_ftrace (p); } -/* Caller must lock 'kprobe_mutex' */ static int __arm_kprobe_ftrace (struct kprobe *p,struct ftrace_ops *ops,int *cnt) {int ret =0 ; +lockdep_assert_held (&kprobe_mutex); + ret =ftrace_set_filter_ip (ops, (unsigned long )p->addr,0 ,0 );if (WARN_ONCE (ret <0 ,"Failed to arm kprobe-ftrace at %pS (error %d)\n" , p->addr, ret))return ret;
@@ -1093,12 +1098,13 @@ static int arm_kprobe_ftrace(struct kprobe *p) ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled); } -/* Caller must lock 'kprobe_mutex'. */ static int __disarm_kprobe_ftrace (struct kprobe *p,struct ftrace_ops *ops,int *cnt) {int ret =0 ; +lockdep_assert_held (&kprobe_mutex); +if (*cnt =1 ) { ret =unregister_ftrace_function (ops);if (WARN (ret <0 ,"Failed to unregister kprobe-ftrace (error %d)\n" , ret))
@@ -1138,7 +1144,6 @@ static inline int disarm_kprobe_ftrace(struct kprobe *p) }#endif -/* Arm a kprobe with 'text_mutex'. */ static int arm_kprobe (struct kprobe *kp) {if (unlikely (kprobe_ftrace (kp)))
@@ -1153,7 +1158,6 @@ static int arm_kprobe(struct kprobe *kp)return 0 ; } -/* Disarm a kprobe with 'text_mutex'. */ static int disarm_kprobe (struct kprobe *kp,bool reopt) {if (unlikely (kprobe_ftrace (kp)))
@@ -1696,12 +1700,13 @@ static int aggr_kprobe_disabled(struct kprobe *ap)return 1 ; } -/* Disable one kprobe: Make sure called under 'kprobe_mutex' is locked. */ static struct kprobe *__disable_kprobe (struct kprobe *p) {struct kprobe *orig_p;int ret; +lockdep_assert_held (&kprobe_mutex); +/* Get an original kprobe for return */ orig_p =__get_valid_kprobe (p);if (unlikely (orig_p =NULL ))
Masami Hiramatsu
maintainerPATCH 5/6 kprobes: treewide: Use 'kprobe_opcode_t *' for the code address in get_optimized_kprobe()
In reply to Masami HiramatsuSince get_optimized_kprobe() is only used inside kprobes, it doesn't need to use 'unsigned long' type for 'addr' parameter. Make it use 'kprobe_opcode_t *' for the 'addr' parameter and subsequent call of arch_within_optimized_kprobe() also should use 'kprobe_opcode_t *'.
Note that MAX_OPTIMIZED_LENGTH and RELATIVEJUMP_SIZE are defined by byte-size, but the size of 'kprobe_opcode_t' depends on the architecture. Therefore, we must be careful when calculating addresses using those macros.
--- arch/arm/probes/kprobes/opt-arm.c | 7 ++++--- arch/powerpc/kernel/optprobes.c | 6 +++--- arch/x86/kernel/kprobes/opt.c | 6 +++--- include/linux/kprobes.h | 2 +- kernel/kprobes.c | 10 +++++----- 5 files changed, 16 insertions(+), 15 deletions(-)
arch/arm/probes/kprobes/opt-arm.c +4 -3
@@ -347,10 +347,11 @@ void arch_unoptimize_kprobes(struct list_head *oplist, }int arch_within_optimized_kprobe (struct optimized_kprobe *op, -unsigned long addr) +kprobe_opcode_t *addr) { -return ((unsigned long )op->kp.addr <= addr && - (unsigned long )op->kp.addr +RELATIVEJUMP_SIZE > addr); +return (op->kp.addr <= addr && + op->kp.addr + (RELATIVEJUMP_SIZE /sizeof (kprobe_opcode_t)) > addr); + }void arch_remove_optimized_kprobe (struct optimized_kprobe *op)
arch/powerpc/kernel/optprobes.c +3 -3
@@ -301,8 +301,8 @@ void arch_unoptimize_kprobes(struct list_head *oplist, struct list_head *done_li } } -int arch_within_optimized_kprobe (struct optimized_kprobe *op,unsigned long addr) +int arch_within_optimized_kprobe (struct optimized_kprobe *op,kprobe_opcode_t *addr) { -return ((unsigned long )op->kp.addr <= addr && - (unsigned long )op->kp.addr +RELATIVEJUMP_SIZE > addr); +return (op->kp.addr <= addr && + op->kp.addr + (RELATIVEJUMP_SIZE /sizeof (kprobe_opcode_t)) > addr); }
arch/x86/kernel/kprobes/opt.c +3 -3
@@ -367,10 +367,10 @@ int arch_check_optimized_kprobe(struct optimized_kprobe *op)/* Check the addr is within the optimized instructions. */ int arch_within_optimized_kprobe (struct optimized_kprobe *op, -unsigned long addr) +kprobe_opcode_t *addr) { -return ((unsigned long )op->kp.addr <= addr && - (unsigned long )op->kp.addr + op->optinsn.size > addr); +return (op->kp.addr <= addr && + op->kp.addr + op->optinsn.size > addr); }/* Free optimized instruction slot */
include/linux/kprobes.h +1 -1
@@ -329,7 +329,7 @@ extern void arch_unoptimize_kprobes(struct list_head *oplist, struct list_head *done_list);extern void arch_unoptimize_kprobe (struct optimized_kprobe *op);extern int arch_within_optimized_kprobe (struct optimized_kprobe *op, -unsigned long addr); +kprobe_opcode_t *addr);extern void opt_pre_handler (struct kprobe *p,struct pt_regs *regs);
kernel/kprobes.c +5 -5
@@ -485,15 +485,15 @@ static int kprobe_queued(struct kprobe *p)* Return an optimized kprobe whose optimizing code replaces * instructions including 'addr' (exclude breakpoint). */ -static struct kprobe *get_optimized_kprobe (unsigned long addr) +static struct kprobe *get_optimized_kprobe (kprobe_opcode_t *addr) {int i;struct kprobe *p =NULL ;struct optimized_kprobe *op;/* Don't check i = 0, since that is a breakpoint case. */ -for (i =1 ; !p && i <MAX_OPTIMIZED_LENGTH ; i++) - p =get_kprobe ((void *)(addr - i)); +for (i =1 ; !p && i <MAX_OPTIMIZED_LENGTH /sizeof (kprobe_opcode_t); i++) + p =get_kprobe (addr - i);if (p &&kprobe_optready (p)) { op = container_of(p, struct optimized_kprobe, kp);
@@ -967,7 +967,7 @@ static void __arm_kprobe(struct kprobe *p)lockdep_assert_held (&text_mutex);/* Find the overlapping optimized kprobes. */ - _p =get_optimized_kprobe ((unsigned long )p->addr); + _p =get_optimized_kprobe (p->addr);if (unlikely (_p))/* Fallback to unoptimized kprobe */ unoptimize_kprobe (_p, true);
@@ -989,7 +989,7 @@ static void __disarm_kprobe(struct kprobe *p, bool reopt)if (!kprobe_queued (p)) {arch_disarm_kprobe (p);/* If another kprobe was blocked, re-optimize it. */ - _p =get_optimized_kprobe ((unsigned long )p->addr); + _p =get_optimized_kprobe (p->addr);if (unlikely (_p) && reopt)optimize_kprobe (_p); }
Masami Hiramatsu
maintainerPATCH 6/6 kprobes: Use bool type for functions which returns boolean value
In reply to Masami HiramatsuUse the 'bool' type instead of 'int' for the functions which returns a boolean value, because this makes clear that those functions don't return any error code.
--- include/linux/kprobes.h | 8 ++++---- kernel/kprobes.c | 26 +++++++++++++------------- 2 files changed, 17 insertions(+), 17 deletions(-)
include/linux/kprobes.h +4 -4
@@ -104,25 +104,25 @@ struct kprobe {#define KPROBE_FLAG_FTRACE 8/* probe is using ftrace */ /* Has this kprobe gone ? */ -static inline int kprobe_gone (struct kprobe *p) +static inline bool kprobe_gone (struct kprobe *p) {return p->flags &KPROBE_FLAG_GONE ; }/* Is this kprobe disabled ? */ -static inline int kprobe_disabled (struct kprobe *p) +static inline bool kprobe_disabled (struct kprobe *p) {return p->flags & (KPROBE_FLAG_DISABLED |KPROBE_FLAG_GONE ); }/* Is this kprobe really running optimized path ? */ -static inline int kprobe_optimized (struct kprobe *p) +static inline bool kprobe_optimized (struct kprobe *p) {return p->flags &KPROBE_FLAG_OPTIMIZED ; }/* Is this kprobe uses ftrace ? */ -static inline int kprobe_ftrace (struct kprobe *p) +static inline bool kprobe_ftrace (struct kprobe *p) {return p->flags &KPROBE_FLAG_FTRACE ; }
kernel/kprobes.c +13 -13
@@ -198,8 +198,8 @@ kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c)return slot; } -/* Return 1 if all garbages are collected, otherwise 0. */ -static int collect_one_slot (struct kprobe_insn_page *kip,int idx) +/* Return true if all garbages are collected, otherwise false. */ +static bool collect_one_slot (struct kprobe_insn_page *kip,int idx) { kip->slot_used[idx] =SLOT_CLEAN ; kip->nused--;
@@ -223,9 +223,9 @@ static int collect_one_slot(struct kprobe_insn_page *kip, int idx) kip->cache->free (kip->insns);kfree (kip); } -return 1 ; +return true; } -return 0 ; +return false; }static int collect_garbage_slots (struct kprobe_insn_cache *c)
@@ -389,13 +389,13 @@ NOKPROBE_SYMBOL(get_kprobe);static int aggr_pre_handler (struct kprobe *p,struct pt_regs *regs);/* Return true if 'p' is an aggregator */ -static inline int kprobe_aggrprobe (struct kprobe *p) +static inline bool kprobe_aggrprobe (struct kprobe *p) {return p->pre_handler = aggr_pre_handler; }/* Return true if 'p' is unused */ -static inline int kprobe_unused (struct kprobe *p) +static inline bool kprobe_unused (struct kprobe *p) {return kprobe_aggrprobe (p) &&kprobe_disabled (p) &&list_empty (&p->list);
@@ -455,7 +455,7 @@ static inline int kprobe_optready(struct kprobe *p) }/* Return true if the kprobe is disarmed. Note: p must be on hash list */ -static inline int kprobe_disarmed (struct kprobe *p) +static inline bool kprobe_disarmed (struct kprobe *p) {struct optimized_kprobe *op;
@@ -469,16 +469,16 @@ static inline int kprobe_disarmed(struct kprobe *p) }/* Return true if the probe is queued on (un)optimizing lists */ -static int kprobe_queued (struct kprobe *p) +static bool kprobe_queued (struct kprobe *p) {struct optimized_kprobe *op;if (kprobe_aggrprobe (p)) { op = container_of(p, struct optimized_kprobe, kp);if (!list_empty (&op->list)) -return 1 ; +return true; } -return 0 ; +return false; }/*
@@ -1683,7 +1683,7 @@ int register_kprobe(struct kprobe *p)EXPORT_SYMBOL_GPL (register_kprobe);/* Check if all probes on the 'ap' are disabled. */ -static int aggr_kprobe_disabled (struct kprobe *ap) +static bool aggr_kprobe_disabled (struct kprobe *ap) {struct kprobe *kp;
@@ -1695,9 +1695,9 @@ static int aggr_kprobe_disabled(struct kprobe *ap)* Since there is an active probe on the list, * we can't disable this 'ap'. */ -return 0 ; +return false; -return 1 ; +return true; }static struct kprobe *__disable_kprobe (struct kprobe *p)
Re: [PATCH -tip 6/6] kprobes: Use bool type for functions which returns boolean value
Quoted 3 lines · Masami Hiramatsu
Use the 'bool' type instead of 'int' for the functions which returns a boolean value, because this makes clear that those functions don't return any error code.
[]
include/linux/kprobes.h
Quoted 1 line
include/linux/kprobes.h +0
-0
[]
Quoted 10 lines
include/linux/kprobes.h +0
-0@@ -104,25 +104,25 @@ struct kprobe { #define KPROBE_FLAG_FTRACE 8 /* probe is using ftrace *//* Has this kprobe gone ? */
include/linux/kprobes.h +1
-1-static inline int kprobe_gone (struct kprobe *p) +static inline bool kprobe_gone (struct kprobe *p){
returnp->flags&KPROBE_FLAG_GONE;
}
This change would also allow the removal of the !! from:
kernel/trace/trace_kprobe.c:104: return !!(kprobe_gone(&tk->rp.kp));
--- kernel/trace/trace_kprobe.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace_kprobe.c +1 -1
@@ -101,7 +101,7 @@ static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk) { -return !!(kprobe_gone (&tk->rp.kp)); +return kprobe_gone (&tk->rp.kp); } static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
Masami Hiramatsu
maintainerRe: [PATCH -tip 6/6] kprobes: Use bool type for functions which returns boolean value
Quoted 20 lines · On Sat, 10 Jul 2021 09:27:22 -0700 Joe Perches <[email protected]> wrote:
Quoted 3 lines · Masami Hiramatsu
Use the 'bool' type instead of 'int' for the functions which returns a boolean value, because this makes clear that those functions don't return any error code.
[]
include/linux/kprobes.h
Quoted 1 line
include/linux/kprobes.h +0
-0[]
Quoted 10 lines
include/linux/kprobes.h +0
-0@@ -104,25 +104,25 @@ struct kprobe { #define KPROBE_FLAG_FTRACE 8 /* probe is using ftrace *//* Has this kprobe gone ? */
include/linux/kprobes.h +1
-1-static inline int kprobe_gone (struct kprobe *p) +static inline bool kprobe_gone (struct kprobe *p){
returnp->flags&KPROBE_FLAG_GONE;
}This change would also allow the removal of the !! from:
Quoted 1 line
kernel/trace/trace_kprobe.c:104: return !!(kprobe_gone(&tk->rp.kp));
Good catch! OK, I'll update
Thank you,
Quoted 19 lines
--- kernel/trace/trace_kprobe.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/trace/trace_kprobe.c +1
-1@@ -101,7 +101,7 @@ static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk) { -return !!(kprobe_gone (&tk->rp.kp)); +return kprobe_gone (&tk->rp.kp); } static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
Signature · 1 line
Masami Hiramatsu <[email protected]>