2020-05-05 14:16:40

by Thomas Gleixner

[permalink] [raw]
Subject: [patch V4 part 1 18/36] samples/kprobes: Add __kprobes and NOKPROBE_SYMBOL() for handlers.

From: Masami Hiramatsu <[email protected]>

Add __kprobes and NOKPROBE_SYMBOL() for sample kprobe handlers.

Signed-off-by: Masami Hiramatsu <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>
Link: https://lkml.kernel.org/r/158523421177.24735.16273975317343670204.stgit@devnote2

---
samples/kprobes/kprobe_example.c | 6 ++++--
samples/kprobes/kretprobe_example.c | 2 ++
2 files changed, 6 insertions(+), 2 deletions(-)

--- a/samples/kprobes/kprobe_example.c
+++ b/samples/kprobes/kprobe_example.c
@@ -25,7 +25,7 @@ static struct kprobe kp = {
};

/* kprobe pre_handler: called just before the probed instruction is executed */
-static int handler_pre(struct kprobe *p, struct pt_regs *regs)
+static int __kprobes handler_pre(struct kprobe *p, struct pt_regs *regs)
{
#ifdef CONFIG_X86
pr_info("<%s> pre_handler: p->addr = 0x%p, ip = %lx, flags = 0x%lx\n",
@@ -54,7 +54,7 @@ static int handler_pre(struct kprobe *p,
}

/* kprobe post_handler: called after the probed instruction is executed */
-static void handler_post(struct kprobe *p, struct pt_regs *regs,
+static void __kprobes handler_post(struct kprobe *p, struct pt_regs *regs,
unsigned long flags)
{
#ifdef CONFIG_X86
@@ -90,6 +90,8 @@ static int handler_fault(struct kprobe *
/* Return 0 because we don't handle the fault. */
return 0;
}
+/* NOKPROBE_SYMBOL() is also available */
+NOKPROBE_SYMBOL(handler_fault);

static int __init kprobe_init(void)
{
--- a/samples/kprobes/kretprobe_example.c
+++ b/samples/kprobes/kretprobe_example.c
@@ -48,6 +48,7 @@ static int entry_handler(struct kretprob
data->entry_stamp = ktime_get();
return 0;
}
+NOKPROBE_SYMBOL(entry_handler);

/*
* Return-probe handler: Log the return value and duration. Duration may turn
@@ -67,6 +68,7 @@ static int ret_handler(struct kretprobe_
func_name, retval, (long long)delta);
return 0;
}
+NOKPROBE_SYMBOL(ret_handler);

static struct kretprobe my_kretprobe = {
.handler = ret_handler,


2020-05-06 16:04:58

by Alexandre Chartre

[permalink] [raw]
Subject: Re: [patch V4 part 1 18/36] samples/kprobes: Add __kprobes and NOKPROBE_SYMBOL() for handlers.


On 5/5/20 3:16 PM, Thomas Gleixner wrote:
> From: Masami Hiramatsu <[email protected]>
>
> Add __kprobes and NOKPROBE_SYMBOL() for sample kprobe handlers.
>
> Signed-off-by: Masami Hiramatsu <[email protected]>
> Signed-off-by: Thomas Gleixner <[email protected]>
> Link: https://lkml.kernel.org/r/158523421177.24735.16273975317343670204.stgit@devnote2
>
> ---
> samples/kprobes/kprobe_example.c | 6 ++++--
> samples/kprobes/kretprobe_example.c | 2 ++
> 2 files changed, 6 insertions(+), 2 deletions(-)

Reviewed-by: Alexandre Chartre <[email protected]>

alex.

>
> --- a/samples/kprobes/kprobe_example.c
> +++ b/samples/kprobes/kprobe_example.c
> @@ -25,7 +25,7 @@ static struct kprobe kp = {
> };
>
> /* kprobe pre_handler: called just before the probed instruction is executed */
> -static int handler_pre(struct kprobe *p, struct pt_regs *regs)
> +static int __kprobes handler_pre(struct kprobe *p, struct pt_regs *regs)
> {
> #ifdef CONFIG_X86
> pr_info("<%s> pre_handler: p->addr = 0x%p, ip = %lx, flags = 0x%lx\n",
> @@ -54,7 +54,7 @@ static int handler_pre(struct kprobe *p,
> }
>
> /* kprobe post_handler: called after the probed instruction is executed */
> -static void handler_post(struct kprobe *p, struct pt_regs *regs,
> +static void __kprobes handler_post(struct kprobe *p, struct pt_regs *regs,
> unsigned long flags)
> {
> #ifdef CONFIG_X86
> @@ -90,6 +90,8 @@ static int handler_fault(struct kprobe *
> /* Return 0 because we don't handle the fault. */
> return 0;
> }
> +/* NOKPROBE_SYMBOL() is also available */
> +NOKPROBE_SYMBOL(handler_fault);
>
> static int __init kprobe_init(void)
> {
> --- a/samples/kprobes/kretprobe_example.c
> +++ b/samples/kprobes/kretprobe_example.c
> @@ -48,6 +48,7 @@ static int entry_handler(struct kretprob
> data->entry_stamp = ktime_get();
> return 0;
> }
> +NOKPROBE_SYMBOL(entry_handler);
>
> /*
> * Return-probe handler: Log the return value and duration. Duration may turn
> @@ -67,6 +68,7 @@ static int ret_handler(struct kretprobe_
> func_name, retval, (long long)delta);
> return 0;
> }
> +NOKPROBE_SYMBOL(ret_handler);
>
> static struct kretprobe my_kretprobe = {
> .handler = ret_handler,
>

Subject: [tip: core/kprobes] samples/kprobes: Add __kprobes and NOKPROBE_SYMBOL() for handlers.

The following commit has been merged into the core/kprobes branch of tip:

Commit-ID: d85eaa9411472a99de4b5732cb59c8bae629d5f1
Gitweb: https://git.kernel.org/tip/d85eaa9411472a99de4b5732cb59c8bae629d5f1
Author: Masami Hiramatsu <[email protected]>
AuthorDate: Thu, 26 Mar 2020 23:50:11 +09:00
Committer: Thomas Gleixner <[email protected]>
CommitterDate: Tue, 12 May 2020 17:15:33 +02:00

samples/kprobes: Add __kprobes and NOKPROBE_SYMBOL() for handlers.

Add __kprobes and NOKPROBE_SYMBOL() for sample kprobe handlers.

Signed-off-by: Masami Hiramatsu <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>
Reviewed-by: Alexandre Chartre <[email protected]>
Acked-by: Peter Zijlstra <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]

---
samples/kprobes/kprobe_example.c | 6 ++++--
samples/kprobes/kretprobe_example.c | 2 ++
2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/samples/kprobes/kprobe_example.c b/samples/kprobes/kprobe_example.c
index d693c23..501911d 100644
--- a/samples/kprobes/kprobe_example.c
+++ b/samples/kprobes/kprobe_example.c
@@ -25,7 +25,7 @@ static struct kprobe kp = {
};

/* kprobe pre_handler: called just before the probed instruction is executed */
-static int handler_pre(struct kprobe *p, struct pt_regs *regs)
+static int __kprobes handler_pre(struct kprobe *p, struct pt_regs *regs)
{
#ifdef CONFIG_X86
pr_info("<%s> pre_handler: p->addr = 0x%p, ip = %lx, flags = 0x%lx\n",
@@ -54,7 +54,7 @@ static int handler_pre(struct kprobe *p, struct pt_regs *regs)
}

/* kprobe post_handler: called after the probed instruction is executed */
-static void handler_post(struct kprobe *p, struct pt_regs *regs,
+static void __kprobes handler_post(struct kprobe *p, struct pt_regs *regs,
unsigned long flags)
{
#ifdef CONFIG_X86
@@ -90,6 +90,8 @@ static int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr)
/* Return 0 because we don't handle the fault. */
return 0;
}
+/* NOKPROBE_SYMBOL() is also available */
+NOKPROBE_SYMBOL(handler_fault);

static int __init kprobe_init(void)
{
diff --git a/samples/kprobes/kretprobe_example.c b/samples/kprobes/kretprobe_example.c
index 186315c..013e8e6 100644
--- a/samples/kprobes/kretprobe_example.c
+++ b/samples/kprobes/kretprobe_example.c
@@ -48,6 +48,7 @@ static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
data->entry_stamp = ktime_get();
return 0;
}
+NOKPROBE_SYMBOL(entry_handler);

/*
* Return-probe handler: Log the return value and duration. Duration may turn
@@ -67,6 +68,7 @@ static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
func_name, retval, (long long)delta);
return 0;
}
+NOKPROBE_SYMBOL(ret_handler);

static struct kretprobe my_kretprobe = {
.handler = ret_handler,