mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@meta.com>
To: menglong8.dong@gmail.com, daniel@iogearbox.net
Cc: ast@kernel.org, andrii@kernel.org, martin.lau@linux.dev,
	song@kernel.org, yhs@fb.com, john.fastabend@gmail.com,
	kpsingh@kernel.org, sdf@google.com, haoluo@google.com,
	jolsa@kernel.org, bpf@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Menglong Dong <imagedong@tencent.com>
Subject: Re: [PATCH] libbpf: resolve kernel function name optimization for kprobe
Date: Mon, 9 Jan 2023 12:29:12 -0800	[thread overview]
Message-ID: <504cc35a-74a8-751a-5899-186d7a0aff87@meta.com> (raw)
In-Reply-To: <20230109094247.1464856-1-imagedong@tencent.com>



On 1/9/23 1:42 AM, menglong8.dong@gmail.com wrote:
> From: Menglong Dong <imagedong@tencent.com>
> 
> The function name in kernel may be changed by the compiler. For example,
> the function 'ip_rcv_core' can be compiled to 'ip_rcv_core.isra.0'.
> 
> This kind optimization can happen in any kernel function. Therefor, we
> should conside this case.
> 
> If we failed to attach kprobe with a '-ENOENT', then we can lookup the
> kallsyms and check if there is a similar function end with '.xxx', and
> retry.

This might produce incorrect result, so this approach won't work
for all .isra.0 cases. When a function name is changed from
<func> to <func>.isra.<num>, it is possible that compiler may have
make some changes to the arguments, e.g., removing one argument,
chaning a semantics of argument, etc. if bpf program still
uses the original function signature, the bpf program may
produce unexpected result.

> 
> Signed-off-by: Menglong Dong <imagedong@tencent.com>
> ---
>   tools/lib/bpf/libbpf.c | 37 ++++++++++++++++++++++++++++++++++++-
>   1 file changed, 36 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index a5c67a3c93c5..fdfb1ca34ced 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -10375,12 +10375,30 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
>   	return libbpf_err_ptr(err);
>   }
>   
> +struct kprobe_resolve {
> +	char pattern[128];
> +	char name[128];
> +};
> +
> +static int kprobe_kallsyms_cb(unsigned long long sym_addr, char sym_type,
> +			      const char *sym_name, void *ctx)
> +{
> +	struct kprobe_resolve *res = ctx;
> +
> +	if (!glob_match(sym_name, res->pattern))
> +		return 0;
> +	strcpy(res->name, sym_name);
> +	return 1;
> +}
> +
>   static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link)
>   {
>   	DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts);
> +	struct kprobe_resolve res = {};
>   	unsigned long offset = 0;
>   	const char *func_name;
>   	char *func;
> +	int err;
>   	int n;
>   
>   	*link = NULL;
> @@ -10408,8 +10426,25 @@ static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf
>   
>   	opts.offset = offset;
>   	*link = bpf_program__attach_kprobe_opts(prog, func, &opts);
> +	err = libbpf_get_error(*link);
> +
> +	if (!err || err != -ENOENT)
> +		goto out;
> +
> +	sprintf(res.pattern, "%s.*", func);
> +	if (!libbpf_kallsyms_parse(kprobe_kallsyms_cb, &res))
> +		goto out;
> +
> +	pr_warn("prog '%s': trying to create %s '%s+0x%zx' perf event instead\n",
> +		prog->name, opts.retprobe ? "kretprobe" : "kprobe",
> +		res.name, offset);
> +
> +	*link = bpf_program__attach_kprobe_opts(prog, res.name, &opts);
> +	err = libbpf_get_error(*link);
> +
> +out:
>   	free(func);
> -	return libbpf_get_error(*link);
> +	return err;
>   }
>   
>   static int attach_ksyscall(const struct bpf_program *prog, long cookie, struct bpf_link **link)

  reply	other threads:[~2023-01-09 20:30 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-09  9:42 menglong8.dong
2023-01-09 20:29 ` Yonghong Song [this message]
2023-01-10  3:11   ` Menglong Dong
2023-01-12  1:11     ` Andrii Nakryiko
2023-01-12  7:23     ` Yonghong Song
2023-01-12 10:19       ` Alan Maguire
2023-01-12 21:07         ` Alexei Starovoitov
2023-01-13  8:00           ` Yonghong Song
2023-01-18 18:15             ` Yonghong Song
2023-01-20 13:20               ` BTF, pahole and static functions (was Re: [PATCH] libbpf: resolve kernel function name optimization for kprobe) Alan Maguire
2023-01-21  5:29                 ` Yonghong Song
2023-01-23 17:14                   ` Alan Maguire
2023-01-24  7:18                     ` Yonghong Song
2023-01-24 12:25                       ` Alexei Starovoitov
2023-01-13  2:53         ` [PATCH] libbpf: resolve kernel function name optimization for kprobe Menglong Dong

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=504cc35a-74a8-751a-5899-186d7a0aff87@meta.com \
    --to=yhs@meta.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=imagedong@tencent.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=menglong8.dong@gmail.com \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    --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

all inboxes | Powered by JetHome®