netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Masami Hiramatsu <mhiramat@kernel.org>
To: Jiri Olsa <jolsa@redhat.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andriin@fb.com>,
	netdev@vger.kernel.org, bpf@vger.kernel.org,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@chromium.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Alan Maguire <alan.maguire@oracle.com>
Subject: Re: [PATCHv3 bpf-next 4/7] bpf: Add bpf_get_func_ip helper for kprobe programs
Date: Sat, 10 Jul 2021 16:55:12 +0900	[thread overview]
Message-ID: <20210710165512.8b0ffc67a894fef9a883eef2@kernel.org> (raw)
In-Reply-To: <20210707214751.159713-5-jolsa@kernel.org>

On Wed,  7 Jul 2021 23:47:48 +0200
Jiri Olsa <jolsa@redhat.com> wrote:

> Adding bpf_get_func_ip helper for BPF_PROG_TYPE_KPROBE programs,
> so it's now possible to call bpf_get_func_ip from both kprobe and
> kretprobe programs.
> 
> Taking the caller's address from 'struct kprobe::addr', which is
> defined for both kprobe and kretprobe.
> 

Note that the kp->addr of kretprobe will be the callee function
address, even if the handler is called when the end of the callee.

Anyway, this looks good to me.

Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>

Thank you,

> Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> ---
>  include/uapi/linux/bpf.h       |  2 +-
>  kernel/bpf/verifier.c          |  2 ++
>  kernel/trace/bpf_trace.c       | 17 +++++++++++++++++
>  tools/include/uapi/linux/bpf.h |  2 +-
>  4 files changed, 21 insertions(+), 2 deletions(-)
> 
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 83e87ffdbb6e..4894f99a1993 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -4783,7 +4783,7 @@ union bpf_attr {
>   *
>   * u64 bpf_get_func_ip(void *ctx)
>   * 	Description
> - * 		Get address of the traced function (for tracing programs).
> + * 		Get address of the traced function (for tracing and kprobe programs).
>   * 	Return
>   * 		Address of the traced function.
>   */
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index f975a3aa9368..79eb9d81a198 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -5979,6 +5979,8 @@ static int has_get_func_ip(struct bpf_verifier_env *env)
>  			return -ENOTSUPP;
>  		}
>  		return 0;
> +	} else if (type == BPF_PROG_TYPE_KPROBE) {
> +		return 0;
>  	}
>  
>  	verbose(env, "func %s#%d not supported for program type %d\n",
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 9edd3b1a00ad..55acf56b0c3a 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -17,6 +17,7 @@
>  #include <linux/error-injection.h>
>  #include <linux/btf_ids.h>
>  #include <linux/bpf_lsm.h>
> +#include <linux/kprobes.h>
>  
>  #include <net/bpf_sk_storage.h>
>  
> @@ -961,6 +962,20 @@ static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
>  	.arg1_type	= ARG_PTR_TO_CTX,
>  };
>  
> +BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs)
> +{
> +	struct kprobe *kp = kprobe_running();
> +
> +	return kp ? (u64) kp->addr : 0;
> +}
> +
> +static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = {
> +	.func		= bpf_get_func_ip_kprobe,
> +	.gpl_only	= true,
> +	.ret_type	= RET_INTEGER,
> +	.arg1_type	= ARG_PTR_TO_CTX,
> +};
> +
>  const struct bpf_func_proto *
>  bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
>  {
> @@ -1092,6 +1107,8 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
>  	case BPF_FUNC_override_return:
>  		return &bpf_override_return_proto;
>  #endif
> +	case BPF_FUNC_get_func_ip:
> +		return &bpf_get_func_ip_proto_kprobe;
>  	default:
>  		return bpf_tracing_func_proto(func_id, prog);
>  	}
> diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
> index 83e87ffdbb6e..4894f99a1993 100644
> --- a/tools/include/uapi/linux/bpf.h
> +++ b/tools/include/uapi/linux/bpf.h
> @@ -4783,7 +4783,7 @@ union bpf_attr {
>   *
>   * u64 bpf_get_func_ip(void *ctx)
>   * 	Description
> - * 		Get address of the traced function (for tracing programs).
> + * 		Get address of the traced function (for tracing and kprobe programs).
>   * 	Return
>   * 		Address of the traced function.
>   */
> -- 
> 2.31.1
> 


-- 
Masami Hiramatsu <mhiramat@kernel.org>

  reply	other threads:[~2021-07-10  7:55 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-07 21:47 [PATCHv3 bpf-next 0/7] bpf, x86: Add bpf_get_func_ip helper Jiri Olsa
2021-07-07 21:47 ` [PATCHv3 bpf-next 1/7] bpf, x86: Store caller's ip in trampoline stack Jiri Olsa
2021-07-07 21:47 ` [PATCHv3 bpf-next 2/7] bpf: Enable BPF_TRAMP_F_IP_ARG for trampolines with call_get_func_ip Jiri Olsa
2021-07-07 21:47 ` [PATCHv3 bpf-next 3/7] bpf: Add bpf_get_func_ip helper for tracing programs Jiri Olsa
2021-07-08  0:06   ` Andrii Nakryiko
2021-07-11 14:48     ` Jiri Olsa
2021-07-08  2:11   ` Alexei Starovoitov
2021-07-11 14:48     ` Jiri Olsa
2021-07-07 21:47 ` [PATCHv3 bpf-next 4/7] bpf: Add bpf_get_func_ip helper for kprobe programs Jiri Olsa
2021-07-10  7:55   ` Masami Hiramatsu [this message]
2021-07-11 14:48     ` Jiri Olsa
2021-07-07 21:47 ` [PATCHv3 bpf-next 5/7] selftests/bpf: Add test for bpf_get_func_ip helper Jiri Olsa
2021-07-08  0:12   ` Andrii Nakryiko
2021-07-11 14:48     ` Jiri Olsa
2021-07-07 21:47 ` [PATCHv3 bpf-next 6/7] libbpf: allow specification of "kprobe/function+offset" Jiri Olsa
2021-07-08  0:14   ` Andrii Nakryiko
2021-07-11 14:48     ` Jiri Olsa
2021-07-07 21:47 ` [PATCHv3 bpf-next 7/7] selftests/bpf: Add test for bpf_get_func_ip in kprobe+offset probe Jiri Olsa
2021-07-08  0:18   ` Andrii Nakryiko
2021-07-11 14:48     ` Jiri Olsa
2021-07-12 23:32       ` Andrii Nakryiko
2021-07-13 14:15         ` Jiri Olsa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210710165512.8b0ffc67a894fef9a883eef2@kernel.org \
    --to=mhiramat@kernel.org \
    --cc=alan.maguire@oracle.com \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@redhat.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@chromium.org \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).