diff mbox series

[bpf,v2,1/2] fprobe: samples: Add use_trace option and show hit/missed counter

Message ID 165461826247.280167.11939123218334322352.stgit@devnote2 (mailing list archive)
State Accepted
Delegated to: BPF
Headers show
Series rethook: Reject getting a rethook if RCU is not watching | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf
netdev/fixes_present success Fixes tag present in non-next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1 this patch: 1
netdev/cc_maintainers warning 5 maintainers not CCed: songliubraving@fb.com yhs@fb.com john.fastabend@gmail.com kafai@fb.com kpsingh@kernel.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param fail Was 0 now: 1
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1 this patch: 1
netdev/checkpatch fail CHECK: Alignment should match open parenthesis ERROR: do not initialise statics to false WARNING: Do not use trace_printk() in production code (this can be ignored if built only with a debug config option)
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-VM_Test-1 success Logs for Kernel LATEST on ubuntu-latest with gcc
bpf/vmtest-bpf-VM_Test-2 success Logs for Kernel LATEST on ubuntu-latest with llvm-15
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-VM_Test-3 success Logs for Kernel LATEST on z15 with gcc

Commit Message

Masami Hiramatsu (Google) June 7, 2022, 4:11 p.m. UTC
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Add use_trace option to use trace_printk() instead of pr_info()
so that the handler doesn't involve the RCU operations.
And show the hit and missed counter so that the user can check
how many times the probe handler hit and missed.

Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 samples/fprobe/fprobe_example.c |   21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

Comments

Steven Rostedt June 17, 2022, 4:06 p.m. UTC | #1
On Wed,  8 Jun 2022 01:11:02 +0900
"Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote:

>  
>  static void sample_entry_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs)
>  {
> -	pr_info("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
> +	if (use_trace)
> +		trace_printk("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);

Could we add a comment stating something like "this is just an example, no
kernel code should call trace_printk() except when actively debugging".

-- Steve


> +	else
> +		pr_info("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
> +	nhit++;
>  	if (stackdump)
>  		show_backtrace();
>  }
> @@ -49,8 +56,13 @@ static void sample_exit_handler(struct fprobe *fp, unsigned long ip, struct pt_r
>  {
>  	unsigned long rip = instruction_pointer(regs);
>  
> -	pr_info("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n",
> -		(void *)ip, (void *)ip, (void *)rip, (void *)rip);
> +	if (use_trace)
> +		trace_printk("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n",
> +			(void *)ip, (void *)ip, (void *)rip, (void *)rip);
> +	else
> +		pr_info("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n",
> +			(void *)ip, (void *)ip, (void *)rip, (void *)rip);
> +	nhit++;
>  	if (stackdump)
>  		show_backtrace();
Masami Hiramatsu (Google) June 22, 2022, 1:41 a.m. UTC | #2
On Fri, 17 Jun 2022 12:06:51 -0400
Steven Rostedt <rostedt@goodmis.org> wrote:

> On Wed,  8 Jun 2022 01:11:02 +0900
> "Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote:
> 
> >  
> >  static void sample_entry_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs)
> >  {
> > -	pr_info("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
> > +	if (use_trace)
> > +		trace_printk("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
> 
> Could we add a comment stating something like "this is just an example, no
> kernel code should call trace_printk() except when actively debugging".

Indeed. I also add a description for this option so that user can
understand this is just a debug option.

Thank you,
diff mbox series

Patch

diff --git a/samples/fprobe/fprobe_example.c b/samples/fprobe/fprobe_example.c
index 24d3cf109140..aad5af0278f4 100644
--- a/samples/fprobe/fprobe_example.c
+++ b/samples/fprobe/fprobe_example.c
@@ -21,6 +21,7 @@ 
 #define BACKTRACE_DEPTH 16
 #define MAX_SYMBOL_LEN 4096
 struct fprobe sample_probe;
+static unsigned long nhit;
 
 static char symbol[MAX_SYMBOL_LEN] = "kernel_clone";
 module_param_string(symbol, symbol, sizeof(symbol), 0644);
@@ -28,6 +29,8 @@  static char nosymbol[MAX_SYMBOL_LEN] = "";
 module_param_string(nosymbol, nosymbol, sizeof(nosymbol), 0644);
 static bool stackdump = true;
 module_param(stackdump, bool, 0644);
+static bool use_trace = false;
+module_param(use_trace, bool, 0644);
 
 static void show_backtrace(void)
 {
@@ -40,7 +43,11 @@  static void show_backtrace(void)
 
 static void sample_entry_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs)
 {
-	pr_info("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
+	if (use_trace)
+		trace_printk("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
+	else
+		pr_info("Enter <%pS> ip = 0x%p\n", (void *)ip, (void *)ip);
+	nhit++;
 	if (stackdump)
 		show_backtrace();
 }
@@ -49,8 +56,13 @@  static void sample_exit_handler(struct fprobe *fp, unsigned long ip, struct pt_r
 {
 	unsigned long rip = instruction_pointer(regs);
 
-	pr_info("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n",
-		(void *)ip, (void *)ip, (void *)rip, (void *)rip);
+	if (use_trace)
+		trace_printk("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n",
+			(void *)ip, (void *)ip, (void *)rip, (void *)rip);
+	else
+		pr_info("Return from <%pS> ip = 0x%p to rip = 0x%p (%pS)\n",
+			(void *)ip, (void *)ip, (void *)rip, (void *)rip);
+	nhit++;
 	if (stackdump)
 		show_backtrace();
 }
@@ -112,7 +124,8 @@  static void __exit fprobe_exit(void)
 {
 	unregister_fprobe(&sample_probe);
 
-	pr_info("fprobe at %s unregistered\n", symbol);
+	pr_info("fprobe at %s unregistered. %ld times hit, %ld times missed\n",
+		symbol, nhit, sample_probe.nmissed);
 }
 
 module_init(fprobe_init)